2022-12-26 02:48:07 -05:00
|
|
|
/*
|
2023-07-11 08:06:05 -04:00
|
|
|
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_openthread.h"
|
|
|
|
#include "esp_openthread_types.h"
|
|
|
|
#include "hal/uart_types.h"
|
|
|
|
#include "lib/spinel/spinel_interface.hpp"
|
2023-07-11 08:06:05 -04:00
|
|
|
#include "lib/hdlc/hdlc.hpp"
|
2022-12-26 02:48:07 -05:00
|
|
|
#include "openthread/error.h"
|
|
|
|
|
|
|
|
namespace esp {
|
|
|
|
namespace openthread {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class defines an UART interface to the Radio Co-processor (RCP).
|
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
class UartSpinelInterface : public ot::Spinel::SpinelInterface {
|
2022-12-26 02:48:07 -05:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief This constructor of object.
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
UartSpinelInterface(void);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This destructor of the object.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
~UartSpinelInterface(void);
|
|
|
|
|
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Initializes the interface to the Radio Co-processor (RCP).
|
|
|
|
*
|
|
|
|
* @note This method should be called before reading and sending spinel frames to the interface.
|
|
|
|
*
|
|
|
|
* @param[in] aCallback Callback on frame received
|
|
|
|
* @param[in] aCallbackContext Callback context
|
|
|
|
* @param[in] aFrameBuffer A reference to a `RxFrameBuffer` object.
|
|
|
|
*
|
|
|
|
* @retval OT_ERROR_NONE The interface is initialized successfully
|
|
|
|
* @retval OT_ERROR_ALREADY The interface is already initialized.
|
|
|
|
* @retval OT_ERROR_FAILED Failed to initialize the interface.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
otError Init(ReceiveFrameCallback aCallback, void *aCallbackContext, RxFrameBuffer &aFrameBuffer);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Deinitializes the interface to the RCP.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
void Deinit(void);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Encodes and sends a spinel frame to Radio Co-processor (RCP) over the socket.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
2023-11-22 06:15:55 -05:00
|
|
|
* @param[in] aFrame A pointer to buffer containing the spinel frame to send.
|
|
|
|
* @param[in] aLength The length (number of bytes) in the frame.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
2023-11-22 06:15:55 -05:00
|
|
|
* @retval OT_ERROR_NONE Successfully encoded and sent the spinel frame.
|
|
|
|
* @retval OT_ERROR_BUSY Failed due to another operation is on going.
|
|
|
|
* @retval OT_ERROR_NO_BUFS Insufficient buffer space available to encode the frame.
|
|
|
|
* @retval OT_ERROR_FAILED Failed to call the SPI driver to send the frame.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
otError SendFrame(const uint8_t *aFrame, uint16_t aLength);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Waits for receiving part or all of spinel frame within specified interval.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
2023-11-22 06:15:55 -05:00
|
|
|
* @param[in] aTimeout The timeout value in microseconds.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
2023-11-22 06:15:55 -05:00
|
|
|
* @retval OT_ERROR_NONE Part or all of spinel frame is received.
|
|
|
|
* @retval OT_ERROR_RESPONSE_TIMEOUT No spinel frame is received within @p aTimeout.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
otError WaitForFrame(uint64_t aTimeoutUs);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Updates the file descriptor sets with file descriptors used by the radio driver.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
2023-11-22 06:15:55 -05:00
|
|
|
* @param[in,out] aMainloopContext A pointer to the mainloop context.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
void UpdateFdSet(void *aMainloopContext);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Performs radio driver processing.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
2023-11-22 06:15:55 -05:00
|
|
|
* @param[in] aMainloopContext A pointer to the mainloop context.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
void Process(const void *aMainloopContext);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Returns the bus speed between the host and the radio.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
2023-11-22 06:15:55 -05:00
|
|
|
* @returns Bus speed in bits/second.
|
2022-12-26 02:48:07 -05:00
|
|
|
*
|
|
|
|
*/
|
2023-11-22 06:15:55 -05:00
|
|
|
uint32_t GetBusSpeed(void) const;
|
2022-12-26 02:48:07 -05:00
|
|
|
|
2023-07-11 08:06:05 -04:00
|
|
|
/**
|
2023-11-22 06:15:55 -05:00
|
|
|
* Hardware resets the RCP.
|
|
|
|
*
|
|
|
|
* @retval OT_ERROR_NONE Successfully reset the RCP.
|
|
|
|
* @retval OT_ERROR_NOT_IMPLEMENT The hardware reset is not implemented.
|
2023-07-11 08:06:05 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
otError HardwareReset(void);
|
2022-12-26 02:48:07 -05:00
|
|
|
|
2023-11-22 06:15:55 -05:00
|
|
|
/**
|
|
|
|
* Returns the RCP interface metrics.
|
|
|
|
*
|
|
|
|
* @returns The RCP interface metrics.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const otRcpInterfaceMetrics *GetRcpInterfaceMetrics(void) const { return &mInterfaceMetrics; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods registers the callback for RCP failure.
|
|
|
|
*
|
|
|
|
* @param[in] handler The RCP failure handler.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void RegisterRcpFailureHandler(esp_openthread_rcp_failure_handler handler) { mRcpFailureHandler = handler; }
|
|
|
|
|
2023-07-11 08:06:05 -04:00
|
|
|
/**
|
|
|
|
* This method is called when RCP is reset to recreate the connection with it.
|
|
|
|
* Intentionally empty.
|
|
|
|
*
|
|
|
|
*/
|
2022-12-26 02:48:07 -05:00
|
|
|
otError ResetConnection(void) { return OT_ERROR_NONE; }
|
|
|
|
|
2023-11-22 06:15:55 -05:00
|
|
|
/**
|
|
|
|
* @brief This method enable the HDLC interface.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_OK on success
|
|
|
|
* - ESP_ERR_NO_MEM if allocation has failed
|
|
|
|
* - ESP_ERROR on failure
|
|
|
|
*/
|
|
|
|
esp_err_t Enable(const esp_openthread_uart_config_t &radio_uart_config);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This method disable the HDLC interface.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
esp_err_t Disable(void);
|
|
|
|
|
2022-12-26 02:48:07 -05:00
|
|
|
private:
|
|
|
|
|
2023-11-22 06:15:55 -05:00
|
|
|
enum {
|
2022-12-26 02:48:07 -05:00
|
|
|
/**
|
|
|
|
* Maximum wait time in Milliseconds for socket to become writable (see `SendFrame`).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
kMaxWaitTime = 2000,
|
|
|
|
};
|
|
|
|
|
|
|
|
esp_err_t InitUart(const esp_openthread_uart_config_t &radio_uart_config);
|
|
|
|
|
|
|
|
esp_err_t DeinitUart(void);
|
|
|
|
|
|
|
|
int TryReadAndDecode(void);
|
|
|
|
|
|
|
|
otError WaitForWritable(void);
|
|
|
|
|
|
|
|
otError Write(const uint8_t *frame, uint16_t length);
|
|
|
|
|
|
|
|
esp_err_t TryRecoverUart(void);
|
|
|
|
|
|
|
|
static void HandleHdlcFrame(void *context, otError error);
|
|
|
|
void HandleHdlcFrame(otError error);
|
|
|
|
|
2023-11-22 06:15:55 -05:00
|
|
|
ReceiveFrameCallback m_receiver_frame_callback;
|
2022-12-26 02:48:07 -05:00
|
|
|
void *m_receiver_frame_context;
|
2023-11-22 06:15:55 -05:00
|
|
|
RxFrameBuffer *m_receive_frame_buffer;
|
2022-12-26 02:48:07 -05:00
|
|
|
|
|
|
|
ot::Hdlc::Decoder m_hdlc_decoder;
|
|
|
|
uint8_t *m_uart_rx_buffer;
|
|
|
|
|
|
|
|
esp_openthread_uart_config_t m_uart_config;
|
|
|
|
int m_uart_fd;
|
|
|
|
|
2023-11-22 06:15:55 -05:00
|
|
|
otRcpInterfaceMetrics mInterfaceMetrics;
|
|
|
|
|
2022-12-26 02:48:07 -05:00
|
|
|
// Non-copyable, intentionally not implemented.
|
|
|
|
UartSpinelInterface(const UartSpinelInterface &);
|
|
|
|
UartSpinelInterface &operator=(const UartSpinelInterface &);
|
|
|
|
|
|
|
|
esp_openthread_rcp_failure_handler mRcpFailureHandler;
|
2024-01-16 06:04:19 -05:00
|
|
|
|
|
|
|
ot::Spinel::FrameBuffer<kMaxFrameSize> encoder_buffer;
|
2022-12-26 02:48:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace openthread
|
|
|
|
} // namespace esp
|