Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check array size and make decode functions boolean #20

Merged
merged 4 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This project contains tools to generate C code for packing DroneCAN messages.

## Dependencies
* `pip install empy`
* `pip install empy==3.3.4`
* `pip install pexpect`

## How To Use
Expand Down
11 changes: 9 additions & 2 deletions templates/msg.c.em
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ uint32_t @(msg_underscored_name)_encode(@(msg_c_type)* msg, uint8_t* buffer
return true if the decode is invalid
*/
bool @(msg_underscored_name)_decode(const CanardRxTransfer* transfer, @(msg_c_type)* msg) {
#if CANARD_ENABLE_TAO_OPTION
if (transfer->tao && (transfer->payload_len > @(msg_define_name.upper())_MAX_SIZE)) {
return true; /* invalid payload length */
}
#endif
uint32_t bit_ofs = 0;
_@(msg_underscored_name)_decode(transfer, &bit_ofs, msg,
if (_@(msg_underscored_name)_decode(transfer, &bit_ofs, msg,
#if CANARD_ENABLE_TAO_OPTION
transfer->tao
#else
true
#endif
);
)) {
return true; /* invalid payload */
}

const uint32_t byte_len = (bit_ofs+7U)/8U;
#if CANARD_ENABLE_TAO_OPTION
Expand Down
27 changes: 20 additions & 7 deletions templates/msg.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ bool @(msg_underscored_name)_decode(const CanardRxTransfer* transfer, @(msg_c_ty
#if defined(CANARD_DSDLC_INTERNAL)
@{indent = 0}@{ind = ' '*indent}@
static inline void _@(msg_underscored_name)_encode(uint8_t* buffer, uint32_t* bit_ofs, @(msg_c_type)* msg, bool tao);
static inline void _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, @(msg_c_type)* msg, bool tao);
static inline bool _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, @(msg_c_type)* msg, bool tao);
void _@(msg_underscored_name)_encode(uint8_t* buffer, uint32_t* bit_ofs, @(msg_c_type)* msg, bool tao) {
@{indent += 1}@{ind = ' '*indent}@
@(ind)(void)buffer;
Expand Down Expand Up @@ -124,7 +124,11 @@ void _@(msg_underscored_name)_encode(uint8_t* buffer, uint32_t* bit_ofs, @(msg_c
@{indent -= 1}@{ind = ' '*indent}@
@(ind)}
@[ end if]@
@(ind)for (size_t i=0; i < msg->@(field.name).len; i++) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
@(ind)const size_t @(field.name)_len = msg->@(field.name).len > @(field.type.max_size) ? @(field.type.max_size) : msg->@(field.name).len;
#pragma GCC diagnostic pop
@(ind)for (size_t i=0; i < @(field.name)_len; i++) {
@[ else]@
@(ind)for (size_t i=0; i < @(field.type.max_size); i++) {
@[ end if]@
Expand Down Expand Up @@ -160,13 +164,15 @@ void _@(msg_underscored_name)_encode(uint8_t* buffer, uint32_t* bit_ofs, @(msg_c
@{indent -= 1}@{ind = ' '*indent}@
@(ind)}

void _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, @(msg_c_type)* msg, bool tao) {
/*
decode @(msg_underscored_name), return true on failure, false on success
*/
bool _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, @(msg_c_type)* msg, bool tao) {
@{indent += 1}@{ind = ' '*indent}@
@(ind)(void)transfer;
@(ind)(void)bit_ofs;
@(ind)(void)msg;
@(ind)(void)tao;

@[ if msg_union]@
@(ind)@(union_msg_tag_uint_type_from_num_fields(len(msg_fields))) union_tag;
@(ind)canardDecodeScalar(transfer, *bit_ofs, @(union_msg_tag_bitlen_from_num_fields(len(msg_fields))), false, &union_tag);
Expand All @@ -182,7 +188,7 @@ void _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t*
@{indent += 1}@{ind = ' '*indent}@
@[ end if]@
@[ if field.type.category == field.type.CATEGORY_COMPOUND]@
@(ind)_@(underscored_name(field.type))_decode(transfer, bit_ofs, &msg->@(field.name), @('tao' if (field == msg_fields[-1] or msg_union) else 'false'));
@(ind)if (_@(underscored_name(field.type))_decode(transfer, bit_ofs, &msg->@(field.name), @('tao' if (field == msg_fields[-1] or msg_union) else 'false'))) {return true;}
@[ elif field.type.category == field.type.CATEGORY_PRIMITIVE]@
@[ if field.type.kind == field.type.KIND_FLOAT and field.type.bitlen == 16]@
@(ind){
Expand Down Expand Up @@ -221,7 +227,7 @@ void _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t*
@(ind)msg->@(field.name).len = 0;
@(ind)while ((transfer->payload_len*8) > *bit_ofs) {
@{indent += 1}@{ind = ' '*indent}@
@(ind)_@(underscored_name(field.type.value_type))_decode(transfer, bit_ofs, &msg->@(field_get_data(field))[msg->@(field.name).len], @[if field == msg_fields[-1] and field.type.value_type.get_min_bitlen() < 8]tao && i==msg->@(field.name).len@[else]false@[end if]@);
@(ind)if (_@(underscored_name(field.type.value_type))_decode(transfer, bit_ofs, &msg->@(field_get_data(field))[msg->@(field.name).len], @[if field == msg_fields[-1] and field.type.value_type.get_min_bitlen() < 8]tao && i==msg->@(field.name).len@[else]false@[end if]@)) {return true;}
@(ind)msg->@(field.name).len++;
@{indent -= 1}@{ind = ' '*indent}@
@(ind)}
Expand All @@ -232,6 +238,12 @@ void _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t*
@[ end if]@
@{indent += 1}@{ind = ' '*indent}@
@[ end if]@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
@(ind)if (msg->@(field.name).len > @(field.type.max_size)) {
@(ind) return true; /* invalid value */
@(ind)}
#pragma GCC diagnostic pop
@(ind)for (size_t i=0; i < msg->@(field.name).len; i++) {
@[ else]@
@(ind)for (size_t i=0; i < @(field.type.max_size); i++) {
Expand All @@ -249,7 +261,7 @@ void _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t*
@[ end if]@
@(ind)*bit_ofs += @(field.type.value_type.bitlen);
@[ elif field.type.value_type.category == field.type.value_type.CATEGORY_COMPOUND]@
@(ind)_@(underscored_name(field.type.value_type))_decode(transfer, bit_ofs, &msg->@(field_get_data(field))[i], @[if field == msg_fields[-1] and field.type.value_type.get_min_bitlen() < 8]tao && i==msg->@(field.name).len@[else]false@[end if]@);
@(ind)if (_@(underscored_name(field.type.value_type))_decode(transfer, bit_ofs, &msg->@(field_get_data(field))[i], @[if field == msg_fields[-1] and field.type.value_type.get_min_bitlen() < 8]tao && i==msg->@(field.name).len@[else]false@[end if]@)) {return true;}
@[ end if]@
@{indent -= 1}@{ind = ' '*indent}@
@(ind)}
Expand All @@ -271,6 +283,7 @@ void _@(msg_underscored_name)_decode(const CanardRxTransfer* transfer, uint32_t*
@{indent -= 1}@{ind = ' '*indent}@
@(ind)}
@[ end if]@
@(ind)return false; /* success */
@{indent -= 1}@{ind = ' '*indent}@
@(ind)}
#endif
Expand Down
8 changes: 6 additions & 2 deletions templates/test_msg.cpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ int main() {
.priority = 0,
.source_node_id = 125,
};
@(msg_underscored_name)_decode(&rx_transfer, &decoded_msg);
if (@(msg_underscored_name)_decode(&rx_transfer, &decoded_msg)) {
std::cout << "decode failed" << std::endl;
return 1;
}

// compare decoded message with original message
data_len = @(msg_underscored_name)_encode(&decoded_msg, buffer);
Expand All @@ -129,7 +132,8 @@ int main() {
if (hex_str.compare(reencoded_hex_str) == 0) {
std::cout << "Messages are equal" << std::endl;
} else {
std::cout << "Messages are not equal" << std::endl;
std::cout << "Messages are not equal" << std::endl;
return 1;
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
set -x

# test compiler on linux
python3 -m pip install -U empy pexpect dronecan
python3 -m pip install -U empy==3.3.4 pexpect dronecan
rm -rf DSDL
git clone https://github.com/DroneCAN/DSDL

Expand Down
2 changes: 1 addition & 1 deletion tests/test_regression.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
set -x

# test compiler on linux
python3 -m pip install -U empy pexpect dronecan
python3 -m pip install -U empy==3.3.4 pexpect dronecan
rm -rf DSDL

git clone https://github.com/DroneCAN/DSDL
Expand Down
2 changes: 1 addition & 1 deletion tests/test_windows.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# simple test of DSDL compilation for windows

python3 -m pip install -U empy pexpect dronecan
python3 -m pip install -U empy==3.3.4 pexpect dronecan

git clone https://github.com/DroneCAN/DSDL

Expand Down
Loading