2
2
3
3
namespace improv {
4
4
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 );
7
7
}
8
8
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;
10
11
Command command = (Command) data[0 ];
11
12
uint8_t data_length = data[1 ];
12
13
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;
15
17
}
16
18
17
- uint8_t checksum = data[length - 1 ];
19
+ if (check_checksum) {
20
+ uint8_t checksum = data[length - 1 ];
18
21
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
+ }
23
26
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
+ }
26
31
}
27
32
28
33
if (command == WIFI_SETTINGS) {
@@ -39,12 +44,11 @@ ImprovCommand parse_improv_data(const uint8_t *data, size_t length) {
39
44
return {.command = command, .ssid = ssid, .password = password};
40
45
}
41
46
42
- return {
43
- .command = command,
44
- };
47
+ improv_command.command = command;
48
+ return improv_command;
45
49
}
46
50
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 ) {
48
52
std::vector<uint8_t > out;
49
53
uint32_t length = 0 ;
50
54
out.push_back (command);
@@ -56,16 +60,19 @@ std::vector<uint8_t> build_rpc_response(Command command, const std::vector<std::
56
60
}
57
61
out.insert (out.begin () + 1 , length);
58
62
59
- uint32_t calculated_checksum = 0 ;
63
+ if (add_checksum) {
64
+ uint32_t calculated_checksum = 0 ;
60
65
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);
63
70
}
64
- out.push_back (calculated_checksum);
65
71
return out;
66
72
}
67
73
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) {
69
76
std::vector<uint8_t > out;
70
77
uint32_t length = 0 ;
71
78
out.push_back (command);
@@ -77,13 +84,16 @@ std::vector<uint8_t> build_rpc_response(Command command, const std::vector<Strin
77
84
}
78
85
out.insert (out.begin () + 1 , length);
79
86
80
- uint32_t calculated_checksum = 0 ;
87
+ if (add_checksum) {
88
+ uint32_t calculated_checksum = 0 ;
81
89
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);
84
94
}
85
- out.push_back (calculated_checksum);
86
95
return out;
87
96
}
97
+ #endif // ARDUINO
88
98
89
99
} // namespace improv
0 commit comments