/*****************************************************************************
 * 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"
#include <QString>
#include <QByteArray>
#include <QDebug>

/* Define --------------------------------------------------------------------*/


typedef signed char int8_t;
typedef unsigned char   uint8_t;
typedef short  int16_t;
typedef unsigned short  uint16_t;
typedef int  int32_t;
typedef unsigned   uint32_t;

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

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

/* DAU 工频通道数. */
#define DAU_PORT_POWER_CNT 8
#define DAU_PORT_DEFECT_MAX 4
#define DAU_PORT_FAULT_MAX 4

/* ADC 通道数. */
#define CFG_ADC_CH_CNT 8
#define SENSOR_SUM 8
/* ADC 校准点. */
#define CFG_ADC_ADJ_CNT 5
#define ADC_COLLECT_CNT 2000                // ADC 每个通道采样次数.

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

#define USART_BUF_SIZE 1088
#define DEBUG_DATA_SIZE 1024
#define UPDATE_DATA_SIZE	DEBUG_DATA_SIZE

#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 COM_WDG_DAU 0x0010
#define COM_WDG_GPS 0x0020

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

#define DEV_CAPABILITY_LEN 16
#define DEV_CAPABILITY_BIT_LEN 128
#define DEV_PASSWORD_LEN 16
#define DEV_COMPILE_TIME_LEN 32
#define DEV_VERSION_STR_LEN 32
#define DEV_NAME_LEN 128
#define DEV_RESET_INFO_NUM 10
#define DEV_APN_LEN 16
#define DEV_APN_LEN_LONG 64

#define DEV_CONFIG_SIZE 1024
#define DEV_RECORD_SIZE 512


/* Exported types ------------------------------------------------------------*/
/* debug模块号. */
typedef enum
{
    DBG_M_ADC = 0,
    DBG_M_4G,
    DBG_M_RS485_SEN,
    DBG_M_RS485_DEBUG,
    DBG_M_DAU,
    DBG_M_DAU_TXRX,
    DBG_M_GPS,
    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 = 121,
    DEBUG_CT_PRV_REPLY = 122,
};

/* 共有命令字. */
enum DEBUG_CMD
{
    DEBUG_C_CONTACT = 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,
    DEBUG_C_TIME = 11
};

/* 私有命令字. */
enum DEBUG_CM_CMD
{
    DEBUG_PRV_REALDATA = 1,
    DEBUG_PRV_POWER = 2,
    DEBUG_PRV_DEFECT = 3,
    DEBUG_PRV_REALDATA_FUALT = 4,
    DEBUG_PRV_POWER_FAULT = 5,
    DEBUG_PRV_FAULT = 6,
    DEBUG_PRV_ADJ_INFO = 7,
    DEBUG_PRV_ADJ_INFO_SET = 8,
    DEBUG_PRV_ADJ_AUTO = 9,
    DEBUG_PRV_DEV_STATE = 10,
    DEBUG_PRV_WAVE_COL = 11,
    DEBUG_PRV_WAVE_CAL = 12,
    DEBUG_PRV_WAVE_MAX = 13,
    DEBUG_PRV_LOG = 14,
    DEBUG_PRV_HISTORY_DATA = 15,
    DEBUG_PRV_ADJ_MANUAL = 16,
    DEBUG_PRV_CSG_CONFIG = 17,
    DEBUG_PRV_CSG_CONFIG_SET = 18,
    DEBUG_PRV_POSITION_WAVE = 19,
    DEBUG_PRV_POSITION = 20
};

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

enum WaveStateE
{
    WAVE_STATE_IDLE = 0,
    WAVE_STATE_START,
    WAVE_STATE_WAIT,
    WAVE_STATE_READ,
    WAVE_STATE_STOP,
    WAVE_STATE_ADC_ERROR,
    WAVE_STATE_TIMEOUT,
    WAVE_STATE_COMPLETION = 255
};
//Q_ENUM(WaveStateE);

/* 状态机状态. */
enum  WlStateE
{
    WL_STATE_SOFT = 0,
    WL_STATE_AT_ATE,
    WL_STATE_AT_QICSGP,
    WL_STATE_AT_QIACT,
    WL_STATE_AT_CSQ,
    WL_STATE_AT_QICFGTS,
    WL_STATE_AT_QICFGWT,
    WL_STATE_AT_QIOPEN,
    WL_STATE_WAKEUP = 128,
    WL_STATE_REALDATA,
    WL_STATE_PWR_FRE,
    WL_STATE_UPDATE,
    WL_STATE_UPDATE_RT,
    WL_STATE_WAVE,
    WL_STATE_WAVE_MAX,
    WL_STATE_KEEPALIVE,
    WL_STATE_END = 255
};
//Q_ENUM(WlStateE);


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
{
    /* 设备信息. 36byte */
    uint8_t typeM;
    uint8_t typeS;
    uint8_t mac[6];
    uint32_t id;
    uint8_t ip[4];
    uint8_t ipMask[4];
    uint8_t ipGw[4];
    uint8_t serverIp[4];
    uint8_t wirelessServerIp[4];
    uint16_t serverPort;
    uint16_t wirelessServerPort;
    /* 版本信息. 224byte */
    uint8_t bootVersion[DEV_VERSION_STR_LEN];
    uint8_t bootCompile_time[DEV_COMPILE_TIME_LEN];
    uint8_t imgVersion[DEV_VERSION_STR_LEN];
    uint8_t imgCompile_time[DEV_VERSION_STR_LEN];
    uint8_t hardwareVersion[DEV_VERSION_STR_LEN];
    uint8_t fpgaVersion[DEV_COMPILE_TIME_LEN];
    uint8_t factoryTime[DEV_COMPILE_TIME_LEN];
    /* 设备其他信息. 128byte */
    uint8_t name[DEV_NAME_LEN];
    uint8_t csgId[8];
    uint16_t csgVersion;
    uint8_t csgCard[6];
} devInfo;

/* RS485 调试通讯协议配置信息. */
typedef struct
{
    /* 设备配置. 12byte */
    uint16_t flag;
    uint8_t energyMode;
    uint8_t isVoltageCol;
    uint8_t sensor_id[SENSOR_SUM];
    uint16_t collectInterval;
    uint16_t waveInterval;
    uint16_t collectThreshold;
    uint16_t waveThreshold;
    uint8_t mainCable;
    uint8_t normalSleep;
    uint8_t isTempCol;
    uint8_t isWaveCol;
    uint8_t APN[DEV_APN_LEN];
    uint8_t APN_long[DEV_APN_LEN_LONG];
} devConfig;

/* 南网配置信息. */
typedef struct
{
    uint8_t passwd[CSG_PASSWD_LEN];
    uint8_t beatInterval;
    uint8_t resetTime[3];
    uint16_t collectInterval;
    uint16_t sleepTime;
    uint16_t onlineTime;
    uint8_t reserve[2];
    uint8_t ciphertext[CSG_CIPHERTEXT_LEN]; // 实时数据认证密文.
} csgConfig_t;

/* 实时数据信息. */
typedef struct
{
    int16_t temperature;                // 设备温度, 单位: 0.1℃.
    uint16_t vbat;                      // 电池电压, 单位: mv.
    int16_t CSQ;                        // 4G 信号质量 > -51dBm 统一显示 -51dBm, < -113 dBm 为无信号.
    uint16_t vin;                       // 工作电压, 单位: mv.
    uint32_t elec[CFG_ADC_CH_CNT];      // 通道电流有效值 mA.
    uint16_t vsc;                       // 超级电容电压, 单位: mv.
    int16_t sen_temp[SENSOR_SUM];       // 传感器温度: 单位: 0.1℃.
    uint16_t senX[SENSOR_SUM];          // 传感器震动 x 轴.
    uint16_t senY[SENSOR_SUM];          // 传感器震动 y 轴.
    uint16_t senZ[SENSOR_SUM];          // 传感器震动 z 轴.
    uint8_t energyMode;                 // 节能模式.
    uint8_t senShort;                   // 传感器是否短路, 0 – 没有短路, 1 – 短路.
    uint8_t senValid[SENSOR_SUM];       // 传感器数据是否有效, 0 - 无效, 1 - 有效.
} realData_t;

/* 设备状态信息. */
typedef struct
{
    uint8_t energyMode;                 // 节能模式.
    uint8_t isAdcCollect;               // ADC 采集完成.
    uint8_t isAdcUp;                    // ADC 上传完成.
    uint8_t AdciCnt;                    // ADCi 错误计数.
    uint8_t AdciState;                  // ADCi 状态.
    uint8_t AdcCnt;                     // ADC 错误计数.
    uint8_t AdcState;                   // ADC 状态.
    uint8_t waveState;                  // 高频录波状态.
    uint32_t waveTime;                  // 高频录波开始时间.
    uint8_t waveStart;                  // 高频录波完成.
    uint8_t waveUpStart;                // 高频录波上传完成.
    uint16_t waveIdx;                   // 高频录波续传索引.
    uint8_t waveRt;                     // 高频录波结果.
    uint8_t waveForce;                  // 是否强制录波.
    uint8_t wlState;                    // 4G 状态.
    uint8_t wlCnt;                      // 4G 错误计数.
    uint16_t wlWaveIdx;                 // 4G 上传录波文件索引.
    uint8_t isWlUpdate;                 // 4G 升级.
    uint8_t isAdcthr;                   // 是否超过工频阈值.
    uint8_t forceSleepUp;               // 强制睡眠报文是否上传.
    uint8_t isBatCharge;                // 电池充电状态.
    uint16_t waveAdj;                   // 高频校准系数.
    int16_t waveMin;                    // 高频采样最大值.
    int16_t waveMax;                    // 高频采样最小值.
    uint32_t sysTime;                   // 系统运行时间.
    uint16_t scMax;                     // 超级电容最大值.
    uint16_t scMin;                     // 超级电容最小值.
    uint32_t scMaxTime;                 // 超级电容最大值时间.
    uint32_t scMinTime;                 // 超级电容最小值时间.
    uint32_t scUpTime;                  // 超级电容充电时间.
    uint32_t scDownTime;                // 超级电容放电时间.
} devState;

/* 高频校准信息. */
typedef struct
{
    /* 高频校准. 4 */
    uint16_t param;
    uint8_t reserve[2];
} waveParam_t;



/* Exported macro ------------------------------------------------------------*/

/* dev_config flag 类型号. */
enum DevConfigFlag
{
    DEV_FLAG_CLI = 0,   // 是否开启命令行模式.
    DEV_FLAG_ADJ,       // 是否开启校准模式.
    DEV_FLAG_FACTORY    // 工厂持续采样模式.
};

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


//QByteArray data
#define HexPrint(func, packet, len) \
do{\
	QByteArray data((const char *)packet, len); \
    QString hexString; \
    qDebug() << func << ":\n"; \
    for (int i = 0; i < data.size(); i++) { \
        hexString += QString("%1 ").arg((quint8)data.at(i), 2, 16, QChar('0')); \
    } \
    qDebug() << hexString; \
}while (0);


/* Extern global variables ---------------------------------------------------*/

/* Extern functions ----------------------------------------------------------*/
extern uint32_t crc32_update(uint32_t crc, char* buf, uint32_t len);
extern void crc32_table_init(void);
extern uint32_t crc32(void const *buf, uint32_t byte_count);
extern uint32_t crc32_update(uint32_t crc, char* buf, uint32_t len);
extern uint16_t crc16(uint8_t *data, uint16_t size);
int32_t stringToChar(QString str, char *buf, uint32_t len);
void printHexArray(const char *msg, const char *array, int length);
#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/
