1010#include " CBORAdapter.h"
1111#include " cbor/CBOR.h"
1212
13+ #ifndef BCP_DEBUG_PACKET
14+ #define BCP_DEBUG_PACKET 0
15+ #endif
16+
1317#define PACKET_VALIDITY_MS 30000
1418
1519/* *****************************************************************************
@@ -86,59 +90,59 @@ BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::sendA
8690 return TransmissionResult::PEER_NOT_AVAILABLE;
8791 }
8892
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 ();
91103
104+ for (int i = 0 ; i < receivedDataLen; i++) {
92105 PacketManager::ReceivingState res;
93106 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 ();
127134 }
135+ } else if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x02 ) {
136+ handleDisconnectRequest ();
128137 }
129- break ;
130- default :
131- break ;
132- }
138+ }
139+ break ;
140+ default :
141+ break ;
133142 }
134143 }
135144 }
136145
137- if (_outputMessagesList.size () > 0 ) {
138- checkOutputPacketValidity ();
139- transmitStream ();
140- }
141-
142146 return transmissionRes;
143147}
144148
@@ -152,11 +156,12 @@ bool BoardConfigurationProtocol::sendData(PacketManager::MessageType type, const
152156 outputMsg.setValidityTs (millis () + PACKET_VALIDITY_MS);
153157
154158 if (!PacketManager::createPacket (outputMsg, type, data, len)) {
155- // DEBUG_WARNING("BoardConfigurationProtocol::%s Failed to create packet", __FUNCTION__);
156159 return false ;
157160 }
158161
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
160165
161166 _outputMessagesList.push_back (outputMsg);
162167
@@ -198,7 +203,6 @@ bool BoardConfigurationProtocol::sendStatus(StatusMessage msg) {
198203 uint8_t data[len];
199204 res = CBORAdapter::statusToCBOR (msg, data, &len);
200205 if (!res) {
201- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed encode status: %d ", __FUNCTION__, (int)msg);
202206 return res;
203207 }
204208
@@ -249,13 +253,12 @@ bool BoardConfigurationProtocol::sendUhwid(const byte *uhwid) {
249253 res = CBORAdapter::uhwidToCBOR (uhwid, data, &cborDataLen);
250254
251255 if (!res) {
252- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed to convert uhwid to CBOR", __FUNCTION__);
253256 return res;
254257 }
255258
256259 res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
257260 if (!res) {
258- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to send uhwid" , __FUNCTION__);
261+ DEBUG_WARNING (" BoardConfigurationProtocol::%s failed to send uhwid" , __FUNCTION__);
259262 return res;
260263 }
261264
@@ -265,7 +268,6 @@ bool BoardConfigurationProtocol::sendUhwid(const byte *uhwid) {
265268bool BoardConfigurationProtocol::sendJwt (const char *jwt, size_t len) {
266269 bool res = false ;
267270 if (len > MAX_JWT_SIZE) {
268- // DEBUG_ERROR("BoardConfigurationProtocol::%s JWT too long", __FUNCTION__);
269271 return res;
270272 }
271273
@@ -274,14 +276,13 @@ bool BoardConfigurationProtocol::sendJwt(const char *jwt, size_t len) {
274276
275277 res = CBORAdapter::jwtToCBOR (jwt, data, &cborDataLen);
276278 if (!res) {
277- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed to convert JWT to CBOR", __FUNCTION__);
278279 return res;
279280 }
280281
281282 res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
282283
283284 if (!res) {
284- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to send JWT" , __FUNCTION__);
285+ DEBUG_WARNING (" BoardConfigurationProtocol::%s failed to send JWT" , __FUNCTION__);
285286 return res;
286287 }
287288
@@ -291,7 +292,6 @@ bool BoardConfigurationProtocol::sendJwt(const char *jwt, size_t len) {
291292bool BoardConfigurationProtocol::sendBleMacAddress (const uint8_t *mac, size_t len) {
292293 bool res = false ;
293294 if (len != BLE_MAC_ADDRESS_SIZE) {
294- // DEBUG_ERROR("BoardConfigurationProtocol::%s Invalid BLE MAC address", __FUNCTION__);
295295 return res;
296296 }
297297
@@ -300,13 +300,12 @@ bool BoardConfigurationProtocol::sendBleMacAddress(const uint8_t *mac, size_t le
300300
301301 res = CBORAdapter::BLEMacAddressToCBOR (mac, data, &cborDataLen);
302302 if (!res) {
303- // DEBUG_ERROR("BoardConfigurationProtocol::%s failed to convert BLE MAC address to CBOR", __FUNCTION__);
304303 return res;
305304 }
306305
307306 res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
308307 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__);
310309 return res;
311310 }
312311 return res;
@@ -320,13 +319,12 @@ bool BoardConfigurationProtocol::sendWifiFWVersion(const char *wifiFWVersion) {
320319
321320 res = CBORAdapter::wifiFWVersionToCBOR (wifiFWVersion, data, &cborDataLen);
322321 if (!res) {
323- DEBUG_ERROR (" BoardConfigurationProtocol::%s failed to convert WiFi FW version to CBOR" , __FUNCTION__);
324322 return res;
325323 }
326324
327325 res = sendData (PacketManager::MessageType::DATA, data, cborDataLen);
328326 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__);
330328 return res;
331329 }
332330
0 commit comments