1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef LWM2M_REGISTRY_H
7 #define LWM2M_REGISTRY_H
8 #include <zephyr/sys/ring_buffer.h>
9 #include "lwm2m_object.h"
10 
11 /**
12  * @brief Creates and registers an object instance to the registry. Object specified by
13  * @p obj_id must exist.
14  *
15  * @param[in] obj_id Object id of the object instance.
16  * @param[in] obj_inst_id Object instance id of the object instance to be created.
17  * @param[out] obj_inst Engine object instance buffer pointer.
18  * @return 0 for success or negative in case of error.
19  */
20 int lwm2m_create_obj_inst(uint16_t obj_id, uint16_t obj_inst_id,
21 			  struct lwm2m_engine_obj_inst **obj_inst);
22 
23 /**
24  * @brief Deletes the object instance given by @p obj_id / @p obj_inst_id.
25  *
26  * @param[in] obj_id Object id of the object instance to be deleted
27  * @param[in] obj_inst_id Object instance id of the object instance to be deleted
28  * @return 0 for success or negative in case of error.
29  */
30 int lwm2m_delete_obj_inst(uint16_t obj_id, uint16_t obj_inst_id);
31 
32 /**
33  * @brief Get the engine object instance specified by @p msg->path. Usually only used in do_write
34  * functions in the different encoders.
35  *
36  * @param[in] msg lwm2m message containing the path to the object instance.
37  * @param[out] obj_inst Engine object instance buffer pointer.
38  * @param[out] created Points to a 1 if an object instance was created. Else 0. Can also be NULL.
39  * @return 0 for success or negative in case of error.
40  */
41 int lwm2m_get_or_create_engine_obj(struct lwm2m_message *msg,
42 				   struct lwm2m_engine_obj_inst **obj_inst, uint8_t *created);
43 
44 /**
45  * @brief Appends an object to the registry. Usually called in the init function
46  * of an object.
47  *
48  * @param[in] obj Object to be registered.
49  */
50 void lwm2m_register_obj(struct lwm2m_engine_obj *obj);
51 
52 /**
53  * @brief Removes an object from the registry.
54  *
55  * @param[in] obj Object to be removed
56  */
57 void lwm2m_unregister_obj(struct lwm2m_engine_obj *obj);
58 
59 /**
60  * @brief Get the resource instance specified by @p path. Creates and allocates a new one
61  * if the resource instance does not exist.
62  *
63  * @param[in] path Path to resource instance (i.e 100/100/100/1)
64  * @param[out] res Engine resource buffer pointer.
65  * @param[out] res_inst Engine resource instance buffer pointer.
66  * @return 0 for success or negative in case of error.
67  */
68 int lwm2m_engine_get_create_res_inst(const struct lwm2m_obj_path *path,
69 				     struct lwm2m_engine_res **res,
70 				     struct lwm2m_engine_res_inst **res_inst);
71 
72 /**
73  * @brief Gets the resource specified by @p path.
74  *
75  * @param[in] path Path to resource (i.e 100/100/100/x, the fourth component is optional)
76  * @param[out] res Engine resource buffer pointer.
77  * @return 0 for success or negative in case of error.
78  */
79 int lwm2m_get_resource(const struct lwm2m_obj_path *path, struct lwm2m_engine_res **res);
80 
81 /**
82  * @brief Returns pointer to the object in the registry specified by @p path.
83  * Returns NULL if it does not exist.
84  *
85  * @param[in] path lwm2m_obj_path to the object.
86  * @return Pointer to engine object in the registry
87  */
88 struct lwm2m_engine_obj *lwm2m_engine_get_obj(const struct lwm2m_obj_path *path);
89 
90 /**
91  * @brief Returns pointer to the object instance in the registry specified by @p path.
92  * Returns NULL if it does not exist.
93  *
94  * @param[in] path lwm2m_obj_path to the object instance.
95  * @return Pointer to engine object instance in the registry
96  */
97 struct lwm2m_engine_obj_inst *lwm2m_engine_get_obj_inst(const struct lwm2m_obj_path *path);
98 
99 /**
100  * @brief Returns pointer to the resource in the registry specified by @p path.
101  * Returns NULL if it does not exist.
102  *
103  * @param[in] path lwm2m_obj_path to the resource.
104  * @return Pointer to engine resource in the registry
105  */
106 struct lwm2m_engine_res *lwm2m_engine_get_res(const struct lwm2m_obj_path *path);
107 
108 /**
109  * @brief Returns pointer to the resource instance in the registry specified by @p path.
110  * Returns NULL if it does not exist.
111  *
112  * @param[in] path lwm2m_obj_path to the resource instance.
113  * @return Pointer to the engine resource instance in the registry
114  */
115 struct lwm2m_engine_res_inst *lwm2m_engine_get_res_inst(const struct lwm2m_obj_path *path);
116 
117 /**
118  * @brief Get the engine obj specified by @p obj_id.
119  *
120  * @param[in] obj_id Object id of the object.
121  * @return Pointer to the engine object.
122  */
123 struct lwm2m_engine_obj *get_engine_obj(int obj_id);
124 
125 /**
126  * @brief Get the engine object instance @p obj_id /@p obj_inst_id
127  *
128  * @param[in] obj_id Object if of the object instance.
129  * @param[in] obj_inst_id Object instance id of object instance
130  * @return Pointer to the object instance.
131  */
132 struct lwm2m_engine_obj_inst *get_engine_obj_inst(int obj_id, int obj_inst_id);
133 
134 /**
135  * @brief Get object instance, field, resource and resource instance. All variables may be NULL.
136  * Example to get resource instance:
137  * int ret = path_to_objs(&path, NULL, NULL, NULL, &res_inst);
138  *
139  * @param[in] path lwm2m object path to the object/object instance/resource/resource instance
140  * to get.
141  * @param[out] obj_inst Engine object instance buffer pointer.
142  * @param[out] obj_field Engine object field buffer pointer.
143  * @param[out] res Engine resource buffer pointer.
144  * @param[out] res_inst Engine resource instance buffer pointer.
145  * @return 0 for success or negative in case of error.
146  */
147 int path_to_objs(const struct lwm2m_obj_path *path, struct lwm2m_engine_obj_inst **obj_inst,
148 		 struct lwm2m_engine_obj_field **obj_field, struct lwm2m_engine_res **res,
149 		 struct lwm2m_engine_res_inst **res_inst);
150 
151 /**
152  * @brief Returns the object instance in the registry with object id = @p obj_id that has the
153  * smallest object instance id strictly larger than @p obj_inst_id.
154  *
155  * @param[in] obj_id Object id of the object instance.
156  * @param[in] obj_inst_id Lower bound of the object instance id.
157  * @return Pointer to the object instance. NULL if not found.
158  */
159 struct lwm2m_engine_obj_inst *next_engine_obj_inst(int obj_id, int obj_inst_id);
160 
161 /**
162  * @brief Returns a boolean to signal whether or not or it's needed to report object version.
163  *
164  * @param[in] obj Pointer to engine object.
165  * @return True if it's needed to report object version, else false
166  */
167 bool lwm2m_engine_shall_report_obj_version(const struct lwm2m_engine_obj *obj);
168 
169 /**
170  * @brief Returns the binding mode in use.
171  *
172  * Defaults to UDP (U). In LwM2M 1.0 when queue mode is enabled, return "UQ".
173  *
174  * @param[out] binding Data buffer.
175  */
176 void lwm2m_engine_get_binding(char *binding);
177 
178 /**
179  * @brief Returns the queue mode (Q) if queue mode is enabled. Else an empty string.
180  *
181  * @param[out] queue Queue mode data buffer
182  */
183 void lwm2m_engine_get_queue_mode(char *queue);
184 
185 /**
186  * @brief Returns the engine object field with resource id @p res_id of the object @p obj.
187  *
188  * @param[in] obj lwm2m engine object of the field.
189  * @param[in] res_id Resource id of the field.
190  * @return Pointer to an engine object field, or NULL if it does not exist
191  */
192 struct lwm2m_engine_obj_field *lwm2m_get_engine_obj_field(struct lwm2m_engine_obj *obj, int res_id);
193 
194 size_t lwm2m_engine_get_opaque_more(struct lwm2m_input_context *in, uint8_t *buf, size_t buflen,
195 				    struct lwm2m_opaque_context *opaque, bool *last_block);
196 
197 
198 /* Resources */
199 sys_slist_t *lwm2m_engine_obj_list(void);
200 sys_slist_t *lwm2m_engine_obj_inst_list(void);
201 
202 /* Data cache Internal API */
203 
204 /**
205  * LwM2M Time series resoursce data storage
206  */
207 struct lwm2m_time_series_resource {
208 	/* object list */
209 	sys_snode_t node;
210 	/* Resource Path url */
211 	struct lwm2m_obj_path path;
212 	/* Ring buffer */
213 	struct ring_buf rb;
214 };
215 
216 #if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT)
217 
218 #define LWM2M_LIMITED_TIMESERIES_RESOURCE_COUNT 20
219 
220 struct lwm2m_cache_read_entry {
221 	struct lwm2m_time_series_resource *cache_data;
222 	int32_t original_get_head;
223 	int32_t original_get_tail;
224 	int32_t original_get_base;
225 };
226 
227 struct lwm2m_cache_read_info {
228 	struct lwm2m_cache_read_entry read_info[CONFIG_LWM2M_MAX_CACHED_RESOURCES];
229 	int entry_limit;
230 	int entry_size;
231 };
232 #endif
233 
234 struct lwm2m_time_series_resource *
235 lwm2m_cache_entry_get_by_object(const struct lwm2m_obj_path *obj_path);
236 bool lwm2m_cache_write(struct lwm2m_time_series_resource *cache_entry,
237 		       struct lwm2m_time_series_elem *buf);
238 bool lwm2m_cache_read(struct lwm2m_time_series_resource *cache_entry,
239 		      struct lwm2m_time_series_elem *buf);
240 size_t lwm2m_cache_size(const struct lwm2m_time_series_resource *cache_entry);
241 
242 #endif /* LWM2M_REGISTRY_H */
243