From ec52b44853c6586b4e824aff66c3511f331f7131 Mon Sep 17 00:00:00 2001 From: srijal30 Date: Tue, 5 Nov 2024 19:28:34 -0500 Subject: [PATCH 1/8] updated protobuf --- BURT_serial.cpp | 1 + Protobuf | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/BURT_serial.cpp b/BURT_serial.cpp index 3e781c3..d713056 100644 --- a/BURT_serial.cpp +++ b/BURT_serial.cpp @@ -34,6 +34,7 @@ void BurtSerial::update() { } } +// CHANGE THIS void BurtSerial::tryConnect(uint8_t* input, int length) { // Parse as an incoming Connect request Connect connect = BurtProto::decode(input, length, Connect_fields); diff --git a/Protobuf b/Protobuf index 72e7804..0278643 160000 --- a/Protobuf +++ b/Protobuf @@ -1 +1 @@ -Subproject commit 72e7804d72b3d1334a2f331b9abc68078e0c8f65 +Subproject commit 027864392bb21b32dcb28d28a341072d19ee8c72 From ca631aef1b84378ddb6d63bd5f84ee0cbc744d71 Mon Sep 17 00:00:00 2001 From: warfarm Date: Mon, 11 Nov 2024 20:08:20 -0500 Subject: [PATCH 2/8] Organize WrappedMessage data --- BURT_proto.h | 5 +++-- BURT_serial.cpp | 35 +++++++++++++++++++---------------- BURT_serial.h | 2 ++ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/BURT_proto.h b/BURT_proto.h index e2ab7d2..bdd972f 100644 --- a/BURT_proto.h +++ b/BURT_proto.h @@ -15,9 +15,10 @@ class BurtProto { static bool decodeRaw(const uint8_t* buffer, int length, const pb_msgdesc_t* fields, void* message); template - static T decode(const uint8_t* buffer, int length, const pb_msgdesc_t* fields) { + static std::optional decode(const uint8_t* buffer, int length, const pb_msgdesc_t* fields) { T result; - decodeRaw(buffer, length, fields, &result); + if(!decodeRaw(buffer, length, fields, &result)) + return {}; return result; } }; diff --git a/BURT_serial.cpp b/BURT_serial.cpp index d713056..1e659bb 100644 --- a/BURT_serial.cpp +++ b/BURT_serial.cpp @@ -8,13 +8,13 @@ BurtSerial::BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t length(length) { } -bool isResetCode(uint8_t* buffer, int length) { - return length >= 4 - && buffer[0] == 0 - && buffer[1] == 0 - && buffer[2] == 0 - && buffer[3] == 0; -} +// 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(); @@ -22,16 +22,19 @@ void BurtSerial::update() { uint8_t input[length]; int receivedLength = Serial.readBytes((char*) input, length); - if (!isConnected) { + 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); - } + + // NO CHECK + WrappedMessage msg = BurtProto::decode(input, length, WrappedMessage_fields); + // } 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); + // } } // CHANGE THIS diff --git a/BURT_serial.h b/BURT_serial.h index d8ecb07..50348f0 100644 --- a/BURT_serial.h +++ b/BURT_serial.h @@ -12,6 +12,8 @@ class BurtSerial { void setup() { /* No setup needed */ } void update(); bool send(const void* message); + void decode(); + bool sendLogMessage(BurtLog message); private: void tryConnect(uint8_t* input, int length); From d42b2a13a04ce09b3c9558412c9af1c86bc53cb6 Mon Sep 17 00:00:00 2001 From: warfarm Date: Mon, 11 Nov 2024 20:12:34 -0500 Subject: [PATCH 3/8] check --- BURT_serial.cpp | 24 ++++++++++++++++++++++++ Protobuf | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/BURT_serial.cpp b/BURT_serial.cpp index 1e659bb..45b8691 100644 --- a/BURT_serial.cpp +++ b/BURT_serial.cpp @@ -27,6 +27,20 @@ void BurtSerial::update() { // NO CHECK WrappedMessage msg = BurtProto::decode(input, length, WrappedMessage_fields); + + switch(msg.type) + { + case MessageType::HEARTBEAT: + break; + case MessageType::DISCONNECT: + break; + case MessageType::COMMAND: + break; + default: + break; + + } + // } else if (isResetCode(input, receivedLength)) { // // This is our special "reset" code. Respond with 1111 // uint8_t response[4] = {0x01, 0x01, 0x01, 0x01}; @@ -67,6 +81,11 @@ void BurtSerial::tryConnect(uint8_t* input, int length) { * @return Returns `true` if the entire message is sent successfully, `false` otherwise. */ bool BurtSerial::send(const void* message) { + + // Wrap it to wrapped message + BurtProto::encode() + + // if (!isConnected) return false; uint8_t* buffer = new uint8_t[length]; @@ -76,3 +95,8 @@ bool BurtSerial::send(const void* message) { delete[] buffer; return encodedLength == sentLength; } + +bool BurtSerial:sendLogMessage(BurtLog message){ + + return true; +} diff --git a/Protobuf b/Protobuf index 0278643..75b5fdc 160000 --- a/Protobuf +++ b/Protobuf @@ -1 +1 @@ -Subproject commit 027864392bb21b32dcb28d28a341072d19ee8c72 +Subproject commit 75b5fdc744862f0e7d0e72b64015a7d2370e4bd5 From 15945eb1322d159d3f5b761d47ca2a151adf4a90 Mon Sep 17 00:00:00 2001 From: warfarm Date: Mon, 11 Nov 2024 20:38:01 -0500 Subject: [PATCH 4/8] More Switch development --- BURT_serial.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/BURT_serial.cpp b/BURT_serial.cpp index 45b8691..0e10ff5 100644 --- a/BURT_serial.cpp +++ b/BURT_serial.cpp @@ -31,13 +31,18 @@ void BurtSerial::update() { switch(msg.type) { case MessageType::HEARTBEAT: + // check sender validity? break; case MessageType::DISCONNECT: + uint8_t response[4] = {0x01, 0x01, 0x01, 0x01}; + Serial.write(response, 4); + isConnected = false; break; case MessageType::COMMAND: + // what special thing we do here other than onMessage(input,length) lil bro break; default: - break; + onMessage(input, length); } From 3406b3d8449a9eb7f2a2b5f34f24e5f163e7beef Mon Sep 17 00:00:00 2001 From: warfarm Date: Fri, 15 Nov 2024 13:03:10 -0500 Subject: [PATCH 5/8] Some fixe --- BURT_proto.h | 2 +- BURT_serial.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/BURT_proto.h b/BURT_proto.h index bdd972f..ac4762d 100644 --- a/BURT_proto.h +++ b/BURT_proto.h @@ -18,7 +18,7 @@ class BurtProto { static std::optional decode(const uint8_t* buffer, int length, const pb_msgdesc_t* fields) { T result; if(!decodeRaw(buffer, length, fields, &result)) - return {}; + return std::nullopt; return result; } }; diff --git a/BURT_serial.cpp b/BURT_serial.cpp index 0e10ff5..0dd9598 100644 --- a/BURT_serial.cpp +++ b/BURT_serial.cpp @@ -22,8 +22,9 @@ void BurtSerial::update() { uint8_t input[length]; int receivedLength = Serial.readBytes((char*) input, length); - if (!isConnected) + if (!isConnected){ tryConnect(input, length); + } // NO CHECK WrappedMessage msg = BurtProto::decode(input, length, WrappedMessage_fields); @@ -35,7 +36,8 @@ void BurtSerial::update() { break; case MessageType::DISCONNECT: uint8_t response[4] = {0x01, 0x01, 0x01, 0x01}; - Serial.write(response, 4); + //Serial.write(response, 4); + BurtSerial::send(response) isConnected = false; break; case MessageType::COMMAND: From 54e56fe9c44bea99e3ff27faba54ec7e27f3cd2c Mon Sep 17 00:00:00 2001 From: warfarm Date: Mon, 18 Nov 2024 20:25:27 -0500 Subject: [PATCH 6/8] Add Version Check --- BURT_serial.cpp | 11 +++++++++-- version.pb.c | 12 ++++++++++++ version.pb.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 version.pb.c create mode 100644 version.pb.h diff --git a/BURT_serial.cpp b/BURT_serial.cpp index 0dd9598..bc1c062 100644 --- a/BURT_serial.cpp +++ b/BURT_serial.cpp @@ -1,11 +1,13 @@ #include "BURT_serial.h" #include "BURT_proto.h" +#include "version.pb.h" -BurtSerial::BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t* descriptor, int length) : +BurtSerial::BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t* descriptor, int length, Version version) : device(device), onMessage(onMessage), descriptor(descriptor), - length(length) + length(length), + version(version) { } // bool isResetCode(uint8_t* buffer, int length) { @@ -29,6 +31,11 @@ void BurtSerial::update() { // NO CHECK WrappedMessage msg = BurtProto::decode(input, length, WrappedMessage_fields); + if(msg.version.major != this->version) + { + // Send back invalid version message? + } + switch(msg.type) { case MessageType::HEARTBEAT: diff --git a/version.pb.c b/version.pb.c new file mode 100644 index 0000000..621c8ce --- /dev/null +++ b/version.pb.c @@ -0,0 +1,12 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.4.9 */ + +#include "version.pb.h" +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +PB_BIND(Version, Version, AUTO) + + + diff --git a/version.pb.h b/version.pb.h new file mode 100644 index 0000000..dcff46a --- /dev/null +++ b/version.pb.h @@ -0,0 +1,51 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.4.9 */ + +#ifndef PB_VERSION_PB_H_INCLUDED +#define PB_VERSION_PB_H_INCLUDED +#include "pb.h" + +#if PB_PROTO_HEADER_VERSION != 40 +#error Regenerate this file with the current version of nanopb generator. +#endif + +/* Struct definitions */ +typedef struct _Version { + int32_t major; + int32_t minor; +} Version; + + +#ifdef __cplusplus +extern "C" { +#endif + +/* Initializer values for message structs */ +#define Version_init_default {0, 0} +#define Version_init_zero {0, 0} + +/* Field tags (for use in manual encoding/decoding) */ +#define Version_major_tag 1 +#define Version_minor_tag 2 + +/* Struct field encoding specification for nanopb */ +#define Version_FIELDLIST(X, a) \ +X(a, STATIC, SINGULAR, INT32, major, 1) \ +X(a, STATIC, SINGULAR, INT32, minor, 2) +#define Version_CALLBACK NULL +#define Version_DEFAULT NULL + +extern const pb_msgdesc_t Version_msg; + +/* Defines for backwards compatibility with code written before nanopb-0.4.0 */ +#define Version_fields &Version_msg + +/* Maximum encoded size of messages (where known) */ +#define VERSION_PB_H_MAX_SIZE Version_size +#define Version_size 22 + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif From b7a1cfaa55a0ec7007526c84635ada55b79507f8 Mon Sep 17 00:00:00 2001 From: warfarm Date: Mon, 18 Nov 2024 20:26:29 -0500 Subject: [PATCH 7/8] Add Version Check --- BURT_serial.h | 4 +++- Protobuf | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/BURT_serial.h b/BURT_serial.h index 50348f0..20b70e1 100644 --- a/BURT_serial.h +++ b/BURT_serial.h @@ -3,12 +3,13 @@ #include #include "BURT_proto.h" +#include "version.pb.h" class BurtSerial { public: bool isConnected = false; - BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t* descriptor, int length); + BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t* descriptor, int length, Version version); void setup() { /* No setup needed */ } void update(); bool send(const void* message); @@ -21,4 +22,5 @@ class BurtSerial { ProtoHandler onMessage; const pb_msgdesc_t* descriptor; int length; + Version version; }; diff --git a/Protobuf b/Protobuf index 75b5fdc..33cf349 160000 --- a/Protobuf +++ b/Protobuf @@ -1 +1 @@ -Subproject commit 75b5fdc744862f0e7d0e72b64015a7d2370e4bd5 +Subproject commit 33cf3499083d2efb238e9ca1a3a8a09c4f5b70b8 From 0f49e31139958036c1da61457057c5cef3fa6eb5 Mon Sep 17 00:00:00 2001 From: warfarm Date: Thu, 19 Dec 2024 14:55:24 -0500 Subject: [PATCH 8/8] add bool receipt in constructor --- BURT_serial.cpp | 10 ++++++---- BURT_serial.h | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/BURT_serial.cpp b/BURT_serial.cpp index bc1c062..f7e335f 100644 --- a/BURT_serial.cpp +++ b/BURT_serial.cpp @@ -2,12 +2,13 @@ #include "BURT_proto.h" #include "version.pb.h" -BurtSerial::BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t* descriptor, int length, Version version) : +BurtSerial::BurtSerial(Device device, ProtoHandler onMessage, const pb_msgdesc_t* descriptor, int length, Version version, bool receipt = false) : device(device), onMessage(onMessage), descriptor(descriptor), length(length), - version(version) + version(version), + receipt(receipt), { } // bool isResetCode(uint8_t* buffer, int length) { @@ -31,8 +32,9 @@ void BurtSerial::update() { // NO CHECK WrappedMessage msg = BurtProto::decode(input, length, WrappedMessage_fields); - if(msg.version.major != this->version) + if(msg.version.major != this->version.major) { + // Send back invalid version message? } @@ -49,7 +51,7 @@ void BurtSerial::update() { break; case MessageType::COMMAND: // what special thing we do here other than onMessage(input,length) lil bro - break; + // break; default: onMessage(input, length); diff --git a/BURT_serial.h b/BURT_serial.h index 20b70e1..3853802 100644 --- a/BURT_serial.h +++ b/BURT_serial.h @@ -15,6 +15,7 @@ class BurtSerial { bool send(const void* message); void decode(); bool sendLogMessage(BurtLog message); + bool receipt; private: void tryConnect(uint8_t* input, int length);