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: mbed Socket lwip-eth lwip-sys lwip
Fork of 6_songs-from-the-cloud 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 "ns_trace.h" 00023 #include <ctype.h> 00024 #include <string.h> 00025 00026 M2MBase& M2MBase::operator=(const M2MBase& other) 00027 { 00028 if (this != &other) { // protect against invalid self-assignment 00029 _operation = other._operation; 00030 _mode = other._mode; 00031 _name = other._name; 00032 _resource_type = other._resource_type; 00033 _interface_description = other._interface_description; 00034 _coap_content_type = other._coap_content_type; 00035 _instance_id = other._instance_id; 00036 _observable = other._observable; 00037 _observation_number = other._observation_number; 00038 _observation_level = other._observation_level; 00039 _observation_handler = other._observation_handler; 00040 00041 if(_token) { 00042 free(_token); 00043 _token = NULL; 00044 _token_length = 0; 00045 } 00046 _token_length = other._token_length; 00047 if(other._token) { 00048 _token = (uint8_t *)malloc(other._token_length+1); 00049 if(_token) { 00050 memset(_token, 0, other._token_length+1); 00051 memcpy((uint8_t *)_token, (uint8_t *)other._token, other._token_length); 00052 } 00053 } 00054 00055 if(_report_handler) { 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 _token(NULL), 00069 _token_length(0) 00070 { 00071 _operation = other._operation; 00072 _mode = other._mode; 00073 _name = other._name; 00074 _resource_type = other._resource_type; 00075 _interface_description = other._interface_description; 00076 _coap_content_type = other._coap_content_type; 00077 _instance_id = other._instance_id; 00078 _observable = other._observable; 00079 _observation_handler = other._observation_handler; 00080 _observation_number = other._observation_number; 00081 _observation_level = other._observation_level; 00082 00083 _token_length = other._token_length; 00084 if(other._token) { 00085 _token = (uint8_t *)malloc(other._token_length+1); 00086 if(_token) { 00087 memset(_token, 0, other._token_length+1); 00088 memcpy((uint8_t *)_token, (uint8_t *)other._token, other._token_length); 00089 } 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 _operation(M2MBase::NOT_ALLOWED), 00102 _mode(mde), 00103 _observation_level(M2MBase::None), 00104 _name(resource_name), 00105 _coap_content_type(0), 00106 _instance_id(0), 00107 _observable(false), 00108 _observation_number(0), 00109 _token(NULL), 00110 _token_length(0) 00111 { 00112 if(is_integer(_name) && _name.size() <= MAX_ALLOWED_STRING_LENGTH) { 00113 _name_id = strtoul(_name.c_str(), NULL, 10); 00114 if(_name_id > 65535){ 00115 _name_id = -1; 00116 } 00117 } else { 00118 _name_id = -1; 00119 } 00120 } 00121 00122 M2MBase::~M2MBase() 00123 { 00124 if(_report_handler) { 00125 delete _report_handler; 00126 _report_handler = NULL; 00127 } 00128 if(_token) { 00129 free(_token); 00130 _token = NULL; 00131 _token_length = 0; 00132 } 00133 } 00134 00135 void M2MBase::set_operation(M2MBase::Operation opr) 00136 { 00137 // If the mode is Static, there is only GET_ALLOWED 00138 // supported. 00139 if(M2MBase::Static == _mode) { 00140 _operation = M2MBase::GET_ALLOWED; 00141 } else { 00142 _operation = opr; 00143 } 00144 } 00145 00146 void M2MBase::set_interface_description(const String &desc) 00147 { 00148 _interface_description = desc; 00149 } 00150 00151 void M2MBase::set_resource_type(const String &res_type) 00152 { 00153 _resource_type = res_type; 00154 } 00155 00156 void M2MBase::set_coap_content_type(const uint8_t con_type) 00157 { 00158 _coap_content_type = con_type; 00159 } 00160 00161 void M2MBase::set_observable(bool observable) 00162 { 00163 _observable = observable; 00164 } 00165 00166 void M2MBase::add_observation_level(M2MBase::Observation observation_level) 00167 { 00168 _observation_level = (M2MBase::Observation)(_observation_level | observation_level); 00169 } 00170 00171 void M2MBase::remove_observation_level(M2MBase::Observation observation_level) 00172 { 00173 _observation_level = (M2MBase::Observation)(_observation_level ^ observation_level); 00174 } 00175 00176 void M2MBase::set_under_observation(bool observed, 00177 M2MObservationHandler *handler) 00178 { 00179 00180 tr_debug("M2MBase::set_under_observation - observed: %d", observed); 00181 tr_debug("M2MBase::set_under_observation - _base_type: %d", _base_type); 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 if(_report_handler) { 00192 delete _report_handler; 00193 _report_handler = NULL; 00194 } 00195 } 00196 } 00197 00198 void M2MBase::set_observation_token(const uint8_t *token, const uint8_t length) 00199 { 00200 if(_token) { 00201 free(_token); 00202 _token = NULL; 00203 _token_length = 0; 00204 } 00205 00206 if( token != NULL && length > 0 ) { 00207 _token = (uint8_t *)malloc(length+1); 00208 if(_token) { 00209 memset(_token, 0, length+1); 00210 memcpy((uint8_t *)_token, (uint8_t *)token, length); 00211 _token_length = length; 00212 } 00213 } 00214 } 00215 00216 void M2MBase::set_instance_id(const uint16_t inst_id) 00217 { 00218 _instance_id = inst_id; 00219 } 00220 00221 void M2MBase::set_observation_number(const uint16_t observation_number) 00222 { 00223 _observation_number = observation_number; 00224 } 00225 00226 M2MBase::BaseType M2MBase::base_type() const 00227 { 00228 return _base_type; 00229 } 00230 00231 M2MBase::Operation M2MBase::operation() const 00232 { 00233 return _operation; 00234 } 00235 00236 const String& M2MBase::name() const 00237 { 00238 return _name; 00239 } 00240 00241 int32_t M2MBase::name_id() const 00242 { 00243 return _name_id; 00244 } 00245 00246 uint16_t M2MBase::instance_id() const 00247 { 00248 return _instance_id; 00249 } 00250 00251 const String& M2MBase::interface_description() const 00252 { 00253 return _interface_description; 00254 } 00255 00256 const String& M2MBase::resource_type() const 00257 { 00258 return _resource_type; 00259 } 00260 00261 uint8_t M2MBase::coap_content_type() const 00262 { 00263 return _coap_content_type; 00264 } 00265 00266 bool M2MBase::is_observable() const 00267 { 00268 return _observable; 00269 } 00270 00271 M2MBase::Observation M2MBase::observation_level() const 00272 { 00273 return _observation_level; 00274 } 00275 00276 void M2MBase::get_observation_token(uint8_t *&token, uint32_t &token_length) 00277 { 00278 token_length = 0; 00279 if(token) { 00280 free(token); 00281 token = NULL; 00282 } 00283 token = (uint8_t *)malloc(_token_length+1); 00284 if(token) { 00285 token_length = _token_length; 00286 memset(token, 0, _token_length+1); 00287 memcpy((uint8_t *)token, (uint8_t *)_token, token_length); 00288 } 00289 } 00290 00291 M2MBase::Mode M2MBase::mode() const 00292 { 00293 return _mode; 00294 } 00295 00296 uint16_t M2MBase::observation_number() const 00297 { 00298 return _observation_number; 00299 } 00300 00301 bool M2MBase::handle_observation_attribute(char *&query) 00302 { 00303 tr_debug("M2MBase::handle_observation_attribute"); 00304 bool success = false; 00305 if(_report_handler) { 00306 success = _report_handler->parse_notification_attribute(query,_base_type); 00307 if (success) { 00308 if ((_report_handler->attribute_flags() & M2MReportHandler::Cancel) == 0) { 00309 _report_handler->set_under_observation(true); 00310 } else { 00311 _report_handler->set_under_observation(false); 00312 } 00313 } 00314 } 00315 return success; 00316 } 00317 00318 void M2MBase::observation_to_be_sent() 00319 { 00320 //TODO: Move this to M2MResourceInstance 00321 if(_observation_handler) { 00322 _observation_handler->observation_to_be_sent(this); 00323 } 00324 } 00325 00326 void M2MBase::set_base_type(M2MBase::BaseType type) 00327 { 00328 _base_type = type; 00329 } 00330 00331 void M2MBase::remove_resource_from_coap(const String &resource_name) 00332 { 00333 if(_observation_handler) { 00334 _observation_handler->resource_to_be_deleted(resource_name); 00335 } 00336 } 00337 00338 void M2MBase::remove_object_from_coap() 00339 { 00340 if(_observation_handler) { 00341 _observation_handler->remove_object(this); 00342 } 00343 } 00344 00345 sn_coap_hdr_s* M2MBase::handle_get_request(nsdl_s */*nsdl*/, 00346 sn_coap_hdr_s */*received_coap_header*/, 00347 M2MObservationHandler */*observation_handler*/) 00348 { 00349 //Handled in M2MResource, M2MObjectInstance and M2MObject classes 00350 return NULL; 00351 } 00352 00353 sn_coap_hdr_s* M2MBase::handle_put_request(nsdl_s */*nsdl*/, 00354 sn_coap_hdr_s */*received_coap_header*/, 00355 M2MObservationHandler */*observation_handler*/) 00356 { 00357 //Handled in M2MResource, M2MObjectInstance and M2MObject classes 00358 return NULL; 00359 } 00360 00361 sn_coap_hdr_s* M2MBase::handle_post_request(nsdl_s */*nsdl*/, 00362 sn_coap_hdr_s */*received_coap_header*/, 00363 M2MObservationHandler */*observation_handler*/) 00364 { 00365 //Handled in M2MResource, M2MObjectInstance and M2MObject classes 00366 return NULL; 00367 } 00368 00369 void *M2MBase::memory_alloc(uint16_t size) 00370 { 00371 if(size) 00372 return malloc(size); 00373 else 00374 return 0; 00375 } 00376 00377 void M2MBase::memory_free(void *ptr) 00378 { 00379 if(ptr) 00380 free(ptr); 00381 } 00382 00383 M2MReportHandler* M2MBase::report_handler() 00384 { 00385 return _report_handler; 00386 } 00387 00388 M2MObservationHandler* M2MBase::observation_handler() 00389 { 00390 return _observation_handler; 00391 } 00392 00393 bool M2MBase::is_integer(const String &value) 00394 { 00395 const char *s = value.c_str(); 00396 if(value.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) { 00397 return false; 00398 } 00399 char * p ; 00400 strtol(value.c_str(), &p, 10); 00401 return (*p == 0); 00402 }
Generated on Tue Jul 12 2022 12:47:47 by
1.7.2
