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.

136 lines
4.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
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
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 "iflytop_xsync_protocol\iflytop_xsync_protocol.h"
  2. #include "project_configs.h"
  3. #include "project_dep.h"
  4. //
  5. #include "global_flag.h"
  6. #include "iflytop_xsync\xs_udp.h"
  7. #include "reg_manager.h"
  8. #define TAG "extern_if_service"
  9. static udp_t m_udp_cmd_server; //
  10. // static udp_broadcast_handler_t m_udp_camera_sync_sender; //
  11. static struct sockaddr_in m_last_rxpacket_client;
  12. static bool m_last_rxpacket_client_valid = false;
  13. static uint8_t txbuf[2048];
  14. /*******************************************************************************
  15. * λ·ִָ *
  16. *******************************************************************************/
  17. typedef struct {
  18. udp_t *server;
  19. struct sockaddr_in *client;
  20. iflytop_xsync_packet_header_t *rxpacket;
  21. } extern_if_service_context_t;
  22. /**
  23. * @brief ͻִݰ
  24. *
  25. * @param context
  26. * @param data
  27. * @param ndata
  28. */
  29. static void create_and_send_receipt(extern_if_service_context_t *context, uint32_t *data, size_t ndata) {
  30. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txbuf;
  31. txpacket->type = kxsync_packet_type_receipt;
  32. txpacket->index = context->rxpacket->index;
  33. txpacket->cmd = context->rxpacket->cmd;
  34. txpacket->ndata = ndata;
  35. memcpy(txpacket->data, data, ndata * sizeof(uint32_t));
  36. xs_udp_send_message2(context->server, context->client, (const char *)txbuf, sizeof(iflytop_xsync_packet_header_t) + ndata * sizeof(uint32_t));
  37. }
  38. #if 0
  39. /**
  40. * @brief ʱݰ
  41. *
  42. * @param client
  43. * @param timecode0
  44. * @param timecode1
  45. */
  46. static void create_and_send_timecode(struct sockaddr_in client, uint32_t timecode0, uint32_t timecode1) {
  47. iflytop_xsync_packet_header_t *txpacket = (iflytop_xsync_packet_header_t *)txbuf;
  48. txpacket->type = kxsync_packet_type_report;
  49. txpacket->index = 0;
  50. txpacket->cmd = kxsync_packet_type_timecode_report;
  51. txpacket->ndata = 2;
  52. txpacket->data[0] = timecode0;
  53. txpacket->data[1] = timecode1;
  54. xs_udp_send_message2(&m_udp_cmd_server, &client, txbuf, sizeof(iflytop_xsync_packet_header_t) + 2 * sizeof(uint32_t));
  55. }
  56. #endif
  57. /**
  58. * @brief ղλ·ݰ
  59. *
  60. * @param server
  61. * @param client
  62. * @param data
  63. * @param len
  64. */
  65. static inline const char *hex2str(uint8_t *data, uint16_t len) {
  66. static char buf[2048];
  67. for (int i = 0; i < len && i < 1024; i++) {
  68. buf[i * 2] = "0123456789ABCDEF"[data[i] >> 4];
  69. buf[i * 2 + 1] = "0123456789ABCDEF"[data[i] & 0x0F];
  70. }
  71. return buf;
  72. }
  73. static void udp_on_packet(udp_t *server, struct sockaddr_in *client, uint8_t *data, uint16_t len) {
  74. // ZLOGI(TAG, "udp_on_packet (%s)%d:", hex2str(data, len), len);
  75. /**
  76. * @brief
  77. */
  78. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)data;
  79. extern_if_service_context_t cx = {0};
  80. cx.client = client;
  81. cx.server = server;
  82. cx.rxpacket = rxpacket;
  83. if (rxpacket->type != kxsync_packet_type_cmd) return;
  84. m_last_rxpacket_client_valid = true;
  85. memcpy(&m_last_rxpacket_client, client, sizeof(struct sockaddr_in));
  86. if (rxpacket->cmd == kxsync_packet_type_reg_read) {
  87. uint32_t regadd = rxpacket->data[0];
  88. uint32_t receipt[2];
  89. receipt[0] = 0; // receipt
  90. receipt[1] = reg_manager_read_reg(regadd); // regdata
  91. // ZLOGI(TAG, "regadd: %d, regdata: 0x%08x", regadd, receipt[1]);
  92. create_and_send_receipt(&cx, receipt, 2);
  93. } else if (rxpacket->cmd == kxsync_packet_type_reg_write) {
  94. uint32_t regadd = rxpacket->data[0];
  95. uint32_t regval = rxpacket->data[1];
  96. uint32_t receipt[2];
  97. receipt[0] = 0; //
  98. receipt[1] = reg_manager_write_reg(regadd, regval);
  99. create_and_send_receipt(&cx, receipt, 2);
  100. if (g_try_reboot_flag) {
  101. osDelay(100);
  102. HAL_NVIC_SystemReset();
  103. }
  104. } else if (rxpacket->cmd == kxsync_packet_type_reg_read_regs) {
  105. uint32_t start_regadd = rxpacket->data[0];
  106. uint32_t nreg = rxpacket->data[1];
  107. static uint32_t regcache[MAX_REG_NUM + 1];
  108. uint32_t len = MAX_REG_NUM;
  109. regcache[0] = 0;
  110. reg_manager_read_regs(start_regadd, nreg, &regcache[1], &len);
  111. create_and_send_receipt(&cx, regcache, len);
  112. }
  113. }
  114. void extern_if_service_init() { ZASSERT(xs_udp_init(&m_udp_cmd_server, "extern_if_udp", IFLYTOP_XSYNC_SERVICE_XSYNC_PORT, udp_on_packet, 1024, NULL)); }
  115. #if 0
  116. void extern_if_service_send_timecode(struct sockaddr_in client, uint32_t timecode0, uint32_t timecode1) { create_and_send_timecode(client, timecode0, timecode1); }
  117. #endif