3 #include "mock_serial.h"
7 void test_msp_receive(void)
10 // GIVEN an instance of the MSP class has been instantiated
11 // WHEN a valid packet is send to processReceivedByte() one byte at a time
12 // THEN the complete packet will be available via getReceivedPacket()
13 // AND the packet values will match the values from the sent bytes
19 packetComplete
= MSPProtocol
.processReceivedByte(c
);
20 TEST_ASSERT_EQUAL(false, packetComplete
);
23 packetComplete
= MSPProtocol
.processReceivedByte(c
);
24 TEST_ASSERT_EQUAL(false, packetComplete
);
27 packetComplete
= MSPProtocol
.processReceivedByte(c
);
28 TEST_ASSERT_EQUAL(false, packetComplete
);
31 packetComplete
= MSPProtocol
.processReceivedByte(c
);
32 TEST_ASSERT_EQUAL(false, packetComplete
);
35 packetComplete
= MSPProtocol
.processReceivedByte(c
);
36 TEST_ASSERT_EQUAL(false, packetComplete
);
39 packetComplete
= MSPProtocol
.processReceivedByte(c
);
40 TEST_ASSERT_EQUAL(false, packetComplete
);
42 c
= 1; // payload size b1
43 packetComplete
= MSPProtocol
.processReceivedByte(c
);
44 TEST_ASSERT_EQUAL(false, packetComplete
);
46 c
= 0; // payload size b2
47 packetComplete
= MSPProtocol
.processReceivedByte(c
);
48 TEST_ASSERT_EQUAL(false, packetComplete
);
51 packetComplete
= MSPProtocol
.processReceivedByte(c
);
52 TEST_ASSERT_EQUAL(false, packetComplete
);
55 packetComplete
= MSPProtocol
.processReceivedByte(c
);
56 TEST_ASSERT_EQUAL(true, packetComplete
);
58 // Assert that values in packet match sent values
59 mspPacket_t
* packet
= MSPProtocol
.getReceivedPacket();
60 TEST_ASSERT_EQUAL(MSP_PACKET_COMMAND
, packet
->type
);
61 TEST_ASSERT_EQUAL(0, packet
->flags
);
62 TEST_ASSERT_EQUAL(1, packet
->function
);
63 TEST_ASSERT_EQUAL(1, packet
->payloadSize
);
64 TEST_ASSERT_EQUAL('A', (char)packet
->payload
[0]);
67 void test_msp_send(void)
70 // GIVEN an instance of the MSP class has been instantiated
71 // WHEN a valid packet is passed as an argument to sendPacket()
72 // THEN the complete packet will be transmitted to the passed in Stream object
73 // AND the return value will be true
75 // Build an MSP reply packet
78 packet
.makeResponse();
83 // Mock out the serial port using a string stream
87 // Ask the MSP class to send the packet to the stream
88 bool result
= MSPProtocol
.sendPacket(&packet
, &ss
);
89 TEST_ASSERT_EQUAL(true, result
); // success?
91 // Assert that each byte sent matches expected
92 TEST_ASSERT_EQUAL('$', buf
[0]);
93 TEST_ASSERT_EQUAL('X', buf
[1]);
94 TEST_ASSERT_EQUAL('>', buf
[2]);
95 TEST_ASSERT_EQUAL(0, buf
[3]); // flags
96 TEST_ASSERT_EQUAL(1, buf
[4]); // lower byte of function
97 TEST_ASSERT_EQUAL(0, buf
[5]); // upper byte of function
98 TEST_ASSERT_EQUAL(1, buf
[6]); // lower byte of payload size
99 TEST_ASSERT_EQUAL(0, buf
[7]); // upper byte of payload size
100 TEST_ASSERT_EQUAL(1, buf
[8]); // payload
101 TEST_ASSERT_EQUAL(224, (uint8_t)buf
[9]); // crc
104 extern void test_encapsulated_msp_send(void);
105 extern void test_encapsulated_msp_send_too_long(void);
107 int main(int argc
, char **argv
)
110 RUN_TEST(test_msp_receive
);
111 RUN_TEST(test_msp_send
);
113 RUN_TEST(test_encapsulated_msp_send
);
114 RUN_TEST(test_encapsulated_msp_send_too_long
);