Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbedConnectorInterfaceWithDM by
DynamicResource.h
00001 /** 00002 * @file DynamicResource.h 00003 * @brief mbed CoAP Endpoint Dynamic Resource class 00004 * @author Doug Anson/Chris Paola 00005 * @version 1.0 00006 * @see 00007 * 00008 * Copyright (c) 2014 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); 00011 * you may not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, 00018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 */ 00022 00023 #ifndef __DYNAMIC_RESOURCE_H__ 00024 #define __DYNAMIC_RESOURCE_H__ 00025 00026 // Base Class 00027 #include "mbed-connector-interface/Resource.h" 00028 00029 // DataWrapper support 00030 #include "mbed-connector-interface/DataWrapper.h" 00031 00032 /** DynamicResource class 00033 */ 00034 class DynamicResource : public Resource<string> 00035 { 00036 public: 00037 /** 00038 Default constructor (char strings) 00039 @param logger input logger instance for this resource 00040 @param obj_name input the Object 00041 @param name input the Resource URI/Name 00042 @param res_type input type for the Resource 00043 @param res_mask input the resource enablement mask (GET, PUT, etc...) 00044 @param observable input the resource is Observable (default: FALSE) 00045 */ 00046 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); 00047 00048 /** 00049 Default constructor (char strings) 00050 @param logger input logger instance for this resource 00051 @param obj_name input the Object 00052 @param name input the Resource URI/Name 00053 @param res_type input type for the Resource 00054 @param value input initial value for the Resource 00055 @param res_mask input the resource enablement mask (GET, PUT, etc...) 00056 @param observable input the resource is Observable (default: FALSE) 00057 */ 00058 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); 00059 00060 /** 00061 constructor with string buffer for name 00062 @param logger input logger instance for this resource 00063 @param obj_name input the Object 00064 @param name input the Resource URI/Name 00065 @param res_type input type for the Resource 00066 @param value input initial value for the Resource 00067 @param res_mask input the resource enablement mask (GET, PUT, etc...) 00068 @param observable input the resource is Observable (default: FALSE) 00069 */ 00070 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); 00071 00072 /** 00073 Copy constructor 00074 @param resource input the DynamicResource that is to be deep copied 00075 */ 00076 DynamicResource(const DynamicResource &resource); 00077 00078 /** 00079 Destructor 00080 */ 00081 virtual ~DynamicResource(); 00082 00083 /** 00084 Bind resource to endpoint 00085 @param ep input endpoint instance pointer 00086 */ 00087 virtual void bind(void *ep); 00088 00089 /** 00090 Process the CoAP message 00091 @param op input the CoAP Verb (operation) 00092 @param type input clarification of the M2MBase instance being passed in (Object vs. ObjectInstance vs. Resource vs. ResourceInstance) 00093 @param args input arguments (unused) 00094 @return 0 - success, 1 - failure 00095 */ 00096 uint8_t process(M2MBase::Operation op,M2MBase::BaseType type,void *args = NULL); 00097 00098 /** 00099 Resource value getter (REQUIRED: must be implemented in derived class as all Binders MUST support and implement GET) 00100 @returns string value of the resource 00101 */ 00102 virtual string get(); 00103 00104 /** 00105 Resource value setter (PUT) (OPTIONAL: defaulted noop if not derived. Binders MAY implement PUT if needed) 00106 @param string value of the resource 00107 */ 00108 virtual void put(const string value); 00109 00110 /** 00111 Resource value setter (POST) (OPTIONAL: defaulted noop if not derived. Binders MAY implement POST if needed) 00112 @param void * args for the post() operation 00113 */ 00114 virtual void post(void *args); 00115 00116 /** 00117 Resource value deleter (OPTIONAL: defaulted noop if not derived. Binders MAY implement DELETE if needed) 00118 @param void * args for the del() operation 00119 */ 00120 virtual void del(void *args); 00121 00122 /** 00123 Send notification of new data 00124 @param data input the new data to update 00125 @returns 1 - success, 0 - failure 00126 */ 00127 int notify(const string data); 00128 00129 /** 00130 Determine whether this dynamic resource is observable or not 00131 @returns true - is observable, false - otherwise 00132 */ 00133 bool isObservable() { return this->m_observable; } 00134 00135 /** 00136 Set the observer pointer 00137 @param observer input the pointer to the ResourceObserver observing this resource 00138 */ 00139 void setObserver(void *observer); 00140 00141 /** 00142 Set the content format for responses 00143 @param content_format short integer CoAP content-format ID 00144 */ 00145 void setContentFormat(uint8_t content_format); 00146 00147 /** 00148 Set the max-age for cache control of responses in a proxy cache 00149 @param maxage short integer CoAP max-age in seconds 00150 */ 00151 void setMaxAge(uint8_t maxage); 00152 00153 /** 00154 Set the data wrapper 00155 @param data_wrapper input the data wrapper instance 00156 */ 00157 void setDataWrapper(DataWrapper *data_wrapper) { this->m_data_wrapper = data_wrapper; } 00158 00159 /** 00160 observe the resource 00161 */ 00162 virtual void observe(); 00163 00164 /** 00165 get the base resource representation 00166 */ 00167 M2MResource *getResource(); 00168 00169 /** 00170 Process a POST message for Resources 00171 */ 00172 void process_resource_post(void *args); 00173 00174 /** 00175 Determine if we are connected or not 00176 */ 00177 bool isConnected(); 00178 00179 /** 00180 Determine if we are registered or not 00181 */ 00182 bool isRegistered(); 00183 00184 /** 00185 Get our Observer 00186 */ 00187 void *getObserver(); 00188 00189 protected: 00190 int notify(uint8_t *data,int data_length); 00191 DataWrapper *getDataWrapper() { return this->m_data_wrapper; } 00192 bool m_observable; 00193 00194 private: 00195 00196 string m_res_type; // a string description of our resource type (i.e. "Counter", etc...) 00197 ResourceType m_type; // the core type of our resource (i.e. String, Integer, etc...) 00198 uint8_t m_res_mask; 00199 uint8_t m_obs_number; 00200 DataWrapper *m_data_wrapper; 00201 void *m_observer; 00202 uint8_t m_maxage; 00203 uint8_t m_content_format; 00204 M2MResource *m_res; 00205 void *m_ep; 00206 00207 public: 00208 // convenience method to create a string from the NSDL CoAP data buffers... 00209 string coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length); 00210 int coapDataToInteger(uint8_t *coap_data_ptr,int coap_data_ptr_length); 00211 float coapDataToFloat(uint8_t *coap_data_ptr,int coap_data_ptr_length); 00212 void *coapDataToOpaque(uint8_t *coap_data_ptr,int coap_data_ptr_length); 00213 }; 00214 00215 #endif // __DYNAMIC_RESOURCE_H__ 00216
Generated on Wed Jul 13 2022 01:24:02 by
