Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / src / test / unit / blackbox_encoding_unittest.cc
blob3167e43088da70bfe613550f72f6395e3ce58669
1 /*
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/>.
17 #include <stdint.h>
18 #include <string.h>
20 extern "C" {
21 #include "platform.h"
23 #include "blackbox/blackbox.h"
24 #include "blackbox/blackbox_encoding.h"
25 #include "common/utils.h"
27 #include "pg/pg.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)
57 while(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;
77 return 0;
80 uint8_t serialRead(serialPort_t *instance)
82 EXPECT_EQ(instance, &serialTestInstance);
83 EXPECT_LT(serialReadPos, serialReadEnd);
84 const uint8_t ch = serialReadBuffer[serialReadPos++];
85 return ch;
88 uint32_t serialTxBytesFree(const serialPort_t *instance)
90 UNUSED(instance);
91 return SERIAL_BUFFER_SIZE - serialWritePos;
94 bool isSerialTransmitBufferEmpty(const serialPort_t *instance)
96 EXPECT_EQ(instance, &serialTestInstance);
97 return true;
100 void serialTestResetBuffers()
102 blackboxPort = &serialTestInstance;
103 memset(&serialReadBuffer, 0, sizeof(serialReadBuffer));
104 serialReadPos = 0;
105 serialReadEnd = 0;
106 memset(&serialWriteBuffer, 0, sizeof(serialWriteBuffer));
107 serialWritePos = 0;
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];
125 int selector;
126 int32_t v[3];
128 // 2 bits per field ss11 2233,
129 v[0] = 0;
130 v[1] = 0;
131 v[2] = 0;
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
136 ++buf;
138 v[0] = 1;
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
142 ++buf;
144 v[0] = 1;
145 v[1] = 1;
146 v[2] = 1;
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
151 ++buf;
153 v[0] = -1;
154 v[1] = -1;
155 v[2] = -1;
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
160 ++buf;
162 v[0] = -2;
163 v[1] = -2;
164 v[2] = -2;
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
169 ++buf;
172 TEST(BlackboxTest, TestWriteTag2_3SVariable_BITS554)
174 serialTestResetBuffers();
175 uint8_t *buf = &serialWriteBuffer[0];
176 int selector;
177 int32_t v[3];
179 // 554 bits per field ss11 1112 2222 3333
180 // 5 bits per field [-16, 15], 4 bits per field [-8, 7]
181 v[0] = 15;
182 v[1] = 15;
183 v[2] = 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
189 buf += 2;
191 v[0] = -16;
192 v[1] = -16;
193 v[2] = -8;
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
199 buf += 2;
201 v[0] = 7;
202 v[1] = 8;
203 v[2] = 5;
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
209 buf += 2;
212 TEST(BlackboxTest, TestWriteTag2_3SVariable_BITS887)
214 serialTestResetBuffers();
215 uint8_t *buf = &serialWriteBuffer[0];
216 int selector;
217 int32_t v[3];
219 // 877 bits per field ss11 1111 1122 2222 2333 3333
220 // 8 bits per field [-128, 127], 7 bits per field [-64, 63]
221 v[0] = 127;
222 v[1] = 63;
223 v[2] = 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
230 buf += 3;
232 v[0] = -128;
233 v[1] = -64;
234 v[2] = -64;
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
241 buf += 3;
243 // STUBS
244 extern "C" {
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;
252 while (*pos) {
253 serialWrite(blackboxPort, *pos);
254 pos++;
256 const int length = pos - (uint8_t*)s;
257 return length;