From 2198645ffaf27403875c13ebbbbd29cc00570a0b Mon Sep 17 00:00:00 2001 From: Sebastian Schildt Date: Thu, 2 Jan 2025 16:41:14 +0000 Subject: [PATCH 1/2] Fix boolean convenience setters for ACF-CAN Signed-off-by: Sebastian Schildt --- include/avtp/acf/Can.h | 6 +++--- src/avtp/acf/Can.c | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/include/avtp/acf/Can.h b/include/avtp/acf/Can.h index b53c629..4429852 100644 --- a/include/avtp/acf/Can.h +++ b/include/avtp/acf/Can.h @@ -119,9 +119,9 @@ void Avtp_Can_SetField(Avtp_Can_t* can_pdu, Avtp_CanFields_t field, uint64_t val void Avtp_Can_SetAcfMsgType(Avtp_Can_t* pdu, uint8_t value); void Avtp_Can_SetAcfMsgLength(Avtp_Can_t* pdu, uint16_t value); void Avtp_Can_SetPad(Avtp_Can_t* pdu, uint8_t value); -void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint8_t value); -void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint8_t value); -void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint8_t value); +void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint64_t value); +void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint32_t value); +void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint32_t value); void Avtp_Can_SetBrs(Avtp_Can_t* pdu, uint8_t value); void Avtp_Can_SetFdf(Avtp_Can_t* pdu, uint8_t value); void Avtp_Can_SetEsi(Avtp_Can_t* pdu, uint8_t value); diff --git a/src/avtp/acf/Can.c b/src/avtp/acf/Can.c index 2c22de7..7cd176a 100644 --- a/src/avtp/acf/Can.c +++ b/src/avtp/acf/Can.c @@ -153,34 +153,58 @@ void Avtp_Can_SetPad(Avtp_Can_t* pdu, uint8_t value) SET_FIELD(AVTP_CAN_FIELD_PAD, value); } -void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint8_t value) +void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint64_t value) { - SET_FIELD(AVTP_CAN_FIELD_MTV, value); + if (value) { + SET_FIELD(AVTP_CAN_FIELD_MTV, 1); + } else { + SET_FIELD(AVTP_CAN_FIELD_MTV, 0); + } } -void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint8_t value) +void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint32_t value) { - SET_FIELD(AVTP_CAN_FIELD_RTR, value); + if (value) { + SET_FIELD(AVTP_CAN_FIELD_RTR, 1); + } else { + SET_FIELD(AVTP_CAN_FIELD_RTR, 0); + } } -void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint8_t value) +void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint32_t value) { - SET_FIELD(AVTP_CAN_FIELD_EFF, value); + if (value) { + SET_FIELD(AVTP_CAN_FIELD_EFF, 1); + } else { + SET_FIELD(AVTP_CAN_FIELD_EFF, 0); + } } void Avtp_Can_SetBrs(Avtp_Can_t* pdu, uint8_t value) { - SET_FIELD(AVTP_CAN_FIELD_BRS, value); + if (value) { + SET_FIELD(AVTP_CAN_FIELD_BRS, 1); + } else { + SET_FIELD(AVTP_CAN_FIELD_BRS, 0); + } } void Avtp_Can_SetFdf(Avtp_Can_t* pdu, uint8_t value) { - SET_FIELD(AVTP_CAN_FIELD_FDF, value); + if (value) { + SET_FIELD(AVTP_CAN_FIELD_FDF, 1); + } else { + SET_FIELD(AVTP_CAN_FIELD_FDF, 0); + } } void Avtp_Can_SetEsi(Avtp_Can_t* pdu, uint8_t value) { - SET_FIELD(AVTP_CAN_FIELD_ESI, value); + if (value) { + SET_FIELD(AVTP_CAN_FIELD_ESI, 1); + } else { + SET_FIELD(AVTP_CAN_FIELD_ESI, 0); + } } void Avtp_Can_SetCanBusId(Avtp_Can_t* pdu, uint8_t value) From 0c63cdb924fb346c3b705d1b4377f71c37666140 Mon Sep 17 00:00:00 2001 From: Sebastian Schildt Date: Tue, 14 Jan 2025 12:56:47 +0000 Subject: [PATCH 2/2] Use explicit bool setters, removing one useless indirection for setting CAN fields Signed-off-by: Sebastian Schildt --- examples/acf-can/acf-can-common.c | 25 ++++--- include/avtp/acf/Can.h | 39 +++++++---- src/avtp/acf/Can.c | 104 ++++++++++++++++-------------- 3 files changed, 98 insertions(+), 70 deletions(-) diff --git a/examples/acf-can/acf-can-common.c b/examples/acf-can/acf-can-common.c index ae53cea..b5e9269 100644 --- a/examples/acf-can/acf-can-common.c +++ b/examples/acf-can/acf-can-common.c @@ -159,9 +159,8 @@ static int prepare_acf_packet(uint8_t* acf_pdu, // Prepare ACF PDU for CAN Avtp_Can_Init(pdu); clock_gettime(CLOCK_REALTIME, &now); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_MESSAGE_TIMESTAMP, - (uint64_t)now.tv_nsec + (uint64_t)(now.tv_sec * 1e9)); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_MTV, 1U); + Avtp_Can_SetMessageTimestamp(pdu, (uint64_t)now.tv_nsec + (uint64_t)(now.tv_sec * 1e9)); + Avtp_Can_EnableMtv(pdu); // Set required CAN Flags #ifdef __linux__ @@ -171,13 +170,23 @@ static int prepare_acf_packet(uint8_t* acf_pdu, can_id = (can_variant == AVTP_CAN_FD) ? (*frame).fd.id : (*frame).cc.id; can_payload_length = (can_variant == AVTP_CAN_FD) ? (*frame).fd.dlc : (*frame).cc.dlc; #endif - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_RTR, can_id & CAN_RTR_FLAG); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_EFF, can_id & CAN_EFF_FLAG); + if (can_id & CAN_EFF_FLAG) { + Avtp_Can_EnableEff(pdu); + } + if (can_id & CAN_RTR_FLAG) { + Avtp_Can_EnableRtr(pdu); + } if (can_variant == AVTP_CAN_FD) { - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_BRS, frame->fd.flags & CANFD_BRS); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_FDF, frame->fd.flags & CANFD_FDF); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_ESI, frame->fd.flags & CANFD_ESI); + if (frame->fd.flags & CANFD_BRS) { + Avtp_Can_EnableBrs(pdu); + } + if (frame->fd.flags & CANFD_FDF) { + Avtp_Can_EnableFdf(pdu); + } + if (frame->fd.flags & CANFD_ESI) { + Avtp_Can_EnableEsi(pdu); + } } // Copy payload to ACF CAN PDU diff --git a/include/avtp/acf/Can.h b/include/avtp/acf/Can.h index 4429852..e7860de 100644 --- a/include/avtp/acf/Can.h +++ b/include/avtp/acf/Can.h @@ -86,13 +86,11 @@ typedef enum { void Avtp_Can_Init(Avtp_Can_t* can_pdu); /** - * Returns the value of an an ACF CAN PDU field as specified in the IEEE 1722 Specification. + * Return the value of an an ACF CAN PDU field as specified in the IEEE 1722 Specification. * * @param can_pdu Pointer to the first bit of an 1722 ACF CAN PDU. - * @param field Specifies the position of the data field to be read * @returns Value of the ACF CAN PDU field. */ -uint64_t Avtp_Can_GetField(Avtp_Can_t* can_pdu, Avtp_CanFields_t field); uint8_t Avtp_Can_GetAcfMsgType(Avtp_Can_t* pdu); uint16_t Avtp_Can_GetAcfMsgLength(Avtp_Can_t* pdu); @@ -108,27 +106,42 @@ uint64_t Avtp_Can_GetMessageTimestamp(Avtp_Can_t* pdu); uint32_t Avtp_Can_GetCanIdentifier(Avtp_Can_t* pdu); /** - * Sets the value of an an ACF CAN PDU field as specified in the IEEE 1722 Specification. + * Set the values of an an ACF CAN PDU field as specified in the IEEE 1722 Specification. * * @param can_pdu Pointer to the first bit of an 1722 ACF CAN PDU. - * @param field Specifies the position of the data field to be read - * @param value Pointer to location to store the value. + * @param value Pointer to location with the value. */ -void Avtp_Can_SetField(Avtp_Can_t* can_pdu, Avtp_CanFields_t field, uint64_t value); +/* Integer fields */ void Avtp_Can_SetAcfMsgType(Avtp_Can_t* pdu, uint8_t value); void Avtp_Can_SetAcfMsgLength(Avtp_Can_t* pdu, uint16_t value); void Avtp_Can_SetPad(Avtp_Can_t* pdu, uint8_t value); -void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint64_t value); -void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint32_t value); -void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint32_t value); -void Avtp_Can_SetBrs(Avtp_Can_t* pdu, uint8_t value); -void Avtp_Can_SetFdf(Avtp_Can_t* pdu, uint8_t value); -void Avtp_Can_SetEsi(Avtp_Can_t* pdu, uint8_t value); void Avtp_Can_SetCanBusId(Avtp_Can_t* pdu, uint8_t value); void Avtp_Can_SetMessageTimestamp(Avtp_Can_t* pdu, uint64_t value); void Avtp_Can_SetCanIdentifier(Avtp_Can_t* pdu, uint32_t value); +/** + * Set or unset 1 bit bool ACF CAN PDU field as specified in the IEEE 1722 Specification. + * + * @param can_pdu Pointer to the first bit of an 1722 ACF CAN PDU. + */ +void Avtp_Can_EnableMtv(Avtp_Can_t* pdu); +void Avtp_Can_DisableMtv(Avtp_Can_t* pdu); +void Avtp_Can_EnableRtr(Avtp_Can_t* pdu); +void Avtp_Can_DisaleRtr(Avtp_Can_t* pdu); +void Avtp_Can_EnableEff(Avtp_Can_t* pdu); +void Avtp_Can_DisableEff(Avtp_Can_t* pdu); +void Avtp_Can_EnableBrs(Avtp_Can_t* pdu); +void Avtp_Can_DisableBrs(Avtp_Can_t* pdu); +void Avtp_Can_EnableFdf(Avtp_Can_t* pdu); +void Avtp_Can_DisableFdf(Avtp_Can_t* pdu); +void Avtp_Can_EnableEsi(Avtp_Can_t* pdu); +void Avtp_Can_DisableEsi(Avtp_Can_t* pdu); + + + + + /** * Copies the payload data and CAN frame ID into the ACF CAN frame. This function will * also set the length and pad fields while inserting the padded bytes. diff --git a/src/avtp/acf/Can.c b/src/avtp/acf/Can.c index 7cd176a..a26b249 100644 --- a/src/avtp/acf/Can.c +++ b/src/avtp/acf/Can.c @@ -64,15 +64,10 @@ void Avtp_Can_Init(Avtp_Can_t* pdu) { if(pdu != NULL) { memset(pdu, 0, sizeof(Avtp_Can_t)); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_ACF_MSG_TYPE, AVTP_ACF_TYPE_CAN); + Avtp_Can_SetAcfMsgType(pdu, AVTP_ACF_TYPE_CAN); } } -uint64_t Avtp_Can_GetField(Avtp_Can_t* pdu, Avtp_CanFields_t field) -{ - return GET_FIELD(field); -} - uint8_t Avtp_Can_GetAcfMsgType(Avtp_Can_t* pdu) { return GET_FIELD(AVTP_CAN_FIELD_ACF_MSG_TYPE); @@ -133,10 +128,11 @@ uint32_t Avtp_Can_GetCanIdentifier(Avtp_Can_t* pdu) return GET_FIELD(AVTP_CAN_FIELD_CAN_IDENTIFIER); } +/* void Avtp_Can_SetField(Avtp_Can_t* pdu, Avtp_CanFields_t field, uint64_t value) { SET_FIELD(field, value); -} +}*/ void Avtp_Can_SetAcfMsgType(Avtp_Can_t* pdu, uint8_t value) { @@ -153,58 +149,64 @@ void Avtp_Can_SetPad(Avtp_Can_t* pdu, uint8_t value) SET_FIELD(AVTP_CAN_FIELD_PAD, value); } -void Avtp_Can_SetMtv(Avtp_Can_t* pdu, uint64_t value) +void Avtp_Can_EnableMtv(Avtp_Can_t* pdu) { - if (value) { - SET_FIELD(AVTP_CAN_FIELD_MTV, 1); - } else { - SET_FIELD(AVTP_CAN_FIELD_MTV, 0); - } + SET_FIELD(AVTP_CAN_FIELD_MTV, 1); } -void Avtp_Can_SetRtr(Avtp_Can_t* pdu, uint32_t value) +void Avtp_Can_DisableMtv(Avtp_Can_t* pdu) { - if (value) { - SET_FIELD(AVTP_CAN_FIELD_RTR, 1); - } else { - SET_FIELD(AVTP_CAN_FIELD_RTR, 0); - } + SET_FIELD(AVTP_CAN_FIELD_MTV, 0); } -void Avtp_Can_SetEff(Avtp_Can_t* pdu, uint32_t value) +void Avtp_Can_EnableRtr(Avtp_Can_t* pdu) { - if (value) { - SET_FIELD(AVTP_CAN_FIELD_EFF, 1); - } else { - SET_FIELD(AVTP_CAN_FIELD_EFF, 0); - } + SET_FIELD(AVTP_CAN_FIELD_RTR, 1); } -void Avtp_Can_SetBrs(Avtp_Can_t* pdu, uint8_t value) +void Avtp_Can_DisableRtr(Avtp_Can_t* pdu) { - if (value) { - SET_FIELD(AVTP_CAN_FIELD_BRS, 1); - } else { - SET_FIELD(AVTP_CAN_FIELD_BRS, 0); - } + SET_FIELD(AVTP_CAN_FIELD_RTR, 0); } -void Avtp_Can_SetFdf(Avtp_Can_t* pdu, uint8_t value) +void Avtp_Can_EnableEff(Avtp_Can_t* pdu) { - if (value) { - SET_FIELD(AVTP_CAN_FIELD_FDF, 1); - } else { - SET_FIELD(AVTP_CAN_FIELD_FDF, 0); - } + SET_FIELD(AVTP_CAN_FIELD_EFF, 1); } -void Avtp_Can_SetEsi(Avtp_Can_t* pdu, uint8_t value) +void Avtp_Can_DisableEff(Avtp_Can_t* pdu) { - if (value) { - SET_FIELD(AVTP_CAN_FIELD_ESI, 1); - } else { - SET_FIELD(AVTP_CAN_FIELD_ESI, 0); - } + SET_FIELD(AVTP_CAN_FIELD_EFF, 0); +} + +void Avtp_Can_EnableBrs(Avtp_Can_t* pdu) +{ + SET_FIELD(AVTP_CAN_FIELD_BRS, 1); +} + +void Avtp_Can_DisableBrs(Avtp_Can_t* pdu) +{ + SET_FIELD(AVTP_CAN_FIELD_BRS, 0); +} + +void Avtp_Can_EnableFdf(Avtp_Can_t* pdu) +{ + SET_FIELD(AVTP_CAN_FIELD_FDF, 1); +} + +void Avtp_Can_DisableFdf(Avtp_Can_t* pdu) +{ + SET_FIELD(AVTP_CAN_FIELD_FDF, 0); +} + +void Avtp_Can_EnableEsi(Avtp_Can_t* pdu) +{ + SET_FIELD(AVTP_CAN_FIELD_ESI, 1); +} + +void Avtp_Can_DisableEsi(Avtp_Can_t* pdu) +{ + SET_FIELD(AVTP_CAN_FIELD_ESI, 0); } 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 Avtp_Can_SetPayload(pdu, payload, payload_length); // Set the Frame ID and CAN variant - int eff = frame_id > 0x7ff? 1 : 0; - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_EFF, eff); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_CAN_IDENTIFIER, frame_id); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_FDF, (uint8_t) can_variant); + if (frame_id > 0x7ff) { + Avtp_Can_EnableEff(pdu); + } + + Avtp_Can_SetCanIdentifier(pdu, frame_id); + if (can_variant == AVTP_CAN_FD) { + Avtp_Can_EnableFdf(pdu); + } // Finalize the AVTP CAN Frame Avtp_Can_Finalize(pdu, payload_length); @@ -251,8 +257,8 @@ void Avtp_Can_Finalize(Avtp_Can_t* pdu, uint16_t payload_length) } // Set the length and padding fields - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_ACF_MSG_LENGTH, (uint64_t) avtpCanLength/AVTP_QUADLET_SIZE); - Avtp_Can_SetField(pdu, AVTP_CAN_FIELD_PAD, padSize); + Avtp_Can_SetAcfMsgLength(pdu, (uint16_t) avtpCanLength/AVTP_QUADLET_SIZE); + Avtp_Can_SetPad(pdu, padSize); } void Avtp_Can_SetPayload(Avtp_Can_t* pdu, uint8_t* payload,