]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/rxtx.c
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[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                         int val;
66                         printf("Enter data byte [%d] ",i);
67                         scanf("%x",&val);
68                         message.data[i]=val;
69                 }
70         }       
71         if (*transmission=='r') {
72                         printf("At which device file would you like to receive the message?\n");
73                         printf(specialfile);
74                         *buf='\0';
75                         fgets(buf,MAXL,stdin);
76                         buf[strcspn(buf,"\n")]='\0';
77                         if(*buf)
78                                 strncpy(specialfile,buf,MAXL);
79                         specialfile[MAXL]='\0';
80                 printf("Enter the Message ID ");
81                 scanf("%lx",&message.id);
82                 getchar();
83         }
84
85         fd=open(specialfile,O_RDWR);
86         if (fd<0) {
87                 printf("Error opening %s\n",specialfile);
88                 return -1;
89         }
90
91         if (transmission[0]=='s') {
92                 printf("Press enter to send message\n");
93                 getchar();
94                 while (getchar() != '\n');
95                 ret=write(fd, &message, sizeof(struct canmsg_t));
96                 if (ret<0)
97                         printf("Error sending message from %s\n",specialfile);
98                 else
99                         printf("Message successfully sent from %s\n",specialfile);
100         }
101
102         if (*transmission=='r') {
103                 ioctl(fd,CONF_FILTER,message.id);
104                 printf("Press enter to receive message or <l>oop\n");
105                 loop = 'l';
106                 while ( loop == 'l') {
107                         loop = getchar(); 
108                         ret=read(fd, &message, sizeof(struct canmsg_t));
109                         if (ret<0)
110                                 printf("Error receiving message on %s\n",
111                                                                 specialfile);
112                         else {
113                                 printf("Id      : %lx\n",message.id);
114                                 printf("length  : %d\n",message.length);
115                                 printf("flags   : 0x%02x\n", message.flags);
116                                 #ifdef CAN_MSG_VERSION_2
117                                 printf("time    : %lds %ldusec\n", message.timestamp.tv_sec,
118                                         message.timestamp.tv_usec);
119                                 #else /* CAN_MSG_VERSION_2 */
120                                 printf("time    : %ld\n", message.timestamp);
121                                 #endif /* CAN_MSG_VERSION_2 */
122                                 for (i=0; i<message.length; i++)
123                                         printf("data%d  : %02x\n",i,
124                                                         message.data[i]);
125                         }
126                 }
127         }
128
129         if (close(fd)) {
130                 printf("Error closing %s\n",specialfile);
131                 return -1;
132         }
133
134         return 0;
135 }