Mistake on this page? Email us
m2mresourcebase.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_RESOURCE_BASE_H
17 #define M2M_RESOURCE_BASE_H
18 
19 #include "mbed-client/m2mbase.h"
21 
22 
25 // Forward declarations
26 
27 class M2MBlockMessage;
28 class M2MCallbackAssociation;
29 
31 typedef void(*execute_callback_2) (void *arguments);
32 
34 typedef void(*notification_sent_callback_2) (void);
35 
36 #ifndef DISABLE_BLOCK_MESSAGE
39 #endif
40 
41 class M2MResource;
42 
47 class M2MResourceBase : public M2MBase {
48 
49 friend class M2MObjectInstance;
50 friend class M2MResource;
51 friend class M2MResourceInstance;
52 
53 public:
54 
55  typedef enum {
56  INIT = 0, // Initial state.
57  SENT, // Notification created/sent but not received ACK yet.
58  DELIVERED // Received ACK from server.
59  } NotificationStatus;
60 
62 
63  typedef void(*notification_status_callback_2) (const uint16_t msg_id,
64  const M2MResourceBase::NotificationStatus status);
65 
70  typedef enum {
71  STRING,
72  INTEGER,
73  FLOAT,
74  BOOLEAN,
75  OPAQUE,
76  TIME,
77  OBJLINK
78  } ResourceType;
79 
80  /*
81  * \brief Value set callback function.
82  * \param resource Pointer to resource whose value should be updated
83  * \param value Pointer to value buffer containing new value, ownership is transferred to callback function
84  * \param value_length Length of value buffer
85  */
86  typedef void(*value_set_callback) (const M2MResourceBase *resource, uint8_t *value, const uint32_t value_length);
87 
88  /*
89  * \brief Read resource value callback function.
90  * \param resource Pointer to resource whose value should will be read
91  * \param buffer[OUT] Buffer containing the resource value
92  * \param buffer_size[IN/OUT] Buffer length
93  * \param client_args Client arguments
94  * \return Error code, 0 on success otherwise < 0
95  */
96  typedef int(*read_resource_value_callback) (const M2MResourceBase& resource,
97  void *buffer,
98  size_t *buffer_size,
99  void *client_args);
100 
101  /*
102  * \brief Type definition for a read resource value callback function.
103  * \param[in] resource Pointer to resource whose value should will be read.
104  * \param[out] buffer Pointer to value buffer.
105  * \param[in, out] buffer_size On input, tells the maximum size of bytes to read. On output, tells how many bytes have been written to buffer.
106  * \param[out] total_size Total size of the resource data.
107  * \param[in] offset Offset to read from in data.
108  * \param[in] client_args Client arguments.
109  * \return CoAP response code for the response.
110  */
111  typedef coap_response_code_e(*read_value_callback) (const M2MResourceBase& resource,
112  uint8_t *&buffer,
113  size_t &buffer_size,
114  size_t &total_size,
115  const size_t offset,
116  void *client_args);
117 
118 
119 
120  /*
121  * \brief Read resource value size callback function.
122  * \param resource Pointer to resource whose size will be read
123  * \param buffer_size[OUT] Buffer size
124  * \param client_args Client arguments
125  * \return Error code, 0 on success otherwise < 0
126  */
127  typedef int(*read_resource_value_size_callback) (const M2MResourceBase& resource,
128  size_t *buffer_size,
129  void *client_args);
130 
131  /*
132  * \brief Set resource value callback function.
133  * \param resource Pointer to resource whose value will be updated
134  * \param buffer Buffer containing the new value
135  * \param buffer_size Size of the data
136  * \param client_args Client arguments
137  * \return error code, True if value storing completed otherwise False
138  */
139  typedef bool(*write_resource_value_callback) (const M2MResourceBase& resource,
140  const uint8_t *buffer,
141  const size_t buffer_size,
142  void *client_args);
143 
144 protected: // Constructor and destructor are private
145  // which means that these objects can be created or
146  // deleted only through a function provided by the M2MObjectInstance.
147 
149  const lwm2m_parameters_s* s,
150  M2MBase::DataType type);
162  const String &resource_name,
164  const String &resource_type,
165  M2MBase::DataType type,
166  char* path,
167  bool external_blockwise_store,
168  bool multiple_instance);
169 
184  const String &resource_name,
185  M2MBase::Mode mode,
186  const String &resource_type,
187  M2MBase::DataType type,
188  const uint8_t *value,
189  const uint8_t value_length,
190  char* path,
191  bool external_blockwise_store,
192  bool multiple_instance);
193 
194  // Prevents the use of default constructor.
195  M2MResourceBase();
196 
197  // Prevents the use of assignment operator.
198  M2MResourceBase& operator=( const M2MResourceBase& /*other*/ );
199 
200  // Prevents the use of copy constructor
201  M2MResourceBase( const M2MResourceBase& /*other*/ );
202 
206  virtual ~M2MResourceBase();
207 
208 public:
209 
215 
222  bool set_execute_function(execute_callback callback);
223 
230  bool set_execute_function(execute_callback_2 callback);
231 
238  bool set_resource_read_callback(read_resource_value_callback callback, void *client_args) m2m_deprecated;
239 
245  bool set_read_resource_function(read_value_callback callback, void *client_args);
246 
253  bool set_resource_read_size_callback(read_resource_value_size_callback callback, void *client_args);
254 
261  bool set_resource_write_callback(write_resource_value_callback callback, void *client_args);
262 
271  int read_resource_value(const M2MResourceBase& resource, void *buffer, size_t *buffer_len);
272 
280  int read_resource_value_size(const M2MResourceBase& resource, size_t *buffer_len);
281 
289  bool write_resource_value(const M2MResourceBase& resource, const uint8_t *buffer, const size_t buffer_size);
290 
301  bool set_value(const uint8_t *value, const uint32_t value_length);
302 
313  bool set_value_raw(uint8_t *value, const uint32_t value_length);
314 
325  bool set_value(int64_t value);
326 
337  bool set_value_float(float value);
338 
342  void clear_value();
343 
348  void execute(void *arguments);
349 
356  void get_value(uint8_t *&value, uint32_t &value_length);
357 
362  int64_t get_value_int() const;
363 
368  String get_value_string() const;
369 
374  float get_value_float() const;
375 
380  uint8_t* value() const;
381 
386  uint32_t value_length() const;
387 
394  void set_value_set_callback(value_set_callback callback);
395 
402  void update_value(uint8_t *value, const uint32_t value_length);
403 
408  void report_to_parents();
409 
418  virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
419  sn_coap_hdr_s *received_coap_header,
430  virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
431  sn_coap_hdr_s *received_coap_header,
433  bool &execute_value_updated);
434 
439  virtual uint16_t object_instance_id() const = 0;
440 
445  virtual const char* object_name() const = 0;
446 
447  virtual M2MResource& get_parent_resource() const = 0;
448 
449 #ifndef DISABLE_BLOCK_MESSAGE
450 
456 
466 
472 
473 #endif
474 
480  bool set_notification_sent_callback(notification_sent_callback callback) m2m_deprecated;
481 
487  bool set_notification_sent_callback(notification_sent_callback_2 callback) m2m_deprecated;
488 
494  bool set_notification_status_callback(notification_status_callback callback) m2m_deprecated;
495 
501  bool set_notification_status_callback(notification_status_callback_2 callback);
502 
506  void notification_sent();
507 
511  void notification_status(const uint16_t msg_id, NotificationStatus status);
512 
517  M2MResourceBase::NotificationStatus notification_status() const;
518 
523 
535  void publish_value_in_registration_msg(bool publish_value);
536 
537 private:
538 
539  void report();
540 
541  void report_value_change();
542 
543  bool has_value_changed(const uint8_t* value, const uint32_t value_len);
544 
545  M2MResourceBase::ResourceType convert_data_type(M2MBase::DataType type) const;
546 
547  void read_data_from_application(M2MCallbackAssociation *item, nsdl_s *nsdl, const sn_coap_hdr_s *received_coap,
548  sn_coap_hdr_s *coap_response, size_t &payload_len);
549 
550 private:
551 
552 #ifndef DISABLE_BLOCK_MESSAGE
553  M2MBlockMessage *_block_message_data;
554 #endif
555 
556  NotificationStatus _notification_status : 2;
557 
558  friend class Test_M2MResourceInstance;
559  friend class Test_M2MResource;
560  friend class Test_M2MObjectInstance;
561  friend class Test_M2MObject;
562  friend class Test_M2MDevice;
563  friend class Test_M2MSecurity;
564  friend class Test_M2MServer;
565  friend class Test_M2MNsdlInterface;
566  friend class Test_M2MTLVSerializer;
567  friend class Test_M2MTLVDeserializer;
568 };
569 
570 #endif // M2M_RESOURCE_BASE_H
Mode
Enum defining a resource type.
Definition: m2mbase.h:89
Definition: m2mobservationhandler.h:30
void get_value(uint8_t *&value, uint32_t &value_length)
Provides the value of the given resource.
bool set_execute_function(execute_callback callback)
Sets the function that should be executed when this resource receives a POST command.
int64_t get_value_int() const
Converts a value to integer and returns it. Note: Conversion errors are not detected.
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 the PUT request for the registered objects.
void clear_notification_status()
Clears the notification send status to initial state.
virtual uint16_t object_instance_id() const =0
Returns the instance ID of the object where the resource exists.
void publish_value_in_registration_msg(bool publish_value)
Set the status whether resource value will be part of registration message. * This only allowed for f...
bool set_resource_write_callback(write_resource_value_callback callback, void *client_args)
Sets the callback function that is executed when writing the resource value.
M2MBlockMessage * block_message() const
Returns the block message object.
Definition: functionpointer.h:281
void set_value_set_callback(value_set_callback callback)
Set the value set callback. The set callback will be called instead of setting the value in set_value...
Definition: m2mbase.h:54
bool set_resource_read_size_callback(read_resource_value_size_callback callback, void *client_args)
Sets the callback function that is executed when reading the resource value size. ...
String get_value_string() const
Definition: m2mobjectinstance.h:42
ResourceType
Definition: m2mresourcebase.h:70
uint32_t value_length() const
Returns the length of the value pointer.
virtual sn_coap_hdr_s * handle_get_request(nsdl_s *nsdl, sn_coap_hdr_s *received_coap_header, M2MObservationHandler *observation_handler=NULL)
Handles the GET request for the registered objects.
Definition: functionpointer.h:27
int read_resource_value(const M2MResourceBase &resource, void *buffer, size_t *buffer_len)
Executes the function that is set in "set_resource_read_callback".
LwM2M parameters.
Definition: m2mbase.h:199
bool set_notification_status_callback(notification_status_callback callback) m2m_deprecated
Sets the function that is executed when this object receives response(Empty ACK) for notification mes...
virtual ~M2MResourceBase()
Header for M2MBase class.
bool set_value_float(float value)
Sets a value of a given resource.
virtual M2MResource & get_parent_resource() const
Get reference to the resource owning this resource instance.
int read_resource_value_size(const M2MResourceBase &resource, size_t *buffer_len)
Executes the function that is set in "set_resource_read_size_callback".
void notification_sent()
Executes the function that is set in "set_notification_sent_callback".
DataType
Enum defining a resource data type.
Definition: m2mbase.h:98
Definition: m2mblockmessage.h:26
bool set_read_resource_function(read_value_callback callback, void *client_args)
Sets the function that is executed when this object receives a GET request.
Header for function pointer classes.
bool set_notification_sent_callback(notification_sent_callback callback) m2m_deprecated
Sets the function that is executed when this object receives response(Empty ACK) for notification mes...
Mode mode() const
Returns the mode of the resource.
Definition: m2mresourceinstance.h:38
bool set_incoming_block_message_callback(incoming_block_message_callback callback)
Sets the function that is executed when this object receives a block-wise message.
virtual const char * object_name() const =0
Returns the name of the object where the resource exists.
bool write_resource_value(const M2MResourceBase &resource, const uint8_t *buffer, const size_t buffer_size)
Executes the function that is set in "set_resource_write_callback".
void update_value(uint8_t *value, const uint32_t value_length)
Default value update function. This function frees old value, stores the new value and informs report...
Definition: m2mresourcebase.h:47
coap_response_code_e
Definition: coap_response.h:26
Definition: m2mresource.h:38
bool set_resource_read_callback(read_resource_value_callback callback, void *client_args) m2m_deprecated
Sets the callback function that is executed when reading the resource value.
Definition: functionpointer.h:197
M2MResourceBase::NotificationStatus notification_status() const
Returns notification send status.
bool set_value_raw(uint8_t *value, const uint32_t value_length)
Sets a value of a given resource.
M2MResourceBase::ResourceType resource_instance_type() const
Returns the resource data type.
const char * resource_type() const
Returns the resource type of the object.
void execute(void *arguments)
Executes the function that is set in "set_execute_function".
void execute_value_updated(const String &name)
Calls the function that is set in the "set_value_updated_function".
virtual M2MObservationHandler * observation_handler() const =0
Returns the Observation Handler object.
bool set_outgoing_block_message_callback(outgoing_block_message_callback callback) m2m_deprecated
Sets the function that is executed when this object receives a GET request. This is called if resourc...
float get_value_float() const
Converts a value to float and returns it. Note: Conversion errors are not detected.
bool set_value(const uint8_t *value, const uint32_t value_length)
Sets a value of a given resource.
Definition: functionpointer.h:113
uint8_t * value() const
Returns the value pointer of the object.
void report_to_parents()
Function to report the value changes to the object instance and object parent of the resource if they...
void clear_value()
Clears the value of a given resource.