1 /* 2 * Copyright (c) 2018, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for the HDLC interface to radio (RCP). 32 */ 33 34 #ifndef OT_POSIX_PLATFORM_HDLC_INTERFACE_HPP_ 35 #define OT_POSIX_PLATFORM_HDLC_INTERFACE_HPP_ 36 37 #include "logger.hpp" 38 #include "openthread-posix-config.h" 39 #include "platform-posix.h" 40 #include "lib/hdlc/hdlc.hpp" 41 #include "lib/spinel/multi_frame_buffer.hpp" 42 #include "lib/spinel/openthread-spinel-config.h" 43 #include "lib/spinel/spinel_interface.hpp" 44 45 namespace ot { 46 namespace Posix { 47 48 /** 49 * Defines an HDLC interface to the Radio Co-processor (RCP) 50 */ 51 class HdlcInterface : public ot::Spinel::SpinelInterface, public Logger<HdlcInterface> 52 { 53 public: 54 static const char kLogModuleName[]; ///< Module name used for logging. 55 56 /** 57 * Initializes the object. 58 * 59 * @param[in] aRadioUrl RadioUrl parsed from radio url. 60 */ 61 HdlcInterface(const Url::Url &aRadioUrl); 62 63 /** 64 * This destructor deinitializes the object. 65 */ 66 ~HdlcInterface(void); 67 68 /** 69 * Initializes the interface to the Radio Co-processor (RCP) 70 * 71 * @note This method should be called before reading and sending spinel frames to the interface. 72 * 73 * @param[in] aCallback Callback on frame received 74 * @param[in] aCallbackContext Callback context 75 * @param[in] aFrameBuffer A reference to a `RxFrameBuffer` object. 76 * 77 * @retval OT_ERROR_NONE The interface is initialized successfully 78 * @retval OT_ERROR_ALREADY The interface is already initialized. 79 * @retval OT_ERROR_FAILED Failed to initialize the interface. 80 */ 81 otError Init(ReceiveFrameCallback aCallback, void *aCallbackContext, RxFrameBuffer &aFrameBuffer); 82 83 /** 84 * Deinitializes the interface to the RCP. 85 */ 86 void Deinit(void); 87 88 /** 89 * Encodes and sends a spinel frame to Radio Co-processor (RCP) over the socket. 90 * 91 * This is blocking call, i.e., if the socket is not writable, this method waits for it to become writable for 92 * up to `kMaxWaitTime` interval. 93 * 94 * @param[in] aFrame A pointer to buffer containing the spinel frame to send. 95 * @param[in] aLength The length (number of bytes) in the frame. 96 * 97 * @retval OT_ERROR_NONE Successfully encoded and sent the spinel frame. 98 * @retval OT_ERROR_NO_BUFS Insufficient buffer space available to encode the frame. 99 * @retval OT_ERROR_FAILED Failed to send due to socket not becoming writable within `kMaxWaitTime`. 100 */ 101 otError SendFrame(const uint8_t *aFrame, uint16_t aLength); 102 103 /** 104 * Waits for receiving part or all of spinel frame within specified interval. 105 * 106 * @param[in] aTimeout The timeout value in microseconds. 107 * 108 * @retval OT_ERROR_NONE Part or all of spinel frame is received. 109 * @retval OT_ERROR_RESPONSE_TIMEOUT No spinel frame is received within @p aTimeout. 110 */ 111 otError WaitForFrame(uint64_t aTimeoutUs); 112 113 /** 114 * Updates the file descriptor sets with file descriptors used by the radio driver. 115 * 116 * @param[in,out] aMainloopContext A pointer to the mainloop context containing fd_sets. 117 */ 118 void UpdateFdSet(void *aMainloopContext); 119 120 /** 121 * Performs radio driver processing. 122 * 123 * @param[in] aMainloopContext A pointer to the mainloop context containing fd_sets. 124 */ 125 void Process(const void *aMainloopContext); 126 127 /** 128 * Returns the bus speed between the host and the radio. 129 * 130 * @returns Bus speed in bits/second. 131 */ GetBusSpeed(void) const132 uint32_t GetBusSpeed(void) const { return mBaudRate; } 133 134 /** 135 * Hardware resets the RCP. 136 * 137 * @retval OT_ERROR_NONE Successfully reset the RCP. 138 * @retval OT_ERROR_NOT_IMPLEMENT The hardware reset is not implemented. 139 */ HardwareReset(void)140 otError HardwareReset(void) { return OT_ERROR_NOT_IMPLEMENTED; } 141 142 /** 143 * Returns the RCP interface metrics. 144 * 145 * @returns The RCP interface metrics. 146 */ GetRcpInterfaceMetrics(void) const147 const otRcpInterfaceMetrics *GetRcpInterfaceMetrics(void) const { return &mInterfaceMetrics; } 148 149 /** 150 * Indicates whether or not the given interface matches this interface name. 151 * 152 * @param[in] aInterfaceName A pointer to the interface name. 153 * 154 * @retval TRUE The given interface name matches this interface name. 155 * @retval FALSE The given interface name doesn't match this interface name. 156 */ IsInterfaceNameMatch(const char * aInterfaceName)157 static bool IsInterfaceNameMatch(const char *aInterfaceName) 158 { 159 static const char kInterfaceName[] = "spinel+hdlc"; 160 return (strncmp(aInterfaceName, kInterfaceName, strlen(kInterfaceName)) == 0); 161 } 162 163 private: 164 /** 165 * Is called when RCP is reset to recreate the connection with it. 166 */ 167 otError ResetConnection(void); 168 169 /** 170 * Instructs `HdlcInterface` to read and decode data from radio over the socket. 171 * 172 * If a full HDLC frame is decoded while reading data, this method invokes the `HandleReceivedFrame()` (on the 173 * `aCallback` object from constructor) to pass the received frame to be processed. 174 */ 175 void Read(void); 176 177 /** 178 * Waits for the socket file descriptor associated with the HDLC interface to become writable within 179 * `kMaxWaitTime` interval. 180 * 181 * @retval OT_ERROR_NONE Socket is writable. 182 * @retval OT_ERROR_FAILED Socket did not become writable within `kMaxWaitTime`. 183 */ 184 otError WaitForWritable(void); 185 186 /** 187 * Writes a given frame to the socket. 188 * 189 * This is blocking call, i.e., if the socket is not writable, this method waits for it to become writable for 190 * up to `kMaxWaitTime` interval. 191 * 192 * @param[in] aFrame A pointer to buffer containing the frame to write. 193 * @param[in] aLength The length (number of bytes) in the frame. 194 * 195 * @retval OT_ERROR_NONE Frame was written successfully. 196 * @retval OT_ERROR_FAILED Failed to write due to socket not becoming writable within `kMaxWaitTime`. 197 */ 198 otError Write(const uint8_t *aFrame, uint16_t aLength); 199 200 /** 201 * Performs HDLC decoding on received data. 202 * 203 * If a full HDLC frame is decoded while reading data, this method invokes the `HandleReceivedFrame()` (on the 204 * `aCallback` object from constructor) to pass the received frame to be processed. 205 * 206 * @param[in] aBuffer A pointer to buffer containing data. 207 * @param[in] aLength The length (number of bytes) in the buffer. 208 */ 209 void Decode(const uint8_t *aBuffer, uint16_t aLength); 210 211 static void HandleHdlcFrame(void *aContext, otError aError); 212 void HandleHdlcFrame(otError aError); 213 214 /** 215 * Opens file specified by aRadioUrl. 216 * 217 * @param[in] aRadioUrl A reference to object containing path to file and data for configuring 218 * the connection with tty type file. 219 * 220 * @retval The file descriptor of newly opened file. 221 */ 222 int OpenFile(const Url::Url &aRadioUrl); 223 224 /** 225 * Closes file associated with the file descriptor. 226 */ 227 void CloseFile(void); 228 229 #if OPENTHREAD_POSIX_CONFIG_RCP_PTY_ENABLE 230 static int ForkPty(const Url::Url &aRadioUrl); 231 #endif 232 233 enum 234 { 235 kMaxWaitTime = 2000, ///< Maximum wait time in Milliseconds for socket to become writable (see `SendFrame`). 236 kResetTimeout = 5000, ///< Maximum wait time in Milliseconds for file to become ready (see `ResetConnection`). 237 kOpenFileDelay = 50, ///< Delay between open file calls, in Milliseconds (see `ResetConnection`). 238 kRemoveRcpDelay = 239 2000, ///< Delay for removing RCP device from host OS after hard reset (see `ResetConnection`). 240 }; 241 242 ReceiveFrameCallback mReceiveFrameCallback; 243 void *mReceiveFrameContext; 244 RxFrameBuffer *mReceiveFrameBuffer; 245 246 int mSockFd; 247 uint32_t mBaudRate; 248 Hdlc::Decoder mHdlcDecoder; 249 const Url::Url &mRadioUrl; 250 251 otRcpInterfaceMetrics mInterfaceMetrics; 252 253 // Non-copyable, intentionally not implemented. 254 HdlcInterface(const HdlcInterface &); 255 HdlcInterface &operator=(const HdlcInterface &); 256 }; 257 258 } // namespace Posix 259 } // namespace ot 260 261 #endif // OT_POSIX_PLATFORM_HDLC_INTERFACE_HPP_ 262