Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the License); you may
nexpaq 0:6c56fb4bc5f0 5 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16 #include "mbed-client/m2mbase.h"
nexpaq 0:6c56fb4bc5f0 17 #include "mbed-client/m2mobservationhandler.h"
nexpaq 0:6c56fb4bc5f0 18 #include "mbed-client/m2mconstants.h"
nexpaq 0:6c56fb4bc5f0 19 #include "mbed-client/m2mtimer.h"
nexpaq 0:6c56fb4bc5f0 20 #include "include/m2mreporthandler.h"
nexpaq 0:6c56fb4bc5f0 21 #include "include/nsdllinker.h"
nexpaq 0:6c56fb4bc5f0 22 #include "mbed-trace/mbed_trace.h"
nexpaq 0:6c56fb4bc5f0 23 #include <assert.h>
nexpaq 0:6c56fb4bc5f0 24 #include <ctype.h>
nexpaq 0:6c56fb4bc5f0 25 #include <string.h>
nexpaq 0:6c56fb4bc5f0 26
nexpaq 0:6c56fb4bc5f0 27 #define TRACE_GROUP "mClt"
nexpaq 0:6c56fb4bc5f0 28
nexpaq 0:6c56fb4bc5f0 29 M2MBase& M2MBase::operator=(const M2MBase& other)
nexpaq 0:6c56fb4bc5f0 30 {
nexpaq 0:6c56fb4bc5f0 31 if (this != &other) { // protect against invalid self-assignment
nexpaq 0:6c56fb4bc5f0 32 _operation = other._operation;
nexpaq 0:6c56fb4bc5f0 33 _mode = other._mode;
nexpaq 0:6c56fb4bc5f0 34 _name = other._name;
nexpaq 0:6c56fb4bc5f0 35 _resource_type = other._resource_type;
nexpaq 0:6c56fb4bc5f0 36 _interface_description = other._interface_description;
nexpaq 0:6c56fb4bc5f0 37 _coap_content_type = other._coap_content_type;
nexpaq 0:6c56fb4bc5f0 38 _instance_id = other._instance_id;
nexpaq 0:6c56fb4bc5f0 39 _observable = other._observable;
nexpaq 0:6c56fb4bc5f0 40 _observation_number = other._observation_number;
nexpaq 0:6c56fb4bc5f0 41 _observation_level = other._observation_level;
nexpaq 0:6c56fb4bc5f0 42 _observation_handler = other._observation_handler;
nexpaq 0:6c56fb4bc5f0 43 _register_uri = other._register_uri;
nexpaq 0:6c56fb4bc5f0 44 _uri_path = other._uri_path;
nexpaq 0:6c56fb4bc5f0 45 _max_age = other._max_age;
nexpaq 0:6c56fb4bc5f0 46 _is_under_observation = other._is_under_observation;
nexpaq 0:6c56fb4bc5f0 47
nexpaq 0:6c56fb4bc5f0 48 free(_token);
nexpaq 0:6c56fb4bc5f0 49 _token = NULL;
nexpaq 0:6c56fb4bc5f0 50
nexpaq 0:6c56fb4bc5f0 51 _token_length = other._token_length;
nexpaq 0:6c56fb4bc5f0 52 if(other._token) {
nexpaq 0:6c56fb4bc5f0 53 _token = alloc_string_copy(other._token, other._token_length);
nexpaq 0:6c56fb4bc5f0 54 }
nexpaq 0:6c56fb4bc5f0 55
nexpaq 0:6c56fb4bc5f0 56 delete _report_handler;
nexpaq 0:6c56fb4bc5f0 57 _report_handler = NULL;
nexpaq 0:6c56fb4bc5f0 58
nexpaq 0:6c56fb4bc5f0 59 if(other._report_handler) {
nexpaq 0:6c56fb4bc5f0 60 _report_handler = new M2MReportHandler(*other._report_handler);
nexpaq 0:6c56fb4bc5f0 61 }
nexpaq 0:6c56fb4bc5f0 62 }
nexpaq 0:6c56fb4bc5f0 63 return *this;
nexpaq 0:6c56fb4bc5f0 64 }
nexpaq 0:6c56fb4bc5f0 65
nexpaq 0:6c56fb4bc5f0 66 M2MBase::M2MBase(const M2MBase& other) :
nexpaq 0:6c56fb4bc5f0 67 _report_handler(NULL),
nexpaq 0:6c56fb4bc5f0 68 _observation_handler(other._observation_handler),
nexpaq 0:6c56fb4bc5f0 69 _name(other._name),
nexpaq 0:6c56fb4bc5f0 70 _resource_type(other._resource_type),
nexpaq 0:6c56fb4bc5f0 71 _interface_description(other._interface_description),
nexpaq 0:6c56fb4bc5f0 72 _uri_path(other._uri_path),
nexpaq 0:6c56fb4bc5f0 73 _max_age(other._max_age),
nexpaq 0:6c56fb4bc5f0 74 _instance_id(other._instance_id),
nexpaq 0:6c56fb4bc5f0 75 _observation_number(other._observation_number),
nexpaq 0:6c56fb4bc5f0 76 _token(NULL),
nexpaq 0:6c56fb4bc5f0 77 _token_length(other._token_length),
nexpaq 0:6c56fb4bc5f0 78 _coap_content_type(other._coap_content_type),
nexpaq 0:6c56fb4bc5f0 79 _operation(other._operation),
nexpaq 0:6c56fb4bc5f0 80 _mode(other._mode),
nexpaq 0:6c56fb4bc5f0 81 _observation_level(other._observation_level),
nexpaq 0:6c56fb4bc5f0 82 _observable(other._observable),
nexpaq 0:6c56fb4bc5f0 83 _register_uri(other._register_uri),
nexpaq 0:6c56fb4bc5f0 84 _is_under_observation(other._is_under_observation),
nexpaq 0:6c56fb4bc5f0 85 _function_pointer(NULL)
nexpaq 0:6c56fb4bc5f0 86 {
nexpaq 0:6c56fb4bc5f0 87
nexpaq 0:6c56fb4bc5f0 88 if(other._token) {
nexpaq 0:6c56fb4bc5f0 89 _token = alloc_string_copy((uint8_t *)other._token, other._token_length);
nexpaq 0:6c56fb4bc5f0 90 }
nexpaq 0:6c56fb4bc5f0 91
nexpaq 0:6c56fb4bc5f0 92 if(other._report_handler) {
nexpaq 0:6c56fb4bc5f0 93 _report_handler = new M2MReportHandler(*other._report_handler);
nexpaq 0:6c56fb4bc5f0 94 }
nexpaq 0:6c56fb4bc5f0 95 }
nexpaq 0:6c56fb4bc5f0 96
nexpaq 0:6c56fb4bc5f0 97 M2MBase::M2MBase(const String & resource_name,
nexpaq 0:6c56fb4bc5f0 98 M2MBase::Mode mde)
nexpaq 0:6c56fb4bc5f0 99 : _report_handler(NULL),
nexpaq 0:6c56fb4bc5f0 100 _observation_handler(NULL),
nexpaq 0:6c56fb4bc5f0 101 _name(resource_name),
nexpaq 0:6c56fb4bc5f0 102 _uri_path(""),
nexpaq 0:6c56fb4bc5f0 103 _max_age(0),
nexpaq 0:6c56fb4bc5f0 104 _instance_id(0),
nexpaq 0:6c56fb4bc5f0 105 _observation_number(0),
nexpaq 0:6c56fb4bc5f0 106 _token(NULL),
nexpaq 0:6c56fb4bc5f0 107 _token_length(0),
nexpaq 0:6c56fb4bc5f0 108 _coap_content_type(0),
nexpaq 0:6c56fb4bc5f0 109 _operation(M2MBase::NOT_ALLOWED),
nexpaq 0:6c56fb4bc5f0 110 _mode(mde),
nexpaq 0:6c56fb4bc5f0 111 _observation_level(M2MBase::None),
nexpaq 0:6c56fb4bc5f0 112 _observable(false),
nexpaq 0:6c56fb4bc5f0 113 _register_uri(true),
nexpaq 0:6c56fb4bc5f0 114 _is_under_observation(false),
nexpaq 0:6c56fb4bc5f0 115 _function_pointer(NULL)
nexpaq 0:6c56fb4bc5f0 116 {
nexpaq 0:6c56fb4bc5f0 117 if(is_integer(_name) && _name.size() <= MAX_ALLOWED_STRING_LENGTH) {
nexpaq 0:6c56fb4bc5f0 118 _name_id = strtoul(_name.c_str(), NULL, 10);
nexpaq 0:6c56fb4bc5f0 119 if(_name_id > 65535){
nexpaq 0:6c56fb4bc5f0 120 _name_id = -1;
nexpaq 0:6c56fb4bc5f0 121 }
nexpaq 0:6c56fb4bc5f0 122 } else {
nexpaq 0:6c56fb4bc5f0 123 _name_id = -1;
nexpaq 0:6c56fb4bc5f0 124 }
nexpaq 0:6c56fb4bc5f0 125 }
nexpaq 0:6c56fb4bc5f0 126
nexpaq 0:6c56fb4bc5f0 127 M2MBase::~M2MBase()
nexpaq 0:6c56fb4bc5f0 128 {
nexpaq 0:6c56fb4bc5f0 129 delete _report_handler;
nexpaq 0:6c56fb4bc5f0 130 free(_token);
nexpaq 0:6c56fb4bc5f0 131 delete _function_pointer;
nexpaq 0:6c56fb4bc5f0 132 }
nexpaq 0:6c56fb4bc5f0 133
nexpaq 0:6c56fb4bc5f0 134 void M2MBase::set_operation(M2MBase::Operation opr)
nexpaq 0:6c56fb4bc5f0 135 {
nexpaq 0:6c56fb4bc5f0 136 // If the mode is Static, there is only GET_ALLOWED
nexpaq 0:6c56fb4bc5f0 137 // supported.
nexpaq 0:6c56fb4bc5f0 138 if(M2MBase::Static == _mode) {
nexpaq 0:6c56fb4bc5f0 139 _operation = M2MBase::GET_ALLOWED;
nexpaq 0:6c56fb4bc5f0 140 } else {
nexpaq 0:6c56fb4bc5f0 141 _operation = opr;
nexpaq 0:6c56fb4bc5f0 142 }
nexpaq 0:6c56fb4bc5f0 143 }
nexpaq 0:6c56fb4bc5f0 144
nexpaq 0:6c56fb4bc5f0 145 void M2MBase::set_interface_description(const String &desc)
nexpaq 0:6c56fb4bc5f0 146 {
nexpaq 0:6c56fb4bc5f0 147 _interface_description = desc;
nexpaq 0:6c56fb4bc5f0 148 }
nexpaq 0:6c56fb4bc5f0 149
nexpaq 0:6c56fb4bc5f0 150 void M2MBase::set_resource_type(const String &res_type)
nexpaq 0:6c56fb4bc5f0 151 {
nexpaq 0:6c56fb4bc5f0 152 _resource_type = res_type;
nexpaq 0:6c56fb4bc5f0 153 }
nexpaq 0:6c56fb4bc5f0 154
nexpaq 0:6c56fb4bc5f0 155 void M2MBase::set_coap_content_type(const uint8_t con_type)
nexpaq 0:6c56fb4bc5f0 156 {
nexpaq 0:6c56fb4bc5f0 157 _coap_content_type = con_type;
nexpaq 0:6c56fb4bc5f0 158 }
nexpaq 0:6c56fb4bc5f0 159
nexpaq 0:6c56fb4bc5f0 160 void M2MBase::set_observable(bool observable)
nexpaq 0:6c56fb4bc5f0 161 {
nexpaq 0:6c56fb4bc5f0 162 _observable = observable;
nexpaq 0:6c56fb4bc5f0 163 }
nexpaq 0:6c56fb4bc5f0 164
nexpaq 0:6c56fb4bc5f0 165 void M2MBase::add_observation_level(M2MBase::Observation obs_level)
nexpaq 0:6c56fb4bc5f0 166 {
nexpaq 0:6c56fb4bc5f0 167 _observation_level = (M2MBase::Observation)(_observation_level | obs_level);
nexpaq 0:6c56fb4bc5f0 168 }
nexpaq 0:6c56fb4bc5f0 169
nexpaq 0:6c56fb4bc5f0 170 void M2MBase::remove_observation_level(M2MBase::Observation obs_level)
nexpaq 0:6c56fb4bc5f0 171 {
nexpaq 0:6c56fb4bc5f0 172 _observation_level = (M2MBase::Observation)(_observation_level & ~obs_level);
nexpaq 0:6c56fb4bc5f0 173 }
nexpaq 0:6c56fb4bc5f0 174
nexpaq 0:6c56fb4bc5f0 175 void M2MBase::set_under_observation(bool observed,
nexpaq 0:6c56fb4bc5f0 176 M2MObservationHandler *handler)
nexpaq 0:6c56fb4bc5f0 177 {
nexpaq 0:6c56fb4bc5f0 178
nexpaq 0:6c56fb4bc5f0 179 tr_debug("M2MBase::set_under_observation - observed: %d", observed);
nexpaq 0:6c56fb4bc5f0 180 tr_debug("M2MBase::set_under_observation - base_type: %d", _base_type);
nexpaq 0:6c56fb4bc5f0 181 _is_under_observation = observed;
nexpaq 0:6c56fb4bc5f0 182 _observation_handler = handler;
nexpaq 0:6c56fb4bc5f0 183 if(handler) {
nexpaq 0:6c56fb4bc5f0 184 if (_base_type != M2MBase::ResourceInstance) {
nexpaq 0:6c56fb4bc5f0 185 if(!_report_handler){
nexpaq 0:6c56fb4bc5f0 186 _report_handler = new M2MReportHandler(*this);
nexpaq 0:6c56fb4bc5f0 187 }
nexpaq 0:6c56fb4bc5f0 188 _report_handler->set_under_observation(observed);
nexpaq 0:6c56fb4bc5f0 189 }
nexpaq 0:6c56fb4bc5f0 190 } else {
nexpaq 0:6c56fb4bc5f0 191 delete _report_handler;
nexpaq 0:6c56fb4bc5f0 192 _report_handler = NULL;
nexpaq 0:6c56fb4bc5f0 193 }
nexpaq 0:6c56fb4bc5f0 194 }
nexpaq 0:6c56fb4bc5f0 195
nexpaq 0:6c56fb4bc5f0 196 void M2MBase::set_observation_token(const uint8_t *token, const uint8_t length)
nexpaq 0:6c56fb4bc5f0 197 {
nexpaq 0:6c56fb4bc5f0 198 free(_token);
nexpaq 0:6c56fb4bc5f0 199 _token = NULL;
nexpaq 0:6c56fb4bc5f0 200 _token_length = 0;
nexpaq 0:6c56fb4bc5f0 201
nexpaq 0:6c56fb4bc5f0 202 if( token != NULL && length > 0 ) {
nexpaq 0:6c56fb4bc5f0 203 _token = alloc_string_copy((uint8_t *)token, length);
nexpaq 0:6c56fb4bc5f0 204 if(_token) {
nexpaq 0:6c56fb4bc5f0 205 _token_length = length;
nexpaq 0:6c56fb4bc5f0 206 }
nexpaq 0:6c56fb4bc5f0 207 }
nexpaq 0:6c56fb4bc5f0 208 }
nexpaq 0:6c56fb4bc5f0 209
nexpaq 0:6c56fb4bc5f0 210 void M2MBase::set_instance_id(const uint16_t inst_id)
nexpaq 0:6c56fb4bc5f0 211 {
nexpaq 0:6c56fb4bc5f0 212 _instance_id = inst_id;
nexpaq 0:6c56fb4bc5f0 213 }
nexpaq 0:6c56fb4bc5f0 214
nexpaq 0:6c56fb4bc5f0 215
nexpaq 0:6c56fb4bc5f0 216 void M2MBase::set_observation_number(const uint16_t /*observation_number*/)
nexpaq 0:6c56fb4bc5f0 217 {
nexpaq 0:6c56fb4bc5f0 218 }
nexpaq 0:6c56fb4bc5f0 219
nexpaq 0:6c56fb4bc5f0 220 void M2MBase::set_max_age(const uint32_t max_age)
nexpaq 0:6c56fb4bc5f0 221 {
nexpaq 0:6c56fb4bc5f0 222 _max_age = max_age;
nexpaq 0:6c56fb4bc5f0 223 }
nexpaq 0:6c56fb4bc5f0 224
nexpaq 0:6c56fb4bc5f0 225 M2MBase::BaseType M2MBase::base_type() const
nexpaq 0:6c56fb4bc5f0 226 {
nexpaq 0:6c56fb4bc5f0 227 return _base_type;
nexpaq 0:6c56fb4bc5f0 228 }
nexpaq 0:6c56fb4bc5f0 229
nexpaq 0:6c56fb4bc5f0 230 M2MBase::Operation M2MBase::operation() const
nexpaq 0:6c56fb4bc5f0 231 {
nexpaq 0:6c56fb4bc5f0 232 return _operation;
nexpaq 0:6c56fb4bc5f0 233 }
nexpaq 0:6c56fb4bc5f0 234
nexpaq 0:6c56fb4bc5f0 235 const String& M2MBase::name() const
nexpaq 0:6c56fb4bc5f0 236 {
nexpaq 0:6c56fb4bc5f0 237 return _name;
nexpaq 0:6c56fb4bc5f0 238 }
nexpaq 0:6c56fb4bc5f0 239
nexpaq 0:6c56fb4bc5f0 240 int32_t M2MBase::name_id() const
nexpaq 0:6c56fb4bc5f0 241 {
nexpaq 0:6c56fb4bc5f0 242 return _name_id;
nexpaq 0:6c56fb4bc5f0 243 }
nexpaq 0:6c56fb4bc5f0 244
nexpaq 0:6c56fb4bc5f0 245 uint16_t M2MBase::instance_id() const
nexpaq 0:6c56fb4bc5f0 246 {
nexpaq 0:6c56fb4bc5f0 247 return _instance_id;
nexpaq 0:6c56fb4bc5f0 248 }
nexpaq 0:6c56fb4bc5f0 249
nexpaq 0:6c56fb4bc5f0 250 const String& M2MBase::interface_description() const
nexpaq 0:6c56fb4bc5f0 251 {
nexpaq 0:6c56fb4bc5f0 252 return _interface_description;
nexpaq 0:6c56fb4bc5f0 253 }
nexpaq 0:6c56fb4bc5f0 254
nexpaq 0:6c56fb4bc5f0 255 const String& M2MBase::resource_type() const
nexpaq 0:6c56fb4bc5f0 256 {
nexpaq 0:6c56fb4bc5f0 257 return _resource_type;
nexpaq 0:6c56fb4bc5f0 258 }
nexpaq 0:6c56fb4bc5f0 259
nexpaq 0:6c56fb4bc5f0 260 uint8_t M2MBase::coap_content_type() const
nexpaq 0:6c56fb4bc5f0 261 {
nexpaq 0:6c56fb4bc5f0 262 return _coap_content_type;
nexpaq 0:6c56fb4bc5f0 263 }
nexpaq 0:6c56fb4bc5f0 264
nexpaq 0:6c56fb4bc5f0 265 bool M2MBase::is_observable() const
nexpaq 0:6c56fb4bc5f0 266 {
nexpaq 0:6c56fb4bc5f0 267 return _observable;
nexpaq 0:6c56fb4bc5f0 268 }
nexpaq 0:6c56fb4bc5f0 269
nexpaq 0:6c56fb4bc5f0 270 M2MBase::Observation M2MBase::observation_level() const
nexpaq 0:6c56fb4bc5f0 271 {
nexpaq 0:6c56fb4bc5f0 272 return _observation_level;
nexpaq 0:6c56fb4bc5f0 273 }
nexpaq 0:6c56fb4bc5f0 274
nexpaq 0:6c56fb4bc5f0 275 void M2MBase::get_observation_token(uint8_t *&token, uint32_t &token_length)
nexpaq 0:6c56fb4bc5f0 276 {
nexpaq 0:6c56fb4bc5f0 277 token_length = 0;
nexpaq 0:6c56fb4bc5f0 278 free(token);
nexpaq 0:6c56fb4bc5f0 279
nexpaq 0:6c56fb4bc5f0 280 token = alloc_string_copy((uint8_t *)_token, _token_length);
nexpaq 0:6c56fb4bc5f0 281 if(token) {
nexpaq 0:6c56fb4bc5f0 282 token_length = _token_length;
nexpaq 0:6c56fb4bc5f0 283 }
nexpaq 0:6c56fb4bc5f0 284 }
nexpaq 0:6c56fb4bc5f0 285
nexpaq 0:6c56fb4bc5f0 286 M2MBase::Mode M2MBase::mode() const
nexpaq 0:6c56fb4bc5f0 287 {
nexpaq 0:6c56fb4bc5f0 288 return _mode;
nexpaq 0:6c56fb4bc5f0 289 }
nexpaq 0:6c56fb4bc5f0 290
nexpaq 0:6c56fb4bc5f0 291 uint16_t M2MBase::observation_number() const
nexpaq 0:6c56fb4bc5f0 292 {
nexpaq 0:6c56fb4bc5f0 293 return _observation_number;
nexpaq 0:6c56fb4bc5f0 294 }
nexpaq 0:6c56fb4bc5f0 295
nexpaq 0:6c56fb4bc5f0 296 uint32_t M2MBase::max_age() const
nexpaq 0:6c56fb4bc5f0 297 {
nexpaq 0:6c56fb4bc5f0 298 return _max_age;
nexpaq 0:6c56fb4bc5f0 299 }
nexpaq 0:6c56fb4bc5f0 300
nexpaq 0:6c56fb4bc5f0 301 bool M2MBase::handle_observation_attribute(char *&query)
nexpaq 0:6c56fb4bc5f0 302 {
nexpaq 0:6c56fb4bc5f0 303 tr_debug("M2MBase::handle_observation_attribute - under observation(%d)", is_under_observation());
nexpaq 0:6c56fb4bc5f0 304 bool success = false;
nexpaq 0:6c56fb4bc5f0 305 if(_report_handler) {
nexpaq 0:6c56fb4bc5f0 306 success = _report_handler->parse_notification_attribute(query,_base_type);
nexpaq 0:6c56fb4bc5f0 307 if (success) {
nexpaq 0:6c56fb4bc5f0 308 if (is_under_observation()) {
nexpaq 0:6c56fb4bc5f0 309 _report_handler->set_under_observation(true);
nexpaq 0:6c56fb4bc5f0 310 }
nexpaq 0:6c56fb4bc5f0 311 } else {
nexpaq 0:6c56fb4bc5f0 312 _report_handler->set_default_values();
nexpaq 0:6c56fb4bc5f0 313 }
nexpaq 0:6c56fb4bc5f0 314 }
nexpaq 0:6c56fb4bc5f0 315 return success;
nexpaq 0:6c56fb4bc5f0 316 }
nexpaq 0:6c56fb4bc5f0 317
nexpaq 0:6c56fb4bc5f0 318 void M2MBase::observation_to_be_sent(m2m::Vector<uint16_t> changed_instance_ids, bool send_object)
nexpaq 0:6c56fb4bc5f0 319 {
nexpaq 0:6c56fb4bc5f0 320 //TODO: Move this to M2MResourceInstance
nexpaq 0:6c56fb4bc5f0 321 if(_observation_handler) {
nexpaq 0:6c56fb4bc5f0 322 _observation_number++;
nexpaq 0:6c56fb4bc5f0 323 _observation_handler->observation_to_be_sent(this,
nexpaq 0:6c56fb4bc5f0 324 _observation_number,
nexpaq 0:6c56fb4bc5f0 325 changed_instance_ids,
nexpaq 0:6c56fb4bc5f0 326 send_object);
nexpaq 0:6c56fb4bc5f0 327 }
nexpaq 0:6c56fb4bc5f0 328 }
nexpaq 0:6c56fb4bc5f0 329
nexpaq 0:6c56fb4bc5f0 330 void M2MBase::set_base_type(M2MBase::BaseType type)
nexpaq 0:6c56fb4bc5f0 331 {
nexpaq 0:6c56fb4bc5f0 332 _base_type = type;
nexpaq 0:6c56fb4bc5f0 333 }
nexpaq 0:6c56fb4bc5f0 334
nexpaq 0:6c56fb4bc5f0 335 void M2MBase::remove_resource_from_coap(const String &resource_name)
nexpaq 0:6c56fb4bc5f0 336 {
nexpaq 0:6c56fb4bc5f0 337 if(_observation_handler) {
nexpaq 0:6c56fb4bc5f0 338 _observation_handler->resource_to_be_deleted(resource_name);
nexpaq 0:6c56fb4bc5f0 339 }
nexpaq 0:6c56fb4bc5f0 340 }
nexpaq 0:6c56fb4bc5f0 341
nexpaq 0:6c56fb4bc5f0 342 void M2MBase::remove_object_from_coap()
nexpaq 0:6c56fb4bc5f0 343 {
nexpaq 0:6c56fb4bc5f0 344 if(_observation_handler) {
nexpaq 0:6c56fb4bc5f0 345 _observation_handler->remove_object(this);
nexpaq 0:6c56fb4bc5f0 346 }
nexpaq 0:6c56fb4bc5f0 347 }
nexpaq 0:6c56fb4bc5f0 348
nexpaq 0:6c56fb4bc5f0 349 sn_coap_hdr_s* M2MBase::handle_get_request(nsdl_s */*nsdl*/,
nexpaq 0:6c56fb4bc5f0 350 sn_coap_hdr_s */*received_coap_header*/,
nexpaq 0:6c56fb4bc5f0 351 M2MObservationHandler */*observation_handler*/)
nexpaq 0:6c56fb4bc5f0 352 {
nexpaq 0:6c56fb4bc5f0 353 //Handled in M2MResource, M2MObjectInstance and M2MObject classes
nexpaq 0:6c56fb4bc5f0 354 return NULL;
nexpaq 0:6c56fb4bc5f0 355 }
nexpaq 0:6c56fb4bc5f0 356
nexpaq 0:6c56fb4bc5f0 357 sn_coap_hdr_s* M2MBase::handle_put_request(nsdl_s */*nsdl*/,
nexpaq 0:6c56fb4bc5f0 358 sn_coap_hdr_s */*received_coap_header*/,
nexpaq 0:6c56fb4bc5f0 359 M2MObservationHandler */*observation_handler*/,
nexpaq 0:6c56fb4bc5f0 360 bool &)
nexpaq 0:6c56fb4bc5f0 361 {
nexpaq 0:6c56fb4bc5f0 362 //Handled in M2MResource, M2MObjectInstance and M2MObject classes
nexpaq 0:6c56fb4bc5f0 363 return NULL;
nexpaq 0:6c56fb4bc5f0 364 }
nexpaq 0:6c56fb4bc5f0 365
nexpaq 0:6c56fb4bc5f0 366 sn_coap_hdr_s* M2MBase::handle_post_request(nsdl_s */*nsdl*/,
nexpaq 0:6c56fb4bc5f0 367 sn_coap_hdr_s */*received_coap_header*/,
nexpaq 0:6c56fb4bc5f0 368 M2MObservationHandler */*observation_handler*/,
nexpaq 0:6c56fb4bc5f0 369 bool &)
nexpaq 0:6c56fb4bc5f0 370 {
nexpaq 0:6c56fb4bc5f0 371 //Handled in M2MResource, M2MObjectInstance and M2MObject classes
nexpaq 0:6c56fb4bc5f0 372 return NULL;
nexpaq 0:6c56fb4bc5f0 373 }
nexpaq 0:6c56fb4bc5f0 374
nexpaq 0:6c56fb4bc5f0 375 void *M2MBase::memory_alloc(uint32_t size)
nexpaq 0:6c56fb4bc5f0 376 {
nexpaq 0:6c56fb4bc5f0 377 if(size)
nexpaq 0:6c56fb4bc5f0 378 return malloc(size);
nexpaq 0:6c56fb4bc5f0 379 else
nexpaq 0:6c56fb4bc5f0 380 return 0;
nexpaq 0:6c56fb4bc5f0 381 }
nexpaq 0:6c56fb4bc5f0 382
nexpaq 0:6c56fb4bc5f0 383 void M2MBase::memory_free(void *ptr)
nexpaq 0:6c56fb4bc5f0 384 {
nexpaq 0:6c56fb4bc5f0 385 free(ptr);
nexpaq 0:6c56fb4bc5f0 386 }
nexpaq 0:6c56fb4bc5f0 387
nexpaq 0:6c56fb4bc5f0 388 uint8_t* M2MBase::alloc_string_copy(const uint8_t* source, uint32_t size)
nexpaq 0:6c56fb4bc5f0 389 {
nexpaq 0:6c56fb4bc5f0 390 assert(source != NULL);
nexpaq 0:6c56fb4bc5f0 391
nexpaq 0:6c56fb4bc5f0 392 uint8_t* result = (uint8_t*)memory_alloc(size + 1);
nexpaq 0:6c56fb4bc5f0 393 if (result) {
nexpaq 0:6c56fb4bc5f0 394 memcpy(result, source, size);
nexpaq 0:6c56fb4bc5f0 395 result[size] = '\0';
nexpaq 0:6c56fb4bc5f0 396 }
nexpaq 0:6c56fb4bc5f0 397 return result;
nexpaq 0:6c56fb4bc5f0 398 }
nexpaq 0:6c56fb4bc5f0 399
nexpaq 0:6c56fb4bc5f0 400 uint8_t* M2MBase::alloc_copy(const uint8_t* source, uint32_t size)
nexpaq 0:6c56fb4bc5f0 401 {
nexpaq 0:6c56fb4bc5f0 402 assert(source != NULL);
nexpaq 0:6c56fb4bc5f0 403
nexpaq 0:6c56fb4bc5f0 404 uint8_t* result = (uint8_t*)memory_alloc(size);
nexpaq 0:6c56fb4bc5f0 405 if (result) {
nexpaq 0:6c56fb4bc5f0 406 memcpy(result, source, size);
nexpaq 0:6c56fb4bc5f0 407 }
nexpaq 0:6c56fb4bc5f0 408 return result;
nexpaq 0:6c56fb4bc5f0 409 }
nexpaq 0:6c56fb4bc5f0 410
nexpaq 0:6c56fb4bc5f0 411 M2MReportHandler* M2MBase::report_handler()
nexpaq 0:6c56fb4bc5f0 412 {
nexpaq 0:6c56fb4bc5f0 413 return _report_handler;
nexpaq 0:6c56fb4bc5f0 414 }
nexpaq 0:6c56fb4bc5f0 415
nexpaq 0:6c56fb4bc5f0 416 M2MObservationHandler* M2MBase::observation_handler()
nexpaq 0:6c56fb4bc5f0 417 {
nexpaq 0:6c56fb4bc5f0 418 return _observation_handler;
nexpaq 0:6c56fb4bc5f0 419 }
nexpaq 0:6c56fb4bc5f0 420
nexpaq 0:6c56fb4bc5f0 421 void M2MBase::set_register_uri( bool register_uri)
nexpaq 0:6c56fb4bc5f0 422 {
nexpaq 0:6c56fb4bc5f0 423 _register_uri = register_uri;
nexpaq 0:6c56fb4bc5f0 424 }
nexpaq 0:6c56fb4bc5f0 425
nexpaq 0:6c56fb4bc5f0 426 bool M2MBase::register_uri()
nexpaq 0:6c56fb4bc5f0 427 {
nexpaq 0:6c56fb4bc5f0 428 return _register_uri;
nexpaq 0:6c56fb4bc5f0 429 }
nexpaq 0:6c56fb4bc5f0 430
nexpaq 0:6c56fb4bc5f0 431 bool M2MBase::is_integer(const String &value)
nexpaq 0:6c56fb4bc5f0 432 {
nexpaq 0:6c56fb4bc5f0 433 const char *s = value.c_str();
nexpaq 0:6c56fb4bc5f0 434 if(value.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) {
nexpaq 0:6c56fb4bc5f0 435 return false;
nexpaq 0:6c56fb4bc5f0 436 }
nexpaq 0:6c56fb4bc5f0 437 char * p ;
nexpaq 0:6c56fb4bc5f0 438 strtol(value.c_str(), &p, 10);
nexpaq 0:6c56fb4bc5f0 439 return (*p == 0);
nexpaq 0:6c56fb4bc5f0 440 }
nexpaq 0:6c56fb4bc5f0 441
nexpaq 0:6c56fb4bc5f0 442 void M2MBase::set_uri_path(const String &path)
nexpaq 0:6c56fb4bc5f0 443 {
nexpaq 0:6c56fb4bc5f0 444 _uri_path = path;
nexpaq 0:6c56fb4bc5f0 445 }
nexpaq 0:6c56fb4bc5f0 446
nexpaq 0:6c56fb4bc5f0 447 const String& M2MBase::uri_path() const
nexpaq 0:6c56fb4bc5f0 448 {
nexpaq 0:6c56fb4bc5f0 449 return _uri_path;
nexpaq 0:6c56fb4bc5f0 450 }
nexpaq 0:6c56fb4bc5f0 451
nexpaq 0:6c56fb4bc5f0 452 bool M2MBase::is_under_observation() const
nexpaq 0:6c56fb4bc5f0 453 {
nexpaq 0:6c56fb4bc5f0 454 return _is_under_observation;
nexpaq 0:6c56fb4bc5f0 455 }
nexpaq 0:6c56fb4bc5f0 456
nexpaq 0:6c56fb4bc5f0 457 void M2MBase::set_value_updated_function(value_updated_callback callback)
nexpaq 0:6c56fb4bc5f0 458 {
nexpaq 0:6c56fb4bc5f0 459 _value_updated_callback = callback;
nexpaq 0:6c56fb4bc5f0 460 }
nexpaq 0:6c56fb4bc5f0 461
nexpaq 0:6c56fb4bc5f0 462 void M2MBase::set_value_updated_function(value_updated_callback2 callback)
nexpaq 0:6c56fb4bc5f0 463 {
nexpaq 0:6c56fb4bc5f0 464 delete _function_pointer;
nexpaq 0:6c56fb4bc5f0 465 _function_pointer = new FP1<void, const char*>(callback);
nexpaq 0:6c56fb4bc5f0 466 set_value_updated_function(value_updated_callback(_function_pointer,
nexpaq 0:6c56fb4bc5f0 467 &FP1<void, const char*>::call));
nexpaq 0:6c56fb4bc5f0 468 }
nexpaq 0:6c56fb4bc5f0 469 bool M2MBase::is_value_updated_function_set()
nexpaq 0:6c56fb4bc5f0 470 {
nexpaq 0:6c56fb4bc5f0 471 return (_value_updated_callback) ? true : false;
nexpaq 0:6c56fb4bc5f0 472 }
nexpaq 0:6c56fb4bc5f0 473
nexpaq 0:6c56fb4bc5f0 474 void M2MBase::execute_value_updated(const String& name)
nexpaq 0:6c56fb4bc5f0 475 {
nexpaq 0:6c56fb4bc5f0 476 if(_value_updated_callback) {
nexpaq 0:6c56fb4bc5f0 477 _value_updated_callback(name.c_str());
nexpaq 0:6c56fb4bc5f0 478 }
nexpaq 0:6c56fb4bc5f0 479 }