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.

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