-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanlib.c.bak
309 lines (251 loc) · 6.67 KB
/
canlib.c.bak
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* canlib.c
*
* This source file contains functions copied from
* can-utils. Copyright (c) 2002-2007 Volkswagen
* Group Electronic Research. All rights reserved.
*
* The original code may have been modified for our
* purposes.
*
* See: https://github.com/linux-can/can-utils
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/error.h>
#include <linux/can/raw.h>
#include <canlib.h>
#define CANID_DELIM '#'
#define DATA_SEPARATOR '.'
const char hex_asc_upper[] = "0123456789ABCDEF";
/* CAN DLC to real data length conversion helpers */
static const unsigned char dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
8, 12, 16, 20, 24, 32, 48, 64};
/* get data length from can_dlc with sanitized can_dlc */
unsigned char can_dlc2len(unsigned char can_dlc)
{
return dlc2len[can_dlc & 0x0F];
}
static const unsigned char len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 9, 9, 9,
10, 10, 10, 10,
11, 11, 11, 11,
12, 12, 12, 12,
13, 13, 13, 13, 13, 13, 13, 13,
14, 14, 14, 14, 14, 14, 14, 14,
14, 14, 14, 14, 14, 14, 14, 14,
15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15};
/* map the sanitized data length to an appropriate data length code */
unsigned char can_len2dlc(unsigned char len)
{
if (len > 64)
return 0xF;
return len2dlc[len];
}
unsigned char asc2nibble(char c)
{
if ((c >= '0') && (c <= '9'))
return c - '0';
if ((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;
if ((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
return 16; /* error */
}
int parse_canframe(char *cs, struct canfd_frame *cf)
{
/* documentation see canlib.h */
int i, idx, dlen, len;
int maxdlen = CAN_MAX_DLEN;
int ret = CAN_MTU;
unsigned char tmp;
len = strlen(cs);
//printf("'%s' len %d\n", cs, len);
memset(cf, 0, sizeof(*cf)); /* init CAN FD frame, e.g. LEN = 0 */
if (len < 4)
return 0;
if (cs[3] == CANID_DELIM) // 3 digits
{
idx = 4;
for (i = 0; i < 3; i++)
{
if ((tmp = asc2nibble(cs[i])) > 0x0F)
return 0;
cf->can_id |= (tmp << (2-i)*4);
}
} else if (cs[8] == CANID_DELIM) // 8 digits
{
idx = 9;
for (i = 0; i < 8; i++)
{
if ((tmp = asc2nibble(cs[i])) > 0x0F)
return 0;
cf->can_id |= (tmp << (7-i)*4);
}
if (!(cf->can_id & CAN_ERR_FLAG)) // 8 digits but no error frame?
cf->can_id |= CAN_EFF_FLAG; // then it is an extended frame
} else
return 0;
if ((cs[idx] == 'R') || (cs[idx] == 'r')) // RTR frame
{
cf->can_id |= CAN_RTR_FLAG;
/* check for optional DLC value for CAN 2.0B frames */
if (cs[++idx] && (tmp = asc2nibble(cs[idx])) <= CAN_MAX_DLC)
cf->len = tmp;
return ret;
}
if (cs[idx] == CANID_DELIM) // CAN FD frame escape char '##'
{
maxdlen = CANFD_MAX_DLEN;
ret = CANFD_MTU;
/* CAN FD frame <canid>##<flags><data> */
if ((tmp = asc2nibble(cs[idx+1])) > 0x0F)
return 0;
cf->flags = tmp;
idx += 2;
}
for (i = 0, dlen = 0; i < maxdlen; i++)
{
if (cs[idx] == DATA_SEPARATOR) // skip (optional) separator
idx++;
if (idx >= len) // end of string => end of data
break;
if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
return 0;
cf->data[i] = (tmp << 4);
if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
return 0;
cf->data[i] |= tmp;
dlen++;
}
cf->len = dlen;
return ret;
}
int cansend(int argc, char **cs)
{
int s; // can raw socket
int required_mtu;
int mtu;
int enable_canfd = 1;
struct sockaddr_can addr;
struct canfd_frame frame;
struct ifreq ifr;
/* check command line options */
if (argc != 3)
{
// print usage
return 1;
}
/* parse CAN frame */
required_mtu = parse_canframe(cs[2], &frame);
if(!required_mtu)
{
fprintf(stderr, "\nWrong CAN frame format!\n\n");
// print usage
return 1;
}
/* open socket */
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
{
perror("socket");
return 1;
}
strncpy(ifr.ifr_name, cs[1], IFNAMSIZ - 1);
ifr.ifr_name[IFNAMSIZ - 1] = '\0';
ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
if (!ifr.ifr_ifindex)
{
perror("if_nametoindex");
return 1;
}
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (required_mtu > (int)CAN_MTU)
{
/* check if frame fits into the CAN netdevice */
if (ioctl(s, SIOCGIFMTU, &ifr) < 0)
{
perror ("SIOCGIFMTU");
return 1;
}
mtu = ifr.ifr_mtu;
if (mtu != CANFD_MTU)
{
printf("CAN interface is not CAN FD capable - sorry.\n");
return 1;
}
/* interface is ok - try to switch the socket into CAN FD mode */
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &enable_canfd,
sizeof(enable_canfd)))
{
printf("error when enabling CAN FD support\n");
return 1;
}
/* ensure discrete CAN FD length values 0..8, 12, 16, 20, 24, 32,
64 */
frame.len = can_dlc2len(can_len2dlc(frame.len));
}
/* disable default receive filter on this RAW socket */
// This is obsolete as we do not read from the socket at all, but for
// this reason we can remove the receive list in the Kernal to save a
// little (really a very little!) CPU usage.
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
return 1;
}
/* send frame */
if (write(s, &frame, required_mtu) != required_mtu)
{
perror("write");
return 1;
}
close(s);
return 0;
}
// copy candump.c
int canread()
{
int s;
int nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct canfd_frame frame;
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
{
perror("socket");
return 1;
}
strcpy(ifr.ifr_name, "can0");
ioctl(s, SIOCGIFINDEX, &ifr);
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
return 1;
}
nbytes = read(s, &frame, sizeof(struct can_frame));
if (nbytes < 0)
{
perror("read");
return 1;
}
printf("0x%03X [%d] ", frame.can_id, 8);
for (int i = 0; i < 8; i++)
{
printf("%02X ", frame.data[i]);
}
printf("\n");
return 0;
}