Jim Carver
/
mbed-cloud-workshop-connect
Fork for workshops
simple-mbed-cloud-client/mbed-cloud-client/mbed-cloud-client/SimpleM2MResource.h@0:6b753f761943, 2018-10-12 (annotated)
- Committer:
- JimCarver
- Date:
- Fri Oct 12 21:22:49 2018 +0000
- Revision:
- 0:6b753f761943
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCarver | 0:6b753f761943 | 1 | // ---------------------------------------------------------------------------- |
JimCarver | 0:6b753f761943 | 2 | // Copyright 2016-2017 ARM Ltd. |
JimCarver | 0:6b753f761943 | 3 | // |
JimCarver | 0:6b753f761943 | 4 | // SPDX-License-Identifier: Apache-2.0 |
JimCarver | 0:6b753f761943 | 5 | // |
JimCarver | 0:6b753f761943 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
JimCarver | 0:6b753f761943 | 7 | // you may not use this file except in compliance with the License. |
JimCarver | 0:6b753f761943 | 8 | // You may obtain a copy of the License at |
JimCarver | 0:6b753f761943 | 9 | // |
JimCarver | 0:6b753f761943 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
JimCarver | 0:6b753f761943 | 11 | // |
JimCarver | 0:6b753f761943 | 12 | // Unless required by applicable law or agreed to in writing, software |
JimCarver | 0:6b753f761943 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
JimCarver | 0:6b753f761943 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JimCarver | 0:6b753f761943 | 15 | // See the License for the specific language governing permissions and |
JimCarver | 0:6b753f761943 | 16 | // limitations under the License. |
JimCarver | 0:6b753f761943 | 17 | // ---------------------------------------------------------------------------- |
JimCarver | 0:6b753f761943 | 18 | |
JimCarver | 0:6b753f761943 | 19 | #ifndef SIMPLE_M2M_RESOURCE_H |
JimCarver | 0:6b753f761943 | 20 | #define SIMPLE_M2M_RESOURCE_H |
JimCarver | 0:6b753f761943 | 21 | |
JimCarver | 0:6b753f761943 | 22 | |
JimCarver | 0:6b753f761943 | 23 | #include "mbed-cloud-client/MbedCloudClient.h" |
JimCarver | 0:6b753f761943 | 24 | |
JimCarver | 0:6b753f761943 | 25 | /*! \file SimpleM2MResource.h |
JimCarver | 0:6b753f761943 | 26 | * \brief SimpleM2MResourceBase. |
JimCarver | 0:6b753f761943 | 27 | * This class provides an easy wrapper base class for creating a simple M2MResource based on |
JimCarver | 0:6b753f761943 | 28 | * integer and string values. This class is NOT meant to be directed instantiated but is used |
JimCarver | 0:6b753f761943 | 29 | * by the SimpleM2MResourceInt and SimpleM2MResourceString classes to create resources. |
JimCarver | 0:6b753f761943 | 30 | */ |
JimCarver | 0:6b753f761943 | 31 | |
JimCarver | 0:6b753f761943 | 32 | class SimpleM2MResourceBase { |
JimCarver | 0:6b753f761943 | 33 | |
JimCarver | 0:6b753f761943 | 34 | protected: |
JimCarver | 0:6b753f761943 | 35 | |
JimCarver | 0:6b753f761943 | 36 | // Prevents the use of default constructor. |
JimCarver | 0:6b753f761943 | 37 | SimpleM2MResourceBase(); |
JimCarver | 0:6b753f761943 | 38 | |
JimCarver | 0:6b753f761943 | 39 | // Prevents the use of assignment operator. |
JimCarver | 0:6b753f761943 | 40 | SimpleM2MResourceBase& operator=( const SimpleM2MResourceBase& /*other*/ ); |
JimCarver | 0:6b753f761943 | 41 | |
JimCarver | 0:6b753f761943 | 42 | // Prevents the use of copy constructor |
JimCarver | 0:6b753f761943 | 43 | SimpleM2MResourceBase( const M2MBase& /*other*/ ); |
JimCarver | 0:6b753f761943 | 44 | |
JimCarver | 0:6b753f761943 | 45 | SimpleM2MResourceBase(MbedCloudClient* client, string route); |
JimCarver | 0:6b753f761943 | 46 | |
JimCarver | 0:6b753f761943 | 47 | /** |
JimCarver | 0:6b753f761943 | 48 | * \brief Destructor |
JimCarver | 0:6b753f761943 | 49 | */ |
JimCarver | 0:6b753f761943 | 50 | virtual ~SimpleM2MResourceBase(); |
JimCarver | 0:6b753f761943 | 51 | |
JimCarver | 0:6b753f761943 | 52 | |
JimCarver | 0:6b753f761943 | 53 | public: |
JimCarver | 0:6b753f761943 | 54 | |
JimCarver | 0:6b753f761943 | 55 | /** |
JimCarver | 0:6b753f761943 | 56 | * \brief Defines M2MResource internally and creates a necessary LWM2M |
JimCarver | 0:6b753f761943 | 57 | * structure like object and object instance based on the given string |
JimCarver | 0:6b753f761943 | 58 | * URI path and sets the right M2M operation to the resource. |
JimCarver | 0:6b753f761943 | 59 | * \param v The URI path for the resource "Test/0/res" in this format. |
JimCarver | 0:6b753f761943 | 60 | * \param opr An operation to be set for the resource. |
JimCarver | 0:6b753f761943 | 61 | * \param observable True if the resource is observable, else false. |
JimCarver | 0:6b753f761943 | 62 | * \return True if resource is created, else false. |
JimCarver | 0:6b753f761943 | 63 | */ |
JimCarver | 0:6b753f761943 | 64 | bool define_resource_internal(string v, |
JimCarver | 0:6b753f761943 | 65 | M2MBase::Operation opr, |
JimCarver | 0:6b753f761943 | 66 | bool observable); |
JimCarver | 0:6b753f761943 | 67 | |
JimCarver | 0:6b753f761943 | 68 | /** |
JimCarver | 0:6b753f761943 | 69 | * \brief Gets the value set in a resource in text format. |
JimCarver | 0:6b753f761943 | 70 | * \return The value set in the resource. |
JimCarver | 0:6b753f761943 | 71 | */ |
JimCarver | 0:6b753f761943 | 72 | string get() const; |
JimCarver | 0:6b753f761943 | 73 | |
JimCarver | 0:6b753f761943 | 74 | /** |
JimCarver | 0:6b753f761943 | 75 | * \brief Sets the value in a resource in text format. |
JimCarver | 0:6b753f761943 | 76 | * \param v The value to be set. |
JimCarver | 0:6b753f761943 | 77 | * \return True if set successfully, else false. |
JimCarver | 0:6b753f761943 | 78 | */ |
JimCarver | 0:6b753f761943 | 79 | bool set(string v); |
JimCarver | 0:6b753f761943 | 80 | |
JimCarver | 0:6b753f761943 | 81 | /** |
JimCarver | 0:6b753f761943 | 82 | * \brief Sets the value in a resource in integer format. |
JimCarver | 0:6b753f761943 | 83 | * \param v The value to be set. |
JimCarver | 0:6b753f761943 | 84 | * \return True if set successfully, else false. |
JimCarver | 0:6b753f761943 | 85 | */ |
JimCarver | 0:6b753f761943 | 86 | bool set(const int& v); |
JimCarver | 0:6b753f761943 | 87 | |
JimCarver | 0:6b753f761943 | 88 | /** |
JimCarver | 0:6b753f761943 | 89 | * \brief Sets the callback function to be called |
JimCarver | 0:6b753f761943 | 90 | * when the resource received a POST command. |
JimCarver | 0:6b753f761943 | 91 | * \param fn A function to be called. |
JimCarver | 0:6b753f761943 | 92 | * This is used for a statically defined function. |
JimCarver | 0:6b753f761943 | 93 | * \return True if set successfully, else false. |
JimCarver | 0:6b753f761943 | 94 | */ |
JimCarver | 0:6b753f761943 | 95 | bool set_post_function(void(*fn)(void*)); |
JimCarver | 0:6b753f761943 | 96 | |
JimCarver | 0:6b753f761943 | 97 | /** |
JimCarver | 0:6b753f761943 | 98 | * \brief Sets the callback function to be called |
JimCarver | 0:6b753f761943 | 99 | * when a resource received the POST command. |
JimCarver | 0:6b753f761943 | 100 | * \param fn A function to be called. |
JimCarver | 0:6b753f761943 | 101 | * This is an overloaded function for a class function. |
JimCarver | 0:6b753f761943 | 102 | * \return True if set successfully, else false. |
JimCarver | 0:6b753f761943 | 103 | */ |
JimCarver | 0:6b753f761943 | 104 | bool set_post_function(execute_callback fn); |
JimCarver | 0:6b753f761943 | 105 | |
JimCarver | 0:6b753f761943 | 106 | /** |
JimCarver | 0:6b753f761943 | 107 | * \brief Returns the M2MResource object of the registered object through |
JimCarver | 0:6b753f761943 | 108 | * the SimpleM2MResourceBase objects. |
JimCarver | 0:6b753f761943 | 109 | * \return The object of the M2MResource. |
JimCarver | 0:6b753f761943 | 110 | */ |
JimCarver | 0:6b753f761943 | 111 | M2MResource* get_resource(); |
JimCarver | 0:6b753f761943 | 112 | |
JimCarver | 0:6b753f761943 | 113 | /** |
JimCarver | 0:6b753f761943 | 114 | * \brief Calls when there is an indication that the value of the resource |
JimCarver | 0:6b753f761943 | 115 | * object is updated by the LWM2M Cloud server. |
JimCarver | 0:6b753f761943 | 116 | */ |
JimCarver | 0:6b753f761943 | 117 | virtual void update(){} |
JimCarver | 0:6b753f761943 | 118 | |
JimCarver | 0:6b753f761943 | 119 | private: |
JimCarver | 0:6b753f761943 | 120 | |
JimCarver | 0:6b753f761943 | 121 | /** |
JimCarver | 0:6b753f761943 | 122 | * \brief An internal helper function to break the URI path into a list of |
JimCarver | 0:6b753f761943 | 123 | * strings such as "Test/0/res" into a list of three strings. |
JimCarver | 0:6b753f761943 | 124 | * \param route The URI path in the format "Test/0/res". |
JimCarver | 0:6b753f761943 | 125 | * \return A list of strings parsed from the URI path. |
JimCarver | 0:6b753f761943 | 126 | */ |
JimCarver | 0:6b753f761943 | 127 | vector<string> parse_route(const char* route); |
JimCarver | 0:6b753f761943 | 128 | |
JimCarver | 0:6b753f761943 | 129 | private: |
JimCarver | 0:6b753f761943 | 130 | MbedCloudClient* _client; // Not owned |
JimCarver | 0:6b753f761943 | 131 | string _route; |
JimCarver | 0:6b753f761943 | 132 | }; |
JimCarver | 0:6b753f761943 | 133 | |
JimCarver | 0:6b753f761943 | 134 | /** |
JimCarver | 0:6b753f761943 | 135 | * \brief SimpleM2MResourceString. |
JimCarver | 0:6b753f761943 | 136 | * This class provides an easy wrapper base class for creating a simple M2MResource based on |
JimCarver | 0:6b753f761943 | 137 | * string values. This class provides an easy access to the M2MResource objects without the application |
JimCarver | 0:6b753f761943 | 138 | * requiring to create Objects and Object Instances. |
JimCarver | 0:6b753f761943 | 139 | */ |
JimCarver | 0:6b753f761943 | 140 | |
JimCarver | 0:6b753f761943 | 141 | class SimpleM2MResourceString : public SimpleM2MResourceBase |
JimCarver | 0:6b753f761943 | 142 | { |
JimCarver | 0:6b753f761943 | 143 | public: |
JimCarver | 0:6b753f761943 | 144 | |
JimCarver | 0:6b753f761943 | 145 | /** |
JimCarver | 0:6b753f761943 | 146 | * \brief Constructor. |
JimCarver | 0:6b753f761943 | 147 | * \param client A handler for MbedCloudClient. |
JimCarver | 0:6b753f761943 | 148 | * \param route The route for the resource such as "Test/0/res". |
JimCarver | 0:6b753f761943 | 149 | * \param v The value of the resource. |
JimCarver | 0:6b753f761943 | 150 | * \param opr An operation that can be supported by the resource. |
JimCarver | 0:6b753f761943 | 151 | * \param observable True if the resource is observable. |
JimCarver | 0:6b753f761943 | 152 | * \param on_update If the resource supports the PUT operation, a function pointer |
JimCarver | 0:6b753f761943 | 153 | * for the callback function that is called when the client receives an |
JimCarver | 0:6b753f761943 | 154 | * updated value for this resource. |
JimCarver | 0:6b753f761943 | 155 | */ |
JimCarver | 0:6b753f761943 | 156 | SimpleM2MResourceString(MbedCloudClient* client, |
JimCarver | 0:6b753f761943 | 157 | const char* route, |
JimCarver | 0:6b753f761943 | 158 | string v, |
JimCarver | 0:6b753f761943 | 159 | M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED, |
JimCarver | 0:6b753f761943 | 160 | bool observable = true, |
JimCarver | 0:6b753f761943 | 161 | FP1<void, string> on_update = NULL); |
JimCarver | 0:6b753f761943 | 162 | |
JimCarver | 0:6b753f761943 | 163 | /** |
JimCarver | 0:6b753f761943 | 164 | * \brief Constructor. This is overloaded function. |
JimCarver | 0:6b753f761943 | 165 | * \param client A handler for MbedCloudClient. |
JimCarver | 0:6b753f761943 | 166 | * \param route The route for the resource such as "Test/0/res". |
JimCarver | 0:6b753f761943 | 167 | * \param v The value of the resource. |
JimCarver | 0:6b753f761943 | 168 | * \param opr An operation that can be supported by the resource. |
JimCarver | 0:6b753f761943 | 169 | * \param observable True if resource is observable. |
JimCarver | 0:6b753f761943 | 170 | * \param on_update If the resource supports the PUT operation, a function pointer |
JimCarver | 0:6b753f761943 | 171 | * for the callback function that is called when the client receives an |
JimCarver | 0:6b753f761943 | 172 | * updated value for this resource. |
JimCarver | 0:6b753f761943 | 173 | */ |
JimCarver | 0:6b753f761943 | 174 | SimpleM2MResourceString(MbedCloudClient* client, |
JimCarver | 0:6b753f761943 | 175 | const char* route, |
JimCarver | 0:6b753f761943 | 176 | string v, |
JimCarver | 0:6b753f761943 | 177 | M2MBase::Operation opr, |
JimCarver | 0:6b753f761943 | 178 | bool observable, |
JimCarver | 0:6b753f761943 | 179 | void(*on_update)(string)); |
JimCarver | 0:6b753f761943 | 180 | |
JimCarver | 0:6b753f761943 | 181 | |
JimCarver | 0:6b753f761943 | 182 | /** |
JimCarver | 0:6b753f761943 | 183 | * \brief Destructor |
JimCarver | 0:6b753f761943 | 184 | */ |
JimCarver | 0:6b753f761943 | 185 | virtual ~SimpleM2MResourceString(); |
JimCarver | 0:6b753f761943 | 186 | |
JimCarver | 0:6b753f761943 | 187 | /** |
JimCarver | 0:6b753f761943 | 188 | * \brief Overloaded operator for = operation. |
JimCarver | 0:6b753f761943 | 189 | */ |
JimCarver | 0:6b753f761943 | 190 | string operator=(const string& new_value); |
JimCarver | 0:6b753f761943 | 191 | |
JimCarver | 0:6b753f761943 | 192 | /** |
JimCarver | 0:6b753f761943 | 193 | * \brief Overloaded operator for string() operation. |
JimCarver | 0:6b753f761943 | 194 | */ |
JimCarver | 0:6b753f761943 | 195 | operator string() const; |
JimCarver | 0:6b753f761943 | 196 | |
JimCarver | 0:6b753f761943 | 197 | /** |
JimCarver | 0:6b753f761943 | 198 | * \brief Calls when there is an indication that the value of the resource |
JimCarver | 0:6b753f761943 | 199 | * object is updated by the LWM2M Cloud server. |
JimCarver | 0:6b753f761943 | 200 | */ |
JimCarver | 0:6b753f761943 | 201 | virtual void update(); |
JimCarver | 0:6b753f761943 | 202 | |
JimCarver | 0:6b753f761943 | 203 | private: |
JimCarver | 0:6b753f761943 | 204 | FP1<void, string> _on_update; |
JimCarver | 0:6b753f761943 | 205 | }; |
JimCarver | 0:6b753f761943 | 206 | |
JimCarver | 0:6b753f761943 | 207 | /** |
JimCarver | 0:6b753f761943 | 208 | * \brief SimpleM2MResourceInt. |
JimCarver | 0:6b753f761943 | 209 | * This class provides an easy wrapper base class for creating a simple M2MResource based on |
JimCarver | 0:6b753f761943 | 210 | * integer values. This class provides easy access to M2MResource objects without the application |
JimCarver | 0:6b753f761943 | 211 | * requiring to create Objects and Object Instances. |
JimCarver | 0:6b753f761943 | 212 | */ |
JimCarver | 0:6b753f761943 | 213 | |
JimCarver | 0:6b753f761943 | 214 | class SimpleM2MResourceInt : public SimpleM2MResourceBase |
JimCarver | 0:6b753f761943 | 215 | { |
JimCarver | 0:6b753f761943 | 216 | public: |
JimCarver | 0:6b753f761943 | 217 | |
JimCarver | 0:6b753f761943 | 218 | /** |
JimCarver | 0:6b753f761943 | 219 | * \brief Constructor. |
JimCarver | 0:6b753f761943 | 220 | * \param client A handler for MbedCloudClient. |
JimCarver | 0:6b753f761943 | 221 | * \param route The route for the resource such as "Test/0/res". |
JimCarver | 0:6b753f761943 | 222 | * \param v The value of the resource. |
JimCarver | 0:6b753f761943 | 223 | * \param opr An operation that can be supported by the resource. |
JimCarver | 0:6b753f761943 | 224 | * \param observable True if the resource is observable, else false. |
JimCarver | 0:6b753f761943 | 225 | * \param on_update If the resource supports the PUT operation, a function pointer |
JimCarver | 0:6b753f761943 | 226 | * for the callback function that is called when the client receives an |
JimCarver | 0:6b753f761943 | 227 | * updated value for this resource. |
JimCarver | 0:6b753f761943 | 228 | */ |
JimCarver | 0:6b753f761943 | 229 | SimpleM2MResourceInt(MbedCloudClient* client, |
JimCarver | 0:6b753f761943 | 230 | const char* route, |
JimCarver | 0:6b753f761943 | 231 | int v, |
JimCarver | 0:6b753f761943 | 232 | M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED, |
JimCarver | 0:6b753f761943 | 233 | bool observable = true, |
JimCarver | 0:6b753f761943 | 234 | FP1<void, int> on_update = NULL); |
JimCarver | 0:6b753f761943 | 235 | |
JimCarver | 0:6b753f761943 | 236 | /** |
JimCarver | 0:6b753f761943 | 237 | * \brief Constructor. This is an overloaded function |
JimCarver | 0:6b753f761943 | 238 | * \param client A handler for MbedCloudClient. |
JimCarver | 0:6b753f761943 | 239 | * \param route The route for the resource such as "Test/0/res" |
JimCarver | 0:6b753f761943 | 240 | * \param v The value of the resource. |
JimCarver | 0:6b753f761943 | 241 | * \param opr An operation that can be supported by the resource. |
JimCarver | 0:6b753f761943 | 242 | * \param observable True if the resource is observable, else false. |
JimCarver | 0:6b753f761943 | 243 | * \param on_update If the resource supports the PUT operation, a function pointer |
JimCarver | 0:6b753f761943 | 244 | * for the callback function that is called when the client receives an |
JimCarver | 0:6b753f761943 | 245 | * updated value for this resource. |
JimCarver | 0:6b753f761943 | 246 | */ |
JimCarver | 0:6b753f761943 | 247 | SimpleM2MResourceInt(MbedCloudClient* client, |
JimCarver | 0:6b753f761943 | 248 | const char* route, |
JimCarver | 0:6b753f761943 | 249 | int v, |
JimCarver | 0:6b753f761943 | 250 | M2MBase::Operation opr, |
JimCarver | 0:6b753f761943 | 251 | bool observable, |
JimCarver | 0:6b753f761943 | 252 | void(*on_update)(int)); |
JimCarver | 0:6b753f761943 | 253 | |
JimCarver | 0:6b753f761943 | 254 | /** |
JimCarver | 0:6b753f761943 | 255 | * \brief Destructor |
JimCarver | 0:6b753f761943 | 256 | */ |
JimCarver | 0:6b753f761943 | 257 | virtual ~SimpleM2MResourceInt(); |
JimCarver | 0:6b753f761943 | 258 | |
JimCarver | 0:6b753f761943 | 259 | /** |
JimCarver | 0:6b753f761943 | 260 | * \brief Overloaded operator for = operation. |
JimCarver | 0:6b753f761943 | 261 | */ |
JimCarver | 0:6b753f761943 | 262 | int operator=(int new_value); |
JimCarver | 0:6b753f761943 | 263 | |
JimCarver | 0:6b753f761943 | 264 | /** |
JimCarver | 0:6b753f761943 | 265 | * \brief Overloaded operator for int() operation. |
JimCarver | 0:6b753f761943 | 266 | */ |
JimCarver | 0:6b753f761943 | 267 | operator int() const; |
JimCarver | 0:6b753f761943 | 268 | |
JimCarver | 0:6b753f761943 | 269 | /** |
JimCarver | 0:6b753f761943 | 270 | * \brief Calls when there is an indication that the value of the resource |
JimCarver | 0:6b753f761943 | 271 | * object is updated by the LWM2M Cloud server. |
JimCarver | 0:6b753f761943 | 272 | */ |
JimCarver | 0:6b753f761943 | 273 | virtual void update(); |
JimCarver | 0:6b753f761943 | 274 | |
JimCarver | 0:6b753f761943 | 275 | private: |
JimCarver | 0:6b753f761943 | 276 | FP1<void, int> _on_update; |
JimCarver | 0:6b753f761943 | 277 | }; |
JimCarver | 0:6b753f761943 | 278 | |
JimCarver | 0:6b753f761943 | 279 | #endif // SIMPLE_M2M_RESOURCE_H |