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.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
m2mbase.cpp
00001 /* 00002 * Copyright (c) 2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "mbed-client/m2mbase.h" 00017 #include "mbed-client/m2mobservationhandler.h" 00018 #include "mbed-client/m2mconstants.h" 00019 #include "mbed-client/m2mtimer.h" 00020 #include "include/m2mreporthandler.h" 00021 #include "include/nsdllinker.h" 00022 #include "mbed-trace/mbed_trace.h" 00023 #include <assert.h> 00024 #include <ctype.h> 00025 #include <string.h> 00026 00027 #define TRACE_GROUP "mClt" 00028 00029 M2MBase& M2MBase::operator=(const M2MBase& other) 00030 { 00031 if (this != &other) { // protect against invalid self-assignment 00032 _operation = other._operation; 00033 _mode = other._mode; 00034 _name = other._name; 00035 _resource_type = other._resource_type; 00036 _interface_description = other._interface_description; 00037 _coap_content_type = other._coap_content_type; 00038 _instance_id = other._instance_id; 00039 _observable = other._observable; 00040 _observation_number = other._observation_number; 00041 _observation_level = other._observation_level; 00042 _observation_handler = other._observation_handler; 00043 _register_uri = other._register_uri; 00044 _uri_path = other._uri_path; 00045 _max_age = other._max_age; 00046 _is_under_observation = other._is_under_observation; 00047 00048 free(_token); 00049 _token = NULL; 00050 00051 _token_length = other._token_length; 00052 if(other._token) { 00053 _token = alloc_string_copy(other._token, other._token_length); 00054 } 00055 00056 delete _report_handler; 00057 _report_handler = NULL; 00058 00059 if(other._report_handler) { 00060 _report_handler = new M2MReportHandler(*other._report_handler); 00061 } 00062 } 00063 return *this; 00064 } 00065 00066 M2MBase::M2MBase(const M2MBase& other) : 00067 _report_handler(NULL), 00068 _observation_handler(other._observation_handler), 00069 _name(other._name), 00070 _resource_type(other._resource_type), 00071 _interface_description(other._interface_description), 00072 _uri_path(other._uri_path), 00073 _max_age(other._max_age), 00074 _instance_id(other._instance_id), 00075 _observation_number(other._observation_number), 00076 _token(NULL), 00077 _token_length(other._token_length), 00078 _coap_content_type(other._coap_content_type), 00079 _operation(other._operation), 00080 _mode(other._mode), 00081 _observation_level(other._observation_level), 00082 _observable(other._observable), 00083 _register_uri(other._register_uri), 00084 _is_under_observation(other._is_under_observation), 00085 _function_pointer(NULL) 00086 { 00087 00088 if(other._token) { 00089 _token = alloc_string_copy((uint8_t *)other._token, other._token_length); 00090 } 00091 00092 if(other._report_handler) { 00093 _report_handler = new M2MReportHandler(*other._report_handler); 00094 } 00095 } 00096 00097 M2MBase::M2MBase(const String & resource_name, 00098 M2MBase::Mode mde) 00099 : _report_handler(NULL), 00100 _observation_handler(NULL), 00101 _name(resource_name), 00102 _uri_path(""), 00103 _max_age(0), 00104 _instance_id(0), 00105 _observation_number(0), 00106 _token(NULL), 00107 _token_length(0), 00108 _coap_content_type(0), 00109 _operation(M2MBase::NOT_ALLOWED), 00110 _mode(mde), 00111 _observation_level(M2MBase::None), 00112 _observable(false), 00113 _register_uri(true), 00114 _is_under_observation(false), 00115 _function_pointer(NULL) 00116 { 00117 if(is_integer(_name) && _name.size() <= MAX_ALLOWED_STRING_LENGTH) { 00118 _name_id = strtoul(_name.c_str(), NULL, 10); 00119 if(_name_id > 65535){ 00120 _name_id = -1; 00121 } 00122 } else { 00123 _name_id = -1; 00124 } 00125 } 00126 00127 M2MBase::~M2MBase() 00128 { 00129 delete _report_handler; 00130 free(_token); 00131 delete _function_pointer; 00132 } 00133 00134 void M2MBase::set_operation(M2MBase::Operation opr) 00135 { 00136 // If the mode is Static, there is only GET_ALLOWED 00137 // supported. 00138 if(M2MBase::Static == _mode) { 00139 _operation = M2MBase::GET_ALLOWED; 00140 } else { 00141 _operation = opr; 00142 } 00143 } 00144 00145 void M2MBase::set_interface_description(const String &desc) 00146 { 00147 _interface_description = desc; 00148 } 00149 00150 void M2MBase::set_resource_type(const String &res_type) 00151 { 00152 _resource_type = res_type; 00153 } 00154 00155 void M2MBase::set_coap_content_type(const uint8_t con_type) 00156 { 00157 _coap_content_type = con_type; 00158 } 00159 00160 void M2MBase::set_observable(bool observable) 00161 { 00162 _observable = observable; 00163 } 00164 00165 void M2MBase::add_observation_level(M2MBase::Observation obs_level) 00166 { 00167 _observation_level = (M2MBase::Observation)(_observation_level | obs_level); 00168 } 00169 00170 void M2MBase::remove_observation_level(M2MBase::Observation obs_level) 00171 { 00172 _observation_level = (M2MBase::Observation)(_observation_level & ~obs_level); 00173 } 00174 00175 void M2MBase::set_under_observation(bool observed, 00176 M2MObservationHandler *handler) 00177 { 00178 00179 tr_debug("M2MBase::set_under_observation - observed: %d", observed); 00180 tr_debug("M2MBase::set_under_observation - base_type: %d", _base_type); 00181 _is_under_observation = observed; 00182 _observation_handler = handler; 00183 if(handler) { 00184 if (_base_type != M2MBase::ResourceInstance) { 00185 if(!_report_handler){ 00186 _report_handler = new M2MReportHandler(*this); 00187 } 00188 _report_handler->set_under_observation(observed); 00189 } 00190 } else { 00191 delete _report_handler; 00192 _report_handler = NULL; 00193 } 00194 } 00195 00196 void M2MBase::set_observation_token(const uint8_t *token, const uint8_t length) 00197 { 00198 free(_token); 00199 _token = NULL; 00200 _token_length = 0; 00201 00202 if( token != NULL && length > 0 ) { 00203 _token = alloc_string_copy((uint8_t *)token, length); 00204 if(_token) { 00205 _token_length = length; 00206 } 00207 } 00208 } 00209 00210 void M2MBase::set_instance_id(const uint16_t inst_id) 00211 { 00212 _instance_id = inst_id; 00213 } 00214 00215 00216 void M2MBase::set_observation_number(const uint16_t /*observation_number*/) 00217 { 00218 } 00219 00220 void M2MBase::set_max_age(const uint32_t max_age) 00221 { 00222 _max_age = max_age; 00223 } 00224 00225 M2MBase::BaseType M2MBase::base_type() const 00226 { 00227 return _base_type; 00228 } 00229 00230 M2MBase::Operation M2MBase::operation() const 00231 { 00232 return _operation; 00233 } 00234 00235 const String& M2MBase::name() const 00236 { 00237 return _name; 00238 } 00239 00240 int32_t M2MBase::name_id() const 00241 { 00242 return _name_id; 00243 } 00244 00245 uint16_t M2MBase::instance_id() const 00246 { 00247 return _instance_id; 00248 } 00249 00250 const String& M2MBase::interface_description() const 00251 { 00252 return _interface_description; 00253 } 00254 00255 const String& M2MBase::resource_type() const 00256 { 00257 return _resource_type; 00258 } 00259 00260 uint8_t M2MBase::coap_content_type() const 00261 { 00262 return _coap_content_type; 00263 } 00264 00265 bool M2MBase::is_observable() const 00266 { 00267 return _observable; 00268 } 00269 00270 M2MBase::Observation M2MBase::observation_level() const 00271 { 00272 return _observation_level; 00273 } 00274 00275 void M2MBase::get_observation_token(uint8_t *&token, uint32_t &token_length) 00276 { 00277 token_length = 0; 00278 free(token); 00279 00280 token = alloc_string_copy((uint8_t *)_token, _token_length); 00281 if(token) { 00282 token_length = _token_length; 00283 } 00284 } 00285 00286 M2MBase::Mode M2MBase::mode() const 00287 { 00288 return _mode; 00289 } 00290 00291 uint16_t M2MBase::observation_number() const 00292 { 00293 return _observation_number; 00294 } 00295 00296 uint32_t M2MBase::max_age() const 00297 { 00298 return _max_age; 00299 } 00300 00301 bool M2MBase::handle_observation_attribute(char *&query) 00302 { 00303 tr_debug("M2MBase::handle_observation_attribute - under observation(%d)", is_under_observation()); 00304 bool success = false; 00305 if(_report_handler) { 00306 success = _report_handler->parse_notification_attribute(query,_base_type); 00307 if (success) { 00308 if (is_under_observation()) { 00309 _report_handler->set_under_observation(true); 00310 } 00311 } else { 00312 _report_handler->set_default_values(); 00313 } 00314 } 00315 return success; 00316 } 00317 00318 void M2MBase::observation_to_be_sent(m2m::Vector<uint16_t> changed_instance_ids, bool send_object) 00319 { 00320 //TODO: Move this to M2MResourceInstance 00321 if(_observation_handler) { 00322 _observation_number++; 00323 _observation_handler->observation_to_be_sent(this, 00324 _observation_number, 00325 changed_instance_ids, 00326 send_object); 00327 } 00328 } 00329 00330 void M2MBase::set_base_type(M2MBase::BaseType type) 00331 { 00332 _base_type = type; 00333 } 00334 00335 void M2MBase::remove_resource_from_coap(const String &resource_name) 00336 { 00337 if(_observation_handler) { 00338 _observation_handler->resource_to_be_deleted(resource_name); 00339 } 00340 } 00341 00342 void M2MBase::remove_object_from_coap() 00343 { 00344 if(_observation_handler) { 00345 _observation_handler->remove_object(this); 00346 } 00347 } 00348 00349 sn_coap_hdr_s* M2MBase::handle_get_request(nsdl_s */*nsdl*/, 00350 sn_coap_hdr_s */*received_coap_header*/, 00351 M2MObservationHandler */*observation_handler*/) 00352 { 00353 //Handled in M2MResource, M2MObjectInstance and M2MObject classes 00354 return NULL; 00355 } 00356 00357 sn_coap_hdr_s* M2MBase::handle_put_request(nsdl_s */*nsdl*/, 00358 sn_coap_hdr_s */*received_coap_header*/, 00359 M2MObservationHandler */*observation_handler*/, 00360 bool &) 00361 { 00362 //Handled in M2MResource, M2MObjectInstance and M2MObject classes 00363 return NULL; 00364 } 00365 00366 sn_coap_hdr_s* M2MBase::handle_post_request(nsdl_s */*nsdl*/, 00367 sn_coap_hdr_s */*received_coap_header*/, 00368 M2MObservationHandler */*observation_handler*/, 00369 bool &) 00370 { 00371 //Handled in M2MResource, M2MObjectInstance and M2MObject classes 00372 return NULL; 00373 } 00374 00375 void *M2MBase::memory_alloc(uint32_t size) 00376 { 00377 if(size) 00378 return malloc(size); 00379 else 00380 return 0; 00381 } 00382 00383 void M2MBase::memory_free(void *ptr) 00384 { 00385 free(ptr); 00386 } 00387 00388 uint8_t* M2MBase::alloc_string_copy(const uint8_t* source, uint32_t size) 00389 { 00390 assert(source != NULL); 00391 00392 uint8_t* result = (uint8_t*)memory_alloc(size + 1); 00393 if (result) { 00394 memcpy(result, source, size); 00395 result[size] = '\0'; 00396 } 00397 return result; 00398 } 00399 00400 uint8_t* M2MBase::alloc_copy(const uint8_t* source, uint32_t size) 00401 { 00402 assert(source != NULL); 00403 00404 uint8_t* result = (uint8_t*)memory_alloc(size); 00405 if (result) { 00406 memcpy(result, source, size); 00407 } 00408 return result; 00409 } 00410 00411 M2MReportHandler* M2MBase::report_handler() 00412 { 00413 return _report_handler; 00414 } 00415 00416 M2MObservationHandler* M2MBase::observation_handler() 00417 { 00418 return _observation_handler; 00419 } 00420 00421 void M2MBase::set_register_uri( bool register_uri) 00422 { 00423 _register_uri = register_uri; 00424 } 00425 00426 bool M2MBase::register_uri() 00427 { 00428 return _register_uri; 00429 } 00430 00431 bool M2MBase::is_integer(const String &value) 00432 { 00433 const char *s = value.c_str(); 00434 if(value.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) { 00435 return false; 00436 } 00437 char * p ; 00438 strtol(value.c_str(), &p, 10); 00439 return (*p == 0); 00440 } 00441 00442 void M2MBase::set_uri_path(const String &path) 00443 { 00444 _uri_path = path; 00445 } 00446 00447 const String& M2MBase::uri_path() const 00448 { 00449 return _uri_path; 00450 } 00451 00452 bool M2MBase::is_under_observation() const 00453 { 00454 return _is_under_observation; 00455 } 00456 00457 void M2MBase::set_value_updated_function(value_updated_callback callback) 00458 { 00459 _value_updated_callback = callback; 00460 } 00461 00462 void M2MBase::set_value_updated_function(value_updated_callback2 callback) 00463 { 00464 delete _function_pointer; 00465 _function_pointer = new FP1<void, const char*> (callback); 00466 set_value_updated_function(value_updated_callback (_function_pointer, 00467 &FP1<void, const char*>::call)); 00468 } 00469 bool M2MBase::is_value_updated_function_set() 00470 { 00471 return (_value_updated_callback) ? true : false; 00472 } 00473 00474 void M2MBase::execute_value_updated(const String& name) 00475 { 00476 if(_value_updated_callback) { 00477 _value_updated_callback(name.c_str()); 00478 } 00479 }
Generated on Tue Jul 12 2022 12:28:36 by
