/
can-test.c
208 lines (167 loc) · 3.86 KB
/
can-test.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <poll.h>
#include <unistd.h>
#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <linux/can.h>
#include <linux/if.h>
#define IP_TOOL "/sbin/ip"
#ifndef PF_CAN
# define PF_CAN 29 /* Controller Area Network. */
#endif
#ifndef AF_CAN
# define AF_CAN PF_CAN
#endif
#if (__GNUC__ * 0x100) + __GNUC_MINOR__ < 0x403
#define spawn(_file, _arg0, _args...) ({ \
pid_t pid = fork(); \
int status; \
\
if (pid == 0) { \
execlp(_file, _arg0, _args); \
_exit(-1); \
} \
\
waitpid(pid, &status, 0); \
status == 0 ? 0 : -1; \
})
#else
static __attribute__ ((__always_inline__))
int spawn(char const *file, char const *arg0, ...)
{
pid_t pid = fork();
int status;
if (pid == 0) {
execlp(file, arg0, __builtin_va_arg_pack());
_exit(-1);
}
waitpid(pid, &status, 0);
return status == 0 ? 0 : -1;
}
#endif
static int setup_net(char const *iface)
{
spawn(IP_TOOL, "ip", "link", "set", iface, "down", NULL);
if (spawn(IP_TOOL, "ip", "link", "set", iface,
"type", "can", "bitrate", "100000", NULL) < 0 ||
spawn(IP_TOOL, "ip", "link", "set", iface, "up", NULL) < 0)
return -1;
return 0;
}
int main(int argc, char *argv[])
{
struct sockaddr_can addr = {
.can_family = AF_CAN
};
int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
struct ifreq ifr;
struct pollfd fds[2];
if (setup_net(argv[1]) < 0)
return EXIT_FAILURE;
strcpy(ifr.ifr_name, argv[1]);
ioctl(s, SIOCGIFINDEX, &ifr);
addr.can_ifindex = ifr.ifr_ifindex;
bind(s, (void *)&addr, sizeof addr);
fds[0].fd = STDIN_FILENO;
fds[0].events = POLLIN;
fds[1].fd = s;
fds[1].events = POLLIN;
for (;;) {
int rc;
fds[0].revents = 0;
fds[1].revents = 0;
rc = poll(fds, 2, -1);
if (rc < 0) {
perror("poll()");
return EXIT_FAILURE;
}
if ((fds[0].revents | fds[1].revents) & POLLERR)
return EXIT_FAILURE;
if (fds[0].revents & POLLIN) {
char buf[1024];
char *data;
ssize_t l = read(fds[0].fd, buf, sizeof(buf) - 1);
int id;
struct can_frame frame;
if (l < 0) {
perror("read(STDIN)");
return EXIT_FAILURE;
}
if (l == 0)
/* EOF */
break;
buf[l] = '\0';
while (l > 0 && iscntrl(buf[l-1]))
buf[--l] = '\0';
id = strtol(buf, &data, 0);
if (data[0] == '\0') {
fprintf(stderr, "missing data in input\n");
continue;
}
++data;
if (id < 0) {
id -= id;
id |= CAN_RTR_FLAG;
}
if (id > (1 << 11) - 1) {
id &= CAN_EFF_MASK | CAN_RTR_FLAG;
id |= CAN_EFF_FLAG;
}
frame.can_dlc = MIN(strlen(data), sizeof frame.data);
frame.can_id = id;
memcpy(frame.data, data, frame.can_dlc);
rc = sendto(s, &frame, sizeof frame, 0,
(void const *)&addr, sizeof addr);
if (rc < 0) {
perror("sendto(CAN)");
return EXIT_FAILURE;
}
}
if (fds[1].revents & POLLIN) {
struct can_frame frame;
struct sockaddr_can r_addr;
socklen_t a_len = sizeof r_addr;
size_t i;
uint32_t id;
rc = recvfrom(s, &frame, sizeof frame, 0,
(void *)&r_addr, &a_len);
if (rc < 0) {
perror("recvfrom(CAN)");
return EXIT_FAILURE;
}
if (rc == 0)
/* EOF? */
break;
id = frame.can_id;
if (!(frame.can_id & CAN_EFF_FLAG))
id &= CAN_SFF_MASK;
else
id &= CAN_EFF_MASK;
printf("[%d]: %s%s%u[%u] = [",
r_addr.can_ifindex,
frame.can_id & CAN_EFF_FLAG ? "E" : "",
frame.can_id & CAN_RTR_FLAG ? "R" : "",
id,
frame.can_dlc);
for (i = 0; i < MIN(frame.can_dlc,
sizeof frame.data); ++i) {
unsigned char c = frame.data[i];
if (i > 0)
printf(",");
if (isprint(c))
printf("'%c'", c);
else
printf("%02x", c);
}
printf("]\n");
}
}
}