Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-cloud-client/source/ServiceClient.cpp@2:bf2124b482f9, 2018-07-02 (annotated)
- Committer:
- MACRUM
- Date:
- Mon Jul 02 08:06:37 2018 +0000
- Revision:
- 2:bf2124b482f9
- Parent:
- 0:276e7a263c35
Update library
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MACRUM | 0:276e7a263c35 | 1 | // ---------------------------------------------------------------------------- |
| MACRUM | 0:276e7a263c35 | 2 | // Copyright 2016-2017 ARM Ltd. |
| MACRUM | 0:276e7a263c35 | 3 | // |
| MACRUM | 0:276e7a263c35 | 4 | // SPDX-License-Identifier: Apache-2.0 |
| MACRUM | 0:276e7a263c35 | 5 | // |
| MACRUM | 0:276e7a263c35 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| MACRUM | 0:276e7a263c35 | 7 | // you may not use this file except in compliance with the License. |
| MACRUM | 0:276e7a263c35 | 8 | // You may obtain a copy of the License at |
| MACRUM | 0:276e7a263c35 | 9 | // |
| MACRUM | 0:276e7a263c35 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
| MACRUM | 0:276e7a263c35 | 11 | // |
| MACRUM | 0:276e7a263c35 | 12 | // Unless required by applicable law or agreed to in writing, software |
| MACRUM | 0:276e7a263c35 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
| MACRUM | 0:276e7a263c35 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| MACRUM | 0:276e7a263c35 | 15 | // See the License for the specific language governing permissions and |
| MACRUM | 0:276e7a263c35 | 16 | // limitations under the License. |
| MACRUM | 0:276e7a263c35 | 17 | // ---------------------------------------------------------------------------- |
| MACRUM | 0:276e7a263c35 | 18 | |
| MACRUM | 0:276e7a263c35 | 19 | // Note: this macro is needed on armcc to get the the PRI*32 macros |
| MACRUM | 0:276e7a263c35 | 20 | // from inttypes.h in a C++ code. |
| MACRUM | 0:276e7a263c35 | 21 | #ifndef __STDC_FORMAT_MACROS |
| MACRUM | 0:276e7a263c35 | 22 | #define __STDC_FORMAT_MACROS |
| MACRUM | 0:276e7a263c35 | 23 | #endif |
| MACRUM | 0:276e7a263c35 | 24 | #include <inttypes.h> |
| MACRUM | 0:276e7a263c35 | 25 | |
| MACRUM | 0:276e7a263c35 | 26 | #include <string> |
| MACRUM | 0:276e7a263c35 | 27 | #include "include/ServiceClient.h" |
| MACRUM | 0:276e7a263c35 | 28 | #include "include/CloudClientStorage.h" |
| MACRUM | 0:276e7a263c35 | 29 | #include "include/UpdateClientResources.h" |
| MACRUM | 0:276e7a263c35 | 30 | #include "include/UpdateClient.h" |
| MACRUM | 0:276e7a263c35 | 31 | #include "factory_configurator_client.h" |
| MACRUM | 0:276e7a263c35 | 32 | #include "mbed-trace/mbed_trace.h" |
| MACRUM | 0:276e7a263c35 | 33 | #include <assert.h> |
| MACRUM | 0:276e7a263c35 | 34 | |
| MACRUM | 0:276e7a263c35 | 35 | #define TRACE_GROUP "mClt" |
| MACRUM | 0:276e7a263c35 | 36 | |
| MACRUM | 0:276e7a263c35 | 37 | #define CONNECT 0 |
| MACRUM | 0:276e7a263c35 | 38 | #define ERROR_UPDATE "Update has failed, check MbedCloudClient::Error" |
| MACRUM | 0:276e7a263c35 | 39 | |
| MACRUM | 0:276e7a263c35 | 40 | /* lookup table for printing hexadecimal values */ |
| MACRUM | 0:276e7a263c35 | 41 | const uint8_t ServiceClient::hex_table[16] = { |
| MACRUM | 0:276e7a263c35 | 42 | '0', '1', '2', '3', '4', '5', '6', '7', |
| MACRUM | 0:276e7a263c35 | 43 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
| MACRUM | 0:276e7a263c35 | 44 | }; |
| MACRUM | 0:276e7a263c35 | 45 | |
| MACRUM | 0:276e7a263c35 | 46 | ServiceClient::ServiceClient(ServiceClientCallback& callback) |
| MACRUM | 0:276e7a263c35 | 47 | : _service_callback(callback), |
| MACRUM | 0:276e7a263c35 | 48 | _service_uri(NULL), |
| MACRUM | 0:276e7a263c35 | 49 | _stack(NULL), |
| MACRUM | 0:276e7a263c35 | 50 | _client_objs(NULL), |
| MACRUM | 0:276e7a263c35 | 51 | _current_state(State_Init), |
| MACRUM | 0:276e7a263c35 | 52 | _event_generated(false), |
| MACRUM | 0:276e7a263c35 | 53 | _state_engine_running(false), |
| MACRUM | 0:276e7a263c35 | 54 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
| MACRUM | 0:276e7a263c35 | 55 | _setup_update_client(false), |
| MACRUM | 0:276e7a263c35 | 56 | #endif |
| MACRUM | 0:276e7a263c35 | 57 | _connector_client(this) |
| MACRUM | 0:276e7a263c35 | 58 | { |
| MACRUM | 0:276e7a263c35 | 59 | } |
| MACRUM | 0:276e7a263c35 | 60 | |
| MACRUM | 0:276e7a263c35 | 61 | ServiceClient::~ServiceClient() |
| MACRUM | 0:276e7a263c35 | 62 | { |
| MACRUM | 0:276e7a263c35 | 63 | } |
| MACRUM | 0:276e7a263c35 | 64 | |
| MACRUM | 0:276e7a263c35 | 65 | void ServiceClient::initialize_and_register(M2MObjectList& client_objs) |
| MACRUM | 0:276e7a263c35 | 66 | { |
| MACRUM | 0:276e7a263c35 | 67 | tr_debug("ServiceClient::initialize_and_register"); |
| MACRUM | 0:276e7a263c35 | 68 | if(_current_state == State_Init || |
| MACRUM | 0:276e7a263c35 | 69 | _current_state == State_Unregister || |
| MACRUM | 0:276e7a263c35 | 70 | _current_state == State_Failure) { |
| MACRUM | 0:276e7a263c35 | 71 | _client_objs = &client_objs; |
| MACRUM | 0:276e7a263c35 | 72 | |
| MACRUM | 0:276e7a263c35 | 73 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
| MACRUM | 0:276e7a263c35 | 74 | tr_debug("ServiceClient::initialize_and_register: update client supported"); |
| MACRUM | 0:276e7a263c35 | 75 | |
| MACRUM | 0:276e7a263c35 | 76 | if(!_setup_update_client) { |
| MACRUM | 0:276e7a263c35 | 77 | _setup_update_client = true; |
| MACRUM | 0:276e7a263c35 | 78 | |
| MACRUM | 0:276e7a263c35 | 79 | #ifdef MBED_CLOUD_DEV_UPDATE_ID |
| MACRUM | 0:276e7a263c35 | 80 | /* Overwrite values stored in KCM. This is for development only |
| MACRUM | 0:276e7a263c35 | 81 | since these IDs should be provisioned in the factory. |
| MACRUM | 0:276e7a263c35 | 82 | */ |
| MACRUM | 0:276e7a263c35 | 83 | tr_debug("ServiceClient::initialize_and_register: update IDs defined"); |
| MACRUM | 0:276e7a263c35 | 84 | |
| MACRUM | 0:276e7a263c35 | 85 | /* Delete VendorId */ |
| MACRUM | 0:276e7a263c35 | 86 | delete_config_parameter("mbed.VendorId"); |
| MACRUM | 0:276e7a263c35 | 87 | /* Store Vendor Id to mbed.VendorId. No conversion is performed. */ |
| MACRUM | 0:276e7a263c35 | 88 | set_device_resource_value(M2MDevice::Manufacturer, |
| MACRUM | 0:276e7a263c35 | 89 | (const char*) arm_uc_vendor_id, |
| MACRUM | 0:276e7a263c35 | 90 | arm_uc_vendor_id_size); |
| MACRUM | 0:276e7a263c35 | 91 | |
| MACRUM | 0:276e7a263c35 | 92 | /* Delete ClassId */ |
| MACRUM | 0:276e7a263c35 | 93 | delete_config_parameter("mbed.ClassId"); |
| MACRUM | 0:276e7a263c35 | 94 | /* Store Class Id to mbed.ClassId. No conversion is performed. */ |
| MACRUM | 0:276e7a263c35 | 95 | set_device_resource_value(M2MDevice::ModelNumber, |
| MACRUM | 0:276e7a263c35 | 96 | (const char*) arm_uc_class_id, |
| MACRUM | 0:276e7a263c35 | 97 | arm_uc_class_id_size); |
| MACRUM | 0:276e7a263c35 | 98 | #endif /* MBED_CLOUD_DEV_UPDATE_ID */ |
| MACRUM | 0:276e7a263c35 | 99 | |
| MACRUM | 0:276e7a263c35 | 100 | #ifdef ARM_UPDATE_CLIENT_VERSION |
| MACRUM | 0:276e7a263c35 | 101 | /* Inject Update Client version number if no other software |
| MACRUM | 0:276e7a263c35 | 102 | version is present in the KCM. |
| MACRUM | 0:276e7a263c35 | 103 | */ |
| MACRUM | 0:276e7a263c35 | 104 | tr_debug("ServiceClient::initialize_and_register: update version defined"); |
| MACRUM | 0:276e7a263c35 | 105 | ; |
| MACRUM | 0:276e7a263c35 | 106 | const size_t buffer_size = 16; |
| MACRUM | 0:276e7a263c35 | 107 | uint8_t buffer[buffer_size]; |
| MACRUM | 0:276e7a263c35 | 108 | size_t size = 0; |
| MACRUM | 0:276e7a263c35 | 109 | |
| MACRUM | 0:276e7a263c35 | 110 | /* check if software version is already set */ |
| MACRUM | 0:276e7a263c35 | 111 | ccs_status_e status = get_config_parameter(KEY_DEVICE_SOFTWAREVERSION, |
| MACRUM | 0:276e7a263c35 | 112 | buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 113 | |
| MACRUM | 0:276e7a263c35 | 114 | if (status == CCS_STATUS_KEY_DOESNT_EXIST) { |
| MACRUM | 0:276e7a263c35 | 115 | tr_debug("ServiceClient::initialize_and_register: insert update version"); |
| MACRUM | 0:276e7a263c35 | 116 | |
| MACRUM | 0:276e7a263c35 | 117 | /* insert value from Update Client Common */ |
| MACRUM | 0:276e7a263c35 | 118 | set_config_parameter(KEY_DEVICE_SOFTWAREVERSION, |
| MACRUM | 0:276e7a263c35 | 119 | (const uint8_t*) ARM_UPDATE_CLIENT_VERSION, |
| MACRUM | 0:276e7a263c35 | 120 | sizeof(ARM_UPDATE_CLIENT_VERSION)); |
| MACRUM | 0:276e7a263c35 | 121 | } |
| MACRUM | 0:276e7a263c35 | 122 | #endif /* ARM_UPDATE_CLIENT_VERSION */ |
| MACRUM | 0:276e7a263c35 | 123 | |
| MACRUM | 0:276e7a263c35 | 124 | /* Update Client adds the OMA LWM2M Firmware Update object */ |
| MACRUM | 0:276e7a263c35 | 125 | UpdateClient::populate_object_list(*_client_objs); |
| MACRUM | 0:276e7a263c35 | 126 | |
| MACRUM | 0:276e7a263c35 | 127 | /* Initialize Update Client */ |
| MACRUM | 0:276e7a263c35 | 128 | FP1<void, int32_t> callback(this, &ServiceClient::update_error_callback); |
| MACRUM | 0:276e7a263c35 | 129 | UpdateClient::UpdateClient(callback); |
| MACRUM | 0:276e7a263c35 | 130 | } |
| MACRUM | 0:276e7a263c35 | 131 | #endif /* MBED_CLOUD_CLIENT_SUPPORT_UPDATE */ |
| MACRUM | 0:276e7a263c35 | 132 | |
| MACRUM | 0:276e7a263c35 | 133 | /* Device Object is mandatory. |
| MACRUM | 0:276e7a263c35 | 134 | Get instance and add it to object list |
| MACRUM | 0:276e7a263c35 | 135 | */ |
| MACRUM | 0:276e7a263c35 | 136 | M2MDevice *device_object = device_object_from_storage(); |
| MACRUM | 0:276e7a263c35 | 137 | |
| MACRUM | 0:276e7a263c35 | 138 | if (device_object) { |
| MACRUM | 0:276e7a263c35 | 139 | /* Publish device object resource to mds */ |
| MACRUM | 0:276e7a263c35 | 140 | M2MResourceList list = device_object->object_instance()->resources(); |
| MACRUM | 0:276e7a263c35 | 141 | if(!list.empty()) { |
| MACRUM | 0:276e7a263c35 | 142 | M2MResourceList::const_iterator it; |
| MACRUM | 0:276e7a263c35 | 143 | it = list.begin(); |
| MACRUM | 0:276e7a263c35 | 144 | for ( ; it != list.end(); it++ ) { |
| MACRUM | 0:276e7a263c35 | 145 | (*it)->set_register_uri(true); |
| MACRUM | 0:276e7a263c35 | 146 | } |
| MACRUM | 0:276e7a263c35 | 147 | } |
| MACRUM | 0:276e7a263c35 | 148 | |
| MACRUM | 0:276e7a263c35 | 149 | /* Add Device Object to object list. */ |
| MACRUM | 0:276e7a263c35 | 150 | _client_objs->push_back(device_object); |
| MACRUM | 0:276e7a263c35 | 151 | } |
| MACRUM | 0:276e7a263c35 | 152 | |
| MACRUM | 0:276e7a263c35 | 153 | internal_event(State_Bootstrap); |
| MACRUM | 0:276e7a263c35 | 154 | } else if (_current_state == State_Success) { |
| MACRUM | 0:276e7a263c35 | 155 | state_success(); |
| MACRUM | 0:276e7a263c35 | 156 | } |
| MACRUM | 0:276e7a263c35 | 157 | } |
| MACRUM | 0:276e7a263c35 | 158 | |
| MACRUM | 0:276e7a263c35 | 159 | ConnectorClient &ServiceClient::connector_client() |
| MACRUM | 0:276e7a263c35 | 160 | { |
| MACRUM | 0:276e7a263c35 | 161 | return _connector_client; |
| MACRUM | 0:276e7a263c35 | 162 | } |
| MACRUM | 0:276e7a263c35 | 163 | |
| MACRUM | 0:276e7a263c35 | 164 | const ConnectorClient &ServiceClient::connector_client() const |
| MACRUM | 0:276e7a263c35 | 165 | { |
| MACRUM | 0:276e7a263c35 | 166 | return _connector_client; |
| MACRUM | 0:276e7a263c35 | 167 | } |
| MACRUM | 0:276e7a263c35 | 168 | |
| MACRUM | 0:276e7a263c35 | 169 | // generates an internal event. called from within a state |
| MACRUM | 0:276e7a263c35 | 170 | // function to transition to a new state |
| MACRUM | 0:276e7a263c35 | 171 | void ServiceClient::internal_event(StartupMainState new_state) |
| MACRUM | 0:276e7a263c35 | 172 | { |
| MACRUM | 0:276e7a263c35 | 173 | tr_debug("ServiceClient::internal_event: state: %d -> %d", _current_state, new_state); |
| MACRUM | 0:276e7a263c35 | 174 | |
| MACRUM | 0:276e7a263c35 | 175 | _event_generated = true; |
| MACRUM | 0:276e7a263c35 | 176 | _current_state = new_state; |
| MACRUM | 0:276e7a263c35 | 177 | |
| MACRUM | 0:276e7a263c35 | 178 | if (!_state_engine_running) { |
| MACRUM | 0:276e7a263c35 | 179 | state_engine(); |
| MACRUM | 0:276e7a263c35 | 180 | } |
| MACRUM | 0:276e7a263c35 | 181 | } |
| MACRUM | 0:276e7a263c35 | 182 | |
| MACRUM | 0:276e7a263c35 | 183 | // the state engine executes the state machine states |
| MACRUM | 0:276e7a263c35 | 184 | void ServiceClient::state_engine(void) |
| MACRUM | 0:276e7a263c35 | 185 | { |
| MACRUM | 0:276e7a263c35 | 186 | tr_debug("ServiceClient::state_engine"); |
| MACRUM | 0:276e7a263c35 | 187 | |
| MACRUM | 0:276e7a263c35 | 188 | // this simple flagging gets rid of recursive calls to this method |
| MACRUM | 0:276e7a263c35 | 189 | _state_engine_running = true; |
| MACRUM | 0:276e7a263c35 | 190 | |
| MACRUM | 0:276e7a263c35 | 191 | // while events are being generated keep executing states |
| MACRUM | 0:276e7a263c35 | 192 | while (_event_generated) { |
| MACRUM | 0:276e7a263c35 | 193 | _event_generated = false; // event used up, reset flag |
| MACRUM | 0:276e7a263c35 | 194 | |
| MACRUM | 0:276e7a263c35 | 195 | state_function(_current_state); |
| MACRUM | 0:276e7a263c35 | 196 | } |
| MACRUM | 0:276e7a263c35 | 197 | |
| MACRUM | 0:276e7a263c35 | 198 | _state_engine_running = false; |
| MACRUM | 0:276e7a263c35 | 199 | } |
| MACRUM | 0:276e7a263c35 | 200 | |
| MACRUM | 0:276e7a263c35 | 201 | void ServiceClient::state_function(StartupMainState current_state) |
| MACRUM | 0:276e7a263c35 | 202 | { |
| MACRUM | 0:276e7a263c35 | 203 | switch (current_state) { |
| MACRUM | 0:276e7a263c35 | 204 | case State_Init: // -> Goes to bootstrap state |
| MACRUM | 0:276e7a263c35 | 205 | case State_Bootstrap: // -> State_Register OR State_Failure |
| MACRUM | 0:276e7a263c35 | 206 | state_bootstrap(); |
| MACRUM | 0:276e7a263c35 | 207 | break; |
| MACRUM | 0:276e7a263c35 | 208 | case State_Register: // -> State_Succes OR State_Failure |
| MACRUM | 0:276e7a263c35 | 209 | state_register(); |
| MACRUM | 0:276e7a263c35 | 210 | break; |
| MACRUM | 0:276e7a263c35 | 211 | case State_Success: // return success to user |
| MACRUM | 0:276e7a263c35 | 212 | state_success(); |
| MACRUM | 0:276e7a263c35 | 213 | break; |
| MACRUM | 0:276e7a263c35 | 214 | case State_Failure: // return error to user |
| MACRUM | 0:276e7a263c35 | 215 | state_failure(); |
| MACRUM | 0:276e7a263c35 | 216 | break; |
| MACRUM | 0:276e7a263c35 | 217 | case State_Unregister: // return error to user |
| MACRUM | 0:276e7a263c35 | 218 | state_unregister(); |
| MACRUM | 0:276e7a263c35 | 219 | break; |
| MACRUM | 0:276e7a263c35 | 220 | } |
| MACRUM | 0:276e7a263c35 | 221 | } |
| MACRUM | 0:276e7a263c35 | 222 | |
| MACRUM | 0:276e7a263c35 | 223 | void ServiceClient::state_bootstrap() |
| MACRUM | 0:276e7a263c35 | 224 | { |
| MACRUM | 0:276e7a263c35 | 225 | tr_info("ServiceClient::state_bootstrap()"); |
| MACRUM | 0:276e7a263c35 | 226 | bool credentials_ready = _connector_client.connector_credentials_available(); |
| MACRUM | 0:276e7a263c35 | 227 | bool bootstrap = _connector_client.use_bootstrap(); |
| MACRUM | 0:276e7a263c35 | 228 | tr_info("ServiceClient::state_bootstrap() - lwm2m credentials available: %d", credentials_ready); |
| MACRUM | 0:276e7a263c35 | 229 | tr_info("ServiceClient::state_bootstrap() - use bootstrap: %d", bootstrap); |
| MACRUM | 0:276e7a263c35 | 230 | if (credentials_ready || !bootstrap) { |
| MACRUM | 0:276e7a263c35 | 231 | internal_event(State_Register); |
| MACRUM | 0:276e7a263c35 | 232 | } else { |
| MACRUM | 0:276e7a263c35 | 233 | _connector_client.start_bootstrap(); |
| MACRUM | 0:276e7a263c35 | 234 | } |
| MACRUM | 0:276e7a263c35 | 235 | } |
| MACRUM | 0:276e7a263c35 | 236 | |
| MACRUM | 0:276e7a263c35 | 237 | void ServiceClient::state_register() |
| MACRUM | 0:276e7a263c35 | 238 | { |
| MACRUM | 0:276e7a263c35 | 239 | tr_info("ServiceClient::state_register()"); |
| MACRUM | 0:276e7a263c35 | 240 | _connector_client.start_registration(_client_objs); |
| MACRUM | 0:276e7a263c35 | 241 | } |
| MACRUM | 0:276e7a263c35 | 242 | |
| MACRUM | 0:276e7a263c35 | 243 | void ServiceClient::registration_process_result(ConnectorClient::StartupSubStateRegistration status) |
| MACRUM | 0:276e7a263c35 | 244 | { |
| MACRUM | 0:276e7a263c35 | 245 | tr_debug("ServiceClient::registration_process_result(): status: %d", status); |
| MACRUM | 0:276e7a263c35 | 246 | if (status == ConnectorClient::State_Registration_Success) { |
| MACRUM | 0:276e7a263c35 | 247 | internal_event(State_Success); |
| MACRUM | 0:276e7a263c35 | 248 | } else if(status == ConnectorClient::State_Registration_Failure || |
| MACRUM | 0:276e7a263c35 | 249 | status == ConnectorClient::State_Bootstrap_Failure){ |
| MACRUM | 0:276e7a263c35 | 250 | internal_event(State_Failure); // XXX: the status should be saved to eg. event object |
| MACRUM | 0:276e7a263c35 | 251 | } |
| MACRUM | 0:276e7a263c35 | 252 | if(status == ConnectorClient::State_Bootstrap_Success) { |
| MACRUM | 0:276e7a263c35 | 253 | internal_event(State_Register); |
| MACRUM | 0:276e7a263c35 | 254 | } |
| MACRUM | 0:276e7a263c35 | 255 | if(status == ConnectorClient::State_Unregistered) { |
| MACRUM | 0:276e7a263c35 | 256 | internal_event(State_Unregister); |
| MACRUM | 0:276e7a263c35 | 257 | } |
| MACRUM | 0:276e7a263c35 | 258 | if (status == ConnectorClient::State_Registration_Updated) { |
| MACRUM | 0:276e7a263c35 | 259 | _service_callback.complete(ServiceClientCallback::Service_Client_Status_Register_Updated); |
| MACRUM | 0:276e7a263c35 | 260 | } |
| MACRUM | 0:276e7a263c35 | 261 | } |
| MACRUM | 0:276e7a263c35 | 262 | |
| MACRUM | 0:276e7a263c35 | 263 | void ServiceClient::connector_error(M2MInterface::Error error, const char *reason) |
| MACRUM | 0:276e7a263c35 | 264 | { |
| MACRUM | 0:276e7a263c35 | 265 | tr_error("ServiceClient::connector_error() error %d", (int)error); |
| MACRUM | 0:276e7a263c35 | 266 | if (_current_state == State_Register) { |
| MACRUM | 0:276e7a263c35 | 267 | registration_process_result(ConnectorClient::State_Registration_Failure); |
| MACRUM | 0:276e7a263c35 | 268 | } |
| MACRUM | 0:276e7a263c35 | 269 | else if (_current_state == State_Bootstrap) { |
| MACRUM | 0:276e7a263c35 | 270 | registration_process_result(ConnectorClient::State_Bootstrap_Failure); |
| MACRUM | 0:276e7a263c35 | 271 | } |
| MACRUM | 0:276e7a263c35 | 272 | _service_callback.error(int(error),reason); |
| MACRUM | 0:276e7a263c35 | 273 | internal_event(State_Failure); |
| MACRUM | 0:276e7a263c35 | 274 | } |
| MACRUM | 0:276e7a263c35 | 275 | |
| MACRUM | 0:276e7a263c35 | 276 | void ServiceClient::value_updated(M2MBase *base, M2MBase::BaseType type) |
| MACRUM | 0:276e7a263c35 | 277 | { |
| MACRUM | 0:276e7a263c35 | 278 | tr_debug("ServiceClient::value_updated()"); |
| MACRUM | 0:276e7a263c35 | 279 | _service_callback.value_updated(base, type); |
| MACRUM | 0:276e7a263c35 | 280 | } |
| MACRUM | 0:276e7a263c35 | 281 | |
| MACRUM | 0:276e7a263c35 | 282 | void ServiceClient::state_success() |
| MACRUM | 0:276e7a263c35 | 283 | { |
| MACRUM | 0:276e7a263c35 | 284 | tr_info("ServiceClient::state_success()"); |
| MACRUM | 0:276e7a263c35 | 285 | // this is verified already at client API level, but this might still catch some logic failures |
| MACRUM | 0:276e7a263c35 | 286 | _service_callback.complete(ServiceClientCallback::Service_Client_Status_Registered); |
| MACRUM | 0:276e7a263c35 | 287 | } |
| MACRUM | 0:276e7a263c35 | 288 | |
| MACRUM | 0:276e7a263c35 | 289 | void ServiceClient::state_failure() |
| MACRUM | 0:276e7a263c35 | 290 | { |
| MACRUM | 0:276e7a263c35 | 291 | tr_error("ServiceClient::state_failure()"); |
| MACRUM | 0:276e7a263c35 | 292 | _service_callback.complete(ServiceClientCallback::Service_Client_Status_Failure); |
| MACRUM | 0:276e7a263c35 | 293 | } |
| MACRUM | 0:276e7a263c35 | 294 | |
| MACRUM | 0:276e7a263c35 | 295 | void ServiceClient::state_unregister() |
| MACRUM | 0:276e7a263c35 | 296 | { |
| MACRUM | 0:276e7a263c35 | 297 | tr_debug("ServiceClient::state_unregister()"); |
| MACRUM | 0:276e7a263c35 | 298 | _service_callback.complete(ServiceClientCallback::Service_Client_Status_Unregistered); |
| MACRUM | 0:276e7a263c35 | 299 | } |
| MACRUM | 0:276e7a263c35 | 300 | |
| MACRUM | 0:276e7a263c35 | 301 | M2MDevice* ServiceClient::device_object_from_storage() |
| MACRUM | 0:276e7a263c35 | 302 | { |
| MACRUM | 0:276e7a263c35 | 303 | M2MDevice *device_object = M2MInterfaceFactory::create_device(); |
| MACRUM | 0:276e7a263c35 | 304 | if (device_object == NULL) { |
| MACRUM | 0:276e7a263c35 | 305 | return NULL; |
| MACRUM | 0:276e7a263c35 | 306 | } |
| MACRUM | 0:276e7a263c35 | 307 | |
| MACRUM | 0:276e7a263c35 | 308 | const size_t buffer_size = 128; |
| MACRUM | 0:276e7a263c35 | 309 | uint8_t buffer[buffer_size]; |
| MACRUM | 0:276e7a263c35 | 310 | size_t size = 0; |
| MACRUM | 0:276e7a263c35 | 311 | |
| MACRUM | 0:276e7a263c35 | 312 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
| MACRUM | 0:276e7a263c35 | 313 | uint8_t guid[sizeof(arm_uc_guid_t)] = {0}; |
| MACRUM | 0:276e7a263c35 | 314 | // Read out the binary Vendor UUID |
| MACRUM | 0:276e7a263c35 | 315 | ccs_status_e status = (ccs_status_e)UpdateClient::getVendorId(guid, sizeof(arm_uc_guid_t), &size); |
| MACRUM | 0:276e7a263c35 | 316 | |
| MACRUM | 0:276e7a263c35 | 317 | // Format the binary Vendor UUID into a hex string |
| MACRUM | 0:276e7a263c35 | 318 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 319 | size_t j = 0; |
| MACRUM | 0:276e7a263c35 | 320 | for(size_t i = 0; i < size; i++) |
| MACRUM | 0:276e7a263c35 | 321 | { |
| MACRUM | 0:276e7a263c35 | 322 | buffer[j++] = hex_table[(guid[i] >> 4) & 0xF]; |
| MACRUM | 0:276e7a263c35 | 323 | buffer[j++] = hex_table[(guid[i] >> 0) & 0xF]; |
| MACRUM | 0:276e7a263c35 | 324 | } |
| MACRUM | 0:276e7a263c35 | 325 | buffer[j] = '\0'; |
| MACRUM | 0:276e7a263c35 | 326 | device_object->create_resource(M2MDevice::Manufacturer, String((char*)buffer, size * 2)); |
| MACRUM | 0:276e7a263c35 | 327 | } |
| MACRUM | 0:276e7a263c35 | 328 | |
| MACRUM | 0:276e7a263c35 | 329 | // Read out the binary Class UUID |
| MACRUM | 0:276e7a263c35 | 330 | status = (ccs_status_e)UpdateClient::getClassId(guid, sizeof(arm_uc_guid_t), &size); |
| MACRUM | 0:276e7a263c35 | 331 | |
| MACRUM | 0:276e7a263c35 | 332 | // Format the binary Class UUID into a hex string |
| MACRUM | 0:276e7a263c35 | 333 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 334 | size_t j = 0; |
| MACRUM | 0:276e7a263c35 | 335 | for(size_t i = 0; i < size; i++) |
| MACRUM | 0:276e7a263c35 | 336 | { |
| MACRUM | 0:276e7a263c35 | 337 | buffer[j++] = hex_table[(guid[i] >> 4) & 0xF]; |
| MACRUM | 0:276e7a263c35 | 338 | buffer[j++] = hex_table[(guid[i] >> 0) & 0xF]; |
| MACRUM | 0:276e7a263c35 | 339 | } |
| MACRUM | 0:276e7a263c35 | 340 | buffer[j] = '\0'; |
| MACRUM | 0:276e7a263c35 | 341 | device_object->create_resource(M2MDevice::ModelNumber, String((char*)buffer, size * 2)); |
| MACRUM | 0:276e7a263c35 | 342 | } |
| MACRUM | 0:276e7a263c35 | 343 | #else |
| MACRUM | 0:276e7a263c35 | 344 | ccs_status_e status = get_config_parameter(g_fcc_manufacturer_parameter_name, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 345 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 346 | device_object->create_resource(M2MDevice::Manufacturer, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 347 | } |
| MACRUM | 0:276e7a263c35 | 348 | status = get_config_parameter(g_fcc_model_number_parameter_name, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 349 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 350 | device_object->create_resource(M2MDevice::ModelNumber, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 351 | } |
| MACRUM | 0:276e7a263c35 | 352 | #endif |
| MACRUM | 0:276e7a263c35 | 353 | status = get_config_parameter(g_fcc_device_serial_number_parameter_name, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 354 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 355 | device_object->create_resource(M2MDevice::SerialNumber, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 356 | } |
| MACRUM | 0:276e7a263c35 | 357 | status = get_config_parameter(g_fcc_device_type_parameter_name, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 358 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 359 | device_object->create_resource(M2MDevice::DeviceType, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 360 | } |
| MACRUM | 0:276e7a263c35 | 361 | status = get_config_parameter(g_fcc_hardware_version_parameter_name, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 362 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 363 | device_object->create_resource(M2MDevice::HardwareVersion, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 364 | } |
| MACRUM | 0:276e7a263c35 | 365 | status = get_config_parameter(KEY_DEVICE_SOFTWAREVERSION, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 366 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 367 | device_object->create_resource(M2MDevice::SoftwareVersion, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 368 | } |
| MACRUM | 0:276e7a263c35 | 369 | uint8_t data[4] = {0}; |
| MACRUM | 0:276e7a263c35 | 370 | uint32_t value; |
| MACRUM | 0:276e7a263c35 | 371 | status = get_config_parameter(g_fcc_memory_size_parameter_name, data, 4, &size); |
| MACRUM | 0:276e7a263c35 | 372 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 373 | memcpy(&value, data, 4); |
| MACRUM | 0:276e7a263c35 | 374 | device_object->create_resource(M2MDevice::MemoryTotal, value); |
| MACRUM | 0:276e7a263c35 | 375 | tr_debug("ServiceClient::device_object_from_storage() - setting memory total value %" PRIu32 " (%s)", value, tr_array(data, 4)); |
| MACRUM | 0:276e7a263c35 | 376 | } |
| MACRUM | 0:276e7a263c35 | 377 | status = get_config_parameter(g_fcc_current_time_parameter_name, data, 4, &size); |
| MACRUM | 0:276e7a263c35 | 378 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 379 | memcpy(&value, data, 4); |
| MACRUM | 0:276e7a263c35 | 380 | device_object->create_resource(M2MDevice::CurrentTime, value); |
| MACRUM | 0:276e7a263c35 | 381 | tr_debug("ServiceClient::device_object_from_storage() - setting current time value %" PRIu32 " (%s)", value, tr_array(data, 4)); |
| MACRUM | 0:276e7a263c35 | 382 | } |
| MACRUM | 0:276e7a263c35 | 383 | status = get_config_parameter(g_fcc_device_time_zone_parameter_name, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 384 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 385 | device_object->create_resource(M2MDevice::Timezone, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 386 | } |
| MACRUM | 0:276e7a263c35 | 387 | status = get_config_parameter(g_fcc_offset_from_utc_parameter_name, buffer, buffer_size, &size); |
| MACRUM | 0:276e7a263c35 | 388 | if (status == CCS_STATUS_SUCCESS) { |
| MACRUM | 0:276e7a263c35 | 389 | device_object->create_resource(M2MDevice::UTCOffset, String((char*)buffer, size)); |
| MACRUM | 0:276e7a263c35 | 390 | } |
| MACRUM | 0:276e7a263c35 | 391 | return device_object; |
| MACRUM | 0:276e7a263c35 | 392 | } |
| MACRUM | 0:276e7a263c35 | 393 | |
| MACRUM | 0:276e7a263c35 | 394 | /** |
| MACRUM | 0:276e7a263c35 | 395 | * \brief Set resource value in the Device Object |
| MACRUM | 0:276e7a263c35 | 396 | * |
| MACRUM | 0:276e7a263c35 | 397 | * \param resource Device enum to have value set. |
| MACRUM | 0:276e7a263c35 | 398 | * \param value String object. |
| MACRUM | 0:276e7a263c35 | 399 | * \return True if successful, false otherwise. |
| MACRUM | 0:276e7a263c35 | 400 | */ |
| MACRUM | 0:276e7a263c35 | 401 | bool ServiceClient::set_device_resource_value(M2MDevice::DeviceResource resource, |
| MACRUM | 0:276e7a263c35 | 402 | const std::string& value) |
| MACRUM | 0:276e7a263c35 | 403 | { |
| MACRUM | 0:276e7a263c35 | 404 | return set_device_resource_value(resource, |
| MACRUM | 0:276e7a263c35 | 405 | value.c_str(), |
| MACRUM | 0:276e7a263c35 | 406 | value.size() - 1); |
| MACRUM | 0:276e7a263c35 | 407 | } |
| MACRUM | 0:276e7a263c35 | 408 | |
| MACRUM | 0:276e7a263c35 | 409 | /** |
| MACRUM | 0:276e7a263c35 | 410 | * \brief Set resource value in the Device Object |
| MACRUM | 0:276e7a263c35 | 411 | * |
| MACRUM | 0:276e7a263c35 | 412 | * \param resource Device enum to have value set. |
| MACRUM | 0:276e7a263c35 | 413 | * \param value Byte buffer. |
| MACRUM | 0:276e7a263c35 | 414 | * \param length Buffer length. |
| MACRUM | 0:276e7a263c35 | 415 | * \return True if successful, false otherwise. |
| MACRUM | 0:276e7a263c35 | 416 | */ |
| MACRUM | 0:276e7a263c35 | 417 | bool ServiceClient::set_device_resource_value(M2MDevice::DeviceResource resource, |
| MACRUM | 0:276e7a263c35 | 418 | const char* value, |
| MACRUM | 0:276e7a263c35 | 419 | uint32_t length) |
| MACRUM | 0:276e7a263c35 | 420 | { |
| MACRUM | 0:276e7a263c35 | 421 | bool retval = false; |
| MACRUM | 0:276e7a263c35 | 422 | |
| MACRUM | 0:276e7a263c35 | 423 | /* sanity check */ |
| MACRUM | 0:276e7a263c35 | 424 | if (value && (length < 256) && (length > 0)) |
| MACRUM | 0:276e7a263c35 | 425 | { |
| MACRUM | 0:276e7a263c35 | 426 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
| MACRUM | 0:276e7a263c35 | 427 | /* Pass resource value to Update Client. |
| MACRUM | 0:276e7a263c35 | 428 | Used for validating the manifest. |
| MACRUM | 0:276e7a263c35 | 429 | */ |
| MACRUM | 0:276e7a263c35 | 430 | switch (resource) { |
| MACRUM | 0:276e7a263c35 | 431 | case M2MDevice::Manufacturer: |
| MACRUM | 0:276e7a263c35 | 432 | ARM_UC_SetVendorId((const uint8_t*) value, length); |
| MACRUM | 0:276e7a263c35 | 433 | break; |
| MACRUM | 0:276e7a263c35 | 434 | case M2MDevice::ModelNumber: |
| MACRUM | 0:276e7a263c35 | 435 | ARM_UC_SetClassId((const uint8_t*) value, length); |
| MACRUM | 0:276e7a263c35 | 436 | break; |
| MACRUM | 0:276e7a263c35 | 437 | default: |
| MACRUM | 0:276e7a263c35 | 438 | break; |
| MACRUM | 0:276e7a263c35 | 439 | } |
| MACRUM | 0:276e7a263c35 | 440 | #endif |
| MACRUM | 0:276e7a263c35 | 441 | |
| MACRUM | 0:276e7a263c35 | 442 | /* Convert resource to printable string if necessary */ |
| MACRUM | 0:276e7a263c35 | 443 | |
| MACRUM | 0:276e7a263c35 | 444 | /* Getting object instance from factory */ |
| MACRUM | 0:276e7a263c35 | 445 | M2MDevice *device_object = M2MInterfaceFactory::create_device(); |
| MACRUM | 0:276e7a263c35 | 446 | |
| MACRUM | 0:276e7a263c35 | 447 | /* Check device object and resource both are present */ |
| MACRUM | 0:276e7a263c35 | 448 | if (device_object && device_object->is_resource_present(resource)) { |
| MACRUM | 0:276e7a263c35 | 449 | /* set counter to not-zero */ |
| MACRUM | 0:276e7a263c35 | 450 | uint8_t printable_length = 0xFF; |
| MACRUM | 0:276e7a263c35 | 451 | |
| MACRUM | 0:276e7a263c35 | 452 | /* set printable_length to 0 if the buffer is not printable */ |
| MACRUM | 0:276e7a263c35 | 453 | for (uint8_t index = 0; index < length; index++) { |
| MACRUM | 0:276e7a263c35 | 454 | /* break if character is not printable */ |
| MACRUM | 0:276e7a263c35 | 455 | if ((value[index] < ' ') || (value[index] > '~')) { |
| MACRUM | 0:276e7a263c35 | 456 | printable_length = 0; |
| MACRUM | 0:276e7a263c35 | 457 | break; |
| MACRUM | 0:276e7a263c35 | 458 | } |
| MACRUM | 0:276e7a263c35 | 459 | } |
| MACRUM | 0:276e7a263c35 | 460 | |
| MACRUM | 0:276e7a263c35 | 461 | /* resource is a string */ |
| MACRUM | 0:276e7a263c35 | 462 | if (printable_length != 0) { |
| MACRUM | 0:276e7a263c35 | 463 | /* reset counter */ |
| MACRUM | 0:276e7a263c35 | 464 | printable_length = 0; |
| MACRUM | 0:276e7a263c35 | 465 | |
| MACRUM | 0:276e7a263c35 | 466 | /* find actual printable length */ |
| MACRUM | 0:276e7a263c35 | 467 | for ( ; printable_length < length; printable_length++) { |
| MACRUM | 0:276e7a263c35 | 468 | /* break prematurely if end-of-string character is found */ |
| MACRUM | 0:276e7a263c35 | 469 | if (value[printable_length] == '\0') { |
| MACRUM | 0:276e7a263c35 | 470 | break; |
| MACRUM | 0:276e7a263c35 | 471 | } |
| MACRUM | 0:276e7a263c35 | 472 | } |
| MACRUM | 0:276e7a263c35 | 473 | |
| MACRUM | 0:276e7a263c35 | 474 | /* convert to string and set value in object */ |
| MACRUM | 0:276e7a263c35 | 475 | String string_value(value, printable_length); |
| MACRUM | 0:276e7a263c35 | 476 | retval = device_object->set_resource_value(resource, string_value); |
| MACRUM | 0:276e7a263c35 | 477 | } |
| MACRUM | 0:276e7a263c35 | 478 | else |
| MACRUM | 0:276e7a263c35 | 479 | { |
| MACRUM | 0:276e7a263c35 | 480 | /* resource is a byte array */ |
| MACRUM | 0:276e7a263c35 | 481 | char value_buffer[0xFF] = { 0 }; |
| MACRUM | 0:276e7a263c35 | 482 | |
| MACRUM | 0:276e7a263c35 | 483 | /* count length */ |
| MACRUM | 0:276e7a263c35 | 484 | uint8_t index = 0; |
| MACRUM | 0:276e7a263c35 | 485 | |
| MACRUM | 0:276e7a263c35 | 486 | /* convert byte array to string */ |
| MACRUM | 0:276e7a263c35 | 487 | for ( ; |
| MACRUM | 0:276e7a263c35 | 488 | (index < length) && ((2*index +1) < 0xFF); |
| MACRUM | 0:276e7a263c35 | 489 | index++) { |
| MACRUM | 0:276e7a263c35 | 490 | |
| MACRUM | 0:276e7a263c35 | 491 | uint8_t byte = value[index]; |
| MACRUM | 0:276e7a263c35 | 492 | |
| MACRUM | 0:276e7a263c35 | 493 | value_buffer[2 * index] = hex_table[byte >> 4]; |
| MACRUM | 0:276e7a263c35 | 494 | value_buffer[2 * index + 1] = hex_table[byte & 0x0F]; |
| MACRUM | 0:276e7a263c35 | 495 | } |
| MACRUM | 0:276e7a263c35 | 496 | |
| MACRUM | 0:276e7a263c35 | 497 | /* convert to string and set value in object */ |
| MACRUM | 0:276e7a263c35 | 498 | String string_value(value_buffer, 2 * (index - 1)); |
| MACRUM | 0:276e7a263c35 | 499 | retval = device_object->set_resource_value(resource, string_value); |
| MACRUM | 0:276e7a263c35 | 500 | } |
| MACRUM | 0:276e7a263c35 | 501 | } |
| MACRUM | 0:276e7a263c35 | 502 | } |
| MACRUM | 0:276e7a263c35 | 503 | |
| MACRUM | 0:276e7a263c35 | 504 | return retval; |
| MACRUM | 0:276e7a263c35 | 505 | } |
| MACRUM | 0:276e7a263c35 | 506 | |
| MACRUM | 0:276e7a263c35 | 507 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
| MACRUM | 0:276e7a263c35 | 508 | void ServiceClient::set_update_authorize_handler(void (*handler)(int32_t request)) |
| MACRUM | 0:276e7a263c35 | 509 | { |
| MACRUM | 0:276e7a263c35 | 510 | UpdateClient::set_update_authorize_handler(handler); |
| MACRUM | 0:276e7a263c35 | 511 | } |
| MACRUM | 0:276e7a263c35 | 512 | |
| MACRUM | 0:276e7a263c35 | 513 | void ServiceClient::update_authorize(int32_t request) |
| MACRUM | 0:276e7a263c35 | 514 | { |
| MACRUM | 0:276e7a263c35 | 515 | UpdateClient::update_authorize(request); |
| MACRUM | 0:276e7a263c35 | 516 | } |
| MACRUM | 0:276e7a263c35 | 517 | |
| MACRUM | 0:276e7a263c35 | 518 | void ServiceClient::set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total)) |
| MACRUM | 0:276e7a263c35 | 519 | { |
| MACRUM | 0:276e7a263c35 | 520 | UpdateClient::set_update_progress_handler(handler); |
| MACRUM | 0:276e7a263c35 | 521 | } |
| MACRUM | 0:276e7a263c35 | 522 | |
| MACRUM | 0:276e7a263c35 | 523 | void ServiceClient::update_error_callback(int32_t error) |
| MACRUM | 0:276e7a263c35 | 524 | { |
| MACRUM | 0:276e7a263c35 | 525 | _service_callback.error(error, ERROR_UPDATE); |
| MACRUM | 0:276e7a263c35 | 526 | } |
| MACRUM | 0:276e7a263c35 | 527 | #endif |