diff --git a/app/include/pd_dau.h b/app/include/pd_dau.h index 4835d84..448c945 100755 --- a/app/include/pd_dau.h +++ b/app/include/pd_dau.h @@ -42,29 +42,18 @@ #include #include -#include "pd_main.h" #include "common.h" +#include "pd_main.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 int32_t (*dau_send_cb)(uint8_t, uint8_t, void*); -typedef void (*dau_recv_fun_cb)(uint8_t, char*, uint16_t); -typedef void (*dau_send_fun_cb)(uint8_t, void*); +typedef void (*dau_data_free_cb)(uint8_t); /* 命令类型. */ enum DAU_CMD_TYPE @@ -124,6 +113,8 @@ enum DAU_SEND_TYPE typedef struct { uint32_t type; + uint8_t slot; + uint8_t reserve[3]; void *data; } dau_send_msg_t; @@ -190,6 +181,7 @@ typedef struct { uint8_t is_insert:1; uint8_t is_connect:1; + uint16_t beat_cnt; } dau_state_t; /* DAU 全局结构体 */ @@ -205,18 +197,12 @@ typedef struct dau_state_t state; // dau 状态 dau_recv_cb recv_cb; // dau 收包处理函数 dau_send_cb send_cb; // dau 收包处理函数 + dau_data_free_cb free_cb; // 内存释放函数 int32_t fifo_send; // 发包 fifo void *private_data; } 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]; @@ -225,8 +211,8 @@ extern dau_t daus[PD_SLOTS_MAX]; extern int32_t dau_handle_init(void); extern int32_t dau_handle_init_after(void); extern void dau_data_send(dau_t *dau, dau_head_init_t *head_data); -extern int32_t dau_msg_send(uint32_t type, void *data); -extern int32_t dau_msg_send_cmd(uint32_t type, void *data); +extern int32_t dau_msg_send(uint32_t type, uint8_t slot, void *data); +extern int32_t dau_msg_send_cmd(uint32_t type, uint8_t slot, void *data); #endif #endif /************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/ diff --git a/app/include/pd_hf.h b/app/include/pd_hf.h index 4f1365f..e255875 100755 --- a/app/include/pd_hf.h +++ b/app/include/pd_hf.h @@ -1,3 +1,38 @@ +/***************************************************************************** + * file include/pd_hf.h + * author YuLiang + * version 1.0.0 + * date 01-July-2025 + * brief This file provides all the headers of the HF functions. + ****************************************************************************** + * Attention + * + *

© COPYRIGHT(c) 2025 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_HF_H__ #define __PD_HF_H__ @@ -5,8 +40,13 @@ /* Includes ------------------------------------------------------------------*/ #include "pd_dau.h" +/* Define --------------------------------------------------------------------*/ #define HF_DATA_LEN (1300) +/* Exported types ------------------------------------------------------------*/ +typedef void (*hf_recv_fun_cb)(uint8_t, char*, uint16_t); +typedef void (*hf_send_fun_cb)(uint8_t, void*); + /*趋势上送数据报文头c语言定义*/ enum HF_TREND_TYPE { @@ -271,14 +311,22 @@ typedef struct typedef struct { hf_heartbeat_t run_status; + hf_cfg_t cfg; + hf_cfg_port_t port[PD_DAU_PORT_SUM]; hf_event_t event; - hf_prps_t *prps; + hf_prps_t prps; hf_trend_t trend; } hf_data_t; +/* Exported macro ------------------------------------------------------------*/ + +/* Extern global variables ---------------------------------------------------*/ + +/* Extern functions ----------------------------------------------------------*/ extern int32_t hf_recv_process(uint8_t slot, char *pkt, uint16_t len); extern int32_t hf_send_process(uint8_t slot, uint8_t type, void *data); extern void* hf_data_malloc(void); +extern void hf_data_free(uint8_t slot); #endif - #endif +/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/ diff --git a/app/lib/a_process/pd_dau.c b/app/lib/a_process/pd_dau.c index 75d6d0f..bae96d3 100755 --- a/app/lib/a_process/pd_dau.c +++ b/app/lib/a_process/pd_dau.c @@ -55,13 +55,14 @@ void _dau_head_init(dau_t *dau, dau_head_init_t *head_data) head->pkt_id = head_data->pkt_id; } -/* dau 插入处理 */ +/* dau 接入处理 */ int _dau_insert(dau_t *dau) { if (3 == dau->info.type_m && 1 == dau->info.type_s) { dau->recv_cb = hf_recv_process; dau->send_cb = hf_send_process; + dau->free_cb = hf_data_free; dau->private_data = hf_data_malloc(); } else @@ -74,6 +75,22 @@ int _dau_insert(dau_t *dau) return E_NONE; } +/* dau 移除处理 */ +int _dau_remove(dau_t *dau) +{ + dau->recv_cb = NULL; + dau->send_cb = NULL; + if (dau->free_cb) + { + dau->free_cb(dau->slot); + } + dau->free_cb = NULL; + dau->private_data = NULL; + dau->state.is_connect = FALSE; + + return E_NONE; +} + /* 初始化UDP服务器 */ int32_t _dau_udp_server_init(dau_t *dau) { @@ -111,12 +128,15 @@ void _dau_recv_connect(dau_t *dau, uint16_t recv_len) dau_contact_t *pnet = (dau_contact_t*)(dau->buf_recv + sizeof(dau_pkt_head_t)); dau_ack_t *ack = (dau_ack_t*)(dau->buf_recv + sizeof(dau_pkt_head_t)); dau_contact_t *info = NULL; + + /* 如果当前状态还是连接状态先释放内存 */ + if (dau->state.is_connect) + { + LD_N_RETURN_N(_dau_remove(dau)); + } /* 复制设备信息 */ memcpy(&dau->info, pnet, sizeof(dau_contact_t)); - - /* 添加 dau */ - LD_N_RETURN_N(_dau_insert(dau)); /* 回复报文 */ ack->result = 0; @@ -127,6 +147,9 @@ void _dau_recv_connect(dau_t *dau, uint16_t recv_len) head_data.len = sizeof(dau_pkt_head_t) + sizeof(dau_ack_t); dau_data_send(dau, &head_data); + /* 添加 dau */ + LD_N_RETURN_N(_dau_insert(dau)); + /* 给后台发送添加信息 */ /* 申请内存 */ info = XMALLOC(MTYPE_CSG, sizeof(dau_contact_t)); @@ -140,7 +163,7 @@ void _dau_recv_connect(dau_t *dau, uint16_t recv_len) memcpy(info, &dau->info, sizeof(dau_contact_t)); /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_ADD, info) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_ADD, dau->slot, info) != E_NONE) { XFREE(MTYPE_CSG, info); } @@ -247,6 +270,33 @@ void *_dau_send_handle(void *arg) return NULL; } +/* dau 收包线程 */ +void *_dau_state_handle(void *arg) +{ + dau_state_t *state = NULL; + uint8_t i = 0; + + /* 等待初始化完成 */ + while(!is_system_init) + { + sleep(1); + } + + while(1) + { + sleep(1); + + for(i = 0; i < PD_SLOTS_MAX; i++) + { + state = &daus[i].state; + state->beat_cnt++; + } + } + + return NULL; +} + +/* Interface functions -------------------------------------------------------*/ /* dau 预初始化 */ int32_t dau_handle_init(void) { @@ -287,6 +337,13 @@ int32_t dau_handle_init_after(void) snprintf(param.thread_name, THREAD_NAME_LEN, "DAU_SEND_%d", i); create_thread(_dau_send_handle, ¶m); } + + /* 监控 dau 状态 */ + param.arg = NULL; + param.priority = 20; + param.log_module = LOG_DAU; + snprintf(param.thread_name, THREAD_NAME_LEN, "DAU_STATE"); + create_thread(_dau_state_handle, ¶m); return E_NONE; } @@ -307,7 +364,7 @@ void dau_data_send(dau_t *dau, dau_head_init_t *head_data) } /* 发送数据到后台通讯进程. */ -int32_t dau_msg_send(uint32_t type, void *data) +int32_t dau_msg_send(uint32_t type, uint8_t slot, void *data) { uint32_t fifo = 0; dau_send_msg_t msg; @@ -324,6 +381,7 @@ int32_t dau_msg_send(uint32_t type, void *data) /* 封装消息. */ msg.type = type; + msg.slot = slot; msg.data = data; /* 发送消息 */ @@ -336,12 +394,13 @@ int32_t dau_msg_send(uint32_t type, void *data) } /* 发送数据到后台通讯进程. */ -int32_t dau_msg_send_cmd(uint32_t type, void *data) +int32_t dau_msg_send_cmd(uint32_t type, uint8_t slot, void *data) { dau_send_msg_t msg; /* 封装消息. */ msg.type = type; + msg.slot = slot; msg.data = data; /* 发送消息 */ diff --git a/app/lib/a_process/pd_hf.c b/app/lib/a_process/pd_hf.c index cb8948b..ab34775 100755 --- a/app/lib/a_process/pd_hf.c +++ b/app/lib/a_process/pd_hf.c @@ -58,6 +58,8 @@ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ + +/* Internal functions --------------------------------------------------------*/ /* 重启发送 */ void _hf_send_reset(uint8_t slot, void *data) { @@ -233,7 +235,7 @@ void _hf_recv_reset(uint8_t slot, char *pkt, uint16_t len) data->slot = slot; /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_RESET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_RESET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -260,7 +262,7 @@ void _hf_recv_info_set(uint8_t slot, char *pkt, uint16_t len) data->slot = slot; /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_INFO_SET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_INFO_SET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -286,7 +288,7 @@ void _hf_recv_info_get(uint8_t slot, char *pkt, uint16_t len) memcpy(data, param, sizeof(hf_dev_info_t)); /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_INFO_GET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_INFO_GET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -316,6 +318,9 @@ void _hf_recv_heartbeat(uint8_t slot, char *pkt, uint16_t len) *timestamp = time(NULL); dau_data_send(dau, &head_data); + /* 将心跳计数归零 */ + dau->state.beat_cnt = 0; + /* 申请内存 */ param = XMALLOC(MTYPE_CSG, sizeof(hf_heartbeat_t)); if (!param) @@ -328,7 +333,7 @@ void _hf_recv_heartbeat(uint8_t slot, char *pkt, uint16_t len) memcpy(param, &hf_data->run_status, sizeof(hf_heartbeat_t)); /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_HEARTBEAT, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_HEARTBEAT, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -355,7 +360,7 @@ void _hf_recv_cfg_set(uint8_t slot, char *pkt, uint16_t len) data->slot = slot; /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_CFG_SET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_CFG_SET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -366,9 +371,13 @@ void _hf_recv_cfg_set(uint8_t slot, char *pkt, uint16_t len) /* 设备配置报文接收 */ void _hf_recv_cfg_get(uint8_t slot, char *pkt, uint16_t len) { + dau_t *dau = &daus[slot]; + hf_data_t *hf_data = (hf_data_t*)dau->private_data; hf_cfg_t *param = (hf_cfg_t*)(pkt + sizeof(dau_pkt_head_t)); hf_cfg_t *data = NULL; + memcpy(&hf_data->cfg, param, sizeof(hf_cfg_t)); + /* 申请内存 */ data = XMALLOC(MTYPE_CSG, sizeof(hf_cfg_t)); if (!data) @@ -378,10 +387,10 @@ void _hf_recv_cfg_get(uint8_t slot, char *pkt, uint16_t len) } /* 装填数据 */ - memcpy(data, param, sizeof(hf_cfg_t)); + memcpy(data, &hf_data->cfg, sizeof(hf_cfg_t)); /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_CFG_GET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_CFG_GET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -409,7 +418,7 @@ void _hf_recv_cfg_set_port(uint8_t slot, char *pkt, uint16_t len) data->slot = slot; /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_PORT_SET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_PORT_SET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -420,8 +429,14 @@ void _hf_recv_cfg_set_port(uint8_t slot, char *pkt, uint16_t len) /* 设备端口配置报文接收 */ void _hf_recv_cfg_get_port(uint8_t slot, char *pkt, uint16_t len) { + dau_t *dau = &daus[slot]; + hf_data_t *hf_data = (hf_data_t*)dau->private_data; hf_cfg_port_t *param = (hf_cfg_port_t*)(pkt + sizeof(dau_pkt_head_t)); hf_cfg_port_t *data = NULL; + uint8_t port = 0; + + port = param->vport - 1; + memcpy(&hf_data->port[port], param, sizeof(hf_cfg_port_t)); /* 申请内存 */ data = XMALLOC(MTYPE_CSG, sizeof(hf_cfg_port_t)); @@ -432,10 +447,10 @@ void _hf_recv_cfg_get_port(uint8_t slot, char *pkt, uint16_t len) } /* 装填数据 */ - memcpy(data, param, sizeof(hf_cfg_port_t)); + memcpy(data, &hf_data->port[port], sizeof(hf_cfg_port_t)); /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_PORT_GET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_PORT_GET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -462,7 +477,7 @@ void _hf_recv_prps_get(uint8_t slot, char *pkt, uint16_t len) data->slot = slot; /* 发送给后台 */ - if (dau_msg_send_cmd(DAU_SEND_PRPS_GET, data) != E_NONE) + if (dau_msg_send_cmd(DAU_SEND_PRPS_GET, slot, data) != E_NONE) { XFREE(MTYPE_CSG, data); } @@ -545,7 +560,8 @@ void _hf_recv_prps(uint8_t slot, char *pkt, uint16_t len) { dau_t *dau = &daus[slot]; hf_data_t *hf_data = (hf_data_t*)dau->private_data; - hf_prps_t *prps = hf_data->prps; + hf_prps_t prps = hf_data->prps; + hf_prps_t *prps_send = NULL; hf_prps_head_t *head_prps = (hf_prps_head_t*)(pkt + sizeof(dau_pkt_head_t)); char *data = (char*)(pkt + sizeof(dau_pkt_head_t) + sizeof(hf_prps_head_t)); char *point = NULL; @@ -553,26 +569,28 @@ void _hf_recv_prps(uint8_t slot, char *pkt, uint16_t len) /* 第一个报文 */ if (0 == head_prps->pkt_index) { - memcpy(&prps->head, head_prps, sizeof(hf_prps_head_t)); + memcpy(&prps.head, head_prps, sizeof(hf_prps_head_t)); } - point = (char*)prps->prps; + point = (char*)prps.prps; point += head_prps->pkt_index * HF_DATA_LEN; memcpy(point, data, head_prps->len); if ((head_prps->pkt_index + 1) == head_prps->pkt_sum) { - /* 发送给后台, 内存由后台释放 */ - if (dau_msg_send(DAU_SEND_PRPS, prps) != E_NONE) + /* 申请内存 */ + prps_send = XMALLOC_Q(MTYPE_CSG, sizeof(hf_prps_t)); + if (!prps_send) { + DBG(DBG_M_PD_HF_ERR, "XMALLOC ERROR!\r\n"); return; } - /* 重新申请内存 */ - hf_data->prps = XMALLOC_Q(MTYPE_CSG, sizeof(hf_prps_t)); - if (!hf_data->prps) + memcpy(prps_send, &prps, sizeof(hf_prps_t)); + + /* 发送给后台, 内存由后台释放 */ + if (dau_msg_send(DAU_SEND_PRPS, slot, prps_send) != E_NONE) { - DBG(DBG_M_PD_HF_ERR, "XMALLOC ERROR!\r\n"); return; } } @@ -617,16 +635,16 @@ void _hf_recv_event(uint8_t slot, char *pkt, uint16_t len) event->head.point_cnt = head_event->point_cnt; } + point = (char*)event->point; + point += head_event->index * HF_DATA_LEN; + memcpy(point, data, head_event->len); + if ((head_event->index + 1) == head_event->sum) { //printh("vport %d power_fre %d type %d max %d\r\n", event->head.vport, event->head.power_fre, event->head.type, event->head.max); //printh("identifier %d utc %d cnt %d avg %d\r\n", event->head.identifier, event->head.utc, event->head.cnt, event->head.avg); //printh("point_cnt %d\r\n\n", event->head.point_cnt); } - - point = (char*)event->point; - point += head_event->index * HF_DATA_LEN; - memcpy(point, data, head_event->len); } /* 回复报文 */ @@ -680,7 +698,7 @@ int32_t _hf_pkt_check(uint8_t slot, char *pkt) } /* 命令映射表 */ -static dau_recv_fun_cb _hf_command[] = +static hf_recv_fun_cb _hf_command[] = { NULL, // 0 NULL, // DAU_C_CONTACT 1 @@ -696,7 +714,7 @@ static dau_recv_fun_cb _hf_command[] = }; /* 命令映射表 */ -static dau_recv_fun_cb _hf_prv_command[] = +static hf_recv_fun_cb _hf_prv_command[] = { NULL, // 0 _hf_recv_cfg_set, // DAU_P_CONFIG_SET 1 @@ -714,7 +732,7 @@ static dau_recv_fun_cb _hf_prv_command[] = }; /* 命令映射表 */ -static dau_send_fun_cb _hf_send_command[] = +static hf_send_fun_cb _hf_send_command[] = { NULL, // 0 NULL, // DAU_SEND_ADD 1 @@ -776,23 +794,25 @@ int32_t hf_send_process(uint8_t slot, uint8_t type, void *data) void* hf_data_malloc(void) { hf_data_t *p = XMALLOC_Q(MTYPE_HF, sizeof(hf_data_t)); - if (!p) { DBG(DBG_M_PD_HF_ERR, "XMALLOC ERROR!\r\n"); return NULL; } - p->prps = XMALLOC_Q(MTYPE_CSG, sizeof(hf_prps_t)); - if (!p->prps) - { - DBG(DBG_M_PD_HF_ERR, "XMALLOC ERROR!\r\n"); - XFREE(MTYPE_HF, p); - return NULL; - } - return p; } +/* 高频私有数据释放 */ +void hf_data_free(uint8_t slot) +{ + dau_t *dau = &daus[slot]; + hf_data_t *hf_data = (hf_data_t*)dau->private_data; + + XFREE(MTYPE_HF, hf_data); + + return; +} + #endif /************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/