]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/rxtx.c
Added RT-Linux version of sendburst and readburst utilities.
[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                 if(message.id>=(1<<11))
59                   message.flags |= MSG_EXT;
60                 printf("Enter the Message Length ");
61                 scanf("%d",&message.length);
62                 for (i=0; i<message.length; i++) {
63                         printf("Enter data byte [%d] ",i);
64                         scanf("%x",(int *)&message.data[i]);
65                 }
66         }       
67         if (*transmission=='r') {
68                         printf("At which device file would you like to receive the message?\n");
69                         printf(specialfile);
70                         *buf='\0';
71                         fgets(buf,MAXL,stdin);
72                         buf[strcspn(buf,"\n")]='\0';
73                         if(*buf)
74                                 strncpy(specialfile,buf,MAXL);
75                         specialfile[MAXL]='\0';
76                 printf("Enter the Message ID ");
77                 scanf("%lx",&message.id);
78                 getchar();
79         }
80
81         fd=open(specialfile,O_RDWR);
82         if (fd<0) {
83                 printf("Error opening %s\n",specialfile);
84                 return -1;
85         }
86
87         if (transmission[0]=='s') {
88                 printf("Press enter to send message\n");
89                 getchar();
90                 while (getchar() != '\n');
91                 ret=write(fd, &message, sizeof(struct canmsg_t));
92                 if (ret<0)
93                         printf("Error sending message from %s\n",specialfile);
94                 else
95                         printf("Message successfully sent from %s\n",specialfile);
96         }
97
98         if (*transmission=='r') {
99                 ioctl(fd,CONF_FILTER,message.id);
100                 printf("Press enter to receive message or <l>oop\n");
101                 loop = 'l';
102                 while ( loop == 'l') {
103                         loop = getchar(); 
104                         ret=read(fd, &message, sizeof(struct canmsg_t));
105                         if (ret<0)
106                                 printf("Error receiving message on %s\n",
107                                                                 specialfile);
108                         else {
109                                 printf("Id      : %lx\n",message.id);
110                                 printf("length  : %d\n",message.length);
111                                 printf("flags   : 0x%02x\n", message.flags);
112                                 printf("time    : %ld\n", message.timestamp);
113                                 for (i=0; i<message.length; i++)
114                                         printf("data%d  : %02x\n",i,
115                                                         message.data[i]);
116                         }
117                 }
118         }
119
120         if (close(fd)) {
121                 printf("Error closing %s\n",specialfile);
122                 return -1;
123         }
124
125         return 0;
126 }