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.

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