sandbox / mbed-client

Fork of mbed-client by Christopher Haster

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 Clears the value of the given resource.
00156      */
00157     virtual void clear_value();
00158 
00159     /**
00160      * \brief Executes the function that is set in "set_execute_function".
00161      * \param arguments The arguments that will be passed to be executed.
00162      */
00163     void execute(void *arguments);
00164 
00165     /**
00166      * \brief Provides the value of the given resource.
00167      * \param value[OUT] A pointer to the resource value.
00168      * \param value_length[OUT] The length of the value pointer.
00169      */
00170     virtual void get_value(uint8_t *&value, uint32_t &value_length);
00171 
00172     /**
00173      * \brief Returns the value pointer of the object.
00174      * \return The value pointer of the object.
00175     */
00176     uint8_t* value() const;
00177 
00178     /**
00179      * \brief Returns the length of the value pointer.
00180      * \return The length of the value pointer.
00181     */
00182     uint32_t value_length() const;
00183 
00184     /**
00185      * \brief Handles the GET request for the registered objects.
00186      * \param nsdl The NSDL handler for the CoAP library.
00187      * \param received_coap_header The CoAP message received from the server.
00188      * \param observation_handler The handler object for sending
00189      * observation callbacks.
00190      * \return sn_coap_hdr_s The message that needs to be sent to the server.
00191      */
00192     virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
00193                                               sn_coap_hdr_s *received_coap_header,
00194                                               M2MObservationHandler *observation_handler = NULL);
00195     /**
00196      * \brief Handles the PUT request for the registered objects.
00197      * \param nsdl The NSDL handler for the CoAP library.
00198      * \param received_coap_header The CoAP message received from the server.
00199      * \param observation_handler The handler object for sending
00200      * observation callbacks.
00201      * \param execute_value_updated True will execute the "value_updated" callback.
00202      * \return sn_coap_hdr_s The message that needs to be sent to the server.
00203      */
00204     virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
00205                                               sn_coap_hdr_s *received_coap_header,                                                                                            
00206                                               M2MObservationHandler *observation_handler,
00207                                               bool &execute_value_updated);
00208 
00209     /**
00210      * \brief Returns the object instance id where resource exists.
00211      * \return Object instance id.
00212     */
00213     uint16_t object_instance_id() const;
00214 
00215     /**
00216      * \brief Returns the object name where resource exists.
00217      * \return Object name.
00218     */
00219     const String& object_name() const;
00220 
00221 protected:
00222 
00223     /**
00224      * \brief Set observer for sending the notification update.
00225      * \param resource The callback handle.
00226      */
00227     void set_resource_observer(M2MResourceCallback *resource);
00228 
00229 private:
00230 
00231     void report();
00232 
00233     bool is_value_changed(const uint8_t* value, const uint32_t value_len);
00234 
00235 private:
00236 
00237     M2MObjectInstanceCallback               &_object_instance_callback;
00238     execute_callback                         _execute_callback;
00239     uint8_t                                 *_value;
00240     uint32_t                                _value_length;
00241     ResourceType                            _resource_type;
00242     M2MResourceCallback                     *_resource_callback; // Not owned
00243     uint16_t                                _object_instance_id;
00244     String                                  _object_name;
00245     FP1<void, void*>                         *_function_pointer;
00246 
00247     friend class Test_M2MResourceInstance;
00248     friend class Test_M2MResource;
00249     friend class Test_M2MObjectInstance;
00250     friend class Test_M2MObject;
00251     friend class Test_M2MDevice;
00252     friend class Test_M2MSecurity;
00253     friend class Test_M2MServer;
00254     friend class Test_M2MNsdlInterface;
00255     friend class Test_M2MFirmware;
00256     friend class Test_M2MTLVSerializer;
00257     friend class Test_M2MTLVDeserializer;
00258 };
00259 
00260 #endif // M2M_RESOURCE_INSTANCE_H