Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
simple-mbed-cloud-client/simple-mbed-cloud-client.cpp@4:e518dde96e59, 2018-10-25 (annotated)
- Committer:
- JimCarver
- Date:
- Thu Oct 25 14:00:12 2018 +0000
- Revision:
- 4:e518dde96e59
- Parent:
- 0:6b753f761943
Simulated dispenser
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCarver | 0:6b753f761943 | 1 | // ---------------------------------------------------------------------------- |
JimCarver | 0:6b753f761943 | 2 | // Copyright 2016-2018 ARM Ltd. |
JimCarver | 0:6b753f761943 | 3 | // |
JimCarver | 0:6b753f761943 | 4 | // SPDX-License-Identifier: Apache-2.0 |
JimCarver | 0:6b753f761943 | 5 | // |
JimCarver | 0:6b753f761943 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
JimCarver | 0:6b753f761943 | 7 | // you may not use this file except in compliance with the License. |
JimCarver | 0:6b753f761943 | 8 | // You may obtain a copy of the License at |
JimCarver | 0:6b753f761943 | 9 | // |
JimCarver | 0:6b753f761943 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
JimCarver | 0:6b753f761943 | 11 | // |
JimCarver | 0:6b753f761943 | 12 | // Unless required by applicable law or agreed to in writing, software |
JimCarver | 0:6b753f761943 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
JimCarver | 0:6b753f761943 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JimCarver | 0:6b753f761943 | 15 | // See the License for the specific language governing permissions and |
JimCarver | 0:6b753f761943 | 16 | // limitations under the License. |
JimCarver | 0:6b753f761943 | 17 | // ---------------------------------------------------------------------------- |
JimCarver | 0:6b753f761943 | 18 | |
JimCarver | 0:6b753f761943 | 19 | #include <stdio.h> |
JimCarver | 0:6b753f761943 | 20 | #include "simple-mbed-cloud-client.h" |
JimCarver | 0:6b753f761943 | 21 | #include "mbed-cloud-client/MbedCloudClient.h" |
JimCarver | 0:6b753f761943 | 22 | #include "m2mdevice.h" |
JimCarver | 0:6b753f761943 | 23 | #include "m2mresource.h" |
JimCarver | 0:6b753f761943 | 24 | #include "mbed-client/m2minterface.h" |
JimCarver | 0:6b753f761943 | 25 | #include "key_config_manager.h" |
JimCarver | 0:6b753f761943 | 26 | #include "resource.h" |
JimCarver | 0:6b753f761943 | 27 | #include "mbed-client/m2mvector.h" |
JimCarver | 0:6b753f761943 | 28 | #include "mbed_cloud_client_resource.h" |
JimCarver | 0:6b753f761943 | 29 | #include "factory_configurator_client.h" |
JimCarver | 0:6b753f761943 | 30 | #include "update_client_hub.h" |
JimCarver | 0:6b753f761943 | 31 | |
JimCarver | 0:6b753f761943 | 32 | #ifdef MBED_CLOUD_CLIENT_USER_CONFIG_FILE |
JimCarver | 0:6b753f761943 | 33 | #include MBED_CLOUD_CLIENT_USER_CONFIG_FILE |
JimCarver | 0:6b753f761943 | 34 | #endif |
JimCarver | 0:6b753f761943 | 35 | |
JimCarver | 0:6b753f761943 | 36 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
JimCarver | 0:6b753f761943 | 37 | #include "update_ui_example.h" |
JimCarver | 0:6b753f761943 | 38 | #endif |
JimCarver | 0:6b753f761943 | 39 | |
JimCarver | 0:6b753f761943 | 40 | #ifdef MBED_HEAP_STATS_ENABLED |
JimCarver | 0:6b753f761943 | 41 | #include "memory_tests.h" |
JimCarver | 0:6b753f761943 | 42 | #endif |
JimCarver | 0:6b753f761943 | 43 | |
JimCarver | 0:6b753f761943 | 44 | #ifndef DEFAULT_FIRMWARE_PATH |
JimCarver | 0:6b753f761943 | 45 | #define DEFAULT_FIRMWARE_PATH "/sd/firmware" |
JimCarver | 0:6b753f761943 | 46 | #endif |
JimCarver | 0:6b753f761943 | 47 | |
JimCarver | 0:6b753f761943 | 48 | BlockDevice *arm_uc_blockdevice; |
JimCarver | 0:6b753f761943 | 49 | |
JimCarver | 0:6b753f761943 | 50 | SimpleMbedCloudClient::SimpleMbedCloudClient(NetworkInterface *net, BlockDevice *bd, FileSystem *fs) : |
JimCarver | 0:6b753f761943 | 51 | _registered(false), |
JimCarver | 0:6b753f761943 | 52 | _register_called(false), |
JimCarver | 0:6b753f761943 | 53 | _register_and_connect_called(false), |
JimCarver | 0:6b753f761943 | 54 | _registered_cb(NULL), |
JimCarver | 0:6b753f761943 | 55 | _unregistered_cb(NULL), |
JimCarver | 0:6b753f761943 | 56 | _net(net), |
JimCarver | 0:6b753f761943 | 57 | _bd(bd), |
JimCarver | 0:6b753f761943 | 58 | _fs(fs) |
JimCarver | 0:6b753f761943 | 59 | { |
JimCarver | 0:6b753f761943 | 60 | arm_uc_blockdevice = bd; |
JimCarver | 0:6b753f761943 | 61 | } |
JimCarver | 0:6b753f761943 | 62 | |
JimCarver | 0:6b753f761943 | 63 | SimpleMbedCloudClient::~SimpleMbedCloudClient() { |
JimCarver | 0:6b753f761943 | 64 | for (unsigned int i = 0; _resources.size(); i++) { |
JimCarver | 0:6b753f761943 | 65 | delete _resources[i]; |
JimCarver | 0:6b753f761943 | 66 | } |
JimCarver | 0:6b753f761943 | 67 | } |
JimCarver | 0:6b753f761943 | 68 | |
JimCarver | 0:6b753f761943 | 69 | int SimpleMbedCloudClient::init() { |
JimCarver | 0:6b753f761943 | 70 | // Requires DAPLink 245+ (https://github.com/ARMmbed/DAPLink/pull/364) |
JimCarver | 0:6b753f761943 | 71 | // Older versions: workaround to prevent possible deletion of credentials: |
JimCarver | 0:6b753f761943 | 72 | wait(1); |
JimCarver | 0:6b753f761943 | 73 | |
JimCarver | 0:6b753f761943 | 74 | extern const uint8_t arm_uc_vendor_id[]; |
JimCarver | 0:6b753f761943 | 75 | extern const uint16_t arm_uc_vendor_id_size; |
JimCarver | 0:6b753f761943 | 76 | extern const uint8_t arm_uc_class_id[]; |
JimCarver | 0:6b753f761943 | 77 | extern const uint16_t arm_uc_class_id_size; |
JimCarver | 0:6b753f761943 | 78 | |
JimCarver | 0:6b753f761943 | 79 | ARM_UC_SetVendorId(arm_uc_vendor_id, arm_uc_vendor_id_size); |
JimCarver | 0:6b753f761943 | 80 | ARM_UC_SetClassId(arm_uc_class_id, arm_uc_class_id_size); |
JimCarver | 0:6b753f761943 | 81 | |
JimCarver | 0:6b753f761943 | 82 | // Initialize the FCC |
JimCarver | 0:6b753f761943 | 83 | fcc_status_e fcc_status = fcc_init(); |
JimCarver | 0:6b753f761943 | 84 | if(fcc_status != FCC_STATUS_SUCCESS) { |
JimCarver | 0:6b753f761943 | 85 | printf("[Simple Cloud Client] Factory Client Configuration failed with status %d. \n", fcc_status); |
JimCarver | 0:6b753f761943 | 86 | return 1; |
JimCarver | 0:6b753f761943 | 87 | } |
JimCarver | 0:6b753f761943 | 88 | |
JimCarver | 0:6b753f761943 | 89 | fcc_status = fcc_verify_device_configured_4mbed_cloud(); |
JimCarver | 0:6b753f761943 | 90 | |
JimCarver | 0:6b753f761943 | 91 | if (fcc_status == FCC_STATUS_KCM_STORAGE_ERROR) { |
JimCarver | 0:6b753f761943 | 92 | int mount_result = mount_storage(); |
JimCarver | 0:6b753f761943 | 93 | if (mount_result != 0) { |
JimCarver | 0:6b753f761943 | 94 | printf("[Simple Cloud Client] Failed to mount file system with status %d. \n", mount_result); |
JimCarver | 0:6b753f761943 | 95 | #if !defined(MBED_CONF_APP_FORMAT_STORAGE_LAYER_ON_ERROR) || MBED_CONF_APP_FORMAT_STORAGE_LAYER_ON_ERROR == 0 |
JimCarver | 0:6b753f761943 | 96 | return 1; |
JimCarver | 0:6b753f761943 | 97 | #endif |
JimCarver | 0:6b753f761943 | 98 | } else { |
JimCarver | 0:6b753f761943 | 99 | // Retry with mounted filesystem. |
JimCarver | 0:6b753f761943 | 100 | fcc_status = fcc_verify_device_configured_4mbed_cloud(); |
JimCarver | 0:6b753f761943 | 101 | } |
JimCarver | 0:6b753f761943 | 102 | } |
JimCarver | 0:6b753f761943 | 103 | |
JimCarver | 0:6b753f761943 | 104 | // This is designed to simplify user-experience by auto-formatting the |
JimCarver | 0:6b753f761943 | 105 | // primary storage if no valid certificates exist. |
JimCarver | 0:6b753f761943 | 106 | // This should never be used for any kind of production devices. |
JimCarver | 0:6b753f761943 | 107 | #if defined(MBED_CONF_APP_FORMAT_STORAGE_LAYER_ON_ERROR) && MBED_CONF_APP_FORMAT_STORAGE_LAYER_ON_ERROR == 1 |
JimCarver | 0:6b753f761943 | 108 | if (fcc_status != FCC_STATUS_SUCCESS) { |
JimCarver | 0:6b753f761943 | 109 | if (reformat_storage() != 0) { |
JimCarver | 0:6b753f761943 | 110 | return 1; |
JimCarver | 0:6b753f761943 | 111 | } |
JimCarver | 0:6b753f761943 | 112 | |
JimCarver | 0:6b753f761943 | 113 | reset_storage(); |
JimCarver | 0:6b753f761943 | 114 | } |
JimCarver | 0:6b753f761943 | 115 | #else |
JimCarver | 0:6b753f761943 | 116 | if (fcc_status != FCC_STATUS_SUCCESS) { |
JimCarver | 0:6b753f761943 | 117 | printf("[Simple Cloud Client] Device not configured for mbed Cloud - try re-formatting your storage device or set MBED_CONF_APP_FORMAT_STORAGE_LAYER_ON_ERROR to 1\n"); |
JimCarver | 0:6b753f761943 | 118 | return 1; |
JimCarver | 0:6b753f761943 | 119 | } |
JimCarver | 0:6b753f761943 | 120 | #endif |
JimCarver | 0:6b753f761943 | 121 | |
JimCarver | 0:6b753f761943 | 122 | // Resets storage to an empty state. |
JimCarver | 0:6b753f761943 | 123 | // Use this function when you want to clear storage from all the factory-tool generated data and user data. |
JimCarver | 0:6b753f761943 | 124 | // After this operation device must be injected again by using factory tool or developer certificate. |
JimCarver | 0:6b753f761943 | 125 | #ifdef RESET_STORAGE |
JimCarver | 0:6b753f761943 | 126 | reset_storage(); |
JimCarver | 0:6b753f761943 | 127 | #endif |
JimCarver | 0:6b753f761943 | 128 | |
JimCarver | 0:6b753f761943 | 129 | // Deletes existing firmware images from storage. |
JimCarver | 0:6b753f761943 | 130 | // This deletes any existing firmware images during application startup. |
JimCarver | 0:6b753f761943 | 131 | // This compilation flag is currently implemented only for mbed OS. |
JimCarver | 0:6b753f761943 | 132 | #ifdef RESET_FIRMWARE |
JimCarver | 0:6b753f761943 | 133 | palStatus_t status = PAL_SUCCESS; |
JimCarver | 0:6b753f761943 | 134 | status = pal_fsRmFiles(DEFAULT_FIRMWARE_PATH); |
JimCarver | 0:6b753f761943 | 135 | if(status == PAL_SUCCESS) { |
JimCarver | 0:6b753f761943 | 136 | printf("[Simple Cloud Client] Firmware storage erased.\n"); |
JimCarver | 0:6b753f761943 | 137 | } else if (status == PAL_ERR_FS_NO_PATH) { |
JimCarver | 0:6b753f761943 | 138 | printf("[Simple Cloud Client] Firmware path not found/does not exist.\n"); |
JimCarver | 0:6b753f761943 | 139 | } else { |
JimCarver | 0:6b753f761943 | 140 | printf("[Simple Cloud Client] Firmware storage erasing failed with %" PRId32, status); |
JimCarver | 0:6b753f761943 | 141 | return 1; |
JimCarver | 0:6b753f761943 | 142 | } |
JimCarver | 0:6b753f761943 | 143 | #endif |
JimCarver | 0:6b753f761943 | 144 | |
JimCarver | 0:6b753f761943 | 145 | #if MBED_CONF_APP_DEVELOPER_MODE == 1 |
JimCarver | 0:6b753f761943 | 146 | printf("[Simple Cloud Client] Starting developer flow\n"); |
JimCarver | 0:6b753f761943 | 147 | fcc_status = fcc_developer_flow(); |
JimCarver | 0:6b753f761943 | 148 | if (fcc_status == FCC_STATUS_KCM_FILE_EXIST_ERROR) { |
JimCarver | 0:6b753f761943 | 149 | printf("[Simple Cloud Client] Developer credentials already exist\n"); |
JimCarver | 0:6b753f761943 | 150 | } else if (fcc_status != FCC_STATUS_SUCCESS) { |
JimCarver | 0:6b753f761943 | 151 | printf("[Simple Cloud Client] Failed to load developer credentials - is the storage device active and accessible?\n"); |
JimCarver | 0:6b753f761943 | 152 | return 1; |
JimCarver | 0:6b753f761943 | 153 | } |
JimCarver | 0:6b753f761943 | 154 | #endif |
JimCarver | 0:6b753f761943 | 155 | |
JimCarver | 0:6b753f761943 | 156 | return 0; |
JimCarver | 0:6b753f761943 | 157 | } |
JimCarver | 0:6b753f761943 | 158 | |
JimCarver | 0:6b753f761943 | 159 | bool SimpleMbedCloudClient::call_register() { |
JimCarver | 0:6b753f761943 | 160 | // need to unregister first before calling this function again |
JimCarver | 0:6b753f761943 | 161 | if (_register_called) return false; |
JimCarver | 0:6b753f761943 | 162 | |
JimCarver | 0:6b753f761943 | 163 | _cloud_client.on_registered(this, &SimpleMbedCloudClient::client_registered); |
JimCarver | 0:6b753f761943 | 164 | _cloud_client.on_unregistered(this, &SimpleMbedCloudClient::client_unregistered); |
JimCarver | 0:6b753f761943 | 165 | _cloud_client.on_error(this, &SimpleMbedCloudClient::error); |
JimCarver | 0:6b753f761943 | 166 | |
JimCarver | 0:6b753f761943 | 167 | bool setup = _cloud_client.setup(_net); |
JimCarver | 0:6b753f761943 | 168 | _register_called = true; |
JimCarver | 0:6b753f761943 | 169 | if (!setup) { |
JimCarver | 0:6b753f761943 | 170 | printf("[Simple Cloud Client] Client setup failed\n"); |
JimCarver | 0:6b753f761943 | 171 | return false; |
JimCarver | 0:6b753f761943 | 172 | } |
JimCarver | 0:6b753f761943 | 173 | |
JimCarver | 0:6b753f761943 | 174 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
JimCarver | 0:6b753f761943 | 175 | /* Set callback functions for authorizing updates and monitoring progress. |
JimCarver | 0:6b753f761943 | 176 | Code is implemented in update_ui_example.cpp |
JimCarver | 0:6b753f761943 | 177 | Both callbacks are completely optional. If no authorization callback |
JimCarver | 0:6b753f761943 | 178 | is set, the update process will procede immediately in each step. |
JimCarver | 0:6b753f761943 | 179 | */ |
JimCarver | 0:6b753f761943 | 180 | update_ui_set_cloud_client(&_cloud_client); |
JimCarver | 0:6b753f761943 | 181 | _cloud_client.set_update_authorize_handler(update_authorize); |
JimCarver | 0:6b753f761943 | 182 | _cloud_client.set_update_progress_handler(update_progress); |
JimCarver | 0:6b753f761943 | 183 | #endif |
JimCarver | 0:6b753f761943 | 184 | return true; |
JimCarver | 0:6b753f761943 | 185 | } |
JimCarver | 0:6b753f761943 | 186 | |
JimCarver | 0:6b753f761943 | 187 | void SimpleMbedCloudClient::close() { |
JimCarver | 0:6b753f761943 | 188 | _cloud_client.close(); |
JimCarver | 0:6b753f761943 | 189 | } |
JimCarver | 0:6b753f761943 | 190 | |
JimCarver | 0:6b753f761943 | 191 | void SimpleMbedCloudClient::register_update() { |
JimCarver | 0:6b753f761943 | 192 | _cloud_client.register_update(); |
JimCarver | 0:6b753f761943 | 193 | } |
JimCarver | 0:6b753f761943 | 194 | |
JimCarver | 0:6b753f761943 | 195 | void SimpleMbedCloudClient::client_registered() { |
JimCarver | 0:6b753f761943 | 196 | _registered = true; |
JimCarver | 0:6b753f761943 | 197 | static const ConnectorClientEndpointInfo* endpoint = NULL; |
JimCarver | 0:6b753f761943 | 198 | if (endpoint == NULL) { |
JimCarver | 0:6b753f761943 | 199 | endpoint = _cloud_client.endpoint_info(); |
JimCarver | 0:6b753f761943 | 200 | if (endpoint && _registered_cb) { |
JimCarver | 0:6b753f761943 | 201 | _registered_cb(endpoint); |
JimCarver | 0:6b753f761943 | 202 | } |
JimCarver | 0:6b753f761943 | 203 | } |
JimCarver | 0:6b753f761943 | 204 | #ifdef MBED_HEAP_STATS_ENABLED |
JimCarver | 0:6b753f761943 | 205 | heap_stats(); |
JimCarver | 0:6b753f761943 | 206 | #endif |
JimCarver | 0:6b753f761943 | 207 | } |
JimCarver | 0:6b753f761943 | 208 | |
JimCarver | 0:6b753f761943 | 209 | void SimpleMbedCloudClient::client_unregistered() { |
JimCarver | 0:6b753f761943 | 210 | _registered = false; |
JimCarver | 0:6b753f761943 | 211 | _register_called = false; |
JimCarver | 0:6b753f761943 | 212 | |
JimCarver | 0:6b753f761943 | 213 | if (_unregistered_cb) { |
JimCarver | 0:6b753f761943 | 214 | _unregistered_cb(); |
JimCarver | 0:6b753f761943 | 215 | } |
JimCarver | 0:6b753f761943 | 216 | |
JimCarver | 0:6b753f761943 | 217 | #ifdef MBED_HEAP_STATS_ENABLED |
JimCarver | 0:6b753f761943 | 218 | heap_stats(); |
JimCarver | 0:6b753f761943 | 219 | #endif |
JimCarver | 0:6b753f761943 | 220 | } |
JimCarver | 0:6b753f761943 | 221 | |
JimCarver | 0:6b753f761943 | 222 | void SimpleMbedCloudClient::error(int error_code) { |
JimCarver | 0:6b753f761943 | 223 | const char *error; |
JimCarver | 0:6b753f761943 | 224 | switch(error_code) { |
JimCarver | 0:6b753f761943 | 225 | case MbedCloudClient::ConnectErrorNone: |
JimCarver | 0:6b753f761943 | 226 | error = "MbedCloudClient::ConnectErrorNone"; |
JimCarver | 0:6b753f761943 | 227 | break; |
JimCarver | 0:6b753f761943 | 228 | case MbedCloudClient::ConnectAlreadyExists: |
JimCarver | 0:6b753f761943 | 229 | error = "MbedCloudClient::ConnectAlreadyExists"; |
JimCarver | 0:6b753f761943 | 230 | break; |
JimCarver | 0:6b753f761943 | 231 | case MbedCloudClient::ConnectBootstrapFailed: |
JimCarver | 0:6b753f761943 | 232 | error = "MbedCloudClient::ConnectBootstrapFailed"; |
JimCarver | 0:6b753f761943 | 233 | break; |
JimCarver | 0:6b753f761943 | 234 | case MbedCloudClient::ConnectInvalidParameters: |
JimCarver | 0:6b753f761943 | 235 | error = "MbedCloudClient::ConnectInvalidParameters"; |
JimCarver | 0:6b753f761943 | 236 | break; |
JimCarver | 0:6b753f761943 | 237 | case MbedCloudClient::ConnectNotRegistered: |
JimCarver | 0:6b753f761943 | 238 | error = "MbedCloudClient::ConnectNotRegistered"; |
JimCarver | 0:6b753f761943 | 239 | break; |
JimCarver | 0:6b753f761943 | 240 | case MbedCloudClient::ConnectTimeout: |
JimCarver | 0:6b753f761943 | 241 | error = "MbedCloudClient::ConnectTimeout"; |
JimCarver | 0:6b753f761943 | 242 | break; |
JimCarver | 0:6b753f761943 | 243 | case MbedCloudClient::ConnectNetworkError: |
JimCarver | 0:6b753f761943 | 244 | error = "MbedCloudClient::ConnectNetworkError"; |
JimCarver | 0:6b753f761943 | 245 | break; |
JimCarver | 0:6b753f761943 | 246 | case MbedCloudClient::ConnectResponseParseFailed: |
JimCarver | 0:6b753f761943 | 247 | error = "MbedCloudClient::ConnectResponseParseFailed"; |
JimCarver | 0:6b753f761943 | 248 | break; |
JimCarver | 0:6b753f761943 | 249 | case MbedCloudClient::ConnectUnknownError: |
JimCarver | 0:6b753f761943 | 250 | error = "MbedCloudClient::ConnectUnknownError"; |
JimCarver | 0:6b753f761943 | 251 | break; |
JimCarver | 0:6b753f761943 | 252 | case MbedCloudClient::ConnectMemoryConnectFail: |
JimCarver | 0:6b753f761943 | 253 | error = "MbedCloudClient::ConnectMemoryConnectFail"; |
JimCarver | 0:6b753f761943 | 254 | break; |
JimCarver | 0:6b753f761943 | 255 | case MbedCloudClient::ConnectNotAllowed: |
JimCarver | 0:6b753f761943 | 256 | error = "MbedCloudClient::ConnectNotAllowed"; |
JimCarver | 0:6b753f761943 | 257 | break; |
JimCarver | 0:6b753f761943 | 258 | case MbedCloudClient::ConnectSecureConnectionFailed: |
JimCarver | 0:6b753f761943 | 259 | error = "MbedCloudClient::ConnectSecureConnectionFailed"; |
JimCarver | 0:6b753f761943 | 260 | break; |
JimCarver | 0:6b753f761943 | 261 | case MbedCloudClient::ConnectDnsResolvingFailed: |
JimCarver | 0:6b753f761943 | 262 | error = "MbedCloudClient::ConnectDnsResolvingFailed"; |
JimCarver | 0:6b753f761943 | 263 | break; |
JimCarver | 0:6b753f761943 | 264 | #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE |
JimCarver | 0:6b753f761943 | 265 | case MbedCloudClient::UpdateWarningCertificateNotFound: |
JimCarver | 0:6b753f761943 | 266 | error = "MbedCloudClient::UpdateWarningCertificateNotFound"; |
JimCarver | 0:6b753f761943 | 267 | break; |
JimCarver | 0:6b753f761943 | 268 | case MbedCloudClient::UpdateWarningIdentityNotFound: |
JimCarver | 0:6b753f761943 | 269 | error = "MbedCloudClient::UpdateWarningIdentityNotFound"; |
JimCarver | 0:6b753f761943 | 270 | break; |
JimCarver | 0:6b753f761943 | 271 | case MbedCloudClient::UpdateWarningCertificateInvalid: |
JimCarver | 0:6b753f761943 | 272 | error = "MbedCloudClient::UpdateWarningCertificateInvalid"; |
JimCarver | 0:6b753f761943 | 273 | break; |
JimCarver | 0:6b753f761943 | 274 | case MbedCloudClient::UpdateWarningSignatureInvalid: |
JimCarver | 0:6b753f761943 | 275 | error = "MbedCloudClient::UpdateWarningSignatureInvalid"; |
JimCarver | 0:6b753f761943 | 276 | break; |
JimCarver | 0:6b753f761943 | 277 | case MbedCloudClient::UpdateWarningVendorMismatch: |
JimCarver | 0:6b753f761943 | 278 | error = "MbedCloudClient::UpdateWarningVendorMismatch"; |
JimCarver | 0:6b753f761943 | 279 | break; |
JimCarver | 0:6b753f761943 | 280 | case MbedCloudClient::UpdateWarningClassMismatch: |
JimCarver | 0:6b753f761943 | 281 | error = "MbedCloudClient::UpdateWarningClassMismatch"; |
JimCarver | 0:6b753f761943 | 282 | break; |
JimCarver | 0:6b753f761943 | 283 | case MbedCloudClient::UpdateWarningDeviceMismatch: |
JimCarver | 0:6b753f761943 | 284 | error = "MbedCloudClient::UpdateWarningDeviceMismatch"; |
JimCarver | 0:6b753f761943 | 285 | break; |
JimCarver | 0:6b753f761943 | 286 | case MbedCloudClient::UpdateWarningURINotFound: |
JimCarver | 0:6b753f761943 | 287 | error = "MbedCloudClient::UpdateWarningURINotFound"; |
JimCarver | 0:6b753f761943 | 288 | break; |
JimCarver | 0:6b753f761943 | 289 | case MbedCloudClient::UpdateWarningRollbackProtection: |
JimCarver | 0:6b753f761943 | 290 | error = "MbedCloudClient::UpdateWarningRollbackProtection"; |
JimCarver | 0:6b753f761943 | 291 | break; |
JimCarver | 0:6b753f761943 | 292 | case MbedCloudClient::UpdateWarningUnknown: |
JimCarver | 0:6b753f761943 | 293 | error = "MbedCloudClient::UpdateWarningUnknown"; |
JimCarver | 0:6b753f761943 | 294 | break; |
JimCarver | 0:6b753f761943 | 295 | case MbedCloudClient::UpdateErrorWriteToStorage: |
JimCarver | 0:6b753f761943 | 296 | error = "MbedCloudClient::UpdateErrorWriteToStorage"; |
JimCarver | 0:6b753f761943 | 297 | break; |
JimCarver | 0:6b753f761943 | 298 | case MbedCloudClient::UpdateErrorInvalidHash: |
JimCarver | 0:6b753f761943 | 299 | error = "MbedCloudClient::UpdateErrorInvalidHash"; |
JimCarver | 0:6b753f761943 | 300 | break; |
JimCarver | 0:6b753f761943 | 301 | #endif |
JimCarver | 0:6b753f761943 | 302 | default: |
JimCarver | 0:6b753f761943 | 303 | error = "UNKNOWN"; |
JimCarver | 0:6b753f761943 | 304 | } |
JimCarver | 0:6b753f761943 | 305 | |
JimCarver | 0:6b753f761943 | 306 | // @todo: move this into user space |
JimCarver | 0:6b753f761943 | 307 | printf("\n[Simple Cloud Client] Error occurred : %s\n", error); |
JimCarver | 0:6b753f761943 | 308 | printf("[Simple Cloud Client] Error code : %d\n", error_code); |
JimCarver | 0:6b753f761943 | 309 | printf("[Simple Cloud Client] Error details : %s\n",_cloud_client.error_description()); |
JimCarver | 0:6b753f761943 | 310 | } |
JimCarver | 0:6b753f761943 | 311 | |
JimCarver | 0:6b753f761943 | 312 | bool SimpleMbedCloudClient::is_client_registered() { |
JimCarver | 0:6b753f761943 | 313 | return _registered; |
JimCarver | 0:6b753f761943 | 314 | } |
JimCarver | 0:6b753f761943 | 315 | |
JimCarver | 0:6b753f761943 | 316 | bool SimpleMbedCloudClient::is_register_called() { |
JimCarver | 0:6b753f761943 | 317 | return _register_called; |
JimCarver | 0:6b753f761943 | 318 | } |
JimCarver | 0:6b753f761943 | 319 | |
JimCarver | 0:6b753f761943 | 320 | bool SimpleMbedCloudClient::register_and_connect() { |
JimCarver | 0:6b753f761943 | 321 | if (_register_and_connect_called) return false; |
JimCarver | 0:6b753f761943 | 322 | |
JimCarver | 0:6b753f761943 | 323 | mcc_resource_def resourceDef; |
JimCarver | 0:6b753f761943 | 324 | |
JimCarver | 0:6b753f761943 | 325 | for (int i = 0; i < _resources.size(); i++) { |
JimCarver | 0:6b753f761943 | 326 | _resources[i]->get_data(&resourceDef); |
JimCarver | 0:6b753f761943 | 327 | M2MResource *res = add_resource(&_obj_list, resourceDef.object_id, resourceDef.instance_id, |
JimCarver | 0:6b753f761943 | 328 | resourceDef.resource_id, resourceDef.name.c_str(), M2MResourceInstance::STRING, |
JimCarver | 0:6b753f761943 | 329 | (M2MBase::Operation)resourceDef.method_mask, resourceDef.value.c_str(), resourceDef.observable, |
JimCarver | 0:6b753f761943 | 330 | resourceDef.put_callback, resourceDef.post_callback, resourceDef.notification_callback); |
JimCarver | 0:6b753f761943 | 331 | _resources[i]->set_m2m_resource(res); |
JimCarver | 0:6b753f761943 | 332 | } |
JimCarver | 0:6b753f761943 | 333 | _cloud_client.add_objects(_obj_list); |
JimCarver | 0:6b753f761943 | 334 | |
JimCarver | 0:6b753f761943 | 335 | _register_and_connect_called = true; |
JimCarver | 0:6b753f761943 | 336 | |
JimCarver | 0:6b753f761943 | 337 | // Start registering to the cloud. |
JimCarver | 0:6b753f761943 | 338 | bool retval = call_register(); |
JimCarver | 0:6b753f761943 | 339 | |
JimCarver | 0:6b753f761943 | 340 | // Print memory statistics if the MBED_HEAP_STATS_ENABLED is defined. |
JimCarver | 0:6b753f761943 | 341 | #ifdef MBED_HEAP_STATS_ENABLED |
JimCarver | 0:6b753f761943 | 342 | printf("[Simple Cloud Client] Register being called\r\n"); |
JimCarver | 0:6b753f761943 | 343 | heap_stats(); |
JimCarver | 0:6b753f761943 | 344 | #endif |
JimCarver | 0:6b753f761943 | 345 | |
JimCarver | 0:6b753f761943 | 346 | return retval; |
JimCarver | 0:6b753f761943 | 347 | } |
JimCarver | 0:6b753f761943 | 348 | |
JimCarver | 0:6b753f761943 | 349 | void SimpleMbedCloudClient::on_registered(Callback<void(const ConnectorClientEndpointInfo*)> cb) { |
JimCarver | 0:6b753f761943 | 350 | _registered_cb = cb; |
JimCarver | 0:6b753f761943 | 351 | } |
JimCarver | 0:6b753f761943 | 352 | |
JimCarver | 0:6b753f761943 | 353 | void SimpleMbedCloudClient::on_unregistered(Callback<void()> cb) { |
JimCarver | 0:6b753f761943 | 354 | _unregistered_cb = cb; |
JimCarver | 0:6b753f761943 | 355 | } |
JimCarver | 0:6b753f761943 | 356 | |
JimCarver | 0:6b753f761943 | 357 | MbedCloudClient& SimpleMbedCloudClient::get_cloud_client() { |
JimCarver | 0:6b753f761943 | 358 | return _cloud_client; |
JimCarver | 0:6b753f761943 | 359 | } |
JimCarver | 0:6b753f761943 | 360 | |
JimCarver | 0:6b753f761943 | 361 | MbedCloudClientResource* SimpleMbedCloudClient::create_resource(const char *path, const char *name) { |
JimCarver | 0:6b753f761943 | 362 | MbedCloudClientResource *resource = new MbedCloudClientResource(this, path, name); |
JimCarver | 0:6b753f761943 | 363 | _resources.push_back(resource); |
JimCarver | 0:6b753f761943 | 364 | return resource; |
JimCarver | 0:6b753f761943 | 365 | } |
JimCarver | 0:6b753f761943 | 366 | |
JimCarver | 0:6b753f761943 | 367 | int SimpleMbedCloudClient::reformat_storage() |
JimCarver | 0:6b753f761943 | 368 | { |
JimCarver | 0:6b753f761943 | 369 | int reformat_result = -1; |
JimCarver | 0:6b753f761943 | 370 | printf("[Simple Cloud Client] Autoformatting the storage.\n"); |
JimCarver | 0:6b753f761943 | 371 | if (_bd) { |
JimCarver | 0:6b753f761943 | 372 | reformat_result = _fs->reformat(_bd); |
JimCarver | 0:6b753f761943 | 373 | if (reformat_result != 0) { |
JimCarver | 0:6b753f761943 | 374 | printf("[Simple Cloud Client] Autoformatting failed with error %d\n", reformat_result); |
JimCarver | 0:6b753f761943 | 375 | } |
JimCarver | 0:6b753f761943 | 376 | } |
JimCarver | 0:6b753f761943 | 377 | return reformat_result; |
JimCarver | 0:6b753f761943 | 378 | } |
JimCarver | 0:6b753f761943 | 379 | |
JimCarver | 0:6b753f761943 | 380 | void SimpleMbedCloudClient::reset_storage() |
JimCarver | 0:6b753f761943 | 381 | { |
JimCarver | 0:6b753f761943 | 382 | printf("[Simple Cloud Client] Reset storage to an empty state.\n"); |
JimCarver | 0:6b753f761943 | 383 | fcc_status_e delete_status = fcc_storage_delete(); |
JimCarver | 0:6b753f761943 | 384 | if (delete_status != FCC_STATUS_SUCCESS) { |
JimCarver | 0:6b753f761943 | 385 | printf("[Simple Cloud Client] Failed to delete storage - %d\n", delete_status); |
JimCarver | 0:6b753f761943 | 386 | } |
JimCarver | 0:6b753f761943 | 387 | } |
JimCarver | 0:6b753f761943 | 388 | |
JimCarver | 0:6b753f761943 | 389 | int SimpleMbedCloudClient::mount_storage() |
JimCarver | 0:6b753f761943 | 390 | { |
JimCarver | 0:6b753f761943 | 391 | int mount_result = -1; |
JimCarver | 0:6b753f761943 | 392 | printf("[Simple Cloud Client] Initializing storage.\n"); |
JimCarver | 0:6b753f761943 | 393 | if (_bd) { |
JimCarver | 0:6b753f761943 | 394 | mount_result = _fs->mount(_bd); |
JimCarver | 0:6b753f761943 | 395 | } |
JimCarver | 0:6b753f761943 | 396 | return mount_result; |
JimCarver | 0:6b753f761943 | 397 | } |