/******************************************************************************
* file lib/process/pd_hf.c
* author YuLiang
* version 1.0.0
* date 05-March-2025
* brief This file provides all the HF operation 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.
*
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef CFG_DEV_TYPE_LAND_PD
/* 标准C库头文件. */
#include
/* 用户代码头文件. */
#include "pd_main.h"
#include "pd_csg.h"
#include "pd_dau.h"
#include "pd_hf.h"
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
extern void _hf_heartbeat_recv(uint8_t slot, char *pkt, uint16_t len);
extern void _hf_prps_get_recv(uint8_t slot, char *pkt, uint16_t len);
extern void _hf_trend_recv(uint8_t slot, char *pkt, uint16_t len);
extern void _hf_event_recv(uint8_t slot, char *pkt, uint16_t len);
extern void _hf_prps_get_send(uint8_t slot, void *data);
// 命令映射表
static dau_recv_fun_cb _hf_command[] =
{
NULL, // 0
NULL, // DAU_C_CONTACT 1
NULL, // DAU_C_ADD_DAU 2
NULL, // DAU_C_RESET 3
NULL, // 4
NULL, // DAU_C_UPDATE 5
NULL, // DAU_C_DEV_INFO_SET 6
NULL, // DAU_C_DEV_INFO_GET 7
NULL, // 8
NULL, // DAU_C_UPDATE_RESULT 9
_hf_heartbeat_recv, // DAU_C_HEARTBEAT 10
};
// 命令映射表
static dau_recv_fun_cb _hf_prv_command[] =
{
NULL, // 0
NULL, // DAU_P_CONFIG_SET 1
NULL, // DAU_P_CONFIG_GET 2
NULL, // DAU_P_CONFIG_PORT_SET 3
NULL, // DAU_P_CONFIG_PORT_GET 4
_hf_prps_get_recv, // DAU_P_CONFIG_REAL_WAVE 5
NULL, // 6
NULL, // 7
NULL, // 8
NULL, // 9
_hf_trend_recv, // DAU_P_TREND 10
NULL, // DAU_P_REAL_PRPS 11
_hf_event_recv, // DAU_P_EVENT 12
};
// 命令映射表
static dau_send_fun_cb _hf_send_command[] =
{
NULL, // 0
_hf_prps_get_send, // DAU_SEND_PRPS 1
};
/* Interface functions -------------------------------------------------------*/
/* PRPS 关注报文发送 */
void _hf_prps_get_send(uint8_t slot, void *data)
{
dau_head_init_t head_data;
dau_t *dau = &daus[slot];
hf_prps_get_t *param = (hf_prps_get_t*)(dau->buf_send + sizeof(dau_pkt_head_t));
/* 回复报文 */
head_data.cmd_type = DAU_PRV_REQUEST;
head_data.cmd = DAU_P_CONFIG_REAL_WAVE;
head_data.pkt_id = dau->pkt_id++;
head_data.pkt = dau->buf_send;
head_data.len = sizeof(dau_pkt_head_t) + sizeof(hf_prps_get_t);
memcpy(param, data, sizeof(hf_prps_get_t));
dau_send_data(dau, &head_data);
return;
}
/* 心跳报文接收 */
void _hf_heartbeat_recv(uint8_t slot, char *pkt, uint16_t len)
{
dau_head_init_t head_data;
dau_t *dau = &daus[slot];
dau_pkt_head_t *head = (dau_pkt_head_t*)pkt;
hf_heartbeat_t *data = (hf_heartbeat_t*)(pkt + sizeof(dau_pkt_head_t));
hf_data_t *hf_data = (hf_data_t*)dau->private_data;
uint32_t *timestamp = (uint32_t*)(pkt + sizeof(dau_pkt_head_t));
memcpy(&hf_data->run_status, data, sizeof(hf_heartbeat_t));
/* 回复报文 */
head_data.cmd_type = DAU_REQUEST;
head_data.cmd = DAU_C_HEARTBEAT;
head_data.pkt_id = head->pkt_id;
head_data.pkt = pkt;
head_data.len = sizeof(dau_pkt_head_t) + 4;
*timestamp = time(NULL);
dau_send_data(dau, &head_data);
return;
}
/* 心跳报文接收 */
void _hf_prps_get_recv(uint8_t slot, char *pkt, uint16_t len)
{
return;
}
/* 趋势报文接收 */
void _hf_trend_recv(uint8_t slot, char *pkt, uint16_t len)
{
dau_head_init_t head_data;
dau_t *dau = &daus[slot];
hf_data_t *hf_data = (hf_data_t*)dau->private_data;
hf_trend_t *trend = &hf_data->trend;
dau_pkt_head_t *head = (dau_pkt_head_t*)pkt;
hf_trend_head_t *head_trend = (hf_trend_head_t*)(pkt + sizeof(dau_pkt_head_t));
char *data = (char*)(pkt + sizeof(dau_pkt_head_t) + sizeof(hf_trend_head_t));
char *point = NULL;
uint8_t port = 0;
hf_trend_ack_t ack;
hf_trend_ack_t *ack_p = (hf_trend_ack_t*)(pkt + sizeof(dau_pkt_head_t));
/* 不处理补包数据 */
if (!head_trend->boosterpack)
{
//printh("#1 %d %d %d %d\r\n", head_trend->vport, head_trend->sum, head_trend->index, head_trend->type);
//printh("#2 %d %d\r\n", head_trend->utc, head_trend->len);
port = head_trend->vport - 1;
if (HF_TREND_TYPE_STAT == head_trend->type)
{
trend->col.index = head_trend->identifier;
trend->col.utc = head_trend->utc;
memcpy(&trend->col.port[port], data, sizeof(hf_trend_stat));
if ((head_trend->index + 1) == head_trend->sum)
{
//printh("vport %d index %d utc %d max %d\r\n", head_trend->vport, trend->col.index, trend->col.utc, trend->col.port[port].max);
//printh("avg %d cnt %d phase %d noise %d\r\n", trend->col.port[port].avg, trend->col.port[port].cnt, trend->col.port[port].phase, trend->col.port[port].noise);
//printh("event_cnt %d\r\n", trend->col.port[port].event_cnt);
}
}
else if (HF_TREND_TYPE_PRPD == head_trend->type)
{
point = (char*)trend->prpd.port[port].data;
point += head_trend->index * HF_DATA_LEN;
memcpy(point, data, head_trend->len);
}
else if (HF_TREND_TYPE_PRPS == head_trend->type)
{
if (0 == head_trend->index)
{
trend->prps.port[port].point_cnt = 0;
}
/* 这里左移 2 位是因为 sizeof(hf_data_point_t) 等于 4 */
trend->prps.port[port].point_cnt += head_trend->len >> 2;
point = (char*)trend->prps.port[port].point;
point += head_trend->index * HF_DATA_LEN;
memcpy(point, data, head_trend->len);
}
}
/* 回复报文 */
head_data.cmd_type = DAU_PRV_REQUEST;
head_data.cmd = DAU_P_TREND;
head_data.pkt_id = head->pkt_id;
head_data.pkt = pkt;
head_data.len = sizeof(dau_pkt_head_t) + sizeof(hf_trend_ack_t);
ack.index = head_trend->index;
ack.result = 0;
ack.boosterpack = head_trend->boosterpack;
memcpy(ack_p, &ack, sizeof(hf_trend_ack_t));
dau_send_data(dau, &head_data);
return;
}
/* 事件报文接收 */
void _hf_event_recv(uint8_t slot, char *pkt, uint16_t len)
{
dau_head_init_t head_data;
dau_t *dau = &daus[slot];
hf_data_t *hf_data = (hf_data_t*)dau->private_data;
hf_event_t *event = &hf_data->event;
dau_pkt_head_t *head = (dau_pkt_head_t*)pkt;
hf_event_head_t *head_event = (hf_event_head_t*)(pkt + sizeof(dau_pkt_head_t));
char *data = (char*)(pkt + sizeof(dau_pkt_head_t) + sizeof(hf_event_head_t));
char *point = NULL;
hf_event_ack_t ack;
hf_event_ack_t *ack_p = (hf_event_ack_t*)(pkt + sizeof(dau_pkt_head_t));
/* 不处理补包数据 */
if (!head_event->boosterpack)
{
//printh("#3 %d %d %d %d\r\n", head_event->vport, head_event->sum, head_event->index, head_event->type);
//printh("#4 %d %d\r\n\n", head_event->utc, head_event->len);
/* 第一个报文 */
if (0 == head_event->index)
{
event->head.vport = head_event->vport;
event->head.boosterpack = head_event->boosterpack;
event->head.power_fre = head_event->power_fre;
event->head.type = head_event->type;
event->head.max = head_event->max;
event->head.identifier = head_event->identifier;
event->head.utc = head_event->utc;
event->head.cnt = head_event->cnt;
event->head.avg_o = head_event->avg_o;
event->head.avg = head_event->avg;
event->head.point_cnt = head_event->point_cnt;
}
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);
}
/* 回复报文 */
head_data.cmd_type = DAU_PRV_REQUEST;
head_data.cmd = DAU_P_EVENT;
head_data.pkt_id = head->pkt_id;
head_data.pkt = pkt;
head_data.len = sizeof(dau_pkt_head_t) + sizeof(hf_event_ack_t);
ack.index = head_event->index;
ack.result = 0;
ack.boosterpack = head_event->boosterpack;
memcpy(ack_p, &ack, sizeof(hf_event_ack_t));
dau_send_data(dau, &head_data);
return;
}
/* 校验收到包的包头, 长度, 校验码. */
int32_t _hf_pkt_check(uint8_t slot, char *pkt)
{
dau_t *dau = &daus[slot];
dau_pkt_head_t *head = (dau_pkt_head_t*)pkt;
/* 对主次设备号进行识别, 次设备号可以是广播. */
if (head->dev_type_m != dau->info.type_m || head->dev_type_s != dau->info.type_s)
{
DBG(DBG_M_PD_HF_ERR, "Device type %d:%d ERROR\r\n", head->dev_type_m, head->dev_type_s);
return E_ERROR;
}
if (head->dev_id != dau->info.dev_id)
{
DBG(DBG_M_PD_HF_ERR, "Device id %08x ERROR\r\n", head->dev_id);
return E_ERROR;
}
if (head->len > DAU_BUF_SIZE)
{
DBG(DBG_M_PD_HF_ERR, "Packet len %d ERROR\r\n", head->len);
return E_ERROR;
}
if ((DAU_REPLY == head->cmd_type && head->cmd >= DAU_C_MAX)
|| (DAU_PRV_REPLY == head->cmd_type && head->cmd >= DAU_P_MAX))
{
DBG(DBG_M_PD_HF_ERR, "Cmd %d:%d ERROR\r\n", head->cmd_type, head->cmd);
return E_ERROR;
}
return E_NONE;
}
/* 高频收包处理函数 */
int32_t hf_recv_process(uint8_t slot, char *pkt, uint16_t len)
{
dau_pkt_head_t *head = (dau_pkt_head_t*)pkt;
/* 报文头和 CRC 校验. */
LD_E_RETURN_N(_hf_pkt_check(slot, pkt));
if (DAU_REPLY == head->cmd_type)
{
if (_hf_command[head->cmd])
{
_hf_command[head->cmd](slot, pkt, len);
}
}
else if (CSG_PRV_REPLY == head->cmd_type)
{
if (_hf_prv_command[head->cmd])
{
_hf_prv_command[head->cmd](slot, pkt, len);
}
}
return E_NONE;
}
/* 高频发包处理函数 */
int32_t hf_send_process(uint8_t slot, uint8_t type, void *data)
{
if (_hf_send_command[type])
{
_hf_send_command[type](slot, data);
}
return E_NONE;
}
/* 高频私有数据 */
void* hf_data_malloc(void)
{
return XMALLOC_Q(MTYPE_HF, sizeof(hf_data_t));
}
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/