observe updates

Fork of mbedConnectorInterface by Doug Anson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DynamicResource.h Source File

DynamicResource.h

Go to the documentation of this file.
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 "Resource.h"
00028 
00029 // NSDL Support
00030 #include "nsdl_support.h"
00031 
00032 // DataWrapper support
00033 #include "DataWrapper.h"
00034 
00035 /** DynamicResource class
00036  */
00037 class DynamicResource : public Resource<string>
00038 {
00039 public:
00040     /**
00041     Default constructor (char strings)
00042     @param logger input logger instance for this resource
00043     @param name input the Resource URI/Name
00044     @param res_type input type for the Resource
00045     @param res_mask input the resource enablement mask (GET, PUT, etc...)
00046     @param observable input the resource is Observable (default: FALSE)
00047     */
00048     DynamicResource(const Logger *logger,const char *name,const char *res_type,uint8_t res_mask,const bool observable = false);
00049 
00050     /**
00051     Default constructor (char strings)
00052     @param logger input logger instance for this resource
00053     @param name input the Resource URI/Name
00054     @param res_type input type for the Resource
00055     @param value input initial value for the Resource
00056     @param res_mask input the resource enablement mask (GET, PUT, etc...)
00057     @param observable input the resource is Observable (default: FALSE)
00058     */
00059     DynamicResource(const Logger *logger,const char *name,const char *res_type,const string value,uint8_t res_mask,const bool observable = false);
00060 
00061     /**
00062     constructor with string buffer for name
00063     @param logger input logger instance for this resource
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 name,const string res_type,const string value,uint8_t res_mask,const bool observable = false);
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 p input pointer to the endpoint resources necessary for binding
00086     */
00087     virtual void bind(void *p);
00088 
00089     /**
00090     Process the CoAP message
00091     @param received_coap_ptr input the received coap pointer
00092     @param address input the NSDL address pointer
00093     @param proto input the NSDL protocol pointer
00094     @return 0 - success, 1 - failure
00095     */
00096     uint8_t process(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto);
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() = 0;
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 string value of the resource
00113     */
00114     virtual void post(const string value);
00115     
00116     /**
00117     Resource value deleter (OPTIONAL: defaulted noop if not derived. Binders MAY implement DELETE if needed)
00118     @param string value of the resource
00119     */
00120     virtual void del(const string value);
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 data wrapper
00143     @param data_wrapper input the data wrapper instance
00144     */
00145     void setDataWrapper(DataWrapper *data_wrapper) { this->m_data_wrapper = data_wrapper; }
00146     
00147     /**
00148     observe the resource
00149     */
00150     virtual void observe();
00151     
00152 protected:
00153     int notify(uint8_t *data,int data_length);
00154     DataWrapper *getDataWrapper() { return this->m_data_wrapper; }
00155     bool              m_observable;
00156      
00157 private:
00158 
00159     string            m_res_type;
00160     uint8_t           m_res_mask;
00161     uint8_t           m_obs_number;
00162     uint8_t          *m_obs_token_ptr;
00163     uint8_t           m_obs_token_len;
00164     DataWrapper      *m_data_wrapper;
00165     void             *m_observer;
00166 
00167     // convenience method to create a string from the NSDL CoAP data buffers...
00168     string coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length);
00169 };
00170 
00171 #endif // __DYNAMIC_RESOURCE_H__