/******************************************************************************
 * 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
 *
 * <h2><center>&copy; COPYRIGHT(c) 2021 LandPower</center></h2>
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>

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

/* 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))

typedef enum _BOOT_MSG
{
    BOOT_NONE = 0,
    BOOT_SYSTEM_RESET,
    BOOT_SYSTEM_LOGOUT,
    BOOT_REMOTE_CFG_SET,
    BOOT_REMOTE_RESET,
    BOOT_REMOTE_UPDATE,
    BOOT_FILE_RECOVER,
    BOOT_4G_ERR,
    BOOT_CONNECT_ERR,
    BOOT_TIMER,
    BOOT_MAX,
} BOOT_MSG;

typedef struct 
{
    BOOT_MSG type;  // 重启类型
    char *msg;      // 重启日志
} boot_msg_t;

#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 int32_t mac_generate_from_ip(char *ip_str, 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 void crc32_table_init(void);
extern uint32_t crc32(void const *buf, uint16_t byte_count);
extern uint32_t crc32_update(uint32_t crc, char* buf, uint32_t 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 void reboot_system(int module, BOOT_MSG type);

#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/
