Bump clang version to 18 (#14116)
[betaflight.git] / src / test / unit / rx_ibus_unittest.cc
blob38ebf1d556a09c8be28f35279cc7b37f6f699ef5
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight 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 * Cleanflight 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 Cleanflight. 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/ibus.h"
29 #include "telemetry/ibus_shared.h"
30 #include "telemetry/telemetry.h"
31 #include "fc/rc_controls.h"
32 #include "fc/rc_modes.h"
33 #include "sensors/battery.h"
36 #include "unittest_macros.h"
37 #include "gtest/gtest.h"
39 bool telemetryCheckRxPortShared(const serialPortConfig_t *portConfig)
41 //TODO: implement
42 (void) portConfig;
43 return false;
46 serialPort_t * telemetrySharedPort = NULL;
48 static uint16_t vbat = 100;
49 uint16_t getVbat(void)
51 return vbat;
54 uint32_t microseconds_stub_value = 0;
55 uint32_t micros(void)
57 return microseconds_stub_value;
59 uint32_t microsISR(void)
61 return micros();
64 #define SERIAL_BUFFER_SIZE 256
65 #define SERIAL_PORT_DUMMY_IDENTIFIER (serialPortIdentifier_e)0x12
67 typedef struct serialPortStub_s {
68 uint8_t buffer[SERIAL_BUFFER_SIZE];
69 int pos = 0;
70 int end = 0;
71 } serialPortStub_t;
73 static serialPort_t serialTestInstance;
74 static serialPortConfig_t serialTestInstanceConfig = {
75 .functionMask = 0,
76 .identifier = SERIAL_PORT_DUMMY_IDENTIFIER,
77 .msp_baudrateIndex = 0,
78 .gps_baudrateIndex = 0,
79 .blackbox_baudrateIndex = 0,
80 .telemetry_baudrateIndex = 0,
83 static serialReceiveCallbackPtr stub_serialRxCallback;
84 static serialPortConfig_t *findSerialPortConfig_stub_retval;
85 static bool openSerial_called = false;
86 static serialPortStub_t serialWriteStub;
87 static bool portIsShared = false;
89 bool isSerialPortShared(const serialPortConfig_t *portConfig,
90 uint16_t functionMask,
91 serialPortFunction_e sharedWithFunction)
93 EXPECT_EQ(portConfig, findSerialPortConfig_stub_retval);
94 EXPECT_EQ(functionMask, FUNCTION_RX_SERIAL);
95 EXPECT_EQ(sharedWithFunction, FUNCTION_TELEMETRY_IBUS);
96 return portIsShared;
99 const serialPortConfig_t *findSerialPortConfig(serialPortFunction_e function)
101 EXPECT_EQ(function, FUNCTION_RX_SERIAL);
102 return findSerialPortConfig_stub_retval;
105 static portMode_e serialExpectedMode = MODE_RX;
106 static portOptions_e serialExpectedOptions = SERIAL_UNIDIR;
108 serialPort_t *openSerialPort(
109 serialPortIdentifier_e identifier,
110 serialPortFunction_e function,
111 serialReceiveCallbackPtr callback,
112 void *callbackData,
113 uint32_t baudrate,
114 portMode_e mode,
115 portOptions_e options
118 openSerial_called = true;
119 EXPECT_FALSE(NULL == callback);
120 EXPECT_TRUE(NULL == callbackData);
121 EXPECT_EQ(identifier, SERIAL_PORT_DUMMY_IDENTIFIER);
122 EXPECT_EQ(options, serialExpectedOptions);
123 EXPECT_EQ(function, FUNCTION_RX_SERIAL);
124 EXPECT_EQ(baudrate, 115200);
125 EXPECT_EQ(mode, serialExpectedMode);
126 stub_serialRxCallback = callback;
127 return &serialTestInstance;
130 void serialWrite(serialPort_t *instance, uint8_t ch)
132 EXPECT_EQ(instance, &serialTestInstance);
133 EXPECT_LT(serialWriteStub.pos, sizeof(serialWriteStub.buffer));
134 serialWriteStub.buffer[serialWriteStub.pos++] = ch;
135 //TODO serialReadStub.buffer[serialReadStub.end++] = ch; //characters echoes back on the shared wire
136 //printf("w: %02d 0x%02x\n", serialWriteStub.pos, ch);
140 void serialTestResetPort()
142 openSerial_called = false;
143 stub_serialRxCallback = NULL;
144 portIsShared = false;
145 serialExpectedMode = MODE_RX;
146 serialExpectedOptions = SERIAL_UNIDIR;
150 static bool isChecksumOkReturnValue = true;
151 bool isChecksumOkIa6b(const uint8_t *ibusPacket, const uint8_t length)
153 (void) ibusPacket;
154 (void) length;
155 return isChecksumOkReturnValue;
159 static bool initSharedIbusTelemetryCalled = false;
160 void initSharedIbusTelemetry(serialPort_t * port)
162 EXPECT_EQ(port, &serialTestInstance);
163 initSharedIbusTelemetryCalled = true;
166 static bool stubTelemetryCalled = false;
167 static uint8_t stubTelemetryPacket[100];
168 static uint8_t stubTelemetryIgnoreRxChars = 0;
170 uint8_t respondToIbusRequest(uint8_t const * const ibusPacket)
172 uint8_t len = ibusPacket[0];
173 EXPECT_LT(len, sizeof(stubTelemetryPacket));
174 memcpy(stubTelemetryPacket, ibusPacket, len);
175 stubTelemetryCalled = true;
176 return stubTelemetryIgnoreRxChars;
179 void resetStubTelemetry(void)
181 memset(stubTelemetryPacket, 0, sizeof(stubTelemetryPacket));
182 stubTelemetryCalled = false;
183 stubTelemetryIgnoreRxChars = 0;
184 initSharedIbusTelemetryCalled = false;
185 isChecksumOkReturnValue = true;
189 class IbusRxInitUnitTest : public ::testing::Test
191 protected:
192 virtual void SetUp()
194 serialTestResetPort();
199 TEST_F(IbusRxInitUnitTest, Test_IbusRxNotEnabled)
201 const rxConfig_t initialRxConfig = {};
202 rxRuntimeState_t rxRuntimeState = {};
203 findSerialPortConfig_stub_retval = NULL;
205 EXPECT_FALSE(ibusInit(&initialRxConfig, &rxRuntimeState));
207 //TODO: Question: I'd expect that runtime conf was not initialized unless there was a serial port to run but the implementation states otherwise
208 // EXPECT_EQ(0, rxRuntimeState.channelCount);
209 // EXPECT_EQ(0, rxRuntimeState.rxRefreshRate);
210 // EXPECT_EQ(NULL, rxRuntimeState.rcReadRawFn);
211 // EXPECT_EQ(NULL, rxRuntimeState.rcFrameStatusFn);
213 EXPECT_EQ(18, rxRuntimeState.channelCount);
214 EXPECT_FALSE(NULL == rxRuntimeState.rcReadRawFn);
215 EXPECT_FALSE(NULL == rxRuntimeState.rcFrameStatusFn);
219 TEST_F(IbusRxInitUnitTest, Test_IbusRxEnabled)
221 const rxConfig_t initialRxConfig = {};
222 rxRuntimeState_t rxRuntimeState = {};
223 findSerialPortConfig_stub_retval = &serialTestInstanceConfig;
225 EXPECT_TRUE(ibusInit(&initialRxConfig, &rxRuntimeState));
227 EXPECT_EQ(18, rxRuntimeState.channelCount);
228 EXPECT_FALSE(NULL == rxRuntimeState.rcReadRawFn);
229 EXPECT_FALSE(NULL == rxRuntimeState.rcFrameStatusFn);
231 EXPECT_TRUE(openSerial_called);
236 class IbusRxProtocollUnitTest : public ::testing::Test
238 protected:
239 rxRuntimeState_t rxRuntimeState = {};
240 virtual void SetUp()
242 serialTestResetPort();
243 resetStubTelemetry();
244 portIsShared = true;
245 serialExpectedOptions = SERIAL_BIDIR;
246 serialExpectedMode = MODE_RXTX;
248 const rxConfig_t initialRxConfig = {};
249 findSerialPortConfig_stub_retval = &serialTestInstanceConfig;
251 EXPECT_TRUE(ibusInit(&initialRxConfig, &rxRuntimeState));
253 EXPECT_TRUE(initSharedIbusTelemetryCalled);
255 //handle that internal ibus position is not set to zero at init
256 microseconds_stub_value += 5000;
257 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
260 virtual void receivePacket(uint8_t const * const packet, const size_t length)
262 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
263 for (size_t i=0; i < length; i++) {
264 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
265 stub_serialRxCallback(packet[i], NULL);
271 TEST_F(IbusRxProtocollUnitTest, Test_InitialFrameState)
273 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
275 //TODO: is it ok to have undefined channel values after init?
279 TEST_F(IbusRxProtocollUnitTest, Test_IA6B_OnePacketReceived)
281 uint8_t packet[] = {0x20, 0x00, //length and reserved (unknown) bits
282 0x00, 0xE0, 0x01, 0x00, 0x02, 0x00, 0x03, 0xF0, 0x04, 0x00, //channel 1..5 + 15 + 16
283 0x05, 0x00, 0x06, 0x00, 0x07, 0x10, 0x08, 0x00, 0x09, 0x10, //channel 6..10 + 17 + 18(lsb)
284 0x0a, 0x10, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14 + 18
285 0x84, 0xfd}; //checksum
287 for (size_t i=0; i < sizeof(packet); i++) {
288 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
289 stub_serialRxCallback(packet[i], NULL);
292 //report frame complete once
293 EXPECT_EQ(RX_FRAME_COMPLETE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
294 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
296 //check that channel values have been updated
297 for (int i=0; i<18; i++) {
298 EXPECT_EQ(i, rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
303 TEST_F(IbusRxProtocollUnitTest, Test_IA6B_OnePacketReceivedWithBadCrc)
305 uint8_t packet[] = {0x20, 0x00, //length and reserved (unknown) bits
306 0x00, 0x33, 0x01, 0x33, 0x02, 0x33, 0x03, 0x33, 0x04, 0x33, //channel 1..5
307 0x05, 0x33, 0x06, 0x33, 0x07, 0x33, 0x08, 0x33, 0x09, 0x33, //channel 6..10
308 0x0a, 0x33, 0x0b, 0x33, 0x0c, 0x33, 0x0d, 0x33, //channel 11..14
309 0x00, 0x00}; //checksum
311 isChecksumOkReturnValue = false;
312 for (size_t i=0; i < sizeof(packet); i++) {
313 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
314 stub_serialRxCallback(packet[i], NULL);
317 //no frame complete
318 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
320 //check that channel values have not been updated
321 for (int i=0; i<14; i++) {
322 EXPECT_NE(i + (0x33 << 8), rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
327 TEST_F(IbusRxProtocollUnitTest, Test_IA6B_HalfPacketReceived_then_interPacketGapReset)
329 const uint8_t packet_half[] = {0x20, 0x00, //length and reserved (unknown) bits
330 0x00, 0xab, 0x01, 0xab, 0x02, 0xab, 0x03, 0xab, 0x04, 0xab, //channel 1..5
331 0x05, 0xab};
332 const uint8_t packet_full[] = {0x20, 0x00, //length and reserved (unknown) bits
333 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
334 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
335 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
336 0x84, 0xff}; //checksum
338 for (size_t i=0; i < sizeof(packet_half); i++) {
339 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
340 stub_serialRxCallback(packet_half[i], NULL);
343 microseconds_stub_value += 5000;
344 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
346 for (size_t i=0; i < sizeof(packet_full); i++) {
347 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
348 stub_serialRxCallback(packet_full[i], NULL);
351 //report frame complete once
352 EXPECT_EQ(RX_FRAME_COMPLETE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
353 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
355 //check that channel values have been updated
356 for (int i=0; i<14; i++) {
357 EXPECT_EQ(i, rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
362 TEST_F(IbusRxProtocollUnitTest, Test_IA6_OnePacketReceived)
364 uint8_t packet[] = {0x55, //sync character
365 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
366 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
367 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
368 0x5b, 0x00}; //checksum
370 for (size_t i=0; i < sizeof(packet); i++) {
371 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
372 stub_serialRxCallback(packet[i], NULL);
375 //report frame complete once
376 EXPECT_EQ(RX_FRAME_COMPLETE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
377 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
379 //check that channel values have been updated
380 for (int i=0; i<14; i++) {
381 EXPECT_EQ(i, rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
386 TEST_F(IbusRxProtocollUnitTest, Test_IA6_OnePacketReceivedBadCrc)
388 uint8_t packet[] = {0x55, //sync character
389 0x00, 0x33, 0x01, 0x33, 0x02, 0x33, 0x03, 0x33, 0x04, 0x33, //channel 1..5
390 0x05, 0x33, 0x06, 0x33, 0x07, 0x33, 0x08, 0x33, 0x09, 0x33, //channel 6..10
391 0x0a, 0x33, 0x0b, 0x33, 0x0c, 0x33, 0x0d, 0x33, //channel 11..14
392 0x00, 0x00}; //checksum
394 for (size_t i=0; i < sizeof(packet); i++) {
395 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
396 stub_serialRxCallback(packet[i], NULL);
399 //no frame complete
400 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
402 //check that channel values have not been updated
403 for (int i=0; i<14; i++) {
404 EXPECT_NE(i + (0x33 << 8), rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
409 TEST_F(IbusRxProtocollUnitTest, Test_IA6B_OnePacketReceived_not_shared_port)
411 uint8_t packet[] = {0x20, 0x00, //length and reserved (unknown) bits
412 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
413 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
414 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
415 0x84, 0xff}; //checksum
419 serialTestResetPort();
420 resetStubTelemetry();
421 portIsShared = false;
422 serialExpectedOptions = SERIAL_NOT_INVERTED;
423 serialExpectedMode = MODE_RX;
425 const rxConfig_t initialRxConfig = {};
426 findSerialPortConfig_stub_retval = &serialTestInstanceConfig;
428 EXPECT_TRUE(ibusInit(&initialRxConfig, &rxRuntimeState));
429 EXPECT_FALSE(initSharedIbusTelemetryCalled);
431 //handle that internal ibus position is not set to zero at init
432 microseconds_stub_value += 5000;
433 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
436 for (size_t i=0; i < sizeof(packet); i++) {
437 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
438 stub_serialRxCallback(packet[i], NULL);
441 //report frame complete once
442 EXPECT_EQ(RX_FRAME_COMPLETE, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
443 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
445 //check that channel values have been updated
446 for (int i=0; i<14; i++) {
447 EXPECT_EQ(i, rxRuntimeState.rcReadRawFn(&rxRuntimeState, i));
452 TEST_F(IbusRxProtocollUnitTest, Test_OneTelemetryPacketReceived)
454 uint8_t packet[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
455 resetStubTelemetry();
457 receivePacket(packet, sizeof(packet));
459 //no frame complete signal to rx system, but telemetry system is called
460 EXPECT_EQ(RX_FRAME_PENDING, rxRuntimeState.rcFrameStatusFn(&rxRuntimeState));
461 EXPECT_TRUE(stubTelemetryCalled);
462 EXPECT_TRUE( 0 == memcmp( stubTelemetryPacket, packet, sizeof(packet)));
466 TEST_F(IbusRxProtocollUnitTest, Test_OneTelemetryIgnoreTxEchoToRx)
468 uint8_t packet[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
469 resetStubTelemetry();
470 stubTelemetryIgnoreRxChars = 4;
472 //given one packet received, that will respond with four characters to be ignored
473 receivePacket(packet, sizeof(packet));
474 rxRuntimeState.rcFrameStatusFn(&rxRuntimeState);
475 EXPECT_TRUE(stubTelemetryCalled);
477 //when those four bytes are sent and looped back
478 resetStubTelemetry();
479 rxRuntimeState.rcFrameStatusFn(&rxRuntimeState);
480 receivePacket(packet, sizeof(packet));
482 //then they are ignored
483 EXPECT_FALSE(stubTelemetryCalled);
485 //and then next packet can be received
486 receivePacket(packet, sizeof(packet));
487 rxRuntimeState.rcFrameStatusFn(&rxRuntimeState);
488 EXPECT_TRUE(stubTelemetryCalled);
492 TEST_F(IbusRxProtocollUnitTest, Test_OneTelemetryShouldNotIgnoreTxEchoAfterInterFrameGap)
494 uint8_t packet[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
495 resetStubTelemetry();
496 stubTelemetryIgnoreRxChars = 4;
498 //given one packet received, that will respond with four characters to be ignored
499 receivePacket(packet, sizeof(packet));
500 rxRuntimeState.rcFrameStatusFn(&rxRuntimeState);
501 EXPECT_TRUE(stubTelemetryCalled);
503 //when there is an interPacketGap
504 microseconds_stub_value += 5000;
505 resetStubTelemetry();
506 rxRuntimeState.rcFrameStatusFn(&rxRuntimeState);
508 //then next packet can be received
509 receivePacket(packet, sizeof(packet));
510 rxRuntimeState.rcFrameStatusFn(&rxRuntimeState);
511 EXPECT_TRUE(stubTelemetryCalled);