Updated "sendburst" utility to reflect enhancements in in "readburst".
[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
14 int can_wait_sec = 1;
15
16 char *can_dev_name = "/dev/can0";
17
18 #define PRT_PREFIX_SIZE 40
19 char prt_prefix[PRT_PREFIX_SIZE];
20
21 char *prt_prefix_in = "CAN %s : ";
22
23
24 static void
25 usage(void)
26 {
27   printf("usage: sendburst\n");
28   printf("  -d, --device  <name>     name of CAN device [/dev/can0]\n");
29   printf("  -i, --id  <num>          ID of generated messages\n");
30   printf("  -f, --flags <num>        CAN filter flags\n");
31   printf("  -w, --wait <num>         number of seconds to wait between messages\n");
32   printf("  -p, --prefix <str>       string prefix for output\n");
33   printf("  -V, --version            show version\n");
34   printf("  -h, --help               this usage screen\n");
35 }
36
37 int main(int argc, char *argv[])
38 {
39         static struct option long_opts[] = {
40                 { "uldev", 1, 0, 'd' },
41                 { "id",    1, 0, 'i' },
42                 { "flags", 1, 0, 'f' },
43                 { "wait",  1, 0, 'w' },
44                 { "prefix",1, 0, 'p' },
45                 { "version",0,0, 'V' },
46                 { "help",  0, 0, 'h' },
47                 { 0, 0, 0, 0}
48         };
49         int opt;
50
51         struct canmsg_t sendmsg={0,0,5,0,8,{1,2,3,4,5,6,7,8}};
52         int fd, ret,i,j;
53
54         while ((opt = getopt_long(argc, argv, "d:i:f:w:p:Vh",
55                             &long_opts[0], NULL)) != EOF) switch (opt) {
56                 case 'd':
57                         can_dev_name=optarg;
58                         break;
59                 case 'i':
60                         canmsg_id = strtol(optarg,NULL,0);
61                         break;
62                 case 'f':
63                         canmsg_flags = strtol(optarg,NULL,0);
64                         break;
65                 case 'w':
66                         can_wait_sec = strtol(optarg,NULL,0);
67                         break;
68                 case 'p':
69                         prt_prefix_in = optarg;
70                         break;
71                 case 'V':
72                         fputs("LinCAN utilities v0.2\n", stdout);
73                         exit(0);
74                 case 'h':
75                 default:
76                         usage();
77                         exit(opt == 'h' ? 0 : 1);
78         }
79
80         if ((fd=open(can_dev_name, O_RDWR)) < 0) {
81                 perror("open");
82                 printf("Error opening %s\n", can_dev_name);
83                 exit(1);        
84         }
85
86         snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
87
88         j=0;
89         while (1) {
90                 for(i=0;i<10;i++) {
91                         sendmsg.flags=canmsg_flags;
92                         sendmsg.id=canmsg_id;
93                         sendmsg.data[0]=i;
94                         sendmsg.data[1]=j;
95                         if ((ret=write(fd,&sendmsg,sizeof(struct canmsg_t))) < 0) {
96                                 perror("write");
97                                 printf("%sError sending message\n", prt_prefix);
98                                 break;
99                         }
100                 }
101                 printf("%sSent block of 10 messages #: %u\n", prt_prefix, j);
102                 j++;
103                 usleep(1000000*can_wait_sec);
104         }
105         close(fd);
106         return 0;
107 }
108
109