Bump clang version to 18 (#14116)
[betaflight.git] / src / test / unit / rx_sumd_unittest.cc
blobd5f9d2a1ff88f59ef5157e33084b28719f36dd6f
1 /*
2 * This file is part of Betaflight.
4 * Betaflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Betaflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Betaflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdint.h>
20 extern "C" {
21 #include "platform.h"
22 #include "pg/pg.h"
23 #include "pg/rx.h"
24 #include "drivers/serial.h"
25 #include "drivers/time.h"
26 #include "io/serial.h"
27 #include "rx/rx.h"
28 #include "rx/sumd.h"
29 #include "telemetry/telemetry.h"
30 #include "fc/rc_controls.h"
31 #include "fc/rc_modes.h"
32 #include "sensors/battery.h"
35 #include "unittest_macros.h"
36 #include "gtest/gtest.h"
38 bool telemetryCheckRxPortShared(const serialPortConfig_t *portConfig, const SerialRXType serialrxProvider)
40 //TODO: implement
41 UNUSED(portConfig);
42 UNUSED(serialrxProvider);
44 return false;
47 serialPort_t *telemetrySharedPort = NULL;
49 static uint16_t vbat = 100;
50 uint16_t getVbat(void)
52 return vbat;
55 uint32_t microseconds_stub_value = 0;
56 uint32_t micros(void)
58 return microseconds_stub_value;
61 uint32_t microsISR(void)
63 return micros();
66 #define SERIAL_BUFFER_SIZE 256
67 #define SERIAL_PORT_DUMMY_IDENTIFIER (serialPortIdentifier_e)0x12
69 typedef struct serialPortStub_s {
70 uint8_t buffer[SERIAL_BUFFER_SIZE];
71 int pos = 0;
72 int end = 0;
73 } serialPortStub_t;
75 static serialPort_t serialTestInstance;
76 static serialPortConfig_t serialTestInstanceConfig = {
77 .functionMask = 0,
78 .identifier = SERIAL_PORT_DUMMY_IDENTIFIER,
79 .msp_baudrateIndex = 0,
80 .gps_baudrateIndex = 0,
81 .blackbox_baudrateIndex = 0,
82 .telemetry_baudrateIndex = 0,
85 static serialReceiveCallbackPtr stub_serialRxCallback;
86 static serialPortConfig_t *findSerialPortConfig_stub_retval;
87 static bool openSerial_called = false;
88 static serialPortStub_t serialWriteStub;
89 static bool portIsShared = false;
91 static portMode_e serialExpectedMode = MODE_RX;
92 static portOptions_e serialExpectedOptions = SERIAL_UNIDIR;
95 const serialPortConfig_t *findSerialPortConfig(serialPortFunction_e function)
97 EXPECT_EQ(function, FUNCTION_RX_SERIAL);
98 return findSerialPortConfig_stub_retval;
101 serialPort_t *openSerialPort(
102 serialPortIdentifier_e identifier,
103 serialPortFunction_e function,
104 serialReceiveCallbackPtr callback,
105 void *callbackData,
106 uint32_t baudrate,
107 portMode_e mode,
108 portOptions_e options
111 openSerial_called = true;
112 EXPECT_FALSE(NULL == callback);
113 EXPECT_TRUE(NULL == callbackData);
114 EXPECT_EQ(identifier, SERIAL_PORT_DUMMY_IDENTIFIER);
115 EXPECT_EQ(options, serialExpectedOptions);
116 EXPECT_EQ(function, FUNCTION_RX_SERIAL);
117 EXPECT_EQ(baudrate, 115200);
118 EXPECT_EQ(mode, serialExpectedMode);
119 stub_serialRxCallback = callback;
120 return &serialTestInstance;
123 void serialWrite(serialPort_t *instance, uint8_t ch)
125 EXPECT_EQ(instance, &serialTestInstance);
126 EXPECT_LT(serialWriteStub.pos, sizeof(serialWriteStub.buffer));
127 serialWriteStub.buffer[serialWriteStub.pos++] = ch;
130 void serialTestResetPort()
132 openSerial_called = false;
133 stub_serialRxCallback = NULL;
134 portIsShared = false;
135 serialExpectedMode = MODE_RX;
136 serialExpectedOptions = SERIAL_UNIDIR;
139 class SumdRxInitUnitTest : public ::testing::Test
141 protected:
142 virtual void SetUp()
144 serialTestResetPort();
149 TEST_F(SumdRxInitUnitTest, Test_SumdRxNotEnabled)
151 const rxConfig_t initialRxConfig = {};
152 rxRuntimeState_t rxRuntimeState = {};
153 findSerialPortConfig_stub_retval = NULL;
155 EXPECT_FALSE(sumdInit(&initialRxConfig, &rxRuntimeState));
157 EXPECT_EQ(18, rxRuntimeState.channelCount);
158 EXPECT_FALSE(NULL == rxRuntimeState.rcReadRawFn);
159 EXPECT_FALSE(NULL == rxRuntimeState.rcFrameStatusFn);
163 TEST_F(SumdRxInitUnitTest, Test_SumdRxEnabled)
165 const rxConfig_t initialRxConfig = {};
166 rxRuntimeState_t rxRuntimeState = {};
167 findSerialPortConfig_stub_retval = &serialTestInstanceConfig;
169 EXPECT_TRUE(sumdInit(&initialRxConfig, &rxRuntimeState));
171 EXPECT_EQ(18, rxRuntimeState.channelCount);
172 EXPECT_FALSE(NULL == rxRuntimeState.rcReadRawFn);
173 EXPECT_FALSE(NULL == rxRuntimeState.rcFrameStatusFn);
175 EXPECT_TRUE(openSerial_called);
180 class SumdRxProtocollUnitTest : public ::testing::Test
182 protected:
183 rxRuntimeState_t rxRuntimeState = {};
184 virtual void SetUp()
186 serialTestResetPort();
188 const rxConfig_t initialRxConfig = {};
189 findSerialPortConfig_stub_retval = &serialTestInstanceConfig;
191 EXPECT_TRUE(sumdInit(&initialRxConfig, &rxRuntimeState));
192 microseconds_stub_value += 5000;
193 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
196 virtual void checkValidChannels()
198 //report frame complete once
199 EXPECT_EQ(RX_FRAME_COMPLETE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
200 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
202 EXPECT_EQ(900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 0));
203 EXPECT_EQ(1100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 1));
204 EXPECT_EQ(1500, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 2));
205 EXPECT_EQ(1900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 3));
206 EXPECT_EQ(2100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 4));
210 * the structure of a sumd packet is
211 * 0xA8 = sync byte, 0x01 = status byte, number of channels
212 * Channel data two bytes per channel
213 * two bytes checksum
215 virtual void sendValidPacket()
217 uint8_t packet[] = {0xA8, 0x01, 20,
218 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
219 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
220 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
221 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
222 0x06, 0x3f
225 for (size_t i = 0; i < sizeof(packet); i++) {
226 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
227 stub_serialRxCallback(packet[i], NULL);
229 checkValidChannels();
232 virtual void sendValidLongPacket()
234 uint8_t packet[] = {0xA8, 0x01, 32,
235 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
236 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
237 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
238 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
239 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
240 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
241 0x1c, 0x20, 0x1c, 0x20,
242 0xeb, 0x61
245 for (size_t i = 0; i < sizeof(packet); i++) {
246 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
247 stub_serialRxCallback(packet[i], NULL);
249 checkValidChannels();
252 virtual void sendValidShortPacket()
254 uint8_t packet[] = {0xA8, 0x01, 16,
255 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
256 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
257 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
258 0x1c, 0x20,
259 0x37, 0x24
262 for (size_t i = 0; i < sizeof(packet); i++) {
263 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
264 stub_serialRxCallback(packet[i], NULL);
267 checkValidChannels();
270 virtual void sendValidFsPacket()
273 uint8_t packet[] = {0xA8, 0x81, 0x08,
274 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0,
275 0xF9, 0x0F
278 for (size_t i = 0; i < sizeof(packet); i++) {
279 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
280 stub_serialRxCallback(packet[i], NULL);
283 EXPECT_EQ(RX_FRAME_COMPLETE | RX_FRAME_FAILSAFE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
284 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
286 for (size_t i = 0; i < 8; i++) {
287 EXPECT_EQ(1500, rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
291 virtual void sendInvalidPacket()
293 uint8_t packet[] = {0xA8, 0x01, 20,
294 0x1c, 0x21, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
295 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
296 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
297 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
298 0x06, 0x3f
301 for (size_t i = 0; i < sizeof(packet); i++) {
302 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
303 stub_serialRxCallback(packet[i], NULL);
306 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
308 EXPECT_EQ(900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 0));
309 EXPECT_EQ(1100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 1));
310 EXPECT_EQ(1500, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 2));
311 EXPECT_EQ(1900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 3));
312 EXPECT_EQ(2100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 4));
315 virtual void sendIncompletePacket()
317 uint8_t packet[] = {0xA8, 0x01, 20,
318 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0
321 for (size_t i = 0; i < sizeof(packet); i++) {
322 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
323 stub_serialRxCallback(packet[i], NULL);
326 microseconds_stub_value += 5000;
328 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
333 TEST_F(SumdRxProtocollUnitTest, Test_OnePacketReceived)
335 sendValidPacket();
338 TEST_F(SumdRxProtocollUnitTest, Test_MultiplePacketsReceived)
340 sendValidPacket();
341 sendValidPacket();
344 TEST_F(SumdRxProtocollUnitTest, Test_Resync)
346 sendIncompletePacket();
347 sendValidPacket();
350 TEST_F(SumdRxProtocollUnitTest, Test_IgnoreInvalidCRC)
352 sendValidPacket();
353 sendInvalidPacket();
356 TEST_F(SumdRxProtocollUnitTest, Test_ShortPacketsReceived)
358 sendValidShortPacket();
359 sendValidShortPacket();
362 TEST_F(SumdRxProtocollUnitTest, TestShortPacketsReceivedWithFailsafe)
364 sendValidFsPacket();
367 TEST_F(SumdRxProtocollUnitTest, Test_LongPacketsReceived)
369 sendValidLongPacket();
370 sendValidLongPacket();