Mistake on this page? Email us
m2mresourcebase.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2021 Pelion. 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 // (space needed for -3.402823 × 10^38) + (magic decimal 6 digits added as no precision is added to "%f") + trailing zero
23 #define REGISTRY_FLOAT_STRING_MAX_LEN 48
24 
27 // Forward declarations
28 
29 class M2MBlockMessage;
30 class M2MCallbackAssociation;
31 
33 typedef void(*execute_callback_2)(void *arguments);
34 
36 typedef void(*notification_sent_callback_2)(void);
37 
38 #ifndef DISABLE_BLOCK_MESSAGE
41 #endif
42 
43 class M2MResource;
44 
49 class M2MResourceBase : public M2MBase {
50 
51  friend class M2MObjectInstance;
52  friend class M2MResource;
53  friend class M2MResourceInstance;
54 
55 public:
56 
61  typedef enum {
62  STRING,
63  INTEGER,
64  FLOAT,
65  BOOLEAN,
66  OPAQUE,
67  TIME,
68  OBJLINK
69  } ResourceType;
70 
77  typedef void(*value_set_callback)(const M2MResourceBase *resource, uint8_t *value, const uint32_t value_length);
78 
87  typedef int(*read_resource_value_callback)(const M2MResourceBase &resource,
88  void *buffer,
89  size_t *buffer_size,
90  void *client_args);
91 
103  uint8_t *&buffer,
104  size_t &buffer_size,
105  size_t &total_size,
106  const size_t offset,
107  void *client_args);
108 
116  typedef int(*read_resource_value_size_callback)(const M2MResourceBase &resource,
117  size_t *buffer_size,
118  void *client_args);
119 
128  typedef bool(*write_resource_value_callback)(const M2MResourceBase &resource,
129  const uint8_t *buffer,
130  const size_t buffer_size,
131  void *client_args);
132 
133 protected: // Constructor and destructor are private
134  // which means that these objects can be created or
135  // deleted only through a function provided by the M2MObjectInstance.
136 
138  const lwm2m_parameters_s *s,
139  M2MBase::DataType type);
151  const String &resource_name,
153  const String &resource_type,
154  M2MBase::DataType type,
155  char *path,
156  bool external_blockwise_store,
157  bool multiple_instance);
158 
173  const String &resource_name,
175  const String &resource_type,
176  M2MBase::DataType type,
177  const uint8_t *value,
178  const uint8_t value_length,
179  char *path,
180  bool external_blockwise_store,
181  bool multiple_instance);
182 
183  // Prevents the use of default constructor.
184  M2MResourceBase();
185 
186  // Prevents the use of assignment operator.
187  M2MResourceBase &operator=(const M2MResourceBase & /*other*/);
188 
189  // Prevents the use of copy constructor
190  M2MResourceBase(const M2MResourceBase & /*other*/);
191 
195  virtual ~M2MResourceBase();
196 
197 public:
198 
204 
213  bool set_execute_function(execute_callback callback);
214 
221  bool set_execute_function(execute_callback_2 callback);
222 
229  bool set_resource_read_callback(read_resource_value_callback callback, void *client_args) m2m_deprecated;
230 
236  bool set_read_resource_function(read_value_callback callback, void *client_args);
237 
244  bool set_resource_read_size_callback(read_resource_value_size_callback callback, void *client_args);
245 
252  bool set_resource_write_callback(write_resource_value_callback callback, void *client_args);
253 
262  int read_resource_value(const M2MResourceBase &resource, void *buffer, size_t *buffer_len);
263 
271  int read_resource_value_size(const M2MResourceBase &resource, size_t *buffer_len);
272 
280  bool write_resource_value(const M2MResourceBase &resource, const uint8_t *buffer, const size_t buffer_size);
281 
292  bool set_value(const uint8_t *value, const uint32_t value_length);
293 
304  bool set_value_raw(uint8_t *value, const uint32_t value_length);
305 
316  bool set_value(int64_t value);
317 
328  bool set_value_float(float value);
329 
333  void clear_value();
334 
339  void execute(void *arguments);
340 
347  void get_value(uint8_t *&value, uint32_t &value_length);
348 
353  int64_t get_value_int() const;
354 
359  String get_value_string() const;
360 
365  float get_value_float() const;
366 
371  uint8_t *value() const;
372 
377  uint32_t value_length() const;
378 
386 
393  void update_value(uint8_t *value, const uint32_t value_length);
394 
401  void report_to_parents();
402 
411  virtual sn_coap_hdr_s *handle_get_request(nsdl_s *nsdl,
412  sn_coap_hdr_s *received_coap_header,
423  virtual sn_coap_hdr_s *handle_put_request(nsdl_s *nsdl,
424  sn_coap_hdr_s *received_coap_header,
426  bool &execute_value_updated);
427 
432  virtual uint16_t object_instance_id() const = 0;
433 
438  virtual const char *object_name() const = 0;
439 
443  virtual M2MResource &get_parent_resource() const = 0;
444 
445 #ifndef DISABLE_BLOCK_MESSAGE
446 
452 
462 
468 
469 #endif
470 
484  void publish_value_in_registration_msg(bool publish_value);
485 
486 private:
487 
488  void report();
489 
490  void report_value_change();
491 
492  bool has_value_changed(const uint8_t *value, const uint32_t value_len);
493 
494  M2MResourceBase::ResourceType convert_data_type(M2MBase::DataType type) const;
495 
496  void read_data_from_application(M2MCallbackAssociation *item, nsdl_s *nsdl, const sn_coap_hdr_s *received_coap,
497  sn_coap_hdr_s *coap_response, size_t &payload_len);
498 
499 private:
500 
501 #ifndef DISABLE_BLOCK_MESSAGE
502  M2MBlockMessage *_block_message_data;
503 #endif
504 
505  friend class Test_M2MResourceInstance;
506  friend class Test_M2MResource;
507  friend class Test_M2MObjectInstance;
508  friend class Test_M2MObject;
509  friend class Test_M2MDevice;
510  friend class Test_M2MSecurity;
511  friend class Test_M2MServer;
512  friend class Test_M2MNsdlInterface;
513  friend class Test_M2MTLVSerializer;
514  friend class Test_M2MTLVDeserializer;
515  friend class Test_M2MDynLog;
516 };
517 
518 #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.
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 fol...
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:61
int(* read_resource_value_size_callback)(const M2MResourceBase &resource, size_t *buffer_size, void *client_args)
Read resource value size callback function.
Definition: m2mresourcebase.h:116
int(* read_resource_value_callback)(const M2MResourceBase &resource, void *buffer, size_t *buffer_size, void *client_args)
Read resource value callback function.
Definition: m2mresourcebase.h:87
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
virtual ~M2MResourceBase()
Header for M2MBase class.
bool(* write_resource_value_callback)(const M2MResourceBase &resource, const uint8_t *buffer, const size_t buffer_size, void *client_args)
Set resource value callback function.
Definition: m2mresourcebase.h:128
bool set_value_float(float value)
Sets a value of a given resource.
virtual M2MResource & get_parent_resource() const =0
coap_response_code_e(* read_value_callback)(const M2MResourceBase &resource, uint8_t *&buffer, size_t &buffer_size, size_t &total_size, const size_t offset, void *client_args)
Type definition for a read resource value callback function.
Definition: m2mresourcebase.h:102
int read_resource_value_size(const M2MResourceBase &resource, size_t *buffer_len)
Executes the function that is set in "set_resource_read_size_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.
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:49
coap_response_code_e
Definition: coap_response.h:26
void(* value_set_callback)(const M2MResourceBase *resource, uint8_t *value, const uint32_t value_length)
Value set callback function.
Definition: m2mresourcebase.h:77
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.
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.