1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef LWM2M_OBSERVATION_H
7 #define LWM2M_OBSERVATION_H
8 #include "lwm2m_object.h"
9 
10 int lwm2m_notify_observer(uint16_t obj_id, uint16_t obj_inst_id, uint16_t res_id);
11 int lwm2m_notify_observer_path(const struct lwm2m_obj_path *path);
12 
13 #define MAX_TOKEN_LEN 8
14 
15 struct observe_node {
16 	sys_snode_t node;
17 	sys_slist_t path_list;               /* List of Observation path */
18 	uint8_t token[MAX_TOKEN_LEN];        /* Observation Token */
19 	int64_t event_timestamp;             /* Timestamp for trig next Notify  */
20 	int64_t last_timestamp;	             /* Timestamp from last Notify */
21 	struct lwm2m_message *active_notify; /* Currently active notification */
22 	uint32_t counter;
23 	uint16_t format;
24 	uint8_t tkl;
25 	bool resource_update : 1;            /* Resource is updated */
26 	bool composite : 1;                  /* Composite Observation */
27 };
28 /* Attribute handling. */
29 
30 struct lwm2m_attr *lwm2m_engine_get_next_attr(const void *ref, struct lwm2m_attr *prev);
31 const char *lwm2m_engine_get_attr_name(const struct lwm2m_attr *attr);
32 
33 const char *const lwm2m_attr_to_str(uint8_t type);
34 
35 void clear_attrs(void *ref);
36 
37 int64_t engine_observe_shedule_next_event(struct observe_node *obs, uint16_t srv_obj_inst,
38 					  const int64_t timestamp);
39 
40 void remove_observer_from_list(struct lwm2m_ctx *ctx, sys_snode_t *prev_node,
41 			       struct observe_node *obs);
42 
43 struct observe_node *engine_observe_node_discover(sys_slist_t *observe_node_list,
44 						  sys_snode_t **prev_node,
45 						  sys_slist_t *lwm2m_path_list,
46 						  const uint8_t *token, uint8_t tkl);
47 
48 int engine_remove_observer_by_token(struct lwm2m_ctx *ctx, const uint8_t *token, uint8_t tkl);
49 
50 int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, struct lwm2m_message *msg);
51 
52 int lwm2m_engine_observation_handler(struct lwm2m_message *msg, int observe, uint16_t accept,
53 				     bool composite);
54 
55 void engine_remove_observer_by_id(uint16_t obj_id, int32_t obj_inst_id);
56 
57 /* path object list */
58 struct lwm2m_obj_path_list {
59 	sys_snode_t node;
60 	struct lwm2m_obj_path path;
61 };
62 /* Initialize path list */
63 void lwm2m_engine_path_list_init(sys_slist_t *lwm2m_path_list, sys_slist_t *lwm2m_free_list,
64 				 struct lwm2m_obj_path_list path_object_buf[],
65 				 uint8_t path_object_size);
66 /**
67  * Add new path to the list while maintaining hierarchical sort order
68  *
69  * @param lwm2m_path_list sorted path list
70  * @param lwm2m_free_list free list
71  * @param path path to be added
72  * @return 0 on success or a negative error code
73  */
74 int lwm2m_engine_add_path_to_list(sys_slist_t *lwm2m_path_list, sys_slist_t *lwm2m_free_list,
75 				  const struct lwm2m_obj_path *path);
76 
77 int lwm2m_get_path_reference_ptr(struct lwm2m_engine_obj *obj, const struct lwm2m_obj_path *path,
78 				 void **ref);
79 
80 /**
81  * Remove paths when parent already exists in the list
82  *
83  * @note Path list must be sorted
84  * @see lwm2m_engine_add_path_to_list()
85  *
86  * @param lwm2m_path_list sorted path list
87  * @param lwm2m_free_list free list
88  */
89 void lwm2m_engine_clear_duplicate_path(sys_slist_t *lwm2m_path_list, sys_slist_t *lwm2m_free_list);
90 
91 /* Resources */
92 sys_slist_t *lwm2m_obs_obj_path_list(void);
93 #endif /* LWM2M_OBSERVATION_H */
94