Skip to content

Commit 5a51e07

Browse files
authored
Merge pull request #37 from boschglobal/feat/get-field
Improve GetField API (breaking changes)
2 parents e7fba30 + 6e87eff commit 5a51e07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3330
-1021
lines changed

CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ add_library(open1722 SHARED
1313
"src/avtp/Rvf.c"
1414
"src/avtp/Udp.c"
1515
"src/avtp/Utils.c"
16-
"src/avtp/aaf/CommonStream.c"
17-
"src/avtp/aaf/PcmStream.c"
16+
"src/avtp/aaf/Aaf.c"
17+
"src/avtp/aaf/Pcm.c"
1818
"src/avtp/acf/FlexRay.c"
1919
"src/avtp/acf/Gpc.c"
2020
"src/avtp/acf/Can.c"
2121
"src/avtp/acf/CanBrief.c"
2222
"src/avtp/acf/Lin.c"
2323
"src/avtp/acf/Most.c"
24-
"src/avtp/acf/Common.c"
24+
"src/avtp/acf/AcfCommon.c"
2525
"src/avtp/acf/Ntscf.c"
2626
"src/avtp/acf/Sensor.c"
2727
"src/avtp/acf/SensorBrief.c"

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,62 @@ To execute the IEEE 1722 CAN Talker application:
7676
```
7777
$ ./bin/acf-can-talker
7878
```
79+
80+
### De/Serialization IEEE 1722 PDUs
81+
82+
Here's a small example how the Open1722 library can be used to build and parse IEEE 1722 PDUs. First we define a C struct for a custom IEEE 1722 packet that can be used to transport a CAN, a LIN and a Flexray message. The frame begins with a Time-synchronous Control Format (TSCF) header. After the TSCF header a list of AVTP Control Format (ACF) messages follows. The first ACF message is a ACF CAN message which consists of ACF CAN header as well as a payload section to carry a 2Byte CAN frame. Similar than with the CAN message another ACF messages for LIN is added.
83+
84+
``` C
85+
// my_1722_pdu.h
86+
87+
#define CAN_PAYLOAD_LEN 2
88+
#define LIN_PAYLOAD_LEN 3
89+
90+
typedef struct {
91+
// IEEE 1722 UDP encapsulation header (optional)
92+
Avtp_Udp_t udp;
93+
// IEEE 1722 TSCF header
94+
Avtp_Tscf_t tscf;
95+
// IEEE 1722 ACF message #1
96+
Avtp_Can_t can;
97+
uint8_t canPayload[CAN_PAYLOAD_LEN];
98+
// IEEE 1722 ACF message #2
99+
Avtp_Lin_t lin;
100+
uint8_t linPayload[LIN_PAYLOAD_LEN];
101+
} My1722Pdu_t;
102+
```
103+
104+
In the next step we're going to c
105+
106+
``` C
107+
// talker.h
108+
109+
#include "my_1722_pdu.h"
110+
111+
int main()
112+
{
113+
My1722Pdu_t pdu;
114+
115+
// Init UDP encapsulation header
116+
Avtp_Udp_Init(&pdu.udp);
117+
118+
// Init TSCF header
119+
Avtp_Tscf_Init(&pdu.tscf);
120+
Avtp_Tscf_SetVersion(&pdu.tscf, 0);
121+
Avtp_Tscf_SetSequenceNum(&pdu.tscf, 12345);
122+
Avtp_Tscf_SetStreamId(&pdu.tscf, 0xAABBCCDDEEFF);
123+
Avtp_Tscf_SetTv(&pdu.tscf, 1);
124+
Avtp_Tscf_SetAvtpTimestamp(&pdu.tscf, 0x11223344);
125+
126+
// Init CAN ACF message
127+
Avtp_Can_Init(&pdu.can);
128+
Avtp_Can_SetCanBusId(&pdu.can, 4);
129+
uint8_t canFrame[CAN_PAYLOAD_LEN] = {0x11, 0x22};
130+
131+
// Init LIN ACF message
132+
Avtp_Lin_Init(&pdu.lin);
133+
uint8_t linFrame[LIN_PAYLOAD_LEN] = {0x11, 0x22, 0x33};
134+
135+
// Send packet to network
136+
}
137+
```

examples/aaf/aaf-listener.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
#include <unistd.h>
7070
#include <inttypes.h>
7171

72-
#include "avtp/aaf/PcmStream.h"
72+
#include "avtp/aaf/Pcm.h"
7373
#include "common/common.h"
7474
#include "avtp/CommonHeader.h"
7575

examples/aaf/aaf-talker.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
#include <string.h>
6565
#include <unistd.h>
6666

67-
#include "avtp/aaf/PcmStream.h"
67+
#include "avtp/aaf/Pcm.h"
6868
#include "common/common.h"
6969
#include "avtp/CommonHeader.h"
7070

examples/acf-can/acf-can-listener.c

+56-67
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include "avtp/Udp.h"
4646
#include "avtp/acf/Ntscf.h"
4747
#include "avtp/acf/Tscf.h"
48-
#include "avtp/acf/Common.h"
48+
#include "avtp/acf/AcfCommon.h"
4949
#include "avtp/acf/Can.h"
5050
#include "avtp/CommonHeader.h"
5151
#include "acf-can-common.h"
@@ -139,14 +139,13 @@ static error_t parser(int key, char *arg, struct argp_state *state)
139139

140140
static struct argp argp = { options, parser, args_doc, doc };
141141

142-
static int is_valid_acf_packet(uint8_t* acf_pdu) {
143-
144-
uint64_t val64;
145-
146-
Avtp_AcfCommon_GetField((Avtp_AcfCommon_t*)acf_pdu, AVTP_ACF_FIELD_ACF_MSG_TYPE, &val64);
147-
if (val64 != AVTP_ACF_TYPE_CAN) {
148-
fprintf(stderr, "ACF type mismatch: expected %u, got %lu\n",
149-
AVTP_ACF_TYPE_CAN, val64);
142+
static int is_valid_acf_packet(uint8_t* acf_pdu)
143+
{
144+
Avtp_AcfCommon_t *pdu = (Avtp_AcfCommon_t*) acf_pdu;
145+
uint8_t acf_msg_type = Avtp_AcfCommon_GetAcfMsgType(pdu);
146+
if (acf_msg_type != AVTP_ACF_TYPE_CAN) {
147+
fprintf(stderr, "ACF type mismatch: expected %"PRIu8", got %"PRIu8"\n",
148+
AVTP_ACF_TYPE_CAN, acf_msg_type);
150149
return 0;
151150
}
152151

@@ -155,71 +154,63 @@ static int is_valid_acf_packet(uint8_t* acf_pdu) {
155154

156155
void print_can_acf(uint8_t* acf_pdu)
157156
{
158-
uint64_t acf_msg_len, can_bus_id, timestamp, can_identifier, pad;
159-
160157
Avtp_Can_t *pdu = (Avtp_Can_t*) acf_pdu;
161-
162-
Avtp_Can_GetField(pdu, AVTP_CAN_FIELD_ACF_MSG_LENGTH, &acf_msg_len);
163-
Avtp_Can_GetField(pdu, AVTP_CAN_FIELD_CAN_BUS_ID, &can_bus_id);
164-
Avtp_Can_GetField(pdu, AVTP_CAN_FIELD_MESSAGE_TIMESTAMP, &timestamp);
165-
Avtp_Can_GetField(pdu, AVTP_CAN_FIELD_CAN_IDENTIFIER, &can_identifier);
166-
Avtp_Can_GetField(pdu, AVTP_CAN_FIELD_PAD, &pad);
158+
uint16_t acf_msg_len = Avtp_Can_GetAcfMsgLength(pdu);
159+
uint8_t can_bus_id = Avtp_Can_GetCanBusId(pdu);
160+
uint64_t timestamp = Avtp_Can_GetMessageTimestamp(pdu);
161+
uint32_t can_identifier = Avtp_Can_GetCanIdentifier(pdu);
162+
uint8_t pad = Avtp_Can_GetPad(pdu);
167163

168164
fprintf(stderr, "------------------------------------\n");
169-
fprintf(stderr, "Msg Length: %"PRIu64"\n", acf_msg_len);
170-
fprintf(stderr, "Can Bus ID: %"PRIu64"\n", can_bus_id);
171-
fprintf(stderr, "Timestamp: %#lx\n", timestamp);
172-
fprintf(stderr, "Can Identifier: %#lx\n", can_identifier);
173-
fprintf(stderr, "Pad: %"PRIu64"\n", pad);
165+
fprintf(stderr, "Msg Length: %"PRIu16"\n", acf_msg_len);
166+
fprintf(stderr, "Can Bus ID: %"PRIu8"\n", can_bus_id);
167+
fprintf(stderr, "Timestamp: %"PRIu64"", timestamp);
168+
fprintf(stderr, "Can Identifier: %"PRIu32"\n", can_identifier);
169+
fprintf(stderr, "Pad: %"PRIu8"\n", pad);
174170
}
175171

176172
static int new_packet(int sk_fd, int can_socket) {
177173

178-
int res;
179-
uint64_t msg_length, proc_bytes = 0, msg_proc_bytes = 0;
180-
uint64_t can_frame_id, udp_seq_num, subtype, flag;
181-
uint16_t payload_length, pdu_length;
174+
int res = 0;
175+
uint64_t proc_bytes = 0, msg_proc_bytes = 0;
176+
uint32_t udp_seq_num;
177+
uint16_t msg_length, can_payload_length, acf_msg_length;
178+
uint8_t subtype;
182179
uint8_t pdu[MAX_PDU_SIZE], i;
183180
uint8_t *cf_pdu, *acf_pdu, *udp_pdu, *can_payload;
184181
frame_t frame;
185182
canid_t can_id;
186183

187184
memset(&frame, 0, sizeof(struct canfd_frame));
188185
res = recv(sk_fd, pdu, MAX_PDU_SIZE, 0);
189-
190186
if (res < 0 || res > MAX_PDU_SIZE) {
191187
perror("Failed to receive data");
192188
return -1;
193189
}
194190

195191
if (use_udp) {
196192
udp_pdu = pdu;
197-
Avtp_Udp_GetField((Avtp_Udp_t *)udp_pdu, AVTP_UDP_FIELD_ENCAPSULATION_SEQ_NO, &udp_seq_num);
193+
udp_seq_num = Avtp_Udp_GetEncapsulationSeqNo((Avtp_Udp_t *)udp_pdu);
198194
cf_pdu = pdu + AVTP_UDP_HEADER_LEN;
199195
proc_bytes += AVTP_UDP_HEADER_LEN;
200196
} else {
201197
cf_pdu = pdu;
202198
}
203199

204-
res = Avtp_CommonHeader_GetField((Avtp_CommonHeader_t*)cf_pdu, AVTP_COMMON_HEADER_FIELD_SUBTYPE, &subtype);
205-
if (res < 0) {
206-
fprintf(stderr, "Failed to get subtype field: %d\n", res);
207-
return -1;
208-
}
209-
200+
subtype = Avtp_CommonHeader_GetSubtype((Avtp_CommonHeader_t*)cf_pdu);
210201
if (!((subtype == AVTP_SUBTYPE_NTSCF) ||
211202
(subtype == AVTP_SUBTYPE_TSCF))) {
212-
fprintf(stderr, "Subtype mismatch: expected %u or %u, got %"PRIu64". Dropping packet\n",
203+
fprintf(stderr, "Subtype mismatch: expected %u or %u, got %"PRIu8". Dropping packet\n",
213204
AVTP_SUBTYPE_NTSCF, AVTP_SUBTYPE_TSCF, subtype);
214205
return -1;
215206
}
216207

217-
if(subtype == AVTP_SUBTYPE_TSCF){
208+
if (subtype == AVTP_SUBTYPE_TSCF){
218209
proc_bytes += AVTP_TSCF_HEADER_LEN;
219-
Avtp_Tscf_GetField((Avtp_Tscf_t*)cf_pdu, AVTP_TSCF_FIELD_STREAM_DATA_LENGTH, (uint64_t *) &msg_length);
220-
}else{
210+
msg_length = Avtp_Tscf_GetStreamDataLength((Avtp_Tscf_t*)cf_pdu);
211+
} else {
221212
proc_bytes += AVTP_NTSCF_HEADER_LEN;
222-
Avtp_Ntscf_GetField((Avtp_Ntscf_t*)cf_pdu, AVTP_NTSCF_FIELD_NTSCF_DATA_LENGTH, (uint64_t *) &msg_length);
213+
msg_length = Avtp_Ntscf_GetNtscfDataLength((Avtp_Ntscf_t*)cf_pdu);
223214
}
224215

225216
while (msg_proc_bytes < msg_length) {
@@ -231,46 +222,44 @@ static int new_packet(int sk_fd, int can_socket) {
231222
return -1;
232223
}
233224

234-
Avtp_Can_GetField((Avtp_Can_t*)acf_pdu, AVTP_CAN_FIELD_CAN_IDENTIFIER,
235-
&(can_frame_id));
236-
can_id = can_frame_id;
225+
can_id = Avtp_Can_GetCanIdentifier((Avtp_Can_t*)acf_pdu);
237226

238-
can_payload = Avtp_Can_GetPayload((Avtp_Can_t*)acf_pdu, &payload_length, &pdu_length);
239-
msg_proc_bytes += pdu_length*4;
227+
can_payload = Avtp_Can_GetPayload((Avtp_Can_t*)acf_pdu);
228+
acf_msg_length = Avtp_Can_GetAcfMsgLength((Avtp_Can_t*)acf_pdu)*4;
229+
can_payload_length = Avtp_Can_GetCanPayloadLength((Avtp_Can_t*)acf_pdu);
230+
msg_proc_bytes += acf_msg_length;
240231

241232
// Handle EFF Flag
242-
Avtp_Can_GetField((Avtp_Can_t*)acf_pdu, AVTP_CAN_FIELD_EFF, &flag);
243-
if (can_id > 0x7FF && !flag) {
244-
fprintf(stderr, "Error: CAN ID is > 0x7FF but the EFF bit is not set.\n");
245-
return -1;
233+
if (Avtp_Can_GetEff((Avtp_Can_t*)acf_pdu)) {
234+
can_id |= CAN_EFF_FLAG;
235+
} else if (can_id > 0x7FF) {
236+
fprintf(stderr, "Error: CAN ID is > 0x7FF but the EFF bit is not set.\n");
237+
return -1;
246238
}
247-
if (flag) can_id |= CAN_EFF_FLAG;
248239

249240
// Handle RTR Flag
250-
Avtp_Can_GetField((Avtp_Can_t*)acf_pdu, AVTP_CAN_FIELD_RTR, &flag);
251-
if (flag) can_id |= CAN_RTR_FLAG;
241+
if (Avtp_Can_GetRtr((Avtp_Can_t*)acf_pdu)) {
242+
can_id |= CAN_RTR_FLAG;
243+
}
252244

253245
if (can_variant == AVTP_CAN_FD) {
254-
255-
Avtp_Can_GetField((Avtp_Can_t*)acf_pdu, AVTP_CAN_FIELD_BRS, &flag);
256-
if (flag) frame.fd.flags |= CANFD_BRS;
257-
258-
Avtp_Can_GetField((Avtp_Can_t*)acf_pdu, AVTP_CAN_FIELD_FDF, &flag);
259-
if (flag) frame.fd.flags |= CANFD_FDF;
260-
261-
Avtp_Can_GetField((Avtp_Can_t*)acf_pdu, AVTP_CAN_FIELD_ESI, &flag);
262-
if (flag) frame.fd.flags |= CANFD_ESI;
263-
246+
if (Avtp_Can_GetBrs((Avtp_Can_t*)acf_pdu)) {
247+
frame.fd.flags |= CANFD_BRS;
248+
}
249+
if (Avtp_Can_GetFdf((Avtp_Can_t*)acf_pdu)) {
250+
frame.fd.flags |= CANFD_FDF;
251+
}
252+
if (Avtp_Can_GetEsi((Avtp_Can_t*)acf_pdu)) {
253+
frame.fd.flags |= CANFD_ESI;
254+
}
264255
frame.fd.can_id = can_id;
265-
frame.fd.len = payload_length;
266-
memcpy(frame.fd.data, can_payload, payload_length);
256+
frame.fd.len = can_payload_length;
257+
memcpy(frame.fd.data, can_payload, can_payload_length);
267258
res = write(can_socket, &frame.fd, sizeof(struct canfd_frame));
268-
269259
} else {
270-
271260
frame.cc.can_id = can_id;
272-
frame.cc.len = payload_length;
273-
memcpy(frame.cc.data, can_payload, payload_length);
261+
frame.cc.len = can_payload_length;
262+
memcpy(frame.cc.data, can_payload, can_payload_length);
274263
res = write(can_socket, &frame.cc, sizeof(struct can_frame));
275264
}
276265

examples/acf-can/acf-can-talker.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ static int prepare_acf_packet(uint8_t* acf_pdu,
228228

229229
// Copy payload to ACF CAN PDU
230230
if(can_variant == AVTP_CAN_FD)
231-
processedBytes = Avtp_Can_SetPayload(pdu, frame.fd.can_id & CAN_EFF_MASK, frame.fd.data,
231+
Avtp_Can_CreateAcfMessage(pdu, frame.fd.can_id & CAN_EFF_MASK, frame.fd.data,
232232
frame.fd.len, can_variant);
233233
else
234-
processedBytes = Avtp_Can_SetPayload(pdu, frame.cc.can_id & CAN_EFF_MASK, frame.cc.data,
234+
Avtp_Can_CreateAcfMessage(pdu, frame.cc.can_id & CAN_EFF_MASK, frame.cc.data,
235235
frame.cc.len, can_variant);
236236

237-
return processedBytes;
237+
return Avtp_Can_GetAcfMsgLength(pdu)*4;
238238
}
239239

240240
int main(int argc, char *argv[])

examples/crf/crf-listener.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
#include <inttypes.h>
101101

102102
#include "avtp/Crf.h"
103-
#include "avtp/aaf/PcmStream.h"
103+
#include "avtp/aaf/Pcm.h"
104104
#include "common/common.h"
105105
#include "avtp/CommonHeader.h"
106106

0 commit comments

Comments
 (0)