Mistake on this page? Email us
m2mresource.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_H
17 #define M2M_RESOURCE_H
18 
19 #include "mbed-client/m2mvector.h"
23 #include <stdlib.h>
24 
27 //FORWARD DECLARATION
28 class M2MObjectInstance;
29 typedef Vector<M2MResourceInstance *> M2MResourceInstanceList;
30 
31 
38 class M2MResource : public M2MResourceBase {
39 
40  friend class M2MObjectInstance;
41 
42 public:
43  class M2MExecuteParameter;
44 
45 private: // Constructor and destructor are private,
46  // which means that these objects can be created or
47  // deleted only through a function provided by the M2MObjectInstance.
48 
50  const lwm2m_parameters_s *s,
51  M2MBase::DataType type);
66  const String &resource_name,
68  const String &resource_type,
69  M2MBase::DataType type,
70  const uint8_t *value,
71  const uint8_t value_length,
72  char *path,
73  bool multiple_instance = false,
74  bool external_blockwise_store = false);
75 
89  const String &resource_name,
90  M2MBase::Mode mode,
91  const String &resource_type,
92  M2MBase::DataType type,
93  bool observable,
94  char *path,
95  bool multiple_instance = false,
96  bool external_blockwise_store = false);
97 
98  // Prevents the use of a default constructor.
99  M2MResource();
100 
101  // Prevents the use of an assignment operator.
102  M2MResource &operator=(const M2MResource & /*other*/);
103 
104  // Prevents the use of a copy constructor
105  M2MResource(const M2MResource & /*other*/);
106 
110  virtual M2MBase *get_parent() const;
111 
115  virtual ~M2MResource();
116 
117 public:
118 
124 
131  bool supports_multiple_instances() const;
132 
133 #ifndef DISABLE_DELAYED_RESPONSE
134 
141 #ifdef ENABLE_ASYNC_REST_RESPONSE
142  void set_delayed_response(bool delayed_response) m2m_deprecated;
143 #else
144  void set_delayed_response(bool delayed_response);
145 #endif
146 
151  bool delayed_response() const;
152 
163  bool send_delayed_post_response(sn_coap_msg_code_e code = COAP_MSG_CODE_RESPONSE_CHANGED);
164 
172  void get_delayed_token(uint8_t *&token, uint8_t &token_length);
173 
174 #endif //DISABLE_DELAYED_RESPONSE
175 
181  bool remove_resource_instance(uint16_t instance_id = 0);
182 
189 
194  const M2MResourceInstanceList &resource_instances() const;
195 
200  uint16_t resource_instance_count() const;
201 
209 
216  virtual void set_observation_handler(M2MObservationHandler *handler);
217 
218 #if defined (MBED_CONF_MBED_CLIENT_ENABLE_OBSERVATION_PARAMETERS) && (MBED_CONF_MBED_CLIENT_ENABLE_OBSERVATION_PARAMETERS == 1)
219 
226  virtual bool handle_observation_attribute(const char *query);
227 #endif
228 
236 
243  virtual void remove_observation_level(M2MBase::Observation observation_level);
244 
255  virtual sn_coap_hdr_s *handle_get_request(nsdl_s *nsdl,
256  sn_coap_hdr_s *received_coap_header,
257  M2MObservationHandler *observation_handler = NULL);
269  virtual sn_coap_hdr_s *handle_put_request(nsdl_s *nsdl,
270  sn_coap_hdr_s *received_coap_header,
271  M2MObservationHandler *observation_handler,
272  bool &execute_value_updated);
284  virtual sn_coap_hdr_s *handle_post_request(nsdl_s *nsdl,
285  sn_coap_hdr_s *received_coap_header,
286  M2MObservationHandler *observation_handler,
287  bool &execute_value_updated,
288  sn_nsdl_addr_s *address = NULL);
289 
294 
299  virtual uint16_t object_instance_id() const;
300 
305  virtual const char *object_name() const;
306 
307  virtual M2MResource &get_parent_resource() const;
308 
309 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
310 
313  void set_manifest_check_status(bool status);
314 
319  bool get_manifest_check_status();
320 #endif
321 
322 private:
323  M2MObjectInstance &_parent;
324 
325  M2MResourceInstanceList _resource_instance_list; // owned
326 
327 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
328  bool _status;
329 #endif
330 
331 #ifndef DISABLE_DELAYED_RESPONSE
332  uint8_t *_delayed_token;
333  uint8_t _delayed_token_len;
334  bool _delayed_response;
335 #endif
336 
337  friend class Test_M2MResource;
338  friend class Test_M2MObjectInstance;
339  friend class Test_M2MObject;
340  friend class Test_M2MDevice;
341  friend class Test_M2MSecurity;
342  friend class Test_M2MServer;
343  friend class Test_M2MReportHandler;
344  friend class Test_M2MNsdlInterface;
345  friend class Test_M2MInterfaceFactory;
346  friend class Test_M2MTLVSerializer;
347  friend class Test_M2MTLVDeserializer;
348  friend class Test_M2MBase;
349  friend class Test_M2MResourceInstance;
350  friend class TestFactory;
351  friend class Test_M2MInterfaceImpl;
352  friend class Test_M2MDiscover;
353  friend class Test_M2MDynLog;
354 };
355 
361 
362 private:
363 
368 
369 #ifdef MEMORY_OPTIMIZED_API
370  M2MExecuteParameter(const char *object_name, const char *resource_name, uint16_t object_instance_id);
371 #else
372 
375  M2MExecuteParameter(const String &object_name, const String &resource_name, uint16_t object_instance_id);
376 #endif
377 public:
378 
383  const uint8_t *get_argument_value() const;
384 
389  uint16_t get_argument_value_length() const;
390 
395 #ifdef MEMORY_OPTIMIZED_API
396  const char *get_argument_object_name() const;
397 #else
398  const String &get_argument_object_name() const;
399 #endif
400 
405 #ifdef MEMORY_OPTIMIZED_API
406  const char *get_argument_resource_name() const;
407 #else
408  const String &get_argument_resource_name() const;
409 #endif
410 
415  uint16_t get_argument_object_instance_id() const;
416 
417 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
418 
421  void set_resource(M2MResource *res);
422 
427  M2MResource *get_resource();
428 
429 #endif
430 
431 private:
432  // pointers to const data, not owned by this instance
433 
434 #ifdef MEMORY_OPTIMIZED_API
435  const char *_object_name;
436  const char *_resource_name;
437 #else
438  const String &_object_name;
439  const String &_resource_name;
440 #endif
441  const uint8_t *_value;
442  uint16_t _value_length;
443  uint16_t _object_instance_id;
444 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
445  M2MResource *_resource;
446 #endif
447 
448  friend class Test_M2MResource;
449  friend class M2MResource;
450 };
451 
452 #endif // M2M_RESOURCE_H
Mode
Enum defining a resource type.
Definition: m2mbase.h:89
Definition: m2mobservationhandler.h:30
void add_resource_instance(M2MResourceInstance *resource_instance)
Adds resource instances to a M2MResource.
virtual void remove_observation_level(M2MBase::Observation observation_level)
Removes the observation level from an object.
Definition: m2mbase.h:54
header for M2MResourceBase.
virtual const char * object_name() const
Returns the name of the object where the resource exists.
Observation
Enum to define observation level.
Definition: m2mbase.h:74
header for M2MResourceInstance.
bool remove_resource_instance(uint16_t instance_id=0)
Removes a resource with a given name.
Definition: m2mobjectinstance.h:42
M2MObjectInstance & get_parent_object_instance() const
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 registered objects.
uint16_t resource_instance_count() const
Returns the total number of resources.
LwM2M parameters.
Definition: m2mbase.h:199
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 registered objects.
const uint8_t * get_argument_value() const
Returns the value of an argument.
CoAP response code values.
void set_delayed_response(bool delayed_response)
Sets whether the resource should send a delayed response for a POST request. This only works for reso...
virtual uint16_t object_instance_id() const
Returns the instance ID of the object where the resource exists.
M2MResourceInstance * resource_instance(uint16_t instance_id=0) const
Returns a resource instance with a given name.
virtual void add_observation_level(M2MBase::Observation observation_level)
Adds the observation level for the object.
DataType
Enum defining a resource data type.
Definition: m2mbase.h:98
const String & get_argument_object_name() const
Returns the name of the object where the resource exists.
Mode mode() const
Returns the mode of the resource.
Definition: m2mresourceinstance.h:38
virtual void set_observation_handler(M2MObservationHandler *handler)
Sets the observation handler.
header for m2m::Vector
bool delayed_response() const
Check if resource is set to send delayed responses for POST request. Use set_delayed_response() for e...
Definition: m2mresourcebase.h:49
virtual M2MResource & get_parent_resource() const
const String & get_argument_resource_name() const
Returns the resource name.
const M2MResourceInstanceList & resource_instances() const
Returns a list of resources.
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 the POST request for registered objects.
Definition: m2mresource.h:38
virtual M2MObservationHandler * observation_handler() const
Returns the Observation Handler object.
uint16_t get_argument_value_length() const
Returns the length of the value argument.
M2MResource::M2MExecuteParameter. This class handles the "Execute" operation arguments.
Definition: m2mresource.h:360
const char * resource_type() const
Returns the resource type of the object.
M2MBase::Observation observation_level() const
Returns the observation level of the object.
bool supports_multiple_instances() const
Returns whether the resource has multiple resource instances or not.
uint16_t instance_id() const
Returns the object&#39;s instance ID.
uint16_t get_argument_object_instance_id() const
Returns the instance ID of the object where the resource exists.
void execute_value_updated(const String &name)
Calls the function that is set in the "set_value_updated_function".
bool send_delayed_post_response(sn_coap_msg_code_e code=COAP_MSG_CODE_RESPONSE_CHANGED)
A trigger to send the delayed response for the POST request. The delayed_response flag must be set be...
uint8_t * value() const
Returns the value pointer of the object.