Merge pull request #1269 from pkendall64/crsf-max-output
[ExpressLRS.git] / src / test / crsf_native / test_crsf.cpp
blobfb7f8b0eb548ac3e4f1cd93c38858868d2674d18
1 #include <cstdint>
2 #include <unity.h>
3 #include <iostream>
4 #include "../msp_native/mock_serial.h"
6 #include "CRSF.h"
8 using namespace std;
9 // Mock out the serial port using a string stream
10 std::string buf;
11 StringStream ss(buf);
13 // Create a CRSF object to test,
14 // using the StringStream as a mock UART
15 CRSF crsf(&ss);
17 GENERIC_CRC8 test_crc(CRSF_CRC_POLY);
19 void test_device_info(void)
21 uint8_t deviceInformation[DEVICE_INFORMATION_LENGTH];
23 TEST_ASSERT_EQUAL(22, DEVICE_INFORMATION_PAYLOAD_LENGTH);
24 TEST_ASSERT_EQUAL(28, DEVICE_INFORMATION_LENGTH);
26 crsf.GetDeviceInformation(deviceInformation, 0);
27 crsf.SetExtendedHeaderAndCrc(deviceInformation, CRSF_FRAMETYPE_DEVICE_INFO, DEVICE_INFORMATION_FRAME_SIZE, CRSF_ADDRESS_CRSF_RECEIVER, CRSF_ADDRESS_FLIGHT_CONTROLLER);
29 crsf_ext_header_t *header = (crsf_ext_header_t *) deviceInformation;
31 TEST_ASSERT_EQUAL(26, header->frame_size);
32 TEST_ASSERT_EQUAL(CRSF_FRAMETYPE_DEVICE_INFO, header->type);
33 TEST_ASSERT_EQUAL(CRSF_ADDRESS_FLIGHT_CONTROLLER, header->dest_addr);
34 TEST_ASSERT_EQUAL(CRSF_ADDRESS_FLIGHT_CONTROLLER, header->device_addr);
35 TEST_ASSERT_EQUAL(CRSF_ADDRESS_CRSF_RECEIVER, header->orig_addr);
36 TEST_ASSERT_EQUAL(DEVICE_INFORMATION_FRAME_SIZE, header->frame_size);
38 uint8_t *data = deviceInformation + sizeof(crsf_ext_header_t);
39 uint8_t compare [] = {0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x0, 0x45, 0x4c, 0x52, 0x53, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
41 for(int i = 0; i < DEVICE_INFORMATION_PAYLOAD_LENGTH; i++)
43 TEST_ASSERT_EQUAL(compare[i], data[i]);
46 TEST_ASSERT_EQUAL(test_crc.calc(&deviceInformation[2], DEVICE_INFORMATION_LENGTH-3), deviceInformation[DEVICE_INFORMATION_LENGTH - 1]);
50 int main(int argc, char **argv)
52 UNITY_BEGIN();
53 RUN_TEST(test_device_info);
54 UNITY_END();
56 return 0;