]> rtime.felk.cvut.cz Git - lincan.git/commitdiff
Updated "sendburst" utility to reflect enhancements in in "readburst".
authorppisa <ppisa>
Sun, 26 Oct 2003 22:38:14 +0000 (22:38 +0000)
committerppisa <ppisa>
Sun, 26 Oct 2003 22:38:14 +0000 (22:38 +0000)
lincan/utils/sendburst.c

index 084a4b2674e95f123f2ec6245a44c89b7d301de8..0e32b63d083761056ff45882b34276372f9ccbd4 100644 (file)
 #include <stdio.h>
+#include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <getopt.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
-
 #include "../include/can.h"
 
-int main(void)
-       {
+int canmsg_flags = 0;
+unsigned long canmsg_id = 5;
+
+int can_wait_sec = 1;
+
+char *can_dev_name = "/dev/can0";
+
+#define PRT_PREFIX_SIZE 40
+char prt_prefix[PRT_PREFIX_SIZE];
+
+char *prt_prefix_in = "CAN %s : ";
+
+
+static void
+usage(void)
+{
+  printf("usage: sendburst\n");
+  printf("  -d, --device  <name>     name of CAN device [/dev/can0]\n");
+  printf("  -i, --id  <num>          ID of generated messages\n");
+  printf("  -f, --flags <num>        CAN filter flags\n");
+  printf("  -w, --wait <num>         number of seconds to wait between messages\n");
+  printf("  -p, --prefix <str>       string prefix for output\n");
+  printf("  -V, --version            show version\n");
+  printf("  -h, --help               this usage screen\n");
+}
+
+int main(int argc, char *argv[])
+{
+       static struct option long_opts[] = {
+               { "uldev", 1, 0, 'd' },
+               { "id",    1, 0, 'i' },
+               { "flags", 1, 0, 'f' },
+               { "wait",  1, 0, 'w' },
+               { "prefix",1, 0, 'p' },
+               { "version",0,0, 'V' },
+               { "help",  0, 0, 'h' },
+               { 0, 0, 0, 0}
+       };
+       int opt;
+
        struct canmsg_t sendmsg={0,0,5,0,8,{1,2,3,4,5,6,7,8}};
        int fd, ret,i,j;
 
-       if ((fd=open("/dev/can0",O_RDWR)) < 0 ) 
-               {
+       while ((opt = getopt_long(argc, argv, "d:i:f:w:p:Vh",
+                           &long_opts[0], NULL)) != EOF) switch (opt) {
+               case 'd':
+                       can_dev_name=optarg;
+                       break;
+               case 'i':
+                       canmsg_id = strtol(optarg,NULL,0);
+                       break;
+               case 'f':
+                       canmsg_flags = strtol(optarg,NULL,0);
+                       break;
+               case 'w':
+                       can_wait_sec = strtol(optarg,NULL,0);
+                       break;
+               case 'p':
+                       prt_prefix_in = optarg;
+                       break;
+               case 'V':
+                       fputs("LinCAN utilities v0.2\n", stdout);
+                       exit(0);
+               case 'h':
+               default:
+                       usage();
+                       exit(opt == 'h' ? 0 : 1);
+       }
+
+       if ((fd=open(can_dev_name, O_RDWR)) < 0) {
                perror("open");
-               printf("Error opening /dev/can0\n");
-               exit(1);
-               }
+               printf("Error opening %s\n", can_dev_name);
+               exit(1);        
+       }
+
+       snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
+
        j=0;
-       while (1)
-               {
-               for(i=0;i<10;i++)
-                       {
+       while (1) {
+               for(i=0;i<10;i++) {
+                       sendmsg.flags=canmsg_flags;
+                       sendmsg.id=canmsg_id;
                        sendmsg.data[0]=i;
                        sendmsg.data[1]=j;
-                       if ((ret=write(fd,&sendmsg,sizeof(struct canmsg_t))) < 0)
-                         {
+                       if ((ret=write(fd,&sendmsg,sizeof(struct canmsg_t))) < 0) {
                                perror("write");
-                               printf("Error sending message\n");
+                               printf("%sError sending message\n", prt_prefix);
                                break;
-                               }
                        }
-               printf("Sent block of 10 messages #: %u\n",j);
-               j++;
-               usleep(500000);
                }
+               printf("%sSent block of 10 messages #: %u\n", prt_prefix, j);
+               j++;
+               usleep(1000000*can_wait_sec);
+       }
        close(fd);
        return 0;
-       }
+}