sandbox / mbed-client

Fork of mbed-client by Christopher Haster

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mbase.cpp Source File

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