3 * Cleanflight is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * Cleanflight is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
23 #include "blackbox/blackbox.h"
24 #include "blackbox/blackbox_encoding.h"
25 #include "common/utils.h"
28 #include "pg/pg_ids.h"
30 #include "drivers/serial.h"
31 #include "io/serial.h"
34 #include "unittest_macros.h"
35 #include "gtest/gtest.h"
38 static serialPort_t
*blackboxPort
;
39 static int serialWritePos
= 0;
40 static int serialReadPos
= 0;
41 static int serialReadEnd
= 0;
42 #define SERIAL_BUFFER_SIZE 256
43 static uint8_t serialReadBuffer
[SERIAL_BUFFER_SIZE
];
44 static uint8_t serialWriteBuffer
[SERIAL_BUFFER_SIZE
];
46 serialPort_t serialTestInstance
;
48 void serialWrite(serialPort_t
*instance
, uint8_t ch
)
50 EXPECT_EQ(instance
, &serialTestInstance
);
51 EXPECT_LT(serialWritePos
, sizeof(serialWriteBuffer
));
52 serialWriteBuffer
[serialWritePos
++] = ch
;
55 void serialWriteBuf(serialPort_t
*instance
, const uint8_t *data
, int count
)
58 serialWrite(instance
, *data
++);
61 void serialBeginWrite(serialPort_t
*instance
)
63 EXPECT_EQ(instance
, &serialTestInstance
);
66 void serialEndWrite(serialPort_t
*instance
)
68 EXPECT_EQ(instance
, &serialTestInstance
);
71 uint32_t serialRxBytesWaiting(const serialPort_t
*instance
)
73 EXPECT_EQ(instance
, &serialTestInstance
);
74 EXPECT_GE(serialReadEnd
, serialReadPos
);
75 int ret
= serialReadEnd
- serialReadPos
;
76 if (ret
>= 0) return ret
;
80 uint8_t serialRead(serialPort_t
*instance
)
82 EXPECT_EQ(instance
, &serialTestInstance
);
83 EXPECT_LT(serialReadPos
, serialReadEnd
);
84 const uint8_t ch
= serialReadBuffer
[serialReadPos
++];
88 uint32_t serialTxBytesFree(const serialPort_t
*instance
)
91 return SERIAL_BUFFER_SIZE
- serialWritePos
;
94 bool isSerialTransmitBufferEmpty(const serialPort_t
*instance
)
96 EXPECT_EQ(instance
, &serialTestInstance
);
100 void serialTestResetBuffers()
102 blackboxPort
= &serialTestInstance
;
103 memset(&serialReadBuffer
, 0, sizeof(serialReadBuffer
));
106 memset(&serialWriteBuffer
, 0, sizeof(serialWriteBuffer
));
110 TEST(BlackboxEncodingTest
, TestWriteUnsignedVB
)
112 serialTestResetBuffers();
114 blackboxWriteUnsignedVB(0);
115 EXPECT_EQ(0, serialWriteBuffer
[0]);
116 blackboxWriteUnsignedVB(128);
117 EXPECT_EQ(0x80, serialWriteBuffer
[1]);
118 EXPECT_EQ(1, serialWriteBuffer
[2]);
121 TEST(BlackboxTest
, TestWriteTag2_3SVariable_BITS2
)
123 serialTestResetBuffers();
124 uint8_t *buf
= &serialWriteBuffer
[0];
128 // 2 bits per field ss11 2233,
132 selector
= blackboxWriteTag2_3SVariable(v
);
133 EXPECT_EQ(0, selector
);
134 EXPECT_EQ(0, buf
[0]);
135 EXPECT_EQ(0, buf
[1]); // ensure next byte has not been written
139 selector
= blackboxWriteTag2_3SVariable(v
);
140 EXPECT_EQ(0x10, buf
[0]); // 00010000
141 EXPECT_EQ(0, buf
[1]); // ensure next byte has not been written
147 selector
= blackboxWriteTag2_3SVariable(v
);
148 EXPECT_EQ(0, selector
);
149 EXPECT_EQ(0x15, buf
[0]); // 00010101
150 EXPECT_EQ(0, buf
[1]); // ensure next byte has not been written
156 selector
= blackboxWriteTag2_3SVariable(v
);
157 EXPECT_EQ(0, selector
);
158 EXPECT_EQ(0x3F, buf
[0]); // 00111111
159 EXPECT_EQ(0, buf
[1]); // ensure next byte has not been written
165 selector
= blackboxWriteTag2_3SVariable(v
);
166 EXPECT_EQ(0, selector
);
167 EXPECT_EQ(0x2A, buf
[0]); // 00101010
168 EXPECT_EQ(0, buf
[1]); // ensure next byte has not been written
172 TEST(BlackboxTest
, TestWriteTag2_3SVariable_BITS554
)
174 serialTestResetBuffers();
175 uint8_t *buf
= &serialWriteBuffer
[0];
179 // 554 bits per field ss11 1112 2222 3333
180 // 5 bits per field [-16, 15], 4 bits per field [-8, 7]
184 selector
= blackboxWriteTag2_3SVariable(v
);
185 EXPECT_EQ(1, selector
);
186 EXPECT_EQ(0x5E, buf
[0]); // 0101 1110
187 EXPECT_EQ(0xF7, buf
[1]); // 1111 0111
188 EXPECT_EQ(0, buf
[2]); // ensure next byte has not been written
194 selector
= blackboxWriteTag2_3SVariable(v
);
195 EXPECT_EQ(1, selector
);
196 EXPECT_EQ(0x61, buf
[0]); // 0110 0001
197 EXPECT_EQ(0x08, buf
[1]); // 0000 1000
198 EXPECT_EQ(0, buf
[2]); // ensure next byte has not been written
204 selector
= blackboxWriteTag2_3SVariable(v
);
205 EXPECT_EQ(1, selector
);
206 EXPECT_EQ(0x4E, buf
[0]); // 0100 1110
207 EXPECT_EQ(0x85, buf
[1]); // 1000 0101
208 EXPECT_EQ(0, buf
[2]); // ensure next byte has not been written
212 TEST(BlackboxTest
, TestWriteTag2_3SVariable_BITS887
)
214 serialTestResetBuffers();
215 uint8_t *buf
= &serialWriteBuffer
[0];
219 // 877 bits per field ss11 1111 1122 2222 2333 3333
220 // 8 bits per field [-128, 127], 7 bits per field [-64, 63]
224 selector
= blackboxWriteTag2_3SVariable(v
);
225 EXPECT_EQ(2, selector
);
226 EXPECT_EQ(0x9F, buf
[0]); // 1001 1111
227 EXPECT_EQ(0xDF, buf
[1]); // 1101 1111
228 EXPECT_EQ(0xBF, buf
[2]); // 1011 1111
229 EXPECT_EQ(0, buf
[3]); // ensure next byte has not been written
235 selector
= blackboxWriteTag2_3SVariable(v
);
236 EXPECT_EQ(2, selector
);
237 EXPECT_EQ(0xA0, buf
[0]); // 1010 0000
238 EXPECT_EQ(0x20, buf
[1]); // 0010 0000
239 EXPECT_EQ(0x40, buf
[2]); // 0100 0000
240 EXPECT_EQ(0, buf
[3]); // ensure next byte has not been written
245 PG_REGISTER(blackboxConfig_t
, blackboxConfig
, PG_BLACKBOX_CONFIG
, 0);
246 int32_t blackboxHeaderBudget
;
247 void mspSerialAllocatePorts(void) {}
248 void blackboxWrite(uint8_t value
) {serialWrite(blackboxPort
, value
);}
249 int blackboxWriteString(const char *s
)
251 const uint8_t *pos
= (uint8_t*)s
;
253 serialWrite(blackboxPort
, *pos
);
256 const int length
= pos
- (uint8_t*)s
;