Add missing wifi targets for some DIY modules (#1210)
[ExpressLRS.git] / src / test / telemetry_native / test_telemetry.cpp
blobeb6b7992111123828626c65ea661eeb0b3bc77c3
1 #include <cstdint>
2 #include <telemetry.h>
3 #include <unity.h>
5 Telemetry telemetry;
7 int sendData(uint8_t *data, int length)
9 for(int i = 0; i < length; i++)
11 if (!telemetry.RXhandleUARTin(data[i]))
13 return i;
17 return length;
20 int sendDataWithoutCheck(uint8_t *data, int length)
22 for(int i = 0; i < length; i++)
24 telemetry.RXhandleUARTin(data[i]);
27 return length;
30 void test_function_battery(void)
32 telemetry.ResetState();
33 uint8_t batterySequence[] = {0xEC,10,CRSF_FRAMETYPE_BATTERY_SENSOR,0,0,0,0,0,0,0,0,109};
34 uint8_t batterySequence2[] = {0xEC,10,CRSF_FRAMETYPE_BATTERY_SENSOR,1,0,0,0,0,0,0,0,46};
35 int length = sizeof(batterySequence);
36 int sentLength = sendData(batterySequence, length);
37 TEST_ASSERT_EQUAL(length, sentLength);
39 uint8_t* data;
40 uint8_t receivedLength;
41 telemetry.GetNextPayload(&receivedLength, &data);
42 TEST_ASSERT_NOT_EQUAL(0, data);
43 for (int i = 0; i < length; i++)
45 TEST_ASSERT_EQUAL(batterySequence[i], data[i]);
48 // simulate sending done + send another message of the same type to make sure that the repeated sending of only one type works
50 // this unlocks the data but does not send it again since it's not updated
51 TEST_ASSERT_EQUAL(false, telemetry.GetNextPayload(&receivedLength, &data));
53 // update data
54 sentLength = sendData(batterySequence2, length);
55 TEST_ASSERT_EQUAL(length, sentLength);
57 // now it's ready to be sent
58 telemetry.GetNextPayload(&receivedLength, &data);
59 TEST_ASSERT_NOT_EQUAL(0, data);
61 for (int i = 0; i < length; i++)
63 TEST_ASSERT_EQUAL(batterySequence2[i], data[i]);
67 void test_function_replace_old(void)
69 telemetry.ResetState();
70 uint8_t batterySequence[] = {0xEC,10,CRSF_FRAMETYPE_BATTERY_SENSOR,0,0,0,0,0,0,0,0,109};
71 uint8_t batterySequence2[] = {0xEC,10,CRSF_FRAMETYPE_BATTERY_SENSOR,1,0,0,0,0,0,0,0,46};
73 sendDataWithoutCheck(batterySequence, sizeof(batterySequence));
75 int length = sizeof(batterySequence2);
76 int sentLength = sendData(batterySequence2, length);
77 TEST_ASSERT_EQUAL(length, sentLength);
79 uint8_t* data;
80 uint8_t receivedLength;
81 telemetry.GetNextPayload(&receivedLength, &data);
82 TEST_ASSERT_NOT_EQUAL(0, data);
83 for (int i = 0; i < length; i++)
85 TEST_ASSERT_EQUAL(batterySequence2[i], data[i]);
89 void test_function_do_not_replace_old_locked(void)
91 telemetry.ResetState();
92 uint8_t batterySequence[] = {0xEC,10, CRSF_FRAMETYPE_BATTERY_SENSOR,0,0,0,0,0,0,0,0,109};
93 uint8_t batterySequence2[] = {0xEC,10, CRSF_FRAMETYPE_BATTERY_SENSOR,1,0,0,0,0,0,0,0,46};
95 sendDataWithoutCheck(batterySequence, sizeof(batterySequence));
96 uint8_t* data;
97 uint8_t receivedLength;
98 telemetry.GetNextPayload(&receivedLength, &data);
99 sendDataWithoutCheck(batterySequence2, sizeof(batterySequence2));
100 TEST_ASSERT_EQUAL(1, telemetry.UpdatedPayloadCount());
101 TEST_ASSERT_NOT_EQUAL(0, data);
102 for (int i = 0; i < sizeof(batterySequence); i++)
104 TEST_ASSERT_EQUAL(batterySequence[i], data[i]);
108 void test_function_add_type(void)
110 telemetry.ResetState();
111 uint8_t batterySequence[] = {0xEC,10, CRSF_FRAMETYPE_BATTERY_SENSOR,0,0,0,0,0,0,0,0,109};
112 uint8_t attitudeSequence[] = {0xEC,8, CRSF_FRAMETYPE_ATTITUDE,0,0,0,0,0,0,48};
114 sendData(batterySequence, sizeof(batterySequence));
116 int length = sizeof(attitudeSequence);
117 int sentLength = sendData(attitudeSequence, length);
118 TEST_ASSERT_EQUAL(length, sentLength);
120 TEST_ASSERT_EQUAL(2, telemetry.UpdatedPayloadCount());
122 uint8_t* data;
123 uint8_t receivedLength;
124 telemetry.GetNextPayload(&receivedLength, &data);
126 telemetry.GetNextPayload(&receivedLength, &data);
127 TEST_ASSERT_NOT_EQUAL(0, data);
128 for (int i = 0; i < length; i++)
130 TEST_ASSERT_EQUAL(attitudeSequence[i], data[i]);
134 void test_function_recover_from_junk(void)
136 telemetry.ResetState();
137 uint8_t bootloaderSequence[] = {
138 0XEC,0xFF,100,10,10,
139 0XEC,4,100,10,10,4,
140 0xEC,0x04,CRSF_FRAMETYPE_COMMAND,0x62,0x6c,0x0A};
141 int length = sizeof(bootloaderSequence);
142 int sentLength = sendDataWithoutCheck(bootloaderSequence, length);
143 TEST_ASSERT_EQUAL(true, telemetry.ShouldCallBootloader());
146 void test_function_bootloader_called(void)
148 telemetry.ResetState();
149 uint8_t bootloaderSequence[] = {0xEC,0x04,CRSF_FRAMETYPE_COMMAND,0x62,0x6c,0x0A};
150 int length = sizeof(bootloaderSequence);
151 int sentLength = sendData(bootloaderSequence, length);
152 TEST_ASSERT_EQUAL(length, sentLength);
153 TEST_ASSERT_EQUAL(true, telemetry.ShouldCallBootloader());
156 void test_function_store_unknown_type(void)
158 telemetry.ResetState();
159 uint8_t unknownSequence[] = {0xEC,0x04,CRSF_FRAMETYPE_PARAMETER_READ,0x62,0x6c,85};
160 int length = sizeof(unknownSequence);
161 int sentLength = sendData(unknownSequence, length);
162 TEST_ASSERT_EQUAL(length, sentLength);
163 TEST_ASSERT_EQUAL(1, telemetry.UpdatedPayloadCount());
166 void test_function_store_unknown_type_two_slots(void)
168 telemetry.ResetState();
169 uint8_t unknownSequence[] = {0xEC,0x04,CRSF_FRAMETYPE_PARAMETER_READ,0x62,0x6c,85};
170 int length = sizeof(unknownSequence);
171 int sentLength = sendData(unknownSequence, length);
172 TEST_ASSERT_EQUAL(length, sentLength);
174 uint8_t* data;
175 uint8_t receivedLength;
176 telemetry.GetNextPayload(&receivedLength, &data);
178 sentLength = sendData(unknownSequence, length);
179 TEST_ASSERT_EQUAL(length, sentLength);
181 TEST_ASSERT_EQUAL(1, telemetry.UpdatedPayloadCount());
184 void test_function_store_ardupilot_status_text(void)
186 telemetry.ResetState();
187 uint8_t statusSequence[] = {0xEC,0x04,CRSF_FRAMETYPE_ARDUPILOT_RESP,CRSF_AP_CUSTOM_TELEM_STATUS_TEXT,0x6c,60};
188 uint8_t otherSequence[] = {0xEC,0x04,CRSF_FRAMETYPE_ARDUPILOT_RESP,CRSF_AP_CUSTOM_TELEM_SINGLE_PACKET_PASSTHROUGH,0x6c,55};
190 int length = sizeof(otherSequence);
191 int sentLength = sendData(otherSequence, length);
192 TEST_ASSERT_EQUAL(length, sentLength);
194 length = sizeof(statusSequence);
195 sentLength = sendData(statusSequence, length);
196 TEST_ASSERT_EQUAL(length, sentLength);
198 TEST_ASSERT_EQUAL(2, telemetry.UpdatedPayloadCount());
201 void test_function_uart_in(void)
203 telemetry.ResetState();
204 TEST_ASSERT_EQUAL(true, telemetry.RXhandleUARTin(0xEC));
208 int main(int argc, char **argv)
210 UNITY_BEGIN();
211 RUN_TEST(test_function_uart_in);
212 RUN_TEST(test_function_bootloader_called);
213 RUN_TEST(test_function_battery);
214 RUN_TEST(test_function_replace_old);
215 RUN_TEST(test_function_do_not_replace_old_locked);
216 RUN_TEST(test_function_add_type);
217 RUN_TEST(test_function_recover_from_junk);
218 RUN_TEST(test_function_store_unknown_type);
219 RUN_TEST(test_function_store_unknown_type_two_slots);
220 RUN_TEST(test_function_store_ardupilot_status_text);
221 UNITY_END();
223 return 0;