forked from Delsian/CanSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cansock.c
executable file
·361 lines (319 loc) · 9.03 KB
/
cansock.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Socket functions for CanSerial
*
* Copyright (C) 2019 Eug Krashtan <[email protected]>
* This file may be distributed under the terms of the GNU GPLv3 license.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <fcntl.h>
#include <pty.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/inotify.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include "cansock.h"
#include "portnumber.h"
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
/* CAN raw socket */
static int Socket;
pthread_t RxTh;
static int threadexit = 0;
static tPorts ports;
static int sock;
static int pingptr = 0;
static int Inotify;
static void CanVportClose(tPortId *p) {
int res;
char fname[15];
sprintf(fname,"/tmp/ttyCAN%d", p->port);
inotify_rm_watch(Inotify, p->watch);
res = unlink(fname);
if (res == 0) {
printf("Unlink port %d from %s\n", p->port, fname);
}
}
static int CanVport(int portid)
{
int res, i;
int fd, sfd;
struct termios ti;
char fname[16];
// check if port exists
for(i=1; i<ports.portptr; i++) {
if (ports.p[i].port == portid) {
// Assign the same virtual port for re-initialized CAN
fprintf(stderr, "Device reset: /tmp/ttyCAN%d\n", portid);
return i;
}
}
if (ports.portptr == ports.portsize) {
// Increase allocated space
ports.portsize *= 2;
ports.p = realloc(ports.p, sizeof(tPortId) * ports.portsize);
ports.VportFd = malloc(sizeof(struct pollfd) * ports.portsize);
if (!ports.p || !ports.VportFd) {
fprintf(stderr, "CreatePipe: realloc failed!\n");
exit(1);
}
}
// Assign packet handlers
ports.p[ports.portptr].canid = portid+PKT_ID_CTL_FILTER;
ports.p[ports.portptr].port = portid;
ports.p[ports.portptr].pingcount = PINGS_BEFORE_DISCONNECT;
ports.p[ports.portptr].active = 0;
ports.VportFd[ports.portptr].revents = 0;
// allocate virtual port
memset(&ti, 0, sizeof(ti));
res = openpty(&fd, &sfd, NULL, &ti, NULL);
if (res) {
fprintf(stderr, "Error: openpty %d\n", res);
return -1;
}
int flags = fcntl(fd, F_GETFL);
if (flags < 0) {
fprintf(stderr, "Error: fcntl getfl %d\n", flags);
return -1;
}
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
fcntl(fd, F_SETFD, FD_CLOEXEC);
fcntl(sfd, F_SETFD, FD_CLOEXEC);
char *tname = ttyname(sfd);
printf("New ptyname %s linked to /tmp/ttyCAN%d\n", tname, portid);
// Create symlink to tty
sprintf(fname,"/tmp/ttyCAN%d", portid);
unlink(fname);
res = symlink(tname, fname);
if (res) {
fprintf(stderr, "Error: symlink %d\n", res);
return -1;
}
res = chmod(tname, 0660);
if (res) {
fprintf(stderr, "Error: chmod %d\n", res);
return -1;
}
ports.VportFd[ports.portptr].fd = fd;
ports.VportFd[ports.portptr].events = POLLIN;
ports.p[ports.portptr].watch = inotify_add_watch(Inotify, fname, IN_OPEN|IN_CLOSE);
ports.portptr++;
return ports.portptr-1;
}
static int ConfigurePort(tCanFrame frame) {
struct __attribute__((__packed__)) {
uint16_t n;
uint8_t u[PORT_UUID_SIZE];
} resp;
// Generate packet id:
// port number + ID offset
int portid = PnGetNumber(frame.data);
resp.n = portid+PKT_ID_CTL_FILTER;
memcpy(resp.u, frame.data, PORT_UUID_SIZE);
// allocate virtual port for client
CanVport(portid);
CanSockSend(PKT_ID_SET, sizeof(resp), (uint8_t *)&resp);
return 0;
}
void *CanRxThread( void *ptr )
{
int i;
int ret;
tCanFrame frame;
uint8_t rxbuf[CAN_DATA_SIZE];
char ev_buf[EVENT_BUF_LEN];
while (threadexit==0) {
ret = poll(ports.VportFd, ports.portptr, 1000);
if (ret>0) { // one of fd's ready
if (ports.VportFd[0].revents) {
if(read(ports.VportFd[0].fd, &frame, sizeof(struct can_frame)) >= 0) {
// Configure port
if (frame.can_id == PKT_ID_UUID_RESP) {
if (ConfigurePort(frame) < 0) {
fprintf(stderr, "Aborting...\n");
break;
}
} else {
for(i=1; i<ports.portptr; i++) {
if (ports.p[i].canid == (frame.can_id - 1)) {
if (frame.can_dlc > 0 && ports.p[i].active)
write(ports.VportFd[i].fd, frame.data, frame.can_dlc);
// refresh channel activity
ports.p[i].pingcount = PINGS_BEFORE_DISCONNECT;
break;
}
}
if ( i == ports.portptr) {
printf("Packet id unknown %d\n", frame.can_id);
// Looks like lost hanshake, try to re-init
uint16_t txaddr = frame.can_id - 1;
CanSockSend(PKT_ID_UUID, 2, (uint8_t*) &(txaddr));
}
}
}
} else {
for(i=1; i<ports.portptr; i++) {
if (ports.VportFd[i].revents) {
ssize_t rl = read (ports.VportFd[i].fd, rxbuf, CAN_DATA_SIZE);
if(rl>0) {
for(int j=0;j<rl;j++) {
if(rxbuf[j] == 0x7E) // End of packet indicator
ports.p[i].active = 1; // Now we can send responses
}
//printf("Buf %d, %d\n", ports.p[i].canid, rl);
CanSockSend(ports.p[i].canid, rl, rxbuf);
}
}
}
}
}
ssize_t ev = read( Inotify, ev_buf, EVENT_BUF_LEN );
if ( ev > 0 ) {
for (char *p = ev_buf; p < ev_buf + ev; ) {
struct inotify_event *event = (struct inotify_event *) p;
for(i=1; i<ports.portptr; i++) {
if(ports.p[i].watch == event->wd) {
if ( event->mask & IN_OPEN ) {
printf("Open %d\n", ports.p[i].canid);
ports.p[i].active = 1;
// Send reset to MCU
CanSockSend(PKT_ID_UUID, 2, (uint8_t*) &(ports.p[i].canid));
} else if ( event->mask & IN_CLOSE ) {
printf("Close %d\n", ports.p[i].canid);
ports.p[i].active = 0;
}
break;
}
}
p += EVENT_SIZE + event->len;
}
}
}
printf("Close ports\n");
// close and delete fd's
for(i=0; i<ports.portptr; i++) {
CanVportClose(&(ports.p[i]));
}
}
void CanPing(void)
{
if(pingptr == 0) {
// request for new port assign
CanSockSend(PKT_ID_UUID, 0, NULL);
pingptr++;
return;
}
if(pingptr >= ports.portptr) {
pingptr=0;
} else {
// Check if we have packets from remote
if(ports.p[pingptr].pingcount == 0) {
// Unlink dead port
// ToDo: Dirty variant. Need to close /dev/pts first.
CanVportClose(&(ports.p[pingptr]));
for(int i= pingptr; i<ports.portptr-1; i++) {
memcpy(&(ports.p[pingptr]), &(ports.p[pingptr+1]), sizeof(tPortId));
memcpy(&(ports.VportFd[pingptr]), &(ports.VportFd[pingptr+1]), sizeof(struct pollfd));
}
ports.portptr--;
return;
}
ports.p[pingptr].pingcount --;
if(ports.p[pingptr].pingcount < 2) {
// To reduce bus load we'll send pings only if necessary
CanSockSend(ports.p[pingptr].canid, 0, NULL);
}
pingptr++;
}
}
int CanSockInit(void)
{
struct sockaddr_can addr;
struct ifreq ifr;
struct can_filter *rfilter;
int retval;
/* open socket */
if ((sock = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
return ENOTSOCK;
}
addr.can_family = AF_CAN;
strcpy(ifr.ifr_name, "can0");
if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
return SIOCGIFINDEX;
}
addr.can_ifindex = ifr.ifr_ifindex;
/* Set filters */
rfilter = malloc(sizeof(struct can_filter) * NUM_CAN_FILTERS);
if (!rfilter) {
return ENOMEM;
}
rfilter[0].can_id = PKT_ID_UUID_FILTER;
rfilter[0].can_mask = PKT_ID_UUID_MASK;
rfilter[1].can_id = PKT_ID_CTL_FILTER;
rfilter[1].can_mask = PKT_ID_CTL_MASK;
setsockopt(sock, SOL_CAN_RAW, CAN_RAW_FILTER,
rfilter, NUM_CAN_FILTERS * sizeof(struct can_filter));
free(rfilter);
/* set timeout */
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
return EIO;
}
// Allocate ports
ports.portsize = 8;
ports.portptr = 1; // skip one position for CAN socket
ports.p = malloc(sizeof(tPortId) * ports.portsize);
ports.VportFd = malloc(sizeof(struct pollfd) * ports.portsize);
memset(ports.VportFd, 0, sizeof(struct pollfd) * ports.portsize);
if (!ports.p || !ports.VportFd) {
fprintf(stderr, "malloc failed!\n");
return ENOMEM;
}
ports.VportFd[0].fd = sock;
ports.VportFd[0].events = POLLIN;
// Inotify for port open/close
Inotify = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
if (Inotify == -1) {
fprintf(stderr, "unable to create inotify fd\n");
return EINVAL;
}
// Create RX thread
if( (retval = pthread_create( &RxTh, NULL, CanRxThread, NULL)) > 0 )
{
return retval;
}
return 0;
}
void CanSockClose()
{
threadexit = 1;
close(sock);
pthread_join( RxTh, NULL);
}
int CanSockSend(canid_t id, uint8_t len, uint8_t* data)
{
tCanFrame frame;
if (len>8)
return EINVAL;
frame.can_id = id;
frame.can_dlc = len;
if (len)
memcpy(frame.data, data, len);
if (write(sock, &frame, sizeof(frame)) != sizeof(frame)) {
return EIO;
}
return 0;
}