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 
28 class M2MBlockMessage;
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 
43 class M2MResourceBase : public M2MBase {
44 
45 friend class M2MObjectInstance;
46 friend class M2MResource;
47 friend class M2MResourceInstance;
48 
49 public:
50 
51  typedef enum {
52  INIT = 0, // Initial state.
53  SENT, // Notification created/sent but not received ACK yet.
54  DELIVERED // Received ACK from server.
55  } NotificationStatus;
56 
58 
59  typedef void(*notification_status_callback_2) (const uint16_t msg_id,
60  const M2MResourceBase::NotificationStatus status);
61 
66  typedef enum {
67  STRING,
68  INTEGER,
69  FLOAT,
70  BOOLEAN,
71  OPAQUE,
72  TIME,
73  OBJLINK
74  } ResourceType;
75 
76  /*
77  * \brief Value set callback function.
78  * \param resource Pointer to resource whose value should be updated
79  * \param value Pointer to value buffer containing new value, ownership is transferred to callback function
80  * \param value_length Length of value buffer
81  */
82  typedef void(*value_set_callback) (const M2MResourceBase *resource, uint8_t *value, const uint32_t value_length);
83 
84  /*
85  * \brief Read resource value callback function.
86  * \param resource Pointer to resource whose value should will be read
87  * \param buffer[OUT] Buffer containing the resource value
88  * \param buffer_size[IN/OUT] Buffer length
89  * \param client_args Client arguments
90  * \return Error code, 0 on success otherwise < 0
91  */
92  typedef int(*read_resource_value_callback) (const M2MResourceBase& resource,
93  void *buffer,
94  size_t *buffer_size,
95  void *client_args);
96 
97  /*
98  * \brief Read resource value size callback function.
99  * \param resource Pointer to resource whose size will be read
100  * \param buffer_size[OUT] Buffer size
101  * \param client_args Client arguments
102  * \return Error code, 0 on success otherwise < 0
103  */
104  typedef int(*read_resource_value_size_callback) (const M2MResourceBase& resource,
105  size_t *buffer_size,
106  void *client_args);
107 
108  /*
109  * \brief Set resource value callback function.
110  * \param resource Pointer to resource whose value will be updated
111  * \param buffer Buffer containing the new value
112  * \param buffer_size Size of the data
113  * \param client_args Client arguments
114  * \return error code, True if value storing completed otherwise False
115  */
116  typedef bool(*write_resource_value_callback) (const M2MResourceBase& resource,
117  const uint8_t *buffer,
118  const size_t buffer_size,
119  void *client_args);
120 
121 protected: // Constructor and destructor are private
122  // which means that these objects can be created or
123  // deleted only through a function provided by the M2MObjectInstance.
124 
126  const lwm2m_parameters_s* s,
127  M2MBase::DataType type);
139  const String &resource_name,
141  const String &resource_type,
142  M2MBase::DataType type,
143  char* path,
144  bool external_blockwise_store,
145  bool multiple_instance);
146 
161  const String &resource_name,
162  M2MBase::Mode mode,
163  const String &resource_type,
164  M2MBase::DataType type,
165  const uint8_t *value,
166  const uint8_t value_length,
167  char* path,
168  bool external_blockwise_store,
169  bool multiple_instance);
170 
171  // Prevents the use of default constructor.
172  M2MResourceBase();
173 
174  // Prevents the use of assignment operator.
175  M2MResourceBase& operator=( const M2MResourceBase& /*other*/ );
176 
177  // Prevents the use of copy constructor
178  M2MResourceBase( const M2MResourceBase& /*other*/ );
179 
183  virtual ~M2MResourceBase();
184 
185 public:
186 
192 
199  bool set_execute_function(execute_callback callback);
200 
207  bool set_execute_function(execute_callback_2 callback);
208 
215  bool set_resource_read_callback(read_resource_value_callback callback, void *client_args);
216 
223  bool set_resource_read_size_callback(read_resource_value_size_callback callback, void *client_args);
224 
231  bool set_resource_write_callback(write_resource_value_callback callback, void *client_args);
232 
241  int read_resource_value(const M2MResourceBase& resource, void *buffer, size_t *buffer_len);
242 
250  int read_resource_value_size(const M2MResourceBase& resource, size_t *buffer_len);
251 
259  bool write_resource_value(const M2MResourceBase& resource, const uint8_t *buffer, const size_t buffer_size);
260 
271  bool set_value(const uint8_t *value, const uint32_t value_length);
272 
283  bool set_value_raw(uint8_t *value, const uint32_t value_length);
284 
295  bool set_value(int64_t value);
296 
307  bool set_value_float(float value);
308 
312  void clear_value();
313 
318  void execute(void *arguments);
319 
326  void get_value(uint8_t *&value, uint32_t &value_length);
327 
332  int64_t get_value_int() const;
333 
338  String get_value_string() const;
339 
344  float get_value_float() const;
345 
350  uint8_t* value() const;
351 
356  uint32_t value_length() const;
357 
364  void set_value_set_callback(value_set_callback callback);
365 
372  void update_value(uint8_t *value, const uint32_t value_length);
373 
382  virtual sn_coap_hdr_s* handle_get_request(nsdl_s *nsdl,
383  sn_coap_hdr_s *received_coap_header,
394  virtual sn_coap_hdr_s* handle_put_request(nsdl_s *nsdl,
395  sn_coap_hdr_s *received_coap_header,
397  bool &execute_value_updated);
398 
403  virtual uint16_t object_instance_id() const = 0;
404 
409  virtual const char* object_name() const = 0;
410 
411  virtual M2MResource& get_parent_resource() const = 0;
412 
413 #ifndef DISABLE_BLOCK_MESSAGE
414 
420 
430 
436 
437 #endif
438 
444  bool set_notification_sent_callback(notification_sent_callback callback) m2m_deprecated;
445 
451  bool set_notification_sent_callback(notification_sent_callback_2 callback) m2m_deprecated;
452 
458  bool set_notification_status_callback(notification_status_callback callback) m2m_deprecated;
459 
465  bool set_notification_status_callback(notification_status_callback_2 callback);
466 
470  void notification_sent();
471 
475  void notification_status(const uint16_t msg_id, NotificationStatus status);
476 
481  M2MResourceBase::NotificationStatus notification_status() const;
482 
487 
499  void publish_value_in_registration_msg(bool publish_value);
500 
501 private:
502 
503  void report();
504 
505  void report_value_change();
506 
507  bool has_value_changed(const uint8_t* value, const uint32_t value_len);
508 
509  M2MResourceBase::ResourceType convert_data_type(M2MBase::DataType type) const;
510 
511 private:
512 
513 #ifndef DISABLE_BLOCK_MESSAGE
514  M2MBlockMessage *_block_message_data;
515 #endif
516 
517  NotificationStatus _notification_status : 2;
518 
519  friend class Test_M2MResourceInstance;
520  friend class Test_M2MResource;
521  friend class Test_M2MObjectInstance;
522  friend class Test_M2MObject;
523  friend class Test_M2MDevice;
524  friend class Test_M2MSecurity;
525  friend class Test_M2MServer;
526  friend class Test_M2MNsdlInterface;
527  friend class Test_M2MFirmware;
528  friend class Test_M2MTLVSerializer;
529  friend class Test_M2MTLVDeserializer;
530 };
531 
532 #endif // M2M_RESOURCE_BASE_H
Mode
Enum defining a resource type.
Definition: m2mbase.h:98
Definition: m2mobservationhandler.h:31
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:284
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...
The base class based on which all LwM2M object models can be created.
Definition: m2mbase.h:63
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:36
ResourceType
Definition: m2mresourcebase.h:66
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:210
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...
bool set_resource_read_callback(read_resource_value_callback callback, void *client_args)
Sets the callback function that is executed when reading the resource value.
virtual ~M2MResourceBase()
Definition: m2mstring.h:33
M2MBase. This class is the base class based on which all LwM2M object models can be created...
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:107
Definition: m2mblockmessage.h:26
A class for storing and calling a pointer to a static or member void function.
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.
bool set_outgoing_block_message_callback(outgoing_block_message_callback callback)
Sets the function that is executed when this object receives a GET request. This is called if resourc...
Definition: m2mresourceinstance.h:32
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:43
The base class for Client Resources.
Definition: m2mresource.h:40
Definition: functionpointer.h:199
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.
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:114
uint8_t * value() const
Returns the value pointer of the object.
void clear_value()
Clears the value of a given resource.