11 #include <sys/ioctl.h>
13 #include "../include/can.h"
17 struct canfilt_t canfilt = {
29 char *can_dev_name = "/dev/can0";
31 #define PRT_PREFIX_SIZE 40
32 char prt_prefix[PRT_PREFIX_SIZE];
34 char *prt_prefix_in = "CAN %s : ";
36 int can_fd_wait(int fd, int wait_sec)
39 struct timeval timeout;
44 timeout.tv_sec = wait_sec;
46 while ((ret=select(FD_SETSIZE,&set, NULL, NULL,&timeout))==-1
52 /*--- handler on SIGINT signal : the program quit with CTL-C ---*/
56 printf("Terminated by user\n");
63 printf("usage: readburst\n");
64 printf(" -d, --device <name> name of CAN device [/dev/can0]\n");
65 printf(" -m, --mask <num> CAN filter mask\n");
66 printf(" -i, --id <num> CAN filter message ID\n");
67 printf(" -f, --flags <num> CAN filter flags\n");
68 printf(" -w, --wait <num> number of seconds to wait in select call\n");
69 printf(" -p, --prefix <str> string prefix for output\n");
70 printf(" -V, --version show version\n");
71 printf(" -h, --help this usage screen\n");
75 int main(int argc, char *argv[])
77 static struct option long_opts[] = {
78 { "uldev", 1, 0, 'd' },
79 { "mask", 1, 0, 'm' },
81 { "flags", 1, 0, 'f' },
82 { "wait", 1, 0, 'w' },
83 { "prefix",1, 0, 'p' },
84 { "version",0,0, 'V' },
85 { "help", 0, 0, 'h' },
92 #ifdef CAN_MSG_VERSION_2
93 struct canmsg_t readmsg={0,0,5,{0,0},0,{0,}};
94 #else /* CAN_MSG_VERSION_2 */
95 struct canmsg_t readmsg={0,0,5,0,0,{0,}};
96 #endif /* CAN_MSG_VERSION_2 */
100 while ((opt = getopt_long(argc, argv, "d:m:i:f:w:p:Vh",
101 &long_opts[0], NULL)) != EOF) switch (opt) {
107 canfilt.mask = strtol(optarg,NULL,0);
111 canfilt.id = strtol(optarg,NULL,0);
115 canfilt.flags = strtol(optarg,NULL,0);
118 can_wait_sec = strtol(optarg,NULL,0);
121 prt_prefix_in = optarg;
124 fputs("LinCAN utilities v0.2\n", stdout);
129 exit(opt == 'h' ? 0 : 1);
133 /*------- register handler on SIGINT signal -------*/
134 act.sa_handler=sortie;
135 sigemptyset(&act.sa_mask);
137 sigaction(SIGINT,&act,0);
138 /*---------------------------------------*/
140 if ((fd=open(can_dev_name, O_RDWR)) < 0) {
142 printf("Error opening %s\n", can_dev_name);
147 ret = ioctl(fd, CANQUE_FILTER, &canfilt);
149 perror("ioctl FILTER_QUE");
153 snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
159 ret=can_fd_wait(fd, can_wait_sec);
160 printf("%scan_fd_wait returned %d\n", prt_prefix, ret);
162 ret=read(fd,&readmsg,sizeof(struct canmsg_t));
164 printf("%sError reading message\n", prt_prefix);
167 printf("%sNo message arrived\n", prt_prefix);
169 printf("%sRx msg #%lu: id=%lX dlc=%u flg=0x%02x",
170 prt_prefix,i,readmsg.id,readmsg.length,readmsg.flags);
171 for(n=0 ; n<readmsg.length ; n++)
172 printf(" %.2X",(unsigned char)readmsg.data[n]);