10
10
#include " CBORAdapter.h"
11
11
#include " cbor/CBOR.h"
12
12
13
- #define PACKET_VALIDITY_S 30000
13
+ #define PACKET_VALIDITY_MS 30000
14
14
15
- BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::sendAndReceive () {
16
- TransmissionResult transmissionRes = TransmissionResult::NOT_COMPLETED;
17
- if (!isPeerConnected ()) {
18
- return TransmissionResult::PEER_NOT_AVAILABLE;
19
- }
20
-
21
- if (hasReceivedBytes ()) {
22
- int receivedDataLen = receivedBytes ();
23
-
24
- PacketManager::ReceivingState res;
25
- PacketManager::ReceivedData receivedData;
26
-
27
- for (int i = 0 ; i < receivedDataLen; i++) {
28
- uint8_t val = readByte ();
29
- res = Packet.handleReceivedByte (receivedData, val);
30
- if (res == PacketManager::ReceivingState::ERROR) {
31
- DEBUG_DEBUG (" BoardConfigurationProtocol::%s Error receiving packet" , __FUNCTION__);
32
- sendNak ();
33
- transmissionRes = TransmissionResult::INVALID_DATA;
34
- break ;
35
- } else if (res == PacketManager::ReceivingState::RECEIVED) {
36
- switch (receivedData.type ) {
37
- case PacketManager::MessageType::DATA:
38
- {
39
- DEBUG_DEBUG (" BoardConfigurationProtocol::%s Received data packet" , __FUNCTION__);
40
- printPacket (" payload" , &receivedData.payload [0 ], receivedData.payload .len ());
41
- _inputMessagesList.push_back (receivedData.payload );
42
- // Consider all sent data as received
43
- _outputMessagesList.clear ();
44
- transmissionRes = TransmissionResult::DATA_RECEIVED;
45
- }
46
- break ;
47
- case PacketManager::MessageType::TRANSMISSION_CONTROL:
48
- {
49
- if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x03 ) {
50
-
51
- DEBUG_DEBUG (" BoardConfigurationProtocol::%s Received NACK packet" , __FUNCTION__);
52
- for (std::list<OutputPacketBuffer>::iterator packet = _outputMessagesList.begin (); packet != _outputMessagesList.end (); ++packet) {
53
- packet->startProgress ();
54
- }
55
- } else if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x02 ) {
56
- DEBUG_DEBUG (" BoardConfigurationProtocol::%s Received disconnect request" , __FUNCTION__);
57
- handleDisconnectRequest ();
58
- }
59
- }
60
- break ;
61
- default :
62
- break ;
63
- }
64
- }
65
- }
66
- }
67
-
68
- if (_outputMessagesList.size () > 0 ) {
69
- checkOutputPacketValidity ();
70
- transmitStream ();
71
- }
72
-
73
- return transmissionRes;
74
- }
75
-
76
- bool BoardConfigurationProtocol::sendNak () {
77
- uint8_t data = 0x03 ;
78
- return sendData (PacketManager::MessageType::TRANSMISSION_CONTROL, &data, sizeof (data));
79
- }
80
-
81
- bool BoardConfigurationProtocol::getReceivedMsg (ProvisioningInputMessage &msg) {
15
+ /* *****************************************************************************
16
+ * PUBLIC MEMBER FUNCTIONS
17
+ ******************************************************************************/
18
+ bool BoardConfigurationProtocol::getMsg (ProvisioningInputMessage &msg) {
82
19
if (_inputMessagesList.size () == 0 ) {
83
20
return false ;
84
21
}
@@ -129,7 +66,7 @@ bool BoardConfigurationProtocol::getReceivedMsg(ProvisioningInputMessage &msg) {
129
66
return true ;
130
67
}
131
68
132
- bool BoardConfigurationProtocol::sendMsg (ProvisioningOutputMessage &msg) {
69
+ bool BoardConfigurationProtocol::sendNewMsg (ProvisioningOutputMessage &msg) {
133
70
bool res = false ;
134
71
switch (msg.type ) {
135
72
case MessageOutputType::STATUS:
@@ -153,16 +90,85 @@ bool BoardConfigurationProtocol::sendMsg(ProvisioningOutputMessage &msg) {
153
90
return res;
154
91
}
155
92
156
- bool BoardConfigurationProtocol::receivedMsgAvailable () {
93
+ bool BoardConfigurationProtocol::msgAvailable () {
157
94
return _inputMessagesList.size () > 0 ;
158
95
}
159
96
97
+ /* *****************************************************************************
98
+ * PROTECTED MEMBER FUNCTIONS
99
+ ******************************************************************************/
100
+ BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::sendAndReceive () {
101
+ TransmissionResult transmissionRes = TransmissionResult::NOT_COMPLETED;
102
+ if (!isPeerConnected ()) {
103
+ return TransmissionResult::PEER_NOT_AVAILABLE;
104
+ }
105
+
106
+ if (hasReceivedBytes ()) {
107
+ int receivedDataLen = receivedBytes ();
108
+
109
+ PacketManager::ReceivingState res;
110
+ PacketManager::ReceivedData receivedData;
111
+
112
+ for (int i = 0 ; i < receivedDataLen; i++) {
113
+ uint8_t val = readByte ();
114
+ res = Packet.handleReceivedByte (receivedData, val);
115
+ if (res == PacketManager::ReceivingState::ERROR) {
116
+ DEBUG_DEBUG (" BoardConfigurationProtocol::%s Error receiving packet" , __FUNCTION__);
117
+ sendNak ();
118
+ transmissionRes = TransmissionResult::INVALID_DATA;
119
+ break ;
120
+ } else if (res == PacketManager::ReceivingState::RECEIVED) {
121
+ switch (receivedData.type ) {
122
+ case PacketManager::MessageType::DATA:
123
+ {
124
+ DEBUG_DEBUG (" BoardConfigurationProtocol::%s Received data packet" , __FUNCTION__);
125
+ printPacket (" payload" , &receivedData.payload [0 ], receivedData.payload .len ());
126
+ _inputMessagesList.push_back (receivedData.payload );
127
+ // Consider all sent data as received
128
+ _outputMessagesList.clear ();
129
+ transmissionRes = TransmissionResult::DATA_RECEIVED;
130
+ }
131
+ break ;
132
+ case PacketManager::MessageType::TRANSMISSION_CONTROL:
133
+ {
134
+ if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x03 ) {
135
+
136
+ DEBUG_DEBUG (" BoardConfigurationProtocol::%s Received NACK packet" , __FUNCTION__);
137
+ for (std::list<OutputPacketBuffer>::iterator packet = _outputMessagesList.begin (); packet != _outputMessagesList.end (); ++packet) {
138
+ packet->startProgress ();
139
+ }
140
+ } else if (receivedData.payload .len () == 1 && receivedData.payload [0 ] == 0x02 ) {
141
+ DEBUG_DEBUG (" BoardConfigurationProtocol::%s Received disconnect request" , __FUNCTION__);
142
+ handleDisconnectRequest ();
143
+ }
144
+ }
145
+ break ;
146
+ default :
147
+ break ;
148
+ }
149
+ }
150
+ }
151
+ }
152
+
153
+ if (_outputMessagesList.size () > 0 ) {
154
+ checkOutputPacketValidity ();
155
+ transmitStream ();
156
+ }
157
+
158
+ return transmissionRes;
159
+ }
160
+
161
+ bool BoardConfigurationProtocol::sendNak () {
162
+ uint8_t data = 0x03 ;
163
+ return sendData (PacketManager::MessageType::TRANSMISSION_CONTROL, &data, sizeof (data));
164
+ }
165
+
160
166
bool BoardConfigurationProtocol::sendData (PacketManager::MessageType type, const uint8_t *data, size_t len) {
161
167
OutputPacketBuffer outputMsg;
162
- outputMsg.setValidityTs (millis () + PACKET_VALIDITY_S );
168
+ outputMsg.setValidityTs (millis () + PACKET_VALIDITY_MS );
163
169
164
- if (!Packet. createPacket (outputMsg, type, data, len)) {
165
- DEBUG_WARNING (" BLEConfiguratorAgent ::%s Failed to create packet" , __FUNCTION__);
170
+ if (!PacketManager:: createPacket (outputMsg, type, data, len)) {
171
+ DEBUG_WARNING (" BoardConfigurationProtocol ::%s Failed to create packet" , __FUNCTION__);
166
172
return false ;
167
173
}
168
174
@@ -181,6 +187,27 @@ bool BoardConfigurationProtocol::sendData(PacketManager::MessageType type, const
181
187
return true ;
182
188
}
183
189
190
+ void BoardConfigurationProtocol::clear () {
191
+ Packet.clear ();
192
+ _outputMessagesList.clear ();
193
+ _inputMessagesList.clear ();
194
+ }
195
+
196
+ void BoardConfigurationProtocol::checkOutputPacketValidity () {
197
+ if (_outputMessagesList.size () == 0 ) {
198
+ return ;
199
+ }
200
+ _outputMessagesList.remove_if ([](OutputPacketBuffer &packet) {
201
+ if (packet.getValidityTs () != 0 && packet.getValidityTs () < millis ()) {
202
+ return true ;
203
+ }
204
+ return false ;
205
+ });
206
+ }
207
+
208
+ /* *****************************************************************************
209
+ * PRIVATE MEMBER FUNCTIONS
210
+ ******************************************************************************/
184
211
bool BoardConfigurationProtocol::sendStatus (StatusMessage msg) {
185
212
bool res = false ;
186
213
size_t len = CBOR_DATA_STATUS_LEN;
@@ -328,24 +355,6 @@ BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::trans
328
355
return res;
329
356
}
330
357
331
- void BoardConfigurationProtocol::clear () {
332
- Packet.clear ();
333
- _outputMessagesList.clear ();
334
- _inputMessagesList.clear ();
335
- }
336
-
337
- void BoardConfigurationProtocol::checkOutputPacketValidity () {
338
- if (_outputMessagesList.size () == 0 ) {
339
- return ;
340
- }
341
- _outputMessagesList.remove_if ([](OutputPacketBuffer &packet) {
342
- if (packet.getValidityTs () != 0 && packet.getValidityTs () < millis ()) {
343
- return true ;
344
- }
345
- return false ;
346
- });
347
- }
348
-
349
358
void BoardConfigurationProtocol::printPacket (const char *label, const uint8_t *data, size_t len) {
350
359
if (Debug.getDebugLevel () == DBG_VERBOSE) {
351
360
DEBUG_VERBOSE (" Print %s data:" , label);
0 commit comments