Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

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 #include "mbed-cloud-client/MbedCloudClient.h"
00023 
00024 // As this whole class is build by using C++'s standard template library (STL),
00025 // we have it behind a flag. The cost of STL is 15KB of flash, depending on compiler,
00026 // so on resource constrained devices it is essential to be able to remove any reference to it.
00027 #if MBED_CLOUD_CLIENT_STL_API
00028 
00029 #include <string>
00030 
00031 /*! \file SimpleM2MResource.h
00032  *  \brief SimpleM2MResourceBase.
00033  * This class provides an easy wrapper base class for creating a simple M2MResource based on
00034  * integer and string values. This class is NOT meant to be directed instantiated but is used
00035  * by the SimpleM2MResourceInt and SimpleM2MResourceString classes to create resources.
00036  */
00037 
00038 class SimpleM2MResourceBase {
00039 
00040 protected:
00041 
00042     // Prevents the use of default constructor.
00043     SimpleM2MResourceBase();
00044 
00045     // Prevents the use of assignment operator.
00046     SimpleM2MResourceBase& operator=( const SimpleM2MResourceBase& /*other*/ );
00047 
00048     // Prevents the use of copy constructor
00049     SimpleM2MResourceBase( const M2MBase& /*other*/ );
00050 
00051     SimpleM2MResourceBase(MbedCloudClient* client, string route);
00052 
00053     /**
00054      * \brief Destructor
00055      */
00056     virtual ~SimpleM2MResourceBase();
00057 
00058 
00059 public:
00060 
00061     /**
00062      * \brief Defines M2MResource internally and creates a necessary LWM2M
00063      * structure like object and object instance based on the given string
00064      * URI path and sets the right M2M operation to the resource.
00065      * \param v The URI path for the resource "Test/0/res" in this format.
00066      * \param opr An operation to be set for the resource.
00067      * \param observable True if the resource is observable, else false.
00068      * \return True if resource is created, else false.
00069      */
00070     bool define_resource_internal(string v,
00071                                   M2MBase::Operation opr,
00072                                   bool observable);
00073 
00074     /**
00075      * \brief Gets the value set in a resource in text format.
00076      * \return The value set in the resource.
00077      */
00078     string get() const;
00079 
00080     /**
00081      * \brief Sets the value in a resource in text format.
00082      * \param v The value to be set.
00083      * \return True if set successfully, else false.
00084      */
00085     bool set(string v);
00086 
00087     /**
00088      * \brief Sets the value in a resource in integer format.
00089      * \param v The value to be set.
00090      * \return True if set successfully, else false.
00091      */
00092     bool set(const int& v);
00093 
00094     /**
00095      * \brief Sets the callback function to be called
00096      * when the resource received a POST command.
00097      * \param fn A function to be called.
00098      * This is used for a statically defined function.
00099      * \return True if set successfully, else false.
00100      */
00101     bool set_post_function(void(*fn)(void*));
00102 
00103     /**
00104      * \brief Sets the callback function to be called
00105      * when a resource received the POST command.
00106      * \param fn A function to be called.
00107      * This is an overloaded function for a class function.
00108      * \return True if set successfully, else false.
00109      */
00110     bool set_post_function(execute_callback fn);
00111 
00112     /**
00113     * \brief Returns the M2MResource object of the registered object through
00114     * the SimpleM2MResourceBase objects.
00115     * \return The object of the M2MResource.
00116     */
00117     M2MResource* get_resource();
00118 
00119     /**
00120     * \brief Calls when there is an indication that the value of the resource
00121     * object is updated by the LWM2M Cloud server.
00122     */
00123     virtual void update(){}
00124 
00125 private:
00126 
00127     /**
00128     * \brief An internal helper function to break the URI path into a list of
00129     * strings such as "Test/0/res" into a list of three strings.
00130     * \param route The URI path in the format "Test/0/res".
00131     * \return A list of strings parsed from the URI path.
00132     */
00133     vector<string> parse_route(const char* route);
00134 
00135 private:
00136     MbedCloudClient*                    _client;   // Not owned
00137     string                              _route;
00138 };
00139 
00140 /**
00141  *  \brief SimpleM2MResourceString.
00142  *  This class provides an easy wrapper base class for creating a simple M2MResource based on
00143  * string values. This class provides an easy access to the M2MResource objects without the application
00144  * requiring to create Objects and Object Instances.
00145  */
00146 
00147 class SimpleM2MResourceString : public SimpleM2MResourceBase
00148 {
00149 public:
00150 
00151     /**
00152      *  \brief Constructor.
00153      *  \note This class is deprecated and may be removed in future releases.
00154      *  \param client A handler for MbedCloudClient.
00155      *  \param route The route for the resource such as "Test/0/res".
00156      *  \param v The value of the resource.
00157      *  \param opr An operation that can be supported by the resource.
00158      *  \param observable True if the resource is observable.
00159      *  \param on_update If the resource supports the PUT operation, a function pointer
00160      *  for the callback function that is called when the client receives an
00161      *  updated value for this resource.
00162      */
00163     SimpleM2MResourceString(MbedCloudClient* client,
00164                    const char* route,
00165                    string v,
00166                    M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED,
00167                    bool observable = true,
00168                    FP1<void, string>  on_update = NULL) m2m_deprecated;
00169 
00170     /**
00171      *  \brief Constructor. This is overloaded function.
00172      *  \note This class is deprecated and may be removed in future releases.
00173      *  \param client A handler for MbedCloudClient.
00174      *  \param route The route for the resource such as "Test/0/res".
00175      *  \param v The value of the resource.
00176      *  \param opr An operation that can be supported by the resource.
00177      *  \param observable True if resource is observable.
00178      *  \param on_update If the resource supports the PUT operation, a function pointer
00179      *  for the callback function that is called when the client receives an
00180      *  updated value for this resource.
00181      */
00182     SimpleM2MResourceString(MbedCloudClient* client,
00183                    const char* route,
00184                    string v,
00185                    M2MBase::Operation opr,
00186                    bool observable,
00187                    void(*on_update)(string)) m2m_deprecated;
00188 
00189 
00190     /**
00191      * \brief Destructor
00192      */
00193     virtual ~SimpleM2MResourceString();
00194 
00195     /**
00196      * \brief Overloaded operator for = operation.
00197      */
00198     string operator=(const string& new_value);
00199 
00200     /**
00201      * \brief Overloaded operator for string() operation.
00202      */
00203     operator string() const;
00204 
00205     /**
00206     * \brief Calls when there is an indication that the value of the resource
00207     * object is updated by the LWM2M Cloud server.
00208     */
00209     virtual void update();
00210 
00211 private:
00212     FP1<void, string>                    _on_update;
00213 };
00214 
00215 /**
00216  *  \brief SimpleM2MResourceInt.
00217  *  This class provides an easy wrapper base class for creating a simple M2MResource based on
00218  * integer values. This class provides easy access to M2MResource objects without the application
00219  * requiring to create Objects and Object Instances.
00220  */
00221 
00222 class SimpleM2MResourceInt : public SimpleM2MResourceBase
00223 {
00224 public:
00225 
00226     /**
00227      *  \brief Constructor.
00228      *  \note This class is deprecated and may be removed in future releases.
00229      *  \param client A handler for MbedCloudClient.
00230      *  \param route The route for the resource such as "Test/0/res".
00231      *  \param v The value of the resource.
00232      *  \param opr An operation that can be supported by the resource.
00233      *  \param observable True if the resource is observable, else false.
00234      *  \param on_update If the resource supports the PUT operation, a function pointer
00235      *  for the callback function that is called when the client receives an
00236      *  updated value for this resource.
00237      */
00238     SimpleM2MResourceInt(MbedCloudClient* client,
00239                 const char* route,
00240                 int v,
00241                 M2MBase::Operation opr = M2MBase::GET_PUT_ALLOWED,
00242                 bool observable = true,
00243                 FP1<void, int>  on_update = NULL) m2m_deprecated;
00244 
00245     /**
00246      *  \brief Constructor. This is an overloaded function
00247      *  \note This class is deprecated and may be removed in future releases.
00248      *  \param client A handler for MbedCloudClient.
00249      *  \param route The route for the resource such as "Test/0/res"
00250      *  \param v The value of the resource.
00251      *  \param opr An operation that can be supported by the resource.
00252      *  \param observable True if the resource is observable, else false.
00253      *  \param on_update If the resource supports the PUT operation, a function pointer
00254      *  for the callback function that is called when the client receives an
00255      *  updated value for this resource.
00256      */
00257     SimpleM2MResourceInt(MbedCloudClient* client,
00258                 const char* route,
00259                 int v,
00260                 M2MBase::Operation opr,
00261                 bool observable,
00262                 void(*on_update)(int)) m2m_deprecated;
00263 
00264     /**
00265      * \brief Destructor
00266      */
00267     virtual ~SimpleM2MResourceInt();
00268 
00269     /**
00270      * \brief Overloaded operator for = operation.
00271      */
00272     int operator=(int new_value);
00273 
00274     /**
00275      * \brief Overloaded operator for int() operation.
00276      */
00277     operator int() const;
00278 
00279     /**
00280     * \brief Calls when there is an indication that the value of the resource
00281     * object is updated by the LWM2M Cloud server.
00282     */
00283     virtual void update();
00284 
00285 private:
00286     FP1<void, int>                   _on_update;
00287 };
00288 
00289 #endif
00290 
00291 #endif // SIMPLE_M2M_RESOURCE_H