]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/sendburst.c
Header-files cleanup and CAN queue edges and ends locking reimplemented.
[lincan.git] / lincan / utils / sendburst.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <getopt.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8
9 #include "../include/can.h"
10
11 int canmsg_flags = 0;
12 unsigned long canmsg_id = 5;
13 int block = 10;
14 int count = 0;
15
16 int can_wait_sec = 1;
17 int o_sync_fl = 0;
18
19 char *can_dev_name = "/dev/can0";
20
21 #define PRT_PREFIX_SIZE 40
22 char prt_prefix[PRT_PREFIX_SIZE];
23
24 char *prt_prefix_in = "CAN %s : ";
25
26
27 static void
28 usage(void)
29 {
30   printf("usage: sendburst\n");
31   printf("  -d, --device  <name>     name of CAN device [/dev/can0]\n");
32   printf("  -i, --id  <num>          ID of generated messages\n");
33   printf("  -f, --flags <num>        CAN filter flags\n");
34   printf("  -s, --sync               open in synchronous mode\n");
35   printf("  -w, --wait <num>         number of seconds to wait between messages\n");
36   printf("  -b, --block <num>        number of messages in  block\n");
37   printf("  -c, --count <num>        number of sent blocks of messages\n");
38   printf("  -p, --prefix <str>       string prefix for output\n");
39   printf("  -V, --version            show version\n");
40   printf("  -h, --help               this usage screen\n");
41 }
42
43 int main(int argc, char *argv[])
44 {
45         static struct option long_opts[] = {
46                 { "uldev", 1, 0, 'd' },
47                 { "id",    1, 0, 'i' },
48                 { "flags", 1, 0, 'f' },
49                 { "sync",  0, 0, 's' },
50                 { "wait",  1, 0, 'w' },
51                 { "block", 1, 0, 'b' },
52                 { "count", 1, 0, 'c' },
53                 { "prefix",1, 0, 'p' },
54                 { "version",0,0, 'V' },
55                 { "help",  0, 0, 'h' },
56                 { 0, 0, 0, 0}
57         };
58         int opt;
59
60         struct canmsg_t sendmsg={0,0,5,0,8,{1,2,3,4,5,6,7,8}};
61         int fd, ret,i,j;
62
63         while ((opt = getopt_long(argc, argv, "d:i:f:sw:b:c:p:Vh",
64                             &long_opts[0], NULL)) != EOF) switch (opt) {
65                 case 'd':
66                         can_dev_name=optarg;
67                         break;
68                 case 'i':
69                         canmsg_id = strtol(optarg,NULL,0);
70                         break;
71                 case 'f':
72                         canmsg_flags = strtol(optarg,NULL,0);
73                         break;
74                 case 's':
75                         o_sync_fl = 1;
76                         break;
77                 case 'w':
78                         can_wait_sec = strtol(optarg,NULL,0);
79                         break;
80                 case 'b':
81                         block = strtol(optarg,NULL,0);
82                         break;
83                 case 'c':
84                         count = strtol(optarg,NULL,0);
85                         break;
86                 case 'p':
87                         prt_prefix_in = optarg;
88                         break;
89                 case 'V':
90                         fputs("LinCAN utilities v0.2\n", stdout);
91                         exit(0);
92                 case 'h':
93                 default:
94                         usage();
95                         exit(opt == 'h' ? 0 : 1);
96         }
97
98         if ((fd=open(can_dev_name, O_RDWR | (o_sync_fl? O_SYNC:0))) < 0) {
99                 perror("open");
100                 printf("Error opening %s\n", can_dev_name);
101                 exit(1);        
102         }
103
104         snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
105
106         j=0;
107         while (1) {
108                 for(i=0;i<block;i++) {
109                         sendmsg.flags=canmsg_flags;
110                         sendmsg.id=canmsg_id;
111                         sendmsg.data[0]=i;
112                         sendmsg.data[1]=j;
113                         if ((ret=write(fd,&sendmsg,sizeof(struct canmsg_t))) < 0) {
114                                 perror("write");
115                                 printf("%sError sending message\n", prt_prefix);
116                                 break;
117                         }
118                 }
119                 printf("%sSent block of %d messages #: %u\n", prt_prefix, block, j);
120                 j++;
121                 usleep(1000000*can_wait_sec);
122                 if(count)
123                         if(!--count) break;
124         }
125         close(fd);
126         return 0;
127 }
128
129