Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:119624335925 1 // ----------------------------------------------------------------------------
MACRUM 0:119624335925 2 // Copyright 2016-2017 ARM Ltd.
MACRUM 0:119624335925 3 //
MACRUM 0:119624335925 4 // SPDX-License-Identifier: Apache-2.0
MACRUM 0:119624335925 5 //
MACRUM 0:119624335925 6 // Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:119624335925 7 // you may not use this file except in compliance with the License.
MACRUM 0:119624335925 8 // You may obtain a copy of the License at
MACRUM 0:119624335925 9 //
MACRUM 0:119624335925 10 // http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:119624335925 11 //
MACRUM 0:119624335925 12 // Unless required by applicable law or agreed to in writing, software
MACRUM 0:119624335925 13 // distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:119624335925 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:119624335925 15 // See the License for the specific language governing permissions and
MACRUM 0:119624335925 16 // limitations under the License.
MACRUM 0:119624335925 17 // ----------------------------------------------------------------------------
MACRUM 0:119624335925 18
MACRUM 0:119624335925 19 #include <string>
MACRUM 0:119624335925 20 #include <assert.h>
MACRUM 0:119624335925 21 #include <stdio.h>
MACRUM 0:119624335925 22 #include "include/ConnectorClient.h"
MACRUM 0:119624335925 23 #include "include/CloudClientStorage.h"
MACRUM 0:119624335925 24 #include "include/CertificateParser.h"
MACRUM 0:119624335925 25 #include "MbedCloudClient.h"
MACRUM 0:119624335925 26 #include "mbed-client/m2minterfacefactory.h"
MACRUM 0:119624335925 27 #include "mbed-client/m2mdevice.h"
MACRUM 0:119624335925 28 #include "mbed-trace/mbed_trace.h"
MACRUM 0:119624335925 29 #include "factory_configurator_client.h"
MACRUM 0:119624335925 30
MACRUM 0:119624335925 31 #define TRACE_GROUP "mClt"
MACRUM 0:119624335925 32
MACRUM 0:119624335925 33 #define INTERNAL_ENDPOINT_PARAM "&iep="
MACRUM 0:119624335925 34 #define DEFAULT_ENDPOINT "endpoint"
MACRUM 0:119624335925 35 #define INTERFACE_ERROR "Client interface is not created. Restart"
MACRUM 0:119624335925 36 #define CREDENTIAL_ERROR "Failed to read credentials from storage"
MACRUM 0:119624335925 37 #define DEVICE_NOT_PROVISIONED "Device not provisioned"
MACRUM 0:119624335925 38 #define ERROR_NO_MEMORY "Not enough memory to stroe LWM2M credentials"
MACRUM 0:119624335925 39
MACRUM 0:119624335925 40 // XXX: nothing here yet
MACRUM 0:119624335925 41 class EventData {
MACRUM 0:119624335925 42
MACRUM 0:119624335925 43 };
MACRUM 0:119624335925 44
MACRUM 0:119624335925 45 ConnectorClient::ConnectorClient(ConnectorClientCallback* callback)
MACRUM 0:119624335925 46 : _callback(callback),
MACRUM 0:119624335925 47 _current_state(State_Bootstrap_Start),
MACRUM 0:119624335925 48 _event_generated(false), _state_engine_running(false),
MACRUM 0:119624335925 49 _interface(NULL), _security(NULL),
MACRUM 0:119624335925 50 _endpoint_info(M2MSecurity::Certificate), _client_objs(NULL),
MACRUM 0:119624335925 51 _rebootstrap_timer(*this), _bootstrap_security_instance(1), _lwm2m_security_instance(0)
MACRUM 0:119624335925 52 {
MACRUM 0:119624335925 53 assert(_callback != NULL);
MACRUM 0:119624335925 54
MACRUM 0:119624335925 55 // Create the lwm2m server security object we need always
MACRUM 0:119624335925 56 _security = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
MACRUM 0:119624335925 57 _interface = M2MInterfaceFactory::create_interface(*this,
MACRUM 0:119624335925 58 DEFAULT_ENDPOINT, // endpoint name string
MACRUM 0:119624335925 59 MBED_CLOUD_CLIENT_ENDPOINT_TYPE, // endpoint type string
MACRUM 0:119624335925 60 MBED_CLOUD_CLIENT_LIFETIME, // lifetime
MACRUM 0:119624335925 61 MBED_CLOUD_CLIENT_LISTEN_PORT, // listen port
MACRUM 0:119624335925 62 _endpoint_info.account_id, // domain string
MACRUM 0:119624335925 63 transport_mode(), // binding mode
MACRUM 0:119624335925 64 M2MInterface::LwIP_IPv4); // network stack
MACRUM 0:119624335925 65
MACRUM 0:119624335925 66 initialize_storage();
MACRUM 0:119624335925 67 }
MACRUM 0:119624335925 68
MACRUM 0:119624335925 69
MACRUM 0:119624335925 70 ConnectorClient::~ConnectorClient()
MACRUM 0:119624335925 71 {
MACRUM 0:119624335925 72 M2MDevice::delete_instance();
MACRUM 0:119624335925 73 M2MSecurity::delete_instance();
MACRUM 0:119624335925 74 delete _interface;
MACRUM 0:119624335925 75 }
MACRUM 0:119624335925 76
MACRUM 0:119624335925 77 void ConnectorClient::start_bootstrap()
MACRUM 0:119624335925 78 {
MACRUM 0:119624335925 79 tr_debug("ConnectorClient::start_bootstrap()");
MACRUM 0:119624335925 80 assert(_callback != NULL);
MACRUM 0:119624335925 81 // Stop rebootstrap timer if it was running
MACRUM 0:119624335925 82 _rebootstrap_timer.stop_timer();
MACRUM 0:119624335925 83 if (create_bootstrap_object()) {
MACRUM 0:119624335925 84 _interface->update_endpoint(_endpoint_info.endpoint_name);
MACRUM 0:119624335925 85 _interface->update_domain(_endpoint_info.account_id);
MACRUM 0:119624335925 86 internal_event(State_Bootstrap_Start);
MACRUM 0:119624335925 87 } else {
MACRUM 0:119624335925 88 tr_error("ConnectorClient::start_bootstrap() - bootstrap object fail");
MACRUM 0:119624335925 89 }
MACRUM 0:119624335925 90 state_engine();
MACRUM 0:119624335925 91 }
MACRUM 0:119624335925 92
MACRUM 0:119624335925 93 void ConnectorClient::start_registration(M2MObjectList* client_objs)
MACRUM 0:119624335925 94 {
MACRUM 0:119624335925 95 tr_debug("ConnectorClient::start_registration()");
MACRUM 0:119624335925 96 assert(_callback != NULL);
MACRUM 0:119624335925 97 _client_objs = client_objs;
MACRUM 0:119624335925 98
MACRUM 0:119624335925 99 // XXX: actually this call should be external_event() to match the pattern used in other m2m classes
MACRUM 0:119624335925 100 create_register_object();
MACRUM 0:119624335925 101 if(_security->get_security_instance_id(M2MSecurity::M2MServer) >= 0) {
MACRUM 0:119624335925 102 if(use_bootstrap()) {
MACRUM 0:119624335925 103 // Bootstrap registration always uses iep
MACRUM 0:119624335925 104 _interface->update_endpoint(_endpoint_info.internal_endpoint_name);
MACRUM 0:119624335925 105 } else {
MACRUM 0:119624335925 106 // Registration without bootstrap always uses external id
MACRUM 0:119624335925 107 _interface->update_endpoint(_endpoint_info.endpoint_name);
MACRUM 0:119624335925 108 }
MACRUM 0:119624335925 109 _interface->update_domain(_endpoint_info.account_id);
MACRUM 0:119624335925 110 internal_event(State_Registration_Start);
MACRUM 0:119624335925 111 } else {
MACRUM 0:119624335925 112 tr_error("ConnectorClient::state_init(): failed to create objs");
MACRUM 0:119624335925 113 _callback->connector_error(M2MInterface::InvalidParameters, INTERFACE_ERROR);
MACRUM 0:119624335925 114 }
MACRUM 0:119624335925 115 state_engine();
MACRUM 0:119624335925 116 }
MACRUM 0:119624335925 117
MACRUM 0:119624335925 118 M2MInterface * ConnectorClient::m2m_interface()
MACRUM 0:119624335925 119 {
MACRUM 0:119624335925 120 return _interface;
MACRUM 0:119624335925 121 }
MACRUM 0:119624335925 122
MACRUM 0:119624335925 123 void ConnectorClient::update_registration()
MACRUM 0:119624335925 124 {
MACRUM 0:119624335925 125 if(_interface && _security && _security->get_security_instance_id(M2MSecurity::M2MServer) >= 0) {
MACRUM 0:119624335925 126 if (_client_objs != NULL) {
MACRUM 0:119624335925 127 _interface->update_registration(_security, *_client_objs);
MACRUM 0:119624335925 128 }
MACRUM 0:119624335925 129 else {
MACRUM 0:119624335925 130 _interface->update_registration(_security);
MACRUM 0:119624335925 131 }
MACRUM 0:119624335925 132 }
MACRUM 0:119624335925 133 }
MACRUM 0:119624335925 134
MACRUM 0:119624335925 135 // generates an internal event. called from within a state
MACRUM 0:119624335925 136 // function to transition to a new state
MACRUM 0:119624335925 137 void ConnectorClient::internal_event(StartupSubStateRegistration new_state)
MACRUM 0:119624335925 138 {
MACRUM 0:119624335925 139 tr_debug("ConnectorClient::internal_event: state: %d -> %d", _current_state, new_state);
MACRUM 0:119624335925 140 _event_generated = true;
MACRUM 0:119624335925 141 _current_state = new_state;
MACRUM 0:119624335925 142
MACRUM 0:119624335925 143 // Avoid recursive chain which eats too much of stack
MACRUM 0:119624335925 144 if (!_state_engine_running) {
MACRUM 0:119624335925 145 state_engine();
MACRUM 0:119624335925 146 }
MACRUM 0:119624335925 147 }
MACRUM 0:119624335925 148
MACRUM 0:119624335925 149 // the state engine executes the state machine states
MACRUM 0:119624335925 150 void ConnectorClient::state_engine(void)
MACRUM 0:119624335925 151 {
MACRUM 0:119624335925 152 tr_debug("ConnectorClient::state_engine");
MACRUM 0:119624335925 153
MACRUM 0:119624335925 154 // this simple flagging gets rid of recursive calls to this method
MACRUM 0:119624335925 155 _state_engine_running = true;
MACRUM 0:119624335925 156
MACRUM 0:119624335925 157 // while events are being generated keep executing states
MACRUM 0:119624335925 158 while (_event_generated) {
MACRUM 0:119624335925 159 _event_generated = false; // event used up, reset flag
MACRUM 0:119624335925 160
MACRUM 0:119624335925 161 state_function(_current_state);
MACRUM 0:119624335925 162 }
MACRUM 0:119624335925 163
MACRUM 0:119624335925 164 _state_engine_running = false;
MACRUM 0:119624335925 165 }
MACRUM 0:119624335925 166
MACRUM 0:119624335925 167 void ConnectorClient::state_function(StartupSubStateRegistration current_state)
MACRUM 0:119624335925 168 {
MACRUM 0:119624335925 169 switch (current_state) {
MACRUM 0:119624335925 170 case State_Bootstrap_Start:
MACRUM 0:119624335925 171 state_bootstrap_start();
MACRUM 0:119624335925 172 break;
MACRUM 0:119624335925 173 case State_Bootstrap_Started:
MACRUM 0:119624335925 174 state_bootstrap_started();
MACRUM 0:119624335925 175 break;
MACRUM 0:119624335925 176 case State_Bootstrap_Success:
MACRUM 0:119624335925 177 state_bootstrap_success();
MACRUM 0:119624335925 178 break;
MACRUM 0:119624335925 179 case State_Bootstrap_Failure:
MACRUM 0:119624335925 180 state_bootstrap_failure();
MACRUM 0:119624335925 181 break;
MACRUM 0:119624335925 182 case State_Registration_Start:
MACRUM 0:119624335925 183 state_registration_start();
MACRUM 0:119624335925 184 break;
MACRUM 0:119624335925 185 case State_Registration_Started:
MACRUM 0:119624335925 186 state_registration_started();
MACRUM 0:119624335925 187 break;
MACRUM 0:119624335925 188 case State_Registration_Success:
MACRUM 0:119624335925 189 state_registration_success();
MACRUM 0:119624335925 190 break;
MACRUM 0:119624335925 191 case State_Registration_Failure:
MACRUM 0:119624335925 192 state_registration_failure();
MACRUM 0:119624335925 193 break;
MACRUM 0:119624335925 194 case State_Unregistered:
MACRUM 0:119624335925 195 state_unregistered();
MACRUM 0:119624335925 196 break;
MACRUM 0:119624335925 197 default:
MACRUM 0:119624335925 198 break;
MACRUM 0:119624335925 199 }
MACRUM 0:119624335925 200 }
MACRUM 0:119624335925 201
MACRUM 0:119624335925 202 /*
MACRUM 0:119624335925 203 * Creates register server object with mbed device server address and other parameters
MACRUM 0:119624335925 204 * required for client to connect to mbed device server.
MACRUM 0:119624335925 205 */
MACRUM 0:119624335925 206 void ConnectorClient::create_register_object()
MACRUM 0:119624335925 207 {
MACRUM 0:119624335925 208 tr_debug("ConnectorClient::create_register_object()");
MACRUM 0:119624335925 209 if(_security && _security->get_security_instance_id(M2MSecurity::M2MServer) == -1) {
MACRUM 0:119624335925 210 _security->create_object_instance(M2MSecurity::M2MServer);
MACRUM 0:119624335925 211 int32_t m2m_id = _security->get_security_instance_id(M2MSecurity::M2MServer);
MACRUM 0:119624335925 212 _security->set_resource_value(M2MSecurity::BootstrapServer, M2MSecurity::M2MServer, m2m_id);
MACRUM 0:119624335925 213 // Add ResourceID's and values to the security ObjectID/ObjectInstance
MACRUM 0:119624335925 214 _security->set_resource_value(M2MSecurity::SecurityMode, _endpoint_info.mode, m2m_id);
MACRUM 0:119624335925 215
MACRUM 0:119624335925 216 // Allocate scratch buffer, this will be used to copy parameters from storage to security object
MACRUM 0:119624335925 217 const int max_size = 2048;
MACRUM 0:119624335925 218 uint8_t *buffer = (uint8_t*)malloc(max_size);
MACRUM 0:119624335925 219 size_t real_size = 0;
MACRUM 0:119624335925 220 bool success = false;
MACRUM 0:119624335925 221 if (buffer != NULL) {
MACRUM 0:119624335925 222 success = true;
MACRUM 0:119624335925 223 }
MACRUM 0:119624335925 224
MACRUM 0:119624335925 225 // Connector CA
MACRUM 0:119624335925 226 if (success) {
MACRUM 0:119624335925 227 success = false;
MACRUM 0:119624335925 228 if (get_config_certificate(g_fcc_lwm2m_server_ca_certificate_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 229 tr_info("ConnectorClient::create_register_object - ServerPublicKey %d", (int)real_size);
MACRUM 0:119624335925 230 success = true;
MACRUM 0:119624335925 231 _security->set_resource_value(M2MSecurity::ServerPublicKey,
MACRUM 0:119624335925 232 buffer,
MACRUM 0:119624335925 233 (uint32_t)real_size,
MACRUM 0:119624335925 234 m2m_id);
MACRUM 0:119624335925 235 }
MACRUM 0:119624335925 236 else {
MACRUM 0:119624335925 237 tr_error("KEY_CONNECTOR_CA cert failed.");
MACRUM 0:119624335925 238 }
MACRUM 0:119624335925 239 }
MACRUM 0:119624335925 240
MACRUM 0:119624335925 241 // Connector device public key
MACRUM 0:119624335925 242 if (success) {
MACRUM 0:119624335925 243 success = false;
MACRUM 0:119624335925 244 if (get_config_certificate(g_fcc_lwm2m_device_certificate_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 245 tr_info("ConnectorClient::create_register_object - PublicKey %d", (int)real_size);
MACRUM 0:119624335925 246 success = true;
MACRUM 0:119624335925 247 _security->set_resource_value(M2MSecurity::PublicKey, buffer, (uint32_t)real_size, m2m_id);
MACRUM 0:119624335925 248 }
MACRUM 0:119624335925 249 else {
MACRUM 0:119624335925 250 tr_error("KEY_CONNECTOR__DEVICE_CERT failed.");
MACRUM 0:119624335925 251 }
MACRUM 0:119624335925 252 }
MACRUM 0:119624335925 253
MACRUM 0:119624335925 254 // Connector device private key
MACRUM 0:119624335925 255 if (success) {
MACRUM 0:119624335925 256 success = false;
MACRUM 0:119624335925 257 if (get_config_private_key(g_fcc_lwm2m_device_private_key_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 258 tr_info("ConnectorClient::create_register_object - SecretKey %d", (int)real_size);
MACRUM 0:119624335925 259 success = true;
MACRUM 0:119624335925 260 _security->set_resource_value(M2MSecurity::Secretkey, buffer, (uint32_t)real_size, m2m_id);
MACRUM 0:119624335925 261 }
MACRUM 0:119624335925 262 else
MACRUM 0:119624335925 263 tr_error("KEY_CONNECTOR_DEVICE_PRIV failed.");
MACRUM 0:119624335925 264 }
MACRUM 0:119624335925 265
MACRUM 0:119624335925 266 // Connector URL
MACRUM 0:119624335925 267 if (success) {
MACRUM 0:119624335925 268 success = false;
MACRUM 0:119624335925 269 if (get_config_parameter(g_fcc_lwm2m_server_uri_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 270 tr_info("ConnectorClient::create_register_object - M2MServerUri %.*s", (int)real_size, buffer);
MACRUM 0:119624335925 271 success = true;
MACRUM 0:119624335925 272 _security->set_resource_value(M2MSecurity::M2MServerUri, buffer, (uint32_t)real_size, m2m_id);
MACRUM 0:119624335925 273 }
MACRUM 0:119624335925 274 else
MACRUM 0:119624335925 275 tr_error("KEY_CONNECTOR_URL failed.");
MACRUM 0:119624335925 276 }
MACRUM 0:119624335925 277
MACRUM 0:119624335925 278 // Endpoint
MACRUM 0:119624335925 279 if (success) {
MACRUM 0:119624335925 280 success = false;
MACRUM 0:119624335925 281 if (get_config_parameter(g_fcc_endpoint_parameter_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 282 tr_info("ConnectorClient::create_register_object - endpoint name %.*s", (int)real_size, buffer);
MACRUM 0:119624335925 283 success = true;
MACRUM 0:119624335925 284 _endpoint_info.endpoint_name = String((const char*)buffer, real_size);
MACRUM 0:119624335925 285 }
MACRUM 0:119624335925 286 else
MACRUM 0:119624335925 287 tr_error("KEY_ENDPOINT_NAME failed.");
MACRUM 0:119624335925 288 }
MACRUM 0:119624335925 289
MACRUM 0:119624335925 290 // Try to get internal endpoint name
MACRUM 0:119624335925 291 if (success) {
MACRUM 0:119624335925 292 if (get_config_parameter(KEY_INTERNAL_ENDPOINT, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 293 _endpoint_info.internal_endpoint_name = String((const char*)buffer, real_size);
MACRUM 0:119624335925 294 tr_info("Using internal endpoint name instead: %s", _endpoint_info.internal_endpoint_name.c_str());
MACRUM 0:119624335925 295 }
MACRUM 0:119624335925 296 else {
MACRUM 0:119624335925 297 tr_debug("KEY_INTERNAL_ENDPOINT failed.");
MACRUM 0:119624335925 298 }
MACRUM 0:119624335925 299 }
MACRUM 0:119624335925 300
MACRUM 0:119624335925 301 // Account ID, not mandatory
MACRUM 0:119624335925 302 if (success) {
MACRUM 0:119624335925 303 if (get_config_parameter(KEY_ACCOUNT_ID, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 304 tr_info("ConnectorClient::create_register_object - AccountId %.*s", (int)real_size, buffer);
MACRUM 0:119624335925 305 _endpoint_info.account_id = String((const char*)buffer, real_size);
MACRUM 0:119624335925 306 }
MACRUM 0:119624335925 307 else
MACRUM 0:119624335925 308 tr_debug("KEY_ACCOUNT_ID failed.");
MACRUM 0:119624335925 309 }
MACRUM 0:119624335925 310
MACRUM 0:119624335925 311 free(buffer);
MACRUM 0:119624335925 312 if (!success) {
MACRUM 0:119624335925 313 tr_error("ConnectorClient::create_register_object - Failed to read credentials");
MACRUM 0:119624335925 314 _callback->connector_error((M2MInterface::Error)MbedCloudClient::ConnectorFailedToReadCredentials,CREDENTIAL_ERROR);
MACRUM 0:119624335925 315 // TODO: what to do with the m2mserver security instance
MACRUM 0:119624335925 316 }
MACRUM 0:119624335925 317 } else {
MACRUM 0:119624335925 318 tr_info("ConnectorClient::create_register_object() - Credentials already exists");
MACRUM 0:119624335925 319 }
MACRUM 0:119624335925 320 }
MACRUM 0:119624335925 321
MACRUM 0:119624335925 322 /*
MACRUM 0:119624335925 323 * Creates bootstrap server object with bootstrap server address and other parameters
MACRUM 0:119624335925 324 * required for connecting to mbed Cloud bootstrap server.
MACRUM 0:119624335925 325 */
MACRUM 0:119624335925 326 bool ConnectorClient::create_bootstrap_object()
MACRUM 0:119624335925 327 {
MACRUM 0:119624335925 328 tr_debug("ConnectorClient::create_bootstrap_object");
MACRUM 0:119624335925 329 bool success = false;
MACRUM 0:119624335925 330
MACRUM 0:119624335925 331 // Check if bootstrap credentials are already stored in KCM
MACRUM 0:119624335925 332 if (bootstrap_credentials_stored_in_kcm() && _security) {
MACRUM 0:119624335925 333 if (_security->get_security_instance_id(M2MSecurity::Bootstrap) == -1) {
MACRUM 0:119624335925 334 _security->create_object_instance(M2MSecurity::Bootstrap);
MACRUM 0:119624335925 335 int32_t bs_id = _security->get_security_instance_id(M2MSecurity::Bootstrap);
MACRUM 0:119624335925 336 _security->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate, bs_id);
MACRUM 0:119624335925 337 tr_info("ConnectorClient::create_bootstrap_object - bs_id = %d", bs_id);
MACRUM 0:119624335925 338 tr_info("ConnectorClient::create_bootstrap_object - use credentials from storage");
MACRUM 0:119624335925 339
MACRUM 0:119624335925 340 // Allocate scratch buffer, this will be used to copy parameters from storage to security object
MACRUM 0:119624335925 341 size_t real_size = 0;
MACRUM 0:119624335925 342 const int max_size = 2048;
MACRUM 0:119624335925 343 uint8_t *buffer = (uint8_t*)malloc(max_size);
MACRUM 0:119624335925 344 if (buffer != NULL) {
MACRUM 0:119624335925 345 success = true;
MACRUM 0:119624335925 346 }
MACRUM 0:119624335925 347
MACRUM 0:119624335925 348 // Read internal endpoint name if it exists, we need to append
MACRUM 0:119624335925 349 // it to bootstrap uri if device already bootstrapped
MACRUM 0:119624335925 350 uint8_t *iep = NULL;
MACRUM 0:119624335925 351 if (success && get_config_parameter_string(KEY_INTERNAL_ENDPOINT, buffer, max_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 352 iep = (uint8_t*)malloc(strlen((const char*)buffer) + strlen(INTERNAL_ENDPOINT_PARAM) + 1);
MACRUM 0:119624335925 353 if (iep != NULL) {
MACRUM 0:119624335925 354 strcpy((char*)iep, INTERNAL_ENDPOINT_PARAM);
MACRUM 0:119624335925 355 strcat((char*)iep, (const char*)buffer);
MACRUM 0:119624335925 356 tr_info("ConnectorClient::create_bootstrap_object - iep: %s", buffer);
MACRUM 0:119624335925 357 }
MACRUM 0:119624335925 358 //TODO: Should handle error if iep exists but allocation fails?
MACRUM 0:119624335925 359 }
MACRUM 0:119624335925 360
MACRUM 0:119624335925 361 // Bootstrap URI
MACRUM 0:119624335925 362 if (success) {
MACRUM 0:119624335925 363 success = false;
MACRUM 0:119624335925 364 if (get_config_parameter_string(g_fcc_bootstrap_server_uri_name, buffer, max_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 365 success = true;
MACRUM 0:119624335925 366
MACRUM 0:119624335925 367 real_size = strlen((const char*)buffer);
MACRUM 0:119624335925 368 // Append iep if we 1. have it 2. it doesn't already exist in uri 3. it fits
MACRUM 0:119624335925 369 if (iep &&
MACRUM 0:119624335925 370 strstr((const char*)buffer, (const char*)iep) == NULL &&
MACRUM 0:119624335925 371 (real_size + strlen((const char*)iep) + 1) <= max_size) {
MACRUM 0:119624335925 372 strcat((char*)buffer, (const char*)iep);
MACRUM 0:119624335925 373 real_size += strlen((const char*)iep) + 1;
MACRUM 0:119624335925 374 }
MACRUM 0:119624335925 375
MACRUM 0:119624335925 376 tr_info("ConnectorClient::create_bootstrap_object - M2MServerUri %.*s", (int)real_size, buffer);
MACRUM 0:119624335925 377 _security->set_resource_value(M2MSecurity::M2MServerUri, buffer, real_size, bs_id);
MACRUM 0:119624335925 378 }
MACRUM 0:119624335925 379 }
MACRUM 0:119624335925 380
MACRUM 0:119624335925 381 free(iep);
MACRUM 0:119624335925 382
MACRUM 0:119624335925 383 // Bootstrap server public key (certificate)
MACRUM 0:119624335925 384 if (success) {
MACRUM 0:119624335925 385 success = false;
MACRUM 0:119624335925 386 if (get_config_certificate(g_fcc_bootstrap_server_ca_certificate_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 387 success = true;
MACRUM 0:119624335925 388 tr_info("ConnectorClient::create_bootstrap_object - ServerPublicKey %d", (int)real_size);
MACRUM 0:119624335925 389 _security->set_resource_value(M2MSecurity::ServerPublicKey, buffer, real_size, bs_id);
MACRUM 0:119624335925 390 }
MACRUM 0:119624335925 391 }
MACRUM 0:119624335925 392
MACRUM 0:119624335925 393 // Bootstrap client public key (certificate)
MACRUM 0:119624335925 394 if (success) {
MACRUM 0:119624335925 395 success = false;
MACRUM 0:119624335925 396 if (get_config_certificate(g_fcc_bootstrap_device_certificate_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 397 success = true;
MACRUM 0:119624335925 398 tr_info("ConnectorClient::create_bootstrap_object - PublicKey %d", (int)real_size);
MACRUM 0:119624335925 399 _security->set_resource_value(M2MSecurity::PublicKey, buffer, real_size, bs_id);
MACRUM 0:119624335925 400 }
MACRUM 0:119624335925 401 }
MACRUM 0:119624335925 402
MACRUM 0:119624335925 403 // Bootstrap client private key
MACRUM 0:119624335925 404 if (success) {
MACRUM 0:119624335925 405 success = false;
MACRUM 0:119624335925 406 if (get_config_private_key(g_fcc_bootstrap_device_private_key_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 407 success = true;
MACRUM 0:119624335925 408 tr_info("ConnectorClient::create_bootstrap_object - Secretkey %d", (int)real_size);
MACRUM 0:119624335925 409 _security->set_resource_value(M2MSecurity::Secretkey, buffer, real_size, bs_id);
MACRUM 0:119624335925 410 }
MACRUM 0:119624335925 411 }
MACRUM 0:119624335925 412
MACRUM 0:119624335925 413 // Endpoint
MACRUM 0:119624335925 414 if (success) {
MACRUM 0:119624335925 415 success = false;
MACRUM 0:119624335925 416 if (get_config_parameter(g_fcc_endpoint_parameter_name, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 417 success = true;
MACRUM 0:119624335925 418 _endpoint_info.endpoint_name = String((const char*)buffer, real_size);
MACRUM 0:119624335925 419 tr_info("ConnectorClient::create_bootstrap_object - Endpoint %s", _endpoint_info.endpoint_name.c_str());
MACRUM 0:119624335925 420 }
MACRUM 0:119624335925 421 }
MACRUM 0:119624335925 422
MACRUM 0:119624335925 423 // Account ID, not mandatory
MACRUM 0:119624335925 424 if (success) {
MACRUM 0:119624335925 425 if (get_config_parameter(KEY_ACCOUNT_ID, buffer, max_size, &real_size) == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 426 _endpoint_info.account_id = String((const char*)buffer, real_size);
MACRUM 0:119624335925 427 tr_info("ConnectorClient::create_bootstrap_object - AccountId %s", _endpoint_info.account_id.c_str());
MACRUM 0:119624335925 428 }
MACRUM 0:119624335925 429 }
MACRUM 0:119624335925 430 free(buffer);
MACRUM 0:119624335925 431
MACRUM 0:119624335925 432 if (!success) {
MACRUM 0:119624335925 433 tr_error("ConnectorClient::create_bootstrap_object - Failed to read credentials");
MACRUM 0:119624335925 434 _callback->connector_error((M2MInterface::Error)MbedCloudClient::ConnectorFailedToReadCredentials,CREDENTIAL_ERROR);
MACRUM 0:119624335925 435 _security->remove_object_instance(bs_id);
MACRUM 0:119624335925 436 }
MACRUM 0:119624335925 437 } else {
MACRUM 0:119624335925 438 success = true;
MACRUM 0:119624335925 439 tr_info("ConnectorClient::create_bootstrap_object - bootstrap object already done");
MACRUM 0:119624335925 440 }
MACRUM 0:119624335925 441 // Device not provisioned
MACRUM 0:119624335925 442 } else {
MACRUM 0:119624335925 443 _callback->connector_error((M2MInterface::Error)MbedCloudClient::ConnectorInvalidCredentials, DEVICE_NOT_PROVISIONED);
MACRUM 0:119624335925 444 tr_error("ConnectorClient::create_bootstrap_object - device not provisioned!");
MACRUM 0:119624335925 445 }
MACRUM 0:119624335925 446 return success;
MACRUM 0:119624335925 447 }
MACRUM 0:119624335925 448
MACRUM 0:119624335925 449 void ConnectorClient::state_bootstrap_start()
MACRUM 0:119624335925 450 {
MACRUM 0:119624335925 451 tr_info("ConnectorClient::state_bootstrap_start()");
MACRUM 0:119624335925 452 assert(_interface != NULL);
MACRUM 0:119624335925 453 assert(_security != NULL);
MACRUM 0:119624335925 454
MACRUM 0:119624335925 455 _interface->bootstrap(_security);
MACRUM 0:119624335925 456
MACRUM 0:119624335925 457 internal_event(State_Bootstrap_Started);
MACRUM 0:119624335925 458 }
MACRUM 0:119624335925 459
MACRUM 0:119624335925 460 void ConnectorClient::state_bootstrap_started()
MACRUM 0:119624335925 461 {
MACRUM 0:119624335925 462 // this state may be useful only for verifying the callbacks?
MACRUM 0:119624335925 463 }
MACRUM 0:119624335925 464
MACRUM 0:119624335925 465 void ConnectorClient::state_bootstrap_success()
MACRUM 0:119624335925 466 {
MACRUM 0:119624335925 467 assert(_callback != NULL);
MACRUM 0:119624335925 468 // Parse internal endpoint name from mDS cert
MACRUM 0:119624335925 469 _callback->registration_process_result(State_Bootstrap_Success);
MACRUM 0:119624335925 470 }
MACRUM 0:119624335925 471
MACRUM 0:119624335925 472 void ConnectorClient::state_bootstrap_failure()
MACRUM 0:119624335925 473 {
MACRUM 0:119624335925 474 assert(_callback != NULL);
MACRUM 0:119624335925 475 // maybe some additional canceling and/or leanup is needed here?
MACRUM 0:119624335925 476 _callback->registration_process_result(State_Bootstrap_Failure);
MACRUM 0:119624335925 477 }
MACRUM 0:119624335925 478
MACRUM 0:119624335925 479 void ConnectorClient::state_registration_start()
MACRUM 0:119624335925 480 {
MACRUM 0:119624335925 481 tr_info("ConnectorClient::state_registration_start()");
MACRUM 0:119624335925 482 assert(_interface != NULL);
MACRUM 0:119624335925 483 assert(_security != NULL);
MACRUM 0:119624335925 484 _interface->register_object(_security, *_client_objs);
MACRUM 0:119624335925 485 internal_event(State_Registration_Started);
MACRUM 0:119624335925 486 }
MACRUM 0:119624335925 487
MACRUM 0:119624335925 488 void ConnectorClient::state_registration_started()
MACRUM 0:119624335925 489 {
MACRUM 0:119624335925 490 // this state may be useful only for verifying the callbacks?
MACRUM 0:119624335925 491 }
MACRUM 0:119624335925 492
MACRUM 0:119624335925 493 void ConnectorClient::state_registration_success()
MACRUM 0:119624335925 494 {
MACRUM 0:119624335925 495 assert(_callback != NULL);
MACRUM 0:119624335925 496 _endpoint_info.internal_endpoint_name = _interface->internal_endpoint_name();
MACRUM 0:119624335925 497 //The endpoint is maximum 32 character long, we put bigger buffer for future extensions
MACRUM 0:119624335925 498 size_t real_size = 0;
MACRUM 0:119624335925 499
MACRUM 0:119624335925 500 //If this returns success, don't do anything else delete old value and set new one
MACRUM 0:119624335925 501 if (size_config_parameter(KEY_INTERNAL_ENDPOINT, &real_size) != CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 502 delete_config_parameter(KEY_INTERNAL_ENDPOINT);
MACRUM 0:119624335925 503 set_config_parameter(KEY_INTERNAL_ENDPOINT,(const uint8_t*)_endpoint_info.internal_endpoint_name.c_str(),
MACRUM 0:119624335925 504 (size_t)_endpoint_info.internal_endpoint_name.size());
MACRUM 0:119624335925 505 }
MACRUM 0:119624335925 506 _callback->registration_process_result(State_Registration_Success);
MACRUM 0:119624335925 507 }
MACRUM 0:119624335925 508
MACRUM 0:119624335925 509 void ConnectorClient::state_registration_failure()
MACRUM 0:119624335925 510 {
MACRUM 0:119624335925 511 assert(_callback != NULL);
MACRUM 0:119624335925 512 // maybe some additional canceling and/or leanup is needed here?
MACRUM 0:119624335925 513 _callback->registration_process_result(State_Registration_Failure);
MACRUM 0:119624335925 514 }
MACRUM 0:119624335925 515
MACRUM 0:119624335925 516 void ConnectorClient::state_unregistered()
MACRUM 0:119624335925 517 {
MACRUM 0:119624335925 518 assert(_callback != NULL);
MACRUM 0:119624335925 519 _callback->registration_process_result(State_Unregistered);
MACRUM 0:119624335925 520 }
MACRUM 0:119624335925 521
MACRUM 0:119624335925 522 void ConnectorClient::bootstrap_done(M2MSecurity *security_object)
MACRUM 0:119624335925 523 {
MACRUM 0:119624335925 524 tr_info("ConnectorClient::bootstrap_done");
MACRUM 0:119624335925 525 ccs_status_e status = CCS_STATUS_ERROR;
MACRUM 0:119624335925 526 StartupSubStateRegistration state = State_Bootstrap_Success;
MACRUM 0:119624335925 527 if(security_object) {
MACRUM 0:119624335925 528 // Update bootstrap credentials (we could skip this if we knew whether they were updated)
MACRUM 0:119624335925 529 // This will also update the address in case of first to claim
MACRUM 0:119624335925 530 status = set_bootstrap_credentials(security_object);
MACRUM 0:119624335925 531 if (status != CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 532 // TODO: what now?
MACRUM 0:119624335925 533 tr_error("ConnectorClient::bootstrap_done - couldn't store bootstrap credentials");
MACRUM 0:119624335925 534 }
MACRUM 0:119624335925 535
MACRUM 0:119624335925 536 // Clear the first to claim flag if it's active
MACRUM 0:119624335925 537 if (is_first_to_claim()) {
MACRUM 0:119624335925 538 status = clear_first_to_claim();
MACRUM 0:119624335925 539 if (status != CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 540 // TODO: what now?
MACRUM 0:119624335925 541 tr_error("ConnectorClient::bootstrap_done - couldn't clear first to claim flag!");
MACRUM 0:119624335925 542 }
MACRUM 0:119624335925 543 }
MACRUM 0:119624335925 544
MACRUM 0:119624335925 545 // Bootstrap might delete m2mserver security object instance completely to force bootstrap
MACRUM 0:119624335925 546 // with new credentials, in that case delete the stored lwm2m credentials as well and re-bootstrap
MACRUM 0:119624335925 547 if (security_object->get_security_instance_id(M2MSecurity::M2MServer) == -1) {
MACRUM 0:119624335925 548 tr_info("ConnectorClient::bootstrap_done() - Clearing lwm2m credentials");
MACRUM 0:119624335925 549 // delete the old connector credentials when BS sends re-direction.
MACRUM 0:119624335925 550 delete_config_parameter(g_fcc_lwm2m_server_uri_name);
MACRUM 0:119624335925 551 delete_config_certificate(g_fcc_lwm2m_server_ca_certificate_name);
MACRUM 0:119624335925 552 delete_config_certificate(g_fcc_lwm2m_device_certificate_name);
MACRUM 0:119624335925 553 delete_config_private_key(g_fcc_lwm2m_device_private_key_name);
MACRUM 0:119624335925 554 // Start re-bootstrap timer
MACRUM 0:119624335925 555 tr_info("ConnectorClient::bootstrap_done() - Re-directing bootstrap in 100 milliseconds");
MACRUM 0:119624335925 556 _rebootstrap_timer.start_timer(100, M2MTimerObserver::BootstrapFlowTimer, true);
MACRUM 0:119624335925 557 return;
MACRUM 0:119624335925 558 }
MACRUM 0:119624335925 559 // Bootstrap wrote M2MServer credentials, store them and also update first to claim status if it's configured
MACRUM 0:119624335925 560 else {
MACRUM 0:119624335925 561 tr_info("ConnectorClient::bootstrap_done() - Storing lwm2m credentials");
MACRUM 0:119624335925 562 status = set_connector_credentials(security_object);
MACRUM 0:119624335925 563 }
MACRUM 0:119624335925 564 }
MACRUM 0:119624335925 565 if (status != CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 566 internal_event(State_Bootstrap_Failure);
MACRUM 0:119624335925 567 //Failed to store credentials, bootstrap failed
MACRUM 0:119624335925 568 _callback->connector_error(M2MInterface::MemoryFail, ERROR_NO_MEMORY); // Translated to error code ConnectMemoryConnectFail
MACRUM 0:119624335925 569 return;
MACRUM 0:119624335925 570 } else {
MACRUM 0:119624335925 571 tr_error("ConnectorClient::bootstrap_done - set_credentials status %d", status);
MACRUM 0:119624335925 572 }
MACRUM 0:119624335925 573 internal_event(state);
MACRUM 0:119624335925 574 }
MACRUM 0:119624335925 575
MACRUM 0:119624335925 576 void ConnectorClient::object_registered(M2MSecurity *security_object, const M2MServer &server_object)
MACRUM 0:119624335925 577 {
MACRUM 0:119624335925 578 internal_event(State_Registration_Success);
MACRUM 0:119624335925 579 }
MACRUM 0:119624335925 580
MACRUM 0:119624335925 581 void ConnectorClient::object_unregistered(M2MSecurity *server_object)
MACRUM 0:119624335925 582 {
MACRUM 0:119624335925 583 internal_event(State_Unregistered);
MACRUM 0:119624335925 584 }
MACRUM 0:119624335925 585
MACRUM 0:119624335925 586 void ConnectorClient::registration_updated(M2MSecurity *security_object, const M2MServer & server_object)
MACRUM 0:119624335925 587 {
MACRUM 0:119624335925 588 _callback->registration_process_result(State_Registration_Updated);
MACRUM 0:119624335925 589 }
MACRUM 0:119624335925 590
MACRUM 0:119624335925 591 void ConnectorClient::error(M2MInterface::Error error)
MACRUM 0:119624335925 592 {
MACRUM 0:119624335925 593 tr_error("ConnectorClient::error() - error: %d", error);
MACRUM 0:119624335925 594 assert(_callback != NULL);
MACRUM 0:119624335925 595 if (_current_state >= State_Registration_Start &&
MACRUM 0:119624335925 596 use_bootstrap() &&
MACRUM 0:119624335925 597 (error == M2MInterface::SecureConnectionFailed ||
MACRUM 0:119624335925 598 error == M2MInterface::InvalidParameters)) {
MACRUM 0:119624335925 599 tr_info("ConnectorClient::error() - Error during lwm2m registration");
MACRUM 0:119624335925 600 tr_info("ConnectorClient::error() - Clearing lwm2m credentials");
MACRUM 0:119624335925 601 // delete the old connector credentials when DTLS handshake fails or
MACRUM 0:119624335925 602 // server rejects the registration.
MACRUM 0:119624335925 603 delete_config_parameter(g_fcc_lwm2m_server_uri_name);
MACRUM 0:119624335925 604 delete_config_certificate(g_fcc_lwm2m_server_ca_certificate_name);
MACRUM 0:119624335925 605 delete_config_certificate(g_fcc_lwm2m_device_certificate_name);
MACRUM 0:119624335925 606 delete_config_private_key(g_fcc_lwm2m_device_private_key_name);
MACRUM 0:119624335925 607 // Delete the lwm2m security instance
MACRUM 0:119624335925 608 int32_t id = _security->get_security_instance_id(M2MSecurity::M2MServer);
MACRUM 0:119624335925 609 if (id >= 0) {
MACRUM 0:119624335925 610 _security->remove_object_instance(id);
MACRUM 0:119624335925 611 }
MACRUM 0:119624335925 612 // Delete bootstrap security instance
MACRUM 0:119624335925 613 id = _security->get_security_instance_id(M2MSecurity::Bootstrap);
MACRUM 0:119624335925 614 if (id >= 0) {
MACRUM 0:119624335925 615 _security->remove_object_instance(id);
MACRUM 0:119624335925 616 }
MACRUM 0:119624335925 617 // Start re-bootstrap timer
MACRUM 0:119624335925 618 tr_info("ConnectorClient::error() - Re-bootstrapping in 100 milliseconds");
MACRUM 0:119624335925 619 _rebootstrap_timer.start_timer(100, M2MTimerObserver::BootstrapFlowTimer, true);
MACRUM 0:119624335925 620 }
MACRUM 0:119624335925 621 else {
MACRUM 0:119624335925 622 _callback->connector_error(error, _interface->error_description());
MACRUM 0:119624335925 623 }
MACRUM 0:119624335925 624 }
MACRUM 0:119624335925 625
MACRUM 0:119624335925 626 void ConnectorClient::value_updated(M2MBase *base, M2MBase::BaseType type)
MACRUM 0:119624335925 627 {
MACRUM 0:119624335925 628 assert(_callback != NULL);
MACRUM 0:119624335925 629 _callback->value_updated(base, type);
MACRUM 0:119624335925 630 }
MACRUM 0:119624335925 631
MACRUM 0:119624335925 632 bool ConnectorClient::connector_credentials_available()
MACRUM 0:119624335925 633 {
MACRUM 0:119624335925 634 tr_debug("ConnectorClient::connector_credentials_available");
MACRUM 0:119624335925 635 const int max_size = 2048;
MACRUM 0:119624335925 636 uint8_t *buffer = (uint8_t*)malloc(max_size);
MACRUM 0:119624335925 637 size_t real_size = 0;
MACRUM 0:119624335925 638 get_config_private_key(g_fcc_lwm2m_device_private_key_name, buffer, max_size, &real_size);
MACRUM 0:119624335925 639 free(buffer);
MACRUM 0:119624335925 640 if (real_size > 0) {
MACRUM 0:119624335925 641 return true;
MACRUM 0:119624335925 642 }
MACRUM 0:119624335925 643 return false;
MACRUM 0:119624335925 644 }
MACRUM 0:119624335925 645
MACRUM 0:119624335925 646 bool ConnectorClient::use_bootstrap()
MACRUM 0:119624335925 647 {
MACRUM 0:119624335925 648 tr_debug("ConnectorClient::use_bootstrap");
MACRUM 0:119624335925 649 const int max_size = 32;
MACRUM 0:119624335925 650 uint8_t *buffer = (uint8_t*)malloc(max_size);
MACRUM 0:119624335925 651 bool ret = false;
MACRUM 0:119624335925 652 if (buffer != NULL) {
MACRUM 0:119624335925 653 memset(buffer, 0, max_size);
MACRUM 0:119624335925 654 size_t real_size = 0;
MACRUM 0:119624335925 655 ccs_status_e status = get_config_parameter(g_fcc_use_bootstrap_parameter_name, buffer, max_size, &real_size);
MACRUM 0:119624335925 656 if (status == CCS_STATUS_SUCCESS && real_size > 0 && buffer[0] > 0) {
MACRUM 0:119624335925 657 ret = true;
MACRUM 0:119624335925 658 }
MACRUM 0:119624335925 659 free(buffer);
MACRUM 0:119624335925 660 }
MACRUM 0:119624335925 661 return ret;
MACRUM 0:119624335925 662 }
MACRUM 0:119624335925 663
MACRUM 0:119624335925 664
MACRUM 0:119624335925 665 bool ConnectorClient::get_key(const char *key, const char *endpoint, char *&key_name)
MACRUM 0:119624335925 666 {
MACRUM 0:119624335925 667 if(key_name) {
MACRUM 0:119624335925 668 free(key_name);
MACRUM 0:119624335925 669 key_name = NULL;
MACRUM 0:119624335925 670 }
MACRUM 0:119624335925 671
MACRUM 0:119624335925 672 key_name = (char*)malloc(strlen(key)+strlen(endpoint)+1);
MACRUM 0:119624335925 673 if(key_name) {
MACRUM 0:119624335925 674 strcpy(key_name, key);
MACRUM 0:119624335925 675 strcat(key_name, endpoint);
MACRUM 0:119624335925 676 tr_debug("key %s", key_name);
MACRUM 0:119624335925 677 return true;
MACRUM 0:119624335925 678 }
MACRUM 0:119624335925 679 return false;
MACRUM 0:119624335925 680 }
MACRUM 0:119624335925 681
MACRUM 0:119624335925 682 ccs_status_e ConnectorClient::set_connector_credentials(M2MSecurity *security)
MACRUM 0:119624335925 683 {
MACRUM 0:119624335925 684 tr_debug("ConnectorClient::set_connector_credentials");
MACRUM 0:119624335925 685 ccs_status_e status = CCS_STATUS_ERROR;
MACRUM 0:119624335925 686
MACRUM 0:119624335925 687 const uint8_t *srv_public_key = NULL;
MACRUM 0:119624335925 688 const uint8_t *public_key = NULL;
MACRUM 0:119624335925 689 const uint8_t *sec_key = NULL;
MACRUM 0:119624335925 690
MACRUM 0:119624335925 691 int32_t m2m_id = security->get_security_instance_id(M2MSecurity::M2MServer);
MACRUM 0:119624335925 692 if (m2m_id == -1) {
MACRUM 0:119624335925 693 return status;
MACRUM 0:119624335925 694 }
MACRUM 0:119624335925 695
MACRUM 0:119624335925 696 uint32_t srv_public_key_size = security->resource_value_buffer(M2MSecurity::ServerPublicKey, srv_public_key, m2m_id);
MACRUM 0:119624335925 697 uint32_t public_key_size = security->resource_value_buffer(M2MSecurity::PublicKey, public_key, m2m_id);
MACRUM 0:119624335925 698 uint32_t sec_key_size = security->resource_value_buffer(M2MSecurity::Secretkey, sec_key, m2m_id);
MACRUM 0:119624335925 699
MACRUM 0:119624335925 700 if(srv_public_key && public_key && sec_key) {
MACRUM 0:119624335925 701 // Parse common name
MACRUM 0:119624335925 702 char common_name[64];
MACRUM 0:119624335925 703 memset(common_name, 0, 64);
MACRUM 0:119624335925 704 if (extract_cn_from_certificate(public_key, public_key_size, common_name)){
MACRUM 0:119624335925 705 tr_info("ConnectorClient::set_connector_credentials - CN: %s", common_name);
MACRUM 0:119624335925 706 _endpoint_info.internal_endpoint_name = String(common_name);
MACRUM 0:119624335925 707 delete_config_parameter(KEY_INTERNAL_ENDPOINT);
MACRUM 0:119624335925 708 status = set_config_parameter(KEY_INTERNAL_ENDPOINT,(uint8_t*)common_name, strlen(common_name));
MACRUM 0:119624335925 709 }
MACRUM 0:119624335925 710
MACRUM 0:119624335925 711 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 712 delete_config_certificate(g_fcc_lwm2m_server_ca_certificate_name);
MACRUM 0:119624335925 713 status = set_config_certificate(g_fcc_lwm2m_server_ca_certificate_name,
MACRUM 0:119624335925 714 srv_public_key,
MACRUM 0:119624335925 715 (size_t)srv_public_key_size);
MACRUM 0:119624335925 716 }
MACRUM 0:119624335925 717 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 718 status = set_config_certificate(g_fcc_lwm2m_device_certificate_name,
MACRUM 0:119624335925 719 public_key,
MACRUM 0:119624335925 720 (size_t)public_key_size);
MACRUM 0:119624335925 721 }
MACRUM 0:119624335925 722 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 723 status = set_config_private_key(g_fcc_lwm2m_device_private_key_name,
MACRUM 0:119624335925 724 sec_key,
MACRUM 0:119624335925 725 (size_t)sec_key_size);
MACRUM 0:119624335925 726 }
MACRUM 0:119624335925 727
MACRUM 0:119624335925 728 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 729 delete_config_parameter(KEY_ACCOUNT_ID);
MACRUM 0:119624335925 730 // AccountID optional so don't fail if unable to store
MACRUM 0:119624335925 731 set_config_parameter(KEY_ACCOUNT_ID,
MACRUM 0:119624335925 732 (const uint8_t*)_endpoint_info.account_id.c_str(),
MACRUM 0:119624335925 733 (size_t)_endpoint_info.account_id.size());
MACRUM 0:119624335925 734 }
MACRUM 0:119624335925 735 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 736 status = set_config_parameter(g_fcc_lwm2m_server_uri_name,
MACRUM 0:119624335925 737 (const uint8_t*)security->resource_value_string(M2MSecurity::M2MServerUri, m2m_id).c_str(),
MACRUM 0:119624335925 738 (size_t)security->resource_value_string(M2MSecurity::M2MServerUri, m2m_id).size());
MACRUM 0:119624335925 739 }
MACRUM 0:119624335925 740 M2MDevice *device = M2MInterfaceFactory::create_device();
MACRUM 0:119624335925 741 if (device) {
MACRUM 0:119624335925 742 String temp = "";
MACRUM 0:119624335925 743 uint32_t currenttime = (uint32_t)device->resource_value_int(M2MDevice::CurrentTime, 0);
MACRUM 0:119624335925 744 uint8_t data[4];
MACRUM 0:119624335925 745 memcpy(data, &currenttime, 4);
MACRUM 0:119624335925 746 delete_config_parameter(g_fcc_current_time_parameter_name);
MACRUM 0:119624335925 747 set_config_parameter(g_fcc_current_time_parameter_name, data, 4);
MACRUM 0:119624335925 748
MACRUM 0:119624335925 749 temp = device->resource_value_string(M2MDevice::Timezone, 0);
MACRUM 0:119624335925 750 delete_config_parameter(g_fcc_device_time_zone_parameter_name);
MACRUM 0:119624335925 751 set_config_parameter(g_fcc_device_time_zone_parameter_name, (const uint8_t*)temp.c_str(), temp.size());
MACRUM 0:119624335925 752
MACRUM 0:119624335925 753 temp = device->resource_value_string(M2MDevice::UTCOffset, 0);
MACRUM 0:119624335925 754 delete_config_parameter(g_fcc_offset_from_utc_parameter_name);
MACRUM 0:119624335925 755 set_config_parameter(g_fcc_offset_from_utc_parameter_name, (const uint8_t*)temp.c_str(), temp.size());
MACRUM 0:119624335925 756
MACRUM 0:119624335925 757 status = CCS_STATUS_SUCCESS;
MACRUM 0:119624335925 758 }
MACRUM 0:119624335925 759 else {
MACRUM 0:119624335925 760 tr_debug("No device object to store!");
MACRUM 0:119624335925 761 }
MACRUM 0:119624335925 762 }
MACRUM 0:119624335925 763
MACRUM 0:119624335925 764 return status;
MACRUM 0:119624335925 765 }
MACRUM 0:119624335925 766
MACRUM 0:119624335925 767 ccs_status_e ConnectorClient::set_bootstrap_credentials(M2MSecurity *security)
MACRUM 0:119624335925 768 {
MACRUM 0:119624335925 769 tr_debug("ConnectorClient::set_bootstrap_credentials");
MACRUM 0:119624335925 770 ccs_status_e status = CCS_STATUS_ERROR;
MACRUM 0:119624335925 771
MACRUM 0:119624335925 772 const uint8_t *srv_public_key = NULL;
MACRUM 0:119624335925 773 const uint8_t *public_key = NULL;
MACRUM 0:119624335925 774 const uint8_t *sec_key = NULL;
MACRUM 0:119624335925 775
MACRUM 0:119624335925 776 int32_t bs_id = security->get_security_instance_id(M2MSecurity::Bootstrap);
MACRUM 0:119624335925 777 if (bs_id == -1) {
MACRUM 0:119624335925 778 return status;
MACRUM 0:119624335925 779 }
MACRUM 0:119624335925 780
MACRUM 0:119624335925 781 uint32_t srv_public_key_size = security->resource_value_buffer(M2MSecurity::ServerPublicKey, srv_public_key, bs_id);
MACRUM 0:119624335925 782 uint32_t public_key_size = security->resource_value_buffer(M2MSecurity::PublicKey, public_key, bs_id);
MACRUM 0:119624335925 783 uint32_t sec_key_size = security->resource_value_buffer(M2MSecurity::Secretkey, sec_key, bs_id);
MACRUM 0:119624335925 784
MACRUM 0:119624335925 785 if(srv_public_key && public_key && sec_key) {
MACRUM 0:119624335925 786 delete_config_certificate(g_fcc_bootstrap_server_ca_certificate_name);
MACRUM 0:119624335925 787 status = set_config_certificate(g_fcc_bootstrap_server_ca_certificate_name,
MACRUM 0:119624335925 788 srv_public_key,
MACRUM 0:119624335925 789 (size_t)srv_public_key_size);
MACRUM 0:119624335925 790 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 791 delete_config_certificate(g_fcc_bootstrap_device_certificate_name);
MACRUM 0:119624335925 792 status = set_config_certificate(g_fcc_bootstrap_device_certificate_name,
MACRUM 0:119624335925 793 public_key,
MACRUM 0:119624335925 794 (size_t)public_key_size);
MACRUM 0:119624335925 795 }
MACRUM 0:119624335925 796 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 797 delete_config_private_key(g_fcc_bootstrap_device_private_key_name);
MACRUM 0:119624335925 798 status = set_config_private_key(g_fcc_bootstrap_device_private_key_name,
MACRUM 0:119624335925 799 sec_key,
MACRUM 0:119624335925 800 (size_t)sec_key_size);
MACRUM 0:119624335925 801 }
MACRUM 0:119624335925 802 if(status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 803 delete_config_parameter(g_fcc_bootstrap_server_uri_name);
MACRUM 0:119624335925 804 status = set_config_parameter(g_fcc_bootstrap_server_uri_name,
MACRUM 0:119624335925 805 (const uint8_t*)security->resource_value_string(M2MSecurity::M2MServerUri, bs_id).c_str(),
MACRUM 0:119624335925 806 (size_t)security->resource_value_string(M2MSecurity::M2MServerUri, bs_id).size());
MACRUM 0:119624335925 807 }
MACRUM 0:119624335925 808 }
MACRUM 0:119624335925 809
MACRUM 0:119624335925 810 return status;
MACRUM 0:119624335925 811 }
MACRUM 0:119624335925 812
MACRUM 0:119624335925 813 ccs_status_e ConnectorClient::store_bootstrap_address(M2MSecurity *security)
MACRUM 0:119624335925 814 {
MACRUM 0:119624335925 815 tr_debug("ConnectorClient::store_bootstrap_address");
MACRUM 0:119624335925 816 ccs_status_e status = CCS_STATUS_ERROR;
MACRUM 0:119624335925 817
MACRUM 0:119624335925 818 const uint8_t *srv_address = NULL;
MACRUM 0:119624335925 819 int32_t bs_id = security->get_security_instance_id(M2MSecurity::Bootstrap);
MACRUM 0:119624335925 820 if (bs_id == -1) {
MACRUM 0:119624335925 821 return status;
MACRUM 0:119624335925 822 }
MACRUM 0:119624335925 823
MACRUM 0:119624335925 824 uint32_t srv_address_size = security->resource_value_buffer(M2MSecurity::M2MServerUri, srv_address, bs_id);
MACRUM 0:119624335925 825
MACRUM 0:119624335925 826 if(srv_address) {
MACRUM 0:119624335925 827 delete_config_parameter(g_fcc_bootstrap_server_uri_name);
MACRUM 0:119624335925 828 status = set_config_parameter(g_fcc_bootstrap_server_uri_name,
MACRUM 0:119624335925 829 srv_address,
MACRUM 0:119624335925 830 (size_t)srv_address_size);
MACRUM 0:119624335925 831 }
MACRUM 0:119624335925 832
MACRUM 0:119624335925 833 return status;
MACRUM 0:119624335925 834 }
MACRUM 0:119624335925 835
MACRUM 0:119624335925 836 ccs_status_e ConnectorClient::clear_first_to_claim()
MACRUM 0:119624335925 837 {
MACRUM 0:119624335925 838 tr_debug("ConnectorClient::clear_first_to_claim");
MACRUM 0:119624335925 839 return delete_config_parameter(KEY_FIRST_TO_CLAIM);
MACRUM 0:119624335925 840 }
MACRUM 0:119624335925 841
MACRUM 0:119624335925 842
MACRUM 0:119624335925 843 const ConnectorClientEndpointInfo *ConnectorClient::endpoint_info() const
MACRUM 0:119624335925 844 {
MACRUM 0:119624335925 845 return &_endpoint_info;
MACRUM 0:119624335925 846 }
MACRUM 0:119624335925 847
MACRUM 0:119624335925 848 bool ConnectorClient::bootstrap_credentials_stored_in_kcm()
MACRUM 0:119624335925 849 {
MACRUM 0:119624335925 850 size_t real_size = 0;
MACRUM 0:119624335925 851 ccs_status_e success = size_config_parameter(g_fcc_bootstrap_server_uri_name, &real_size);
MACRUM 0:119624335925 852 // Return true if bootstrap uri exists in KCM
MACRUM 0:119624335925 853 if ((success == CCS_STATUS_SUCCESS) && real_size > 0) {
MACRUM 0:119624335925 854 return true;
MACRUM 0:119624335925 855 } else {
MACRUM 0:119624335925 856 return false;
MACRUM 0:119624335925 857 }
MACRUM 0:119624335925 858 }
MACRUM 0:119624335925 859
MACRUM 0:119624335925 860 bool ConnectorClient::is_first_to_claim()
MACRUM 0:119624335925 861 {
MACRUM 0:119624335925 862 size_t real_size = 0;
MACRUM 0:119624335925 863 uint8_t data[4] = {0};
MACRUM 0:119624335925 864 uint32_t value = 0;
MACRUM 0:119624335925 865 ccs_status_e status = get_config_parameter(KEY_FIRST_TO_CLAIM, data, 4, &real_size);
MACRUM 0:119624335925 866 if (status == CCS_STATUS_SUCCESS) {
MACRUM 0:119624335925 867 memcpy(&value, data, 4);
MACRUM 0:119624335925 868 // Return true if bootstrap uri exists in KCM
MACRUM 0:119624335925 869 if (value == 1) {
MACRUM 0:119624335925 870 return true;
MACRUM 0:119624335925 871 }
MACRUM 0:119624335925 872 }
MACRUM 0:119624335925 873 return false;
MACRUM 0:119624335925 874 }
MACRUM 0:119624335925 875
MACRUM 0:119624335925 876 void ConnectorClient::timer_expired(M2MTimerObserver::Type type)
MACRUM 0:119624335925 877 {
MACRUM 0:119624335925 878 if (type == M2MTimerObserver::BootstrapFlowTimer) {
MACRUM 0:119624335925 879 start_bootstrap();
MACRUM 0:119624335925 880 }
MACRUM 0:119624335925 881 }
MACRUM 0:119624335925 882
MACRUM 0:119624335925 883 M2MInterface::BindingMode ConnectorClient::transport_mode()
MACRUM 0:119624335925 884 {
MACRUM 0:119624335925 885 #ifdef MBED_CLOUD_CLIENT_TRANSPORT_MODE_UDP
MACRUM 0:119624335925 886 return M2MInterface::UDP;
MACRUM 0:119624335925 887 #elif defined MBED_CLOUD_CLIENT_TRANSPORT_MODE_TCP
MACRUM 0:119624335925 888 return M2MInterface::TCP;
MACRUM 0:119624335925 889 #elif defined MBED_CLOUD_CLIENT_TRANSPORT_MODE_UDP_QUEUE
MACRUM 0:119624335925 890 return M2MInterface::UDP_QUEUE;
MACRUM 0:119624335925 891 #elif defined MBED_CLOUD_CLIENT_TRANSPORT_MODE_TCP_QUEUE
MACRUM 0:119624335925 892 return M2MInterface::TCP_QUEUE;
MACRUM 0:119624335925 893 #else
MACRUM 0:119624335925 894 return M2MInterface::UDP;
MACRUM 0:119624335925 895 #endif
MACRUM 0:119624335925 896 }