mbed Connector Endpoint interface. This interface permits a mbed endpoint to easily setup MDS resources and emit those resources to an MDS server.
Dependents: IoT_LED_demo ServoTest uWater_Project hackathon ... more
api/DynamicResource.cpp@31:bacc63106754, 2015-04-08 (annotated)
- Committer:
- ansond
- Date:
- Wed Apr 08 15:49:56 2015 +0000
- Revision:
- 31:bacc63106754
- Parent:
- 30:113c2a1d8db2
- Child:
- 36:1c6c45584c13
updates for tasklet observing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:b438482ebbfc | 1 | /** |
ansond | 0:b438482ebbfc | 2 | * @file DynamicResource.cpp |
ansond | 0:b438482ebbfc | 3 | * @brief mbed CoAP Endpoint Dynamic Resource class |
ansond | 0:b438482ebbfc | 4 | * @author Doug Anson/Chris Paola |
ansond | 0:b438482ebbfc | 5 | * @version 1.0 |
sam_grove | 2:853f9ecc12df | 6 | * @see |
ansond | 0:b438482ebbfc | 7 | * |
ansond | 0:b438482ebbfc | 8 | * Copyright (c) 2014 |
ansond | 0:b438482ebbfc | 9 | * |
ansond | 0:b438482ebbfc | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 0:b438482ebbfc | 11 | * you may not use this file except in compliance with the License. |
ansond | 0:b438482ebbfc | 12 | * You may obtain a copy of the License at |
ansond | 0:b438482ebbfc | 13 | * |
ansond | 0:b438482ebbfc | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 0:b438482ebbfc | 15 | * |
ansond | 0:b438482ebbfc | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 0:b438482ebbfc | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 0:b438482ebbfc | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 0:b438482ebbfc | 19 | * See the License for the specific language governing permissions and |
ansond | 0:b438482ebbfc | 20 | * limitations under the License. |
ansond | 0:b438482ebbfc | 21 | */ |
sam_grove | 2:853f9ecc12df | 22 | |
sam_grove | 2:853f9ecc12df | 23 | #include "DynamicResource.h" |
sam_grove | 2:853f9ecc12df | 24 | |
sam_grove | 2:853f9ecc12df | 25 | // InstancePointerTable Helper |
sam_grove | 2:853f9ecc12df | 26 | #include "InstancePointerTableHelper.h" |
sam_grove | 2:853f9ecc12df | 27 | |
ansond | 31:bacc63106754 | 28 | // ResourceObserver help |
ansond | 31:bacc63106754 | 29 | #include "ResourceObserver.h" |
ansond | 31:bacc63106754 | 30 | |
ansond | 31:bacc63106754 | 31 | // Start/Stop Observations... |
ansond | 31:bacc63106754 | 32 | #define START_OBS 0 |
ansond | 31:bacc63106754 | 33 | #define STOP_OBS 1 |
ansond | 31:bacc63106754 | 34 | |
sam_grove | 2:853f9ecc12df | 35 | // default constructor |
sam_grove | 2:853f9ecc12df | 36 | DynamicResource::DynamicResource(const Logger *logger,const char *name,const char *res_type,uint8_t res_mask,const bool observable) : Resource<string>(logger,string(name),string("")) |
sam_grove | 2:853f9ecc12df | 37 | { |
ansond | 0:b438482ebbfc | 38 | this->m_res_type = string(res_type); |
ansond | 0:b438482ebbfc | 39 | this->m_observable = observable; |
sam_grove | 2:853f9ecc12df | 40 | this->m_res_mask = res_mask; |
ansond | 21:8487990a3baa | 41 | this->m_obs_number = 0; |
ansond | 21:8487990a3baa | 42 | this->m_obs_token_ptr = NULL; |
ansond | 21:8487990a3baa | 43 | this->m_obs_token_len = 0; |
ansond | 24:a6915e19814e | 44 | this->m_data_wrapper = NULL; |
ansond | 31:bacc63106754 | 45 | this->m_observer = NULL; |
sam_grove | 2:853f9ecc12df | 46 | } |
sam_grove | 2:853f9ecc12df | 47 | |
sam_grove | 2:853f9ecc12df | 48 | // constructor (input initial value) |
sam_grove | 2:853f9ecc12df | 49 | DynamicResource::DynamicResource(const Logger *logger,const char *name,const char *res_type,const string value,uint8_t res_mask,const bool observable) : Resource<string>(logger,string(name),value) |
sam_grove | 2:853f9ecc12df | 50 | { |
ansond | 0:b438482ebbfc | 51 | this->m_res_type = string(res_type); |
ansond | 0:b438482ebbfc | 52 | this->m_observable = observable; |
sam_grove | 2:853f9ecc12df | 53 | this->m_res_mask = res_mask; |
ansond | 21:8487990a3baa | 54 | this->m_obs_number = 0; |
ansond | 21:8487990a3baa | 55 | this->m_obs_token_ptr = NULL; |
ansond | 21:8487990a3baa | 56 | this->m_obs_token_len = 0; |
ansond | 24:a6915e19814e | 57 | this->m_data_wrapper = NULL; |
sam_grove | 2:853f9ecc12df | 58 | } |
sam_grove | 2:853f9ecc12df | 59 | |
sam_grove | 2:853f9ecc12df | 60 | // constructor (strings) |
sam_grove | 2:853f9ecc12df | 61 | DynamicResource::DynamicResource(const Logger *logger,const string name,const string res_type,const string value,uint8_t res_mask,const bool observable) : Resource<string>(logger,name,value) |
sam_grove | 2:853f9ecc12df | 62 | { |
ansond | 0:b438482ebbfc | 63 | this->m_res_type = res_type; |
ansond | 0:b438482ebbfc | 64 | this->m_observable = observable; |
sam_grove | 2:853f9ecc12df | 65 | this->m_res_mask = res_mask; |
ansond | 21:8487990a3baa | 66 | this->m_obs_number = 0; |
ansond | 21:8487990a3baa | 67 | this->m_obs_token_ptr = NULL; |
ansond | 21:8487990a3baa | 68 | this->m_obs_token_len = 0; |
ansond | 24:a6915e19814e | 69 | this->m_data_wrapper = NULL; |
sam_grove | 2:853f9ecc12df | 70 | } |
sam_grove | 2:853f9ecc12df | 71 | |
sam_grove | 2:853f9ecc12df | 72 | // copy constructor |
sam_grove | 2:853f9ecc12df | 73 | DynamicResource::DynamicResource(const DynamicResource &resource) : Resource<string>((const Resource<string> &)resource) |
sam_grove | 2:853f9ecc12df | 74 | { |
ansond | 0:b438482ebbfc | 75 | this->m_res_type = resource.m_res_type; |
ansond | 0:b438482ebbfc | 76 | this->m_observable = resource.m_observable; |
ansond | 0:b438482ebbfc | 77 | this->m_res_mask = resource.m_res_mask; |
ansond | 21:8487990a3baa | 78 | this->m_obs_number = resource.m_obs_number; |
ansond | 21:8487990a3baa | 79 | this->m_obs_token_ptr = resource.m_obs_token_ptr; |
ansond | 21:8487990a3baa | 80 | this->m_obs_token_len = resource.m_obs_token_len; |
ansond | 24:a6915e19814e | 81 | this->m_data_wrapper = resource.m_data_wrapper; |
sam_grove | 2:853f9ecc12df | 82 | } |
sam_grove | 2:853f9ecc12df | 83 | |
sam_grove | 2:853f9ecc12df | 84 | // destructor |
sam_grove | 2:853f9ecc12df | 85 | DynamicResource::~DynamicResource() |
sam_grove | 2:853f9ecc12df | 86 | { |
ansond | 21:8487990a3baa | 87 | if(this->m_obs_token_ptr) free(this->m_obs_token_ptr); |
sam_grove | 2:853f9ecc12df | 88 | } |
sam_grove | 2:853f9ecc12df | 89 | |
sam_grove | 2:853f9ecc12df | 90 | // bind resource to NSDL |
sam_grove | 2:853f9ecc12df | 91 | void DynamicResource::bind(void *p) |
sam_grove | 2:853f9ecc12df | 92 | { |
ansond | 0:b438482ebbfc | 93 | if (p != NULL) { |
ansond | 0:b438482ebbfc | 94 | sn_nsdl_resource_info_s *resource_ptr = (sn_nsdl_resource_info_s *)p; |
ansond | 0:b438482ebbfc | 95 | const uint8_t *name = (const uint8_t *)(this->getName().c_str()); |
ansond | 0:b438482ebbfc | 96 | const uint8_t *res_type = (const uint8_t *)this->m_res_type.c_str(); |
ansond | 0:b438482ebbfc | 97 | int name_length = this->getName().size(); |
ansond | 0:b438482ebbfc | 98 | int res_type_length = this->m_res_type.size(); |
sam_grove | 2:853f9ecc12df | 99 | int is_observable = 0; |
sam_grove | 2:853f9ecc12df | 100 | if (this->m_observable == true) is_observable = 1; |
ansond | 0:b438482ebbfc | 101 | const string *key = new string(this->getName()); |
ansond | 0:b438482ebbfc | 102 | ipt_helper_add_instance_pointer(key,this); |
ansond | 0:b438482ebbfc | 103 | nsdl_create_dynamic_resource(resource_ptr,name_length,(uint8_t *)name,res_type_length,(uint8_t *)res_type,is_observable,&ipt_helper_nsdl_callback_stub,this->m_res_mask); |
ansond | 20:abfbaf524192 | 104 | this->logger()->log("DynamicResource: [%s] type: [%s] bound (observable: %d)",name,res_type,is_observable); |
sam_grove | 2:853f9ecc12df | 105 | } else { |
ansond | 5:a929d65eb385 | 106 | this->logger()->log("DynamicResource: NULL parameter in bind()"); |
ansond | 0:b438482ebbfc | 107 | } |
sam_grove | 2:853f9ecc12df | 108 | } |
ansond | 0:b438482ebbfc | 109 | |
sam_grove | 2:853f9ecc12df | 110 | // process NSDL message |
sam_grove | 2:853f9ecc12df | 111 | uint8_t DynamicResource::process(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto) |
sam_grove | 2:853f9ecc12df | 112 | { |
ansond | 0:b438482ebbfc | 113 | sn_coap_hdr_s *coap_res_ptr = 0; |
ansond | 31:bacc63106754 | 114 | |
ansond | 0:b438482ebbfc | 115 | // create our key for debugging output... |
ansond | 25:1fc958ac14d1 | 116 | DataWrapper *hold = this->getDataWrapper(); |
ansond | 25:1fc958ac14d1 | 117 | this->setDataWrapper(NULL); |
ansond | 0:b438482ebbfc | 118 | string key = this->coapDataToString(received_coap_ptr->uri_path_ptr,received_coap_ptr->uri_path_len); |
ansond | 25:1fc958ac14d1 | 119 | this->setDataWrapper(hold); |
sam_grove | 2:853f9ecc12df | 120 | |
sam_grove | 2:853f9ecc12df | 121 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) { |
ansond | 0:b438482ebbfc | 122 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
sam_grove | 2:853f9ecc12df | 123 | |
sam_grove | 2:853f9ecc12df | 124 | // process the GET if we have registered a callback for it... |
ansond | 0:b438482ebbfc | 125 | if ((this->m_res_mask&SN_GRS_GET_ALLOWED) != 0) { |
ansond | 0:b438482ebbfc | 126 | // call the resource get() to get the resource value |
ansond | 5:a929d65eb385 | 127 | this->logger()->log("Calling resource(GET) for [%s]...",key.c_str()); |
ansond | 0:b438482ebbfc | 128 | string value = this->get(); |
sam_grove | 2:853f9ecc12df | 129 | |
ansond | 24:a6915e19814e | 130 | // convert the string from the GET to something suitable for CoAP payloads |
ansond | 24:a6915e19814e | 131 | if (this->getDataWrapper() != NULL) { |
ansond | 24:a6915e19814e | 132 | // wrap the data... |
ansond | 24:a6915e19814e | 133 | this->getDataWrapper()->wrap((uint8_t *)value.c_str(),(int)value.size()); |
ansond | 24:a6915e19814e | 134 | |
ansond | 24:a6915e19814e | 135 | // announce (after wrap) |
ansond | 24:a6915e19814e | 136 | this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),this->getDataWrapper()->get()); |
ansond | 24:a6915e19814e | 137 | |
ansond | 24:a6915e19814e | 138 | // fill in the CoAP response payload |
ansond | 24:a6915e19814e | 139 | coap_res_ptr->payload_len = this->getDataWrapper()->length(); |
ansond | 24:a6915e19814e | 140 | coap_res_ptr->payload_ptr = this->getDataWrapper()->get(); |
ansond | 24:a6915e19814e | 141 | } |
ansond | 24:a6915e19814e | 142 | else { |
ansond | 24:a6915e19814e | 143 | // announce (no wrap) |
ansond | 24:a6915e19814e | 144 | this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),value.c_str()); |
ansond | 24:a6915e19814e | 145 | |
ansond | 24:a6915e19814e | 146 | // do not wrap the data... |
ansond | 24:a6915e19814e | 147 | coap_res_ptr->payload_len = value.size(); |
ansond | 24:a6915e19814e | 148 | coap_res_ptr->payload_ptr = (uint8_t *)value.c_str(); |
ansond | 24:a6915e19814e | 149 | } |
ansond | 21:8487990a3baa | 150 | |
ansond | 21:8487990a3baa | 151 | // Observation handling... |
ansond | 21:8487990a3baa | 152 | if(received_coap_ptr->token_ptr) { |
ansond | 21:8487990a3baa | 153 | if(this->m_obs_token_ptr) { |
ansond | 21:8487990a3baa | 154 | free(this->m_obs_token_ptr); |
ansond | 21:8487990a3baa | 155 | this->m_obs_token_ptr = NULL; |
ansond | 23:caa0260acc21 | 156 | this->m_obs_token_len = 0; |
ansond | 21:8487990a3baa | 157 | } |
ansond | 22:192b598ba389 | 158 | |
ansond | 21:8487990a3baa | 159 | this->m_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); |
ansond | 21:8487990a3baa | 160 | if(this->m_obs_token_ptr) { |
ansond | 21:8487990a3baa | 161 | memcpy(this->m_obs_token_ptr, received_coap_ptr->token_ptr,received_coap_ptr->token_len); |
ansond | 21:8487990a3baa | 162 | this->m_obs_token_len = received_coap_ptr->token_len; |
ansond | 21:8487990a3baa | 163 | } |
ansond | 21:8487990a3baa | 164 | } |
ansond | 21:8487990a3baa | 165 | |
ansond | 22:192b598ba389 | 166 | // Observation handling... |
ansond | 22:192b598ba389 | 167 | if(received_coap_ptr->options_list_ptr && received_coap_ptr->options_list_ptr->observe) { |
ansond | 31:bacc63106754 | 168 | // ResourceObserver |
ansond | 31:bacc63106754 | 169 | ResourceObserver *observer = (ResourceObserver *)this->m_observer; |
ansond | 31:bacc63106754 | 170 | |
ansond | 31:bacc63106754 | 171 | // create the options list pointer |
ansond | 21:8487990a3baa | 172 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
ansond | 21:8487990a3baa | 173 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
ansond | 31:bacc63106754 | 174 | |
ansond | 31:bacc63106754 | 175 | // get observe start/stop value from received options list pointer |
ansond | 31:bacc63106754 | 176 | uint8_t OBS_command = *received_coap_ptr->options_list_ptr->observe_ptr; |
ansond | 31:bacc63106754 | 177 | if (OBS_command == START_OBS) { |
ansond | 31:bacc63106754 | 178 | coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below... |
ansond | 31:bacc63106754 | 179 | coap_res_ptr->options_list_ptr->observe_len = 1; |
ansond | 31:bacc63106754 | 180 | this->m_obs_number++; |
ansond | 31:bacc63106754 | 181 | if (observer != NULL) observer->beginObservation(); |
ansond | 31:bacc63106754 | 182 | } |
ansond | 31:bacc63106754 | 183 | if (OBS_command == STOP_OBS) { |
ansond | 31:bacc63106754 | 184 | if (observer != NULL) observer->stopObservation(); |
ansond | 31:bacc63106754 | 185 | } |
ansond | 21:8487990a3baa | 186 | } |
sam_grove | 2:853f9ecc12df | 187 | |
sam_grove | 2:853f9ecc12df | 188 | // build out the response and send... |
ansond | 21:8487990a3baa | 189 | sn_nsdl_send_coap_message(address,coap_res_ptr); |
ansond | 22:192b598ba389 | 190 | |
ansond | 22:192b598ba389 | 191 | // |
ansond | 22:192b598ba389 | 192 | // nullify note: |
ansond | 22:192b598ba389 | 193 | // |
ansond | 22:192b598ba389 | 194 | // because our obs_number (assigned to observe_ptr) is part of this object instance, we dont |
ansond | 22:192b598ba389 | 195 | // want to have the underlying free() try to free it... to just nullify here |
ansond | 22:192b598ba389 | 196 | // |
ansond | 22:192b598ba389 | 197 | if (coap_res_ptr->options_list_ptr) coap_res_ptr->options_list_ptr->observe_ptr = 0; |
ansond | 21:8487990a3baa | 198 | } |
ansond | 21:8487990a3baa | 199 | else { |
ansond | 5:a929d65eb385 | 200 | this->logger()->log("ERROR: resource(GET) mask is munged (mask: 0x%x)",this->m_res_mask); |
ansond | 0:b438482ebbfc | 201 | } |
sam_grove | 2:853f9ecc12df | 202 | } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) { |
sam_grove | 2:853f9ecc12df | 203 | if(received_coap_ptr->payload_len > 0) { |
ansond | 0:b438482ebbfc | 204 | // process the PUT if we have registered a callback for it... |
ansond | 0:b438482ebbfc | 205 | if ((this->m_res_mask&SN_GRS_PUT_ALLOWED) != 0) { |
ansond | 28:8fd9336edeb9 | 206 | // put() delivers values as std::string |
ansond | 0:b438482ebbfc | 207 | string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len); |
sam_grove | 2:853f9ecc12df | 208 | |
ansond | 0:b438482ebbfc | 209 | // call the resource put() to set the resource value |
ansond | 5:a929d65eb385 | 210 | this->logger()->log("Calling resource(PUT) with [%s]=[%s]...",key.c_str(),value.c_str()); |
ansond | 0:b438482ebbfc | 211 | this->put(value); |
sam_grove | 2:853f9ecc12df | 212 | |
ansond | 0:b438482ebbfc | 213 | // build out the response and send... |
ansond | 5:a929d65eb385 | 214 | this->logger()->log("resource(PUT) completed for [%s]...",key.c_str()); |
ansond | 21:8487990a3baa | 215 | coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED); |
ansond | 21:8487990a3baa | 216 | sn_nsdl_send_coap_message(address,coap_res_ptr); |
sam_grove | 2:853f9ecc12df | 217 | } else { |
ansond | 5:a929d65eb385 | 218 | this->logger()->log("ERROR: resource(PUT) mask is munged (mask: 0x%x)",this->m_res_mask); |
ansond | 0:b438482ebbfc | 219 | } |
sam_grove | 2:853f9ecc12df | 220 | } else { |
ansond | 5:a929d65eb385 | 221 | this->logger()->log("ERROR: Binder(PUT) payload is NULL..."); |
ansond | 0:b438482ebbfc | 222 | } |
ansond | 21:8487990a3baa | 223 | } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) { |
ansond | 21:8487990a3baa | 224 | if(received_coap_ptr->payload_len > 0) { |
ansond | 21:8487990a3baa | 225 | // process the POST if we have registered a callback for it... |
ansond | 21:8487990a3baa | 226 | if ((this->m_res_mask&SN_GRS_POST_ALLOWED) != 0) { |
ansond | 28:8fd9336edeb9 | 227 | // post() delivers values as std::string |
ansond | 21:8487990a3baa | 228 | string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len); |
ansond | 21:8487990a3baa | 229 | |
ansond | 21:8487990a3baa | 230 | // call the resource post() to set the resource value |
ansond | 21:8487990a3baa | 231 | this->logger()->log("Calling resource(POST) with [%s]=[%s]...",key.c_str(),value.c_str()); |
ansond | 21:8487990a3baa | 232 | this->post(value); |
ansond | 21:8487990a3baa | 233 | |
ansond | 21:8487990a3baa | 234 | // build out the response and send... |
ansond | 21:8487990a3baa | 235 | this->logger()->log("resource(POST) completed for [%s]...",key.c_str()); |
ansond | 21:8487990a3baa | 236 | coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED); |
ansond | 21:8487990a3baa | 237 | sn_nsdl_send_coap_message(address,coap_res_ptr); |
ansond | 21:8487990a3baa | 238 | } else { |
ansond | 21:8487990a3baa | 239 | this->logger()->log("ERROR: resource(POST) mask is munged (mask: 0x%x)",this->m_res_mask); |
ansond | 21:8487990a3baa | 240 | } |
ansond | 21:8487990a3baa | 241 | } else { |
ansond | 21:8487990a3baa | 242 | this->logger()->log("ERROR: Binder(POST) payload is NULL..."); |
ansond | 21:8487990a3baa | 243 | } |
ansond | 21:8487990a3baa | 244 | } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) { |
ansond | 21:8487990a3baa | 245 | if(received_coap_ptr->payload_len > 0) { |
ansond | 21:8487990a3baa | 246 | // process the DELETE if we have registered a callback for it... |
ansond | 21:8487990a3baa | 247 | if ((this->m_res_mask&SN_GRS_DELETE_ALLOWED) != 0) { |
ansond | 28:8fd9336edeb9 | 248 | // del() delivers values as std::string |
ansond | 21:8487990a3baa | 249 | string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len); |
ansond | 21:8487990a3baa | 250 | |
ansond | 21:8487990a3baa | 251 | // call the resource del() to set the resource value |
ansond | 21:8487990a3baa | 252 | this->logger()->log("Calling resource(DELETE) with [%s]=[%s]...",key.c_str(),value.c_str()); |
ansond | 21:8487990a3baa | 253 | this->del(value); |
ansond | 21:8487990a3baa | 254 | |
ansond | 21:8487990a3baa | 255 | // build out the response and send... |
ansond | 21:8487990a3baa | 256 | this->logger()->log("resource(DELETE) completed for [%s]...",key.c_str()); |
ansond | 21:8487990a3baa | 257 | coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED); |
ansond | 21:8487990a3baa | 258 | sn_nsdl_send_coap_message(address,coap_res_ptr); |
ansond | 21:8487990a3baa | 259 | } else { |
ansond | 21:8487990a3baa | 260 | this->logger()->log("ERROR: resource(DELETE) mask is munged (mask: 0x%x)",this->m_res_mask); |
ansond | 21:8487990a3baa | 261 | } |
ansond | 21:8487990a3baa | 262 | } else { |
ansond | 21:8487990a3baa | 263 | this->logger()->log("ERROR: Binder(DELETE) payload is NULL..."); |
ansond | 21:8487990a3baa | 264 | } |
ansond | 0:b438482ebbfc | 265 | } |
ansond | 0:b438482ebbfc | 266 | |
ansond | 0:b438482ebbfc | 267 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
ansond | 22:192b598ba389 | 268 | |
ansond | 0:b438482ebbfc | 269 | return 0; |
sam_grove | 2:853f9ecc12df | 270 | } |
sam_grove | 2:853f9ecc12df | 271 | |
ansond | 21:8487990a3baa | 272 | // send the notification |
ansond | 21:8487990a3baa | 273 | int DynamicResource::notify(const string data) { |
ansond | 21:8487990a3baa | 274 | return this->notify((uint8_t *)data.c_str(),(int)data.length()); |
ansond | 21:8487990a3baa | 275 | } |
ansond | 21:8487990a3baa | 276 | |
ansond | 21:8487990a3baa | 277 | // send the notification |
ansond | 21:8487990a3baa | 278 | int DynamicResource::notify(uint8_t *data,int data_length) { |
ansond | 27:eb6818ad257a | 279 | uint8_t *notify_data = NULL; |
ansond | 27:eb6818ad257a | 280 | int notify_data_length = 0; |
ansond | 27:eb6818ad257a | 281 | |
ansond | 27:eb6818ad257a | 282 | // convert the string from the GET to something suitable for CoAP payloads |
ansond | 27:eb6818ad257a | 283 | if (this->getDataWrapper() != NULL) { |
ansond | 27:eb6818ad257a | 284 | // wrap the data... |
ansond | 27:eb6818ad257a | 285 | this->getDataWrapper()->wrap((uint8_t *)data,data_length); |
ansond | 27:eb6818ad257a | 286 | |
ansond | 27:eb6818ad257a | 287 | // announce (after wrap) |
ansond | 27:eb6818ad257a | 288 | this->logger()->log("Notify payload [%s]...",this->getDataWrapper()->get()); |
ansond | 27:eb6818ad257a | 289 | |
ansond | 27:eb6818ad257a | 290 | // fill notify |
ansond | 27:eb6818ad257a | 291 | notify_data_length = this->getDataWrapper()->length(); |
ansond | 27:eb6818ad257a | 292 | notify_data = this->getDataWrapper()->get(); |
ansond | 27:eb6818ad257a | 293 | } |
ansond | 27:eb6818ad257a | 294 | else { |
ansond | 27:eb6818ad257a | 295 | // announce (no wrap) |
ansond | 27:eb6818ad257a | 296 | this->logger()->log("Notify payload [%s]...",data); |
ansond | 27:eb6818ad257a | 297 | |
ansond | 27:eb6818ad257a | 298 | // do not wrap the data... |
ansond | 27:eb6818ad257a | 299 | notify_data_length = data_length; |
ansond | 27:eb6818ad257a | 300 | notify_data = data; |
ansond | 27:eb6818ad257a | 301 | } |
ansond | 27:eb6818ad257a | 302 | |
ansond | 27:eb6818ad257a | 303 | // send the observation... |
ansond | 27:eb6818ad257a | 304 | int status = sn_nsdl_send_observation_notification(this->m_obs_token_ptr,this->m_obs_token_len,notify_data,notify_data_length,&this->m_obs_number,1,COAP_MSG_TYPE_NON_CONFIRMABLE,0); |
ansond | 21:8487990a3baa | 305 | if (status == 0) { |
ansond | 21:8487990a3baa | 306 | this->logger()->log("ERROR: resource(NOTIFY) send failed..."); |
ansond | 21:8487990a3baa | 307 | } |
ansond | 27:eb6818ad257a | 308 | |
ansond | 27:eb6818ad257a | 309 | // return our status |
ansond | 21:8487990a3baa | 310 | return status; |
ansond | 21:8487990a3baa | 311 | } |
ansond | 21:8487990a3baa | 312 | |
sam_grove | 2:853f9ecc12df | 313 | // default PUT (does nothing) |
sam_grove | 2:853f9ecc12df | 314 | void DynamicResource::put(const string value) |
sam_grove | 2:853f9ecc12df | 315 | { |
sam_grove | 2:853f9ecc12df | 316 | // not used by default |
sam_grove | 2:853f9ecc12df | 317 | ; |
sam_grove | 2:853f9ecc12df | 318 | } |
sam_grove | 2:853f9ecc12df | 319 | |
ansond | 21:8487990a3baa | 320 | // default POST (does nothing) |
ansond | 21:8487990a3baa | 321 | void DynamicResource::post(const string value) |
ansond | 21:8487990a3baa | 322 | { |
ansond | 21:8487990a3baa | 323 | // not used by default |
ansond | 21:8487990a3baa | 324 | ; |
ansond | 21:8487990a3baa | 325 | } |
ansond | 21:8487990a3baa | 326 | |
ansond | 21:8487990a3baa | 327 | // default DELETE (does nothing) |
ansond | 21:8487990a3baa | 328 | void DynamicResource::del(const string value) |
ansond | 21:8487990a3baa | 329 | { |
ansond | 21:8487990a3baa | 330 | // not used by default |
ansond | 21:8487990a3baa | 331 | ; |
ansond | 21:8487990a3baa | 332 | } |
ansond | 21:8487990a3baa | 333 | |
ansond | 30:113c2a1d8db2 | 334 | // default observe behavior |
ansond | 30:113c2a1d8db2 | 335 | void DynamicResource::observe() { |
ansond | 30:113c2a1d8db2 | 336 | if (this->m_observable == true) { |
ansond | 30:113c2a1d8db2 | 337 | this->notify(this->get()); |
ansond | 30:113c2a1d8db2 | 338 | } |
ansond | 30:113c2a1d8db2 | 339 | } |
ansond | 30:113c2a1d8db2 | 340 | |
ansond | 31:bacc63106754 | 341 | // set the observer pointer |
ansond | 31:bacc63106754 | 342 | void DynamicResource::setObserver(void *observer) { |
ansond | 31:bacc63106754 | 343 | this->m_observer = observer; |
ansond | 31:bacc63106754 | 344 | } |
ansond | 31:bacc63106754 | 345 | |
sam_grove | 2:853f9ecc12df | 346 | // convenience method to get the URI from its buffer field... |
sam_grove | 2:853f9ecc12df | 347 | string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length) |
sam_grove | 2:853f9ecc12df | 348 | { |
ansond | 0:b438482ebbfc | 349 | if (coap_data_ptr != NULL && coap_data_ptr_length > 0) { |
ansond | 24:a6915e19814e | 350 | if (this->getDataWrapper() != NULL) { |
ansond | 24:a6915e19814e | 351 | // unwrap the data... |
ansond | 24:a6915e19814e | 352 | this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length); |
ansond | 24:a6915e19814e | 353 | char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper... |
ansond | 24:a6915e19814e | 354 | return string(buf); |
ansond | 24:a6915e19814e | 355 | } |
ansond | 24:a6915e19814e | 356 | else { |
ansond | 24:a6915e19814e | 357 | // no unwrap of the data... |
ansond | 24:a6915e19814e | 358 | char buf[MAX_VALUE_BUFFER_LENGTH+1]; |
ansond | 24:a6915e19814e | 359 | memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1); |
ansond | 24:a6915e19814e | 360 | memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length); |
ansond | 24:a6915e19814e | 361 | return string(buf); |
ansond | 24:a6915e19814e | 362 | } |
ansond | 0:b438482ebbfc | 363 | } |
ansond | 0:b438482ebbfc | 364 | return string(""); |
sam_grove | 2:853f9ecc12df | 365 | } |