From b29830ab651305eeb7ede08d49b7eee2eab7b68b Mon Sep 17 00:00:00 2001 From: yuliang Date: Thu, 10 Jul 2025 12:32:31 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20|=20=E8=BF=9C=E7=A8=8B=E5=8D=87=E7=BA=A7?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/include/pd_dau.h | 3 + app/include/pd_hf.h | 35 +++++++- app/lib/a_process/pd_dau.c | 1 + app/lib/a_process/pd_hf.c | 159 ++++++++++++++++++++++++++++++++++++- 4 files changed, 193 insertions(+), 5 deletions(-) diff --git a/app/include/pd_dau.h b/app/include/pd_dau.h index ed2f953..54891e5 100755 --- a/app/include/pd_dau.h +++ b/app/include/pd_dau.h @@ -98,6 +98,7 @@ enum DAU_SEND_TYPE { DAU_SEND_ADD = 1, DAU_SEND_RESET = 2, + DAU_SEND_UPDATE = 3, DAU_SEND_INFO_SET = 4, DAU_SEND_INFO_GET = 5, DAU_SEND_HEARTBEAT = 7, @@ -193,6 +194,7 @@ typedef struct { uint8_t slot; // dau slot uint16_t pkt_id; // 发送报文 id + uint16_t pkt_index; // 发送报文 index 号, 用于升级等功能 int fd; // dau socket fd struct sockaddr_in server; // dau client ip char buf_recv[DAU_BUF_SIZE]; // dau 收包 buf @@ -204,6 +206,7 @@ typedef struct dau_show_cfg_cb show_cfg_cb; // 显示全局配置函数 dau_show_port_cb show_port_cb; // 显示端口配置函数 int32_t fifo_send; // 发包 fifo + sem_t sem_send; // 发包信号量 void *private_data; } dau_t; diff --git a/app/include/pd_hf.h b/app/include/pd_hf.h index 940cce5..c1904de 100755 --- a/app/include/pd_hf.h +++ b/app/include/pd_hf.h @@ -42,6 +42,9 @@ /* Define --------------------------------------------------------------------*/ #define HF_DATA_LEN (1300) +#define HF_FILE_NAME_LEN (128) +#define HF_SEND_TIMEOUT (3) +#define HF_SEND_ERR_CNT (3) /* Exported types ------------------------------------------------------------*/ typedef void (*hf_recv_fun_cb)(uint8_t, char*, uint16_t); @@ -67,11 +70,37 @@ typedef struct /* 一般应答 */ typedef struct { - uint8_t result; // 应答结果. 0:失败 1:成功 - uint8_t slot; // slot id - uint8_t reserved[2]; // 保留 + uint8_t result; // 应答结果. 0:失败 1:成功 + uint8_t slot; // slot id + uint8_t reserved[2]; // 保留 } hf_ack_t; +/* 应答升级结构体 */ +typedef struct +{ + uint16_t index; // 应答包序号. + uint8_t result; // 应答结果. 0:失败 1:成功 + uint8_t reserve; // 保留 +} hf_upate_ack_t; + +/* 升级文件包结构体 */ +typedef struct +{ + uint8_t type; // 升级类型 + uint8_t resverd[3]; + char name[HF_FILE_NAME_LEN]; // 文件名字 +} hf_upate_msg_t; + +/* 升级文件包结构体 */ +typedef struct +{ + uint8_t type; // 升级类型 + uint8_t resverd[3]; + uint16_t index; // 报文索引 + uint16_t sum; // 总包数. + uint32_t len; // 数据包长度. +} hf_upate_t; + /* 设备信息 */ typedef struct { diff --git a/app/lib/a_process/pd_dau.c b/app/lib/a_process/pd_dau.c index 7f092ae..275c30b 100755 --- a/app/lib/a_process/pd_dau.c +++ b/app/lib/a_process/pd_dau.c @@ -540,6 +540,7 @@ int32_t dau_handle_init_after(void) { snprintf(fifo_name, THREAD_NAME_LEN, "DAU_SEND_%d", i); daus[i].fifo_send = fifo_create(fifo_name, DUA_SEND_FIFO_NUM); + sem_init(&daus[i].sem_send, 0, 0); param.arg = (void*)(&daus[i]); param.priority = 80; diff --git a/app/lib/a_process/pd_hf.c b/app/lib/a_process/pd_hf.c index cee0cea..69d031f 100755 --- a/app/lib/a_process/pd_hf.c +++ b/app/lib/a_process/pd_hf.c @@ -41,6 +41,11 @@ #ifdef CFG_DEV_TYPE_LAND_PD /* 标准C库头文件. */ +#include +#include +#include +#include +#include #include /* 用户代码头文件. */ @@ -61,6 +66,38 @@ /* Private function prototypes -----------------------------------------------*/ /* Internal functions --------------------------------------------------------*/ +/* description: 发包线程锁 + param: slot -- slot id + sec -- 等待的秒 + nsec -- 等待的纳秒 + return: */ +int32_t _hf_lock_timeout(uint8_t slot, uint32_t sec, uint32_t nsec) +{ + struct timespec time_out; + dau_t *dau = &daus[slot]; + + clock_gettime(CLOCK_REALTIME, &time_out); + time_out.tv_sec += sec; + time_out.tv_nsec += nsec; + if (time_out.tv_nsec >= 1000000000) + { + time_out.tv_nsec -= 1000000000; + time_out.tv_sec++; + } + + return sem_timedwait(&dau->sem_send, &time_out); +} + +/* description: 发包线程解锁 + param: unit -- DAU id + return: */ +void _hf_unlock(uint8_t slot) +{ + dau_t *dau = &daus[slot]; + + sem_post(&dau->sem_send); +} + /* 重启发送 */ void _hf_send_reset(uint8_t slot, void *data) { @@ -79,6 +116,106 @@ void _hf_send_reset(uint8_t slot, void *data) return; } +/* 发送升级文件 */ +void _hf_send_update(uint8_t slot, void *data) +{ + struct stat file_stat; + dau_head_init_t head_data; + hf_upate_msg_t *msg = (hf_upate_msg_t*)data; + dau_t *dau = &daus[slot]; + hf_upate_t *head = (hf_upate_t*)(dau->buf_send + sizeof(dau_pkt_head_t)); + char *data_update = dau->buf_send + sizeof(dau_pkt_head_t) + sizeof(hf_upate_t); + uint8_t err_cnt = 0; + uint16_t index = 0; + int fd = -1; + int fd1 = -1; + int32_t len = 0; + + fd1 = open("test", O_RDWR | O_CREAT | O_TRUNC, 0777); + + /* 打开文件 */ + fd = open(msg->name, O_RDONLY, 0777); + if (-1 == fd) + { + DBG(DBG_M_PD_HF_ERR, "Can't open %s\r\n", msg->name); + return; + } + + /* 获取文件大小, 计算报文总数 */ + if (stat(msg->name, &file_stat) < 0) + { + DBG(DBG_M_PD_HF_ERR, "Can't open %s\r\n", msg->name); + close(fd); + close(fd1); + return; + } + + head->type = msg->type; + head->sum = file_stat.st_size / HF_DATA_LEN; + if (file_stat.st_size % HF_DATA_LEN) + { + head->sum++; + } + printh("#1 type %d sum %d\r\n", head->type, head->sum); + + /* 初始化报文头 */ + head_data.cmd_type = DAU_REQUEST; + head_data.cmd = DAU_C_UPDATE; + head_data.pkt = dau->buf_send; + + /* 发送报文 */ + while(1) + { + /* 读取文件数据 */ + len = read(fd, data_update, HF_DATA_LEN); + if (len <= 0) + { + /* 正好读完, 直接退出 */ + break; + } + + /* 初始化报文头 */ + dau->pkt_index = index; + head->index = index; + head->len = len; + head_data.pkt_id = dau->pkt_id; + head_data.len = sizeof(dau_pkt_head_t) + sizeof(hf_upate_t) + len; + printh("#2 index %d len %d\r\n", index, len); + + /* 发送报文, 等待数据回复 */ + while(1) + { + dau_data_send(dau, &head_data); + if (_hf_lock_timeout(slot, HF_SEND_TIMEOUT, 0) != 0) + { + err_cnt++; + if (err_cnt >= HF_SEND_ERR_CNT) + { + DBG(DBG_M_PD_HF_ERR, "Send error cnt %d.\r\n", err_cnt); + close(fd1); + close(fd); + return; + } + DBG(DBG_M_PD_HF_ERR, "Send sem error.\r\n"); + continue; + } + else + { + break; + } + } + + write(fd1, data_update, len); + + dau->pkt_id++; + index++; + } + + close(fd); + close(fd1); + return; +} + /* 设备信息配置发送 */ void _hf_send_info_set(uint8_t slot, void *data) { @@ -247,6 +384,24 @@ void _hf_recv_reset(uint8_t slot, char *pkt, uint16_t len) return; } +/* 升级报文接收 */ +void _hf_recv_update(uint8_t slot, char *pkt, uint16_t len) +{ + dau_t *dau = &daus[slot]; + hf_upate_ack_t *ack = (hf_upate_ack_t*)(pkt + sizeof(dau_pkt_head_t)); + + if (ack->result && ack->index == dau->pkt_index) + { + _hf_unlock(slot); + } + else + { + DBG(DBG_M_PD_HF_ERR, "Update rv %d index %d err!\r\n", ack->result, ack->index); + } + + return; +} + /* 设备信息配置报文接收 */ void _hf_recv_info_set(uint8_t slot, char *pkt, uint16_t len) { @@ -739,7 +894,7 @@ static hf_recv_fun_cb _hf_command[] = NULL, // DAU_C_ADD_DAU 2 _hf_recv_reset, // DAU_C_RESET 3 NULL, // 4 - NULL, // DAU_C_UPDATE 5 + _hf_recv_update, // DAU_C_UPDATE 5 _hf_recv_info_set, // DAU_C_DEV_INFO_SET 6 _hf_recv_info_get, // DAU_C_DEV_INFO_GET 7 NULL, // 8 @@ -771,7 +926,7 @@ static hf_send_fun_cb _hf_send_command[] = NULL, // 0 NULL, // DAU_SEND_ADD 1 _hf_send_reset, // DAU_SEND_RESET 2 - NULL, // 3 + _hf_send_update, // DAU_SEND_UPDATE 3 _hf_send_info_set, // DAU_SEND_INFO_SET 4 _hf_send_info_get, // DAU_SEND_INFO_GET 5 NULL, // 6