Skip to content

Commit 88d0fea

Browse files
committed
enable or disable every type of agent
1 parent 659eb9c commit 88d0fea

File tree

4 files changed

+73
-67
lines changed

4 files changed

+73
-67
lines changed

src/Arduino_NetworkConfigurator.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ NetworkConfiguratorClass::NetworkConfiguratorClass(ConnectionHandler &connection
3434
_state{ NetworkConfiguratorStates::END },
3535
_connectionHandler{ &connectionHandler },
3636
_connectionHandlerIstantiated{ false },
37-
_bleEnabled{ true },
3837
_kvstore{ nullptr },
3938
_connectionTimeout{ NC_CONNECTION_TIMEOUT_ms, NC_CONNECTION_TIMEOUT_ms },
4039
_connectionRetryTimer{ NC_CONNECTION_RETRY_TIMER_ms, NC_CONNECTION_RETRY_TIMER_ms },
@@ -200,13 +199,12 @@ void NetworkConfiguratorClass::addReconfigurePinCallback(ResetInput::ResetInputC
200199
_resetInput->setPinChangedCallback(callback);
201200
}
202201

203-
bool NetworkConfiguratorClass::isBLEenabled() {
204-
return _agentsManager->isBLEAgentEnabled();
202+
bool NetworkConfiguratorClass::isAgentEnabled(ConfiguratorAgent::AgentTypes type) {
203+
return _agentsManager->isAgentEnabled(type);
205204
}
206205

207-
void NetworkConfiguratorClass::enableBLE(bool enable) {
208-
_bleEnabled = enable;
209-
_agentsManager->enableBLEAgent(enable);
206+
void NetworkConfiguratorClass::enableAgent(ConfiguratorAgent::AgentTypes type, bool enable) {
207+
_agentsManager->enableAgent(type, enable);
210208
}
211209

212210
void NetworkConfiguratorClass::disconnectAgent() {
@@ -469,7 +467,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleReadStorage() {
469467

470468
if(_kvstore->exists(START_BLE_AT_STARTUP_KEY)) {
471469
if(_kvstore->getBool(START_BLE_AT_STARTUP_KEY)) {
472-
_agentsManager->enableBLEAgent(true);
470+
_agentsManager->startAgent(ConfiguratorAgent::AgentTypes::BLE);
473471
}
474472
_kvstore->remove(START_BLE_AT_STARTUP_KEY);
475473
}
@@ -525,9 +523,6 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleConnecting() {
525523
ConnectionResult res = connectToNetwork(&err);
526524

527525
if (res == ConnectionResult::SUCCESS) {
528-
if(_agentsManager->isBLEAgentEnabled() && !_bleEnabled) {
529-
_agentsManager->enableBLEAgent(false);
530-
}
531526
return NetworkConfiguratorStates::CONFIGURED;
532527
} else if (res == ConnectionResult::FAILED) {
533528
sendStatus(err);

src/Arduino_NetworkConfigurator.h

+14-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#if NETWORK_CONFIGURATOR_COMPATIBLE
1212

1313
#include "Arduino.h"
14-
#include "GenericConnectionHandler.h"
14+
#include "GenericConnectionHandler.h"//TODO fix include
1515
#include "ConfiguratorAgents/AgentsManager.h"
1616
#include <settings/settings.h>
1717
#include <Arduino_TimedAttempt.h>
@@ -29,6 +29,7 @@ enum class NetworkConfiguratorStates { ZERO_TOUCH_CONFIG,
2929
END };
3030

3131
/**
32+
* @class NetworkConfiguratorClass
3233
* @brief Class responsible for managing the network configuration of an Arduino_ConnectionHandler instance.
3334
* The library provides a way to configure and update
3435
* the network settings using different interfaces like BLE, Serial, etc.
@@ -70,7 +71,7 @@ class NetworkConfiguratorClass {
7071

7172
/**
7273
* @brief Resets the stored network configuration.
73-
* This method is alternative to the reconfiguration procedure
74+
* This method is an alternative to the reconfiguration procedure
7475
* without forcing the restart of the BLE interface if turned off.
7576
* @return True if the reset is successful, false otherwise.
7677
*/
@@ -97,7 +98,7 @@ class NetworkConfiguratorClass {
9798

9899
/**
99100
* @brief Sets the pin used for the reconfiguration procedure.
100-
* It must be set before calling the begin() method.
101+
* This must be set before calling the begin() method.
101102
* @param pin The pin number to be used for reconfiguration,
102103
* internally it's mapped to an interrupt with INPUT_PULLUP mode.
103104
*/
@@ -111,25 +112,27 @@ class NetworkConfiguratorClass {
111112
void addReconfigurePinCallback(ResetInput::ResetInputCallback callback);
112113

113114
/**
114-
* @brief Checks if BLE (Bluetooth Low Energy) is enabled.
115-
* @return True if BLE is enabled, false otherwise.
115+
* @brief Checks if a specific configuration agent is enabled.
116+
* @param type The type of the agent to check (e.g., BLE, Serial).
117+
* @return True if the agent is enabled, false otherwise.
116118
*/
117-
bool isBLEenabled();
119+
bool isAgentEnabled(ConfiguratorAgent::AgentTypes type);
118120

119121
/**
120-
* @brief Enables or disables BLE (Bluetooth Low Energy).
121-
* @param enable True to enable BLE, false to disable it.
122+
* @brief Enables or disables a specific configuration agent.
123+
* @param type The type of the agent to enable or disable (e.g., BLE, Serial).
124+
* @param enable True to enable the agent, false to disable it.
122125
*/
123-
void enableBLE(bool enable);
126+
void enableAgent(ConfiguratorAgent::AgentTypes type, bool enable);
124127

125128
/**
126-
* @brief Disconnects the current agent from the peer.
129+
* @brief Disconnects the current configuration agent from the peer.
127130
*/
128131
void disconnectAgent();
129132

130133
/**
131134
* @brief Adds a new configurator agent.
132-
* Configurator agents handle the configuration interfaces BLE, Serial, etc.
135+
* Configurator agents handle the configuration interfaces such as BLE, Serial, etc.
133136
* This method should be called before the begin() method.
134137
* @param agent Reference to a ConfiguratorAgent object.
135138
* @return True if the agent is added successfully, false otherwise.
@@ -143,7 +146,6 @@ class NetworkConfiguratorClass {
143146
bool _connectionHandlerIstantiated;
144147
ResetInput *_resetInput;
145148
LEDFeedbackClass *_ledFeedback;
146-
bool _bleEnabled;
147149
TimedAttempt _connectionTimeout;
148150
TimedAttempt _connectionRetryTimer;
149151
TimedAttempt _optionUpdateTimer;

src/ConfiguratorAgents/AgentsManager.cpp

+47-39
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,8 @@ bool AgentsManagerClass::begin() {
3636
}
3737

3838
for (std::list<ConfiguratorAgent *>::iterator agent = _agentsList.begin(); agent != _agentsList.end(); ++agent) {
39-
if((*agent)->getAgentType() == ConfiguratorAgent::AgentTypes::BLE) {
40-
if (!_bleAgentEnabled) {
41-
continue;
42-
}
39+
if( _enabledAgents[(int)(*agent)->getAgentType()] == false) {
40+
continue;
4341
}
4442
if ((*agent)->begin() == ConfiguratorAgent::AgentConfiguratorStates::ERROR) {
4543
DEBUG_ERROR("AgentsManagerClass::%s agent type %d fails", __FUNCTION__, (int)(*agent)->getAgentType());
@@ -64,21 +62,51 @@ AgentsManagerStates AgentsManagerClass::poll() {
6462
return _state;
6563
}
6664

67-
void AgentsManagerClass::enableBLEAgent(bool enable) {
68-
if (_bleAgentEnabled == enable) {
65+
void AgentsManagerClass::enableAgent(ConfiguratorAgent::AgentTypes type, bool enable) {
66+
bool _agentState = _enabledAgents[(int)type];
67+
68+
if (_agentState == enable) {
6969
return;
7070
}
71-
_bleAgentEnabled = enable;
7271

72+
_enabledAgents[(int)type] = enable;
7373
if (enable) {
74-
startBLEAgent();
74+
startAgent(type);
7575
} else {
76-
stopBLEAgent();
76+
stopAgent(type);
7777
}
7878
}
7979

80-
bool AgentsManagerClass::isBLEAgentEnabled() {
81-
return _bleAgentEnabled;
80+
bool AgentsManagerClass::isAgentEnabled(ConfiguratorAgent::AgentTypes type) {
81+
return _enabledAgents[(int)type];
82+
}
83+
84+
bool AgentsManagerClass::startAgent(ConfiguratorAgent::AgentTypes type) {
85+
if (_state == AgentsManagerStates::END) {
86+
return false;
87+
}
88+
89+
for (std::list<ConfiguratorAgent *>::iterator agent = _agentsList.begin(); agent != _agentsList.end(); ++agent) {
90+
if ((*agent)->getAgentType() == type) {
91+
(*agent)->begin();
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
98+
bool AgentsManagerClass::stopAgent(ConfiguratorAgent::AgentTypes type) {
99+
for (std::list<ConfiguratorAgent *>::iterator agent = _agentsList.begin(); agent != _agentsList.end(); ++agent) {
100+
if ((*agent)->getAgentType() == type) {
101+
(*agent)->end();
102+
if (*agent == _selectedAgent) {
103+
handlePeerDisconnected();
104+
_state = AgentsManagerStates::INIT;
105+
}
106+
return true;
107+
}
108+
}
109+
return false;
82110
}
83111

84112
bool AgentsManagerClass::end() {
@@ -187,11 +215,12 @@ AgentsManagerClass::AgentsManagerClass():
187215
_returnNetworkSettingsCb{ nullptr },
188216
_selectedAgent{ nullptr },
189217
_instances{ 0 },
190-
_bleAgentEnabled{ true },
191218
_initStatusMsg{ StatusMessage::NONE },
192219
_statusRequest{ .completion = 0, .pending = false, .key = RequestType::NONE },
193220
_state{ AgentsManagerStates::END }
194-
{}
221+
{
222+
memset(_enabledAgents, 0x01, sizeof(_enabledAgents));
223+
}
195224

196225
AgentsManagerStates AgentsManagerClass::handleInit() {
197226
AgentsManagerStates nextState = _state;
@@ -369,38 +398,17 @@ bool AgentsManagerClass::sendStatus(StatusMessage msg) {
369398
void AgentsManagerClass::handlePeerDisconnected() {
370399
//Peer disconnected, restore all stopped agents
371400
for (std::list<ConfiguratorAgent *>::iterator agent = _agentsList.begin(); agent != _agentsList.end(); ++agent) {
401+
if (_enabledAgents[(int)(*agent)->getAgentType()] == false) {
402+
(*agent)->end();
403+
continue;
404+
}
405+
372406
if (*agent != _selectedAgent) {
373-
if ((*agent)->getAgentType() == ConfiguratorAgent::AgentTypes::BLE && !_bleAgentEnabled) {
374-
continue;
375-
}
376407
(*agent)->begin();
377408
}
378409
}
379410
_selectedAgent = nullptr;
380411
return;
381412
}
382413

383-
void AgentsManagerClass::stopBLEAgent() {
384-
for (std::list<ConfiguratorAgent *>::iterator agent = _agentsList.begin(); agent != _agentsList.end(); ++agent) {
385-
if ((*agent)->getAgentType() == ConfiguratorAgent::AgentTypes::BLE) {
386-
(*agent)->end();
387-
if (*agent == _selectedAgent) {
388-
handlePeerDisconnected();
389-
_state = AgentsManagerStates::INIT;
390-
}
391-
}
392-
}
393-
}
394-
395-
void AgentsManagerClass::startBLEAgent() {
396-
if (_state == AgentsManagerStates::END || !_bleAgentEnabled) {
397-
return;
398-
}
399-
std::for_each(_agentsList.begin(), _agentsList.end(), [](ConfiguratorAgent *agent) {
400-
if (agent->getAgentType() == ConfiguratorAgent::AgentTypes::BLE) {
401-
agent->begin();
402-
}
403-
});
404-
}
405-
406414
#endif // NETWORK_CONFIGURATOR_COMPATIBLE

src/ConfiguratorAgents/AgentsManager.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ class AgentsManagerClass {
3636
bool begin();
3737
bool end();
3838
void disconnect();
39-
AgentsManagerStates poll();
40-
void enableBLEAgent(bool enable);
41-
bool isBLEAgentEnabled();
39+
AgentsManagerStates poll(); //TODO rename to update
40+
void enableAgent(ConfiguratorAgent::AgentTypes type, bool enable);
41+
bool isAgentEnabled(ConfiguratorAgent::AgentTypes type);
42+
// Force starting agent even if disabled
43+
bool startAgent(ConfiguratorAgent::AgentTypes type);
44+
bool stopAgent(ConfiguratorAgent::AgentTypes type);
4245
ConfiguratorAgent *getConnectedAgent();
4346
bool sendMsg(ProvisioningOutputMessage &msg);
4447
bool addAgent(ConfiguratorAgent &agent);
@@ -54,12 +57,12 @@ class AgentsManagerClass {
5457
AgentsManagerClass();
5558
AgentsManagerStates _state;
5659
std::list<ConfiguratorAgent *> _agentsList;
60+
bool _enabledAgents[2];
5761
ConfiguratorRequestHandler _reqHandlers[6];
5862
ReturnTimestamp _returnTimestampCb;
5963
ReturnNetworkSettings _returnNetworkSettingsCb;
6064
ConfiguratorAgent *_selectedAgent;
6165
uint8_t _instances;
62-
bool _bleAgentEnabled;
6366
StatusMessage _initStatusMsg;
6467
NetworkOptions _netOptions;
6568
typedef struct {
@@ -87,6 +90,4 @@ class AgentsManagerClass {
8790
bool sendStatus(StatusMessage msg);
8891

8992
void handlePeerDisconnected();
90-
void stopBLEAgent();
91-
void startBLEAgent();
9293
};

0 commit comments

Comments
 (0)