Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:119624335925 1 // ----------------------------------------------------------------------------
MACRUM 0:119624335925 2 // Copyright 2016-2017 ARM Ltd.
MACRUM 0:119624335925 3 //
MACRUM 0:119624335925 4 // SPDX-License-Identifier: Apache-2.0
MACRUM 0:119624335925 5 //
MACRUM 0:119624335925 6 // Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:119624335925 7 // you may not use this file except in compliance with the License.
MACRUM 0:119624335925 8 // You may obtain a copy of the License at
MACRUM 0:119624335925 9 //
MACRUM 0:119624335925 10 // http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:119624335925 11 //
MACRUM 0:119624335925 12 // Unless required by applicable law or agreed to in writing, software
MACRUM 0:119624335925 13 // distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:119624335925 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:119624335925 15 // See the License for the specific language governing permissions and
MACRUM 0:119624335925 16 // limitations under the License.
MACRUM 0:119624335925 17 // ----------------------------------------------------------------------------
MACRUM 0:119624335925 18
MACRUM 0:119624335925 19 #include <stdio.h>
MACRUM 0:119624335925 20 #include "simple-mbed-cloud-client.h"
MACRUM 0:119624335925 21 #include "mbed-cloud-client/MbedCloudClient.h"
MACRUM 0:119624335925 22 #include "m2mdevice.h"
MACRUM 0:119624335925 23 #include "m2mresource.h"
MACRUM 0:119624335925 24 #include "mbed-client/m2minterface.h"
MACRUM 0:119624335925 25 #include "key_config_manager.h"
MACRUM 0:119624335925 26 #include "resource.h"
MACRUM 0:119624335925 27 #include "mbed-client/m2mvector.h"
MACRUM 0:119624335925 28 #include "mbed_cloud_client_resource.h"
MACRUM 0:119624335925 29 #include "factory_configurator_client.h"
MACRUM 0:119624335925 30
MACRUM 0:119624335925 31 #ifdef MBED_CLOUD_CLIENT_USER_CONFIG_FILE
MACRUM 0:119624335925 32 #include MBED_CLOUD_CLIENT_USER_CONFIG_FILE
MACRUM 0:119624335925 33 #endif
MACRUM 0:119624335925 34
MACRUM 0:119624335925 35 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
MACRUM 0:119624335925 36 #include "update_ui_example.h"
MACRUM 0:119624335925 37 #endif
MACRUM 0:119624335925 38
MACRUM 0:119624335925 39 #ifdef MBED_HEAP_STATS_ENABLED
MACRUM 0:119624335925 40 #include "memory_tests.h"
MACRUM 0:119624335925 41 #endif
MACRUM 0:119624335925 42
MACRUM 0:119624335925 43 #define DEFAULT_FIRMWARE_PATH "/sd/firmware"
MACRUM 0:119624335925 44
MACRUM 0:119624335925 45 SimpleMbedCloudClient::SimpleMbedCloudClient(NetworkInterface *net) :
MACRUM 0:119624335925 46 _registered(false),
MACRUM 0:119624335925 47 _register_called(false),
MACRUM 0:119624335925 48 net(net) {
MACRUM 0:119624335925 49 }
MACRUM 0:119624335925 50
MACRUM 0:119624335925 51 SimpleMbedCloudClient::~SimpleMbedCloudClient() {
MACRUM 0:119624335925 52 for (unsigned int i = 0; _resources.size(); i++) {
MACRUM 0:119624335925 53 delete _resources[i];
MACRUM 0:119624335925 54 }
MACRUM 0:119624335925 55 }
MACRUM 0:119624335925 56
MACRUM 0:119624335925 57 int SimpleMbedCloudClient::init() {
MACRUM 0:119624335925 58 // Initialize the FCC
MACRUM 0:119624335925 59 fcc_status_e fcc_status = fcc_init();
MACRUM 0:119624335925 60 if(fcc_status != FCC_STATUS_SUCCESS) {
MACRUM 0:119624335925 61 printf("fcc_init failed with status %d! - exit\n", fcc_status);
MACRUM 0:119624335925 62 return 1;
MACRUM 0:119624335925 63 }
MACRUM 0:119624335925 64
MACRUM 0:119624335925 65 // Resets storage to an empty state.
MACRUM 0:119624335925 66 // Use this function when you want to clear storage from all the factory-tool generated data and user data.
MACRUM 0:119624335925 67 // After this operation device must be injected again by using factory tool or developer certificate.
MACRUM 0:119624335925 68 #ifdef RESET_STORAGE
MACRUM 0:119624335925 69 printf("Reset storage to an empty state.\n");
MACRUM 0:119624335925 70 fcc_status_e delete_status = fcc_storage_delete();
MACRUM 0:119624335925 71 if (delete_status != FCC_STATUS_SUCCESS) {
MACRUM 0:119624335925 72 printf("Failed to delete storage - %d\n", delete_status);
MACRUM 0:119624335925 73 }
MACRUM 0:119624335925 74 #endif
MACRUM 0:119624335925 75
MACRUM 0:119624335925 76 // Deletes existing firmware images from storage.
MACRUM 0:119624335925 77 // This deletes any existing firmware images during application startup.
MACRUM 0:119624335925 78 // This compilation flag is currently implemented only for mbed OS.
MACRUM 0:119624335925 79 #ifdef RESET_FIRMWARE
MACRUM 0:119624335925 80 palStatus_t status = PAL_SUCCESS;
MACRUM 0:119624335925 81 status = pal_fsRmFiles(DEFAULT_FIRMWARE_PATH);
MACRUM 0:119624335925 82 if(status == PAL_SUCCESS) {
MACRUM 0:119624335925 83 printf("Firmware storage erased.\n");
MACRUM 0:119624335925 84 } else if (status == PAL_ERR_FS_NO_PATH) {
MACRUM 0:119624335925 85 printf("Firmware path not found/does not exist.\n");
MACRUM 0:119624335925 86 } else {
MACRUM 0:119624335925 87 printf("Firmware storage erasing failed with %" PRId32, status);
MACRUM 0:119624335925 88 return 1;
MACRUM 0:119624335925 89 }
MACRUM 0:119624335925 90 #endif
MACRUM 0:119624335925 91
MACRUM 0:119624335925 92 #if MBED_CONF_APP_DEVELOPER_MODE == 1
MACRUM 0:119624335925 93 printf("Start developer flow\n");
MACRUM 0:119624335925 94 fcc_status = fcc_developer_flow();
MACRUM 0:119624335925 95 if (fcc_status == FCC_STATUS_KCM_FILE_EXIST_ERROR) {
MACRUM 0:119624335925 96 printf("Developer credentials already exist\n");
MACRUM 0:119624335925 97 } else if (fcc_status != FCC_STATUS_SUCCESS) {
MACRUM 0:119624335925 98 printf("Failed to load developer credentials - exit\n");
MACRUM 0:119624335925 99 return 1;
MACRUM 0:119624335925 100 }
MACRUM 0:119624335925 101 #endif
MACRUM 0:119624335925 102 fcc_status = fcc_verify_device_configured_4mbed_cloud();
MACRUM 0:119624335925 103 if (fcc_status != FCC_STATUS_SUCCESS) {
MACRUM 0:119624335925 104 printf("Device not configured for mbed Cloud - exit\n");
MACRUM 0:119624335925 105 return 1;
MACRUM 0:119624335925 106 }
MACRUM 0:119624335925 107 }
MACRUM 0:119624335925 108
MACRUM 0:119624335925 109 bool SimpleMbedCloudClient::call_register() {
MACRUM 0:119624335925 110
MACRUM 0:119624335925 111 _cloud_client.on_registered(this, &SimpleMbedCloudClient::client_registered);
MACRUM 0:119624335925 112 _cloud_client.on_unregistered(this, &SimpleMbedCloudClient::client_unregistered);
MACRUM 0:119624335925 113 _cloud_client.on_error(this, &SimpleMbedCloudClient::error);
MACRUM 0:119624335925 114
MACRUM 0:119624335925 115 printf("Connecting...\n");
MACRUM 0:119624335925 116 bool setup = _cloud_client.setup(net);
MACRUM 0:119624335925 117 _register_called = true;
MACRUM 0:119624335925 118 if (!setup) {
MACRUM 0:119624335925 119 printf("Client setup failed\n");
MACRUM 0:119624335925 120 return false;
MACRUM 0:119624335925 121 }
MACRUM 0:119624335925 122
MACRUM 0:119624335925 123 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
MACRUM 0:119624335925 124 /* Set callback functions for authorizing updates and monitoring progress.
MACRUM 0:119624335925 125 Code is implemented in update_ui_example.cpp
MACRUM 0:119624335925 126 Both callbacks are completely optional. If no authorization callback
MACRUM 0:119624335925 127 is set, the update process will procede immediately in each step.
MACRUM 0:119624335925 128 */
MACRUM 0:119624335925 129 update_ui_set_cloud_client(&_cloud_client);
MACRUM 0:119624335925 130 _cloud_client.set_update_authorize_handler(update_authorize);
MACRUM 0:119624335925 131 _cloud_client.set_update_progress_handler(update_progress);
MACRUM 0:119624335925 132 #endif
MACRUM 0:119624335925 133 return true;
MACRUM 0:119624335925 134 }
MACRUM 0:119624335925 135
MACRUM 0:119624335925 136 void SimpleMbedCloudClient::close() {
MACRUM 0:119624335925 137 _cloud_client.close();
MACRUM 0:119624335925 138 }
MACRUM 0:119624335925 139
MACRUM 0:119624335925 140 void SimpleMbedCloudClient::register_update() {
MACRUM 0:119624335925 141 _cloud_client.register_update();
MACRUM 0:119624335925 142 }
MACRUM 0:119624335925 143
MACRUM 0:119624335925 144 void SimpleMbedCloudClient::client_registered() {
MACRUM 0:119624335925 145 _registered = true;
MACRUM 0:119624335925 146 printf("\nClient registered\n\n");
MACRUM 0:119624335925 147 static const ConnectorClientEndpointInfo* endpoint = NULL;
MACRUM 0:119624335925 148 if (endpoint == NULL) {
MACRUM 0:119624335925 149 endpoint = _cloud_client.endpoint_info();
MACRUM 0:119624335925 150 if (endpoint) {
MACRUM 0:119624335925 151
MACRUM 0:119624335925 152 #if MBED_CONF_APP_DEVELOPER_MODE == 1
MACRUM 0:119624335925 153 printf("Endpoint Name: %s\r\n", endpoint->internal_endpoint_name.c_str());
MACRUM 0:119624335925 154 #else
MACRUM 0:119624335925 155 printf("Endpoint Name: %s\r\n", endpoint->endpoint_name.c_str());
MACRUM 0:119624335925 156 #endif
MACRUM 0:119624335925 157 printf("Device Id: %s\r\n", endpoint->internal_endpoint_name.c_str());
MACRUM 0:119624335925 158 }
MACRUM 0:119624335925 159 }
MACRUM 0:119624335925 160 #ifdef MBED_HEAP_STATS_ENABLED
MACRUM 0:119624335925 161 heap_stats();
MACRUM 0:119624335925 162 #endif
MACRUM 0:119624335925 163 }
MACRUM 0:119624335925 164
MACRUM 0:119624335925 165 void SimpleMbedCloudClient::client_unregistered() {
MACRUM 0:119624335925 166 _registered = false;
MACRUM 0:119624335925 167 _register_called = false;
MACRUM 0:119624335925 168 printf("\nClient unregistered - Exiting application\n\n");
MACRUM 0:119624335925 169 #ifdef MBED_HEAP_STATS_ENABLED
MACRUM 0:119624335925 170 heap_stats();
MACRUM 0:119624335925 171 #endif
MACRUM 0:119624335925 172 }
MACRUM 0:119624335925 173
MACRUM 0:119624335925 174 void SimpleMbedCloudClient::error(int error_code) {
MACRUM 0:119624335925 175 const char *error;
MACRUM 0:119624335925 176 switch(error_code) {
MACRUM 0:119624335925 177 case MbedCloudClient::ConnectErrorNone:
MACRUM 0:119624335925 178 error = "MbedCloudClient::ConnectErrorNone";
MACRUM 0:119624335925 179 break;
MACRUM 0:119624335925 180 case MbedCloudClient::ConnectAlreadyExists:
MACRUM 0:119624335925 181 error = "MbedCloudClient::ConnectAlreadyExists";
MACRUM 0:119624335925 182 break;
MACRUM 0:119624335925 183 case MbedCloudClient::ConnectBootstrapFailed:
MACRUM 0:119624335925 184 error = "MbedCloudClient::ConnectBootstrapFailed";
MACRUM 0:119624335925 185 break;
MACRUM 0:119624335925 186 case MbedCloudClient::ConnectInvalidParameters:
MACRUM 0:119624335925 187 error = "MbedCloudClient::ConnectInvalidParameters";
MACRUM 0:119624335925 188 break;
MACRUM 0:119624335925 189 case MbedCloudClient::ConnectNotRegistered:
MACRUM 0:119624335925 190 error = "MbedCloudClient::ConnectNotRegistered";
MACRUM 0:119624335925 191 break;
MACRUM 0:119624335925 192 case MbedCloudClient::ConnectTimeout:
MACRUM 0:119624335925 193 error = "MbedCloudClient::ConnectTimeout";
MACRUM 0:119624335925 194 break;
MACRUM 0:119624335925 195 case MbedCloudClient::ConnectNetworkError:
MACRUM 0:119624335925 196 error = "MbedCloudClient::ConnectNetworkError";
MACRUM 0:119624335925 197 break;
MACRUM 0:119624335925 198 case MbedCloudClient::ConnectResponseParseFailed:
MACRUM 0:119624335925 199 error = "MbedCloudClient::ConnectResponseParseFailed";
MACRUM 0:119624335925 200 break;
MACRUM 0:119624335925 201 case MbedCloudClient::ConnectUnknownError:
MACRUM 0:119624335925 202 error = "MbedCloudClient::ConnectUnknownError";
MACRUM 0:119624335925 203 break;
MACRUM 0:119624335925 204 case MbedCloudClient::ConnectMemoryConnectFail:
MACRUM 0:119624335925 205 error = "MbedCloudClient::ConnectMemoryConnectFail";
MACRUM 0:119624335925 206 break;
MACRUM 0:119624335925 207 case MbedCloudClient::ConnectNotAllowed:
MACRUM 0:119624335925 208 error = "MbedCloudClient::ConnectNotAllowed";
MACRUM 0:119624335925 209 break;
MACRUM 0:119624335925 210 case MbedCloudClient::ConnectSecureConnectionFailed:
MACRUM 0:119624335925 211 error = "MbedCloudClient::ConnectSecureConnectionFailed";
MACRUM 0:119624335925 212 break;
MACRUM 0:119624335925 213 case MbedCloudClient::ConnectDnsResolvingFailed:
MACRUM 0:119624335925 214 error = "MbedCloudClient::ConnectDnsResolvingFailed";
MACRUM 0:119624335925 215 break;
MACRUM 0:119624335925 216 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
MACRUM 0:119624335925 217 case MbedCloudClient::UpdateWarningCertificateNotFound:
MACRUM 0:119624335925 218 error = "MbedCloudClient::UpdateWarningCertificateNotFound";
MACRUM 0:119624335925 219 break;
MACRUM 0:119624335925 220 case MbedCloudClient::UpdateWarningIdentityNotFound:
MACRUM 0:119624335925 221 error = "MbedCloudClient::UpdateWarningIdentityNotFound";
MACRUM 0:119624335925 222 break;
MACRUM 0:119624335925 223 case MbedCloudClient::UpdateWarningCertificateInvalid:
MACRUM 0:119624335925 224 error = "MbedCloudClient::UpdateWarningCertificateInvalid";
MACRUM 0:119624335925 225 break;
MACRUM 0:119624335925 226 case MbedCloudClient::UpdateWarningSignatureInvalid:
MACRUM 0:119624335925 227 error = "MbedCloudClient::UpdateWarningSignatureInvalid";
MACRUM 0:119624335925 228 break;
MACRUM 0:119624335925 229 case MbedCloudClient::UpdateWarningVendorMismatch:
MACRUM 0:119624335925 230 error = "MbedCloudClient::UpdateWarningVendorMismatch";
MACRUM 0:119624335925 231 break;
MACRUM 0:119624335925 232 case MbedCloudClient::UpdateWarningClassMismatch:
MACRUM 0:119624335925 233 error = "MbedCloudClient::UpdateWarningClassMismatch";
MACRUM 0:119624335925 234 break;
MACRUM 0:119624335925 235 case MbedCloudClient::UpdateWarningDeviceMismatch:
MACRUM 0:119624335925 236 error = "MbedCloudClient::UpdateWarningDeviceMismatch";
MACRUM 0:119624335925 237 break;
MACRUM 0:119624335925 238 case MbedCloudClient::UpdateWarningURINotFound:
MACRUM 0:119624335925 239 error = "MbedCloudClient::UpdateWarningURINotFound";
MACRUM 0:119624335925 240 break;
MACRUM 0:119624335925 241 case MbedCloudClient::UpdateWarningRollbackProtection:
MACRUM 0:119624335925 242 error = "MbedCloudClient::UpdateWarningRollbackProtection";
MACRUM 0:119624335925 243 break;
MACRUM 0:119624335925 244 case MbedCloudClient::UpdateWarningUnknown:
MACRUM 0:119624335925 245 error = "MbedCloudClient::UpdateWarningUnknown";
MACRUM 0:119624335925 246 break;
MACRUM 0:119624335925 247 case MbedCloudClient::UpdateErrorWriteToStorage:
MACRUM 0:119624335925 248 error = "MbedCloudClient::UpdateErrorWriteToStorage";
MACRUM 0:119624335925 249 break;
MACRUM 0:119624335925 250 case MbedCloudClient::UpdateErrorInvalidHash:
MACRUM 0:119624335925 251 error = "MbedCloudClient::UpdateErrorInvalidHash";
MACRUM 0:119624335925 252 break;
MACRUM 0:119624335925 253 #endif
MACRUM 0:119624335925 254 default:
MACRUM 0:119624335925 255 error = "UNKNOWN";
MACRUM 0:119624335925 256 }
MACRUM 0:119624335925 257 printf("\nError occurred : %s\r\n", error);
MACRUM 0:119624335925 258 printf("Error code : %d\r\n\n", error_code);
MACRUM 0:119624335925 259 printf("Error details : %s\r\n\n",_cloud_client.error_description());
MACRUM 0:119624335925 260 }
MACRUM 0:119624335925 261
MACRUM 0:119624335925 262 bool SimpleMbedCloudClient::is_client_registered() {
MACRUM 0:119624335925 263 return _registered;
MACRUM 0:119624335925 264 }
MACRUM 0:119624335925 265
MACRUM 0:119624335925 266 bool SimpleMbedCloudClient::is_register_called() {
MACRUM 0:119624335925 267 return _register_called;
MACRUM 0:119624335925 268 }
MACRUM 0:119624335925 269
MACRUM 0:119624335925 270 void SimpleMbedCloudClient::register_and_connect() {
MACRUM 0:119624335925 271 // TODO this might not work if called more than once...
MACRUM 0:119624335925 272 mcc_resource_def resourceDef;
MACRUM 0:119624335925 273
MACRUM 0:119624335925 274 // TODO clean up
MACRUM 0:119624335925 275 for (unsigned int i = 0; i < _resources.size(); i++) {
MACRUM 0:119624335925 276 _resources[i]->get_data(&resourceDef);
MACRUM 0:119624335925 277 M2MResource *res = add_resource(&_obj_list, resourceDef.object_id, resourceDef.instance_id,
MACRUM 0:119624335925 278 resourceDef.resource_id, resourceDef.name.c_str(), M2MResourceInstance::STRING,
MACRUM 0:119624335925 279 (M2MBase::Operation)resourceDef.method_mask, resourceDef.value.c_str(), resourceDef.observable,
MACRUM 0:119624335925 280 resourceDef.put_callback, resourceDef.post_callback, resourceDef.notification_callback);
MACRUM 0:119624335925 281 _resources[i]->set_resource(res);
MACRUM 0:119624335925 282 }
MACRUM 0:119624335925 283 _cloud_client.add_objects(_obj_list);
MACRUM 0:119624335925 284
MACRUM 0:119624335925 285 // Start registering to the cloud.
MACRUM 0:119624335925 286 call_register();
MACRUM 0:119624335925 287
MACRUM 0:119624335925 288 // Print memory statistics if the MBED_HEAP_STATS_ENABLED is defined.
MACRUM 0:119624335925 289 #ifdef MBED_HEAP_STATS_ENABLED
MACRUM 0:119624335925 290 printf("Register being called\r\n");
MACRUM 0:119624335925 291 heap_stats();
MACRUM 0:119624335925 292 #endif
MACRUM 0:119624335925 293 }
MACRUM 0:119624335925 294
MACRUM 0:119624335925 295 MbedCloudClient& SimpleMbedCloudClient::get_cloud_client() {
MACRUM 0:119624335925 296 return _cloud_client;
MACRUM 0:119624335925 297 }
MACRUM 0:119624335925 298
MACRUM 0:119624335925 299 MbedCloudClientResource* SimpleMbedCloudClient::create_resource(const char *path, const char *name) {
MACRUM 0:119624335925 300 MbedCloudClientResource *resource = new MbedCloudClientResource(this, path, name);
MACRUM 0:119624335925 301 _resources.push_back(resource);
MACRUM 0:119624335925 302 return resource;
MACRUM 0:119624335925 303 }