Skip to content

Commit

Permalink
Added more error handling to burt_can
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing-Rover committed Sep 27, 2023
1 parent 6f67b94 commit 9b63f06
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/src/can/socket_ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.");
Expand Down
1 change: 1 addition & 0 deletions lib/subsystems.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SubsystemsCollection {

/// Initializes all the resources needed by the subsystems.
Future<void> init() async {
logger.debug("Running in debug mode...");
can.init();
await server.init();
logger.info("Subsystems initialized");
Expand Down
23 changes: 19 additions & 4 deletions src/burt_can/burt_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -29,13 +33,15 @@ 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;
}

// Define the interface we'll use on the socket
strcpy(ifr.ifr_name, interface);
ioctl(handle, SIOCGIFINDEX, &ifr);
if (!ifr.ifr_ifindex) {
printError();
return BurtCanStatus::INTERFACE_PARSE_ERROR;
}

Expand All @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/burt_can/burt_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef enum BurtCanStatus {
// IO errors
WRITE_ERROR = 9,
READ_ERROR = 10,
FRAME_NOT_FULLY_READ = 11,
} BurtCanStatus;

typedef struct NativeCanMessage {
Expand Down

0 comments on commit 9b63f06

Please sign in to comment.