]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/rxtx.c
Change to CAN_MSG_VERSION_2 and elimination of linux/*.h headers from user space...
[lincan.git] / lincan / utils / rxtx.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6
7 #include "../include/can.h"
8 #define MAXL 40
9
10 int main(void)
11 {
12         int i=0, fd=0, ret=0, count=0;
13         int msglen;
14         char loop=0;
15         char ch, transmission[MAXL+1], specialfile[MAXL+1]="/dev/can0", emptystring[MAXL+1]="", buf[MAXL+1];
16         char remote[MAXL+1];
17         struct canmsg_t message;
18
19         printf("\nThis program allows you to send or receive Can messages.\n");
20         printf("Please answer the following questions:\n\n");
21
22         while ( (*transmission!='s') && (*transmission!='r') ) {
23                 printf("Would you like to send or receive a message?\n");
24                 printf("send: <s> | receive: <r> ");
25                 strcpy(transmission,emptystring);
26                 count=0;
27                 while ( (ch=getchar()) != '\n' )
28                         transmission[count++]=ch;
29                 transmission[count]='\0';
30         }
31
32         while ( *remote!='y' && *remote!='n' ) {
33                 printf("Should the message be configured for Remote Transmission Requests?\n");
34                 printf("yes: <y> | no: <n> ");
35                 strcpy(remote,emptystring);
36                 count=0;
37                 while ( (ch=getchar()) != '\n' )
38                         remote[count++]=ch;
39                 remote[count]='\0';
40         }
41         if (remote[0]=='y')
42                 message.flags = MSG_RTR;
43         else
44                 message.flags = 0;
45 //      message.flags |= MSG_EXT;  hard code EXT for now
46
47         if (transmission[0]=='s') {
48                         printf("From wich device file would you like to send the message?\n");
49                         printf(specialfile);
50                         *buf='\0';
51                         fgets(buf,MAXL,stdin);
52                         buf[strcspn(buf,"\n")]='\0';
53                         if(*buf)
54                                 strncpy(specialfile,buf,MAXL);
55                         specialfile[MAXL]='\0';
56                         specialfile[MAXL]='\0';
57                 printf("Enter the Message ID ");
58                 scanf("%lx",&message.id);
59                 if(message.id>=(1<<11))
60                   message.flags |= MSG_EXT;
61                 printf("Enter the Message Length ");
62                 scanf("%d",&msglen);
63                 message.length=msglen;
64                 for (i=0; i<message.length; i++) {
65                         printf("Enter data byte [%d] ",i);
66                         scanf("%x",(int *)&message.data[i]);
67                 }
68         }       
69         if (*transmission=='r') {
70                         printf("At which device file would you like to receive the message?\n");
71                         printf(specialfile);
72                         *buf='\0';
73                         fgets(buf,MAXL,stdin);
74                         buf[strcspn(buf,"\n")]='\0';
75                         if(*buf)
76                                 strncpy(specialfile,buf,MAXL);
77                         specialfile[MAXL]='\0';
78                 printf("Enter the Message ID ");
79                 scanf("%lx",&message.id);
80                 getchar();
81         }
82
83         fd=open(specialfile,O_RDWR);
84         if (fd<0) {
85                 printf("Error opening %s\n",specialfile);
86                 return -1;
87         }
88
89         if (transmission[0]=='s') {
90                 printf("Press enter to send message\n");
91                 getchar();
92                 while (getchar() != '\n');
93                 ret=write(fd, &message, sizeof(struct canmsg_t));
94                 if (ret<0)
95                         printf("Error sending message from %s\n",specialfile);
96                 else
97                         printf("Message successfully sent from %s\n",specialfile);
98         }
99
100         if (*transmission=='r') {
101                 ioctl(fd,CONF_FILTER,message.id);
102                 printf("Press enter to receive message or <l>oop\n");
103                 loop = 'l';
104                 while ( loop == 'l') {
105                         loop = getchar(); 
106                         ret=read(fd, &message, sizeof(struct canmsg_t));
107                         if (ret<0)
108                                 printf("Error receiving message on %s\n",
109                                                                 specialfile);
110                         else {
111                                 printf("Id      : %lx\n",message.id);
112                                 printf("length  : %d\n",message.length);
113                                 printf("flags   : 0x%02x\n", message.flags);
114                                 #ifdef CAN_MSG_VERSION_2
115                                 printf("time    : %lds %ldusec\n", message.timestamp.tv_sec,
116                                         message.timestamp.tv_usec);
117                                 #else /* CAN_MSG_VERSION_2 */
118                                 printf("time    : %ld\n", message.timestamp);
119                                 #endif /* CAN_MSG_VERSION_2 */
120                                 for (i=0; i<message.length; i++)
121                                         printf("data%d  : %02x\n",i,
122                                                         message.data[i]);
123                         }
124                 }
125         }
126
127         if (close(fd)) {
128                 printf("Error closing %s\n",specialfile);
129                 return -1;
130         }
131
132         return 0;
133 }