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
)
180 uint8_t len
= ibusPacket
[0];
181 EXPECT_LT(len
, sizeof(stubTelemetryPacket
));
182 memcpy(stubTelemetryPacket
, ibusPacket
, len
);
183 stubTelemetryCalled
= true;
184 return stubTelemetryIgnoreRxChars
;
187 void resetStubTelemetry(void)
189 memset(stubTelemetryPacket
, 0, sizeof(stubTelemetryPacket
));
190 stubTelemetryCalled
= false;
191 stubTelemetryIgnoreRxChars
= 0;
192 initSharedIbusTelemetryCalled
= false;
193 isChecksumOkReturnValue
= true;
197 class IbusRxInitUnitTest
: public ::testing::Test
202 serialTestResetPort();
207 TEST_F(IbusRxInitUnitTest
, Test_IbusRxNotEnabled
)
209 const rxConfig_t initialRxConfig
= {};
210 rxRuntimeState_t rxRuntimeState
= {};
211 findSerialPortConfig_stub_retval
= NULL
;
213 EXPECT_FALSE(ibusInit(&initialRxConfig
, &rxRuntimeState
));
215 //TODO: Question: I'd expect that runtime conf was not initialized unless there was a serial port to run but the implementation states otherwise
216 // EXPECT_EQ(0, rxRuntimeState.channelCount);
217 // EXPECT_EQ(0, rxRuntimeState.rxRefreshRate);
218 // EXPECT_EQ(NULL, rxRuntimeState.rcReadRawFn);
219 // EXPECT_EQ(NULL, rxRuntimeState.rcFrameStatusFn);
221 EXPECT_EQ(18, rxRuntimeState
.channelCount
);
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_FALSE(NULL
== rxRuntimeState
.rcReadRawFn
);
237 EXPECT_FALSE(NULL
== rxRuntimeState
.rcFrameStatusFn
);
239 EXPECT_TRUE(openSerial_called
);
244 class IbusRxProtocollUnitTest
: public ::testing::Test
247 rxRuntimeState_t rxRuntimeState
= {};
250 serialTestResetPort();
251 resetStubTelemetry();
253 serialExpectedOptions
= SERIAL_BIDIR
;
254 serialExpectedMode
= MODE_RXTX
;
256 const rxConfig_t initialRxConfig
= {};
257 findSerialPortConfig_stub_retval
= &serialTestInstanceConfig
;
259 EXPECT_TRUE(ibusInit(&initialRxConfig
, &rxRuntimeState
));
261 EXPECT_TRUE(initSharedIbusTelemetryCalled
);
263 //handle that internal ibus position is not set to zero at init
264 microseconds_stub_value
+= 5000;
265 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
268 virtual void receivePacket(uint8_t const * const packet
, const size_t length
)
270 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
271 for (size_t i
=0; i
< length
; i
++) {
272 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
273 stub_serialRxCallback(packet
[i
], NULL
);
279 TEST_F(IbusRxProtocollUnitTest
, Test_InitialFrameState
)
281 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
283 //TODO: is it ok to have undefined channel values after init?
287 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_OnePacketReceived
)
289 uint8_t packet
[] = {0x20, 0x00, //length and reserved (unknown) bits
290 0x00, 0xE0, 0x01, 0x00, 0x02, 0x00, 0x03, 0xF0, 0x04, 0x00, //channel 1..5 + 15 + 16
291 0x05, 0x00, 0x06, 0x00, 0x07, 0x10, 0x08, 0x00, 0x09, 0x10, //channel 6..10 + 17 + 18(lsb)
292 0x0a, 0x10, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14 + 18
293 0x84, 0xfd}; //checksum
295 for (size_t i
=0; i
< sizeof(packet
); i
++) {
296 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
297 stub_serialRxCallback(packet
[i
], NULL
);
300 //report frame complete once
301 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
302 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
304 //check that channel values have been updated
305 for (int i
=0; i
<18; i
++) {
306 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
311 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_OnePacketReceivedWithBadCrc
)
313 uint8_t packet
[] = {0x20, 0x00, //length and reserved (unknown) bits
314 0x00, 0x33, 0x01, 0x33, 0x02, 0x33, 0x03, 0x33, 0x04, 0x33, //channel 1..5
315 0x05, 0x33, 0x06, 0x33, 0x07, 0x33, 0x08, 0x33, 0x09, 0x33, //channel 6..10
316 0x0a, 0x33, 0x0b, 0x33, 0x0c, 0x33, 0x0d, 0x33, //channel 11..14
317 0x00, 0x00}; //checksum
319 isChecksumOkReturnValue
= false;
320 for (size_t i
=0; i
< sizeof(packet
); i
++) {
321 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
322 stub_serialRxCallback(packet
[i
], NULL
);
326 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
328 //check that channel values have not been updated
329 for (int i
=0; i
<14; i
++) {
330 EXPECT_NE(i
+ (0x33 << 8), rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
335 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_HalfPacketReceived_then_interPacketGapReset
)
337 const uint8_t packet_half
[] = {0x20, 0x00, //length and reserved (unknown) bits
338 0x00, 0xab, 0x01, 0xab, 0x02, 0xab, 0x03, 0xab, 0x04, 0xab, //channel 1..5
340 const uint8_t packet_full
[] = {0x20, 0x00, //length and reserved (unknown) bits
341 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
342 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
343 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
344 0x84, 0xff}; //checksum
346 for (size_t i
=0; i
< sizeof(packet_half
); i
++) {
347 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
348 stub_serialRxCallback(packet_half
[i
], NULL
);
351 microseconds_stub_value
+= 5000;
352 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
354 for (size_t i
=0; i
< sizeof(packet_full
); i
++) {
355 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
356 stub_serialRxCallback(packet_full
[i
], NULL
);
359 //report frame complete once
360 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
361 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
363 //check that channel values have been updated
364 for (int i
=0; i
<14; i
++) {
365 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
370 TEST_F(IbusRxProtocollUnitTest
, Test_IA6_OnePacketReceived
)
372 uint8_t packet
[] = {0x55, //sync character
373 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
374 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
375 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
376 0x5b, 0x00}; //checksum
378 for (size_t i
=0; i
< sizeof(packet
); i
++) {
379 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
380 stub_serialRxCallback(packet
[i
], NULL
);
383 //report frame complete once
384 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
385 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
387 //check that channel values have been updated
388 for (int i
=0; i
<14; i
++) {
389 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
394 TEST_F(IbusRxProtocollUnitTest
, Test_IA6_OnePacketReceivedBadCrc
)
396 uint8_t packet
[] = {0x55, //sync character
397 0x00, 0x33, 0x01, 0x33, 0x02, 0x33, 0x03, 0x33, 0x04, 0x33, //channel 1..5
398 0x05, 0x33, 0x06, 0x33, 0x07, 0x33, 0x08, 0x33, 0x09, 0x33, //channel 6..10
399 0x0a, 0x33, 0x0b, 0x33, 0x0c, 0x33, 0x0d, 0x33, //channel 11..14
400 0x00, 0x00}; //checksum
402 for (size_t i
=0; i
< sizeof(packet
); i
++) {
403 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
404 stub_serialRxCallback(packet
[i
], NULL
);
408 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
410 //check that channel values have not been updated
411 for (int i
=0; i
<14; i
++) {
412 EXPECT_NE(i
+ (0x33 << 8), rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
417 TEST_F(IbusRxProtocollUnitTest
, Test_IA6B_OnePacketReceived_not_shared_port
)
419 uint8_t packet
[] = {0x20, 0x00, //length and reserved (unknown) bits
420 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, //channel 1..5
421 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, //channel 6..10
422 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, //channel 11..14
423 0x84, 0xff}; //checksum
427 serialTestResetPort();
428 resetStubTelemetry();
429 portIsShared
= false;
430 serialExpectedOptions
= SERIAL_NOT_INVERTED
;
431 serialExpectedMode
= MODE_RX
;
433 const rxConfig_t initialRxConfig
= {};
434 findSerialPortConfig_stub_retval
= &serialTestInstanceConfig
;
436 EXPECT_TRUE(ibusInit(&initialRxConfig
, &rxRuntimeState
));
437 EXPECT_FALSE(initSharedIbusTelemetryCalled
);
439 //handle that internal ibus position is not set to zero at init
440 microseconds_stub_value
+= 5000;
441 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
444 for (size_t i
=0; i
< sizeof(packet
); i
++) {
445 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
446 stub_serialRxCallback(packet
[i
], NULL
);
449 //report frame complete once
450 EXPECT_EQ(RX_FRAME_COMPLETE
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
451 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
453 //check that channel values have been updated
454 for (int i
=0; i
<14; i
++) {
455 EXPECT_EQ(i
, rxRuntimeState
.rcReadRawFn(&rxRuntimeState
, i
));
460 TEST_F(IbusRxProtocollUnitTest
, Test_OneTelemetryPacketReceived
)
462 uint8_t packet
[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
463 resetStubTelemetry();
465 receivePacket(packet
, sizeof(packet
));
467 //no frame complete signal to rx system, but telemetry system is called
468 EXPECT_EQ(RX_FRAME_PENDING
, rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
));
469 EXPECT_TRUE(stubTelemetryCalled
);
470 EXPECT_TRUE( 0 == memcmp( stubTelemetryPacket
, packet
, sizeof(packet
)));
474 TEST_F(IbusRxProtocollUnitTest
, Test_OneTelemetryIgnoreTxEchoToRx
)
476 uint8_t packet
[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
477 resetStubTelemetry();
478 stubTelemetryIgnoreRxChars
= 4;
480 //given one packet received, that will respond with four characters to be ignored
481 receivePacket(packet
, sizeof(packet
));
482 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
483 EXPECT_TRUE(stubTelemetryCalled
);
485 //when those four bytes are sent and looped back
486 resetStubTelemetry();
487 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
488 receivePacket(packet
, sizeof(packet
));
490 //then they are ignored
491 EXPECT_FALSE(stubTelemetryCalled
);
493 //and then next packet can be received
494 receivePacket(packet
, sizeof(packet
));
495 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
496 EXPECT_TRUE(stubTelemetryCalled
);
500 TEST_F(IbusRxProtocollUnitTest
, Test_OneTelemetryShouldNotIgnoreTxEchoAfterInterFrameGap
)
502 uint8_t packet
[] = {0x04, 0x81, 0x7a, 0xff}; //ibus sensor discovery
503 resetStubTelemetry();
504 stubTelemetryIgnoreRxChars
= 4;
506 //given one packet received, that will respond with four characters to be ignored
507 receivePacket(packet
, sizeof(packet
));
508 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
509 EXPECT_TRUE(stubTelemetryCalled
);
511 //when there is an interPacketGap
512 microseconds_stub_value
+= 5000;
513 resetStubTelemetry();
514 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
516 //then next packet can be received
517 receivePacket(packet
, sizeof(packet
));
518 rxRuntimeState
.rcFrameStatusFn(&rxRuntimeState
);
519 EXPECT_TRUE(stubTelemetryCalled
);