/*****************************************************************************
* file include/pd_dau.h
* author YuLiang
* version 1.0.0
* date 03-Feb-2023
* brief This file provides all the headers of the dau functions.
******************************************************************************
* Attention
*
*
© COPYRIGHT(c) 2021 LandPower
*
* 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 __PD_DAU_H__
#define __PD_DAU_H__
#ifdef CFG_DEV_TYPE_LAND_PD
/* Includes ------------------------------------------------------------------*/
#include
#include
#include
#include "pd_main.h"
#include "common.h"
/* Define --------------------------------------------------------------------*/
#define DAU_ETH_SLOTS_SUM 4
#define RS485_SLOTS 2
#define DAU_BUF_SIZE 1536
#define UHF "UDP_UHF"
#define HF "UDP_HF"
#define ULTRASONIC "UDP_ULTRASONIC"
#define IRONCORE "RS485_IRONCORE"
/* Exported types ------------------------------------------------------------*/
typedef int32_t (*dau_recv_cb)(uint8_t, char*, uint16_t);
typedef void (*dau_recv_fun_cb)(uint8_t, char*, uint16_t);
/* 命令类型. */
enum DAU_CMD_TYPE
{
DAU_REQUEST = 1,
DAU_REPLY = 2,
DAU_PRV_REQUEST = 121,
DAU_PRV_REPLY = 122
};
/* 共有命令字. */
enum DAU_CMD
{
DAU_C_CONTACT = 1,
DAU_C_ADD_DAU = 2,
DAU_C_RESET = 3,
DAU_C_UPDATE = 5,
DAU_C_DEV_INFO_SET = 6,
DAU_C_DEV_INFO_GET = 7,
DAU_C_UPDATE_RESULT = 9,
DAU_C_HEARTBEAT = 10,
DAU_C_MAX
};
/* 私有命令字. */
enum DAU_P_CMD
{
DAU_P_CONFIG_SET = 1, // 设备全局参数设置
DAU_P_CONFIG_GET = 2, // 设备全局参数获取
DAU_P_CONFIG_PORT_SET = 3, // 设备端口参数设置
DAU_P_CONFIG_PORT_GET = 4, // 设备端口参数获取
DAU_P_CONFIG_REAL_WAVE = 5, // 实时波形配置
DAU_P_TREND = 10,
DAU_P_REAL_PRPS = 11,
DAU_P_EVENT = 12,
DAU_P_MAX
};
// 板卡类型枚举
typedef enum {
DAU_TYPE_UDP,
DAU_TYPE_RS485,
DAU_TYPE_NONE
} DauType;
// 板卡状态枚举
typedef enum {
DAU_STATE_DISCONNECTED,
DAU_STATE_CONNECTED,
DAU_STATE_REGISTERED
} DauState;
// 通信回调函数类型
//typedef void (*data_recv_callback)(void* priv_data, const void* data, size_t len);
//typedef void (*data_send_callback)(void* priv_data, void* data, size_t len);
// 板卡私有数据结构
typedef struct
{
DauType type;
union
{
struct
{
int sockfd;
struct sockaddr_in addr;
} udp;
struct
{
int fd;
char port[20];
struct termios options;
} rs485;
} comm;
char board_id[32];
void *rx_buffer;
size_t buffer_size;
uint8_t cmd_type;
uint8_t cmd;
} dau_private_data_t;
// 板卡操作回调函数结构
typedef struct
{
int (*init)(dau_private_data_t *data, const char *config);
int (*receive)(dau_private_data_t *data, void *buf, size_t len);
int (*transmit)(dau_private_data_t *data, const void *buf, size_t len);
void (*cleanup)(dau_private_data_t *data);
} dau_operations_t;
// UDP客户端信息
typedef struct {
struct sockaddr_in addr;
char board_id[32];
} udp_client_data;
// RS485设备信息
typedef struct {
int fd; // 串口文件描述符
char board_id[32];
uint8_t address; // 从机地址
} rs485_device_data;
/* 报文头初始化结构体 */
typedef struct
{
uint8_t cmd_type;
uint8_t cmd;
uint16_t pkt_id;
char *pkt;
uint32_t len;
} dau_head_init_t;
/* 应答报文 */
typedef struct
{
uint8_t result; // 应答结果. 0:成功 1:失败
uint8_t reserved[3]; // 保留
} dau_ack_t;
/* 报文头 */
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 version;
uint8_t reserve1[2];
uint8_t slot;
uint32_t sdev_id;
uint8_t reserve2[12];
} dau_pkt_head_t;
/* 开机联络 */
typedef struct
{
uint8_t type_m; // 主设备号
uint8_t type_s; // 次设备号
uint8_t reserved1[2]; // 保留
uint32_t dev_id; // 设备ID
char hostname[FILE_NAME_LEN]; // 设备名 128byte
uint32_t factory_date; // 出厂日期.
uint32_t deployment_date; // 部署日期.
uint8_t app_version[DEV_VERSION_STR_LEN]; // 软件版本
uint8_t app_compile_time[DEV_VERSION_STR_LEN]; // 软件编译时间
uint8_t hardware_version[DEV_VERSION_STR_LEN]; // 硬件版本
uint8_t FPGA_version[DEV_VERSION_STR_LEN]; // fpga版本
uint32_t ip; // 本机 IP.
uint32_t mask; // 本机 MASK.
uint32_t gw; // 本机网关
uint8_t mac[MAC_ADDR_LEN]; // MAC地址.
uint16_t server_port; // 服务器端口号.
uint32_t server_ipv4; // 服务器 IP.
uint8_t port[PD_PORT_SUM];
uint8_t port_type[PD_PORT_SUM];
} dau_contact_t;
/* 心跳报文. */
typedef struct
{
float freq; // 实测同步源工频频率.
uint8_t dau_state[4]; // 采集模块的状态.
uint8_t out_sync; // 外接调频同步 1: 有效 0: 无效.
uint8_t pt_sync; // PT同步 1: 有效 0: 无效.
uint8_t in_sync; // 内同步 1: 有效 0: 无效.
uint8_t reserved1; // 预留 字节对齐.
uint8_t port_link_alarm[PD_PORT_SUM]; // 通道传感器连接状态 断线告警.
uint8_t dau_port_num[4]; // 采集板端口数量.
} dau_heartbeat_t;
/* 时间头 */
typedef struct
{
uint16_t index; // 包索引
uint16_t sum; // 总包数
uint8_t vport; // 通道号
uint8_t boosterpack; // 是否补包 0:否 1:是
int16_t max; // 通道的最大值.
uint32_t power_fre; // 工频周期.
uint8_t type; // 事件类型.
uint8_t reserved[3]; // 保留
uint32_t identifier; // 数据编号: 0 - (2^32-1) 循环.
uint32_t utc; // UTC 时标.
uint32_t cnt; // 通道每秒脉冲计数值.
uint16_t avg_o; // 通道原始信号 (降噪前) 的平均值.
uint16_t avg; // 脉冲平均值.
uint32_t point_cnt; // 数据累计点数
uint32_t len; // 当前包长度
} dau_event_head_t;
// 板卡管理器结构
typedef struct
{
//dau_private_data_t *private_data;
dau_operations_t ops;
// udp
int sockfd;
struct sockaddr_in addr;
// rs485
int fd;
char port[20];
struct termios options;
char board_id[32];
void *rx_buffer;
size_t buffer_size;
uint8_t cmd_type;
uint8_t cmd;
pthread_t thread_id;
int slot;
bool occupied; // 槽位占用标志
} dau_manager_t;
/* DAU 状态结构体 */
typedef struct
{
uint8_t is_insert:1;
uint8_t is_connect:1;
} dau_state_t;
/* DAU 全局结构体 */
typedef struct
{
uint8_t slot; // dau slot
DauType type;
int fd; // dau socket fd
struct sockaddr_in server; // dau client ip
char buf_recv[DAU_BUF_SIZE]; // dau 收包 buf
DauState stat;
void *private_data;
dau_contact_t info; // dau 信息
dau_heartbeat_t run_status; // dau 运行状态
dau_state_t state; // dau 状态
dau_recv_cb recv_cb; // dau 收包处理函数
} dau_t;
/* Exported macro ------------------------------------------------------------*/
#define SET_BIT(REG, BIT) ((REG) |= (BIT))
#define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT))
#define READ_BIT(REG, BIT) ((REG) & (BIT))
#define WRITE_REG(REG, VAL) ((REG) = (VAL))
#define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((REG) & (~(CLEARMASK))) | (SETMASK)))
#define DAU_REG_PORT_ADDR_GET(port) ((port + 1) << 8)
/* Extern global variables ---------------------------------------------------*/
extern dau_t daus[PD_SLOTS_MAX];
/* Extern functions ----------------------------------------------------------*/
extern int32_t dau_handle_init(void);
extern int32_t dau_handle_init_after(void);
extern void dau_send_data(dau_t *dau, dau_head_init_t *head_data);
#endif
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/