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:
Thu Mar 19 04:05:08 2015 +0000
Revision:
23:caa0260acc21
Parent:
22:192b598ba389
Child:
24:a6915e19814e
updates for k64f hrm

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());
sam_grove 2:853f9ecc12df 117
sam_grove 2:853f9ecc12df 118 // fill in the CoAP response payload
ansond 23:caa0260acc21 119 coap_res_ptr->payload_len = value.size();
ansond 23:caa0260acc21 120 coap_res_ptr->payload_ptr = (uint8_t *)value.c_str();
ansond 21:8487990a3baa 121
ansond 21:8487990a3baa 122 // Observation handling...
ansond 21:8487990a3baa 123 if(received_coap_ptr->token_ptr) {
ansond 21:8487990a3baa 124 if(this->m_obs_token_ptr) {
ansond 21:8487990a3baa 125 free(this->m_obs_token_ptr);
ansond 21:8487990a3baa 126 this->m_obs_token_ptr = NULL;
ansond 23:caa0260acc21 127 this->m_obs_token_len = 0;
ansond 21:8487990a3baa 128 }
ansond 22:192b598ba389 129
ansond 21:8487990a3baa 130 this->m_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
ansond 21:8487990a3baa 131 if(this->m_obs_token_ptr) {
ansond 21:8487990a3baa 132 memcpy(this->m_obs_token_ptr, received_coap_ptr->token_ptr,received_coap_ptr->token_len);
ansond 21:8487990a3baa 133 this->m_obs_token_len = received_coap_ptr->token_len;
ansond 21:8487990a3baa 134 }
ansond 21:8487990a3baa 135 }
ansond 21:8487990a3baa 136
ansond 22:192b598ba389 137 // Observation handling...
ansond 22:192b598ba389 138 if(received_coap_ptr->options_list_ptr && received_coap_ptr->options_list_ptr->observe) {
ansond 21:8487990a3baa 139 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
ansond 21:8487990a3baa 140 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
ansond 22:192b598ba389 141 coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below...
ansond 21:8487990a3baa 142 coap_res_ptr->options_list_ptr->observe_len = 1;
ansond 21:8487990a3baa 143 this->m_obs_number++;
ansond 21:8487990a3baa 144 }
sam_grove 2:853f9ecc12df 145
sam_grove 2:853f9ecc12df 146 // build out the response and send...
ansond 21:8487990a3baa 147 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 22:192b598ba389 148
ansond 22:192b598ba389 149 //
ansond 22:192b598ba389 150 // nullify note:
ansond 22:192b598ba389 151 //
ansond 22:192b598ba389 152 // because our obs_number (assigned to observe_ptr) is part of this object instance, we dont
ansond 22:192b598ba389 153 // want to have the underlying free() try to free it... to just nullify here
ansond 22:192b598ba389 154 //
ansond 22:192b598ba389 155 if (coap_res_ptr->options_list_ptr) coap_res_ptr->options_list_ptr->observe_ptr = 0;
ansond 21:8487990a3baa 156 }
ansond 21:8487990a3baa 157 else {
ansond 5:a929d65eb385 158 this->logger()->log("ERROR: resource(GET) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 159 }
sam_grove 2:853f9ecc12df 160 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
sam_grove 2:853f9ecc12df 161 if(received_coap_ptr->payload_len > 0) {
ansond 0:b438482ebbfc 162 // process the PUT if we have registered a callback for it...
ansond 0:b438482ebbfc 163 if ((this->m_res_mask&SN_GRS_PUT_ALLOWED) != 0) {
ansond 0:b438482ebbfc 164 // binder interacts only with strings
ansond 0:b438482ebbfc 165 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
sam_grove 2:853f9ecc12df 166
ansond 0:b438482ebbfc 167 // call the resource put() to set the resource value
ansond 5:a929d65eb385 168 this->logger()->log("Calling resource(PUT) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 0:b438482ebbfc 169 this->put(value);
sam_grove 2:853f9ecc12df 170
ansond 0:b438482ebbfc 171 // build out the response and send...
ansond 5:a929d65eb385 172 this->logger()->log("resource(PUT) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 173 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 174 sn_nsdl_send_coap_message(address,coap_res_ptr);
sam_grove 2:853f9ecc12df 175 } else {
ansond 5:a929d65eb385 176 this->logger()->log("ERROR: resource(PUT) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 177 }
sam_grove 2:853f9ecc12df 178 } else {
ansond 5:a929d65eb385 179 this->logger()->log("ERROR: Binder(PUT) payload is NULL...");
ansond 0:b438482ebbfc 180 }
ansond 21:8487990a3baa 181 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
ansond 21:8487990a3baa 182 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 183 // process the POST if we have registered a callback for it...
ansond 21:8487990a3baa 184 if ((this->m_res_mask&SN_GRS_POST_ALLOWED) != 0) {
ansond 21:8487990a3baa 185 // binder interacts only with strings
ansond 21:8487990a3baa 186 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 187
ansond 21:8487990a3baa 188 // call the resource post() to set the resource value
ansond 21:8487990a3baa 189 this->logger()->log("Calling resource(POST) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 190 this->post(value);
ansond 21:8487990a3baa 191
ansond 21:8487990a3baa 192 // build out the response and send...
ansond 21:8487990a3baa 193 this->logger()->log("resource(POST) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 194 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 195 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 21:8487990a3baa 196 } else {
ansond 21:8487990a3baa 197 this->logger()->log("ERROR: resource(POST) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 198 }
ansond 21:8487990a3baa 199 } else {
ansond 21:8487990a3baa 200 this->logger()->log("ERROR: Binder(POST) payload is NULL...");
ansond 21:8487990a3baa 201 }
ansond 21:8487990a3baa 202 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
ansond 21:8487990a3baa 203 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 204 // process the DELETE if we have registered a callback for it...
ansond 21:8487990a3baa 205 if ((this->m_res_mask&SN_GRS_DELETE_ALLOWED) != 0) {
ansond 21:8487990a3baa 206 // binder interacts only with strings
ansond 21:8487990a3baa 207 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 208
ansond 21:8487990a3baa 209 // call the resource del() to set the resource value
ansond 21:8487990a3baa 210 this->logger()->log("Calling resource(DELETE) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 211 this->del(value);
ansond 21:8487990a3baa 212
ansond 21:8487990a3baa 213 // build out the response and send...
ansond 21:8487990a3baa 214 this->logger()->log("resource(DELETE) 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);
ansond 21:8487990a3baa 217 } else {
ansond 21:8487990a3baa 218 this->logger()->log("ERROR: resource(DELETE) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 219 }
ansond 21:8487990a3baa 220 } else {
ansond 21:8487990a3baa 221 this->logger()->log("ERROR: Binder(DELETE) payload is NULL...");
ansond 21:8487990a3baa 222 }
ansond 0:b438482ebbfc 223 }
ansond 0:b438482ebbfc 224
ansond 0:b438482ebbfc 225 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
ansond 22:192b598ba389 226
ansond 0:b438482ebbfc 227 return 0;
sam_grove 2:853f9ecc12df 228 }
sam_grove 2:853f9ecc12df 229
ansond 21:8487990a3baa 230 // send the notification
ansond 21:8487990a3baa 231 int DynamicResource::notify(const string data) {
ansond 21:8487990a3baa 232 return this->notify((uint8_t *)data.c_str(),(int)data.length());
ansond 21:8487990a3baa 233 }
ansond 21:8487990a3baa 234
ansond 21:8487990a3baa 235 // send the notification
ansond 21:8487990a3baa 236 int DynamicResource::notify(uint8_t *data,int data_length) {
ansond 23:caa0260acc21 237 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 238 if (status == 0) {
ansond 21:8487990a3baa 239 this->logger()->log("ERROR: resource(NOTIFY) send failed...");
ansond 21:8487990a3baa 240 }
ansond 21:8487990a3baa 241 return status;
ansond 21:8487990a3baa 242 }
ansond 21:8487990a3baa 243
sam_grove 2:853f9ecc12df 244 // default PUT (does nothing)
sam_grove 2:853f9ecc12df 245 void DynamicResource::put(const string value)
sam_grove 2:853f9ecc12df 246 {
sam_grove 2:853f9ecc12df 247 // not used by default
sam_grove 2:853f9ecc12df 248 ;
sam_grove 2:853f9ecc12df 249 }
sam_grove 2:853f9ecc12df 250
ansond 21:8487990a3baa 251 // default POST (does nothing)
ansond 21:8487990a3baa 252 void DynamicResource::post(const string value)
ansond 21:8487990a3baa 253 {
ansond 21:8487990a3baa 254 // not used by default
ansond 21:8487990a3baa 255 ;
ansond 21:8487990a3baa 256 }
ansond 21:8487990a3baa 257
ansond 21:8487990a3baa 258 // default DELETE (does nothing)
ansond 21:8487990a3baa 259 void DynamicResource::del(const string value)
ansond 21:8487990a3baa 260 {
ansond 21:8487990a3baa 261 // not used by default
ansond 21:8487990a3baa 262 ;
ansond 21:8487990a3baa 263 }
ansond 21:8487990a3baa 264
sam_grove 2:853f9ecc12df 265 // convenience method to get the URI from its buffer field...
sam_grove 2:853f9ecc12df 266 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
sam_grove 2:853f9ecc12df 267 {
ansond 0:b438482ebbfc 268 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 0:b438482ebbfc 269 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 0:b438482ebbfc 270 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 0:b438482ebbfc 271 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 0:b438482ebbfc 272 return string(buf);
ansond 0:b438482ebbfc 273 }
ansond 0:b438482ebbfc 274 return string("");
sam_grove 2:853f9ecc12df 275 }