Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MbedCloudClient.cpp Source File

MbedCloudClient.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "mbed-cloud-client/MbedCloudClientConfig.h"
00020 #include "mbed-cloud-client/MbedCloudClient.h"
00021 #include "mbed-cloud-client/SimpleM2MResource.h"
00022 
00023 #include "mbed-trace/mbed_trace.h"
00024 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
00025 #include "CertificateEnrollmentClient.h"
00026 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
00027 
00028 #include <assert.h>
00029 
00030 #define xstr(s) str(s)
00031 #define str(s) #s
00032 
00033 #define TRACE_GROUP "mClt"
00034 
00035 MbedCloudClient::MbedCloudClient()
00036 :_client(*this),
00037  _value_callback(NULL),
00038  _error_description(NULL)
00039 {
00040 }
00041 
00042 MbedCloudClient::~MbedCloudClient()
00043 {
00044     _object_list.clear();
00045 }
00046 
00047 void MbedCloudClient::add_objects(const M2MObjectList& object_list)
00048 {
00049     if(!object_list.empty()) {
00050         M2MObjectList::const_iterator it;
00051         it = object_list.begin();
00052         for (; it!= object_list.end(); it++) {
00053             _object_list.push_back((M2MBase*)*it);
00054         }
00055     }
00056 }
00057 
00058 void MbedCloudClient::add_objects(const M2MBaseList& base_list)
00059 {
00060     if(!base_list.empty()) {
00061         M2MBaseList::const_iterator it;
00062         it = base_list.begin();
00063         for (; it!= base_list.end(); it++) {
00064             _object_list.push_back(*it);
00065         }
00066     }
00067 }
00068 
00069 void MbedCloudClient::remove_object(M2MBase *object)
00070 {
00071     // finish the ServiceClient's initialization and M2MInterface
00072     bool success = _client.connector_client().setup();
00073 
00074     M2MBaseList::const_iterator it;
00075     int found_index = -1;
00076     int index;
00077     tr_debug("MbedCloudClient::remove_object %p", object);
00078     for (it = _object_list.begin(), index = 0; it != _object_list.end(); it++, index++) {
00079         if(*it == object) {
00080             found_index = index;
00081             break;
00082         }
00083     }
00084     if (found_index != -1) {
00085         tr_debug("  object found at index %d", found_index);
00086         _object_list.erase(found_index);
00087         if (success) {
00088             _client.connector_client().m2m_interface()->remove_object(object);
00089         }
00090     }
00091 }
00092 
00093 void MbedCloudClient::set_update_callback(MbedCloudClientCallback *callback)
00094 {
00095     _value_callback = callback;
00096 }
00097 
00098 bool MbedCloudClient::setup(void* iface)
00099 {
00100     tr_debug("MbedCloudClient setup()");
00101 
00102     // Add objects to list
00103 #if MBED_CLOUD_CLIENT_STL_API
00104     map<string, M2MObject*>::iterator it;
00105     for (it = _objects.begin(); it != _objects.end(); it++)
00106     {
00107         _object_list.push_back((M2MBase*)it->second);
00108     }
00109 #endif
00110 
00111     // finish the ServiceClient's initialization and M2MInterface
00112     bool success = _client.connector_client().setup();
00113 
00114     if (success) {
00115         // set the network interface to M2MInterface
00116         _client.connector_client().m2m_interface()->set_platform_network_handler(iface);
00117         _client.initialize_and_register(_object_list);
00118     }
00119     return success;
00120 }
00121 
00122 void MbedCloudClient::on_registered(void(*fn)(void))
00123 {
00124     FP0<void> fp(fn);
00125     _on_registered = fp;
00126 }
00127 
00128 
00129 void MbedCloudClient::on_error(void(*fn)(int))
00130 {
00131     _on_error = fn;
00132 }
00133 
00134 
00135 void MbedCloudClient::on_unregistered(void(*fn)(void))
00136 {
00137     FP0<void> fp(fn);
00138     _on_unregistered = fp;
00139 }
00140 
00141 void MbedCloudClient::on_registration_updated(void(*fn)(void))
00142 {
00143     FP0<void> fp(fn);
00144     _on_registration_updated = fp;
00145 }
00146 
00147 void MbedCloudClient::keep_alive()
00148 {
00149     _client.connector_client().update_registration();
00150 }
00151 
00152 void MbedCloudClient::register_update()
00153 {
00154     _client.connector_client().update_registration();
00155 }
00156 
00157 void MbedCloudClient::close()
00158 {
00159     // finish the ServiceClient's initialization and M2MInterface
00160     bool success = _client.connector_client().setup();
00161 
00162     if (success) {
00163         _client.connector_client().m2m_interface()->unregister_object(NULL);
00164     }
00165 }
00166 
00167 const ConnectorClientEndpointInfo *MbedCloudClient::endpoint_info() const
00168 {
00169     return _client.connector_client().endpoint_info();
00170 }
00171 
00172 void MbedCloudClient::set_queue_sleep_handler(callback_handler handler)
00173 {
00174     // finish the ServiceClient's initialization and M2MInterface
00175     bool success = _client.connector_client().setup();
00176 
00177     if (success) {
00178         _client.connector_client().m2m_interface()->set_queue_sleep_handler(handler);
00179     }
00180 }
00181 
00182 void MbedCloudClient::set_random_number_callback(random_number_cb callback)
00183 {
00184     // finish the ServiceClient's initialization and M2MInterface
00185     bool success = _client.connector_client().setup();
00186 
00187     if (success) {
00188         _client.connector_client().m2m_interface()->set_random_number_callback(callback);
00189     }
00190 }
00191 
00192 void MbedCloudClient::set_entropy_callback(entropy_cb callback)
00193 {
00194     // finish the ServiceClient's initialization and M2MInterface
00195     bool success = _client.connector_client().setup();
00196 
00197     if (success) {
00198         _client.connector_client().m2m_interface()->set_entropy_callback(callback);
00199     }
00200 }
00201 
00202 #if MBED_CLOUD_CLIENT_STL_API
00203 bool MbedCloudClient::set_device_resource_value(M2MDevice::DeviceResource resource,
00204                                                 const std::string &value)
00205 {
00206     return _client.set_device_resource_value(resource, value);
00207 }
00208 
00209 void MbedCloudClient::register_update_callback(string route,
00210                                                SimpleM2MResourceBase* resource)
00211 {
00212     _update_values[route] = resource;
00213 }
00214 #endif // MBED_CLOUD_CLIENT_STL_API
00215 
00216 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
00217 void MbedCloudClient::set_update_authorize_handler(void (*handler)(int32_t request))
00218 {
00219     _client.set_update_authorize_handler(handler);
00220 }
00221 
00222 void MbedCloudClient::set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total))
00223 {
00224     _client.set_update_progress_handler(handler);
00225 }
00226 
00227 void MbedCloudClient::update_authorize(int32_t request)
00228 {
00229     _client.update_authorize(request);
00230 }
00231 #endif
00232 
00233 const char *MbedCloudClient::error_description() const
00234 {
00235     return _error_description;
00236 }
00237 
00238 
00239 void MbedCloudClient::complete(ServiceClientCallbackStatus status)
00240 {
00241     tr_info("MbedCloudClient::complete status (%d)", status);
00242     if (status == Service_Client_Status_Registered) {
00243         _on_registered.call();
00244     } else if (status == Service_Client_Status_Unregistered) {
00245         _object_list.clear();
00246         _on_unregistered.call();
00247     } else if (status == Service_Client_Status_Register_Updated) {
00248         _on_registration_updated.call();
00249     }
00250 }
00251 
00252 void MbedCloudClient::error(int error, const char *reason)
00253 {
00254     tr_error("MbedCloudClient::error code (%d)", error);
00255     _error_description = reason;
00256     _on_error(error);
00257 }
00258 
00259 void MbedCloudClient::value_updated(M2MBase *base, M2MBase::BaseType type)
00260 {
00261     if (base) {
00262         tr_info("MbedCloudClient::value_updated path %s", base->uri_path());
00263         if (base->uri_path()) {
00264 #if MBED_CLOUD_CLIENT_STL_API
00265             if (_update_values.count(base->uri_path()) != 0) {
00266                 tr_debug("MbedCloudClient::value_updated calling update() for %s", base->uri_path());
00267                 _update_values[base->uri_path()]->update();
00268             } else
00269 #endif
00270             {
00271                 // way to tell application that there is a value update
00272                 if (_value_callback) {
00273                     _value_callback->value_updated(base, type);
00274                 }
00275             }
00276         }
00277     }
00278 }
00279 
00280 void MbedCloudClient::send_get_request(DownloadType type,
00281                                        const char *uri,
00282                                        const size_t offset,
00283                                        get_data_cb data_cb,
00284                                        get_data_error_cb error_cb,
00285                                        void *context)
00286 {
00287     // finish the ServiceClient's initialization and M2MInterface
00288     bool success = _client.connector_client().setup();
00289 
00290     if (success) {
00291         _client.connector_client().m2m_interface()->get_data_request(type,
00292                                                                     uri,
00293                                                                     offset,
00294                                                                     true,
00295                                                                     data_cb,
00296                                                                     error_cb,
00297                                                                     context);
00298     }
00299 }
00300 
00301 #ifndef MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
00302 ce_status_e  MbedCloudClient::certificate_renew(const char *cert_name)
00303 {
00304     return CertificateEnrollmentClient::certificate_renew(cert_name);
00305 }
00306 
00307 void MbedCloudClient::on_certificate_renewal(cert_renewal_cb_f user_cb)
00308 {
00309     CertificateEnrollmentClient::on_certificate_renewal(user_cb);
00310 }
00311 #endif // MBED_CONF_MBED_CLOUD_CLIENT_DISABLE_CERTIFICATE_ENROLLMENT
00312 
00313 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
00314 const M2MBaseList* MbedCloudClient::get_object_list() const
00315 {
00316     return &_object_list;
00317 }
00318 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION
00319 
00320 void MbedCloudClient::pause()
00321 {
00322     // finish the ServiceClient's initialization and M2MInterface
00323     bool success = _client.connector_client().setup();
00324 
00325     if (success) {
00326         _client.connector_client().m2m_interface()->pause();
00327     }
00328 }
00329 
00330 void MbedCloudClient::resume(void *iface)
00331 {
00332     // finish the ServiceClient's initialization and M2MInterface
00333     bool success = _client.connector_client().setup();
00334 
00335     if (success) {
00336         _client.connector_client().m2m_interface()->resume(iface, _object_list);
00337     }
00338 }