From 2456e528b7c46abb72ad686b8184e6d71f13e282 Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Mon, 7 Oct 2024 00:14:58 +0900 Subject: [PATCH 01/10] support canfd --- src/socketcandcl.c | 86 +++++++++++++++++++++++++----- src/state_bcm.c | 64 +++++++++++----------- src/state_raw.c | 130 +++++++++++++++++++++++++++++++++++++++------ 3 files changed, 219 insertions(+), 61 deletions(-) diff --git a/src/socketcandcl.c b/src/socketcandcl.c index 50716eb..d049956 100644 --- a/src/socketcandcl.c +++ b/src/socketcandcl.c @@ -34,6 +34,7 @@ #include #include +#include #define MAXLEN 4000 #define PORT 29536 @@ -266,7 +267,7 @@ int main(int argc, char **argv) inline void state_connected() { int ret; - static struct can_frame frame; + static struct canfd_frame frame; static struct ifreq ifr; static struct sockaddr_can addr; fd_set readfds; @@ -296,6 +297,25 @@ inline void state_connected() state = STATE_SHUTDOWN; return; } + + if(ioctl(raw_socket,SIOCGIFMTU,&ifr) < 0) { + PRINT_ERROR("Error while searching for bus MTU %s\n", strerror(errno)); + state = STATE_SHUTDOWN; + return; + } + + if (ifr.ifr_mtu == CANFD_MTU) { + const int canfd_on = 1; + if(setsockopt(raw_socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0) { + PRINT_ERROR("Could not enable CAN FD support\n"); + state = STATE_SHUTDOWN; + return; + } + } + +// fprintf(stderr, "MTU of %s is %d\n", ldev, ifr.ifr_mtu); + + /* bind socket */ if (bind(raw_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) { PRINT_ERROR("Error while binding RAW socket %s\n", strerror(errno)); @@ -342,7 +362,9 @@ inline void state_connected() if ((s - buf - 7) > 4) frame.can_id |= CAN_EFF_FLAG; - frame.can_dlc = strlen(data_str) / 2; + frame.len = strlen(data_str) / 2; + +// fprintf(stderr, "frame.can_id: %d frame.flags: %d frame.len: %d\n", frame.can_id, frame.flags, frame.len); sscanf(data_str, "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", &frame.data[0], &frame.data[1], @@ -351,7 +373,34 @@ inline void state_connected() &frame.data[6], &frame.data[7]); ret = write(raw_socket, &frame, sizeof(struct can_frame)); - if (ret < sizeof(struct can_frame)) { + if(ret != sizeof(struct can_frame)) { + perror("Writing CAN frame to can socket\n"); + } + } else if(!strncmp("< fdframe", buf, 9)) { + char data_str[2*64]; + + sscanf(buf, "< fdframe %x %hhx %*d.%*d %s >", &frame.can_id, &frame.flags, + data_str); + + char* s = buf + 9; + for(; ++s;) { + if(*s== ' ') { + break; + } + } + if((s - buf - 9) > 4) + frame.can_id |= CAN_EFF_FLAG; + + frame.len = strlen(data_str) / 2; + + sscanf(data_str, "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", + &frame.data[0], &frame.data[1], + &frame.data[2], &frame.data[3], + &frame.data[4], &frame.data[5], + &frame.data[6], &frame.data[7]); + + ret = write(raw_socket, &frame, sizeof(struct canfd_frame)); + if(ret != sizeof(struct canfd_frame)) { perror("Writing CAN frame to can socket\n"); } } @@ -388,17 +437,30 @@ inline void state_connected() /* TODO implement */ } else { int i; - if (frame.can_id & CAN_EFF_FLAG) { - ret = sprintf(buf, "< send %08X %d ", - frame.can_id & CAN_EFF_MASK, frame.can_dlc); - } else { - ret = sprintf(buf, "< send %03X %d ", - frame.can_id & CAN_SFF_MASK, frame.can_dlc); + +// fprintf(stderr, "frame.can_id: %d frame.flags: %d frame.len: %d\n", frame.can_id, frame.flags, frame.len); + + if(ret == sizeof(struct can_frame)) { + if(frame.can_id & CAN_EFF_FLAG) { + ret = sprintf(buf, "< send %08X %d ", + frame.can_id & CAN_EFF_MASK, frame.len); + } else { + ret = sprintf(buf, "< send %03X %d ", + frame.can_id & CAN_SFF_MASK, frame.len); + } + } else if(ret == sizeof(struct canfd_frame)) { + if(frame.can_id & CAN_EFF_FLAG) { + ret = sprintf(buf, "< fdsend %08X %02X %d ", + frame.can_id & CAN_EFF_MASK, frame.flags, frame.len); + } else { + ret = sprintf(buf, "< fdsend %03X %02X %d ", + frame.can_id & CAN_SFF_MASK, frame.flags, frame.len); + } } - for (i = 0; i < frame.can_dlc; i++) { - ret += sprintf(buf + ret, "%02x ", frame.data[i]); + for(i=0; i"); + sprintf(buf+ret, ">"); const size_t len = strlen(buf); ret = send(server_socket, buf, len, 0); diff --git a/src/state_bcm.c b/src/state_bcm.c index 993e66f..d1c9a84 100644 --- a/src/state_bcm.c +++ b/src/state_bcm.c @@ -41,12 +41,12 @@ void state_bcm() struct { struct bcm_msg_head msg_head; - struct can_frame frame; + struct canfd_frame frame; } msg; struct { struct bcm_msg_head msg_head; - struct can_frame frame[257]; /* MAX_NFRAMES + MUX MASK */ + struct canfd_frame frame[257]; /* MAX_NFRAMES + MUX MASK */ } muxmsg; if (previous_state != STATE_BCM) { @@ -102,13 +102,13 @@ void state_bcm() } /* Check if this is an error frame */ - if (msg.msg_head.can_id & CAN_ERR_FLAG) { - if (msg.frame.can_dlc != CAN_ERR_DLC) { - PRINT_ERROR("Error frame has a wrong DLC!\n"); - } else { + if(msg.msg_head.can_id & CAN_ERR_FLAG) { + if(msg.frame.len != CAN_ERR_DLC) { + PRINT_ERROR("Error frame has a wrong DLC!\n") + } else { snprintf(rxmsg, RXLEN, "< error %03X %ld.%06ld ", msg.msg_head.can_id, tv.tv_sec, tv.tv_usec); - for (i = 0; i < msg.frame.can_dlc; i++) + for ( i = 0; i < msg.frame.len; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -125,7 +125,7 @@ void state_bcm() msg.msg_head.can_id & CAN_SFF_MASK, tv.tv_sec, tv.tv_usec); } - for (i = 0; i < msg.frame.can_dlc; i++) + for ( i = 0; i < msg.frame.len; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -172,7 +172,7 @@ void state_bcm() "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -182,11 +182,11 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if ((items < 2) || - (msg.frame.can_dlc > 8) || - (items != 2 + msg.frame.can_dlc)) { - PRINT_ERROR("Syntax error in send command\n"); - return; + if ( (items < 2) || + (msg.frame.len > 64) || + (items != 2 + msg.frame.len)) { + PRINT_ERROR("Syntax error in send command\n") + return; } /* < send XXXXXXXX ... > check for extended identifier */ @@ -209,7 +209,7 @@ void state_bcm() &msg.msg_head.ival2.tv_sec, &msg.msg_head.ival2.tv_usec, &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -219,9 +219,9 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if ((items < 4) || - (msg.frame.can_dlc > 8) || - (items != 4 + msg.frame.can_dlc)) { + if( (items < 4) || + (msg.frame.len > 64) || + (items != 4 + msg.frame.len) ) { PRINT_ERROR("Syntax error in add command.\n"); return; } @@ -245,7 +245,7 @@ void state_bcm() "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -255,11 +255,11 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if ((items < 2) || - (msg.frame.can_dlc > 8) || - (items != 2 + msg.frame.can_dlc)) { - PRINT_ERROR("Syntax error in update send job command\n"); - return; + if ( (items < 2) || + (msg.frame.len > 64) || + (items != 2 + msg.frame.len)) { + PRINT_ERROR("Syntax error in update send job command\n") + return; } /* < update XXXXXXXX ... > check for extended identifier */ @@ -305,7 +305,7 @@ void state_bcm() &msg.msg_head.ival2.tv_sec, &msg.msg_head.ival2.tv_usec, &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -315,11 +315,11 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if ((items < 4) || - (msg.frame.can_dlc > 8) || - (items != 4 + msg.frame.can_dlc)) { - PRINT_ERROR("syntax error in filter command.\n"); - return; + if( (items < 4) || + (msg.frame.len > 64) || + (items != 4 + msg.frame.len) ) { + PRINT_ERROR("syntax error in filter command.\n") + return; } /* < filter sec usec XXXXXXXX ... > check for extended identifier */ @@ -399,8 +399,8 @@ void state_bcm() if (!ioctl(sc, SIOCGIFINDEX, &ifr)) { caddr.can_ifindex = ifr.ifr_ifindex; sendto(sc, &muxmsg, sizeof(struct bcm_msg_head) + - sizeof(struct can_frame) * muxmsg.msg_head.nframes, - 0, (struct sockaddr *)&caddr, sizeof(caddr)); + sizeof(struct canfd_frame) * muxmsg.msg_head.nframes, + 0, (struct sockaddr*)&caddr, sizeof(caddr)); } /* Add a filter */ } else if (!strncmp("< subscribe ", buf, 12)) { diff --git a/src/state_raw.c b/src/state_raw.c index 8246f1f..a7733e1 100644 --- a/src/state_raw.c +++ b/src/state_raw.c @@ -21,12 +21,13 @@ #include #include +#include int raw_socket; struct ifreq ifr; struct sockaddr_can addr; struct msghdr msg; -struct can_frame frame; +struct canfd_frame frame; struct iovec iov; char ctrlmsg[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(__u32))]; struct timeval tv; @@ -62,7 +63,22 @@ void state_raw() return; } - if (bind(raw_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + if(ioctl(raw_socket,SIOCGIFMTU,&ifr) < 0) { + PRINT_ERROR("Error while searching for bus MTU %s\n", strerror(errno)); + state = STATE_SHUTDOWN; + return; + } + + if (ifr.ifr_mtu == CANFD_MTU) { + const int canfd_on = 1; + if(setsockopt(raw_socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0) { + PRINT_ERROR("Could not enable CAN FD support\n"); + state = STATE_SHUTDOWN; + return; + } + } + + if(bind(raw_socket, (struct sockaddr *) &addr, sizeof(addr)) < 0) { PRINT_ERROR("Error while binding RAW socket %s\n", strerror(errno)); state = STATE_SHUTDOWN; return; @@ -104,9 +120,9 @@ void state_raw() msg.msg_controllen = sizeof(ctrlmsg); ret = recvmsg(raw_socket, &msg, 0); - if (ret < sizeof(struct can_frame)) { - PRINT_ERROR("Error reading frame from RAW socket\n"); - } else { + if(ret != sizeof(struct canfd_frame) && ret != sizeof(struct can_frame)) { + PRINT_ERROR("Error reading frame from RAW socket\n") + } else { /* read timestamp data */ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg && (cmsg->cmsg_level == SOL_SOCKET); @@ -124,13 +140,24 @@ void state_raw() } else if (frame.can_id & CAN_RTR_FLAG) { /* TODO implement */ } else { - if (frame.can_id & CAN_EFF_FLAG) { - ret = sprintf(buf, "< frame %08X %ld.%06ld ", frame.can_id & CAN_EFF_MASK, tv.tv_sec, tv.tv_usec); - } else { - ret = sprintf(buf, "< frame %03X %ld.%06ld ", frame.can_id & CAN_SFF_MASK, tv.tv_sec, tv.tv_usec); + switch(ret) { + case sizeof(struct can_frame): + if(frame.can_id & CAN_EFF_FLAG) { + ret = sprintf(buf, "< frame %08X %ld.%06ld ", frame.can_id & CAN_EFF_MASK, tv.tv_sec, tv.tv_usec); + } else { + ret = sprintf(buf, "< frame %03X %ld.%06ld ", frame.can_id & CAN_SFF_MASK, tv.tv_sec, tv.tv_usec); + } + break; + case sizeof(struct canfd_frame): + if(frame.can_id & CAN_EFF_FLAG) { + ret = sprintf(buf, "< fdframe %08X %02X %ld.%06ld ", frame.can_id & CAN_EFF_MASK, frame.flags, tv.tv_sec, tv.tv_usec); + } else { + ret = sprintf(buf, "< fdframe %03X %02X %ld.%06ld ", frame.can_id & CAN_SFF_MASK, frame.flags, tv.tv_sec, tv.tv_usec); + } } - for (i = 0; i < frame.can_dlc; i++) { - ret += sprintf(buf + ret, "%02X", frame.data[i]); + + for(i=0;i"); send(client_socket, buf, strlen(buf), 0); @@ -163,7 +190,7 @@ void state_raw() "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &frame.can_id, - &frame.can_dlc, + &frame.len, &frame.data[0], &frame.data[1], &frame.data[2], @@ -173,11 +200,13 @@ void state_raw() &frame.data[6], &frame.data[7]); - if ((items < 2) || - (frame.can_dlc > 8) || - (items != 2 + frame.can_dlc)) { - PRINT_ERROR("Syntax error in send command\n"); - return; +// fprintf(stderr, "%s\n", buf); + + if ( (items < 2) || + (frame.len > 8) || + (items != 2 + frame.len)) { + PRINT_ERROR("Syntax error in send command\n") + return; } /* < send XXXXXXXX ... > check for extended identifier */ @@ -189,6 +218,73 @@ void state_raw() state = STATE_SHUTDOWN; return; } + } else if(!strncmp("< fdsend ", buf, 9)) { + // First, read the fixed part of the frame + items = sscanf(buf, "< %*s %x %hhx %hhu", + &frame.can_id, + &frame.flags, + &frame.len); + + if (items != 3) { + PRINT_ERROR("Syntax error in fdsend command\n"); + return; + } + + // Ensure frame.len does not exceed the maximum allowed length + if (frame.len > 64) { + PRINT_ERROR("Frame length exceeds maximum allowed length\n"); + return; + } + + // Construct the format string dynamically based on frame.len + char format[256]; + snprintf(format, sizeof(format), "< %%*s %%x %%hhx %%hhu"); + for (int i = 0; i < frame.len; i++) { + strncat(format, " %hhx", sizeof(format) - strlen(format) - 1); + } + strncat(format, " >", sizeof(format) - strlen(format) - 1); + + // Read the variable-length frame.data + items = sscanf(buf, format, + &frame.can_id, + &frame.flags, + &frame.len, + &frame.data[0], &frame.data[1], &frame.data[2], &frame.data[3], + &frame.data[4], &frame.data[5], &frame.data[6], &frame.data[7], + &frame.data[8], &frame.data[9], &frame.data[10], &frame.data[11], + &frame.data[12], &frame.data[13], &frame.data[14], &frame.data[15], + &frame.data[16], &frame.data[17], &frame.data[18], &frame.data[19], + &frame.data[20], &frame.data[21], &frame.data[22], &frame.data[23], + &frame.data[24], &frame.data[25], &frame.data[26], &frame.data[27], + &frame.data[28], &frame.data[29], &frame.data[30], &frame.data[31], + &frame.data[32], &frame.data[33], &frame.data[34], &frame.data[35], + &frame.data[36], &frame.data[37], &frame.data[38], &frame.data[39], + &frame.data[40], &frame.data[41], &frame.data[42], &frame.data[43], + &frame.data[44], &frame.data[45], &frame.data[46], &frame.data[47], + &frame.data[48], &frame.data[49], &frame.data[50], &frame.data[51], + &frame.data[52], &frame.data[53], &frame.data[54], &frame.data[55], + &frame.data[56], &frame.data[57], &frame.data[58], &frame.data[59], + &frame.data[60], &frame.data[61], &frame.data[62], &frame.data[63]); + +// fprintf(stderr, "items: %d frames: %d\n", items, frame.len); +// fprintf(stderr, "%s %s\n", buf, format); + + if ( (items < 2) || + (frame.len > 64) || + (items != 3 + frame.len)) { + PRINT_ERROR("Syntax error in send command\n") + return; + } + + /* < send XXXXXXXX ... > check for extended identifier */ + if(element_length(buf, 2) == 8) + frame.can_id |= CAN_EFF_FLAG; + + ret = send(raw_socket, &frame, sizeof(struct canfd_frame), 0); + if(ret==-1) { + state = STATE_SHUTDOWN; + return; + } } else { PRINT_ERROR("unknown command '%s'\n", buf); From 1ab74cc95246f8a16d27a9eaeb2cad70cf3bfc6a Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Sat, 12 Oct 2024 21:20:41 +0900 Subject: [PATCH 02/10] update comments --- src/state_raw.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/state_raw.c b/src/state_raw.c index a7733e1..c8383cb 100644 --- a/src/state_raw.c +++ b/src/state_raw.c @@ -69,6 +69,10 @@ void state_raw() return; } + /* + *if the device supports CAN FD, use it + * if you don't want to use CAN FD, you should initialize the device as classic CAN + */ if (ifr.ifr_mtu == CANFD_MTU) { const int canfd_on = 1; if(setsockopt(raw_socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0) { @@ -141,6 +145,7 @@ void state_raw() /* TODO implement */ } else { switch(ret) { + // if the frame is a classic CAN frame case sizeof(struct can_frame): if(frame.can_id & CAN_EFF_FLAG) { ret = sprintf(buf, "< frame %08X %ld.%06ld ", frame.can_id & CAN_EFF_MASK, tv.tv_sec, tv.tv_usec); @@ -148,6 +153,7 @@ void state_raw() ret = sprintf(buf, "< frame %03X %ld.%06ld ", frame.can_id & CAN_SFF_MASK, tv.tv_sec, tv.tv_usec); } break; + // if the frame is a CAN FD frame case sizeof(struct canfd_frame): if(frame.can_id & CAN_EFF_FLAG) { ret = sprintf(buf, "< fdframe %08X %02X %ld.%06ld ", frame.can_id & CAN_EFF_MASK, frame.flags, tv.tv_sec, tv.tv_usec); @@ -200,8 +206,6 @@ void state_raw() &frame.data[6], &frame.data[7]); -// fprintf(stderr, "%s\n", buf); - if ( (items < 2) || (frame.len > 8) || (items != 2 + frame.len)) { @@ -218,6 +222,7 @@ void state_raw() state = STATE_SHUTDOWN; return; } + /* Send a single CANFD frame */ } else if(!strncmp("< fdsend ", buf, 9)) { // First, read the fixed part of the frame items = sscanf(buf, "< %*s %x %hhx %hhu", @@ -266,9 +271,6 @@ void state_raw() &frame.data[56], &frame.data[57], &frame.data[58], &frame.data[59], &frame.data[60], &frame.data[61], &frame.data[62], &frame.data[63]); -// fprintf(stderr, "items: %d frames: %d\n", items, frame.len); -// fprintf(stderr, "%s %s\n", buf, format); - if ( (items < 2) || (frame.len > 64) || (items != 3 + frame.len)) { @@ -276,7 +278,7 @@ void state_raw() return; } - /* < send XXXXXXXX ... > check for extended identifier */ + /* < fdsend XXXXXXXX ... > check for extended identifier */ if(element_length(buf, 2) == 8) frame.can_id |= CAN_EFF_FLAG; From 47540785cc60c32c967ed45680e456ff6c26c8b2 Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Sun, 13 Oct 2024 06:03:49 +0900 Subject: [PATCH 03/10] update message --- src/state_raw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state_raw.c b/src/state_raw.c index c8383cb..cfa765a 100644 --- a/src/state_raw.c +++ b/src/state_raw.c @@ -274,7 +274,7 @@ void state_raw() if ( (items < 2) || (frame.len > 64) || (items != 3 + frame.len)) { - PRINT_ERROR("Syntax error in send command\n") + PRINT_ERROR("Syntax error in fdsend command\n") return; } From 01700ea2eca993ed8c4e195c3df0720808d5f80f Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Sun, 13 Oct 2024 06:08:10 +0900 Subject: [PATCH 04/10] support canfd for can raw --- src/state_bcm.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/state_bcm.c b/src/state_bcm.c index d1c9a84..aa2343f 100644 --- a/src/state_bcm.c +++ b/src/state_bcm.c @@ -41,12 +41,12 @@ void state_bcm() struct { struct bcm_msg_head msg_head; - struct canfd_frame frame; + struct can_frame frame; } msg; struct { struct bcm_msg_head msg_head; - struct canfd_frame frame[257]; /* MAX_NFRAMES + MUX MASK */ + struct can_frame frame[257]; /* MAX_NFRAMES + MUX MASK */ } muxmsg; if (previous_state != STATE_BCM) { @@ -103,12 +103,12 @@ void state_bcm() /* Check if this is an error frame */ if(msg.msg_head.can_id & CAN_ERR_FLAG) { - if(msg.frame.len != CAN_ERR_DLC) { + if(msg.frame.can_dlc != CAN_ERR_DLC) { PRINT_ERROR("Error frame has a wrong DLC!\n") } else { snprintf(rxmsg, RXLEN, "< error %03X %ld.%06ld ", msg.msg_head.can_id, tv.tv_sec, tv.tv_usec); - for ( i = 0; i < msg.frame.len; i++) + for ( i = 0; i < msg.frame.can_dlc; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -125,7 +125,7 @@ void state_bcm() msg.msg_head.can_id & CAN_SFF_MASK, tv.tv_sec, tv.tv_usec); } - for ( i = 0; i < msg.frame.len; i++) + for ( i = 0; i < msg.frame.can_dlc; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -172,7 +172,7 @@ void state_bcm() "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &msg.msg_head.can_id, - &msg.frame.len, + &msg.frame.can_dlc, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -183,8 +183,8 @@ void state_bcm() &msg.frame.data[7]); if ( (items < 2) || - (msg.frame.len > 64) || - (items != 2 + msg.frame.len)) { + (msg.frame.can_dlc > 8) || + (items != 2 + msg.frame.can_dlc)) { PRINT_ERROR("Syntax error in send command\n") return; } @@ -209,7 +209,7 @@ void state_bcm() &msg.msg_head.ival2.tv_sec, &msg.msg_head.ival2.tv_usec, &msg.msg_head.can_id, - &msg.frame.len, + &msg.frame.can_dlc, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -220,8 +220,8 @@ void state_bcm() &msg.frame.data[7]); if( (items < 4) || - (msg.frame.len > 64) || - (items != 4 + msg.frame.len) ) { + (msg.frame.can_dlc > 8) || + (items != 4 + msg.frame.can_dlc) ) { PRINT_ERROR("Syntax error in add command.\n"); return; } @@ -245,7 +245,7 @@ void state_bcm() "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &msg.msg_head.can_id, - &msg.frame.len, + &msg.frame.can_dlc, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -256,8 +256,8 @@ void state_bcm() &msg.frame.data[7]); if ( (items < 2) || - (msg.frame.len > 64) || - (items != 2 + msg.frame.len)) { + (msg.frame.can_dlc > 8) || + (items != 2 + msg.frame.can_dlc)) { PRINT_ERROR("Syntax error in update send job command\n") return; } @@ -305,7 +305,7 @@ void state_bcm() &msg.msg_head.ival2.tv_sec, &msg.msg_head.ival2.tv_usec, &msg.msg_head.can_id, - &msg.frame.len, + &msg.frame.can_dlc, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -316,8 +316,8 @@ void state_bcm() &msg.frame.data[7]); if( (items < 4) || - (msg.frame.len > 64) || - (items != 4 + msg.frame.len) ) { + (msg.frame.can_dlc > 8) || + (items != 4 + msg.frame.can_dlc) ) { PRINT_ERROR("syntax error in filter command.\n") return; } @@ -399,7 +399,7 @@ void state_bcm() if (!ioctl(sc, SIOCGIFINDEX, &ifr)) { caddr.can_ifindex = ifr.ifr_ifindex; sendto(sc, &muxmsg, sizeof(struct bcm_msg_head) + - sizeof(struct canfd_frame) * muxmsg.msg_head.nframes, + sizeof(struct can_frame) * muxmsg.msg_head.nframes, 0, (struct sockaddr*)&caddr, sizeof(caddr)); } /* Add a filter */ From 97947a817fbb6c9cd5b8a6352e63d34c92366809 Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Sun, 13 Oct 2024 09:02:07 +0900 Subject: [PATCH 05/10] fix insufficient size of buffer --- src/state_bcm.c | 138 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 112 insertions(+), 26 deletions(-) diff --git a/src/state_bcm.c b/src/state_bcm.c index aa2343f..402cdd1 100644 --- a/src/state_bcm.c +++ b/src/state_bcm.c @@ -25,7 +25,7 @@ #include #include -#define RXLEN 128 +#define RXLEN 256 int sc = -1; @@ -41,7 +41,7 @@ void state_bcm() struct { struct bcm_msg_head msg_head; - struct can_frame frame; + struct canfd_frame frame; } msg; struct { @@ -57,7 +57,7 @@ void state_bcm() return; } - memset(&caddr, 0, sizeof(caddr)); + memset(&caddr, 0, sizeof(caddr)); caddr.can_family = PF_CAN; /* can_ifindex is set to 0 (any device) => need for sendto() */ @@ -103,12 +103,12 @@ void state_bcm() /* Check if this is an error frame */ if(msg.msg_head.can_id & CAN_ERR_FLAG) { - if(msg.frame.can_dlc != CAN_ERR_DLC) { + if(msg.frame.len != CAN_ERR_DLC) { PRINT_ERROR("Error frame has a wrong DLC!\n") } else { snprintf(rxmsg, RXLEN, "< error %03X %ld.%06ld ", msg.msg_head.can_id, tv.tv_sec, tv.tv_usec); - for ( i = 0; i < msg.frame.can_dlc; i++) + for ( i = 0; i < msg.frame.len; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -117,15 +117,34 @@ void state_bcm() tcp_quickack(client_socket); } } else { - if (msg.msg_head.can_id & CAN_EFF_FLAG) { - snprintf(rxmsg, RXLEN, "< frame %08X %ld.%06ld ", - msg.msg_head.can_id & CAN_EFF_MASK, tv.tv_sec, tv.tv_usec); - } else { - snprintf(rxmsg, RXLEN, "< frame %03X %ld.%06ld ", - msg.msg_head.can_id & CAN_SFF_MASK, tv.tv_sec, tv.tv_usec); +// fprintf(stderr, "msg.msg_head.can_id: %d ret size: %d\n", msg.msg_head.can_id, ret); + switch(ret) { + // if the frame is a classic CAN frame + case sizeof(struct can_frame): + if(msg.msg_head.can_id & CAN_EFF_FLAG) { + snprintf(rxmsg, RXLEN, "< frame %08X %ld.%06ld ", + msg.msg_head.can_id & CAN_EFF_MASK, tv.tv_sec, tv.tv_usec); + } else { + snprintf(rxmsg, RXLEN, "< frame %03X %ld.%06ld ", + msg.msg_head.can_id & CAN_SFF_MASK, tv.tv_sec, tv.tv_usec); + } + break; + // if the frame is a CAN FD frame + case sizeof(struct canfd_frame): + if(msg.msg_head.can_id & CAN_EFF_FLAG) { + snprintf(rxmsg, RXLEN, "< fdframe %08X %02X %ld.%06ld ", + msg.msg_head.can_id & CAN_EFF_MASK, msg.msg_head.flags, tv.tv_sec, tv.tv_usec); + } else { + snprintf(rxmsg, RXLEN, "< fdframe %03X %02X %ld.%06ld ", + msg.msg_head.can_id & CAN_SFF_MASK, msg.msg_head.flags, tv.tv_sec, tv.tv_usec); + } + break; + default: + PRINT_ERROR("Unknown frame size %d\n", ret); + return; } - for ( i = 0; i < msg.frame.can_dlc; i++) + for ( i = 0; i < msg.frame.len; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -140,7 +159,7 @@ void state_bcm() ret = receive_command(client_socket, buf); - if (ret != 0) { + if(ret != 0) { state = STATE_SHUTDOWN; return; } @@ -172,7 +191,7 @@ void state_bcm() "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -183,8 +202,8 @@ void state_bcm() &msg.frame.data[7]); if ( (items < 2) || - (msg.frame.can_dlc > 8) || - (items != 2 + msg.frame.can_dlc)) { + (msg.frame.len > 8) || + (items != 2 + msg.frame.len)) { PRINT_ERROR("Syntax error in send command\n") return; } @@ -202,14 +221,81 @@ void state_bcm() (struct sockaddr *)&caddr, sizeof(caddr)); } /* Add a send job */ - } else if (!strncmp("< add ", buf, 6)) { + } else if(!strncmp("< fdsend ", buf, 9)) { + // First, read the fixed part of the frame + items = sscanf(buf, "< %*s %x %hhx %hhu", + &msg.msg_head.can_id, + &msg.frame.flags, + &msg.frame.len); + + if (items != 3) { + PRINT_ERROR("Syntax error in fdsend command\n"); + return; + } + + // Ensure frame.len does not exceed the maximum allowed length + if (msg.frame.len > 64) { + PRINT_ERROR("Frame length exceeds maximum allowed length\n"); + return; + } + + // Construct the format string for the dynamically based on frame.len + char format[256]; + snprintf(format, sizeof(format), "< %%*s %%x %%hhx %%hhu"); + for (int i = 0; i < msg.frame.len; i++) { + strncat(format, " %hhx", sizeof(format) - strlen(format) - 1); + } + strncat(format, " >", sizeof(format) - strlen(format) - 1); + + // Read the variable-length frame.data + items = sscanf(buf, format, + &msg.msg_head.can_id, + &msg.frame.flags, + &msg.frame.len, + &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], &msg.frame.data[3], + &msg.frame.data[4], &msg.frame.data[5], &msg.frame.data[6], &msg.frame.data[7], + &msg.frame.data[8], &msg.frame.data[9], &msg.frame.data[10], &msg.frame.data[11], + &msg.frame.data[12], &msg.frame.data[13], &msg.frame.data[14], &msg.frame.data[15], + &msg.frame.data[16], &msg.frame.data[17], &msg.frame.data[18], &msg.frame.data[19], + &msg.frame.data[20], &msg.frame.data[21], &msg.frame.data[22], &msg.frame.data[23], + &msg.frame.data[24], &msg.frame.data[25], &msg.frame.data[26], &msg.frame.data[27], + &msg.frame.data[28], &msg.frame.data[29], &msg.frame.data[30], &msg.frame.data[31], + &msg.frame.data[32], &msg.frame.data[33], &msg.frame.data[34], &msg.frame.data[35], + &msg.frame.data[36], &msg.frame.data[37], &msg.frame.data[38], &msg.frame.data[39], + &msg.frame.data[40], &msg.frame.data[41], &msg.frame.data[42], &msg.frame.data[43], + &msg.frame.data[44], &msg.frame.data[45], &msg.frame.data[46], &msg.frame.data[47], + &msg.frame.data[48], &msg.frame.data[49], &msg.frame.data[50], &msg.frame.data[51], + &msg.frame.data[52], &msg.frame.data[53], &msg.frame.data[54], &msg.frame.data[55], + &msg.frame.data[56], &msg.frame.data[57], &msg.frame.data[58], &msg.frame.data[59], + &msg.frame.data[60], &msg.frame.data[61], &msg.frame.data[62], &msg.frame.data[63]); + + if ( (items < 2) || + (msg.frame.len > 64) || + (items != 3 + msg.frame.len)) { + PRINT_ERROR("Syntax error in fdsend command\n") + return; + } + + /* < fdsend XXXXXXXX ... > check for extended identifier */ + if(element_length(buf, 2) == 8) + msg.msg_head.can_id |= CAN_EFF_FLAG; + + msg.msg_head.opcode = TX_SEND; + msg.frame.can_id = msg.msg_head.can_id; + + if (!ioctl(sc, SIOCGIFINDEX, &ifr)) { + caddr.can_ifindex = ifr.ifr_ifindex; + sendto(sc, &msg, sizeof(msg), 0, (struct sockaddr*)&caddr, sizeof(caddr)); + } + } else if(!strncmp("< add ", buf, 6)) { + fprintf(stderr, "%s\n", buf); items = sscanf(buf, "< %*s %lu %lu %x %hhu " "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &msg.msg_head.ival2.tv_sec, &msg.msg_head.ival2.tv_usec, &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -220,8 +306,8 @@ void state_bcm() &msg.frame.data[7]); if( (items < 4) || - (msg.frame.can_dlc > 8) || - (items != 4 + msg.frame.can_dlc) ) { + (msg.frame.len > 8) || + (items != 4 + msg.frame.len) ) { PRINT_ERROR("Syntax error in add command.\n"); return; } @@ -245,7 +331,7 @@ void state_bcm() "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -256,8 +342,8 @@ void state_bcm() &msg.frame.data[7]); if ( (items < 2) || - (msg.frame.can_dlc > 8) || - (items != 2 + msg.frame.can_dlc)) { + (msg.frame.len > 8) || + (items != 2 + msg.frame.len)) { PRINT_ERROR("Syntax error in update send job command\n") return; } @@ -305,7 +391,7 @@ void state_bcm() &msg.msg_head.ival2.tv_sec, &msg.msg_head.ival2.tv_usec, &msg.msg_head.can_id, - &msg.frame.can_dlc, + &msg.frame.len, &msg.frame.data[0], &msg.frame.data[1], &msg.frame.data[2], @@ -316,8 +402,8 @@ void state_bcm() &msg.frame.data[7]); if( (items < 4) || - (msg.frame.can_dlc > 8) || - (items != 4 + msg.frame.can_dlc) ) { + (msg.frame.len > 8) || + (items != 4 + msg.frame.len) ) { PRINT_ERROR("syntax error in filter command.\n") return; } From 02231251bb93152006c19022756eaf11dc58bfa2 Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Fri, 27 Dec 2024 10:03:57 +0900 Subject: [PATCH 06/10] fix cannot handle data size larger than length 8 --- src/socketcandcl.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/socketcandcl.c b/src/socketcandcl.c index d049956..df461f2 100644 --- a/src/socketcandcl.c +++ b/src/socketcandcl.c @@ -393,11 +393,32 @@ inline void state_connected() frame.len = strlen(data_str) / 2; - sscanf(data_str, "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", - &frame.data[0], &frame.data[1], - &frame.data[2], &frame.data[3], - &frame.data[4], &frame.data[5], - &frame.data[6], &frame.data[7]); + sscanf(data_str, + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx" + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", + &frame.data[0], &frame.data[1], &frame.data[2], &frame.data[3], + &frame.data[4], &frame.data[5], &frame.data[6], &frame.data[7], + &frame.data[8], &frame.data[9], &frame.data[10], &frame.data[11], + &frame.data[12], &frame.data[13], &frame.data[14], &frame.data[15], + &frame.data[16], &frame.data[17], &frame.data[18], &frame.data[19], + &frame.data[20], &frame.data[21], &frame.data[22], &frame.data[23], + &frame.data[24], &frame.data[25], &frame.data[26], &frame.data[27], + &frame.data[28], &frame.data[29], &frame.data[30], &frame.data[31], + &frame.data[32], &frame.data[33], &frame.data[34], &frame.data[35], + &frame.data[36], &frame.data[37], &frame.data[38], &frame.data[39], + &frame.data[40], &frame.data[41], &frame.data[42], &frame.data[43], + &frame.data[44], &frame.data[45], &frame.data[46], &frame.data[47], + &frame.data[48], &frame.data[49], &frame.data[50], &frame.data[51], + &frame.data[52], &frame.data[53], &frame.data[54], &frame.data[55], + &frame.data[56], &frame.data[57], &frame.data[58], &frame.data[59], + &frame.data[60], &frame.data[61], &frame.data[62], &frame.data[63] + ); ret = write(raw_socket, &frame, sizeof(struct canfd_frame)); if(ret != sizeof(struct canfd_frame)) { @@ -462,6 +483,10 @@ inline void state_connected() } sprintf(buf+ret, ">"); +#ifdef DEBUG + PRINT_VERBOSE("%s\n", buf); +#endif + const size_t len = strlen(buf); ret = send(server_socket, buf, len, 0); if (ret < sizeof(len)) { From e8196b2b8b2484bf6ac9f61a36aa7712c5e981ff Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Fri, 27 Dec 2024 12:44:02 +0900 Subject: [PATCH 07/10] fix should be canfd_frame --- src/socketcandcl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socketcandcl.c b/src/socketcandcl.c index df461f2..68d310d 100644 --- a/src/socketcandcl.c +++ b/src/socketcandcl.c @@ -447,7 +447,7 @@ inline void state_connected() } if (FD_ISSET(raw_socket, &readfds)) { - ret = recv(raw_socket, &frame, sizeof(struct can_frame), MSG_WAITALL); + ret = recv(raw_socket, &frame, sizeof(struct canfd_frame), MSG_WAITALL); if (ret < sizeof(struct can_frame)) { PRINT_ERROR("Error reading frame from RAW socket\n"); perror("Reading CAN socket\n"); From 41ce96b259a5ec13f5d4310a0e9a405a7dc6cbdb Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Fri, 27 Dec 2024 12:46:32 +0900 Subject: [PATCH 08/10] fix lack ";" --- src/state_bcm.c | 12 ++++++------ src/state_raw.c | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/state_bcm.c b/src/state_bcm.c index 402cdd1..4bdef73 100644 --- a/src/state_bcm.c +++ b/src/state_bcm.c @@ -104,8 +104,8 @@ void state_bcm() /* Check if this is an error frame */ if(msg.msg_head.can_id & CAN_ERR_FLAG) { if(msg.frame.len != CAN_ERR_DLC) { - PRINT_ERROR("Error frame has a wrong DLC!\n") - } else { + PRINT_ERROR("Error frame has a wrong DLC!\n"); + } else { snprintf(rxmsg, RXLEN, "< error %03X %ld.%06ld ", msg.msg_head.can_id, tv.tv_sec, tv.tv_usec); for ( i = 0; i < msg.frame.len; i++) @@ -204,7 +204,7 @@ void state_bcm() if ( (items < 2) || (msg.frame.len > 8) || (items != 2 + msg.frame.len)) { - PRINT_ERROR("Syntax error in send command\n") + PRINT_ERROR("Syntax error in send command\n"); return; } @@ -272,7 +272,7 @@ void state_bcm() if ( (items < 2) || (msg.frame.len > 64) || (items != 3 + msg.frame.len)) { - PRINT_ERROR("Syntax error in fdsend command\n") + PRINT_ERROR("Syntax error in fdsend command\n"); return; } @@ -344,7 +344,7 @@ void state_bcm() if ( (items < 2) || (msg.frame.len > 8) || (items != 2 + msg.frame.len)) { - PRINT_ERROR("Syntax error in update send job command\n") + PRINT_ERROR("Syntax error in update send job command\n"); return; } @@ -404,7 +404,7 @@ void state_bcm() if( (items < 4) || (msg.frame.len > 8) || (items != 4 + msg.frame.len) ) { - PRINT_ERROR("syntax error in filter command.\n") + PRINT_ERROR("syntax error in filter command.\n"); return; } diff --git a/src/state_raw.c b/src/state_raw.c index cfa765a..83e8955 100644 --- a/src/state_raw.c +++ b/src/state_raw.c @@ -125,8 +125,8 @@ void state_raw() ret = recvmsg(raw_socket, &msg, 0); if(ret != sizeof(struct canfd_frame) && ret != sizeof(struct can_frame)) { - PRINT_ERROR("Error reading frame from RAW socket\n") - } else { + PRINT_ERROR("Error reading frame from RAW socket\n"); + } else { /* read timestamp data */ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg && (cmsg->cmsg_level == SOL_SOCKET); @@ -209,7 +209,7 @@ void state_raw() if ( (items < 2) || (frame.len > 8) || (items != 2 + frame.len)) { - PRINT_ERROR("Syntax error in send command\n") + PRINT_ERROR("Syntax error in send command\n"); return; } @@ -274,7 +274,7 @@ void state_raw() if ( (items < 2) || (frame.len > 64) || (items != 3 + frame.len)) { - PRINT_ERROR("Syntax error in fdsend command\n") + PRINT_ERROR("Syntax error in fdsend command\n"); return; } From 210e2c0f471c6044a8604b2b6bcd024025b991e6 Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Fri, 27 Dec 2024 12:46:55 +0900 Subject: [PATCH 09/10] fix too short buffer size --- src/state_bcm.c | 2 +- src/state_raw.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/state_bcm.c b/src/state_bcm.c index 4bdef73..32f68d0 100644 --- a/src/state_bcm.c +++ b/src/state_bcm.c @@ -240,7 +240,7 @@ void state_bcm() } // Construct the format string for the dynamically based on frame.len - char format[256]; + char format[512]; snprintf(format, sizeof(format), "< %%*s %%x %%hhx %%hhu"); for (int i = 0; i < msg.frame.len; i++) { strncat(format, " %hhx", sizeof(format) - strlen(format) - 1); diff --git a/src/state_raw.c b/src/state_raw.c index 83e8955..8b79cf4 100644 --- a/src/state_raw.c +++ b/src/state_raw.c @@ -242,7 +242,7 @@ void state_raw() } // Construct the format string dynamically based on frame.len - char format[256]; + char format[512]; snprintf(format, sizeof(format), "< %%*s %%x %%hhx %%hhu"); for (int i = 0; i < frame.len; i++) { strncat(format, " %hhx", sizeof(format) - strlen(format) - 1); From 427f76bde571caffa23381ae71cf07cbedc23211 Mon Sep 17 00:00:00 2001 From: nanitsuku Date: Fri, 27 Dec 2024 12:50:51 +0900 Subject: [PATCH 10/10] fix spacing & delete unnecessary lines --- src/socketcandcl.c | 41 +++++++++++++-------------------- src/state_bcm.c | 56 ++++++++++++++++++++++------------------------ src/state_raw.c | 12 +++++----- 3 files changed, 48 insertions(+), 61 deletions(-) diff --git a/src/socketcandcl.c b/src/socketcandcl.c index 68d310d..183223d 100644 --- a/src/socketcandcl.c +++ b/src/socketcandcl.c @@ -298,7 +298,7 @@ inline void state_connected() return; } - if(ioctl(raw_socket,SIOCGIFMTU,&ifr) < 0) { + if (ioctl(raw_socket,SIOCGIFMTU,&ifr) < 0) { PRINT_ERROR("Error while searching for bus MTU %s\n", strerror(errno)); state = STATE_SHUTDOWN; return; @@ -306,16 +306,13 @@ inline void state_connected() if (ifr.ifr_mtu == CANFD_MTU) { const int canfd_on = 1; - if(setsockopt(raw_socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0) { + if (setsockopt(raw_socket, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)) < 0) { PRINT_ERROR("Could not enable CAN FD support\n"); state = STATE_SHUTDOWN; return; } } -// fprintf(stderr, "MTU of %s is %d\n", ldev, ifr.ifr_mtu); - - /* bind socket */ if (bind(raw_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) { PRINT_ERROR("Error while binding RAW socket %s\n", strerror(errno)); @@ -364,8 +361,6 @@ inline void state_connected() frame.len = strlen(data_str) / 2; -// fprintf(stderr, "frame.can_id: %d frame.flags: %d frame.len: %d\n", frame.can_id, frame.flags, frame.len); - sscanf(data_str, "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", &frame.data[0], &frame.data[1], &frame.data[2], &frame.data[3], @@ -373,22 +368,22 @@ inline void state_connected() &frame.data[6], &frame.data[7]); ret = write(raw_socket, &frame, sizeof(struct can_frame)); - if(ret != sizeof(struct can_frame)) { + if (ret != sizeof(struct can_frame)) { perror("Writing CAN frame to can socket\n"); } - } else if(!strncmp("< fdframe", buf, 9)) { + } else if (!strncmp("< fdframe", buf, 9)) { char data_str[2*64]; sscanf(buf, "< fdframe %x %hhx %*d.%*d %s >", &frame.can_id, &frame.flags, data_str); char* s = buf + 9; - for(; ++s;) { - if(*s== ' ') { + for (; ++s;) { + if (*s== ' ') { break; } } - if((s - buf - 9) > 4) + if ((s - buf - 9) > 4) frame.can_id |= CAN_EFF_FLAG; frame.len = strlen(data_str) / 2; @@ -421,7 +416,7 @@ inline void state_connected() ); ret = write(raw_socket, &frame, sizeof(struct canfd_frame)); - if(ret != sizeof(struct canfd_frame)) { + if (ret != sizeof(struct canfd_frame)) { perror("Writing CAN frame to can socket\n"); } } @@ -459,18 +454,16 @@ inline void state_connected() } else { int i; -// fprintf(stderr, "frame.can_id: %d frame.flags: %d frame.len: %d\n", frame.can_id, frame.flags, frame.len); - - if(ret == sizeof(struct can_frame)) { - if(frame.can_id & CAN_EFF_FLAG) { + if (ret == sizeof(struct can_frame)) { + if (frame.can_id & CAN_EFF_FLAG) { ret = sprintf(buf, "< send %08X %d ", frame.can_id & CAN_EFF_MASK, frame.len); } else { ret = sprintf(buf, "< send %03X %d ", frame.can_id & CAN_SFF_MASK, frame.len); } - } else if(ret == sizeof(struct canfd_frame)) { - if(frame.can_id & CAN_EFF_FLAG) { + } else if (ret == sizeof(struct canfd_frame)) { + if (frame.can_id & CAN_EFF_FLAG) { ret = sprintf(buf, "< fdsend %08X %02X %d ", frame.can_id & CAN_EFF_MASK, frame.flags, frame.len); } else { @@ -478,14 +471,10 @@ inline void state_connected() frame.can_id & CAN_SFF_MASK, frame.flags, frame.len); } } - for(i=0; i"); - -#ifdef DEBUG - PRINT_VERBOSE("%s\n", buf); -#endif + sprintf(buf + ret, " >"); const size_t len = strlen(buf); ret = send(server_socket, buf, len, 0); diff --git a/src/state_bcm.c b/src/state_bcm.c index 32f68d0..ac4a712 100644 --- a/src/state_bcm.c +++ b/src/state_bcm.c @@ -57,7 +57,7 @@ void state_bcm() return; } - memset(&caddr, 0, sizeof(caddr)); + memset(&caddr, 0, sizeof(caddr)); caddr.can_family = PF_CAN; /* can_ifindex is set to 0 (any device) => need for sendto() */ @@ -102,13 +102,13 @@ void state_bcm() } /* Check if this is an error frame */ - if(msg.msg_head.can_id & CAN_ERR_FLAG) { - if(msg.frame.len != CAN_ERR_DLC) { + if (msg.msg_head.can_id & CAN_ERR_FLAG) { + if (msg.frame.len != CAN_ERR_DLC) { PRINT_ERROR("Error frame has a wrong DLC!\n"); } else { snprintf(rxmsg, RXLEN, "< error %03X %ld.%06ld ", msg.msg_head.can_id, tv.tv_sec, tv.tv_usec); - for ( i = 0; i < msg.frame.len; i++) + for (i = 0; i < msg.frame.len; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -117,11 +117,10 @@ void state_bcm() tcp_quickack(client_socket); } } else { -// fprintf(stderr, "msg.msg_head.can_id: %d ret size: %d\n", msg.msg_head.can_id, ret); switch(ret) { // if the frame is a classic CAN frame case sizeof(struct can_frame): - if(msg.msg_head.can_id & CAN_EFF_FLAG) { + if (msg.msg_head.can_id & CAN_EFF_FLAG) { snprintf(rxmsg, RXLEN, "< frame %08X %ld.%06ld ", msg.msg_head.can_id & CAN_EFF_MASK, tv.tv_sec, tv.tv_usec); } else { @@ -131,7 +130,7 @@ void state_bcm() break; // if the frame is a CAN FD frame case sizeof(struct canfd_frame): - if(msg.msg_head.can_id & CAN_EFF_FLAG) { + if (msg.msg_head.can_id & CAN_EFF_FLAG) { snprintf(rxmsg, RXLEN, "< fdframe %08X %02X %ld.%06ld ", msg.msg_head.can_id & CAN_EFF_MASK, msg.msg_head.flags, tv.tv_sec, tv.tv_usec); } else { @@ -144,7 +143,7 @@ void state_bcm() return; } - for ( i = 0; i < msg.frame.len; i++) + for (i = 0; i < msg.frame.len; i++) snprintf(rxmsg + strlen(rxmsg), RXLEN - strlen(rxmsg), "%02X ", msg.frame.data[i]); @@ -159,7 +158,7 @@ void state_bcm() ret = receive_command(client_socket, buf); - if(ret != 0) { + if (ret != 0) { state = STATE_SHUTDOWN; return; } @@ -201,11 +200,11 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if ( (items < 2) || - (msg.frame.len > 8) || - (items != 2 + msg.frame.len)) { + if ((items < 2) || + (msg.frame.len > 8) || + (items != 2 + msg.frame.len)) { PRINT_ERROR("Syntax error in send command\n"); - return; + return; } /* < send XXXXXXXX ... > check for extended identifier */ @@ -221,7 +220,7 @@ void state_bcm() (struct sockaddr *)&caddr, sizeof(caddr)); } /* Add a send job */ - } else if(!strncmp("< fdsend ", buf, 9)) { + } else if (!strncmp("< fdsend ", buf, 9)) { // First, read the fixed part of the frame items = sscanf(buf, "< %*s %x %hhx %hhu", &msg.msg_head.can_id, @@ -269,15 +268,15 @@ void state_bcm() &msg.frame.data[56], &msg.frame.data[57], &msg.frame.data[58], &msg.frame.data[59], &msg.frame.data[60], &msg.frame.data[61], &msg.frame.data[62], &msg.frame.data[63]); - if ( (items < 2) || - (msg.frame.len > 64) || - (items != 3 + msg.frame.len)) { + if ((items < 2) || + (msg.frame.len > 64) || + (items != 3 + msg.frame.len)) { PRINT_ERROR("Syntax error in fdsend command\n"); - return; + return; } /* < fdsend XXXXXXXX ... > check for extended identifier */ - if(element_length(buf, 2) == 8) + if (element_length(buf, 2) == 8) msg.msg_head.can_id |= CAN_EFF_FLAG; msg.msg_head.opcode = TX_SEND; @@ -287,8 +286,7 @@ void state_bcm() caddr.can_ifindex = ifr.ifr_ifindex; sendto(sc, &msg, sizeof(msg), 0, (struct sockaddr*)&caddr, sizeof(caddr)); } - } else if(!strncmp("< add ", buf, 6)) { - fprintf(stderr, "%s\n", buf); + } else if (!strncmp("< add ", buf, 6)) { items = sscanf(buf, "< %*s %lu %lu %x %hhu " "%hhx %hhx %hhx %hhx %hhx %hhx " "%hhx %hhx >", @@ -305,7 +303,7 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if( (items < 4) || + if ((items < 4) || (msg.frame.len > 8) || (items != 4 + msg.frame.len) ) { PRINT_ERROR("Syntax error in add command.\n"); @@ -341,11 +339,11 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if ( (items < 2) || - (msg.frame.len > 8) || - (items != 2 + msg.frame.len)) { + if ((items < 2) || + (msg.frame.len > 8) || + (items != 2 + msg.frame.len)) { PRINT_ERROR("Syntax error in update send job command\n"); - return; + return; } /* < update XXXXXXXX ... > check for extended identifier */ @@ -401,11 +399,11 @@ void state_bcm() &msg.frame.data[6], &msg.frame.data[7]); - if( (items < 4) || + if ((items < 4) || (msg.frame.len > 8) || (items != 4 + msg.frame.len) ) { PRINT_ERROR("syntax error in filter command.\n"); - return; + return; } /* < filter sec usec XXXXXXXX ... > check for extended identifier */ @@ -486,7 +484,7 @@ void state_bcm() caddr.can_ifindex = ifr.ifr_ifindex; sendto(sc, &muxmsg, sizeof(struct bcm_msg_head) + sizeof(struct can_frame) * muxmsg.msg_head.nframes, - 0, (struct sockaddr*)&caddr, sizeof(caddr)); + 0, (struct sockaddr *)&caddr, sizeof(caddr)); } /* Add a filter */ } else if (!strncmp("< subscribe ", buf, 12)) { diff --git a/src/state_raw.c b/src/state_raw.c index 8b79cf4..7d4d335 100644 --- a/src/state_raw.c +++ b/src/state_raw.c @@ -162,8 +162,8 @@ void state_raw() } } - for(i=0;i"); send(client_socket, buf, strlen(buf), 0); @@ -206,11 +206,11 @@ void state_raw() &frame.data[6], &frame.data[7]); - if ( (items < 2) || - (frame.len > 8) || - (items != 2 + frame.len)) { + if ((items < 2) || + (frame.len > 8) || + (items != 2 + frame.len)) { PRINT_ERROR("Syntax error in send command\n"); - return; + return; } /* < send XXXXXXXX ... > check for extended identifier */