From fb401496c9dbbc1b0c0bcf25df073aebf947d7f7 Mon Sep 17 00:00:00 2001 From: Nick James Kirkby <20824939+driftregion@users.noreply.github.com> Date: Fri, 20 Oct 2023 23:53:08 -0700 Subject: [PATCH 1/4] initialize struct ifreq ifr #19 --- iso14229.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/iso14229.c b/iso14229.c index e430b6c..0d96efb 100644 --- a/iso14229.c +++ b/iso14229.c @@ -220,27 +220,27 @@ static ssize_t tp_send(UDSTpHandle_t *hdl, const void *buf, size_t count, UDSTpA #if UDS_TP == UDS_TP_ISOTP_SOCKET static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid) { int fd = 0; - if ((fd = socket(AF_CAN, SOCK_DGRAM | SOCK_NONBLOCK, CAN_ISOTP)) < 0) { - perror("Socket"); - return -1; - } - + struct ifreq ifr = {0}; + struct sockaddr_can addr = {0}; struct can_isotp_fc_options fcopts = { .bs = 0x10, .stmin = 3, .wftmax = 0, }; + + if ((fd = socket(AF_CAN, SOCK_DGRAM | SOCK_NONBLOCK, CAN_ISOTP)) < 0) { + perror("Socket"); + return -1; + } + if (setsockopt(fd, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts)) < 0) { perror("setsockopt"); return -1; } - struct ifreq ifr; - strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name)); + strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1); ioctl(fd, SIOCGIFINDEX, &ifr); - struct sockaddr_can addr; - memset(&addr, 0, sizeof(addr)); addr.can_family = AF_CAN; addr.can_addr.tp.rx_id = rxid; addr.can_addr.tp.tx_id = txid; From 55b50c38aa4a50f984aa1b7e97d0eeef2ac38562 Mon Sep 17 00:00:00 2001 From: Nick James Kirkby <20824939+driftregion@users.noreply.github.com> Date: Fri, 20 Oct 2023 23:57:41 -0700 Subject: [PATCH 2/4] check ioctl return code as suggested by @muehlke --- iso14229.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/iso14229.c b/iso14229.c index 0d96efb..6d841a3 100644 --- a/iso14229.c +++ b/iso14229.c @@ -239,7 +239,10 @@ static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid) { } strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1); - ioctl(fd, SIOCGIFINDEX, &ifr); + if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) { + printf("ioctl: %s %s\n", strerror(errno), if_name); + return -1; + } addr.can_family = AF_CAN; addr.can_addr.tp.rx_id = rxid; From 711c1384b3ca76f1503ca66fcc555a49790f8fe8 Mon Sep 17 00:00:00 2001 From: Nick James Kirkby <20824939+driftregion@users.noreply.github.com> Date: Sat, 21 Oct 2023 00:01:43 -0700 Subject: [PATCH 3/4] use fprintf(stderr, ...) for error messages --- iso14229.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/iso14229.c b/iso14229.c index 6d841a3..5856a0e 100644 --- a/iso14229.c +++ b/iso14229.c @@ -229,7 +229,7 @@ static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid) { }; if ((fd = socket(AF_CAN, SOCK_DGRAM | SOCK_NONBLOCK, CAN_ISOTP)) < 0) { - perror("Socket"); + perror("socket"); return -1; } @@ -240,7 +240,7 @@ static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid) { strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1); if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) { - printf("ioctl: %s %s\n", strerror(errno), if_name); + fprintf(stderr, "ioctl: %s %s\n", strerror(errno), if_name); return -1; } @@ -250,7 +250,7 @@ static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid) { addr.can_ifindex = ifr.ifr_ifindex; if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { - printf("Bind: %s %s\n", strerror(errno), if_name); + fprintf(stderr, "bind: %s %s\n", strerror(errno), if_name); return -1; } printf("opened ISO-TP link fd: %d, rxid: %03x, txid: %03x\n", fd, rxid, txid); From a0da50f7a94f011f59e261fdf29dba5533be206c Mon Sep 17 00:00:00 2001 From: Nick James Kirkby <20824939+driftregion@users.noreply.github.com> Date: Tue, 31 Oct 2023 19:58:40 -0700 Subject: [PATCH 4/4] feedback --- iso14229.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iso14229.c b/iso14229.c index 5856a0e..6933053 100644 --- a/iso14229.c +++ b/iso14229.c @@ -229,7 +229,7 @@ static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid) { }; if ((fd = socket(AF_CAN, SOCK_DGRAM | SOCK_NONBLOCK, CAN_ISOTP)) < 0) { - perror("socket"); + fprintf(stderr, "socket: %s", strerror(errno)); return -1; }