]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/utils/send.c
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[lincan.git] / lincan / utils / send.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;
13         unsigned long bits;
14         int msglen;
15         char specialfile[MAXL+1]="/dev/can0", buf[MAXL+1];
16         struct canmsg_t message;
17
18         printf("\nThis program allows you to send  a stream of Can messages.\n");
19         printf("Please answer the following questions:\n\n");
20
21 //              message.flags = 0;
22         message.flags |= MSG_EXT;  //hard code EXT for now
23
24         printf("From wich device file would you like to send the message?\n");
25         printf(specialfile);
26         *buf='\0';
27         fgets(buf,MAXL,stdin);
28         buf[strcspn(buf,"\n")]='\0';
29         if(*buf)
30                 strncpy(specialfile,buf,MAXL);
31         specialfile[MAXL]='\0';
32         printf("Enter the starting Message ID ");
33         scanf("%lx",&message.id);
34         printf("Enter the Message Length ");
35         scanf("%d",&msglen);
36         message.length=msglen;
37         for (i=0; i<message.length; i++) {
38                 int val;
39                 printf("Enter data byte [%d] ",i);
40                 scanf("%x",&val);
41                 message.data[i]=val;
42         }
43
44         fd=open(specialfile,O_RDWR);
45         if (fd<0) {
46                 printf("Error opening %s\n",specialfile);
47                 return -1;
48         }
49
50         bits=0;
51         while(1) {
52                 message.flags = MSG_EXT;
53                 message.id++;
54                 message.length %= 8;
55                 message.length++;
56                 bits += 8*(4+message.length);
57                 memcpy(message.data,&bits,sizeof(bits));
58                 ret=write(fd, &message, sizeof(struct canmsg_t));
59                 if (ret<0)
60                         printf("Error sending message from %s\n",specialfile);
61         }
62
63         if (close(fd)) {
64                 printf("Error closing %s\n",specialfile);
65                 return -1;
66         }
67
68         return 0;
69 }