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.

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