makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / test / msp_native / encapsulated_msp_tests.cpp
bloba5a9a38861c8b7e9141244b08bc0c5e6ac711bbd
1 #include <unity.h>
2 #include "msp.h"
3 #include "msptypes.h"
4 #include "mock_serial.h"
6 #include "CRSF.h"
8 // Mock out the serial port using a string stream
9 std::string buf;
10 StringStream ss(buf);
12 // Create a CRSF object to test,
13 // using the StringStream as a mock UART
14 CRSF crsf(&ss);
16 void test_encapsulated_msp_send(void)
18 // TEST CASE:
19 // GIVEN the CRSF class has been instantiated using a mock UART
20 // WHEN the crsf.sendMSPFrameToFC() function is called with a valid mspPacket_t MSP command to change the VTX to F1
21 // THEN the mspPacket_t will be transcoded into an embedded crsf msp packet
22 // AND the transcoded packet will be sent to the Stream object associated with the CRSF class
24 // Make sure no msp messages are in the fifo
25 crsf.ResetMspQueue();
27 // Build an MSP packet with the MSP_SET_VTX_CONFIG cmd
28 mspPacket_t packet;
29 packet.reset();
30 packet.makeCommand();
31 packet.flags = 0;
32 packet.function = 0x59; // MSP_SET_VTX_CONFIG
33 packet.addByte(0x18); // change to band 4 channel 1
34 packet.addByte(0x00); // second byte of band/channel
35 packet.addByte(0x01); // change to power at index 1
36 packet.addByte(0x00); // don't enable pitmode
38 // Ask the CRSF class to send the encapsulated packet to the stream
39 crsf.AddMspMessage(&packet);
41 uint8_t* data;
42 uint8_t len;
43 crsf.GetMspMessage(&data, &len);
45 // Assert that the correct number of total bytes were sent to the stream
46 TEST_ASSERT_NOT_EQUAL(NULL, data);
47 TEST_ASSERT_EQUAL(14, len);
49 // Assert that each byte sent to the stream matches expected
50 TEST_ASSERT_EQUAL(CRSF_ADDRESS_BROADCAST, data[0]); // device_addr
51 TEST_ASSERT_EQUAL(12, data[1]); // frame_size
52 TEST_ASSERT_EQUAL(CRSF_FRAMETYPE_MSP_WRITE, data[2]); // type
53 TEST_ASSERT_EQUAL(CRSF_ADDRESS_FLIGHT_CONTROLLER, (uint8_t)data[3]); // dest_addr
54 TEST_ASSERT_EQUAL(CRSF_ADDRESS_RADIO_TRANSMITTER, (uint8_t)data[4]); // orig_addr
55 TEST_ASSERT_EQUAL(0x30, data[5]); // header
56 TEST_ASSERT_EQUAL(0x04, data[6]); // mspPayloadSize
57 TEST_ASSERT_EQUAL(packet.function, (uint8_t)data[7]); // packet->cmd
58 TEST_ASSERT_EQUAL(0x18, data[8]); // newFrequency b1
59 TEST_ASSERT_EQUAL(0x00, data[9]); // newFrequency b2
60 TEST_ASSERT_EQUAL(0x01, data[10]); // newFrequency b2
61 TEST_ASSERT_EQUAL(0x00, data[11]); // pitmode
62 TEST_ASSERT_EQUAL(0x44, data[12]); // msp crc
63 TEST_ASSERT_EQUAL(0x5E, data[13]); // crsf crc
66 void test_encapsulated_msp_send_too_long(void)
68 // TEST CASE:
69 // GIVEN the CRSF class has been instantiated using a mock UART
70 // WHEN the crsf.sendMSPFrameToFC() function is called with a invalid mspPacket_t MSP command (payload too long)
71 // THEN the mspPacket_t will NOT be transcoded into an embedded crsf msp packet
72 // AND nothing will be sent to the stream
74 // Make sure no msp messages are in the fifo
75 crsf.ResetMspQueue();
77 // Build an MSP packet with a payload that is too long to send (>4 bytes)
78 mspPacket_t packet;
79 packet.reset();
80 packet.makeCommand();
81 packet.flags = 0;
82 packet.function = 0x01;
83 packet.addByte(0x01);
84 packet.addByte(0x02);
85 packet.addByte(0x03);
86 packet.addByte(0x04);
87 packet.addByte(0x05);
89 // Ask the CRSF class to send the encapsulated packet to the stream
90 crsf.AddMspMessage(&packet);
92 uint8_t* data;
93 uint8_t len;
94 crsf.GetMspMessage(&data, &len);
96 // Assert that nothing was sent to the stream
97 TEST_ASSERT_EQUAL(NULL, data);
98 TEST_ASSERT_EQUAL(0, len);