sandbox / mbed-client

Fork of mbed-client by Christopher Haster

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mbase.h Source File

m2mbase.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_BASE_H
00017 #define M2M_BASE_H
00018 
00019 // Support for std args
00020 #include <stdint.h>
00021 #include "mbed-client/m2mconfig.h"
00022 #include "mbed-client/m2mreportobserver.h"
00023 
00024 //FORWARD DECLARATION
00025 struct sn_coap_hdr_;
00026 typedef sn_coap_hdr_ sn_coap_hdr_s;
00027 struct nsdl_s;
00028 
00029 class M2MObservationHandler;
00030 class M2MReportHandler;
00031 
00032 /**
00033  *  \brief M2MBase.
00034  *  This class is the base class based on which all LWM2M object models
00035  *  can be created. This serves base class for Object, ObjectInstances and Resources.
00036  */
00037 
00038 
00039 
00040 class M2MBase : public M2MReportObserver {
00041 
00042 public:
00043 
00044     /**
00045       * Enum to define the type of object.
00046       */
00047     typedef enum {
00048         Object = 0x0,
00049         Resource = 0x1,
00050         ObjectInstance = 0x2,
00051         ResourceInstance = 0x3
00052     } BaseType;
00053 
00054     /**
00055       * Enum to define observation level.
00056       */
00057     typedef enum {
00058         None                 = 0x0,
00059         R_Attribute          = 0x01,
00060         OI_Attribute         = 0x02,
00061         OIR_Attribute        = 0x03,
00062         O_Attribute          = 0x04,
00063         OR_Attribute         = 0x05,
00064         OOI_Attribute        = 0x06,
00065         OOIR_Attribute       = 0x07
00066     } Observation;
00067 
00068 
00069     /**
00070      * \brief Enum defining an operation that can be
00071      * supported by a given resource.
00072     */
00073     typedef enum {
00074         Static,
00075         Dynamic,
00076         Directory
00077     }Mode;
00078 
00079     /**
00080      * Enum defining an operation that can be
00081      * supported by a given resource.
00082     */
00083     typedef enum {
00084         NOT_ALLOWED                 = 0x00,
00085         GET_ALLOWED                 = 0x01,
00086         PUT_ALLOWED                 = 0x02,
00087         GET_PUT_ALLOWED             = 0x03,
00088         POST_ALLOWED                = 0x04,
00089         GET_POST_ALLOWED            = 0x05,
00090         PUT_POST_ALLOWED            = 0x06,
00091         GET_PUT_POST_ALLOWED        = 0x07,
00092         DELETE_ALLOWED              = 0x08,
00093         GET_DELETE_ALLOWED          = 0x09,
00094         PUT_DELETE_ALLOWED          = 0x0A,
00095         GET_PUT_DELETE_ALLOWED      = 0x0B,
00096         POST_DELETE_ALLOWED         = 0x0C,
00097         GET_POST_DELETE_ALLOWED     = 0x0D,
00098         PUT_POST_DELETE_ALLOWED     = 0x0E,
00099         GET_PUT_POST_DELETE_ALLOWED = 0x0F,
00100 
00101     }Operation;
00102 
00103 protected:
00104 
00105     // Prevents the use of default constructor.
00106     M2MBase();
00107 
00108     // Prevents the use of assignment operator.
00109     M2MBase& operator=( const M2MBase& /*other*/ );
00110 
00111     // Prevents the use of copy constructor
00112     M2MBase( const M2MBase& /*other*/ );
00113 
00114     /**
00115      * \brief Constructor
00116      * \param baseType Type of the object created
00117      * \param name Name of the object
00118      * \param id ID of the object
00119      */
00120     M2MBase(const String &name,
00121             M2MBase::Mode mode);
00122 public:
00123 
00124     /**
00125      * Destructor
00126      */
00127     virtual ~M2MBase();
00128 
00129     /**
00130      * \brief Sets the operation type for an object.
00131      * \param operation Operation to be set.
00132      */
00133     virtual void set_operation(M2MBase::Operation operation);
00134 
00135     /**
00136      * \brief Sets the interface description of the object.
00137      * \param description Description to be set.
00138      */
00139     virtual void set_interface_description(const String &description);
00140 
00141     /**
00142      * \brief Sets the resource type of the object.
00143      * \param resource_type Resource type to be set.
00144      */
00145     virtual void set_resource_type(const String &resource_type);
00146 
00147     /**
00148      * \brief Sets the CoAP content type of the object.
00149      * \param content_type Content Type to be set based on
00150      * CoAP specifications.
00151      */
00152     virtual void set_coap_content_type(const uint8_t content_type);
00153 
00154     /**
00155      * \brief Sets the observable mode for the object.
00156      * \param observable Value for the observation.
00157      */
00158     virtual void set_observable(bool observable);
00159 
00160     /**
00161      * \brief Adds the observation level for the object.
00162      * \param observation_level Level of the observation.
00163      */
00164     virtual void add_observation_level(M2MBase::Observation observation_level);
00165 
00166     /**
00167      * \brief Removes the observation level for the object.
00168      * \param observation_level Level of the observation.
00169      */
00170     virtual void remove_observation_level(M2MBase::Observation observation_level);
00171 
00172     /**
00173      * \brief Sets the object under observation.
00174      * \param observed Value for the observation. When true, starts observing. When false, ongoing observation is cancelled.
00175      * \param handler Handler object for sending
00176      * observation callbacks.
00177      */
00178     virtual void set_under_observation(bool observed,
00179                                        M2MObservationHandler *handler);
00180 
00181     /**
00182      * \brief Sets the observation token value.
00183      * \param token Pointer to the token of the resource.
00184      * \param length Length of the token pointer.
00185      */
00186     virtual void set_observation_token(const uint8_t *token,
00187                                        const uint8_t length);
00188 
00189     /**
00190      * \brief Sets the instance ID of the object.
00191      * \param instance_id Instance ID of the object.
00192      */
00193     virtual void set_instance_id(const uint16_t instance_id);
00194 
00195     /**
00196      * Function is deprecated. Increment of observation number is done internally.
00197      * \brief Sets the observation number of the object.
00198      * \param observation_number Observation number of the object.
00199      */
00200     virtual void set_observation_number(const uint16_t observation_number)
00201         __attribute__ ((deprecated));
00202 
00203     /**
00204      * \brief Sets the max age for the resource value to be cached.
00205      * \param max_age Max age in seconds.
00206      */
00207     virtual void set_max_age(const uint32_t max_age);
00208 
00209     /**
00210      * \brief Returns object type.
00211      * \return BaseType of the object.
00212      */
00213     virtual M2MBase::BaseType base_type() const;
00214 
00215     /**
00216      * \brief Returns the operation type of the object.
00217      * \return Operation Supported operation on the object.
00218      */
00219     virtual M2MBase::Operation operation() const;
00220 
00221     /**
00222      * \brief Returns the object name.
00223      * \return Name of the object.
00224      */
00225     virtual const String &name() const;
00226 
00227     /**
00228      * \brief Returns the object name in integer.
00229      * \return Name of the object in integer.
00230      */
00231     virtual int32_t name_id() const;
00232 
00233     /**
00234      * \brief Returns the object's Instance ID.
00235      * \returns Instance ID of the object.
00236      */
00237     virtual uint16_t instance_id() const;
00238 
00239     /**
00240      * \brief Returns the interface description of the object.
00241      * \return Description of the object.
00242      */
00243     virtual const String& interface_description() const;
00244 
00245     /**
00246      * \brief Returns the resource type of the object.
00247      * \return Resource type of the object.
00248      */
00249     virtual const String& resource_type() const;
00250 
00251     /**
00252      * \brief Returns the CoAP content type of the object.
00253      * \return Content type of the object.
00254      */
00255     virtual uint8_t coap_content_type() const;
00256 
00257     /**
00258      * \brief Returns the observation status of the object.
00259      * \return True if observable, else false.
00260      */
00261     virtual bool is_observable() const;
00262 
00263     /**
00264      * \brief Returns the observation level of the object.
00265      * \return Observation level of the object.
00266      */
00267     virtual M2MBase::Observation observation_level() const;
00268 
00269     /**
00270      * \brief Provides the observation token of the object.
00271      * \param value[OUT] A pointer to the value of the token.
00272      * \param value_length[OUT] Length of the token pointer.
00273      */
00274     virtual void get_observation_token(uint8_t *&token, uint32_t &token_length);
00275 
00276     /**
00277      * \brief Returns the mode of the resource.
00278      * \return Mode of the resource.
00279      */
00280      virtual Mode mode() const;
00281 
00282     /**
00283      * \brief Returns the observation number.
00284      * \return Observation number of the object.
00285      */
00286     virtual uint16_t observation_number() const;
00287 
00288     /**
00289      * \brief Returns max age for the resource value to be cached.
00290      * \return Max age in seconds.
00291      */
00292     virtual uint32_t max_age() const;
00293 
00294 
00295     /**
00296      * \brief Parses the received query for the notification
00297      * attribute.
00298      * \param query The query that needs to be parsed.
00299      * \return True if required attributes are present, else false.
00300      */
00301     virtual bool handle_observation_attribute(char *&query);
00302 
00303     /**
00304      * \brief Handles GET request for the registered objects.
00305      * \param nsdl NSDL handler for the CoAP library.
00306      * \param received_coap_header Received CoAP message from the server.
00307      * \param observation_handler Handler object for sending
00308      * observation callbacks.
00309      * \return sn_coap_hdr_s The message that needs to be sent to server.
00310      */
00311     virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
00312                                               sn_coap_hdr_s *received_coap_header,
00313                                               M2MObservationHandler *observation_handler = NULL);
00314     /**
00315      * \brief Handles PUT request for the registered objects.
00316      * \param nsdl NSDL handler for the CoAP library.
00317      * \param received_coap_header The received CoAP message from the server.
00318      * \param observation_handler The handler object for sending
00319      * observation callbacks.
00320      * \param execute_value_updated True will execute "value_updated" callback.
00321      * \return sn_coap_hdr_s The message that needs to be sent to server.
00322      */
00323     virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
00324                                               sn_coap_hdr_s *received_coap_header,                                                                                            
00325                                               M2MObservationHandler *observation_handler,
00326                                               bool &execute_value_updated);
00327 
00328     /**
00329      * \brief Handles GET request for the registered objects.
00330      * \param nsdl NSDL handler for the CoAP library.
00331      * \param received_coap_header The received CoAP message from the server.
00332      * \param observation_handler The handler object for sending
00333      * observation callbacks.
00334      * \param execute_value_updated True will execute "value_updated" callback.
00335      * \return sn_coap_hdr_s  The message that needs to be sent to server.
00336      */
00337     virtual sn_coap_hdr_s* handle_post_request(nsdl_s *nsdl,
00338                                                sn_coap_hdr_s *received_coap_header,                                               
00339                                                M2MObservationHandler *observation_handler,
00340                                                bool &execute_value_updated);
00341 
00342     /**
00343      * \brief Sets whether this resource will be published to server or not.
00344      * \param register_uri True sets the resource as part of registration message.
00345      */
00346     virtual void set_register_uri( bool register_uri);
00347 
00348     /**
00349      * \brief Returns whether this resource will be published to server or not.
00350      * \return True if the resource is part of the registration message, else false.
00351      */
00352     virtual bool register_uri();
00353 
00354     /**
00355      * \brief Sets object URI path.
00356      * \param path Object path
00357      */
00358     virtual void set_uri_path(const String &path);
00359 
00360     /**
00361      * \brief Returns the URI path of the object.
00362      * \return URI path of the object.
00363      */
00364     virtual const String &uri_path() const;
00365 
00366 protected : // from M2MReportObserver
00367 
00368     virtual void observation_to_be_sent(m2m::Vector<uint16_t> changed_instance_ids,
00369                                         bool send_object = false);
00370 
00371 protected:
00372 
00373     /**
00374      * \brief Sets the Base type for object.
00375      * \param type The type of the base object.
00376      */
00377     virtual void set_base_type(M2MBase::BaseType type);
00378 
00379     /**
00380      * \brief Removes the resource from the CoAP structure.
00381      * \param resource_name The name of the resource.
00382      */
00383     virtual void remove_resource_from_coap(const String &resource_name);
00384 
00385     /**
00386      * \brief Removes an object from the NSDL list.
00387      */
00388     virtual void remove_object_from_coap();
00389 
00390     /**
00391      * \brief Memory allocation required for libCoap.
00392      * \param size The size of memory to be reserved.
00393     */
00394     virtual void* memory_alloc(uint16_t size);
00395 
00396     /**
00397      * \brief Memory free functions required for libCoap.
00398      * \param ptr The Object whose memory needs to be freed.
00399     */
00400     virtual void memory_free(void *ptr);
00401 
00402     /**
00403      * \brief Returns the Report Handler object.
00404      * \return M2MReportHandler object.
00405     */
00406     M2MReportHandler* report_handler();
00407 
00408     /**
00409      * \brief Returns the Observation Handler object.
00410      * \return M2MObservationHandler object.
00411     */
00412     M2MObservationHandler* observation_handler();
00413 
00414 private:
00415 
00416     bool is_integer(const String &value);
00417 
00418 private:
00419 
00420 
00421     M2MReportHandler           *_report_handler;
00422     M2MObservationHandler      *_observation_handler;
00423     M2MBase::Operation          _operation;
00424     M2MBase::Mode               _mode;
00425     M2MBase::BaseType           _base_type;
00426     M2MBase::Observation        _observation_level;
00427     String                      _name;
00428     String                      _resource_type;
00429     int32_t                     _name_id;
00430     String                      _interface_description;
00431     uint8_t                     _coap_content_type;
00432     uint16_t                    _instance_id;
00433     bool                        _observable;
00434     uint16_t                    _observation_number;
00435     uint8_t                     *_token;
00436     uint8_t                     _token_length;
00437     bool                        _register_uri;
00438     String                      _uri_path;
00439     uint32_t                    _max_age;
00440 
00441 friend class Test_M2MBase;
00442 
00443 };
00444 
00445 #endif // M2M_BASE_H
00446