/*****************************************************************************
 * file    include/ca_coll.h 
 * author  YuLiang
 * version 1.0.0
 * date    28-Nov-2023
 * brief   This file provides all the headers of the collect functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2023 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 __CA_COLL_H__
#define __CA_COLL_H__

#ifdef CFG_DEV_TYPE_LAND_CA
/* Includes ------------------------------------------------------------------*/

/* Define --------------------------------------------------------------------*/
#define CA_COLL_ADC_CH_SUM 8
#define CA_COLL_SENSOR_SUM 8
#define CA_COLL_VERSION_LEN 32
#define CA_COLL_COMPILE_LEN 32
#define CA_COLL_NAME_LEN 128
#define CA_COLL_USART_NAME_LEN 64
#define CA_COLL_ERR_CNT_MAX 9
#define CA_COLL_VPN_LEN 64

/* Exported types ------------------------------------------------------------*/
/* 环流设备信息结构体 */
typedef struct
{
    /* 设备信息. 36byte */
    uint8_t type_m;
    uint8_t type_s;
    uint8_t mac[6];
    uint32_t id;
    uint8_t ip[4];
    uint8_t ip_mask[4];
    uint8_t ip_gw[4];
    uint8_t server_ip[4];
    uint8_t wireless_server_ip[4];
    uint16_t server_port;
    uint16_t wireless_server_port;
    /* 版本信息. 224byte */
    uint8_t boot_version[CA_COLL_VERSION_LEN];
    uint8_t boot_compile_time[CA_COLL_COMPILE_LEN];
    uint8_t img_version[CA_COLL_VERSION_LEN];
    uint8_t img_compile_time[CA_COLL_COMPILE_LEN];
    uint8_t hardware_version[CA_COLL_VERSION_LEN];
    uint8_t FPGA_version[CA_COLL_VERSION_LEN];
    uint8_t factory_time[CA_COLL_COMPILE_LEN];
    /* 设备其他信息. 128byte */
    uint8_t name[CA_COLL_NAME_LEN];
} ca_coll_dev_info_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[CA_COLL_ADC_CH_SUM];      // 通道电流有效值 mA.
    uint16_t vsc;                           // 超级电容电压, 单位: mv.
    uint16_t sen_temp[CA_COLL_SENSOR_SUM];  // 传感器温度: 单位: 0.1℃.
    uint16_t sen_x[CA_COLL_SENSOR_SUM];     // 传感器震动 x 轴.
    uint16_t sen_y[CA_COLL_SENSOR_SUM];     // 传感器震动 y 轴.
    uint16_t sen_z[CA_COLL_SENSOR_SUM];     // 传感器震动 z 轴.
    uint8_t energy_mode;                    // 当前设备工作模式.
    uint8_t sen_short;                      // 传感器是否短路, 0 – 没有短路, 1 – 短路.
    uint8_t sen_valid[CA_COLL_SENSOR_SUM];  // 传感器数据是否有效, 0 - 无效, 1 - 有效.
    uint32_t run_time;                      // 设备运行时长, 单位 s.    
    uint8_t fre_valid;                      // 工频数据有效, 有线协议使用.
    uint8_t wave_valid;                     // 高频数据有效, 有线协议使用.
    uint8_t reserve[2];
} ca_coll_dev_data_t;

/* 环流配置结构体. */
typedef struct
{
    uint16_t interval;                  // 环流采集间隔, 单位: m
    uint16_t threshold;                 // 工频采样阈值, 单位: A
    uint16_t wave_threshold;            // 高频采样阈值, 单位: mv
    uint8_t wave_interval;              // 高频采样间隔, 单位: h
    uint8_t main_cable;                 // 主缆 ID (0-6) 默认 5
    uint8_t normal_sleep;               // 正常模式是否休眠: 0 - 否 1 - 是默认 0
    uint8_t is_voltage_col;             // 是否开启电流采集
    uint8_t is_temp_col;                // 是否开启温度震度
    uint8_t is_wave_col;                // 是否开启高频录波
} ca_coll_dev_cfg_t;

/* 设备配置结构体. */
typedef struct
{
    int fd;                             // 打开的文件描述符
    char dev[CA_COLL_USART_NAME_LEN];   // 串口驱动位置
    int32_t baud;                       // 波特率
    int8_t bits;                        // 几位数据位
    char parity;                        // 奇偶校验位
    int8_t stop;                        // 几位停止位
    int8_t is_4G;                       // 4G 是否使能
    int32_t coll_inr;                   // 采集间隔, 单位: s
    char apn[CA_COLL_VPN_LEN];          // VPN
    int8_t is_MQTT;                     // 是否打开 MQTT
    int8_t is_land;                     // 是否打开协议
    uint16_t land_port;                 // 私有协议端口
    char land_ip[INET_ADDRSTRLEN];      // 私有协议地址
} ca_coll_cfg_t;

/* 设备状态结构体. */
typedef struct
{
    int fd_read;                        // 环流升级文件描述符
    uint32_t send_err_cnt;              // 环流发送失败计数
    uint32_t update_index;              // 环流升级报文索引
    bool is_update_cable;               // 环流设备升级中
    bool is_4g_connect;                 // EC20 是否拨号成功
} ca_coll_state_t;

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

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

/* Extern functions ----------------------------------------------------------*/
extern int32_t ca_coll_init(void);
extern int32_t ca_coll_init_after(void);
extern ca_coll_cfg_t* ca_coll_cfg_get(void);
extern ca_coll_dev_info_t* ca_coll_cable_info_get(void);
extern ca_coll_dev_cfg_t* ca_coll_cable_cfg_get(void);
extern ca_coll_dev_data_t* ca_coll_cable_data_get(void);
extern ca_coll_state_t* ca_coll_cable_state_get(void);
extern void ca_coll_cable_update_set(bool enable);
extern void ca_coll_show_dev(void);
extern void ca_coll_show_dev_data(void);
extern void ca_coll_show_dev_state(void);
#endif
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/

