dronecan.remoteid.SecureCommand_req.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #define CANARD_DSDLC_INTERNAL
  2. #include <dronecan.remoteid.SecureCommand_req.h>
  3. #include <dronecan.remoteid.SecureCommand_res.h>
  4. #include <string.h>
  5. #ifdef CANARD_DSDLC_TEST_BUILD
  6. #include <test_helpers.h>
  7. #endif
  8. uint32_t dronecan_remoteid_SecureCommandRequest_encode(struct dronecan_remoteid_SecureCommandRequest* msg, uint8_t* buffer
  9. #if CANARD_ENABLE_TAO_OPTION
  10. , bool tao
  11. #endif
  12. ) {
  13. uint32_t bit_ofs = 0;
  14. memset(buffer, 0, DRONECAN_REMOTEID_SECURECOMMAND_REQUEST_MAX_SIZE);
  15. _dronecan_remoteid_SecureCommandRequest_encode(buffer, &bit_ofs, msg,
  16. #if CANARD_ENABLE_TAO_OPTION
  17. tao
  18. #else
  19. true
  20. #endif
  21. );
  22. return ((bit_ofs+7)/8);
  23. }
  24. /*
  25. return true if the decode is invalid
  26. */
  27. bool dronecan_remoteid_SecureCommandRequest_decode(const CanardRxTransfer* transfer, struct dronecan_remoteid_SecureCommandRequest* msg) {
  28. #if CANARD_ENABLE_TAO_OPTION
  29. if (transfer->tao && (transfer->payload_len > DRONECAN_REMOTEID_SECURECOMMAND_REQUEST_MAX_SIZE)) {
  30. return true; /* invalid payload length */
  31. }
  32. #endif
  33. uint32_t bit_ofs = 0;
  34. if (_dronecan_remoteid_SecureCommandRequest_decode(transfer, &bit_ofs, msg,
  35. #if CANARD_ENABLE_TAO_OPTION
  36. transfer->tao
  37. #else
  38. true
  39. #endif
  40. )) {
  41. return true; /* invalid payload */
  42. }
  43. const uint32_t byte_len = (bit_ofs+7U)/8U;
  44. #if CANARD_ENABLE_TAO_OPTION
  45. // if this could be CANFD then the dlc could indicating more bytes than
  46. // we actually have
  47. if (!transfer->tao) {
  48. return byte_len > transfer->payload_len;
  49. }
  50. #endif
  51. return byte_len != transfer->payload_len;
  52. }
  53. #ifdef CANARD_DSDLC_TEST_BUILD
  54. struct dronecan_remoteid_SecureCommandRequest sample_dronecan_remoteid_SecureCommandRequest_msg(void) {
  55. struct dronecan_remoteid_SecureCommandRequest msg;
  56. msg.sequence = (uint32_t)random_bitlen_unsigned_val(32);
  57. msg.operation = (uint32_t)random_bitlen_unsigned_val(32);
  58. msg.sig_length = (uint8_t)random_bitlen_unsigned_val(8);
  59. msg.data.len = (uint8_t)random_range_unsigned_val(0, 220);
  60. for (size_t i=0; i < msg.data.len; i++) {
  61. msg.data.data[i] = (uint8_t)random_bitlen_unsigned_val(8);
  62. }
  63. return msg;
  64. }
  65. #endif