custom for >5 resources

Fork of mbedConnectorInterface by Doug Anson

Committer:
sam_grove
Date:
Tue Jan 27 23:41:34 2015 +0000
Revision:
2:853f9ecc12df
Parent:
0:b438482ebbfc
Child:
5:a929d65eb385
Use auto-format on code and add markup to render class documentation

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;
sam_grove 2:853f9ecc12df 34 }
sam_grove 2:853f9ecc12df 35
sam_grove 2:853f9ecc12df 36 // constructor (input initial value)
sam_grove 2:853f9ecc12df 37 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 38 {
ansond 0:b438482ebbfc 39 this->m_res_type = string(res_type);
ansond 0:b438482ebbfc 40 this->m_observable = observable;
sam_grove 2:853f9ecc12df 41 this->m_res_mask = res_mask;
sam_grove 2:853f9ecc12df 42 }
sam_grove 2:853f9ecc12df 43
sam_grove 2:853f9ecc12df 44 // constructor (strings)
sam_grove 2:853f9ecc12df 45 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 46 {
ansond 0:b438482ebbfc 47 this->m_res_type = res_type;
ansond 0:b438482ebbfc 48 this->m_observable = observable;
sam_grove 2:853f9ecc12df 49 this->m_res_mask = res_mask;
sam_grove 2:853f9ecc12df 50 }
sam_grove 2:853f9ecc12df 51
sam_grove 2:853f9ecc12df 52 // copy constructor
sam_grove 2:853f9ecc12df 53 DynamicResource::DynamicResource(const DynamicResource &resource) : Resource<string>((const Resource<string> &)resource)
sam_grove 2:853f9ecc12df 54 {
ansond 0:b438482ebbfc 55 this->m_res_type = resource.m_res_type;
ansond 0:b438482ebbfc 56 this->m_observable = resource.m_observable;
ansond 0:b438482ebbfc 57 this->m_res_mask = resource.m_res_mask;
sam_grove 2:853f9ecc12df 58 }
sam_grove 2:853f9ecc12df 59
sam_grove 2:853f9ecc12df 60 // destructor
sam_grove 2:853f9ecc12df 61 DynamicResource::~DynamicResource()
sam_grove 2:853f9ecc12df 62 {
sam_grove 2:853f9ecc12df 63 }
sam_grove 2:853f9ecc12df 64
sam_grove 2:853f9ecc12df 65 // bind resource to NSDL
sam_grove 2:853f9ecc12df 66 void DynamicResource::bind(void *p)
sam_grove 2:853f9ecc12df 67 {
ansond 0:b438482ebbfc 68 if (p != NULL) {
ansond 0:b438482ebbfc 69 sn_nsdl_resource_info_s *resource_ptr = (sn_nsdl_resource_info_s *)p;
ansond 0:b438482ebbfc 70 std::printf("DynamicResource: name[%s] type:[%s] mask: 0x%.2x\r\n",this->getName().c_str(),this->m_res_type.c_str(),this->m_res_mask);
ansond 0:b438482ebbfc 71 const uint8_t *name = (const uint8_t *)(this->getName().c_str());
ansond 0:b438482ebbfc 72 const uint8_t *res_type = (const uint8_t *)this->m_res_type.c_str();
ansond 0:b438482ebbfc 73 int name_length = this->getName().size();
ansond 0:b438482ebbfc 74 int res_type_length = this->m_res_type.size();
sam_grove 2:853f9ecc12df 75 int is_observable = 0;
sam_grove 2:853f9ecc12df 76 if (this->m_observable == true) is_observable = 1;
ansond 0:b438482ebbfc 77 const string *key = new string(this->getName());
ansond 0:b438482ebbfc 78 ipt_helper_add_instance_pointer(key,this);
ansond 0:b438482ebbfc 79 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 0:b438482ebbfc 80 std::printf("DynamicResource[%s(%d)] type: %s(%d) bound mask: 0x%.2x\r\n",name,name_length,res_type,res_type_length,this->m_res_mask);
sam_grove 2:853f9ecc12df 81 } else {
ansond 0:b438482ebbfc 82 std::printf("DynamicResource: NULL parameter in bind()\r\n");
ansond 0:b438482ebbfc 83 }
sam_grove 2:853f9ecc12df 84 }
ansond 0:b438482ebbfc 85
sam_grove 2:853f9ecc12df 86 // process NSDL message
sam_grove 2:853f9ecc12df 87 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 88 {
ansond 0:b438482ebbfc 89 sn_coap_hdr_s *coap_res_ptr = 0;
sam_grove 2:853f9ecc12df 90
ansond 0:b438482ebbfc 91 // create our key for debugging output...
ansond 0:b438482ebbfc 92 string key = this->coapDataToString(received_coap_ptr->uri_path_ptr,received_coap_ptr->uri_path_len);
sam_grove 2:853f9ecc12df 93
sam_grove 2:853f9ecc12df 94 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
ansond 0:b438482ebbfc 95 std::printf("GET requested for [%s]...\r\n",key.c_str());
ansond 0:b438482ebbfc 96 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
sam_grove 2:853f9ecc12df 97
sam_grove 2:853f9ecc12df 98 // process the GET if we have registered a callback for it...
ansond 0:b438482ebbfc 99 if ((this->m_res_mask&SN_GRS_GET_ALLOWED) != 0) {
ansond 0:b438482ebbfc 100 // call the resource get() to get the resource value
ansond 0:b438482ebbfc 101 std::printf("Calling resource(GET) for [%s]...\r\n",key.c_str());
ansond 0:b438482ebbfc 102 string value = this->get();
sam_grove 2:853f9ecc12df 103
sam_grove 2:853f9ecc12df 104 // convert the string from the GET to something suitable for CoAP payloads
sam_grove 2:853f9ecc12df 105 std::printf("Building payload for [%s]=[%s]...\r\n",key.c_str(),value.c_str());
ansond 0:b438482ebbfc 106 int length = value.size();
ansond 0:b438482ebbfc 107 char value_buffer[MAX_VALUE_BUFFER_LENGTH+1];
ansond 0:b438482ebbfc 108 if (length > MAX_VALUE_BUFFER_LENGTH) length = MAX_VALUE_BUFFER_LENGTH;
ansond 0:b438482ebbfc 109 memset(value_buffer,0,MAX_VALUE_BUFFER_LENGTH+1);
sam_grove 2:853f9ecc12df 110 memcpy(value_buffer,value.c_str(),length);
sam_grove 2:853f9ecc12df 111
sam_grove 2:853f9ecc12df 112 // fill in the CoAP response payload
ansond 0:b438482ebbfc 113 coap_res_ptr->payload_len = length;
ansond 0:b438482ebbfc 114 coap_res_ptr->payload_ptr = (uint8_t *)value_buffer;
sam_grove 2:853f9ecc12df 115
sam_grove 2:853f9ecc12df 116 // build out the response and send...
ansond 0:b438482ebbfc 117 sn_nsdl_send_coap_message(address, coap_res_ptr);
sam_grove 2:853f9ecc12df 118 } else {
ansond 0:b438482ebbfc 119 std::printf("ERROR: resource(GET) mask is munged (mask: 0x%x)\r\n",this->m_res_mask);
ansond 0:b438482ebbfc 120 }
sam_grove 2:853f9ecc12df 121 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
ansond 0:b438482ebbfc 122 std::printf("PUT requested for [%s]...\r\n",key.c_str());
sam_grove 2:853f9ecc12df 123 if(received_coap_ptr->payload_len > 0) {
ansond 0:b438482ebbfc 124 // process the PUT if we have registered a callback for it...
ansond 0:b438482ebbfc 125 if ((this->m_res_mask&SN_GRS_PUT_ALLOWED) != 0) {
ansond 0:b438482ebbfc 126 // binder interacts only with strings
ansond 0:b438482ebbfc 127 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
sam_grove 2:853f9ecc12df 128
ansond 0:b438482ebbfc 129 // call the resource put() to set the resource value
ansond 0:b438482ebbfc 130 std::printf("Calling resource(PUT) with [%s]=[%s]...\r\n",key.c_str(),value.c_str());
ansond 0:b438482ebbfc 131 this->put(value);
sam_grove 2:853f9ecc12df 132
ansond 0:b438482ebbfc 133 // build out the response and send...
ansond 0:b438482ebbfc 134 std::printf("resource(PUT) completed for [%s]...\r\n",key.c_str());
ansond 0:b438482ebbfc 135 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 0:b438482ebbfc 136 sn_nsdl_send_coap_message(address, coap_res_ptr);
sam_grove 2:853f9ecc12df 137 } else {
ansond 0:b438482ebbfc 138 std::printf("ERROR: resource(PUT) mask is munged (mask: 0x%x)\r\n",this->m_res_mask);
ansond 0:b438482ebbfc 139 }
sam_grove 2:853f9ecc12df 140 } else {
ansond 0:b438482ebbfc 141 std::printf("ERROR: Binder(PUT) payload is NULL...\r\n");
ansond 0:b438482ebbfc 142 }
ansond 0:b438482ebbfc 143 }
ansond 0:b438482ebbfc 144
ansond 0:b438482ebbfc 145 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
ansond 0:b438482ebbfc 146
ansond 0:b438482ebbfc 147 return 0;
sam_grove 2:853f9ecc12df 148 }
sam_grove 2:853f9ecc12df 149
sam_grove 2:853f9ecc12df 150 // default PUT (does nothing)
sam_grove 2:853f9ecc12df 151 void DynamicResource::put(const string value)
sam_grove 2:853f9ecc12df 152 {
sam_grove 2:853f9ecc12df 153 // not used by default
sam_grove 2:853f9ecc12df 154 ;
sam_grove 2:853f9ecc12df 155 }
sam_grove 2:853f9ecc12df 156
sam_grove 2:853f9ecc12df 157 // convenience method to get the URI from its buffer field...
sam_grove 2:853f9ecc12df 158 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
sam_grove 2:853f9ecc12df 159 {
ansond 0:b438482ebbfc 160 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 0:b438482ebbfc 161 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 0:b438482ebbfc 162 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 0:b438482ebbfc 163 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 0:b438482ebbfc 164 return string(buf);
ansond 0:b438482ebbfc 165 }
ansond 0:b438482ebbfc 166 return string("");
sam_grove 2:853f9ecc12df 167 }