8 #include "crsf_protocol.h"
9 #include "telemetry_protocol.h"
16 #define OTA4_PACKET_SIZE 8U
17 #define OTA4_CRC_CALC_LEN offsetof(OTA_Packet4_s, crcLow)
18 #define OTA8_PACKET_SIZE 13U
19 #define OTA8_CRC_CALC_LEN offsetof(OTA_Packet8_s, crc)
21 // Packet header types
22 #define PACKET_TYPE_DATA 0b01
23 // Uplink only header types
24 #define PACKET_TYPE_RCDATA 0b00
25 #define PACKET_TYPE_SYNC 0b10
26 // Downlink only header types
27 #define PACKET_TYPE_LINKSTATS 0b00
29 // Mask used to XOR the ModelId into the SYNC packet for ModelMatch
30 #define MODELMATCH_MASK 0x3f
36 uint8_t switchEncMode
:1,
46 uint8_t uplink_RSSI_1
:7,
48 uint8_t uplink_RSSI_2
:7,
53 } PACKED OTA_LinkStats_s
;
56 uint8_t raw
[5]; // 4x 10-bit channels, see PackUInt11ToChannels4x10 for encoding
57 } PACKED OTA_Channels_4x10
;
60 // The packet type must always be the low two bits of the first byte of the
61 // packet to match the same placement in OTA_Packet8_s
65 /** PACKET_TYPE_RCDATA **/
72 uint32_t packetNum
; // LittleEndian
74 } PACKED dbg_linkstats
;
75 /** PACKET_TYPE_MSP **/
77 uint8_t packageIndex
:7,
79 uint8_t payload
[ELRS4_MSP_BYTES_PER_CALL
];
81 /** PACKET_TYPE_SYNC **/
83 /** PACKET_TYPE_TLM **/
90 OTA_LinkStats_s stats
;
91 uint8_t trueDiversityAvailable
:1,
93 } PACKED ul_link_stats
;
94 uint8_t payload
[ELRS4_TELEMETRY_BYTES_PER_CALL
];
96 } tlm_dl
; // PACKET_TYPE_TLM
97 /** PACKET_TYPE_AIRPORT **/
101 uint8_t payload
[ELRS4_TELEMETRY_BYTES_PER_CALL
];
105 } PACKED OTA_Packet4_s
;
108 // Like OTA_Packet4_s **the type is always the low two bits of the first byte**,
109 // but they are speficied in each type in the union as the other bits there
112 /** PACKET_TYPE_RCDATA **/
114 uint8_t packetType
: 2,
116 uplinkPower
: 3, // CRSF_power_level - 1 (1-8 is 0-7 in the air)
117 isHighAux
: 1, // true if chHigh are AUX6-9
119 OTA_Channels_4x10 chLow
; // CH0-CH3
120 OTA_Channels_4x10 chHigh
; // AUX2-5 or AUX6-9
123 uint8_t packetType
; // actually struct rc's first byte
124 uint32_t packetNum
; // LittleEndian
126 } PACKED dbg_linkstats
;
127 /** PACKET_TYPE_MSP **/
129 uint8_t packetType
: 2,
132 uint8_t payload
[ELRS8_MSP_BYTES_PER_CALL
];
134 /** PACKET_TYPE_SYNC **/
136 uint8_t packetType
; // only low 2 bits
140 /** PACKET_TYPE_TLM **/
142 uint8_t packetType
: 2,
147 OTA_LinkStats_s stats
;
148 uint8_t trueDiversityAvailable
:1,
150 uint8_t payload
[ELRS8_TELEMETRY_BYTES_PER_CALL
- sizeof(OTA_LinkStats_s
) - 1];
151 } PACKED ul_link_stats
;
152 uint8_t payload
[ELRS8_TELEMETRY_BYTES_PER_CALL
]; // containsLinkStats == false
155 /** PACKET_TYPE_AIRPORT **/
157 uint8_t packetType
: 2,
160 uint8_t payload
[ELRS8_TELEMETRY_BYTES_PER_CALL
];
163 uint16_t crc
; // crc16 LittleEndian
164 } PACKED OTA_Packet8_s
;
171 } PACKED OTA_Packet_s
;
173 extern bool OtaIsFullRes
;
174 extern volatile uint8_t OtaNonce
;
175 extern uint16_t OtaCrcInitializer
;
176 void OtaUpdateCrcInitFromUid();
178 enum OtaSwitchMode_e
{ smWideOr8ch
= 0, smHybridOr16ch
= 1, sm12ch
= 2 };
179 void OtaUpdateSerializers(OtaSwitchMode_e
const mode
, uint8_t packetSize
);
180 extern OtaSwitchMode_e OtaSwitchModeCurrent
;
183 typedef bool (*ValidatePacketCrc_t
)(OTA_Packet_s
* const otaPktPtr
);
184 typedef void (*GeneratePacketCrc_t
)(OTA_Packet_s
* const otaPktPtr
);
185 extern ValidatePacketCrc_t OtaValidatePacketCrc
;
186 extern GeneratePacketCrc_t OtaGeneratePacketCrc
;
187 // Value is implicit leading 1, comment is Koopman formatting (implicit trailing 1) https://users.ece.cmu.edu/~koopman/crc/
188 #define ELRS_CRC_POLY 0x07 // 0x83
189 #define ELRS_CRC14_POLY 0x2E57 // 0x372b
190 #define ELRS_CRC16_POLY 0x3D65 // 0x9eb2
192 #if defined(TARGET_TX) || defined(UNIT_TEST)
193 typedef void (*PackChannelData_t
)(OTA_Packet_s
* const otaPktPtr
, const uint32_t *channelData
, bool TelemetryStatus
, uint8_t tlmDenom
);
194 extern PackChannelData_t OtaPackChannelData
;
195 #if defined(UNIT_TEST)
196 void OtaSetHybrid8NextSwitchIndex(uint8_t idx
);
197 void OtaSetFullResNextChannelSet(bool next
);
201 #if defined(TARGET_RX) || defined(UNIT_TEST)
202 typedef bool (*UnpackChannelData_t
)(OTA_Packet_s
const * const otaPktPtr
, uint32_t *channelData
, uint8_t tlmDenom
);
203 extern UnpackChannelData_t OtaUnpackChannelData
;
206 void OtaPackAirportData(OTA_Packet_s
* const otaPktPtr
, FIFO
<AP_MAX_BUF_LEN
> *inputBuffer
);
207 void OtaUnpackAirportData(OTA_Packet_s
const * const otaPktPtr
, FIFO
<AP_MAX_BUF_LEN
> *outputBuffer
);
209 #if defined(DEBUG_RCVR_LINKSTATS)
210 extern uint32_t debugRcvrLinkstatsPacketId
;