/******************************************************************************
* 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"
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void _hf_contact_recv(int slot, char *pkt);
void _hf_heartbeat_recv(int slot, char *pkt);
void _hf_real_image_recv(int slot, char *pkt);
void _hf_trend_recv(int slot, char *pkt);
void _hf_event_recv(int slot, char *pkt);
// 命令映射表
static command_entry hf_request_command_table[] =
{
{CSG_C_CONTACT, _hf_contact_recv},
{CSG_C_HEARTBEAT, _hf_heartbeat_recv},
{CMD_INVALID, NULL}
};
// 命令映射表
static command_entry hf_prv_request_command_table[] =
{
{CSG_PRV_TREND, _hf_event_recv},
{CSG_PRV_REAL_PRPS, _hf_event_recv},
{CSG_PRV_EVENT, _hf_event_recv},
{CMD_INVALID, NULL}
};
/* Interface functions -------------------------------------------------------*/
void _hf_contact_recv(int slot, char *pkt)
{
csg_pkt_head_t *head = (csg_pkt_head_t*)pkt;
csg_contact_t *pnet = (csg_contact_t *)(pkt + sizeof(csg_pkt_head_t));
csg_add_dau_t snd = {0} ;
dau_info_t *pinfo = &daus[slot].info;
memset(pinfo, 0, sizeof(dau_info_t));
pinfo->type_m = pnet->type_m;
pinfo->type_s = pnet->type_s;
pinfo->dev_id = pnet->dev_id;
strncpy(pinfo->hostname, pnet->hostname, sizeof(pinfo->hostname) - 1);
pinfo->factory_date = pnet->factory_date;
pinfo->deployment_date = pnet->deployment_date;
strncpy((char *)pinfo->app_version, (char *)pnet->app_version, sizeof(pinfo->app_version) -1);
strncpy((char *)pinfo->app_compile_time, (char *)pnet->app_compile_time, sizeof(pinfo->app_compile_time) -1);
strncpy((char *)pinfo->hardware_version, (char *)pnet->hardware_version, sizeof(pinfo->hardware_version) -1);
strncpy((char *)pinfo->FPGA_version, (char *)pnet->FPGA_version, sizeof(pinfo->FPGA_version) -1);
pinfo->ip = pnet->ip;
pinfo->mask = pnet->mask;
pinfo->gw = pnet->gw;
memcpy(pinfo->mac, pnet->mac, sizeof(pinfo->mac));
pinfo->server_port = pnet->server_port;
pinfo->server_ipv4 = pnet->server_ipv4;
memcpy(pinfo->port, pnet->port, sizeof(pinfo->port));
memcpy(pinfo->port_type, pnet->port_type, sizeof(pinfo->port_type));
snd.dev_id = pnet->dev_id;
snd.slot = slot;
snd.status = 1;
memcpy(snd.port, pnet->port, sizeof(pnet->port));
memcpy(snd.port_type, pnet->port_type, sizeof(pnet->port_type));
memcpy(pkt + sizeof(csg_pkt_head_t), (char *)&snd, sizeof(csg_add_dau_t));
head->slot = slot;
head->sdev_id = head->dev_id;
printf("recv contact dev_id = 0x%x slot = %d\n", snd.dev_id, snd.slot);
_csg_send_data(CSG_REPLY, CSG_C_ADD_DAU, pkt, sizeof(csg_add_dau_t));
}
void _hf_heartbeat_recv(int slot, char *pkt)
{
csg_pkt_head_t *head = (csg_pkt_head_t*)pkt;
printf("_hf_heartbeat_recv slot = %d\n", slot);
head->cmd_type = CSG_REQUEST;
head->cmd = CSG_C_HEARTBEAT;
head->len = CSG_HEAD_LEN + 4;
uint32_t *timestamp = (uint32_t *)(pkt + CSG_HEAD_LEN);
*timestamp = time(NULL);
_dau_response(slot, pkt, head->len);
}
void _hf_real_image_recv(int slot, char *pkt)
{
csg_pkt_head_t *head = (csg_pkt_head_t*)pkt;
head->slot = slot;
head->sdev_id = head->dev_id;
_csg_send_data(CSG_PRV_REPLY, CSG_PRV_REAL_PRPS, pkt, head->len - sizeof(csg_pkt_head_t));
}
void _hf_trend_recv(int slot, char *pkt)
{
csg_pkt_head_t *head = (csg_pkt_head_t*)pkt;
head->slot = slot;
head->sdev_id = head->dev_id;
_csg_send_data(CSG_PRV_REPLY, CSG_PRV_TREND, pkt, head->len - sizeof(csg_pkt_head_t));
}
void _hf_event_recv(int slot, char *pkt)
{
csg_pkt_head_t *head = (csg_pkt_head_t*)pkt;
head->slot = slot;
head->sdev_id = head->dev_id;
_csg_send_data(CSG_PRV_REPLY, CSG_PRV_EVENT, pkt, head->len - sizeof(csg_pkt_head_t));
}
int32_t _hf_recv_process(int slot, char *pkt)
{
csg_pkt_head_t *head = (csg_pkt_head_t *)pkt;
command_handler handle = NULL;
/* 报文头和 CRC 校验. */
LD_E_RETURN(DBG_M_PD_CSG_ERR, _csg_pkt_check(pkt));
if (CSG_REPLY == head->cmd_type)
{
handle = _csg_get_table_handle(hf_request_command_table, head->cmd);
if (handle)
{
handle(slot, pkt);
}
}
else if (CSG_PRV_REPLY == head->cmd_type)
{
handle = _csg_get_table_handle(hf_prv_request_command_table, head->cmd);
if (handle)
{
handle(slot, pkt);
}
}
return E_NONE;
}
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/