Skip to content

Commit

Permalink
Use explicit bool setters, removing one useless indirection for setti…
Browse files Browse the repository at this point in the history
…ng CAN fields

Signed-off-by: Sebastian Schildt <[email protected]>
  • Loading branch information
SebastianSchildt committed Jan 14, 2025
1 parent 2198645 commit 0c63cdb
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 70 deletions.
25 changes: 17 additions & 8 deletions examples/acf-can/acf-can-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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
Expand Down
39 changes: 26 additions & 13 deletions include/avtp/acf/Can.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down
104 changes: 55 additions & 49 deletions src/avtp/acf/Can.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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,
Expand Down

0 comments on commit 0c63cdb

Please sign in to comment.