uavcan.protocol.param.Value.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <canard.h>
  5. #include <uavcan.protocol.param.Empty.h>
  6. #define UAVCAN_PROTOCOL_PARAM_VALUE_MAX_SIZE 130
  7. #define UAVCAN_PROTOCOL_PARAM_VALUE_SIGNATURE (0x29F14BF484727267ULL)
  8. enum uavcan_protocol_param_Value_type_t {
  9. UAVCAN_PROTOCOL_PARAM_VALUE_EMPTY,
  10. UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE,
  11. UAVCAN_PROTOCOL_PARAM_VALUE_REAL_VALUE,
  12. UAVCAN_PROTOCOL_PARAM_VALUE_BOOLEAN_VALUE,
  13. UAVCAN_PROTOCOL_PARAM_VALUE_STRING_VALUE,
  14. };
  15. struct uavcan_protocol_param_Value {
  16. enum uavcan_protocol_param_Value_type_t union_tag;
  17. union {
  18. struct uavcan_protocol_param_Empty empty;
  19. int64_t integer_value;
  20. float real_value;
  21. uint8_t boolean_value;
  22. struct { uint8_t len; uint8_t data[128]; }string_value;
  23. };
  24. };
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. uint32_t _uavcan_protocol_param_Value_encode(struct uavcan_protocol_param_Value* msg, uint8_t* buffer
  30. #if CANARD_ENABLE_TAO_OPTION
  31. , bool tao
  32. #endif
  33. );
  34. bool _uavcan_protocol_param_Value_decode(const CanardRxTransfer* transfer, struct uavcan_protocol_param_Value* msg);
  35. static inline uint32_t uavcan_protocol_param_Value_encode(struct uavcan_protocol_param_Value* msg, uint8_t* buffer
  36. #if CANARD_ENABLE_TAO_OPTION
  37. , bool tao
  38. #endif
  39. ) {
  40. return _uavcan_protocol_param_Value_encode(msg, buffer
  41. #if CANARD_ENABLE_TAO_OPTION
  42. , tao
  43. #endif
  44. );
  45. }
  46. static inline bool uavcan_protocol_param_Value_decode(const CanardRxTransfer* transfer, struct uavcan_protocol_param_Value* msg) {
  47. return _uavcan_protocol_param_Value_decode(transfer, msg);
  48. }
  49. #if defined(CANARD_DSDLC_INTERNAL)
  50. static inline void __uavcan_protocol_param_Value_encode(uint8_t* buffer, uint32_t* bit_ofs, struct uavcan_protocol_param_Value* msg, bool tao);
  51. static inline bool __uavcan_protocol_param_Value_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, struct uavcan_protocol_param_Value* msg, bool tao);
  52. void __uavcan_protocol_param_Value_encode(uint8_t* buffer, uint32_t* bit_ofs, struct uavcan_protocol_param_Value* msg, bool tao) {
  53. (void)buffer;
  54. (void)bit_ofs;
  55. (void)msg;
  56. (void)tao;
  57. uint8_t union_tag = msg->union_tag;
  58. canardEncodeScalar(buffer, *bit_ofs, 3, &union_tag);
  59. *bit_ofs += 3;
  60. switch(msg->union_tag) {
  61. case UAVCAN_PROTOCOL_PARAM_VALUE_EMPTY: {
  62. __uavcan_protocol_param_Empty_encode(buffer, bit_ofs, &msg->empty, tao);
  63. break;
  64. }
  65. case UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE: {
  66. canardEncodeScalar(buffer, *bit_ofs, 64, &msg->integer_value);
  67. *bit_ofs += 64;
  68. break;
  69. }
  70. case UAVCAN_PROTOCOL_PARAM_VALUE_REAL_VALUE: {
  71. canardEncodeScalar(buffer, *bit_ofs, 32, &msg->real_value);
  72. *bit_ofs += 32;
  73. break;
  74. }
  75. case UAVCAN_PROTOCOL_PARAM_VALUE_BOOLEAN_VALUE: {
  76. canardEncodeScalar(buffer, *bit_ofs, 8, &msg->boolean_value);
  77. *bit_ofs += 8;
  78. break;
  79. }
  80. case UAVCAN_PROTOCOL_PARAM_VALUE_STRING_VALUE: {
  81. #pragma GCC diagnostic push
  82. #pragma GCC diagnostic ignored "-Wtype-limits"
  83. const uint8_t string_value_len = msg->string_value.len > 128 ? 128 : msg->string_value.len;
  84. #pragma GCC diagnostic pop
  85. if (!tao) {
  86. canardEncodeScalar(buffer, *bit_ofs, 8, &string_value_len);
  87. *bit_ofs += 8;
  88. }
  89. for (size_t i=0; i < string_value_len; i++) {
  90. canardEncodeScalar(buffer, *bit_ofs, 8, &msg->string_value.data[i]);
  91. *bit_ofs += 8;
  92. }
  93. break;
  94. }
  95. }
  96. }
  97. /*
  98. decode uavcan_protocol_param_Value, return true on failure, false on success
  99. */
  100. bool __uavcan_protocol_param_Value_decode(const CanardRxTransfer* transfer, uint32_t* bit_ofs, struct uavcan_protocol_param_Value* msg, bool tao) {
  101. (void)transfer;
  102. (void)bit_ofs;
  103. (void)msg;
  104. (void)tao;
  105. uint8_t union_tag;
  106. canardDecodeScalar(transfer, *bit_ofs, 3, false, &union_tag);
  107. *bit_ofs += 3;
  108. #pragma GCC diagnostic push
  109. #pragma GCC diagnostic ignored "-Wtype-limits"
  110. if (union_tag >= 5) {
  111. return true; /* invalid value */
  112. }
  113. #pragma GCC diagnostic pop
  114. msg->union_tag = (enum uavcan_protocol_param_Value_type_t)union_tag;
  115. switch(msg->union_tag) {
  116. case UAVCAN_PROTOCOL_PARAM_VALUE_EMPTY: {
  117. if (__uavcan_protocol_param_Empty_decode(transfer, bit_ofs, &msg->empty, tao)) {return true;}
  118. break;
  119. }
  120. case UAVCAN_PROTOCOL_PARAM_VALUE_INTEGER_VALUE: {
  121. canardDecodeScalar(transfer, *bit_ofs, 64, true, &msg->integer_value);
  122. *bit_ofs += 64;
  123. break;
  124. }
  125. case UAVCAN_PROTOCOL_PARAM_VALUE_REAL_VALUE: {
  126. canardDecodeScalar(transfer, *bit_ofs, 32, true, &msg->real_value);
  127. *bit_ofs += 32;
  128. break;
  129. }
  130. case UAVCAN_PROTOCOL_PARAM_VALUE_BOOLEAN_VALUE: {
  131. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->boolean_value);
  132. *bit_ofs += 8;
  133. break;
  134. }
  135. case UAVCAN_PROTOCOL_PARAM_VALUE_STRING_VALUE: {
  136. if (!tao) {
  137. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->string_value.len);
  138. *bit_ofs += 8;
  139. } else {
  140. msg->string_value.len = ((transfer->payload_len*8)-*bit_ofs)/8;
  141. }
  142. #pragma GCC diagnostic push
  143. #pragma GCC diagnostic ignored "-Wtype-limits"
  144. if (msg->string_value.len > 128) {
  145. return true; /* invalid value */
  146. }
  147. #pragma GCC diagnostic pop
  148. for (size_t i=0; i < msg->string_value.len; i++) {
  149. canardDecodeScalar(transfer, *bit_ofs, 8, false, &msg->string_value.data[i]);
  150. *bit_ofs += 8;
  151. }
  152. break;
  153. }
  154. }
  155. return false; /* success */
  156. }
  157. #endif
  158. #ifdef CANARD_DSDLC_TEST_BUILD
  159. struct uavcan_protocol_param_Value sample_uavcan_protocol_param_Value_msg(void);
  160. #endif
  161. #ifdef __cplusplus
  162. } // extern "C"
  163. #ifdef DRONECAN_CXX_WRAPPERS
  164. #include <canard/cxx_wrappers.h>
  165. #endif
  166. #endif