Skip to content

Commit dd5718f

Browse files
committed
refactor AgentsConfiguratorManager sendMsg
1 parent 8a0de0c commit dd5718f

File tree

8 files changed

+112
-109
lines changed

8 files changed

+112
-109
lines changed

examples/auto-retry/provisioning.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "provisioning.h"
22
#include "ConfiguratorAgents/MessagesDefinitions.h"
3+
const char * UHWID = "identifier1212123210111213141516";
4+
const char * TOKEN = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjODcxODE3ZTRkN2VhY2QxOGI5NGYwYWE4YjAwNDZkZDE5Zjg0MGEwMDgzMzZiNTA5YWJmOTkwYjQ3ZTNiMTg3IiwiaWF0IjoxNzI4NDc1MTA4LCJleHAiOjE3Mjg0Nzg3MDh9.-f_UodDftALaC27UXj3lmp8SCWXCpHLBYl70Pl6DbHzb4lc-GqLYt2I_g_pquiNPym2YApUPL_7yqO-NfWuQHQ";
35
#if defined(ARDUINO_OPTA) || defined(ARDUINO_PORTENTA_H7_M7)
46
#include "mbed.h"
57
#include "mbed_mem_trace.h"
@@ -70,12 +72,21 @@ bool Provisioning::poll() {
7072
_reqReceived = false;
7173

7274
if (_ts != 0) {
73-
74-
_agentManager->setID("identifier1212123210111213141516", "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjODcxODE3ZTRkN2VhY2QxOGI5NGYwYWE4YjAwNDZkZDE5Zjg0MGEwMDgzMzZiNTA5YWJmOTkwYjQ3ZTNiMTg3IiwiaWF0IjoxNzI4NDc1MTA4LCJleHAiOjE3Mjg0Nzg3MDh9.-f_UodDftALaC27UXj3lmp8SCWXCpHLBYl70Pl6DbHzb4lc-GqLYt2I_g_pquiNPym2YApUPL_7yqO-NfWuQHQ");
75+
//Send UHWID
76+
ProvisioningOutputMessage idMsg;
77+
idMsg.type = MessageOutputType::UHWID;
78+
idMsg.m.uhwid = UHWID;
79+
_agentManager->sendMsg(idMsg);
80+
//Send JWT
81+
ProvisioningOutputMessage jwtMsg;
82+
jwtMsg.type = MessageOutputType::JWT;
83+
jwtMsg.m.jwt = TOKEN;
84+
_agentManager->sendMsg(jwtMsg);
7585
_reqCompleted = true;
7686
} else {
7787
Serial.println("Error: timestamp not provided");
78-
_agentManager->setStatusMessage(MessageTypeCodes::PARAMS_NOT_FOUND);
88+
ProvisioningOutputMessage msg = { MessageOutputType::STATUS, { MessageTypeCodes::ERROR } };
89+
_agentManager->sendMsg(msg);
7990
}
8091
}
8192
return _reqCompleted;

examples/provisioning/ClaimingHandler.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ void ClaimingHandlerClass::poll() {
5050
if(_state == ClaimingHandlerStates::END) {
5151
return;
5252
}
53-
5453
_agentManager->poll();
5554

5655
switch (_receivedEvent) {
@@ -67,7 +66,7 @@ void ClaimingHandlerClass::getIdReqHandler() {
6766
Serial.println(*_uhwid);
6867
if (*_uhwid == "") {
6968
DEBUG_ERROR("ClaimingHandlerClass::%s Error: UHWID not found", __FUNCTION__);
70-
_agentManager->setStatusMessage(MessageTypeCodes::ERROR);
69+
sendStatus(MessageTypeCodes::ERROR);
7170
return;
7271
}
7372

@@ -76,31 +75,38 @@ void ClaimingHandlerClass::getIdReqHandler() {
7675
Serial.println(token);
7776
if (token == "") {
7877
DEBUG_ERROR("ClaimingHandlerClass::%s Error: token not created", __FUNCTION__);
79-
_agentManager->setStatusMessage(MessageTypeCodes::ERROR);
78+
sendStatus(MessageTypeCodes::ERROR);
8079
return;
8180
}
8281
byte _uhwidBytes[33];
8382
hexStringToBytes(*_uhwid, _uhwidBytes, _uhwid->length());
8483
_uhwidBytes[32] = '\0';
85-
_agentManager->setID((char *)_uhwidBytes, token);
84+
//Send UHWID
85+
ProvisioningOutputMessage idMsg = {MessageOutputType::UHWID};
86+
idMsg.m.uhwid = (char *)_uhwidBytes;
87+
_agentManager->sendMsg(idMsg);
88+
//Send JWT
89+
ProvisioningOutputMessage jwtMsg = {MessageOutputType::JWT};
90+
jwtMsg.m.jwt = token.c_str();
91+
_agentManager->sendMsg(jwtMsg);
8692
_ts = 0;
8793
} else {
8894
DEBUG_DEBUG("ClaimingHandlerClass::%s Error: timestamp not provided" , __FUNCTION__);
89-
_agentManager->setStatusMessage(MessageTypeCodes::PARAMS_NOT_FOUND);
95+
sendStatus(MessageTypeCodes::PARAMS_NOT_FOUND);
9096
}
9197
}
9298

9399
void ClaimingHandlerClass::resetStoredCredReqHandler() {
94100
if( _clearStoredCredentials == nullptr) {
95-
_agentManager->setStatusMessage(MessageTypeCodes::ERROR);
101+
sendStatus(MessageTypeCodes::ERROR);
96102
return;
97103
}
98104

99105
if( !_clearStoredCredentials()){
100106
DEBUG_ERROR("ClaimingHandlerClass::%s Error: reset stored credentials failed", __FUNCTION__);
101-
_agentManager->setStatusMessage(MessageTypeCodes::ERROR);
107+
sendStatus(MessageTypeCodes::ERROR);
102108
} else {
103-
_agentManager->setStatusMessage(MessageTypeCodes::RESET_COMPLETED);
109+
sendStatus(MessageTypeCodes::RESET_COMPLETED);
104110
}
105111

106112
}
@@ -116,3 +122,8 @@ void ClaimingHandlerClass::resetStoredCredRequestCb() {
116122
DEBUG_DEBUG("ClaimingHandlerClass::%s Reset stored credentials request received", __FUNCTION__);
117123
_receivedEvent = ClaimingReqEvents::RESET;
118124
}
125+
126+
bool ClaimingHandlerClass::sendStatus(StatusMessage msg) {
127+
ProvisioningOutputMessage statusMsg = { MessageOutputType::STATUS, { msg } };
128+
return _agentManager->sendMsg(statusMsg);
129+
}

examples/provisioning/ClaimingHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ClaimingHandlerClass {
3535
ClearStoredCredentialsHandler _clearStoredCredentials = nullptr;
3636
void getIdReqHandler();
3737
void resetStoredCredReqHandler();
38+
bool sendStatus(StatusMessage msg);
3839
static void getIdRequestCb();
3940
static void setTimestamp(uint64_t ts);
4041
static void resetStoredCredRequestCb();

src/ConfiguratorAgents/AgentsConfiguratorManager.cpp

Lines changed: 46 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -106,72 +106,55 @@ void AgentsConfiguratorManager::disconnect() {
106106
}
107107
}
108108

109-
bool AgentsConfiguratorManager::setStatusMessage(StatusMessage msg) {
110-
if ((int)msg < 0) {
111-
if (_statusRequest.pending) {
112-
_statusRequest.reset();
113-
}
114-
} else if (msg == MessageTypeCodes::CONNECTED) {
115-
if (_statusRequest.pending && _statusRequest.key == RequestType::CONNECT) {
116-
_statusRequest.reset();
117-
}
118-
} else if ((msg == MessageTypeCodes::SCANNING || msg == MessageTypeCodes::CONNECTING) && _state != AgentsConfiguratorManagerStates::CONFIG_IN_PROGRESS) {
119-
return true;
120-
} else if (msg == MessageTypeCodes::RESET_COMPLETED) {
121-
if (_statusRequest.pending && _statusRequest.key == RequestType::RESET) {
122-
_statusRequest.reset();
123-
}
124-
}
125-
126-
if (_selectedAgent) {
127-
if (!sendStatus(msg)) {
128-
DEBUG_WARNING("AgentsConfiguratorManager::%s failed to send status to the peer", __FUNCTION__);
129-
}
130-
} else {
131-
_initStatusMsg = msg;
132-
}
109+
bool AgentsConfiguratorManager::sendMsg(ProvisioningOutputMessage &msg) {
133110

134-
return true;
135-
}
136-
137-
bool AgentsConfiguratorManager::setNetworkOptions(NetworkOptions netOptions) {
138-
memcpy(&_netOptions, &netOptions, sizeof(NetworkOptions));
139-
if (_statusRequest.pending && _statusRequest.key == RequestType::SCAN) {
140-
_statusRequest.reset();
111+
switch (msg.type) {
112+
case MessageOutputType::STATUS:
113+
{
114+
if ((int)msg.m.status < 0) {
115+
if (_statusRequest.pending) {
116+
_statusRequest.reset();
117+
}
118+
_initStatusMsg = msg.m.status;
119+
} else if (msg.m.status == MessageTypeCodes::CONNECTED) {
120+
if (_statusRequest.pending && _statusRequest.key == RequestType::CONNECT) {
121+
_statusRequest.reset();
122+
}
123+
_initStatusMsg = msg.m.status;
124+
} else if (msg.m.status == MessageTypeCodes::RESET_COMPLETED) {
125+
if (_statusRequest.pending && _statusRequest.key == RequestType::RESET) {
126+
_statusRequest.reset();
127+
}
128+
}
129+
}
130+
break;
131+
case MessageOutputType::NETWORK_OPTIONS:
132+
{
133+
memcpy(&_netOptions, msg.m.netOptions, sizeof(NetworkOptions));
134+
if (_statusRequest.pending && _statusRequest.key == RequestType::SCAN) {
135+
_statusRequest.reset();
136+
}
137+
}
138+
break;
139+
case MessageOutputType::UHWID:
140+
case MessageOutputType::JWT:
141+
{
142+
_statusRequest.completion++;
143+
if (_statusRequest.pending && _statusRequest.key == RequestType::GET_ID && _statusRequest.completion == 2) {
144+
_statusRequest.reset();
145+
}
146+
}
147+
break;
148+
default:
149+
break;
141150
}
142151

143-
if (_state == AgentsConfiguratorManagerStates::CONFIG_IN_PROGRESS) {
144-
//Send immediately if agent is connected with a peer and configuration is in progress
145-
sendNetworkOptions();
152+
if(_state == AgentsConfiguratorManagerStates::CONFIG_IN_PROGRESS) {
153+
return _selectedAgent->sendMsg(msg);
146154
}
147-
148155
return true;
149156
}
150157

151-
bool AgentsConfiguratorManager::setID(String uhwid, String jwt) {
152-
bool res = false;
153-
if (_statusRequest.pending && _statusRequest.key == RequestType::GET_ID) {
154-
if (_selectedAgent) {
155-
ProvisioningOutputMessage msg;
156-
msg.type = MessageOutputType::UHWID;
157-
msg.m.uhwid = uhwid.c_str();
158-
res = _selectedAgent->sendMsg(msg);
159-
if (!res) {
160-
return res;
161-
}
162-
163-
msg.type = MessageOutputType::JWT;
164-
msg.m.jwt = jwt.c_str();
165-
res = _selectedAgent->sendMsg(msg);
166-
if (!res) {
167-
return res;
168-
}
169-
_statusRequest.reset();
170-
}
171-
}
172-
return res;
173-
}
174-
175158
bool AgentsConfiguratorManager::addRequestHandler(RequestType type, ConfiguratorRequestHandler callback) {
176159
bool success = false;
177160
if (_reqHandlers[(int)type - 1] == nullptr) {
@@ -241,7 +224,9 @@ AgentsConfiguratorManagerStates AgentsConfiguratorManager::handleSendInitialStat
241224

242225
AgentsConfiguratorManagerStates AgentsConfiguratorManager::handleSendNetworkOptions() {
243226
AgentsConfiguratorManagerStates nextState = _state;
244-
if (sendNetworkOptions()) {
227+
ProvisioningOutputMessage networkOptionMsg = { MessageOutputType::NETWORK_OPTIONS };
228+
networkOptionMsg.m.netOptions = &_netOptions;
229+
if (_selectedAgent->sendMsg(networkOptionMsg)) {
245230
nextState = AgentsConfiguratorManagerStates::CONFIG_IN_PROGRESS;
246231
}
247232
return nextState;
@@ -373,13 +358,6 @@ void AgentsConfiguratorManager::handleGetBleMacAddressCommand() {
373358

374359
}
375360

376-
bool AgentsConfiguratorManager::sendNetworkOptions() {
377-
ProvisioningOutputMessage outputMsg;
378-
outputMsg.type = MessageOutputType::NETWORK_OPTIONS;
379-
outputMsg.m.netOptions = &_netOptions;
380-
return _selectedAgent->sendMsg(outputMsg);
381-
}
382-
383361
void AgentsConfiguratorManager::handleResetCommand() {
384362
if (_statusRequest.pending) {
385363
DEBUG_DEBUG("AgentsConfiguratorManager::%s received a GetUnique request while executing another request", __FUNCTION__);
@@ -393,9 +371,7 @@ void AgentsConfiguratorManager::handleResetCommand() {
393371
}
394372

395373
bool AgentsConfiguratorManager::sendStatus(StatusMessage msg) {
396-
ProvisioningOutputMessage outputMsg;
397-
outputMsg.type = MessageOutputType::STATUS;
398-
outputMsg.m.status = msg;
374+
ProvisioningOutputMessage outputMsg = { MessageOutputType::STATUS, { msg } };
399375
return _selectedAgent->sendMsg(outputMsg);
400376
}
401377

src/ConfiguratorAgents/AgentsConfiguratorManager.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef void (*ReturnNetworkSettings)(models::NetworkSetting *netSetting);
2525
enum class RequestType { NONE,
2626
CONNECT,
2727
SCAN,
28-
GET_ID,
28+
GET_ID,
2929
RESET };
3030

3131
class AgentsConfiguratorManager {
@@ -40,9 +40,8 @@ class AgentsConfiguratorManager {
4040
bool isBLEAgentEnabled() {
4141
return _bleAgentEnabled;
4242
};
43-
bool setStatusMessage(StatusMessage msg);
44-
bool setNetworkOptions(NetworkOptions netOptions);
45-
bool setID(String uhwid, String jwt);
43+
44+
bool sendMsg(ProvisioningOutputMessage &msg);
4645
bool addAgent(ConfiguratorAgent &agent);
4746
bool addRequestHandler(RequestType type, ConfiguratorRequestHandler callback);
4847
void removeRequestHandler(RequestType type) {
@@ -75,12 +74,14 @@ class AgentsConfiguratorManager {
7574
void reset() {
7675
pending = false;
7776
key = RequestType::NONE;
77+
completion = 0;
7878
};
79+
uint8_t completion;
7980
bool pending;
8081
RequestType key;
8182
} StatusRequest;
8283

83-
StatusRequest _statusRequest = { .pending = false, .key = RequestType::NONE };
84+
StatusRequest _statusRequest = { .completion = 0 , .pending = false, .key = RequestType::NONE};
8485

8586
AgentsConfiguratorManagerStates handleInit();
8687
AgentsConfiguratorManagerStates handleSendInitialStatus();
@@ -93,7 +94,6 @@ class AgentsConfiguratorManager {
9394
void handleGetIDCommand();
9495
void handleGetBleMacAddressCommand();
9596
void handleResetCommand();
96-
bool sendNetworkOptions();
9797
bool sendStatus(StatusMessage msg);
9898

9999
AgentsConfiguratorManagerStates handlePeerDisconnected();

src/ConfiguratorAgents/MessagesDefinitions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum class MessageTypeCodes {
3131
HW_ERROR_CONN_MODULE = -101,
3232
WIFI_IDLE = -102,
3333
WIFI_STOPPED = -103,
34-
ERROR_STORAGE_BEGIN = -200,
34+
ERROR_STORAGE_BEGIN = -200,
3535
ERROR = -255
3636
};
3737

@@ -41,7 +41,7 @@ enum class RemoteCommands { CONNECT = 1,
4141
GET_ID = 2,
4242
GET_BLE_MAC_ADDRESS = 3,
4343
RESET = 4,
44-
SCAN = 100
44+
SCAN = 100
4545
};
4646

4747
enum class MessageOutputType { STATUS,

0 commit comments

Comments
 (0)