Skip to content

Commit 75f8355

Browse files
committed
Add Led feedback (#38)
fix sequence animation turn off
1 parent 7a33ca8 commit 75f8355

File tree

10 files changed

+400
-24
lines changed

10 files changed

+400
-24
lines changed

examples/provisioning/CSRHandler.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "SecretsHelper.h"
1616
#include <utility/time/TimeService.h>
1717
#include <stdlib.h>
18+
#include "Utility/LEDFeedback/LEDFeedback.h"
1819

1920
#define RESPONSE_MAX_LEN 1100
2021
#define RESPONSE_TIMEOUT 5000
@@ -42,6 +43,7 @@ void CSRHandlerClass::begin(ConnectionHandler *connectionHandler, SecureElement
4243
_uhwid = uhwid;
4344
if (*_uhwid == "") {
4445
Serial.println("Error: UHWID not found");
46+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
4547
_state = CSRHandlerStates::ERROR;
4648
return;
4749
}
@@ -82,7 +84,7 @@ CSRHandlerClass::CSRHandlerStates CSRHandlerClass::poll() {
8284
case CSRHandlerStates::CERT_CREATED: _state = handleCertCreated (); break;
8385
case CSRHandlerStates::WAITING_COMPLETE_RES: _state = handleWaitingCompleteRes(); break;
8486
case CSRHandlerStates::COMPLETED: break;
85-
case CSRHandlerStates::ERROR: break;
87+
case CSRHandlerStates::ERROR: handleError (); break;
8688
case CSRHandlerStates::END: break;
8789
}
8890

@@ -156,13 +158,15 @@ CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleBuildCSR() {
156158
}
157159
if (!_certForCSR->begin()) {
158160
Serial.println("Error starting CSR generation!");
161+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
159162
return CSRHandlerStates::ERROR;
160163
}
161164

162165
_certForCSR->setSubjectCommonName(*_uhwid);
163166

164167
if (!SElementCSR::build(*_secureElement, *_certForCSR, static_cast<int>(SElementArduinoCloudSlot::Key), true)) {
165168
Serial.println("Error generating CSR!");
169+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
166170
return CSRHandlerStates::ERROR;
167171
}
168172
return CSRHandlerStates::REQUEST_SIGNATURE;
@@ -277,13 +281,15 @@ CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleBuildCertificate() {
277281

278282
if (!SElementArduinoCloudDeviceId::write(*_secureElement, _deviceId, SElementArduinoCloudSlot::DeviceId)) {
279283
Serial.println("Error storing device id!");
284+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
280285
return CSRHandlerStates::ERROR;
281286
}
282287

283288

284289
ECP256Certificate cert;
285290
if (!cert.begin()) {
286291
Serial.println("Error starting secureElement storage!");
292+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
287293
return CSRHandlerStates::ERROR;
288294
}
289295

@@ -303,11 +309,13 @@ CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleBuildCertificate() {
303309

304310
if (!SElementArduinoCloudCertificate::build(*_secureElement, cert, static_cast<int>(SElementArduinoCloudSlot::Key))) {
305311
Serial.println("Error building secureElement compressed cert!");
312+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
306313
return CSRHandlerStates::ERROR;
307314
}
308315

309316
if (!SElementArduinoCloudCertificate::write(*_secureElement, cert, SElementArduinoCloudSlot::CompressedCertificate)) {
310317
Serial.println("Error storing cert!");
318+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
311319
return CSRHandlerStates::ERROR;
312320
}
313321

@@ -379,3 +387,7 @@ CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleWaitingCompleteRes() {
379387

380388
return nextState;
381389
}
390+
391+
void CSRHandlerClass::handleError() {
392+
LEDFeedbackClass::getInstance().poll();
393+
}

examples/provisioning/CSRHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,5 @@ class CSRHandlerClass {
6565
CSRHandlerStates handleBuildCertificate();
6666
CSRHandlerStates handleCertCreated();
6767
CSRHandlerStates handleWaitingCompleteRes();
68+
void handleError();
6869
};

examples/provisioning/ClaimingHandler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ClaimingHandler.h"
1010
#include "SecretsHelper.h"
1111
#include "Arduino_DebugUtils.h"
12+
#include "Utility/LEDFeedback/LEDFeedback.h"
1213
#define PROVISIONING_SERVICEID_FOR_AGENTMANAGER 0xB1
1314

1415
ClaimingHandlerClass::ClaimingHandlerClass(AgentsManagerClass &agc) {
@@ -50,6 +51,7 @@ void ClaimingHandlerClass::poll() {
5051
if(_state == ClaimingHandlerStates::END) {
5152
return;
5253
}
54+
LEDFeedbackClass::getInstance().poll();
5355
_agentManager->poll();
5456

5557
switch (_receivedEvent) {
@@ -67,6 +69,7 @@ void ClaimingHandlerClass::getIdReqHandler() {
6769
if (*_uhwid == "") {
6870
DEBUG_ERROR("ClaimingHandlerClass::%s Error: UHWID not found", __FUNCTION__);
6971
sendStatus(StatusMessage::ERROR);
72+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
7073
return;
7174
}
7275

@@ -83,6 +86,7 @@ void ClaimingHandlerClass::getIdReqHandler() {
8386
if (token == "") {
8487
DEBUG_ERROR("ClaimingHandlerClass::%s Error: token not created", __FUNCTION__);
8588
sendStatus(StatusMessage::ERROR);
89+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
8690
return;
8791
}
8892

@@ -106,6 +110,7 @@ void ClaimingHandlerClass::resetStoredCredReqHandler() {
106110
if( !_clearStoredCredentials()){
107111
DEBUG_ERROR("ClaimingHandlerClass::%s Error: reset stored credentials failed", __FUNCTION__);
108112
sendStatus(StatusMessage::ERROR);
113+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
109114
} else {
110115
sendStatus(StatusMessage::RESET_COMPLETED);
111116
}

examples/provisioning/provisioning.ino

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <Arduino_SecureElement.h>
1414
#include <utility/SElementArduinoCloudDeviceId.h>
1515
#include <utility/SElementArduinoCloudCertificate.h>
16+
#include "Utility/LEDFeedback/LEDFeedback.h"
1617

1718
#if defined(ARDUINO_OPTA) || defined(ARDUINO_PORTENTA_H7_M7)
1819
#include "mbed.h"
@@ -24,7 +25,7 @@ void display_freeram();
2425
#define RESETCRED_BUTTON BTN_USER
2526
#elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT)
2627
#define RESETCRED_BUTTON 7
27-
#else
28+
#else
2829
#define RESETCRED_BUTTON 13
2930
#endif
3031

@@ -93,15 +94,20 @@ void setup() {
9394
if (!secureElement.writeConfiguration()) {
9495
Serial.println("Writing secureElement configuration failed!");
9596
Serial.println("Stopping Provisioning");
96-
while (1)
97-
;
97+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
98+
while (1){
99+
LEDFeedbackClass::getInstance().poll();
100+
}
101+
98102
}
99103

100104
if (!secureElement.lock()) {
101105
Serial.println("Locking secureElement configuration failed!");
102106
Serial.println("Stopping Provisioning");
103-
while (1)
104-
;
107+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
108+
while (1){
109+
LEDFeedbackClass::getInstance().poll();
110+
}
105111
}
106112

107113
Serial.println("secureElement locked successfully");
@@ -176,6 +182,18 @@ DeviceState handleCSR() {
176182
return nextState;
177183
}
178184

185+
void cloudConnectedHandler(bool connected) {
186+
static bool _status = false;
187+
if(connected != _status){
188+
_status = connected;
189+
if(connected){
190+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::CONNECTED_TO_CLOUD);
191+
} else {
192+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::NONE);
193+
}
194+
}
195+
}
196+
179197
DeviceState handleBeginCloud() {
180198
// Close the connection to the peer (App mobile, FE, etc)
181199
AgentsManager.disconnect();
@@ -198,6 +216,8 @@ DeviceState handleRun() {
198216
}
199217
ArduinoCloud.update();
200218

219+
cloudConnectedHandler(ArduinoCloud.connected());
220+
201221
if (NetworkConfigurator.poll() == NetworkConfiguratorStates::UPDATING_CONFIG) {
202222
nextState = DeviceState::FIRST_CONFIG;
203223
}
@@ -233,9 +253,14 @@ void onCounterChange() {
233253
// Add your code here to act upon Counter change
234254
}
235255

256+
void cloudConnected() {
257+
// Add your code here to act upon Cloud connection
258+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::CONNECTED_TO_CLOUD);
259+
}
260+
236261
int freeRam() {
237262
char top;
238-
#if defined(ARDUINO_OPTA) || defined(ARDUINO_PORTENTA_H7_M7)
263+
#if defined(ARDUINO_OPTA) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_GIGA)
239264
int cnt = osThreadGetCount();
240265
mbed_stats_stack_t* stats = (mbed_stats_stack_t*)malloc(cnt * sizeof(mbed_stats_stack_t));
241266

src/ConfiguratorAgents/AgentsManager.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <settings/settings.h>
1212
#include "AgentsManager.h"
1313
#include "NetworkOptionsDefinitions.h"
14+
#include "Utility/LEDFeedback/LEDFeedback.h"
1415
#if !defined(ARDUINO_SAMD_MKRGSM1400) && !defined(ARDUINO_SAMD_MKRNB1500) && !defined(ARDUINO_SAMD_MKRWAN1300) && !defined(ARDUINO_SAMD_MKRWAN1310)
1516
#define BOARD_HAS_BLE
1617
#endif
@@ -89,12 +90,6 @@ bool AgentsManagerClass::end(uint8_t id) {
8990
std::for_each(_agentsList.begin(), _agentsList.end(), [](ConfiguratorAgent *agent) {
9091
agent->end();
9192
});
92-
93-
#if defined(ARDUINO_PORTENTA_H7_M7)
94-
digitalWrite(LED_BUILTIN, HIGH);
95-
#else
96-
digitalWrite(LED_BUILTIN, LOW);
97-
#endif
9893
_selectedAgent = nullptr;
9994
_statusRequest.reset();
10095
_initStatusMsg = StatusMessage::NONE;
@@ -200,11 +195,6 @@ AgentsManagerStates AgentsManagerClass::handleInit() {
200195
for (std::list<ConfiguratorAgent *>::iterator agent = _agentsList.begin(); agent != _agentsList.end(); ++agent) {
201196
if ((*agent)->poll() == ConfiguratorAgent::AgentConfiguratorStates::PEER_CONNECTED) {
202197
_selectedAgent = *agent;
203-
#if defined(ARDUINO_PORTENTA_H7_M7)
204-
digitalWrite(LED_BUILTIN, LOW);
205-
#else
206-
digitalWrite(LED_BUILTIN, HIGH);
207-
#endif
208198
nextState = AgentsManagerStates::SEND_INITIAL_STATUS;
209199
break;
210200
}
@@ -217,6 +207,7 @@ AgentsManagerStates AgentsManagerClass::handleInit() {
217207
(*agent)->end();
218208
}
219209
}
210+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED);
220211
}
221212
return nextState;
222213
}
@@ -416,11 +407,6 @@ AgentsManagerStates AgentsManagerClass::handlePeerDisconnected() {
416407
(*agent)->begin();
417408
}
418409
}
419-
#if defined(ARDUINO_PORTENTA_H7_M7)
420-
digitalWrite(LED_BUILTIN, HIGH);
421-
#else
422-
digitalWrite(LED_BUILTIN, LOW);
423-
#endif
424410
_selectedAgent = nullptr;
425411
return AgentsManagerStates::INIT;
426412
}

src/ConfiguratorAgents/agents/BLE/BLEAgent.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "BLEStringCharacteristic.h"
1313
#include "BLECharacteristic.h"
1414
#include "BLEAgent.h"
15+
#include "Utility/LEDFeedback/LEDFeedback.h"
1516
#define DEBUG_PACKET
1617
#define BASE_LOCAL_NAME "Arduino"
1718
#define ARDUINO_COMPANY_ID 0x09A3
@@ -77,6 +78,7 @@ ConfiguratorAgent::AgentConfiguratorStates BLEAgentClass::begin() {
7778

7879
// start advertising
7980
BLE.advertise();
81+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::BLE_AVAILABLE);
8082
_state = AgentConfiguratorStates::INIT;
8183
DEBUG_DEBUG("BLEAgentClass begin completed");
8284
return _state;
@@ -91,6 +93,7 @@ ConfiguratorAgent::AgentConfiguratorStates BLEAgentClass::end() {
9193
BLE.stopAdvertise();
9294
BLE.end();
9395
clear();
96+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::NONE);
9497
_state = AgentConfiguratorStates::END;
9598
}
9699

@@ -107,11 +110,13 @@ ConfiguratorAgent::AgentConfiguratorStates BLEAgentClass::poll() {
107110
case BLEEvent::SUBSCRIBED:
108111
if (_state != AgentConfiguratorStates::PEER_CONNECTED) {
109112
_state = AgentConfiguratorStates::PEER_CONNECTED;
113+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED);
110114
}
111115
break;
112116
case BLEEvent::DISCONNECTED:
113117
clearInputBuffer();
114118
clear();
119+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::BLE_AVAILABLE);
115120
_state = AgentConfiguratorStates::INIT;
116121
break;
117122
default:
@@ -254,6 +259,7 @@ void BLEAgentClass::disconnectPeer() {
254259
BLE.poll();
255260
} while (millis() - start < 200);
256261
clear();
262+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::BLE_AVAILABLE);
257263
_state = AgentConfiguratorStates::INIT;
258264
return;
259265
}

src/ConfiguratorAgents/agents/Serial/SerialAgent.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <Arduino_DebugUtils.h>
1010
#include "SerialAgent.h"
11+
#include "Utility/LEDFeedback/LEDFeedback.h"
1112

1213
SerialAgentClass::SerialAgentClass() {
1314
}
@@ -43,6 +44,7 @@ ConfiguratorAgent::AgentConfiguratorStates SerialAgentClass::poll() {
4344
if (_disconnectRequest) {
4445
_disconnectRequest = false;
4546
clear();
47+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::NONE);
4648
_state = AgentConfiguratorStates::INIT;
4749
}
4850

@@ -55,6 +57,7 @@ void SerialAgentClass::disconnectPeer() {
5557
uint8_t data = 0x02;
5658
sendData(PacketManager::MessageType::TRANSMISSION_CONTROL, &data, sizeof(data));
5759
clear();
60+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::NONE);
5861
_state = AgentConfiguratorStates::INIT;
5962
}
6063

src/NetworkConfigurator.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "ConnectionHandlerDefinitions.h"
1111
#include "ConfiguratorAgents/MessagesDefinitions.h"
1212
#include "Arduino_ConnectionHandler.h"
13+
#include "Utility/LEDFeedback/LEDFeedback.h"
1314
#ifdef BOARD_HAS_WIFI
1415
#include "WiFiConnectionHandler.h"
1516
#endif
@@ -45,7 +46,7 @@ bool NetworkConfiguratorClass::begin() {
4546
}
4647
_state = NetworkConfiguratorStates::READ_STORED_CONFIG;
4748
memset(&_networkSetting, 0x00, sizeof(models::NetworkSetting));
48-
49+
LEDFeedbackClass::getInstance().begin();
4950
#ifdef BOARD_HAS_WIFI
5051
#ifndef ARDUINO_ARCH_ESP32
5152
String fv = WiFi.firmwareVersion();
@@ -94,6 +95,8 @@ bool NetworkConfiguratorClass::begin() {
9495

9596
NetworkConfiguratorStates NetworkConfiguratorClass::poll() {
9697
NetworkConfiguratorStates nextState = _state;
98+
LEDFeedbackClass::getInstance().poll();
99+
97100
switch (_state) {
98101
#ifdef BOARD_HAS_ETHERNET
99102
case NetworkConfiguratorStates::CHECK_ETH: nextState = handleCheckEth (); break;
@@ -323,11 +326,13 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleConnectRequest() {
323326
if (!_kvstore.begin()) {
324327
DEBUG_ERROR("NetworkConfiguratorClass::%s error initializing kvstore", __FUNCTION__);
325328
sendStatus(StatusMessage::ERROR_STORAGE_BEGIN);
329+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
326330
return nextState;
327331
}
328332
if (!_kvstore.putBytes(STORAGE_KEY, (uint8_t *)&_networkSetting, sizeof(models::NetworkSetting))) {
329333
DEBUG_ERROR("NetworkConfiguratorClass::%s error saving network settings", __FUNCTION__);
330334
sendStatus(StatusMessage::ERROR);
335+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
331336
return nextState;
332337
}
333338

@@ -338,6 +343,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleConnectRequest() {
338343
if(disconnectFromNetwork() == ConnectionResult::FAILED) {
339344
DEBUG_ERROR("NetworkConfiguratorClass::%s Impossible to disconnect the network", __FUNCTION__);
340345
sendStatus(StatusMessage::ERROR);
346+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
341347
return nextState;
342348
}
343349
}
@@ -350,6 +356,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleConnectRequest() {
350356

351357
_connectionHandlerIstantiated = true;
352358
nextState = NetworkConfiguratorStates::CONNECTING;
359+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::CONNECTING_TO_NETWORK);
353360
return nextState;
354361
}
355362

@@ -412,6 +419,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleReadStorage() {
412419
if (!_kvstore.begin()) {
413420
DEBUG_ERROR("NetworkConfiguratorClass::%s error initializing kvstore", __FUNCTION__);
414421
sendStatus(StatusMessage::ERROR_STORAGE_BEGIN);
422+
LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR);
415423
return nextState;
416424
}
417425

0 commit comments

Comments
 (0)