diff --git a/i_xsync_udp.hpp b/i_xsync_udp.hpp index 3b53566..f9d141b 100644 --- a/i_xsync_udp.hpp +++ b/i_xsync_udp.hpp @@ -11,6 +11,7 @@ #include #include "xsync_errcode.hpp" +#include "iflytop_xsync_protocol\iflytop_xsync_protocol.h" namespace iflytop { using namespace std; @@ -65,7 +66,9 @@ class I_XSUDP { * @return xs_error_code_t */ virtual xs_error_code_t startReceive(onMessage_t onMessage) = 0; - virtual xs_error_code_t stopReceive() = 0; + virtual xs_error_code_t stopReceive() = 0; + + virtual xs_error_code_t clearRxBuffer() = 0; virtual ~I_XSUDP() {} }; diff --git a/iflytop_xsync_protocol b/iflytop_xsync_protocol index 3a13ffc..85b6bcc 160000 --- a/iflytop_xsync_protocol +++ b/iflytop_xsync_protocol @@ -1 +1 @@ -Subproject commit 3a13ffc983b65ba14947d88ea5a340d592f1fd22 +Subproject commit 85b6bcc87f9188f061402b6ff6bac7b226c0025c diff --git a/xsync.cpp b/xsync.cpp index cbe9ab2..03e20ae 100644 --- a/xsync.cpp +++ b/xsync.cpp @@ -2,11 +2,6 @@ using namespace iflytop; Xsync::Xsync(/* args */) {} -Xsync::~Xsync() {} - -xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue) {} -xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t ®value) {} -xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t ®value) {} Xsync &Xsync::Ins() { static Xsync xsync; @@ -15,12 +10,12 @@ Xsync &Xsync::Ins() { void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; } void Xsync::connect(string xsync_ip) { + m_xsync_ip = xsync_ip; /** * @brief 创建 m_xsync_reg_udp */ m_xsync_reg_udp = m_xsync_udp_factory->createXSUDP(); m_xsync_reg_udp->initialize(m_xsync_ip, IFLYTOP_XSYNC_SERVICE_PC_PORT); - /** * @brief 创建 m_xsync_timecode_udp_listener */ @@ -34,12 +29,147 @@ void Xsync::connect(string xsync_ip) { m_xsync_camera_sync_udp_listener = m_xsync_udp_factory->createXSUDP(); m_xsync_camera_sync_udp_listener->initialize(m_xsync_ip, IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT); m_xsync_camera_sync_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseCameraSyncMsgAndReport(from, data, length); }); + + m_net_state = kxsync_net_state_connected; } void Xsync::disConnect() { 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; +} +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) { + m_xsync_reg_udp->clearRxBuffer(); + + cmd->index = txpacket_index++; + + XsyncNetAdd toadd = {m_xsync_ip, IFLYTOP_XSYNC_SERVICE_PC_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) { + /** + * @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; + } + + 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 ®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) { @@ -81,4 +211,4 @@ void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t camera_sync_data.frameIndex = count; if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data); -} \ No newline at end of file +} diff --git a/xsync.hpp b/xsync.hpp index 3fba12f..eba9134 100644 --- a/xsync.hpp +++ b/xsync.hpp @@ -52,7 +52,13 @@ class Xsync { xsync_on_camera_sync_msg_t m_on_camera_sync_msg_cb = nullptr; xsync_on_timecode_msg_t m_on_timecode_msg_cb = nullptr; - Xsync(/* args */){}; + int txpacket_index = 0; + + uint8_t m_xync_cmd_rxdata_cache[2560]; + + xsync_net_state_t m_net_state = kxsync_net_state_disconnect; + + Xsync(/* args */); public: static Xsync &Ins(); @@ -70,7 +76,9 @@ class Xsync { xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue); xs_error_code_t reg_read(uint32_t regadd, uint32_t ®value); - xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t ®value); + xs_error_code_t reg_read_muti(uint32_t regadd, uint32_t nreg, vector ®values); + + xs_error_code_t xsync_send_cmd_block(iflytop_xsync_packet_header_t *cmd, iflytop_xsync_packet_header_t *rx_data, int32_t buffersize); private: void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length); diff --git a/xsync_errcode.hpp b/xsync_errcode.hpp index 5fff457..4b9c1a7 100644 --- a/xsync_errcode.hpp +++ b/xsync_errcode.hpp @@ -11,13 +11,4 @@ #include namespace iflytop { using namespace std; -typedef enum { - kxs_ec_success = 0, - kxs_ec_overtime, - kxs_ec_socket_fail, - kxs_ec_bind_fail, - kxs_ec_send_fail, - kxs_ec_receive_fail, - kxs_ec_setsockopt_rx_timeout_fail, -} xs_error_code_t; } // namespace iflytop