iso14229 0.9.0
ISO14229-1 (UDS) C Library
Loading...
Searching...
No Matches
server.c
Go to the documentation of this file.
1/**
2 * @file examples/linux_rdbi_wdbi/server.c
3 * @brief UDS server demonstrating Read/Write Data By Identifier (0x22/0x2E)
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;
16
17static bool done = false;
18static uint16_t val_0xf190 = 0;
19static uint16_t val_0xf191 = 0;
20
21void sigint_handler(int signum) {
22 printf("SIGINT received\n");
23 done = true;
24}
25
26static int fn(UDSServer_t *srv, UDSEvent_t ev, void *arg) {
27 switch (ev) {
29 UDSRDBIArgs_t *r = (UDSRDBIArgs_t *)arg;
30 printf("received RDBI %04x\n", r->dataId);
31 switch (r->dataId) {
32 case 0xf190: {
33 return r->copy(srv, &val_0xf190, sizeof(val_0xf190));
34 break;
35 }
36 case 0xf191: {
37 return r->copy(srv, &val_0xf191, sizeof(val_0xf191));
38 break;
39 }
40 default:
41 return UDS_NRC_RequestOutOfRange;
42 }
43 }
45 UDSWDBIArgs_t *r = (UDSWDBIArgs_t *)arg;
46 switch (r->dataId) {
47 case 0xf190: {
48 if (r->len != sizeof(val_0xf190)) {
49 return UDS_NRC_IncorrectMessageLengthOrInvalidFormat;
50 }
51 val_0xf190 = (r->data[0] << 8) + r->data[1];
52 return UDS_PositiveResponse;
53 }
54 default:
55 return UDS_NRC_RequestOutOfRange;
56 }
57 }
58 default:
59 printf("Unhandled event: %s\n", UDSEventToStr(ev));
60 return UDS_NRC_ServiceNotSupported;
61 }
62}
63
64static int sleep_ms(uint32_t tms) {
65 struct timespec ts;
66 int ret;
67 ts.tv_sec = tms / 1000;
68 ts.tv_nsec = (tms % 1000) * 1000000;
69 do {
70 ret = nanosleep(&ts, &ts);
71 } while (ret && errno == EINTR);
72 return ret;
73}
74
75int main(int ac, char **av) {
76 struct sigaction sa;
77 memset(&sa, 0, sizeof(sa));
78 sa.sa_handler = sigint_handler;
79 sigaction(SIGINT, &sa, NULL);
80
81 if (UDSTpIsoTpSockInitServer(&tp, "vcan0", 0x7E0, 0x7E8, 0x7DF)) {
82 fprintf(stderr, "UDSTpIsoTpSockInitServer failed\n");
83 exit(-1);
84 }
85
86 if (UDSServerInit(&srv)) {
87 fprintf(stderr, "UDSServerInit failed\n");
88 }
89
90 srv.tp = (UDSTp_t *)&tp;
91 srv.fn = fn;
92
93 printf("server up, polling . . .\n");
94 while (!done) {
95 UDSServerPoll(&srv);
96 sleep_ms(1);
97 }
98 printf("server exiting\n");
99 return 0;
100}
ISO14229-1 (UDS) library.
UDSEvent_t
UDS events.
Definition iso14229.h:298
@ UDS_EVT_WriteDataByIdent
Definition iso14229.h:310
@ UDS_EVT_ReadDataByIdent
Definition iso14229.h:305
Read data by identifier arguments.
Definition iso14229.h:978
uint8_t(* copy)(UDSServer_t *srv, const void *src, uint16_t count)
Definition iso14229.h:980
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
Write data by identifier arguments.
Definition iso14229.h:1026
const uint16_t len
Definition iso14229.h:1029
const uint8_t *const data
Definition iso14229.h:1028