/*****************************************************************************
 * file    Inc/wireless.h 
 * author  YuLiang
 * version 1.0.0
 * date    14-Jun-2022
 * brief   This file provides all the headers of wireless 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 __WIRELESS_H__
#define __WIRELESS_H__

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"

/* Define --------------------------------------------------------------------*/
#define WL_CRC32_LEN 4
#define WL_DMA_RX_BUF_LEN 1088
#define WL_DMA_TX_BUF_LEN 1088

#define WL_BUF_LIST_LEN 4

#define CSG_PKT_DATA_SUM 60

#define CSG_START_FLAG 0x68
#define CSG_END_FLAG 0x16

#define CSG_HEAD_LEN 10
#define CSG_CRC_LEN 1
#define CSG_OTHER_LEN 12

#define CSG_CONNECT_LEN 2
#define CSG_TIMING_LEN 0
#define CSG_DATA_LEN 26
#define CSG_DATA_FOLLOW_LEN 16
#define CSG_BEAT_LEN 8
#define CSG_SERVER_GET_LEN 12
#define CSG_CONFIG_GET_LEN 21
#define CSG_TIME_GET_LEN 6
#define CSG_ERROR_LEN 2

/* 共有命令字. */
enum CSG_CMD
{
    CSG_CONNECT = 0x00,                     // 开机联络.
    CSG_TIMING,                             // 校时.
    CSG_PASSWD_SET,                         // 设置密码.
    CSG_CONFIG_SET,                         // 主站下发参数配置.
    CSG_BEAT = 0x05,                        // 装置心跳信息.
    CSG_SERVER_SET,                         // 更改主站 IP\端口\卡号.
    CSG_SERVER_GET,                         // 查询主站 IP\端口\卡号.
    CSG_RESET,                              // 装置复位.
    CSG_CONFIG_GET = 0x0A,                  // 查询装置配置.
    CSG_TIME_GET = 0x0D,                    // 查询装置时间.
    CSG_DATA_GET = 0x21,                    // 主站请求装置数据.
    CSG_DATA = 0x44                         // 接地电流数据.
};

/* 共有命令字. */
enum CSG_DATA_TYPE
{
    CSG_DATA_TYPE_NONE = 0x00,              // 无请求数据.
    CSG_DATA_TYPE_HIS,                      // 请求历史数据.
    CSG_DATA_TYPE_REAL,                     // 请求实时数据.
};

/* Exported types ------------------------------------------------------------*/
/* 无线 buf 数组. */
typedef struct
{
    uint8_t buf[WL_BUF_LIST_LEN][WL_DMA_RX_BUF_LEN];
    uint16_t buf_cnt[WL_BUF_LIST_LEN];
    uint8_t valid;
    uint8_t current;
    uint16_t buf_addr;
} wl_buf_list_t;

/* 无线 buf 数组. */
typedef struct
{
    uint16_t cmd_buf_index;
    int16_t CSQ;
    uint8_t cmd_buf[WL_DMA_RX_BUF_LEN];
    uint8_t csg_buf[WL_DMA_RX_BUF_LEN];
    uint8_t dma_rx_buf[WL_DMA_RX_BUF_LEN];
    uint8_t dma_tx_buf[WL_DMA_TX_BUF_LEN];
    uint32_t time_send;
    uint32_t mul_idx;
    uint8_t state;
    uint8_t send_cnt;
    uint16_t wave_index;
    uint8_t energy_mode_old;
    uint8_t is_update;
    uint16_t update_len;
    int32_t update_rt;
    uint32_t keepalive;
    uint16_t pkt_id;
    uint8_t err_cnt;
    uint8_t data_request;
    uint32_t time_beat;
} wl_ctrl_t;

/* 南网协议头. */
typedef struct
{
    uint8_t flag;
    uint8_t dev_id[6];
    uint8_t cmd;
    uint16_t len;
} csg_head_t;

/* 南网协议头. */
typedef struct
{
    uint8_t year;
    uint8_t mon;
    uint8_t day;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
} csg_time_t;

/* 环流数据. */
typedef struct
{
    uint8_t Ciphertext[4];
    uint8_t flag;                   // 帧标识.
    uint8_t sum;                    // 包数
    uint8_t time[6];                // 采集时间, 年月日时分秒.
    uint16_t Ia;                    // CH1: A 相接地电流.
    uint16_t Ib;                    // CH2: B 相接地电流.
    uint16_t Ic;                    // CH3: C 相接地电流.
    uint16_t ground;                // CH4: 总接地电流.
    uint16_t rIa;                   // CH5: 运行电流.
    uint16_t rIb;
    uint16_t rIc;
} csg_data_t;

/* 环流数据. */
typedef struct
{
    uint16_t time;                  // 采集时间, 与第一包的时间差.
    uint16_t Ia;                    // CH1: A 相接地电流.
    uint16_t Ib;                    // CH2: B 相接地电流.
    uint16_t Ic;                    // CH3: C 相接地电流.
    uint16_t ground;                // CH4: 总接地电流.
    uint16_t rIa;                   // CH5: 运行电流.
    uint16_t rIb;
    uint16_t rIc;
} csg_data_follow_t;

typedef struct{
    uint8_t time[6];                // 时间, 年月日时分秒.
    uint8_t CSQ;                    // 信号强度.
    uint8_t vbat;                   // 电池电压  实际值乘 10.
} csg_beat_t;

typedef struct{
    uint8_t passwd_old[CSG_PASSWD_LEN];         // 原始密码.
    uint8_t passwd_new[CSG_PASSWD_LEN];         // 新密码.
} csg_passwd_t;

typedef struct{
    uint8_t passwd[CSG_PASSWD_LEN];
    uint8_t ip1[4];
    uint16_t port1;
    uint8_t ip2[4];
    uint16_t port2;
    uint8_t card1[6];
    uint8_t card2[6];
} csg_server_t;

typedef struct{

    uint8_t ip[4];
    uint16_t port;
    uint8_t card[6];
} csg_server_get_t;

/* Exported macro ----tm_structure.tm_sec --------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/
extern osThreadId_t WLHandle;
extern wl_buf_list_t wl_buf_list_rx;
extern wl_ctrl_t wl_ctrl;

/* Extern functions ----------------------------------------------------------*/
extern void wl_init_os(void);
extern void wl_show(void);
#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/