Skip to content

Commit

Permalink
Change web based CAN log file format to CANdump (#655)
Browse files Browse the repository at this point in the history
* Update web page CAN logging to CANdump format
* Improve performance of web based CAN logging by removing unnecessary strcat and strlen calls.
  • Loading branch information
mvgalen authored Dec 4, 2024
1 parent 025a9fe commit 3d1a3eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
46 changes: 20 additions & 26 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -642,44 +642,38 @@ void print_can_frame(CAN_frame frame, frameDirection msgDir) {
#endif //#DEBUG_CAN_DATA

if (datalayer.system.info.can_logging_active) { // If user clicked on CAN Logging page in webserver, start recording
char* message_string = datalayer.system.info.logged_can_messages;
int offset = datalayer.system.info.logged_can_messages_offset; // Keeps track of the current position in the buffer
size_t message_string_size = sizeof(datalayer.system.info.logged_can_messages);

char message_string[128]; // Buffer to hold the message string
int offset = 0; // Keeps track of the current position in the buffer

if (offset + 128 > sizeof(datalayer.system.info.logged_can_messages)) {
// Not enough space, reset and start from the beginning
offset = 0;
}
unsigned long currentTime = millis();
// Add timestamp
offset += snprintf(message_string + offset, sizeof(message_string) - offset, "%lu ", millis());
offset += snprintf(message_string + offset, message_string_size - offset, "(%lu.%03lu) ", currentTime / 1000,
currentTime % 1000);

// Add direction
// Add direction. The 0 and 1 after RX and TX ensures that SavvyCAN puts TX and RX in a different bus.
offset +=
snprintf(message_string + offset, sizeof(message_string) - offset, "%s ", (msgDir == MSG_RX) ? "RX" : "TX");
snprintf(message_string + offset, message_string_size - offset, "%s ", (msgDir == MSG_RX) ? "RX0" : "TX1");

// Add ID and DLC
offset += snprintf(message_string + offset, sizeof(message_string) - offset, "%X %u ", frame.ID, frame.DLC);
offset += snprintf(message_string + offset, message_string_size - offset, "%X [%u] ", frame.ID, frame.DLC);

// Add data bytes
for (uint8_t i = 0; i < frame.DLC; i++) {
offset += snprintf(message_string + offset, sizeof(message_string) - offset, "%s%X ",
frame.data.u8[i] < 16 ? "0" : "", frame.data.u8[i]);
if (i < frame.DLC - 1) {
offset += snprintf(message_string + offset, message_string_size - offset, "%02X ", frame.data.u8[i]);
} else {
offset += snprintf(message_string + offset, message_string_size - offset, "%02X", frame.data.u8[i]);
}
}
// Add linebreak
offset += snprintf(message_string + offset, sizeof(message_string) - offset, "\n");

// Ensure the string is null-terminated
message_string[sizeof(message_string) - 1] = '\0';

// Append the message string to the system info structure
size_t current_len =
strnlen(datalayer.system.info.logged_can_messages, sizeof(datalayer.system.info.logged_can_messages));
size_t available_space =
sizeof(datalayer.system.info.logged_can_messages) - current_len - 1; // Space left for new data

if (available_space < strlen(message_string) + 1) {
// Not enough space, reset and start from the beginning
current_len = 0;
datalayer.system.info.logged_can_messages[0] = '\0';
}
offset += snprintf(message_string + offset, message_string_size - offset, "\n");

strncat(datalayer.system.info.logged_can_messages, message_string, available_space);
datalayer.system.info.logged_can_messages_offset = offset; // Update offset in buffer
}
}

Expand Down
1 change: 1 addition & 0 deletions Software/src/datalayer/datalayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ typedef struct {
char inverter_protocol[64] = {0};
/** array with incoming CAN messages, for displaying on webserver */
char logged_can_messages[15000] = {0};
size_t logged_can_messages_offset = 0;
/** bool, determines if CAN messages should be logged for webserver */
bool can_logging_active = false;

Expand Down

0 comments on commit 3d1a3eb

Please sign in to comment.