10
10
#include " CBORAdapter.h"
11
11
#include " cbor/CBOR.h"
12
12
13
+ #ifndef BCP_DEBUG_PACKET
14
+ #define BCP_DEBUG_PACKET 0
15
+ #endif
16
+
13
17
#define PACKET_VALIDITY_MS 30000
14
18
15
19
/* *****************************************************************************
@@ -86,59 +90,59 @@ BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::sendA
86
90
return TransmissionResult::PEER_NOT_AVAILABLE;
87
91
}
88
92
89
- if (hasReceivedBytes ()) {
90
- int receivedDataLen = receivedBytes ();
93
+ if (_outputMessagesList.size () > 0 ) {
94
+ checkOutputPacketValidity ();
95
+ transmitStream ();
96
+ }
97
+
98
+ if (!hasReceivedBytes ()) {
99
+ return transmissionRes;
100
+ }
101
+
102
+ int receivedDataLen = receivedBytes ();
91
103
104
+ for (int i = 0 ; i < receivedDataLen; i++) {
92
105
PacketManager::ReceivingState res;
93
106
PacketManager::ReceivedData receivedData;
94
-
95
- for (int i = 0 ; i < receivedDataLen; i++) {
96
- uint8_t val = readByte ();
97
- res = PacketManager::getInstance ().handleReceivedByte (receivedData, val);
98
- if (res == PacketManager::ReceivingState::ERROR) {
99
- DEBUG_DEBUG (" BoardConfigurationProtocol::%s Error receiving packet" , __FUNCTION__);
100
- sendNak ();
101
- clearInputBuffer ();
102
- transmissionRes = TransmissionResult::INVALID_DATA;
103
- break ;
104
- } else if (res == PacketManager::ReceivingState::RECEIVED) {
105
- switch (receivedData.type ) {
106
- case PacketManager::MessageType::DATA:
107
- {
108
- // DEBUG_DEBUG("BoardConfigurationProtocol::%s Received data packet", __FUNCTION__);
109
- // printPacket("payload", receivedData.payload.get_ptr(), receivedData.payload.len());
110
- _inputMessagesList.push_back (receivedData.payload );
111
- // Consider all sent data as received
112
- _outputMessagesList.clear ();
113
- transmissionRes = TransmissionResult::DATA_RECEIVED;
114
- }
115
- break ;
116
- case PacketManager::MessageType::TRANSMISSION_CONTROL:
117
- {
118
- if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x03 ) {
119
-
120
- // DEBUG_DEBUG("BoardConfigurationProtocol::%s Received NACK packet", __FUNCTION__);
121
- for (std::list<OutputPacketBuffer>::iterator packet = _outputMessagesList.begin (); packet != _outputMessagesList.end (); ++packet) {
122
- packet->startProgress ();
123
- }
124
- } else if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x02 ) {
125
- // DEBUG_DEBUG("BoardConfigurationProtocol::%s Received disconnect request", __FUNCTION__);
126
- handleDisconnectRequest ();
107
+ uint8_t val = readByte ();
108
+
109
+ res = PacketManager::getInstance ().handleReceivedByte (receivedData, val);
110
+ if (res == PacketManager::ReceivingState::ERROR) {
111
+ DEBUG_DEBUG (" BoardConfigurationProtocol::%s Malformed packet" , __FUNCTION__);
112
+ sendNak ();
113
+ clearInputBuffer ();
114
+ transmissionRes = TransmissionResult::INVALID_DATA;
115
+ break ;
116
+ } else if (res == PacketManager::ReceivingState::RECEIVED) {
117
+ switch (receivedData.type ) {
118
+ case PacketManager::MessageType::DATA:
119
+ {
120
+ #ifdef BCP_DEBUG_PACKET
121
+ printPacket (" payload" , receivedData.payload .get_ptr (), receivedData.payload .len ());
122
+ #endif
123
+ _inputMessagesList.push_back (receivedData.payload );
124
+ // Consider all sent data as received
125
+ _outputMessagesList.clear ();
126
+ transmissionRes = TransmissionResult::DATA_RECEIVED;
127
+ }
128
+ break ;
129
+ case PacketManager::MessageType::TRANSMISSION_CONTROL:
130
+ {
131
+ if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x03 ) {
132
+ for (std::list<OutputPacketBuffer>::iterator packet = _outputMessagesList.begin (); packet != _outputMessagesList.end (); ++packet) {
133
+ packet->startProgress ();
127
134
}
135
+ } else if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x02 ) {
136
+ handleDisconnectRequest ();
128
137
}
129
- break ;
130
- default :
131
- break ;
132
- }
138
+ }
139
+ break ;
140
+ default :
141
+ break ;
133
142
}
134
143
}
135
144
}
136
145
137
- if (_outputMessagesList.size () > 0 ) {
138
- checkOutputPacketValidity ();
139
- transmitStream ();
140
- }
141
-
142
146
return transmissionRes;
143
147
}
144
148
@@ -152,11 +156,12 @@ bool BoardConfigurationProtocol::sendData(PacketManager::MessageType type, const
152
156
outputMsg.setValidityTs (millis () + PACKET_VALIDITY_MS);
153
157
154
158
if (!PacketManager::createPacket (outputMsg, type, data, len)) {
155
- // DEBUG_WARNING("BoardConfigurationProtocol::%s Failed to create packet", __FUNCTION__);
156
159
return false ;
157
160
}
158
161
159
- // printPacket("output message", outputMsg.get_ptr(), outputMsg.len());
162
+ #ifdef BCP_DEBUG_PACKET
163
+ printPacket (" output message" , outputMsg.get_ptr (), outputMsg.len ());
164
+ #endif
160
165
161
166
_outputMessagesList.push_back (outputMsg);
162
167
@@ -198,7 +203,6 @@ bool BoardConfigurationProtocol::sendStatus(StatusMessage msg) {
198
203
uint8_t data[len];
199
204
res = CBORAdapter::statusToCBOR (msg, data, &len);
200
205
if (!res) {
201
- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed encode status: %d ", __FUNCTION__, (int)msg);
202
206
return res;
203
207
}
204
208
@@ -249,13 +253,12 @@ bool BoardConfigurationProtocol::sendUhwid(const byte *uhwid) {
249
253
res = CBORAdapter::uhwidToCBOR (uhwid, data, &cborDataLen);
250
254
251
255
if (!res) {
252
- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed to convert uhwid to CBOR", __FUNCTION__);
253
256
return res;
254
257
}
255
258
256
259
res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
257
260
if (!res) {
258
- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to send uhwid" , __FUNCTION__);
261
+ DEBUG_WARNING (" BoardConfigurationProtocol::%s failed to send uhwid" , __FUNCTION__);
259
262
return res;
260
263
}
261
264
@@ -265,7 +268,6 @@ bool BoardConfigurationProtocol::sendUhwid(const byte *uhwid) {
265
268
bool BoardConfigurationProtocol::sendJwt (const char *jwt, size_t len) {
266
269
bool res = false ;
267
270
if (len > MAX_JWT_SIZE) {
268
- // DEBUG_ERROR("BoardConfigurationProtocol::%s JWT too long", __FUNCTION__);
269
271
return res;
270
272
}
271
273
@@ -274,14 +276,13 @@ bool BoardConfigurationProtocol::sendJwt(const char *jwt, size_t len) {
274
276
275
277
res = CBORAdapter::jwtToCBOR (jwt, data, &cborDataLen);
276
278
if (!res) {
277
- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed to convert JWT to CBOR", __FUNCTION__);
278
279
return res;
279
280
}
280
281
281
282
res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
282
283
283
284
if (!res) {
284
- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to send JWT" , __FUNCTION__);
285
+ DEBUG_WARNING (" BoardConfigurationProtocol::%s failed to send JWT" , __FUNCTION__);
285
286
return res;
286
287
}
287
288
@@ -291,7 +292,6 @@ bool BoardConfigurationProtocol::sendJwt(const char *jwt, size_t len) {
291
292
bool BoardConfigurationProtocol::sendBleMacAddress (const uint8_t *mac, size_t len) {
292
293
bool res = false ;
293
294
if (len != BLE_MAC_ADDRESS_SIZE) {
294
- // DEBUG_ERROR("BoardConfigurationProtocol::%s Invalid BLE MAC address", __FUNCTION__);
295
295
return res;
296
296
}
297
297
@@ -300,13 +300,12 @@ bool BoardConfigurationProtocol::sendBleMacAddress(const uint8_t *mac, size_t le
300
300
301
301
res = CBORAdapter::BLEMacAddressToCBOR (mac, data, &cborDataLen);
302
302
if (!res) {
303
- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed to convert BLE MAC address to CBOR", __FUNCTION__);
304
303
return res;
305
304
}
306
305
307
306
res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
308
307
if (!res) {
309
- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to send BLE MAC address" , __FUNCTION__);
308
+ DEBUG_WARNING (" BoardConfigurationProtocol::%s failed to send BLE MAC address" , __FUNCTION__);
310
309
return res;
311
310
}
312
311
return res;
@@ -320,13 +319,12 @@ bool BoardConfigurationProtocol::sendWifiFWVersion(const char *wifiFWVersion) {
320
319
321
320
res = CBORAdapter::wifiFWVersionToCBOR (wifiFWVersion, data, &cborDataLen);
322
321
if (!res) {
323
- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to convert WiFi FW version to CBOR" , __FUNCTION__);
324
322
return res;
325
323
}
326
324
327
325
res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
328
326
if (!res) {
329
- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to send WiFi FW version" , __FUNCTION__);
327
+ DEBUG_WARNING (" BoardConfigurationProtocol::%s failed to send WiFi FW version" , __FUNCTION__);
330
328
return res;
331
329
}
332
330
0 commit comments