Simple interface for Mbed Cloud Client

Dependents:  

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mresource.h Source File

m2mresource.h

Go to the documentation of this file.
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_H
00017 #define M2M_RESOURCE_H
00018 
00019 #include "mbed-client/m2mvector.h"
00020 #include "mbed-client/m2mresourcebase.h"
00021 #include "mbed-client/m2mresourceinstance.h"
00022 
00023 //FORWARD DECLARATION
00024 class M2MObjectInstance;
00025 typedef Vector<M2MResourceInstance *> M2MResourceInstanceList;
00026 
00027 /*! \file m2mresource.h
00028  *  \brief M2MResource.
00029  *  This class is the base class for mbed Client Resources. All defined
00030  *  LWM2M object models can be created using it. This class will also hold all resources
00031  *  instances associated with the given object.
00032  */
00033 
00034 class M2MResource : public M2MResourceBase {
00035 
00036     friend class M2MObjectInstance;
00037 
00038 public:
00039     class M2MExecuteParameter;
00040 
00041 private: // Constructor and destructor are private,
00042          // which means that these objects can be created or
00043          // deleted only through a function provided by the M2MObjectInstance.
00044 
00045     M2MResource(M2MObjectInstance &_parent,
00046                  const lwm2m_parameters_s* s,
00047                  M2MBase::DataType type);
00048     /**
00049      * \brief Constructor
00050      * \param resource_name The resource name of the object.
00051      * \param resource_type The resource type of the object.
00052      * \param type The resource data type of the object.
00053      * \param value The value pointer of the object.
00054      * \param value_length The length of the value pointer.
00055      * \param path Full path of the resource, eg. 1/2/3. Ownership of the memory is transferred.
00056      * \param object_name The name of the object where the resource exists.
00057      * \param multiple_instance True if the resource supports instances.
00058      * \param external_blockwise_store If true CoAP blocks are passed to application through callbacks
00059      *        otherwise handled in mbed-client-c.
00060      */
00061     M2MResource(M2MObjectInstance &_parent,
00062                 const String &resource_name,
00063                 M2MBase::Mode mode,
00064                 const String &resource_type,
00065                 M2MBase::DataType type,
00066                 const uint8_t *value,
00067                 const uint8_t value_length,
00068                 char *path,
00069                 bool multiple_instance = false,
00070                 bool external_blockwise_store = false);
00071 
00072     /**
00073      * \brief Constructor
00074      * \param resource_name The resource name of the object.
00075      * \param resource_type The resource type of the object.
00076      * \param type The resource data type of the object.
00077      * \param observable Indicates whether the resource is observable or not.
00078      * \param path Full path of the resource, eg. 1/2/3. Ownership of the memory is transferred.
00079      * \param object_name The name of the object where the resource exists.
00080      * \param multiple_instance True if the resource supports instances.
00081      * \param external_blockwise_store If true CoAP blocks are passed to application through callbacks
00082      *        otherwise handled in mbed-client-c.
00083      */
00084     M2MResource(M2MObjectInstance &_parent,
00085                 const String &resource_name,
00086                 M2MBase::Mode mode,
00087                 const String &resource_type,
00088                 M2MBase::DataType type,
00089                 bool observable,
00090                 char *path,
00091                 bool multiple_instance = false,
00092                 bool external_blockwise_store = false);
00093 
00094     // Prevents the use of a default constructor.
00095     M2MResource();
00096 
00097     // Prevents the use of an assignment operator.
00098     M2MResource& operator=( const M2MResource& /*other*/ );
00099 
00100     // Prevents the use of a copy constructor
00101     M2MResource( const M2MResource& /*other*/ );
00102 
00103     /**
00104      * Destructor
00105      */
00106     virtual ~M2MResource();
00107 
00108 public:
00109 
00110     /**
00111      * \brief Adds resource instances to a M2MResource.
00112      * \param resource_instance The resource instance to be added.
00113      */
00114     void add_resource_instance(M2MResourceInstance *resource_instance);
00115 
00116     /**
00117      * \brief Returns whether the resource has multiple
00118      * resource instances or not.
00119      * \return True if the resource base has multiple instances,
00120      * else false.
00121      */
00122     bool supports_multiple_instances() const;
00123 
00124 #ifndef DISABLE_DELAYED_RESPONSE
00125     /**
00126      * \brief Sets whether the resource should send a delayed response for a POST request.
00127      * \param delayed_response A boolean value to set the delayed response.
00128      */
00129     void set_delayed_response(bool delayed_response);
00130 
00131     /**
00132      * \brief A trigger to send the delayed response for the POST request.
00133      * The delayed_response flag must be set before receiving the POST request
00134      * and the value of the resource must be updated before calling this function.
00135      * \return True if a response is sent, else false.
00136      */
00137     bool send_delayed_post_response();
00138 
00139     /**
00140      * \brief Provides the value of the given token.
00141      * \param value[OUT] A pointer to the token value.
00142      * \param value_length[OUT] The length of the token pointer.
00143      */
00144     void get_delayed_token(uint8_t *&token, uint8_t &token_length);
00145 #endif
00146 
00147     /**
00148      * \brief Removes a resource with a given name.
00149      * \param name The name of the resource to be removed.
00150      * \param instance_id The instance ID of the resource to be removed, default is 0.
00151      * \return True if removed, else false.
00152      */
00153     bool remove_resource_instance(uint16_t instance_id = 0);
00154 
00155     /**
00156      * \brief Returns a resource instance with a given name.
00157      * \param instance_id The instance ID of the requested resource, default is 0
00158      * \return M2MResourceInstance object if found, else NULL.
00159      */
00160     M2MResourceInstance* resource_instance(uint16_t instance_id = 0) const;
00161 
00162     /**
00163      * \brief Returns a list of resources.
00164      * \return A list of resources.
00165      */
00166     const M2MResourceInstanceList& resource_instances() const;
00167 
00168     /**
00169      * \brief Returns the total number of resources.
00170      * \return The total number of resources.
00171      */
00172     uint16_t resource_instance_count() const;
00173 
00174     /**
00175      * \brief Returns the value set for delayed response.
00176      * \return The value for delayed response.
00177      */
00178     bool delayed_response() const;
00179 
00180     /**
00181      * \brief Returns the Observation Handler object.
00182      * \return M2MObservationHandler object.
00183     */
00184     virtual M2MObservationHandler* observation_handler() const;
00185 
00186     /**
00187      * \brief Sets the observation handler
00188      * \param handler Observation handler
00189     */
00190     virtual void set_observation_handler(M2MObservationHandler *handler);
00191 
00192     /**
00193      * \brief Parses the received query for a notification
00194      * attribute.
00195      * \return True if required attributes are present, else false.
00196      */
00197     virtual bool handle_observation_attribute(const char *query);
00198 
00199     /**
00200      * \brief Adds the observation level for the object.
00201      * \param observation_level The level of observation.
00202      */
00203     virtual void add_observation_level(M2MBase::Observation observation_level);
00204 
00205     /**
00206      * \brief Removes the observation level from an object.
00207      * \param observation_level The level of observation.
00208      */
00209     virtual void remove_observation_level(M2MBase::Observation observation_level);
00210 
00211     /**
00212      * \brief Handles the GET request for registered objects.
00213      * \param nsdl An NSDL handler for the CoAP library.
00214      * \param received_coap_header The CoAP message received from the server.
00215      * \param observation_handler A handler object for sending
00216      * observation callbacks.
00217      * \return sn_coap_hdr_s The message that needs to be sent to the server.
00218      */
00219     virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
00220                                               sn_coap_hdr_s *received_coap_header,
00221                                               M2MObservationHandler *observation_handler = NULL);
00222     /**
00223      * \brief Handles the PUT request for registered objects.
00224      * \param nsdl An NSDL handler for the CoAP library.
00225      * \param received_coap_header The CoAP message received from the server.
00226      * \param observation_handler A handler object for sending
00227      * observation callbacks.
00228      * \param execute_value_updated True executes the "value_updated" callback.
00229      * \return sn_coap_hdr_s The message that needs to be sent to the server.
00230      */
00231     virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
00232                                               sn_coap_hdr_s *received_coap_header,
00233                                               M2MObservationHandler *observation_handler,
00234                                               bool &execute_value_updated);
00235     /**
00236      * \brief Handles the POST request for registered objects.
00237      * \param nsdl An NSDL handler for the CoAP library.
00238      * \param received_coap_header The CoAP message received from the server.
00239      * \param observation_handler A handler object for sending
00240      * observation callbacks.
00241      * \param execute_value_updated True executes the "value_updated" callback.
00242      * \return sn_coap_hdr_s The message that needs to be sent to the server.
00243      */
00244     virtual sn_coap_hdr_s* handle_post_request(nsdl_s *nsdl,
00245                                                sn_coap_hdr_s *received_coap_header,
00246                                                M2MObservationHandler *observation_handler,
00247                                                bool &execute_value_updated,
00248                                                sn_nsdl_addr_s *address = NULL);
00249 
00250     M2MObjectInstance& get_parent_object_instance() const;
00251 
00252     /**
00253      * \brief Returns the instance ID of the object where the resource exists.
00254      * \return Object instance ID.
00255     */
00256     virtual uint16_t object_instance_id() const;
00257 
00258     /**
00259      * \brief Returns the name of the object where the resource exists.
00260      * \return Object name.
00261     */
00262     virtual const char* object_name() const;
00263 
00264     virtual M2MResource& get_parent_resource() const;
00265 private:
00266     M2MObjectInstance &_parent;
00267 
00268     M2MResourceInstanceList     _resource_instance_list; // owned
00269 #ifndef DISABLE_DELAYED_RESPONSE
00270     uint8_t                     *_delayed_token;
00271     uint8_t                     _delayed_token_len;
00272     bool                        _delayed_response;
00273 #endif    
00274 
00275 friend class Test_M2MResource;
00276 friend class Test_M2MObjectInstance;
00277 friend class Test_M2MObject;
00278 friend class Test_M2MDevice;
00279 friend class Test_M2MSecurity;
00280 friend class Test_M2MServer;
00281 friend class Test_M2MNsdlInterface;
00282 friend class Test_M2MFirmware;
00283 friend class Test_M2MTLVSerializer;
00284 friend class Test_M2MTLVDeserializer;
00285 friend class Test_M2MBase;
00286 friend class Test_M2MResourceInstance;
00287 };
00288 
00289 /**
00290  *  \brief M2MResource::M2MExecuteParameter.
00291  *  This class handles the "Execute" operation arguments.
00292  */
00293 class M2MResource::M2MExecuteParameter {
00294 
00295 private:
00296 
00297     /**
00298      * \brief Constructor, since there is no implementation, it prevents invalid use of it
00299      */
00300     M2MExecuteParameter();
00301 
00302 #ifdef MEMORY_OPTIMIZED_API
00303     M2MExecuteParameter(const char *object_name, const char *resource_name, uint16_t object_instance_id);
00304 #else
00305     // This is a deprecated constructor, to be removed on next release.
00306     M2MExecuteParameter(const String &object_name, const String &resource_name, uint16_t object_instance_id);
00307 #endif
00308 
00309 public:
00310 
00311     /**
00312      * \brief Returns the value of an argument.
00313      * \return uint8_t * The argument value.
00314      */
00315     const uint8_t *get_argument_value() const;
00316 
00317     /**
00318      * \brief Returns the length of the value argument.
00319      * \return uint8_t The argument value length.
00320      */
00321     uint16_t get_argument_value_length() const;
00322 
00323     /**
00324      * \brief Returns the name of the object where the resource exists.
00325      * \return Object name.
00326     */
00327 #ifdef MEMORY_OPTIMIZED_API
00328     const char* get_argument_object_name() const;
00329 #else
00330     const String& get_argument_object_name() const;
00331 #endif
00332 
00333     /**
00334      * \brief Returns the resource name.
00335      * \return Resource name.
00336     */
00337 #ifdef MEMORY_OPTIMIZED_API
00338     const char* get_argument_resource_name() const;
00339 #else
00340     const String& get_argument_resource_name() const;
00341 #endif
00342 
00343     /**
00344      * \brief Returns the instance ID of the object where the resource exists.
00345      * \return Object instance ID.
00346     */
00347     uint16_t get_argument_object_instance_id() const;
00348 
00349 private:
00350     // pointers to const data, not owned by this instance
00351 
00352 #ifdef MEMORY_OPTIMIZED_API
00353     const char      *_object_name;
00354     const char      *_resource_name;
00355 #else
00356     const String    &_object_name;
00357     const String    &_resource_name;
00358 #endif
00359 
00360     const uint8_t   *_value;
00361     uint16_t        _value_length;
00362     uint16_t        _object_instance_id;
00363 
00364 friend class Test_M2MResource;
00365 friend class M2MResource;
00366 };
00367 
00368 #endif // M2M_RESOURCE_H