Anjay Lite
Loading...
Searching...
No Matches
utils.h File Reference

Public utility helpers used throughout Anjay Lite. More...

#include <anj/init.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <anj/defs.h>

Go to the source code of this file.

Macros

#define ANJ_ID_INVALID   UINT16_MAX
 
#define ANJ_CONTAINER_OF(ptr, type, member)    ((type *) (void *) ((char *) (intptr_t) (ptr) -offsetof(type, member)))
 
#define ANJ_MIN(a, b)   ((a) < (b) ? (a) : (b))
 
#define ANJ_MAX(a, b)   ((a) < (b) ? (b) : (a))
 
#define ANJ_ARRAY_SIZE(arr)   (sizeof(arr) / sizeof(arr[0]))
 
#define ANJ_QUOTE(Value)   # Value
 
#define ANJ_QUOTE_MACRO(Value)   ANJ_QUOTE(Value)
 
#define ANJ_ASSERT(cond, msg)   assert((cond) && (bool) "" msg)
 
#define ANJ_UNREACHABLE(msg)   ANJ_ASSERT(0, msg)
 
#define ANJ_CONCAT(...)
 
#define ANJ_STATIC_ASSERT(condition, message)
 
#define ANJ_MAKE_RESOURCE_INSTANCE_PATH(Oid, Iid, Rid, Riid)    _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, Riid, 4)
 
#define ANJ_MAKE_RESOURCE_PATH(Oid, Iid, Rid)    _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, ANJ_ID_INVALID, 3)
 
#define ANJ_MAKE_INSTANCE_PATH(Oid, Iid)    _ANJ_MAKE_URI_PATH(Oid, Iid, ANJ_ID_INVALID, ANJ_ID_INVALID, 2)
 
#define ANJ_MAKE_OBJECT_PATH(Oid)
 
#define ANJ_MAKE_ROOT_PATH()
 

Functions

static bool anj_uri_path_equal (const anj_uri_path_t *left, const anj_uri_path_t *right)
 
static size_t anj_uri_path_length (const anj_uri_path_t *path)
 
static bool anj_uri_path_has (const anj_uri_path_t *path, anj_id_type_t id_type)
 
static bool anj_uri_path_is (const anj_uri_path_t *path, anj_id_type_t id_type)
 
static bool anj_uri_path_outside_base (const anj_uri_path_t *path, const anj_uri_path_t *base)
 
bool anj_uri_path_increasing (const anj_uri_path_t *previous_path, const anj_uri_path_t *current_path)
 
uint16_t anj_determine_block_buffer_size (size_t buff_size)
 
size_t anj_uint16_to_string_value (char *out_buff, uint16_t value)
 
size_t anj_uint32_to_string_value (char *out_buff, uint32_t value)
 
size_t anj_uint64_to_string_value (char *out_buff, uint64_t value)
 
size_t anj_int64_to_string_value (char *out_buff, int64_t value)
 
size_t anj_double_to_string_value (char *out_buff, double value)
 
int anj_string_to_uint32_value (uint32_t *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)
 
int anj_string_to_objlnk_value (anj_objlnk_value_t *out, const char *objlnk)
 
int anj_string_to_double_value (double *out_val, const char *buff, size_t buff_len)
 

Detailed Description

Public utility helpers used throughout Anjay Lite.

This header provides small generic macros, helpers for working with LwM2M paths, and number/string conversion routines used by the library. It is safe to include from application code.

Macro Definition Documentation

◆ ANJ_ARRAY_SIZE

#define ANJ_ARRAY_SIZE (   arr)    (sizeof(arr) / sizeof(arr[0]))

Number of elements in a C array.

◆ ANJ_ASSERT

#define ANJ_ASSERT (   cond,
  msg 
)    assert((cond) && (bool) "" msg)

Assertion helper with a fixed string literal message.

◆ ANJ_CONCAT

#define ANJ_CONCAT (   ...)
Value:
_ANJ_CONCAT_INTERNAL__( \
_ANJ_CONCAT_INTERNAL__(_ANJ_CONCAT_INTERNAL_, \
_ANJ_VARARG_LENGTH(__VA_ARGS__)), \
__) \
(__VA_ARGS__)

Concatenates tokens after performing macro-argument expansion. Useful when building identifiers from macro parameters.

◆ ANJ_CONTAINER_OF

#define ANJ_CONTAINER_OF (   ptr,
  type,
  member 
)     ((type *) (void *) ((char *) (intptr_t) (ptr) -offsetof(type, member)))

Returns a pointer to the structure that contains member at address ptr.

Parameters
ptrPointer to a structure member.
typeType of the outer structure.
memberMember name inside type.

◆ ANJ_ID_INVALID

#define ANJ_ID_INVALID   UINT16_MAX

Special “invalid” value used by LwM2M for all identifier kinds: Object IDs, Object Instance IDs, Resource IDs, Resource Instance IDs and Short Server IDs.

◆ ANJ_MAKE_INSTANCE_PATH

#define ANJ_MAKE_INSTANCE_PATH (   Oid,
  Iid 
)     _ANJ_MAKE_URI_PATH(Oid, Iid, ANJ_ID_INVALID, ANJ_ID_INVALID, 2)

Constructs an anj_uri_path_t compound literal representing an Object Instance path: /Oid/Iid.

◆ ANJ_MAKE_OBJECT_PATH

#define ANJ_MAKE_OBJECT_PATH (   Oid)
Value:
_ANJ_MAKE_URI_PATH( \
#define ANJ_ID_INVALID
Definition utils.h:45

Constructs an anj_uri_path_t compound literal representing an Object path: /Oid.

◆ ANJ_MAKE_RESOURCE_INSTANCE_PATH

#define ANJ_MAKE_RESOURCE_INSTANCE_PATH (   Oid,
  Iid,
  Rid,
  Riid 
)     _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, Riid, 4)

Constructs an anj_uri_path_t compound literal representing a Resource Instance path: /Oid/Iid/Rid/Riid.

◆ ANJ_MAKE_RESOURCE_PATH

#define ANJ_MAKE_RESOURCE_PATH (   Oid,
  Iid,
  Rid 
)     _ANJ_MAKE_URI_PATH(Oid, Iid, Rid, ANJ_ID_INVALID, 3)

Constructs an anj_uri_path_t compound literal representing a Resource path: /Oid/Iid/Rid.

◆ ANJ_MAKE_ROOT_PATH

#define ANJ_MAKE_ROOT_PATH ( )
Value:
_ANJ_MAKE_URI_PATH(ANJ_ID_INVALID, \
0)

Constructs an anj_uri_path_t compound literal representing the root path.

◆ ANJ_MAX

#define ANJ_MAX (   a,
 
)    ((a) < (b) ? (b) : (a))

Maximum of two values.

◆ ANJ_MIN

#define ANJ_MIN (   a,
 
)    ((a) < (b) ? (a) : (b))

Minimum of two values.

◆ ANJ_QUOTE

#define ANJ_QUOTE (   Value)    # Value

Turns a token into a string literal without expanding it.

◆ ANJ_QUOTE_MACRO

#define ANJ_QUOTE_MACRO (   Value)    ANJ_QUOTE(Value)

Turns a token into a string literal after performing macro expansion.

◆ ANJ_STATIC_ASSERT

#define ANJ_STATIC_ASSERT (   condition,
  message 
)
Value:
struct ANJ_CONCAT(static_assert_, message) { \
char message[(condition) ? 1 : -1]; \
}
#define ANJ_CONCAT(...)
Definition utils.h:85

C99-compatible replacement for static_assert.

Produces a compile-time error if condition is false.

Parameters
conditionBoolean expression evaluated at compile time.
messageIdentifier used in the generated struct name.

◆ ANJ_UNREACHABLE

#define ANJ_UNREACHABLE (   msg)    ANJ_ASSERT(0, msg)

Marks code that should be unreachable in a correct program flow. Triggers an assertion failure in debug builds.

Function Documentation

◆ anj_determine_block_buffer_size()

uint16_t anj_determine_block_buffer_size ( size_t  buff_size)

Determine a Block-Wise transfer buffer size that satisfies RFC 7959 constraints (power of two in the range 16-1024), not exceeding buff_size.

If buff_size is smaller than 16, returns 0.

Parameters
buff_sizeAvailable buffer size in bytes.
Returns
Selected block size in bytes.

◆ anj_double_to_string_value()

size_t anj_double_to_string_value ( char *  out_buff,
double  value 
)

Convert a double to a string (without a terminating NUL).

This function is used to encode LwM2M attribute values whose textual format is defined as 1*DIGIT ["." 1*DIGIT]. For very large/small magnitudes (>|UINT64_MAX|, <1e-10) an exponential notation may be used. NaN and infinities are emitted as "nan" and "inf".

If ANJ_WITH_CUSTOM_CONVERSION_FUNCTIONS is enabled, a lightweight formatter is used (may incur rounding error at extreme magnitudes).

The buffer must be at least ANJ_DOUBLE_STR_MAX_LEN bytes long.

Returns
Number of bytes written.

◆ anj_int64_to_string_value()

size_t anj_int64_to_string_value ( char *  out_buff,
int64_t  value 
)

Convert an int64_t to a decimal string (without a terminating nullchar). The buffer must be at least ANJ_I64_STR_MAX_LEN bytes long.

Returns
Number of bytes written.

◆ anj_string_to_double_value()

int anj_string_to_double_value ( double *  out_val,
const char *  buff,
size_t  buff_len 
)

Parse a decimal string into double (no inf / nan support, consistent with LwM2M attribute representation).

Note
The input string is not required to be null-terminated. The caller must explicitly provide the length in buff_len (e.g., using strlen() if the content is null-terminated).
Returns
0 on success, -1 on error (empty input, invalid characters, etc.).

◆ anj_string_to_int64_value()

int anj_string_to_int64_value ( int64_t *  out_val,
const char *  buff,
size_t  buff_len 
)

Parse a decimal string into int64_t (optional leading sign allowed).

Note
The input string is not required to be null-terminated. The caller must explicitly provide the length in buff_len (e.g., using strlen() if the content is null-terminated).
Returns
0 on success, -1 on error (empty input, invalid characters, or value outside INT64_MIN..INT64_MAX).
Parameters
[out]out_valParsed value.
buffInput buffer.
buff_lenInput length.
Returns
0 on success, -1 on error (empty input, non-digits present (except leading plus/minus sign), overflow, or excessive length).

◆ anj_string_to_objlnk_value()

int anj_string_to_objlnk_value ( anj_objlnk_value_t out,
const char *  objlnk 
)

Parse an Objlnk string (e.g., "3:0") into anj_objlnk_value_t.

Note
Unlike the numeric parsers, this function expects a standard null-terminated string.
Parameters
[out]outOutput structure.
objlnknull-terminated input string.
Returns
0 on success, -1 if the input is not a valid Objlnk.

◆ anj_string_to_uint32_value()

int anj_string_to_uint32_value ( uint32_t *  out_val,
const char *  buff,
size_t  buff_len 
)

Parse a decimal string into uint32_t.

Note
The input string is not required to be null-terminated. The caller must explicitly provide the length in buff_len (e.g., using strlen() if the content is null-terminated).
Parameters
[out]out_valParsed value.
buffInput buffer.
buff_lenInput length.
Returns
0 on success, -1 on error (empty input, non-digits present, overflow, or excessive length).

◆ anj_string_to_uint64_value()

int anj_string_to_uint64_value ( uint64_t *  out_val,
const char *  buff,
size_t  buff_len 
)

Parse a decimal string into uint64_t.

Note
The input string is not required to be null-terminated. The caller must explicitly provide the length in buff_len (e.g., using strlen() if the content is null-terminated).
Parameters
[out]out_valParsed value.
buffInput buffer.
buff_lenInput length.
Returns
0 on success, -1 on error (empty input, non-digits present, overflow, or excessive length).

◆ anj_uint16_to_string_value()

size_t anj_uint16_to_string_value ( char *  out_buff,
uint16_t  value 
)

Convert a uint16_t to a decimal string (without a terminating nullchar). The buffer must be at least ANJ_U16_STR_MAX_LEN bytes long.

Returns
Number of bytes written.

◆ anj_uint32_to_string_value()

size_t anj_uint32_to_string_value ( char *  out_buff,
uint32_t  value 
)

Convert a uint32_t to a decimal string (without a terminating nullchar). The buffer must be at least ANJ_U32_STR_MAX_LEN bytes long.

Returns
Number of bytes written.

◆ anj_uint64_to_string_value()

size_t anj_uint64_to_string_value ( char *  out_buff,
uint64_t  value 
)

Convert a uint64_t to a decimal string (without a terminating nullchar). The buffer must be at least ANJ_U64_STR_MAX_LEN bytes long.

Returns
Number of bytes written.

◆ anj_uri_path_equal()

static bool anj_uri_path_equal ( const anj_uri_path_t left,
const anj_uri_path_t right 
)
inlinestatic

Compares two LwM2M paths for equality (same length and same components).

◆ anj_uri_path_has()

static bool anj_uri_path_has ( const anj_uri_path_t path,
anj_id_type_t  id_type 
)
inlinestatic

Checks whether path contains the component identified by id_type. For example, /Oid/Iid “has” ANJ_ID_OID and ANJ_ID_IID.

◆ anj_uri_path_increasing()

bool anj_uri_path_increasing ( const anj_uri_path_t previous_path,
const anj_uri_path_t current_path 
)

Checks that current_path is lexicographically greater than previous_path.

◆ anj_uri_path_is()

static bool anj_uri_path_is ( const anj_uri_path_t path,
anj_id_type_t  id_type 
)
inlinestatic

Checks whether path is exactly of the level identified by id_type. For example, /Oid/Iid “is” ANJ_ID_IID.

◆ anj_uri_path_length()

static size_t anj_uri_path_length ( const anj_uri_path_t path)
inlinestatic

Returns the number of components in path.

◆ anj_uri_path_outside_base()

static bool anj_uri_path_outside_base ( const anj_uri_path_t path,
const anj_uri_path_t base 
)
inlinestatic

Checks whether path lies outside of the subtree rooted at base. Returns true if path is shorter than base or diverges at any component.