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.
248 lines
7.6 KiB
248 lines
7.6 KiB
#include "xsync.hpp"
|
|
using namespace iflytop;
|
|
|
|
Xsync::Xsync(/* args */) {}
|
|
|
|
Xsync &Xsync::Ins() {
|
|
static Xsync xsync;
|
|
return xsync;
|
|
}
|
|
void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; }
|
|
|
|
xs_error_code_t Xsync::connect(string xsync_ip) {
|
|
lock_guard<recursive_mutex> lock(lock_);
|
|
|
|
m_xsync_ip = xsync_ip;
|
|
disConnect();
|
|
/**
|
|
* @brief 创建 m_xsync_reg_udp
|
|
*/
|
|
|
|
xs_error_code_t ecode = kxs_ec_success;
|
|
|
|
auto xsync_reg_udp = m_xsync_udp_factory->createXSUDP();
|
|
ecode = xsync_reg_udp->initialize("0.0.0.0", IFLYTOP_XSYNC_SERVICE_PC_PORT);
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
/**
|
|
* @brief 创建 m_xsync_timecode_udp_listener
|
|
*/
|
|
auto xsync_timecode_udp_listener = m_xsync_udp_factory->createXSUDP();
|
|
|
|
ecode = xsync_timecode_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT);
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
ecode = xsync_timecode_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseTimecodeMsgAndReport(from, data, length); });
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
|
|
/**
|
|
* @brief 创建 m_xsync_camera_sync_udp_listener
|
|
*/
|
|
auto xsync_camera_sync_udp_listener = m_xsync_udp_factory->createXSUDP();
|
|
|
|
ecode = xsync_camera_sync_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT);
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
ecode = xsync_camera_sync_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseCameraSyncMsgAndReport(from, data, length); });
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
|
|
m_xsync_reg_udp = xsync_reg_udp;
|
|
m_xsync_timecode_udp_listener = xsync_timecode_udp_listener;
|
|
m_xsync_camera_sync_udp_listener = xsync_camera_sync_udp_listener;
|
|
|
|
m_net_state = kxsync_net_state_connected;
|
|
return ecode;
|
|
}
|
|
xs_error_code_t Xsync::disConnect() {
|
|
lock_guard<recursive_mutex> lock(lock_);
|
|
|
|
if (m_xsync_reg_udp != nullptr) {
|
|
m_xsync_reg_udp->stopReceive();
|
|
m_xsync_reg_udp = nullptr;
|
|
}
|
|
|
|
if (m_xsync_timecode_udp_listener != nullptr) {
|
|
m_xsync_timecode_udp_listener->stopReceive();
|
|
m_xsync_timecode_udp_listener = nullptr;
|
|
}
|
|
|
|
if (m_xsync_camera_sync_udp_listener != nullptr) {
|
|
m_xsync_camera_sync_udp_listener->stopReceive();
|
|
m_xsync_camera_sync_udp_listener = nullptr;
|
|
}
|
|
|
|
m_net_state = kxsync_net_state_disconnect;
|
|
return kxs_ec_success;
|
|
}
|
|
xsync_net_state_t Xsync::getNetState() { return m_net_state; }
|
|
|
|
void Xsync::regOnTimecodeMsg(xsync_on_timecode_msg_t on_timecode_msg_cb) { m_on_timecode_msg_cb = on_timecode_msg_cb; }
|
|
void Xsync::regOnCameraSyncMsg(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb) { m_on_camera_sync_msg_cb = on_camera_sync_msg_cb; }
|
|
|
|
xs_error_code_t Xsync::xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize) {
|
|
lock_guard<recursive_mutex> lock(lock_);
|
|
if (!m_xsync_reg_udp) return kxs_ec_lose_connect;
|
|
m_xsync_reg_udp->clearRxBuffer();
|
|
|
|
cmd->index = txpacket_index++;
|
|
|
|
XsyncNetAdd toadd = {m_xsync_ip, IFLYTOP_XSYNC_SERVICE_XSYNC_PORT};
|
|
xs_error_code_t ecode = //
|
|
m_xsync_reg_udp->sendto(toadd, (const char *)cmd, sizeof(iflytop_xsync_packet_header_t) + cmd->ndata * 4, nullptr);
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
|
|
XsyncNetAdd fromadd;
|
|
ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, 100);
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
return (xs_error_code_t)rx_data->data[0];
|
|
}
|
|
|
|
xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t ®backvalue) {
|
|
/**
|
|
* @brief
|
|
* 协议说明
|
|
*
|
|
* kxsync_packet_type_reg_write
|
|
* tx: regadd,regdata
|
|
* rx: ecode,regdata
|
|
*/
|
|
|
|
uint8_t txdata[128] = {0};
|
|
uint8_t rxdata[128] = {0};
|
|
|
|
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
|
|
iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
|
|
|
|
txpacket->type = kxsync_packet_type_cmd;
|
|
txpacket->index = txpacket_index++;
|
|
txpacket->cmd = kxsync_packet_type_reg_write;
|
|
txpacket->ndata = 2;
|
|
txpacket->data[0] = regadd;
|
|
txpacket->data[1] = regvalue;
|
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
|
|
regbackvalue = rxpacket->data[1];
|
|
return ecode;
|
|
}
|
|
|
|
xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t ®value) {
|
|
/**
|
|
* @brief
|
|
* 协议说明
|
|
*
|
|
* kxsync_packet_type_reg_write
|
|
* tx: regadd,regdata
|
|
* rx: ecode,regdata
|
|
*/
|
|
|
|
uint8_t txdata[128] = {0};
|
|
uint8_t rxdata[128] = {0};
|
|
|
|
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
|
|
iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
|
|
|
|
txpacket->type = kxsync_packet_type_cmd;
|
|
txpacket->index = txpacket_index++;
|
|
txpacket->cmd = kxsync_packet_type_reg_read;
|
|
txpacket->ndata = 2;
|
|
txpacket->data[0] = regadd;
|
|
txpacket->data[1] = regvalue;
|
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
regvalue = rxpacket->data[1];
|
|
return ecode;
|
|
}
|
|
xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> ®values) {
|
|
/**
|
|
* @brief
|
|
* 协议说明
|
|
*
|
|
* kxsync_packet_type_reg_read_regs
|
|
* tx: regstartadd,nreg
|
|
* rx: ecode,regdatas
|
|
*/
|
|
|
|
uint8_t txdata[128] = {0};
|
|
uint8_t rxdata[1280] = {0};
|
|
|
|
iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
|
|
iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
|
|
|
|
txpacket->type = kxsync_packet_type_cmd;
|
|
txpacket->index = txpacket_index++;
|
|
txpacket->cmd = kxsync_packet_type_reg_read_regs;
|
|
txpacket->ndata = 2;
|
|
txpacket->data[0] = regadd;
|
|
txpacket->data[1] = nreg;
|
|
|
|
auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
|
|
if (ecode != kxs_ec_success) {
|
|
return ecode;
|
|
}
|
|
|
|
if (rxpacket->ndata > 0) {
|
|
for (int i = 0; i < rxpacket->ndata - 1; i++) {
|
|
regvalues.push_back(rxpacket->data[i + 1]);
|
|
}
|
|
}
|
|
return ecode;
|
|
}
|
|
|
|
void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
|
|
//
|
|
iflytop_timecode_report_packet_t *packet = (iflytop_timecode_report_packet_t *)data;
|
|
xysnc_timecode_t timecode;
|
|
/**
|
|
* @brief
|
|
*/
|
|
|
|
uint8_t frameuints = packet->timecode0 & 0x0f;
|
|
uint8_t frame10s = (packet->timecode0 >> 8) & 0x3;
|
|
uint8_t seconduints = (packet->timecode0 >> 16) & 0x0f;
|
|
uint8_t second10s = (packet->timecode0 >> 24) & 0x03;
|
|
|
|
uint8_t minuteuints = packet->timecode1 & 0x0f;
|
|
uint8_t minute10s = (packet->timecode1 >> 8) & 0x03;
|
|
uint8_t houruints = (packet->timecode1 >> 16) & 0x0f;
|
|
uint8_t hour10s = (packet->timecode1 >> 24) & 0x03;
|
|
|
|
timecode.hour = hour10s * 10 + houruints;
|
|
timecode.minute = minute10s * 10 + minuteuints;
|
|
timecode.second = second10s * 10 + seconduints;
|
|
timecode.frame = frame10s * 10 + frameuints;
|
|
|
|
if (m_on_timecode_msg_cb) m_on_timecode_msg_cb(&timecode);
|
|
}
|
|
void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
|
|
uint32_t count = 0;
|
|
|
|
uint32_t data0 = data[0];
|
|
uint32_t data1 = data[1];
|
|
uint32_t data2 = data[2];
|
|
uint32_t data3 = data[3];
|
|
|
|
count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
|
|
|
|
xysnc_camera_sync_data_t camera_sync_data;
|
|
camera_sync_data.frameIndex = count;
|
|
|
|
if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
|
|
}
|