Anjay Lite
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1/*
2 * Copyright 2023-2025 AVSystem <avsystem@avsystem.com>
3 * AVSystem Anjay Lite LwM2M SDK
4 * All rights reserved.
5 *
6 * Licensed under AVSystem Anjay Lite LwM2M Client SDK - Non-Commercial License.
7 * See the attached LICENSE file for details.
8 */
9
10#include <anj/init.h>
11
21#ifndef ANJ_UTILS_H
22# define ANJ_UTILS_H
23
24# include <stddef.h>
25# include <stdint.h>
26# include <stdlib.h>
27
28# include <anj/defs.h>
29
31# define ANJ_INTERNAL_INCLUDE_UTILS
32# include <anj_internal/utils.h>
33# undef ANJ_INTERNAL_INCLUDE_UTILS
36# ifdef __cplusplus
37extern "C" {
38# endif
39
45# define ANJ_ID_INVALID UINT16_MAX
46
54# define ANJ_CONTAINER_OF(ptr, type, member) \
55 ((type *) (void *) ((char *) (intptr_t) (ptr) -offsetof(type, member)))
56
58# define ANJ_MIN(a, b) ((a) < (b) ? (a) : (b))
59
61# define ANJ_MAX(a, b) ((a) < (b) ? (b) : (a))
62
64# define ANJ_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
65
67# define ANJ_QUOTE(Value) # Value
68
70# define ANJ_QUOTE_MACRO(Value) ANJ_QUOTE(Value)
71
73# define ANJ_ASSERT(cond, msg) assert((cond) && (bool) "" msg)
74
79# define ANJ_UNREACHABLE(msg) ANJ_ASSERT(0, msg)
80
85# define ANJ_CONCAT(...) \
86 _ANJ_CONCAT_INTERNAL__( \
87 _ANJ_CONCAT_INTERNAL__(_ANJ_CONCAT_INTERNAL_, \
88 _ANJ_VARARG_LENGTH(__VA_ARGS__)), \
89 __) \
90 (__VA_ARGS__)
91
100# define ANJ_STATIC_ASSERT(condition, message) \
101 struct ANJ_CONCAT(static_assert_, message) { \
102 char message[(condition) ? 1 : -1]; \
103 }
104
109# define ANJ_MAKE_RESOURCE_INSTANCE_PATH(Oid, Iid, Rid, Riid) \
110 _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, Riid, 4)
111
116# define ANJ_MAKE_RESOURCE_PATH(Oid, Iid, Rid) \
117 _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, ANJ_ID_INVALID, 3)
118
123# define ANJ_MAKE_INSTANCE_PATH(Oid, Iid) \
124 _ANJ_MAKE_URI_PATH(Oid, Iid, ANJ_ID_INVALID, ANJ_ID_INVALID, 2)
125
130# define ANJ_MAKE_OBJECT_PATH(Oid) \
131 _ANJ_MAKE_URI_PATH( \
132 Oid, ANJ_ID_INVALID, ANJ_ID_INVALID, ANJ_ID_INVALID, 1)
133
138# define ANJ_MAKE_ROOT_PATH() \
139 _ANJ_MAKE_URI_PATH(ANJ_ID_INVALID, \
140 ANJ_ID_INVALID, \
141 ANJ_ID_INVALID, \
142 ANJ_ID_INVALID, \
143 0)
144
148static inline bool anj_uri_path_equal(const anj_uri_path_t *left,
149 const anj_uri_path_t *right) {
150 if (left->uri_len != right->uri_len) {
151 return false;
152 }
153 for (size_t i = 0; i < left->uri_len; ++i) {
154 if (left->ids[i] != right->ids[i]) {
155 return false;
156 }
157 }
158 return true;
159}
160
162static inline size_t anj_uri_path_length(const anj_uri_path_t *path) {
163 return path->uri_len;
164}
165
170static inline bool anj_uri_path_has(const anj_uri_path_t *path,
171 anj_id_type_t id_type) {
172 return path->uri_len > id_type;
173}
174
179static inline bool anj_uri_path_is(const anj_uri_path_t *path,
180 anj_id_type_t id_type) {
181 return path->uri_len == (size_t) id_type + 1u;
182}
183
188static inline bool anj_uri_path_outside_base(const anj_uri_path_t *path,
189 const anj_uri_path_t *base) {
190 if (path->uri_len < base->uri_len) {
191 return true;
192 }
193 for (size_t i = 0; i < base->uri_len; ++i) {
194 if (path->ids[i] != base->ids[i]) {
195 return true;
196 }
197 }
198 return false;
199}
200
205bool anj_uri_path_increasing(const anj_uri_path_t *previous_path,
206 const anj_uri_path_t *current_path);
207
217uint16_t anj_determine_block_buffer_size(size_t buff_size);
218
225size_t anj_uint16_to_string_value(char *out_buff, uint16_t value);
226
233size_t anj_uint32_to_string_value(char *out_buff, uint32_t value);
234
241size_t anj_uint64_to_string_value(char *out_buff, uint64_t value);
242
249size_t anj_int64_to_string_value(char *out_buff, int64_t value);
250
266size_t anj_double_to_string_value(char *out_buff, double value);
267
282int anj_string_to_uint32_value(uint32_t *out_val,
283 const char *buff,
284 size_t buff_len);
285
300int anj_string_to_uint64_value(uint64_t *out_val,
301 const char *buff,
302 size_t buff_len);
303
322int anj_string_to_int64_value(int64_t *out_val,
323 const char *buff,
324 size_t buff_len);
325
337int anj_string_to_objlnk_value(anj_objlnk_value_t *out, const char *objlnk);
338
349int anj_string_to_double_value(double *out_val,
350 const char *buff,
351 size_t buff_len);
352
353# ifdef __cplusplus
354}
355# endif
356
357#endif // ANJ_UTILS_H
Core type and constant definitions for Anjay Lite.
anj_id_type_t
Definition defs.h:238
Global configuration validation header for Anjay Lite.
Definition defs.h:525
Definition defs.h:260
uint16_t ids[ANJ_URI_PATH_MAX_LENGTH]
Definition defs.h:261
size_t uri_len
Definition defs.h:262
static bool anj_uri_path_equal(const anj_uri_path_t *left, const anj_uri_path_t *right)
Definition utils.h:148
size_t anj_uint32_to_string_value(char *out_buff, uint32_t value)
static bool anj_uri_path_is(const anj_uri_path_t *path, anj_id_type_t id_type)
Definition utils.h:179
int anj_string_to_objlnk_value(anj_objlnk_value_t *out, const char *objlnk)
static bool anj_uri_path_outside_base(const anj_uri_path_t *path, const anj_uri_path_t *base)
Definition utils.h:188
int anj_string_to_uint32_value(uint32_t *out_val, const char *buff, size_t buff_len)
size_t anj_int64_to_string_value(char *out_buff, int64_t value)
size_t anj_uint16_to_string_value(char *out_buff, uint16_t value)
size_t anj_uint64_to_string_value(char *out_buff, uint64_t value)
bool anj_uri_path_increasing(const anj_uri_path_t *previous_path, const anj_uri_path_t *current_path)
static bool anj_uri_path_has(const anj_uri_path_t *path, anj_id_type_t id_type)
Definition utils.h:170
size_t anj_double_to_string_value(char *out_buff, double value)
uint16_t anj_determine_block_buffer_size(size_t buff_size)
int anj_string_to_double_value(double *out_val, const char *buff, size_t buff_len)
int anj_string_to_uint64_value(uint64_t *out_val, const char *buff, size_t buff_len)
int anj_string_to_int64_value(int64_t *out_val, const char *buff, size_t buff_len)
static size_t anj_uri_path_length(const anj_uri_path_t *path)
Definition utils.h:162