1 #include "AP_Frsky_MAVlite_SPortToMAVlite.h"
3 #include "AP_Frsky_MAVlite.h"
5 #if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL
6 void AP_Frsky_MAVlite_SPortToMAVlite::reset(void)
11 payload_next_byte
= 0;
12 parse_state
= State::WANT_LEN
;
15 void AP_Frsky_MAVlite_SPortToMAVlite::update_checksum(const uint8_t c
)
17 checksum
+= c
; //0-1FF
18 checksum
+= checksum
>> 8;
23 Parses sport packets and if successful fills the rxmsg mavlite struct
25 bool AP_Frsky_MAVlite_SPortToMAVlite::process(AP_Frsky_MAVlite_Message
&rxmsg
, const AP_Frsky_SPort::sport_packet_t
&packet
)
27 // the two skipped bytes in packet.raw here are sensor and frame.
28 // appid and data are used to transport the mavlite message.
30 // deal with packet sequence number:
31 const uint8_t received_seq
= (packet
.raw
[2] & 0x3F);
32 // if the first byte of any sport passthrough packet is zero then we reset:
33 if (received_seq
== 0) {
36 if (received_seq
!= expected_seq
) {
37 parse_state
= State::ERROR
;
40 update_checksum(received_seq
);
41 expected_seq
= received_seq
+ 1;
43 // deal with the remainder (post-sequence) of the packet:
44 for (uint8_t i
=3; i
<ARRAY_SIZE(packet
.raw
); i
++) {
47 if (parse_state
== State::MESSAGE_RECEIVED
) {
54 void AP_Frsky_MAVlite_SPortToMAVlite::parse(uint8_t byte
)
56 switch (parse_state
) {
59 // it is an error to receive anything but offset==0 byte=0xx0
61 parse_state
= State::ERROR
;
65 // waiting for offset==0 && byte==0x00 to bump us into WANT_LEN
70 update_checksum(byte
);
71 parse_state
= State::WANT_MSGID
;
74 case State::WANT_MSGID
:
76 update_checksum(byte
);
77 if (_rxmsg
.len
== 0) {
78 parse_state
= State::WANT_CHECKSUM
;
80 parse_state
= State::WANT_PAYLOAD
;
84 case State::WANT_PAYLOAD
:
85 // add byte to payload
86 _rxmsg
.payload
[payload_next_byte
++] = byte
;
87 update_checksum(byte
);
89 if (payload_next_byte
>= _rxmsg
.len
) {
90 parse_state
= State::WANT_CHECKSUM
;
94 case State::WANT_CHECKSUM
:
95 if (checksum
!= byte
) {
96 parse_state
= State::ERROR
;
99 parse_state
= State::MESSAGE_RECEIVED
;
102 case State::MESSAGE_RECEIVED
: