iso14229 0.9.0
ISO14229-1 (UDS) C Library
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1/**
2 * @file examples/linux_server/main.c
3 * @brief Basic Linux UDS server example using socketcan ISO-TP
4 */
5#include "iso14229.h"
6#include <stdio.h>
7#include <stdlib.h>
8#include <errno.h>
9#include <signal.h>
10#include <sys/time.h>
11#include <sys/types.h>
12#include <time.h>
13
14static UDSServer_t srv;
15static UDSTpIsoTpSock_t tp;
16static bool done = false;
17static int sleep_ms(uint32_t tms);
18
19void sigint_handler(int signum) {
20 printf("SIGINT received\n");
21 done = true;
22}
23
24static UDSErr_t fn(UDSServer_t *srv, UDSEvent_t ev, void *arg) {
25 switch (ev) {
26 default:
27 printf("Unhandled event: %d\n", ev);
28 return UDS_NRC_ServiceNotSupported;
29 }
30}
31
32int main(int ac, char **av) {
33 struct sigaction sa;
34 memset(&sa, 0, sizeof(sa));
35 sa.sa_handler = sigint_handler;
36 sigaction(SIGINT, &sa, NULL);
37
38 // 1. Initialize a transport
39 if (UDSTpIsoTpSockInitServer(&tp, "vcan0", 0x7E0, 0x7E8, 0x7DF)) {
40 fprintf(stderr, "UDSTpIsoTpSockInitServer failed\n");
41 exit(-1);
42 }
43
44 if (UDSServerInit(&srv)) {
45 fprintf(stderr, "UDSServerInit failed\n");
46 }
47
48 srv.tp = (UDSTp_t *)&tp;
49 srv.fn = fn;
50
51 printf("server up, polling . . .\n");
52 while (!done) {
53 UDSServerPoll(&srv);
54 sleep_ms(1);
55 }
56 printf("server exiting\n");
57 return 0;
58}
59
60static int sleep_ms(uint32_t tms) {
61 struct timespec ts;
62 int ret;
63 ts.tv_sec = tms / 1000;
64 ts.tv_nsec = (tms % 1000) * 1000000;
65 do {
66 ret = nanosleep(&ts, &ts);
67 } while (ret && errno == EINTR);
68 return ret;
69}
ISO14229-1 (UDS) library.
UDSEvent_t
UDS events.
Definition iso14229.h:298
UDS server structure.
Definition iso14229.h:850
UDSErr_t(* fn)(struct UDSServer *srv, UDSEvent_t event, void *arg)
Definition iso14229.h:852
UDSTp_t * tp
Definition iso14229.h:851
UDS Transport layer.
Definition iso14229.h:254