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