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.

241 lines
7.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "xsync.hpp"
  2. using namespace iflytop;
  3. Xsync::Xsync(/* args */) {}
  4. Xsync &Xsync::Ins() {
  5. static Xsync xsync;
  6. return xsync;
  7. }
  8. void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; }
  9. xs_error_code_t Xsync::connect(string xsync_ip) {
  10. m_xsync_ip = xsync_ip;
  11. disConnect();
  12. /**
  13. * @brief m_xsync_reg_udp
  14. */
  15. xs_error_code_t ecode = kxs_ec_success;
  16. auto xsync_reg_udp = m_xsync_udp_factory->createXSUDP();
  17. ecode = xsync_reg_udp->initialize("0.0.0.0", IFLYTOP_XSYNC_SERVICE_PC_PORT);
  18. if (ecode != kxs_ec_success) {
  19. return ecode;
  20. }
  21. /**
  22. * @brief m_xsync_timecode_udp_listener
  23. */
  24. auto xsync_timecode_udp_listener = m_xsync_udp_factory->createXSUDP();
  25. ecode = xsync_timecode_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT);
  26. if (ecode != kxs_ec_success) {
  27. return ecode;
  28. }
  29. ecode = xsync_timecode_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseTimecodeMsgAndReport(from, data, length); });
  30. if (ecode != kxs_ec_success) {
  31. return ecode;
  32. }
  33. /**
  34. * @brief m_xsync_camera_sync_udp_listener
  35. */
  36. auto xsync_camera_sync_udp_listener = m_xsync_udp_factory->createXSUDP();
  37. ecode = xsync_camera_sync_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT);
  38. if (ecode != kxs_ec_success) {
  39. return ecode;
  40. }
  41. ecode = xsync_camera_sync_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseCameraSyncMsgAndReport(from, data, length); });
  42. if (ecode != kxs_ec_success) {
  43. return ecode;
  44. }
  45. m_xsync_reg_udp = xsync_reg_udp;
  46. m_xsync_timecode_udp_listener = xsync_timecode_udp_listener;
  47. m_xsync_camera_sync_udp_listener = xsync_camera_sync_udp_listener;
  48. m_net_state = kxsync_net_state_connected;
  49. return ecode;
  50. }
  51. xs_error_code_t Xsync::disConnect() {
  52. if (m_xsync_reg_udp != nullptr) {
  53. m_xsync_reg_udp->stopReceive();
  54. m_xsync_reg_udp = nullptr;
  55. }
  56. if (m_xsync_timecode_udp_listener != nullptr) {
  57. m_xsync_timecode_udp_listener->stopReceive();
  58. m_xsync_timecode_udp_listener = nullptr;
  59. }
  60. if (m_xsync_camera_sync_udp_listener != nullptr) {
  61. m_xsync_camera_sync_udp_listener->stopReceive();
  62. m_xsync_camera_sync_udp_listener = nullptr;
  63. }
  64. m_net_state = kxsync_net_state_disconnect;
  65. }
  66. xsync_net_state_t Xsync::getNetState() { return m_net_state; }
  67. void Xsync::regOnTimecodeMsg(xsync_on_timecode_msg_t on_timecode_msg_cb) { m_on_timecode_msg_cb = on_timecode_msg_cb; }
  68. 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; }
  69. 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) {
  70. if (!m_xsync_reg_udp) return kxs_ec_lose_connect;
  71. m_xsync_reg_udp->clearRxBuffer();
  72. cmd->index = txpacket_index++;
  73. XsyncNetAdd toadd = {m_xsync_ip, IFLYTOP_XSYNC_SERVICE_XSYNC_PORT};
  74. xs_error_code_t ecode = //
  75. m_xsync_reg_udp->sendto(toadd, (const char *)cmd, sizeof(iflytop_xsync_packet_header_t) + cmd->ndata * 4, nullptr);
  76. if (ecode != kxs_ec_success) {
  77. return ecode;
  78. }
  79. XsyncNetAdd fromadd;
  80. ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, 100);
  81. if (ecode != kxs_ec_success) {
  82. return ecode;
  83. }
  84. return (xs_error_code_t)rx_data->data[0];
  85. }
  86. xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue) {
  87. /**
  88. * @brief
  89. *
  90. *
  91. * kxsync_packet_type_reg_write
  92. * tx: regadd,regdata
  93. * rx: ecode,regdata
  94. */
  95. uint8_t txdata[128] = {0};
  96. uint8_t rxdata[128] = {0};
  97. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  98. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  99. txpacket->type = kxsync_packet_type_cmd;
  100. txpacket->index = txpacket_index++;
  101. txpacket->cmd = kxsync_packet_type_reg_write;
  102. txpacket->ndata = 2;
  103. txpacket->data[0] = regadd;
  104. txpacket->data[1] = regvalue;
  105. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
  106. if (ecode != kxs_ec_success) {
  107. return ecode;
  108. }
  109. return ecode;
  110. }
  111. xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t &regvalue) {
  112. /**
  113. * @brief
  114. *
  115. *
  116. * kxsync_packet_type_reg_write
  117. * tx: regadd,regdata
  118. * rx: ecode,regdata
  119. */
  120. uint8_t txdata[128] = {0};
  121. uint8_t rxdata[128] = {0};
  122. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  123. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  124. txpacket->type = kxsync_packet_type_cmd;
  125. txpacket->index = txpacket_index++;
  126. txpacket->cmd = kxsync_packet_type_reg_read;
  127. txpacket->ndata = 2;
  128. txpacket->data[0] = regadd;
  129. txpacket->data[1] = regvalue;
  130. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
  131. if (ecode != kxs_ec_success) {
  132. return ecode;
  133. }
  134. regvalue = rxpacket->data[1];
  135. return ecode;
  136. }
  137. xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues) {
  138. /**
  139. * @brief
  140. *
  141. *
  142. * kxsync_packet_type_reg_read_regs
  143. * tx: regstartadd,nreg
  144. * rx: ecode,regdatas
  145. */
  146. uint8_t txdata[128] = {0};
  147. uint8_t rxdata[1280] = {0};
  148. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  149. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  150. txpacket->type = kxsync_packet_type_cmd;
  151. txpacket->index = txpacket_index++;
  152. txpacket->cmd = kxsync_packet_type_reg_read_regs;
  153. txpacket->ndata = 2;
  154. txpacket->data[0] = regadd;
  155. txpacket->data[1] = nreg;
  156. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
  157. if (ecode != kxs_ec_success) {
  158. return ecode;
  159. }
  160. if (rxpacket->ndata > 0) {
  161. for (int i = 0; i < rxpacket->ndata - 1; i++) {
  162. regvalues.push_back(rxpacket->data[i + 1]);
  163. }
  164. }
  165. return ecode;
  166. }
  167. void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  168. //
  169. iflytop_timecode_report_packet_t *packet = (iflytop_timecode_report_packet_t *)data;
  170. xysnc_timecode_t timecode;
  171. /**
  172. * @brief
  173. */
  174. uint8_t frameuints = packet->timecode0 & 0x0f;
  175. uint8_t frame10s = (packet->timecode0 >> 8) & 0x3;
  176. uint8_t seconduints = (packet->timecode0 >> 16) & 0x0f;
  177. uint8_t second10s = (packet->timecode0 >> 24) & 0x03;
  178. uint8_t minuteuints = packet->timecode1 & 0x0f;
  179. uint8_t minute10s = (packet->timecode1 >> 8) & 0x03;
  180. uint8_t houruints = (packet->timecode1 >> 16) & 0x0f;
  181. uint8_t hour10s = (packet->timecode1 >> 24) & 0x03;
  182. timecode.hour = hour10s * 10 + houruints;
  183. timecode.minute = minute10s * 10 + minuteuints;
  184. timecode.second = second10s * 10 + seconduints;
  185. timecode.frame = frame10s * 10 + frameuints;
  186. if (m_on_timecode_msg_cb) m_on_timecode_msg_cb(&timecode);
  187. }
  188. void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  189. uint32_t count = 0;
  190. uint32_t data0 = data[0];
  191. uint32_t data1 = data[1];
  192. uint32_t data2 = data[2];
  193. uint32_t data3 = data[3];
  194. count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
  195. xysnc_camera_sync_data_t camera_sync_data;
  196. camera_sync_data.frameIndex = count;
  197. if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
  198. }