Skip to content

Commit d9880ef

Browse files
authored
Enable error frame reception in raw mode (#67)
* Enable error frame reception in raw mode * add error_mask option
1 parent 30e584a commit d9880ef

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/socketcand.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ int cmd_index = 0;
6464
char *description;
6565
char *afuxname;
6666
int more_elements = 0;
67+
can_err_mask_t error_mask = 0;
6768
struct sockaddr_in saddr, broadcast_addr;
6869
struct sockaddr_un unaddr;
6970
socklen_t unaddrlen;
@@ -212,11 +213,12 @@ int main(int argc, char **argv)
212213
{ "daemon", no_argument, 0, 'd' },
213214
{ "version", no_argument, 0, 'z' },
214215
{ "no-beacon", no_argument, 0, 'n' },
216+
{ "error-mask", required_argument, 0, 'e' },
215217
{ "help", no_argument, 0, 'h' },
216218
{ 0, 0, 0, 0 }
217219
};
218220

219-
c = getopt_long(argc, argv, "vi:p:qu:l:dznh", long_options, &option_index);
221+
c = getopt_long(argc, argv, "vi:p:qu:l:dzne:h", long_options, &option_index);
220222

221223
if (c == -1)
222224
break;
@@ -269,6 +271,10 @@ int main(int argc, char **argv)
269271
disable_beacon = 1;
270272
break;
271273

274+
case 'e':
275+
error_mask = strtoul(optarg, NULL, 16);
276+
break;
277+
272278
case 'h':
273279
print_usage();
274280
return 0;
@@ -651,6 +657,7 @@ void print_usage(void)
651657
printf("\t-l <interface> (changes the default network interface the daemon will\n\t\tbind to - default: %s)\n", DEFAULT_INTERFACE);
652658
printf("\t-u <name> (the AF_UNIX socket path - abstract name when leading '/' is missing)\n\t\t(N.B. the AF_UNIX binding will supersede the port/interface settings)\n");
653659
printf("\t-n (deactivates the discovery beacon)\n");
660+
printf("\t-e <error_mask> (enable CAN error frames in raw mode providing an hexadecimal error mask, e.g: 0x1FFFFFFF)\n");
654661
printf("\t-d (set this flag if you want log to syslog instead of STDOUT)\n");
655662
printf("\t-h (prints this message)\n");
656663
}

src/socketcand.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <pthread.h>
44
#include <syslog.h>
5+
#include <linux/can.h>
56

67
/* max. length for ISO 15765-2 PDUs */
78
#define ISOTPLEN 4095
@@ -69,6 +70,7 @@ extern pthread_t statistics_thread;
6970
extern int more_elements;
7071
extern struct sockaddr_in broadcast_addr;
7172
extern struct sockaddr_in saddr;
73+
extern can_err_mask_t error_mask;
7274

7375
int receive_command(int socket, char *buf);
7476
int state_changed(char *buf, int current_state);

src/state_raw.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <syslog.h>
2222

2323
#include <linux/can.h>
24+
#include <linux/can/raw.h>
2425

2526
int raw_socket;
2627
struct ifreq ifr;
@@ -62,6 +63,15 @@ void state_raw()
6263
return;
6364
}
6465

66+
if (error_mask != 0) {
67+
can_err_mask_t err_mask = (error_mask & CAN_ERR_MASK);
68+
if (setsockopt(raw_socket, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask)) < 0) {
69+
PRINT_ERROR("Could not enable CAN_RAW_ERR_FILTER\n");
70+
state = STATE_SHUTDOWN;
71+
return;
72+
}
73+
}
74+
6575
if (bind(raw_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
6676
PRINT_ERROR("Error while binding RAW socket %s\n", strerror(errno));
6777
state = STATE_SHUTDOWN;

0 commit comments

Comments
 (0)