hci_slip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /**
  2. * Copyright (c) 2013 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(HCI_SLIP)
  42. #include "hci_slip.h"
  43. #include <stdlib.h>
  44. #include "app_uart.h"
  45. #include "nrf_error.h"
  46. #define APP_SLIP_END 0xC0 /**< SLIP code for identifying the beginning and end of a packet frame.. */
  47. #define APP_SLIP_ESC 0xDB /**< SLIP escape code. This code is used to specify that the following character is specially encoded. */
  48. #define APP_SLIP_ESC_END 0xDC /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xC0.. */
  49. #define APP_SLIP_ESC_ESC 0xDD /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xDB. */
  50. /** @brief States for the SLIP state machine. */
  51. typedef enum
  52. {
  53. SLIP_OFF, /**< SLIP state OFF. */
  54. SLIP_READY, /**< SLIP state ON. */
  55. SLIP_TRANSMITTING, /**< SLIP state is transmitting indicating write() has been called but data transmission has not completed. */
  56. } slip_states_t;
  57. static slip_states_t m_current_state = SLIP_OFF; /** Current state for the SLIP TX state machine. */
  58. static hci_slip_event_handler_t m_slip_event_handler; /** Event callback function for handling of SLIP events, @ref hci_slip_evt_type_t . */
  59. static const uint8_t * mp_tx_buffer; /** Pointer to the current TX buffer that is in transmission. */
  60. static uint32_t m_tx_buffer_length; /** Length of the current TX buffer that is in transmission. */
  61. static volatile uint32_t m_tx_buffer_index; /** Current index for next byte to transmit in the mp_tx_buffer. */
  62. static uint8_t * mp_rx_buffer; /** Pointer to the current RX buffer where the next SLIP decoded packet will be stored. */
  63. static uint32_t m_rx_buffer_length; /** Length of the current RX buffer. */
  64. static uint32_t m_rx_received_count; /** Number of SLIP decoded bytes received and stored in mp_rx_buffer. */
  65. /**@brief Function for parsing bytes received on the UART until a SLIP escape byte is received.
  66. *
  67. * @param[in] byte Byte received in UART module.
  68. */
  69. static void handle_rx_byte_default(uint8_t byte);
  70. /**@brief Function for parsing bytes received on the UART until SLIP end byte is received.
  71. *
  72. * @param[in] byte Byte received in UART module.
  73. */
  74. static void handle_rx_byte_wait_start(uint8_t byte);
  75. /**@brief Function for decoding a received SLIP escape byte.
  76. * It will ensure correct decoding of the byte following the SLIP escape byte.
  77. *
  78. * @param[in] byte Byte received in UART module.
  79. */
  80. static void handle_rx_byte_esc(uint8_t byte);
  81. /**@brief Function pointer for parsing and decoding SLIP bytes from the UART module.
  82. *
  83. * @param[in] byte Byte received in UART module.
  84. */
  85. static void (*handle_rx_byte) (uint8_t byte) = handle_rx_byte_wait_start;
  86. /**@brief Function pointer for sending a byte through the UART module.
  87. */
  88. static uint32_t send_tx_byte_default(void);
  89. /**@brief Function for transferring a SLIP escape byte (0xDB) when special bytes are transferred,
  90. * that is 0xC0 and 0xDB.
  91. */
  92. static uint32_t send_tx_byte_esc(void);
  93. /**@brief Function for transferring a byte when it collides with SLIP commands and follows the SLIP
  94. * escape byte, that is 0xC0 => 0xDC and 0xDB => 0xDD.
  95. */
  96. static uint32_t send_tx_byte_encoded(void);
  97. /**@brief Function for transferring the SLIP end frame byte, 0xC0.
  98. */
  99. static uint32_t send_tx_byte_end(void);
  100. /**@brief Function pointer for sending a byte through the UART module.
  101. */
  102. uint32_t (*send_tx_byte) (void) = send_tx_byte_default;
  103. static uint32_t send_tx_byte_end(void)
  104. {
  105. uint32_t err_code = app_uart_put(APP_SLIP_END);
  106. if ((err_code == NRF_SUCCESS) && (m_tx_buffer_index == 0))
  107. {
  108. // Packet transmission started.
  109. send_tx_byte = send_tx_byte_default;
  110. }
  111. return err_code;
  112. }
  113. static uint32_t send_tx_byte_default(void)
  114. {
  115. uint32_t err_code = app_uart_put(mp_tx_buffer[m_tx_buffer_index]);
  116. if (err_code == NRF_SUCCESS)
  117. {
  118. m_tx_buffer_index++;
  119. }
  120. return err_code;
  121. }
  122. static uint32_t send_tx_byte_encoded(void)
  123. {
  124. uint32_t err_code;
  125. switch (mp_tx_buffer[m_tx_buffer_index])
  126. {
  127. case APP_SLIP_END:
  128. err_code = app_uart_put(APP_SLIP_ESC_END);
  129. break;
  130. case APP_SLIP_ESC:
  131. err_code = app_uart_put(APP_SLIP_ESC_ESC);
  132. break;
  133. default:
  134. err_code = NRF_ERROR_NO_MEM;
  135. break;
  136. }
  137. if (err_code == NRF_SUCCESS)
  138. {
  139. m_tx_buffer_index++;
  140. send_tx_byte = send_tx_byte_default;
  141. }
  142. return err_code;
  143. }
  144. static uint32_t send_tx_byte_esc(void)
  145. {
  146. uint32_t err_code = app_uart_put(APP_SLIP_ESC);
  147. if (err_code == NRF_SUCCESS)
  148. {
  149. send_tx_byte = send_tx_byte_encoded;
  150. }
  151. return err_code;
  152. }
  153. /** @brief Function for transferring the content of the mp_tx_buffer to the UART.
  154. * It continues to transfer bytes until the UART buffer is full or the complete buffer is
  155. * transferred.
  156. */
  157. static void transmit_buffer(void)
  158. {
  159. uint32_t err_code = NRF_SUCCESS;
  160. while (m_tx_buffer_index < m_tx_buffer_length)
  161. {
  162. if ((mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_END ||
  163. mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_ESC) &&
  164. send_tx_byte == send_tx_byte_default)
  165. {
  166. send_tx_byte = send_tx_byte_esc;
  167. }
  168. err_code = send_tx_byte();
  169. if (err_code == NRF_ERROR_NO_MEM || err_code == NRF_ERROR_BUSY)
  170. {
  171. // No memory left in UART TX buffer. Abort and wait for APP_UART_TX_EMPTY to continue.
  172. return;
  173. }
  174. }
  175. send_tx_byte = send_tx_byte_end;
  176. err_code = send_tx_byte();
  177. if (err_code == NRF_SUCCESS)
  178. {
  179. // Packet transmission ended. Notify higher level.
  180. m_current_state = SLIP_READY;
  181. if (m_slip_event_handler != NULL)
  182. {
  183. hci_slip_evt_t event = {HCI_SLIP_TX_DONE, mp_tx_buffer, m_tx_buffer_index};
  184. m_slip_event_handler(event);
  185. }
  186. }
  187. }
  188. /** @brief Function for handling the reception of a SLIP end byte.
  189. * If the number of bytes received is greater than zero it will call m_slip_event_handler
  190. * with number of bytes received and invalidate the mp_rx_buffer to protect against data
  191. * corruption.
  192. * No new bytes can be received until a new RX buffer is supplied.
  193. */
  194. static void handle_slip_end(void)
  195. {
  196. if (m_rx_received_count > 0)
  197. {
  198. // Full packet received, push it up.
  199. if (m_slip_event_handler != NULL)
  200. {
  201. hci_slip_evt_t event = {HCI_SLIP_RX_RDY, mp_rx_buffer, m_rx_received_count};
  202. m_rx_received_count = 0;
  203. mp_rx_buffer = NULL;
  204. m_slip_event_handler(event);
  205. }
  206. }
  207. }
  208. static void handle_rx_byte_esc(uint8_t byte)
  209. {
  210. switch (byte)
  211. {
  212. case APP_SLIP_END:
  213. handle_slip_end();
  214. break;
  215. case APP_SLIP_ESC_END:
  216. mp_rx_buffer[m_rx_received_count++] = APP_SLIP_END;
  217. break;
  218. case APP_SLIP_ESC_ESC:
  219. mp_rx_buffer[m_rx_received_count++] = APP_SLIP_ESC;
  220. break;
  221. default:
  222. mp_rx_buffer[m_rx_received_count++] = byte;
  223. break;
  224. }
  225. handle_rx_byte = handle_rx_byte_default;
  226. }
  227. static void handle_rx_byte_default(uint8_t byte)
  228. {
  229. switch (byte)
  230. {
  231. case APP_SLIP_END:
  232. handle_slip_end();
  233. break;
  234. case APP_SLIP_ESC:
  235. handle_rx_byte = handle_rx_byte_esc;
  236. break;
  237. default:
  238. mp_rx_buffer[m_rx_received_count++] = byte;
  239. break;
  240. }
  241. }
  242. static void handle_rx_byte_wait_start(uint8_t byte)
  243. {
  244. if (byte == APP_SLIP_END)
  245. {
  246. handle_rx_byte = handle_rx_byte_default;
  247. }
  248. }
  249. /** @brief Function for checking the current index and length of the RX buffer to determine if the
  250. * buffer is full. If an event handler has been registered, the callback function will
  251. * be executed..
  252. *
  253. * @retval true If RX buffer has overflowed.
  254. * @retval false otherwise.
  255. *
  256. */
  257. static bool rx_buffer_overflowed(void)
  258. {
  259. if (mp_rx_buffer == NULL || m_rx_received_count >= m_rx_buffer_length)
  260. {
  261. if (m_slip_event_handler != NULL)
  262. {
  263. hci_slip_evt_t event = {HCI_SLIP_RX_OVERFLOW, mp_rx_buffer, m_rx_received_count};
  264. m_slip_event_handler(event);
  265. }
  266. return true;
  267. }
  268. return false;
  269. }
  270. /** @brief Function for handling the UART module event. It parses events from the UART when
  271. * bytes are received/transmitted.
  272. *
  273. * @param[in] uart_event Event received from app_uart module.
  274. */
  275. static void slip_uart_eventhandler(app_uart_evt_t * uart_event)
  276. {
  277. if (uart_event->evt_type == APP_UART_TX_EMPTY && m_current_state == SLIP_TRANSMITTING)
  278. {
  279. transmit_buffer();
  280. }
  281. if ((uart_event->evt_type == APP_UART_DATA) && (!rx_buffer_overflowed()))
  282. {
  283. handle_rx_byte(uart_event->data.value);
  284. }
  285. }
  286. /** @brief Function for enabling the UART module when the SLIP layer is opened.
  287. */
  288. static uint32_t slip_uart_open(void)
  289. {
  290. uint32_t err_code;
  291. app_uart_comm_params_t comm_params =
  292. {
  293. HCI_UART_RX_PIN,
  294. HCI_UART_TX_PIN,
  295. HCI_UART_RTS_PIN,
  296. HCI_UART_CTS_PIN,
  297. (app_uart_flow_control_t)HCI_UART_FLOW_CONTROL,
  298. false,
  299. HCI_UART_BAUDRATE
  300. };
  301. err_code = app_uart_init(&comm_params,
  302. NULL,
  303. slip_uart_eventhandler,
  304. APP_IRQ_PRIORITY_LOWEST);
  305. if (err_code == NRF_SUCCESS)
  306. {
  307. m_current_state = SLIP_READY;
  308. }
  309. return err_code;
  310. }
  311. uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler)
  312. {
  313. m_slip_event_handler = event_handler;
  314. return NRF_SUCCESS;
  315. }
  316. uint32_t hci_slip_open()
  317. {
  318. switch (m_current_state)
  319. {
  320. case SLIP_OFF:
  321. return slip_uart_open();
  322. default:
  323. // Do nothing.
  324. break;
  325. }
  326. return NRF_SUCCESS;
  327. }
  328. uint32_t hci_slip_close()
  329. {
  330. m_current_state = SLIP_OFF;
  331. uint32_t err_code = app_uart_close();
  332. return err_code;
  333. }
  334. uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length)
  335. {
  336. if (p_buffer == NULL)
  337. {
  338. return NRF_ERROR_INVALID_ADDR;
  339. }
  340. switch (m_current_state)
  341. {
  342. case SLIP_READY:
  343. m_tx_buffer_index = 0;
  344. m_tx_buffer_length = length;
  345. mp_tx_buffer = p_buffer;
  346. m_current_state = SLIP_TRANSMITTING;
  347. send_tx_byte = send_tx_byte_end;
  348. transmit_buffer();
  349. return NRF_SUCCESS;
  350. case SLIP_TRANSMITTING:
  351. return NRF_ERROR_NO_MEM;
  352. case SLIP_OFF:
  353. default:
  354. return NRF_ERROR_INVALID_STATE;
  355. }
  356. }
  357. uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length)
  358. {
  359. mp_rx_buffer = p_buffer;
  360. m_rx_buffer_length = length;
  361. m_rx_received_count = 0;
  362. handle_rx_byte = handle_rx_byte_wait_start;
  363. return NRF_SUCCESS;
  364. }
  365. #endif //NRF_MODULE_ENABLED(HCI_SLIP)