-
Notifications
You must be signed in to change notification settings - Fork 0
/
BURT_serial.cpp
74 lines (65 loc) · 2.35 KB
/
BURT_serial.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "BURT_serial.h"
#include "BURT_proto.h"
BurtSerial::BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t* descriptor, int length) :
device(device),
onMessage(onMessage),
descriptor(descriptor),
length(length)
{ }
bool isResetCode(uint8_t* buffer, int length) {
return length >= 4
&& buffer[0] == 0
&& buffer[1] == 0
&& buffer[2] == 0
&& buffer[3] == 0;
}
void BurtSerial::update() {
int length = Serial.available();
if (length == 0) return;
uint8_t input[length];
int receivedLength = Serial.readBytes((char*) input, length);
if (!isConnected) {
tryConnect(input, length);
} else if (isResetCode(input, receivedLength)) {
// This is our special "reset" code. Respond with 1111
uint8_t response[4] = {0x01, 0x01, 0x01, 0x01};
Serial.write(response, 4);
isConnected = false;
} else {
onMessage(input, length);
}
}
void BurtSerial::tryConnect(uint8_t* input, int length) {
// Parse as an incoming Connect request
Connect connect = BurtProto::decode<Connect>(input, length, Connect_fields);
bool isValid = connect.receiver == Device::Device_FIRMWARE;
if (!isValid) return;
// Send a Connect response
Connect response;
response.receiver = connect.sender;
response.sender = device;
uint8_t buffer[8];
int newLength = BurtProto::encode(buffer, Connect_fields, &response, Connect_size);
Serial.write(buffer, newLength);
isConnected = true;
}
/**
* @brief Sends an encoded protobuf message over a serial connection.
*
* This function encodes the given message using the provided field structure.
* Then, it sends the encoded message over a serial connection
* if the connection has been established using the tryConnect method of BurtSerial.
*
* @param fields The Protobuf descriptor for this message. Use the generated MessageName_fields.
* @param message Pointer to the message structure to be sent, must be a protobuf message.
* @param length The maximum length of the encoded message. Use the generated MessageName_size.
* @return Returns `true` if the entire message is sent successfully, `false` otherwise.
*/
bool BurtSerial::send(const void* message) {
if (!isConnected) return false;
uint8_t* buffer = new uint8_t[length];
int encodedLength = BurtProto::encode(buffer, descriptor, message, length);
int sentLength = Serial.write(buffer, encodedLength);
delete[] buffer;
return encodedLength == sentLength;
}