Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SimpleM2MResource.h Source File

SimpleM2MResource.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #ifndef SIMPLE_M2M_RESOURCE_H
00020 #define SIMPLE_M2M_RESOURCE_H
00021 
00022 
00023 #include "mbed-cloud-client/MbedCloudClient.h"
00024 
00025 /*! \file SimpleM2MResource.h
00026  *  \brief SimpleM2MResourceBase.
00027  * This class provides an easy wrapper base class for creating a simple M2MResource based on
00028  * integer and string values. This class is NOT meant to be directed instantiated but is used
00029  * by the SimpleM2MResourceInt and SimpleM2MResourceString classes to create resources.
00030  */
00031 
00032 class SimpleM2MResourceBase {
00033 
00034 protected:
00035 
00036     // Prevents the use of default constructor.
00037     SimpleM2MResourceBase();
00038 
00039     // Prevents the use of assignment operator.
00040     SimpleM2MResourceBase& operator=( const SimpleM2MResourceBase& /*other*/ );
00041 
00042     // Prevents the use of copy constructor
00043     SimpleM2MResourceBase( const M2MBase& /*other*/ );
00044 
00045     SimpleM2MResourceBase(MbedCloudClient* client, string route);
00046 
00047     /**
00048      * \brief Destructor
00049      */
00050     virtual ~SimpleM2MResourceBase();
00051 
00052 
00053 public:
00054 
00055     /**
00056      * \brief Defines M2MResource internally and creates a necessary LWM2M
00057      * structure like object and object instance based on the given string
00058      * URI path and sets the right M2M operation to the resource.
00059      * \param v The URI path for the resource "Test/0/res" in this format.
00060      * \param opr An operation to be set for the resource.
00061      * \param observable True if the resource is observable, else false.
00062      * \return True if resource is created, else false.
00063      */
00064     bool define_resource_internal(string v,
00065                                   M2MBase::Operation opr,
00066                                   bool observable);
00067 
00068     /**
00069      * \brief Gets the value set in a resource in text format.
00070      * \return The value set in the resource.
00071      */
00072     string get() const;
00073 
00074     /**
00075      * \brief Sets the value in a resource in text format.
00076      * \param v The value to be set.
00077      * \return True if set successfully, else false.
00078      */
00079     bool set(string v);
00080 
00081     /**
00082      * \brief Sets the value in a resource in integer format.
00083      * \param v The value to be set.
00084      * \return True if set successfully, else false.
00085      */
00086     bool set(const int& v);
00087 
00088     /**
00089      * \brief Sets the callback function to be called
00090      * when the resource received a POST command.
00091      * \param fn A function to be called.
00092      * This is used for a statically defined function.
00093      * \return True if set successfully, else false.
00094      */
00095     bool set_post_function(void(*fn)(void*));
00096 
00097     /**
00098      * \brief Sets the callback function to be called
00099      * when a resource received the POST command.
00100      * \param fn A function to be called.
00101      * This is an overloaded function for a class function.
00102      * \return True if set successfully, else false.
00103      */
00104     bool set_post_function(execute_callback fn);
00105 
00106     /**
00107     * \brief Returns the M2MResource object of the registered object through
00108     * the SimpleM2MResourceBase objects.
00109     * \return The object of the M2MResource.
00110     */
00111     M2MResource* get_resource();
00112 
00113     /**
00114     * \brief Calls when there is an indication that the value of the resource
00115     * object is updated by the LWM2M Cloud server.
00116     */
00117     virtual void update(){}
00118 
00119 private:
00120 
00121     /**
00122     * \brief An internal helper function to break the URI path into a list of
00123     * strings such as "Test/0/res" into a list of three strings.
00124     * \param route The URI path in the format "Test/0/res".
00125     * \return A list of strings parsed from the URI path.
00126     */
00127     vector<string> parse_route(const char* route);
00128 
00129 private:
00130     MbedCloudClient*                    _client;   // Not owned
00131     string                              _route;
00132 };
00133 
00134 /**
00135  *  \brief SimpleM2MResourceString.
00136  *  This class provides an easy wrapper base class for creating a simple M2MResource based on
00137  * string values. This class provides an easy access to the M2MResource objects without the application
00138  * requiring to create Objects and Object Instances.
00139  */
00140 
00141 class SimpleM2MResourceString : public SimpleM2MResourceBase
00142 {
00143 public:
00144 
00145     /**
00146      *  \brief Constructor.
00147      *  \param client A handler for MbedCloudClient.
00148      *  \param route The route for the resource such as "Test/0/res".
00149      *  \param v The value of the resource.
00150      *  \param opr An operation that can be supported by the resource.
00151      *  \param observable True if the resource is observable.
00152      *  \param on_update If the resource supports the PUT operation, a function pointer
00153      *  for the callback function that is called when the client receives an
00154      *  updated value for this resource.
00155      */
00156     SimpleM2MResourceString(MbedCloudClient* client,
00157                    const char* route,
00158                    string v,
00159                    M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED,
00160                    bool observable = true,
00161                    FP1<void, string>  on_update = NULL);
00162 
00163     /**
00164      *  \brief Constructor. This is overloaded function.
00165      *  \param client A handler for MbedCloudClient.
00166      *  \param route The route for the resource such as "Test/0/res".
00167      *  \param v The value of the resource.
00168      *  \param opr An operation that can be supported by the resource.
00169      *  \param observable True if resource is observable.
00170      *  \param on_update If the resource supports the PUT operation, a function pointer
00171      *  for the callback function that is called when the client receives an
00172      *  updated value for this resource.
00173      */
00174     SimpleM2MResourceString(MbedCloudClient* client,
00175                    const char* route,
00176                    string v,
00177                    M2MBase::Operation opr,
00178                    bool observable,
00179                    void(*on_update)(string));
00180 
00181 
00182     /**
00183      * \brief Destructor
00184      */
00185     virtual ~SimpleM2MResourceString();
00186 
00187     /**
00188      * \brief Overloaded operator for = operation.
00189      */
00190     string operator=(const string& new_value);
00191 
00192     /**
00193      * \brief Overloaded operator for string() operation.
00194      */
00195     operator string() const;
00196 
00197     /**
00198     * \brief Calls when there is an indication that the value of the resource
00199     * object is updated by the LWM2M Cloud server.
00200     */
00201     virtual void update();
00202 
00203 private:
00204     FP1<void, string>                    _on_update;
00205 };
00206 
00207 /**
00208  *  \brief SimpleM2MResourceInt.
00209  *  This class provides an easy wrapper base class for creating a simple M2MResource based on
00210  * integer values. This class provides easy access to M2MResource objects without the application
00211  * requiring to create Objects and Object Instances.
00212  */
00213 
00214 class SimpleM2MResourceInt : public SimpleM2MResourceBase
00215 {
00216 public:
00217 
00218     /**
00219      *  \brief Constructor.
00220      *  \param client A handler for MbedCloudClient.
00221      *  \param route The route for the resource such as "Test/0/res".
00222      *  \param v The value of the resource.
00223      *  \param opr An operation that can be supported by the resource.
00224      *  \param observable True if the resource is observable, else false.
00225      *  \param on_update If the resource supports the PUT operation, a function pointer
00226      *  for the callback function that is called when the client receives an
00227      *  updated value for this resource.
00228      */
00229     SimpleM2MResourceInt(MbedCloudClient* client,
00230                 const char* route,
00231                 int v,
00232                 M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED,
00233                 bool observable = true,
00234                 FP1<void, int>  on_update = NULL);
00235 
00236     /**
00237      *  \brief Constructor. This is an overloaded function
00238      *  \param client A handler for MbedCloudClient.
00239      *  \param route The route for the resource such as "Test/0/res"
00240      *  \param v The value of the resource.
00241      *  \param opr An operation that can be supported by the resource.
00242      *  \param observable True if the resource is observable, else false.
00243      *  \param on_update If the resource supports the PUT operation, a function pointer
00244      *  for the callback function that is called when the client receives an
00245      *  updated value for this resource.
00246      */
00247     SimpleM2MResourceInt(MbedCloudClient* client,
00248                 const char* route,
00249                 int v,
00250                 M2MBase::Operation opr,
00251                 bool observable,
00252                 void(*on_update)(int));
00253 
00254     /**
00255      * \brief Destructor
00256      */
00257     virtual ~SimpleM2MResourceInt();
00258 
00259     /**
00260      * \brief Overloaded operator for = operation.
00261      */
00262     int operator=(int new_value);
00263 
00264     /**
00265      * \brief Overloaded operator for int() operation.
00266      */
00267     operator int() const;
00268 
00269     /**
00270     * \brief Calls when there is an indication that the value of the resource
00271     * object is updated by the LWM2M Cloud server.
00272     */
00273     virtual void update();
00274 
00275 private:
00276     FP1<void, int>                   _on_update;
00277 };
00278 
00279 #endif // SIMPLE_M2M_RESOURCE_H