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.

130 lines
4.4 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
2 years ago
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 void udp_on_packet(udp_t *server, struct sockaddr_in *client, uint8_t *data, uint16_t len) {
  66. ZLOGI(TAG, "udp_on_packet %d:", len);
  67. for (int i = 0; i < len; i++) {
  68. printf("%02x ", data[i]);
  69. }
  70. printf("\n");
  71. /**
  72. * @brief
  73. */
  74. iflytop_xsync_packet_header_t *rxpacket = (iflytop_xsync_packet_header_t *)data;
  75. extern_if_service_context_t cx = {0};
  76. cx.client = client;
  77. cx.server = server;
  78. cx.rxpacket = rxpacket;
  79. if (rxpacket->type != kxsync_packet_type_cmd) return;
  80. m_last_rxpacket_client_valid = true;
  81. memcpy(&m_last_rxpacket_client, client, sizeof(struct sockaddr_in));
  82. if (rxpacket->cmd == kxsync_packet_type_reg_read) {
  83. uint32_t regadd = rxpacket->data[0];
  84. uint32_t receipt[2];
  85. receipt[0] = 0; // receipt
  86. receipt[1] = reg_manager_read_reg(regadd); // regdata
  87. // ZLOGI(TAG, "regadd: %d, regdata: 0x%08x", regadd, receipt[1]);
  88. create_and_send_receipt(&cx, receipt, 2);
  89. } else if (rxpacket->cmd == kxsync_packet_type_reg_write) {
  90. uint32_t regadd = rxpacket->data[0];
  91. uint32_t regval = rxpacket->data[1];
  92. uint32_t receipt[2];
  93. receipt[0] = 0; //
  94. receipt[1] = reg_manager_write_reg(regadd, regval);
  95. create_and_send_receipt(&cx, receipt, 2);
  96. if (g_try_reboot_flag) {
  97. osDelay(100);
  98. HAL_NVIC_SystemReset();
  99. }
  100. } else if (rxpacket->cmd == kxsync_packet_type_reg_read_regs) {
  101. uint32_t start_regadd = rxpacket->data[0];
  102. uint32_t nreg = rxpacket->data[1];
  103. static uint32_t regcache[MAX_REG_NUM + 1];
  104. uint32_t len = MAX_REG_NUM;
  105. regcache[0] = 0;
  106. reg_manager_read_regs(start_regadd, nreg, &regcache[1], &len);
  107. create_and_send_receipt(&cx, regcache, len);
  108. }
  109. }
  110. 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)); }
  111. #if 0
  112. void extern_if_service_send_timecode(struct sockaddr_in client, uint32_t timecode0, uint32_t timecode1) { create_and_send_timecode(client, timecode0, timecode1); }
  113. #endif