iso14229 0.9.0
ISO14229-1 (UDS) C Library
Loading...
Searching...
No Matches
client.c
Go to the documentation of this file.
1/**
2 * @file examples/linux_rdbi_wdbi/client.c
3 * @brief UDS client demonstrating Read/Write Data By Identifier (0x22/0x2E)
4 */
5#include "iso14229.h"
6#include <stdint.h>
7#include <string.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <errno.h>
11#include <signal.h>
12#include <sys/time.h>
13#include <sys/types.h>
14#include <time.h>
15
16typedef struct {
17 enum {
18 Step_0_RDBI_Send,
19 Step_1_RDBI_Recv,
20 Step_2_WDBI_Send,
21 Step_3_WDBI_Recv,
22 Step_DONE,
23 } step;
24 UDSErr_t err;
25 uint16_t rdbi_f190;
27
28UDSErr_t fn(UDSClient_t *client, UDSEvent_t evt, void *ev_data) {
30 if (evt != UDS_EVT_Poll) {
31 UDS_LOGI(__FILE__, "%s (%d)", UDSEventToStr(evt), evt);
32 }
33 if (UDS_EVT_Err == evt) {
34 UDS_LOGE(__FILE__, "Exiting on step %d with error: %s", c->step,
35 UDSErrToStr(*(UDSErr_t *)ev_data));
36 c->err = *(UDSErr_t *)ev_data;
37 c->step = Step_DONE;
38 }
39 switch (c->step) {
40 case Step_0_RDBI_Send: {
41 const uint16_t dids[] = {0xf190};
42 c->err = UDSSendRDBI(client, dids, 1);
43 if (c->err) {
44 UDS_LOGE(__FILE__, "UDSSendRDBI failed with err: %d", c->err);
45 c->step = Step_DONE;
46 }
47 c->step = Step_1_RDBI_Recv;
48 break;
49 }
50 case Step_1_RDBI_Recv: {
51 UDSRDBIVar_t vars[] = {
52 {0xf190, 2, &(c->rdbi_f190), memmove},
53 };
54 if (UDS_EVT_ResponseReceived == evt) {
55 c->err = UDSUnpackRDBIResponse(client, vars, 1);
56 if (c->err) {
57 UDS_LOGE(__FILE__, "UDSUnpackRDBIResponse failed with err: %s",
58 UDSErrToStr(c->err));
59 c->step = Step_DONE;
60 }
61 UDS_LOGI(__FILE__, "0xf190 has value %d", c->rdbi_f190);
62 c->step = Step_2_WDBI_Send;
63 }
64 break;
65 }
66 case Step_2_WDBI_Send: {
67 uint16_t val = c->rdbi_f190 + 1;
68 uint8_t data[2] = {
69 (val & 0xff00) >> 8,
70 val & 0x00ff,
71 };
72 c->err = UDSSendWDBI(client, 0xf190, data, sizeof(data));
73 if (c->err) {
74 UDS_LOGE(__FILE__, "UDSSendWDBI failed with err: %s", UDSErrToStr(c->err));
75 c->step = Step_DONE;
76 }
77 c->step = Step_3_WDBI_Recv;
78 break;
79 }
80 case Step_3_WDBI_Recv: {
81 if (UDS_EVT_ResponseReceived == evt) {
82 UDS_LOGI(__FILE__, "WDBI response received");
83 c->step = Step_DONE;
84 }
85 default:
86 break;
87 }
88 }
89 return UDS_OK;
90}
91
92int main(int ac, char **av) {
93 UDSClient_t client;
95
96 if (UDSTpIsoTpSockInitClient(&tp, "vcan0", 0x7E8, 0x7E0, 0x7DF)) {
97 UDS_LOGE(__FILE__, "UDSTpIsoTpSockInitClient failed");
98 exit(-1);
99 }
100
101 if (UDSClientInit(&client)) {
102 exit(-1);
103 }
104
105 client.tp = (UDSTp_t *)&tp;
106 client.fn = fn;
107
108 SequenceContext_t ctx = {0};
109 client.fn_data = &ctx;
110
111 UDS_LOGI(__FILE__, "polling");
112 while (ctx.step != Step_DONE) {
113 UDSClientPoll(&client);
114 }
115
116 return ctx.err;
117}
ISO14229-1 (UDS) library.
UDSEvent_t
UDS events.
Definition iso14229.h:298
@ UDS_EVT_Poll
Definition iso14229.h:326
@ UDS_EVT_ResponseReceived
Definition iso14229.h:328
@ UDS_EVT_Err
Definition iso14229.h:299
UDS client structure.
Definition iso14229.h:729
int(* fn)(struct UDSClient *client, UDSEvent_t evt, void *ev_data)
Definition iso14229.h:741
void * fn_data
Definition iso14229.h:742
UDSTp_t * tp
Definition iso14229.h:732
Read data by identifier variable structure.
Definition iso14229.h:779
UDS Transport layer.
Definition iso14229.h:254