Add missing wifi targets for some DIY modules (#1210)
[ExpressLRS.git] / src / test / stubborn_native / test_stubborn.cpp
blob71847f7808edd6a4cdedf662590bdd2410dbe74d
1 #include <cstdint>
2 #include <telemetry_protocol.h>
3 #include <stubborn_sender.h>
4 #include <stubborn_receiver.h>
5 #include <unity.h>
6 #include <iostream>
7 #include <bitset>
8 #include "helpers.h"
10 StubbornSender sender(ELRS_TELEMETRY_MAX_PACKAGES);
11 StubbornReceiver receiver(ELRS_TELEMETRY_MAX_PACKAGES);
13 void test_stubborn_link_sends_data(void)
15 uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
16 sender.ResetState();
17 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 1);
18 uint8_t *data;
19 uint8_t maxLength;
20 uint8_t packageIndex;
21 bool confirmValue = true;
23 for(int i = 0; i < sizeof(batterySequence); i++)
25 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
26 TEST_ASSERT_EQUAL(1, maxLength);
27 TEST_ASSERT_EQUAL(i + 1, packageIndex);
28 TEST_ASSERT_EQUAL(batterySequence[i], *data);
29 sender.ConfirmCurrentPayload(confirmValue);
30 confirmValue = !confirmValue;
33 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
34 TEST_ASSERT_EQUAL(0, packageIndex);
35 TEST_ASSERT_EQUAL(true, sender.IsActive());
36 sender.ConfirmCurrentPayload(!confirmValue);
37 TEST_ASSERT_EQUAL(true, sender.IsActive());
38 sender.ConfirmCurrentPayload(confirmValue);
39 TEST_ASSERT_EQUAL(false, sender.IsActive());
42 void test_stubborn_link_sends_data_even_bytes_per_call(void)
44 uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
45 sender.ResetState();
46 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 2);
47 uint8_t *data;
48 uint8_t maxLength;
49 uint8_t packageIndex;
50 bool confirmValue = true;
52 for(int i = 0; i < sizeof(batterySequence) / 2; i++)
54 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
55 TEST_ASSERT_EQUAL(2, maxLength);
56 TEST_ASSERT_EQUAL(batterySequence[i*2], data[0]);
57 TEST_ASSERT_EQUAL(batterySequence[i*2+1], data[1]);
58 sender.ConfirmCurrentPayload(confirmValue);
59 confirmValue = !confirmValue;
61 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
62 TEST_ASSERT_EQUAL(0, maxLength);
65 void test_stubborn_link_sends_data_odd_bytes_per_call(void)
67 uint8_t batterySequence[] = {0xEC,11, 0x08,0,0,0,0,0,0,0,109};
68 sender.ResetState();
69 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 3);
70 uint8_t *data;
71 uint8_t maxLength;
72 uint8_t packageIndex;
73 bool confirmValue = true;
75 for(int i = 0; i < sizeof(batterySequence) / 3; i++)
77 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
78 TEST_ASSERT_EQUAL(3, maxLength);
79 TEST_ASSERT_EQUAL(batterySequence[i*3], data[0]);
80 TEST_ASSERT_EQUAL(batterySequence[i*3+1], data[1]);
81 TEST_ASSERT_EQUAL(batterySequence[i*3+2], data[2]);
82 sender.ConfirmCurrentPayload(confirmValue);
83 confirmValue = !confirmValue;
85 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
86 TEST_ASSERT_EQUAL(2, maxLength);
87 TEST_ASSERT_EQUAL(batterySequence[9], data[0]);
88 TEST_ASSERT_EQUAL(batterySequence[10], data[1]);
90 sender.ConfirmCurrentPayload(confirmValue);
91 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
92 TEST_ASSERT_EQUAL(0, maxLength);
95 void test_stubborn_link_sends_data_larger_frame_size(void)
97 uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
98 sender.ResetState();
99 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 13);
100 uint8_t *data;
101 uint8_t maxLength;
102 uint8_t packageIndex;
103 bool confirmValue = true;
105 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
106 TEST_ASSERT_EQUAL(12, maxLength);
107 for(int i = 0; i < sizeof(batterySequence); i++)
109 TEST_ASSERT_EQUAL(batterySequence[i], data[i]);
112 sender.ConfirmCurrentPayload(confirmValue);
113 confirmValue = !confirmValue;
114 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
115 TEST_ASSERT_EQUAL(0, maxLength);
118 void test_stubborn_link_receives_data(void)
120 volatile uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
121 uint8_t buffer[100];
122 receiver.ResetState();
123 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
125 for(int i = 0; i < sizeof(batterySequence); i++)
127 receiver.ReceiveData(i+1, batterySequence + i);
129 receiver.ReceiveData(0, 0);
131 for(int i = 0; i < sizeof(batterySequence); i++)
133 TEST_ASSERT_EQUAL(batterySequence[i], buffer[i]);
136 receiver.Unlock();
139 void test_stubborn_link_receives_data_with_multiple_bytes(void)
141 volatile uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109,0,0,0};
142 uint8_t buffer[100];
143 receiver.ResetState();
144 receiver.SetDataToReceive(sizeof(buffer), buffer, 5);
146 for(int i = 0; i < sizeof(batterySequence) / 3; i++)
148 receiver.ReceiveData(i+1, batterySequence + (i * 5));
150 receiver.ReceiveData(0, 0);
152 for(int i = 0; i < sizeof(batterySequence); i++)
154 TEST_ASSERT_EQUAL(batterySequence[i], buffer[i]);
157 receiver.Unlock();
160 void test_stubborn_link_resyncs(void)
162 uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
163 uint8_t buffer[100];
164 uint8_t *data;
165 uint8_t maxLength;
166 uint8_t packageIndex;
168 receiver.ResetState();
169 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
171 sender.ResetState();
172 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 1);
174 // send and confirm two packages
175 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
176 receiver.ReceiveData(packageIndex, data);
177 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
179 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
180 receiver.ReceiveData(packageIndex, data);
181 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
183 // now reset receiver so the receiver sends the wrong confirm value
184 // all communication would be stuck
185 receiver.ResetState();
186 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
188 // wait for resync to happen
189 for(int i = 0; i < sender.GetMaxPacketsBeforeResync() + 1; i++)
191 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
192 TEST_ASSERT_EQUAL(3, packageIndex);
193 receiver.ReceiveData(packageIndex, data);
194 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
197 // resync active
198 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
199 TEST_ASSERT_EQUAL(ELRS_TELEMETRY_MAX_PACKAGES, packageIndex);
200 receiver.ReceiveData(packageIndex, data);
201 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
202 TEST_ASSERT_EQUAL(false, sender.IsActive());
204 // both are in sync again
205 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 1);
206 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
207 receiver.ReceiveData(packageIndex, data);
208 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
210 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
211 TEST_ASSERT_EQUAL(2, packageIndex);
213 receiver.Unlock();
216 void test_stubborn_link_resyncs_during_last_confirm(void)
218 uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
219 uint8_t buffer[100];
220 uint8_t *data;
221 uint8_t maxLength;
222 uint8_t packageIndex;
224 receiver.ResetState();
225 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
227 sender.ResetState();
228 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 1);
230 // send and confirm twelfe packages
231 for (int i = 0; i < sizeof(batterySequence); i++)
233 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
234 receiver.ReceiveData(packageIndex, data);
235 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
238 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
239 TEST_ASSERT_EQUAL(0, packageIndex);
241 // now reset receiver so the receiver sends the wrong confirm value
242 // all communication would be stuck since sender waits for final confirmation
243 receiver.ResetState();
244 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
246 // wait for resync to happen
247 for(int i = 0; i < sender.GetMaxPacketsBeforeResync() + 1; i++)
249 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
250 TEST_ASSERT_EQUAL(0, packageIndex);
251 receiver.ReceiveData(packageIndex, data);
252 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
255 // resync active
256 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
257 TEST_ASSERT_EQUAL(ELRS_TELEMETRY_MAX_PACKAGES, packageIndex);
258 receiver.ReceiveData(packageIndex, data);
259 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
260 TEST_ASSERT_EQUAL(false, sender.IsActive());
262 // both are in sync again
263 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 1);
264 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
265 receiver.ReceiveData(packageIndex, data);
266 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
268 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
269 TEST_ASSERT_EQUAL(2, packageIndex);
271 receiver.Unlock();
274 void test_stubborn_link_sends_data_until_confirmation(void)
276 uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
277 uint8_t *data;
278 uint8_t maxLength;
279 uint8_t packageIndex;
280 uint8_t buffer[100];
282 sender.ResetState();
283 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 1);
285 receiver.ResetState();
286 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
288 for(int i = 0; i < sender.GetMaxPacketsBeforeResync(); i++)
290 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
291 TEST_ASSERT_EQUAL(1, packageIndex);
292 receiver.ReceiveData(packageIndex, data);
293 sender.ConfirmCurrentPayload(!receiver.GetCurrentConfirm());
296 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
297 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
298 TEST_ASSERT_EQUAL(2, packageIndex);
301 void test_stubborn_link_multiple_packages(void)
303 uint8_t batterySequence[] = {0xEC,10, 0x08,0,0,0,0,0,0,0,0,109};
304 uint8_t *data;
305 uint8_t maxLength;
306 uint8_t packageIndex;
307 uint8_t buffer[100];
309 receiver.ResetState();
310 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
311 sender.ResetState();
313 for (int i = 0; i < 3; i++)
315 sender.SetDataToTransmit(sizeof(batterySequence), batterySequence, 1);
316 TEST_ASSERT_EQUAL(true, sender.IsActive());
317 for(int currentByte = 0; currentByte <= sizeof(batterySequence); currentByte++)
319 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
320 receiver.ReceiveData(packageIndex, data);
321 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
324 TEST_ASSERT_EQUAL(false, sender.IsActive());
325 TEST_ASSERT_EQUAL(true, receiver.HasFinishedData());
326 receiver.Unlock();
327 TEST_ASSERT_EQUAL(false, receiver.HasFinishedData());
332 static void test_stubborn_link_resync_then_send(void)
334 uint8_t testSequence1[] = {1,2,3,4,5,6,7,8,9,10};
335 uint8_t testSequence2[] = {11,12,13,14,15,16,17,18,19,20};
336 uint8_t buffer[100];
337 uint8_t *data;
338 uint8_t maxLength;
339 uint8_t packageIndex;
341 receiver.ResetState();
342 receiver.SetDataToReceive(sizeof(buffer), buffer, 1);
344 sender.ResetState();
345 sender.SetDataToTransmit(sizeof(testSequence1), testSequence1, 1);
347 // send and confirm two packages
348 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
349 receiver.ReceiveData(packageIndex, data);
350 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
352 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
353 receiver.ReceiveData(packageIndex, data);
354 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
356 // Abort the transfer by changing the payload
357 sender.SetDataToTransmit(sizeof(testSequence2), testSequence2, 1);
359 // Send next packet, which should be a RESYNC
360 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
361 TEST_ASSERT_EQUAL(ELRS_TELEMETRY_MAX_PACKAGES, packageIndex);
362 receiver.ReceiveData(packageIndex, data);
363 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
365 // Complete the transfer
366 int maxSends = ELRS_TELEMETRY_MAX_PACKAGES + 1;
367 while (!receiver.HasFinishedData() && maxSends)
369 sender.GetCurrentPayload(&packageIndex, &maxLength, &data);
370 receiver.ReceiveData(packageIndex, data);
371 sender.ConfirmCurrentPayload(receiver.GetCurrentConfirm());
374 // Should not have exhausted all the sends to get the package to go
375 TEST_ASSERT_NOT_EQUAL(0, maxSends);
376 // Make sure the second package was received, not the first
377 TEST_ASSERT_EQUAL_UINT8_ARRAY(testSequence2, buffer, ARRAY_SIZE(testSequence2));
380 int main(int argc, char **argv)
382 UNITY_BEGIN();
383 RUN_TEST(test_stubborn_link_sends_data);
384 RUN_TEST(test_stubborn_link_sends_data_even_bytes_per_call);
385 RUN_TEST(test_stubborn_link_sends_data_odd_bytes_per_call);
386 RUN_TEST(test_stubborn_link_sends_data_larger_frame_size);
387 RUN_TEST(test_stubborn_link_receives_data);
388 RUN_TEST(test_stubborn_link_receives_data_with_multiple_bytes);
389 RUN_TEST(test_stubborn_link_resyncs);
390 RUN_TEST(test_stubborn_link_sends_data_until_confirmation);
391 RUN_TEST(test_stubborn_link_multiple_packages);
392 RUN_TEST(test_stubborn_link_resyncs_during_last_confirm);
393 RUN_TEST(test_stubborn_link_resync_then_send);
394 UNITY_END();
396 return 0;