-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-ioctl.c
134 lines (119 loc) · 2.7 KB
/
send-ioctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* send-ioctl sends a CAN message specified at the command line */
/* It is using the SEND ioctl command */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "can4linux.h"
#define STDDEV "can1"
#ifndef TRUE
# define TRUE 1
# define FALSE 0
#endif
int can_fd;
int debug = FALSE;
int extd = FALSE;
int rtr = FALSE;
canmsg_t message;
void usage(char *s)
{
static char *usage_text = "\
-r send message as rtr message.\n\
-e send message in extended message format.\n\
-d - debug On\n\
\n\
";
fprintf(stderr, "usage: %s [options] [dev]\n", s);
fprintf(stderr, "%s",usage_text);
}
int main(int argc,char **argv)
{
int c;
extern char *optarg;
extern int optind, opterr, optopt;
int cnt;
char device[40] = STDDEV;
Send_par_t SendTeil;
/* our default 8 byte message */
message.id = 100;
message.cob = 0;
message.flags = 0;
message.length = 8;
message.data[0] = 0x55;
message.data[1] = 2;
message.data[2] = 3;
message.data[3] = 4;
message.data[4] = 5;
message.data[5] = 6;
message.data[6] = 7;
message.data[7] = 0xaa;
while ((c = getopt(argc, argv, "derh")) != EOF)
{
switch (c)
{
case 'r':
rtr = TRUE;
break;
case 'e':
extd = TRUE;
break;
case 'd':
debug = TRUE;
break;
case 'h':
default:
usage(argv[0]); exit(0);
}
}
/* look for additional arguments given on the command line */
if ( argc - optind > 0 )
{
/* at least one additional argument, the device name is given */
char *darg = argv[optind];
if ( darg[0] == '/')
{
sprintf(device, "%s", darg);
}
else
{
sprintf(device, "/dev/%s", darg);
}
}
else
{
sprintf(device, "/dev/%s", STDDEV);
}
if(debug) printf("using CAN device %s\n", device);
if(( can_fd = open(device, O_RDWR )) < 0 )
{
fprintf(stderr,"Error opening CAN device %s\n", device);
exit(1);
}
if (rtr)
{
message.flags |= MSG_RTR;
}
if (extd)
{
message.flags |= MSG_EXT;
}
if ( debug == TRUE )
{
printf("send-ioctl " __DATE__ "\n");
printf("(c) 2006 Advantech\n");
printf(" using canmsg_t with %d bytes\n", sizeof(canmsg_t));
}
SendTeil.Tx = &message;
cnt = ioctl(can_fd, CAN_IOCTL_SEND, &SendTeil);
if(debug)
{
printf("CAN Send retuned with %d/0x%x\n", cnt, cnt);
}
usleep(10000);
close(can_fd);
return 0;
}