uavcan.tunnel.Broadcast.h 3.9 KB

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