uavcan.protocol.HardwareVersion.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <canard.h>
  5. #define UAVCAN_PROTOCOL_HARDWAREVERSION_MAX_SIZE 274
  6. #define UAVCAN_PROTOCOL_HARDWAREVERSION_SIGNATURE (0xAD5C4C933F4A0C4ULL)
  7. struct uavcan_protocol_HardwareVersion {
  8. uint8_t major;
  9. uint8_t minor;
  10. uint8_t unique_id[16];
  11. struct { uint8_t len; uint8_t data[255]; }certificate_of_authenticity;
  12. };
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. uint32_t _uavcan_protocol_HardwareVersion_encode(struct uavcan_protocol_HardwareVersion* msg, uint8_t* buffer
  18. #if CANARD_ENABLE_TAO_OPTION
  19. , bool tao
  20. #endif
  21. );
  22. bool _uavcan_protocol_HardwareVersion_decode(const CanardRxTransfer* transfer, struct uavcan_protocol_HardwareVersion* msg);
  23. static inline uint32_t uavcan_protocol_HardwareVersion_encode(struct uavcan_protocol_HardwareVersion* msg, uint8_t* buffer
  24. #if CANARD_ENABLE_TAO_OPTION
  25. , bool tao
  26. #endif
  27. ) {
  28. return _uavcan_protocol_HardwareVersion_encode(msg, buffer
  29. #if CANARD_ENABLE_TAO_OPTION
  30. , tao
  31. #endif
  32. );
  33. }
  34. static inline bool uavcan_protocol_HardwareVersion_decode(const CanardRxTransfer* transfer, struct uavcan_protocol_HardwareVersion* msg) {
  35. return _uavcan_protocol_HardwareVersion_decode(transfer, msg);
  36. }
  37. #if defined(CANARD_DSDLC_INTERNAL)
  38. static inline void __uavcan_protocol_HardwareVersion_encode(uint8_t* buffer, uint32_t* bit_ofs, struct uavcan_protocol_HardwareVersion* msg, bool tao);
  39. static inline bool __uavcan_protocol_HardwareVersion_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, struct uavcan_protocol_HardwareVersion* msg, bool tao);
  40. void __uavcan_protocol_HardwareVersion_encode(uint8_t* buffer, uint32_t* bit_ofs, struct uavcan_protocol_HardwareVersion* msg, bool tao) {
  41. (void)buffer;
  42. (void)bit_ofs;
  43. (void)msg;
  44. (void)tao;
  45. canardEncodeScalar(buffer, *bit_ofs, 8, &msg->major);
  46. *bit_ofs += 8;
  47. canardEncodeScalar(buffer, *bit_ofs, 8, &msg->minor);
  48. *bit_ofs += 8;
  49. for (size_t i=0; i < 16; i++) {
  50. canardEncodeScalar(buffer, *bit_ofs, 8, &msg->unique_id[i]);
  51. *bit_ofs += 8;
  52. }
  53. #pragma GCC diagnostic push
  54. #pragma GCC diagnostic ignored "-Wtype-limits"
  55. const uint8_t certificate_of_authenticity_len = msg->certificate_of_authenticity.len > 255 ? 255 : msg->certificate_of_authenticity.len;
  56. #pragma GCC diagnostic pop
  57. if (!tao) {
  58. canardEncodeScalar(buffer, *bit_ofs, 8, &certificate_of_authenticity_len);
  59. *bit_ofs += 8;
  60. }
  61. for (size_t i=0; i < certificate_of_authenticity_len; i++) {
  62. canardEncodeScalar(buffer, *bit_ofs, 8, &msg->certificate_of_authenticity.data[i]);
  63. *bit_ofs += 8;
  64. }
  65. }
  66. /*
  67. decode uavcan_protocol_HardwareVersion, return true on failure, false on success
  68. */
  69. bool __uavcan_protocol_HardwareVersion_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, struct uavcan_protocol_HardwareVersion* msg, bool tao) {
  70. (void)transfer;
  71. (void)bit_ofs;
  72. (void)msg;
  73. (void)tao;
  74. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->major);
  75. *bit_ofs += 8;
  76. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->minor);
  77. *bit_ofs += 8;
  78. for (size_t i=0; i < 16; i++) {
  79. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->unique_id[i]);
  80. *bit_ofs += 8;
  81. }
  82. if (!tao) {
  83. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->certificate_of_authenticity.len);
  84. *bit_ofs += 8;
  85. } else {
  86. msg->certificate_of_authenticity.len = ((transfer->payload_len*8)-*bit_ofs)/8;
  87. }
  88. #pragma GCC diagnostic push
  89. #pragma GCC diagnostic ignored "-Wtype-limits"
  90. if (msg->certificate_of_authenticity.len > 255) {
  91. return true; /* invalid value */
  92. }
  93. #pragma GCC diagnostic pop
  94. for (size_t i=0; i < msg->certificate_of_authenticity.len; i++) {
  95. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->certificate_of_authenticity.data[i]);
  96. *bit_ofs += 8;
  97. }
  98. return false; /* success */
  99. }
  100. #endif
  101. #ifdef CANARD_DSDLC_TEST_BUILD
  102. struct uavcan_protocol_HardwareVersion sample_uavcan_protocol_HardwareVersion_msg(void);
  103. #endif
  104. #ifdef __cplusplus
  105. } // extern "C"
  106. #ifdef DRONECAN_CXX_WRAPPERS
  107. #include <canard/cxx_wrappers.h>
  108. #endif
  109. #endif