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

Committer:
ansond
Date:
Fri Mar 13 06:22:41 2015 +0000
Revision:
22:192b598ba389
Parent:
21:8487990a3baa
Child:
23:caa0260acc21
notifications working now...

Who changed what in which revision?

UserRevisionLine numberNew 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
sam_grove 2:853f9ecc12df 28 // default constructor
sam_grove 2:853f9ecc12df 29 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 30 {
ansond 0:b438482ebbfc 31 this->m_res_type = string(res_type);
ansond 0:b438482ebbfc 32 this->m_observable = observable;
sam_grove 2:853f9ecc12df 33 this->m_res_mask = res_mask;
ansond 21:8487990a3baa 34 this->m_obs_number = 0;
ansond 21:8487990a3baa 35 this->m_obs_token_ptr = NULL;
ansond 21:8487990a3baa 36 this->m_obs_token_len = 0;
sam_grove 2:853f9ecc12df 37 }
sam_grove 2:853f9ecc12df 38
sam_grove 2:853f9ecc12df 39 // constructor (input initial value)
sam_grove 2:853f9ecc12df 40 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 41 {
ansond 0:b438482ebbfc 42 this->m_res_type = string(res_type);
ansond 0:b438482ebbfc 43 this->m_observable = observable;
sam_grove 2:853f9ecc12df 44 this->m_res_mask = res_mask;
ansond 21:8487990a3baa 45 this->m_obs_number = 0;
ansond 21:8487990a3baa 46 this->m_obs_token_ptr = NULL;
ansond 21:8487990a3baa 47 this->m_obs_token_len = 0;
sam_grove 2:853f9ecc12df 48 }
sam_grove 2:853f9ecc12df 49
sam_grove 2:853f9ecc12df 50 // constructor (strings)
sam_grove 2:853f9ecc12df 51 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 52 {
ansond 0:b438482ebbfc 53 this->m_res_type = res_type;
ansond 0:b438482ebbfc 54 this->m_observable = observable;
sam_grove 2:853f9ecc12df 55 this->m_res_mask = res_mask;
ansond 21:8487990a3baa 56 this->m_obs_number = 0;
ansond 21:8487990a3baa 57 this->m_obs_token_ptr = NULL;
ansond 21:8487990a3baa 58 this->m_obs_token_len = 0;
sam_grove 2:853f9ecc12df 59 }
sam_grove 2:853f9ecc12df 60
sam_grove 2:853f9ecc12df 61 // copy constructor
sam_grove 2:853f9ecc12df 62 DynamicResource::DynamicResource(const DynamicResource &resource) : Resource<string>((const Resource<string> &)resource)
sam_grove 2:853f9ecc12df 63 {
ansond 0:b438482ebbfc 64 this->m_res_type = resource.m_res_type;
ansond 0:b438482ebbfc 65 this->m_observable = resource.m_observable;
ansond 0:b438482ebbfc 66 this->m_res_mask = resource.m_res_mask;
ansond 21:8487990a3baa 67 this->m_obs_number = resource.m_obs_number;
ansond 21:8487990a3baa 68 this->m_obs_token_ptr = resource.m_obs_token_ptr;
ansond 21:8487990a3baa 69 this->m_obs_token_len = resource.m_obs_token_len;
sam_grove 2:853f9ecc12df 70 }
sam_grove 2:853f9ecc12df 71
sam_grove 2:853f9ecc12df 72 // destructor
sam_grove 2:853f9ecc12df 73 DynamicResource::~DynamicResource()
sam_grove 2:853f9ecc12df 74 {
ansond 21:8487990a3baa 75 if(this->m_obs_token_ptr) free(this->m_obs_token_ptr);
sam_grove 2:853f9ecc12df 76 }
sam_grove 2:853f9ecc12df 77
sam_grove 2:853f9ecc12df 78 // bind resource to NSDL
sam_grove 2:853f9ecc12df 79 void DynamicResource::bind(void *p)
sam_grove 2:853f9ecc12df 80 {
ansond 0:b438482ebbfc 81 if (p != NULL) {
ansond 0:b438482ebbfc 82 sn_nsdl_resource_info_s *resource_ptr = (sn_nsdl_resource_info_s *)p;
ansond 0:b438482ebbfc 83 const uint8_t *name = (const uint8_t *)(this->getName().c_str());
ansond 0:b438482ebbfc 84 const uint8_t *res_type = (const uint8_t *)this->m_res_type.c_str();
ansond 0:b438482ebbfc 85 int name_length = this->getName().size();
ansond 0:b438482ebbfc 86 int res_type_length = this->m_res_type.size();
sam_grove 2:853f9ecc12df 87 int is_observable = 0;
sam_grove 2:853f9ecc12df 88 if (this->m_observable == true) is_observable = 1;
ansond 0:b438482ebbfc 89 const string *key = new string(this->getName());
ansond 0:b438482ebbfc 90 ipt_helper_add_instance_pointer(key,this);
ansond 0:b438482ebbfc 91 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 92 this->logger()->log("DynamicResource: [%s] type: [%s] bound (observable: %d)",name,res_type,is_observable);
sam_grove 2:853f9ecc12df 93 } else {
ansond 5:a929d65eb385 94 this->logger()->log("DynamicResource: NULL parameter in bind()");
ansond 0:b438482ebbfc 95 }
sam_grove 2:853f9ecc12df 96 }
ansond 0:b438482ebbfc 97
sam_grove 2:853f9ecc12df 98 // process NSDL message
sam_grove 2:853f9ecc12df 99 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 100 {
ansond 0:b438482ebbfc 101 sn_coap_hdr_s *coap_res_ptr = 0;
sam_grove 2:853f9ecc12df 102
ansond 0:b438482ebbfc 103 // create our key for debugging output...
ansond 0:b438482ebbfc 104 string key = this->coapDataToString(received_coap_ptr->uri_path_ptr,received_coap_ptr->uri_path_len);
sam_grove 2:853f9ecc12df 105
sam_grove 2:853f9ecc12df 106 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
ansond 0:b438482ebbfc 107 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
sam_grove 2:853f9ecc12df 108
sam_grove 2:853f9ecc12df 109 // process the GET if we have registered a callback for it...
ansond 0:b438482ebbfc 110 if ((this->m_res_mask&SN_GRS_GET_ALLOWED) != 0) {
ansond 0:b438482ebbfc 111 // call the resource get() to get the resource value
ansond 5:a929d65eb385 112 this->logger()->log("Calling resource(GET) for [%s]...",key.c_str());
ansond 0:b438482ebbfc 113 string value = this->get();
sam_grove 2:853f9ecc12df 114
sam_grove 2:853f9ecc12df 115 // convert the string from the GET to something suitable for CoAP payloads
ansond 5:a929d65eb385 116 this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),value.c_str());
ansond 0:b438482ebbfc 117 int length = value.size();
ansond 0:b438482ebbfc 118 char value_buffer[MAX_VALUE_BUFFER_LENGTH+1];
ansond 0:b438482ebbfc 119 if (length > MAX_VALUE_BUFFER_LENGTH) length = MAX_VALUE_BUFFER_LENGTH;
ansond 0:b438482ebbfc 120 memset(value_buffer,0,MAX_VALUE_BUFFER_LENGTH+1);
sam_grove 2:853f9ecc12df 121 memcpy(value_buffer,value.c_str(),length);
sam_grove 2:853f9ecc12df 122
sam_grove 2:853f9ecc12df 123 // fill in the CoAP response payload
ansond 0:b438482ebbfc 124 coap_res_ptr->payload_len = length;
ansond 0:b438482ebbfc 125 coap_res_ptr->payload_ptr = (uint8_t *)value_buffer;
ansond 21:8487990a3baa 126
ansond 21:8487990a3baa 127 // Observation handling...
ansond 21:8487990a3baa 128 if(received_coap_ptr->token_ptr) {
ansond 21:8487990a3baa 129 if(this->m_obs_token_ptr) {
ansond 21:8487990a3baa 130 free(this->m_obs_token_ptr);
ansond 21:8487990a3baa 131 this->m_obs_token_ptr = NULL;
ansond 21:8487990a3baa 132 }
ansond 22:192b598ba389 133
ansond 21:8487990a3baa 134 this->m_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
ansond 21:8487990a3baa 135 if(this->m_obs_token_ptr) {
ansond 21:8487990a3baa 136 memcpy(this->m_obs_token_ptr, received_coap_ptr->token_ptr,received_coap_ptr->token_len);
ansond 21:8487990a3baa 137 this->m_obs_token_len = received_coap_ptr->token_len;
ansond 21:8487990a3baa 138 }
ansond 21:8487990a3baa 139 }
ansond 21:8487990a3baa 140
ansond 22:192b598ba389 141 // Observation handling...
ansond 22:192b598ba389 142 if(received_coap_ptr->options_list_ptr && received_coap_ptr->options_list_ptr->observe) {
ansond 21:8487990a3baa 143 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
ansond 21:8487990a3baa 144 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
ansond 22:192b598ba389 145 coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below...
ansond 21:8487990a3baa 146 coap_res_ptr->options_list_ptr->observe_len = 1;
ansond 21:8487990a3baa 147 this->m_obs_number++;
ansond 21:8487990a3baa 148 }
sam_grove 2:853f9ecc12df 149
sam_grove 2:853f9ecc12df 150 // build out the response and send...
ansond 21:8487990a3baa 151 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 22:192b598ba389 152
ansond 22:192b598ba389 153 //
ansond 22:192b598ba389 154 // nullify note:
ansond 22:192b598ba389 155 //
ansond 22:192b598ba389 156 // because our obs_number (assigned to observe_ptr) is part of this object instance, we dont
ansond 22:192b598ba389 157 // want to have the underlying free() try to free it... to just nullify here
ansond 22:192b598ba389 158 //
ansond 22:192b598ba389 159 if (coap_res_ptr->options_list_ptr) coap_res_ptr->options_list_ptr->observe_ptr = 0;
ansond 21:8487990a3baa 160 }
ansond 21:8487990a3baa 161 else {
ansond 5:a929d65eb385 162 this->logger()->log("ERROR: resource(GET) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 163 }
sam_grove 2:853f9ecc12df 164 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
sam_grove 2:853f9ecc12df 165 if(received_coap_ptr->payload_len > 0) {
ansond 0:b438482ebbfc 166 // process the PUT if we have registered a callback for it...
ansond 0:b438482ebbfc 167 if ((this->m_res_mask&SN_GRS_PUT_ALLOWED) != 0) {
ansond 0:b438482ebbfc 168 // binder interacts only with strings
ansond 0:b438482ebbfc 169 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
sam_grove 2:853f9ecc12df 170
ansond 0:b438482ebbfc 171 // call the resource put() to set the resource value
ansond 5:a929d65eb385 172 this->logger()->log("Calling resource(PUT) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 0:b438482ebbfc 173 this->put(value);
sam_grove 2:853f9ecc12df 174
ansond 0:b438482ebbfc 175 // build out the response and send...
ansond 5:a929d65eb385 176 this->logger()->log("resource(PUT) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 177 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 178 sn_nsdl_send_coap_message(address,coap_res_ptr);
sam_grove 2:853f9ecc12df 179 } else {
ansond 5:a929d65eb385 180 this->logger()->log("ERROR: resource(PUT) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 181 }
sam_grove 2:853f9ecc12df 182 } else {
ansond 5:a929d65eb385 183 this->logger()->log("ERROR: Binder(PUT) payload is NULL...");
ansond 0:b438482ebbfc 184 }
ansond 21:8487990a3baa 185 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
ansond 21:8487990a3baa 186 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 187 // process the POST if we have registered a callback for it...
ansond 21:8487990a3baa 188 if ((this->m_res_mask&SN_GRS_POST_ALLOWED) != 0) {
ansond 21:8487990a3baa 189 // binder interacts only with strings
ansond 21:8487990a3baa 190 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 191
ansond 21:8487990a3baa 192 // call the resource post() to set the resource value
ansond 21:8487990a3baa 193 this->logger()->log("Calling resource(POST) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 194 this->post(value);
ansond 21:8487990a3baa 195
ansond 21:8487990a3baa 196 // build out the response and send...
ansond 21:8487990a3baa 197 this->logger()->log("resource(POST) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 198 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 199 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 21:8487990a3baa 200 } else {
ansond 21:8487990a3baa 201 this->logger()->log("ERROR: resource(POST) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 202 }
ansond 21:8487990a3baa 203 } else {
ansond 21:8487990a3baa 204 this->logger()->log("ERROR: Binder(POST) payload is NULL...");
ansond 21:8487990a3baa 205 }
ansond 21:8487990a3baa 206 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
ansond 21:8487990a3baa 207 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 208 // process the DELETE if we have registered a callback for it...
ansond 21:8487990a3baa 209 if ((this->m_res_mask&SN_GRS_DELETE_ALLOWED) != 0) {
ansond 21:8487990a3baa 210 // binder interacts only with strings
ansond 21:8487990a3baa 211 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 212
ansond 21:8487990a3baa 213 // call the resource del() to set the resource value
ansond 21:8487990a3baa 214 this->logger()->log("Calling resource(DELETE) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 215 this->del(value);
ansond 21:8487990a3baa 216
ansond 21:8487990a3baa 217 // build out the response and send...
ansond 21:8487990a3baa 218 this->logger()->log("resource(DELETE) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 219 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 220 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 21:8487990a3baa 221 } else {
ansond 21:8487990a3baa 222 this->logger()->log("ERROR: resource(DELETE) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 223 }
ansond 21:8487990a3baa 224 } else {
ansond 21:8487990a3baa 225 this->logger()->log("ERROR: Binder(DELETE) payload is NULL...");
ansond 21:8487990a3baa 226 }
ansond 0:b438482ebbfc 227 }
ansond 0:b438482ebbfc 228
ansond 0:b438482ebbfc 229 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
ansond 22:192b598ba389 230
ansond 0:b438482ebbfc 231 return 0;
sam_grove 2:853f9ecc12df 232 }
sam_grove 2:853f9ecc12df 233
ansond 21:8487990a3baa 234 // send the notification
ansond 21:8487990a3baa 235 int DynamicResource::notify(const string data) {
ansond 21:8487990a3baa 236 return this->notify((uint8_t *)data.c_str(),(int)data.length());
ansond 21:8487990a3baa 237 }
ansond 21:8487990a3baa 238
ansond 21:8487990a3baa 239 // send the notification
ansond 21:8487990a3baa 240 int DynamicResource::notify(uint8_t *data,int data_length) {
ansond 21:8487990a3baa 241 int status = sn_nsdl_send_observation_notification(this->m_obs_token_ptr,this->m_obs_token_len,data,data_length, &this->m_obs_number, 1,COAP_MSG_TYPE_NON_CONFIRMABLE,0);
ansond 21:8487990a3baa 242 if (status == 0) {
ansond 21:8487990a3baa 243 this->logger()->log("ERROR: resource(NOTIFY) send failed...");
ansond 21:8487990a3baa 244 }
ansond 21:8487990a3baa 245 return status;
ansond 21:8487990a3baa 246 }
ansond 21:8487990a3baa 247
sam_grove 2:853f9ecc12df 248 // default PUT (does nothing)
sam_grove 2:853f9ecc12df 249 void DynamicResource::put(const string value)
sam_grove 2:853f9ecc12df 250 {
sam_grove 2:853f9ecc12df 251 // not used by default
sam_grove 2:853f9ecc12df 252 ;
sam_grove 2:853f9ecc12df 253 }
sam_grove 2:853f9ecc12df 254
ansond 21:8487990a3baa 255 // default POST (does nothing)
ansond 21:8487990a3baa 256 void DynamicResource::post(const string value)
ansond 21:8487990a3baa 257 {
ansond 21:8487990a3baa 258 // not used by default
ansond 21:8487990a3baa 259 ;
ansond 21:8487990a3baa 260 }
ansond 21:8487990a3baa 261
ansond 21:8487990a3baa 262 // default DELETE (does nothing)
ansond 21:8487990a3baa 263 void DynamicResource::del(const string value)
ansond 21:8487990a3baa 264 {
ansond 21:8487990a3baa 265 // not used by default
ansond 21:8487990a3baa 266 ;
ansond 21:8487990a3baa 267 }
ansond 21:8487990a3baa 268
sam_grove 2:853f9ecc12df 269 // convenience method to get the URI from its buffer field...
sam_grove 2:853f9ecc12df 270 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
sam_grove 2:853f9ecc12df 271 {
ansond 0:b438482ebbfc 272 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 0:b438482ebbfc 273 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 0:b438482ebbfc 274 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 0:b438482ebbfc 275 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 0:b438482ebbfc 276 return string(buf);
ansond 0:b438482ebbfc 277 }
ansond 0:b438482ebbfc 278 return string("");
sam_grove 2:853f9ecc12df 279 }