Anjay Lite
Loading...
Searching...
No Matches
utils.h File Reference
#include <anj/init.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <anj/defs.h>
#include <anj_internal/utils.h>

Go to the source code of this file.

Macros

#define ANJ_INTERNAL_INCLUDE_UTILS
 
#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_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()
 
#define ANJ_CONCAT(...)
 

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)
 

Macro Definition Documentation

◆ ANJ_ARRAY_SIZE

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

◆ ANJ_ASSERT

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

◆ ANJ_CONCAT

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

Concatenates tokens passed as arguments. Can be used to do macro expansion before standard C preprocessor concatenation.

◆ ANJ_CONTAINER_OF

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

◆ ANJ_ID_INVALID

#define ANJ_ID_INVALID   UINT16_MAX

Value reserved by the LwM2M spec for all kinds of IDs (Object IDs, Object Instance IDs, Resource IDs, Resource Instance IDs, Short Server IDs).

◆ ANJ_INTERNAL_INCLUDE_UTILS

#define ANJ_INTERNAL_INCLUDE_UTILS

◆ ANJ_MAKE_INSTANCE_PATH

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

◆ ANJ_MAKE_OBJECT_PATH

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

◆ ANJ_MAKE_RESOURCE_INSTANCE_PATH

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

Below group of ANJ_MAKE_*_PATH macros are used to construct in place an unnamed structure (compound literal) of an anj_uri_path_t type.

◆ ANJ_MAKE_RESOURCE_PATH

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

◆ ANJ_MAKE_ROOT_PATH

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

◆ ANJ_MAX

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

◆ ANJ_MIN

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

◆ ANJ_QUOTE

#define ANJ_QUOTE (   Value)    # Value

Stringifies a token.

◆ ANJ_QUOTE_MACRO

#define ANJ_QUOTE_MACRO (   Value)    ANJ_QUOTE(Value)

Stringifies a token with performing additional macro expansion step.

Parameters
ValueToken to stringify.

◆ 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:70

C89-compliant replacement for static_assert.

◆ ANJ_UNREACHABLE

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

Function Documentation

◆ anj_determine_block_buffer_size()

uint16_t anj_determine_block_buffer_size ( size_t  buff_size)

Determines the size of the buffer consistent with the requirements of Block-Wise transfers - power of two and range from 16 to 1024. The calculated size will always be equal to or less than buff_size. If the buff_size is less than 16, the function will return 0.

Parameters
buff_sizeSize of the buffer.
Returns
Block-Wise buffer size.

◆ anj_double_to_string_value()

size_t anj_double_to_string_value ( char *  out_buff,
double  value 
)

Converts double value to string and copies it to out_buff (without the terminating nullbyte). The minimum required out_buff size is ANJ_DOUBLE_STR_MAX_LEN.

IMPORTANT: This function is used to encode LwM2M attributes whose float/double format is defined by LwM2M Specification: 1*DIGIT ["."1*DIGIT]. However for absolute values greater than UINT64_MAX and less than 1e-10 (or 1e-5 in case of using sprintf) exponential notation is used. Since the specification does not define the format for the value of NaN and infinite, so in this case "nan" and "inf" will be set.

IMPORTANT: This function doesn't use sprintf() if ANJ_WITH_CUSTOM_CONVERSION_FUNCTIONS is defined and is intended to be lightweight. For very large and very small numbers, a rounding error may occur.

Parameters
[out]out_buffOutput buffer.
valueInput value.
Returns
Number of bytes written.

◆ anj_int64_to_string_value()

size_t anj_int64_to_string_value ( char *  out_buff,
int64_t  value 
)

Converts int64_t value to string and copies it to out_buff (without the terminating nullbyte). The minimum required out_buff size is ANJ_I64_STR_MAX_LEN.

Parameters
[out]out_buffOutput buffer.
valueInput value.
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 
)

Converts string representation of numerical value to double value. Does not support infinitive and NAN values (LwM2M attributes representation doesn't allow for this).

Parameters
[out]out_valOutput value.
buffInput buffer.
buff_lenInput buffer length.
Returns
0 in case of success and -1 if buff_len is equal to 0 or there are characters in the buff that are not digits (exceptions shown above).

◆ anj_string_to_int64_value()

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

Converts string representation of numerical value to int64_t value.

Parameters
[out]out_valOutput value.
buffInput buffer.
buff_lenInput buffer length.
Returns
0 in case of success and -1 in case of:
  • buff_len is equal to 0
  • there are characters in the buff that are not digits
  • string represented numerical value exceeds INT64_MAX or is less than INT64_MIN
  • string is too long

◆ anj_string_to_objlnk_value()

int anj_string_to_objlnk_value ( anj_objlnk_value_t out,
const char *  objlnk 
)

Converts string representation of an LwM2M Objlnk value to a anj_objlnk_value_t strucure.

Parameters
outStructure to store the parsed value in.
objlnkNull-terminated string.
Returns
0 in case of success or -1 if the input was not a valid Objlnk string.

◆ anj_string_to_uint32_value()

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

Converts string representation of numerical value to uint32_t value.

Parameters
[out]out_valOutput value.
buffInput buffer.
buff_lenInput buffer length.
Returns
0 in case of success and -1 in case of:
  • buff_len is equal to 0
  • there are characters in the buff that are not digits
  • string represented numerical value exceeds UINT32_MAX
  • string is too long

◆ anj_string_to_uint64_value()

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

Converts string representation of numerical value to uint64_t value.

Parameters
[out]out_valOutput value.
buffInput buffer.
buff_lenInput buffer length.
Returns
0 in case of success and -1 in case of:
  • buff_len is equal to 0
  • there are characters in the buff that are not digits
  • string represented numerical value exceeds UINT64_MAX
  • string is too long

◆ anj_uint16_to_string_value()

size_t anj_uint16_to_string_value ( char *  out_buff,
uint16_t  value 
)

Converts uint16_t value to string and copies it to out_buff (without the terminating nullbyte). The minimum required out_buff size is ANJ_U16_STR_MAX_LEN.

Parameters
[out]out_buffOutput buffer.
valueInput value.
Returns
Number of bytes written.

◆ anj_uint32_to_string_value()

size_t anj_uint32_to_string_value ( char *  out_buff,
uint32_t  value 
)

Converts uint32_t value to string and copies it to out_buff (without the terminating nullbyte). The minimum required out_buff size is ANJ_U32_STR_MAX_LEN.

Parameters
[out]out_buffOutput buffer.
valueInput value.
Returns
Number of bytes written.

◆ anj_uint64_to_string_value()

size_t anj_uint64_to_string_value ( char *  out_buff,
uint64_t  value 
)

Converts uint64_t value to string and copies it to out_buff (without the terminating nullbyte). The minimum required out_buff size is ANJ_U64_STR_MAX_LEN.

Parameters
[out]out_buffOutput buffer.
valueInput value.
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

◆ anj_uri_path_has()

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

◆ anj_uri_path_increasing()

bool anj_uri_path_increasing ( const anj_uri_path_t previous_path,
const anj_uri_path_t current_path 
)

◆ anj_uri_path_is()

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

◆ anj_uri_path_length()

static size_t anj_uri_path_length ( const anj_uri_path_t path)
inlinestatic

◆ 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