Skip to content

Commit f21b467

Browse files
committed
Storage and Memory optimization (#33)
* wrap BLE enable/disable of AgentsManager * optimize storage and mem consumption for final cloud sketch * make AgentManager as singleton move cbor encoders and decoders obj instance to include file to save 2kB of flash mem
1 parent b2464c0 commit f21b467

25 files changed

+669
-613
lines changed

examples/auto-retry/auto-retry.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ void setup() {
7979

8080
void loop() {
8181
if (provisioningCompleted && networkConfigured) {
82-
AgentsManager.disconnect();
83-
if (AgentsManager.isBLEAgentEnabled()) {
84-
AgentsManager.enableBLEAgent(false);
82+
NetworkConfigurator.configurationCompleted();
83+
if (NetworkConfigurator.isBLEenabled()) {
84+
NetworkConfigurator.enableBLE(false);
8585
}
8686
}
8787
if (provisioningCompleted == false && ProvisioningSystem.poll()) {
@@ -109,8 +109,8 @@ void loop() {
109109
if (digitalRead(RESETCRED_BUTTON) == HIGH) {
110110
#endif
111111
Serial.println("Update config");
112-
if (!AgentsManager.isBLEAgentEnabled()) {
113-
AgentsManager.enableBLEAgent(true);
112+
if (!NetworkConfigurator.isBLEenabled()) {
113+
NetworkConfigurator.enableBLE(true);
114114
}
115115

116116
networkConfigured = false;

examples/auto-retry/provisioning.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ void display_freeram(){
4343
Serial.println(freeRam());
4444
}
4545
#define PROVISIONING_SERVICEID_FOR_AGENTMANAGER 0xB1
46-
Provisioning::Provisioning(AgentsManagerClass &agc)
47-
: _agentManager{ &agc } {
46+
Provisioning::Provisioning() {
47+
_agentManager = &AgentsManagerClass::getInstance();
4848
}
4949

5050
void Provisioning::begin() {
@@ -100,4 +100,4 @@ void Provisioning::setTimestamp(uint64_t ts) {
100100
_ts = ts;
101101
Serial.print("received TS: ");
102102
Serial.println(_ts);
103-
}
103+
}

examples/auto-retry/provisioning.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Provisioning {
66
public:
7-
Provisioning(AgentsManagerClass &agc);
7+
Provisioning();
88
void begin();
99
void end();
1010
bool poll();
@@ -16,4 +16,4 @@ class Provisioning {
1616
static inline bool _reqReceived = false;
1717
static void getIdRequestHandler();
1818
static void setTimestamp(uint64_t ts);
19-
};
19+
};

examples/auto-retry/thingProperties.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <ArduinoIoTCloud.h>
44
#include <GenericConnectionHandler.h>
55
#include "NetworkConfigurator.h"
6-
#include "ConfiguratorAgents/AgentsManager.h"
76
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
87
#include "ConfiguratorAgents/agents/BLE/BLEAgent.h"
98
#endif
@@ -17,15 +16,20 @@ void onCounterChange();
1716

1817
int counter;
1918
GenericConnectionHandler ArduinoIoTPreferredConnection;
20-
NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection);
21-
Provisioning ProvisioningSystem(AgentsManager);
19+
Provisioning ProvisioningSystem;
2220
KVStore kvstore;
21+
22+
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
23+
BLEAgentClass BLEAgent;
24+
#endif
25+
SerialAgentClass SerialAgent;
26+
NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection);
2327
void initProperties() {
2428

2529
ArduinoCloud.addProperty(counter, READWRITE, ON_CHANGE, onCounterChange);
2630
NetworkConfigurator.setStorage(kvstore);
2731
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
28-
AgentsManager.addAgent(BLEAgent);
32+
NetworkConfigurator.addAgent(BLEAgent);
2933
#endif
30-
AgentsManager.addAgent(SerialAgent);
34+
NetworkConfigurator.addAgent(SerialAgent);
3135
}

examples/network-setting/network-setting.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ DeviceMode deviceMode = DeviceMode::CONFIG;
3131
void changeMode(DeviceMode nextMode){
3232
if(nextMode == DeviceMode::RUN){
3333
if(deviceMode == DeviceMode::CONFIG){
34-
AgentsManager.disconnect();
35-
if (AgentsManager.isBLEAgentEnabled()) {
36-
AgentsManager.enableBLEAgent(false);
34+
NetworkConfigurator.configurationCompleted();
35+
if (NetworkConfigurator.isBLEenabled()) {
36+
NetworkConfigurator.enableBLE(false);
3737
}
3838
}
3939
deviceMode = DeviceMode::RUN;
@@ -102,8 +102,8 @@ void loop() {
102102
if (digitalRead(RESETCRED_BUTTON) == HIGH) {
103103
#endif
104104
Serial.println("Update config");
105-
if (!AgentsManager.isBLEAgentEnabled()) {
106-
AgentsManager.enableBLEAgent(true);
105+
if (!NetworkConfigurator.isBLEenabled()) {
106+
NetworkConfigurator.enableBLE(true);
107107
}
108108
}
109109

@@ -127,4 +127,4 @@ void loop() {
127127
*/
128128
void onCounterChange() {
129129
// Add your code here to act upon Counter change
130-
}
130+
}

examples/network-setting/thingProperties.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <ArduinoIoTCloud.h>
44
#include <GenericConnectionHandler.h>
55
#include "NetworkConfigurator.h"
6-
#include "ConfiguratorAgents/AgentsManager.h"
76
#include <Arduino_KVStore.h>
87
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
98
#include "ConfiguratorAgents/agents/BLE/BLEAgent.h"
@@ -18,17 +17,22 @@ void onCounterChange();
1817
int counter;
1918

2019
GenericConnectionHandler ArduinoIoTPreferredConnection;
21-
NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection);
2220
KVStore kvstore;
2321

22+
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
23+
BLEAgentClass BLEAgent;
24+
#endif
25+
SerialAgentClass SerialAgent;
26+
NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection);
27+
2428
void initProperties(){
2529

2630
ArduinoCloud.addProperty(counter, READWRITE, ON_CHANGE, onCounterChange);
2731
NetworkConfigurator.setStorage(kvstore);
2832
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
29-
AgentsManager.addAgent(BLEAgent);
33+
NetworkConfigurator.addAgent(BLEAgent);
3034
#endif
31-
AgentsManager.addAgent(SerialAgent);
35+
NetworkConfigurator.addAgent(SerialAgent);
3236
}
3337

3438

examples/provisioning/ClaimingHandler.cpp

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@
1010
#include "SecretsHelper.h"
1111
#include "Arduino_DebugUtils.h"
1212
#include "Utility/LEDFeedback/LEDFeedback.h"
13+
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
14+
#define BOARD_HAS_BLE
15+
#endif
16+
#ifdef BOARD_HAS_BLE
17+
#include <ArduinoBLE.h>
18+
#include "utility/HCI.h"
19+
#endif
20+
1321
#define PROVISIONING_SERVICEID_FOR_AGENTMANAGER 0xB1
1422

15-
ClaimingHandlerClass::ClaimingHandlerClass(AgentsManagerClass &agc) {
16-
_agentManager = &agc;
23+
ClaimingHandlerClass::ClaimingHandlerClass() {
24+
_agentManager = &AgentsManagerClass::getInstance();
1725
}
1826

1927
bool ClaimingHandlerClass::begin(SecureElement *secureElement, String *uhwid, ClearStoredCredentialsHandler clearStoredCredentials) {
@@ -29,6 +37,10 @@ bool ClaimingHandlerClass::begin(SecureElement *secureElement, String *uhwid, Cl
2937
return false;
3038
}
3139

40+
if(!_agentManager->addRequestHandler(RequestType::GET_BLE_MAC_ADDRESS, getBLEMacAddressRequestCb)) {
41+
return false;
42+
}
43+
3244
if (!_agentManager->addReturnTimestampCallback(setTimestamp)) {
3345
return false;
3446
}
@@ -55,8 +67,9 @@ void ClaimingHandlerClass::poll() {
5567
_agentManager->poll();
5668

5769
switch (_receivedEvent) {
58-
case ClaimingReqEvents::GET_ID: getIdReqHandler (); break;
59-
case ClaimingReqEvents::RESET: resetStoredCredReqHandler(); break;
70+
case ClaimingReqEvents::GET_ID: getIdReqHandler (); break;
71+
case ClaimingReqEvents::RESET: resetStoredCredReqHandler (); break;
72+
case ClaimingReqEvents::GET_BLE_MAC_ADDRESS: getBLEMacAddressReqHandler(); break;
6073
}
6174
_receivedEvent = ClaimingReqEvents::NONE;
6275
return;
@@ -117,6 +130,35 @@ void ClaimingHandlerClass::resetStoredCredReqHandler() {
117130

118131
}
119132

133+
void ClaimingHandlerClass::getBLEMacAddressReqHandler() {
134+
uint8_t mac[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
135+
#ifdef BOARD_HAS_BLE
136+
bool activated = false;
137+
ConfiguratorAgent * connectedAgent = _agentManager->getConnectedAgent();
138+
if(!_agentManager->isBLEAgentEnabled() || (connectedAgent != nullptr &&
139+
connectedAgent->getAgentType() != ConfiguratorAgent::AgentTypes::BLE)) {
140+
activated = true;
141+
BLE.begin();
142+
}
143+
144+
HCI.readBdAddr(mac);
145+
146+
for(int i = 0; i < 3; i++){
147+
uint8_t byte = mac[i];
148+
mac[i] = mac[5-i];
149+
mac[5-i] = byte;
150+
}
151+
if (activated) {
152+
BLE.end();
153+
}
154+
#endif
155+
156+
ProvisioningOutputMessage outputMsg;
157+
outputMsg.type = MessageOutputType::BLE_MAC_ADDRESS;
158+
outputMsg.m.BLEMacAddress = mac;
159+
_agentManager->sendMsg(outputMsg);
160+
}
161+
120162
void ClaimingHandlerClass::getIdRequestCb() {
121163
_receivedEvent = ClaimingReqEvents::GET_ID;
122164
}
@@ -129,6 +171,10 @@ void ClaimingHandlerClass::resetStoredCredRequestCb() {
129171
_receivedEvent = ClaimingReqEvents::RESET;
130172
}
131173

174+
void ClaimingHandlerClass::getBLEMacAddressRequestCb() {
175+
_receivedEvent = ClaimingReqEvents::GET_BLE_MAC_ADDRESS;
176+
}
177+
132178
bool ClaimingHandlerClass::sendStatus(StatusMessage msg) {
133179
ProvisioningOutputMessage statusMsg = { MessageOutputType::STATUS, { msg } };
134180
return _agentManager->sendMsg(statusMsg);

examples/provisioning/ClaimingHandler.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
#include "ConfiguratorAgents/AgentsManager.h"
1212
#include <Arduino_SecureElement.h>
1313

14-
typedef bool (*ClearStoredCredentialsHandler)();
14+
typedef bool (*ClearStoredCredentialsHandler)();
1515
class ClaimingHandlerClass {
1616
public:
17-
ClaimingHandlerClass(AgentsManagerClass &agc);
17+
ClaimingHandlerClass();
1818
bool begin(SecureElement *secureElement, String *uhwid, ClearStoredCredentialsHandler clearStoredCredentials);
1919
void end();
2020
void poll();
@@ -26,7 +26,8 @@ class ClaimingHandlerClass {
2626
};
2727
enum class ClaimingReqEvents { NONE,
2828
GET_ID,
29-
RESET };
29+
RESET,
30+
GET_BLE_MAC_ADDRESS };
3031
static inline ClaimingReqEvents _receivedEvent = ClaimingReqEvents::NONE;
3132
ClaimingHandlerStates _state = ClaimingHandlerStates::END;
3233
AgentsManagerClass *_agentManager = nullptr;
@@ -35,8 +36,10 @@ class ClaimingHandlerClass {
3536
ClearStoredCredentialsHandler _clearStoredCredentials = nullptr;
3637
void getIdReqHandler();
3738
void resetStoredCredReqHandler();
39+
void getBLEMacAddressReqHandler();
3840
bool sendStatus(StatusMessage msg);
3941
static void getIdRequestCb();
4042
static void setTimestamp(uint64_t ts);
4143
static void resetStoredCredRequestCb();
42-
};
44+
static void getBLEMacAddressRequestCb();
45+
};

examples/provisioning/provisioning.ino

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ void display_freeram();
2929
#define RESETCRED_BUTTON 13
3030
#endif
3131

32+
#define CLEAR_CREDENTIALS_ON_RESET true
33+
#define CLEAR_ON_RESET true
34+
35+
3236
enum class DeviceState { FIRST_CONFIG,
3337
CSR,
3438
BEGIN_CLOUD,
@@ -123,12 +127,15 @@ void setup() {
123127
#endif
124128

125129
#if defined(ARDUINO_OPTA)
126-
if(digitalRead(RESETCRED_BUTTON) == LOW){
130+
if(CLEAR_ON_RESET ||digitalRead(RESETCRED_BUTTON) == LOW){
127131
#else
128-
if (digitalRead(RESETCRED_BUTTON) == HIGH) {
132+
if (CLEAR_ON_RESET ||digitalRead(RESETCRED_BUTTON) == HIGH) {
129133
#endif
130134
Serial.println("Resetting cred");
131135
NetworkConfigurator.resetStoredConfiguration();
136+
if (CLEAR_CREDENTIALS_ON_RESET) {
137+
clearStoredCredentials();
138+
}
132139
}
133140

134141
NetworkConfigurator.updateNetworkOptions();
@@ -196,9 +203,9 @@ void cloudConnectedHandler(bool connected) {
196203

197204
DeviceState handleBeginCloud() {
198205
// Close the connection to the peer (App mobile, FE, etc)
199-
AgentsManager.disconnect();
200-
if (AgentsManager.isBLEAgentEnabled()) {
201-
AgentsManager.enableBLEAgent(false);
206+
AgentsManagerClass::getInstance().disconnect();
207+
if (AgentsManagerClass::getInstance().isBLEAgentEnabled()) {
208+
AgentsManagerClass::getInstance().enableBLEAgent(false);
202209
}
203210
// Connect to Arduino IoT Cloud
204211
ArduinoCloud.begin(ArduinoIoTPreferredConnection, false, "mqtts-sa.iot.oniudra.cc");

examples/provisioning/thingProperties.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <GenericConnectionHandler.h>
55
#include <Arduino_KVStore.h>
66
#include "NetworkConfigurator.h"
7-
#include "ConfiguratorAgents/AgentsManager.h"
87
#include "ConfiguratorAgents/agents/BLE/BLEAgent.h"
98
#include "ConfiguratorAgents/agents/Serial/SerialAgent.h"
109
#include "ClaimingHandler.h"
@@ -14,14 +13,16 @@ void onCounterChange();
1413
int counter;
1514
KVStore kvstore;
1615
GenericConnectionHandler ArduinoIoTPreferredConnection;
16+
BLEAgentClass BLEAgent;
17+
SerialAgentClass SerialAgent;
1718
NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection);
18-
ClaimingHandlerClass ClaimingHandler(AgentsManager);
19+
ClaimingHandlerClass ClaimingHandler;
1920

2021
void initProperties() {
2122

2223
ArduinoCloud.addProperty(counter, READWRITE, ON_CHANGE, onCounterChange);
23-
AgentsManager.addAgent(BLEAgent);
24-
AgentsManager.addAgent(SerialAgent);
24+
AgentsManagerClass::getInstance().addAgent(BLEAgent);
25+
AgentsManagerClass::getInstance().addAgent(SerialAgent);
2526
NetworkConfigurator.setStorage(kvstore);
2627
}
2728

0 commit comments

Comments
 (0)