]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/readburst.c
Change to CAN_MSG_VERSION_2 and elimination of linux/*.h headers from user space...
[lincan.git] / lincan / utils / readburst.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <signal.h>
6 #include <getopt.h>
7 #include <fcntl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/time.h>
11 #include <sys/ioctl.h>
12
13 #include "../include/can.h"
14
15 int fd;
16
17 struct canfilt_t canfilt = {
18         .flags = 0,
19         .queid = 0,
20         .cob = 0,
21         .id = 0,
22         .mask = 0
23 };
24
25 int canfilt_fl;
26
27 int can_wait_sec = 5;
28
29 char *can_dev_name = "/dev/can0";
30
31 #define PRT_PREFIX_SIZE 40
32 char prt_prefix[PRT_PREFIX_SIZE];
33
34 char *prt_prefix_in = "CAN %s : ";
35
36 int can_fd_wait(int fd, int wait_sec)
37 {
38         int ret;
39         struct timeval timeout;
40         fd_set set;
41
42         FD_ZERO (&set);
43         FD_SET (fd, &set);
44         timeout.tv_sec = wait_sec;
45         timeout.tv_usec = 0;
46         while ((ret=select(FD_SETSIZE,&set, NULL, NULL,&timeout))==-1
47           &&errno==-EINTR);
48         return ret;
49 }
50
51
52 /*--- handler on SIGINT signal : the program quit with CTL-C ---*/
53 void sortie(int sig)
54 {
55         close(fd);
56         printf("Terminated by user\n");
57         exit(0);
58 }
59
60 static void
61 usage(void)
62 {
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");
72 }
73
74
75 int main(int argc, char *argv[])
76 {
77         static struct option long_opts[] = {
78                 { "uldev", 1, 0, 'd' },
79                 { "mask",  1, 0, 'm' },
80                 { "id",    1, 0, 'i' },
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' },
86                 { 0, 0, 0, 0}
87         };
88         int opt;
89
90         int n,ret;
91         unsigned long i=0;
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 */
97         struct sigaction act;
98
99
100         while ((opt = getopt_long(argc, argv, "d:m:i:f:w:p:Vh",
101                             &long_opts[0], NULL)) != EOF) switch (opt) {
102                 case 'd':
103                         can_dev_name=optarg;
104                         break;
105                 case 'm':
106                         canfilt_fl=1;
107                         canfilt.mask = strtol(optarg,NULL,0);
108                         break;
109                 case 'i':
110                         canfilt_fl=1;
111                         canfilt.id = strtol(optarg,NULL,0);
112                         break;
113                 case 'f':
114                         canfilt_fl=1;
115                         canfilt.flags = strtol(optarg,NULL,0);
116                         break;
117                 case 'w':
118                         can_wait_sec = strtol(optarg,NULL,0);
119                         break;
120                 case 'p':
121                         prt_prefix_in = optarg;
122                         break;
123                 case 'V':
124                         fputs("LinCAN utilities v0.2\n", stdout);
125                         exit(0);
126                 case 'h':
127                 default:
128                         usage();
129                         exit(opt == 'h' ? 0 : 1);
130         }
131
132
133         /*------- register handler on SIGINT signal -------*/
134         act.sa_handler=sortie;
135         sigemptyset(&act.sa_mask);
136         act.sa_flags=0;
137         sigaction(SIGINT,&act,0);
138         /*---------------------------------------*/     
139
140         if ((fd=open(can_dev_name, O_RDWR)) < 0) {
141                 perror("open");
142                 printf("Error opening %s\n", can_dev_name);
143                 exit(1);        
144         }
145         
146         if (canfilt_fl) {
147                 ret = ioctl(fd, CANQUE_FILTER, &canfilt);
148                 if(ret<0) {
149                         perror("ioctl FILTER_QUE");
150                 }
151         }
152
153         snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
154         
155         while (1) {
156                 readmsg.flags=0;
157                 readmsg.cob=0;
158             #if 1
159                 ret=can_fd_wait(fd, can_wait_sec);
160                 printf("%scan_fd_wait returned %d\n", prt_prefix, ret);
161             #endif
162                 ret=read(fd,&readmsg,sizeof(struct canmsg_t));
163                 if(ret <0) {
164                         printf("%sError reading message\n", prt_prefix);
165                 }
166                 else if(ret == 0) {
167                         printf("%sNo message arrived\n", prt_prefix);
168                 } else {
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]);
173                         printf("\n");
174                         i++;
175                 }
176         }
177         return 0;
178 }