leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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