Position hold depends on GPS (#14101)
[betaflight.git] / src / test / unit / rx_sumd_unittest.cc
blob843d69ae71f98c33a68e6f3a529730abe7856008
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/barometer.h"
33 #include "sensors/battery.h"
36 #include "unittest_macros.h"
37 #include "gtest/gtest.h"
40 extern "C" {
41 uint8_t batteryCellCount = 3;
42 float rcCommand[4] = {0, 0, 0, 0};
43 int16_t telemTemperature1 = 0;
44 baro_t baro = { .temperature = 50 };
45 telemetryConfig_t telemetryConfig_System;
46 timeUs_t rxFrameTimeUs(void) { return 0; }
50 bool telemetryCheckRxPortShared(const serialPortConfig_t *portConfig, const SerialRXType serialrxProvider)
52 //TODO: implement
53 UNUSED(portConfig);
54 UNUSED(serialrxProvider);
56 return false;
59 serialPort_t *telemetrySharedPort = NULL;
61 static uint16_t vbat = 100;
62 uint16_t getVbat(void)
64 return vbat;
67 uint32_t microseconds_stub_value = 0;
68 uint32_t micros(void)
70 return microseconds_stub_value;
73 uint32_t microsISR(void)
75 return micros();
78 #define SERIAL_BUFFER_SIZE 256
79 #define SERIAL_PORT_DUMMY_IDENTIFIER (serialPortIdentifier_e)0x12
81 typedef struct serialPortStub_s {
82 uint8_t buffer[SERIAL_BUFFER_SIZE];
83 int pos = 0;
84 int end = 0;
85 } serialPortStub_t;
87 static serialPort_t serialTestInstance;
88 static serialPortConfig_t serialTestInstanceConfig = {
89 .functionMask = 0,
90 .identifier = SERIAL_PORT_DUMMY_IDENTIFIER,
93 static serialReceiveCallbackPtr stub_serialRxCallback;
94 static serialPortConfig_t *findSerialPortConfig_stub_retval;
95 static bool openSerial_called = false;
96 static serialPortStub_t serialWriteStub;
97 static bool portIsShared = false;
99 static portMode_e serialExpectedMode = MODE_RX;
100 static portOptions_e serialExpectedOptions = SERIAL_UNIDIR;
103 const serialPortConfig_t *findSerialPortConfig(serialPortFunction_e function)
105 EXPECT_EQ(function, FUNCTION_RX_SERIAL);
106 return findSerialPortConfig_stub_retval;
109 serialPort_t *openSerialPort(
110 serialPortIdentifier_e identifier,
111 serialPortFunction_e function,
112 serialReceiveCallbackPtr callback,
113 void *callbackData,
114 uint32_t baudrate,
115 portMode_e mode,
116 portOptions_e options
119 openSerial_called = true;
120 EXPECT_FALSE(NULL == callback);
121 EXPECT_TRUE(NULL == callbackData);
122 EXPECT_EQ(identifier, SERIAL_PORT_DUMMY_IDENTIFIER);
123 EXPECT_EQ(options, serialExpectedOptions);
124 EXPECT_EQ(function, FUNCTION_RX_SERIAL);
125 EXPECT_EQ(baudrate, 115200);
126 EXPECT_EQ(mode, serialExpectedMode);
127 stub_serialRxCallback = callback;
128 return &serialTestInstance;
131 void serialWrite(serialPort_t *instance, uint8_t ch)
133 EXPECT_EQ(instance, &serialTestInstance);
134 EXPECT_LT(serialWriteStub.pos, sizeof(serialWriteStub.buffer));
135 serialWriteStub.buffer[serialWriteStub.pos++] = ch;
138 void serialTestResetPort()
140 openSerial_called = false;
141 stub_serialRxCallback = NULL;
142 portIsShared = false;
143 serialExpectedMode = MODE_RX;
144 serialExpectedOptions = SERIAL_UNIDIR;
147 class SumdRxInitUnitTest : public ::testing::Test
149 protected:
150 virtual void SetUp()
152 serialTestResetPort();
157 TEST_F(SumdRxInitUnitTest, Test_SumdRxNotEnabled)
159 const rxConfig_t initialRxConfig = {};
160 rxRuntimeState_t rxRuntimeState = {};
161 findSerialPortConfig_stub_retval = NULL;
163 EXPECT_FALSE(sumdInit(&initialRxConfig, &rxRuntimeState));
165 EXPECT_EQ(18, rxRuntimeState.channelCount);
166 EXPECT_FALSE(NULL == rxRuntimeState.rcReadRawFn);
167 EXPECT_FALSE(NULL == rxRuntimeState.rcFrameStatusFn);
171 TEST_F(SumdRxInitUnitTest, Test_SumdRxEnabled)
173 const rxConfig_t initialRxConfig = {};
174 rxRuntimeState_t rxRuntimeState = {};
175 findSerialPortConfig_stub_retval = &serialTestInstanceConfig;
177 EXPECT_TRUE(sumdInit(&initialRxConfig, &rxRuntimeState));
179 EXPECT_EQ(18, rxRuntimeState.channelCount);
180 EXPECT_FALSE(NULL == rxRuntimeState.rcReadRawFn);
181 EXPECT_FALSE(NULL == rxRuntimeState.rcFrameStatusFn);
183 EXPECT_TRUE(openSerial_called);
188 class SumdRxProtocollUnitTest : public ::testing::Test
190 protected:
191 rxRuntimeState_t rxRuntimeState = {};
192 virtual void SetUp()
194 serialTestResetPort();
196 const rxConfig_t initialRxConfig = {};
197 findSerialPortConfig_stub_retval = &serialTestInstanceConfig;
199 EXPECT_TRUE(sumdInit(&initialRxConfig, &rxRuntimeState));
200 microseconds_stub_value += 5000;
201 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
204 virtual void checkValidChannels()
206 //report frame complete once
207 EXPECT_EQ(RX_FRAME_COMPLETE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
208 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
210 EXPECT_EQ(900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 0));
211 EXPECT_EQ(1100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 1));
212 EXPECT_EQ(1500, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 2));
213 EXPECT_EQ(1900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 3));
214 EXPECT_EQ(2100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 4));
218 * the structure of a sumd packet is
219 * 0xA8 = sync byte, 0x01 = status byte, number of channels
220 * Channel data two bytes per channel
221 * two bytes checksum
223 virtual void sendValidPacket()
225 uint8_t packet[] = {0xA8, 0x01, 20,
226 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
227 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
228 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
229 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
230 0x06, 0x3f
233 for (size_t i = 0; i < sizeof(packet); i++) {
234 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
235 stub_serialRxCallback(packet[i], NULL);
237 checkValidChannels();
240 virtual void sendValidLongPacket()
242 uint8_t packet[] = {0xA8, 0x01, 32,
243 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
244 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
245 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
246 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
247 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
248 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
249 0x1c, 0x20, 0x1c, 0x20,
250 0xeb, 0x61
253 for (size_t i = 0; i < sizeof(packet); i++) {
254 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
255 stub_serialRxCallback(packet[i], NULL);
257 checkValidChannels();
260 virtual void sendValidShortPacket()
262 uint8_t packet[] = {0xA8, 0x01, 16,
263 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
264 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
265 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
266 0x1c, 0x20,
267 0x37, 0x24
270 for (size_t i = 0; i < sizeof(packet); i++) {
271 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
272 stub_serialRxCallback(packet[i], NULL);
275 checkValidChannels();
278 virtual void sendValidFsPacket()
281 uint8_t packet[] = {0xA8, 0x81, 0x08,
282 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0, 0x2E, 0xE0,
283 0xF9, 0x0F
286 for (size_t i = 0; i < sizeof(packet); i++) {
287 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
288 stub_serialRxCallback(packet[i], NULL);
291 EXPECT_EQ(RX_FRAME_COMPLETE | RX_FRAME_FAILSAFE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
292 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
294 for (size_t i = 0; i < 8; i++) {
295 EXPECT_EQ(1500, rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
299 virtual void sendInvalidPacket()
301 uint8_t packet[] = {0xA8, 0x01, 20,
302 0x1c, 0x21, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0,
303 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
304 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
305 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20, 0x1c, 0x20,
306 0x06, 0x3f
309 for (size_t i = 0; i < sizeof(packet); i++) {
310 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
311 stub_serialRxCallback(packet[i], NULL);
314 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
316 EXPECT_EQ(900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 0));
317 EXPECT_EQ(1100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 1));
318 EXPECT_EQ(1500, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 2));
319 EXPECT_EQ(1900, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 3));
320 EXPECT_EQ(2100, rxRuntimeState.rcReadRawFn(&rxRuntimeState, 4));
323 virtual void sendIncompletePacket()
325 uint8_t packet[] = {0xA8, 0x01, 20,
326 0x1c, 0x20, 0x22, 0x60, 0x2e, 0xe0, 0x3b, 0x60, 0x41, 0xa0
329 for (size_t i = 0; i < sizeof(packet); i++) {
330 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
331 stub_serialRxCallback(packet[i], NULL);
334 microseconds_stub_value += 5000;
336 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
341 TEST_F(SumdRxProtocollUnitTest, Test_OnePacketReceived)
343 sendValidPacket();
346 TEST_F(SumdRxProtocollUnitTest, Test_MultiplePacketsReceived)
348 sendValidPacket();
349 sendValidPacket();
352 TEST_F(SumdRxProtocollUnitTest, Test_Resync)
354 sendIncompletePacket();
355 sendValidPacket();
358 TEST_F(SumdRxProtocollUnitTest, Test_IgnoreInvalidCRC)
360 sendValidPacket();
361 sendInvalidPacket();
364 TEST_F(SumdRxProtocollUnitTest, Test_ShortPacketsReceived)
366 sendValidShortPacket();
367 sendValidShortPacket();
370 TEST_F(SumdRxProtocollUnitTest, TestShortPacketsReceivedWithFailsafe)
372 sendValidFsPacket();
375 TEST_F(SumdRxProtocollUnitTest, Test_LongPacketsReceived)
377 sendValidLongPacket();
378 sendValidLongPacket();