-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardConfigurationProtocol.cpp
385 lines (320 loc) · 11.8 KB
/
BoardConfigurationProtocol.cpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "ANetworkConfigurator_Config.h"
#if NETWORK_CONFIGURATOR_COMPATIBLE
#include "BoardConfigurationProtocol.h"
#include "Arduino_DebugUtils.h"
#include "CBORAdapter.h"
#include "cbor/CBOR.h"
#define PACKET_VALIDITY_MS 30000
/******************************************************************************
* PUBLIC MEMBER FUNCTIONS
******************************************************************************/
bool BoardConfigurationProtocol::getMsg(ProvisioningInputMessage &msg) {
if (_inputMessagesList.size() == 0) {
return false;
}
InputPacketBuffer *buf = &_inputMessagesList.front();
ProvisioningMessageDown cborMsg;
bool decodeRes = CBORAdapter::getMsgFromCBOR(buf->get_ptr(), buf->len(), &cborMsg);
_inputMessagesList.pop_front();
if (!decodeRes) {
DEBUG_DEBUG("BoardConfigurationProtocol::%s Invalid message", __FUNCTION__);
sendStatus(StatusMessage::INVALID_PARAMS);
return false;
}
if (cborMsg.c.id == ProvisioningMessageId::TimestampProvisioningMessageId) {
msg.type = MessageInputType::TIMESTAMP;
msg.m.timestamp = cborMsg.provisioningTimestamp.timestamp;
} else if (cborMsg.c.id == ProvisioningMessageId::CommandsProvisioningMessageId) {
msg.type = MessageInputType::COMMANDS;
msg.m.cmd = (RemoteCommands)cborMsg.provisioningCommands.cmd;
} else {
msg.type = MessageInputType::NETWORK_SETTINGS;
msg.m.netSetting.type = NetworkAdapter::NONE;
memcpy(&msg.m.netSetting, &cborMsg.provisioningNetworkConfig.networkSetting, sizeof(models::NetworkSetting));
}
return true;
}
bool BoardConfigurationProtocol::sendMsg(ProvisioningOutputMessage &msg) {
bool res = false;
switch (msg.type) {
case MessageOutputType::STATUS:
res = sendStatus(msg.m.status);
break;
case MessageOutputType::NETWORK_OPTIONS:
res = sendNetworkOptions(msg.m.netOptions);
break;
case MessageOutputType::UHWID:
res = sendUhwid(msg.m.uhwid);
break;
case MessageOutputType::JWT:
res = sendJwt(msg.m.jwt, strlen(msg.m.jwt));
break;
case MessageOutputType::BLE_MAC_ADDRESS:
res = sendBleMacAddress(msg.m.BLEMacAddress, BLE_MAC_ADDRESS_SIZE);
break;
case MessageOutputType::WIFI_FW_VERSION:
res = sendVersion(msg.m.wifiFwVersion, msg.type);
break;
case MessageOutputType::PROV_SKETCH_VERSION:
res = sendVersion(msg.m.provSketchVersion, msg.type);
break;
case MessageOutputType::NETCONFIG_LIB_VERSION:
res = sendVersion(msg.m.netConfigLibVersion, msg.type);
break;
default:
break;
}
return res;
}
bool BoardConfigurationProtocol::msgAvailable() {
return _inputMessagesList.size() > 0;
}
/******************************************************************************
* PROTECTED MEMBER FUNCTIONS
******************************************************************************/
BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::sendAndReceive() {
TransmissionResult transmissionRes = TransmissionResult::NOT_COMPLETED;
if (!isPeerConnected()) {
return TransmissionResult::PEER_NOT_AVAILABLE;
}
if (_outputMessagesList.size() > 0) {
checkOutputPacketValidity();
transmitStream();
}
if (!received()) {
return transmissionRes;
}
int receivedDataLen = available();
for (int i = 0; i < receivedDataLen; i++) {
PacketManager::ReceivingState res;
uint8_t val = read();
res = PacketManager::PacketReceiver::getInstance().handleReceivedByte(_packet, val);
if (res == PacketManager::ReceivingState::ERROR) {
DEBUG_DEBUG("BoardConfigurationProtocol::%s Malformed packet", __FUNCTION__);
sendNak();
clearInputBuffer();
transmissionRes = TransmissionResult::INVALID_DATA;
break;
} else if (res == PacketManager::ReceivingState::RECEIVED) {
switch (_packet.Type) {
case PacketManager::MessageType::DATA:
{
#if BCP_DEBUG_PACKET == 1
printPacket("payload", _packet.Payload.get_ptr(), _packet.Payload.len());
#endif
_inputMessagesList.push_back(_packet.Payload);
//Consider all sent data as received
_outputMessagesList.clear();
transmissionRes = TransmissionResult::DATA_RECEIVED;
}
break;
case PacketManager::MessageType::TRANSMISSION_CONTROL:
{
if (_packet.Payload.len() == 1 && _packet.Payload[0] == (uint8_t)PacketManager::TransmissionControlMessage::NACK) {
for (std::list<OutputPacketBuffer>::iterator packet = _outputMessagesList.begin(); packet != _outputMessagesList.end(); ++packet) {
packet->startProgress();
}
} else if (_packet.Payload.len() == 1 && _packet.Payload[0] == (uint8_t)PacketManager::TransmissionControlMessage::DISCONNECT) {
handleDisconnectRequest();
}
}
break;
default:
break;
}
PacketManager::PacketReceiver::getInstance().clear(_packet);
}
}
return transmissionRes;
}
bool BoardConfigurationProtocol::sendNak() {
uint8_t data = 0x03;
return sendData(PacketManager::MessageType::TRANSMISSION_CONTROL, &data, sizeof(data));
}
bool BoardConfigurationProtocol::sendData(PacketManager::MessageType type, const uint8_t *data, size_t len) {
OutputPacketBuffer outputMsg;
outputMsg.setValidityTs(millis() + PACKET_VALIDITY_MS);
if (!PacketManager::createPacket(outputMsg, type, data, len)) {
return false;
}
#if BCP_DEBUG_PACKET == 1
printPacket("output message", outputMsg.get_ptr(), outputMsg.len());
#endif
_outputMessagesList.push_back(outputMsg);
TransmissionResult res = TransmissionResult::NOT_COMPLETED;
do {
res = transmitStream();
if (res == TransmissionResult::PEER_NOT_AVAILABLE) {
break;
}
} while (res == TransmissionResult::NOT_COMPLETED);
return true;
}
void BoardConfigurationProtocol::clear() {
PacketManager::PacketReceiver::getInstance().clear(_packet);
_outputMessagesList.clear();
_inputMessagesList.clear();
}
void BoardConfigurationProtocol::checkOutputPacketValidity() {
if (_outputMessagesList.size() == 0) {
return;
}
_outputMessagesList.remove_if([](OutputPacketBuffer &packet) {
if (packet.getValidityTs() != 0 && packet.getValidityTs() < millis()) {
return true;
}
return false;
});
}
/******************************************************************************
* PRIVATE MEMBER FUNCTIONS
******************************************************************************/
bool BoardConfigurationProtocol::sendStatus(StatusMessage msg) {
bool res = false;
size_t len = CBOR_DATA_STATUS_LEN;
uint8_t data[len];
res = CBORAdapter::statusToCBOR(msg, data, &len);
if (!res) {
return res;
}
res = sendData(PacketManager::MessageType::DATA, data, len);
if (!res) {
DEBUG_WARNING("BoardConfigurationProtocol::%s failed to send status: %d ", __FUNCTION__, (int)msg);
}
return res;
}
size_t BoardConfigurationProtocol::calculateTotalOptionsLength(const NetworkOptions *netOptions) {
size_t length = CBOR_DATA_HEADER_LEN;
if (netOptions->type == NetworkOptionsClass::WIFI) {
for (uint8_t i = 0; i < netOptions->option.wifi.numDiscoveredWiFiNetworks; i++) {
length += 4; //for RSSI and text identifier
length += netOptions->option.wifi.discoveredWifiNetworks[i].SSIDsize;
}
}
return length;
}
bool BoardConfigurationProtocol::sendNetworkOptions(const NetworkOptions *netOptions) {
bool res = false;
size_t len = calculateTotalOptionsLength(netOptions);
uint8_t data[len];
if (!CBORAdapter::networkOptionsToCBOR(netOptions, data, &len)) {
return res;
}
res = sendData(PacketManager::MessageType::DATA, data, len);
if (!res) {
DEBUG_WARNING("BoardConfigurationProtocol::%s failed to send network options", __FUNCTION__);
}
return res;
}
bool BoardConfigurationProtocol::sendUhwid(const byte *uhwid) {
bool res = false;
size_t cborDataLen = CBOR_DATA_UHWID_LEN;
uint8_t data[cborDataLen];
res = CBORAdapter::uhwidToCBOR(uhwid, data, &cborDataLen);
if (!res) {
return res;
}
res = sendData(PacketManager::MessageType::DATA, data, cborDataLen);
if (!res) {
DEBUG_WARNING("BoardConfigurationProtocol::%s failed to send uhwid", __FUNCTION__);
return res;
}
return res;
}
bool BoardConfigurationProtocol::sendJwt(const char *jwt, size_t len) {
bool res = false;
if (len > MAX_JWT_SIZE) {
return res;
}
size_t cborDataLen = CBOR_DATA_JWT_LEN;
uint8_t data[cborDataLen];
res = CBORAdapter::jwtToCBOR(jwt, data, &cborDataLen);
if (!res) {
return res;
}
res = sendData(PacketManager::MessageType::DATA, data, cborDataLen);
if (!res) {
DEBUG_WARNING("BoardConfigurationProtocol::%s failed to send JWT", __FUNCTION__);
return res;
}
return res;
}
bool BoardConfigurationProtocol::sendBleMacAddress(const uint8_t *mac, size_t len) {
bool res = false;
if (len != BLE_MAC_ADDRESS_SIZE) {
return res;
}
size_t cborDataLen = CBOR_DATA_BLE_MAC_LEN;
uint8_t data[cborDataLen];
res = CBORAdapter::BLEMacAddressToCBOR(mac, data, &cborDataLen);
if (!res) {
return res;
}
res = sendData(PacketManager::MessageType::DATA, data, cborDataLen);
if (!res) {
DEBUG_WARNING("BoardConfigurationProtocol::%s failed to send BLE MAC address", __FUNCTION__);
return res;
}
return res;
}
bool BoardConfigurationProtocol::sendVersion(const char *version, MessageOutputType type) {
bool res = false;
size_t cborDataLen = CBOR_MIN_WIFI_FW_VERSION_LEN + strlen(version);
uint8_t data[cborDataLen];
switch (type)
{
case MessageOutputType::WIFI_FW_VERSION: res = CBORAdapter::wifiFWVersionToCBOR (version, data, &cborDataLen); break;
case MessageOutputType::PROV_SKETCH_VERSION: res = CBORAdapter::provSketchVersionToCBOR (version, data, &cborDataLen); break;
case MessageOutputType::NETCONFIG_LIB_VERSION: res = CBORAdapter::netConfigLibVersionToCBOR(version, data, &cborDataLen); break;
default: return false;
}
if (!res) {
return res;
}
res = sendData(PacketManager::MessageType::DATA, data, cborDataLen);
if (!res) {
DEBUG_WARNING("BoardConfigurationProtocol::%s failed to send version of type %d", __FUNCTION__, (int)type);
}
return res;
}
BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::transmitStream() {
if (!isPeerConnected()) {
return TransmissionResult::PEER_NOT_AVAILABLE;
}
if (_outputMessagesList.size() == 0) {
return TransmissionResult::COMPLETED;
}
TransmissionResult res = TransmissionResult::COMPLETED;
for (std::list<OutputPacketBuffer>::iterator packet = _outputMessagesList.begin(); packet != _outputMessagesList.end(); ++packet) {
if (packet->hasBytesToSend()) {
res = TransmissionResult::NOT_COMPLETED;
packet->incrementBytesSent(write(packet->get_ptrAt(packet->bytesSent()), packet->bytesToSend()));
#if BCP_DEBUG_PACKET == 1
DEBUG_DEBUG("BoardConfigurationProtocol::%s transferred: %d of %d", __FUNCTION__, packet->bytesSent(), packet->len());
#endif
break;
}
}
return res;
}
void BoardConfigurationProtocol::printPacket(const char *label, const uint8_t *data, size_t len) {
if (Debug.getDebugLevel() == DBG_VERBOSE) {
DEBUG_VERBOSE("Print %s data:", label);
Debug.newlineOff();
for (size_t i = 0; i < len; i++) {
DEBUG_VERBOSE("%02x ", data[i]);
if ((i + 1) % 10 == 0) {
DEBUG_VERBOSE("\n");
}
}
DEBUG_VERBOSE("\n");
}
Debug.newlineOn();
}
#endif // NETWORK_CONFIGURATOR_COMPATIBLE