diff --git a/CMakeLists.txt b/CMakeLists.txt index d2d61d2..42ac473 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ add_library(open1722 SHARED "src/avtp/acf/CanBrief.c" "src/avtp/acf/Lin.c" "src/avtp/acf/Most.c" - "src/avtp/acf/Common.c" + "src/avtp/acf/AcfCommon.c" "src/avtp/acf/Ntscf.c" "src/avtp/acf/Sensor.c" "src/avtp/acf/SensorBrief.c" diff --git a/README.md b/README.md index 0186eee..18e82b8 100644 --- a/README.md +++ b/README.md @@ -76,3 +76,62 @@ To execute the IEEE 1722 CAN Talker application: ``` $ ./bin/acf-can-talker ``` + +### De/Serialization IEEE 1722 PDUs + +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. + +``` C +// my_1722_pdu.h + +#define CAN_PAYLOAD_LEN 2 +#define LIN_PAYLOAD_LEN 3 + +typedef struct { + // IEEE 1722 UDP encapsulation header (optional) + Avtp_Udp_t udp; + // IEEE 1722 TSCF header + Avtp_Tscf_t tscf; + // IEEE 1722 ACF message #1 + Avtp_Can_t can; + uint8_t canPayload[CAN_PAYLOAD_LEN]; + // IEEE 1722 ACF message #2 + Avtp_Lin_t lin; + uint8_t linPayload[LIN_PAYLOAD_LEN]; +} My1722Pdu_t; +``` + +In the next step we're going to c + +``` C +// talker.h + +#include "my_1722_pdu.h" + +int main() +{ + My1722Pdu_t pdu; + + // Init UDP encapsulation header + Avtp_Udp_Init(&pdu.udp); + + // Init TSCF header + Avtp_Tscf_Init(&pdu.tscf); + Avtp_Tscf_SetVersion(&pdu.tscf, 0); + Avtp_Tscf_SetSequenceNum(&pdu.tscf, 12345); + Avtp_Tscf_SetStreamId(&pdu.tscf, 0xAABBCCDDEEFF); + Avtp_Tscf_SetTv(&pdu.tscf, 1); + Avtp_Tscf_SetAvtpTimestamp(&pdu.tscf, 0x11223344); + + // Init CAN ACF message + Avtp_Can_Init(&pdu.can); + Avtp_Can_SetCanBusId(&pdu.can, 4); + uint8_t canFrame[CAN_PAYLOAD_LEN] = {0x11, 0x22}; + + // Init LIN ACF message + Avtp_Lin_Init(&pdu.lin); + uint8_t linFrame[LIN_PAYLOAD_LEN] = {0x11, 0x22, 0x33}; + + // Send packet to network +} +``` diff --git a/include/avtp/acf/Can.h b/include/avtp/acf/Can.h index bb04207..7f7d6af 100644 --- a/include/avtp/acf/Can.h +++ b/include/avtp/acf/Can.h @@ -38,7 +38,7 @@ #include #include "avtp/Defines.h" -#include "avtp/acf/Common.h" +#include "avtp/acf/AcfCommon.h" #define AVTP_CAN_HEADER_LEN (4 * AVTP_QUADLET_SIZE) diff --git a/include/avtp/acf/CanBrief.h b/include/avtp/acf/CanBrief.h index 60d3c36..5ee0184 100644 --- a/include/avtp/acf/CanBrief.h +++ b/include/avtp/acf/CanBrief.h @@ -38,7 +38,7 @@ #include #include "avtp/Defines.h" -#include "avtp/acf/Common.h" +#include "avtp/acf/AcfCommon.h" #include "avtp/acf/Can.h" #define AVTP_CAN_BRIEF_HEADER_LEN (2 * AVTP_QUADLET_SIZE) @@ -141,4 +141,4 @@ int Avtp_CanBrief_SetPayload(Avtp_CanBrief_t* can_pdu, uint32_t frame_id , uint8 * @param payload_length Length of the payload. * @returns Returns number of processed bytes (header + payload + padding) */ -int Avtp_CanBrief_Finalize(Avtp_CanBrief_t* can_pdu, uint16_t payload_length); \ No newline at end of file +int Avtp_CanBrief_Finalize(Avtp_CanBrief_t* can_pdu, uint16_t payload_length); diff --git a/include/avtp/acf/FlexRay.h b/include/avtp/acf/FlexRay.h index 6e4b62a..7270d3f 100644 --- a/include/avtp/acf/FlexRay.h +++ b/include/avtp/acf/FlexRay.h @@ -37,7 +37,7 @@ #include #include "avtp/Defines.h" -#include "avtp/acf/Common.h" +#include "avtp/acf/AcfCommon.h" /** Length of ACF FlexRay header. */ #define AVTP_FLEXRAY_HEADER_LEN (4 * AVTP_QUADLET_SIZE) @@ -88,6 +88,20 @@ void Avtp_FlexRay_Init(Avtp_FlexRay_t* pdu); */ uint64_t Avtp_FlexRay_GetField(Avtp_FlexRay_t* pdu, Avtp_FlexRayFields_t field); +uint8_t Avtp_FlexRay_GetAcfMsgType(Avtp_FlexRay_t* pdu); +uint16_t Avtp_FlexRay_GetAcfMsgLength(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetPad(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetMtv(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetFrBusId(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetChan(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetStr(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetSyn(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetPre(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetNfi(Avtp_FlexRay_t* pdu); +uint64_t Avtp_FlexRay_GetMessageTimestamp(Avtp_FlexRay_t* pdu); +uint16_t Avtp_FlexRay_GetFrFrameId(Avtp_FlexRay_t* pdu); +uint8_t Avtp_FlexRay_GetCycle(Avtp_FlexRay_t* pdu); + /** * Sets the value of an ACF FlexRay PDU field. * @@ -97,3 +111,17 @@ uint64_t Avtp_FlexRay_GetField(Avtp_FlexRay_t* pdu, Avtp_FlexRayFields_t field); * @returns Returns 0 if the data field was successfully set. */ void Avtp_FlexRay_SetField(Avtp_FlexRay_t* pdu, Avtp_FlexRayFields_t field, uint64_t value); + +void Avtp_FlexRay_SetAcfMsgType(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetAcfMsgLength(Avtp_FlexRay_t* pdu, uint16_t value); +void Avtp_FlexRay_SetPad(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetMtv(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetFrBusId(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetChan(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetStr(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetSyn(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetPre(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetNfi(Avtp_FlexRay_t* pdu, uint8_t value); +void Avtp_FlexRay_SetMessageTimestamp(Avtp_FlexRay_t* pdu, uint64_t value); +void Avtp_FlexRay_SetFrFrameId(Avtp_FlexRay_t* pdu, uint16_t value); +void Avtp_FlexRay_SetCycle(Avtp_FlexRay_t* pdu, uint8_t value); diff --git a/include/avtp/acf/Gpc.h b/include/avtp/acf/Gpc.h index 73ec159..2295a7c 100644 --- a/include/avtp/acf/Gpc.h +++ b/include/avtp/acf/Gpc.h @@ -37,7 +37,7 @@ #include #include "avtp/Defines.h" -#include "avtp/acf/Common.h" +#include "avtp/acf/AcfCommon.h" #define AVTP_GPC_HEADER_LEN (2 * AVTP_QUADLET_SIZE) diff --git a/include/avtp/acf/Lin.h b/include/avtp/acf/Lin.h index 7f822b7..7e5fd1c 100644 --- a/include/avtp/acf/Lin.h +++ b/include/avtp/acf/Lin.h @@ -37,7 +37,7 @@ #include #include "avtp/Defines.h" -#include "avtp/acf/Common.h" +#include "avtp/acf/AcfCommon.h" /** Length of ACF Lin header. */ #define AVTP_LIN_HEADER_LEN (3 * AVTP_QUADLET_SIZE) diff --git a/src/avtp/acf/Can.c b/src/avtp/acf/Can.c index d2e029d..8754b95 100644 --- a/src/avtp/acf/Can.c +++ b/src/avtp/acf/Can.c @@ -30,7 +30,6 @@ #include #include -#include "avtp/acf/Common.h" #include "avtp/acf/Can.h" #include "avtp/Utils.h" #include "avtp/Defines.h" diff --git a/src/avtp/acf/CanBrief.c b/src/avtp/acf/CanBrief.c index 189ab92..738429e 100644 --- a/src/avtp/acf/CanBrief.c +++ b/src/avtp/acf/CanBrief.c @@ -30,7 +30,6 @@ #include #include -#include "avtp/acf/Common.h" #include "avtp/acf/CanBrief.h" #include "avtp/Utils.h" #include "avtp/Defines.h" diff --git a/src/avtp/acf/FlexRay.c b/src/avtp/acf/FlexRay.c index 0079bb1..b247185 100644 --- a/src/avtp/acf/FlexRay.c +++ b/src/avtp/acf/FlexRay.c @@ -31,7 +31,12 @@ #include #include "avtp/acf/FlexRay.h" -#include "avtp/Utils.h" +#include "avtp/Utils.h" + +#define GET_FIELD(field) \ + (Avtp_GetField(Avtp_FlexRayFieldDesc, AVTP_FLEXRAY_FIELD_MAX, (uint8_t*)pdu, field)) +#define SET_FIELD(field, value) \ + (Avtp_SetField(Avtp_FlexRayFieldDesc, AVTP_FLEXRAY_FIELD_MAX, (uint8_t*)pdu, field, value)) /** * This table describes all the offsets of the ACF FlexRay header fields. @@ -67,10 +72,140 @@ void Avtp_FlexRay_Init(Avtp_FlexRay_t* pdu) uint64_t Avtp_FlexRay_GetField(Avtp_FlexRay_t* pdu, Avtp_FlexRayFields_t field) { - return Avtp_GetField(Avtp_FlexRayFieldDesc, AVTP_FLEXRAY_FIELD_MAX, (uint8_t*)pdu, (uint8_t)field); + return GET_FIELD(field); +} + +uint8_t Avtp_FlexRay_GetAcfMsgType(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_ACF_MSG_TYPE); +} + +uint16_t Avtp_FlexRay_GetAcfMsgLength(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_ACF_MSG_LENGTH); +} + +uint8_t Avtp_FlexRay_GetPad(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_PAD); +} + +uint8_t Avtp_FlexRay_GetMtv(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_MTV); +} + +uint8_t Avtp_FlexRay_GetFrBusId(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_FR_BUS_ID); +} + +uint8_t Avtp_FlexRay_GetChan(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_CHAN); +} + +uint8_t Avtp_FlexRay_GetStr(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_STR); +} + +uint8_t Avtp_FlexRay_GetSyn(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_SYN); +} + +uint8_t Avtp_FlexRay_GetPre(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_PRE); +} + +uint8_t Avtp_FlexRay_GetNfi(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_NFI); +} + +uint64_t Avtp_FlexRay_GetMessageTimestamp(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_MESSAGE_TIMESTAMP); +} + +uint16_t Avtp_FlexRay_GetFrFrameId(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_FR_FRAME_ID); +} + +uint8_t Avtp_FlexRay_GetCycle(Avtp_FlexRay_t* pdu) +{ + return GET_FIELD(AVTP_FLEXRAY_FIELD_CYCLE); } void Avtp_FlexRay_SetField(Avtp_FlexRay_t* pdu, Avtp_FlexRayFields_t field, uint64_t value) { - Avtp_SetField(Avtp_FlexRayFieldDesc, AVTP_FLEXRAY_FIELD_MAX, (uint8_t*)pdu, (uint8_t)field, value); + SET_FIELD(field, value); +} + +void Avtp_FlexRay_SetAcfMsgType(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_ACF_MSG_TYPE, value); +} + +void Avtp_FlexRay_SetAcfMsgLength(Avtp_FlexRay_t* pdu, uint16_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_ACF_MSG_LENGTH, value); +} + +void Avtp_FlexRay_SetPad(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_PAD, value); +} + +void Avtp_FlexRay_SetMtv(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_MTV, value); +} + +void Avtp_FlexRay_SetFrBusId(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_FR_BUS_ID, value); +} + +void Avtp_FlexRay_SetChan(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_CHAN, value); +} + +void Avtp_FlexRay_SetStr(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_STR, value); +} + +void Avtp_FlexRay_SetSyn(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_SYN, value); +} + +void Avtp_FlexRay_SetPre(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_PRE, value); +} + +void Avtp_FlexRay_SetNfi(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_NFI, value); +} + +void Avtp_FlexRay_SetMessageTimestamp(Avtp_FlexRay_t* pdu, uint64_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_MESSAGE_TIMESTAMP, value); +} + +void Avtp_FlexRay_SetFrFrameId(Avtp_FlexRay_t* pdu, uint16_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_FR_FRAME_ID, value); +} + +void Avtp_FlexRay_SetCycle(Avtp_FlexRay_t* pdu, uint8_t value) +{ + SET_FIELD(AVTP_FLEXRAY_FIELD_CYCLE, value); } diff --git a/src/avtp/acf/Gpc.c b/src/avtp/acf/Gpc.c index 911e25a..28c8324 100644 --- a/src/avtp/acf/Gpc.c +++ b/src/avtp/acf/Gpc.c @@ -30,7 +30,6 @@ #include #include -#include "avtp/acf/Common.h" #include "avtp/acf/Gpc.h" #include "avtp/Utils.h" #include "avtp/Defines.h"