use TCP to connect to mbed connector
Fork of mbedConnectorInterfaceWithDM by
mbed-connector-interface/DynamicResource.h@28:cf32aa9842ce, 2016-06-14 (annotated)
- Committer:
- ansond
- Date:
- Tue Jun 14 04:22:23 2016 +0000
- Revision:
- 28:cf32aa9842ce
- Parent:
- 27:b8aaf7dc7023
- Child:
- 54:dfee8691c83a
updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 13:9edad7677211 | 1 | /** |
ansond | 13:9edad7677211 | 2 | * @file DynamicResource.h |
ansond | 13:9edad7677211 | 3 | * @brief mbed CoAP Endpoint Dynamic Resource class |
ansond | 13:9edad7677211 | 4 | * @author Doug Anson/Chris Paola |
ansond | 13:9edad7677211 | 5 | * @version 1.0 |
ansond | 13:9edad7677211 | 6 | * @see |
ansond | 13:9edad7677211 | 7 | * |
ansond | 13:9edad7677211 | 8 | * Copyright (c) 2014 |
ansond | 13:9edad7677211 | 9 | * |
ansond | 13:9edad7677211 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 13:9edad7677211 | 11 | * you may not use this file except in compliance with the License. |
ansond | 13:9edad7677211 | 12 | * You may obtain a copy of the License at |
ansond | 13:9edad7677211 | 13 | * |
ansond | 13:9edad7677211 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 13:9edad7677211 | 15 | * |
ansond | 13:9edad7677211 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 13:9edad7677211 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 13:9edad7677211 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 13:9edad7677211 | 19 | * See the License for the specific language governing permissions and |
ansond | 13:9edad7677211 | 20 | * limitations under the License. |
ansond | 13:9edad7677211 | 21 | */ |
ansond | 13:9edad7677211 | 22 | |
ansond | 13:9edad7677211 | 23 | #ifndef __DYNAMIC_RESOURCE_H__ |
ansond | 13:9edad7677211 | 24 | #define __DYNAMIC_RESOURCE_H__ |
ansond | 13:9edad7677211 | 25 | |
ansond | 13:9edad7677211 | 26 | // Base Class |
ansond | 13:9edad7677211 | 27 | #include "mbed-connector-interface/Resource.h" |
ansond | 13:9edad7677211 | 28 | |
ansond | 13:9edad7677211 | 29 | // DataWrapper support |
ansond | 13:9edad7677211 | 30 | #include "mbed-connector-interface/DataWrapper.h" |
ansond | 13:9edad7677211 | 31 | |
ansond | 13:9edad7677211 | 32 | /** DynamicResource class |
ansond | 13:9edad7677211 | 33 | */ |
ansond | 13:9edad7677211 | 34 | class DynamicResource : public Resource<string> |
ansond | 13:9edad7677211 | 35 | { |
ansond | 13:9edad7677211 | 36 | public: |
ansond | 13:9edad7677211 | 37 | /** |
ansond | 13:9edad7677211 | 38 | Default constructor (char strings) |
ansond | 13:9edad7677211 | 39 | @param logger input logger instance for this resource |
ansond | 13:9edad7677211 | 40 | @param obj_name input the Object |
ansond | 13:9edad7677211 | 41 | @param name input the Resource URI/Name |
ansond | 13:9edad7677211 | 42 | @param res_type input type for the Resource |
ansond | 13:9edad7677211 | 43 | @param res_mask input the resource enablement mask (GET, PUT, etc...) |
ansond | 13:9edad7677211 | 44 | @param observable input the resource is Observable (default: FALSE) |
ansond | 13:9edad7677211 | 45 | */ |
ansond | 27:b8aaf7dc7023 | 46 | DynamicResource(const Logger *logger,const char *obj_name,const char *res_name,const char *res_type,uint8_t res_mask,const bool observable = false,const ResourceType type = STRING); |
ansond | 13:9edad7677211 | 47 | |
ansond | 13:9edad7677211 | 48 | /** |
ansond | 13:9edad7677211 | 49 | Default constructor (char strings) |
ansond | 13:9edad7677211 | 50 | @param logger input logger instance for this resource |
ansond | 13:9edad7677211 | 51 | @param obj_name input the Object |
ansond | 13:9edad7677211 | 52 | @param name input the Resource URI/Name |
ansond | 13:9edad7677211 | 53 | @param res_type input type for the Resource |
ansond | 13:9edad7677211 | 54 | @param value input initial value for the Resource |
ansond | 13:9edad7677211 | 55 | @param res_mask input the resource enablement mask (GET, PUT, etc...) |
ansond | 13:9edad7677211 | 56 | @param observable input the resource is Observable (default: FALSE) |
ansond | 13:9edad7677211 | 57 | */ |
ansond | 27:b8aaf7dc7023 | 58 | DynamicResource(const Logger *logger,const char *obj_name,const char *res_name,const char *res_type,const string value,uint8_t res_mask,const bool observable = false,const ResourceType type = STRING); |
ansond | 13:9edad7677211 | 59 | |
ansond | 13:9edad7677211 | 60 | /** |
ansond | 13:9edad7677211 | 61 | constructor with string buffer for name |
ansond | 13:9edad7677211 | 62 | @param logger input logger instance for this resource |
ansond | 13:9edad7677211 | 63 | @param obj_name input the Object |
ansond | 13:9edad7677211 | 64 | @param name input the Resource URI/Name |
ansond | 13:9edad7677211 | 65 | @param res_type input type for the Resource |
ansond | 13:9edad7677211 | 66 | @param value input initial value for the Resource |
ansond | 13:9edad7677211 | 67 | @param res_mask input the resource enablement mask (GET, PUT, etc...) |
ansond | 13:9edad7677211 | 68 | @param observable input the resource is Observable (default: FALSE) |
ansond | 13:9edad7677211 | 69 | */ |
ansond | 27:b8aaf7dc7023 | 70 | DynamicResource(const Logger *logger,const string obj_name,const string res_name,const string res_type,const string value,uint8_t res_mask,const bool observable = false,const ResourceType type = STRING); |
ansond | 13:9edad7677211 | 71 | |
ansond | 13:9edad7677211 | 72 | /** |
ansond | 13:9edad7677211 | 73 | Copy constructor |
ansond | 13:9edad7677211 | 74 | @param resource input the DynamicResource that is to be deep copied |
ansond | 13:9edad7677211 | 75 | */ |
ansond | 13:9edad7677211 | 76 | DynamicResource(const DynamicResource &resource); |
ansond | 13:9edad7677211 | 77 | |
ansond | 13:9edad7677211 | 78 | /** |
ansond | 13:9edad7677211 | 79 | Destructor |
ansond | 13:9edad7677211 | 80 | */ |
ansond | 13:9edad7677211 | 81 | virtual ~DynamicResource(); |
ansond | 13:9edad7677211 | 82 | |
ansond | 13:9edad7677211 | 83 | /** |
ansond | 13:9edad7677211 | 84 | Bind resource to endpoint |
ansond | 27:b8aaf7dc7023 | 85 | @param ep input endpoint instance pointer |
ansond | 13:9edad7677211 | 86 | */ |
ansond | 27:b8aaf7dc7023 | 87 | virtual void bind(void *ep); |
ansond | 13:9edad7677211 | 88 | |
ansond | 13:9edad7677211 | 89 | /** |
ansond | 13:9edad7677211 | 90 | Process the CoAP message |
ansond | 13:9edad7677211 | 91 | @param op input the CoAP Verb (operation) |
ansond | 13:9edad7677211 | 92 | @param type input clarification of the M2MBase instance being passed in (Object vs. ObjectInstance vs. Resource vs. ResourceInstance) |
ansond | 13:9edad7677211 | 93 | @param args input arguments (unused) |
ansond | 13:9edad7677211 | 94 | @return 0 - success, 1 - failure |
ansond | 13:9edad7677211 | 95 | */ |
ansond | 13:9edad7677211 | 96 | uint8_t process(M2MBase::Operation op,M2MBase::BaseType type,void *args = NULL); |
ansond | 13:9edad7677211 | 97 | |
ansond | 13:9edad7677211 | 98 | /** |
ansond | 13:9edad7677211 | 99 | Resource value getter (REQUIRED: must be implemented in derived class as all Binders MUST support and implement GET) |
ansond | 13:9edad7677211 | 100 | @returns string value of the resource |
ansond | 13:9edad7677211 | 101 | */ |
ansond | 27:b8aaf7dc7023 | 102 | virtual string get(); |
ansond | 0:1f1f55e73248 | 103 | |
ansond | 13:9edad7677211 | 104 | /** |
ansond | 13:9edad7677211 | 105 | Resource value setter (PUT) (OPTIONAL: defaulted noop if not derived. Binders MAY implement PUT if needed) |
ansond | 13:9edad7677211 | 106 | @param string value of the resource |
ansond | 13:9edad7677211 | 107 | */ |
ansond | 13:9edad7677211 | 108 | virtual void put(const string value); |
ansond | 13:9edad7677211 | 109 | |
ansond | 13:9edad7677211 | 110 | /** |
ansond | 13:9edad7677211 | 111 | Resource value setter (POST) (OPTIONAL: defaulted noop if not derived. Binders MAY implement POST if needed) |
ansond | 13:9edad7677211 | 112 | @param void * args for the post() operation |
ansond | 13:9edad7677211 | 113 | */ |
ansond | 13:9edad7677211 | 114 | virtual void post(void *args); |
ansond | 13:9edad7677211 | 115 | |
ansond | 13:9edad7677211 | 116 | /** |
ansond | 13:9edad7677211 | 117 | Resource value deleter (OPTIONAL: defaulted noop if not derived. Binders MAY implement DELETE if needed) |
ansond | 13:9edad7677211 | 118 | @param void * args for the del() operation |
ansond | 13:9edad7677211 | 119 | */ |
ansond | 13:9edad7677211 | 120 | virtual void del(void *args); |
ansond | 13:9edad7677211 | 121 | |
ansond | 13:9edad7677211 | 122 | /** |
ansond | 13:9edad7677211 | 123 | Send notification of new data |
ansond | 13:9edad7677211 | 124 | @param data input the new data to update |
ansond | 13:9edad7677211 | 125 | @returns 1 - success, 0 - failure |
ansond | 13:9edad7677211 | 126 | */ |
ansond | 13:9edad7677211 | 127 | int notify(const string data); |
ansond | 13:9edad7677211 | 128 | |
ansond | 13:9edad7677211 | 129 | /** |
ansond | 13:9edad7677211 | 130 | Determine whether this dynamic resource is observable or not |
ansond | 13:9edad7677211 | 131 | @returns true - is observable, false - otherwise |
ansond | 13:9edad7677211 | 132 | */ |
ansond | 13:9edad7677211 | 133 | bool isObservable() { return this->m_observable; } |
ansond | 13:9edad7677211 | 134 | |
ansond | 13:9edad7677211 | 135 | /** |
ansond | 13:9edad7677211 | 136 | Set the observer pointer |
ansond | 13:9edad7677211 | 137 | @param observer input the pointer to the ResourceObserver observing this resource |
ansond | 13:9edad7677211 | 138 | */ |
ansond | 13:9edad7677211 | 139 | void setObserver(void *observer); |
ansond | 13:9edad7677211 | 140 | |
ansond | 13:9edad7677211 | 141 | /** |
ansond | 13:9edad7677211 | 142 | Set the content format for responses |
ansond | 13:9edad7677211 | 143 | @param content_format short integer CoAP content-format ID |
ansond | 13:9edad7677211 | 144 | */ |
ansond | 13:9edad7677211 | 145 | void setContentFormat(uint8_t content_format); |
ansond | 13:9edad7677211 | 146 | |
ansond | 13:9edad7677211 | 147 | /** |
ansond | 13:9edad7677211 | 148 | Set the max-age for cache control of responses in a proxy cache |
ansond | 13:9edad7677211 | 149 | @param maxage short integer CoAP max-age in seconds |
ansond | 13:9edad7677211 | 150 | */ |
ansond | 13:9edad7677211 | 151 | void setMaxAge(uint8_t maxage); |
ansond | 13:9edad7677211 | 152 | |
ansond | 13:9edad7677211 | 153 | /** |
ansond | 13:9edad7677211 | 154 | Set the data wrapper |
ansond | 13:9edad7677211 | 155 | @param data_wrapper input the data wrapper instance |
ansond | 13:9edad7677211 | 156 | */ |
ansond | 13:9edad7677211 | 157 | void setDataWrapper(DataWrapper *data_wrapper) { this->m_data_wrapper = data_wrapper; } |
ansond | 13:9edad7677211 | 158 | |
ansond | 13:9edad7677211 | 159 | /** |
ansond | 13:9edad7677211 | 160 | observe the resource |
ansond | 13:9edad7677211 | 161 | */ |
ansond | 13:9edad7677211 | 162 | virtual void observe(); |
ansond | 13:9edad7677211 | 163 | |
ansond | 13:9edad7677211 | 164 | /** |
ansond | 13:9edad7677211 | 165 | get the base resource representation |
ansond | 13:9edad7677211 | 166 | */ |
ansond | 27:b8aaf7dc7023 | 167 | M2MResource *getResource(); |
ansond | 13:9edad7677211 | 168 | |
ansond | 13:9edad7677211 | 169 | /** |
ansond | 13:9edad7677211 | 170 | Process a POST message for Resources |
ansond | 13:9edad7677211 | 171 | */ |
ansond | 13:9edad7677211 | 172 | void process_resource_post(void *args); |
ansond | 13:9edad7677211 | 173 | |
ansond | 13:9edad7677211 | 174 | /** |
ansond | 13:9edad7677211 | 175 | Determine if we are connected or not |
ansond | 13:9edad7677211 | 176 | */ |
ansond | 13:9edad7677211 | 177 | bool isConnected(); |
ansond | 15:c11dbe4d354c | 178 | |
ansond | 15:c11dbe4d354c | 179 | /** |
ansond | 15:c11dbe4d354c | 180 | Determine if we are registered or not |
ansond | 15:c11dbe4d354c | 181 | */ |
ansond | 15:c11dbe4d354c | 182 | bool isRegistered(); |
ansond | 13:9edad7677211 | 183 | |
ansond | 13:9edad7677211 | 184 | protected: |
ansond | 27:b8aaf7dc7023 | 185 | int notify(uint8_t *data,int data_length); |
ansond | 27:b8aaf7dc7023 | 186 | DataWrapper *getDataWrapper() { return this->m_data_wrapper; } |
ansond | 13:9edad7677211 | 187 | bool m_observable; |
ansond | 13:9edad7677211 | 188 | |
ansond | 13:9edad7677211 | 189 | private: |
ansond | 13:9edad7677211 | 190 | |
ansond | 27:b8aaf7dc7023 | 191 | string m_res_type; // a string description of our resource type (i.e. "Counter", etc...) |
ansond | 27:b8aaf7dc7023 | 192 | ResourceType m_type; // the core type of our resource (i.e. String, Integer, etc...) |
ansond | 13:9edad7677211 | 193 | uint8_t m_res_mask; |
ansond | 13:9edad7677211 | 194 | uint8_t m_obs_number; |
ansond | 15:c11dbe4d354c | 195 | DataWrapper *m_data_wrapper; |
ansond | 13:9edad7677211 | 196 | void *m_observer; |
ansond | 15:c11dbe4d354c | 197 | uint8_t m_maxage; |
ansond | 15:c11dbe4d354c | 198 | uint8_t m_content_format; |
ansond | 15:c11dbe4d354c | 199 | M2MResource *m_res; |
ansond | 27:b8aaf7dc7023 | 200 | void *m_ep; |
ansond | 13:9edad7677211 | 201 | |
ansond | 28:cf32aa9842ce | 202 | public: |
ansond | 13:9edad7677211 | 203 | // convenience method to create a string from the NSDL CoAP data buffers... |
ansond | 13:9edad7677211 | 204 | string coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length); |
ansond | 27:b8aaf7dc7023 | 205 | int coapDataToInteger(uint8_t *coap_data_ptr,int coap_data_ptr_length); |
ansond | 27:b8aaf7dc7023 | 206 | float coapDataToFloat(uint8_t *coap_data_ptr,int coap_data_ptr_length); |
ansond | 27:b8aaf7dc7023 | 207 | void *coapDataToOpaque(uint8_t *coap_data_ptr,int coap_data_ptr_length); |
ansond | 13:9edad7677211 | 208 | }; |
ansond | 13:9edad7677211 | 209 | |
ansond | 13:9edad7677211 | 210 | #endif // __DYNAMIC_RESOURCE_H__ |
ansond | 13:9edad7677211 | 211 |