You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

116 lines
4.0 KiB

#include "iflytop_xsync_protocol\iflytop_xsync_protocol.h"
#include "project_configs.h"
#include "project_dep.h"
//
#include "iflytop_xsync\xs_udp.h"
#include "reg_manager.h"
static udp_t m_udp_cmd_server; //
// static udp_broadcast_handler_t m_udp_camera_sync_sender; //
static struct sockaddr_in m_last_rxpacket_client;
static bool m_last_rxpacket_client_valid = false;
static uint8_t txbuf[2048];
/*******************************************************************************
* 上位机下发指令回执服务 *
*******************************************************************************/
typedef struct {
udp_t *server;
struct sockaddr_in *client;
iflytop_xsync_packet_header_t *rxpacket;
} extern_if_service_context_t;
/**
* @brief 创建并发送回执数据包
*
* @param context
* @param data
* @param ndata
*/
static void create_and_send_receipt(extern_if_service_context_t *context, uint32_t *data, size_t ndata) {
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txbuf;
txpacket->type = kxsync_packet_type_receipt;
txpacket->index = context->rxpacket->index;
txpacket->cmd = context->rxpacket->cmd;
txpacket->ndata = ndata;
memcpy(txpacket->data, data, ndata * sizeof(uint32_t));
xs_udp_send_message2(context->server, context->client, (const char *)txbuf, sizeof(iflytop_xsync_packet_header_t) + ndata * sizeof(uint32_t));
}
#if 0
/**
* @brief 构建并发送时间码数据包
*
* @param client
* @param timecode0
* @param timecode1
*/
static void create_and_send_timecode(struct sockaddr_in client, uint32_t timecode0, uint32_t timecode1) {
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txbuf;
txpacket->type = kxsync_packet_type_report;
txpacket->index = 0;
txpacket->cmd = kxsync_packet_type_timecode_report;
txpacket->ndata = 2;
txpacket->data[0] = timecode0;
txpacket->data[1] = timecode1;
xs_udp_send_message2(&m_udp_cmd_server, &client, txbuf, sizeof(iflytop_xsync_packet_header_t) + 2 * sizeof(uint32_t));
}
#endif
/**
* @brief 接收并解析从上位机下发下来的数据包
*
* @param server
* @param client
* @param data
* @param len
*/
static void udp_on_packet(udp_t *server, struct sockaddr_in *client, uint8_t *data, uint16_t len) {
/**
* @brief
*/
iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)data;
extern_if_service_context_t cx = {0};
cx.client = client;
cx.server = server;
cx.rxpacket = rxpacket;
if (rxpacket->type != kxsync_packet_type_cmd) return;
m_last_rxpacket_client_valid = true;
memcpy(&m_last_rxpacket_client, client, sizeof(struct sockaddr_in));
if (rxpacket->cmd == kxsync_packet_type_reg_read) {
uint32_t regadd = rxpacket->data[0];
uint32_t receipt[2];
receipt[0] = 0; // receipt
receipt[1] = reg_manager_read_reg(regadd); // regdata
create_and_send_receipt(&cx, receipt, 2);
} else if (rxpacket->cmd == kxsync_packet_type_reg_write) {
uint32_t regadd = rxpacket->data[0];
uint32_t regval = rxpacket->data[1];
uint32_t receipt[2];
receipt[0] = 0; //
receipt[1] = reg_manager_write_reg(regadd, regval);
create_and_send_receipt(&cx, receipt, 2);
} else if (rxpacket->cmd == kxsync_packet_type_reg_read_regs) {
uint32_t start_regadd = rxpacket->data[0];
uint32_t nreg = rxpacket->data[1];
static uint32_t regcache[MAX_REG_NUM + 1];
uint32_t len = MAX_REG_NUM;
regcache[0] = 0;
reg_manager_read_regs(start_regadd, nreg, &regcache[1], &len);
create_and_send_receipt(&cx, regcache, len);
}
}
void extern_if_service_init() { ZASSERT(xs_udp_init(&m_udp_cmd_server, IFLYTOP_XSYNC_SERVICE_PORT, udp_on_packet, 1024, NULL)); }
#if 0
void extern_if_service_send_timecode(struct sockaddr_in client, uint32_t timecode0, uint32_t timecode1) { create_and_send_timecode(client, timecode0, timecode1); }
#endif