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/>.
24 #include "drivers/serial.h"
25 #include "drivers/time.h"
26 #include "io/serial.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/barometer.h"
34 #include "sensors/battery.h"
37 #include "unittest_macros.h"
38 #include "gtest/gtest.h"
42 uint8_t batteryCellCount
= 3;
43 float rcCommand
[4] = {0, 0, 0, 0};
44 int16_t telemTemperature1
= 0;
45 baro_t baro
= { .baroTemperature
= 50 };
46 telemetryConfig_t telemetryConfig_System
;
47 timeUs_t
rxFrameTimeUs(void) { return 0; }
51 bool telemetryCheckRxPortShared(const serialPortConfig_t
*portConfig
)
58 serialPort_t
* telemetrySharedPort
= NULL
;
60 static uint16_t vbat
= 100;
61 uint16_t getVbat(void)
66 uint32_t microseconds_stub_value
= 0;
69 return microseconds_stub_value
;
71 uint32_t microsISR(void)
76 #define SERIAL_BUFFER_SIZE 256
77 #define SERIAL_PORT_DUMMY_IDENTIFIER (serialPortIdentifier_e)0x12
79 typedef struct serialPortStub_s
{
80 uint8_t buffer
[SERIAL_BUFFER_SIZE
];
85 static serialPort_t serialTestInstance
;
86 static serialPortConfig_t serialTestInstanceConfig
= {
87 .identifier
= SERIAL_PORT_DUMMY_IDENTIFIER
,
91 static serialReceiveCallbackPtr stub_serialRxCallback
;
92 static serialPortConfig_t
*findSerialPortConfig_stub_retval
;
93 static bool openSerial_called
= false;
94 static serialPortStub_t serialWriteStub
;
95 static bool portIsShared
= false;
97 bool isSerialPortShared(const serialPortConfig_t
*portConfig
,
98 uint16_t functionMask
,
99 serialPortFunction_e sharedWithFunction
)
101 EXPECT_EQ(portConfig
, findSerialPortConfig_stub_retval
);
102 EXPECT_EQ(functionMask
, FUNCTION_RX_SERIAL
);
103 EXPECT_EQ(sharedWithFunction
, FUNCTION_TELEMETRY_IBUS
);
107 const serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e function
)
109 EXPECT_EQ(function
, FUNCTION_RX_SERIAL
);
110 return findSerialPortConfig_stub_retval
;
113 static portMode_e serialExpectedMode
= MODE_RX
;
114 static portOptions_e serialExpectedOptions
= SERIAL_UNIDIR
;
116 serialPort_t
*openSerialPort(
117 serialPortIdentifier_e identifier
,
118 serialPortFunction_e function
,
119 serialReceiveCallbackPtr callback
,
123 portOptions_e options
126 openSerial_called
= true;
127 EXPECT_FALSE(NULL
== callback
);
128 EXPECT_TRUE(NULL
== callbackData
);
129 EXPECT_EQ(identifier
, SERIAL_PORT_DUMMY_IDENTIFIER
);
130 EXPECT_EQ(options
, serialExpectedOptions
);
131 EXPECT_EQ(function
, FUNCTION_RX_SERIAL
);
132 EXPECT_EQ(baudrate
, 115200);
133 EXPECT_EQ(mode
, serialExpectedMode
);
134 stub_serialRxCallback
= callback
;
135 return &serialTestInstance
;
138 void serialWrite(serialPort_t
*instance
, uint8_t ch
)
140 EXPECT_EQ(instance
, &serialTestInstance
);
141 EXPECT_LT(serialWriteStub
.pos
, sizeof(serialWriteStub
.buffer
));
142 serialWriteStub
.buffer
[serialWriteStub
.pos
++] = ch
;
143 //TODO serialReadStub.buffer[serialReadStub.end++] = ch; //characters echoes back on the shared wire
144 //printf("w: %02d 0x%02x\n", serialWriteStub.pos, ch);
148 void serialTestResetPort()
150 openSerial_called
= false;
151 stub_serialRxCallback
= NULL
;
152 portIsShared
= false;
153 serialExpectedMode
= MODE_RX
;
154 serialExpectedOptions
= SERIAL_UNIDIR
;
158 static bool isChecksumOkReturnValue
= true;
159 bool isChecksumOkIa6b(const uint8_t *ibusPacket
, const uint8_t length
)
163 return isChecksumOkReturnValue
;
167 static bool initSharedIbusTelemetryCalled
= false;
168 void initSharedIbusTelemetry(serialPort_t
* port
)
170 EXPECT_EQ(port
, &serialTestInstance
);
171 initSharedIbusTelemetryCalled
= true;
174 static bool stubTelemetryCalled
= false;
175 static uint8_t stubTelemetryPacket
[100];
176 static uint8_t stubTelemetryIgnoreRxChars
= 0;
178 uint8_t respondToIbusRequest(uint8_t const * const ibusPacket
) {
179 uint8_t len
= ibusPacket
[0];
180 EXPECT_LT(len
, sizeof(stubTelemetryPacket
));
181 memcpy(stubTelemetryPacket
, ibusPacket
, len
);
182 stubTelemetryCalled
= true;
183 return stubTelemetryIgnoreRxChars
;
186 void resetStubTelemetry(void)
188 memset(stubTelemetryPacket
, 0, sizeof(stubTelemetryPacket
));
189 stubTelemetryCalled
= false;
190 stubTelemetryIgnoreRxChars
= 0;
191 initSharedIbusTelemetryCalled
= false;
192 isChecksumOkReturnValue
= true;
196 class IbusRxInitUnitTest
: public ::testing::Test
201 serialTestResetPort();
206 TEST_F(IbusRxInitUnitTest
, Test_IbusRxNotEnabled
)
208 const rxConfig_t initialRxConfig
= {};
209 rxRuntimeState_t rxRuntimeState
= {};
210 findSerialPortConfig_stub_retval
= NULL
;
212 EXPECT_FALSE(ibusInit(&initialRxConfig
, &rxRuntimeState
));
214 //TODO: Question: I'd expect that runtime conf was not initialized unless there was a serial port to run but the implementation states otherwise
215 // EXPECT_EQ(0, rxRuntimeState.channelCount);
216 // EXPECT_EQ(0, rxRuntimeState.rxRefreshRate);
217 // EXPECT_EQ(NULL, rxRuntimeState.rcReadRawFn);
218 // EXPECT_EQ(NULL, rxRuntimeState.rcFrameStatusFn);
220 EXPECT_EQ(18, rxRuntimeState
.channelCount
);
221 EXPECT_EQ(20000, rxRuntimeState
.rxRefreshRate
);
222 EXPECT_FALSE(NULL
== rxRuntimeState
.rcReadRawFn
);
223 EXPECT_FALSE(NULL
== rxRuntimeState
.rcFrameStatusFn
);
227 TEST_F(IbusRxInitUnitTest
, Test_IbusRxEnabled
)
229 const rxConfig_t initialRxConfig
= {};
230 rxRuntimeState_t rxRuntimeState
= {};
231 findSerialPortConfig_stub_retval
= &serialTestInstanceConfig
;
233 EXPECT_TRUE(ibusInit(&initialRxConfig
, &rxRuntimeState
));
235 EXPECT_EQ(18, rxRuntimeState
.channelCount
);
236 EXPECT_EQ(20000, rxRuntimeState
.rxRefreshRate
);
237 EXPECT_FALSE(NULL
== rxRuntimeState
.rcReadRawFn
);
238 EXPECT_FALSE(NULL
== rxRuntimeState
.rcFrameStatusFn
);
240 EXPECT_TRUE(openSerial_called
);
245 class IbusRxProtocollUnitTest
: public ::testing::Test
248 rxRuntimeState_t rxRuntimeState
= {};
251 serialTestResetPort();
252 resetStubTelemetry();
254 serialExpectedOptions
= SERIAL_BIDIR
;
255 serialExpectedMode
= MODE_RXTX
;
257 const rxConfig_t initialRxConfig
= {};
258 findSerialPortConfig_stub_retval
= &serialTestInstanceConfig
;
260 EXPECT_TRUE(ibusInit(&initialRxConfig
, &rxRuntimeState
));
262 EXPECT_TRUE(initSharedIbusTelemetryCalled
);
264 //handle that internal ibus position is not set to zero at init
265 microseconds_stub_value
+= 5000;
266 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
269 virtual void receivePacket(uint8_t const * const packet
, const size_t length
)
271 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
272 for (size_t i
=0; i
< length
; i
++) {
273 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
274 stub_serialRxCallback(packet
[i
], NULL
);
280 TEST_F(IbusRxProtocollUnitTest
, Test_InitialFrameState
)
282 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
284 //TODO: is it ok to have undefined channel values after init?
288 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_OnePacketReceived
)
290 uint8_t packet
[] = {0x20, 0x00, //length and reserved (unknown) bits
291 0x00, 0xE0, 0x01, 0x00, 0x02, 0x00, 0x03, 0xF0, 0x04, 0x00, //channel 1..5 + 15 + 16
292 0x05, 0x00, 0x06, 0x00, 0x07, 0x10, 0x08, 0x00, 0x09, 0x10, //channel 6..10 + 17 + 18(lsb)
293 0x0a, 0x10, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14 + 18
294 0x84, 0xfd}; //checksum
296 for (size_t i
=0; i
< sizeof(packet
); i
++) {
297 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
298 stub_serialRxCallback(packet
[i
], NULL
);
301 //report frame complete once
302 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
303 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
305 //check that channel values have been updated
306 for (int i
=0; i
<18; i
++) {
307 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
312 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_OnePacketReceivedWithBadCrc
)
314 uint8_t packet
[] = {0x20, 0x00, //length and reserved (unknown) bits
315 0x00, 0x33, 0x01, 0x33, 0x02, 0x33, 0x03, 0x33, 0x04, 0x33, //channel 1..5
316 0x05, 0x33, 0x06, 0x33, 0x07, 0x33, 0x08, 0x33, 0x09, 0x33, //channel 6..10
317 0x0a, 0x33, 0x0b, 0x33, 0x0c, 0x33, 0x0d, 0x33, //channel 11..14
318 0x00, 0x00}; //checksum
320 isChecksumOkReturnValue
= false;
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
);
327 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
329 //check that channel values have not been updated
330 for (int i
=0; i
<14; i
++) {
331 EXPECT_NE(i
+ (0x33 << 8), rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
336 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_HalfPacketReceived_then_interPacketGapReset
)
338 const uint8_t packet_half
[] = {0x20, 0x00, //length and reserved (unknown) bits
339 0x00, 0xab, 0x01, 0xab, 0x02, 0xab, 0x03, 0xab, 0x04, 0xab, //channel 1..5
341 const uint8_t packet_full
[] = {0x20, 0x00, //length and reserved (unknown) bits
342 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
343 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
344 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
345 0x84, 0xff}; //checksum
347 for (size_t i
=0; i
< sizeof(packet_half
); i
++) {
348 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
349 stub_serialRxCallback(packet_half
[i
], NULL
);
352 microseconds_stub_value
+= 5000;
353 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
355 for (size_t i
=0; i
< sizeof(packet_full
); i
++) {
356 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
357 stub_serialRxCallback(packet_full
[i
], NULL
);
360 //report frame complete once
361 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
362 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
364 //check that channel values have been updated
365 for (int i
=0; i
<14; i
++) {
366 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
371 TEST_F(IbusRxProtocollUnitTest
, Test_IA6_OnePacketReceived
)
373 uint8_t packet
[] = {0x55, //sync character
374 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
375 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
376 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
377 0x5b, 0x00}; //checksum
379 for (size_t i
=0; i
< sizeof(packet
); i
++) {
380 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
381 stub_serialRxCallback(packet
[i
], NULL
);
384 //report frame complete once
385 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
386 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
388 //check that channel values have been updated
389 for (int i
=0; i
<14; i
++) {
390 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
395 TEST_F(IbusRxProtocollUnitTest
, Test_IA6_OnePacketReceivedBadCrc
)
397 uint8_t packet
[] = {0x55, //sync character
398 0x00, 0x33, 0x01, 0x33, 0x02, 0x33, 0x03, 0x33, 0x04, 0x33, //channel 1..5
399 0x05, 0x33, 0x06, 0x33, 0x07, 0x33, 0x08, 0x33, 0x09, 0x33, //channel 6..10
400 0x0a, 0x33, 0x0b, 0x33, 0x0c, 0x33, 0x0d, 0x33, //channel 11..14
401 0x00, 0x00}; //checksum
403 for (size_t i
=0; i
< sizeof(packet
); i
++) {
404 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
405 stub_serialRxCallback(packet
[i
], NULL
);
409 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
411 //check that channel values have not been updated
412 for (int i
=0; i
<14; i
++) {
413 EXPECT_NE(i
+ (0x33 << 8), rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
418 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_OnePacketReceived_not_shared_port
)
420 uint8_t packet
[] = {0x20, 0x00, //length and reserved (unknown) bits
421 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
422 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
423 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
424 0x84, 0xff}; //checksum
428 serialTestResetPort();
429 resetStubTelemetry();
430 portIsShared
= false;
431 serialExpectedOptions
= SERIAL_NOT_INVERTED
;
432 serialExpectedMode
= MODE_RX
;
434 const rxConfig_t initialRxConfig
= {};
435 findSerialPortConfig_stub_retval
= &serialTestInstanceConfig
;
437 EXPECT_TRUE(ibusInit(&initialRxConfig
, &rxRuntimeState
));
438 EXPECT_FALSE(initSharedIbusTelemetryCalled
);
440 //handle that internal ibus position is not set to zero at init
441 microseconds_stub_value
+= 5000;
442 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
445 for (size_t i
=0; i
< sizeof(packet
); i
++) {
446 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
447 stub_serialRxCallback(packet
[i
], NULL
);
450 //report frame complete once
451 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
452 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
454 //check that channel values have been updated
455 for (int i
=0; i
<14; i
++) {
456 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
461 TEST_F(IbusRxProtocollUnitTest
, Test_OneTelemetryPacketReceived
)
463 uint8_t packet
[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
464 resetStubTelemetry();
466 receivePacket(packet
, sizeof(packet
));
468 //no frame complete signal to rx system, but telemetry system is called
469 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
470 EXPECT_TRUE(stubTelemetryCalled
);
471 EXPECT_TRUE( 0 == memcmp( stubTelemetryPacket
, packet
, sizeof(packet
)));
475 TEST_F(IbusRxProtocollUnitTest
, Test_OneTelemetryIgnoreTxEchoToRx
)
477 uint8_t packet
[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
478 resetStubTelemetry();
479 stubTelemetryIgnoreRxChars
= 4;
481 //given one packet received, that will respond with four characters to be ignored
482 receivePacket(packet
, sizeof(packet
));
483 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
484 EXPECT_TRUE(stubTelemetryCalled
);
486 //when those four bytes are sent and looped back
487 resetStubTelemetry();
488 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
489 receivePacket(packet
, sizeof(packet
));
491 //then they are ignored
492 EXPECT_FALSE(stubTelemetryCalled
);
494 //and then next packet can be received
495 receivePacket(packet
, sizeof(packet
));
496 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
497 EXPECT_TRUE(stubTelemetryCalled
);
501 TEST_F(IbusRxProtocollUnitTest
, Test_OneTelemetryShouldNotIgnoreTxEchoAfterInterFrameGap
)
503 uint8_t packet
[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
504 resetStubTelemetry();
505 stubTelemetryIgnoreRxChars
= 4;
507 //given one packet received, that will respond with four characters to be ignored
508 receivePacket(packet
, sizeof(packet
));
509 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
510 EXPECT_TRUE(stubTelemetryCalled
);
512 //when there is an interPacketGap
513 microseconds_stub_value
+= 5000;
514 resetStubTelemetry();
515 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
517 //then next packet can be received
518 receivePacket(packet
, sizeof(packet
));
519 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
520 EXPECT_TRUE(stubTelemetryCalled
);