code format

This commit is contained in:
2022-12-14 14:13:52 +03:00
parent 430a41fefc
commit c2b8a8d6da
297 changed files with 27331 additions and 24162 deletions

View File

@@ -1,37 +1,38 @@
/*
PIP - Platform Independent Primitives
CAN
Andrey Bychkov work.a.b@yandex.ru
PIP - Platform Independent Primitives
CAN
Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pican.h"
#include "pipropertystorage.h"
#include "piwaitevent_p.h"
#if !defined(WINDOWS) && !defined(MAC_OS) && !defined(MICRO_PIP)
# define PIP_CAN
#endif
#ifdef PIP_CAN
# include <sys/ioctl.h>
# include <net/if.h>
# include <linux/can.h>
# include <linux/can/raw.h>
# include <net/if.h>
# include <sys/ioctl.h>
# ifndef AF_CAN
# define AF_CAN 29
# define AF_CAN 29
# endif
# ifndef PF_CAN
# define PF_CAN AF_CAN
# define PF_CAN AF_CAN
# endif
#endif
@@ -44,11 +45,11 @@ PRIVATE_DEFINITION_START(PICAN)
PRIVATE_DEFINITION_END(PICAN)
PICAN::PICAN(const PIString & path, PIIODevice::DeviceMode mode) : PIIODevice(path, mode) {
PICAN::PICAN(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode) {
setThreadedReadBufferSize(256);
setPath(path);
can_id = 0;
sock = 0;
sock = 0;
PRIVATE->event.create();
}
@@ -64,27 +65,27 @@ bool PICAN::openDevice() {
#ifdef PIP_CAN
piCout << "PICAN open device" << path();
sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if(sock < 0){
if (sock < 0) {
piCoutObj << "Error! while opening socket";
return false;
}
ifreq ifr;
strcpy(ifr.ifr_name, path().dataAscii());
piCout << "PICAN try to get interface index...";
if(ioctl(sock, SIOCGIFINDEX, &ifr) < 0){
if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
piCoutObj << "Error! while determin the interface ioctl";
return false;
}
struct timeval tv;
tv.tv_sec = 1;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv);
// bind socket to all CAN interface
sockaddr_can addr;
addr.can_family = AF_CAN;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
piCout << "PICAN try to bind socket to interface" << ifr.ifr_ifindex;
if(bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0){
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
piCoutObj << "Error! while binding socket";
return false;
}
@@ -108,13 +109,14 @@ bool PICAN::closeDevice() {
ssize_t PICAN::readDevice(void * read_to, ssize_t max_size) {
#ifdef PIP_CAN
//piCout << "PICAN read";
// piCout << "PICAN read";
can_frame frame;
ssize_t ret = 0;
if (PRIVATE->event.wait(sock))
ret = ::read(sock, &frame, sizeof(can_frame));
if (ret < 0) {/*piCoutObj << "Error while read CAN frame " << ret;*/ return -1;}
//piCoutObj << "receive CAN frame Id =" << frame.can_id;
if (PRIVATE->event.wait(sock)) ret = ::read(sock, &frame, sizeof(can_frame));
if (ret < 0) { /*piCoutObj << "Error while read CAN frame " << ret;*/
return -1;
}
// piCoutObj << "receive CAN frame Id =" << frame.can_id;
memcpy(read_to, frame.data, piMini(frame.can_dlc, max_size));
readed_id = frame.can_id;
return piMini(frame.can_dlc, max_size);
@@ -125,15 +127,21 @@ ssize_t PICAN::readDevice(void * read_to, ssize_t max_size) {
ssize_t PICAN::writeDevice(const void * data, ssize_t max_size) {
#ifdef PIP_CAN
//piCout << "PICAN write" << can_id << max_size;
if (max_size > 8) {piCoutObj << "Can't send CAN frame bigger than 8 bytes (requested " << max_size << ")!"; return -1;}
// piCout << "PICAN write" << can_id << max_size;
if (max_size > 8) {
piCoutObj << "Can't send CAN frame bigger than 8 bytes (requested " << max_size << ")!";
return -1;
}
can_frame frame;
frame.can_id = can_id;
frame.can_id = can_id;
frame.can_dlc = max_size;
memcpy(frame.data, data, max_size);
ssize_t ret = 0;
ret = ::write(sock, &frame, sizeof(can_frame));
if (ret < 0) {piCoutObj << "Error while send CAN frame " << ret; return -1;}
ret = ::write(sock, &frame, sizeof(can_frame));
if (ret < 0) {
piCoutObj << "Error while send CAN frame " << ret;
return -1;
}
return max_size;
#endif
return 0;
@@ -183,7 +191,7 @@ void PICAN::configureFromFullPathDevice(const PIString & full_path) {
PIPropertyStorage PICAN::constructVariantDevice() const {
PIPropertyStorage ret;
ret.addProperty("path", path());
ret.addProperty("CAN ID", PIString::fromNumber(CANID(),16));
ret.addProperty("CAN ID", PIString::fromNumber(CANID(), 16));
return ret;
}