custom for >5 resources

Fork of mbedConnectorInterface by Doug Anson

Committer:
ansond
Date:
Fri Mar 13 05:46:48 2015 +0000
Revision:
21:8487990a3baa
Parent:
20:abfbaf524192
Child:
22:192b598ba389
added observability support, post() and del() support

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