1

Committer:
group-STM32F031
Date:
Wed Mar 21 18:13:41 2018 +0000
Revision:
0:d193d40d4fa1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-STM32F031 0:d193d40d4fa1 1 // ----------------------------------------------------------------------------
group-STM32F031 0:d193d40d4fa1 2 // Copyright 2016-2017 ARM Ltd.
group-STM32F031 0:d193d40d4fa1 3 //
group-STM32F031 0:d193d40d4fa1 4 // SPDX-License-Identifier: Apache-2.0
group-STM32F031 0:d193d40d4fa1 5 //
group-STM32F031 0:d193d40d4fa1 6 // Licensed under the Apache License, Version 2.0 (the "License");
group-STM32F031 0:d193d40d4fa1 7 // you may not use this file except in compliance with the License.
group-STM32F031 0:d193d40d4fa1 8 // You may obtain a copy of the License at
group-STM32F031 0:d193d40d4fa1 9 //
group-STM32F031 0:d193d40d4fa1 10 // http://www.apache.org/licenses/LICENSE-2.0
group-STM32F031 0:d193d40d4fa1 11 //
group-STM32F031 0:d193d40d4fa1 12 // Unless required by applicable law or agreed to in writing, software
group-STM32F031 0:d193d40d4fa1 13 // distributed under the License is distributed on an "AS IS" BASIS,
group-STM32F031 0:d193d40d4fa1 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-STM32F031 0:d193d40d4fa1 15 // See the License for the specific language governing permissions and
group-STM32F031 0:d193d40d4fa1 16 // limitations under the License.
group-STM32F031 0:d193d40d4fa1 17 // ----------------------------------------------------------------------------
group-STM32F031 0:d193d40d4fa1 18
group-STM32F031 0:d193d40d4fa1 19 #include <stdio.h>
group-STM32F031 0:d193d40d4fa1 20 #include "simple-mbed-cloud-client.h"
group-STM32F031 0:d193d40d4fa1 21 #include "mbed-cloud-client/MbedCloudClient.h"
group-STM32F031 0:d193d40d4fa1 22 #include "m2mdevice.h"
group-STM32F031 0:d193d40d4fa1 23 #include "setup.h"
group-STM32F031 0:d193d40d4fa1 24 #include "m2mresource.h"
group-STM32F031 0:d193d40d4fa1 25 #include "mbed-client/m2minterface.h"
group-STM32F031 0:d193d40d4fa1 26 #include "key_config_manager.h"
group-STM32F031 0:d193d40d4fa1 27 #include "resource.h"
group-STM32F031 0:d193d40d4fa1 28 #include "mbed-client/m2mvector.h"
group-STM32F031 0:d193d40d4fa1 29 #include "mbed_cloud_client_resource.h"
group-STM32F031 0:d193d40d4fa1 30
group-STM32F031 0:d193d40d4fa1 31 #ifdef MBED_CLOUD_CLIENT_USER_CONFIG_FILE
group-STM32F031 0:d193d40d4fa1 32 #include MBED_CLOUD_CLIENT_USER_CONFIG_FILE
group-STM32F031 0:d193d40d4fa1 33 #endif
group-STM32F031 0:d193d40d4fa1 34
group-STM32F031 0:d193d40d4fa1 35 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
group-STM32F031 0:d193d40d4fa1 36 #include "update_ui_example.h"
group-STM32F031 0:d193d40d4fa1 37 #endif
group-STM32F031 0:d193d40d4fa1 38
group-STM32F031 0:d193d40d4fa1 39 #ifdef MBED_HEAP_STATS_ENABLED
group-STM32F031 0:d193d40d4fa1 40 #include "memory_tests.h"
group-STM32F031 0:d193d40d4fa1 41 #endif
group-STM32F031 0:d193d40d4fa1 42
group-STM32F031 0:d193d40d4fa1 43
group-STM32F031 0:d193d40d4fa1 44 SimpleMbedCloudClient::SimpleMbedCloudClient() :
group-STM32F031 0:d193d40d4fa1 45 _registered(false),
group-STM32F031 0:d193d40d4fa1 46 _register_called(false){
group-STM32F031 0:d193d40d4fa1 47 }
group-STM32F031 0:d193d40d4fa1 48
group-STM32F031 0:d193d40d4fa1 49 SimpleMbedCloudClient::~SimpleMbedCloudClient() {
group-STM32F031 0:d193d40d4fa1 50 for (unsigned int i = 0; _resources.size(); i++) {
group-STM32F031 0:d193d40d4fa1 51 delete _resources[i];
group-STM32F031 0:d193d40d4fa1 52 }
group-STM32F031 0:d193d40d4fa1 53 }
group-STM32F031 0:d193d40d4fa1 54
group-STM32F031 0:d193d40d4fa1 55 bool SimpleMbedCloudClient::call_register() {
group-STM32F031 0:d193d40d4fa1 56
group-STM32F031 0:d193d40d4fa1 57 _cloud_client.on_registered(this, &SimpleMbedCloudClient::client_registered);
group-STM32F031 0:d193d40d4fa1 58 _cloud_client.on_unregistered(this, &SimpleMbedCloudClient::client_unregistered);
group-STM32F031 0:d193d40d4fa1 59 _cloud_client.on_error(this, &SimpleMbedCloudClient::error);
group-STM32F031 0:d193d40d4fa1 60
group-STM32F031 0:d193d40d4fa1 61 if (init_connection()) {
group-STM32F031 0:d193d40d4fa1 62 printf("Network initialized, connecting...\n");
group-STM32F031 0:d193d40d4fa1 63 bool setup = _cloud_client.setup(get_network_interface());
group-STM32F031 0:d193d40d4fa1 64 _register_called = true;
group-STM32F031 0:d193d40d4fa1 65 if (!setup) {
group-STM32F031 0:d193d40d4fa1 66 printf("Client setup failed\n");
group-STM32F031 0:d193d40d4fa1 67 return false;
group-STM32F031 0:d193d40d4fa1 68 }
group-STM32F031 0:d193d40d4fa1 69 } else {
group-STM32F031 0:d193d40d4fa1 70 printf("Failed to initialize connection\n");
group-STM32F031 0:d193d40d4fa1 71 return false;
group-STM32F031 0:d193d40d4fa1 72 }
group-STM32F031 0:d193d40d4fa1 73
group-STM32F031 0:d193d40d4fa1 74 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
group-STM32F031 0:d193d40d4fa1 75 /* Set callback functions for authorizing updates and monitoring progress.
group-STM32F031 0:d193d40d4fa1 76 Code is implemented in update_ui_example.cpp
group-STM32F031 0:d193d40d4fa1 77 Both callbacks are completely optional. If no authorization callback
group-STM32F031 0:d193d40d4fa1 78 is set, the update process will procede immediately in each step.
group-STM32F031 0:d193d40d4fa1 79 */
group-STM32F031 0:d193d40d4fa1 80 update_ui_set_cloud_client(&_cloud_client);
group-STM32F031 0:d193d40d4fa1 81 _cloud_client.set_update_authorize_handler(update_authorize);
group-STM32F031 0:d193d40d4fa1 82 _cloud_client.set_update_progress_handler(update_progress);
group-STM32F031 0:d193d40d4fa1 83 #endif
group-STM32F031 0:d193d40d4fa1 84 return true;
group-STM32F031 0:d193d40d4fa1 85 }
group-STM32F031 0:d193d40d4fa1 86
group-STM32F031 0:d193d40d4fa1 87 void SimpleMbedCloudClient::close() {
group-STM32F031 0:d193d40d4fa1 88 _cloud_client.close();
group-STM32F031 0:d193d40d4fa1 89 }
group-STM32F031 0:d193d40d4fa1 90
group-STM32F031 0:d193d40d4fa1 91 void SimpleMbedCloudClient::register_update() {
group-STM32F031 0:d193d40d4fa1 92 _cloud_client.register_update();
group-STM32F031 0:d193d40d4fa1 93 }
group-STM32F031 0:d193d40d4fa1 94
group-STM32F031 0:d193d40d4fa1 95 void SimpleMbedCloudClient::client_registered() {
group-STM32F031 0:d193d40d4fa1 96 _registered = true;
group-STM32F031 0:d193d40d4fa1 97 printf("\nClient registered\n\n");
group-STM32F031 0:d193d40d4fa1 98 static const ConnectorClientEndpointInfo* endpoint = NULL;
group-STM32F031 0:d193d40d4fa1 99 if (endpoint == NULL) {
group-STM32F031 0:d193d40d4fa1 100 endpoint = _cloud_client.endpoint_info();
group-STM32F031 0:d193d40d4fa1 101 if (endpoint) {
group-STM32F031 0:d193d40d4fa1 102
group-STM32F031 0:d193d40d4fa1 103 #if MBED_CONF_APP_DEVELOPER_MODE == 1
group-STM32F031 0:d193d40d4fa1 104 printf("Endpoint Name: %s\r\n", endpoint->internal_endpoint_name.c_str());
group-STM32F031 0:d193d40d4fa1 105 #else
group-STM32F031 0:d193d40d4fa1 106 printf("Endpoint Name: %s\r\n", endpoint->endpoint_name.c_str());
group-STM32F031 0:d193d40d4fa1 107 #endif
group-STM32F031 0:d193d40d4fa1 108 printf("Device Id: %s\r\n", endpoint->internal_endpoint_name.c_str());
group-STM32F031 0:d193d40d4fa1 109 }
group-STM32F031 0:d193d40d4fa1 110 }
group-STM32F031 0:d193d40d4fa1 111 #ifdef MBED_HEAP_STATS_ENABLED
group-STM32F031 0:d193d40d4fa1 112 heap_stats();
group-STM32F031 0:d193d40d4fa1 113 #endif
group-STM32F031 0:d193d40d4fa1 114 }
group-STM32F031 0:d193d40d4fa1 115
group-STM32F031 0:d193d40d4fa1 116 void SimpleMbedCloudClient::client_unregistered() {
group-STM32F031 0:d193d40d4fa1 117 _registered = false;
group-STM32F031 0:d193d40d4fa1 118 _register_called = false;
group-STM32F031 0:d193d40d4fa1 119 printf("\nClient unregistered - Exiting application\n\n");
group-STM32F031 0:d193d40d4fa1 120 #ifdef MBED_HEAP_STATS_ENABLED
group-STM32F031 0:d193d40d4fa1 121 heap_stats();
group-STM32F031 0:d193d40d4fa1 122 #endif
group-STM32F031 0:d193d40d4fa1 123 }
group-STM32F031 0:d193d40d4fa1 124
group-STM32F031 0:d193d40d4fa1 125 void SimpleMbedCloudClient::error(int error_code) {
group-STM32F031 0:d193d40d4fa1 126 const char *error;
group-STM32F031 0:d193d40d4fa1 127 switch(error_code) {
group-STM32F031 0:d193d40d4fa1 128 case MbedCloudClient::ConnectErrorNone:
group-STM32F031 0:d193d40d4fa1 129 error = "MbedCloudClient::ConnectErrorNone";
group-STM32F031 0:d193d40d4fa1 130 break;
group-STM32F031 0:d193d40d4fa1 131 case MbedCloudClient::ConnectAlreadyExists:
group-STM32F031 0:d193d40d4fa1 132 error = "MbedCloudClient::ConnectAlreadyExists";
group-STM32F031 0:d193d40d4fa1 133 break;
group-STM32F031 0:d193d40d4fa1 134 case MbedCloudClient::ConnectBootstrapFailed:
group-STM32F031 0:d193d40d4fa1 135 error = "MbedCloudClient::ConnectBootstrapFailed";
group-STM32F031 0:d193d40d4fa1 136 break;
group-STM32F031 0:d193d40d4fa1 137 case MbedCloudClient::ConnectInvalidParameters:
group-STM32F031 0:d193d40d4fa1 138 error = "MbedCloudClient::ConnectInvalidParameters";
group-STM32F031 0:d193d40d4fa1 139 break;
group-STM32F031 0:d193d40d4fa1 140 case MbedCloudClient::ConnectNotRegistered:
group-STM32F031 0:d193d40d4fa1 141 error = "MbedCloudClient::ConnectNotRegistered";
group-STM32F031 0:d193d40d4fa1 142 break;
group-STM32F031 0:d193d40d4fa1 143 case MbedCloudClient::ConnectTimeout:
group-STM32F031 0:d193d40d4fa1 144 error = "MbedCloudClient::ConnectTimeout";
group-STM32F031 0:d193d40d4fa1 145 break;
group-STM32F031 0:d193d40d4fa1 146 case MbedCloudClient::ConnectNetworkError:
group-STM32F031 0:d193d40d4fa1 147 error = "MbedCloudClient::ConnectNetworkError";
group-STM32F031 0:d193d40d4fa1 148 break;
group-STM32F031 0:d193d40d4fa1 149 case MbedCloudClient::ConnectResponseParseFailed:
group-STM32F031 0:d193d40d4fa1 150 error = "MbedCloudClient::ConnectResponseParseFailed";
group-STM32F031 0:d193d40d4fa1 151 break;
group-STM32F031 0:d193d40d4fa1 152 case MbedCloudClient::ConnectUnknownError:
group-STM32F031 0:d193d40d4fa1 153 error = "MbedCloudClient::ConnectUnknownError";
group-STM32F031 0:d193d40d4fa1 154 break;
group-STM32F031 0:d193d40d4fa1 155 case MbedCloudClient::ConnectMemoryConnectFail:
group-STM32F031 0:d193d40d4fa1 156 error = "MbedCloudClient::ConnectMemoryConnectFail";
group-STM32F031 0:d193d40d4fa1 157 break;
group-STM32F031 0:d193d40d4fa1 158 case MbedCloudClient::ConnectNotAllowed:
group-STM32F031 0:d193d40d4fa1 159 error = "MbedCloudClient::ConnectNotAllowed";
group-STM32F031 0:d193d40d4fa1 160 break;
group-STM32F031 0:d193d40d4fa1 161 case MbedCloudClient::ConnectSecureConnectionFailed:
group-STM32F031 0:d193d40d4fa1 162 error = "MbedCloudClient::ConnectSecureConnectionFailed";
group-STM32F031 0:d193d40d4fa1 163 break;
group-STM32F031 0:d193d40d4fa1 164 case MbedCloudClient::ConnectDnsResolvingFailed:
group-STM32F031 0:d193d40d4fa1 165 error = "MbedCloudClient::ConnectDnsResolvingFailed";
group-STM32F031 0:d193d40d4fa1 166 break;
group-STM32F031 0:d193d40d4fa1 167 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
group-STM32F031 0:d193d40d4fa1 168 case MbedCloudClient::UpdateWarningCertificateNotFound:
group-STM32F031 0:d193d40d4fa1 169 error = "MbedCloudClient::UpdateWarningCertificateNotFound";
group-STM32F031 0:d193d40d4fa1 170 break;
group-STM32F031 0:d193d40d4fa1 171 case MbedCloudClient::UpdateWarningIdentityNotFound:
group-STM32F031 0:d193d40d4fa1 172 error = "MbedCloudClient::UpdateWarningIdentityNotFound";
group-STM32F031 0:d193d40d4fa1 173 break;
group-STM32F031 0:d193d40d4fa1 174 case MbedCloudClient::UpdateWarningCertificateInvalid:
group-STM32F031 0:d193d40d4fa1 175 error = "MbedCloudClient::UpdateWarningCertificateInvalid";
group-STM32F031 0:d193d40d4fa1 176 break;
group-STM32F031 0:d193d40d4fa1 177 case MbedCloudClient::UpdateWarningSignatureInvalid:
group-STM32F031 0:d193d40d4fa1 178 error = "MbedCloudClient::UpdateWarningSignatureInvalid";
group-STM32F031 0:d193d40d4fa1 179 break;
group-STM32F031 0:d193d40d4fa1 180 case MbedCloudClient::UpdateWarningVendorMismatch:
group-STM32F031 0:d193d40d4fa1 181 error = "MbedCloudClient::UpdateWarningVendorMismatch";
group-STM32F031 0:d193d40d4fa1 182 break;
group-STM32F031 0:d193d40d4fa1 183 case MbedCloudClient::UpdateWarningClassMismatch:
group-STM32F031 0:d193d40d4fa1 184 error = "MbedCloudClient::UpdateWarningClassMismatch";
group-STM32F031 0:d193d40d4fa1 185 break;
group-STM32F031 0:d193d40d4fa1 186 case MbedCloudClient::UpdateWarningDeviceMismatch:
group-STM32F031 0:d193d40d4fa1 187 error = "MbedCloudClient::UpdateWarningDeviceMismatch";
group-STM32F031 0:d193d40d4fa1 188 break;
group-STM32F031 0:d193d40d4fa1 189 case MbedCloudClient::UpdateWarningURINotFound:
group-STM32F031 0:d193d40d4fa1 190 error = "MbedCloudClient::UpdateWarningURINotFound";
group-STM32F031 0:d193d40d4fa1 191 break;
group-STM32F031 0:d193d40d4fa1 192 case MbedCloudClient::UpdateWarningRollbackProtection:
group-STM32F031 0:d193d40d4fa1 193 error = "MbedCloudClient::UpdateWarningRollbackProtection";
group-STM32F031 0:d193d40d4fa1 194 break;
group-STM32F031 0:d193d40d4fa1 195 case MbedCloudClient::UpdateWarningUnknown:
group-STM32F031 0:d193d40d4fa1 196 error = "MbedCloudClient::UpdateWarningUnknown";
group-STM32F031 0:d193d40d4fa1 197 break;
group-STM32F031 0:d193d40d4fa1 198 case MbedCloudClient::UpdateErrorWriteToStorage:
group-STM32F031 0:d193d40d4fa1 199 error = "MbedCloudClient::UpdateErrorWriteToStorage";
group-STM32F031 0:d193d40d4fa1 200 break;
group-STM32F031 0:d193d40d4fa1 201 case MbedCloudClient::UpdateErrorInvalidHash:
group-STM32F031 0:d193d40d4fa1 202 error = "MbedCloudClient::UpdateErrorInvalidHash";
group-STM32F031 0:d193d40d4fa1 203 break;
group-STM32F031 0:d193d40d4fa1 204 #endif
group-STM32F031 0:d193d40d4fa1 205 default:
group-STM32F031 0:d193d40d4fa1 206 error = "UNKNOWN";
group-STM32F031 0:d193d40d4fa1 207 }
group-STM32F031 0:d193d40d4fa1 208 printf("\nError occurred : %s\r\n", error);
group-STM32F031 0:d193d40d4fa1 209 printf("Error code : %d\r\n\n", error_code);
group-STM32F031 0:d193d40d4fa1 210 printf("Error details : %s\r\n\n",_cloud_client.error_description());
group-STM32F031 0:d193d40d4fa1 211 }
group-STM32F031 0:d193d40d4fa1 212
group-STM32F031 0:d193d40d4fa1 213 bool SimpleMbedCloudClient::is_client_registered() {
group-STM32F031 0:d193d40d4fa1 214 return _registered;
group-STM32F031 0:d193d40d4fa1 215 }
group-STM32F031 0:d193d40d4fa1 216
group-STM32F031 0:d193d40d4fa1 217 bool SimpleMbedCloudClient::is_register_called() {
group-STM32F031 0:d193d40d4fa1 218 return _register_called;
group-STM32F031 0:d193d40d4fa1 219 }
group-STM32F031 0:d193d40d4fa1 220
group-STM32F031 0:d193d40d4fa1 221 void SimpleMbedCloudClient::register_and_connect() {
group-STM32F031 0:d193d40d4fa1 222
group-STM32F031 0:d193d40d4fa1 223 _cloud_client.add_objects(_obj_list);
group-STM32F031 0:d193d40d4fa1 224
group-STM32F031 0:d193d40d4fa1 225 // Start registering to the cloud.
group-STM32F031 0:d193d40d4fa1 226 call_register();
group-STM32F031 0:d193d40d4fa1 227
group-STM32F031 0:d193d40d4fa1 228 // Print memory statistics if the MBED_HEAP_STATS_ENABLED is defined.
group-STM32F031 0:d193d40d4fa1 229 #ifdef MBED_HEAP_STATS_ENABLED
group-STM32F031 0:d193d40d4fa1 230 printf("Register being called\r\n");
group-STM32F031 0:d193d40d4fa1 231 heap_stats();
group-STM32F031 0:d193d40d4fa1 232 #endif
group-STM32F031 0:d193d40d4fa1 233 }
group-STM32F031 0:d193d40d4fa1 234
group-STM32F031 0:d193d40d4fa1 235 MbedCloudClient& SimpleMbedCloudClient::get_cloud_client() {
group-STM32F031 0:d193d40d4fa1 236 return _cloud_client;
group-STM32F031 0:d193d40d4fa1 237 }
group-STM32F031 0:d193d40d4fa1 238
group-STM32F031 0:d193d40d4fa1 239 MbedCloudClientResource* SimpleMbedCloudClient::create_resource(const char *path, const char *name) {
group-STM32F031 0:d193d40d4fa1 240 MbedCloudClientResource *resource = new MbedCloudClientResource(this, path, name);
group-STM32F031 0:d193d40d4fa1 241 _resources.push_back(resource);
group-STM32F031 0:d193d40d4fa1 242 return resource;
group-STM32F031 0:d193d40d4fa1 243 }
group-STM32F031 0:d193d40d4fa1 244
group-STM32F031 0:d193d40d4fa1 245 M2MResource* SimpleMbedCloudClient::add_cloud_resource(uint16_t object_id, uint16_t instance_id,
group-STM32F031 0:d193d40d4fa1 246 uint16_t resource_id, const char *resource_type,
group-STM32F031 0:d193d40d4fa1 247 M2MResourceInstance::ResourceType data_type,
group-STM32F031 0:d193d40d4fa1 248 M2MBase::Operation allowed, const char *value,
group-STM32F031 0:d193d40d4fa1 249 bool observable, void *cb, void *notification_status_cb) {
group-STM32F031 0:d193d40d4fa1 250 return add_resource(&_obj_list, object_id, instance_id, resource_id, resource_type, data_type,
group-STM32F031 0:d193d40d4fa1 251 allowed, value, observable, cb, notification_status_cb);
group-STM32F031 0:d193d40d4fa1 252 }