]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/sendburst.c
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[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        #ifdef CAN_MSG_VERSION_2
61         struct canmsg_t sendmsg={0,0,5,{0,0},8,{1,2,3,4,5,6,7,8}};
62        #else /* CAN_MSG_VERSION_2 */
63         struct canmsg_t sendmsg={0,0,5,0,8,{1,2,3,4,5,6,7,8}};
64        #endif /* CAN_MSG_VERSION_2 */
65         int fd, ret,i,j;
66
67         while ((opt = getopt_long(argc, argv, "d:i:f:sw:b:c:p:Vh",
68                             &long_opts[0], NULL)) != EOF) switch (opt) {
69                 case 'd':
70                         can_dev_name=optarg;
71                         break;
72                 case 'i':
73                         canmsg_id = strtol(optarg,NULL,0);
74                         break;
75                 case 'f':
76                         canmsg_flags = strtol(optarg,NULL,0);
77                         break;
78                 case 's':
79                         o_sync_fl = 1;
80                         break;
81                 case 'w':
82                         can_wait_sec = strtol(optarg,NULL,0);
83                         break;
84                 case 'b':
85                         block = strtol(optarg,NULL,0);
86                         break;
87                 case 'c':
88                         count = strtol(optarg,NULL,0);
89                         break;
90                 case 'p':
91                         prt_prefix_in = optarg;
92                         break;
93                 case 'V':
94                         fputs("LinCAN utilities v0.2\n", stdout);
95                         exit(0);
96                 case 'h':
97                 default:
98                         usage();
99                         exit(opt == 'h' ? 0 : 1);
100         }
101
102         if ((fd=open(can_dev_name, O_RDWR | (o_sync_fl? O_SYNC:0))) < 0) {
103                 perror("open");
104                 printf("Error opening %s\n", can_dev_name);
105                 exit(1);        
106         }
107
108         snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
109
110         j=0;
111         while (1) {
112                 for(i=0;i<block;i++) {
113                         sendmsg.flags=canmsg_flags;
114                         sendmsg.id=canmsg_id;
115                         sendmsg.data[0]=i;
116                         sendmsg.data[1]=j;
117                         if ((ret=write(fd,&sendmsg,sizeof(struct canmsg_t))) < 0) {
118                                 perror("write");
119                                 printf("%sError sending message\n", prt_prefix);
120                                 break;
121                         }
122                 }
123                 printf("%sSent block of %d messages #: %u\n", prt_prefix, block, j);
124                 j++;
125                 usleep(1000000*can_wait_sec);
126                 if(count)
127                         if(!--count) break;
128         }
129         close(fd);
130         return 0;
131 }
132
133