11 #include <sys/ioctl.h>
13 #include "../include/can.h"
17 struct canfilt_t canfilt = {
31 char *can_dev_name = "/dev/can0";
33 #define PRT_PREFIX_SIZE 40
34 char prt_prefix[PRT_PREFIX_SIZE];
36 char *prt_prefix_in = "CAN %s : ";
38 int can_fd_wait(int fd, int wait_sec)
41 struct timeval timeout;
46 timeout.tv_sec = wait_sec;
48 while ((ret=select(FD_SETSIZE,&set, NULL, NULL,&timeout))==-1
54 /*--- handler on SIGINT signal : the program quit with CTL-C ---*/
58 printf("Terminated by user\n");
65 printf("usage: readburst\n");
66 printf(" -d, --device <name> name of CAN device [/dev/can0]\n");
67 printf(" -m, --mask <num> CAN filter mask\n");
68 printf(" -i, --id <num> CAN filter message ID\n");
69 printf(" -f, --flags <num> CAN filter flags\n");
70 printf(" -w, --wait <num> number of seconds to wait in select call\n");
71 printf(" -p, --prefix <str> string prefix for output\n");
72 printf(" -q, --query query driver features\n");
73 printf(" -V, --version show version\n");
74 printf(" -h, --help this usage screen\n");
78 int main(int argc, char *argv[])
80 static struct option long_opts[] = {
81 { "uldev", 1, 0, 'd' },
82 { "mask", 1, 0, 'm' },
84 { "flags", 1, 0, 'f' },
85 { "wait", 1, 0, 'w' },
86 { "prefix",1, 0, 'p' },
87 { "query" ,0 ,0, 'q' },
88 { "version",0,0, 'V' },
89 { "help", 0, 0, 'h' },
96 #ifdef CAN_MSG_VERSION_2
97 struct canmsg_t readmsg={0,0,5,{0,0},0,{0,}};
98 #else /* CAN_MSG_VERSION_2 */
99 struct canmsg_t readmsg={0,0,5,0,0,{0,}};
100 #endif /* CAN_MSG_VERSION_2 */
101 struct sigaction act;
104 while ((opt = getopt_long(argc, argv, "d:m:i:f:w:p:qVh",
105 &long_opts[0], NULL)) != EOF) switch (opt) {
111 canfilt.mask = strtol(optarg,NULL,0);
115 canfilt.id = strtol(optarg,NULL,0);
119 canfilt.flags = strtol(optarg,NULL,0);
122 can_wait_sec = strtol(optarg,NULL,0);
125 prt_prefix_in = optarg;
131 fputs("LinCAN utilities v0.2\n", stdout);
136 exit(opt == 'h' ? 0 : 1);
140 /*------- register handler on SIGINT signal -------*/
141 act.sa_handler=sortie;
142 sigemptyset(&act.sa_mask);
144 sigaction(SIGINT,&act,0);
145 /*---------------------------------------*/
147 if ((fd=open(can_dev_name, O_RDWR)) < 0) {
149 printf("Error opening %s\n", can_dev_name);
154 n=ioctl(fd, CAN_DRV_QUERY, CAN_DRV_QRY_BRANCH);
155 printf("CAN driver branch: %c%c%c%c\n",(n>>24)&0xff,(n>>16)&0xff,(n>>8)&0xff,n&0xff);
156 n=ioctl(fd, CAN_DRV_QUERY, CAN_DRV_QRY_VERSION);
157 printf("CAN driver version: %d.%d.%d\n",(n>>16)&0xff,(n>>8)&0xff,n&0xff);
158 n=ioctl(fd, CAN_DRV_QUERY, CAN_DRV_QRY_MSGFORMAT);
159 printf("CAN message format: %08x\n",n);
165 ret = ioctl(fd, CANQUE_FILTER, &canfilt);
167 perror("ioctl FILTER_QUE");
171 snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
177 ret=can_fd_wait(fd, can_wait_sec);
178 printf("%scan_fd_wait returned %d\n", prt_prefix, ret);
180 ret=read(fd,&readmsg,sizeof(struct canmsg_t));
182 printf("%sError reading message\n", prt_prefix);
185 printf("%sNo message arrived\n", prt_prefix);
187 printf("%sRx msg #%lu: id=%lX dlc=%u flg=0x%02x",
188 prt_prefix,i,readmsg.id,readmsg.length,readmsg.flags);
189 for(n=0 ; n<readmsg.length ; n++)
190 printf(" %.2X",(unsigned char)readmsg.data[n]);