Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 // ----------------------------------------------------------------------------
vithyat 0:977e87915078 2 // Copyright 2016-2017 ARM Ltd.
vithyat 0:977e87915078 3 //
vithyat 0:977e87915078 4 // SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 5 //
vithyat 0:977e87915078 6 // Licensed under the Apache License, Version 2.0 (the "License");
vithyat 0:977e87915078 7 // you may not use this file except in compliance with the License.
vithyat 0:977e87915078 8 // You may obtain a copy of the License at
vithyat 0:977e87915078 9 //
vithyat 0:977e87915078 10 // http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 11 //
vithyat 0:977e87915078 12 // Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 13 // distributed under the License is distributed on an "AS IS" BASIS,
vithyat 0:977e87915078 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 15 // See the License for the specific language governing permissions and
vithyat 0:977e87915078 16 // limitations under the License.
vithyat 0:977e87915078 17 // ----------------------------------------------------------------------------
vithyat 0:977e87915078 18
vithyat 0:977e87915078 19 #include "mbed-cloud-client/MbedCloudClientConfig.h"
vithyat 0:977e87915078 20 #include "mbed-cloud-client/MbedCloudClient.h"
vithyat 0:977e87915078 21 #include "mbed-cloud-client/SimpleM2MResource.h"
vithyat 0:977e87915078 22
vithyat 0:977e87915078 23 #include "mbed-trace/mbed_trace.h"
vithyat 0:977e87915078 24 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
vithyat 0:977e87915078 25 #include "CertificateEnrollmentClient.h"
vithyat 0:977e87915078 26 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
vithyat 0:977e87915078 27
vithyat 0:977e87915078 28 #include <assert.h>
vithyat 0:977e87915078 29
vithyat 0:977e87915078 30 #define xstr(s) str(s)
vithyat 0:977e87915078 31 #define str(s) #s
vithyat 0:977e87915078 32
vithyat 0:977e87915078 33 #define TRACE_GROUP "mClt"
vithyat 0:977e87915078 34
vithyat 0:977e87915078 35 MbedCloudClient::MbedCloudClient()
vithyat 0:977e87915078 36 :_client(*this),
vithyat 0:977e87915078 37 _value_callback(NULL),
vithyat 0:977e87915078 38 _error_description(NULL)
vithyat 0:977e87915078 39 {
vithyat 0:977e87915078 40 }
vithyat 0:977e87915078 41
vithyat 0:977e87915078 42 MbedCloudClient::~MbedCloudClient()
vithyat 0:977e87915078 43 {
vithyat 0:977e87915078 44 _object_list.clear();
vithyat 0:977e87915078 45 }
vithyat 0:977e87915078 46
vithyat 0:977e87915078 47 void MbedCloudClient::add_objects(const M2MObjectList& object_list)
vithyat 0:977e87915078 48 {
vithyat 0:977e87915078 49 if(!object_list.empty()) {
vithyat 0:977e87915078 50 M2MObjectList::const_iterator it;
vithyat 0:977e87915078 51 it = object_list.begin();
vithyat 0:977e87915078 52 for (; it!= object_list.end(); it++) {
vithyat 0:977e87915078 53 _object_list.push_back((M2MBase*)*it);
vithyat 0:977e87915078 54 }
vithyat 0:977e87915078 55 }
vithyat 0:977e87915078 56 }
vithyat 0:977e87915078 57
vithyat 0:977e87915078 58 void MbedCloudClient::add_objects(const M2MBaseList& base_list)
vithyat 0:977e87915078 59 {
vithyat 0:977e87915078 60 if(!base_list.empty()) {
vithyat 0:977e87915078 61 M2MBaseList::const_iterator it;
vithyat 0:977e87915078 62 it = base_list.begin();
vithyat 0:977e87915078 63 for (; it!= base_list.end(); it++) {
vithyat 0:977e87915078 64 _object_list.push_back(*it);
vithyat 0:977e87915078 65 }
vithyat 0:977e87915078 66 }
vithyat 0:977e87915078 67 }
vithyat 0:977e87915078 68
vithyat 0:977e87915078 69 void MbedCloudClient::remove_object(M2MBase *object)
vithyat 0:977e87915078 70 {
vithyat 0:977e87915078 71 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 72 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 73
vithyat 0:977e87915078 74 M2MBaseList::const_iterator it;
vithyat 0:977e87915078 75 int found_index = -1;
vithyat 0:977e87915078 76 int index;
vithyat 0:977e87915078 77 tr_debug("MbedCloudClient::remove_object %p", object);
vithyat 0:977e87915078 78 for (it = _object_list.begin(), index = 0; it != _object_list.end(); it++, index++) {
vithyat 0:977e87915078 79 if(*it == object) {
vithyat 0:977e87915078 80 found_index = index;
vithyat 0:977e87915078 81 break;
vithyat 0:977e87915078 82 }
vithyat 0:977e87915078 83 }
vithyat 0:977e87915078 84 if (found_index != -1) {
vithyat 0:977e87915078 85 tr_debug(" object found at index %d", found_index);
vithyat 0:977e87915078 86 _object_list.erase(found_index);
vithyat 0:977e87915078 87 if (success) {
vithyat 0:977e87915078 88 _client.connector_client().m2m_interface()->remove_object(object);
vithyat 0:977e87915078 89 }
vithyat 0:977e87915078 90 }
vithyat 0:977e87915078 91 }
vithyat 0:977e87915078 92
vithyat 0:977e87915078 93 void MbedCloudClient::set_update_callback(MbedCloudClientCallback *callback)
vithyat 0:977e87915078 94 {
vithyat 0:977e87915078 95 _value_callback = callback;
vithyat 0:977e87915078 96 }
vithyat 0:977e87915078 97
vithyat 0:977e87915078 98 bool MbedCloudClient::setup(void* iface)
vithyat 0:977e87915078 99 {
vithyat 0:977e87915078 100 tr_debug("MbedCloudClient setup()");
vithyat 0:977e87915078 101
vithyat 0:977e87915078 102 // Add objects to list
vithyat 0:977e87915078 103 #if MBED_CLOUD_CLIENT_STL_API
vithyat 0:977e87915078 104 map<string, M2MObject*>::iterator it;
vithyat 0:977e87915078 105 for (it = _objects.begin(); it != _objects.end(); it++)
vithyat 0:977e87915078 106 {
vithyat 0:977e87915078 107 _object_list.push_back((M2MBase*)it->second);
vithyat 0:977e87915078 108 }
vithyat 0:977e87915078 109 #endif
vithyat 0:977e87915078 110
vithyat 0:977e87915078 111 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 112 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 113
vithyat 0:977e87915078 114 if (success) {
vithyat 0:977e87915078 115 // set the network interface to M2MInterface
vithyat 0:977e87915078 116 _client.connector_client().m2m_interface()->set_platform_network_handler(iface);
vithyat 0:977e87915078 117 _client.initialize_and_register(_object_list);
vithyat 0:977e87915078 118 }
vithyat 0:977e87915078 119 return success;
vithyat 0:977e87915078 120 }
vithyat 0:977e87915078 121
vithyat 0:977e87915078 122 void MbedCloudClient::on_registered(void(*fn)(void))
vithyat 0:977e87915078 123 {
vithyat 0:977e87915078 124 FP0<void> fp(fn);
vithyat 0:977e87915078 125 _on_registered = fp;
vithyat 0:977e87915078 126 }
vithyat 0:977e87915078 127
vithyat 0:977e87915078 128
vithyat 0:977e87915078 129 void MbedCloudClient::on_error(void(*fn)(int))
vithyat 0:977e87915078 130 {
vithyat 0:977e87915078 131 _on_error = fn;
vithyat 0:977e87915078 132 }
vithyat 0:977e87915078 133
vithyat 0:977e87915078 134
vithyat 0:977e87915078 135 void MbedCloudClient::on_unregistered(void(*fn)(void))
vithyat 0:977e87915078 136 {
vithyat 0:977e87915078 137 FP0<void> fp(fn);
vithyat 0:977e87915078 138 _on_unregistered = fp;
vithyat 0:977e87915078 139 }
vithyat 0:977e87915078 140
vithyat 0:977e87915078 141 void MbedCloudClient::on_registration_updated(void(*fn)(void))
vithyat 0:977e87915078 142 {
vithyat 0:977e87915078 143 FP0<void> fp(fn);
vithyat 0:977e87915078 144 _on_registration_updated = fp;
vithyat 0:977e87915078 145 }
vithyat 0:977e87915078 146
vithyat 0:977e87915078 147 void MbedCloudClient::keep_alive()
vithyat 0:977e87915078 148 {
vithyat 0:977e87915078 149 _client.connector_client().update_registration();
vithyat 0:977e87915078 150 }
vithyat 0:977e87915078 151
vithyat 0:977e87915078 152 void MbedCloudClient::register_update()
vithyat 0:977e87915078 153 {
vithyat 0:977e87915078 154 _client.connector_client().update_registration();
vithyat 0:977e87915078 155 }
vithyat 0:977e87915078 156
vithyat 0:977e87915078 157 void MbedCloudClient::close()
vithyat 0:977e87915078 158 {
vithyat 0:977e87915078 159 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 160 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 161
vithyat 0:977e87915078 162 if (success) {
vithyat 0:977e87915078 163 _client.connector_client().m2m_interface()->unregister_object(NULL);
vithyat 0:977e87915078 164 }
vithyat 0:977e87915078 165 }
vithyat 0:977e87915078 166
vithyat 0:977e87915078 167 const ConnectorClientEndpointInfo *MbedCloudClient::endpoint_info() const
vithyat 0:977e87915078 168 {
vithyat 0:977e87915078 169 return _client.connector_client().endpoint_info();
vithyat 0:977e87915078 170 }
vithyat 0:977e87915078 171
vithyat 0:977e87915078 172 void MbedCloudClient::set_queue_sleep_handler(callback_handler handler)
vithyat 0:977e87915078 173 {
vithyat 0:977e87915078 174 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 175 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 176
vithyat 0:977e87915078 177 if (success) {
vithyat 0:977e87915078 178 _client.connector_client().m2m_interface()->set_queue_sleep_handler(handler);
vithyat 0:977e87915078 179 }
vithyat 0:977e87915078 180 }
vithyat 0:977e87915078 181
vithyat 0:977e87915078 182 void MbedCloudClient::set_random_number_callback(random_number_cb callback)
vithyat 0:977e87915078 183 {
vithyat 0:977e87915078 184 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 185 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 186
vithyat 0:977e87915078 187 if (success) {
vithyat 0:977e87915078 188 _client.connector_client().m2m_interface()->set_random_number_callback(callback);
vithyat 0:977e87915078 189 }
vithyat 0:977e87915078 190 }
vithyat 0:977e87915078 191
vithyat 0:977e87915078 192 void MbedCloudClient::set_entropy_callback(entropy_cb callback)
vithyat 0:977e87915078 193 {
vithyat 0:977e87915078 194 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 195 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 196
vithyat 0:977e87915078 197 if (success) {
vithyat 0:977e87915078 198 _client.connector_client().m2m_interface()->set_entropy_callback(callback);
vithyat 0:977e87915078 199 }
vithyat 0:977e87915078 200 }
vithyat 0:977e87915078 201
vithyat 0:977e87915078 202 #if MBED_CLOUD_CLIENT_STL_API
vithyat 0:977e87915078 203 bool MbedCloudClient::set_device_resource_value(M2MDevice::DeviceResource resource,
vithyat 0:977e87915078 204 const std::string &value)
vithyat 0:977e87915078 205 {
vithyat 0:977e87915078 206 return _client.set_device_resource_value(resource, value);
vithyat 0:977e87915078 207 }
vithyat 0:977e87915078 208
vithyat 0:977e87915078 209 void MbedCloudClient::register_update_callback(string route,
vithyat 0:977e87915078 210 SimpleM2MResourceBase* resource)
vithyat 0:977e87915078 211 {
vithyat 0:977e87915078 212 _update_values[route] = resource;
vithyat 0:977e87915078 213 }
vithyat 0:977e87915078 214 #endif // MBED_CLOUD_CLIENT_STL_API
vithyat 0:977e87915078 215
vithyat 0:977e87915078 216 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
vithyat 0:977e87915078 217 void MbedCloudClient::set_update_authorize_handler(void (*handler)(int32_t request))
vithyat 0:977e87915078 218 {
vithyat 0:977e87915078 219 _client.set_update_authorize_handler(handler);
vithyat 0:977e87915078 220 }
vithyat 0:977e87915078 221
vithyat 0:977e87915078 222 void MbedCloudClient::set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total))
vithyat 0:977e87915078 223 {
vithyat 0:977e87915078 224 _client.set_update_progress_handler(handler);
vithyat 0:977e87915078 225 }
vithyat 0:977e87915078 226
vithyat 0:977e87915078 227 void MbedCloudClient::update_authorize(int32_t request)
vithyat 0:977e87915078 228 {
vithyat 0:977e87915078 229 _client.update_authorize(request);
vithyat 0:977e87915078 230 }
vithyat 0:977e87915078 231 #endif
vithyat 0:977e87915078 232
vithyat 0:977e87915078 233 const char *MbedCloudClient::error_description() const
vithyat 0:977e87915078 234 {
vithyat 0:977e87915078 235 return _error_description;
vithyat 0:977e87915078 236 }
vithyat 0:977e87915078 237
vithyat 0:977e87915078 238
vithyat 0:977e87915078 239 void MbedCloudClient::complete(ServiceClientCallbackStatus status)
vithyat 0:977e87915078 240 {
vithyat 0:977e87915078 241 tr_info("MbedCloudClient::complete status (%d)", status);
vithyat 0:977e87915078 242 if (status == Service_Client_Status_Registered) {
vithyat 0:977e87915078 243 _on_registered.call();
vithyat 0:977e87915078 244 } else if (status == Service_Client_Status_Unregistered) {
vithyat 0:977e87915078 245 _object_list.clear();
vithyat 0:977e87915078 246 _on_unregistered.call();
vithyat 0:977e87915078 247 } else if (status == Service_Client_Status_Register_Updated) {
vithyat 0:977e87915078 248 _on_registration_updated.call();
vithyat 0:977e87915078 249 }
vithyat 0:977e87915078 250 }
vithyat 0:977e87915078 251
vithyat 0:977e87915078 252 void MbedCloudClient::error(int error, const char *reason)
vithyat 0:977e87915078 253 {
vithyat 0:977e87915078 254 tr_error("MbedCloudClient::error code (%d)", error);
vithyat 0:977e87915078 255 _error_description = reason;
vithyat 0:977e87915078 256 _on_error(error);
vithyat 0:977e87915078 257 }
vithyat 0:977e87915078 258
vithyat 0:977e87915078 259 void MbedCloudClient::value_updated(M2MBase *base, M2MBase::BaseType type)
vithyat 0:977e87915078 260 {
vithyat 0:977e87915078 261 if (base) {
vithyat 0:977e87915078 262 tr_info("MbedCloudClient::value_updated path %s", base->uri_path());
vithyat 0:977e87915078 263 if (base->uri_path()) {
vithyat 0:977e87915078 264 #if MBED_CLOUD_CLIENT_STL_API
vithyat 0:977e87915078 265 if (_update_values.count(base->uri_path()) != 0) {
vithyat 0:977e87915078 266 tr_debug("MbedCloudClient::value_updated calling update() for %s", base->uri_path());
vithyat 0:977e87915078 267 _update_values[base->uri_path()]->update();
vithyat 0:977e87915078 268 } else
vithyat 0:977e87915078 269 #endif
vithyat 0:977e87915078 270 {
vithyat 0:977e87915078 271 // way to tell application that there is a value update
vithyat 0:977e87915078 272 if (_value_callback) {
vithyat 0:977e87915078 273 _value_callback->value_updated(base, type);
vithyat 0:977e87915078 274 }
vithyat 0:977e87915078 275 }
vithyat 0:977e87915078 276 }
vithyat 0:977e87915078 277 }
vithyat 0:977e87915078 278 }
vithyat 0:977e87915078 279
vithyat 0:977e87915078 280 void MbedCloudClient::send_get_request(DownloadType type,
vithyat 0:977e87915078 281 const char *uri,
vithyat 0:977e87915078 282 const size_t offset,
vithyat 0:977e87915078 283 get_data_cb data_cb,
vithyat 0:977e87915078 284 get_data_error_cb error_cb,
vithyat 0:977e87915078 285 void *context)
vithyat 0:977e87915078 286 {
vithyat 0:977e87915078 287 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 288 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 289
vithyat 0:977e87915078 290 if (success) {
vithyat 0:977e87915078 291 _client.connector_client().m2m_interface()->get_data_request(type,
vithyat 0:977e87915078 292 uri,
vithyat 0:977e87915078 293 offset,
vithyat 0:977e87915078 294 true,
vithyat 0:977e87915078 295 data_cb,
vithyat 0:977e87915078 296 error_cb,
vithyat 0:977e87915078 297 context);
vithyat 0:977e87915078 298 }
vithyat 0:977e87915078 299 }
vithyat 0:977e87915078 300
vithyat 0:977e87915078 301 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
vithyat 0:977e87915078 302 ce_status_e MbedCloudClient::certificate_renew(const char *cert_name)
vithyat 0:977e87915078 303 {
vithyat 0:977e87915078 304 return CertificateEnrollmentClient::certificate_renew(cert_name);
vithyat 0:977e87915078 305 }
vithyat 0:977e87915078 306
vithyat 0:977e87915078 307 void MbedCloudClient::on_certificate_renewal(cert_renewal_cb_f user_cb)
vithyat 0:977e87915078 308 {
vithyat 0:977e87915078 309 CertificateEnrollmentClient::on_certificate_renewal(user_cb);
vithyat 0:977e87915078 310 }
vithyat 0:977e87915078 311 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
vithyat 0:977e87915078 312
vithyat 0:977e87915078 313 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 314 const M2MBaseList* MbedCloudClient::get_object_list() const
vithyat 0:977e87915078 315 {
vithyat 0:977e87915078 316 return &_object_list;
vithyat 0:977e87915078 317 }
vithyat 0:977e87915078 318 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 319
vithyat 0:977e87915078 320 void MbedCloudClient::pause()
vithyat 0:977e87915078 321 {
vithyat 0:977e87915078 322 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 323 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 324
vithyat 0:977e87915078 325 if (success) {
vithyat 0:977e87915078 326 _client.connector_client().m2m_interface()->pause();
vithyat 0:977e87915078 327 }
vithyat 0:977e87915078 328 }
vithyat 0:977e87915078 329
vithyat 0:977e87915078 330 void MbedCloudClient::resume(void *iface)
vithyat 0:977e87915078 331 {
vithyat 0:977e87915078 332 // finish the ServiceClient's initialization and M2MInterface
vithyat 0:977e87915078 333 bool success = _client.connector_client().setup();
vithyat 0:977e87915078 334
vithyat 0:977e87915078 335 if (success) {
vithyat 0:977e87915078 336 _client.connector_client().m2m_interface()->resume(iface, _object_list);
vithyat 0:977e87915078 337 }
vithyat 0:977e87915078 338 }