Skip to content

Commit 507b9dc

Browse files
tpwrulestridge
authored andcommitted
table code types containing static arrays
Static arrays have a 2-entry header (8 bytes), plus the entries describing the contents of the array. Array contents cannot consist of more than 256 entries (including header entries and entries describing nested types). The array length cannot exceed 65535.
1 parent 31a0045 commit 507b9dc

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

canard.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,23 @@ CANARD_INTERNAL bool tableDecodeCore(const CanardCodingTableEntry* entry,
19231923
*bit_ofs += bitlen;
19241924
break;
19251925

1926+
case CANARD_TABLE_CODING_ARRAY_STATIC: {
1927+
const CanardCodingTableEntry* aux = ++entry;
1928+
const CanardCodingTableEntry* array_entry = ++entry;
1929+
const CanardCodingTableEntry* array_entry_last = array_entry + bitlen;
1930+
entry = array_entry_last; // point entry to last for ++entry at end of loop
1931+
1932+
uint16_t elems = aux->type | ((uint16_t)aux->bitlen << 8);
1933+
while (elems--) {
1934+
if (tableDecodeCore(array_entry, array_entry_last, transfer, bit_ofs, p)) {
1935+
return true;
1936+
}
1937+
p = (char*)p + aux->offset;
1938+
}
1939+
1940+
break;
1941+
}
1942+
19261943
default:
19271944
return true; // invalid type
19281945
}
@@ -1964,6 +1981,21 @@ CANARD_INTERNAL void tableEncodeCore(const CanardCodingTableEntry* entry,
19641981
*bit_ofs += bitlen;
19651982
break;
19661983

1984+
case CANARD_TABLE_CODING_ARRAY_STATIC: {
1985+
const CanardCodingTableEntry* aux = ++entry;
1986+
const CanardCodingTableEntry* array_entry = ++entry;
1987+
const CanardCodingTableEntry* array_entry_last = array_entry + bitlen;
1988+
entry = array_entry_last; // point entry to last for ++entry at end of loop
1989+
1990+
uint16_t elems = aux->type | ((uint16_t)aux->bitlen << 8);
1991+
while (elems--) {
1992+
tableEncodeCore(array_entry, array_entry_last, buffer, bit_ofs, p);
1993+
p = (const char*)p + aux->offset;
1994+
}
1995+
1996+
break;
1997+
}
1998+
19671999
default:
19682000
return; // invalid type
19692001
}

canard.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ struct CanardRxTransfer
469469
#define CANARD_TABLE_CODING_SIGNED (1)
470470
#define CANARD_TABLE_CODING_FLOAT (2)
471471
#define CANARD_TABLE_CODING_VOID (3)
472+
#define CANARD_TABLE_CODING_ARRAY_STATIC (4)
472473

473474
/**
474475
* This structure describes the encoded form of part of a particular message. It
@@ -500,6 +501,24 @@ typedef struct {
500501
#define CANARD_TABLE_CODING_ENTRY_VOID(bitlen) \
501502
{0, CANARD_TABLE_CODING_VOID, bitlen}
502503

504+
/**
505+
* Coding table entries (2 total) for array type with a static length.
506+
*
507+
* first entry:
508+
* offset: offset, in chars, to the storage of the first element in the message struct
509+
* type: 4 for static array
510+
* bitlen: total number of entries after these which describe the array contents (may encompass e.g. other arrays), minus one
511+
* second entry:
512+
* offset: size, in chars, of one array element, i.e. sizeof(arr[0])
513+
* type: low 8 bits of array length
514+
* bitlen: high 8 bits of array length
515+
*
516+
* note: entries which describe the array contents have offsets relative to the start of the array storage
517+
*/
518+
#define CANARD_TABLE_CODING_ENTRIES_ARRAY_STATIC(offset, num_entries, elem_size, array_len) \
519+
{offset, CANARD_TABLE_CODING_ARRAY_STATIC, (num_entries)-1}, \
520+
{elem_size, (array_len)&0xFF, (array_len)>>8}
521+
503522
/**
504523
* This structure describes the encoded form of a particular message. It can be
505524
* contained in ROM. It should be generated using dronecan_dsdlc.

0 commit comments

Comments
 (0)