Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m2mbase.h Source File

m2mbase.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_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 #include "mbed-client/functionpointer.h"
00024 #include "mbed-client/m2mstringbuffer.h"
00025 #ifdef ENABLE_ASYNC_REST_RESPONSE
00026 #include "mbed-client/coap_response.h"
00027 #endif
00028 #include "nsdl-c/sn_nsdl.h"
00029 #include "sn_coap_header.h"
00030 #include "nsdl-c/sn_nsdl_lib.h"
00031 
00032 //FORWARD DECLARATION
00033 struct sn_coap_hdr_;
00034 typedef sn_coap_hdr_ sn_coap_hdr_s;
00035 struct nsdl_s;
00036 struct sn_nsdl_addr_;
00037 typedef sn_nsdl_addr_ sn_nsdl_addr_s;
00038 
00039 typedef FP1<void, const char*> value_updated_callback;
00040 typedef void(*value_updated_callback2) (const char* object_name);
00041 class M2MObservationHandler;
00042 class M2MReportHandler;
00043 
00044 class M2MObjectInstance;
00045 class M2MObject;
00046 class M2MResource;
00047 class M2MEndpoint;
00048 
00049 
00050 /*! \file m2mbase.h
00051  *  \brief M2MBase.
00052  *  This class is the base class based on which all LWM2M object models
00053  *  can be created.
00054  *
00055  *  This serves as a base class for Objects, ObjectInstances and Resources.
00056  */
00057 
00058 /*! \class M2MBase
00059  *  \brief The base class based on which all LwM2M object models can be created.
00060  *
00061  * It serves as the base class for Objects, ObjectInstances and Resources.
00062  */
00063 class M2MBase : public M2MReportObserver {
00064 
00065 public:
00066 
00067     /**
00068       * \brief Enum to define the type of object.
00069       */
00070     typedef enum {
00071         Object = 0x0,
00072         Resource = 0x1,
00073         ObjectInstance = 0x2,
00074         ResourceInstance = 0x3
00075 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
00076         ,ObjectDirectory = 0x4
00077 #endif
00078     } BaseType;
00079 
00080     /**
00081       * \brief Enum to define observation level.
00082       */
00083     typedef enum {
00084         None                 = 0x0,
00085         R_Attribute          = 0x01,
00086         OI_Attribute         = 0x02,
00087         OIR_Attribute        = 0x03,
00088         O_Attribute          = 0x04,
00089         OR_Attribute         = 0x05,
00090         OOI_Attribute        = 0x06,
00091         OOIR_Attribute       = 0x07
00092     } Observation;
00093 
00094 
00095     /**
00096      * \brief Enum defining a resource type.
00097     */
00098     typedef enum {
00099         Static,
00100         Dynamic,
00101         Directory
00102     }Mode;
00103 
00104     /**
00105      * \brief Enum defining a resource data type.
00106     */
00107     typedef enum {
00108         STRING,
00109         INTEGER,
00110         FLOAT,
00111         BOOLEAN,
00112         OPAQUE,
00113         TIME,
00114         OBJLINK
00115     }DataType;
00116 
00117     /**
00118      * \brief Enum defining an operation that can be
00119      * supported by a given resource.
00120     */
00121     typedef enum {
00122         NOT_ALLOWED                 = 0x00,
00123         GET_ALLOWED                 = 0x01,
00124         PUT_ALLOWED                 = 0x02,
00125         GET_PUT_ALLOWED             = 0x03,
00126         POST_ALLOWED                = 0x04,
00127         GET_POST_ALLOWED            = 0x05,
00128         PUT_POST_ALLOWED            = 0x06,
00129         GET_PUT_POST_ALLOWED        = 0x07,
00130         DELETE_ALLOWED              = 0x08,
00131         GET_DELETE_ALLOWED          = 0x09,
00132         PUT_DELETE_ALLOWED          = 0x0A,
00133         GET_PUT_DELETE_ALLOWED      = 0x0B,
00134         POST_DELETE_ALLOWED         = 0x0C,
00135         GET_POST_DELETE_ALLOWED     = 0x0D,
00136         PUT_POST_DELETE_ALLOWED     = 0x0E,
00137         GET_PUT_POST_DELETE_ALLOWED = 0x0F
00138     }Operation;
00139 
00140     /**
00141      * \brief Enum defining an status codes that can happen when
00142      * sending confirmable message.
00143     */
00144     typedef enum {
00145         MESSAGE_STATUS_INIT = 0,           // Initial state.
00146         MESSAGE_STATUS_BUILD_ERROR,        // CoAP message building fails.
00147         MESSAGE_STATUS_RESEND_QUEUE_FULL,  // CoAP resend queue full.
00148         MESSAGE_STATUS_SENT,               // Message sent to the server but ACK not yet received.
00149         MESSAGE_STATUS_DELIVERED,          // Received ACK from server.
00150         MESSAGE_STATUS_SEND_FAILED,        // Message sending failed.
00151         MESSAGE_STATUS_SUBSCRIBED,         // Server has started the observation
00152         MESSAGE_STATUS_UNSUBSCRIBED,       // Server has stopped the observation (RESET message or GET with observe 1)
00153         MESSAGE_STATUS_REJECTED            // Server has rejected the response
00154     } MessageDeliveryStatus;
00155 
00156     typedef enum {
00157         NOTIFICATION = 0,
00158         DELAYED_POST_RESPONSE,
00159         BLOCK_SUBSCRIBE,
00160         PING,
00161 #ifdef ENABLE_ASYNC_REST_RESPONSE
00162         DELAYED_RESPONSE,
00163 #endif // ENABLE_ASYNC_REST_RESPONSE
00164     } MessageType;
00165 
00166     enum MaxPathSize {
00167         MAX_NAME_SIZE = 64,
00168         MAX_INSTANCE_SIZE = 5,
00169 
00170         MAX_PATH_SIZE = ((MAX_NAME_SIZE * 2) + (MAX_INSTANCE_SIZE * 2) + 3 + 1),
00171         MAX_PATH_SIZE_2 = ((MAX_NAME_SIZE * 2) + MAX_INSTANCE_SIZE + 2 + 1),
00172         MAX_PATH_SIZE_3 = (MAX_NAME_SIZE + (MAX_INSTANCE_SIZE * 2) + 2 + 1),
00173         MAX_PATH_SIZE_4 = (MAX_NAME_SIZE + MAX_INSTANCE_SIZE + 1 + 1)
00174     };
00175 
00176     // The setter for this callback (set_notification_delivery_status_cb()) is in m2m_deprecated
00177     // category, but it can not be used here as then the GCC will scream for the declaration of
00178     // setter, not just from references of it.
00179     typedef void(*notification_delivery_status_cb) (const M2MBase& base,
00180                                                     const NotificationDeliveryStatus status,
00181                                                     void *client_args);
00182 
00183     typedef void(*message_delivery_status_cb) (const M2MBase& base,
00184                                                const MessageDeliveryStatus status,
00185                                                const MessageType type,
00186                                                void *client_args);
00187 
00188 #ifdef ENABLE_ASYNC_REST_RESPONSE
00189     /**
00190      * \brief Type definition for an asynchronous CoAP request callback function.
00191      * \param operation The operation, for example M2MBase::PUT_ALLOWED.
00192      * \param token The token. Client needs to copy this if it cannot respond immediately.
00193      * \param token_len The length of the token.
00194      * \param buffer The payload of the request. Client needs to copy this if it cannot respond immediately.
00195      * \param buffer_size The size of the payload.
00196      * \param client_args Some pointer given by client when requesting asynchronus request callback using
00197      *        set_async_coap_request_cb.
00198      */
00199     typedef void (*handle_async_coap_request_cb)(const M2MBase &base,
00200                                                  M2MBase::Operation operation,
00201                                                  const uint8_t *token,
00202                                                  const uint8_t token_len,
00203                                                  const uint8_t *buffer,
00204                                                  size_t buffer_size,
00205                                                  void *client_args);
00206 #endif // ENABLE_ASYNC_REST_RESPONSE
00207 
00208     /*! \brief LwM2M parameters.
00209      */
00210     typedef struct lwm2m_parameters {
00211         //add multiple_instances
00212         uint32_t            max_age; // todo: add flag
00213         /*! \union identifier
00214          *  \brief Parameter identifier.
00215          */
00216         union {
00217             char*               name; //for backwards compatibility
00218             uint16_t            instance_id; // XXX: this is not properly aligned now, need to reorder these after the elimination is done
00219         } identifier;
00220         sn_nsdl_dynamic_resource_parameters_s *dynamic_resource_params;
00221         BaseType            base_type : 3;
00222         M2MBase::DataType   data_type : 3;
00223         bool                multiple_instance;
00224         bool                free_on_delete;   /**< \brief true if struct is dynamically allocated and it
00225                                                  and its members (name) are to be freed on destructor.
00226                                                  \note The `sn_nsdl_dynamic_resource_parameters_s` has
00227                                                  its own similar, independent flag.
00228 
00229                                                  \note This also serves as a read-only flag. */
00230        bool                 identifier_int_type;
00231        bool                 read_write_callback_set; /**< \brief If set, all the read and write operations are handled in callbacks
00232                                                          and the resource value is not stored anymore in M2MResourceBase. */
00233     } lwm2m_parameters_s;
00234 
00235 protected:
00236 
00237     // Prevents the use of default constructor.
00238     M2MBase();
00239 
00240     // Prevents the use of assignment operator.
00241     M2MBase& operator=( const M2MBase& /*other*/ );
00242 
00243     // Prevents the use of copy constructor
00244     M2MBase( const M2MBase& /*other*/ );
00245 
00246     /**
00247      * \brief Constructor
00248      * \param name Name of the object created.
00249      * \param mode Type of the resource.
00250      * \param resource_type Textual information of resource.
00251      * \param path Path of the object like 3/0/1
00252      * \param external_blockwise_store If true CoAP blocks are passed to application through callbacks
00253      *        otherwise handled in mbed-client-c.
00254      */
00255     M2MBase(const String &name,
00256             M2MBase::Mode mode,
00257 #ifndef DISABLE_RESOURCE_TYPE
00258             const String &resource_type,
00259 #endif
00260             char *path,
00261             bool external_blockwise_store,
00262             bool multiple_instance,
00263             M2MBase::DataType type = M2MBase::OBJLINK);
00264 
00265     M2MBase(const lwm2m_parameters_s* s);
00266 
00267 public:
00268 
00269     /**
00270      * \brief Destructor
00271      */
00272     virtual ~M2MBase();
00273 
00274     /**
00275      * \brief Sets the operation type for an object.
00276      * \param operation The operation to be set.
00277      */
00278     void set_operation(M2MBase::Operation operation);
00279 
00280 #if !defined(MEMORY_OPTIMIZED_API) || defined(RESOURCE_ATTRIBUTES_LIST)
00281     /**
00282      * \brief Sets the interface description of the object.
00283      * \param description The description to be set.
00284      */
00285 #if !defined(DISABLE_INTERFACE_DESCRIPTION) || defined(RESOURCE_ATTRIBUTES_LIST)
00286     void set_interface_description(const String &description);
00287 
00288     /**
00289      * \brief Sets the interface description of the object.
00290      * \param description The description to be set.
00291      */
00292     void set_interface_description(const char *description);
00293 
00294     /**
00295      * \brief Returns the interface description of the object.
00296      * \return The interface description of the object.
00297      */
00298     const char* interface_description() const;
00299 #endif
00300 #if !defined(DISABLE_RESOURCE_TYPE) || defined(RESOURCE_ATTRIBUTES_LIST)
00301     /**
00302      * \brief Sets the resource type of the object.
00303      * \param resource_type The resource type to be set.
00304      */
00305     virtual void set_resource_type(const String &resource_type);
00306 
00307     /**
00308      * \brief Sets the resource type of the object.
00309      * \param resource_type The resource type to be set.
00310      */
00311     virtual void set_resource_type(const char *resource_type);
00312 
00313     /**
00314      * \brief Returns the resource type of the object.
00315      * \return The resource type of the object.
00316      */
00317     const char* resource_type() const;
00318 #endif
00319 #endif
00320 
00321     /**
00322      * \brief Sets the CoAP content type of the object.
00323      * \param content_type The content type to be set based on
00324      * CoAP specifications.
00325      */
00326     void set_coap_content_type(const uint16_t content_type);
00327 
00328     /**
00329      * \brief Sets the observable mode for the object.
00330      * \param observable A value for the observation.
00331      */
00332     void set_observable(bool observable);
00333 
00334     /**
00335      * \brief Sets the object to be auto-observable.
00336      *
00337      * \note This is not a standard CoAP or LWM2M feature and it only works in mbed Cloud.
00338      * \note This must be called before registration process, since this info must be in a registration message.
00339      * \note Auto-observable will take higher precedence if both observable methods are set.
00340      *
00341      * \param auto_observable Is auto-obs feature enabled or not.
00342      */
00343     void set_auto_observable(bool auto_observable);
00344 
00345     /**
00346      * \brief Adds the observation level for the object.
00347      * \param observation_level The level of observation.
00348      */
00349     virtual void add_observation_level(M2MBase::Observation observation_level);
00350 
00351     /**
00352      * \brief Removes the observation level for the object.
00353      * \param observation_level The level of observation.
00354      */
00355     virtual void remove_observation_level(M2MBase::Observation observation_level);
00356 
00357     /**
00358      * \brief Sets the object under observation.
00359      * \param observed The value for observation. When true, starts observing. When false, the ongoing observation is cancelled.
00360      * \param handler A handler object for sending
00361      * observation callbacks.
00362      */
00363     void set_under_observation(bool observed,
00364                                M2MObservationHandler *handler);
00365     /**
00366      * \brief Returns the Observation Handler object.
00367      * \return M2MObservationHandler object.
00368     */
00369     virtual M2MObservationHandler* observation_handler() const = 0;
00370 
00371     /**
00372      * \brief Sets the observation handler
00373      * \param handler Observation handler
00374     */
00375     virtual void set_observation_handler(M2MObservationHandler *handler) = 0;
00376 
00377     /**
00378      * \brief Sets the instance ID of the object.
00379      * \param instance_id The instance ID of the object.
00380      */
00381     void set_instance_id(const uint16_t instance_id);
00382 
00383     /**
00384      * \brief Sets the max age for the resource value to be cached.
00385      * \param max_age The max age in seconds.
00386      */
00387     void set_max_age(const uint32_t max_age);
00388 
00389     /**
00390      * \brief Returns the object type.
00391      * \return The base type of the object.
00392      */
00393     M2MBase::BaseType base_type() const;
00394 
00395     /**
00396      * \brief Returns the operation type of the object.
00397      * \return The supported operation on the object.
00398      */
00399     M2MBase::Operation operation() const;
00400 
00401     /**
00402      * \brief Returns the object name.
00403      * \return The name of the object.
00404      */
00405     const char* name() const;
00406 
00407     /**
00408      * \brief Returns the object name in integer.
00409      * \return The name of the object in integer.
00410      */
00411     int32_t name_id() const;
00412 
00413     /**
00414      * \brief Returns the object's instance ID.
00415      * \returns The instance ID of the object.
00416      */
00417     uint16_t instance_id() const;
00418 
00419     /**
00420      * \brief Returns the path of the object.
00421      * \return The path of the object (eg. 3/0/1).
00422      */
00423     const char* uri_path() const;
00424 
00425     /**
00426      * \brief Returns the CoAP content type of the object.
00427      * \return The CoAP content type of the object.
00428      */
00429     uint16_t coap_content_type() const;
00430 
00431     /**
00432      * \brief Returns the observation status of the object.
00433      * \return True if observable, else false.
00434      */
00435     bool is_observable() const;
00436 
00437     /**
00438      * \brief Returns the auto observation status of the object.
00439      * \return True if observable, else false.
00440      */
00441     bool is_auto_observable() const;
00442 
00443     /**
00444      * \brief Returns the observation level of the object.
00445      * \return The observation level of the object.
00446      */
00447     M2MBase::Observation observation_level() const;
00448 
00449     /**
00450      * \brief Returns the mode of the resource.
00451      * \return The mode of the resource.
00452      */
00453      Mode mode() const;
00454 
00455     /**
00456      * \brief Returns the observation number.
00457      * \return The observation number of the object.
00458      */
00459     uint16_t observation_number() const;
00460 
00461     /**
00462      * \brief Returns the max age for the resource value to be cached.
00463      * \return The maax age in seconds.
00464      */
00465     uint32_t max_age() const;
00466 
00467     /**
00468      * \brief Parses the received query for the notification
00469      * attribute.
00470      * \param query The query that needs to be parsed.
00471      * \return True if required attributes are present, else false.
00472      */
00473     virtual bool handle_observation_attribute(const char *query);
00474 
00475     /**
00476      * \brief Handles GET request for the registered objects.
00477      * \param nsdl An NSDL handler for the CoAP library.
00478      * \param received_coap_header The received CoAP message from the server.
00479      * \param observation_handler A handler object for sending
00480      * observation callbacks.
00481      * \return sn_coap_hdr_s The message that needs to be sent to server.
00482      */
00483     virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
00484                                               sn_coap_hdr_s *received_coap_header,
00485                                               M2MObservationHandler *observation_handler = NULL);
00486     /**
00487      * \brief Handles PUT request for the registered objects.
00488      * \param nsdl An NSDL handler for the CoAP library.
00489      * \param received_coap_header The received CoAP message from the server.
00490      * \param observation_handler A handler object for sending
00491      * observation callbacks.
00492      * \param execute_value_updated True executes the "value_updated" callback.
00493      * \return sn_coap_hdr_s The message that needs to be sent to server.
00494      */
00495     virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
00496                                               sn_coap_hdr_s *received_coap_header,
00497                                               M2MObservationHandler *observation_handler,
00498                                               bool &execute_value_updated);
00499 
00500     /**
00501      * \brief Handles GET request for the registered objects.
00502      * \param nsdl An NSDL handler for the CoAP library.
00503      * \param received_coap_header The received CoAP message from the server.
00504      * \param observation_handler A handler object for sending
00505      * observation callbacks.
00506      * \param execute_value_updated True executes the "value_updated" callback.
00507      * \return sn_coap_hdr_s  The message that needs to be sent to server.
00508      */
00509     virtual sn_coap_hdr_s* handle_post_request(nsdl_s *nsdl,
00510                                                sn_coap_hdr_s *received_coap_header,
00511                                                M2MObservationHandler *observation_handler,
00512                                                bool &execute_value_updated,
00513                                                sn_nsdl_addr_s *address = NULL);
00514 
00515     /**
00516      * \brief Executes the function that is set in "set_notification_delivery_status_cb".
00517      * Note: the setter for this callback is marked as m2m_deprecated, but there is no point
00518      * having it here, as then the code will always give warnings. This simply must be there
00519      * until the set_notification_delivery_status_cb() is removed.
00520      */
00521     void send_notification_delivery_status(const M2MBase& object, const NotificationDeliveryStatus status);
00522 
00523     /**
00524      * \brief Executes the function that is set in "set_message_delivery_status_cb".
00525      */
00526     void send_message_delivery_status(const M2MBase& object, const MessageDeliveryStatus status, const MessageType type);
00527 
00528     /**
00529      * \brief Sets whether this resource is published to server or not.
00530      * \param register_uri True sets the resource as part of registration message.
00531      */
00532     void set_register_uri(bool register_uri);
00533 
00534     /**
00535      * \brief Returns whether this resource is published to server or not.
00536      * \return True if the resource is a part of the registration message, else false.
00537      */
00538     bool register_uri();
00539 
00540     /**
00541      * @brief Returns whether this resource is under observation or not.
00542      * @return True if the resource is under observation, else false,
00543      */
00544     bool is_under_observation() const;
00545 
00546     /**
00547      * @brief Sets the function that is executed when this
00548      * object receives a PUT or POST command.
00549      * @param callback The function pointer that is called.
00550      * @return True, if callback could be set, false otherwise.
00551      */
00552     bool set_value_updated_function(value_updated_callback callback);
00553 
00554     /**
00555      * @brief Sets the function that is executed when this
00556      * object receives a PUT or POST command.
00557      * @param callback The function pointer that is called.
00558      * @return True, if callback could be set, false otherwise.
00559      */
00560     bool set_value_updated_function(value_updated_callback2 callback);
00561 
00562     /**
00563      * @brief Returns whether a callback function is set or not.
00564      * @return True if the callback function is set, else false.
00565      */
00566     bool is_value_updated_function_set() const;
00567 
00568     /**
00569      * @brief Calls the function that is set in the "set_value_updated_function".
00570      * @param name The name of the object.
00571      */
00572     void execute_value_updated(const String& name);
00573 
00574     /**
00575      * @brief Returns length of the object name.
00576      * @return Length of the object name.
00577      */
00578     size_t resource_name_length() const;
00579 
00580     /**
00581      * @brief Returns the resource information.
00582      * @return Resource information.
00583      */
00584     sn_nsdl_dynamic_resource_parameters_s* get_nsdl_resource() const;
00585 
00586     /**
00587      * @brief Returns the resource structure.
00588      * @return Resource structure.
00589      */
00590     M2MBase::lwm2m_parameters_s* get_lwm2m_parameters() const;
00591 
00592 #ifdef ENABLE_ASYNC_REST_RESPONSE
00593     /**
00594      * \brief A trigger to send the async response for the CoAP request.
00595      * \param code The code for the response, for example: 'COAP_RESPONSE_CHANGED'.
00596      * \param payload Payload for the resource.
00597      * \param payload_len Length of the payload.
00598      * \param token Token for the incoming CoAP request.
00599      * \param token_len Token length for the incoming CoAP request.
00600      * \return True if a response is sent, else False.
00601      */
00602     bool send_async_response_with_code(const uint8_t *payload,
00603                                        size_t payload_len,
00604                                        const uint8_t* token,
00605                                        const uint8_t token_len,
00606                                        coap_response_code_e code = COAP_RESPONSE_CHANGED);
00607 
00608     /**
00609      * @brief Sets the function that is executed when CoAP request arrives.
00610      * Callback is not called if the request are invalid, for example content-type is not matching.
00611      * In that case the error response is sent by the client itself.
00612      * @param callback The function pointer that is called.
00613      * @param client_args The argument which is passed to the callback function.
00614      */
00615     bool set_async_coap_request_cb(handle_async_coap_request_cb callback, void *client_args);
00616 
00617 #endif //ENABLE_ASYNC_REST_RESPONSE
00618 
00619     /**
00620      * @brief Returns the notification message id.
00621      * @return Message id.
00622      */
00623     uint16_t get_notification_msgid() const m2m_deprecated;
00624 
00625     /**
00626      * @brief Sets the notification message id.
00627      * This is used to map RESET and EMPTY ACK messages.
00628      * @param msgid The message id.
00629      */
00630     void set_notification_msgid(uint16_t msgid) m2m_deprecated;
00631 
00632     /**
00633      * @brief Sets the function that is executed when notification message state changes.
00634      * @param callback The function pointer that is called.
00635      * @param client_args The argument which is passed to the callback function.
00636      */
00637     bool set_notification_delivery_status_cb(notification_delivery_status_cb callback, void *client_args) m2m_deprecated;
00638 
00639     /**
00640      * @brief Sets the function that is executed when message state changes.
00641      * Currently this is used to track notifications and delayed response delivery statuses.
00642      * @param callback The function pointer that is called.
00643      * @param client_args The argument which is passed to the callback function.
00644      */
00645     bool set_message_delivery_status_cb(message_delivery_status_cb callback, void *client_args);
00646 
00647 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
00648     static char* create_path(const M2MEndpoint &parent, const char *name);
00649 #endif
00650     static char* create_path(const M2MObject &parent, const char *name);
00651     static char* create_path(const M2MObject &parent, uint16_t object_instance);
00652     static char* create_path(const M2MResource &parent, uint16_t resource_instance);
00653     static char* create_path(const M2MResource &parent, const char *name);
00654     static char* create_path(const M2MObjectInstance &parent, const char *name);
00655 
00656 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
00657 
00658     /**
00659      * @brief The data is set deleted and it needs to be updated into Device Management.
00660      *        Current implementation maintains the deleted state only in M2MEndpoint.
00661      *        The deleted state is `false` for every other M2M class.
00662      */
00663     virtual void set_deleted();
00664 
00665 
00666     /**
00667      * @brief The deleted state check function.
00668      * @return True if the deleted state is set, else false.
00669      */
00670     virtual bool is_deleted();
00671 
00672 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION
00673 
00674 protected: // from M2MReportObserver
00675 
00676     virtual bool observation_to_be_sent(const m2m::Vector<uint16_t> &changed_instance_ids,
00677                                         uint16_t obs_number,
00678                                         bool send_object = false);
00679 
00680     /**
00681      * \brief Sets the base type for an object.
00682      * \param type The base type of the object.
00683      */
00684     void set_base_type(M2MBase::BaseType type);
00685 
00686     /**
00687      * \brief Memory allocation required for libCoap.
00688      * \param size The size of memory to be reserved.
00689     */
00690     static void* memory_alloc(uint32_t size);
00691 
00692     /**
00693      * \brief Memory free functions required for libCoap.
00694      * \param ptr The object whose memory needs to be freed.
00695     */
00696     static void memory_free(void *ptr);
00697 
00698     /**
00699      * \brief Allocate and make a copy of given zero terminated string. This
00700      * is functionally equivalent with strdup().
00701      * \param source The source string to copy, may not be NULL.
00702     */
00703     static char* alloc_string_copy(const char* source);
00704 
00705     /**
00706      * \brief Allocate (size + 1) amount of memory, copy size bytes into
00707      * it and add zero termination.
00708      * \param source The source string to copy, may not be NULL.
00709      * \param size The size of memory to be reserved.
00710     */
00711     static uint8_t* alloc_string_copy(const uint8_t* source, uint32_t size);
00712 
00713     /**
00714      * \brief Allocate (size) amount of memory, copy size bytes into it.
00715      * \param source The source buffer to copy, may not be NULL.
00716      * \param size The size of memory to be reserved.
00717     */
00718     static uint8_t* alloc_copy(const uint8_t* source, uint32_t size);
00719 
00720     // validate string length to be [min_length..max_length]
00721     static bool validate_string_length(const String &string, size_t min_length, size_t max_length);
00722     static bool validate_string_length(const char* string, size_t min_length, size_t max_length);
00723 
00724     /**
00725      * \brief Create Report Handler object.
00726      * \return M2MReportHandler object.
00727     */
00728     M2MReportHandler* create_report_handler();
00729 
00730     /**
00731      * \brief Returns the Report Handler object.
00732      * \return M2MReportHandler object.
00733     */
00734     M2MReportHandler* report_handler() const;
00735 
00736     static bool build_path(StringBuffer<MAX_PATH_SIZE> &buffer, const char *s1, uint16_t i1, const char *s2, uint16_t i2);
00737 
00738     static bool build_path(StringBuffer<MAX_PATH_SIZE_2> &buffer, const char *s1, uint16_t i1, const char *s2);
00739 
00740     static bool build_path(StringBuffer<MAX_PATH_SIZE_3> &buffer, const char *s1, uint16_t i1, uint16_t i2);
00741 
00742     static bool build_path(StringBuffer<MAX_PATH_SIZE_4> &buffer, const char *s1, uint16_t i1);
00743 
00744     static char* stringdup(const char* s);
00745 
00746     /**
00747      * \brief Delete the resource structures owned by this object. Note: this needs
00748      * to be called separately from each subclass' destructor as this method uses a
00749      * virtual method and the call needs to be done at same class which has the
00750      * implementation of the pure virtual method.
00751      */
00752     void free_resources();
00753 
00754     /**
00755      * \brief Returns notification send status.
00756      * \return Notification status.
00757      */
00758     NotificationDeliveryStatus get_notification_delivery_status() const m2m_deprecated;
00759 
00760     /**
00761      * \brief Clears the notification send status to initial state.
00762      */
00763     void clear_notification_delivery_status() m2m_deprecated;
00764 
00765     /**
00766      * \brief Provides the observation token of the object.
00767      * \param[out] token A pointer to the value of the token.
00768      * \param[out] token_length The length of the token pointer.
00769      */
00770     void get_observation_token(uint8_t *token, uint8_t &token_length) const;
00771 
00772     /**
00773      * \brief Sets the observation token value.
00774      * \param token A pointer to the token of the resource.
00775      * \param length The length of the token pointer.
00776      */
00777     void set_observation_token(const uint8_t *token,
00778                                const uint8_t length);
00779 
00780     /**
00781      * \brief The data has changed and it needs to be updated into Mbed Cloud.
00782      *        Current implementation maintains the changed state only in M2MEndpoint. If any of the changes in an
00783      *        object changes the M2M registration structure, the information is propagated to M2MEndpoint using
00784      *        this interface.
00785      */
00786     virtual void set_changed();
00787 
00788     /**
00789      * \brief Returns the owner object. Can return NULL if the object has no parent.
00790      */
00791     virtual M2MBase *get_parent() const;
00792 
00793     /**
00794      * \brief Checks whether blockwise is needed to send resource value to server.
00795      * \param nsdl An NSDL handler for the CoAP library.
00796      * \param payload_len Length of the CoAP payload.
00797      * \return True if blockwise transfer is needed, else false.
00798      */
00799     static bool is_blockwise_needed(const nsdl_s *nsdl, uint32_t payload_len);
00800 
00801     /**
00802      * \brief Handles subscription request.
00803      * \param nsdl An NSDL handler for the CoAP library.
00804      * \param received_coap_header The received CoAP message from the server.
00805      * \param coap_response The CoAP response to be sent to server.
00806      * \param observation_handler A handler object for sending
00807      * observation callbacks.
00808      */
00809     void handle_observation(nsdl_s *nsdl,
00810                             const sn_coap_hdr_s &received_coap_header,
00811                             sn_coap_hdr_s &coap_response,
00812                             M2MObservationHandler *observation_handler,
00813                             sn_coap_msg_code_e &response_code);
00814 
00815     /**
00816      * \brief Start the observation.
00817      * \param received_coap_header An NSDL handler for the CoAP library.
00818      * \param observation_handler A handler object for sending
00819      * observation callbacks.
00820      */
00821     void start_observation(const sn_coap_hdr_s &received_coap_header, M2MObservationHandler *observation_handler);
00822 
00823 #ifdef ENABLE_ASYNC_REST_RESPONSE
00824 
00825     /**
00826      * @brief Executes the callback set in 'set_async_coap_request_cb'.
00827      * @param coap_request CoAP request containing the requesting payload and payload size.
00828      * @param operation Operation mode to be passed to the application.
00829      * @param handled Caller to know whether callback is processed or not.
00830      */
00831     void call_async_coap_request_callback(sn_coap_hdr_s *coap_request,
00832                                           M2MBase::Operation operation,
00833                                           bool &handled);
00834 
00835     /**
00836      * @brief Returns whether asynchronous callback is set or not.
00837      * @return True if set otherwise False.
00838      */
00839     bool is_async_coap_request_callback_set();
00840 
00841 #endif //ENABLE_ASYNC_REST_RESPONSE
00842 
00843 private:
00844     static bool is_integer(const String &value);
00845 
00846     static bool is_integer(const char *value);
00847 
00848     static char* create_path_base(const M2MBase &parent, const char *name);
00849 
00850     lwm2m_parameters_s          *_sn_resource;
00851     M2MReportHandler            *_report_handler; // TODO: can be broken down to smaller classes with inheritance.
00852 
00853 friend class Test_M2MBase;
00854 friend class Test_M2MObject;
00855 friend class M2MNsdlInterface;
00856 friend class M2MInterfaceFactory;
00857 friend class M2MObject;
00858 };
00859 
00860 #endif // M2M_BASE_H