custom for >5 resources
Fork of mbedConnectorInterface by
api/DynamicResource.cpp@20:abfbaf524192, 2015-03-05 (annotated)
- Committer:
- ansond
- Date:
- Thu Mar 05 18:23:28 2015 +0000
- Revision:
- 20:abfbaf524192
- Parent:
- 5:a929d65eb385
- Child:
- 21:8487990a3baa
updates
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 | |
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 | const uint8_t *name = (const uint8_t *)(this->getName().c_str()); |
ansond | 0:b438482ebbfc | 71 | const uint8_t *res_type = (const uint8_t *)this->m_res_type.c_str(); |
ansond | 0:b438482ebbfc | 72 | int name_length = this->getName().size(); |
ansond | 0:b438482ebbfc | 73 | int res_type_length = this->m_res_type.size(); |
sam_grove | 2:853f9ecc12df | 74 | int is_observable = 0; |
sam_grove | 2:853f9ecc12df | 75 | if (this->m_observable == true) is_observable = 1; |
ansond | 0:b438482ebbfc | 76 | const string *key = new string(this->getName()); |
ansond | 0:b438482ebbfc | 77 | ipt_helper_add_instance_pointer(key,this); |
ansond | 0:b438482ebbfc | 78 | 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 | 79 | this->logger()->log("DynamicResource: [%s] type: [%s] bound (observable: %d)",name,res_type,is_observable); |
sam_grove | 2:853f9ecc12df | 80 | } else { |
ansond | 5:a929d65eb385 | 81 | this->logger()->log("DynamicResource: NULL parameter in bind()"); |
ansond | 0:b438482ebbfc | 82 | } |
sam_grove | 2:853f9ecc12df | 83 | } |
ansond | 0:b438482ebbfc | 84 | |
sam_grove | 2:853f9ecc12df | 85 | // process NSDL message |
sam_grove | 2:853f9ecc12df | 86 | 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 | 87 | { |
ansond | 0:b438482ebbfc | 88 | sn_coap_hdr_s *coap_res_ptr = 0; |
sam_grove | 2:853f9ecc12df | 89 | |
ansond | 0:b438482ebbfc | 90 | // create our key for debugging output... |
ansond | 0:b438482ebbfc | 91 | string key = this->coapDataToString(received_coap_ptr->uri_path_ptr,received_coap_ptr->uri_path_len); |
sam_grove | 2:853f9ecc12df | 92 | |
sam_grove | 2:853f9ecc12df | 93 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) { |
ansond | 0:b438482ebbfc | 94 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
sam_grove | 2:853f9ecc12df | 95 | |
sam_grove | 2:853f9ecc12df | 96 | // process the GET if we have registered a callback for it... |
ansond | 0:b438482ebbfc | 97 | if ((this->m_res_mask&SN_GRS_GET_ALLOWED) != 0) { |
ansond | 0:b438482ebbfc | 98 | // call the resource get() to get the resource value |
ansond | 5:a929d65eb385 | 99 | this->logger()->log("Calling resource(GET) for [%s]...",key.c_str()); |
ansond | 0:b438482ebbfc | 100 | string value = this->get(); |
sam_grove | 2:853f9ecc12df | 101 | |
sam_grove | 2:853f9ecc12df | 102 | // convert the string from the GET to something suitable for CoAP payloads |
ansond | 5:a929d65eb385 | 103 | this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),value.c_str()); |
ansond | 0:b438482ebbfc | 104 | int length = value.size(); |
ansond | 0:b438482ebbfc | 105 | char value_buffer[MAX_VALUE_BUFFER_LENGTH+1]; |
ansond | 0:b438482ebbfc | 106 | if (length > MAX_VALUE_BUFFER_LENGTH) length = MAX_VALUE_BUFFER_LENGTH; |
ansond | 0:b438482ebbfc | 107 | memset(value_buffer,0,MAX_VALUE_BUFFER_LENGTH+1); |
sam_grove | 2:853f9ecc12df | 108 | memcpy(value_buffer,value.c_str(),length); |
sam_grove | 2:853f9ecc12df | 109 | |
sam_grove | 2:853f9ecc12df | 110 | // fill in the CoAP response payload |
ansond | 0:b438482ebbfc | 111 | coap_res_ptr->payload_len = length; |
ansond | 0:b438482ebbfc | 112 | coap_res_ptr->payload_ptr = (uint8_t *)value_buffer; |
sam_grove | 2:853f9ecc12df | 113 | |
sam_grove | 2:853f9ecc12df | 114 | // build out the response and send... |
ansond | 0:b438482ebbfc | 115 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
sam_grove | 2:853f9ecc12df | 116 | } else { |
ansond | 5:a929d65eb385 | 117 | this->logger()->log("ERROR: resource(GET) mask is munged (mask: 0x%x)",this->m_res_mask); |
ansond | 0:b438482ebbfc | 118 | } |
sam_grove | 2:853f9ecc12df | 119 | } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) { |
sam_grove | 2:853f9ecc12df | 120 | if(received_coap_ptr->payload_len > 0) { |
ansond | 0:b438482ebbfc | 121 | // process the PUT if we have registered a callback for it... |
ansond | 0:b438482ebbfc | 122 | if ((this->m_res_mask&SN_GRS_PUT_ALLOWED) != 0) { |
ansond | 0:b438482ebbfc | 123 | // binder interacts only with strings |
ansond | 0:b438482ebbfc | 124 | string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len); |
sam_grove | 2:853f9ecc12df | 125 | |
ansond | 0:b438482ebbfc | 126 | // call the resource put() to set the resource value |
ansond | 5:a929d65eb385 | 127 | this->logger()->log("Calling resource(PUT) with [%s]=[%s]...",key.c_str(),value.c_str()); |
ansond | 0:b438482ebbfc | 128 | this->put(value); |
sam_grove | 2:853f9ecc12df | 129 | |
ansond | 0:b438482ebbfc | 130 | // build out the response and send... |
ansond | 5:a929d65eb385 | 131 | this->logger()->log("resource(PUT) completed for [%s]...",key.c_str()); |
ansond | 0:b438482ebbfc | 132 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); |
ansond | 0:b438482ebbfc | 133 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
sam_grove | 2:853f9ecc12df | 134 | } else { |
ansond | 5:a929d65eb385 | 135 | this->logger()->log("ERROR: resource(PUT) mask is munged (mask: 0x%x)",this->m_res_mask); |
ansond | 0:b438482ebbfc | 136 | } |
sam_grove | 2:853f9ecc12df | 137 | } else { |
ansond | 5:a929d65eb385 | 138 | this->logger()->log("ERROR: Binder(PUT) payload is NULL..."); |
ansond | 0:b438482ebbfc | 139 | } |
ansond | 0:b438482ebbfc | 140 | } |
ansond | 0:b438482ebbfc | 141 | |
ansond | 0:b438482ebbfc | 142 | sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
ansond | 0:b438482ebbfc | 143 | |
ansond | 0:b438482ebbfc | 144 | return 0; |
sam_grove | 2:853f9ecc12df | 145 | } |
sam_grove | 2:853f9ecc12df | 146 | |
sam_grove | 2:853f9ecc12df | 147 | // default PUT (does nothing) |
sam_grove | 2:853f9ecc12df | 148 | void DynamicResource::put(const string value) |
sam_grove | 2:853f9ecc12df | 149 | { |
sam_grove | 2:853f9ecc12df | 150 | // not used by default |
sam_grove | 2:853f9ecc12df | 151 | ; |
sam_grove | 2:853f9ecc12df | 152 | } |
sam_grove | 2:853f9ecc12df | 153 | |
sam_grove | 2:853f9ecc12df | 154 | // convenience method to get the URI from its buffer field... |
sam_grove | 2:853f9ecc12df | 155 | string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length) |
sam_grove | 2:853f9ecc12df | 156 | { |
ansond | 0:b438482ebbfc | 157 | if (coap_data_ptr != NULL && coap_data_ptr_length > 0) { |
ansond | 0:b438482ebbfc | 158 | char buf[MAX_VALUE_BUFFER_LENGTH+1]; |
ansond | 0:b438482ebbfc | 159 | memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1); |
ansond | 0:b438482ebbfc | 160 | memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length); |
ansond | 0:b438482ebbfc | 161 | return string(buf); |
ansond | 0:b438482ebbfc | 162 | } |
ansond | 0:b438482ebbfc | 163 | return string(""); |
sam_grove | 2:853f9ecc12df | 164 | } |