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