Skip to content

Commit d6c791c

Browse files
committed
add wifi fw version command (#35)
1 parent 10b2cdf commit d6c791c

13 files changed

+155
-8
lines changed

extras/test/src/test_provisioning_command_encode.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,30 @@
207207
}
208208
}
209209

210+
WHEN("Encode a message with provisioning wifi fw version ")
211+
{
212+
WiFiFWVersionProvisioningMessage command;
213+
command.c.id = ProvisioningMessageId::WiFiFWVersionProvisioningMessageId;
214+
command.wifiFwVersion = "1.6.0";
215+
uint8_t buffer[512];
216+
size_t bytes_encoded = sizeof(buffer);
217+
218+
CBORMessageEncoder encoder;
219+
Encoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded);
220+
221+
uint8_t expected_result[] = {
222+
0xda, 0x00, 0x01, 0x20, 0x14, 0x81, 0x65, 0x31, 0x2E, 0x36, 0x2E, 0x30
223+
};
224+
225+
// Test the encoding is
226+
//DA 00012014 # tag(73748)
227+
// 81 # array(1)
228+
// 65 # text(5)
229+
// 312E362E30 # "1.6.0"
230+
THEN("The encoding is successful") {
231+
REQUIRE(err == Encoder::Status::Complete);
232+
REQUIRE(bytes_encoded == sizeof(expected_result));
233+
REQUIRE(memcmp(buffer, expected_result, sizeof(expected_result)) == 0);
234+
}
235+
}
210236
}

src/ConfiguratorAgents/AgentsManager.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ bool AgentsManagerClass::sendMsg(ProvisioningOutputMessage &msg) {
150150
}
151151
}
152152
break;
153+
case MessageOutputType::WIFI_FW_VERSION:
154+
{
155+
if (_statusRequest.pending && _statusRequest.key == RequestType::GET_WIFI_FW_VERSION) {
156+
_statusRequest.reset();
157+
}
158+
}
159+
break;
153160
default:
154161
break;
155162
}
@@ -265,6 +272,7 @@ void AgentsManagerClass::handleReceivedCommands(RemoteCommands cmd) {
265272
case RemoteCommands::GET_ID: handleGetIDCommand (); break;
266273
case RemoteCommands::GET_BLE_MAC_ADDRESS: handleGetBleMacAddressCommand(); break;
267274
case RemoteCommands::RESET: handleResetCommand (); break;
275+
case RemoteCommands::GET_WIFI_FW_VERSION: handleGetWiFiFWVersionCommand(); break;
268276
}
269277
}
270278

@@ -381,6 +389,18 @@ void AgentsManagerClass::handleResetCommand() {
381389
callHandler(RequestType::RESET);
382390
}
383391

392+
void AgentsManagerClass::handleGetWiFiFWVersionCommand() {
393+
if (_statusRequest.pending) {
394+
DEBUG_DEBUG("AgentsManagerClass::%s received a GetWiFiFWVersion request while executing another request", __FUNCTION__);
395+
sendStatus(StatusMessage::OTHER_REQUEST_IN_EXECUTION);
396+
return;
397+
}
398+
399+
_statusRequest.pending = true;
400+
_statusRequest.key = RequestType::GET_WIFI_FW_VERSION;
401+
callHandler(RequestType::GET_WIFI_FW_VERSION);
402+
}
403+
384404
bool AgentsManagerClass::sendStatus(StatusMessage msg) {
385405
ProvisioningOutputMessage outputMsg = { MessageOutputType::STATUS, { msg } };
386406
return _selectedAgent->sendMsg(outputMsg);

src/ConfiguratorAgents/AgentsManager.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ enum class RequestType { NONE,
2626
CONNECT,
2727
SCAN,
2828
GET_ID,
29-
RESET };
29+
RESET,
30+
GET_WIFI_FW_VERSION}; //TODO fix when rebasing
3031

3132
class AgentsManagerClass {
3233
public:
@@ -62,7 +63,7 @@ class AgentsManagerClass {
6263
AgentsManagerStates _state = AgentsManagerStates::END;
6364
std::list<ConfiguratorAgent *> _agentsList;
6465
std::list<uint8_t> _servicesList;
65-
ConfiguratorRequestHandler _reqHandlers[4];
66+
ConfiguratorRequestHandler _reqHandlers[5]; //TODO fix when rebasing
6667
ReturnTimestamp _returnTimestampCb = nullptr;
6768
ReturnNetworkSettings _returnNetworkSettingsCb = nullptr;
6869
ConfiguratorAgent *_selectedAgent = nullptr;
@@ -94,6 +95,7 @@ class AgentsManagerClass {
9495
void handleGetIDCommand();
9596
void handleGetBleMacAddressCommand();
9697
void handleResetCommand();
98+
void handleGetWiFiFWVersionCommand();
9799
bool sendStatus(StatusMessage msg);
98100

99101
AgentsManagerStates handlePeerDisconnected();

src/ConfiguratorAgents/MessagesDefinitions.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ enum class RemoteCommands { CONNECT = 1,
3939
GET_ID = 2,
4040
GET_BLE_MAC_ADDRESS = 3,
4141
RESET = 4,
42-
SCAN = 100
42+
SCAN = 100,
43+
GET_WIFI_FW_VERSION = 101
4344
};
4445

4546
enum class MessageOutputType { STATUS,
4647
NETWORK_OPTIONS,
4748
UHWID,
4849
JWT,
49-
BLE_MAC_ADDRESS
50+
BLE_MAC_ADDRESS,
51+
WIFI_FW_VERSION
5052
};
5153

5254
enum class MessageInputType {
@@ -63,6 +65,7 @@ struct ProvisioningOutputMessage {
6365
const byte *uhwid; // Must be a pointer to a byte array of MAX_UHWID_SIZE
6466
const char *jwt;
6567
const uint8_t *BLEMacAddress;
68+
const char *wifiFwVersion;
6669
} m;
6770
};
6871

src/ConfiguratorAgents/agents/BoardConfigurationProtocol/BoardConfigurationProtocol.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ bool BoardConfigurationProtocol::sendNewMsg(ProvisioningOutputMessage &msg) {
6262
break;
6363
case MessageOutputType::BLE_MAC_ADDRESS:
6464
res = sendBleMacAddress(msg.m.BLEMacAddress, BLE_MAC_ADDRESS_SIZE);
65+
break;
66+
case MessageOutputType::WIFI_FW_VERSION:
67+
res = sendWifiFWVersion(msg.m.wifiFwVersion);
68+
break;
6569
default:
6670
break;
6771
}
@@ -308,6 +312,27 @@ bool BoardConfigurationProtocol::sendBleMacAddress(const uint8_t *mac, size_t le
308312
return res;
309313
}
310314

315+
bool BoardConfigurationProtocol::sendWifiFWVersion(const char *wifiFWVersion) {
316+
bool res = false;
317+
318+
size_t cborDataLen = CBOR_MIN_WIFI_FW_VERSION_LEN + strlen(wifiFWVersion);
319+
uint8_t data[cborDataLen];
320+
321+
res = CBORAdapter::wifiFWVersionToCBOR(wifiFWVersion, data, &cborDataLen);
322+
if (!res) {
323+
DEBUG_ERROR("BoardConfigurationProtocol::%s failed to convert WiFi FW version to CBOR", __FUNCTION__);
324+
return res;
325+
}
326+
327+
res = sendData(PacketManager::MessageType::DATA, data, cborDataLen);
328+
if (!res) {
329+
DEBUG_ERROR("BoardConfigurationProtocol::%s failed to send WiFi FW version", __FUNCTION__);
330+
return res;
331+
}
332+
333+
return res;
334+
}
335+
311336
BoardConfigurationProtocol::TransmissionResult BoardConfigurationProtocol::transmitStream() {
312337
if (!isPeerConnected()) {
313338
return TransmissionResult::PEER_NOT_AVAILABLE;

src/ConfiguratorAgents/agents/BoardConfigurationProtocol/BoardConfigurationProtocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class BoardConfigurationProtocol {
4444
bool sendUhwid(const byte *uhwid);
4545
bool sendJwt(const char *jwt, size_t len);
4646
bool sendBleMacAddress(const uint8_t *mac, size_t len);
47+
bool sendWifiFWVersion(const char *wifiFWVersion);
4748
TransmissionResult transmitStream();
4849
void printPacket(const char *label, const uint8_t *data, size_t len);
4950
std::list<OutputPacketBuffer> _outputMessagesList;

src/ConfiguratorAgents/agents/BoardConfigurationProtocol/CBORAdapter.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ bool CBORAdapter::statusToCBOR(StatusMessage msg, uint8_t *data, size_t *len) {
8282
return result;
8383
}
8484

85+
bool CBORAdapter::wifiFWVersionToCBOR(const char *wifiFWVersion, uint8_t *data, size_t *len) {
86+
CBORMessageEncoder encoder;
87+
if(*len < CBOR_MIN_WIFI_FW_VERSION_LEN + strlen(wifiFWVersion)) {
88+
return false;
89+
}
90+
WiFiFWVersionProvisioningMessage wifiFWVersionMsg;
91+
wifiFWVersionMsg.c.id = ProvisioningMessageId::WiFiFWVersionProvisioningMessageId;
92+
wifiFWVersionMsg.wifiFwVersion = wifiFWVersion;
93+
94+
Encoder::Status status = encoder.encode((Message *)&wifiFWVersionMsg, data, *len);
95+
96+
return status == Encoder::Status::Complete ? true : false;
97+
}
98+
8599
bool CBORAdapter::networkOptionsToCBOR(const NetworkOptions *netOptions, uint8_t *data, size_t *len) {
86100
bool result = false;
87101
switch (netOptions->type) {

src/ConfiguratorAgents/agents/BoardConfigurationProtocol/CBORAdapter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
#define CBOR_DATA_JWT_LEN MAX_JWT_SIZE + 3 + CBOR_DATA_HEADER_LEN //Signature size + 2 bytes for CBOR array of bytes identifier + CBOR header size
1919
#define CBOR_DATA_STATUS_LEN 4 + CBOR_DATA_HEADER_LEN
2020
#define CBOR_DATA_BLE_MAC_LEN BLE_MAC_ADDRESS_SIZE + 2 + CBOR_DATA_HEADER_LEN
21-
21+
#define CBOR_MIN_WIFI_FW_VERSION_LEN CBOR_DATA_HEADER_LEN + 1 // CBOR_DATA_HEADER_LEN + 1 byte for the length of the string
2222

2323
class CBORAdapter {
2424
public:
2525
static bool uhwidToCBOR(const byte *uhwid, uint8_t *data, size_t *len);
2626
static bool jwtToCBOR(const char *jwt, uint8_t *data, size_t *len);
2727
static bool BLEMacAddressToCBOR(const uint8_t *mac, uint8_t *data, size_t *len);
28+
static bool wifiFWVersionToCBOR(const char *wifiFWVersion, uint8_t *data, size_t *len);
2829
static bool statusToCBOR(StatusMessage msg, uint8_t *data, size_t *len);
2930
static bool networkOptionsToCBOR(const NetworkOptions *netOptions, uint8_t *data, size_t *len);
3031
static bool getMsgFromCBOR(const uint8_t *data, size_t len, ProvisioningMessageDown *msg);

src/ConfiguratorAgents/agents/BoardConfigurationProtocol/cbor/Encoder.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,28 @@ Encoder::Status BLEMacAddressProvisioningMessageEncoder::encode(CborEncoder* enc
114114
return Encoder::Status::Complete;
115115
}
116116

117+
Encoder::Status WiFiFWVersionProvisioningMessageEncoder::encode(CborEncoder* encoder, Message *msg) {
118+
WiFiFWVersionProvisioningMessage * provisioningWiFiFWVersion = (WiFiFWVersionProvisioningMessage*) msg;
119+
CborEncoder array_encoder;
120+
121+
if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) {
122+
return Encoder::Status::Error;
123+
}
124+
125+
if(cbor_encode_text_stringz(&array_encoder, provisioningWiFiFWVersion->wifiFwVersion) != CborNoError) {
126+
return Encoder::Status::Error;
127+
}
128+
129+
if(cbor_encoder_close_container(encoder, &array_encoder) != CborNoError) {
130+
return Encoder::Status::Error;
131+
}
132+
133+
return Encoder::Status::Complete;
134+
}
135+
117136
static StatusProvisioningMessageEncoder statusProvisioningMessageEncoder;
118137
static ListWifiNetworksProvisioningMessageEncoder listWifiNetworksProvisioningMessageEncoder;
119138
static UniqueHardwareIdProvisioningMessageEncoder uniqueHardwareIdProvisioningMessageEncoder;
120139
static JWTProvisioningMessageEncoder jWTProvisioningMessageEncoder;
121140
static BLEMacAddressProvisioningMessageEncoder bLEMacAddressProvisioningMessageEncoder;
141+
static WiFiFWVersionProvisioningMessageEncoder wiFiFWVersionProvisioningMessageEncoder;

src/ConfiguratorAgents/agents/BoardConfigurationProtocol/cbor/Encoder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ class BLEMacAddressProvisioningMessageEncoder: public CBORMessageEncoderInterfac
5252
protected:
5353
Encoder::Status encode(CborEncoder* encoder, Message *msg) override;
5454
};
55+
56+
class WiFiFWVersionProvisioningMessageEncoder: public CBORMessageEncoderInterface {
57+
public:
58+
WiFiFWVersionProvisioningMessageEncoder()
59+
: CBORMessageEncoderInterface(CBORWiFiFWVersionProvisioningMessage, WiFiFWVersionProvisioningMessageId) {}
60+
protected:
61+
Encoder::Status encode(CborEncoder* encoder, Message *msg) override;
62+
};

0 commit comments

Comments
 (0)