/******************************************************************************
 * file    include/common.h 
 * author  YuLiang
 * version 1.0.0
 * date    10-Sep-2021
 * brief   This file provides all the headers of the common functions.
 ******************************************************************************
 * Attention
 *
 * 
© COPYRIGHT(c) 2021 LandPower
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of LandPower nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/
#ifndef _COMMON_H_
#define _COMMON_H_
/* Includes ------------------------------------------------------------------*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "array.h"
#include "better_log.h"
#include "dbg.h"
#include "memory.h"
#include "thread_monitor.h"
/* Define --------------------------------------------------------------------*/
#define FALSE 0
#define TRUE 1
#define E_NONE                      0
#define E_ERROR                     -1
#define E_BAD_PARAM                 -2
#define E_MEM                       -3
#define E_SYS_CALL                  -4
#define E_NULL                      -5
#define E_NOT_FOUND                 -6
#define E_NOT_IDENTIFY              -7
#define E_TIMEOUT                   -8
#define E_SAME                      -9
#define OUT
#define BACKTRACE_SIZE 20
#define TIME_STR_LEN 64
#define RECV_MSG_LEN 1536
#define DEVICE_INVALID 255
#define FILE_NAME_LEN 128
#define DIR_PATH_LEN 512
#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46
#define ETH_GIS_DEV_ID_LEN 16
#define ETH_GIS_CMD_PARAM_LEN 16
#define DEVICE_ID_LEN 6
#define MAC_ADDR_LEN 6
#define IP_ADDR_LEN 4
#define DEV_DATA_LEN 1000
#define DEV_DATA_BITMAP_LEN 8
#define DEV_NAME_STR_LEN 32
/* Does the I/O error indicate that the operation should be retried later? */
#define ERRNO_IO_RETRY(EN) \
    (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
/* Exported types ------------------------------------------------------------*/
typedef int8_t bool;
typedef void* (*thread_func_t)(void*);
typedef struct
{
    void *arg;
    int priority;
    int log_module;
    char *thread_name;
} thread_param_t;
/* Exported macro ------------------------------------------------------------*/
#define BITMAP_SET(t, b)        ((t) |= 1 << (b))
#define BITMAP_RESET(t, b)      ((t) &= ~(1 << (b)))
#define IS_BITMAP_SET(t, b)     ((t) & (1 << b))
#define LD_E_RETURN(_module_, _f_) \
    do { \
        int _rv_ = E_ERROR; \
        if ((_rv_ = _f_) != E_NONE) \
        { \
            DBG(_module_, "ERROR return %d!\r\n", _rv_); \
            return _rv_; \
        } \
    } while(0)
#define LD_NULL_RETURN(_module_, _f_) \
    do { \
        if (NULL == (_f_)) \
        { \
            DBG(_module_, "ERROR return %d!\r\n", _rv_); \
            return E_NULL; \
        } \
    } while(0)
/* Extern global variables ---------------------------------------------------*/
/* Extern functions ----------------------------------------------------------*/
extern char *str_to_lower(char *str);
extern char *str_omit_space(char *str, bool omit_end);
extern size_t time_string(int32_t timestamp_precision, char *buf, size_t buflen);
extern const char *safe_strerror(int errnum);
extern void buf_print(char *buf, int32_t len);
extern void buf_print_16(uint16_t *buf, int32_t len);
extern int32_t mac_generate_from_ip(uint32_t ip, uint8_t *mac);
extern uint16_t crc16(uint8_t *data, uint16_t size);
extern uint16_t crc16_modbus(uint8_t *data, uint16_t size);
extern unsigned int crc32(unsigned int crc, char *buf, unsigned long len);
extern void speed_detection_stat(void);
extern void speed_detection_end(void);
extern int printh(const char *format, ...);
extern char* version_get();
extern char* version_date_get();
extern uint16_t sofrware_version_get(void);
extern int32_t str_to_mac(char *mac_str, OUT uint8_t *mac);
extern int32_t str_to_id(char *id_str, OUT uint32_t *id);
extern int32_t bitmap_set(uint8_t *buf, int32_t nbit, int32_t buf_len);
extern int32_t bitmap_reset(uint8_t *buf, int32_t nbit, int32_t buf_len);
extern int32_t is_bitmap_set(uint8_t *buf, int32_t nbit, int32_t buf_len);
extern int32_t time_str_to_long(char *date, char *time, uint32_t *t);
extern uint16_t version_str_to_int(void);
extern void time_set(time_t timestamp);
extern int32_t create_thread(thread_func_t func, thread_param_t *pParam);
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/