joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mresourceinstance.h Source File

m2mresourceinstance.h

00001     /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef M2M_RESOURCE_INSTANCE_H
00017 #define M2M_RESOURCE_INSTANCE_H
00018 
00019 #include "mbed-client/m2mbase.h"
00020 #include "mbed-client/functionpointer.h"
00021 
00022 class M2MObjectInstanceCallback {
00023 public:
00024     virtual void notification_update(M2MBase::Observation observation_level) = 0;
00025 };
00026 
00027 /**
00028  *  \brief M2MResourceInstance.
00029  *  This class is the base class for mbed Client Resources. All defined
00030  *  LWM2M resource models can be created based on it.
00031  */
00032 typedef FP1<void,void*>  execute_callback ;
00033 typedef void(*execute_callback_2) (void *arguments);
00034 
00035 class M2MResourceCallback;
00036 
00037 class M2MResourceInstance : public M2MBase {
00038 
00039 friend class M2MObjectInstance;
00040 friend class M2MResource;
00041 
00042 public:
00043 
00044     /**
00045      * Enum defining a resource type that can be
00046      * supported by a given resource.
00047     */
00048     typedef enum {
00049         STRING,
00050         INTEGER,
00051         FLOAT,
00052         BOOLEAN,
00053         OPAQUE,
00054         TIME,
00055         OBJLINK
00056     }ResourceType;
00057 
00058 
00059 private: // Constructor and destructor are private
00060          // which means that these objects can be created or
00061          // deleted only through a function provided by the M2MObjectInstance.
00062     /**
00063      * \brief A constructor for creating a resource.
00064      * \param resource_name The name of the resource.
00065      * \param resource_type The type of the resource.
00066      * \param type, The resource data type of the object.
00067      * \param object_instance_id Object instance id where resource exists.
00068      * \param object_name Object name where resource exists.
00069      */
00070     M2MResourceInstance(const String &resource_name,
00071                         const String &resource_type,
00072                         M2MResourceInstance::ResourceType type,
00073                         M2MObjectInstanceCallback &object_instance_callback,
00074                         const uint16_t object_instance_id = 0,
00075                         const String &object_name = "");
00076 
00077     /**
00078      * \brief A Constructor for creating a resource.
00079      * \param resource_name The name of the resource.
00080      * \param resource_type The type of the resource.
00081      * \param type The resource data type of the object.
00082      * \param value The value pointer of the object.
00083      * \param value_length The length of the value pointer.
00084      * \param value_length The length of the value pointer.
00085      * \param object_instance_id Object instance id where resource exists.
00086      * \param object_name Object name where resource exists.
00087      */
00088     M2MResourceInstance(const String &resource_name,
00089                         const String &resource_type,
00090                         M2MResourceInstance::ResourceType type,
00091                         const uint8_t *value,
00092                         const uint8_t value_length,
00093                         M2MObjectInstanceCallback &object_instance_callback,
00094                         const uint16_t object_instance_id = 0,
00095                         const String &object_name = "");
00096 
00097     // Prevents the use of default constructor.
00098     M2MResourceInstance();
00099 
00100     // Prevents the use of assignment operator.
00101     M2MResourceInstance& operator=( const M2MResourceInstance& /*other*/ );
00102 
00103     // Prevents the use of copy constructor
00104     M2MResourceInstance( const M2MResourceInstance& /*other*/ );
00105 
00106     /**
00107      * Destructor
00108      */
00109     virtual ~M2MResourceInstance();
00110 
00111 public:
00112 
00113     /**
00114      * \brief Returns the object type.
00115      * \return BaseType.
00116      */
00117     virtual M2MBase::BaseType base_type() const;
00118 
00119     /**
00120      * \brief Returns the resource data type.
00121      * \return ResourceType.
00122      */
00123     virtual M2MResourceInstance::ResourceType resource_instance_type() const;
00124 
00125     /**
00126      * \brief Parses the received query for a notification
00127      * attribute.
00128      * \return True if required attributes are present, else false.
00129      */
00130     virtual bool handle_observation_attribute(char *&query);
00131 
00132     /**
00133      * \brief Sets the function that should be executed when this
00134      * resource receives a POST command.
00135      * \param callback The function pointer that needs to be executed.
00136      */
00137     virtual void set_execute_function(execute_callback  callback);
00138 
00139     /**
00140      * \brief Sets the function that should be executed when this
00141      * resource receives a POST command.
00142      * \param callback The function pointer that needs to be executed.
00143      */
00144     virtual void set_execute_function(execute_callback_2 callback);
00145 
00146     /**
00147      * \brief Sets the value of the given resource.
00148      * \param value, A pointer to the value to be set on the resource.
00149      * \param value_length The length of the value pointer.
00150      * \return True if successfully set, else false.
00151      */
00152     virtual bool set_value(const uint8_t *value, const uint32_t value_length);
00153 
00154     /**
00155      * \brief Sets the value of the given resource.
00156      * \param value, new value which is to be formatted into a string
00157      * and set on the resource.
00158      * \return True if successfully set, else false.
00159      */
00160     virtual bool set_value(int64_t value);
00161 
00162     /**
00163      * \brief Clears the value of the given resource.
00164      */
00165     virtual void clear_value();
00166 
00167     /**
00168      * \brief Executes the function that is set in "set_execute_function".
00169      * \param arguments The arguments that will be passed to be executed.
00170      */
00171     void execute(void *arguments);
00172 
00173     /**
00174      * \brief Provides the value of the given resource.
00175      * \param value[OUT] A pointer to the resource value.
00176      * \param value_length[OUT] The length of the value pointer.
00177      */
00178     virtual void get_value(uint8_t *&value, uint32_t &value_length);
00179 
00180     /**
00181      * \brief Converts value to int and return it. Note: conversion
00182      * errors are not detected.
00183      */
00184     int get_value_int();
00185 
00186     /**
00187      * Get the value as a String object. No encoding/charset conversions
00188      * done for the value, just a raw copy.
00189      */
00190     String get_value_string() const;
00191 
00192     /**
00193      * \brief Returns the value pointer of the object.
00194      * \return The value pointer of the object.
00195     */
00196     uint8_t* value() const;
00197 
00198     /**
00199      * \brief Returns the length of the value pointer.
00200      * \return The length of the value pointer.
00201     */
00202     uint32_t value_length() const;
00203 
00204     /**
00205      * \brief Handles the GET request for the registered objects.
00206      * \param nsdl The NSDL handler for the CoAP library.
00207      * \param received_coap_header The CoAP message received from the server.
00208      * \param observation_handler The handler object for sending
00209      * observation callbacks.
00210      * \return sn_coap_hdr_s The message that needs to be sent to the server.
00211      */
00212     virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
00213                                               sn_coap_hdr_s *received_coap_header,
00214                                               M2MObservationHandler *observation_handler = NULL);
00215     /**
00216      * \brief Handles the PUT request for the registered objects.
00217      * \param nsdl The NSDL handler for the CoAP library.
00218      * \param received_coap_header The CoAP message received from the server.
00219      * \param observation_handler The handler object for sending
00220      * observation callbacks.
00221      * \param execute_value_updated True will execute the "value_updated" callback.
00222      * \return sn_coap_hdr_s The message that needs to be sent to the server.
00223      */
00224     virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
00225                                               sn_coap_hdr_s *received_coap_header,                                                                                            
00226                                               M2MObservationHandler *observation_handler,
00227                                               bool &execute_value_updated);
00228 
00229     /**
00230      * \brief Returns the object instance id where resource exists.
00231      * \return Object instance id.
00232     */
00233     uint16_t object_instance_id() const;
00234 
00235     /**
00236      * \brief Returns the object name where resource exists.
00237      * \return Object name.
00238     */
00239     const String& object_name() const;
00240 
00241 protected:
00242 
00243     /**
00244      * \brief Set observer for sending the notification update.
00245      * \param resource The callback handle.
00246      */
00247     void set_resource_observer(M2MResourceCallback *resource);
00248 
00249 private:
00250 
00251     void report();
00252 
00253     bool is_value_changed(const uint8_t* value, const uint32_t value_len);
00254 
00255 private:
00256 
00257     M2MObjectInstanceCallback               &_object_instance_callback;
00258     execute_callback                         _execute_callback;
00259     uint8_t                                 *_value;
00260     uint32_t                                _value_length;
00261     M2MResourceCallback                     *_resource_callback; // Not owned
00262     String                                  _object_name;
00263     FP1<void, void*>                         *_function_pointer;
00264 
00265     uint16_t                                _object_instance_id;
00266     ResourceType                            _resource_type;
00267 
00268     friend class Test_M2MResourceInstance;
00269     friend class Test_M2MResource;
00270     friend class Test_M2MObjectInstance;
00271     friend class Test_M2MObject;
00272     friend class Test_M2MDevice;
00273     friend class Test_M2MSecurity;
00274     friend class Test_M2MServer;
00275     friend class Test_M2MNsdlInterface;
00276     friend class Test_M2MFirmware;
00277     friend class Test_M2MTLVSerializer;
00278     friend class Test_M2MTLVDeserializer;
00279 };
00280 
00281 #endif // M2M_RESOURCE_INSTANCE_H