/*************************************************************************
  File name     : util.h
  Module        : common
  Author        : 
  Copyright     : 
  Version       : 0.1
  Created on    : 2022-08-18
  Creator       : amir.liang
  Description   :
      utils
  Modify History:
  1. Date:        Author:         Modification:
***************************************************************************/

#ifndef __UTIL_H__
#define __UTIL_H__

#include <stdint.h>



#ifndef OK
#define OK (uint8_t)(0)
#endif
#ifndef NG
#define NG (uint8_t)(1)
#endif
#ifndef MAX_PATH
#define MAX_PATH (256)
#endif
#ifndef MIN
#define MIN(x,y) (((x)<(y))?(x):(y))
#endif

#ifndef MAX
#define MAX(x,y) (((x)>(y))?(x):(y))
#endif

#define SET_BITS(value, bits) ((value) |  (bits))

#define CLEAR_BITS(value, bits) ((value) & ~(bits))

#define CHECK_BITS(value, bits) ((value) &  (bits))

#define TOGGLE_BITS(value, bits) ((value) ^  (bits))

// note: 'a' must be align 2
#define ROUNDUP(x, a) (((x) + (a)-1) & ~((a)-1)) // ALIGN(x, a)

#define CHECK_SAME(a, b) ({\
    if ( a != b ){\
        hloge("%s [%d]!=[%d], fail.", __func__, a, b); \
        }; a==b;})

#define CHECK_SAME_RETURN(a, b) ({if (!CHECK_SAME(a, b) ){ return NG;}})


#define CHECK_NULL(who, val) ({if(!val){hloge("%s %s is null param", __func__, who);}; val;})

#define CHECK_NULL_RETURN(who, val) if (!CHECK_NULL(who, val)){return NULL;}

#define CHECK_LESS_RETURN(who, val) if(who<val){hloge("%s %d<%d", __func__, who, val); return NG;}


#ifndef ARRAY_SIZE
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
#endif



#define DECLAR_UT_CMD(FUNC, ARRAY, CMD, USAGE) \
    {FUNC, ARRAY, ARRAY_SIZE(ARRAY), CMD, USAGE}

typedef struct unittest_st unittest_cmd;

typedef struct unittest_st
{
    int32_t  (*exec_func)(int32_t argc, char **argv); /* 如果不为NULL 即为终函数 */
    unittest_cmd *next_cmd;
    uint16_t next_cmd_cnt;
    char *cmd_name;
    char *cmd_help; /* 如果exec_func=NULL 即 一个'cmd_name'提示help*/
} unittest_cmd;


const char *get_filename(char *path);

uint8_t file_exist(const char* path);

uint8_t isIP(const char*ip);

#endif


