mavlink.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. mavlink class for handling OpenDroneID messages
  3. */
  4. #include <Arduino.h>
  5. #include "mavlink.h"
  6. #include "board_config.h"
  7. #include "version.h"
  8. #include "parameters.h"
  9. #define SERIAL_BAUD 115200
  10. static HardwareSerial *serial_ports[MAVLINK_COMM_NUM_BUFFERS];
  11. #include <generated/mavlink_helpers.h>
  12. mavlink_system_t mavlink_system = {0, MAV_COMP_ID_ODID_TXRX_1};
  13. /*
  14. send a buffer out a MAVLink channel
  15. */
  16. void comm_send_buffer(mavlink_channel_t chan, const uint8_t *buf, uint8_t len)
  17. {
  18. if (chan >= MAVLINK_COMM_NUM_BUFFERS || serial_ports[uint8_t(chan)] == nullptr) {
  19. return;
  20. }
  21. auto &serial = *serial_ports[uint8_t(chan)];
  22. serial.write(buf, len);
  23. }
  24. /*
  25. abstraction for MAVLink on a serial port
  26. */
  27. MAVLinkSerial::MAVLinkSerial(HardwareSerial &_serial, mavlink_channel_t _chan) :
  28. serial(_serial),
  29. chan(_chan)
  30. {
  31. serial_ports[uint8_t(_chan - MAVLINK_COMM_0)] = &serial;
  32. }
  33. void MAVLinkSerial::init(void)
  34. {
  35. // print banner at startup
  36. serial.printf("ArduRemoteID version %u.%u %08x\n",
  37. FW_VERSION_MAJOR, FW_VERSION_MINOR, GIT_VERSION);
  38. mavlink_system.sysid = g.mavlink_sysid;
  39. }
  40. void MAVLinkSerial::update(void)
  41. {
  42. const uint32_t now_ms = millis();
  43. if (mavlink_system.sysid != 0) {
  44. update_send();
  45. } else if (g.mavlink_sysid != 0) {
  46. mavlink_system.sysid = g.mavlink_sysid;
  47. } else if (now_ms - last_hb_warn_ms >= 2000) {
  48. last_hb_warn_ms = millis();
  49. serial.printf("Waiting for heartbeat\n");
  50. }
  51. update_receive();
  52. if (param_request_last_ms != 0 && now_ms - param_request_last_ms > 50) {
  53. param_request_last_ms = now_ms;
  54. float value;
  55. if (param_next->get_as_float(value)) {
  56. mavlink_msg_param_value_send(chan,
  57. param_next->name, value,
  58. MAV_PARAM_TYPE_REAL32,
  59. g.param_count_float(),
  60. g.param_index_float(param_next));
  61. }
  62. param_next++;
  63. if (param_next->ptype == Parameters::ParamType::NONE) {
  64. param_next = nullptr;
  65. param_request_last_ms = 0;
  66. }
  67. }
  68. }
  69. void MAVLinkSerial::update_send(void)
  70. {
  71. uint32_t now_ms = millis();
  72. if (now_ms - last_hb_ms >= 1000) {
  73. last_hb_ms = now_ms;
  74. mavlink_msg_heartbeat_send(
  75. chan,
  76. MAV_TYPE_ODID,
  77. MAV_AUTOPILOT_INVALID,
  78. 0,
  79. 0,
  80. 0);
  81. // send arming status
  82. arm_status_send();
  83. }
  84. }
  85. void MAVLinkSerial::update_receive(void)
  86. {
  87. // receive new packets
  88. mavlink_message_t msg;
  89. mavlink_status_t status;
  90. status.packet_rx_drop_count = 0;
  91. const uint16_t nbytes = serial.available();
  92. for (uint16_t i=0; i<nbytes; i++) {
  93. const uint8_t c = (uint8_t)serial.read();
  94. // Try to get a new message
  95. if (mavlink_parse_char(chan, c, &msg, &status)) {
  96. process_packet(status, msg);
  97. }
  98. }
  99. }
  100. /*
  101. printf via MAVLink STATUSTEXT for debugging
  102. */
  103. void MAVLinkSerial::mav_printf(uint8_t severity, const char *fmt, ...)
  104. {
  105. va_list arg_list;
  106. char text[MAVLINK_MSG_STATUSTEXT_FIELD_TEXT_LEN+1] {};
  107. va_start(arg_list, fmt);
  108. vsnprintf(text, sizeof(text), fmt, arg_list);
  109. va_end(arg_list);
  110. mavlink_msg_statustext_send(chan,
  111. severity,
  112. text,
  113. 0, 0);
  114. }
  115. void MAVLinkSerial::process_packet(mavlink_status_t &status, mavlink_message_t &msg)
  116. {
  117. const uint32_t now_ms = millis();
  118. switch (msg.msgid) {
  119. case MAVLINK_MSG_ID_HEARTBEAT: {
  120. mavlink_heartbeat_t hb;
  121. if (mavlink_system.sysid == 0) {
  122. mavlink_msg_heartbeat_decode(&msg, &hb);
  123. if (msg.sysid > 0 && hb.type != MAV_TYPE_GCS) {
  124. mavlink_system.sysid = msg.sysid;
  125. }
  126. }
  127. break;
  128. }
  129. case MAVLINK_MSG_ID_OPEN_DRONE_ID_LOCATION: {
  130. mavlink_msg_open_drone_id_location_decode(&msg, &location);
  131. if (last_location_timestamp != location.timestamp) {
  132. //only update the timestamp if we receive information with a different timestamp
  133. last_location_ms = millis();
  134. last_location_timestamp = location.timestamp;
  135. }
  136. last_location_ms = now_ms;
  137. break;
  138. }
  139. case MAVLINK_MSG_ID_OPEN_DRONE_ID_BASIC_ID: {
  140. mavlink_open_drone_id_basic_id_t basic_id_tmp;
  141. mavlink_msg_open_drone_id_basic_id_decode(&msg, &basic_id_tmp);
  142. if ((strlen((const char*) basic_id_tmp.uas_id) > 0) && (basic_id_tmp.id_type > 0) && (basic_id_tmp.id_type <= MAV_ODID_ID_TYPE_SPECIFIC_SESSION_ID)) {
  143. //only update if we receive valid data
  144. basic_id = basic_id_tmp;
  145. last_basic_id_ms = now_ms;
  146. }
  147. break;
  148. }
  149. case MAVLINK_MSG_ID_OPEN_DRONE_ID_AUTHENTICATION: {
  150. mavlink_msg_open_drone_id_authentication_decode(&msg, &authentication);
  151. break;
  152. }
  153. case MAVLINK_MSG_ID_OPEN_DRONE_ID_SELF_ID: {
  154. mavlink_msg_open_drone_id_self_id_decode(&msg, &self_id);
  155. last_self_id_ms = now_ms;
  156. break;
  157. }
  158. case MAVLINK_MSG_ID_OPEN_DRONE_ID_SYSTEM: {
  159. mavlink_msg_open_drone_id_system_decode(&msg, &system);
  160. if ((last_system_timestamp != system.timestamp) || (system.timestamp == 0)) {
  161. //only update the timestamp if we receive information with a different timestamp
  162. last_system_ms = millis();
  163. last_system_timestamp = system.timestamp;
  164. }
  165. break;
  166. }
  167. case MAVLINK_MSG_ID_OPEN_DRONE_ID_SYSTEM_UPDATE: {
  168. mavlink_open_drone_id_system_update_t pkt_system_update;
  169. mavlink_msg_open_drone_id_system_update_decode(&msg, &pkt_system_update);
  170. system.operator_latitude = pkt_system_update.operator_latitude;
  171. system.operator_longitude = pkt_system_update.operator_longitude;
  172. system.operator_altitude_geo = pkt_system_update.operator_altitude_geo;
  173. system.timestamp = pkt_system_update.timestamp;
  174. if (last_system_ms != 0) {
  175. // we can only mark system as updated if we have the other
  176. // information already
  177. if ((last_system_timestamp != system.timestamp) || (pkt_system_update.timestamp == 0)) {
  178. //only update the timestamp if we receive information with a different timestamp
  179. last_system_ms = millis();
  180. last_system_timestamp = pkt_system_update.timestamp;
  181. }
  182. last_system_ms = now_ms;
  183. }
  184. break;
  185. }
  186. case MAVLINK_MSG_ID_OPEN_DRONE_ID_OPERATOR_ID: {
  187. mavlink_msg_open_drone_id_operator_id_decode(&msg, &operator_id);
  188. last_operator_id_ms = now_ms;
  189. break;
  190. }
  191. case MAVLINK_MSG_ID_PARAM_REQUEST_LIST: {
  192. param_next = g.find_by_index_float(0);
  193. param_request_last_ms = millis();
  194. break;
  195. };
  196. case MAVLINK_MSG_ID_PARAM_REQUEST_READ: {
  197. mavlink_param_request_read_t pkt;
  198. mavlink_msg_param_request_read_decode(&msg, &pkt);
  199. const Parameters::Param *p;
  200. if (pkt.param_index < 0) {
  201. p = g.find(pkt.param_id);
  202. } else {
  203. p = g.find_by_index_float(pkt.param_index);
  204. }
  205. float value;
  206. if (!p || !p->get_as_float(value)) {
  207. return;
  208. }
  209. if (p != nullptr && (p->flags & PARAM_FLAG_HIDDEN)) {
  210. return;
  211. }
  212. mavlink_msg_param_value_send(chan,
  213. p->name, value,
  214. MAV_PARAM_TYPE_REAL32,
  215. g.param_count_float(),
  216. g.param_index_float(p));
  217. break;
  218. }
  219. case MAVLINK_MSG_ID_PARAM_SET: {
  220. mavlink_param_set_t pkt;
  221. mavlink_msg_param_set_decode(&msg, &pkt);
  222. auto *p = g.find(pkt.param_id);
  223. float value;
  224. if (pkt.param_type != MAV_PARAM_TYPE_REAL32 ||
  225. !p || !p->get_as_float(value)) {
  226. return;
  227. }
  228. p->get_as_float(value);
  229. if (g.lock_level != 0 &&
  230. (strcmp(p->name, "LOCK_LEVEL") != 0 ||
  231. uint8_t(pkt.param_value) <= uint8_t(value))) {
  232. // only param set allowed is to increase lock level
  233. mav_printf(MAV_SEVERITY_ERROR, "Parameters locked");
  234. } else {
  235. p->set_as_float(pkt.param_value);
  236. }
  237. p->get_as_float(value);
  238. mavlink_msg_param_value_send(chan,
  239. p->name, value,
  240. MAV_PARAM_TYPE_REAL32,
  241. g.param_count_float(),
  242. g.param_index_float(p));
  243. break;
  244. }
  245. case MAVLINK_MSG_ID_SECURE_COMMAND:
  246. case MAVLINK_MSG_ID_SECURE_COMMAND_REPLY: {
  247. mavlink_secure_command_t pkt;
  248. mavlink_msg_secure_command_decode(&msg, &pkt);
  249. handle_secure_command(pkt);
  250. break;
  251. }
  252. default:
  253. // we don't care about other packets
  254. break;
  255. }
  256. }
  257. void MAVLinkSerial::arm_status_send(void)
  258. {
  259. const uint8_t status = parse_fail==nullptr?MAV_ODID_GOOD_TO_ARM:MAV_ODID_PRE_ARM_FAIL_GENERIC;
  260. const char *reason = parse_fail==nullptr?"":parse_fail;
  261. mavlink_msg_open_drone_id_arm_status_send(
  262. chan,
  263. status,
  264. reason);
  265. }