Repository navigation
Expand file tree
/
Copy pathlumen_command_handler.cpp
More file actions
237 lines (194 loc) · 7.83 KB
/
Copy pathlumen_command_handler.cpp
File metadata and controls
237 lines (194 loc) · 7.83 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "lumen_command_handler.h"
#include <ArduinoJson.h>
#include <esp_log.h>
#include <math.h>
#include "lumen_type_validation.h"
namespace {
static const char* TAG = "command_handler";
static constexpr size_t COMMAND_ID_DOC_SLACK = COMMAND_ID_MAX_LEN + 32U;
static constexpr size_t LED_COMMAND_DOC_SIZE = JSON_OBJECT_SIZE(9) + COMMAND_ID_DOC_SLACK;
static constexpr size_t CONFIG_COMMAND_DOC_SIZE =
JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + COMMAND_ID_DOC_SLACK;
static constexpr size_t MODE_COMMAND_DOC_SIZE = JSON_OBJECT_SIZE(2) + COMMAND_ID_DOC_SLACK;
bool readBoolField(JsonVariantConst value, bool& out_value) {
if (!value.is<bool>()) {
return false;
}
out_value = value.as<bool>();
return true;
}
bool readU8Field(JsonVariantConst value, uint8_t& out_value) {
if (!value.is<uint32_t>()) {
return false;
}
const uint32_t raw_value = value.as<uint32_t>();
if (raw_value > UINT8_MAX) {
ESP_LOGW(TAG, "u8 field out of range: %lu", static_cast<unsigned long>(raw_value));
return false;
}
out_value = static_cast<uint8_t>(raw_value);
return true;
}
bool readPercentField(JsonVariantConst value, uint8_t& out_value) {
if (!readU8Field(value, out_value)) {
return false;
}
if (out_value > 100U) {
ESP_LOGW(TAG, "percent field out of range: %u", out_value);
return false;
}
return true;
}
bool readFloatField(JsonVariantConst value, float& out_value) {
// is<float>() accepts integer values too (ArduinoJson 6 NUMBER_BIT check).
if (!value.is<float>()) {
return false;
}
const float raw_value = value.as<float>();
if (!isfinite(raw_value)) {
ESP_LOGW(TAG, "float field not finite");
return false;
}
out_value = raw_value;
return true;
}
bool readModeField(JsonVariantConst value, DeviceMode& out_mode) {
uint8_t raw_mode = 0;
if (!readU8Field(value, raw_mode)) {
return false;
}
if (!parseDeviceMode(raw_mode, out_mode)) {
ESP_LOGW(TAG, "mode invalid: %u", raw_mode);
return false;
}
return true;
}
// Deserializes the frame and parses "command_id". This is the only place that turns an
// untrusted id into a CommandId. A bad id cannot form an ack topic, so the caller cannot ack it.
template <size_t DocSize>
bool decodeBaseDocument(
const uint8_t* payload,
uint16_t length,
StaticJsonDocument<DocSize>& doc,
CommandId& out_command_id
) {
// CommandHandler::decode() already rejected a null or empty payload.
const DeserializationError error = deserializeMsgPack(doc, payload, length);
if (error) {
ESP_LOGW(TAG, "decode failed: %s", error.c_str());
return false;
}
const JsonVariantConst command_id_value = doc["command_id"];
if (!command_id_value.is<const char*>()) {
ESP_LOGW(TAG, "command id missing");
return false;
}
// JsonString keeps the MsgPack byte count, so parseCommandId() sees an embedded NUL.
const JsonString command_id = command_id_value.as<JsonString>();
if (!parseCommandId(command_id.c_str(), command_id.size(), out_command_id)) {
ESP_LOGW(TAG, "command id invalid (length %u)", static_cast<unsigned>(command_id.size()));
return false;
}
return true;
}
using CommandHandler::DecodeStatus;
DecodeStatus decodeLedPayload(
const uint8_t* payload, uint16_t length, CommandEnvelope& out_command
) {
StaticJsonDocument<LED_COMMAND_DOC_SIZE> doc;
out_command = CommandEnvelope{};
if (!decodeBaseDocument(payload, length, doc, out_command.command_id)) {
return DecodeStatus::UNADDRESSABLE;
}
LedState led = {};
const bool has_power = readBoolField(doc["power"], led.power);
const bool has_red_enabled = readBoolField(doc["red_enabled"], led.red_enabled);
const bool has_blue_enabled = readBoolField(doc["blue_enabled"], led.blue_enabled);
const bool has_far_red_enabled = readBoolField(doc["far_red_enabled"], led.far_red_enabled);
const bool has_brightness = readPercentField(doc["brightness_pct"], led.brightness_pct);
const bool has_red_pct = readPercentField(doc["red_dist_pct"], led.red_dist_pct);
const bool has_blue_pct = readPercentField(doc["blue_dist_pct"], led.blue_dist_pct);
const bool has_far_red_pct = readPercentField(doc["far_red_dist_pct"], led.far_red_dist_pct);
if (!has_power || !has_red_enabled || !has_blue_enabled || !has_far_red_enabled ||
!has_brightness || !has_red_pct || !has_blue_pct || !has_far_red_pct) {
ESP_LOGW(TAG, "led payload incomplete");
return DecodeStatus::INVALID_FIELDS;
}
const std::optional<ValidLedState> valid_led = ValidLedState::parse(led);
if (!valid_led) {
ESP_LOGW(TAG, "led payload invalid");
return DecodeStatus::INVALID_FIELDS;
}
out_command.payload = LedCommand{*valid_led};
return DecodeStatus::DECODED;
}
DecodeStatus decodeConfigPayload(
const uint8_t* payload, uint16_t length, CommandEnvelope& out_command
) {
StaticJsonDocument<CONFIG_COMMAND_DOC_SIZE> doc;
out_command = CommandEnvelope{};
if (!decodeBaseDocument(payload, length, doc, out_command.command_id)) {
return DecodeStatus::UNADDRESSABLE;
}
const JsonVariantConst thresholds_value = doc["thresholds"];
if (!thresholds_value.is<JsonObjectConst>()) {
ESP_LOGW(TAG, "thresholds missing");
return DecodeStatus::INVALID_FIELDS;
}
ThresholdConfig thresholds = {};
const bool has_temp_min = readFloatField(thresholds_value["temp_min_c"], thresholds.temp_min_c);
const bool has_temp_max = readFloatField(thresholds_value["temp_max_c"], thresholds.temp_max_c);
const bool has_humidity_min =
readFloatField(thresholds_value["humidity_min_pct"], thresholds.humidity_min_pct);
const bool has_humidity_max =
readFloatField(thresholds_value["humidity_max_pct"], thresholds.humidity_max_pct);
if (!has_temp_min || !has_temp_max || !has_humidity_min || !has_humidity_max) {
ESP_LOGW(TAG, "threshold payload incomplete");
return DecodeStatus::INVALID_FIELDS;
}
const std::optional<ValidThresholds> valid_thresholds = ValidThresholds::parse(thresholds);
if (!valid_thresholds) {
ESP_LOGW(TAG, "threshold payload invalid");
return DecodeStatus::INVALID_FIELDS;
}
out_command.payload = ConfigCommand{*valid_thresholds};
return DecodeStatus::DECODED;
}
DecodeStatus decodeModePayload(
const uint8_t* payload, uint16_t length, CommandEnvelope& out_command
) {
StaticJsonDocument<MODE_COMMAND_DOC_SIZE> doc;
out_command = CommandEnvelope{};
if (!decodeBaseDocument(payload, length, doc, out_command.command_id)) {
return DecodeStatus::UNADDRESSABLE;
}
DeviceMode mode = DeviceMode::AUTONOMOUS;
if (!readModeField(doc["mode"], mode)) {
return DecodeStatus::INVALID_FIELDS;
}
out_command.payload = ModeCommand{mode};
return DecodeStatus::DECODED;
}
} // namespace
namespace CommandHandler {
DecodeStatus decode(
CommandKind kind, const uint8_t* payload, uint16_t length, CommandEnvelope& out_command
) {
if (payload == nullptr || length == 0U) {
ESP_LOGW(TAG, "invalid inbound command");
return DecodeStatus::UNADDRESSABLE;
}
switch (kind) {
case CommandKind::LED_CONTROL:
return decodeLedPayload(payload, length, out_command);
case CommandKind::CONFIG_UPDATE:
return decodeConfigPayload(payload, length, out_command);
case CommandKind::MODE_CHANGE:
return decodeModePayload(payload, length, out_command);
}
// Only a value outside the enum reaches this line. The switch has no default, so -Wswitch
// flags a new CommandKind.
ESP_LOGE(TAG, "unknown command kind: %u", static_cast<unsigned>(kind));
return DecodeStatus::UNADDRESSABLE;
}
} // namespace CommandHandler