Timothy Beight / Mbed 2 deprecated 6_songs-from-the-cloud

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

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 class M2MResourceCallback;
00034 
00035 class M2MResourceInstance : public M2MBase {
00036 
00037 friend class M2MObjectInstance;
00038 friend class M2MResource;
00039 
00040 public:
00041 
00042     /**
00043      * Enum defining a resource type that can be
00044      * supported by a given resource.
00045     */
00046     typedef enum {
00047         STRING,
00048         INTEGER,
00049         FLOAT,
00050         BOOLEAN,
00051         OPAQUE,
00052         TIME,
00053         OBJLINK
00054     }ResourceType;
00055 
00056 
00057 private: // Constructor and destructor are private
00058          // which means that these objects can be created or
00059          // deleted only through a function provided by the M2MObjectInstance.
00060     /**
00061      * @brief Constructor for creating resource.
00062      * @param resource_name, Name of the resource.
00063      * @param resource_type, Resource Type.
00064      * @param type, Resource Data Type of the object.
00065      */
00066     M2MResourceInstance(const String &resource_name,
00067                         const String &resource_type,
00068                         M2MResourceInstance::ResourceType type,
00069                         M2MObjectInstanceCallback &object_instance_callback);
00070 
00071     /**
00072      * @brief Constructor for creating a resource.
00073      * @param resource_name, Name of the resource.
00074      * @param resource_type, Resource Type.
00075      * @param type, Resource Data Type of the object.
00076      * @param value, Value pointer of the object.
00077      * @param value_length, Length of the value pointer.
00078      */
00079     M2MResourceInstance(const String &resource_name,
00080                         const String &resource_type,
00081                         M2MResourceInstance::ResourceType type,
00082                         const uint8_t *value,
00083                         const uint8_t value_length,
00084                         M2MObjectInstanceCallback &object_instance_callback);
00085 
00086     // Prevents the use of default constructor.
00087     M2MResourceInstance();
00088 
00089     // Prevents the use of assignment operator.
00090     M2MResourceInstance& operator=( const M2MResourceInstance& /*other*/ );
00091 
00092     // Prevents the use of copy constructor
00093     M2MResourceInstance( const M2MResourceInstance& /*other*/ );
00094 
00095     /**
00096      * Destructor
00097      */
00098     virtual ~M2MResourceInstance();
00099 
00100 public:
00101 
00102     /**
00103      * @brief Returns object type.
00104      * @return BaseType.
00105      */
00106     virtual M2MBase::BaseType base_type() const;
00107 
00108     /**
00109      * @brief Returns resource data type.
00110      * @return ResourceType.
00111      */
00112     virtual M2MResourceInstance::ResourceType resource_instance_type() const;
00113 
00114     /**
00115      * @brief Parses the received query for a notification
00116      * attribute.
00117      * @return True if required attributes are present, else false.
00118      */
00119     virtual bool handle_observation_attribute(char *&query);
00120 
00121     /**
00122      * @brief Sets the function that should be executed when this
00123      * resource receives a POST command.
00124      * @param callback, Function pointer that needs to be executed.
00125      */
00126     virtual void set_execute_function(execute_callback  callback);
00127 
00128     /**
00129      * @brief Sets the value of the given resource.
00130      * @param value, Pointer to the value to be set on the resource.
00131      * @param value_length , Length of the value pointer.
00132      * @return True if successfully set, else false.
00133      */
00134     virtual bool set_value(const uint8_t *value, const uint32_t value_length);
00135 
00136     /**
00137      * @brief Clears the value of the given resource.
00138      */
00139     virtual void clear_value();
00140 
00141     /**
00142      * @brief Executes the function that is set in "set_execute_function".
00143      * @param arguments, Arguments that will be passed to be executed.
00144      */
00145     void execute(void *arguments);
00146 
00147     /**
00148      * @brief Provides the value of the given resource.
00149      * @param value[OUT], Pointer to the resource value.
00150      * @param value_length[OUT], Length of the value pointer.
00151      */
00152     virtual void get_value(uint8_t *&value, uint32_t &value_length);
00153 
00154     /**
00155      * @brief Returns value pointer of the object.
00156      * @return Value pointer of the object.
00157     */
00158     uint8_t* value() const;
00159 
00160     /**
00161      * @brief Returns length of the value pointer.
00162      * @return Length of the value pointer.
00163     */
00164     uint32_t value_length() const;
00165 
00166     /**
00167      * @brief Handles GET request for the registered objects.
00168      * @param nsdl, NSDL handler for the CoAP library.
00169      * @param received_coap_header, CoAP message received from the server.
00170      * @param observation_handler, Handler object for sending
00171      * observation callbacks.
00172      * @return sn_coap_hdr_s,  Message that needs to be sent to the server.
00173      */
00174     virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
00175                                               sn_coap_hdr_s *received_coap_header,
00176                                               M2MObservationHandler *observation_handler = NULL);
00177     /**
00178      * @brief Handles PUT request for the registered objects.
00179      * @param nsdl, NSDL handler for the CoAP library.
00180      * @param received_coap_header, CoAP message received from the server.
00181      * @param observation_handler, Handler object for sending
00182      * observation callbacks.
00183      * @return sn_coap_hdr_s,  Message that needs to be sent to the server.
00184      */
00185     virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
00186                                               sn_coap_hdr_s *received_coap_header,
00187                                               M2MObservationHandler *observation_handler = NULL);
00188 
00189 protected:
00190 
00191     /**
00192      * @brief Set observer for sending notification update.
00193      * @param resource, callback handle.
00194      */
00195     void set_resource_observer(M2MResourceCallback *resource);
00196 
00197 private:
00198 
00199     void report();
00200 
00201     bool is_value_changed(const uint8_t* value, const uint32_t value_len);
00202 
00203 private:
00204 
00205     M2MObjectInstanceCallback               &_object_instance_callback;
00206     execute_callback                         _execute_callback;
00207     uint8_t                                 *_value;
00208     uint32_t                                _value_length;
00209     ResourceType                            _resource_type;
00210     M2MResourceCallback                     *_resource_callback; // Not owned
00211 
00212     friend class Test_M2MResourceInstance;
00213     friend class Test_M2MResource;
00214     friend class Test_M2MObjectInstance;
00215     friend class Test_M2MObject;
00216     friend class Test_M2MDevice;
00217     friend class Test_M2MSecurity;
00218     friend class Test_M2MServer;
00219     friend class Test_M2MNsdlInterface;
00220     friend class Test_M2MFirmware;
00221     friend class Test_M2MTLVSerializer;
00222     friend class Test_M2MTLVDeserializer;
00223 };
00224 
00225 #endif // M2M_RESOURCE_INSTANCE_H