-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathCO_epoll_interface.c
623 lines (547 loc) · 21.2 KB
/
CO_epoll_interface.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* Helper functions for Linux epoll interface to CANopenNode.
*
* @file CO_epoll_interface.c
* @author Janez Paternoster
* @author Martin Wagner
* @copyright 2004 - 2015 Janez Paternoster
* @copyright 2018 - 2020 Neuberger Gebaeudeautomation GmbH
*
*
* This file is part of <https://github.com/CANopenNode/CANopenNode>, a CANopen Stack.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
/* following macro is necessary for accept4() function call (sockets) */
#define _GNU_SOURCE
#include "CO_epoll_interface.h"
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <time.h>
#include <fcntl.h>
#if (CO_CONFIG_GTW) & CO_CONFIG_GTW_ASCII
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <signal.h>
#ifndef LISTEN_BACKLOG
#define LISTEN_BACKLOG 50
#endif
#endif /* (CO_CONFIG_GTW) & CO_CONFIG_GTW_ASCII */
/* delay for recall CANsend(), if CAN TX buffer is full */
#ifndef CANSEND_DELAY_US
#define CANSEND_DELAY_US 100
#endif
/* EPOLL **********************************************************************/
/* Helper function - get monotonic clock time in microseconds */
static inline uint64_t
clock_gettime_us(void) {
struct timespec ts;
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
}
CO_ReturnError_t
CO_epoll_create(CO_epoll_t* ep, uint32_t timerInterval_us) {
int ret;
struct epoll_event ev = {0};
if (ep == NULL) {
return CO_ERROR_ILLEGAL_ARGUMENT;
}
/* Configure epoll for mainline */
ep->epoll_new = false;
ep->epoll_fd = epoll_create(1);
if (ep->epoll_fd < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_create()");
return CO_ERROR_SYSCALL;
}
/* Configure eventfd for notifications and add it to epoll */
ep->event_fd = eventfd(0, EFD_NONBLOCK);
if (ep->event_fd < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "eventfd()");
return CO_ERROR_SYSCALL;
}
ev.events = EPOLLIN;
ev.data.fd = ep->event_fd;
ret = epoll_ctl(ep->epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(event_fd)");
return CO_ERROR_SYSCALL;
}
/* Configure timer for timerInterval_us and add it to epoll */
ep->timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
if (ep->timer_fd < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "timerfd_create()");
return CO_ERROR_SYSCALL;
}
ep->tm.it_interval.tv_sec = timerInterval_us / 1000000;
ep->tm.it_interval.tv_nsec = (timerInterval_us % 1000000) * 1000;
ep->tm.it_value.tv_sec = 0;
ep->tm.it_value.tv_nsec = 1;
ret = timerfd_settime(ep->timer_fd, 0, &ep->tm, NULL);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "timerfd_settime");
return CO_ERROR_SYSCALL;
}
ev.events = EPOLLIN;
ev.data.fd = ep->timer_fd;
ret = epoll_ctl(ep->epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(timer_fd)");
return CO_ERROR_SYSCALL;
}
ep->timerInterval_us = timerInterval_us;
ep->previousTime_us = clock_gettime_us();
ep->timeDifference_us = 0;
return CO_ERROR_NO;
}
void
CO_epoll_close(CO_epoll_t* ep) {
if (ep == NULL) {
return;
}
close(ep->epoll_fd);
ep->epoll_fd = -1;
close(ep->event_fd);
ep->event_fd = -1;
close(ep->timer_fd);
ep->timer_fd = -1;
}
void
CO_epoll_wait(CO_epoll_t* ep) {
if (ep == NULL) {
return;
}
/* wait for an event */
int ready = epoll_wait(ep->epoll_fd, &ep->ev, 1, -1);
ep->epoll_new = true;
ep->timerEvent = false;
/* calculate time difference since last call */
uint64_t now = clock_gettime_us();
ep->timeDifference_us = (uint32_t)(now - ep->previousTime_us);
ep->previousTime_us = now;
/* application may will lower this */
ep->timerNext_us = ep->timerInterval_us;
/* process event */
if (ready != 1 && errno == EINTR) {
/* event from interrupt or signal, nothing to process, continue */
ep->epoll_new = false;
} else if (ready != 1) {
log_printf(LOG_DEBUG, DBG_ERRNO, "epoll_wait");
ep->epoll_new = false;
} else if ((ep->ev.events & EPOLLIN) != 0 && ep->ev.data.fd == ep->event_fd) {
uint64_t val;
ssize_t s = read(ep->event_fd, &val, sizeof(uint64_t));
if (s != sizeof(uint64_t)) {
log_printf(LOG_DEBUG, DBG_ERRNO, "read(event_fd)");
}
ep->epoll_new = false;
} else if ((ep->ev.events & EPOLLIN) != 0 && ep->ev.data.fd == ep->timer_fd) {
uint64_t val;
ssize_t s = read(ep->timer_fd, &val, sizeof(uint64_t));
if (s != sizeof(uint64_t) && errno != EAGAIN) {
log_printf(LOG_DEBUG, DBG_ERRNO, "read(timer_fd)");
}
ep->epoll_new = false;
ep->timerEvent = true;
}
}
void
CO_epoll_processLast(CO_epoll_t* ep) {
if (ep == NULL) {
return;
}
if (ep->epoll_new) {
log_printf(LOG_DEBUG, DBG_EPOLL_UNKNOWN, ep->ev.events, ep->ev.data.fd);
ep->epoll_new = false;
}
/* lower next timer interval if changed by application */
if (ep->timerNext_us < ep->timerInterval_us) {
/* add one microsecond extra delay and make sure it is not zero */
ep->timerNext_us += 1;
if (ep->timerInterval_us < 1000000) {
ep->tm.it_value.tv_nsec = ep->timerNext_us * 1000;
} else {
ep->tm.it_value.tv_sec = ep->timerNext_us / 1000000;
ep->tm.it_value.tv_nsec = (ep->timerNext_us % 1000000) * 1000;
}
int ret = timerfd_settime(ep->timer_fd, 0, &ep->tm, NULL);
if (ret < 0) {
log_printf(LOG_DEBUG, DBG_ERRNO, "timerfd_settime");
}
}
}
/* MAINLINE *******************************************************************/
#ifndef CO_SINGLE_THREAD
/* Send event to wake CO_epoll_processMain() */
static void
wakeupCallback(void* object) {
CO_epoll_t* ep = (CO_epoll_t*)object;
uint64_t u = 1;
ssize_t s;
s = write(ep->event_fd, &u, sizeof(uint64_t));
if (s != sizeof(uint64_t)) {
log_printf(LOG_DEBUG, DBG_ERRNO, "write()");
}
}
#endif
void
CO_epoll_initCANopenMain(CO_epoll_t* ep, CO_t* co) {
if (ep == NULL || co == NULL) {
return;
}
#ifndef CO_SINGLE_THREAD
/* Configure LSS slave callback function */
#if (CO_CONFIG_LSS) & CO_CONFIG_FLAG_CALLBACK_PRE
#if (CO_CONFIG_LSS) & CO_CONFIG_LSS_SLAVE
CO_LSSslave_initCallbackPre(co->LSSslave, (void*)ep, wakeupCallback);
#endif
#endif
if (co->nodeIdUnconfigured) {
return;
}
/* Configure callback functions */
#if (CO_CONFIG_NMT) & CO_CONFIG_FLAG_CALLBACK_PRE
CO_NMT_initCallbackPre(co->NMT, (void*)ep, wakeupCallback);
#endif
#if (CO_CONFIG_HB_CONS) & CO_CONFIG_FLAG_CALLBACK_PRE
CO_HBconsumer_initCallbackPre(co->HBcons, (void*)ep, wakeupCallback);
#endif
#if (CO_CONFIG_EM) & CO_CONFIG_FLAG_CALLBACK_PRE
CO_EM_initCallbackPre(co->em, (void*)ep, wakeupCallback);
#endif
#if (CO_CONFIG_SDO_SRV) & CO_CONFIG_FLAG_CALLBACK_PRE
CO_SDOserver_initCallbackPre(&co->SDOserver[0], (void*)ep, wakeupCallback);
#endif
#if (CO_CONFIG_SDO_CLI) & CO_CONFIG_FLAG_CALLBACK_PRE
CO_SDOclient_initCallbackPre(&co->SDOclient[0], (void*)ep, wakeupCallback);
#endif
#if (CO_CONFIG_TIME) & CO_CONFIG_FLAG_CALLBACK_PRE
CO_TIME_initCallbackPre(co->TIME, (void*)ep, wakeupCallback);
#endif
#if (CO_CONFIG_LSS) & CO_CONFIG_FLAG_CALLBACK_PRE
#if (CO_CONFIG_LSS) & CO_CONFIG_LSS_MASTER
CO_LSSmaster_initCallbackPre(co->LSSmaster, (void*)ep, wakeupCallback);
#endif
#endif
#endif /* CO_SINGLE_THREAD */
}
void
CO_epoll_processMain(CO_epoll_t* ep, CO_t* co, bool_t enableGateway, CO_NMT_reset_cmd_t* reset) {
if (ep == NULL || co == NULL || reset == NULL) {
return;
}
/* process CANopen objects */
*reset = CO_process(co, enableGateway, ep->timeDifference_us, &ep->timerNext_us);
/* If there are unsent CAN messages, call CO_CANmodule_process() earlier */
if (co->CANmodule->CANtxCount > 0 && ep->timerNext_us > CANSEND_DELAY_US) {
ep->timerNext_us = CANSEND_DELAY_US;
}
}
/* CANrx and REALTIME *********************************************************/
void
CO_epoll_processRT(CO_epoll_t* ep, CO_t* co, bool_t realtime) {
if (co == NULL || ep == NULL) {
return;
}
/* Verify for epoll events */
if (ep->epoll_new) {
if (CO_CANrxFromEpoll(co->CANmodule, &ep->ev, NULL, NULL)) {
ep->epoll_new = false;
}
}
if (!realtime || ep->timerEvent) {
uint32_t* pTimerNext_us = realtime ? NULL : &ep->timerNext_us;
CO_LOCK_OD(co->CANmodule);
if (!co->nodeIdUnconfigured && co->CANmodule->CANnormal) {
bool_t syncWas = false;
#if (CO_CONFIG_SYNC) & CO_CONFIG_SYNC_ENABLE
syncWas = CO_process_SYNC(co, ep->timeDifference_us, pTimerNext_us);
#endif
#if (CO_CONFIG_PDO) & CO_CONFIG_RPDO_ENABLE
CO_process_RPDO(co, syncWas, ep->timeDifference_us, pTimerNext_us);
#endif
#if (CO_CONFIG_PDO) & CO_CONFIG_TPDO_ENABLE
CO_process_TPDO(co, syncWas, ep->timeDifference_us, pTimerNext_us);
#endif
(void)syncWas;
(void)pTimerNext_us;
}
CO_UNLOCK_OD(co->CANmodule);
}
}
/* GATEWAY ********************************************************************/
#if (CO_CONFIG_GTW) & CO_CONFIG_GTW_ASCII
/* write response string from gateway-ascii object */
static size_t
gtwa_write_response(void* object, const char* buf, size_t count, uint8_t* connectionOK) {
int* fd = (int*)object;
/* nWritten = count -> in case of error (non-existing fd) data are purged */
size_t nWritten = count;
if (fd != NULL && *fd >= 0) {
ssize_t n = write(*fd, (const void*)buf, count);
if (n >= 0) {
nWritten = (size_t)n;
} else {
/* probably EAGAIN - "Resource temporarily unavailable". Retry. */
log_printf(LOG_DEBUG, DBG_ERRNO, "write(gtwa_response)");
nWritten = 0;
}
} else {
*connectionOK = 0;
}
return nWritten;
}
static inline void
socketAcceptEnableForEpoll(CO_epoll_gtw_t* epGtw) {
struct epoll_event ev = {0};
int ret;
ev.events = EPOLLIN | EPOLLONESHOT;
ev.data.fd = epGtw->gtwa_fdSocket;
ret = epoll_ctl(epGtw->epoll_fd, EPOLL_CTL_MOD, ev.data.fd, &ev);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(gtwa_fdSocket)");
}
}
CO_ReturnError_t
CO_epoll_createGtw(CO_epoll_gtw_t* epGtw, int epoll_fd, int32_t commandInterface, uint32_t socketTimeout_ms,
char* localSocketPath) {
int ret;
struct epoll_event ev = {0};
if (epGtw == NULL || epoll_fd < 0) {
return CO_ERROR_ILLEGAL_ARGUMENT;
}
epGtw->epoll_fd = epoll_fd;
epGtw->commandInterface = commandInterface;
epGtw->socketTimeout_us = (socketTimeout_ms < (UINT_MAX / 1000 - 1000000)) ? socketTimeout_ms * 1000
: (UINT_MAX - 1000000);
epGtw->gtwa_fdSocket = -1;
epGtw->gtwa_fd = -1;
if (commandInterface == CO_COMMAND_IF_STDIO) {
epGtw->gtwa_fd = STDIN_FILENO;
log_printf(LOG_INFO, DBG_COMMAND_STDIO_INFO);
} else if (commandInterface == CO_COMMAND_IF_LOCAL_SOCKET) {
struct sockaddr_un addr;
epGtw->localSocketPath = localSocketPath;
/* Create, bind and listen local socket */
epGtw->gtwa_fdSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (epGtw->gtwa_fdSocket < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "socket(local)");
return CO_ERROR_SYSCALL;
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, localSocketPath, sizeof(addr.sun_path) - 1);
ret = bind(epGtw->gtwa_fdSocket, (struct sockaddr*)&addr, sizeof(struct sockaddr_un));
if (ret < 0) {
log_printf(LOG_CRIT, DBG_COMMAND_LOCAL_BIND, localSocketPath);
return CO_ERROR_SYSCALL;
}
ret = listen(epGtw->gtwa_fdSocket, LISTEN_BACKLOG);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "listen(local)");
return CO_ERROR_SYSCALL;
}
/* Ignore the SIGPIPE signal, which may happen, if remote client broke
* the connection. Signal may be triggered by write call inside
* gtwa_write_response() function */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
log_printf(LOG_CRIT, DBG_ERRNO, "signal");
return CO_ERROR_SYSCALL;
}
log_printf(LOG_INFO, DBG_COMMAND_LOCAL_INFO, localSocketPath);
} else if (commandInterface >= CO_COMMAND_IF_TCP_SOCKET_MIN && commandInterface <= CO_COMMAND_IF_TCP_SOCKET_MAX) {
struct sockaddr_in addr;
const int yes = 1;
/* Create, bind and listen socket */
epGtw->gtwa_fdSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (epGtw->gtwa_fdSocket < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "socket(tcp)");
return CO_ERROR_SYSCALL;
}
setsockopt(epGtw->gtwa_fdSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(commandInterface);
addr.sin_addr.s_addr = INADDR_ANY;
ret = bind(epGtw->gtwa_fdSocket, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));
if (ret < 0) {
log_printf(LOG_CRIT, DBG_COMMAND_TCP_BIND, commandInterface);
return CO_ERROR_SYSCALL;
}
ret = listen(epGtw->gtwa_fdSocket, LISTEN_BACKLOG);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "listen(tcp)");
return CO_ERROR_SYSCALL;
}
/* Ignore the SIGPIPE signal, which may happen, if remote client broke
* the connection. Signal may be triggered by write call inside
* gtwa_write_response() function */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
log_printf(LOG_CRIT, DBG_ERRNO, "signal");
return CO_ERROR_SYSCALL;
}
log_printf(LOG_INFO, DBG_COMMAND_TCP_INFO, commandInterface);
} else {
epGtw->commandInterface = CO_COMMAND_IF_DISABLED;
}
if (epGtw->gtwa_fd >= 0) {
ev.events = EPOLLIN;
ev.data.fd = epGtw->gtwa_fd;
ret = epoll_ctl(epGtw->epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(gtwa_fd)");
return CO_ERROR_SYSCALL;
}
}
if (epGtw->gtwa_fdSocket >= 0) {
/* prepare epoll for listening for new socket connection. After
* connection will be accepted, fd for io operation will be defined. */
ev.events = EPOLLIN | EPOLLONESHOT;
ev.data.fd = epGtw->gtwa_fdSocket;
ret = epoll_ctl(epGtw->epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(gtwa_fdSocket)");
return CO_ERROR_SYSCALL;
}
}
return CO_ERROR_NO;
}
void
CO_epoll_closeGtw(CO_epoll_gtw_t* epGtw) {
if (epGtw == NULL) {
return;
}
if (epGtw->commandInterface == CO_COMMAND_IF_LOCAL_SOCKET) {
if (epGtw->gtwa_fd > 0) {
close(epGtw->gtwa_fd);
}
close(epGtw->gtwa_fdSocket);
/* Remove local socket file from filesystem. */
if (remove(epGtw->localSocketPath) < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "remove(local)");
}
} else if (epGtw->commandInterface >= CO_COMMAND_IF_TCP_SOCKET_MIN) {
if (epGtw->gtwa_fd > 0) {
close(epGtw->gtwa_fd);
}
close(epGtw->gtwa_fdSocket);
}
epGtw->gtwa_fd = -1;
epGtw->gtwa_fdSocket = -1;
}
void
CO_epoll_initCANopenGtw(CO_epoll_gtw_t* epGtw, CO_t* co) {
if (epGtw == NULL || co == NULL || co->nodeIdUnconfigured) {
return;
}
CO_GTWA_initRead(co->gtwa, gtwa_write_response, (void*)&epGtw->gtwa_fd);
epGtw->freshCommand = true;
}
void
CO_epoll_processGtw(CO_epoll_gtw_t* epGtw, CO_t* co, CO_epoll_t* ep) {
if (epGtw == NULL || co == NULL || ep == NULL) {
return;
}
/* Verify for epoll events */
if (ep->epoll_new && (ep->ev.data.fd == epGtw->gtwa_fdSocket || ep->ev.data.fd == epGtw->gtwa_fd)) {
if ((ep->ev.events & EPOLLIN) != 0 && ep->ev.data.fd == epGtw->gtwa_fdSocket) {
bool_t fail = false;
epGtw->gtwa_fd = accept4(epGtw->gtwa_fdSocket, NULL, NULL, SOCK_NONBLOCK);
if (epGtw->gtwa_fd < 0) {
fail = true;
if (errno != EAGAIN && errno != EWOULDBLOCK) {
log_printf(LOG_CRIT, DBG_ERRNO, "accept(gtwa_fdSocket)");
}
} else {
/* add fd to epoll */
struct epoll_event ev2 = {0};
ev2.events = EPOLLIN;
ev2.data.fd = epGtw->gtwa_fd;
int ret = epoll_ctl(ep->epoll_fd, EPOLL_CTL_ADD, ev2.data.fd, &ev2);
if (ret < 0) {
fail = true;
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(add, gtwa_fd)");
}
epGtw->socketTimeoutTmr_us = 0;
}
if (fail) {
socketAcceptEnableForEpoll(epGtw);
}
ep->epoll_new = false;
} else if ((ep->ev.events & EPOLLIN) != 0 && ep->ev.data.fd == epGtw->gtwa_fd) {
char buf[CO_CONFIG_GTWA_COMM_BUF_SIZE];
size_t space = co->nodeIdUnconfigured ? CO_CONFIG_GTWA_COMM_BUF_SIZE : CO_GTWA_write_getSpace(co->gtwa);
ssize_t s = read(epGtw->gtwa_fd, buf, space);
if (space == 0 || co->nodeIdUnconfigured) {
/* continue or purge data */
} else if (s < 0 && errno != EAGAIN) {
log_printf(LOG_DEBUG, DBG_ERRNO, "read(gtwa_fd)");
} else if (s >= 0) {
if (epGtw->commandInterface == CO_COMMAND_IF_STDIO) {
/* simplify command interface on stdio, make hard to type
* sequence optional, prepend "[0] " to string, if missing */
const char sequence[] = "[0] ";
bool_t closed = (buf[s - 1] == '\n'); /* is command closed? */
if (buf[0] != '[' && (space - s) >= strlen(sequence) && isgraph(buf[0]) && buf[0] != '#' && closed
&& epGtw->freshCommand) {
CO_GTWA_write(co->gtwa, sequence, strlen(sequence));
}
epGtw->freshCommand = closed;
CO_GTWA_write(co->gtwa, buf, s);
} else { /* socket, local or tcp */
if (s == 0) {
/* EOF received, close connection and enable socket
* accepting */
int ret = epoll_ctl(ep->epoll_fd, EPOLL_CTL_DEL, epGtw->gtwa_fd, NULL);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(del, gtwa_fd)");
}
if (close(epGtw->gtwa_fd) < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "close(gtwa_fd)");
}
epGtw->gtwa_fd = -1;
socketAcceptEnableForEpoll(epGtw);
} else {
CO_GTWA_write(co->gtwa, buf, s);
}
}
}
epGtw->socketTimeoutTmr_us = 0;
ep->epoll_new = false;
} else if ((ep->ev.events & (EPOLLERR | EPOLLHUP)) != 0) {
log_printf(LOG_DEBUG, DBG_GENERAL, "socket error or hangup, event=", ep->ev.events);
if (close(epGtw->gtwa_fd) < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "close(gtwa_fd, hangup)");
}
}
} /* if (ep->epoll_new) */
/* if socket connection is established, verify timeout */
if (epGtw->socketTimeout_us > 0 && epGtw->gtwa_fdSocket > 0 && epGtw->gtwa_fd > 0) {
if (epGtw->socketTimeoutTmr_us > epGtw->socketTimeout_us) {
/* timout expired, close current connection and accept next */
int ret = epoll_ctl(ep->epoll_fd, EPOLL_CTL_DEL, epGtw->gtwa_fd, NULL);
if (ret < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "epoll_ctl(del, gtwa_fd), tmo");
}
if (close(epGtw->gtwa_fd) < 0) {
log_printf(LOG_CRIT, DBG_ERRNO, "close(gtwa_fd), tmo");
}
epGtw->gtwa_fd = -1;
socketAcceptEnableForEpoll(epGtw);
} else {
epGtw->socketTimeoutTmr_us += ep->timeDifference_us;
}
}
}
#endif /* (CO_CONFIG_GTW) & CO_CONFIG_GTW_ASCII */