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/>.
28 #include "build/debug.h"
29 #include "build/atomic.h"
31 #include "common/crc.h"
32 #include "common/utils.h"
33 #include "common/printf.h"
34 #include "common/gps_conversion.h"
35 #include "common/streambuf.h"
36 #include "common/typeconversion.h"
39 #include "pg/pg_ids.h"
42 #include "drivers/nvic.h"
43 #include "drivers/serial.h"
44 #include "drivers/system.h"
46 #include "fc/runtime_config.h"
47 #include "config/config.h"
48 #include "flight/imu.h"
50 #include "io/serial.h"
58 #include "sensors/battery.h"
59 #include "sensors/sensors.h"
61 #include "telemetry/telemetry.h"
62 #include "telemetry/msp_shared.h"
63 #include "telemetry/smartport.h"
64 #include "sensors/acceleration.h"
66 rssiSource_e rssiSource
;
67 bool handleMspFrame(uint8_t *frameStart
, uint8_t frameLength
, uint8_t *skipsBeforeResponse
);
68 bool sendMspReply(uint8_t payloadSize
, mspResponseFnPtr responseFn
);
69 uint8_t sbufReadU8(sbuf_t
*src
);
70 int sbufBytesRemaining(sbuf_t
*buf
);
72 uint16_t testBatteryVoltage
= 0;
74 int32_t testAmperage
= 0;
75 uint8_t mspTxData
[64]; // max frame size
77 uint8_t crsfFrameOut
[CRSF_FRAME_SIZE_MAX
];
78 uint8_t payloadOutput
[64];
79 sbuf_t payloadOutputBuf
;
80 int32_t testmAhDrawn
= 0;
82 PG_REGISTER(batteryConfig_t
, batteryConfig
, PG_BATTERY_CONFIG
, 0);
83 PG_REGISTER(telemetryConfig_t
, telemetryConfig
, PG_TELEMETRY_CONFIG
, 0);
84 PG_REGISTER(systemConfig_t
, systemConfig
, PG_SYSTEM_CONFIG
, 0);
85 PG_REGISTER(rxConfig_t
, rxConfig
, PG_RX_CONFIG
, 0);
86 PG_REGISTER(accelerometerConfig_t
, accelerometerConfig
, PG_ACCELEROMETER_CONFIG
,0);
88 extern bool crsfFrameDone
;
89 extern crsfFrame_t crsfFrame
;
90 extern uint8_t requestBuffer
[MSP_TLM_INBUF_SIZE
];
91 extern struct mspPacket_s requestPacket
;
92 extern struct mspPacket_s responsePacket
;
98 #include "unittest_macros.h"
99 #include "gtest/gtest.h"
101 typedef struct crsfMspFrame_s
{
102 uint8_t deviceAddress
;
107 uint8_t payload
[CRSF_FRAME_RX_MSP_FRAME_SIZE
+ CRSF_FRAME_LENGTH_CRC
];
110 const uint8_t crsfPidRequest
[] = {
111 0x00,0x0D,0x7A,0xC8,0xEA,0x30,0x00,0x70,0x70,0x00,0x00,0x00,0x00,0x69
114 TEST(CrossFireMSPTest
, RequestBufferTest
)
119 const crsfMspFrame_t
*framePtr
= (const crsfMspFrame_t
*)crsfPidRequest
;
120 crsfFrame
= *(const crsfFrame_t
*)framePtr
;
121 crsfFrameDone
= true;
122 EXPECT_EQ(CRSF_ADDRESS_BROADCAST
, crsfFrame
.frame
.deviceAddress
);
123 EXPECT_EQ(CRSF_FRAME_LENGTH_ADDRESS
+ CRSF_FRAME_LENGTH_EXT_TYPE_CRC
+ CRSF_FRAME_RX_MSP_FRAME_SIZE
, crsfFrame
.frame
.frameLength
);
124 EXPECT_EQ(CRSF_FRAMETYPE_MSP_REQ
, crsfFrame
.frame
.type
);
125 uint8_t *destination
= (uint8_t *)&crsfFrame
.frame
.payload
;
126 uint8_t *origin
= (uint8_t *)&crsfFrame
.frame
.payload
+ 1;
127 uint8_t *frameStart
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
128 uint8_t *frameEnd
= (uint8_t *)&crsfFrame
.frame
.payload
+ CRSF_FRAME_RX_MSP_FRAME_SIZE
+ 2;
129 EXPECT_EQ(0xC8, *destination
);
130 EXPECT_EQ(0xEA, *origin
);
131 EXPECT_EQ(0x30, *frameStart
);
132 EXPECT_EQ(0x69, *frameEnd
);
135 TEST(CrossFireMSPTest
, ResponsePacketTest
)
138 const crsfMspFrame_t
*framePtr
= (const crsfMspFrame_t
*)crsfPidRequest
;
139 crsfFrame
= *(const crsfFrame_t
*)framePtr
;
140 crsfFrameDone
= true;
141 uint8_t *frameStart
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
142 handleMspFrame(frameStart
, CRSF_FRAME_RX_MSP_FRAME_SIZE
, NULL
);
143 for (unsigned int ii
=1; ii
<30; ii
++) {
144 EXPECT_EQ(ii
, sbufReadU8(&responsePacket
.buf
));
149 // sync size type to from stts size fn 0 1 2 3 4
150 const uint8_t crsfPidWrite1
[] = {0x00,0x0C,0x7A,0xC8,0xEA,0x31,0x1E,0xCA,0x29,0x28,0x1E,0x3A,0x32};
151 const uint8_t crsfPidWrite2
[] = {0x00,0x0C,0x7A,0xC8,0xEA,0x22,0x23,0x46,0x2D,0x14,0x32,0x00,0x00};
152 const uint8_t crsfPidWrite3
[] = {0x00,0x0C,0x7A,0xC8,0xEA,0x23,0x0F,0x00,0x00,0x22,0x0E,0x35,0x19};
153 const uint8_t crsfPidWrite4
[] = {0x00,0x0C,0x7A,0xC8,0xEA,0x24,0x21,0x53,0x32,0x32,0x4B,0x28,0x00};
154 const uint8_t crsfPidWrite5
[] = {0x00,0x0C,0x7A,0xC8,0xEA,0x25,0x00,0x37,0x37,0x4B,0xF8,0x00,0x00};
156 TEST(CrossFireMSPTest
, WriteResponseTest
)
159 const crsfMspFrame_t
*framePtr1
= (const crsfMspFrame_t
*)crsfPidWrite1
;
160 crsfFrame
= *(const crsfFrame_t
*)framePtr1
;
161 crsfFrameDone
= true;
162 uint8_t *frameStart
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
163 bool pending1
= handleMspFrame(frameStart
, CRSF_FRAME_RX_MSP_FRAME_SIZE
, NULL
);
164 EXPECT_FALSE(pending1
); // not done yet
165 EXPECT_EQ(0x29, requestBuffer
[0]);
166 EXPECT_EQ(0x28, requestBuffer
[1]);
167 EXPECT_EQ(0x1E, requestBuffer
[2]);
168 EXPECT_EQ(0x3A, requestBuffer
[3]);
169 EXPECT_EQ(0x32, requestBuffer
[4]);
171 const crsfMspFrame_t
*framePtr2
= (const crsfMspFrame_t
*)crsfPidWrite2
;
172 crsfFrame
= *(const crsfFrame_t
*)framePtr2
;
173 crsfFrameDone
= true;
174 uint8_t *frameStart2
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
175 bool pending2
= handleMspFrame(frameStart2
, CRSF_FRAME_RX_MSP_FRAME_SIZE
, NULL
);
176 EXPECT_FALSE(pending2
); // not done yet
177 EXPECT_EQ(0x23, requestBuffer
[5]);
178 EXPECT_EQ(0x46, requestBuffer
[6]);
179 EXPECT_EQ(0x2D, requestBuffer
[7]);
180 EXPECT_EQ(0x14, requestBuffer
[8]);
181 EXPECT_EQ(0x32, requestBuffer
[9]);
182 EXPECT_EQ(0x00, requestBuffer
[10]);
183 EXPECT_EQ(0x00, requestBuffer
[11]);
185 const crsfMspFrame_t
*framePtr3
= (const crsfMspFrame_t
*)crsfPidWrite3
;
186 crsfFrame
= *(const crsfFrame_t
*)framePtr3
;
187 crsfFrameDone
= true;
188 uint8_t *frameStart3
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
189 bool pending3
= handleMspFrame(frameStart3
, CRSF_FRAME_RX_MSP_FRAME_SIZE
, NULL
);
190 EXPECT_FALSE(pending3
); // not done yet
191 EXPECT_EQ(0x0F, requestBuffer
[12]);
192 EXPECT_EQ(0x00, requestBuffer
[13]);
193 EXPECT_EQ(0x00, requestBuffer
[14]);
194 EXPECT_EQ(0x22, requestBuffer
[15]);
195 EXPECT_EQ(0x0E, requestBuffer
[16]);
196 EXPECT_EQ(0x35, requestBuffer
[17]);
197 EXPECT_EQ(0x19, requestBuffer
[18]);
199 const crsfMspFrame_t
*framePtr4
= (const crsfMspFrame_t
*)crsfPidWrite4
;
200 crsfFrame
= *(const crsfFrame_t
*)framePtr4
;
201 crsfFrameDone
= true;
202 uint8_t *frameStart4
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
203 bool pending4
= handleMspFrame(frameStart4
, CRSF_FRAME_RX_MSP_FRAME_SIZE
, NULL
);
204 EXPECT_FALSE(pending4
); // not done yet
205 EXPECT_EQ(0x21, requestBuffer
[19]);
206 EXPECT_EQ(0x53, requestBuffer
[20]);
207 EXPECT_EQ(0x32, requestBuffer
[21]);
208 EXPECT_EQ(0x32, requestBuffer
[22]);
209 EXPECT_EQ(0x4B, requestBuffer
[23]);
210 EXPECT_EQ(0x28, requestBuffer
[24]);
211 EXPECT_EQ(0x00, requestBuffer
[25]);
213 const crsfMspFrame_t
*framePtr5
= (const crsfMspFrame_t
*)crsfPidWrite5
;
214 crsfFrame
= *(const crsfFrame_t
*)framePtr5
;
215 crsfFrameDone
= true;
216 uint8_t *frameStart5
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
217 bool pending5
= handleMspFrame(frameStart5
, CRSF_FRAME_RX_MSP_FRAME_SIZE
, NULL
);
218 EXPECT_TRUE(pending5
); // not done yet
219 EXPECT_EQ(0x00, requestBuffer
[26]);
220 EXPECT_EQ(0x37, requestBuffer
[27]);
221 EXPECT_EQ(0x37, requestBuffer
[28]);
222 EXPECT_EQ(0x4B, requestBuffer
[29]);
226 void testSendMspResponse(uint8_t *payload
, const uint8_t ) {
227 sbuf_t
*plOut
= sbufInit(&payloadOutputBuf
, payloadOutput
, payloadOutput
+ 64);
228 sbufWriteData(plOut
, payload
, *payload
+ 64);
229 sbufSwitchToReader(&payloadOutputBuf
, payloadOutput
);
232 TEST(CrossFireMSPTest
, SendMspReply
) {
234 const crsfMspFrame_t
*framePtr
= (const crsfMspFrame_t
*)crsfPidRequest
;
235 crsfFrame
= *(const crsfFrame_t
*)framePtr
;
236 crsfFrameDone
= true;
237 uint8_t *frameStart
= (uint8_t *)&crsfFrame
.frame
.payload
+ 2;
238 bool handled
= handleMspFrame(frameStart
, CRSF_FRAME_RX_MSP_FRAME_SIZE
, NULL
);
239 EXPECT_TRUE(handled
);
240 bool replyPending
= sendMspReply(64, &testSendMspResponse
);
241 EXPECT_FALSE(replyPending
);
242 EXPECT_EQ(0x30, sbufReadU8(&payloadOutputBuf
)); // status (MSPv1 + #0)
243 EXPECT_EQ(0x1E, sbufReadU8(&payloadOutputBuf
)); // payload size
244 EXPECT_EQ(0x70, sbufReadU8(&payloadOutputBuf
)); // function ID
245 for (unsigned int ii
=1; ii
<=30; ii
++) {
246 EXPECT_EQ(ii
, sbufReadU8(&payloadOutputBuf
));
254 gpsSolutionData_t gpsSol
;
255 attitudeEulerAngles_t attitude
= { { 0, 0, 0 } };
256 uint8_t responseBuffer
[MSP_TLM_OUTBUF_SIZE
];
258 uint32_t micros(void) {return dummyTimeUs
;}
259 uint32_t microsISR(void) {return micros();}
260 serialPort_t
*openSerialPort(serialPortIdentifier_e
, serialPortFunction_e
, serialReceiveCallbackPtr
, void *, uint32_t, portMode_e
, portOptions_e
) {return NULL
;}
261 const serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e
) {return NULL
;}
262 bool isBatteryVoltageConfigured(void) { return true; }
263 uint16_t getBatteryVoltage(void) {
264 return testBatteryVoltage
;
266 uint16_t getLegacyBatteryVoltage(void) {
267 return (testBatteryVoltage
+ 5) / 10;
269 uint16_t getBatteryAverageCellVoltage(void) {
272 bool isAmperageConfigured(void) { return true; }
273 int32_t getAmperage(void) {
277 uint8_t calculateBatteryPercentageRemaining(void) {
281 int32_t getEstimatedAltitudeCm(void) {
285 bool featureIsEnabled(uint32_t) {return false;}
287 bool airmodeIsEnabled(void) {return true;}
289 mspDescriptor_t
mspDescriptorAlloc(void) {return 0;}
291 mspResult_e
mspFcProcessCommand(mspDescriptor_t srcDesc
, mspPacket_t
*cmd
, mspPacket_t
*reply
, mspPostProcessFnPtr
*mspPostProcessFn
) {
294 UNUSED(mspPostProcessFn
);
296 sbuf_t
*dst
= &reply
->buf
;
297 const uint8_t cmdMSP
= cmd
->cmd
;
298 reply
->cmd
= cmd
->cmd
;
300 if (cmdMSP
== 0x70) {
301 for (unsigned int ii
=1; ii
<=30; ii
++) {
302 sbufWriteU8(dst
, ii
);
304 } else if (cmdMSP
== 0xCA) {
305 return MSP_RESULT_ACK
;
308 return MSP_RESULT_ACK
;
311 void beeperConfirmationBeeps(uint8_t ) {}
313 int32_t getMAhDrawn(void) {
317 bool telemetryIsSensorEnabled(sensor_e
) {
321 timeUs_t
rxFrameTimeUs(void) { return 0; }