diff --git a/lib/src/can/socket_ffi.dart b/lib/src/can/socket_ffi.dart index 2eed04a..3a52070 100644 --- a/lib/src/can/socket_ffi.dart +++ b/lib/src/can/socket_ffi.dart @@ -57,7 +57,7 @@ class CanFFI implements CanSocket { final error = getCanError(nativeLib.BurtCan_open(_can)); if (error != null) throw CanException(error); _startListening(); - + logger.info("Listening on CAN interface $canInterface"); } @override @@ -79,7 +79,8 @@ class CanFFI implements CanSocket { int count = 0; while (true) { final pointer = nativeLib.NativeCanMessage_create(); - nativeLib.BurtCan_receive(_can, pointer); + final error = getCanError(nativeLib.BurtCan_receive(_can, pointer)); + if (error != null) throw CanException(error); if (pointer.ref.length == 0) return; count++; if (count == 10) logger.warning("Processed over 10 CAN messages in one callback. Consider decreasing the CAN read interval."); diff --git a/lib/subsystems.dart b/lib/subsystems.dart index 20af0fa..584fbc2 100644 --- a/lib/subsystems.dart +++ b/lib/subsystems.dart @@ -15,6 +15,7 @@ class SubsystemsCollection { /// Initializes all the resources needed by the subsystems. Future init() async { + logger.debug("Running in debug mode..."); can.init(); await server.init(); logger.info("Subsystems initialized"); diff --git a/src/burt_can/burt_can.cpp b/src/burt_can/burt_can.cpp index 0e1f68a..c727be8 100755 --- a/src/burt_can/burt_can.cpp +++ b/src/burt_can/burt_can.cpp @@ -16,6 +16,10 @@ #include "burt_can.hpp" +void printError() { + std::cout << "Error from C code: " << strerror(errno) << std::endl; +} + burt_can::BurtCan::BurtCan(const char* interface, int32_t readTimeout, BurtCanType mode) : interface(interface), readTimeout(readTimeout), @@ -29,6 +33,7 @@ BurtCanStatus burt_can::BurtCan::open() { // Open the socket handle = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (handle < 0) { + printError(); return BurtCanStatus::SOCKET_CREATE_ERROR; } @@ -36,6 +41,7 @@ BurtCanStatus burt_can::BurtCan::open() { strcpy(ifr.ifr_name, interface); ioctl(handle, SIOCGIFINDEX, &ifr); if (!ifr.ifr_ifindex) { + printError(); return BurtCanStatus::INTERFACE_PARSE_ERROR; } @@ -44,10 +50,13 @@ BurtCanStatus burt_can::BurtCan::open() { int enableFD = 1; if (ioctl(handle, SIOCGIFMTU, &ifr) < 0) { + printError(); return BurtCanStatus::MTU_ERROR; } else if (mtu != CANFD_MTU) { + printError(); return BurtCanStatus::CANFD_NOT_SUPPORTED; } else if (setsockopt(handle, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enableFD, sizeof(enableFD))) { + printError(); return BurtCanStatus::FD_MISC_ERROR; } } @@ -62,6 +71,7 @@ BurtCanStatus burt_can::BurtCan::open() { // Bind the socket to the address if (bind(handle, (struct sockaddr*) &address, sizeof(address)) < 0) { + printError(); return BurtCanStatus::BIND_ERROR; } return BurtCanStatus::OK; @@ -77,25 +87,30 @@ BurtCanStatus burt_can::BurtCan::send(const NativeCanMessage* frame) { if (write(handle, &raw, size) == size) { return BurtCanStatus::OK; } else { + printError(); return BurtCanStatus::WRITE_ERROR; } } BurtCanStatus burt_can::BurtCan::receive(NativeCanMessage* frame) { can_frame raw; - int size = sizeof(raw); - if (read(handle, &raw, size) == size) { + long bytesRead = read(handle, &raw, sizeof(raw)); + if (bytesRead < 0) { + printError(); + return BurtCanStatus::READ_ERROR; + } else if (bytesRead < (long) sizeof(raw)) { + return BurtCanStatus::FRAME_NOT_FULLY_READ; + } else { frame->id = raw.can_id; frame->length = raw.len; std::memcpy(frame->data, raw.data, 8); return BurtCanStatus::OK; - } else { - return BurtCanStatus::READ_ERROR; } } BurtCanStatus burt_can::BurtCan::dispose() { if (close(handle) < 0) { + printError(); return BurtCanStatus::CLOSE_ERROR; } else { return BurtCanStatus::OK; diff --git a/src/burt_can/burt_can.h b/src/burt_can/burt_can.h index 055f9da..8c5efc5 100644 --- a/src/burt_can/burt_can.h +++ b/src/burt_can/burt_can.h @@ -24,6 +24,7 @@ typedef enum BurtCanStatus { // IO errors WRITE_ERROR = 9, READ_ERROR = 10, + FRAME_NOT_FULLY_READ = 11, } BurtCanStatus; typedef struct NativeCanMessage {