Skip to content

Commit f13a63e

Browse files
committed
Require length field for cyphalHeaderCRCAdd function. and fixes to formatting.
1 parent 95dd1e4 commit f13a63e

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

libudpard/udpard.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ typedef uint16_t CyphalHeaderCRC;
190190
#define CYPHAL_HEADER_CRC_INITIAL 0xFFFFU
191191
#define CYPHAL_HEADER_CRC_SIZE_BYTES 2U
192192

193-
UDPARD_PRIVATE CyphalHeaderCRC CyphalHeaderCrcAddByte(const CyphalHeaderCRC crc, const uint8_t byte)
193+
UDPARD_PRIVATE CyphalHeaderCRC cyphalHeaderCrcAddByte(const CyphalHeaderCRC crc, const uint8_t byte)
194194
{
195195
// Based on CRC-16-CCITT-FALSE Function
196196
static const CyphalHeaderCRC Top = 0x8000U;
@@ -205,17 +205,17 @@ UDPARD_PRIVATE CyphalHeaderCRC CyphalHeaderCrcAddByte(const CyphalHeaderCRC crc,
205205
out = (uint16_t) ((uint16_t) (out << 1U) ^ (((out & Top) != 0U) ? Poly : 0U));
206206
out = (uint16_t) ((uint16_t) (out << 1U) ^ (((out & Top) != 0U) ? Poly : 0U));
207207
out = (uint16_t) ((uint16_t) (out << 1U) ^ (((out & Top) != 0U) ? Poly : 0U));
208-
return out;}
208+
return out;
209+
}
209210

210-
UDPARD_PRIVATE CyphalHeaderCRC CyphalHeaderCrcAdd(const CyphalHeaderCRC crc, const void* const header)
211+
UDPARD_PRIVATE CyphalHeaderCRC cyphalHeaderCrcAdd(const CyphalHeaderCRC crc, const size_t size, const void* const header)
211212
{
212213
UDPARD_ASSERT(header != NULL);
213214
CyphalHeaderCRC out = crc;
214215
const uint8_t* p = (const uint8_t*) header;
215-
size_t cyphal_header_size_without_crc = sizeof(UdpardFrameHeader) - CYPHAL_HEADER_CRC_SIZE_BYTES;
216-
for (size_t i = 0; i < cyphal_header_size_without_crc; i++)
216+
for (size_t i = 0; i < size; i++)
217217
{
218-
out = CyphalHeaderCrcAddByte(out, *p);
218+
out = cyphalHeaderCrcAddByte(out, *p);
219219
++p;
220220
}
221221
return out;
@@ -363,12 +363,13 @@ UDPARD_PRIVATE void txMakeFrameHeader(UdpardFrameHeader* const header,
363363
{
364364
UDPARD_ASSERT(frame_index <= UDPARD_MAX_FRAME_INDEX);
365365
uint32_t end_of_transfer_mask = (uint32_t) (end_of_transfer ? 1 : 0) << (uint32_t) UDPARD_END_OF_TRANSFER_OFFSET;
366+
size_t cyphal_header_size_without_crc = sizeof(UdpardFrameHeader) - CYPHAL_HEADER_CRC_SIZE_BYTES;
366367
header->transfer_id = transfer_id;
367368
header->priority = (uint8_t) priority;
368369
header->frame_index_eot = end_of_transfer_mask | frame_index;
369370
header->source_node_id = src_node_id;
370371
header->destination_node_id = dst_node_id;
371-
header->cyphal_header_checksum = CyphalHeaderCrcAdd(CYPHAL_HEADER_CRC_INITIAL, header);
372+
header->cyphal_header_checksum = cyphalHeaderCrcAdd(CYPHAL_HEADER_CRC_INITIAL, cyphal_header_size_without_crc, header);
372373
if (transfer_kind == UdpardTransferKindMessage)
373374
{
374375
header->data_specifier = (uint16_t) UPDARD_DATA_SPECIFIER_MESSAGE & port_id; // SNM (0) + Subject ID

tests/exposed.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extern "C" {
7979

8080
auto crcAdd(const std::uint32_t crc, const std::size_t size, const void* const bytes) -> std::uint32_t;
8181

82-
auto CyphalHeaderCrcAdd(const std::uint16_t crc, const void* const bytes) -> std::uint16_t;
82+
auto cyphalHeaderCrcAdd(const std::uint16_t crc, const std::size_t size, const void* const bytes) -> std::uint16_t;
8383

8484
auto crcValue(const std::uint32_t crc) -> std::uint32_t;
8585

tests/test_private_crc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ TEST_CASE("TransferCRC")
2424

2525
TEST_CASE("CyphalHeaderCRC")
2626
{
27-
using exposed::CyphalHeaderCrcAdd;
27+
using exposed::cyphalHeaderCrcAdd;
2828

2929
std::uint16_t crc = 0xFFFFU;
3030
const uint8_t* header = reinterpret_cast<const uint8_t*>("\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x20\x21\x22\x23\x24");
31-
crc = CyphalHeaderCrcAdd(crc, header);
31+
crc = cyphalHeaderCrcAdd(crc, 22, header);
3232
REQUIRE(0xB731 == crc);
3333
}

0 commit comments

Comments
 (0)