Skip to content

Commit 3c158b9

Browse files
committed
new Provisioning_2.0 sketch
add GET_PROVISIONING_SKETCH_VERSION command add define for compile with test env
1 parent 00cc42d commit 3c158b9

File tree

8 files changed

+1029
-1
lines changed

8 files changed

+1029
-1
lines changed

examples/utility/Provisioning_2.0/CSRHandler.cpp

+428
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright (c) 2024 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#pragma once
10+
#include <Arduino.h>
11+
#include <Arduino_ConnectionHandler.h>
12+
#include <Arduino_SecureElement.h>
13+
#include <tls/utility/TLSClientMqtt.h>
14+
#include <ArduinoHttpClient.h>
15+
#include "Utility/LEDFeedback/LEDFeedback.h"
16+
#define JITTER_BASE 0
17+
#define JITTER_MAX 1000
18+
19+
class CSRHandlerClass {
20+
public:
21+
CSRHandlerClass();
22+
~CSRHandlerClass();
23+
enum class CSRHandlerStates {
24+
BUILD_CSR,
25+
REQUEST_SIGNATURE,
26+
WAITING_RESPONSE,
27+
PARSE_RESPONSE,
28+
BUILD_CERTIFICATE,
29+
CERT_CREATED,
30+
WAITING_COMPLETE_RES,
31+
COMPLETED,
32+
ERROR,
33+
END
34+
};
35+
bool begin(ConnectionHandler &connectionHandler, SecureElement &secureElement, String &uhwid);
36+
void end();
37+
CSRHandlerStates poll();
38+
private:
39+
CSRHandlerStates _state;
40+
unsigned long _nextRequestAt;
41+
uint32_t _requestAttempt;
42+
uint32_t _startWaitingResponse;
43+
String *_uhwid;
44+
String _fw_version;
45+
46+
int _issueYear;
47+
uint8_t _issueMonth;
48+
uint8_t _issueDay;
49+
uint8_t _issueHour;
50+
byte _serialNumber[16];
51+
byte _authorityKeyIdentifier[20];
52+
byte _signature[64];
53+
String _deviceId;
54+
55+
ECP256Certificate *_certForCSR;
56+
ConnectionHandler *_connectionHandler;
57+
SecureElement *_secureElement;
58+
TLSClientMqtt *_tlsClient;
59+
HttpClient *_client;
60+
LEDFeedbackClass &_ledFeedback;
61+
void updateNextRequestAt();
62+
void nextNetworkRetry();
63+
uint32_t jitter(uint32_t base = JITTER_BASE, uint32_t max = JITTER_MAX);
64+
bool postRequest(const char *url, String &postData);
65+
uint32_t getTimestamp();
66+
CSRHandlerStates handleBuildCSR();
67+
CSRHandlerStates handleRequestSignature();
68+
CSRHandlerStates handleWaitingResponse();
69+
CSRHandlerStates handleParseResponse();
70+
CSRHandlerStates handleBuildCertificate();
71+
CSRHandlerStates handleCertCreated();
72+
CSRHandlerStates handleWaitingCompleteRes();
73+
void handleError();
74+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
Copyright (c) 2024 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#include "ClaimingHandler.h"
10+
#include <utility/SElementArduinoCloudJWT.h>
11+
#include "Arduino_DebugUtils.h"
12+
#include <ArduinoBLE.h>
13+
#include "utility/HCI.h"
14+
#include <Arduino_HEX.h>
15+
16+
extern const char *SKETCH_VERSION;
17+
18+
ClaimingHandlerClass::ClaimingHandlerClass():
19+
_uhwid {nullptr},
20+
_state {ClaimingHandlerStates::END},
21+
_secureElement {nullptr},
22+
_clearStoredCredentials {nullptr},
23+
_agentManager { AgentsManagerClass::getInstance()},
24+
_ledFeedback {LEDFeedbackClass::getInstance()} {
25+
_receivedEvent = ClaimingReqEvents::NONE;
26+
_ts = 0;
27+
}
28+
29+
bool ClaimingHandlerClass::begin(SecureElement &secureElement, String &uhwid, ClearStoredCredentialsHandler clearStoredCredentials) {
30+
if(_state != ClaimingHandlerStates::END) {
31+
return true;
32+
}
33+
34+
if(uhwid == "" || clearStoredCredentials == nullptr) {
35+
return false;
36+
}
37+
38+
if (!_agentManager.addRequestHandler(RequestType::GET_ID, getIdRequestCb)) {
39+
return false;
40+
}
41+
42+
if (!_agentManager.addRequestHandler(RequestType::RESET, resetStoredCredRequestCb)) {
43+
return false;
44+
}
45+
46+
if(!_agentManager.addRequestHandler(RequestType::GET_BLE_MAC_ADDRESS, getBLEMacAddressRequestCb)) {
47+
return false;
48+
}
49+
50+
if(!_agentManager.addRequestHandler(RequestType::GET_PROVISIONING_SKETCH_VERSION, getProvSketchVersionRequestCb)) {
51+
return false;
52+
}
53+
54+
if (!_agentManager.addReturnTimestampCallback(setTimestamp)) {
55+
return false;
56+
}
57+
58+
_agentManager.begin();
59+
_uhwid = &uhwid;
60+
_secureElement = &secureElement;
61+
_clearStoredCredentials = clearStoredCredentials;
62+
_state = ClaimingHandlerStates::INIT;
63+
}
64+
65+
void ClaimingHandlerClass::end() {
66+
if(_state == ClaimingHandlerStates::END) {
67+
return;
68+
}
69+
70+
_agentManager.removeReturnTimestampCallback();
71+
_agentManager.removeRequestHandler(RequestType::GET_ID);
72+
_agentManager.removeRequestHandler(RequestType::RESET);
73+
_agentManager.end();
74+
_state = ClaimingHandlerStates::END;
75+
}
76+
77+
void ClaimingHandlerClass::poll() {
78+
if(_state == ClaimingHandlerStates::END) {
79+
return;
80+
}
81+
_ledFeedback.update();
82+
_agentManager.update();
83+
84+
switch (_receivedEvent) {
85+
case ClaimingReqEvents::GET_ID: getIdReqHandler (); break;
86+
case ClaimingReqEvents::RESET: resetStoredCredReqHandler (); break;
87+
case ClaimingReqEvents::GET_BLE_MAC_ADDRESS: getBLEMacAddressReqHandler (); break;
88+
case ClaimingReqEvents::GET_PROV_SKETCH_VERSION: getProvSketchVersionReqHandler(); break;
89+
}
90+
_receivedEvent = ClaimingReqEvents::NONE;
91+
return;
92+
}
93+
94+
void ClaimingHandlerClass::getIdReqHandler() {
95+
if (_ts != 0) {
96+
byte _uhwidBytes[32];
97+
hex::decode(_uhwid->c_str(), _uhwidBytes, _uhwid->length());
98+
//Send UHWID
99+
ProvisioningOutputMessage idMsg = {MessageOutputType::UHWID};
100+
idMsg.m.uhwid = _uhwidBytes;
101+
_agentManager.sendMsg(idMsg);
102+
103+
String token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, 1);
104+
if (token == "") {
105+
DEBUG_ERROR("CH::%s Error: token not created", __FUNCTION__);
106+
sendStatus(StatusMessage::ERROR);
107+
return;
108+
}
109+
110+
//Send JWT
111+
ProvisioningOutputMessage jwtMsg = {MessageOutputType::JWT};
112+
jwtMsg.m.jwt = token.c_str();
113+
_agentManager.sendMsg(jwtMsg);
114+
_ts = 0;
115+
} else {
116+
DEBUG_ERROR("CH::%s Error: timestamp not provided" , __FUNCTION__);
117+
sendStatus(StatusMessage::PARAMS_NOT_FOUND);
118+
}
119+
}
120+
121+
void ClaimingHandlerClass::resetStoredCredReqHandler() {
122+
if( !_clearStoredCredentials()){
123+
DEBUG_ERROR("CH::%s Error: reset stored credentials failed", __FUNCTION__);
124+
sendStatus(StatusMessage::ERROR);
125+
} else {
126+
sendStatus(StatusMessage::RESET_COMPLETED);
127+
}
128+
129+
}
130+
131+
void ClaimingHandlerClass::getBLEMacAddressReqHandler() {
132+
uint8_t mac[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
133+
134+
bool activated = false;
135+
ConfiguratorAgent * connectedAgent = _agentManager.getConnectedAgent();
136+
if(!_agentManager.isAgentEnabled(ConfiguratorAgent::AgentTypes::BLE) || (connectedAgent != nullptr &&
137+
connectedAgent->getAgentType() != ConfiguratorAgent::AgentTypes::BLE)) {
138+
activated = true;
139+
BLE.begin();
140+
}
141+
142+
HCI.readBdAddr(mac);
143+
144+
for(int i = 0; i < 3; i++){
145+
uint8_t byte = mac[i];
146+
mac[i] = mac[5-i];
147+
mac[5-i] = byte;
148+
}
149+
if (activated) {
150+
BLE.end();
151+
}
152+
153+
ProvisioningOutputMessage outputMsg;
154+
outputMsg.type = MessageOutputType::BLE_MAC_ADDRESS;
155+
outputMsg.m.BLEMacAddress = mac;
156+
_agentManager.sendMsg(outputMsg);
157+
}
158+
159+
void ClaimingHandlerClass::getProvSketchVersionReqHandler() {
160+
ProvisioningOutputMessage outputMsg;
161+
outputMsg.type = MessageOutputType::PROV_SKETCH_VERSION;
162+
outputMsg.m.provSketchVersion = SKETCH_VERSION;
163+
_agentManager.sendMsg(outputMsg);
164+
}
165+
166+
void ClaimingHandlerClass::getIdRequestCb() {
167+
DEBUG_VERBOSE("CH Get ID request received");
168+
_receivedEvent = ClaimingReqEvents::GET_ID;
169+
}
170+
void ClaimingHandlerClass::setTimestamp(uint64_t ts) {
171+
_ts = ts;
172+
}
173+
174+
void ClaimingHandlerClass::resetStoredCredRequestCb() {
175+
DEBUG_VERBOSE("CH Reset stored credentials request received");
176+
_receivedEvent = ClaimingReqEvents::RESET;
177+
}
178+
179+
void ClaimingHandlerClass::getBLEMacAddressRequestCb() {
180+
DEBUG_VERBOSE("CH Get BLE MAC address request received");
181+
_receivedEvent = ClaimingReqEvents::GET_BLE_MAC_ADDRESS;
182+
}
183+
184+
void ClaimingHandlerClass::getProvSketchVersionRequestCb() {
185+
DEBUG_VERBOSE("CH Get provisioning sketch version request received");
186+
_receivedEvent = ClaimingReqEvents::GET_PROV_SKETCH_VERSION;
187+
}
188+
189+
bool ClaimingHandlerClass::sendStatus(StatusMessage msg) {
190+
ProvisioningOutputMessage statusMsg = { MessageOutputType::STATUS, { msg } };
191+
return _agentManager.sendMsg(statusMsg);
192+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright (c) 2024 Arduino SA
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#pragma once
10+
#include "Arduino.h"
11+
#include "ConfiguratorAgents/AgentsManager.h"
12+
#include <Arduino_SecureElement.h>
13+
#include "Utility/LEDFeedback/LEDFeedback.h"
14+
15+
typedef bool (*ClearStoredCredentialsHandler)();
16+
class ClaimingHandlerClass {
17+
public:
18+
ClaimingHandlerClass();
19+
bool begin(SecureElement &secureElement, String &uhwid, ClearStoredCredentialsHandler clearStoredCredentials);
20+
void end();
21+
void poll();
22+
private:
23+
String *_uhwid;
24+
enum class ClaimingHandlerStates {
25+
INIT,
26+
END
27+
};
28+
enum class ClaimingReqEvents { NONE,
29+
GET_ID,
30+
RESET,
31+
GET_BLE_MAC_ADDRESS,
32+
GET_PROV_SKETCH_VERSION};
33+
static inline ClaimingReqEvents _receivedEvent;
34+
ClaimingHandlerStates _state;
35+
AgentsManagerClass &_agentManager;
36+
LEDFeedbackClass &_ledFeedback;
37+
static inline uint64_t _ts;
38+
SecureElement *_secureElement;
39+
40+
bool sendStatus(StatusMessage msg);
41+
/* Commands handlers */
42+
void getIdReqHandler();
43+
void resetStoredCredReqHandler();
44+
void getBLEMacAddressReqHandler();
45+
void getProvSketchVersionReqHandler();
46+
ClearStoredCredentialsHandler _clearStoredCredentials;
47+
/* Callbacks for receiving commands */
48+
static void getIdRequestCb();
49+
static void setTimestamp(uint64_t ts);
50+
static void resetStoredCredRequestCb();
51+
static void getBLEMacAddressRequestCb();
52+
static void getProvSketchVersionRequestCb();
53+
};

0 commit comments

Comments
 (0)