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.

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