Mistake on this page? Email us
m2mbase.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 ARM Limited. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  * Licensed under the Apache License, Version 2.0 (the License); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef M2M_BASE_H
17 #define M2M_BASE_H
18 
19 // Support for std args
20 #include <stdint.h>
21 #include "mbed-client/m2mconfig.h"
25 #ifdef ENABLE_ASYNC_REST_RESPONSE
27 #endif
28 #include "nsdl-c/sn_nsdl.h"
29 #include "sn_coap_header.h"
30 #include "nsdl-c/sn_nsdl_lib.h"
31 
32 //FORWARD DECLARATION
33 struct sn_coap_hdr_;
34 typedef sn_coap_hdr_ sn_coap_hdr_s;
35 struct nsdl_s;
36 struct sn_nsdl_addr_;
37 typedef sn_nsdl_addr_ sn_nsdl_addr_s;
38 
40 typedef void(*value_updated_callback2) (const char* object_name);
42 class M2MReportHandler;
43 
44 class M2MObjectInstance;
45 class M2MObject;
46 class M2MResource;
47 class M2MEndpoint;
48 
49 
63 class M2MBase : public M2MReportObserver {
64 
65 public:
66 
70  typedef enum {
71  Object = 0x0,
72  Resource = 0x1,
73  ObjectInstance = 0x2,
74  ResourceInstance = 0x3
75 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
76  ,ObjectDirectory = 0x4
77 #endif
78  } BaseType;
79 
83  typedef enum {
84  None = 0x0,
85  R_Attribute = 0x01,
86  OI_Attribute = 0x02,
87  OIR_Attribute = 0x03,
88  O_Attribute = 0x04,
89  OR_Attribute = 0x05,
90  OOI_Attribute = 0x06,
91  OOIR_Attribute = 0x07
92  } Observation;
93 
94 
98  typedef enum {
99  Static,
100  Dynamic,
101  Directory
102  }Mode;
103 
107  typedef enum {
108  STRING,
109  INTEGER,
110  FLOAT,
111  BOOLEAN,
112  OPAQUE,
113  TIME,
114  OBJLINK
115  }DataType;
116 
121  typedef enum {
122  NOT_ALLOWED = 0x00,
123  GET_ALLOWED = 0x01,
124  PUT_ALLOWED = 0x02,
125  GET_PUT_ALLOWED = 0x03,
126  POST_ALLOWED = 0x04,
127  GET_POST_ALLOWED = 0x05,
128  PUT_POST_ALLOWED = 0x06,
129  GET_PUT_POST_ALLOWED = 0x07,
130  DELETE_ALLOWED = 0x08,
131  GET_DELETE_ALLOWED = 0x09,
132  PUT_DELETE_ALLOWED = 0x0A,
133  GET_PUT_DELETE_ALLOWED = 0x0B,
134  POST_DELETE_ALLOWED = 0x0C,
135  GET_POST_DELETE_ALLOWED = 0x0D,
136  PUT_POST_DELETE_ALLOWED = 0x0E,
137  GET_PUT_POST_DELETE_ALLOWED = 0x0F
138  }Operation;
139 
144  typedef enum {
145  MESSAGE_STATUS_INIT = 0, // Initial state.
146  MESSAGE_STATUS_BUILD_ERROR, // CoAP message building fails.
147  MESSAGE_STATUS_RESEND_QUEUE_FULL, // CoAP resend queue full.
148  MESSAGE_STATUS_SENT, // Message sent to the server but ACK not yet received.
149  MESSAGE_STATUS_DELIVERED, // Received ACK from server.
150  MESSAGE_STATUS_SEND_FAILED, // Message sending failed.
151  MESSAGE_STATUS_SUBSCRIBED, // Server has started the observation
152  MESSAGE_STATUS_UNSUBSCRIBED, // Server has stopped the observation (RESET message or GET with observe 1)
153  MESSAGE_STATUS_REJECTED // Server has rejected the response
155 
156  typedef enum {
157  NOTIFICATION = 0,
158  DELAYED_POST_RESPONSE,
159  BLOCK_SUBSCRIBE,
160  PING,
161 #ifdef ENABLE_ASYNC_REST_RESPONSE
162  DELAYED_RESPONSE,
163 #endif // ENABLE_ASYNC_REST_RESPONSE
164  } MessageType;
165 
166  enum MaxPathSize {
167  MAX_NAME_SIZE = 64,
168  MAX_INSTANCE_SIZE = 5,
169 
170  MAX_PATH_SIZE = ((MAX_NAME_SIZE * 2) + (MAX_INSTANCE_SIZE * 2) + 3 + 1),
171  MAX_PATH_SIZE_2 = ((MAX_NAME_SIZE * 2) + MAX_INSTANCE_SIZE + 2 + 1),
172  MAX_PATH_SIZE_3 = (MAX_NAME_SIZE + (MAX_INSTANCE_SIZE * 2) + 2 + 1),
173  MAX_PATH_SIZE_4 = (MAX_NAME_SIZE + MAX_INSTANCE_SIZE + 1 + 1)
174  };
175 
176  // The setter for this callback (set_notification_delivery_status_cb()) is in m2m_deprecated
177  // category, but it can not be used here as then the GCC will scream for the declaration of
178  // setter, not just from references of it.
179  typedef void(*notification_delivery_status_cb) (const M2MBase& base,
180  const NotificationDeliveryStatus status,
181  void *client_args);
182 
183  typedef void(*message_delivery_status_cb) (const M2MBase& base,
184  const MessageDeliveryStatus status,
185  const MessageType type,
186  void *client_args);
187 
188 #ifdef ENABLE_ASYNC_REST_RESPONSE
189 
199  typedef void (*handle_async_coap_request_cb)(const M2MBase &base,
201  const uint8_t *token,
202  const uint8_t token_len,
203  const uint8_t *buffer,
204  size_t buffer_size,
205  void *client_args);
206 #endif // ENABLE_ASYNC_REST_RESPONSE
207 
210  typedef struct lwm2m_parameters {
211  //add multiple_instances
212  uint32_t max_age; // todo: add flag
216  union {
217  char* name; //for backwards compatibility
218  uint16_t instance_id; // XXX: this is not properly aligned now, need to reorder these after the elimination is done
219  } identifier;
220  sn_nsdl_dynamic_resource_parameters_s *dynamic_resource_params;
221  BaseType base_type : 3;
222  M2MBase::DataType data_type : 3;
223  bool multiple_instance;
230  bool identifier_int_type;
234 
235 protected:
236 
237  // Prevents the use of default constructor.
238  M2MBase();
239 
240  // Prevents the use of assignment operator.
241  M2MBase& operator=( const M2MBase& /*other*/ );
242 
243  // Prevents the use of copy constructor
244  M2MBase( const M2MBase& /*other*/ );
245 
255  M2MBase(const String &name,
257 #ifndef DISABLE_RESOURCE_TYPE
258  const String &resource_type,
259 #endif
260  char *path,
261  bool external_blockwise_store,
262  bool multiple_instance,
263  M2MBase::DataType type = M2MBase::OBJLINK);
264 
265  M2MBase(const lwm2m_parameters_s* s);
266 
267 public:
268 
272  virtual ~M2MBase();
273 
278  void set_operation(M2MBase::Operation operation);
279 
280 #if !defined(MEMORY_OPTIMIZED_API) || defined(RESOURCE_ATTRIBUTES_LIST)
281 
285 #if !defined(DISABLE_INTERFACE_DESCRIPTION) || defined(RESOURCE_ATTRIBUTES_LIST)
286  void set_interface_description(const String &description);
287 
292  void set_interface_description(const char *description);
293 
298  const char* interface_description() const;
299 #endif
300 #if !defined(DISABLE_RESOURCE_TYPE) || defined(RESOURCE_ATTRIBUTES_LIST)
301 
305  virtual void set_resource_type(const String &resource_type);
306 
311  virtual void set_resource_type(const char *resource_type);
312 
317  const char* resource_type() const;
318 #endif
319 #endif
320 
326  void set_coap_content_type(const uint16_t content_type);
327 
332  void set_observable(bool observable);
333 
343  void set_auto_observable(bool auto_observable);
344 
350 
356 
363  void set_under_observation(bool observed,
364  M2MObservationHandler *handler);
369  virtual M2MObservationHandler* observation_handler() const = 0;
370 
375  virtual void set_observation_handler(M2MObservationHandler *handler) = 0;
376 
381  void set_instance_id(const uint16_t instance_id);
382 
387  void set_max_age(const uint32_t max_age);
388 
393  M2MBase::BaseType base_type() const;
394 
400 
405  const char* name() const;
406 
411  int32_t name_id() const;
412 
417  uint16_t instance_id() const;
418 
423  const char* uri_path() const;
424 
429  uint16_t coap_content_type() const;
430 
435  bool is_observable() const;
436 
441  bool is_auto_observable() const;
442 
448 
453  Mode mode() const;
454 
459  uint16_t observation_number() const;
460 
465  uint32_t max_age() const;
466 
467 #if defined (MBED_CONF_MBED_CLIENT_ENABLE_OBSERVATION_PARAMETERS) && (MBED_CONF_MBED_CLIENT_ENABLE_OBSERVATION_PARAMETERS == 1)
468 
474  virtual bool handle_observation_attribute(const char *query);
475 #endif
476 
485  virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
486  sn_coap_hdr_s *received_coap_header,
497  virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
498  sn_coap_hdr_s *received_coap_header,
500  bool &execute_value_updated);
501 
511  virtual sn_coap_hdr_s* handle_post_request(nsdl_s *nsdl,
512  sn_coap_hdr_s *received_coap_header,
514  bool &execute_value_updated,
515  sn_nsdl_addr_s *address = NULL);
516 
523  void send_notification_delivery_status(const M2MBase& object, const NotificationDeliveryStatus status);
524 
528  void send_message_delivery_status(const M2MBase& object, const MessageDeliveryStatus status, const MessageType type);
529 
534  void set_register_uri(bool register_uri);
535 
540  bool register_uri();
541 
546  bool is_under_observation() const;
547 
555 
562  bool set_value_updated_function(value_updated_callback2 callback);
563 
568  bool is_value_updated_function_set() const;
569 
574  void execute_value_updated(const String& name);
575 
580  size_t resource_name_length() const;
581 
586  sn_nsdl_dynamic_resource_parameters_s* get_nsdl_resource() const;
587 
593 
594 #ifdef ENABLE_ASYNC_REST_RESPONSE
595 
604  bool send_async_response_with_code(const uint8_t *payload,
605  size_t payload_len,
606  const uint8_t* token,
607  const uint8_t token_len,
608  coap_response_code_e code = COAP_RESPONSE_CHANGED);
609 
617  bool set_async_coap_request_cb(handle_async_coap_request_cb callback, void *client_args);
618 
619 #endif //ENABLE_ASYNC_REST_RESPONSE
620 
625  uint16_t get_notification_msgid() const m2m_deprecated;
626 
632  void set_notification_msgid(uint16_t msgid) m2m_deprecated;
633 
639  bool set_notification_delivery_status_cb(notification_delivery_status_cb callback, void *client_args) m2m_deprecated;
640 
647  bool set_message_delivery_status_cb(message_delivery_status_cb callback, void *client_args);
648 
649 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
650  static char* create_path(const M2MEndpoint &parent, const char *name);
651 #endif
652  static char* create_path(const M2MObject &parent, const char *name);
653  static char* create_path(const M2MObject &parent, uint16_t object_instance);
654  static char* create_path(const M2MResource &parent, uint16_t resource_instance);
655  static char* create_path(const M2MResource &parent, const char *name);
656  static char* create_path(const M2MObjectInstance &parent, const char *name);
657 
658 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
659 
665  virtual void set_deleted();
666 
667 
672  virtual bool is_deleted();
673 
674 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION
675 
676 protected: // from M2MReportObserver
677 
678  virtual bool observation_to_be_sent(const m2m::Vector<uint16_t> &changed_instance_ids,
679  uint16_t obs_number,
680  bool send_object = false);
681 
686  void set_base_type(M2MBase::BaseType type);
687 
692  static void* memory_alloc(uint32_t size);
693 
698  static void memory_free(void *ptr);
699 
705  static char* alloc_string_copy(const char* source);
706 
713  static uint8_t* alloc_string_copy(const uint8_t* source, uint32_t size);
714 
720  static uint8_t* alloc_copy(const uint8_t* source, uint32_t size);
721 
722  // validate string length to be [min_length..max_length]
723  static bool validate_string_length(const String &string, size_t min_length, size_t max_length);
724  static bool validate_string_length(const char* string, size_t min_length, size_t max_length);
725 
730  M2MReportHandler* create_report_handler();
731 
736  M2MReportHandler* report_handler() const;
737 
738  static bool build_path(StringBuffer<MAX_PATH_SIZE> &buffer, const char *s1, uint16_t i1, const char *s2, uint16_t i2);
739 
740  static bool build_path(StringBuffer<MAX_PATH_SIZE_2> &buffer, const char *s1, uint16_t i1, const char *s2);
741 
742  static bool build_path(StringBuffer<MAX_PATH_SIZE_3> &buffer, const char *s1, uint16_t i1, uint16_t i2);
743 
744  static bool build_path(StringBuffer<MAX_PATH_SIZE_4> &buffer, const char *s1, uint16_t i1);
745 
746  static char* stringdup(const char* s);
747 
754  void free_resources();
755 
760  NotificationDeliveryStatus get_notification_delivery_status() const m2m_deprecated;
761 
765  void clear_notification_delivery_status() m2m_deprecated;
766 
772  void get_observation_token(uint8_t *token, uint8_t &token_length) const;
773 
779  void set_observation_token(const uint8_t *token,
780  const uint8_t length);
781 
788  virtual void set_changed();
789 
793  virtual M2MBase *get_parent() const;
794 
801  static bool is_blockwise_needed(const nsdl_s *nsdl, uint32_t payload_len);
802 
811  void handle_observation(nsdl_s *nsdl,
812  const sn_coap_hdr_s &received_coap_header,
813  sn_coap_hdr_s &coap_response,
815  sn_coap_msg_code_e &response_code);
816 
823  void start_observation(const sn_coap_hdr_s &received_coap_header, M2MObservationHandler *observation_handler);
824 
825 #ifdef ENABLE_ASYNC_REST_RESPONSE
826 
833  void call_async_coap_request_callback(sn_coap_hdr_s *coap_request,
834  M2MBase::Operation operation,
835  bool &handled);
836 
841  bool is_async_coap_request_callback_set();
842 
843 #endif //ENABLE_ASYNC_REST_RESPONSE
844 
845 private:
846  static bool is_integer(const String &value);
847 
848  static bool is_integer(const char *value);
849 
850  static char* create_path_base(const M2MBase &parent, const char *name);
851 
852  lwm2m_parameters_s *_sn_resource;
853  M2MReportHandler *_report_handler; // TODO: can be broken down to smaller classes with inheritance.
854 
855 friend class Test_M2MBase;
856 friend class Test_M2MObject;
857 friend class M2MNsdlInterface;
858 friend class M2MInterfaceFactory;
859 friend class M2MObject;
860 };
861 
862 #endif // M2M_BASE_H
bool set_message_delivery_status_cb(message_delivery_status_cb callback, void *client_args)
Sets the function that is executed when message state changes. Currently this is used to track notifi...
sn_nsdl_dynamic_resource_parameters_s * get_nsdl_resource() const
Returns the resource information.
Mode
Enum defining a resource type.
Definition: m2mbase.h:98
The base class for Client Objects.
Definition: m2mobject.h:39
Definition: m2mobservationhandler.h:31
virtual void set_changed()
The data has changed and it needs to be updated into Device Management. Current implementation mainta...
const char * interface_description() const
Returns the interface description of the object.
size_t resource_name_length() const
Returns length of the object name.
int32_t name_id() const
Returns the object name in integer.
void set_observable(bool observable)
Sets the observable mode for the object.
void free_resources()
Delete the resource structures owned by this object. Note: this needs to be called separately from ea...
void get_observation_token(uint8_t *token, uint8_t &token_length) const
Provides the observation token of the object.
bool is_value_updated_function_set() const
Returns whether a callback function is set or not.
The base class based on which all LwM2M object models can be created.
Definition: m2mbase.h:63
void set_register_uri(bool register_uri)
Sets whether this resource is published to server or not.
void set_base_type(M2MBase::BaseType type)
Sets the base type for an object.
void set_under_observation(bool observed, M2MObservationHandler *handler)
Sets the object under observation.
static uint8_t * alloc_copy(const uint8_t *source, uint32_t size)
Allocate (size) amount of memory, copy size bytes into it.
M2MBase::Operation operation() const
Returns the operation type of the object.
void send_notification_delivery_status(const M2MBase &object, const NotificationDeliveryStatus status)
Executes the function that is set in "set_notification_delivery_status_cb". Note: the setter for this...
Observation
Enum to define observation level.
Definition: m2mbase.h:83
Definition: m2mobjectinstance.h:36
virtual sn_coap_hdr_s * handle_get_request(nsdl_s *nsdl, sn_coap_hdr_s *received_coap_header, M2MObservationHandler *observation_handler=NULL)
Handles GET request for the registered objects.
BaseType
Enum to define the type of object.
Definition: m2mbase.h:70
M2MBase::lwm2m_parameters_s * get_lwm2m_parameters() const
Returns the resource structure.
uint16_t observation_number() const
Returns the observation number.
bool register_uri()
Returns whether this resource is published to server or not.
Definition: m2mstringbuffer.h:30
bool is_under_observation() const
Returns whether this resource is under observation or not.
StringBuffer. This class performs common string concatenation operations on a fixed-sized buffers...
virtual ~M2MBase()
Destructor.
virtual sn_coap_hdr_s * handle_put_request(nsdl_s *nsdl, sn_coap_hdr_s *received_coap_header, M2MObservationHandler *observation_handler, bool &execute_value_updated)
Handles PUT request for the registered objects.
LwM2M parameters.
Definition: m2mbase.h:210
M2MReportObserver. An interface for inviting the base class to send a report to the server...
virtual void set_observation_handler(M2MObservationHandler *handler)=0
Sets the observation handler.
Definition: m2mstring.h:33
virtual bool observation_to_be_sent(const m2m::Vector< uint16_t > &changed_instance_ids, uint16_t obs_number, bool send_object=false)
An observation callback to be sent to the server due to a change in the observed parameter.
CoAP response code values.
static void memory_free(void *ptr)
Memory free functions required for libCoap.
M2MReportHandler * create_report_handler()
Create Report Handler object.
static bool is_blockwise_needed(const nsdl_s *nsdl, uint32_t payload_len)
Checks whether blockwise is needed to send resource value to server.
bool is_auto_observable() const
Returns the auto observation status of the object.
void set_notification_msgid(uint16_t msgid) m2m_deprecated
Sets the notification message id. This is used to map RESET and EMPTY ACK messages.
DataType
Enum defining a resource data type.
Definition: m2mbase.h:107
bool set_value_updated_function(value_updated_callback callback)
Sets the function that is executed when this object receives a PUT or POST command.
A class for storing and calling a pointer to a static or member void function.
Definition: m2mreportobserver.h:28
const char * uri_path() const
Returns the path of the object.
Mode mode() const
Returns the mode of the resource.
bool set_notification_delivery_status_cb(notification_delivery_status_cb callback, void *client_args) m2m_deprecated
Sets the function that is executed when notification message state changes.
void set_interface_description(const String &description)
Sets the interface description of the object.
NotificationDeliveryStatus get_notification_delivery_status() const m2m_deprecated
Returns notification send status.
virtual sn_coap_hdr_s * handle_post_request(nsdl_s *nsdl, sn_coap_hdr_s *received_coap_header, M2MObservationHandler *observation_handler, bool &execute_value_updated, sn_nsdl_addr_s *address=NULL)
Handles GET request for the registered objects.
File defining all system build time configuration used by mbed-client.
Definition: m2mvector.h:30
uint16_t coap_content_type() const
Returns the CoAP content type of the object.
bool read_write_callback_set
If set, all the read and write operations are handled in callbacks and the resource value is not stor...
Definition: m2mbase.h:231
void set_instance_id(const uint16_t instance_id)
Sets the instance ID of the object.
virtual M2MBase * get_parent() const
Returns the owner object. Can return NULL if the object has no parent.
void set_auto_observable(bool auto_observable)
Sets the object to be auto-observable.
uint16_t get_notification_msgid() const m2m_deprecated
Returns the notification message id.
M2MReportHandler * report_handler() const
Returns the Report Handler object.
The base class for Client Resources.
Definition: m2mresource.h:40
struct M2MBase::lwm2m_parameters lwm2m_parameters_s
LwM2M parameters.
void set_operation(M2MBase::Operation operation)
Sets the operation type for an object.
void send_message_delivery_status(const M2MBase &object, const MessageDeliveryStatus status, const MessageType type)
Executes the function that is set in "set_message_delivery_status_cb".
void set_observation_token(const uint8_t *token, const uint8_t length)
Sets the observation token value.
virtual void remove_observation_level(M2MBase::Observation observation_level)
Removes the observation level for the object.
virtual void add_observation_level(M2MBase::Observation observation_level)
Adds the observation level for the object.
const char * resource_type() const
Returns the resource type of the object.
M2MBase::Observation observation_level() const
Returns the observation level of the object.
Definition: m2minterfacefactory.h:36
bool is_observable() const
Returns the observation status of the object.
bool free_on_delete
true if struct is dynamically allocated and it and its members (name) are to be freed on destructor...
Definition: m2mbase.h:224
static void * memory_alloc(uint32_t size)
Memory allocation required for libCoap.
Operation
Enum defining an operation that can be supported by a given resource.
Definition: m2mbase.h:121
void execute_value_updated(const String &name)
Calls the function that is set in the "set_value_updated_function".
void set_coap_content_type(const uint16_t content_type)
Sets the CoAP content type of the object.
virtual M2MObservationHandler * observation_handler() const =0
Returns the Observation Handler object.
void handle_observation(nsdl_s *nsdl, const sn_coap_hdr_s &received_coap_header, sn_coap_hdr_s &coap_response, M2MObservationHandler *observation_handler, sn_coap_msg_code_e &response_code)
Handles subscription request.
MessageDeliveryStatus
Enum defining an status codes that can happen when sending confirmable message.
Definition: m2mbase.h:144
virtual void set_resource_type(const String &resource_type)
Sets the resource type of the object.
void clear_notification_delivery_status() m2m_deprecated
Clears the notification send status to initial state.
Definition: functionpointer.h:114
Parameter identifier.
void set_max_age(const uint32_t max_age)
Sets the max age for the resource value to be cached.
void start_observation(const sn_coap_hdr_s &received_coap_header, M2MObservationHandler *observation_handler)
Start the observation.
static char * alloc_string_copy(const char *source)
Allocate and make a copy of given zero terminated string. This is functionally equivalent with strdup...