/*
 * @Author: wzh
 * @Date: 2024-07-04 16:01:47
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-09-18 15:52:30
 * @Description:
 *
 * Copyright (c) 2024 by wzh, All Rights Reserved.
 */
#ifndef __ETK_DEF_H__
#define __ETK_DEF_H__
#include <math.h>   // fabs used
#include <stddef.h> // offsetof define in this
#include <stdint.h>
#include <stdio.h>

#define ETK_VERSION "1.0.0"

#define SYS_NONE_OS  0
#define SYS_FREERTOS 1
#define SYS_RTTHREAD 2
#define SYS_LINUX    3

#if (SYSTEM_TYPE == SYS_RTTHREAD)
#include <rtthread.h>
#endif


#define etk_container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))


#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#define __CLANG_ARM
#endif

/* Compiler Related Definitions */
#if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM Compiler */
#include <stdarg.h>
#define SECTION(x) __attribute__((section(x)))
#define ETK_UNUSED __attribute__((unused))
#define ETK_USED   __attribute__((used))
#define ALIGN(n)   __attribute__((aligned(n)))

#define ETK_WEAK __attribute__((weak))
#define ETK_DEPRECATED
#define etk_inline static __inline

#elif defined(__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
#include <stdarg.h>
#define SECTION(x) @x
#define ETK_UNUSED
#define ETK_USED  __root
#define PRAGMA(x) _Pragma(#x)
#define ALIGN(n)  PRAGMA(data_alignment = n)
#define ETK_WEAK  __weak
#define ETK_DEPRECATED
#define etk_inline static inline

#elif defined(__GNUC__) /* GNU GCC Compiler */
// #ifdef ETK_USING_NEWLIB
// #include <stdarg.h>
// #else
// /* the version of GNU GCC must be greater than 4.x */
// typedef __builtin_va_list __gnuc_va_list;
// typedef __gnuc_va_list    va_list;
// #define va_start(v, l) __builtin_va_start(v, l)
// #define va_end(v)      __builtin_va_end(v)
// #define va_arg(v, l)   __builtin_va_arg(v, l)
// #endif

#define SECTION(x)     __attribute__((section(x)))
#define ETK_UNUSED(x)  (void)x
#define ETK_USED       __attribute__((used))
#define ALIGN(n)       __attribute__((aligned(n)))
#define ETK_WEAK       __attribute__((weak))
#define ETK_DEPRECATED __attribute__((deprecated))
#define etk_inline     static __inline
#elif defined(__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
#include <stdarg.h>
#define SECTION(x) __attribute__((section(x)))
#define ETK_UNUSED __attribute__((unused))
#define ETK_USED   __attribute__((used))
#define ALIGN(n)   __attribute__((aligned(n)))
#define ETK_WEAK   __attribute__((weak))
#define ETK_DEPRECATED
#define etk_inline static inline
#elif defined(_MSC_VER)
#include <stdarg.h>
#define SECTION(x)
#define ETK_UNUSED
#define ETK_USED
#define ALIGN(n) __declspec(align(n))
#define ETK_WEAK
#define ETK_DEPRECATED __declspec(deprecated)
#define etk_inline     static __inline
#elif defined(__TI_COMPILER_VERSION__)
#include <stdarg.h>
/* The way that TI compiler set section is different from other(at least
 * GCC and MDK) compilers. See ARM Optimizing C/C++ Compiler 5.9.3 for more
 * details. */
#define SECTION(x)
#define ETK_UNUSED
#define ETK_USED
#define PRAGMA(x) _Pragma(#x)
#define ALIGN(n)
#define ETK_WEAK
#define ETK_DEPRECATED
#define etk_inline static inline
#else
#error not supported tool chain
#endif

// Convert value to string
#define ETK_STRINGIFY(x) #x
#define ETK_TOSTRING(x)  ETK_STRINGIFY(x)

/* maximum value of base type */
#define ETK_INT8_MAX   0x7f           /**< Maximum number of INT8 */
#define ETK_INT8_MIN   (-0x80)        /**< Minimum number of INT8 */
#define ETK_UINT8_MAX  0xff           /**< Maxium number of UINT8 */
#define ETK_UINT16_MAX 0xffff         /**< Maxium number of UINT16 */
#define ETK_INT16_MAX  0x7fff         /**< Maximum number of INT16 */
#define ETK_INT16_MIN  (-0x8000)      /**< Minimum number of INT16 */
#define ETK_UINT32_MAX 0xffffffff     /**< Maxium number of UINT32 */
#define ETK_INT32_MAX  0x7fffffff     /**< Maximum number of INT32 */
#define ETK_INT32_MIN  (-0x80000000)  /**< Minimum number of INT32 */
#define ETK_TICK_MAX   ETK_UINT32_MAX /**< Maxium number of tick */

// magic number used to check the validity of the object
#define ETK_MAGIC_NUMBER 0xDEADBEEF

// return value define
#define ETK_OK            0  /**< Success */
#define ETK_ERROR         -1 /**< General error */
#define ETK_FAIL          -2 /**< Operation failed */
#define ETK_TIMEOUT       -3 /**< Operation timeout */
#define ETK_QUEUE_FULL    -4 /**< Queue is full */
#define ETK_QUEUE_EMPTY   -5 /**< Queue is empty */
#define ETK_BUSY          -6 /**< Resource busy */
#define ETK_INVALID       -7 /**< Invalid parameter */
#define ETK_NOT_FOUND     -8 /**< Resource not found */
#define ETK_NO_MEMORY     -9 /**< Out of memory */
#define ETK_PERMISSION   -10 /**< Permission denied */
#define ETK_NOT_SUPPORT  -11 /**< Operation not supported */
#define ETK_ALREADY_EXISTS -12 /**< Resource already exists */

#endif                     // @page end