Compare commits

...

4 Commits

Author SHA1 Message Date
Carsten Schmidt 0e0e9ccf05
Merge 15ed00a985 into 287469245c 2025-04-09 05:30:12 -04:00
Marc Kleine-Budde 287469245c
Merge pull request #123 from twasilczyk/slcand-android
Don't fail when it wasn't possible to fetch the interface name.
2025-04-06 10:11:38 +02:00
Tomasz Wasilczyk 7a318636e7 slcand: Don't fail when it wasn't possible to fetch the interface name.
On some systems SIOCGIFNAME may fail with ENOTTY, but the actual
slcanX interface gets properly configured. Instead of crashing hard on
such case, let's gracefuly degrade and just not display the interface
name.
2025-04-06 10:03:36 +02:00
Carsten Schmidt 15ed00a985 Read PDU to send from binary file. 2023-03-25 16:41:45 +01:00
2 changed files with 36 additions and 4 deletions

View File

@ -42,6 +42,7 @@
* *
*/ */
#include <fcntl.h>
#include <libgen.h> #include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -78,12 +79,32 @@ void print_usage(char *prg)
fprintf(stderr, " -S (SF broadcast mode - for functional addressing)\n"); fprintf(stderr, " -S (SF broadcast mode - for functional addressing)\n");
fprintf(stderr, " -C (CF broadcast mode - no wait for flow controls)\n"); fprintf(stderr, " -C (CF broadcast mode - no wait for flow controls)\n");
fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n"); fprintf(stderr, " -L <mtu>:<tx_dl>:<tx_flags> (link layer options for CAN FD)\n");
fprintf(stderr, " -i <filename> (Read PDU data from binary file instead of STDIN)\n");
fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n"); fprintf(stderr, "\nCAN IDs and addresses are given and expected in hexadecimal values.\n");
fprintf(stderr, "The pdu data is expected on STDIN in space separated ASCII hex values.\n"); fprintf(stderr, "The pdu data is expected on STDIN in space separated ASCII hex values.\n");
fprintf(stderr, "(*) = Use '-t %s' to set N_As to zero for Linux version 5.18+\n", ZERO_STRING); fprintf(stderr, "(*) = Use '-t %s' to set N_As to zero for Linux version 5.18+\n", ZERO_STRING);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
ssize_t read_binary_file(void *buffer, const size_t siz_buffer, const char *filename)
{
int fd = -1;
ssize_t num_read = -1;
if( buffer == NULL || siz_buffer < 1 || filename == NULL ) {
return 0;
}
if( (fd = open(filename, O_RDONLY)) == -1 ) {
return 0;
}
num_read = read(fd, buffer, siz_buffer);
close(fd);
return num_read > 0 ? num_read : 0;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int s; int s;
@ -102,7 +123,7 @@ int main(int argc, char **argv)
addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID; addr.can_addr.tp.tx_id = addr.can_addr.tp.rx_id = NO_CAN_ID;
while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:D:l:g:bSCL:?")) != -1) { while ((opt = getopt(argc, argv, "s:d:x:p:P:t:f:D:l:g:bSCL:i:?")) != -1) {
switch (opt) { switch (opt) {
case 's': case 's':
addr.can_addr.tp.tx_id = strtoul(optarg, NULL, 16); addr.can_addr.tp.tx_id = strtoul(optarg, NULL, 16);
@ -227,6 +248,10 @@ int main(int argc, char **argv)
} }
break; break;
case 'i':
buflen = read_binary_file(&buf[0], sizeof(buf), optarg);
break;
case '?': case '?':
print_usage(basename(argv[0])); print_usage(basename(argv[0]));
exit(0); exit(0);
@ -285,6 +310,7 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
if( buflen < 1 ) {
if (!datalen) { if (!datalen) {
while (buflen < BUFSIZE && scanf("%hhx", &buf[buflen]) == 1) while (buflen < BUFSIZE && scanf("%hhx", &buf[buflen]) == 1)
buflen++; buflen++;
@ -292,6 +318,7 @@ int main(int argc, char **argv)
for (buflen = 0; buflen < datalen; buflen++) for (buflen = 0; buflen < datalen; buflen++)
buf[buflen] = ((buflen % 0xFF) + 1) & 0xFF; buf[buflen] = ((buflen % 0xFF) + 1) & 0xFF;
} }
}
loop: loop:
if (usecs) if (usecs)
@ -313,7 +340,7 @@ loop:
goto loop; goto loop;
} }
/* /*
* due to a Kernel internal wait queue the PDU is sent completely * due to a Kernel internal wait queue the PDU is sent completely
* before close() returns. * before close() returns.
*/ */

View File

@ -370,8 +370,13 @@ int main(int argc, char *argv[])
/* retrieve the name of the created CAN netdevice */ /* retrieve the name of the created CAN netdevice */
if (ioctl(fd, SIOCGIFNAME, ifr.ifr_name) < 0) { if (ioctl(fd, SIOCGIFNAME, ifr.ifr_name) < 0) {
perror("ioctl SIOCGIFNAME"); if (name) {
exit(EXIT_FAILURE); perror("ioctl SIOCGIFNAME");
exit(EXIT_FAILURE);
} else {
/* Graceful degradation: we only needed the name for display. */
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "<unknown>");
}
} }
syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, ifr.ifr_name); syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, ifr.ifr_name);