added max-age and content-format
Fork of mbedConnectorInterface by
api/InstancePointerTableHelper.h@0:b438482ebbfc, 2015-01-27 (annotated)
- Committer:
- ansond
- Date:
- Tue Jan 27 22:23:51 2015 +0000
- Revision:
- 0:b438482ebbfc
- Child:
- 2:853f9ecc12df
initial check in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:b438482ebbfc | 1 | /** |
ansond | 0:b438482ebbfc | 2 | * @file InstancePointerTableHelper.h |
ansond | 0:b438482ebbfc | 3 | * @brief instance pointer table helper functions for DynamicResource |
ansond | 0:b438482ebbfc | 4 | * @author Doug Anson |
ansond | 0:b438482ebbfc | 5 | * @version 1.0 |
ansond | 0:b438482ebbfc | 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 | */ |
ansond | 0:b438482ebbfc | 22 | |
ansond | 0:b438482ebbfc | 23 | #ifndef __INSTANCE_POINTER_TABLE_HELPER_H__ |
ansond | 0:b438482ebbfc | 24 | #define __INSTANCE_POINTER_TABLE_HELPER_H__ |
ansond | 0:b438482ebbfc | 25 | |
ansond | 0:b438482ebbfc | 26 | // InstancePointerTable |
ansond | 0:b438482ebbfc | 27 | #include "InstancePointerTable.h" |
ansond | 0:b438482ebbfc | 28 | extern Logger logger; |
ansond | 0:b438482ebbfc | 29 | InstancePointerTable __nsdl_lookup_table(&logger); // Instance Pointer Table |
ansond | 0:b438482ebbfc | 30 | |
ansond | 0:b438482ebbfc | 31 | |
ansond | 0:b438482ebbfc | 32 | // lookup a DynamicResource instance indexed by our resource URI as the key |
ansond | 0:b438482ebbfc | 33 | extern "C" DynamicResource *__lookup_instance_pointer(const char *uri,const int uri_length) { |
ansond | 0:b438482ebbfc | 34 | if (uri != NULL && uri_length > 0) { |
ansond | 0:b438482ebbfc | 35 | string key(uri,uri_length); |
ansond | 0:b438482ebbfc | 36 | return (DynamicResource *)__nsdl_lookup_table.get(key); |
ansond | 0:b438482ebbfc | 37 | } |
ansond | 0:b438482ebbfc | 38 | return NULL; |
ansond | 0:b438482ebbfc | 39 | } |
ansond | 0:b438482ebbfc | 40 | |
ansond | 0:b438482ebbfc | 41 | // callback function for NSDL library to call into... ASSUMPTION: 1 and only 1 DynamicResource<> instance per Resource URI... |
ansond | 0:b438482ebbfc | 42 | extern "C" uint8_t ipt_helper_nsdl_callback_stub(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) { |
ansond | 0:b438482ebbfc | 43 | uint8_t status = 1; |
ansond | 0:b438482ebbfc | 44 | if (received_coap_ptr != NULL) { |
ansond | 0:b438482ebbfc | 45 | // retrieve our resource instance via lookup/index by our resource URI |
ansond | 0:b438482ebbfc | 46 | DynamicResource *instance = __lookup_instance_pointer((const char *)received_coap_ptr->uri_path_ptr,received_coap_ptr->uri_path_len); |
ansond | 0:b438482ebbfc | 47 | if (instance != NULL) { |
ansond | 0:b438482ebbfc | 48 | std::printf("Instance retrieved.. processing callback...\r\n"); |
ansond | 0:b438482ebbfc | 49 | status = instance->process(received_coap_ptr,address,proto); |
ansond | 0:b438482ebbfc | 50 | } |
ansond | 0:b438482ebbfc | 51 | else { |
ansond | 0:b438482ebbfc | 52 | std::printf("Unable to process callback: unable to lookup DynamicResource<> instance pointer... ignorning...\r\n"); |
ansond | 0:b438482ebbfc | 53 | } |
ansond | 0:b438482ebbfc | 54 | } |
ansond | 0:b438482ebbfc | 55 | else { |
ansond | 0:b438482ebbfc | 56 | std::printf("Unable to process callback: received coap pointer is NULL... ignoring...\r\n"); |
ansond | 0:b438482ebbfc | 57 | } |
ansond | 0:b438482ebbfc | 58 | return status; |
ansond | 0:b438482ebbfc | 59 | } |
ansond | 0:b438482ebbfc | 60 | |
ansond | 0:b438482ebbfc | 61 | // add a instance pointer to our lookup table keyed by the key |
ansond | 0:b438482ebbfc | 62 | extern "C" void ipt_helper_add_instance_pointer(const string *key,DynamicResource *instance) { |
ansond | 0:b438482ebbfc | 63 | __nsdl_lookup_table.add(*key,instance); |
ansond | 0:b438482ebbfc | 64 | if (key != NULL) delete key; |
ansond | 0:b438482ebbfc | 65 | } |
ansond | 0:b438482ebbfc | 66 | |
ansond | 0:b438482ebbfc | 67 | #endif // __INSTANCE_POINTER_TABLE_HELPER_H__ |