From 37ec3f28efd2aa179f1a3ad73d0f45ef08eaf6cc Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Fri, 24 Jan 2025 17:28:20 +0100 Subject: [PATCH 1/5] Add custom commands --- src/improv.cpp | 39 +++++++++++++++++++-------------------- src/improv.h | 17 +++++++++++++++-- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/improv.cpp b/src/improv.cpp index 8a540e9..df97b02 100644 --- a/src/improv.cpp +++ b/src/improv.cpp @@ -7,6 +7,23 @@ ImprovCommand parse_improv_data(const std::vector &data, bool check_che return parse_improv_data(data.data(), data.size(), check_checksum); } +ImprovCommand parse_data_command(Command command, const uint8_t *data, size_t length) { + ImprovCommand cmd; + std::vector> data_fields; + size_t field_start = 3; + size_t field_end; + do { + size_t field_end = field_start + data[field_start - 1]; + if (field_end > length) { + cmd.command = UNKNOWN; + return cmd; + } + data_fields.push_back(std::vector(data + field_start, data + field_end)); + field_start = field_end + 1; + } while (field_end < length); + return {.command = command, .data = data_fields}; +} + ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum) { ImprovCommand improv_command; Command command = (Command) data[0]; @@ -31,26 +48,8 @@ ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_c } } - if (command == WIFI_SETTINGS) { - uint8_t ssid_length = data[2]; - uint8_t ssid_start = 3; - size_t ssid_end = ssid_start + ssid_length; - if (ssid_end > length) { - improv_command.command = UNKNOWN; - return improv_command; - } - - uint8_t pass_length = data[ssid_end]; - size_t pass_start = ssid_end + 1; - size_t pass_end = pass_start + pass_length; - if (pass_end > length) { - improv_command.command = UNKNOWN; - return improv_command; - } - - std::string ssid(data + ssid_start, data + ssid_end); - std::string password(data + pass_start, data + pass_end); - return {.command = command, .ssid = ssid, .password = password}; + if (command == WIFI_SETTINGS || command == CUSTOM) { + return parse_data_command(command, data, length); } improv_command.command = command; diff --git a/src/improv.h b/src/improv.h index d6befbd..a6afbf1 100644 --- a/src/improv.h +++ b/src/improv.h @@ -1,5 +1,6 @@ #pragma once +#include #ifdef ARDUINO #include #endif // ARDUINO @@ -42,6 +43,7 @@ enum Command : uint8_t { GET_CURRENT_STATE = 0x02, GET_DEVICE_INFO = 0x03, GET_WIFI_NETWORKS = 0x04, + CUSTOM = 0x80, BAD_CHECKSUM = 0xFF, }; @@ -57,8 +59,19 @@ enum ImprovSerialType : uint8_t { struct ImprovCommand { Command command; - std::string ssid; - std::string password; + std::vector> data; + std::string ssid() const { + if (!data.empty() && !data[0].empty() && command == WIFI_SETTINGS) { + return std::string(data[0].begin(), data[0].end()); + } + return ""; + } + std::string password() const { + if (data.size() > 1 && !data[1].empty() && command == WIFI_SETTINGS) { + return std::string(data[1].begin(), data[1].end()); + } + return ""; + } }; ImprovCommand parse_improv_data(const std::vector &data, bool check_checksum = true); From 10cc9002973c106c35158b0501bc6c5c0f4319ab Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Fri, 24 Jan 2025 17:47:02 +0100 Subject: [PATCH 2/5] Fix typo --- src/improv.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/improv.cpp b/src/improv.cpp index df97b02..3c985a6 100644 --- a/src/improv.cpp +++ b/src/improv.cpp @@ -11,7 +11,7 @@ ImprovCommand parse_data_command(Command command, const uint8_t *data, size_t le ImprovCommand cmd; std::vector> data_fields; size_t field_start = 3; - size_t field_end; + size_t field_end = 0; do { size_t field_end = field_start + data[field_start - 1]; if (field_end > length) { From a0d7c5bdddeca6224a3d6b9ceba6a59a6153b53c Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Fri, 24 Jan 2025 23:39:06 +0100 Subject: [PATCH 3/5] Test --- src/improv.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/improv.cpp b/src/improv.cpp index 3c985a6..5247540 100644 --- a/src/improv.cpp +++ b/src/improv.cpp @@ -1,4 +1,5 @@ #include "improv.h" +#include #include namespace improv { @@ -8,15 +9,14 @@ ImprovCommand parse_improv_data(const std::vector &data, bool check_che } ImprovCommand parse_data_command(Command command, const uint8_t *data, size_t length) { - ImprovCommand cmd; std::vector> data_fields; size_t field_start = 3; size_t field_end = 0; do { size_t field_end = field_start + data[field_start - 1]; + printf("field_end: %zu\n", field_end); if (field_end > length) { - cmd.command = UNKNOWN; - return cmd; + return {.command = UNKNOWN}; } data_fields.push_back(std::vector(data + field_start, data + field_end)); field_start = field_end + 1; From fdc16218c31510b6dfd758ca47ee1fda9172fd1d Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Fri, 24 Jan 2025 23:52:07 +0100 Subject: [PATCH 4/5] Fix fake unknown --- src/improv.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/improv.cpp b/src/improv.cpp index 5247540..45ef98c 100644 --- a/src/improv.cpp +++ b/src/improv.cpp @@ -11,16 +11,12 @@ ImprovCommand parse_improv_data(const std::vector &data, bool check_che ImprovCommand parse_data_command(Command command, const uint8_t *data, size_t length) { std::vector> data_fields; size_t field_start = 3; - size_t field_end = 0; - do { - size_t field_end = field_start + data[field_start - 1]; - printf("field_end: %zu\n", field_end); - if (field_end > length) { - return {.command = UNKNOWN}; - } + size_t field_end = field_start + data[2]; + while (field_end < length) { data_fields.push_back(std::vector(data + field_start, data + field_end)); field_start = field_end + 1; - } while (field_end < length); + field_end = field_start + data[field_start - 1]; + }; return {.command = command, .data = data_fields}; } @@ -48,7 +44,16 @@ ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_c } } - if (command == WIFI_SETTINGS || command == CUSTOM) { + if (command == WIFI_SETTINGS) { + improv_command = parse_data_command(command, data, length); + // There must be two data fields for the SSID and password + if (improv_command.data.size() != 2) { + improv_command.command = UNKNOWN; + } + return improv_command; + } + + if (command == CUSTOM) { return parse_data_command(command, data, length); } From d89a0a022e94579917cd49e50569cea7cd7b3425 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Sat, 25 Jan 2025 00:08:35 +0100 Subject: [PATCH 5/5] Remove headers --- src/improv.cpp | 1 - src/improv.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/improv.cpp b/src/improv.cpp index 45ef98c..ad145c0 100644 --- a/src/improv.cpp +++ b/src/improv.cpp @@ -1,5 +1,4 @@ #include "improv.h" -#include #include namespace improv { diff --git a/src/improv.h b/src/improv.h index a6afbf1..7c12b01 100644 --- a/src/improv.h +++ b/src/improv.h @@ -1,6 +1,5 @@ #pragma once -#include #ifdef ARDUINO #include #endif // ARDUINO