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