Example
Dependencies: FXAS21002 FXOS8700Q
simple-mbed-cloud-client/simple-mbed-cloud-client/simple-mbed-cloud-client.cpp@0:11cc2b7889af, 2019-11-19 (annotated)
- Committer:
- maygup01
- Date:
- Tue Nov 19 09:49:38 2019 +0000
- Revision:
- 0:11cc2b7889af
Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maygup01 | 0:11cc2b7889af | 1 | // ---------------------------------------------------------------------------- |
maygup01 | 0:11cc2b7889af | 2 | // Copyright 2016-2018 ARM Ltd. |
maygup01 | 0:11cc2b7889af | 3 | // |
maygup01 | 0:11cc2b7889af | 4 | // SPDX-License-Identifier: Apache-2.0 |
maygup01 | 0:11cc2b7889af | 5 | // |
maygup01 | 0:11cc2b7889af | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
maygup01 | 0:11cc2b7889af | 7 | // you may not use this file except in compliance with the License. |
maygup01 | 0:11cc2b7889af | 8 | // You may obtain a copy of the License at |
maygup01 | 0:11cc2b7889af | 9 | // |
maygup01 | 0:11cc2b7889af | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
maygup01 | 0:11cc2b7889af | 11 | // |
maygup01 | 0:11cc2b7889af | 12 | // Unless required by applicable law or agreed to in writing, software |
maygup01 | 0:11cc2b7889af | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
maygup01 | 0:11cc2b7889af | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maygup01 | 0:11cc2b7889af | 15 | // See the License for the specific language governing permissions and |
maygup01 | 0:11cc2b7889af | 16 | // limitations under the License. |
maygup01 | 0:11cc2b7889af | 17 | // ---------------------------------------------------------------------------- |
maygup01 | 0:11cc2b7889af | 18 | |
maygup01 | 0:11cc2b7889af | 19 | #include <stdio.h> |
maygup01 | 0:11cc2b7889af | 20 | #include "simple-mbed-cloud-client.h" |
maygup01 | 0:11cc2b7889af | 21 | #include "mbed-trace-helper.h" |
maygup01 | 0:11cc2b7889af | 22 | #include "resource-helper.h" |
maygup01 | 0:11cc2b7889af | 23 | |
maygup01 | 0:11cc2b7889af | 24 | #ifdef MBED_CLOUD_DEV_UPDATE_ID |
maygup01 | 0:11cc2b7889af | 25 | #include "update_client_hub.h" |
maygup01 | 0:11cc2b7889af | 26 | #endif |
maygup01 | 0:11cc2b7889af | 27 | |
maygup01 | 0:11cc2b7889af | 28 | #define TRACE_GROUP "SMCC" |
maygup01 | 0:11cc2b7889af | 29 | |
maygup01 | 0:11cc2b7889af | 30 | #ifdef MBED_CLOUD_CLIENT_USER_CONFIG_FILE |
maygup01 | 0:11cc2b7889af | 31 | #include MBED_CLOUD_CLIENT_USER_CONFIG_FILE |
maygup01 | 0:11cc2b7889af | 32 | #endif |
maygup01 | 0:11cc2b7889af | 33 | |
maygup01 | 0:11cc2b7889af | 34 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
maygup01 | 0:11cc2b7889af | 35 | #include "update-helper/update-helper.h" |
maygup01 | 0:11cc2b7889af | 36 | #endif |
maygup01 | 0:11cc2b7889af | 37 | |
maygup01 | 0:11cc2b7889af | 38 | #ifdef MBED_HEAP_STATS_ENABLED |
maygup01 | 0:11cc2b7889af | 39 | #include "memory_tests.h" |
maygup01 | 0:11cc2b7889af | 40 | #endif |
maygup01 | 0:11cc2b7889af | 41 | |
maygup01 | 0:11cc2b7889af | 42 | #ifndef DEFAULT_FIRMWARE_PATH |
maygup01 | 0:11cc2b7889af | 43 | #define DEFAULT_FIRMWARE_PATH "/fs/firmware" |
maygup01 | 0:11cc2b7889af | 44 | #endif |
maygup01 | 0:11cc2b7889af | 45 | |
maygup01 | 0:11cc2b7889af | 46 | SimpleMbedCloudClient::SimpleMbedCloudClient(NetworkInterface *net, BlockDevice *bd, FileSystem *fs) : |
maygup01 | 0:11cc2b7889af | 47 | _registered(false), |
maygup01 | 0:11cc2b7889af | 48 | _register_called(false), |
maygup01 | 0:11cc2b7889af | 49 | _register_and_connect_called(false), |
maygup01 | 0:11cc2b7889af | 50 | _registered_cb(NULL), |
maygup01 | 0:11cc2b7889af | 51 | _unregistered_cb(NULL), |
maygup01 | 0:11cc2b7889af | 52 | _error_cb(NULL), |
maygup01 | 0:11cc2b7889af | 53 | _net(net), |
maygup01 | 0:11cc2b7889af | 54 | _bd(bd), |
maygup01 | 0:11cc2b7889af | 55 | _fs(fs), |
maygup01 | 0:11cc2b7889af | 56 | _storage(bd, fs) |
maygup01 | 0:11cc2b7889af | 57 | { |
maygup01 | 0:11cc2b7889af | 58 | } |
maygup01 | 0:11cc2b7889af | 59 | |
maygup01 | 0:11cc2b7889af | 60 | SimpleMbedCloudClient::~SimpleMbedCloudClient() { |
maygup01 | 0:11cc2b7889af | 61 | for (unsigned int i = 0; _resources.size(); i++) { |
maygup01 | 0:11cc2b7889af | 62 | delete _resources[i]; |
maygup01 | 0:11cc2b7889af | 63 | } |
maygup01 | 0:11cc2b7889af | 64 | } |
maygup01 | 0:11cc2b7889af | 65 | |
maygup01 | 0:11cc2b7889af | 66 | int SimpleMbedCloudClient::init(bool format) { |
maygup01 | 0:11cc2b7889af | 67 | // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364) |
maygup01 | 0:11cc2b7889af | 68 | // Older versions: workaround to prevent possible deletion of credentials: |
maygup01 | 0:11cc2b7889af | 69 | wait(1); |
maygup01 | 0:11cc2b7889af | 70 | |
maygup01 | 0:11cc2b7889af | 71 | #ifdef MBED_CLOUD_DEV_UPDATE_ID |
maygup01 | 0:11cc2b7889af | 72 | |
maygup01 | 0:11cc2b7889af | 73 | extern const uint8_t arm_uc_vendor_id[]; |
maygup01 | 0:11cc2b7889af | 74 | extern const uint16_t arm_uc_vendor_id_size; |
maygup01 | 0:11cc2b7889af | 75 | extern const uint8_t arm_uc_class_id[]; |
maygup01 | 0:11cc2b7889af | 76 | extern const uint16_t arm_uc_class_id_size; |
maygup01 | 0:11cc2b7889af | 77 | |
maygup01 | 0:11cc2b7889af | 78 | ARM_UC_SetVendorId(arm_uc_vendor_id, arm_uc_vendor_id_size); |
maygup01 | 0:11cc2b7889af | 79 | ARM_UC_SetClassId(arm_uc_class_id, arm_uc_class_id_size); |
maygup01 | 0:11cc2b7889af | 80 | |
maygup01 | 0:11cc2b7889af | 81 | #endif |
maygup01 | 0:11cc2b7889af | 82 | |
maygup01 | 0:11cc2b7889af | 83 | // Initialize Mbed Trace for debugging |
maygup01 | 0:11cc2b7889af | 84 | // Create mutex for tracing to avoid broken lines in logs |
maygup01 | 0:11cc2b7889af | 85 | if(!mbed_trace_helper_create_mutex()) { |
maygup01 | 0:11cc2b7889af | 86 | printf("[SMCC] ERROR - Mutex creation for mbed_trace failed!\n"); |
maygup01 | 0:11cc2b7889af | 87 | return 1; |
maygup01 | 0:11cc2b7889af | 88 | } |
maygup01 | 0:11cc2b7889af | 89 | |
maygup01 | 0:11cc2b7889af | 90 | // Initialize mbed trace |
maygup01 | 0:11cc2b7889af | 91 | mbed_trace_init(); |
maygup01 | 0:11cc2b7889af | 92 | mbed_trace_helper_create_mutex(); |
maygup01 | 0:11cc2b7889af | 93 | mbed_trace_mutex_wait_function_set(mbed_trace_helper_mutex_wait); |
maygup01 | 0:11cc2b7889af | 94 | mbed_trace_mutex_release_function_set(mbed_trace_helper_mutex_release); |
maygup01 | 0:11cc2b7889af | 95 | |
maygup01 | 0:11cc2b7889af | 96 | // Initialize the FCC |
maygup01 | 0:11cc2b7889af | 97 | int status = fcc_init(); |
maygup01 | 0:11cc2b7889af | 98 | if (status != FCC_STATUS_SUCCESS && status != FCC_STATUS_ENTROPY_ERROR && status != FCC_STATUS_ROT_ERROR) { |
maygup01 | 0:11cc2b7889af | 99 | tr_error("Factory Client Configuration failed with status %d", status); |
maygup01 | 0:11cc2b7889af | 100 | return 1; |
maygup01 | 0:11cc2b7889af | 101 | } |
maygup01 | 0:11cc2b7889af | 102 | |
maygup01 | 0:11cc2b7889af | 103 | status = _storage.init(); |
maygup01 | 0:11cc2b7889af | 104 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 105 | tr_error("Failed to initialize storage layer (%d)", status); |
maygup01 | 0:11cc2b7889af | 106 | return 1; |
maygup01 | 0:11cc2b7889af | 107 | } |
maygup01 | 0:11cc2b7889af | 108 | |
maygup01 | 0:11cc2b7889af | 109 | status = _storage.sotp_init(); |
maygup01 | 0:11cc2b7889af | 110 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 111 | tr_error("Could not initialize SOTP (%d)", status); |
maygup01 | 0:11cc2b7889af | 112 | fcc_finalize(); |
maygup01 | 0:11cc2b7889af | 113 | return 1; |
maygup01 | 0:11cc2b7889af | 114 | } |
maygup01 | 0:11cc2b7889af | 115 | |
maygup01 | 0:11cc2b7889af | 116 | #if RESET_STORAGE |
maygup01 | 0:11cc2b7889af | 117 | status = reset_storage(); |
maygup01 | 0:11cc2b7889af | 118 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 119 | tr_error("reset_storage (triggered by RESET_STORAGE macro) failed (%d)", status); |
maygup01 | 0:11cc2b7889af | 120 | return 1; |
maygup01 | 0:11cc2b7889af | 121 | } |
maygup01 | 0:11cc2b7889af | 122 | // Reinitialize SOTP |
maygup01 | 0:11cc2b7889af | 123 | status = _storage.sotp_init(); |
maygup01 | 0:11cc2b7889af | 124 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 125 | tr_error("Could not initialize SOTP (%d)", status); |
maygup01 | 0:11cc2b7889af | 126 | return 1; |
maygup01 | 0:11cc2b7889af | 127 | } |
maygup01 | 0:11cc2b7889af | 128 | #endif |
maygup01 | 0:11cc2b7889af | 129 | |
maygup01 | 0:11cc2b7889af | 130 | status = verify_cloud_configuration(format); |
maygup01 | 0:11cc2b7889af | 131 | |
maygup01 | 0:11cc2b7889af | 132 | if (status != 0) { |
maygup01 | 0:11cc2b7889af | 133 | // This is designed to simplify user-experience by auto-formatting the |
maygup01 | 0:11cc2b7889af | 134 | // primary storage if no valid certificates exist. |
maygup01 | 0:11cc2b7889af | 135 | // This should never be used for any kind of production devices. |
maygup01 | 0:11cc2b7889af | 136 | #if MBED_CONF_APP_FORMAT_STORAGE_LAYER_ON_ERROR == 1 |
maygup01 | 0:11cc2b7889af | 137 | tr_info("Could not load certificate (e.g. no certificates or RoT might have changed), resetting storage..."); |
maygup01 | 0:11cc2b7889af | 138 | status = reset_storage(); |
maygup01 | 0:11cc2b7889af | 139 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 140 | return status; |
maygup01 | 0:11cc2b7889af | 141 | } |
maygup01 | 0:11cc2b7889af | 142 | status = _storage.sotp_init(); |
maygup01 | 0:11cc2b7889af | 143 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 144 | return status; |
maygup01 | 0:11cc2b7889af | 145 | } |
maygup01 | 0:11cc2b7889af | 146 | status = verify_cloud_configuration(format); |
maygup01 | 0:11cc2b7889af | 147 | if (status != 0) { |
maygup01 | 0:11cc2b7889af | 148 | return status; |
maygup01 | 0:11cc2b7889af | 149 | } |
maygup01 | 0:11cc2b7889af | 150 | #else |
maygup01 | 0:11cc2b7889af | 151 | return 1; |
maygup01 | 0:11cc2b7889af | 152 | #endif |
maygup01 | 0:11cc2b7889af | 153 | } |
maygup01 | 0:11cc2b7889af | 154 | |
maygup01 | 0:11cc2b7889af | 155 | // Deletes existing firmware images from storage. |
maygup01 | 0:11cc2b7889af | 156 | // This deletes any existing firmware images during application startup. |
maygup01 | 0:11cc2b7889af | 157 | // This compilation flag is currently implemented only for mbed OS. |
maygup01 | 0:11cc2b7889af | 158 | #ifdef RESET_FIRMWARE |
maygup01 | 0:11cc2b7889af | 159 | palStatus_t status = PAL_SUCCESS; |
maygup01 | 0:11cc2b7889af | 160 | status = pal_fsRmFiles(DEFAULT_FIRMWARE_PATH); |
maygup01 | 0:11cc2b7889af | 161 | if(status == PAL_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 162 | printf("[SMCC] Firmware storage erased\n"); |
maygup01 | 0:11cc2b7889af | 163 | } else if (status == PAL_ERR_FS_NO_PATH) { |
maygup01 | 0:11cc2b7889af | 164 | tr_info("Firmware path not found/does not exist"); |
maygup01 | 0:11cc2b7889af | 165 | } else { |
maygup01 | 0:11cc2b7889af | 166 | tr_error("Firmware storage erasing failed with %" PRId32, status); |
maygup01 | 0:11cc2b7889af | 167 | return 1; |
maygup01 | 0:11cc2b7889af | 168 | } |
maygup01 | 0:11cc2b7889af | 169 | #endif |
maygup01 | 0:11cc2b7889af | 170 | |
maygup01 | 0:11cc2b7889af | 171 | return 0; |
maygup01 | 0:11cc2b7889af | 172 | } |
maygup01 | 0:11cc2b7889af | 173 | |
maygup01 | 0:11cc2b7889af | 174 | bool SimpleMbedCloudClient::call_register() { |
maygup01 | 0:11cc2b7889af | 175 | // need to unregister first before calling this function again |
maygup01 | 0:11cc2b7889af | 176 | if (_register_called) return false; |
maygup01 | 0:11cc2b7889af | 177 | |
maygup01 | 0:11cc2b7889af | 178 | _cloud_client.on_registered(this, &SimpleMbedCloudClient::client_registered); |
maygup01 | 0:11cc2b7889af | 179 | _cloud_client.on_unregistered(this, &SimpleMbedCloudClient::client_unregistered); |
maygup01 | 0:11cc2b7889af | 180 | _cloud_client.on_error(this, &SimpleMbedCloudClient::error); |
maygup01 | 0:11cc2b7889af | 181 | |
maygup01 | 0:11cc2b7889af | 182 | bool setup = _cloud_client.setup(_net); |
maygup01 | 0:11cc2b7889af | 183 | _register_called = true; |
maygup01 | 0:11cc2b7889af | 184 | if (!setup) { |
maygup01 | 0:11cc2b7889af | 185 | tr_error("Client setup failed"); |
maygup01 | 0:11cc2b7889af | 186 | return false; |
maygup01 | 0:11cc2b7889af | 187 | } |
maygup01 | 0:11cc2b7889af | 188 | |
maygup01 | 0:11cc2b7889af | 189 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
maygup01 | 0:11cc2b7889af | 190 | /* Set callback functions for authorizing updates and monitoring progress. |
maygup01 | 0:11cc2b7889af | 191 | Code is implemented in update_ui_example.cpp |
maygup01 | 0:11cc2b7889af | 192 | Both callbacks are completely optional. If no authorization callback |
maygup01 | 0:11cc2b7889af | 193 | is set, the update process will procede immediately in each step. |
maygup01 | 0:11cc2b7889af | 194 | */ |
maygup01 | 0:11cc2b7889af | 195 | update_helper_set_cloud_client(&_cloud_client); |
maygup01 | 0:11cc2b7889af | 196 | _cloud_client.set_update_authorize_handler(update_authorize); |
maygup01 | 0:11cc2b7889af | 197 | _cloud_client.set_update_progress_handler(update_progress); |
maygup01 | 0:11cc2b7889af | 198 | #endif |
maygup01 | 0:11cc2b7889af | 199 | return true; |
maygup01 | 0:11cc2b7889af | 200 | } |
maygup01 | 0:11cc2b7889af | 201 | |
maygup01 | 0:11cc2b7889af | 202 | void SimpleMbedCloudClient::close() { |
maygup01 | 0:11cc2b7889af | 203 | _cloud_client.close(); |
maygup01 | 0:11cc2b7889af | 204 | } |
maygup01 | 0:11cc2b7889af | 205 | |
maygup01 | 0:11cc2b7889af | 206 | void SimpleMbedCloudClient::register_update() { |
maygup01 | 0:11cc2b7889af | 207 | _cloud_client.register_update(); |
maygup01 | 0:11cc2b7889af | 208 | } |
maygup01 | 0:11cc2b7889af | 209 | |
maygup01 | 0:11cc2b7889af | 210 | void SimpleMbedCloudClient::client_registered() { |
maygup01 | 0:11cc2b7889af | 211 | _registered = true; |
maygup01 | 0:11cc2b7889af | 212 | static const ConnectorClientEndpointInfo* endpoint = NULL; |
maygup01 | 0:11cc2b7889af | 213 | if (endpoint == NULL) { |
maygup01 | 0:11cc2b7889af | 214 | endpoint = _cloud_client.endpoint_info(); |
maygup01 | 0:11cc2b7889af | 215 | if (endpoint && _registered_cb) { |
maygup01 | 0:11cc2b7889af | 216 | _registered_cb(endpoint); |
maygup01 | 0:11cc2b7889af | 217 | } |
maygup01 | 0:11cc2b7889af | 218 | } |
maygup01 | 0:11cc2b7889af | 219 | #ifdef MBED_HEAP_STATS_ENABLED |
maygup01 | 0:11cc2b7889af | 220 | heap_stats(); |
maygup01 | 0:11cc2b7889af | 221 | #endif |
maygup01 | 0:11cc2b7889af | 222 | } |
maygup01 | 0:11cc2b7889af | 223 | |
maygup01 | 0:11cc2b7889af | 224 | void SimpleMbedCloudClient::client_unregistered() { |
maygup01 | 0:11cc2b7889af | 225 | _registered = false; |
maygup01 | 0:11cc2b7889af | 226 | _register_called = false; |
maygup01 | 0:11cc2b7889af | 227 | |
maygup01 | 0:11cc2b7889af | 228 | if (_unregistered_cb) { |
maygup01 | 0:11cc2b7889af | 229 | _unregistered_cb(); |
maygup01 | 0:11cc2b7889af | 230 | } |
maygup01 | 0:11cc2b7889af | 231 | |
maygup01 | 0:11cc2b7889af | 232 | #ifdef MBED_HEAP_STATS_ENABLED |
maygup01 | 0:11cc2b7889af | 233 | heap_stats(); |
maygup01 | 0:11cc2b7889af | 234 | #endif |
maygup01 | 0:11cc2b7889af | 235 | } |
maygup01 | 0:11cc2b7889af | 236 | |
maygup01 | 0:11cc2b7889af | 237 | void SimpleMbedCloudClient::error(int error_code) { |
maygup01 | 0:11cc2b7889af | 238 | const char *error; |
maygup01 | 0:11cc2b7889af | 239 | switch(error_code) { |
maygup01 | 0:11cc2b7889af | 240 | case MbedCloudClient::ConnectErrorNone: |
maygup01 | 0:11cc2b7889af | 241 | error = "MbedCloudClient::ConnectErrorNone"; |
maygup01 | 0:11cc2b7889af | 242 | break; |
maygup01 | 0:11cc2b7889af | 243 | case MbedCloudClient::ConnectAlreadyExists: |
maygup01 | 0:11cc2b7889af | 244 | error = "MbedCloudClient::ConnectAlreadyExists"; |
maygup01 | 0:11cc2b7889af | 245 | break; |
maygup01 | 0:11cc2b7889af | 246 | case MbedCloudClient::ConnectBootstrapFailed: |
maygup01 | 0:11cc2b7889af | 247 | error = "MbedCloudClient::ConnectBootstrapFailed"; |
maygup01 | 0:11cc2b7889af | 248 | break; |
maygup01 | 0:11cc2b7889af | 249 | case MbedCloudClient::ConnectInvalidParameters: |
maygup01 | 0:11cc2b7889af | 250 | error = "MbedCloudClient::ConnectInvalidParameters"; |
maygup01 | 0:11cc2b7889af | 251 | break; |
maygup01 | 0:11cc2b7889af | 252 | case MbedCloudClient::ConnectNotRegistered: |
maygup01 | 0:11cc2b7889af | 253 | error = "MbedCloudClient::ConnectNotRegistered"; |
maygup01 | 0:11cc2b7889af | 254 | break; |
maygup01 | 0:11cc2b7889af | 255 | case MbedCloudClient::ConnectTimeout: |
maygup01 | 0:11cc2b7889af | 256 | error = "MbedCloudClient::ConnectTimeout"; |
maygup01 | 0:11cc2b7889af | 257 | break; |
maygup01 | 0:11cc2b7889af | 258 | case MbedCloudClient::ConnectNetworkError: |
maygup01 | 0:11cc2b7889af | 259 | error = "MbedCloudClient::ConnectNetworkError"; |
maygup01 | 0:11cc2b7889af | 260 | break; |
maygup01 | 0:11cc2b7889af | 261 | case MbedCloudClient::ConnectResponseParseFailed: |
maygup01 | 0:11cc2b7889af | 262 | error = "MbedCloudClient::ConnectResponseParseFailed"; |
maygup01 | 0:11cc2b7889af | 263 | break; |
maygup01 | 0:11cc2b7889af | 264 | case MbedCloudClient::ConnectUnknownError: |
maygup01 | 0:11cc2b7889af | 265 | error = "MbedCloudClient::ConnectUnknownError"; |
maygup01 | 0:11cc2b7889af | 266 | break; |
maygup01 | 0:11cc2b7889af | 267 | case MbedCloudClient::ConnectMemoryConnectFail: |
maygup01 | 0:11cc2b7889af | 268 | error = "MbedCloudClient::ConnectMemoryConnectFail"; |
maygup01 | 0:11cc2b7889af | 269 | break; |
maygup01 | 0:11cc2b7889af | 270 | case MbedCloudClient::ConnectNotAllowed: |
maygup01 | 0:11cc2b7889af | 271 | error = "MbedCloudClient::ConnectNotAllowed"; |
maygup01 | 0:11cc2b7889af | 272 | break; |
maygup01 | 0:11cc2b7889af | 273 | case MbedCloudClient::ConnectSecureConnectionFailed: |
maygup01 | 0:11cc2b7889af | 274 | error = "MbedCloudClient::ConnectSecureConnectionFailed"; |
maygup01 | 0:11cc2b7889af | 275 | break; |
maygup01 | 0:11cc2b7889af | 276 | case MbedCloudClient::ConnectDnsResolvingFailed: |
maygup01 | 0:11cc2b7889af | 277 | error = "MbedCloudClient::ConnectDnsResolvingFailed"; |
maygup01 | 0:11cc2b7889af | 278 | break; |
maygup01 | 0:11cc2b7889af | 279 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
maygup01 | 0:11cc2b7889af | 280 | case MbedCloudClient::UpdateWarningCertificateNotFound: |
maygup01 | 0:11cc2b7889af | 281 | error = "MbedCloudClient::UpdateWarningCertificateNotFound"; |
maygup01 | 0:11cc2b7889af | 282 | break; |
maygup01 | 0:11cc2b7889af | 283 | case MbedCloudClient::UpdateWarningIdentityNotFound: |
maygup01 | 0:11cc2b7889af | 284 | error = "MbedCloudClient::UpdateWarningIdentityNotFound"; |
maygup01 | 0:11cc2b7889af | 285 | break; |
maygup01 | 0:11cc2b7889af | 286 | case MbedCloudClient::UpdateWarningCertificateInvalid: |
maygup01 | 0:11cc2b7889af | 287 | error = "MbedCloudClient::UpdateWarningCertificateInvalid"; |
maygup01 | 0:11cc2b7889af | 288 | break; |
maygup01 | 0:11cc2b7889af | 289 | case MbedCloudClient::UpdateWarningSignatureInvalid: |
maygup01 | 0:11cc2b7889af | 290 | error = "MbedCloudClient::UpdateWarningSignatureInvalid"; |
maygup01 | 0:11cc2b7889af | 291 | break; |
maygup01 | 0:11cc2b7889af | 292 | case MbedCloudClient::UpdateWarningVendorMismatch: |
maygup01 | 0:11cc2b7889af | 293 | error = "MbedCloudClient::UpdateWarningVendorMismatch"; |
maygup01 | 0:11cc2b7889af | 294 | break; |
maygup01 | 0:11cc2b7889af | 295 | case MbedCloudClient::UpdateWarningClassMismatch: |
maygup01 | 0:11cc2b7889af | 296 | error = "MbedCloudClient::UpdateWarningClassMismatch"; |
maygup01 | 0:11cc2b7889af | 297 | break; |
maygup01 | 0:11cc2b7889af | 298 | case MbedCloudClient::UpdateWarningDeviceMismatch: |
maygup01 | 0:11cc2b7889af | 299 | error = "MbedCloudClient::UpdateWarningDeviceMismatch"; |
maygup01 | 0:11cc2b7889af | 300 | break; |
maygup01 | 0:11cc2b7889af | 301 | case MbedCloudClient::UpdateWarningURINotFound: |
maygup01 | 0:11cc2b7889af | 302 | error = "MbedCloudClient::UpdateWarningURINotFound"; |
maygup01 | 0:11cc2b7889af | 303 | break; |
maygup01 | 0:11cc2b7889af | 304 | case MbedCloudClient::UpdateWarningRollbackProtection: |
maygup01 | 0:11cc2b7889af | 305 | error = "MbedCloudClient::UpdateWarningRollbackProtection"; |
maygup01 | 0:11cc2b7889af | 306 | break; |
maygup01 | 0:11cc2b7889af | 307 | case MbedCloudClient::UpdateWarningUnknown: |
maygup01 | 0:11cc2b7889af | 308 | error = "MbedCloudClient::UpdateWarningUnknown"; |
maygup01 | 0:11cc2b7889af | 309 | break; |
maygup01 | 0:11cc2b7889af | 310 | case MbedCloudClient::UpdateErrorWriteToStorage: |
maygup01 | 0:11cc2b7889af | 311 | error = "MbedCloudClient::UpdateErrorWriteToStorage"; |
maygup01 | 0:11cc2b7889af | 312 | break; |
maygup01 | 0:11cc2b7889af | 313 | case MbedCloudClient::UpdateErrorInvalidHash: |
maygup01 | 0:11cc2b7889af | 314 | error = "MbedCloudClient::UpdateErrorInvalidHash"; |
maygup01 | 0:11cc2b7889af | 315 | break; |
maygup01 | 0:11cc2b7889af | 316 | #endif |
maygup01 | 0:11cc2b7889af | 317 | default: |
maygup01 | 0:11cc2b7889af | 318 | error = "UNKNOWN"; |
maygup01 | 0:11cc2b7889af | 319 | } |
maygup01 | 0:11cc2b7889af | 320 | |
maygup01 | 0:11cc2b7889af | 321 | if (_error_cb) { |
maygup01 | 0:11cc2b7889af | 322 | _error_cb(error_code, error); |
maygup01 | 0:11cc2b7889af | 323 | return; |
maygup01 | 0:11cc2b7889af | 324 | } |
maygup01 | 0:11cc2b7889af | 325 | |
maygup01 | 0:11cc2b7889af | 326 | printf("\n[SMCC] Error occurred : %s\n", error); |
maygup01 | 0:11cc2b7889af | 327 | printf("[SMCC] Error code : %d\n", error_code); |
maygup01 | 0:11cc2b7889af | 328 | printf("[SMCC] Error details : %s\n",_cloud_client.error_description()); |
maygup01 | 0:11cc2b7889af | 329 | } |
maygup01 | 0:11cc2b7889af | 330 | |
maygup01 | 0:11cc2b7889af | 331 | bool SimpleMbedCloudClient::is_client_registered() { |
maygup01 | 0:11cc2b7889af | 332 | return _registered; |
maygup01 | 0:11cc2b7889af | 333 | } |
maygup01 | 0:11cc2b7889af | 334 | |
maygup01 | 0:11cc2b7889af | 335 | bool SimpleMbedCloudClient::is_register_called() { |
maygup01 | 0:11cc2b7889af | 336 | return _register_called; |
maygup01 | 0:11cc2b7889af | 337 | } |
maygup01 | 0:11cc2b7889af | 338 | |
maygup01 | 0:11cc2b7889af | 339 | bool SimpleMbedCloudClient::register_and_connect() { |
maygup01 | 0:11cc2b7889af | 340 | if (_register_and_connect_called) return false; |
maygup01 | 0:11cc2b7889af | 341 | |
maygup01 | 0:11cc2b7889af | 342 | mcc_resource_def resourceDef; |
maygup01 | 0:11cc2b7889af | 343 | |
maygup01 | 0:11cc2b7889af | 344 | for (int i = 0; i < _resources.size(); i++) { |
maygup01 | 0:11cc2b7889af | 345 | _resources[i]->get_data(&resourceDef); |
maygup01 | 0:11cc2b7889af | 346 | M2MResource *res = add_resource(&_obj_list, resourceDef.object_id, resourceDef.instance_id, |
maygup01 | 0:11cc2b7889af | 347 | resourceDef.resource_id, resourceDef.name.c_str(), M2MResourceInstance::STRING, |
maygup01 | 0:11cc2b7889af | 348 | (M2MBase::Operation)resourceDef.method_mask, resourceDef.value.c_str(), resourceDef.observable, |
maygup01 | 0:11cc2b7889af | 349 | resourceDef.put_callback, resourceDef.post_callback, resourceDef.notification_callback); |
maygup01 | 0:11cc2b7889af | 350 | _resources[i]->set_m2m_resource(res); |
maygup01 | 0:11cc2b7889af | 351 | } |
maygup01 | 0:11cc2b7889af | 352 | _cloud_client.add_objects(_obj_list); |
maygup01 | 0:11cc2b7889af | 353 | |
maygup01 | 0:11cc2b7889af | 354 | _register_and_connect_called = true; |
maygup01 | 0:11cc2b7889af | 355 | |
maygup01 | 0:11cc2b7889af | 356 | // Start registering to the cloud. |
maygup01 | 0:11cc2b7889af | 357 | bool retval = call_register(); |
maygup01 | 0:11cc2b7889af | 358 | |
maygup01 | 0:11cc2b7889af | 359 | // Print memory statistics if the MBED_HEAP_STATS_ENABLED is defined. |
maygup01 | 0:11cc2b7889af | 360 | #ifdef MBED_HEAP_STATS_ENABLED |
maygup01 | 0:11cc2b7889af | 361 | tr_info("Register being called"); |
maygup01 | 0:11cc2b7889af | 362 | heap_stats(); |
maygup01 | 0:11cc2b7889af | 363 | #endif |
maygup01 | 0:11cc2b7889af | 364 | |
maygup01 | 0:11cc2b7889af | 365 | return retval; |
maygup01 | 0:11cc2b7889af | 366 | } |
maygup01 | 0:11cc2b7889af | 367 | |
maygup01 | 0:11cc2b7889af | 368 | void SimpleMbedCloudClient::on_registered(Callback<void(const ConnectorClientEndpointInfo*)> cb) { |
maygup01 | 0:11cc2b7889af | 369 | _registered_cb = cb; |
maygup01 | 0:11cc2b7889af | 370 | } |
maygup01 | 0:11cc2b7889af | 371 | |
maygup01 | 0:11cc2b7889af | 372 | void SimpleMbedCloudClient::on_unregistered(Callback<void()> cb) { |
maygup01 | 0:11cc2b7889af | 373 | _unregistered_cb = cb; |
maygup01 | 0:11cc2b7889af | 374 | } |
maygup01 | 0:11cc2b7889af | 375 | |
maygup01 | 0:11cc2b7889af | 376 | void SimpleMbedCloudClient::on_update_authorized(void (*cb)(int32_t request)) { |
maygup01 | 0:11cc2b7889af | 377 | _cloud_client.set_update_authorize_handler(cb); |
maygup01 | 0:11cc2b7889af | 378 | } |
maygup01 | 0:11cc2b7889af | 379 | |
maygup01 | 0:11cc2b7889af | 380 | void SimpleMbedCloudClient::on_update_progress(void (*cb)(uint32_t progress, uint32_t total)) { |
maygup01 | 0:11cc2b7889af | 381 | _cloud_client.set_update_progress_handler(cb); |
maygup01 | 0:11cc2b7889af | 382 | } |
maygup01 | 0:11cc2b7889af | 383 | |
maygup01 | 0:11cc2b7889af | 384 | void SimpleMbedCloudClient::on_error_cb(Callback<void(int, const char*)> cb) { |
maygup01 | 0:11cc2b7889af | 385 | _error_cb = cb; |
maygup01 | 0:11cc2b7889af | 386 | } |
maygup01 | 0:11cc2b7889af | 387 | |
maygup01 | 0:11cc2b7889af | 388 | int SimpleMbedCloudClient::reformat_storage() { |
maygup01 | 0:11cc2b7889af | 389 | return _storage.reformat_storage(); |
maygup01 | 0:11cc2b7889af | 390 | } |
maygup01 | 0:11cc2b7889af | 391 | |
maygup01 | 0:11cc2b7889af | 392 | MbedCloudClient *SimpleMbedCloudClient::get_cloud_client() { |
maygup01 | 0:11cc2b7889af | 393 | return &_cloud_client; |
maygup01 | 0:11cc2b7889af | 394 | } |
maygup01 | 0:11cc2b7889af | 395 | |
maygup01 | 0:11cc2b7889af | 396 | MbedCloudClientResource* SimpleMbedCloudClient::create_resource(const char *path, const char *name) { |
maygup01 | 0:11cc2b7889af | 397 | MbedCloudClientResource *resource = new MbedCloudClientResource(this, path, name); |
maygup01 | 0:11cc2b7889af | 398 | _resources.push_back(resource); |
maygup01 | 0:11cc2b7889af | 399 | return resource; |
maygup01 | 0:11cc2b7889af | 400 | } |
maygup01 | 0:11cc2b7889af | 401 | |
maygup01 | 0:11cc2b7889af | 402 | int SimpleMbedCloudClient::reset_storage() { |
maygup01 | 0:11cc2b7889af | 403 | tr_info("Resetting storage to an empty state..."); |
maygup01 | 0:11cc2b7889af | 404 | int status = fcc_storage_delete(); |
maygup01 | 0:11cc2b7889af | 405 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 406 | tr_debug("Failed to delete FCC storage (%d), formatting...", status); |
maygup01 | 0:11cc2b7889af | 407 | |
maygup01 | 0:11cc2b7889af | 408 | status = _storage.reformat_storage(); |
maygup01 | 0:11cc2b7889af | 409 | if (status == 0) { |
maygup01 | 0:11cc2b7889af | 410 | tr_debug("Storage reformatted, resetting storage again..."); |
maygup01 | 0:11cc2b7889af | 411 | // Try to reset storage again after format. |
maygup01 | 0:11cc2b7889af | 412 | // It is required to run fcc_storage_delete() after format. |
maygup01 | 0:11cc2b7889af | 413 | status = fcc_storage_delete(); |
maygup01 | 0:11cc2b7889af | 414 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 415 | tr_warn("Failed to delete FCC storage (again) (%d)", status); |
maygup01 | 0:11cc2b7889af | 416 | } |
maygup01 | 0:11cc2b7889af | 417 | else { |
maygup01 | 0:11cc2b7889af | 418 | tr_debug("Deleted FCC storage"); |
maygup01 | 0:11cc2b7889af | 419 | } |
maygup01 | 0:11cc2b7889af | 420 | } |
maygup01 | 0:11cc2b7889af | 421 | } |
maygup01 | 0:11cc2b7889af | 422 | |
maygup01 | 0:11cc2b7889af | 423 | if (status == FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 424 | tr_info("OK - Reset storage to an empty state..."); |
maygup01 | 0:11cc2b7889af | 425 | } |
maygup01 | 0:11cc2b7889af | 426 | |
maygup01 | 0:11cc2b7889af | 427 | return status; |
maygup01 | 0:11cc2b7889af | 428 | } |
maygup01 | 0:11cc2b7889af | 429 | |
maygup01 | 0:11cc2b7889af | 430 | int SimpleMbedCloudClient::verify_cloud_configuration(bool format) { |
maygup01 | 0:11cc2b7889af | 431 | int status; |
maygup01 | 0:11cc2b7889af | 432 | |
maygup01 | 0:11cc2b7889af | 433 | #if MBED_CONF_DEVICE_MANAGEMENT_DEVELOPER_MODE == 1 |
maygup01 | 0:11cc2b7889af | 434 | tr_debug("Starting developer flow"); |
maygup01 | 0:11cc2b7889af | 435 | if( format ) { |
maygup01 | 0:11cc2b7889af | 436 | status = reset_storage(); |
maygup01 | 0:11cc2b7889af | 437 | if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 438 | tr_debug("Failed to reset storage"); |
maygup01 | 0:11cc2b7889af | 439 | return status; |
maygup01 | 0:11cc2b7889af | 440 | } |
maygup01 | 0:11cc2b7889af | 441 | } |
maygup01 | 0:11cc2b7889af | 442 | status = fcc_developer_flow(); |
maygup01 | 0:11cc2b7889af | 443 | if (status == FCC_STATUS_KCM_FILE_EXIST_ERROR) { |
maygup01 | 0:11cc2b7889af | 444 | tr_debug("Developer credentials already exist on storage layer, verifying credentials..."); |
maygup01 | 0:11cc2b7889af | 445 | } else if (status != FCC_STATUS_SUCCESS) { |
maygup01 | 0:11cc2b7889af | 446 | tr_debug("No developer credentials on storage layer yet"); |
maygup01 | 0:11cc2b7889af | 447 | return status; |
maygup01 | 0:11cc2b7889af | 448 | } |
maygup01 | 0:11cc2b7889af | 449 | #endif |
maygup01 | 0:11cc2b7889af | 450 | status = fcc_verify_device_configured_4mbed_cloud(); |
maygup01 | 0:11cc2b7889af | 451 | return status; |
maygup01 | 0:11cc2b7889af | 452 | } |