Structured comments updated.
[lincan.git] / lincan / utils / sendburst_rtl.c
1 #include <rtl.h>
2 #include <time.h>
3 #include <signal.h>
4 #include <pthread.h>
5 #include <posix/unistd.h>
6
7 #define printf rtl_printf
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 MODULE_PARM_DESC(can_dev_name,"name of CAN device [/dev/can0]");
27 MODULE_PARM(can_dev_name,"1s");
28 MODULE_PARM_DESC(canmsg_id,"ID of generated messages");
29 MODULE_PARM(canmsg_id,"1i");
30 MODULE_PARM_DESC(canmsg_flags,"CAN filter flags");
31 MODULE_PARM(canmsg_flags,"1i");
32 MODULE_PARM_DESC(o_sync_fl,"open in synchronous mode");
33 MODULE_PARM(o_sync_fl,"1i");
34 MODULE_PARM_DESC(can_wait_sec,"number of seconds to wait between messages");
35 MODULE_PARM(can_wait_sec,"1i");
36 MODULE_PARM_DESC(block,"number of messages in  block");
37 MODULE_PARM(block,"1i");
38 MODULE_PARM_DESC(count,"number of sent blocks of messages");
39 MODULE_PARM(count,"1i");
40 MODULE_PARM_DESC(prt_prefix_in,"string prefix for output");
41 MODULE_PARM(prt_prefix_in,"1i");
42
43
44 MODULE_SUPPORTED_DEVICE("sendburst_rtl");
45 MODULE_AUTHOR("Pavel Pisa <pisa@cmp.felk.cvut.cz>");
46 MODULE_DESCRIPTION("sendburst_rtl: generatot of CAN messages for RT-Linux interface to LinCAN driver");
47 MODULE_LICENSE("GPL");
48
49 void sendburst_cleanup(void *arg)
50 {
51         printf("%scleanup handler called\n", prt_prefix);
52         close((int)arg);
53 }
54
55 int sendburst_main(void *arg)
56 {
57        #ifdef CAN_MSG_VERSION_2
58         struct canmsg_t sendmsg={0,0,5,{0,0},8,{1,2,3,4,5,6,7,8}};
59        #else /* CAN_MSG_VERSION_2 */
60         struct canmsg_t sendmsg={0,0,5,0,8,{1,2,3,4,5,6,7,8}};
61        #endif /* CAN_MSG_VERSION_2 */
62         int fd, ret,i,j;
63
64
65         if ((fd=open(can_dev_name, O_RDWR | (o_sync_fl? O_SYNC:0))) < 0) {
66                 printf("Error opening %s\n", can_dev_name);
67                 return -1;
68         }
69         pthread_cleanup_push(sendburst_cleanup,(void*)fd);
70
71         snprintf(prt_prefix, PRT_PREFIX_SIZE, prt_prefix_in, can_dev_name);
72
73         j=0;
74         while (1) {
75                 for(i=0;i<block;i++) {
76                         sendmsg.flags=canmsg_flags;
77                         sendmsg.id=canmsg_id;
78                         sendmsg.data[0]=i;
79                         sendmsg.data[1]=j;
80                         if ((ret=write(fd,&sendmsg,sizeof(struct canmsg_t))) < 0) {
81                                 printf("%sError sending message\n", prt_prefix);
82                                 break;
83                         }
84                 }
85                 printf("%sSent block of %d messages #: %u\n", prt_prefix, block, j);
86                 j++;
87                 pthread_testcancel();
88                 usleep(1000000*can_wait_sec);
89                 if(count)
90                         if(!--count) break;
91                 pthread_testcancel();
92         }
93         /* close(fd); is called by cleanup handler*/
94         pthread_cleanup_pop(1);
95         return 0;
96 }
97
98         
99 /*===========================================================*/
100
101
102 void * t1_routine(void *arg)
103 {
104   sendburst_main(NULL);
105
106   while (1) {
107     pthread_wait_np ();
108
109   }
110   return 0;
111 }
112
113 pthread_t t1;
114
115 int init_module(void) {
116         
117         return pthread_create (&t1, NULL, t1_routine, 0);
118
119 }
120
121 void cleanup_module(void) {
122         /*pthread_delete_np (t1);*/
123         
124         pthread_cancel(t1);
125         pthread_join(t1, NULL);
126 }