|
| 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 | +} |
0 commit comments