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.

251 lines
7.6 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
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, uint32_t &regbackvalue) {
  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. regbackvalue = rxpacket->data[1];
  114. return ecode;
  115. }
  116. xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t &regvalue) {
  117. /**
  118. * @brief
  119. *
  120. *
  121. * kxsync_packet_type_reg_write
  122. * tx: regadd,regdata
  123. * rx: ecode,regdata
  124. */
  125. uint8_t txdata[128] = {0};
  126. uint8_t rxdata[128] = {0};
  127. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  128. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  129. txpacket->type = kxsync_packet_type_cmd;
  130. txpacket->index = txpacket_index++;
  131. txpacket->cmd = kxsync_packet_type_reg_read;
  132. txpacket->ndata = 2;
  133. txpacket->data[0] = regadd;
  134. txpacket->data[1] = regvalue;
  135. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
  136. if (ecode != kxs_ec_success) {
  137. return ecode;
  138. }
  139. regvalue = rxpacket->data[1];
  140. return ecode;
  141. }
  142. xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues) {
  143. /**
  144. * @brief
  145. *
  146. *
  147. * kxsync_packet_type_reg_read_regs
  148. * tx: regstartadd,nreg
  149. * rx: ecode,regdatas
  150. */
  151. uint8_t txdata[128] = {0};
  152. uint8_t rxdata[1280] = {0};
  153. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  154. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  155. txpacket->type = kxsync_packet_type_cmd;
  156. txpacket->index = txpacket_index++;
  157. txpacket->cmd = kxsync_packet_type_reg_read_regs;
  158. txpacket->ndata = 2;
  159. txpacket->data[0] = regadd;
  160. txpacket->data[1] = nreg;
  161. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata));
  162. if (ecode != kxs_ec_success) {
  163. return ecode;
  164. }
  165. if (rxpacket->ndata > 0) {
  166. for (int i = 0; i < rxpacket->ndata - 1; i++) {
  167. regvalues.push_back(rxpacket->data[i + 1]);
  168. }
  169. }
  170. return ecode;
  171. }
  172. void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  173. //
  174. iflytop_timecode_report_packet_t *packet = (iflytop_timecode_report_packet_t *)data;
  175. xysnc_timecode_t timecode;
  176. /**
  177. * @brief
  178. *
  179. * 01 02 03 04 01 02 03 04
  180. *
  181. */
  182. uint8_t frameuints = packet->timecode0 & 0x0f;
  183. uint8_t frame10s = (packet->timecode0 >> 8) & 0x3;
  184. uint8_t seconduints = (packet->timecode0 >> 16) & 0x0f;
  185. uint8_t second10s = (packet->timecode0 >> 24) & 0x03;
  186. uint8_t minuteuints = packet->timecode1 & 0x0f;
  187. uint8_t minute10s = (packet->timecode1 >> 8) & 0x03;
  188. uint8_t houruints = (packet->timecode1 >> 16) & 0x0f;
  189. uint8_t hour10s = (packet->timecode1 >> 24) & 0x03;
  190. timecode.hour = hour10s * 10 + houruints;
  191. timecode.minute = minute10s * 10 + minuteuints;
  192. timecode.second = second10s * 10 + seconduints;
  193. timecode.frame = frame10s * 10 + frameuints;
  194. if (m_on_timecode_msg_cb) m_on_timecode_msg_cb(&timecode);
  195. }
  196. void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  197. uint32_t count = 0;
  198. uint32_t data0 = data[0];
  199. uint32_t data1 = data[1];
  200. uint32_t data2 = data[2];
  201. uint32_t data3 = data[3];
  202. count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
  203. xysnc_camera_sync_data_t camera_sync_data;
  204. camera_sync_data.frameIndex = count;
  205. if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
  206. }