From 374dc2192a256dc856ccafea56dc46f77363a560 Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Tue, 15 Apr 2025 19:03:45 +0200 Subject: [PATCH 1/2] canutils/slcan: Work with CAN interfaces with names longer than 4 characters. Signed-off-by: Carlos Sanchez --- canutils/slcan/slcan.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/canutils/slcan/slcan.c b/canutils/slcan/slcan.c index f6b6c4e3ccc..8d4053c2f9c 100644 --- a/canutils/slcan/slcan.c +++ b/canutils/slcan/slcan.c @@ -137,8 +137,7 @@ static int caninit(char *candev, int *s, struct sockaddr_can *addr, syslog(LOG_ERR, "Error opening CAN socket\n"); return -1; } - strncpy(ifr.ifr_name, candev, 4); - ifr.ifr_name[4] = '\0'; + strlcpy(ifr.ifr_name, candev, IFNAMSIZ); ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name); if (!ifr.ifr_ifindex) { From a77b2d8ea75516960bb8de2882df8fd557ae672d Mon Sep 17 00:00:00 2001 From: Carlos Sanchez Date: Tue, 15 Apr 2025 19:09:48 +0200 Subject: [PATCH 2/2] canutils/slcan: explicitly manage the interface. A recent change (https://github.com/apache/nuttx/pull/16199) has made the bitrate setting no longer bring the interface up. Moreover, it is now no longer possible to change bitrate of a CAN interface if it is up. Therefore, slcan needs to bring the interface down to change it. Fortunately, it already had commands to open and close the interface which map nicely to this. Signed-off-by: Carlos Sanchez --- canutils/slcan/slcan.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/canutils/slcan/slcan.c b/canutils/slcan/slcan.c index 8d4053c2f9c..6515194dd09 100644 --- a/canutils/slcan/slcan.c +++ b/canutils/slcan/slcan.c @@ -316,9 +316,22 @@ int main(int argc, char *argv[]) { /* open CAN interface */ - mode = 1; - debug_print("Open interface\n"); - ok_return(fd); + struct ifreq ifr; + + strlcpy(ifr.ifr_name, argv[1], IFNAMSIZ); + + ifr.ifr_flags = IFF_UP; + if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) + { + syslog(LOG_ERR, "Open interface failed\n"); + fail_return(fd); + } + else + { + mode = 1; + debug_print("Open interface\n"); + ok_return(fd); + } } else if (buf[0] == 'S') { @@ -394,9 +407,22 @@ int main(int argc, char *argv[]) { /* close interface */ - mode = 0; - debug_print("Close interface\n"); - ok_return(fd); + struct ifreq ifr; + + strlcpy(ifr.ifr_name, argv[1], IFNAMSIZ); + + ifr.ifr_flags = 0; + if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) + { + syslog(LOG_ERR, "Close interface failed\n"); + fail_return(fd); + } + else + { + mode = 0; + debug_print("Close interface\n"); + ok_return(fd); + } } else if (buf[0] == 'T') {