hci_slip.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "hci_slip.h"
  13. #include <stdlib.h>
  14. #include "hci_transport_config.h"
  15. #include "app_uart.h"
  16. #include "nrf_error.h"
  17. #define APP_SLIP_END 0xC0 /**< SLIP code for identifying the beginning and end of a packet frame.. */
  18. #define APP_SLIP_ESC 0xDB /**< SLIP escape code. This code is used to specify that the following character is specially encoded. */
  19. #define APP_SLIP_ESC_END 0xDC /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xC0.. */
  20. #define APP_SLIP_ESC_ESC 0xDD /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xDB. */
  21. /** @brief States for the SLIP state machine. */
  22. typedef enum
  23. {
  24. SLIP_OFF, /**< SLIP state OFF. */
  25. SLIP_READY, /**< SLIP state ON. */
  26. SLIP_TRANSMITTING, /**< SLIP state is transmitting indicating write() has been called but data transmission has not completed. */
  27. } slip_states_t;
  28. static slip_states_t m_current_state = SLIP_OFF; /** Current state for the SLIP TX state machine. */
  29. static hci_slip_event_handler_t m_slip_event_handler; /** Event callback function for handling of SLIP events, @ref hci_slip_evt_type_t . */
  30. static const uint8_t * mp_tx_buffer; /** Pointer to the current TX buffer that is in transmission. */
  31. static uint32_t m_tx_buffer_length; /** Length of the current TX buffer that is in transmission. */
  32. static volatile uint32_t m_tx_buffer_index; /** Current index for next byte to transmit in the mp_tx_buffer. */
  33. static uint8_t * mp_rx_buffer; /** Pointer to the current RX buffer where the next SLIP decoded packet will be stored. */
  34. static uint32_t m_rx_buffer_length; /** Length of the current RX buffer. */
  35. static uint32_t m_rx_received_count; /** Number of SLIP decoded bytes received and stored in mp_rx_buffer. */
  36. /**@brief Function for parsing bytes received on the UART until a SLIP escape byte is received.
  37. *
  38. * @param[in] byte Byte received in UART module.
  39. */
  40. static void handle_rx_byte_default(uint8_t byte);
  41. /**@brief Function for parsing bytes received on the UART until SLIP end byte is received.
  42. *
  43. * @param[in] byte Byte received in UART module.
  44. */
  45. static void handle_rx_byte_wait_start(uint8_t byte);
  46. /**@brief Function for decoding a received SLIP escape byte.
  47. * It will ensure correct decoding of the byte following the SLIP escape byte.
  48. *
  49. * @param[in] byte Byte received in UART module.
  50. */
  51. static void handle_rx_byte_esc(uint8_t byte);
  52. /**@brief Function pointer for parsing and decoding SLIP bytes from the UART module.
  53. *
  54. * @param[in] byte Byte received in UART module.
  55. */
  56. static void (*handle_rx_byte) (uint8_t byte) = handle_rx_byte_wait_start;
  57. /**@brief Function pointer for sending a byte through the UART module.
  58. */
  59. static uint32_t send_tx_byte_default(void);
  60. /**@brief Function for transferring a SLIP escape byte (0xDB) when special bytes are transferred,
  61. * that is 0xC0 and 0xDB.
  62. */
  63. static uint32_t send_tx_byte_esc(void);
  64. /**@brief Function for transferring a byte when it collides with SLIP commands and follows the SLIP
  65. * escape byte, that is 0xC0 => 0xDC and 0xDB => 0xDD.
  66. */
  67. static uint32_t send_tx_byte_encoded(void);
  68. /**@brief Function for transferring the SLIP end frame byte, 0xC0.
  69. */
  70. static uint32_t send_tx_byte_end(void);
  71. /**@brief Function pointer for sending a byte through the UART module.
  72. */
  73. uint32_t (*send_tx_byte) (void) = send_tx_byte_default;
  74. static uint32_t send_tx_byte_end(void)
  75. {
  76. uint32_t err_code = app_uart_put(APP_SLIP_END);
  77. if ((err_code == NRF_SUCCESS) && (m_tx_buffer_index == 0))
  78. {
  79. // Packet transmission started.
  80. send_tx_byte = send_tx_byte_default;
  81. }
  82. return err_code;
  83. }
  84. static uint32_t send_tx_byte_default(void)
  85. {
  86. uint32_t err_code = app_uart_put(mp_tx_buffer[m_tx_buffer_index]);
  87. if (err_code == NRF_SUCCESS)
  88. {
  89. m_tx_buffer_index++;
  90. }
  91. return err_code;
  92. }
  93. static uint32_t send_tx_byte_encoded(void)
  94. {
  95. uint32_t err_code;
  96. switch(mp_tx_buffer[m_tx_buffer_index])
  97. {
  98. case APP_SLIP_END:
  99. err_code = app_uart_put(APP_SLIP_ESC_END);
  100. break;
  101. case APP_SLIP_ESC:
  102. err_code = app_uart_put(APP_SLIP_ESC_ESC);
  103. break;
  104. default:
  105. err_code = NRF_ERROR_NO_MEM;
  106. break;
  107. }
  108. if (err_code == NRF_SUCCESS)
  109. {
  110. m_tx_buffer_index++;
  111. send_tx_byte = send_tx_byte_default;
  112. }
  113. return err_code;
  114. }
  115. static uint32_t send_tx_byte_esc(void)
  116. {
  117. uint32_t err_code = app_uart_put(APP_SLIP_ESC);
  118. if (err_code == NRF_SUCCESS)
  119. {
  120. send_tx_byte = send_tx_byte_encoded;
  121. }
  122. return err_code;
  123. }
  124. /** @brief Function for transferring the content of the mp_tx_buffer to the UART.
  125. * It continues to transfer bytes until the UART buffer is full or the complete buffer is
  126. * transferred.
  127. */
  128. static void transmit_buffer(void)
  129. {
  130. uint32_t err_code = NRF_SUCCESS;
  131. while (m_tx_buffer_index < m_tx_buffer_length)
  132. {
  133. if ((mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_END ||
  134. mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_ESC) &&
  135. send_tx_byte == send_tx_byte_default)
  136. {
  137. send_tx_byte = send_tx_byte_esc;
  138. }
  139. err_code = send_tx_byte();
  140. if (err_code == NRF_ERROR_NO_MEM || err_code == NRF_ERROR_BUSY)
  141. {
  142. // No memory left in UART TX buffer. Abort and wait for APP_UART_TX_EMPTY to continue.
  143. return;
  144. }
  145. }
  146. send_tx_byte = send_tx_byte_end;
  147. err_code = send_tx_byte();
  148. if (err_code == NRF_SUCCESS)
  149. {
  150. // Packet transmission ended. Notify higher level.
  151. m_current_state = SLIP_READY;
  152. if (m_slip_event_handler != NULL)
  153. {
  154. hci_slip_evt_t event = {HCI_SLIP_TX_DONE, mp_tx_buffer, m_tx_buffer_index};
  155. m_slip_event_handler(event);
  156. }
  157. }
  158. }
  159. /** @brief Function for handling the reception of a SLIP end byte.
  160. * If the number of bytes received is greater than zero it will call m_slip_event_handler
  161. * with number of bytes received and invalidate the mp_rx_buffer to protect against data
  162. * corruption.
  163. * No new bytes can be received until a new RX buffer is supplied.
  164. */
  165. static void handle_slip_end(void)
  166. {
  167. if (m_rx_received_count > 0)
  168. {
  169. // Full packet received, push it up.
  170. if (m_slip_event_handler != NULL)
  171. {
  172. hci_slip_evt_t event = {HCI_SLIP_RX_RDY, mp_rx_buffer, m_rx_received_count};
  173. m_rx_received_count = 0;
  174. mp_rx_buffer = NULL;
  175. m_slip_event_handler(event);
  176. }
  177. }
  178. }
  179. static void handle_rx_byte_esc(uint8_t byte)
  180. {
  181. switch (byte)
  182. {
  183. case APP_SLIP_END:
  184. handle_slip_end();
  185. break;
  186. case APP_SLIP_ESC_END:
  187. mp_rx_buffer[m_rx_received_count++] = APP_SLIP_END;
  188. break;
  189. case APP_SLIP_ESC_ESC:
  190. mp_rx_buffer[m_rx_received_count++] = APP_SLIP_ESC;
  191. break;
  192. default:
  193. mp_rx_buffer[m_rx_received_count++] = byte;
  194. break;
  195. }
  196. handle_rx_byte = handle_rx_byte_default;
  197. }
  198. static void handle_rx_byte_default(uint8_t byte)
  199. {
  200. switch (byte)
  201. {
  202. case APP_SLIP_END:
  203. handle_slip_end();
  204. break;
  205. case APP_SLIP_ESC:
  206. handle_rx_byte = handle_rx_byte_esc;
  207. break;
  208. default:
  209. mp_rx_buffer[m_rx_received_count++] = byte;
  210. break;
  211. }
  212. }
  213. static void handle_rx_byte_wait_start(uint8_t byte)
  214. {
  215. if (byte == APP_SLIP_END)
  216. {
  217. handle_rx_byte = handle_rx_byte_default;
  218. }
  219. }
  220. /** @brief Function for checking the current index and length of the RX buffer to determine if the
  221. * buffer is full. If an event handler has been registered, the callback function will
  222. * be executed..
  223. *
  224. * @retval true If RX buffer has overflowed.
  225. * @retval false otherwise.
  226. *
  227. */
  228. static bool rx_buffer_overflowed(void)
  229. {
  230. if (mp_rx_buffer == NULL || m_rx_received_count >= m_rx_buffer_length)
  231. {
  232. if (m_slip_event_handler != NULL)
  233. {
  234. hci_slip_evt_t event = {HCI_SLIP_RX_OVERFLOW, mp_rx_buffer, m_rx_received_count};
  235. m_slip_event_handler(event);
  236. }
  237. return true;
  238. }
  239. return false;
  240. }
  241. /** @brief Function for handling the UART module event. It parses events from the UART when
  242. * bytes are received/transmitted.
  243. *
  244. * @param[in] uart_event Event received from app_uart module.
  245. */
  246. static void slip_uart_eventhandler(app_uart_evt_t * uart_event)
  247. {
  248. if (uart_event->evt_type == APP_UART_TX_EMPTY && m_current_state == SLIP_TRANSMITTING)
  249. {
  250. transmit_buffer();
  251. }
  252. if ((uart_event->evt_type == APP_UART_DATA) && (!rx_buffer_overflowed()))
  253. {
  254. handle_rx_byte(uart_event->data.value);
  255. }
  256. }
  257. /** @brief Function for enabling the UART module when the SLIP layer is opened.
  258. */
  259. static uint32_t slip_uart_open(void)
  260. {
  261. uint32_t err_code;
  262. app_uart_comm_params_t comm_params =
  263. {
  264. HCI_SLIP_UART_RX_PIN_NUMBER,
  265. HCI_SLIP_UART_TX_PIN_NUMBER,
  266. HCI_SLIP_UART_RTS_PIN_NUMBER,
  267. HCI_SLIP_UART_CTS_PIN_NUMBER,
  268. HCI_SLIP_UART_MODE,
  269. false,
  270. HCI_SLIP_UART_BAUDRATE
  271. };
  272. err_code = app_uart_init(&comm_params,
  273. NULL,
  274. slip_uart_eventhandler,
  275. APP_IRQ_PRIORITY_LOW);
  276. if (err_code == NRF_SUCCESS)
  277. {
  278. m_current_state = SLIP_READY;
  279. }
  280. return err_code;
  281. }
  282. uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler)
  283. {
  284. m_slip_event_handler = event_handler;
  285. return NRF_SUCCESS;
  286. }
  287. uint32_t hci_slip_open()
  288. {
  289. switch (m_current_state)
  290. {
  291. case SLIP_OFF:
  292. return slip_uart_open();
  293. default:
  294. // Do nothing.
  295. break;
  296. }
  297. return NRF_SUCCESS;
  298. }
  299. uint32_t hci_slip_close()
  300. {
  301. m_current_state = SLIP_OFF;
  302. uint32_t err_code = app_uart_close();
  303. return err_code;
  304. }
  305. uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length)
  306. {
  307. if (p_buffer == NULL)
  308. {
  309. return NRF_ERROR_INVALID_ADDR;
  310. }
  311. switch (m_current_state)
  312. {
  313. case SLIP_READY:
  314. m_tx_buffer_index = 0;
  315. m_tx_buffer_length = length;
  316. mp_tx_buffer = p_buffer;
  317. m_current_state = SLIP_TRANSMITTING;
  318. send_tx_byte = send_tx_byte_end;
  319. transmit_buffer();
  320. return NRF_SUCCESS;
  321. case SLIP_TRANSMITTING:
  322. return NRF_ERROR_NO_MEM;
  323. case SLIP_OFF:
  324. default:
  325. return NRF_ERROR_INVALID_STATE;
  326. }
  327. }
  328. uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length)
  329. {
  330. mp_rx_buffer = p_buffer;
  331. m_rx_buffer_length = length;
  332. m_rx_received_count = 0;
  333. handle_rx_byte = handle_rx_byte_wait_start;
  334. return NRF_SUCCESS;
  335. }