uavcan.protocol.debug.KeyValue.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <canard.h>
  5. #define UAVCAN_PROTOCOL_DEBUG_KEYVALUE_MAX_SIZE 63
  6. #define UAVCAN_PROTOCOL_DEBUG_KEYVALUE_SIGNATURE (0xE02F25D6E0C98AE0ULL)
  7. #define UAVCAN_PROTOCOL_DEBUG_KEYVALUE_ID 16370
  8. #if defined(__cplusplus) && defined(DRONECAN_CXX_WRAPPERS)
  9. class uavcan_protocol_debug_KeyValue_cxx_iface;
  10. #endif
  11. struct uavcan_protocol_debug_KeyValue {
  12. #if defined(__cplusplus) && defined(DRONECAN_CXX_WRAPPERS)
  13. using cxx_iface = uavcan_protocol_debug_KeyValue_cxx_iface;
  14. #endif
  15. float value;
  16. struct { uint8_t len; uint8_t data[58]; }key;
  17. };
  18. #ifdef __cplusplus
  19. extern "C"
  20. {
  21. #endif
  22. uint32_t _uavcan_protocol_debug_KeyValue_encode(struct uavcan_protocol_debug_KeyValue* msg, uint8_t* buffer
  23. #if CANARD_ENABLE_TAO_OPTION
  24. , bool tao
  25. #endif
  26. );
  27. bool _uavcan_protocol_debug_KeyValue_decode(const CanardRxTransfer* transfer, struct uavcan_protocol_debug_KeyValue* msg);
  28. static inline uint32_t uavcan_protocol_debug_KeyValue_encode(struct uavcan_protocol_debug_KeyValue* msg, uint8_t* buffer
  29. #if CANARD_ENABLE_TAO_OPTION
  30. , bool tao
  31. #endif
  32. ) {
  33. return _uavcan_protocol_debug_KeyValue_encode(msg, buffer
  34. #if CANARD_ENABLE_TAO_OPTION
  35. , tao
  36. #endif
  37. );
  38. }
  39. static inline bool uavcan_protocol_debug_KeyValue_decode(const CanardRxTransfer* transfer, struct uavcan_protocol_debug_KeyValue* msg) {
  40. return _uavcan_protocol_debug_KeyValue_decode(transfer, msg);
  41. }
  42. #if defined(CANARD_DSDLC_INTERNAL)
  43. static inline void __uavcan_protocol_debug_KeyValue_encode(uint8_t* buffer, uint32_t* bit_ofs, struct uavcan_protocol_debug_KeyValue* msg, bool tao);
  44. static inline bool __uavcan_protocol_debug_KeyValue_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, struct uavcan_protocol_debug_KeyValue* msg, bool tao);
  45. void __uavcan_protocol_debug_KeyValue_encode(uint8_t* buffer, uint32_t* bit_ofs, struct uavcan_protocol_debug_KeyValue* msg, bool tao) {
  46. (void)buffer;
  47. (void)bit_ofs;
  48. (void)msg;
  49. (void)tao;
  50. canardEncodeScalar(buffer, *bit_ofs, 32, &msg->value);
  51. *bit_ofs += 32;
  52. #pragma GCC diagnostic push
  53. #pragma GCC diagnostic ignored "-Wtype-limits"
  54. const uint8_t key_len = msg->key.len > 58 ? 58 : msg->key.len;
  55. #pragma GCC diagnostic pop
  56. if (!tao) {
  57. canardEncodeScalar(buffer, *bit_ofs, 6, &key_len);
  58. *bit_ofs += 6;
  59. }
  60. for (size_t i=0; i < key_len; i++) {
  61. canardEncodeScalar(buffer, *bit_ofs, 8, &msg->key.data[i]);
  62. *bit_ofs += 8;
  63. }
  64. }
  65. /*
  66. decode uavcan_protocol_debug_KeyValue, return true on failure, false on success
  67. */
  68. bool __uavcan_protocol_debug_KeyValue_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, struct uavcan_protocol_debug_KeyValue* msg, bool tao) {
  69. (void)transfer;
  70. (void)bit_ofs;
  71. (void)msg;
  72. (void)tao;
  73. canardDecodeScalar(transfer, *bit_ofs, 32, true, &msg->value);
  74. *bit_ofs += 32;
  75. if (!tao) {
  76. canardDecodeScalar(transfer, *bit_ofs, 6, false, &msg->key.len);
  77. *bit_ofs += 6;
  78. } else {
  79. msg->key.len = ((transfer->payload_len*8)-*bit_ofs)/8;
  80. }
  81. #pragma GCC diagnostic push
  82. #pragma GCC diagnostic ignored "-Wtype-limits"
  83. if (msg->key.len > 58) {
  84. return true; /* invalid value */
  85. }
  86. #pragma GCC diagnostic pop
  87. for (size_t i=0; i < msg->key.len; i++) {
  88. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->key.data[i]);
  89. *bit_ofs += 8;
  90. }
  91. return false; /* success */
  92. }
  93. #endif
  94. #ifdef CANARD_DSDLC_TEST_BUILD
  95. struct uavcan_protocol_debug_KeyValue sample_uavcan_protocol_debug_KeyValue_msg(void);
  96. #endif
  97. #ifdef __cplusplus
  98. } // extern "C"
  99. #ifdef DRONECAN_CXX_WRAPPERS
  100. #include <canard/cxx_wrappers.h>
  101. BROADCAST_MESSAGE_CXX_IFACE(uavcan_protocol_debug_KeyValue, UAVCAN_PROTOCOL_DEBUG_KEYVALUE_ID, UAVCAN_PROTOCOL_DEBUG_KEYVALUE_SIGNATURE, UAVCAN_PROTOCOL_DEBUG_KEYVALUE_MAX_SIZE);
  102. #endif
  103. #endif