4 #include "mock_serial.h"
8 // Mock out the serial port using a string stream
12 // Create a CRSF object to test,
13 // using the StringStream as a mock UART
16 void test_encapsulated_msp_send(void)
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
27 // Build an MSP packet with the MSP_SET_VTX_CONFIG cmd
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
);
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)
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
77 // Build an MSP packet with a payload that is too long to send (>4 bytes)
82 packet
.function
= 0x01;
89 // Ask the CRSF class to send the encapsulated packet to the stream
90 crsf
.AddMspMessage(&packet
);
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
);