Skip to content

Commit 972ce5c

Browse files
committed
speed up forwarding of packets
1 parent d641f78 commit 972ce5c

File tree

4 files changed

+35
-77
lines changed

4 files changed

+35
-77
lines changed

code/controller/src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ void loop()
112112
static unsigned long lastSend = 0;
113113
if (millis() - lastSend > 2000)
114114
{
115+
// data
115116
std::vector<uint8_t> data = {
116117
static_cast<uint8_t>(random(65)),
117118
static_cast<uint8_t>(random(65)),
118119
static_cast<uint8_t>(random(65)),
119120
static_cast<uint8_t>(random(65)),
120121
static_cast<uint8_t>(random(65))};
121122

122-
std::vector<uint8_t> packet = createPacket(data);
123+
auto packet = createPacket(data);
123124

124125
// print the packet
125126
Serial.print("Packet: ");

code/module/src/main.cpp

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ HardwareSerial mySerial(2);
1010

1111
// Buffer for serial data
1212
std::vector<uint8_t> serialBuffer;
13+
bool readingPacket = false;
14+
15+
void handleMyByte(uint8_t byte)
16+
{
17+
Serial.println("Received byte: " + String(byte));
18+
// TODO: set the flap number
19+
}
1320

1421
void setup()
1522
{
@@ -24,44 +31,24 @@ void loop()
2431
// Check if data is available to read
2532
if (mySerial.available())
2633
{
27-
// Resize vector to available bytes
28-
serialBuffer.resize(mySerial.available());
29-
int bytesRead = mySerial.readBytes(serialBuffer.data(), serialBuffer.size());
30-
Serial.print("Received ");
31-
Serial.print(bytesRead);
32-
Serial.println(" bytes");
34+
uint8_t incomming = mySerial.read();
3335

34-
// print as hex
35-
Serial.print("Data: ");
36-
for (int i = 0; i < bytesRead; i++)
36+
if (!readingPacket && incomming != END_BYTE)
3737
{
38-
Serial.print("0x");
39-
Serial.print(serialBuffer[i], HEX);
40-
Serial.print(" ");
38+
readingPacket = true;
39+
handleMyByte(incomming);
4140
}
42-
Serial.println();
43-
44-
// validate the packet
45-
if (!validatePacket(serialBuffer))
41+
else
4642
{
47-
Serial.println("Invalid packet");
48-
return;
43+
// forward the data
44+
Serial.print(String(incomming) + " ");
45+
mySerial.write(incomming);
4946
}
5047

51-
// get first byte
52-
uint8_t command = serialBuffer[1];
53-
Serial.print("Command: ");
54-
Serial.println(command);
55-
56-
// create packet to forward from remaining bytes
57-
auto forwardPacket = createForwardPacket(serialBuffer);
58-
if (forwardPacket.size() <= 3)
48+
if (incomming == END_BYTE)
5949
{
60-
Serial.println("Forward packet is too small");
61-
return;
50+
Serial.println("End of packet");
51+
readingPacket = false;
6252
}
63-
64-
// forward the data
65-
mySerial.write(forwardPacket.data(), forwardPacket.size());
6653
}
6754
}

code/shared/include/utils.h

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
#include <vector>
44

5-
#define START_BYTE 0xAA // 10101010 in binary
6-
#define END_BYTE 0x55 // 01010101 in binary
5+
// #define START_BYTE 0xAA // 10101010 in binary
6+
#define END_BYTE 0xFF // 01010101 in binary
77

88
uint8_t calculateCRC(uint8_t *data, uint8_t length)
99
{
@@ -17,26 +17,19 @@ uint8_t calculateCRC(uint8_t *data, uint8_t length)
1717

1818
/**
1919
* create a packet
20-
* [START][DATA...][CRC][END]
20+
* [DATA...][END]
2121
*/
2222
std::vector<uint8_t> createPacket(const std::vector<uint8_t> &data)
2323
{
24-
std::vector<uint8_t> packet(data.size() + 3);
25-
packet[0] = START_BYTE;
26-
std::copy(data.begin(), data.end(), packet.begin() + 1);
27-
packet[data.size() + 1] = calculateCRC(const_cast<uint8_t *>(data.data()), data.size());
28-
packet[data.size() + 2] = END_BYTE;
24+
std::vector<uint8_t> packet(data.size() + 1); // +1 for END byte only
25+
std::copy(data.begin(), data.end(), packet.begin()); // Copy data at the start
26+
packet[data.size()] = END_BYTE; // Add END_BYTE at the end
2927
return packet;
3028
}
3129

3230
bool validatePacket(const std::vector<uint8_t> &packet)
3331
{
34-
if (packet.size() < 3)
35-
{
36-
return false;
37-
}
38-
39-
if (packet[0] != START_BYTE)
32+
if (packet.size() < 2)
4033
{
4134
return false;
4235
}
@@ -46,16 +39,6 @@ bool validatePacket(const std::vector<uint8_t> &packet)
4639
return false;
4740
}
4841

49-
// Extract data portion and verify CRC
50-
std::vector<uint8_t> data(packet.begin() + 1, packet.end() - 2);
51-
uint8_t receivedCRC = packet[packet.size() - 2];
52-
uint8_t calculatedCRC = calculateCRC(const_cast<uint8_t *>(data.data()), data.size());
53-
54-
if (receivedCRC != calculatedCRC)
55-
{
56-
return false;
57-
}
58-
5942
return true;
6043
}
6144

@@ -64,6 +47,6 @@ bool validatePacket(const std::vector<uint8_t> &packet)
6447
*/
6548
std::vector<uint8_t> createForwardPacket(const std::vector<uint8_t> &packet)
6649
{
67-
std::vector<uint8_t> data(packet.begin() + 2, packet.end() - 2); // skip START byte and first data byte, and skip CRC and END bytes
50+
std::vector<uint8_t> data(packet.begin() + 1, packet.end() - 1);
6851
return createPacket(data);
6952
}

code/test/utils_test.cpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,26 @@ void test_createPacket_function(void)
2727
std::vector<uint8_t> packet = createPacket(data);
2828

2929
uint8_t length = data.size();
30-
// Check START_BYTE
31-
TEST_ASSERT_EQUAL(START_BYTE, packet[0]);
3230

3331
// Check data
3432
for (size_t i = 0; i < length; i++)
3533
{
36-
TEST_ASSERT_EQUAL(data[i], packet[i + 1]);
34+
TEST_ASSERT_EQUAL(data[i], packet[i]);
3735
}
3836

39-
// Check CRC (0x01 ^ 0x02 ^ 0x03 ^ 0x04 ^ 0x05 = 0x01)
40-
TEST_ASSERT_EQUAL(0x01, packet[length + 1]);
41-
4237
// Check END_BYTE
43-
TEST_ASSERT_EQUAL(END_BYTE, packet[length + 2]);
38+
TEST_ASSERT_EQUAL(packet[length], END_BYTE);
4439
}
4540

4641
void test_validatePacket_function(void)
4742
{
4843
// valid packet
49-
std::vector<uint8_t> packet = {0xAA, 0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x55};
44+
std::vector<uint8_t> packet = {0xAA, 0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0xFF};
5045
TEST_ASSERT_TRUE(validatePacket(packet));
5146

52-
// invalid crc
53-
std::vector<uint8_t> packet2 = {0xAA, 0x01, 0x02, 0x03, 0x04, 0x05, 0x02, 0x55};
54-
TEST_ASSERT_FALSE(validatePacket(packet2));
55-
56-
// missing start byte
57-
std::vector<uint8_t> packet3 = {0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x55};
58-
TEST_ASSERT_FALSE(validatePacket(packet3));
59-
6047
// missing end byte
61-
std::vector<uint8_t> packet4 = {0xAA, 0x01, 0x02, 0x03, 0x04, 0x05, 0x01};
62-
TEST_ASSERT_FALSE(validatePacket(packet4));
48+
std::vector<uint8_t> packet2 = {0x01, 0x02, 0x03, 0x04, 0x05, 0x01};
49+
TEST_ASSERT_FALSE(validatePacket(packet2));
6350
}
6451

6552
void test_createForwardPacket_function(void)
@@ -68,8 +55,8 @@ void test_createForwardPacket_function(void)
6855
std::vector<uint8_t> forwardPacket = createForwardPacket(packet);
6956
TEST_ASSERT_EQUAL(packet.size() - 1, forwardPacket.size());
7057

71-
TEST_ASSERT_EQUAL(forwardPacket[1], 0x02);
72-
TEST_ASSERT_EQUAL(forwardPacket[2], 0x03);
58+
TEST_ASSERT_EQUAL(forwardPacket[0], 0x02);
59+
TEST_ASSERT_EQUAL(forwardPacket[1], 0x03);
7360
}
7461

7562
int main(int argc, char **argv)

0 commit comments

Comments
 (0)