From b074d984597b852baa9b600856dc7a0a00f859c1 Mon Sep 17 00:00:00 2001 From: Naresh Nayak Date: Thu, 29 Oct 2026 21:31:42 +0000 Subject: [PATCH 1/6] Added ACF_CRC format along with unit tests. Signed-off-by: Naresh Nayak --- README.md | 1 + include/avtp/acf/Crc.h | 261 ++++++++++++++++++++++++++++++++ src/avtp/export/InlineExports.c | 1 + unit/CMakeLists.txt | 1 + unit/test-crc.c | 187 +++++++++++++++++++++++ 5 files changed, 451 insertions(+) create mode 100644 include/avtp/acf/Crc.h create mode 100644 unit/test-crc.c diff --git a/README.md b/README.md index 48010e7..7f3cd50 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ The following is the list of the formats currently supported by Open1722: - CAN Brief (v1 & v2) - CAN-XL - CAN-XL Brief + - CRC - Flexray - LIN - MOST diff --git a/include/avtp/acf/Crc.h b/include/avtp/acf/Crc.h new file mode 100644 index 0000000..2e52719 --- /dev/null +++ b/include/avtp/acf/Crc.h @@ -0,0 +1,261 @@ +/* + * Copyright (c) 2026, COVESA + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of COVESA nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/** + * @file This file contains functions for de-/serialization of the ACF_CRC + * message format. For details see IEEE Std. 1722-2025, chapter 9.4.21. + */ + +#pragma once +#include "avtp/Inline.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#include "avtp/Utils.h" +#include "avtp/Defines.h" +#include "avtp/acf/AcfCommon.h" + +#define GET_CRC_FIELD(field) \ + (Avtp_GetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, (const uint8_t *)pdu, field)) +#define SET_CRC_FIELD(field, value) \ + (Avtp_SetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, (uint8_t *)pdu, field, value)) + +/** + * Length of ACF_CRC message header in bytes. + */ +#define AVTP_CRC_HEADER_LEN (1 * AVTP_QUADLET_SIZE) + +/** + * ACF_CRC message structure suitable for in-place de-/serialization of fields + * in the ACF_CRC message header. + */ +typedef struct { + uint8_t header[AVTP_CRC_HEADER_LEN]; + uint8_t payload[0]; +} __attribute__((packed)) Avtp_Crc_t; + +/** + * Fields encoded in the ACF_CRC header. + */ +typedef enum { + /* ACF common header fields */ + AVTP_CRC_FIELD_ACF_MSG_TYPE = 0, + AVTP_CRC_FIELD_ACF_MSG_LENGTH, + /* ACF_CRC header fields */ + AVTP_CRC_FIELD_RESERVED, + AVTP_CRC_FIELD_CRC_TYPE, + /* Count number of fields for bound checks */ + AVTP_CRC_FIELD_MAX +} Avtp_CrcFields_t; + +/** + * CRC types encoded in the ACF_CRC header. + */ +typedef enum { + AVTP_CRC_TYPE_CRC_ETH = 0, + AVTP_CRC_TYPE_CRC_32P4 = 1, + AVTP_CRC_TYPE_CRC_USER = 0xF, +} Avtp_CrcType_t; + +/** + * This table describes all the offsets of the ACF_CRC header fields. + */ +static const Avtp_FieldDescriptor_t Avtp_CrcFieldDesc[AVTP_CRC_FIELD_MAX] = { + /* ACF common header fields */ + [AVTP_CRC_FIELD_ACF_MSG_TYPE] = {.quadlet = 0, .offset = 0, .bits = 7}, + [AVTP_CRC_FIELD_ACF_MSG_LENGTH] = {.quadlet = 0, .offset = 7, .bits = 9}, + /* ACF_CRC header fields */ + [AVTP_CRC_FIELD_RESERVED] = {.quadlet = 0, .offset = 16, .bits = 12}, + [AVTP_CRC_FIELD_CRC_TYPE] = {.quadlet = 0, .offset = 28, .bits = 4}, +}; + +/** + * Sets the CRC type field in an ACF_CRC message header. + * + * @param pdu Pointer to an ACF_CRC message. + * @param crc_type The value to set. + */ +OPEN1722_INLINE void Avtp_Crc_SetCrcType(Avtp_Crc_t *pdu, Avtp_CrcType_t crc_type) +{ + SET_CRC_FIELD(AVTP_CRC_FIELD_CRC_TYPE, crc_type); +} + +/** + * Returns the value of the CRC type field from an ACF_CRC message header. + * + * @param pdu Pointer to an ACF_CRC message. + * @returns The value of the CRC type field. + */ +OPEN1722_INLINE Avtp_CrcType_t Avtp_Crc_GetCrcType(const Avtp_Crc_t *const pdu) +{ + return (Avtp_CrcType_t)GET_CRC_FIELD(AVTP_CRC_FIELD_CRC_TYPE); +} + +/** + * Returns pointer to payload of an ACF CRC frame. + * + * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. + * @return Pointer to ACF CRC frame payload + */ +OPEN1722_INLINE const uint8_t *Avtp_Crc_GetPayload(const Avtp_Crc_t *const pdu) +{ + return pdu->payload; +} + +/** + * Sets the CRC payload in an ACF CRC frame. + * + * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. + * @param payload Pointer to the payload byte array + * @param payload_length Length of the payload + */ +OPEN1722_INLINE void Avtp_Crc_SetPayload(Avtp_Crc_t *pdu, uint8_t *payload, uint16_t payload_length) +{ + memcpy(pdu->payload, payload, payload_length); +} + +/** + * Finalizes the ACF CRC frame. This function sets the quadlet-encoded + * length field based on the given payload length and zeroes the trailing + * padding octets. + * + * The ACF CRC format has no explicit pad field. The number of padding + * octets is determined by the payload length. Trailing padding octets + * are set to zero to make the ACF CRC frame a multiple of 4 octets in length. + * + * @param pdu Pointer to the first bit of an 1722 ACF CRC PDU. + * @param payload_length Length of the CRC payload. + */ +OPEN1722_INLINE void Avtp_Crc_SetPayloadLength(Avtp_Crc_t *pdu, uint16_t payload_length) +{ + uint16_t msgLenBytes = AVTP_CRC_HEADER_LEN + payload_length; + uint8_t pad = (uint8_t)(4 - (msgLenBytes % 4)) % 4; + if (pad > 0) { + memset(pdu->payload + payload_length, 0, pad); + } + uint16_t msgLenQuadlets = (uint16_t)((msgLenBytes + pad) / 4); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)pdu, msgLenQuadlets); +} + +/* + * Note: there is intentionally no Avtp_Crc_GetPayloadLength() function. + * + * The ACF CRC header carries neither a pad field nor a payload-length field, so + * the true payload length cannot be recovered from the wire format alone. + * + * If the CRC type is AVTP_CRC_TYPE_CRC_ETH, the payload length is always 4 bytes. + * If the CRC type is AVTP_CRC_TYPE_CRC_32P4, the payload length is always 4 bytes. + * + * If the CRC type is AVTP_CRC_TYPE_CRC_USER, the payload length is variable and + * must be tracked by the caller. There is no way to determine the payload length + * from the ACF CRC header alone. + */ + +/** + * Initializes an ACF CRC PDU header as specified in the IEEE 1722 Specification. + * + * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. + */ +OPEN1722_INLINE void Avtp_Crc_Init(Avtp_Crc_t *pdu) +{ + if (pdu != NULL) { + memset(pdu, 0, sizeof(Avtp_Crc_t)); + Avtp_AcfCommon_SetAcfMsgType((Avtp_AcfCommon_t *)pdu, AVTP_ACF_TYPE_CRC); + } +} + +/** + * Copies the payload data and CRC type into the ACF CRC frame. + * + * @param pdu Pointer to the first bit of an 1722 ACF CRC PDU. + * @param crc_type CRC type + * @param payload Pointer to the payload byte array + * @param payload_length Length of the payload. + */ +OPEN1722_INLINE void Avtp_Crc_CreateAcfMessage(Avtp_Crc_t *pdu, Avtp_CrcType_t crc_type, + uint8_t *payload, uint16_t payload_length) +{ + // Initialize the ACF CRC header + Avtp_Crc_Init(pdu); + + // Set the CRC type + Avtp_Crc_SetCrcType(pdu, crc_type); + + // Copy the payload into the CRC PDU + Avtp_Crc_SetPayload(pdu, payload, payload_length); + + // Finalize the AVTP CRC Frame + Avtp_Crc_SetPayloadLength(pdu, payload_length); +} + +/** + * Checks if the ACF CRC frame is valid by checking: + * 1) if the length field of AVTP/ACF messages contains a value larger than the actual size of + * the buffer that contains the AVTP message. 2) if other format specific invariants are not upheld + * + * @param pdu Pointer to the first bit of an 1722 ACF CRC PDU. + * @param bufferSize Size of the buffer containing the ACF CRC frame. + * @return true if the ACF CRC frame is valid, false otherwise. + */ +OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t bufferSize) +{ + if (pdu == NULL) { + return false; + } + + if (bufferSize < AVTP_CRC_HEADER_LEN) { + return false; + } + + if (Avtp_AcfCommon_GetAcfMsgType((const Avtp_AcfCommon_t *)pdu) != AVTP_ACF_TYPE_CRC) { + return false; + } + + uint16_t msg_length_bytes = + Avtp_AcfCommon_GetAcfMsgLengthInBytes((const Avtp_AcfCommon_t *)pdu); + if (msg_length_bytes > bufferSize) { + return false; + } + + if (msg_length_bytes < AVTP_CRC_HEADER_LEN) { + return false; + } + + return true; +} + +#ifdef __cplusplus +} +#endif diff --git a/src/avtp/export/InlineExports.c b/src/avtp/export/InlineExports.c index 4219be7..41d8032 100644 --- a/src/avtp/export/InlineExports.c +++ b/src/avtp/export/InlineExports.c @@ -74,6 +74,7 @@ #include "avtp/acf/CanV2.h" #include "avtp/acf/CanXl.h" #include "avtp/acf/CanXlBrief.h" +#include "avtp/acf/Crc.h" #include "avtp/acf/FlexRay.h" #include "avtp/acf/Gbb.h" #include "avtp/acf/Gisf.h" diff --git a/unit/CMakeLists.txt b/unit/CMakeLists.txt index 75aeeeb..b1d6c2c 100644 --- a/unit/CMakeLists.txt +++ b/unit/CMakeLists.txt @@ -89,6 +89,7 @@ add_dual_test(test-pcm test-pcm.c) add_dual_test(test-crf test-crf.c) add_dual_test(test-cvf test-cvf.c) add_dual_test(test-rvf test-rvf.c) +add_dual_test(test-crc test-crc.c) add_dual_test(test-tscf test-tscf.c) add_dual_test(test-ntscf test-ntscf.c) add_dual_test(test-vss test-vss.c open1722custom) diff --git a/unit/test-crc.c b/unit/test-crc.c new file mode 100644 index 0000000..ca16230 --- /dev/null +++ b/unit/test-crc.c @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2026, COVESA + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of COVESA nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#if defined(__cplusplus) +extern "C" { +#include +} +#else +#include +#endif +#include + +#include "avtp/acf/Crc.h" + +#define MAX_PDU_SIZE 1500 + +static void crc_init(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + uint8_t init_pdu[AVTP_CRC_HEADER_LEN] = {0}; + + assert_int_equal(sizeof(Avtp_Crc_t), AVTP_CRC_HEADER_LEN); + + Avtp_Crc_Init(NULL); + + memset(pdu, 0xAA, sizeof(pdu)); + Avtp_Crc_Init((Avtp_Crc_t *)pdu); + init_pdu[0] = AVTP_ACF_TYPE_CRC << 1; + assert_memory_equal(init_pdu, pdu, AVTP_CRC_HEADER_LEN); + assert_int_equal(pdu[AVTP_CRC_HEADER_LEN], 0xAA); +} + +static void crc_field_descriptors_cover_header(void **state) +{ + (void)state; + uint8_t coverage[AVTP_CRC_HEADER_LEN * 8] = {0}; + + for (uint8_t field = 0; field < AVTP_CRC_FIELD_MAX; field++) { + const Avtp_FieldDescriptor_t *desc = &Avtp_CrcFieldDesc[field]; + + assert_int_equal(desc->quadlet, 0); + for (uint8_t bit = 0; bit < desc->bits; bit++) { + size_t bit_index = desc->offset + bit; + + assert_true(bit_index < sizeof(coverage)); + assert_int_equal(coverage[bit_index], 0); + coverage[bit_index] = 1; + } + } + + for (size_t bit = 0; bit < sizeof(coverage); bit++) { + assert_int_equal(coverage[bit], 1); + } +} + +static void crc_fields(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + Avtp_Crc_Init(crc); + assert_int_equal(Avtp_AcfCommon_GetAcfMsgType((Avtp_AcfCommon_t *)crc), AVTP_ACF_TYPE_CRC); + + Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_32P4); + assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_32P4); + + Avtp_SetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED, 0xABC); + assert_int_equal( + Avtp_GetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED), 0xABC); + assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_32P4); + + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 1); + assert_int_equal(Avtp_AcfCommon_GetAcfMsgLength((Avtp_AcfCommon_t *)crc), 1); +} + +static void crc_payload_and_length(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + uint8_t payload[] = {0x10, 0x20, 0x30, 0x40, 0x50}; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + for (uint16_t payload_length = 0; payload_length <= sizeof(payload); payload_length++) { + uint16_t message_length = AVTP_CRC_HEADER_LEN + payload_length; + uint16_t padded_length = + ((message_length + AVTP_QUADLET_SIZE - 1) / AVTP_QUADLET_SIZE) * AVTP_QUADLET_SIZE; + + memset(pdu, 0xAA, sizeof(pdu)); + Avtp_Crc_Init(crc); + Avtp_Crc_SetPayload(crc, payload, payload_length); + assert_ptr_equal(Avtp_Crc_GetPayload(crc), pdu + AVTP_CRC_HEADER_LEN); + if (payload_length > 0) { + assert_memory_equal(Avtp_Crc_GetPayload(crc), payload, payload_length); + } + + Avtp_Crc_SetPayloadLength(crc, payload_length); + assert_int_equal(Avtp_AcfCommon_GetAcfMsgLengthInBytes((Avtp_AcfCommon_t *)crc), + padded_length); + for (uint16_t pad = 0; pad < padded_length - message_length; pad++) { + assert_int_equal(pdu[message_length + pad], 0); + } + } +} + +static void crc_create_message(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + uint8_t payload[] = {0xDE, 0xAD, 0xBE, 0xEF}; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + memset(pdu, 0xAA, sizeof(pdu)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + + assert_int_equal(Avtp_AcfCommon_GetAcfMsgType((Avtp_AcfCommon_t *)crc), AVTP_ACF_TYPE_CRC); + assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_ETH); + assert_int_equal(Avtp_AcfCommon_GetAcfMsgLengthInBytes((Avtp_AcfCommon_t *)crc), 8); + assert_memory_equal(Avtp_Crc_GetPayload(crc), payload, sizeof(payload)); + assert_true(Avtp_Crc_IsValid(crc, 8)); +} + +static void crc_is_valid(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + uint8_t payload[] = {0, 1, 2, 3}; + + assert_false(Avtp_Crc_IsValid(NULL, sizeof(pdu))); + + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + assert_true(Avtp_Crc_IsValid(crc, 8)); + + assert_false(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN - 1)); + + Avtp_AcfCommon_SetAcfMsgType((Avtp_AcfCommon_t *)crc, AVTP_ACF_TYPE_GPC); + assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0); + assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + assert_false(Avtp_Crc_IsValid(crc, 7)); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(crc_init), cmocka_unit_test(crc_field_descriptors_cover_header), + cmocka_unit_test(crc_fields), cmocka_unit_test(crc_payload_and_length), + cmocka_unit_test(crc_create_message), cmocka_unit_test(crc_is_valid), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} From 13224df1b087ca553eac066b8568b90bff07a105 Mon Sep 17 00:00:00 2001 From: Naresh Nayak Date: Fri, 30 Oct 2026 09:08:49 +0000 Subject: [PATCH 2/6] Added additional checks for IsValid() for ACF_CRC format. Signed-off-by: Naresh Nayak --- include/avtp/acf/Crc.h | 7 ++++++ unit/test-crc.c | 50 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/include/avtp/acf/Crc.h b/include/avtp/acf/Crc.h index 2e52719..4efb509 100644 --- a/include/avtp/acf/Crc.h +++ b/include/avtp/acf/Crc.h @@ -245,6 +245,13 @@ OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t buffer uint16_t msg_length_bytes = Avtp_AcfCommon_GetAcfMsgLengthInBytes((const Avtp_AcfCommon_t *)pdu); + if (Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_ETH || + Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_32P4) { + if (msg_length_bytes != (AVTP_CRC_HEADER_LEN + 4)) { + return false; + } + } + if (msg_length_bytes > bufferSize) { return false; } diff --git a/unit/test-crc.c b/unit/test-crc.c index ca16230..15dd710 100644 --- a/unit/test-crc.c +++ b/unit/test-crc.c @@ -175,12 +175,56 @@ static void crc_is_valid(void **state) assert_false(Avtp_Crc_IsValid(crc, 7)); } +/* CRC_ETH and CRC_32P4 carry a fixed 4 byte CRC, so the message must be exactly 8 bytes. */ +static void crc_is_valid_fixed_length_types(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + uint8_t payload[8] = {0}; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + const Avtp_CrcType_t types[] = {AVTP_CRC_TYPE_CRC_ETH, AVTP_CRC_TYPE_CRC_32P4}; + + for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); i++) { + Avtp_CrcType_t type = types[i]; + + Avtp_Crc_CreateAcfMessage(crc, type, payload, 4); + assert_true(Avtp_Crc_IsValid(crc, sizeof(pdu))); + + /* Too short: header only, and below the fixed length. */ + Avtp_Crc_CreateAcfMessage(crc, type, payload, 0); + assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + + /* Too long: fits the buffer but exceeds the fixed length. */ + Avtp_Crc_CreateAcfMessage(crc, type, payload, 8); + assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + } +} + +/* CRC_USER has a variable payload length, so lengths other than 8 bytes remain valid. */ +static void crc_is_valid_user_type_variable_length(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + uint8_t payload[16] = {0}; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + for (uint16_t payload_length = 0; payload_length <= sizeof(payload); payload_length += 4) { + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_USER, payload, payload_length); + assert_true(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN + payload_length)); + } +} + int main(void) { const struct CMUnitTest tests[] = { - cmocka_unit_test(crc_init), cmocka_unit_test(crc_field_descriptors_cover_header), - cmocka_unit_test(crc_fields), cmocka_unit_test(crc_payload_and_length), - cmocka_unit_test(crc_create_message), cmocka_unit_test(crc_is_valid), + cmocka_unit_test(crc_init), + cmocka_unit_test(crc_field_descriptors_cover_header), + cmocka_unit_test(crc_fields), + cmocka_unit_test(crc_payload_and_length), + cmocka_unit_test(crc_create_message), + cmocka_unit_test(crc_is_valid), + cmocka_unit_test(crc_is_valid_fixed_length_types), + cmocka_unit_test(crc_is_valid_user_type_variable_length), }; return cmocka_run_group_tests(tests, NULL, NULL); From cf6d4ad06941bce3dbf9c99782a709e2128e5da0 Mon Sep 17 00:00:00 2001 From: Naresh Nayak Date: Fri, 30 Oct 2026 09:15:29 +0000 Subject: [PATCH 3/6] Minor changes for improved test coverage. - Added unit tests for ACF_CRC to improve coverage. - Refactored conditions in IsValid() for better test coverage. Signed-off-by: Naresh Nayak --- include/avtp/acf/Crc.h | 13 +++++++------ unit/test-crc.c | 5 +++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/avtp/acf/Crc.h b/include/avtp/acf/Crc.h index 4efb509..c650871 100644 --- a/include/avtp/acf/Crc.h +++ b/include/avtp/acf/Crc.h @@ -245,12 +245,6 @@ OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t buffer uint16_t msg_length_bytes = Avtp_AcfCommon_GetAcfMsgLengthInBytes((const Avtp_AcfCommon_t *)pdu); - if (Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_ETH || - Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_32P4) { - if (msg_length_bytes != (AVTP_CRC_HEADER_LEN + 4)) { - return false; - } - } if (msg_length_bytes > bufferSize) { return false; @@ -260,6 +254,13 @@ OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t buffer return false; } + if (Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_ETH || + Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_32P4) { + if (msg_length_bytes != (AVTP_CRC_HEADER_LEN + 4)) { + return false; + } + } + return true; } diff --git a/unit/test-crc.c b/unit/test-crc.c index 15dd710..608a06d 100644 --- a/unit/test-crc.c +++ b/unit/test-crc.c @@ -197,6 +197,11 @@ static void crc_is_valid_fixed_length_types(void **state) /* Too long: fits the buffer but exceeds the fixed length. */ Avtp_Crc_CreateAcfMessage(crc, type, payload, 8); assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + + /* Zero length */ + Avtp_Crc_CreateAcfMessage(crc, type, payload, 8); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0); + assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); } } From 3d2fdde80a48d7a93dedab9856ba6252544dbd1f Mon Sep 17 00:00:00 2001 From: Naresh Nayak Date: Fri, 30 Oct 2026 14:40:28 +0000 Subject: [PATCH 4/6] Integrated the review from #228 - Added function for accessing CRC data - Disallowed ACF_CRC without CRC data - Fixed length for CRC_ETH and CRC32P4 - Improved test coverage Signed-off-by: Naresh Nayak --- include/avtp/acf/Crc.h | 117 +++++++++++++++++++++++++++++++-------- unit/test-crc.c | 123 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 206 insertions(+), 34 deletions(-) diff --git a/include/avtp/acf/Crc.h b/include/avtp/acf/Crc.h index c650871..89cd55e 100644 --- a/include/avtp/acf/Crc.h +++ b/include/avtp/acf/Crc.h @@ -54,6 +54,13 @@ extern "C" { /** * Length of ACF_CRC message header in bytes. + * + * Note: The ACF_CRC PDU included the CRC data field at the end of the PDU. + * But the CRC data field is variable-length and can be of different sizes depending on the CRC + * type. E.g., for CRC_ETH and CRC_32P4 types, the length of the CRC data is 4 bytes. For CRC_USER + * type, the length of the CRC data can be variable and is determined by the user. + * + * We treat the CRC data as a kind of payload, hence the header length is defined as 4 bytes. */ #define AVTP_CRC_HEADER_LEN (1 * AVTP_QUADLET_SIZE) @@ -76,6 +83,9 @@ typedef enum { /* ACF_CRC header fields */ AVTP_CRC_FIELD_RESERVED, AVTP_CRC_FIELD_CRC_TYPE, + /* This is a variable-length field and is used to store the first 4-bytes of CRC data. + This field goes beyond the ACF_CRC header and is treated as a payload. */ + AVTP_CRC_FIELD_CRC_DATA, /* Count number of fields for bound checks */ AVTP_CRC_FIELD_MAX } Avtp_CrcFields_t; @@ -99,6 +109,9 @@ static const Avtp_FieldDescriptor_t Avtp_CrcFieldDesc[AVTP_CRC_FIELD_MAX] = { /* ACF_CRC header fields */ [AVTP_CRC_FIELD_RESERVED] = {.quadlet = 0, .offset = 16, .bits = 12}, [AVTP_CRC_FIELD_CRC_TYPE] = {.quadlet = 0, .offset = 28, .bits = 4}, + /* This is a variable-length field but has a minimum length of 32 bits (4 bytes) */ + [AVTP_CRC_FIELD_CRC_DATA] = {.quadlet = 1, .offset = 0, .bits = 32}, + }; /** @@ -123,8 +136,24 @@ OPEN1722_INLINE Avtp_CrcType_t Avtp_Crc_GetCrcType(const Avtp_Crc_t *const pdu) return (Avtp_CrcType_t)GET_CRC_FIELD(AVTP_CRC_FIELD_CRC_TYPE); } +/** + * Returns the CRC data field from an ACF_CRC PDU. + * Use this function to access the CRC data ONLY if it is 4 bytes long. + * This will be the case for CRC_ETH and CRC_32P4 types. + * For CRC_USER type, use Avtp_Crc_GetPayload() to access the variable-length CRC data. + * + * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. + * @return CRC data field as a 32-bit unsigned integer. + */ +OPEN1722_INLINE uint32_t Avtp_Crc_GetCrcData(const Avtp_Crc_t *const pdu) +{ + return (uint32_t)GET_CRC_FIELD(AVTP_CRC_FIELD_CRC_DATA); +} + /** * Returns pointer to payload of an ACF CRC frame. + * Use this function to access the CRC data if it is more than 4 bytes long. + * Else use Avtp_Crc_GetCrcData() to access the first 4 bytes of CRC data. * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @return Pointer to ACF CRC frame payload @@ -135,7 +164,9 @@ OPEN1722_INLINE const uint8_t *Avtp_Crc_GetPayload(const Avtp_Crc_t *const pdu) } /** - * Sets the CRC payload in an ACF CRC frame. + * Sets the CRC Data in an ACF CRC frame. + * Use this function to set the CRC data ONLY if it is more than 4 bytes long. + * Else use Avtp_Crc_SetCrcData() to set the first 4 bytes * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @param payload Pointer to the payload byte array @@ -146,14 +177,29 @@ OPEN1722_INLINE void Avtp_Crc_SetPayload(Avtp_Crc_t *pdu, uint8_t *payload, uint memcpy(pdu->payload, payload, payload_length); } +/** + * Sets the CRC Data in an ACF CRC frame. + * Use this function to set the CRC data ONLY if it is 4 bytes long. + * This will be the case for CRC_ETH and CRC_32P4 types. + * For CRC_USER type, use Avtp_Crc_SetPayload() to set the variable-length CRC data. + * + * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. + * @param crc_data The CRC data as a 32-bit unsigned integer. + */ +OPEN1722_INLINE void Avtp_Crc_SetCrcData(Avtp_Crc_t *pdu, uint32_t crc_data) +{ + SET_CRC_FIELD(AVTP_CRC_FIELD_CRC_DATA, crc_data); +} + /** * Finalizes the ACF CRC frame. This function sets the quadlet-encoded - * length field based on the given payload length and zeroes the trailing - * padding octets. + * length field based on the given payload length. * - * The ACF CRC format has no explicit pad field. The number of padding - * octets is determined by the payload length. Trailing padding octets - * are set to zero to make the ACF CRC frame a multiple of 4 octets in length. + * The ACF CRC format has no explicit pad field. The spec. requires that + * the payload is quadlet-aligned. For sake of simplicity, this function + * will pad the payload with zeroes to ensure quadlet-alignment. The caller + * must ensure that the buffer containing the ACF CRC frame is large enough to + * accommodate the padded payload. * * @param pdu Pointer to the first bit of an 1722 ACF CRC PDU. * @param payload_length Length of the CRC payload. @@ -169,19 +215,22 @@ OPEN1722_INLINE void Avtp_Crc_SetPayloadLength(Avtp_Crc_t *pdu, uint16_t payload Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)pdu, msgLenQuadlets); } -/* - * Note: there is intentionally no Avtp_Crc_GetPayloadLength() function. - * - * The ACF CRC header carries neither a pad field nor a payload-length field, so - * the true payload length cannot be recovered from the wire format alone. +/** + * Returns the length of the CRC payload. * - * If the CRC type is AVTP_CRC_TYPE_CRC_ETH, the payload length is always 4 bytes. - * If the CRC type is AVTP_CRC_TYPE_CRC_32P4, the payload length is always 4 bytes. + * Precondition: the caller must have validated the PDU with + * Avtp_Crc_IsValid(). This function performs no further bounds checking + * and assumes those invariants already hold. * - * If the CRC type is AVTP_CRC_TYPE_CRC_USER, the payload length is variable and - * must be tracked by the caller. There is no way to determine the payload length - * from the ACF CRC header alone. + * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. + * @return Length of CRC payload in bytes */ +OPEN1722_INLINE uint16_t Avtp_Crc_GetPayloadLength(const Avtp_Crc_t *const pdu) +{ + uint16_t acf_length_bytes = + Avtp_AcfCommon_GetAcfMsgLengthInBytes((const Avtp_AcfCommon_t *)pdu); + return (uint16_t)(acf_length_bytes - AVTP_CRC_HEADER_LEN); +} /** * Initializes an ACF CRC PDU header as specified in the IEEE 1722 Specification. @@ -199,10 +248,16 @@ OPEN1722_INLINE void Avtp_Crc_Init(Avtp_Crc_t *pdu) /** * Copies the payload data and CRC type into the ACF CRC frame. * + * Note: CRC_ETH and CRC_32P4 types have a fixed payload length of 4 bytes. + * The caller must ensure that the payload length is 4 bytes for these types and that the + * endianness of the payload is correct. This function will not perform any validation on + * the payload length for these types. + * * @param pdu Pointer to the first bit of an 1722 ACF CRC PDU. * @param crc_type CRC type * @param payload Pointer to the payload byte array - * @param payload_length Length of the payload. + * @param payload_length Length of the payload. This will be considered only for CRC_USER type. For + * CRC_ETH and CRC_32P4 types, the payload length must be 4 bytes. */ OPEN1722_INLINE void Avtp_Crc_CreateAcfMessage(Avtp_Crc_t *pdu, Avtp_CrcType_t crc_type, uint8_t *payload, uint16_t payload_length) @@ -213,11 +268,24 @@ OPEN1722_INLINE void Avtp_Crc_CreateAcfMessage(Avtp_Crc_t *pdu, Avtp_CrcType_t c // Set the CRC type Avtp_Crc_SetCrcType(pdu, crc_type); - // Copy the payload into the CRC PDU - Avtp_Crc_SetPayload(pdu, payload, payload_length); + // Based on the CRC type, set the payload data + if (crc_type == AVTP_CRC_TYPE_CRC_ETH || crc_type == AVTP_CRC_TYPE_CRC_32P4) { + // For CRC_ETH and CRC_32P4 types, the payload length must be 4 bytes. + // The function will only set the first 4 bytes of the payload as the CRC data. + // The endianness of the payload will be adjusted based on the host endianness. + uint32_t crc_data; + memcpy(&crc_data, payload, sizeof(crc_data)); + Avtp_Crc_SetCrcData(pdu, crc_data); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)pdu, + (uint16_t)((AVTP_CRC_HEADER_LEN + 4) / 4)); + } else { + // For CRC_USER type, the payload length can be variable. + // The function will copy the entire payload into the ACF CRC frame. + Avtp_Crc_SetPayload(pdu, payload, payload_length); - // Finalize the AVTP CRC Frame - Avtp_Crc_SetPayloadLength(pdu, payload_length); + // Finalize the AVTP CRC Frame + Avtp_Crc_SetPayloadLength(pdu, payload_length); + } } /** @@ -250,12 +318,13 @@ OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t buffer return false; } - if (msg_length_bytes < AVTP_CRC_HEADER_LEN) { + // Check if the message length is at least the header length plus 4 bytes for the CRC data + if (msg_length_bytes < AVTP_CRC_HEADER_LEN + 4) { return false; } - if (Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_ETH || - Avtp_Crc_GetCrcType(pdu) == AVTP_CRC_TYPE_CRC_32P4) { + Avtp_CrcType_t crc_type = Avtp_Crc_GetCrcType(pdu); + if (crc_type == AVTP_CRC_TYPE_CRC_ETH || crc_type == AVTP_CRC_TYPE_CRC_32P4) { if (msg_length_bytes != (AVTP_CRC_HEADER_LEN + 4)) { return false; } diff --git a/unit/test-crc.c b/unit/test-crc.c index 608a06d..9c1e6ca 100644 --- a/unit/test-crc.c +++ b/unit/test-crc.c @@ -63,14 +63,13 @@ static void crc_init(void **state) static void crc_field_descriptors_cover_header(void **state) { (void)state; - uint8_t coverage[AVTP_CRC_HEADER_LEN * 8] = {0}; + uint8_t coverage[(AVTP_CRC_HEADER_LEN + AVTP_QUADLET_SIZE) * 8] = {0}; for (uint8_t field = 0; field < AVTP_CRC_FIELD_MAX; field++) { const Avtp_FieldDescriptor_t *desc = &Avtp_CrcFieldDesc[field]; - assert_int_equal(desc->quadlet, 0); for (uint8_t bit = 0; bit < desc->bits; bit++) { - size_t bit_index = desc->offset + bit; + size_t bit_index = (desc->quadlet * 32) + desc->offset + bit; assert_true(bit_index < sizeof(coverage)); assert_int_equal(coverage[bit_index], 0); @@ -137,16 +136,16 @@ static void crc_create_message(void **state) { (void)state; uint8_t pdu[MAX_PDU_SIZE]; - uint8_t payload[] = {0xDE, 0xAD, 0xBE, 0xEF}; + uint32_t crcdata = 0xdeadbeef; Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; memset(pdu, 0xAA, sizeof(pdu)); - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, (uint8_t *)&crcdata, sizeof(crcdata)); assert_int_equal(Avtp_AcfCommon_GetAcfMsgType((Avtp_AcfCommon_t *)crc), AVTP_ACF_TYPE_CRC); assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_ETH); assert_int_equal(Avtp_AcfCommon_GetAcfMsgLengthInBytes((Avtp_AcfCommon_t *)crc), 8); - assert_memory_equal(Avtp_Crc_GetPayload(crc), payload, sizeof(payload)); + assert_int_equal(Avtp_Crc_GetCrcData(crc), crcdata); assert_true(Avtp_Crc_IsValid(crc, 8)); } @@ -182,7 +181,8 @@ static void crc_is_valid_fixed_length_types(void **state) uint8_t pdu[MAX_PDU_SIZE]; uint8_t payload[8] = {0}; Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; - const Avtp_CrcType_t types[] = {AVTP_CRC_TYPE_CRC_ETH, AVTP_CRC_TYPE_CRC_32P4}; + const Avtp_CrcType_t types[] = {AVTP_CRC_TYPE_CRC_ETH, AVTP_CRC_TYPE_CRC_32P4, + AVTP_CRC_TYPE_CRC_USER}; for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); i++) { Avtp_CrcType_t type = types[i]; @@ -192,11 +192,17 @@ static void crc_is_valid_fixed_length_types(void **state) /* Too short: header only, and below the fixed length. */ Avtp_Crc_CreateAcfMessage(crc, type, payload, 0); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0); assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); /* Too long: fits the buffer but exceeds the fixed length. */ - Avtp_Crc_CreateAcfMessage(crc, type, payload, 8); - assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + Avtp_Crc_CreateAcfMessage(crc, type, payload, 12); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 3); + if (type == AVTP_CRC_TYPE_CRC_ETH || type == AVTP_CRC_TYPE_CRC_32P4) { + assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + } else { + assert_true(Avtp_Crc_IsValid(crc, sizeof(pdu))); + } /* Zero length */ Avtp_Crc_CreateAcfMessage(crc, type, payload, 8); @@ -213,12 +219,104 @@ static void crc_is_valid_user_type_variable_length(void **state) uint8_t payload[16] = {0}; Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; - for (uint16_t payload_length = 0; payload_length <= sizeof(payload); payload_length += 4) { + for (uint16_t payload_length = 0; payload_length < 4; payload_length += 4) { + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_USER, payload, payload_length); + assert_false(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN + payload_length)); + } + + for (uint16_t payload_length = 8; payload_length <= sizeof(payload); payload_length += 4) { Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_USER, payload, payload_length); assert_true(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN + payload_length)); } } +static void crc_get_payload_length_fixed_types(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + uint8_t payload[4] = {0xDE, 0xAD, 0xBE, 0xEF}; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + assert_int_equal(Avtp_Crc_GetPayloadLength(crc), 4); + + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_32P4, payload, sizeof(payload)); + assert_int_equal(Avtp_Crc_GetPayloadLength(crc), 4); +} + +/* There is no pad field, so the returned length includes the zero padding. */ +static void crc_get_payload_length_user_type_includes_padding(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + uint8_t payload[16] = {0}; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + for (uint16_t payload_length = 0; payload_length <= sizeof(payload); payload_length++) { + uint16_t padded_length = + ((payload_length + AVTP_QUADLET_SIZE - 1) / AVTP_QUADLET_SIZE) * AVTP_QUADLET_SIZE; + + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_USER, payload, payload_length); + assert_int_equal(Avtp_Crc_GetPayloadLength(crc), padded_length); + } +} + +static void crc_get_payload_length_from_message_length(void **state) +{ + (void)state; + uint8_t pdu[MAX_PDU_SIZE]; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + /* Header only, no payload. */ + Avtp_Crc_Init(crc); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 1); + assert_int_equal(Avtp_Crc_GetPayloadLength(crc), 0); + + /* Lengths above 255 bytes must not be truncated to uint8_t. */ + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 101); + assert_int_equal(Avtp_Crc_GetPayloadLength(crc), 400); + + /* Maximum 9-bit quadlet length. */ + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0x1FF); + assert_int_equal(Avtp_Crc_GetPayloadLength(crc), + 0x1FF * AVTP_QUADLET_SIZE - AVTP_CRC_HEADER_LEN); +} + +/* Init + CRC_32P4 + 4-byte CRC data. */ +static void crc_wire_format(void **state) +{ + (void)state; + const uint8_t expected[] = {0xEE, 0x02, 0x00, 0x01, 0x11, 0x22, 0x33, 0x44}; + uint8_t pdu[MAX_PDU_SIZE]; + Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; + + memset(pdu, 0xAA, sizeof(pdu)); + Avtp_Crc_Init(crc); + Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_32P4); + Avtp_Crc_SetCrcData(crc, 0x11223344); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 2); + assert_memory_equal(pdu, expected, sizeof(expected)); + assert_int_equal(pdu[sizeof(expected)], 0xAA); + + assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_32P4); + assert_int_equal(Avtp_Crc_GetCrcData(crc), 0x11223344); + assert_true(Avtp_Crc_IsValid(crc, sizeof(expected))); +} + +/* CreateAcfMessage takes the CRC data as a host-order integer, so the wire bytes are the same. */ +static void crc_wire_format_create_message(void **state) +{ + (void)state; + const uint8_t expected[] = {0xEE, 0x02, 0x00, 0x01, 0x11, 0x22, 0x33, 0x44}; + uint8_t pdu[MAX_PDU_SIZE]; + uint32_t crc_data = 0x11223344; + + memset(pdu, 0xAA, sizeof(pdu)); + Avtp_Crc_CreateAcfMessage((Avtp_Crc_t *)pdu, AVTP_CRC_TYPE_CRC_32P4, (uint8_t *)&crc_data, + sizeof(crc_data)); + assert_memory_equal(pdu, expected, sizeof(expected)); +} + int main(void) { const struct CMUnitTest tests[] = { @@ -230,6 +328,11 @@ int main(void) cmocka_unit_test(crc_is_valid), cmocka_unit_test(crc_is_valid_fixed_length_types), cmocka_unit_test(crc_is_valid_user_type_variable_length), + cmocka_unit_test(crc_get_payload_length_fixed_types), + cmocka_unit_test(crc_get_payload_length_user_type_includes_padding), + cmocka_unit_test(crc_get_payload_length_from_message_length), + cmocka_unit_test(crc_wire_format), + cmocka_unit_test(crc_wire_format_create_message), }; return cmocka_run_group_tests(tests, NULL, NULL); From c5a62963516d3a6bb6e94a2ea078fd0fb0177d18 Mon Sep 17 00:00:00 2001 From: Naresh Nayak Date: Mon, 2 Nov 2026 19:24:42 +0000 Subject: [PATCH 5/6] Adjusted the CRC based on the reviews from #228 - CRC Data is now handled using an accessor - AcfCreateMessage creates only PDUs for fixed length CRC - CRC Data does not overlap with header fields. Signed-off-by: Naresh Nayak --- include/avtp/acf/Crc.h | 110 ++++++++++++++++++++++------------------- unit/test-crc.c | 58 +++++++++++++--------- 2 files changed, 95 insertions(+), 73 deletions(-) diff --git a/include/avtp/acf/Crc.h b/include/avtp/acf/Crc.h index 89cd55e..38ef1b7 100644 --- a/include/avtp/acf/Crc.h +++ b/include/avtp/acf/Crc.h @@ -48,22 +48,22 @@ extern "C" { #include "avtp/acf/AcfCommon.h" #define GET_CRC_FIELD(field) \ - (Avtp_GetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, (const uint8_t *)pdu, field)) + (Avtp_GetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, (const uint8_t *)pdu, field)) #define SET_CRC_FIELD(field, value) \ - (Avtp_SetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, (uint8_t *)pdu, field, value)) + (Avtp_SetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, (uint8_t *)pdu, field, value)) /** * Length of ACF_CRC message header in bytes. * - * Note: The ACF_CRC PDU included the CRC data field at the end of the PDU. - * But the CRC data field is variable-length and can be of different sizes depending on the CRC - * type. E.g., for CRC_ETH and CRC_32P4 types, the length of the CRC data is 4 bytes. For CRC_USER - * type, the length of the CRC data can be variable and is determined by the user. - * - * We treat the CRC data as a kind of payload, hence the header length is defined as 4 bytes. + * We treat the CRC data as payload, hence the header length is defined as 4 bytes. */ #define AVTP_CRC_HEADER_LEN (1 * AVTP_QUADLET_SIZE) +/** + * Length of ACF_CRC message with minimum payload in bytes. + */ +#define AVTP_CRC_PAYLOAD_MIN_LEN (1 * AVTP_QUADLET_SIZE) + /** * ACF_CRC message structure suitable for in-place de-/serialization of fields * in the ACF_CRC message header. @@ -83,13 +83,20 @@ typedef enum { /* ACF_CRC header fields */ AVTP_CRC_FIELD_RESERVED, AVTP_CRC_FIELD_CRC_TYPE, - /* This is a variable-length field and is used to store the first 4-bytes of CRC data. - This field goes beyond the ACF_CRC header and is treated as a payload. */ - AVTP_CRC_FIELD_CRC_DATA, /* Count number of fields for bound checks */ AVTP_CRC_FIELD_MAX } Avtp_CrcFields_t; +/** + * Fields encoded in the ACF_CRC Payload. + */ +typedef enum { + /* ACF_CRC payload fields */ + AVTP_CRC_PAYLOAD_FIELD_CRC_DATA = 0, + /* Count number of fields for bound checks */ + AVTP_CRC_PAYLOAD_FIELD_MAX +} Avtp_CrcPayloadFields_t; + /** * CRC types encoded in the ACF_CRC header. */ @@ -102,16 +109,24 @@ typedef enum { /** * This table describes all the offsets of the ACF_CRC header fields. */ -static const Avtp_FieldDescriptor_t Avtp_CrcFieldDesc[AVTP_CRC_FIELD_MAX] = { +static const Avtp_FieldDescriptor_t Avtp_CrcHeaderFieldDesc[AVTP_CRC_FIELD_MAX] = { /* ACF common header fields */ [AVTP_CRC_FIELD_ACF_MSG_TYPE] = {.quadlet = 0, .offset = 0, .bits = 7}, [AVTP_CRC_FIELD_ACF_MSG_LENGTH] = {.quadlet = 0, .offset = 7, .bits = 9}, /* ACF_CRC header fields */ [AVTP_CRC_FIELD_RESERVED] = {.quadlet = 0, .offset = 16, .bits = 12}, [AVTP_CRC_FIELD_CRC_TYPE] = {.quadlet = 0, .offset = 28, .bits = 4}, - /* This is a variable-length field but has a minimum length of 32 bits (4 bytes) */ - [AVTP_CRC_FIELD_CRC_DATA] = {.quadlet = 1, .offset = 0, .bits = 32}, +}; +/** + * This table describes all the offsets of the ACF_CRC payload fields. + */ +static const Avtp_FieldDescriptor_t Avtp_CrcPayloadFieldDesc[AVTP_CRC_PAYLOAD_FIELD_MAX] = { + /* This is a variable-length field but has a minimum length of 32 bits (4 bytes) + Depending on the type of the CRC, the payload may be longer than 4 bytes. + The CRC data is always quadlet-aligned. + */ + [AVTP_CRC_PAYLOAD_FIELD_CRC_DATA] = {.quadlet = 1, .offset = 0, .bits = 32}, }; /** @@ -142,18 +157,22 @@ OPEN1722_INLINE Avtp_CrcType_t Avtp_Crc_GetCrcType(const Avtp_Crc_t *const pdu) * This will be the case for CRC_ETH and CRC_32P4 types. * For CRC_USER type, use Avtp_Crc_GetPayload() to access the variable-length CRC data. * + * Note: The returned value is in host endian format. + * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @return CRC data field as a 32-bit unsigned integer. */ OPEN1722_INLINE uint32_t Avtp_Crc_GetCrcData(const Avtp_Crc_t *const pdu) { - return (uint32_t)GET_CRC_FIELD(AVTP_CRC_FIELD_CRC_DATA); + return (uint32_t)Avtp_GetField(Avtp_CrcPayloadFieldDesc, AVTP_CRC_PAYLOAD_FIELD_MAX, + (const uint8_t *)pdu, AVTP_CRC_PAYLOAD_FIELD_CRC_DATA); } /** * Returns pointer to payload of an ACF CRC frame. * Use this function to access the CRC data if it is more than 4 bytes long. - * Else use Avtp_Crc_GetCrcData() to access the first 4 bytes of CRC data. + * This is the case for CRC_USER type, which has a variable-length payload. + * For CRC_ETH and CRC_32P4 types, use Avtp_Crc_GetCrcData() to access CRC data. * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @return Pointer to ACF CRC frame payload @@ -166,7 +185,8 @@ OPEN1722_INLINE const uint8_t *Avtp_Crc_GetPayload(const Avtp_Crc_t *const pdu) /** * Sets the CRC Data in an ACF CRC frame. * Use this function to set the CRC data ONLY if it is more than 4 bytes long. - * Else use Avtp_Crc_SetCrcData() to set the first 4 bytes + * This is the case for CRC_USER type, which has a variable-length payload. + * For CRC_ETH and CRC_32P4 types, use Avtp_Crc_SetCrcData() to set CRC data. * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @param payload Pointer to the payload byte array @@ -183,12 +203,15 @@ OPEN1722_INLINE void Avtp_Crc_SetPayload(Avtp_Crc_t *pdu, uint8_t *payload, uint * This will be the case for CRC_ETH and CRC_32P4 types. * For CRC_USER type, use Avtp_Crc_SetPayload() to set the variable-length CRC data. * + * Note: This function takes the CRC data using host endian format. + * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @param crc_data The CRC data as a 32-bit unsigned integer. */ OPEN1722_INLINE void Avtp_Crc_SetCrcData(Avtp_Crc_t *pdu, uint32_t crc_data) { - SET_CRC_FIELD(AVTP_CRC_FIELD_CRC_DATA, crc_data); + Avtp_SetField(Avtp_CrcPayloadFieldDesc, AVTP_CRC_PAYLOAD_FIELD_MAX, (uint8_t *)pdu, + AVTP_CRC_PAYLOAD_FIELD_CRC_DATA, crc_data); } /** @@ -196,10 +219,10 @@ OPEN1722_INLINE void Avtp_Crc_SetCrcData(Avtp_Crc_t *pdu, uint32_t crc_data) * length field based on the given payload length. * * The ACF CRC format has no explicit pad field. The spec. requires that - * the payload is quadlet-aligned. For sake of simplicity, this function - * will pad the payload with zeroes to ensure quadlet-alignment. The caller - * must ensure that the buffer containing the ACF CRC frame is large enough to - * accommodate the padded payload. + * the payload is quadlet-aligned. Nonetheless, this function will pad the payload + * with zeroes to ensure quadlet-alignment. The caller must ensure that the buffer + * containing the ACF CRC frame is large enough to accommodate the padded payload or + * make sure that the payload length is already quadlet-aligned. * * @param pdu Pointer to the first bit of an 1722 ACF CRC PDU. * @param payload_length Length of the CRC payload. @@ -248,19 +271,14 @@ OPEN1722_INLINE void Avtp_Crc_Init(Avtp_Crc_t *pdu) /** * Copies the payload data and CRC type into the ACF CRC frame. * - * Note: CRC_ETH and CRC_32P4 types have a fixed payload length of 4 bytes. - * The caller must ensure that the payload length is 4 bytes for these types and that the - * endianness of the payload is correct. This function will not perform any validation on - * the payload length for these types. + * Note: This function will create a valid ACF CRC frame for CRC_ETH and CRC_32P4 types only. * * @param pdu Pointer to the first bit of an 1722 ACF CRC PDU. * @param crc_type CRC type - * @param payload Pointer to the payload byte array - * @param payload_length Length of the payload. This will be considered only for CRC_USER type. For - * CRC_ETH and CRC_32P4 types, the payload length must be 4 bytes. + * @param crc_data 32-bit CRC data for CRC_ETH and CRC_32P4 types */ OPEN1722_INLINE void Avtp_Crc_CreateAcfMessage(Avtp_Crc_t *pdu, Avtp_CrcType_t crc_type, - uint8_t *payload, uint16_t payload_length) + uint32_t crc_data) { // Initialize the ACF CRC header Avtp_Crc_Init(pdu); @@ -268,24 +286,12 @@ OPEN1722_INLINE void Avtp_Crc_CreateAcfMessage(Avtp_Crc_t *pdu, Avtp_CrcType_t c // Set the CRC type Avtp_Crc_SetCrcType(pdu, crc_type); - // Based on the CRC type, set the payload data - if (crc_type == AVTP_CRC_TYPE_CRC_ETH || crc_type == AVTP_CRC_TYPE_CRC_32P4) { - // For CRC_ETH and CRC_32P4 types, the payload length must be 4 bytes. - // The function will only set the first 4 bytes of the payload as the CRC data. - // The endianness of the payload will be adjusted based on the host endianness. - uint32_t crc_data; - memcpy(&crc_data, payload, sizeof(crc_data)); - Avtp_Crc_SetCrcData(pdu, crc_data); - Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)pdu, - (uint16_t)((AVTP_CRC_HEADER_LEN + 4) / 4)); - } else { - // For CRC_USER type, the payload length can be variable. - // The function will copy the entire payload into the ACF CRC frame. - Avtp_Crc_SetPayload(pdu, payload, payload_length); - - // Finalize the AVTP CRC Frame - Avtp_Crc_SetPayloadLength(pdu, payload_length); - } + // Set the CRC data + Avtp_Crc_SetCrcData(pdu, crc_data); + + // Set the message length field in the ACF common header + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)pdu, + (AVTP_CRC_HEADER_LEN + AVTP_CRC_PAYLOAD_MIN_LEN) / 4); } /** @@ -303,7 +309,7 @@ OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t buffer return false; } - if (bufferSize < AVTP_CRC_HEADER_LEN) { + if (bufferSize < AVTP_CRC_HEADER_LEN + AVTP_CRC_PAYLOAD_MIN_LEN) { return false; } @@ -318,11 +324,13 @@ OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t buffer return false; } - // Check if the message length is at least the header length plus 4 bytes for the CRC data - if (msg_length_bytes < AVTP_CRC_HEADER_LEN + 4) { + // Check if the message length is at least the header length plus the minimum payload length + if (msg_length_bytes < AVTP_CRC_HEADER_LEN + AVTP_CRC_PAYLOAD_MIN_LEN) { return false; } + // For CRC_ETH and CRC_32P4 types, the message length must be exactly 8 bytes (header + 4-byte + // payload) Avtp_CrcType_t crc_type = Avtp_Crc_GetCrcType(pdu); if (crc_type == AVTP_CRC_TYPE_CRC_ETH || crc_type == AVTP_CRC_TYPE_CRC_32P4) { if (msg_length_bytes != (AVTP_CRC_HEADER_LEN + 4)) { diff --git a/unit/test-crc.c b/unit/test-crc.c index 9c1e6ca..2ab4657 100644 --- a/unit/test-crc.c +++ b/unit/test-crc.c @@ -63,11 +63,12 @@ static void crc_init(void **state) static void crc_field_descriptors_cover_header(void **state) { (void)state; - uint8_t coverage[(AVTP_CRC_HEADER_LEN + AVTP_QUADLET_SIZE) * 8] = {0}; + uint8_t coverage[AVTP_CRC_HEADER_LEN * 8] = {0}; for (uint8_t field = 0; field < AVTP_CRC_FIELD_MAX; field++) { - const Avtp_FieldDescriptor_t *desc = &Avtp_CrcFieldDesc[field]; + const Avtp_FieldDescriptor_t *desc = &Avtp_CrcHeaderFieldDesc[field]; + assert_int_equal(desc->quadlet, 0); for (uint8_t bit = 0; bit < desc->bits; bit++) { size_t bit_index = (desc->quadlet * 32) + desc->offset + bit; @@ -94,9 +95,10 @@ static void crc_fields(void **state) Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_32P4); assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_32P4); - Avtp_SetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED, 0xABC); + Avtp_SetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED, 0xABC); assert_int_equal( - Avtp_GetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED), 0xABC); + Avtp_GetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED), + 0xABC); assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_32P4); Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 1); @@ -140,7 +142,7 @@ static void crc_create_message(void **state) Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; memset(pdu, 0xAA, sizeof(pdu)); - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, (uint8_t *)&crcdata, sizeof(crcdata)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, crcdata); assert_int_equal(Avtp_AcfCommon_GetAcfMsgType((Avtp_AcfCommon_t *)crc), AVTP_ACF_TYPE_CRC); assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_ETH); @@ -154,11 +156,11 @@ static void crc_is_valid(void **state) (void)state; uint8_t pdu[MAX_PDU_SIZE]; Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; - uint8_t payload[] = {0, 1, 2, 3}; + uint32_t payload = 0xDEADBEEF; assert_false(Avtp_Crc_IsValid(NULL, sizeof(pdu))); - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload); assert_true(Avtp_Crc_IsValid(crc, 8)); assert_false(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN - 1)); @@ -166,11 +168,15 @@ static void crc_is_valid(void **state) Avtp_AcfCommon_SetAcfMsgType((Avtp_AcfCommon_t *)crc, AVTP_ACF_TYPE_GPC); assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload); Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0); assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 12); + assert_false(Avtp_Crc_IsValid(crc, 8)); + + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload); assert_false(Avtp_Crc_IsValid(crc, 7)); } @@ -179,7 +185,7 @@ static void crc_is_valid_fixed_length_types(void **state) { (void)state; uint8_t pdu[MAX_PDU_SIZE]; - uint8_t payload[8] = {0}; + uint32_t payload = 0xDEADBEEF; Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; const Avtp_CrcType_t types[] = {AVTP_CRC_TYPE_CRC_ETH, AVTP_CRC_TYPE_CRC_32P4, AVTP_CRC_TYPE_CRC_USER}; @@ -187,16 +193,16 @@ static void crc_is_valid_fixed_length_types(void **state) for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); i++) { Avtp_CrcType_t type = types[i]; - Avtp_Crc_CreateAcfMessage(crc, type, payload, 4); + Avtp_Crc_CreateAcfMessage(crc, type, payload); assert_true(Avtp_Crc_IsValid(crc, sizeof(pdu))); /* Too short: header only, and below the fixed length. */ - Avtp_Crc_CreateAcfMessage(crc, type, payload, 0); + Avtp_Crc_CreateAcfMessage(crc, type, payload); Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0); assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); /* Too long: fits the buffer but exceeds the fixed length. */ - Avtp_Crc_CreateAcfMessage(crc, type, payload, 12); + Avtp_Crc_CreateAcfMessage(crc, type, payload); Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 3); if (type == AVTP_CRC_TYPE_CRC_ETH || type == AVTP_CRC_TYPE_CRC_32P4) { assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); @@ -205,7 +211,7 @@ static void crc_is_valid_fixed_length_types(void **state) } /* Zero length */ - Avtp_Crc_CreateAcfMessage(crc, type, payload, 8); + Avtp_Crc_CreateAcfMessage(crc, type, payload); Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0); assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); } @@ -220,12 +226,18 @@ static void crc_is_valid_user_type_variable_length(void **state) Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; for (uint16_t payload_length = 0; payload_length < 4; payload_length += 4) { - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_USER, payload, payload_length); + Avtp_Crc_Init(crc); + Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_USER); + Avtp_Crc_SetPayload(crc, payload, payload_length); + Avtp_Crc_SetPayloadLength(crc, payload_length); assert_false(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN + payload_length)); } for (uint16_t payload_length = 8; payload_length <= sizeof(payload); payload_length += 4) { - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_USER, payload, payload_length); + Avtp_Crc_Init(crc); + Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_USER); + Avtp_Crc_SetPayload(crc, payload, payload_length); + Avtp_Crc_SetPayloadLength(crc, payload_length); assert_true(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN + payload_length)); } } @@ -234,13 +246,13 @@ static void crc_get_payload_length_fixed_types(void **state) { (void)state; uint8_t pdu[MAX_PDU_SIZE]; - uint8_t payload[4] = {0xDE, 0xAD, 0xBE, 0xEF}; + uint32_t payload = 0xDEADBEEF; Avtp_Crc_t *crc = (Avtp_Crc_t *)pdu; - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, sizeof(payload)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload); assert_int_equal(Avtp_Crc_GetPayloadLength(crc), 4); - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_32P4, payload, sizeof(payload)); + Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_32P4, payload); assert_int_equal(Avtp_Crc_GetPayloadLength(crc), 4); } @@ -256,7 +268,10 @@ static void crc_get_payload_length_user_type_includes_padding(void **state) uint16_t padded_length = ((payload_length + AVTP_QUADLET_SIZE - 1) / AVTP_QUADLET_SIZE) * AVTP_QUADLET_SIZE; - Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_USER, payload, payload_length); + Avtp_Crc_Init(crc); + Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_USER); + Avtp_Crc_SetPayload(crc, payload, payload_length); + Avtp_Crc_SetPayloadLength(crc, payload_length); assert_int_equal(Avtp_Crc_GetPayloadLength(crc), padded_length); } } @@ -312,8 +327,7 @@ static void crc_wire_format_create_message(void **state) uint32_t crc_data = 0x11223344; memset(pdu, 0xAA, sizeof(pdu)); - Avtp_Crc_CreateAcfMessage((Avtp_Crc_t *)pdu, AVTP_CRC_TYPE_CRC_32P4, (uint8_t *)&crc_data, - sizeof(crc_data)); + Avtp_Crc_CreateAcfMessage((Avtp_Crc_t *)pdu, AVTP_CRC_TYPE_CRC_32P4, crc_data); assert_memory_equal(pdu, expected, sizeof(expected)); } From 193435934a38228e50c53a50d97f375840216cf2 Mon Sep 17 00:00:00 2001 From: Naresh Nayak Date: Fri, 20 Nov 2026 14:48:06 +0000 Subject: [PATCH 6/6] Improved comments and tests for ACF_CRC. Signed-off-by: Naresh Nayak --- include/avtp/acf/Crc.h | 26 ++++++++++++++------------ unit/test-crc.c | 11 ++++++----- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/include/avtp/acf/Crc.h b/include/avtp/acf/Crc.h index 38ef1b7..808e2e4 100644 --- a/include/avtp/acf/Crc.h +++ b/include/avtp/acf/Crc.h @@ -48,9 +48,9 @@ extern "C" { #include "avtp/acf/AcfCommon.h" #define GET_CRC_FIELD(field) \ - (Avtp_GetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, (const uint8_t *)pdu, field)) + (Avtp_GetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, (const uint8_t *)pdu, field)) #define SET_CRC_FIELD(field, value) \ - (Avtp_SetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, (uint8_t *)pdu, field, value)) + (Avtp_SetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, (uint8_t *)pdu, field, value)) /** * Length of ACF_CRC message header in bytes. @@ -60,7 +60,7 @@ extern "C" { #define AVTP_CRC_HEADER_LEN (1 * AVTP_QUADLET_SIZE) /** - * Length of ACF_CRC message with minimum payload in bytes. + * Minimum length of the ACF_CRC payload in bytes. */ #define AVTP_CRC_PAYLOAD_MIN_LEN (1 * AVTP_QUADLET_SIZE) @@ -109,7 +109,7 @@ typedef enum { /** * This table describes all the offsets of the ACF_CRC header fields. */ -static const Avtp_FieldDescriptor_t Avtp_CrcHeaderFieldDesc[AVTP_CRC_FIELD_MAX] = { +static const Avtp_FieldDescriptor_t Avtp_CrcFieldDesc[AVTP_CRC_FIELD_MAX] = { /* ACF common header fields */ [AVTP_CRC_FIELD_ACF_MSG_TYPE] = {.quadlet = 0, .offset = 0, .bits = 7}, [AVTP_CRC_FIELD_ACF_MSG_LENGTH] = {.quadlet = 0, .offset = 7, .bits = 9}, @@ -170,9 +170,10 @@ OPEN1722_INLINE uint32_t Avtp_Crc_GetCrcData(const Avtp_Crc_t *const pdu) /** * Returns pointer to payload of an ACF CRC frame. - * Use this function to access the CRC data if it is more than 4 bytes long. - * This is the case for CRC_USER type, which has a variable-length payload. - * For CRC_ETH and CRC_32P4 types, use Avtp_Crc_GetCrcData() to access CRC data. + * + * Use this function to access the raw network order CRC data. + * This is useful for CRC_USER types that may have different length than the standard CRC_ETH + * and CRC_32P4 types. For CRC_ETH and CRC_32P4 types, use Avtp_Crc_GetCrcData() to access CRC data. * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @return Pointer to ACF CRC frame payload @@ -184,9 +185,10 @@ OPEN1722_INLINE const uint8_t *Avtp_Crc_GetPayload(const Avtp_Crc_t *const pdu) /** * Sets the CRC Data in an ACF CRC frame. - * Use this function to set the CRC data ONLY if it is more than 4 bytes long. - * This is the case for CRC_USER type, which has a variable-length payload. - * For CRC_ETH and CRC_32P4 types, use Avtp_Crc_SetCrcData() to set CRC data. + * + * Use this function to set CRC data directly. Bytes are copied verbatim into the network buffer. + * This is useful for CRC_USER types that may have different length than the standard CRC_ETH and + * CRC_32P4 types. For CRC_ETH and CRC_32P4 types, use Avtp_Crc_SetCrcData() to set CRC data. * * @param pdu Pointer to the first bit of a 1722 ACF CRC PDU. * @param payload Pointer to the payload byte array @@ -269,7 +271,7 @@ OPEN1722_INLINE void Avtp_Crc_Init(Avtp_Crc_t *pdu) } /** - * Copies the payload data and CRC type into the ACF CRC frame. + * Sets the CRC type and 32 bit CRC data. * * Note: This function will create a valid ACF CRC frame for CRC_ETH and CRC_32P4 types only. * @@ -333,7 +335,7 @@ OPEN1722_INLINE bool Avtp_Crc_IsValid(const Avtp_Crc_t *const pdu, size_t buffer // payload) Avtp_CrcType_t crc_type = Avtp_Crc_GetCrcType(pdu); if (crc_type == AVTP_CRC_TYPE_CRC_ETH || crc_type == AVTP_CRC_TYPE_CRC_32P4) { - if (msg_length_bytes != (AVTP_CRC_HEADER_LEN + 4)) { + if (msg_length_bytes != (AVTP_CRC_HEADER_LEN + AVTP_CRC_PAYLOAD_MIN_LEN)) { return false; } } diff --git a/unit/test-crc.c b/unit/test-crc.c index 2ab4657..565b3fc 100644 --- a/unit/test-crc.c +++ b/unit/test-crc.c @@ -66,7 +66,7 @@ static void crc_field_descriptors_cover_header(void **state) uint8_t coverage[AVTP_CRC_HEADER_LEN * 8] = {0}; for (uint8_t field = 0; field < AVTP_CRC_FIELD_MAX; field++) { - const Avtp_FieldDescriptor_t *desc = &Avtp_CrcHeaderFieldDesc[field]; + const Avtp_FieldDescriptor_t *desc = &Avtp_CrcFieldDesc[field]; assert_int_equal(desc->quadlet, 0); for (uint8_t bit = 0; bit < desc->bits; bit++) { @@ -95,10 +95,9 @@ static void crc_fields(void **state) Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_32P4); assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_32P4); - Avtp_SetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED, 0xABC); + Avtp_SetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED, 0xABC); assert_int_equal( - Avtp_GetField(Avtp_CrcHeaderFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED), - 0xABC); + Avtp_GetField(Avtp_CrcFieldDesc, AVTP_CRC_FIELD_MAX, pdu, AVTP_CRC_FIELD_RESERVED), 0xABC); assert_int_equal(Avtp_Crc_GetCrcType(crc), AVTP_CRC_TYPE_CRC_32P4); Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 1); @@ -200,6 +199,8 @@ static void crc_is_valid_fixed_length_types(void **state) Avtp_Crc_CreateAcfMessage(crc, type, payload); Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 0); assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); + Avtp_AcfCommon_SetAcfMsgLength((Avtp_AcfCommon_t *)crc, 1); + assert_false(Avtp_Crc_IsValid(crc, sizeof(pdu))); /* Too long: fits the buffer but exceeds the fixed length. */ Avtp_Crc_CreateAcfMessage(crc, type, payload); @@ -233,7 +234,7 @@ static void crc_is_valid_user_type_variable_length(void **state) assert_false(Avtp_Crc_IsValid(crc, AVTP_CRC_HEADER_LEN + payload_length)); } - for (uint16_t payload_length = 8; payload_length <= sizeof(payload); payload_length += 4) { + for (uint16_t payload_length = 4; payload_length <= sizeof(payload); payload_length += 4) { Avtp_Crc_Init(crc); Avtp_Crc_SetCrcType(crc, AVTP_CRC_TYPE_CRC_USER); Avtp_Crc_SetPayload(crc, payload, payload_length);