diff --git a/i_xsync_udp.hpp b/i_xsync_udp.hpp new file mode 100644 index 0000000..3b53566 --- /dev/null +++ b/i_xsync_udp.hpp @@ -0,0 +1,78 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "xsync_errcode.hpp" +namespace iflytop { +using namespace std; + +class XsyncNetAdd { + public: + XsyncNetAdd(){}; + XsyncNetAdd(string ip, uint32_t port) : ip(ip), port(port) {} + string ip; + uint32_t port; +}; + +class I_XSUDP { + public: + typedef function onMessage_t; + + public: + /** + * @brief 初始化UDP + * + * @param ip localip default 0 + * @param localport localport + * @return int + */ + virtual xs_error_code_t initialize(string ip, int localport) = 0; + /** + * @brief 发送UDP消息 + * + * @param to + * @param data + * @param length + * @return int + * >0 发送成功,返回发送的字节数 + * <0 发送失败 + */ + virtual xs_error_code_t sendto(const XsyncNetAdd& to, const char* data, int32_t length, int32_t* sendlength) = 0; + /** + * @brief 接收UDP消息 + * + * @param data + * @param length + * @param from + * @return int + * >0 接收成功,返回接收的字节数 + * <0 接收失败 + */ + virtual xs_error_code_t receive(char* data, int32_t& length, XsyncNetAdd& from, int overtimems) = 0; + + /** + * @brief 开始接收UDP消息 + * + * @param onMessage + * @return xs_error_code_t + */ + virtual xs_error_code_t startReceive(onMessage_t onMessage) = 0; + virtual xs_error_code_t stopReceive() = 0; + + virtual ~I_XSUDP() {} +}; + +class I_XSUDPFactory { + public: + virtual shared_ptr createXSUDP() = 0; +}; + +} // namespace iflytop diff --git a/iflytop_xsync_protocol b/iflytop_xsync_protocol index dd0c38c..3a13ffc 160000 --- a/iflytop_xsync_protocol +++ b/iflytop_xsync_protocol @@ -1 +1 @@ -Subproject commit dd0c38c767378dbe9010cd7e94bf1bf7cf0e40d5 +Subproject commit 3a13ffc983b65ba14947d88ea5a340d592f1fd22 diff --git a/xsync.cpp b/xsync.cpp index ee2d9b9..cbe9ab2 100644 --- a/xsync.cpp +++ b/xsync.cpp @@ -2,4 +2,83 @@ using namespace iflytop; Xsync::Xsync(/* args */) {} -Xsync::~Xsync() {} \ No newline at end of file +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; + return xsync; +} +void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; } + +void Xsync::connect(string 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 + */ + m_xsync_timecode_udp_listener = m_xsync_udp_factory->createXSUDP(); + m_xsync_timecode_udp_listener->initialize(m_xsync_ip, IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT); + m_xsync_timecode_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseTimecodeMsgAndReport(from, data, length); }); + + /** + * @brief 创建 m_xsync_camera_sync_udp_listener + */ + 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); }); +} +void Xsync::disConnect() { + if (m_xsync_reg_udp != nullptr) { + m_xsync_reg_udp->stopReceive(); + m_xsync_reg_udp = nullptr; + } +} + +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); +} \ No newline at end of file diff --git a/xsync.hpp b/xsync.hpp index ccc34f7..3fba12f 100644 --- a/xsync.hpp +++ b/xsync.hpp @@ -9,86 +9,72 @@ #include #include #include +// +#include "i_xsync_udp.hpp" +#include "iflytop_xsync_protocol/iflytop_xsync_protocol.h" +#include "xsync_errcode.hpp" 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; - -class XsyncNetAdd { - public: - XsyncNetAdd(){}; - XsyncNetAdd(string ip, uint32_t port) : ip(ip), port(port) {} - string ip; - uint32_t port; -}; + kxsync_net_state_disconnect, + kxsync_net_state_connecting, + kxsync_net_state_connected, +} xsync_net_state_t; +typedef struct { + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t frame; +} xysnc_timecode_t; -class I_XSUDP { - public: - typedef function onMessage_t; +typedef struct { + uint32_t frameIndex; +} xysnc_camera_sync_data_t; - public: - /** - * @brief 初始化UDP - * - * @param ip localip default 0 - * @param localport localport - * @return int - */ - virtual xs_error_code_t initialize(string ip, int localport) = 0; - /** - * @brief 发送UDP消息 - * - * @param to - * @param data - * @param length - * @return int - * >0 发送成功,返回发送的字节数 - * <0 发送失败 - */ - virtual xs_error_code_t sendto(const XsyncNetAdd& to, const char* data, int32_t length, int32_t* sendlength) = 0; - /** - * @brief 接收UDP消息 - * - * @param data - * @param length - * @param from - * @return int - * >0 接收成功,返回接收的字节数 - * <0 接收失败 - */ - virtual xs_error_code_t receive(char* data, int32_t& length, XsyncNetAdd& from, int overtimems) = 0; - - /** - * @brief 开始接收UDP消息 - * - * @param onMessage - * @return xs_error_code_t - */ - virtual xs_error_code_t startReceive(onMessage_t onMessage) = 0; - - virtual ~I_XSUDP() {} -}; - -class I_XSyncUDPFactory { - public: - virtual shared_ptr createXSUDP() = 0; -}; +typedef function xsync_on_timecode_msg_t; +typedef function xsync_on_camera_sync_msg_t; class Xsync { + public: private: /* data */ + + I_XSUDPFactory *m_xsync_udp_factory = nullptr; + shared_ptr m_xsync_reg_udp = nullptr; + shared_ptr m_xsync_timecode_udp_listener = nullptr; + shared_ptr m_xsync_camera_sync_udp_listener = nullptr; + + string m_xsync_ip; + bool m_is_connected = false; + + 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 */){}; + public: - Xsync(/* args */); - ~Xsync(); + static Xsync &Ins(); + + void initialize(I_XSUDPFactory *xsync_udp_factory); + + bool ping(string xsync_ip); + + void connect(string xsync_ip); + void disConnect(); + xsync_net_state_t getNetState(); + + void regOnTimecodeMsg(xsync_on_timecode_msg_t on_timecode_msg_cb); + void regOnCameraSyncMsg(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb); + + 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); + + private: + void parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length); + void parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length); }; } // namespace iflytop diff --git a/xsync_errcode.hpp b/xsync_errcode.hpp new file mode 100644 index 0000000..5fff457 --- /dev/null +++ b/xsync_errcode.hpp @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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