/*****************************************************************************
 * file    Inc/common.h 
 * author  YuLiang
 * version 1.0.0
 * date    10-Jun-2022
 * brief   This file provides all the headers of common function.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2022 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 WTOE 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 <stdint.h>
#include "cmsis_os.h"
#include "task.h"

/* Define --------------------------------------------------------------------*/
#define LD_DEBUG 1

#define TRUE 1
#define FALSE 0
#define OTHER 2

#define KEY_DEL 0x7f
#define KEY_BACK 0x08

#define IS_VALID_DEC(c)       (((c) >= '0') && ((c) <= '9'))
#define IS_CAP_LETTER(c)    (((c) >= 'A') && ((c) <= 'F'))
#define IS_LC_LETTER(c)     (((c) >= 'a') && ((c) <= 'f'))
#define ISVALIDHEX(c)       (IS_CAP_LETTER(c) || IS_LC_LETTER(c) || IS_VALID_DEC(c))

/* int16_t无效时的数据. */
#define INVALID_DATE_8 0x7F
/* uint16_t无效数据.*/
#define INVALID_DATA_U8 0xFF
/* int16_t无效时的数据. */
#define INVALID_DATE_16 0x7FFF
/* uint16_t无效数据.*/
#define INVALID_DATA_U16 0xFFFF
/* int32_t无效时的数据. */
#define INVALID_DATE_32 0x7FFFFFFF
/* uint32_t无效数据.*/
#define INVALID_DATA_U32 0xFFFFFFFF

/* ADC 通道数. */
#define CFG_ADC_CH_CNT 8                    // ADC 通道数.
/* ADC 校准点. */
#define CFG_ADC_ADJ_CNT 5

/* RS485 传感器数量. */
#define SENSOR_SUM 8
#define WAVE_SUM 4
#define POSITION_SUM 3

#define USART_BUF_SIZE 1088
#define DEBUG_DATA_SIZE 1024

#define MONITOR_ADCi_NOTIFY_OK 0x101
#define MONITOR_ADC_NOTIFY_OK 0x102
#define MONITOR_SEN_NOTIFY_OK 0x201

#define COM_WDG_COM 0x0001
#define COM_WDG_ADC 0x0002
#define COM_WDG_CLI 0x0004
#define COM_WDG_WIR 0x0008

#define CSG_CIPHERTEXT_LEN 4
#define CSG_PASSWD_LEN 4

#define BIT00 (0x01)
#define BIT01 (0x02)
#define BIT02 (0x04)
#define BIT03 (0x08)
#define BIT04 (0x10)
#define BIT05 (0x20)
#define BIT06 (0x40)
#define BIT07 (0x80)

#define BIT08 (0x0100)
#define BIT09 (0x0200)
#define BIT10 (0x0400)
#define BIT11 (0x0800)
#define BIT12 (0x1000)
#define BIT13 (0x2000)
#define BIT14 (0x4000)
#define BIT15 (0x8000)

#define BIT16 (0x00010000)
#define BIT17 (0x00020000)
#define BIT18 (0x00040000)
#define BIT19 (0x00080000)
#define BIT20 (0x00100000)
#define BIT21 (0x00200000)
#define BIT22 (0x00400000)
#define BIT23 (0x00800000)

#define BIT24 (0x01000000)
#define BIT25 (0x02000000)
#define BIT26 (0x04000000)
#define BIT27 (0x08000000)
#define BIT28 (0x10000000)
#define BIT29 (0x20000000)
#define BIT30 (0x40000000)
#define BIT31 (0x80000000)

/* Exported types ------------------------------------------------------------*/
/* debug模块号. */
typedef enum
{
    DBG_M_ADC = 0,
    DBG_M_4G,
    DBG_M_RS485_SEN,
    DBG_M_RS485_DEBUG,
    DBG_M_WAVE,
    DBG_M_POSITION,
    DBG_M_COUNT
} DBG_MODULE_E;

/* debug命令字. */
typedef enum
{
    DBG_CMD_ON = 0,
    DBG_CMD_OFF,
    DBG_CMD_OFF_ALL,
} DBG_CMD_E;

/* 公共监控任务标志. */
typedef enum
{
    COM_SYS_SAVE_CONFIG = 0,
    COM_SYS_SAVE_RECORD,
    COM_SYS_SAVE_INFO,
    COM_SYS_RESET,
    COM_SYS_SHUTDOWN,
} COM_SYS_FLAG_E;

/* 命令类型. */
enum DEBUG_CMD_TYPE
{
    DEBUG_CT_REQUEST = 1,
    DEBUG_CT_REPLY = 2,
    DEBUG_CT_PRV_REQUEST = 128,
    DEBUG_CT_PRV_REPLY = 129,
};

/* 共有命令字. */
enum DEBUG_CMD
{
    DEBUG_C_DEV_INFO = 1,
    DEBUG_C_DEV_INFO_SET = 2,
    DEBUG_C_RESET = 3,
    DEBUG_C_DEFAULT = 4,
    DEBUG_C_UPDATE_APP = 5,
    DEBUG_C_UPDATE_IAP = 6,
    DEBUG_C_DEV_CONFIG = 7,
    DEBUG_C_DEV_CONFIG_SET = 8,
    DEBUG_C_UPDATE_APP_RT = 9,
    DEBUG_C_KEEPALIVE = 10
};

/* 私有命令字. */
enum DEBUG_CM_CMD
{
    DEBUG_PRV_ADJ_INFO = 1,
    DEBUG_PRV_REAL_DATA = 2,
    DEBUG_PRV_POWER_FRE = 3,
    DEBUG_PRV_ADJ_INFO_SET = 4,
    DEBUG_PRV_ADJ_AUTO = 5,
    DEBUG_PRV_WAVE = 6,
    DEBUG_PRV_DEV_STATE = 7,
    DEBUG_PRV_WAVE_COL = 8,
    DEBUG_PRV_WAVE_CAL = 9,
    DEBUG_PRV_WAVE_MAX = 10,
    DEBUG_PRV_LOG = 11,
    DEBUG_PRV_HISTORY_DATA = 12,
    DEBUG_PRV_ADJ_MANUAL = 13,
    DEBUG_PRV_CSG_CONFIG = 14,
    DEBUG_PRV_CSG_CONFIG_SET = 15,
    DEBUG_PRV_POSITION_WAVE = 16,
    DEBUG_PRV_POSITION = 17
};

/* . */
typedef enum
{
    ADC_ENERGY_AUTO = 0,
    ADC_ENERGY_NORMAL,
    ADC_ENERGY_SLEEP,
    ADC_ENERGY_SLEEP_FORCE,
    ADC_ENERGY_CNT
} ADC_ENERGY_E;

typedef struct
{
    uint16_t len;
    uint8_t dev_type_m;
    uint8_t dev_type_s;
    uint32_t dev_id;
    uint8_t cmd_type;
    uint8_t cmd;
    uint16_t pkt_id;
    uint8_t reserve[8];
} proto_head_t;

typedef struct
{
    uint32_t index;
    uint32_t len;
} mul_head_t;

typedef struct
{
    uint16_t index;
    uint16_t len;
    uint32_t file_s;
    uint32_t file_ns;
} position_head_t;

/* bin报文头结构体. */
typedef struct
{
    uint8_t dev_type_m;
    uint8_t dev_type_s;
    uint8_t file_type;
    uint8_t version_m;
    uint16_t version_s;
    int8_t reserve[10];
    uint32_t crc32;
} bin_pkt_tail_t;

/* 串口 buf 数组. */
typedef struct
{
    uint8_t buf[USART_BUF_SIZE];
    uint16_t end;
    uint8_t is_half;                        // DMA收包一半的时候会产生中断, 这个标志用于过滤这给中断.
} usart_buf_t;

/* Exported macro ------------------------------------------------------------*/
/* 大小端网络序处理 */
#ifndef LITTLE_ENDIAN
    #define LITTLE_ENDIAN 1234
#endif

#ifndef BIG_ENDIAN
    #define BIG_ENDIAN 4321
#endif

#ifndef BYTE_ORDER
    #define BYTE_ORDER LITTLE_ENDIAN
#endif

#if BYTE_ORDER == BIG_ENDIAN
    #define iap_htons(x) (x)
    #define iap_ntohs(x) (x)
    #define iap_htonl(x) (x)
    #define iap_ntohl(x) (x)
    #define PP_HTONS(x) (x)
    #define PP_NTOHS(x) (x)
    #define PP_HTONL(x) (x)
    #define PP_NTOHL(x) (x)
#else /* BYTE_ORDER != BIG_ENDIAN */
    #ifndef iap_htons
        uint16_t iap_htons(uint16_t x);
    #endif
    #define iap_ntohs(x) iap_htons(x)

    #ifndef iap_htonl
        uint32_t iap_htonl(uint32_t x);
    #endif
    #define iap_ntohl(x) iap_htonl(x)

    /* These macros should be calculated by the preprocessor and are used
       with compile-time constants only (so that there is no little-endian
       overhead at runtime). */
    #define PP_HTONS(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8))
    #define PP_NTOHS(x) PP_HTONS(x)
    #define PP_HTONL(x) ((((x) & 0xff) << 24) | \
                        (((x) & 0xff00) << 8) | \
                        (((x) & 0xff0000UL) >> 8) | \
                        (((x) & 0xff000000UL) >> 24))
    #define PP_NTOHL(x) PP_HTONL(x)
#endif /* BYTE_ORDER == BIG_ENDIAN */

/* bitmap操作. */
#define MONITOR_BITMAP_SET(t, b)    ((t) |= 1 << (b))
#define MONITOR_BITMAP_RESET(t, b)  ((t) &= ~(1 << (b)))
#define IS_MONITOR_BIT_SET(t, b)    ((t) & (1 << b))

#define ERROR_PRINT(_rv_) vty_print_error(__FUNCTION__, __LINE__, _rv_)

#define HAL_E_RETURN(_f_) \
    do { \
        HAL_StatusTypeDef _rv_ = HAL_ERROR; \
        if ((_rv_ = _f_) != HAL_OK) \
        { \
            ERROR_PRINT(_rv_); \
            return _rv_; \
        } \
    } while(0)

#if LD_DEBUG
    #define DBG_BYTE_LEN 16
    extern int32_t dbg_cmd_hander(uint8_t cmd, uint8_t data);
    extern int32_t dbg_stat_get(uint8_t module);
    #define DBG(_MODULE_, _format_, _msg_...) \
        do { \
            if (dbg_stat_get(_MODULE_)) vty_print("%s(%d): " _format_, __FUNCTION__, __LINE__, ##_msg_); \
        } while(0)
#else
    #define DBG(_MODULE_, _msg_...)
#endif


/* Extern global variables ---------------------------------------------------*/
extern char *energy_str[ADC_ENERGY_CNT];

/* Extern functions ----------------------------------------------------------*/
extern void bit_list_set(uint8_t *bit_list, uint32_t bit);
extern int bit_list_tst(uint8_t *bit_list, uint32_t bit);
extern void bit_list_clr(uint8_t *bit_list, uint32_t bit);
extern void common_watchdog_set(uint32_t id);
extern void common_watchdog_clr(void);
extern int32_t common_watchdog_tst(uint32_t id);
extern int32_t common_watchdog_tst_all(void);
extern void common_watchdog_hw_refresh(void);
extern void notify_to_task(TaskHandle_t task);
extern void notify_to_task_form_val(TaskHandle_t task, uint32_t val);
extern void system_shtudown(uint32_t time);
extern uint8_t crc8(uint8_t *p, uint8_t len);
extern uint16_t crc16(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 vty_print(char *format, ...);
extern void vty_print_usart(char *format, ...);
extern void vty_print_error(char const *func, int line, int rv);
extern void buf_print(uint8_t *buf, int32_t len);
extern void buf_print_division(uint8_t *buf, int32_t len, uint16_t division);
extern void common_sys_init_os(void);
extern void common_sys_set(uint8_t flag, void *arg);
extern uint32_t abs_cal(int32_t a, int32_t b);
extern uint32_t abs_cal_u(uint32_t a, uint32_t b);
extern void rtc_time_set(uint32_t utc);
extern void restart_ontime(void);

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