Skip to content

Commit 129af4f

Browse files
SebastianSchildtnayakned
authored andcommitted
Use explicit bool setters, removing one useless indirection for setting CAN fields
Signed-off-by: Sebastian Schildt <sebastian.schildt@de.bosch.com>
1 parent 38f800f commit 129af4f

File tree

3 files changed

+98
-70
lines changed

3 files changed

+98
-70
lines changed

examples/acf-can/acf-can-common.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ static int prepare_acf_packet(uint8_t* acf_pdu,
159159
// Prepare ACF PDU for CAN
160160
Avtp_Can_Init(pdu);
161161
clock_gettime(CLOCK_REALTIME, &now);
162-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_MESSAGE_TIMESTAMP,
163-
(uint64_t)now.tv_nsec + (uint64_t)(now.tv_sec * 1e9));
164-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_MTV, 1U);
162+
Avtp_Can_SetMessageTimestamp(pdu, (uint64_t)now.tv_nsec + (uint64_t)(now.tv_sec * 1e9));
163+
Avtp_Can_EnableMtv(pdu);
165164

166165
// Set required CAN Flags
167166
#ifdef __linux__
@@ -171,13 +170,23 @@ static int prepare_acf_packet(uint8_t* acf_pdu,
171170
can_id = (can_variant == AVTP_CAN_FD) ? (*frame).fd.id : (*frame).cc.id;
172171
can_payload_length = (can_variant == AVTP_CAN_FD) ? (*frame).fd.dlc : (*frame).cc.dlc;
173172
#endif
174-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_RTR, can_id & CAN_RTR_FLAG);
175-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_EFF, can_id & CAN_EFF_FLAG);
173+
if (can_id & CAN_EFF_FLAG) {
174+
Avtp_Can_EnableEff(pdu);
175+
}
176+
if (can_id & CAN_RTR_FLAG) {
177+
Avtp_Can_EnableRtr(pdu);
178+
}
176179

177180
if (can_variant == AVTP_CAN_FD) {
178-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_BRS, frame->fd.flags & CANFD_BRS);
179-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_FDF, frame->fd.flags & CANFD_FDF);
180-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_ESI, frame->fd.flags & CANFD_ESI);
181+
if (frame->fd.flags & CANFD_BRS) {
182+
Avtp_Can_EnableBrs(pdu);
183+
}
184+
if (frame->fd.flags & CANFD_FDF) {
185+
Avtp_Can_EnableFdf(pdu);
186+
}
187+
if (frame->fd.flags & CANFD_ESI) {
188+
Avtp_Can_EnableEsi(pdu);
189+
}
181190
}
182191

183192
// Copy payload to ACF CAN PDU

include/avtp/acf/Can.h

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,11 @@ typedef enum {
8686
void Avtp_Can_Init(Avtp_Can_t* can_pdu);
8787

8888
/**
89-
* Returns the value of an an ACF CAN PDU field as specified in the IEEE 1722 Specification.
89+
* Return the value of an an ACF CAN PDU field as specified in the IEEE 1722 Specification.
9090
*
9191
* @param can_pdu Pointer to the first bit of an 1722 ACF CAN PDU.
92-
* @param field Specifies the position of the data field to be read
9392
* @returns Value of the ACF CAN PDU field.
9493
*/
95-
uint64_t Avtp_Can_GetField(Avtp_Can_t* can_pdu, Avtp_CanFields_t field);
9694

9795
uint8_t Avtp_Can_GetAcfMsgType(Avtp_Can_t* pdu);
9896
uint16_t Avtp_Can_GetAcfMsgLength(Avtp_Can_t* pdu);
@@ -108,27 +106,42 @@ uint64_t Avtp_Can_GetMessageTimestamp(Avtp_Can_t* pdu);
108106
uint32_t Avtp_Can_GetCanIdentifier(Avtp_Can_t* pdu);
109107

110108
/**
111-
* Sets the value of an an ACF CAN PDU field as specified in the IEEE 1722 Specification.
109+
* Set the values of an an ACF CAN PDU field as specified in the IEEE 1722 Specification.
112110
*
113111
* @param can_pdu Pointer to the first bit of an 1722 ACF CAN PDU.
114-
* @param field Specifies the position of the data field to be read
115-
* @param value Pointer to location to store the value.
112+
* @param value Pointer to location with the value.
116113
*/
117-
void Avtp_Can_SetField(Avtp_Can_t* can_pdu, Avtp_CanFields_t field, uint64_t value);
118114

115+
/* Integer fields */
119116
void Avtp_Can_SetAcfMsgType(Avtp_Can_t* pdu, uint8_t value);
120117
void Avtp_Can_SetAcfMsgLength(Avtp_Can_t* pdu, uint16_t value);
121118
void Avtp_Can_SetPad(Avtp_Can_t* pdu, uint8_t value);
122-
void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint64_t value);
123-
void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint32_t value);
124-
void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint32_t value);
125-
void Avtp_Can_SetBrs(Avtp_Can_t* pdu, uint8_t value);
126-
void Avtp_Can_SetFdf(Avtp_Can_t* pdu, uint8_t value);
127-
void Avtp_Can_SetEsi(Avtp_Can_t* pdu, uint8_t value);
128119
void Avtp_Can_SetCanBusId(Avtp_Can_t* pdu, uint8_t value);
129120
void Avtp_Can_SetMessageTimestamp(Avtp_Can_t* pdu, uint64_t value);
130121
void Avtp_Can_SetCanIdentifier(Avtp_Can_t* pdu, uint32_t value);
131122

123+
/**
124+
* Set or unset 1 bit bool ACF CAN PDU field as specified in the IEEE 1722 Specification.
125+
*
126+
* @param can_pdu Pointer to the first bit of an 1722 ACF CAN PDU.
127+
*/
128+
void Avtp_Can_EnableMtv(Avtp_Can_t* pdu);
129+
void Avtp_Can_DisableMtv(Avtp_Can_t* pdu);
130+
void Avtp_Can_EnableRtr(Avtp_Can_t* pdu);
131+
void Avtp_Can_DisaleRtr(Avtp_Can_t* pdu);
132+
void Avtp_Can_EnableEff(Avtp_Can_t* pdu);
133+
void Avtp_Can_DisableEff(Avtp_Can_t* pdu);
134+
void Avtp_Can_EnableBrs(Avtp_Can_t* pdu);
135+
void Avtp_Can_DisableBrs(Avtp_Can_t* pdu);
136+
void Avtp_Can_EnableFdf(Avtp_Can_t* pdu);
137+
void Avtp_Can_DisableFdf(Avtp_Can_t* pdu);
138+
void Avtp_Can_EnableEsi(Avtp_Can_t* pdu);
139+
void Avtp_Can_DisableEsi(Avtp_Can_t* pdu);
140+
141+
142+
143+
144+
132145
/**
133146
* Copies the payload data and CAN frame ID into the ACF CAN frame. This function will
134147
* also set the length and pad fields while inserting the padded bytes.

src/avtp/acf/Can.c

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,10 @@ void Avtp_Can_Init(Avtp_Can_t* pdu)
6464
{
6565
if(pdu != NULL) {
6666
memset(pdu, 0, sizeof(Avtp_Can_t));
67-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_ACF_MSG_TYPE, AVTP_ACF_TYPE_CAN);
67+
Avtp_Can_SetAcfMsgType(pdu, AVTP_ACF_TYPE_CAN);
6868
}
6969
}
7070

71-
uint64_t Avtp_Can_GetField(Avtp_Can_t* pdu, Avtp_CanFields_t field)
72-
{
73-
return GET_FIELD(field);
74-
}
75-
7671
uint8_t Avtp_Can_GetAcfMsgType(Avtp_Can_t* pdu)
7772
{
7873
return GET_FIELD(AVTP_CAN_FIELD_ACF_MSG_TYPE);
@@ -133,10 +128,11 @@ uint32_t Avtp_Can_GetCanIdentifier(Avtp_Can_t* pdu)
133128
return GET_FIELD(AVTP_CAN_FIELD_CAN_IDENTIFIER);
134129
}
135130

131+
/*
136132
void Avtp_Can_SetField(Avtp_Can_t* pdu, Avtp_CanFields_t field, uint64_t value)
137133
{
138134
SET_FIELD(field, value);
139-
}
135+
}*/
140136

141137
void Avtp_Can_SetAcfMsgType(Avtp_Can_t* pdu, uint8_t value)
142138
{
@@ -153,58 +149,64 @@ void Avtp_Can_SetPad(Avtp_Can_t* pdu, uint8_t value)
153149
SET_FIELD(AVTP_CAN_FIELD_PAD, value);
154150
}
155151

156-
void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint64_t value)
152+
void Avtp_Can_EnableMtv(Avtp_Can_t* pdu)
157153
{
158-
if (value) {
159-
SET_FIELD(AVTP_CAN_FIELD_MTV, 1);
160-
} else {
161-
SET_FIELD(AVTP_CAN_FIELD_MTV, 0);
162-
}
154+
SET_FIELD(AVTP_CAN_FIELD_MTV, 1);
163155
}
164156

165-
void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint32_t value)
157+
void Avtp_Can_DisableMtv(Avtp_Can_t* pdu)
166158
{
167-
if (value) {
168-
SET_FIELD(AVTP_CAN_FIELD_RTR, 1);
169-
} else {
170-
SET_FIELD(AVTP_CAN_FIELD_RTR, 0);
171-
}
159+
SET_FIELD(AVTP_CAN_FIELD_MTV, 0);
172160
}
173161

174-
void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint32_t value)
162+
void Avtp_Can_EnableRtr(Avtp_Can_t* pdu)
175163
{
176-
if (value) {
177-
SET_FIELD(AVTP_CAN_FIELD_EFF, 1);
178-
} else {
179-
SET_FIELD(AVTP_CAN_FIELD_EFF, 0);
180-
}
164+
SET_FIELD(AVTP_CAN_FIELD_RTR, 1);
181165
}
182166

183-
void Avtp_Can_SetBrs(Avtp_Can_t* pdu, uint8_t value)
167+
void Avtp_Can_DisableRtr(Avtp_Can_t* pdu)
184168
{
185-
if (value) {
186-
SET_FIELD(AVTP_CAN_FIELD_BRS, 1);
187-
} else {
188-
SET_FIELD(AVTP_CAN_FIELD_BRS, 0);
189-
}
169+
SET_FIELD(AVTP_CAN_FIELD_RTR, 0);
190170
}
191171

192-
void Avtp_Can_SetFdf(Avtp_Can_t* pdu, uint8_t value)
172+
void Avtp_Can_EnableEff(Avtp_Can_t* pdu)
193173
{
194-
if (value) {
195-
SET_FIELD(AVTP_CAN_FIELD_FDF, 1);
196-
} else {
197-
SET_FIELD(AVTP_CAN_FIELD_FDF, 0);
198-
}
174+
SET_FIELD(AVTP_CAN_FIELD_EFF, 1);
199175
}
200176

201-
void Avtp_Can_SetEsi(Avtp_Can_t* pdu, uint8_t value)
177+
void Avtp_Can_DisableEff(Avtp_Can_t* pdu)
202178
{
203-
if (value) {
204-
SET_FIELD(AVTP_CAN_FIELD_ESI, 1);
205-
} else {
206-
SET_FIELD(AVTP_CAN_FIELD_ESI, 0);
207-
}
179+
SET_FIELD(AVTP_CAN_FIELD_EFF, 0);
180+
}
181+
182+
void Avtp_Can_EnableBrs(Avtp_Can_t* pdu)
183+
{
184+
SET_FIELD(AVTP_CAN_FIELD_BRS, 1);
185+
}
186+
187+
void Avtp_Can_DisableBrs(Avtp_Can_t* pdu)
188+
{
189+
SET_FIELD(AVTP_CAN_FIELD_BRS, 0);
190+
}
191+
192+
void Avtp_Can_EnableFdf(Avtp_Can_t* pdu)
193+
{
194+
SET_FIELD(AVTP_CAN_FIELD_FDF, 1);
195+
}
196+
197+
void Avtp_Can_DisableFdf(Avtp_Can_t* pdu)
198+
{
199+
SET_FIELD(AVTP_CAN_FIELD_FDF, 0);
200+
}
201+
202+
void Avtp_Can_EnableEsi(Avtp_Can_t* pdu)
203+
{
204+
SET_FIELD(AVTP_CAN_FIELD_ESI, 1);
205+
}
206+
207+
void Avtp_Can_DisableEsi(Avtp_Can_t* pdu)
208+
{
209+
SET_FIELD(AVTP_CAN_FIELD_ESI, 0);
208210
}
209211

210212
void Avtp_Can_SetCanBusId(Avtp_Can_t* pdu, uint8_t value)
@@ -229,10 +231,14 @@ void Avtp_Can_CreateAcfMessage(Avtp_Can_t* pdu, uint32_t frame_id, uint8_t* payl
229231
Avtp_Can_SetPayload(pdu, payload, payload_length);
230232

231233
// Set the Frame ID and CAN variant
232-
int eff = frame_id > 0x7ff? 1 : 0;
233-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_EFF, eff);
234-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_CAN_IDENTIFIER, frame_id);
235-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_FDF, (uint8_t) can_variant);
234+
if (frame_id > 0x7ff) {
235+
Avtp_Can_EnableEff(pdu);
236+
}
237+
238+
Avtp_Can_SetCanIdentifier(pdu, frame_id);
239+
if (can_variant == AVTP_CAN_FD) {
240+
Avtp_Can_EnableFdf(pdu);
241+
}
236242

237243
// Finalize the AVTP CAN Frame
238244
Avtp_Can_Finalize(pdu, payload_length);
@@ -251,8 +257,8 @@ void Avtp_Can_Finalize(Avtp_Can_t* pdu, uint16_t payload_length)
251257
}
252258

253259
// Set the length and padding fields
254-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_ACF_MSG_LENGTH, (uint64_t) avtpCanLength/AVTP_QUADLET_SIZE);
255-
Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_PAD, padSize);
260+
Avtp_Can_SetAcfMsgLength(pdu, (uint16_t) avtpCanLength/AVTP_QUADLET_SIZE);
261+
Avtp_Can_SetPad(pdu, padSize);
256262
}
257263

258264
void Avtp_Can_SetPayload(Avtp_Can_t* pdu, uint8_t* payload,

0 commit comments

Comments
 (0)