esp_uart_spinel_interface.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_err.h"
  8. #include "esp_openthread.h"
  9. #include "esp_openthread_types.h"
  10. #include "hal/uart_types.h"
  11. #include "lib/spinel/spinel_interface.hpp"
  12. #include "openthread/error.h"
  13. namespace esp {
  14. namespace openthread {
  15. /**
  16. * This class defines an UART interface to the Radio Co-processor (RCP).
  17. *
  18. */
  19. class UartSpinelInterface {
  20. public:
  21. /**
  22. * @brief This constructor of object.
  23. *
  24. * @param[in] callback Callback on frame received
  25. * @param[in] callback_context Callback context
  26. * @param[in] frame_buffer A reference to a `RxFrameBuffer` object.
  27. *
  28. */
  29. UartSpinelInterface(ot::Spinel::SpinelInterface::ReceiveFrameCallback callback, void *callback_context,
  30. ot::Spinel::SpinelInterface::RxFrameBuffer &frame_buffer);
  31. /**
  32. * @brief This destructor of the object.
  33. *
  34. */
  35. ~UartSpinelInterface(void);
  36. /**
  37. * @brief This method initializes the HDLC interface.
  38. *
  39. * @return
  40. * - ESP_OK on success
  41. * - ESP_ERR_NO_MEM if allocation has failed
  42. * - ESP_ERROR on failure
  43. */
  44. esp_err_t Init(const esp_openthread_uart_config_t &radio_uart_config);
  45. /**
  46. * @brief This method deinitializes the HDLC interface.
  47. *
  48. */
  49. esp_err_t Deinit(void);
  50. /**
  51. * @brief This method encodes and sends a spinel frame to Radio Co-processor (RCP) over the socket.
  52. *
  53. * @note This is blocking call, i.e., if the socket is not writable, this method waits for it to become writable
  54. * for up to `kMaxWaitTime` interval.
  55. *
  56. * @param[in] frame A pointer to buffer containing the spinel frame to send.
  57. * @param[in] length The length (number of bytes) in the frame.
  58. *
  59. * @return
  60. * -OT_ERROR_NONE Successfully encoded and sent the spinel frame.
  61. * -OT_ERROR_NO_BUFS Insufficient buffer space available to encode the frame.
  62. * -OT_ERROR_FAILED Failed to send due to socket not becoming writable within `kMaxWaitTime`.
  63. *
  64. */
  65. otError SendFrame(const uint8_t *frame, uint16_t length);
  66. /**
  67. * This method waits for receiving part or all of spinel frame within specified timeout.
  68. *
  69. * @param[in] timeout_us The timeout value in microseconds.
  70. *
  71. * @return
  72. * -OT_ERROR_NONE Part or all of spinel frame is received.
  73. * -OT_ERROR_RESPONSE_TIMEOUT No spinel frame is received within @p timeout_us.
  74. *
  75. */
  76. otError WaitForFrame(uint64_t timeout_us);
  77. /**
  78. * This method performs uart processing to the RCP.
  79. *
  80. * @param[in] mainloop The mainloop context
  81. *
  82. */
  83. void Process(const esp_openthread_mainloop_context_t &mainloop);
  84. /**
  85. * This methods updates the mainloop context.
  86. *
  87. * @param[inout] mainloop The mainloop context.
  88. *
  89. */
  90. void Update(esp_openthread_mainloop_context_t &mainloop);
  91. /**
  92. * This methods registers the callback for RCP failure.
  93. *
  94. * @param[in] handler The RCP failure handler.
  95. *
  96. */
  97. void RegisterRcpFailureHandler(esp_openthread_rcp_failure_handler handler) { mRcpFailureHandler = handler; }
  98. void OnRcpReset(void);
  99. otError ResetConnection(void) { return OT_ERROR_NONE; }
  100. private:
  101. enum {
  102. /**
  103. * Maximum spinel frame size.
  104. *
  105. */
  106. kMaxFrameSize = ot::Spinel::SpinelInterface::kMaxFrameSize,
  107. /**
  108. * Maximum wait time in Milliseconds for socket to become writable (see `SendFrame`).
  109. *
  110. */
  111. kMaxWaitTime = 2000,
  112. };
  113. esp_err_t InitUart(const esp_openthread_uart_config_t &radio_uart_config);
  114. esp_err_t DeinitUart(void);
  115. int TryReadAndDecode(void);
  116. otError WaitForWritable(void);
  117. otError Write(const uint8_t *frame, uint16_t length);
  118. esp_err_t TryRecoverUart(void);
  119. static void HandleHdlcFrame(void *context, otError error);
  120. void HandleHdlcFrame(otError error);
  121. ot::Spinel::SpinelInterface::ReceiveFrameCallback m_receiver_frame_callback;
  122. void *m_receiver_frame_context;
  123. ot::Spinel::SpinelInterface::RxFrameBuffer &m_receive_frame_buffer;
  124. ot::Hdlc::Decoder m_hdlc_decoder;
  125. uint8_t *m_uart_rx_buffer;
  126. esp_openthread_uart_config_t m_uart_config;
  127. int m_uart_fd;
  128. // Non-copyable, intentionally not implemented.
  129. UartSpinelInterface(const UartSpinelInterface &);
  130. UartSpinelInterface &operator=(const UartSpinelInterface &);
  131. esp_openthread_rcp_failure_handler mRcpFailureHandler;
  132. };
  133. } // namespace openthread
  134. } // namespace esp