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.
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 00025 #include <assert.h> 00026 00027 #define xstr(s) str(s) 00028 #define str(s) #s 00029 00030 #define TRACE_GROUP "mClt" 00031 00032 MbedCloudClient::MbedCloudClient() 00033 :_client(*this), 00034 _value_callback(NULL), 00035 _error_description(NULL) 00036 { 00037 } 00038 00039 MbedCloudClient::~MbedCloudClient() 00040 { 00041 _object_list.clear(); 00042 } 00043 00044 void MbedCloudClient::add_objects(const M2MObjectList& object_list) 00045 { 00046 if(!object_list.empty()) { 00047 M2MObjectList::const_iterator it; 00048 it = object_list.begin(); 00049 for (; it!= object_list.end(); it++) { 00050 _object_list.push_back((M2MBase*)*it); 00051 } 00052 } 00053 } 00054 00055 void MbedCloudClient::add_objects(const M2MBaseList& base_list) 00056 { 00057 if(!base_list.empty()) { 00058 M2MBaseList::const_iterator it; 00059 it = base_list.begin(); 00060 for (; it!= base_list.end(); it++) { 00061 _object_list.push_back(*it); 00062 } 00063 } 00064 } 00065 00066 void MbedCloudClient::remove_object(M2MBase *object) 00067 { 00068 M2MBaseList::const_iterator it; 00069 int found_index = -1; 00070 int index; 00071 tr_debug("MbedCloudClient::remove_object %p", object); 00072 for (it = _object_list.begin(), index = 0; it != _object_list.end(); it++, index++) { 00073 if(*it == object) { 00074 found_index = index; 00075 break; 00076 } 00077 } 00078 if(found_index != -1) { 00079 tr_debug(" object found at index %d", found_index); 00080 _object_list.erase(found_index); 00081 _client.connector_client().m2m_interface()->remove_object(object); 00082 } 00083 } 00084 00085 void MbedCloudClient::set_update_callback(MbedCloudClientCallback *callback) 00086 { 00087 _value_callback = callback; 00088 } 00089 00090 bool MbedCloudClient::setup(void* iface) 00091 { 00092 tr_debug("MbedCloudClient setup()"); 00093 // Add objects to list 00094 map<string, M2MObject*>::iterator it; 00095 for (it = _objects.begin(); it != _objects.end(); it++) 00096 { 00097 _object_list.push_back((M2MBase*)it->second); 00098 } 00099 _client.connector_client().m2m_interface()->set_platform_network_handler(iface); 00100 00101 _client.initialize_and_register(_object_list); 00102 return true; 00103 } 00104 00105 void MbedCloudClient::on_registered(void(*fn)(void)) 00106 { 00107 FP0<void> fp(fn); 00108 _on_registered = fp; 00109 } 00110 00111 00112 void MbedCloudClient::on_error(void(*fn)(int)) 00113 { 00114 _on_error = fn; 00115 } 00116 00117 00118 void MbedCloudClient::on_unregistered(void(*fn)(void)) 00119 { 00120 FP0<void> fp(fn); 00121 _on_unregistered = fp; 00122 } 00123 00124 void MbedCloudClient::on_registration_updated(void(*fn)(void)) 00125 { 00126 FP0<void> fp(fn); 00127 _on_registration_updated = fp; 00128 } 00129 00130 void MbedCloudClient::keep_alive() 00131 { 00132 _client.connector_client().update_registration(); 00133 } 00134 00135 void MbedCloudClient::register_update() 00136 { 00137 _client.connector_client().update_registration(); 00138 } 00139 00140 void MbedCloudClient::close() 00141 { 00142 _client.connector_client().m2m_interface()->unregister_object(NULL); 00143 } 00144 00145 const ConnectorClientEndpointInfo *MbedCloudClient::endpoint_info() const 00146 { 00147 return _client.connector_client().endpoint_info(); 00148 } 00149 00150 void MbedCloudClient::set_queue_sleep_handler(callback_handler handler) 00151 { 00152 _client.connector_client().m2m_interface()->set_queue_sleep_handler(handler); 00153 } 00154 00155 void MbedCloudClient::set_random_number_callback(random_number_cb callback) 00156 { 00157 _client.connector_client().m2m_interface()->set_random_number_callback(callback); 00158 } 00159 00160 void MbedCloudClient::set_entropy_callback(entropy_cb callback) 00161 { 00162 _client.connector_client().m2m_interface()->set_entropy_callback(callback); 00163 } 00164 00165 bool MbedCloudClient::set_device_resource_value(M2MDevice::DeviceResource resource, 00166 const std::string &value) 00167 { 00168 return _client.set_device_resource_value(resource, value); 00169 } 00170 00171 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE 00172 void MbedCloudClient::set_update_authorize_handler(void (*handler)(int32_t request)) 00173 { 00174 _client.set_update_authorize_handler(handler); 00175 } 00176 00177 void MbedCloudClient::set_update_progress_handler(void (*handler)(uint32_t progress, uint32_t total)) 00178 { 00179 _client.set_update_progress_handler(handler); 00180 } 00181 00182 void MbedCloudClient::update_authorize(int32_t request) 00183 { 00184 _client.update_authorize(request); 00185 } 00186 #endif 00187 00188 const char *MbedCloudClient::error_description() const 00189 { 00190 return _error_description; 00191 } 00192 00193 00194 void MbedCloudClient::register_update_callback(string route, 00195 SimpleM2MResourceBase* resource) 00196 { 00197 _update_values[route] = resource; 00198 } 00199 00200 void MbedCloudClient::complete(ServiceClientCallbackStatus status) 00201 { 00202 tr_info("MbedCloudClient::complete status (%d)", status); 00203 if (status == Service_Client_Status_Registered) { 00204 _on_registered.call(); 00205 } else if (status == Service_Client_Status_Unregistered) { 00206 _object_list.clear(); 00207 _on_unregistered.call(); 00208 } else if (status == Service_Client_Status_Register_Updated) { 00209 _on_registration_updated.call(); 00210 } 00211 } 00212 00213 void MbedCloudClient::error(int error, const char *reason) 00214 { 00215 tr_error("MbedCloudClient::error code (%d)", error); 00216 _error_description = reason; 00217 _on_error(error); 00218 } 00219 00220 void MbedCloudClient::value_updated(M2MBase *base, M2MBase::BaseType type) 00221 { 00222 if (base) { 00223 tr_info("MbedCloudClient::value_updated path %s", base->uri_path()); 00224 if (base->uri_path()) { 00225 if (_update_values.count(base->uri_path()) != 0) { 00226 tr_debug("MbedCloudClient::value_updated calling update() for %s", base->uri_path()); 00227 _update_values[base->uri_path()]->update(); 00228 } else { 00229 // way to tell application that there is a value update 00230 if (_value_callback) { 00231 _value_callback->value_updated(base, type); 00232 } 00233 } 00234 } 00235 } 00236 } 00237 00238 void MbedCloudClient::send_get_request(const char *uri, 00239 const size_t offset, 00240 get_data_cb data_cb, 00241 get_data_error_cb error_cb, 00242 void *context) 00243 { 00244 _client.connector_client().m2m_interface()->get_data_request(uri, 00245 offset, 00246 true, 00247 data_cb, 00248 error_cb, 00249 context); 00250 }
Generated on Tue Jul 12 2022 16:24:17 by
1.7.2