uavcan.protocol.Panic.h 3.5 KB

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