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
12#ifndef ANJ_UTILS_H
13# define ANJ_UTILS_H
14
15# include <stddef.h>
16# include <stdint.h>
17# include <stdlib.h>
18
19# include <anj/defs.h>
20
21# define ANJ_INTERNAL_INCLUDE_UTILS
22# include <anj_internal/utils.h>
23# undef ANJ_INTERNAL_INCLUDE_UTILS
24
25# ifdef __cplusplus
26extern "C" {
27# endif
28
33# define ANJ_ID_INVALID UINT16_MAX
34
35# define ANJ_CONTAINER_OF(ptr, type, member) \
36 ((type *) (void *) ((char *) (intptr_t) (ptr) -offsetof(type, member)))
37# define ANJ_MIN(a, b) ((a) < (b) ? (a) : (b))
38# define ANJ_MAX(a, b) ((a) < (b) ? (b) : (a))
39# define ANJ_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
40
44# define ANJ_QUOTE(Value) # Value
45
51# define ANJ_QUOTE_MACRO(Value) ANJ_QUOTE(Value)
52
53/*
54 * Definition of an assert with hard-coded string literal message that does not
55 * trigger compiler warnings.
56 */
57# define ANJ_ASSERT(cond, msg) assert((cond) && (bool) "" msg)
58
59/*
60 * Marks an execution path as breaking some invariants, which should never
61 * happen in correct code.
62 */
63# define ANJ_UNREACHABLE(msg) ANJ_ASSERT(0, msg)
64
70# define ANJ_CONCAT(...) \
71 _ANJ_CONCAT_INTERNAL__( \
72 _ANJ_CONCAT_INTERNAL__(_ANJ_CONCAT_INTERNAL_, \
73 _ANJ_VARARG_LENGTH(__VA_ARGS__)), \
74 __) \
75 (__VA_ARGS__)
81# define ANJ_STATIC_ASSERT(condition, message) \
82 struct ANJ_CONCAT(static_assert_, message) { \
83 char message[(condition) ? 1 : -1]; \
84 }
85
90# define ANJ_MAKE_RESOURCE_INSTANCE_PATH(Oid, Iid, Rid, Riid) \
91 _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, Riid, 4)
92
93# define ANJ_MAKE_RESOURCE_PATH(Oid, Iid, Rid) \
94 _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, ANJ_ID_INVALID, 3)
95
96# define ANJ_MAKE_INSTANCE_PATH(Oid, Iid) \
97 _ANJ_MAKE_URI_PATH(Oid, Iid, ANJ_ID_INVALID, ANJ_ID_INVALID, 2)
98
99# define ANJ_MAKE_OBJECT_PATH(Oid) \
100 _ANJ_MAKE_URI_PATH( \
101 Oid, ANJ_ID_INVALID, ANJ_ID_INVALID, ANJ_ID_INVALID, 1)
102
103# define ANJ_MAKE_ROOT_PATH() \
104 _ANJ_MAKE_URI_PATH(ANJ_ID_INVALID, \
105 ANJ_ID_INVALID, \
106 ANJ_ID_INVALID, \
107 ANJ_ID_INVALID, \
108 0)
109
110static inline bool anj_uri_path_equal(const anj_uri_path_t *left,
111 const anj_uri_path_t *right) {
112 if (left->uri_len != right->uri_len) {
113 return false;
114 }
115 for (size_t i = 0; i < left->uri_len; ++i) {
116 if (left->ids[i] != right->ids[i]) {
117 return false;
118 }
119 }
120 return true;
121}
122
123static inline size_t anj_uri_path_length(const anj_uri_path_t *path) {
124 return path->uri_len;
125}
126
127static inline bool anj_uri_path_has(const anj_uri_path_t *path,
128 anj_id_type_t id_type) {
129 return path->uri_len > id_type;
130}
131
132static inline bool anj_uri_path_is(const anj_uri_path_t *path,
133 anj_id_type_t id_type) {
134 return path->uri_len == (size_t) id_type + 1u;
135}
136
137static inline bool anj_uri_path_outside_base(const anj_uri_path_t *path,
138 const anj_uri_path_t *base) {
139 if (path->uri_len < base->uri_len) {
140 return true;
141 }
142 for (size_t i = 0; i < base->uri_len; ++i) {
143 if (path->ids[i] != base->ids[i]) {
144 return true;
145 }
146 }
147 return false;
148}
149
150bool anj_uri_path_increasing(const anj_uri_path_t *previous_path,
151 const anj_uri_path_t *current_path);
152
163uint16_t anj_determine_block_buffer_size(size_t buff_size);
164
176size_t anj_uint16_to_string_value(char *out_buff, uint16_t value);
177
188size_t anj_uint32_to_string_value(char *out_buff, uint32_t value);
189
200size_t anj_uint64_to_string_value(char *out_buff, uint64_t value);
201
212size_t anj_int64_to_string_value(char *out_buff, int64_t value);
213
236size_t anj_double_to_string_value(char *out_buff, double value);
237
251int anj_string_to_uint32_value(uint32_t *out_val,
252 const char *buff,
253 size_t buff_len);
254
268int anj_string_to_uint64_value(uint64_t *out_val,
269 const char *buff,
270 size_t buff_len);
271
286int anj_string_to_int64_value(int64_t *out_val,
287 const char *buff,
288 size_t buff_len);
289
300int anj_string_to_objlnk_value(anj_objlnk_value_t *out, const char *objlnk);
301
314int anj_string_to_double_value(double *out_val,
315 const char *buff,
316 size_t buff_len);
317
318# ifdef __cplusplus
319}
320# endif
321
322#endif // ANJ_UTILS_H
anj_id_type_t
Definition defs.h:196
Definition defs.h:482
Definition defs.h:213
uint16_t ids[ANJ_URI_PATH_MAX_LENGTH]
Definition defs.h:214
size_t uri_len
Definition defs.h:215
static bool anj_uri_path_equal(const anj_uri_path_t *left, const anj_uri_path_t *right)
Definition utils.h:110
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:132
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:137
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:127
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:123