33#include " MAVLink.h"
44#include < config.h>
55
6- // Returns whether the message is a control message, i.e. a message where we don't want to
7- // pass the message to the flight controller, but rather handle it ourselves. If the message
8- // is a control message, the function handles it internally.
9- bool MAVLink::handleControlMessage (mavlink_message_t *msg)
6+ void
7+ MAVLink::ProcessMAVLinkFromTX (uint8_t c)
108{
11- bool shouldForward = true ;
12- // Check for messages addressed to the Backpack
13- switch (msg->msgid )
9+ mavlink_status_t status;
10+ mavlink_message_t msg;
11+
12+ if (mavlink_frame_char (MAVLINK_COMM_0, c, &msg, &status) != MAVLINK_FRAMING_INCOMPLETE)
1413 {
15- case MAVLINK_MSG_ID_COMMAND_INT:
16- mavlink_command_int_t commandMsg;
17- mavlink_msg_command_int_decode (msg, &commandMsg);
18- if (commandMsg.target_component == MAV_COMP_ID_UDP_BRIDGE)
14+ if (mavlink_to_gcs_buf_count >= MAVLINK_BUF_SIZE)
15+ {
16+ // Cant fit any more msgs in the queue,
17+ // drop the oldest msg and start overwriting
18+ mavlink_stats.overflows_downlink ++;
19+ mavlink_to_gcs_buf_count = 0 ;
20+ }
21+
22+ // Track gaps in the sequence number, add to a dropped counter
23+ uint8_t seq = msg.seq ;
24+ if (expectedSeqSet && seq != expectedSeq)
1925 {
20- shouldForward = false ;
21- constexpr uint8_t ELRS_MODE_CHANGE = 0x8 ;
22- switch (commandMsg.command )
26+ // account for rollovers
27+ if (seq < expectedSeq)
2328 {
24- case MAV_CMD_USER_1:
25- switch ((int )commandMsg.param1 )
26- {
27- case ELRS_MODE_CHANGE:
28- switch ((int )commandMsg.param2 )
29- {
30- case 0 : // TX_NORMAL_MODE
31- config.SetStartWiFiOnBoot (false );
32- ESP.restart ();
33- break ;
34- case 1 : // TX_MAVLINK_MODE
35- if (config.GetWiFiService () != WIFI_SERVICE_MAVLINK_TX)
36- {
37- config.SetWiFiService (WIFI_SERVICE_MAVLINK_TX);
38- config.SetStartWiFiOnBoot (true );
39- config.Commit ();
40- ESP.restart ();
41- }
42- break ;
43- }
44- }
29+ mavlink_stats.drops_downlink += (UINT8_MAX - expectedSeq) + seq;
4530 }
46- break ;
31+ else
32+ {
33+ mavlink_stats.drops_downlink += seq - expectedSeq;
34+ }
35+ }
36+ expectedSeq = seq + 1 ;
37+ expectedSeqSet = true ;
38+
39+ // Queue the msgs, to forward to peers
40+ mavlink_to_gcs_buf[mavlink_to_gcs_buf_count] = msg;
41+ mavlink_to_gcs_buf_count++;
42+ mavlink_stats.packets_downlink ++;
43+ }
44+ }
45+
46+ void
47+ MAVLink::ProcessMAVLinkFromGCS (uint8_t *data, uint16_t len)
48+ {
49+ mavlink_status_t status;
50+ mavlink_message_t msg;
51+
52+ for (uint16_t i = 0 ; i < len; i++)
53+ {
54+ if (mavlink_frame_char (MAVLINK_COMM_1, data[i], &msg, &status) != MAVLINK_FRAMING_INCOMPLETE)
55+ {
56+ // Send the message to the tx uart
57+ uint8_t buf[MAVLINK_MAX_PACKET_LEN];
58+ uint16_t len = mavlink_msg_to_send_buffer (buf, &msg);
59+ Serial.write (buf, len);
60+ mavlink_stats.packets_uplink ++;
4761 }
4862 }
49- return shouldForward;
5063}
51- #endif
64+ #endif
0 commit comments