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.

890 lines
28 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
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
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
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
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
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. #include <string.h>
  3. #include <map>
  4. #define ENABLE_LOG
  5. #ifdef ENABLE_LOG
  6. #include "../src/logger.hpp"
  7. #endif
  8. #define TAG "XSYNC"
  9. using namespace xsync;
  10. using namespace std;
  11. /**
  12. * @brief XSYNC协议端口
  13. */
  14. #define IFLYTOP_XSYNC_SERVICE_XSYNC_PORT 19900 // xsync端端口
  15. #define IFLYTOP_XSYNC_SERVICE_PC_PORT 19901 // pc 端端口
  16. #define IFLYTOP_XSYNC_TIMECODE_REPORT_XSYNC_PORT 19902 // xsync端端口
  17. #define IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT 19903 // pc端端口
  18. #define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_XSYNC_PORT 13013 // xsync端端口
  19. #define IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT 13014 // pc端端口
  20. #define DO_XSYNC(exptr) \
  21. { \
  22. xs_error_code_t ecode = exptr; \
  23. if (ecode != kxs_ec_success) return ecode; \
  24. }
  25. static uint32_t ipToUint32(const std::string &ipAddress, bool &suc) {
  26. uint32_t result = 0;
  27. std::istringstream iss(ipAddress);
  28. std::string segment;
  29. int i = 0;
  30. while (std::getline(iss, segment, '.')) {
  31. uint32_t octet = std::stoi(segment);
  32. if (octet > 255) {
  33. suc = false;
  34. return 0;
  35. }
  36. result |= (octet << ((3 - i) * 8));
  37. i++;
  38. }
  39. if (i != 4) {
  40. suc = false;
  41. return 0;
  42. }
  43. suc = true;
  44. uint32_t result_n = 0;
  45. result_n |= ((result & 0xff000000) >> 24);
  46. result_n |= ((result & 0x00ff0000) >> 8);
  47. result_n |= ((result & 0x0000ff00) << 8);
  48. result_n |= ((result & 0x000000ff) << 24);
  49. return result_n;
  50. }
  51. namespace xsync {
  52. namespace ttlout_module {
  53. static map<string, TriggerSigType_t> TriggerSigType2StrMap = {
  54. {"logic0", tri_logic0},
  55. {"logic1", tri_logic1},
  56. {"ttlin1_module_ext", tri_ttlin1_module_ext},
  57. {"ttlin1_module_divide", tri_ttlin1_module_divide},
  58. {"ttlin2_module_ext", tri_ttlin2_module_ext},
  59. {"ttlin2_module_divide", tri_ttlin2_module_divide},
  60. {"ttlin3_module_ext", tri_ttlin3_module_ext},
  61. {"ttlin3_module_divide", tri_ttlin3_module_divide},
  62. {"ttlin4_module_ext", tri_ttlin4_module_ext},
  63. {"ttlin4_module_divide", tri_ttlin4_module_divide},
  64. {"internal_en_flag", tri_internal_en_flag},
  65. {"genlock_frame_sync_ext", tri_genlock_frame_sync_ext},
  66. {"genlock_frame_sync_internal", tri_genlock_frame_sync_internal},
  67. {"timecode_frame_sync_ext", tri_timecode_frame_sync_ext},
  68. {"timecode_frame_sync_internal", tri_timecode_frame_sync_internal},
  69. {"timecode_serial_data_ext", tri_timecode_serial_data_ext},
  70. {"timecode_serial_data_internal", tri_timecode_serial_data_internal},
  71. {"internal_100hz", tri_internal_100hz},
  72. };
  73. static map<string, OutputSigType_t> Str2TriggerSigTypeMap = {
  74. {"OutSigType_logic0", OutSigType_logic0}, //
  75. {"OutSigType_logic1", OutSigType_logic1}, //
  76. {"OutSigType_test_signal", OutSigType_test_signal}, //
  77. {"OutSigType_input_signal", OutSigType_input_signal}, //
  78. {"OutSigType_input_signal_mirror", OutSigType_input_signal_mirror}, //
  79. {"OutSigType_trigger_mode_signal", OutSigType_trigger_mode_signal}, //
  80. {"OutSigType_trigger_mode_signal_mirror", OutSigType_trigger_mode_signal_mirror},
  81. };
  82. string TriggerSigType2Str(TriggerSigType_t type) {
  83. for (auto &item : TriggerSigType2StrMap) {
  84. if (item.second == type) return item.first;
  85. }
  86. return "unkown";
  87. }
  88. TriggerSigType_t Str2TriggerSigType(string type) {
  89. auto it = TriggerSigType2StrMap.find(type);
  90. if (it != TriggerSigType2StrMap.end()) {
  91. return it->second;
  92. }
  93. return tri_logic0;
  94. }
  95. string OutputSigType2Str(OutputSigType_t type) {
  96. for (auto &item : Str2TriggerSigTypeMap) {
  97. if (item.second == type) return item.first;
  98. }
  99. return "unkown";
  100. }
  101. OutputSigType_t Str2OutputSigType(string type) {
  102. auto it = Str2TriggerSigTypeMap.find(type);
  103. if (it != Str2TriggerSigTypeMap.end()) {
  104. return it->second;
  105. }
  106. return OutSigType_logic0;
  107. }
  108. list<string> TriggerSigTypeStrSet() {
  109. list<string> ret;
  110. for (auto &item : TriggerSigType2StrMap) {
  111. ret.push_back(item.first);
  112. }
  113. return ret;
  114. }
  115. list<string> OutputSigTypeStrSet() {
  116. list<string> ret;
  117. for (auto &item : Str2TriggerSigTypeMap) {
  118. ret.push_back(item.first);
  119. }
  120. return ret;
  121. }
  122. } // namespace ttlout_module
  123. namespace sig_generator_module {
  124. static map<string, ControlMode_t> Str2ControlModeMap = {
  125. {"manualTrigger", kControlMode_manualTrigger}, //
  126. {"extTimecodeTrigger", kControlMode_externalTimecodeTrigger},
  127. {"ttl1Trigger", kControlMode_externalTTL1Trigger},
  128. {"ttl2Trigger", kControlMode_externalTTL2Trigger},
  129. {"ttl3Trigger", kControlMode_externalTTL3Trigger},
  130. {"ttl4Trigger", kControlMode_externalTTL4Trigger},
  131. };
  132. string ControlMode2Str(ControlMode_t mode) {
  133. for (auto &item : Str2ControlModeMap) {
  134. if (item.second == mode) return item.first;
  135. }
  136. return "unkown";
  137. }
  138. ControlMode_t Str2ControlMode(string mode) {
  139. auto it = Str2ControlModeMap.find(mode);
  140. if (it != Str2ControlModeMap.end()) {
  141. return it->second;
  142. }
  143. return kControlMode_manualTrigger;
  144. }
  145. list<string> ControlModeStrSet() {
  146. list<string> ret;
  147. for (auto &item : Str2ControlModeMap) {
  148. ret.push_back(item.first);
  149. }
  150. return ret;
  151. }
  152. } // namespace sig_generator_module
  153. static map<string, TimecodeFormat_t> Str2TimecodeFormatMap = {
  154. {"fps2398", TIMECODE_FPS2398}, //
  155. {"fps2400", TIMECODE_FPS2400}, //
  156. {"fps2500", TIMECODE_FPS2500}, //
  157. {"fps2997", TIMECODE_FPS2997}, //
  158. {"fps2997Drop", TIMECODE_FPS2997Drop}, {"fps3000", TIMECODE_FPS3000}, //
  159. };
  160. static map<string, GenlockFormat_t> Str2GenlockFormatMap = {
  161. {"fps2397", GENLOCK_FPS2397}, //
  162. {"fps2398", GENLOCK_FPS2398}, //
  163. {"fps2400", GENLOCK_FPS2400}, //
  164. {"fps2500", GENLOCK_FPS2500}, //
  165. {"fps2997", GENLOCK_FPS2997}, //
  166. {"fps3000", GENLOCK_FPS3000}, //
  167. {"fps5000", GENLOCK_FPS5000}, //
  168. {"fps5994", GENLOCK_FPS5994}, //
  169. {"fps6000", GENLOCK_FPS6000}, //
  170. };
  171. string GenlockFormatToStr(GenlockFormat_t fomrat) {
  172. for (auto &item : Str2GenlockFormatMap) {
  173. if (item.second == fomrat) return item.first;
  174. }
  175. return "unkown";
  176. }
  177. string TimecodeFormatToStr(TimecodeFormat_t fomrat) {
  178. for (auto &item : Str2TimecodeFormatMap) {
  179. if (item.second == fomrat) return item.first;
  180. }
  181. return "unkown";
  182. }
  183. GenlockFormat_t Str2GenlockFormat(string format) {
  184. auto it = Str2GenlockFormatMap.find(format);
  185. if (it != Str2GenlockFormatMap.end()) {
  186. return it->second;
  187. }
  188. return GENLOCK_FPS2397;
  189. }
  190. TimecodeFormat_t Str2TimecodeFormat(string format) {
  191. auto it = Str2TimecodeFormatMap.find(format);
  192. if (it != Str2TimecodeFormatMap.end()) {
  193. return it->second;
  194. }
  195. return TIMECODE_FPS2398;
  196. }
  197. list<string> GenlockFormatStrSet() {
  198. list<string> ret;
  199. for (auto &item : Str2GenlockFormatMap) {
  200. ret.push_back(item.first);
  201. }
  202. return ret;
  203. }
  204. list<string> TimecodeFormatStrSet() {
  205. list<string> ret;
  206. for (auto &item : Str2TimecodeFormatMap) {
  207. ret.push_back(item.first);
  208. }
  209. return ret;
  210. }
  211. string XsyncTimecodeToStr(XsyncTimecode_t timecode) {
  212. char buf[32] = {0};
  213. sprintf(buf, "%02d:%02d:%02d:%02d", timecode.hour, timecode.minute, timecode.second, timecode.frame);
  214. return string(buf);
  215. }
  216. XsyncTimecode_t Str2XsyncTimecode(string timecode) {
  217. XsyncTimecode_t ret;
  218. char buf[128] = {0};
  219. strncpy(buf, timecode.c_str(), 127);
  220. sscanf(buf, "%02d:%02d:%02d:%02d", &ret.hour, &ret.minute, &ret.second, &ret.frame);
  221. return ret;
  222. }
  223. } // namespace xsync
  224. static XsyncTimecode_t timecode64ToXsyncTimeCode(Timecode64_t tc64) {
  225. uint8_t frameuints = tc64.tc0 & 0x0f;
  226. uint8_t frame10s = (tc64.tc0 >> 8) & 0x3;
  227. uint8_t seconduints = (tc64.tc0 >> 16) & 0x0f;
  228. uint8_t second10s = (tc64.tc0 >> 24) & 0x07;
  229. uint8_t minuteuints = tc64.tc1 & 0x0f;
  230. uint8_t minute10s = (tc64.tc1 >> 8) & 0x07;
  231. uint8_t houruints = (tc64.tc1 >> 16) & 0x0f;
  232. uint8_t hour10s = (tc64.tc1 >> 24) & 0x03;
  233. XsyncTimecode_t timecode;
  234. timecode.hour = hour10s * 10 + houruints;
  235. timecode.minute = minute10s * 10 + minuteuints;
  236. timecode.second = second10s * 10 + seconduints;
  237. timecode.frame = frame10s * 10 + frameuints;
  238. return timecode;
  239. }
  240. static Timecode64_t timecodeTo64(XsyncTimecode_t tc) {
  241. Timecode64_t tc64;
  242. uint32_t frameuints = tc.frame % 10;
  243. uint32_t frame10s = tc.frame / 10;
  244. uint32_t seconduints = tc.second % 10;
  245. uint32_t second10s = tc.second / 10;
  246. uint32_t minuteuints = tc.minute % 10;
  247. uint32_t minute10s = tc.minute / 10;
  248. uint32_t houruints = tc.hour % 10;
  249. uint32_t hour10s = tc.hour / 10;
  250. tc64.tc0 = frameuints + (frame10s << 8) + (seconduints << 16) + (second10s << 24);
  251. tc64.tc1 = minuteuints + (minute10s << 8) + (houruints << 16) + (hour10s << 24);
  252. return tc64;
  253. }
  254. /*******************************************************************************
  255. * Xsync *
  256. *******************************************************************************/
  257. Xsync::Xsync(/* args */) {}
  258. Xsync &Xsync::Ins() {
  259. static Xsync xsync;
  260. return xsync;
  261. }
  262. void Xsync::initialize(I_XSUDPFactory *xsync_udp_factory) { m_xsync_udp_factory = xsync_udp_factory; }
  263. xs_error_code_t Xsync::connect(string xsync_ip) {
  264. lock_guard<recursive_mutex> lock(lock_);
  265. m_xsync_ip = xsync_ip;
  266. disConnect();
  267. /**
  268. * @brief m_xsync_reg_udp
  269. */
  270. xs_error_code_t ecode = kxs_ec_success;
  271. auto xsync_reg_udp = m_xsync_udp_factory->createXSUDP();
  272. ecode = xsync_reg_udp->initialize("0.0.0.0", IFLYTOP_XSYNC_SERVICE_PC_PORT);
  273. if (ecode != kxs_ec_success) {
  274. return ecode;
  275. }
  276. /**
  277. * @brief m_xsync_timecode_udp_listener
  278. */
  279. auto xsync_timecode_udp_listener = m_xsync_udp_factory->createXSUDP();
  280. ecode = xsync_timecode_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_TIMECODE_REPORT_PC_PORT);
  281. if (ecode != kxs_ec_success) {
  282. return ecode;
  283. }
  284. ecode = xsync_timecode_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseTimecodeMsgAndReport(from, data, length); });
  285. if (ecode != kxs_ec_success) {
  286. return ecode;
  287. }
  288. /**
  289. * @brief m_xsync_camera_sync_udp_listener
  290. */
  291. auto xsync_camera_sync_udp_listener = m_xsync_udp_factory->createXSUDP();
  292. ecode = xsync_camera_sync_udp_listener->initialize("0.0.0.0", IFLYTOP_XSYNC_CAMERA_SYNC_PACKET_PC_PORT);
  293. if (ecode != kxs_ec_success) {
  294. return ecode;
  295. }
  296. ecode = xsync_camera_sync_udp_listener->startReceive([this](XsyncNetAdd &from, uint8_t *data, size_t length) { parseCameraSyncMsgAndReport(from, data, length); });
  297. if (ecode != kxs_ec_success) {
  298. return ecode;
  299. }
  300. m_xsync_reg_udp = xsync_reg_udp;
  301. m_xsync_timecode_udp_listener = xsync_timecode_udp_listener;
  302. m_xsync_camera_sync_udp_listener = xsync_camera_sync_udp_listener;
  303. m_net_state = kxsync_net_state_connected;
  304. return ecode;
  305. }
  306. xs_error_code_t Xsync::disConnect() {
  307. lock_guard<recursive_mutex> lock(lock_);
  308. if (m_xsync_reg_udp != nullptr) {
  309. m_xsync_reg_udp->stopReceive();
  310. m_xsync_reg_udp = nullptr;
  311. }
  312. if (m_xsync_timecode_udp_listener != nullptr) {
  313. m_xsync_timecode_udp_listener->stopReceive();
  314. m_xsync_timecode_udp_listener = nullptr;
  315. }
  316. if (m_xsync_camera_sync_udp_listener != nullptr) {
  317. m_xsync_camera_sync_udp_listener->stopReceive();
  318. m_xsync_camera_sync_udp_listener = nullptr;
  319. }
  320. m_net_state = kxsync_net_state_disconnect;
  321. return kxs_ec_success;
  322. }
  323. xsync_net_state_t Xsync::getNetState() { return m_net_state; }
  324. void Xsync::Basic_registerOnTimecodeMsgCallback(xsync_on_timecode_msg_t on_timecode_msg_cb) { m_on_timecode_msg_cb = on_timecode_msg_cb; }
  325. void Xsync::Basic_registerOnCameraSyncMsgCallback(xsync_on_camera_sync_msg_t on_camera_sync_msg_cb) { m_on_camera_sync_msg_cb = on_camera_sync_msg_cb; }
  326. 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, int32_t overtime_ms) {
  327. lock_guard<recursive_mutex> lock(lock_);
  328. if (!m_xsync_reg_udp) return kxs_ec_lose_connect;
  329. m_xsync_reg_udp->clearRxBuffer();
  330. cmd->index = txpacket_index++;
  331. XsyncNetAdd toadd = {m_xsync_ip, IFLYTOP_XSYNC_SERVICE_XSYNC_PORT};
  332. xs_error_code_t ecode = //
  333. m_xsync_reg_udp->sendto(toadd, (const char *)cmd, sizeof(iflytop_xsync_packet_header_t) + cmd->ndata * 4, nullptr);
  334. if (ecode != kxs_ec_success) {
  335. return ecode;
  336. }
  337. XsyncNetAdd fromadd;
  338. while (true) {
  339. // ZLOGI(TAG, "start rx wait for rxdata");
  340. ecode = m_xsync_reg_udp->receive((char *)rx_data, buffersize, fromadd, overtime_ms);
  341. // ZLOGI(TAG, "end rx wait for rxdata");
  342. if (ecode != kxs_ec_success) {
  343. return ecode;
  344. }
  345. if (rx_data->index != cmd->index) {
  346. // ZLOGI(TAG, "packet index error %d %d", cmd->index, rx_data->index);
  347. continue;
  348. }
  349. break;
  350. }
  351. return (xs_error_code_t)rx_data->data[0];
  352. }
  353. xs_error_code_t Xsync::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t &regbackvalue, int32_t overtime_ms) {
  354. /**
  355. * @brief
  356. *
  357. *
  358. * kxsync_packet_type_reg_write
  359. * tx: regadd,regdata
  360. * rx: ecode,regdata
  361. */
  362. uint8_t txdata[128] = {0};
  363. uint8_t rxdata[128] = {0};
  364. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  365. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  366. txpacket->type = kxsync_packet_type_cmd;
  367. txpacket->index = txpacket_index++;
  368. txpacket->cmd = kxsync_packet_type_reg_write;
  369. txpacket->ndata = 2;
  370. txpacket->data[0] = regadd;
  371. txpacket->data[1] = regvalue;
  372. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  373. if (ecode != kxs_ec_success) {
  374. return ecode;
  375. }
  376. regbackvalue = rxpacket->data[1];
  377. return ecode;
  378. }
  379. xs_error_code_t Xsync::reg_read(uint32_t regadd, uint32_t &regvalue, int32_t overtime_ms) {
  380. /**
  381. * @brief
  382. *
  383. *
  384. * kxsync_packet_type_reg_write
  385. * tx: regadd,regdata
  386. * rx: ecode,regdata
  387. */
  388. uint8_t txdata[128] = {0};
  389. uint8_t rxdata[128] = {0};
  390. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  391. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  392. txpacket->type = kxsync_packet_type_cmd;
  393. txpacket->index = txpacket_index++;
  394. txpacket->cmd = kxsync_packet_type_reg_read;
  395. txpacket->ndata = 2;
  396. txpacket->data[0] = regadd;
  397. txpacket->data[1] = regvalue;
  398. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  399. if (ecode != kxs_ec_success) {
  400. return ecode;
  401. }
  402. regvalue = rxpacket->data[1];
  403. return ecode;
  404. }
  405. xs_error_code_t Xsync::reg_read_muti(uint32_t regadd, uint32_t nreg, vector<uint32_t> &regvalues, int32_t overtime_ms) {
  406. /**
  407. * @brief
  408. *
  409. *
  410. * kxsync_packet_type_reg_read_regs
  411. * tx: regstartadd,nreg
  412. * rx: ecode,regdatas
  413. */
  414. uint8_t txdata[128] = {0};
  415. uint8_t rxdata[1280] = {0};
  416. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txdata;
  417. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)rxdata;
  418. txpacket->type = kxsync_packet_type_cmd;
  419. txpacket->index = txpacket_index++;
  420. txpacket->cmd = kxsync_packet_type_reg_read_regs;
  421. txpacket->ndata = 2;
  422. txpacket->data[0] = regadd;
  423. txpacket->data[1] = nreg;
  424. auto ecode = xsync_send_cmd_block(txpacket, rxpacket, sizeof(rxdata), overtime_ms);
  425. if (ecode != kxs_ec_success) {
  426. return ecode;
  427. }
  428. if (rxpacket->ndata > 0) {
  429. for (int i = 0; i < rxpacket->ndata - 1; i++) {
  430. regvalues.push_back(rxpacket->data[i + 1]);
  431. }
  432. }
  433. return ecode;
  434. }
  435. void Xsync::parseTimecodeMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  436. //
  437. iflytop_timecode_report_packet_t *packet = (iflytop_timecode_report_packet_t *)data;
  438. Timecode64_t tc64;
  439. tc64.tc0 = packet->timecode0;
  440. tc64.tc1 = packet->timecode1;
  441. XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64);
  442. if (m_on_timecode_msg_cb) m_on_timecode_msg_cb(&timecode);
  443. }
  444. void Xsync::parseCameraSyncMsgAndReport(XsyncNetAdd &from, uint8_t *data, size_t length) {
  445. uint32_t count = 0;
  446. uint32_t data0 = data[7];
  447. uint32_t data1 = data[6];
  448. uint32_t data2 = data[5];
  449. uint32_t data3 = data[4];
  450. count = data0 + (data1 << 8) + (data2 << 16) + (data3 << 24);
  451. xysnc_camera_sync_data_t camera_sync_data;
  452. camera_sync_data.frameIndex = count;
  453. if (m_on_camera_sync_msg_cb) m_on_camera_sync_msg_cb(&camera_sync_data);
  454. }
  455. xs_error_code_t Xsync::Basic_generatorNewMac() { return doaction(xsync_stm32_action_generator_new_mac, 0, nullptr, 2000); }
  456. xs_error_code_t Xsync::Basic_factoryReset() { return doaction(xsync_stm32_action_factory_reset, 0, nullptr, 1000); }
  457. xs_error_code_t Xsync::Basic_reboot() { return doaction(xsync_stm32_action_Basic_reboot, 0, nullptr); }
  458. xs_error_code_t Xsync::storageConfig() { return doaction(xsync_stm32_action_storage_cfg, 0, nullptr, 1000); }
  459. xs_error_code_t Xsync::Basic_changeNetworkConfig(string ip, string mask, string gateway) {
  460. uint32_t ip32 = 0;
  461. uint32_t mask32 = 0;
  462. uint32_t gateway32 = 0;
  463. xs_error_code_t ecode;
  464. bool suc = false;
  465. ip32 = (uint32_t)ipToUint32(ip.c_str(), suc);
  466. if (!suc) return kxs_ec_param_error;
  467. mask32 = (uint32_t)ipToUint32(mask.c_str(), suc);
  468. if (!suc) return kxs_ec_param_error;
  469. gateway32 = (uint32_t)ipToUint32(gateway.c_str(), suc);
  470. if (!suc) return kxs_ec_param_error;
  471. uint32_t readbak = 0;
  472. ecode = reg_write(reg::kstm32_ip, ip32, readbak);
  473. if (ecode != kxs_ec_success) return ecode;
  474. ecode = reg_write(reg::kstm32_netmask, mask32, readbak);
  475. if (ecode != kxs_ec_success) return ecode;
  476. ecode = reg_write(reg::kstm32_gw, gateway32, readbak);
  477. if (ecode != kxs_ec_success) return ecode;
  478. ecode = storageConfig();
  479. if (ecode != kxs_ec_success) return ecode;
  480. return kxs_ec_success;
  481. }
  482. xs_error_code_t Xsync::Basic_clearXsyncCameraSyncIndexCount() {
  483. uint32_t readbak = 0;
  484. return reg_write(reg::kstm32_camera_sync_signal_count, 0, readbak);
  485. }
  486. xs_error_code_t Xsync::doaction(uint32_t action, uint32_t actionval, uint32_t *ackreturn, int32_t overtime_ms) {
  487. //
  488. uint32_t readbak = 0;
  489. xs_error_code_t ecode;
  490. ecode = reg_write(reg::kstm32_action_val0, actionval, readbak);
  491. if (ecode != kxs_ec_success) return ecode;
  492. ecode = reg_write(reg::kstm32_action0, action, readbak, overtime_ms);
  493. if (ecode != kxs_ec_success) return ecode;
  494. if (ackreturn) *ackreturn = readbak;
  495. return ecode;
  496. }
  497. xs_error_code_t Xsync::Basic_setGenlockFormat(GenlockFormat_t format) {
  498. DO_XSYNC(SigGenerator_setGenlockFormat(format));
  499. return kxs_ec_success;
  500. }
  501. xs_error_code_t Xsync::Basic_getGenlockFormat(GenlockFormat_t &format) {
  502. DO_XSYNC(SigGenerator_getGenlockFormat(format));
  503. return kxs_ec_success;
  504. }
  505. xs_error_code_t Xsync::Basic_setTimecodeFormat(TimecodeFormat_t format) {
  506. DO_XSYNC(SigGenerator_setTimecodeFormat(format));
  507. return kxs_ec_success;
  508. }
  509. xs_error_code_t Xsync::Basic_getTimecodeFormat(TimecodeFormat_t &format) {
  510. DO_XSYNC(SigGenerator_getTimecodeFormat(format));
  511. return kxs_ec_success;
  512. }
  513. /*******************************************************************************
  514. * TTLOutputModule *
  515. *******************************************************************************/
  516. xs_error_code_t Xsync::TTLOutputModule_setInputSigType(int32_t index, ttlout_module::TriggerSigType_t source) {
  517. xs_error_code_t ecode = kxs_ec_success;
  518. uint32_t readbak = 0;
  519. uint32_t regAdd = 0;
  520. if (index == 1) {
  521. regAdd = reg::kttlout1_input_sig_slt;
  522. } else if (index == 2) {
  523. regAdd = reg::kttlout2_input_sig_slt;
  524. } else if (index == 3) {
  525. regAdd = reg::kttlout3_input_sig_slt;
  526. } else if (index == 4) {
  527. regAdd = reg::kttlout4_input_sig_slt;
  528. } else {
  529. return kxs_ec_param_error;
  530. }
  531. return reg_write(regAdd, source, readbak, 10);
  532. }
  533. xs_error_code_t Xsync::TTLOutputModule_getInputSigType(int32_t index, ttlout_module::TriggerSigType_t &source) {
  534. xs_error_code_t ecode = kxs_ec_success;
  535. uint32_t readbak = 0;
  536. uint32_t regAdd = 0;
  537. if (index == 1) {
  538. regAdd = reg::kttlout1_input_sig_slt;
  539. } else if (index == 2) {
  540. regAdd = reg::kttlout2_input_sig_slt;
  541. } else if (index == 3) {
  542. regAdd = reg::kttlout3_input_sig_slt;
  543. } else if (index == 4) {
  544. regAdd = reg::kttlout4_input_sig_slt;
  545. } else {
  546. return kxs_ec_param_error;
  547. }
  548. ecode = reg_read(regAdd, readbak, 10);
  549. if (ecode != kxs_ec_success) return ecode;
  550. source = (ttlout_module::TriggerSigType_t)readbak;
  551. return ecode;
  552. }
  553. xs_error_code_t Xsync::TTLOutputModule_setOutputSigType(int32_t index, ttlout_module::OutputSigType_t output_type) {
  554. xs_error_code_t ecode = kxs_ec_success;
  555. uint32_t readbak = 0;
  556. uint32_t regAdd = 0;
  557. if (index == 1) {
  558. regAdd = reg::kttlout1_output_sig_slt;
  559. } else if (index == 2) {
  560. regAdd = reg::kttlout2_output_sig_slt;
  561. } else if (index == 3) {
  562. regAdd = reg::kttlout3_output_sig_slt;
  563. } else if (index == 4) {
  564. regAdd = reg::kttlout4_output_sig_slt;
  565. } else {
  566. return kxs_ec_param_error;
  567. }
  568. return reg_write(regAdd, output_type, readbak, 10);
  569. }
  570. xs_error_code_t Xsync::TTLOutputModule_getOutputSigType(int32_t index, ttlout_module::OutputSigType_t &output_type) {
  571. xs_error_code_t ecode = kxs_ec_success;
  572. uint32_t readbak = 0;
  573. uint32_t regAdd = 0;
  574. if (index == 1) {
  575. regAdd = reg::kttlout1_output_sig_slt;
  576. } else if (index == 2) {
  577. regAdd = reg::kttlout2_output_sig_slt;
  578. } else if (index == 3) {
  579. regAdd = reg::kttlout3_output_sig_slt;
  580. } else if (index == 4) {
  581. regAdd = reg::kttlout4_output_sig_slt;
  582. } else {
  583. return kxs_ec_param_error;
  584. }
  585. ecode = reg_read(regAdd, readbak, 10);
  586. if (ecode != kxs_ec_success) return ecode;
  587. output_type = (ttlout_module::OutputSigType_t)readbak;
  588. return ecode;
  589. }
  590. xs_error_code_t Xsync::TTLOutputModule_setTriggerModePulseWidth(int32_t index, uint32_t pulse_width_ms) {
  591. xs_error_code_t ecode = kxs_ec_success;
  592. uint32_t readbak = 0;
  593. uint32_t regAdd = 0;
  594. if (index == 1) {
  595. regAdd = reg::kttlout1_pulse_mode_duration;
  596. } else if (index == 2) {
  597. regAdd = reg::kttlout2_pulse_mode_duration;
  598. } else if (index == 3) {
  599. regAdd = reg::kttlout3_pulse_mode_duration;
  600. } else if (index == 4) {
  601. regAdd = reg::kttlout4_pulse_mode_duration;
  602. } else {
  603. return kxs_ec_param_error;
  604. }
  605. uint32_t pluse_width = pulse_width_ms * 1000; // 1MHZ计数
  606. return reg_write(regAdd, pluse_width, readbak, 10);
  607. }
  608. xs_error_code_t Xsync::TTLOutputModule_getTriggerModePulseWidth(int32_t index, uint32_t &pulse_width_ms) {
  609. xs_error_code_t ecode = kxs_ec_success;
  610. uint32_t regAdd = 0;
  611. uint32_t readbak = 0;
  612. if (index == 1) {
  613. regAdd = reg::kttlout1_pulse_mode_duration;
  614. } else if (index == 2) {
  615. regAdd = reg::kttlout2_pulse_mode_duration;
  616. } else if (index == 3) {
  617. regAdd = reg::kttlout3_pulse_mode_duration;
  618. } else if (index == 4) {
  619. regAdd = reg::kttlout4_pulse_mode_duration;
  620. } else {
  621. return kxs_ec_param_error;
  622. }
  623. ecode = reg_read(regAdd, regAdd, 10);
  624. if (ecode != kxs_ec_success) return ecode;
  625. pulse_width_ms = readbak / 1000;
  626. return ecode;
  627. }
  628. xs_error_code_t Xsync::TTLOutputModule_setTriggerModePulseDelay(int32_t index, uint32_t pulse_delay_ms) {
  629. xs_error_code_t ecode = kxs_ec_success;
  630. uint32_t regAdd = 0;
  631. uint32_t readbak = 0;
  632. if (index == 1) {
  633. regAdd = reg::kttlout1_pulse_mode_delay;
  634. } else if (index == 2) {
  635. regAdd = reg::kttlout2_pulse_mode_delay;
  636. } else if (index == 3) {
  637. regAdd = reg::kttlout3_pulse_mode_delay;
  638. } else if (index == 4) {
  639. regAdd = reg::kttlout4_pulse_mode_delay;
  640. } else {
  641. return kxs_ec_param_error;
  642. }
  643. uint32_t pulse_delay = pulse_delay_ms * 1000; // 1MHZ计数
  644. return reg_write(regAdd, pulse_delay, readbak, 10);
  645. }
  646. xs_error_code_t Xsync::TTLOutputModule_getTriggerModePulseDelay(int32_t index, uint32_t &pulse_delay_ms) {
  647. xs_error_code_t ecode = kxs_ec_success;
  648. uint32_t regAdd = 0;
  649. uint32_t readbak = 0;
  650. if (index == 1) {
  651. regAdd = reg::kttlout1_pulse_mode_delay;
  652. } else if (index == 2) {
  653. regAdd = reg::kttlout2_pulse_mode_delay;
  654. } else if (index == 3) {
  655. regAdd = reg::kttlout3_pulse_mode_delay;
  656. } else if (index == 4) {
  657. regAdd = reg::kttlout4_pulse_mode_delay;
  658. } else {
  659. return kxs_ec_param_error;
  660. }
  661. ecode = reg_read(regAdd, readbak, 10);
  662. if (ecode != kxs_ec_success) return ecode;
  663. pulse_delay_ms = readbak / 1000;
  664. return ecode;
  665. }
  666. xs_error_code_t Xsync::SigGenerator_setControlMode(sig_generator_module::ControlMode_t mode) {
  667. uint32_t readbak = 0;
  668. return reg_write(reg::kSigGenerator_ctl, mode, readbak, 10);
  669. }
  670. xs_error_code_t Xsync::SigGenerator_getControlMode(sig_generator_module::ControlMode_t &mode) {
  671. uint32_t readbak = 0;
  672. auto ecode = reg_read(reg::kSigGenerator_ctl, readbak, 10);
  673. if (ecode != kxs_ec_success) return ecode;
  674. mode = (sig_generator_module::ControlMode_t)readbak;
  675. return ecode;
  676. }
  677. xs_error_code_t Xsync::SigGenerator_manualStart() {
  678. uint32_t readbak = 0;
  679. return reg_write(reg::kSigGenerator_control_trigger_reg, 1, readbak, 10);
  680. }
  681. xs_error_code_t Xsync::SigGenerator_manualStop() {
  682. uint32_t readbak = 0;
  683. return reg_write(reg::kSigGenerator_control_trigger_reg, 0, readbak, 10);
  684. }
  685. xs_error_code_t Xsync::SigGenerator_setTimecode(XsyncTimecode_t timecode) {
  686. uint32_t readbak = 0;
  687. xs_error_code_t ecode = kxs_ec_success;
  688. Timecode64_t tc64 = timecodeTo64(timecode);
  689. ecode = reg_write(reg::kSigGenerator_timecode0, tc64.tc0, readbak, 10);
  690. if (ecode != kxs_ec_success) return ecode;
  691. ecode = reg_write(reg::kSigGenerator_timecode1, tc64.tc1, readbak, 10);
  692. if (ecode != kxs_ec_success) return ecode;
  693. return ecode;
  694. }
  695. xs_error_code_t Xsync::SigGenerator_getTimecode(XsyncTimecode_t &timecode) {
  696. uint32_t readbak = 0;
  697. xs_error_code_t ecode = kxs_ec_success;
  698. uint32_t tc0 = 0;
  699. uint32_t tc1 = 0;
  700. ecode = reg_read(reg::kSigGenerator_timecode0, tc0, 10);
  701. if (ecode != kxs_ec_success) return ecode;
  702. ecode = reg_read(reg::kSigGenerator_timecode1, tc1, 10);
  703. if (ecode != kxs_ec_success) return ecode;
  704. Timecode64_t tc64;
  705. tc64.tc0 = tc0;
  706. tc64.tc1 = tc1;
  707. timecode = timecode64ToXsyncTimeCode(tc64);
  708. return ecode;
  709. }
  710. xs_error_code_t Xsync::SigGenerator_setAutoStartTimecode(XsyncTimecode_t timecode) {
  711. uint32_t readbak = 0;
  712. xs_error_code_t ecode = kxs_ec_success;
  713. Timecode64_t tc64 = timecodeTo64(timecode);
  714. ecode = reg_write(reg::kSigGenerator_timecode_start0, tc64.tc0, readbak, 10);
  715. if (ecode != kxs_ec_success) return ecode;
  716. ecode = reg_write(reg::kSigGenerator_timecode_start1, tc64.tc1, readbak, 10);
  717. if (ecode != kxs_ec_success) return ecode;
  718. return ecode;
  719. }
  720. xs_error_code_t Xsync::SigGenerator_getAutoStartTimecode(XsyncTimecode_t &timecode) {
  721. uint32_t readbak = 0;
  722. xs_error_code_t ecode = kxs_ec_success;
  723. uint32_t tc0 = 0;
  724. uint32_t tc1 = 0;
  725. ecode = reg_read(reg::kSigGenerator_timecode_start0, tc0, 10);
  726. if (ecode != kxs_ec_success) return ecode;
  727. ecode = reg_read(reg::kSigGenerator_timecode_start1, tc1, 10);
  728. if (ecode != kxs_ec_success) return ecode;
  729. Timecode64_t tc64;
  730. tc64.tc0 = tc0;
  731. tc64.tc1 = tc1;
  732. timecode = timecode64ToXsyncTimeCode(tc64);
  733. return ecode;
  734. }
  735. xs_error_code_t Xsync::SigGenerator_getWorkState(uint32_t &work_state) {
  736. uint32_t readbak = 0;
  737. auto ecode = reg_read(reg::kSigGenerator_work_state, readbak, 10);
  738. if (ecode != kxs_ec_success) return ecode;
  739. work_state = readbak;
  740. return ecode;
  741. }
  742. xs_error_code_t Xsync::SigGenerator_setGenlockFormat(GenlockFormat_t format) {
  743. uint32_t readbak = 0;
  744. return reg_write(reg::kSigGenerator_genlock_format, format, readbak, 10);
  745. }
  746. xs_error_code_t Xsync::SigGenerator_getGenlockFormat(GenlockFormat_t &format) {
  747. uint32_t readbak = 0;
  748. auto ecode = reg_read(reg::kSigGenerator_genlock_format, readbak, 10);
  749. if (ecode != kxs_ec_success) return ecode;
  750. format = (GenlockFormat_t)readbak;
  751. return ecode;
  752. }
  753. xs_error_code_t Xsync::SigGenerator_setTimecodeFormat(TimecodeFormat_t format) {
  754. uint32_t readbak = 0;
  755. return reg_write(reg::kSigGenerator_timecode_format, format, readbak, 10);
  756. }
  757. xs_error_code_t Xsync::SigGenerator_getTimecodeFormat(TimecodeFormat_t &format) {
  758. uint32_t readbak = 0;
  759. auto ecode = reg_read(reg::kSigGenerator_timecode_format, readbak, 10);
  760. if (ecode != kxs_ec_success) return ecode;
  761. format = (TimecodeFormat_t)readbak;
  762. return ecode;
  763. }