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.
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2mendpoint.cpp@0:8f0bb79ddd48, 2021-05-04 (annotated)
- Committer:
- leothedragon
- Date:
- Tue May 04 08:55:12 2021 +0000
- Revision:
- 0:8f0bb79ddd48
nmn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:8f0bb79ddd48 | 1 | /* |
leothedragon | 0:8f0bb79ddd48 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
leothedragon | 0:8f0bb79ddd48 | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:8f0bb79ddd48 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:8f0bb79ddd48 | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:8f0bb79ddd48 | 6 | * You may obtain a copy of the License at |
leothedragon | 0:8f0bb79ddd48 | 7 | * |
leothedragon | 0:8f0bb79ddd48 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:8f0bb79ddd48 | 9 | * |
leothedragon | 0:8f0bb79ddd48 | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:8f0bb79ddd48 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:8f0bb79ddd48 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:8f0bb79ddd48 | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:8f0bb79ddd48 | 14 | * limitations under the License. |
leothedragon | 0:8f0bb79ddd48 | 15 | */ |
leothedragon | 0:8f0bb79ddd48 | 16 | |
leothedragon | 0:8f0bb79ddd48 | 17 | #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION |
leothedragon | 0:8f0bb79ddd48 | 18 | |
leothedragon | 0:8f0bb79ddd48 | 19 | #include "mbed-client/m2mendpoint.h" |
leothedragon | 0:8f0bb79ddd48 | 20 | #include "mbed-client/m2mobject.h" |
leothedragon | 0:8f0bb79ddd48 | 21 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:8f0bb79ddd48 | 22 | #include "include/m2mtlvserializer.h" |
leothedragon | 0:8f0bb79ddd48 | 23 | #include "include/m2mtlvdeserializer.h" |
leothedragon | 0:8f0bb79ddd48 | 24 | #include "include/m2mreporthandler.h" |
leothedragon | 0:8f0bb79ddd48 | 25 | #include "mbed-trace/mbed_trace.h" |
leothedragon | 0:8f0bb79ddd48 | 26 | #include "mbed-client/m2mstringbuffer.h" |
leothedragon | 0:8f0bb79ddd48 | 27 | #include "mbed-client/m2mstring.h" |
leothedragon | 0:8f0bb79ddd48 | 28 | #include "nsdl-c/sn_nsdl_lib.h" |
leothedragon | 0:8f0bb79ddd48 | 29 | |
leothedragon | 0:8f0bb79ddd48 | 30 | #include <stdlib.h> |
leothedragon | 0:8f0bb79ddd48 | 31 | |
leothedragon | 0:8f0bb79ddd48 | 32 | #define BUFFER_SIZE 10 |
leothedragon | 0:8f0bb79ddd48 | 33 | #define TRACE_GROUP "mClt" |
leothedragon | 0:8f0bb79ddd48 | 34 | |
leothedragon | 0:8f0bb79ddd48 | 35 | M2MEndpoint::M2MEndpoint(const String &object_name, char *path) |
leothedragon | 0:8f0bb79ddd48 | 36 | : M2MBase(object_name, |
leothedragon | 0:8f0bb79ddd48 | 37 | M2MBase::Dynamic, |
leothedragon | 0:8f0bb79ddd48 | 38 | #ifndef DISABLE_RESOURCE_TYPE |
leothedragon | 0:8f0bb79ddd48 | 39 | "", |
leothedragon | 0:8f0bb79ddd48 | 40 | #endif |
leothedragon | 0:8f0bb79ddd48 | 41 | path, |
leothedragon | 0:8f0bb79ddd48 | 42 | false, |
leothedragon | 0:8f0bb79ddd48 | 43 | false), |
leothedragon | 0:8f0bb79ddd48 | 44 | _observation_handler(NULL), |
leothedragon | 0:8f0bb79ddd48 | 45 | _ctx(NULL), |
leothedragon | 0:8f0bb79ddd48 | 46 | _changed(true), |
leothedragon | 0:8f0bb79ddd48 | 47 | _deleted(false) |
leothedragon | 0:8f0bb79ddd48 | 48 | { |
leothedragon | 0:8f0bb79ddd48 | 49 | M2MBase::set_base_type(M2MBase::ObjectDirectory); |
leothedragon | 0:8f0bb79ddd48 | 50 | get_nsdl_resource()->always_publish = false; |
leothedragon | 0:8f0bb79ddd48 | 51 | #ifdef RESOURCE_ATTRIBUTES_LIST |
leothedragon | 0:8f0bb79ddd48 | 52 | sn_nsdl_attribute_item_s item; |
leothedragon | 0:8f0bb79ddd48 | 53 | item.attribute_name = ATTR_ENDPOINT_NAME; |
leothedragon | 0:8f0bb79ddd48 | 54 | item.value = (char*)alloc_string_copy((uint8_t*) object_name.c_str(), object_name.length()); |
leothedragon | 0:8f0bb79ddd48 | 55 | sn_nsdl_set_resource_attribute(get_nsdl_resource()->static_resource_parameters, &item); |
leothedragon | 0:8f0bb79ddd48 | 56 | #endif |
leothedragon | 0:8f0bb79ddd48 | 57 | } |
leothedragon | 0:8f0bb79ddd48 | 58 | |
leothedragon | 0:8f0bb79ddd48 | 59 | |
leothedragon | 0:8f0bb79ddd48 | 60 | M2MEndpoint::~M2MEndpoint() |
leothedragon | 0:8f0bb79ddd48 | 61 | { |
leothedragon | 0:8f0bb79ddd48 | 62 | tr_debug("~M2MEndpoint %p", this); |
leothedragon | 0:8f0bb79ddd48 | 63 | if(!_object_list.empty()) { |
leothedragon | 0:8f0bb79ddd48 | 64 | |
leothedragon | 0:8f0bb79ddd48 | 65 | M2MObjectList::const_iterator it; |
leothedragon | 0:8f0bb79ddd48 | 66 | it = _object_list.begin(); |
leothedragon | 0:8f0bb79ddd48 | 67 | M2MObject* obj = NULL; |
leothedragon | 0:8f0bb79ddd48 | 68 | uint16_t index = 0; |
leothedragon | 0:8f0bb79ddd48 | 69 | for (; it!=_object_list.end(); it++, index++ ) { |
leothedragon | 0:8f0bb79ddd48 | 70 | //Free allocated memory for object instances. |
leothedragon | 0:8f0bb79ddd48 | 71 | obj = *it; |
leothedragon | 0:8f0bb79ddd48 | 72 | tr_debug(" deleting object %p", obj); |
leothedragon | 0:8f0bb79ddd48 | 73 | delete obj; |
leothedragon | 0:8f0bb79ddd48 | 74 | } |
leothedragon | 0:8f0bb79ddd48 | 75 | |
leothedragon | 0:8f0bb79ddd48 | 76 | _object_list.clear(); |
leothedragon | 0:8f0bb79ddd48 | 77 | } |
leothedragon | 0:8f0bb79ddd48 | 78 | |
leothedragon | 0:8f0bb79ddd48 | 79 | free_resources(); |
leothedragon | 0:8f0bb79ddd48 | 80 | } |
leothedragon | 0:8f0bb79ddd48 | 81 | |
leothedragon | 0:8f0bb79ddd48 | 82 | M2MObject* M2MEndpoint::create_object(const String &name) |
leothedragon | 0:8f0bb79ddd48 | 83 | { |
leothedragon | 0:8f0bb79ddd48 | 84 | M2MObject *obj = NULL; |
leothedragon | 0:8f0bb79ddd48 | 85 | if (object(name) == NULL) { |
leothedragon | 0:8f0bb79ddd48 | 86 | char *path = create_path(*this, name.c_str()); |
leothedragon | 0:8f0bb79ddd48 | 87 | obj = new M2MObject(name, path, false); |
leothedragon | 0:8f0bb79ddd48 | 88 | if (obj != NULL) { |
leothedragon | 0:8f0bb79ddd48 | 89 | obj->set_endpoint(this); |
leothedragon | 0:8f0bb79ddd48 | 90 | _object_list.push_back(obj); |
leothedragon | 0:8f0bb79ddd48 | 91 | } |
leothedragon | 0:8f0bb79ddd48 | 92 | } |
leothedragon | 0:8f0bb79ddd48 | 93 | return obj; |
leothedragon | 0:8f0bb79ddd48 | 94 | } |
leothedragon | 0:8f0bb79ddd48 | 95 | |
leothedragon | 0:8f0bb79ddd48 | 96 | bool M2MEndpoint::remove_object(const String &name) |
leothedragon | 0:8f0bb79ddd48 | 97 | { |
leothedragon | 0:8f0bb79ddd48 | 98 | bool success = false; |
leothedragon | 0:8f0bb79ddd48 | 99 | if (object_count() == 0) { |
leothedragon | 0:8f0bb79ddd48 | 100 | return success; |
leothedragon | 0:8f0bb79ddd48 | 101 | } |
leothedragon | 0:8f0bb79ddd48 | 102 | M2MObjectList::const_iterator it; |
leothedragon | 0:8f0bb79ddd48 | 103 | M2MObject *obj = NULL; |
leothedragon | 0:8f0bb79ddd48 | 104 | int pos = 0; |
leothedragon | 0:8f0bb79ddd48 | 105 | it = _object_list.begin(); |
leothedragon | 0:8f0bb79ddd48 | 106 | for (; it != _object_list.end(); it++, pos++) { |
leothedragon | 0:8f0bb79ddd48 | 107 | obj = *it; |
leothedragon | 0:8f0bb79ddd48 | 108 | if (name == obj->name()) { |
leothedragon | 0:8f0bb79ddd48 | 109 | delete obj; |
leothedragon | 0:8f0bb79ddd48 | 110 | _object_list.erase(pos); |
leothedragon | 0:8f0bb79ddd48 | 111 | success = true; |
leothedragon | 0:8f0bb79ddd48 | 112 | break; |
leothedragon | 0:8f0bb79ddd48 | 113 | } |
leothedragon | 0:8f0bb79ddd48 | 114 | } |
leothedragon | 0:8f0bb79ddd48 | 115 | return success; |
leothedragon | 0:8f0bb79ddd48 | 116 | |
leothedragon | 0:8f0bb79ddd48 | 117 | } |
leothedragon | 0:8f0bb79ddd48 | 118 | |
leothedragon | 0:8f0bb79ddd48 | 119 | M2MObject* M2MEndpoint::object(const String &name) const |
leothedragon | 0:8f0bb79ddd48 | 120 | { |
leothedragon | 0:8f0bb79ddd48 | 121 | M2MObject *obj = NULL; |
leothedragon | 0:8f0bb79ddd48 | 122 | if (object_count() == 0) { |
leothedragon | 0:8f0bb79ddd48 | 123 | return obj; |
leothedragon | 0:8f0bb79ddd48 | 124 | } |
leothedragon | 0:8f0bb79ddd48 | 125 | M2MObjectList::const_iterator it = _object_list.begin(); |
leothedragon | 0:8f0bb79ddd48 | 126 | for (; it != _object_list.end(); it++) { |
leothedragon | 0:8f0bb79ddd48 | 127 | if (name == (*it)->name()) { |
leothedragon | 0:8f0bb79ddd48 | 128 | obj = *it; |
leothedragon | 0:8f0bb79ddd48 | 129 | break; |
leothedragon | 0:8f0bb79ddd48 | 130 | } |
leothedragon | 0:8f0bb79ddd48 | 131 | } |
leothedragon | 0:8f0bb79ddd48 | 132 | return obj; |
leothedragon | 0:8f0bb79ddd48 | 133 | } |
leothedragon | 0:8f0bb79ddd48 | 134 | |
leothedragon | 0:8f0bb79ddd48 | 135 | const M2MObjectList& M2MEndpoint::objects() const |
leothedragon | 0:8f0bb79ddd48 | 136 | { |
leothedragon | 0:8f0bb79ddd48 | 137 | return _object_list; |
leothedragon | 0:8f0bb79ddd48 | 138 | } |
leothedragon | 0:8f0bb79ddd48 | 139 | |
leothedragon | 0:8f0bb79ddd48 | 140 | uint16_t M2MEndpoint::object_count() const |
leothedragon | 0:8f0bb79ddd48 | 141 | { |
leothedragon | 0:8f0bb79ddd48 | 142 | return _object_list.size(); |
leothedragon | 0:8f0bb79ddd48 | 143 | } |
leothedragon | 0:8f0bb79ddd48 | 144 | |
leothedragon | 0:8f0bb79ddd48 | 145 | M2MObservationHandler* M2MEndpoint::observation_handler() const |
leothedragon | 0:8f0bb79ddd48 | 146 | { |
leothedragon | 0:8f0bb79ddd48 | 147 | return _observation_handler; |
leothedragon | 0:8f0bb79ddd48 | 148 | } |
leothedragon | 0:8f0bb79ddd48 | 149 | |
leothedragon | 0:8f0bb79ddd48 | 150 | void M2MEndpoint::set_observation_handler(M2MObservationHandler *handler) |
leothedragon | 0:8f0bb79ddd48 | 151 | { |
leothedragon | 0:8f0bb79ddd48 | 152 | _observation_handler = handler; |
leothedragon | 0:8f0bb79ddd48 | 153 | } |
leothedragon | 0:8f0bb79ddd48 | 154 | |
leothedragon | 0:8f0bb79ddd48 | 155 | void M2MEndpoint::add_observation_level(M2MBase::Observation observation_level) |
leothedragon | 0:8f0bb79ddd48 | 156 | { |
leothedragon | 0:8f0bb79ddd48 | 157 | (void)observation_level; |
leothedragon | 0:8f0bb79ddd48 | 158 | } |
leothedragon | 0:8f0bb79ddd48 | 159 | |
leothedragon | 0:8f0bb79ddd48 | 160 | void M2MEndpoint::remove_observation_level(M2MBase::Observation observation_level) |
leothedragon | 0:8f0bb79ddd48 | 161 | { |
leothedragon | 0:8f0bb79ddd48 | 162 | (void)observation_level; |
leothedragon | 0:8f0bb79ddd48 | 163 | } |
leothedragon | 0:8f0bb79ddd48 | 164 | |
leothedragon | 0:8f0bb79ddd48 | 165 | sn_coap_hdr_s* M2MEndpoint::handle_get_request(nsdl_s *nsdl, |
leothedragon | 0:8f0bb79ddd48 | 166 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:8f0bb79ddd48 | 167 | M2MObservationHandler *observation_handler) |
leothedragon | 0:8f0bb79ddd48 | 168 | { |
leothedragon | 0:8f0bb79ddd48 | 169 | tr_debug("M2MEndpoint::handle_get_request()"); |
leothedragon | 0:8f0bb79ddd48 | 170 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:8f0bb79ddd48 | 171 | sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:8f0bb79ddd48 | 172 | received_coap_header, msg_code); |
leothedragon | 0:8f0bb79ddd48 | 173 | return coap_response; |
leothedragon | 0:8f0bb79ddd48 | 174 | |
leothedragon | 0:8f0bb79ddd48 | 175 | } |
leothedragon | 0:8f0bb79ddd48 | 176 | |
leothedragon | 0:8f0bb79ddd48 | 177 | sn_coap_hdr_s* M2MEndpoint::handle_put_request(nsdl_s *nsdl, |
leothedragon | 0:8f0bb79ddd48 | 178 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:8f0bb79ddd48 | 179 | M2MObservationHandler */*observation_handler*/, |
leothedragon | 0:8f0bb79ddd48 | 180 | bool &/*execute_value_updated*/) |
leothedragon | 0:8f0bb79ddd48 | 181 | { |
leothedragon | 0:8f0bb79ddd48 | 182 | tr_debug("M2MEndpoint::handle_put_request()"); |
leothedragon | 0:8f0bb79ddd48 | 183 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:8f0bb79ddd48 | 184 | sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:8f0bb79ddd48 | 185 | received_coap_header, msg_code); |
leothedragon | 0:8f0bb79ddd48 | 186 | return coap_response; |
leothedragon | 0:8f0bb79ddd48 | 187 | } |
leothedragon | 0:8f0bb79ddd48 | 188 | |
leothedragon | 0:8f0bb79ddd48 | 189 | |
leothedragon | 0:8f0bb79ddd48 | 190 | sn_coap_hdr_s* M2MEndpoint::handle_post_request(nsdl_s *nsdl, |
leothedragon | 0:8f0bb79ddd48 | 191 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:8f0bb79ddd48 | 192 | M2MObservationHandler *observation_handler, |
leothedragon | 0:8f0bb79ddd48 | 193 | bool &execute_value_updated, |
leothedragon | 0:8f0bb79ddd48 | 194 | sn_nsdl_addr_s *) |
leothedragon | 0:8f0bb79ddd48 | 195 | { |
leothedragon | 0:8f0bb79ddd48 | 196 | tr_debug("M2MEndpoint::handle_post_request()"); |
leothedragon | 0:8f0bb79ddd48 | 197 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:8f0bb79ddd48 | 198 | sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:8f0bb79ddd48 | 199 | received_coap_header, msg_code); |
leothedragon | 0:8f0bb79ddd48 | 200 | return coap_response; |
leothedragon | 0:8f0bb79ddd48 | 201 | } |
leothedragon | 0:8f0bb79ddd48 | 202 | |
leothedragon | 0:8f0bb79ddd48 | 203 | void M2MEndpoint::set_context(void *ctx) |
leothedragon | 0:8f0bb79ddd48 | 204 | { |
leothedragon | 0:8f0bb79ddd48 | 205 | _ctx = ctx; |
leothedragon | 0:8f0bb79ddd48 | 206 | } |
leothedragon | 0:8f0bb79ddd48 | 207 | |
leothedragon | 0:8f0bb79ddd48 | 208 | void* M2MEndpoint::get_context() const |
leothedragon | 0:8f0bb79ddd48 | 209 | { |
leothedragon | 0:8f0bb79ddd48 | 210 | return _ctx; |
leothedragon | 0:8f0bb79ddd48 | 211 | } |
leothedragon | 0:8f0bb79ddd48 | 212 | |
leothedragon | 0:8f0bb79ddd48 | 213 | void M2MEndpoint::set_changed() |
leothedragon | 0:8f0bb79ddd48 | 214 | { |
leothedragon | 0:8f0bb79ddd48 | 215 | _changed = true; |
leothedragon | 0:8f0bb79ddd48 | 216 | } |
leothedragon | 0:8f0bb79ddd48 | 217 | |
leothedragon | 0:8f0bb79ddd48 | 218 | void M2MEndpoint::clear_changed() |
leothedragon | 0:8f0bb79ddd48 | 219 | { |
leothedragon | 0:8f0bb79ddd48 | 220 | _changed = false; |
leothedragon | 0:8f0bb79ddd48 | 221 | } |
leothedragon | 0:8f0bb79ddd48 | 222 | |
leothedragon | 0:8f0bb79ddd48 | 223 | bool M2MEndpoint::get_changed() const |
leothedragon | 0:8f0bb79ddd48 | 224 | { |
leothedragon | 0:8f0bb79ddd48 | 225 | return _changed; |
leothedragon | 0:8f0bb79ddd48 | 226 | } |
leothedragon | 0:8f0bb79ddd48 | 227 | |
leothedragon | 0:8f0bb79ddd48 | 228 | void M2MEndpoint::set_deleted() |
leothedragon | 0:8f0bb79ddd48 | 229 | { |
leothedragon | 0:8f0bb79ddd48 | 230 | _deleted = true; |
leothedragon | 0:8f0bb79ddd48 | 231 | set_changed(); |
leothedragon | 0:8f0bb79ddd48 | 232 | } |
leothedragon | 0:8f0bb79ddd48 | 233 | |
leothedragon | 0:8f0bb79ddd48 | 234 | bool M2MEndpoint::is_deleted() |
leothedragon | 0:8f0bb79ddd48 | 235 | { |
leothedragon | 0:8f0bb79ddd48 | 236 | return _deleted; |
leothedragon | 0:8f0bb79ddd48 | 237 | } |
leothedragon | 0:8f0bb79ddd48 | 238 | |
leothedragon | 0:8f0bb79ddd48 | 239 | #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION |