use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Fri Feb 19 17:32:14 2016 +0000
Revision:
0:1f1f55e73248
Child:
13:9edad7677211
initial checkin

Who changed what in which revision?

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