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