Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mendpoint.cpp Source File

m2mendpoint.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 
00017 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
00018 
00019 #include "mbed-client/m2mendpoint.h"
00020 #include "mbed-client/m2mobject.h"
00021 #include "mbed-client/m2mconstants.h"
00022 #include "include/m2mtlvserializer.h"
00023 #include "include/m2mtlvdeserializer.h"
00024 #include "include/m2mreporthandler.h"
00025 #include "mbed-trace/mbed_trace.h"
00026 #include "mbed-client/m2mstringbuffer.h"
00027 #include "mbed-client/m2mstring.h"
00028 #include "nsdl-c/sn_nsdl_lib.h"
00029 
00030 #include <stdlib.h>
00031 
00032 #define BUFFER_SIZE 10
00033 #define TRACE_GROUP "mClt"
00034 
00035 M2MEndpoint::M2MEndpoint(const String &object_name, char *path)
00036 : M2MBase(object_name,
00037           M2MBase::Dynamic,
00038 #ifndef DISABLE_RESOURCE_TYPE
00039           "",
00040 #endif
00041           path,
00042           false,
00043           false),
00044     _observation_handler(NULL),
00045     _ctx(NULL),
00046     _changed(true),
00047     _deleted(false)
00048 {
00049     M2MBase::set_base_type(M2MBase::ObjectDirectory);
00050     get_nsdl_resource()->always_publish = false;
00051 #ifdef RESOURCE_ATTRIBUTES_LIST
00052     sn_nsdl_attribute_item_s item;
00053     item.attribute_name = ATTR_ENDPOINT_NAME;
00054     item.value = (char*)alloc_string_copy((uint8_t*) object_name.c_str(), object_name.length());
00055     sn_nsdl_set_resource_attribute(get_nsdl_resource()->static_resource_parameters, &item);
00056 #endif
00057 }
00058 
00059 
00060 M2MEndpoint::~M2MEndpoint()
00061 {
00062     tr_debug("~M2MEndpoint %p", this);
00063     if(!_object_list.empty()) {
00064 
00065         M2MObjectList::const_iterator it;
00066         it = _object_list.begin();
00067         M2MObject* obj = NULL;
00068         uint16_t index = 0;
00069         for (; it!=_object_list.end(); it++, index++ ) {
00070             //Free allocated memory for object instances.
00071             obj = *it;
00072             tr_debug("  deleting object %p", obj);
00073             delete obj;
00074         }
00075 
00076         _object_list.clear();
00077     }
00078 
00079     free_resources();
00080 }
00081 
00082 M2MObject* M2MEndpoint::create_object(const String &name)
00083 {
00084     M2MObject *obj = NULL;
00085     if (object(name) == NULL) {
00086         char *path = create_path(*this, name.c_str());
00087         obj = new M2MObject(name, path, false);
00088         if (obj != NULL) {
00089             obj->set_endpoint(this);
00090             _object_list.push_back(obj);
00091         }
00092     }
00093     return obj;
00094 }
00095 
00096 bool M2MEndpoint::remove_object(const String &name)
00097 {
00098     bool success = false;
00099     if (object_count() == 0) {
00100         return success;
00101     }
00102     M2MObjectList::const_iterator it;
00103     M2MObject *obj = NULL;
00104     int pos = 0;
00105     it = _object_list.begin();
00106     for (; it != _object_list.end(); it++, pos++) {
00107         obj = *it;
00108         if (name == obj->name()) {
00109             delete obj;
00110             _object_list.erase(pos);
00111             success = true;
00112             break;
00113         }
00114     }
00115     return success;
00116 
00117 }
00118 
00119 M2MObject* M2MEndpoint::object(const String &name) const
00120 {
00121     M2MObject *obj = NULL;
00122     if (object_count() == 0) {
00123         return obj;
00124     }
00125     M2MObjectList::const_iterator it = _object_list.begin();
00126     for (; it != _object_list.end(); it++) {
00127         if (name == (*it)->name()) {
00128             obj = *it;
00129             break;
00130         }
00131     }
00132     return obj;
00133 }
00134 
00135 const M2MObjectList& M2MEndpoint::objects() const
00136 {
00137     return _object_list;
00138 }
00139 
00140 uint16_t M2MEndpoint::object_count() const
00141 {
00142     return _object_list.size();
00143 }
00144 
00145 M2MObservationHandler* M2MEndpoint::observation_handler() const
00146 {
00147     return _observation_handler;
00148 }
00149 
00150 void M2MEndpoint::set_observation_handler(M2MObservationHandler *handler)
00151 {
00152     _observation_handler = handler;
00153 }
00154 
00155 void M2MEndpoint::add_observation_level(M2MBase::Observation observation_level)
00156 {
00157     (void)observation_level;
00158 }
00159 
00160 void M2MEndpoint::remove_observation_level(M2MBase::Observation observation_level)
00161 {
00162     (void)observation_level;
00163 }
00164 
00165 sn_coap_hdr_s* M2MEndpoint::handle_get_request(nsdl_s *nsdl,
00166                                              sn_coap_hdr_s *received_coap_header,
00167                                              M2MObservationHandler *observation_handler)
00168 {
00169     tr_debug("M2MEndpoint::handle_get_request()");
00170     sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
00171     sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
00172             received_coap_header, msg_code);
00173     return coap_response;
00174 
00175 }
00176 
00177 sn_coap_hdr_s* M2MEndpoint::handle_put_request(nsdl_s *nsdl,
00178                                              sn_coap_hdr_s *received_coap_header,
00179                                              M2MObservationHandler */*observation_handler*/,
00180                                              bool &/*execute_value_updated*/)
00181 {
00182     tr_debug("M2MEndpoint::handle_put_request()");
00183     sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
00184     sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
00185             received_coap_header, msg_code);
00186     return coap_response;
00187 }
00188 
00189 
00190 sn_coap_hdr_s* M2MEndpoint::handle_post_request(nsdl_s *nsdl,
00191                                               sn_coap_hdr_s *received_coap_header,
00192                                               M2MObservationHandler *observation_handler,
00193                                               bool &execute_value_updated,
00194                                               sn_nsdl_addr_s *)
00195 {
00196     tr_debug("M2MEndpoint::handle_post_request()");
00197     sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
00198     sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
00199             received_coap_header, msg_code);
00200     return coap_response;
00201 }
00202 
00203 void M2MEndpoint::set_context(void *ctx)
00204 {
00205     _ctx = ctx;
00206 }
00207 
00208 void* M2MEndpoint::get_context() const
00209 {
00210     return _ctx;
00211 }
00212 
00213 void M2MEndpoint::set_changed()
00214 {
00215     _changed = true;
00216 }
00217 
00218 void M2MEndpoint::clear_changed()
00219 {
00220     _changed = false;
00221 }
00222 
00223 bool M2MEndpoint::get_changed() const
00224 {
00225     return _changed;
00226 }
00227 
00228 void M2MEndpoint::set_deleted()
00229 {
00230     _deleted = true;
00231     set_changed();
00232 }
00233 
00234 bool M2MEndpoint::is_deleted()
00235 {
00236     return _deleted;
00237 }
00238 
00239 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION