Skip to content

Commit f7f11d3

Browse files
authored
A few updates for ESPHome and improv serial (#3)
1 parent 022e29d commit f7f11d3

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

src/improv.cpp

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

33
namespace improv {
44

5-
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data) {
6-
return parse_improv_data(data.data(), data.size());
5+
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data, bool check_checksum) {
6+
return parse_improv_data(data.data(), data.size(), check_checksum);
77
}
88

9-
ImprovCommand parse_improv_data(const uint8_t *data, size_t length) {
9+
ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum) {
10+
ImprovCommand improv_command;
1011
Command command = (Command) data[0];
1112
uint8_t data_length = data[1];
1213

13-
if (data_length != length - 3) {
14-
return {.command = UNKNOWN};
14+
if (data_length != length - 2 - check_checksum) {
15+
improv_command.command = UNKNOWN;
16+
return improv_command;
1517
}
1618

17-
uint8_t checksum = data[length - 1];
19+
if (check_checksum) {
20+
uint8_t checksum = data[length - 1];
1821

19-
uint32_t calculated_checksum = 0;
20-
for (uint8_t i = 0; i < length - 1; i++) {
21-
calculated_checksum += data[i];
22-
}
22+
uint32_t calculated_checksum = 0;
23+
for (uint8_t i = 0; i < length - 1; i++) {
24+
calculated_checksum += data[i];
25+
}
2326

24-
if ((uint8_t) calculated_checksum != checksum) {
25-
return {.command = BAD_CHECKSUM};
27+
if ((uint8_t) calculated_checksum != checksum) {
28+
improv_command.command = BAD_CHECKSUM;
29+
return improv_command;
30+
}
2631
}
2732

2833
if (command == WIFI_SETTINGS) {
@@ -39,12 +44,11 @@ ImprovCommand parse_improv_data(const uint8_t *data, size_t length) {
3944
return {.command = command, .ssid = ssid, .password = password};
4045
}
4146

42-
return {
43-
.command = command,
44-
};
47+
improv_command.command = command;
48+
return improv_command;
4549
}
4650

47-
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum) {
51+
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum, bool add_checksum) {
4852
std::vector<uint8_t> out;
4953
uint32_t length = 0;
5054
out.push_back(command);
@@ -56,16 +60,19 @@ std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::
5660
}
5761
out.insert(out.begin() + 1, length);
5862

59-
uint32_t calculated_checksum = 0;
63+
if (add_checksum) {
64+
uint32_t calculated_checksum = 0;
6065

61-
for (uint8_t byte : out) {
62-
calculated_checksum += byte;
66+
for (uint8_t byte : out) {
67+
calculated_checksum += byte;
68+
}
69+
out.push_back(calculated_checksum);
6370
}
64-
out.push_back(calculated_checksum);
6571
return out;
6672
}
6773

68-
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum) {
74+
#ifdef ARDUINO
75+
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum, bool add_checksum) {
6976
std::vector<uint8_t> out;
7077
uint32_t length = 0;
7178
out.push_back(command);
@@ -77,13 +84,16 @@ std::vector<uint8_t> build_rpc_response(Command command, const std::vector<Strin
7784
}
7885
out.insert(out.begin() + 1, length);
7986

80-
uint32_t calculated_checksum = 0;
87+
if (add_checksum) {
88+
uint32_t calculated_checksum = 0;
8189

82-
for (uint8_t byte : out) {
83-
calculated_checksum += byte;
90+
for (uint8_t byte : out) {
91+
calculated_checksum += byte;
92+
}
93+
out.push_back(calculated_checksum);
8494
}
85-
out.push_back(calculated_checksum);
8695
return out;
8796
}
97+
#endif // ARDUINO
8898

8999
} // namespace improv

src/improv.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3+
#ifdef ARDUINO
34
#include "WString.h"
5+
#endif // ARDUINO
6+
47
#include <cstdint>
58
#include <string>
69
#include <vector>
@@ -35,6 +38,8 @@ enum Command : uint8_t {
3538
UNKNOWN = 0x00,
3639
WIFI_SETTINGS = 0x01,
3740
IDENTIFY = 0x02,
41+
GET_CURRENT_STATE = 0x02,
42+
GET_DEVICE_INFO = 0x03,
3843
BAD_CHECKSUM = 0xFF,
3944
};
4045

@@ -46,10 +51,13 @@ struct ImprovCommand {
4651
std::string password;
4752
};
4853

49-
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data);
50-
ImprovCommand parse_improv_data(const uint8_t *data, size_t length);
54+
ImprovCommand parse_improv_data(const std::vector<uint8_t> &data, bool check_checksum = true);
55+
ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum = true);
5156

52-
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum);
53-
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum);
57+
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::string> &datum,
58+
bool add_checksum = true);
59+
#ifdef ARDUINO
60+
std::vector<uint8_t> build_rpc_response(Command command, const std::vector<String> &datum, bool add_checksum = true);
61+
#endif // ARDUINO
5462

5563
} // namespace improv

0 commit comments

Comments
 (0)