Program Listing for File utils.h

Return to documentation for file (include_public/anj/utils.h)

/*
 * Copyright 2023-2026 AVSystem <avsystem@avsystem.com>
 * AVSystem Anjay Lite LwM2M SDK
 * All rights reserved.
 *
 * Licensed under AVSystem Anjay Lite LwM2M Client SDK - Non-Commercial License.
 * See the attached LICENSE file for details.
 */

#include <anj/init.h>

#ifndef ANJ_UTILS_H
#    define ANJ_UTILS_H

#    include <stddef.h>
#    include <stdint.h>
#    include <stdlib.h>

#    include <anj/defs.h>

#    define ANJ_INTERNAL_INCLUDE_UTILS
#    include <anj_internal/utils.h>
#    undef ANJ_INTERNAL_INCLUDE_UTILS
#    ifdef __cplusplus
extern "C" {
#    endif

#    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(...)                                              \
        _ANJ_CONCAT_INTERNAL__(                                          \
                _ANJ_CONCAT_INTERNAL__(_ANJ_CONCAT_INTERNAL_,            \
                                       _ANJ_VARARG_LENGTH(__VA_ARGS__)), \
                __)                                                      \
        (__VA_ARGS__)

#    define ANJ_STATIC_ASSERT(condition, message)    \
        struct ANJ_CONCAT(static_assert_, message) { \
            char message[(condition) ? 1 : -1];      \
        }

#    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) \
        _ANJ_MAKE_URI_PATH(           \
                Oid, ANJ_ID_INVALID, ANJ_ID_INVALID, ANJ_ID_INVALID, 1)

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

static inline bool anj_uri_path_equal(const anj_uri_path_t *left,
                                      const anj_uri_path_t *right) {
    if (left->uri_len != right->uri_len) {
        return false;
    }
    for (size_t i = 0; i < left->uri_len; ++i) {
        if (left->ids[i] != right->ids[i]) {
            return false;
        }
    }
    return true;
}

static inline size_t anj_uri_path_length(const anj_uri_path_t *path) {
    return path->uri_len;
}

static inline bool anj_uri_path_has(const anj_uri_path_t *path,
                                    anj_id_type_t id_type) {
    return path->uri_len > id_type;
}

static inline bool anj_uri_path_is(const anj_uri_path_t *path,
                                   anj_id_type_t id_type) {
    return path->uri_len == (size_t) id_type + 1u;
}

static inline bool anj_uri_path_outside_base(const anj_uri_path_t *path,
                                             const anj_uri_path_t *base) {
    if (path->uri_len < base->uri_len) {
        return true;
    }
    for (size_t i = 0; i < base->uri_len; ++i) {
        if (path->ids[i] != base->ids[i]) {
            return true;
        }
    }
    return false;
}

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);

#    ifdef __cplusplus
}
#    endif

#endif // ANJ_UTILS_H