hci_transport.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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_transport_config.h"
  13. #include "hci_transport.h"
  14. #include "hci_slip.h"
  15. #include "crc16.h"
  16. #include "hci_mem_pool.h"
  17. #include "hci_mem_pool_internal.h"
  18. #include "app_timer.h"
  19. #include "app_error.h"
  20. #include <stdio.h>
  21. #include "sdk_common.h"
  22. #define PKT_HDR_SIZE 4u /**< Packet header size in number of bytes. */
  23. #define PKT_CRC_SIZE 2u /**< Packet CRC size in number of bytes. */
  24. #define PKT_TYPE_VENDOR_SPECIFIC 14u /**< Packet type vendor specific. */
  25. #define PKT_TYPE_ACK 0 /**< Packet type acknowledgement. */
  26. #define DATA_INTEGRITY_MASK (1u << 6u) /**< Mask for data integrity bit in the packet header. */
  27. #define RELIABLE_PKT_MASK (1u << 7u) /**< Mask for reliable packet bit in the packet header. */
  28. #define INITIAL_ACK_NUMBER_EXPECTED 1u /**< Initial acknowledge number expected. */
  29. #define INITIAL_ACK_NUMBER_TX INITIAL_ACK_NUMBER_EXPECTED /**< Initial acknowledge number transmitted. */
  30. #define INVALID_PKT_TYPE 0xFFFFFFFFu /**< Internal invalid packet type value. */
  31. #define MAX_TRANSMISSION_TIME (ROUNDED_DIV((MAX_PACKET_SIZE_IN_BITS * 1000u), USED_BAUD_RATE)) /**< Max transmission time of a single application packet over UART in units of mseconds. */
  32. #define RETRANSMISSION_TIMEOUT_IN_MS (3u * MAX_TRANSMISSION_TIME) /**< Retransmission timeout for application packet in units of mseconds. */
  33. #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
  34. #define RETRANSMISSION_TIMEOUT_IN_TICKS APP_TIMER_TICKS(RETRANSMISSION_TIMEOUT_IN_MS, APP_TIMER_PRESCALER) /**< Retransmission timeout for application packet in units of timer ticks. */
  35. #define MAX_RETRY_COUNT 5u /**< Max retransmission retry count for application packets. */
  36. #define ACK_BUF_SIZE 5u /**< Length of module internal RX buffer which is big enough to hold an acknowledgement packet. */
  37. /**@brief States of the TX state machine. */
  38. typedef enum
  39. {
  40. TX_STATE_IDLE, /**< State for: no application transmission packet processing in progress. */
  41. TX_STATE_PENDING, /**< State for: TX in progress in slip layer and TX-done event is waited for to signal the end of transmission. */
  42. TX_STATE_ACTIVE /**< State for: application packet has been delivered to slip for transmission and peer transport entity acknowledgement packet is waited for. */
  43. } tx_state_t;
  44. /**@brief TX state machine events. */
  45. typedef enum
  46. {
  47. TX_EVENT_STATE_ENTRY, /**< Event for: state entry use case. */
  48. TX_EVENT_SLIP_TX_DONE, /**< Event for: HCI_SLIP_TX_DONE event use case. */
  49. TX_EVENT_TIMEOUT, /**< Event for: retransmission timeout use case. */
  50. TX_EVENT_VALID_RX_ACK /**< Event for: valid acknowledgement received for TX packet use case. */
  51. } tx_event_t;
  52. static void tx_sm_state_change(tx_state_t new_state);
  53. static tx_state_t m_tx_state; /**< Current TX state. */
  54. static hci_transport_tx_done_handler_t m_transport_tx_done_handle; /**< TX done event callback function. */
  55. static hci_transport_event_handler_t m_transport_event_handle; /**< Event handler callback function. */
  56. static uint8_t * mp_slip_used_rx_buffer; /**< Reference to RX buffer used by the slip layer. */
  57. static uint32_t m_packet_expected_seq_number; /**< Sequence number counter of the packet expected to be received . */
  58. static uint32_t m_packet_transmit_seq_number; /**< Sequence number counter of the transmitted packet for which acknowledgement packet is waited for. */
  59. static uint8_t * mp_tx_buffer; /**< Pointer to TX application buffer to be transmitted. */
  60. static uint32_t m_tx_buffer_length; /**< Length of application TX packet data to be transmitted in bytes. */
  61. static bool m_is_slip_decode_ready; /**< Boolean to determine has slip decode been completed or not. */
  62. APP_TIMER_DEF(m_app_timer_id); /**< Application timer id. */
  63. static uint32_t m_tx_retry_counter; /**< Application packet retransmission counter. */
  64. static hci_transport_tx_done_result_t m_tx_done_result_code; /**< TX done event callback function result code. */
  65. static uint8_t m_rx_ack_buffer[ACK_BUF_SIZE];/**< RX buffer big enough to hold an acknowledgement packet and which is taken in use upon receiving HCI_SLIP_RX_OVERFLOW event. */
  66. /**@brief Function for validating a received packet.
  67. *
  68. * @param[in] p_buffer Pointer to the packet data.
  69. * @param[in] length Length of packet data in bytes.
  70. *
  71. * @return true if received packet is valid, false in other case.
  72. */
  73. static bool is_rx_pkt_valid(const uint8_t * p_buffer, uint32_t length)
  74. {
  75. // Executed packet filtering algorithm order:
  76. // - verify packet overall length
  77. // - verify data integrity bit set
  78. // - verify reliable packet bit set
  79. // - verify supported packet type
  80. // - verify header checksum
  81. // - verify payload length field
  82. // - verify CRC
  83. if (length <= PKT_HDR_SIZE)
  84. {
  85. return false;
  86. }
  87. if (!(p_buffer[0] & DATA_INTEGRITY_MASK))
  88. {
  89. return false;
  90. }
  91. if (!(p_buffer[0] & RELIABLE_PKT_MASK))
  92. {
  93. return false;
  94. }
  95. if ((p_buffer[1] & 0x0Fu) != PKT_TYPE_VENDOR_SPECIFIC)
  96. {
  97. return false;
  98. }
  99. const uint32_t expected_checksum =
  100. ((p_buffer[0] + p_buffer[1] + p_buffer[2] + p_buffer[3])) & 0xFFu;
  101. if (expected_checksum != 0)
  102. {
  103. return false;
  104. }
  105. const uint16_t crc_calculated = crc16_compute(p_buffer, (length - PKT_CRC_SIZE), NULL);
  106. const uint16_t crc_received = uint16_decode(&p_buffer[length - PKT_CRC_SIZE]);
  107. if (crc_calculated != crc_received)
  108. {
  109. return false;
  110. }
  111. return true;
  112. }
  113. /**@brief Function for getting the sequence number of the next reliable packet expected.
  114. *
  115. * @return sequence number of the next reliable packet expected.
  116. */
  117. static __INLINE uint8_t packet_number_expected_get(void)
  118. {
  119. return (uint8_t) m_packet_expected_seq_number;
  120. }
  121. /**@brief Function for calculating a packet header checksum.
  122. *
  123. * @param[in] p_hdr Pointer to the packet header.
  124. *
  125. * @return Calculated checksum.
  126. */
  127. static uint8_t header_checksum_calculate(const uint8_t * p_hdr)
  128. {
  129. // @note: no pointer validation check needed as already checked by calling function.
  130. uint32_t checksum;
  131. checksum = p_hdr[0];
  132. checksum += p_hdr[1];
  133. checksum += p_hdr[2];
  134. checksum &= 0xFFu;
  135. checksum = (~checksum + 1u);
  136. return (uint8_t)checksum;
  137. }
  138. /**@brief Function for writing an acknowledgment packet for transmission.
  139. */
  140. static void ack_transmit(void)
  141. {
  142. static uint8_t ack_packet[PKT_HDR_SIZE];
  143. // TX ACK packet format:
  144. // - Unreliable Packet type
  145. // - Payload Length set to 0
  146. // - Sequence Number set to 0
  147. // - Header checksum calculated
  148. // - Acknowledge Number set correctly
  149. ack_packet[0] = (packet_number_expected_get() << 3u);
  150. ack_packet[1] = 0;
  151. ack_packet[2] = 0;
  152. ack_packet[3] = header_checksum_calculate(ack_packet);
  153. // @note: no return value check needed for hci_slip_write(...) call as acknowledgement packets
  154. // are considered to be from system design point of view unreliable packets.Use case where
  155. // underlying slip layer does not accept a packet for transmission is managed either by:
  156. // - acknowledged by possible future application packet as acknowledgement number header field
  157. // is included
  158. // - protocol peer entity will retransmit the packet
  159. UNUSED_VARIABLE(hci_slip_write(ack_packet, sizeof(ack_packet)));
  160. }
  161. /**@brief Function for validating a received packet.
  162. *
  163. * @param[in] p_buffer Pointer to the packet data.
  164. *
  165. * @return sequence number field of the packet header with unrelated data masked out.
  166. */
  167. static __INLINE uint8_t packet_seq_nmbr_extract(const uint8_t * p_buffer)
  168. {
  169. return (p_buffer[0] & 0x07u);
  170. }
  171. /**@brief Function for incrementing the sequence number counter for next reliable packet expected.
  172. */
  173. static __INLINE void packet_number_expected_inc(void)
  174. {
  175. ++m_packet_expected_seq_number;
  176. m_packet_expected_seq_number &= 0x07u;
  177. }
  178. /**@brief Function for decoding a packet type field.
  179. *
  180. * @param[in] p_buffer Pointer to the packet data.
  181. * @param[in] length Length of packet data in bytes.
  182. *
  183. * @return Packet type field or INVALID_PKT_TYPE in case of decode error.
  184. */
  185. static __INLINE uint32_t packet_type_decode(const uint8_t * p_buffer, uint32_t length)
  186. {
  187. // @note: no pointer validation check needed as allready checked by calling function.
  188. uint32_t return_value;
  189. if (length >= PKT_HDR_SIZE)
  190. {
  191. return_value = (p_buffer[1] & 0x0Fu);
  192. }
  193. else
  194. {
  195. return_value = INVALID_PKT_TYPE;
  196. }
  197. return return_value;
  198. }
  199. /**@brief Function for processing a received vendor specific packet.
  200. *
  201. * @param[in] p_buffer Pointer to the packet data.
  202. * @param[in] length Length of packet data in bytes.
  203. */
  204. static void rx_vendor_specific_pkt_type_handle(const uint8_t * p_buffer, uint32_t length)
  205. {
  206. // @note: no pointer validation check needed as allready checked by calling function.
  207. uint32_t err_code;
  208. if (is_rx_pkt_valid(p_buffer, length))
  209. {
  210. // RX packet is valid: validate sequence number.
  211. const uint8_t rx_seq_number = packet_seq_nmbr_extract(p_buffer);
  212. if (packet_number_expected_get() == rx_seq_number)
  213. {
  214. // Sequence number is valid: transmit acknowledgement.
  215. packet_number_expected_inc();
  216. ack_transmit();
  217. m_is_slip_decode_ready = true;
  218. err_code = hci_mem_pool_rx_data_size_set(length);
  219. APP_ERROR_CHECK(err_code);
  220. err_code = hci_mem_pool_rx_produce(RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer);
  221. APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NO_MEM));
  222. // If memory pool RX buffer produce succeeded we register that buffer to slip layer
  223. // otherwise we register the internal acknowledgement buffer.
  224. err_code = hci_slip_rx_buffer_register(
  225. (err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer,
  226. (err_code == NRF_SUCCESS) ? RX_BUF_SIZE : ACK_BUF_SIZE);
  227. APP_ERROR_CHECK(err_code);
  228. if (m_transport_event_handle != NULL)
  229. {
  230. // Send application event of RX packet reception.
  231. const hci_transport_evt_t evt = {HCI_TRANSPORT_RX_RDY};
  232. m_transport_event_handle(evt);
  233. }
  234. }
  235. else
  236. {
  237. // RX packet discarded: sequence number not valid, set the same buffer to slip layer in
  238. // order to avoid buffer overrun.
  239. err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE);
  240. APP_ERROR_CHECK(err_code);
  241. // As packet did not have expected sequence number: send acknowledgement with the
  242. // current expected sequence number.
  243. ack_transmit();
  244. }
  245. }
  246. else
  247. {
  248. // RX packet discarded: reset the same buffer to slip layer in order to avoid buffer
  249. // overrun.
  250. err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE);
  251. APP_ERROR_CHECK(err_code);
  252. }
  253. }
  254. /**@brief Function for getting the sequence number of a reliable TX packet for which peer protocol
  255. * entity acknowledgment is pending.
  256. *
  257. * @return sequence number of a reliable TX packet for which peer protocol entity acknowledgement
  258. * is pending.
  259. */
  260. static __INLINE uint8_t packet_number_to_transmit_get(void)
  261. {
  262. return m_packet_transmit_seq_number;
  263. }
  264. /**@brief Function for getting the expected acknowledgement number.
  265. *
  266. * @return expected acknowledgement number.
  267. */
  268. static __INLINE uint8_t expected_ack_number_get(void)
  269. {
  270. uint8_t seq_nmbr = packet_number_to_transmit_get();
  271. ++seq_nmbr;
  272. seq_nmbr &= 0x07u;
  273. return seq_nmbr;
  274. }
  275. /**@brief Function for processing a received acknowledgement packet.
  276. *
  277. * Verifies does the received acknowledgement packet has the expected acknowledgement number and
  278. * that the header checksum is correct.
  279. *
  280. * @param[in] p_buffer Pointer to the packet data.
  281. *
  282. * @return true if valid acknowledgement packet received.
  283. */
  284. static __INLINE bool rx_ack_pkt_type_handle(const uint8_t * p_buffer)
  285. {
  286. // @note: no pointer validation check needed as allready checked by calling function.
  287. // Verify header checksum.
  288. const uint32_t expected_checksum =
  289. ((p_buffer[0] + p_buffer[1] + p_buffer[2] + p_buffer[3])) & 0xFFu;
  290. if (expected_checksum != 0)
  291. {
  292. return false;
  293. }
  294. const uint8_t ack_number = (p_buffer[0] >> 3u) & 0x07u;
  295. // Verify expected acknowledgment number.
  296. return (ack_number == expected_ack_number_get());
  297. }
  298. /**@brief Function for incrementing the sequence number counter of the TX packet.
  299. */
  300. static __INLINE void packet_number_tx_inc(void)
  301. {
  302. ++m_packet_transmit_seq_number;
  303. m_packet_transmit_seq_number &= 0x07u;
  304. }
  305. /**@brief Function for TX state machine event processing in a state centric manner.
  306. *
  307. * @param[in] event Type of event occurred.
  308. */
  309. static void tx_sm_event_handle(tx_event_t event)
  310. {
  311. uint32_t err_code;
  312. switch (m_tx_state)
  313. {
  314. case TX_STATE_IDLE:
  315. if (event == TX_EVENT_STATE_ENTRY)
  316. {
  317. err_code = app_timer_stop(m_app_timer_id);
  318. APP_ERROR_CHECK(err_code);
  319. // Send TX-done event if registered handler exists.
  320. if (m_transport_tx_done_handle != NULL)
  321. {
  322. m_transport_tx_done_handle(m_tx_done_result_code);
  323. }
  324. }
  325. break;
  326. case TX_STATE_PENDING:
  327. if (event == TX_EVENT_SLIP_TX_DONE)
  328. {
  329. // @note: this call should always succeed as called from HCI_SLIP_TX_DONE context
  330. // and error cases are managed by dedicated error event from the slip layer.
  331. err_code = hci_slip_write(mp_tx_buffer,
  332. (m_tx_buffer_length + PKT_HDR_SIZE + PKT_CRC_SIZE));
  333. APP_ERROR_CHECK(err_code);
  334. tx_sm_state_change(TX_STATE_ACTIVE);
  335. }
  336. break;
  337. case TX_STATE_ACTIVE:
  338. switch (event)
  339. {
  340. case TX_EVENT_VALID_RX_ACK:
  341. // Tx sequence number counter incremented as packet transmission
  342. // acknowledged by peer transport entity.
  343. packet_number_tx_inc();
  344. tx_sm_state_change(TX_STATE_IDLE);
  345. break;
  346. case TX_EVENT_STATE_ENTRY:
  347. m_tx_retry_counter = 0;
  348. err_code = app_timer_start(m_app_timer_id,
  349. RETRANSMISSION_TIMEOUT_IN_TICKS,
  350. NULL);
  351. APP_ERROR_CHECK(err_code);
  352. break;
  353. case TX_EVENT_TIMEOUT:
  354. if (m_tx_retry_counter != MAX_RETRY_COUNT)
  355. {
  356. ++m_tx_retry_counter;
  357. // @note: no return value check done for hci_slip_write(...) call as current
  358. // system design allows use case where retransmission is not accepted by the
  359. // slip layer due to existing acknowledgement packet transmission in the
  360. // slip layer.
  361. UNUSED_VARIABLE(hci_slip_write(mp_tx_buffer,
  362. (m_tx_buffer_length +
  363. PKT_HDR_SIZE +
  364. PKT_CRC_SIZE)));
  365. }
  366. else
  367. {
  368. // Application packet retransmission count reached:
  369. // - set correct TX done event callback function result code
  370. // - execute state change
  371. // @note: m_tx_retry_counter is reset in TX_STATE_ACTIVE state entry.
  372. m_tx_done_result_code = HCI_TRANSPORT_TX_DONE_FAILURE;
  373. tx_sm_state_change(TX_STATE_IDLE);
  374. }
  375. break;
  376. default:
  377. // No implementation needed.
  378. break;
  379. }
  380. break;
  381. default:
  382. // No implementation needed.
  383. break;
  384. }
  385. }
  386. /**@brief Function for changing the state of the TX state machine.
  387. *
  388. * @param[in] new_state State TX state machine transits to.
  389. */
  390. static void tx_sm_state_change(tx_state_t new_state)
  391. {
  392. m_tx_state = new_state;
  393. tx_sm_event_handle(TX_EVENT_STATE_ENTRY);
  394. }
  395. /**@brief Function for handling slip events.
  396. *
  397. * @param[in] event The event structure.
  398. */
  399. void slip_event_handle(hci_slip_evt_t event)
  400. {
  401. uint32_t return_code;
  402. uint32_t err_code;
  403. switch (event.evt_type)
  404. {
  405. case HCI_SLIP_TX_DONE:
  406. tx_sm_event_handle(TX_EVENT_SLIP_TX_DONE);
  407. break;
  408. case HCI_SLIP_RX_RDY:
  409. return_code = packet_type_decode(event.packet, event.packet_length);
  410. switch (return_code)
  411. {
  412. case PKT_TYPE_VENDOR_SPECIFIC:
  413. rx_vendor_specific_pkt_type_handle(event.packet, event.packet_length);
  414. break;
  415. case PKT_TYPE_ACK:
  416. if (rx_ack_pkt_type_handle(event.packet))
  417. {
  418. // Valid expected acknowledgement packet received: set correct TX done event
  419. // callback function result code and execute state change.
  420. m_tx_done_result_code = HCI_TRANSPORT_TX_DONE_SUCCESS;
  421. tx_sm_event_handle(TX_EVENT_VALID_RX_ACK);
  422. }
  423. /* fall-through */
  424. default:
  425. // RX packet dropped: reset memory buffer to slip in order to avoid RX buffer
  426. // overflow.
  427. // If existing mem pool produced RX buffer exists reuse that one. If existing
  428. // mem pool produced RX buffer does not exist try to produce new one. If
  429. // producing fails use the internal acknowledgement buffer.
  430. if (mp_slip_used_rx_buffer != NULL)
  431. {
  432. err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE);
  433. APP_ERROR_CHECK(err_code);
  434. }
  435. else
  436. {
  437. err_code = hci_mem_pool_rx_produce(RX_BUF_SIZE,
  438. (void **)&mp_slip_used_rx_buffer);
  439. APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) ||
  440. (err_code == NRF_ERROR_NO_MEM));
  441. err_code = hci_slip_rx_buffer_register(
  442. (err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer,
  443. (err_code == NRF_SUCCESS) ? RX_BUF_SIZE : ACK_BUF_SIZE);
  444. APP_ERROR_CHECK(err_code);
  445. }
  446. break;
  447. }
  448. break;
  449. case HCI_SLIP_RX_OVERFLOW:
  450. err_code = hci_slip_rx_buffer_register(m_rx_ack_buffer, ACK_BUF_SIZE);
  451. APP_ERROR_CHECK(err_code);
  452. break;
  453. case HCI_SLIP_ERROR:
  454. APP_ERROR_HANDLER(event.evt_type);
  455. break;
  456. default:
  457. APP_ERROR_HANDLER(event.evt_type);
  458. break;
  459. }
  460. }
  461. uint32_t hci_transport_evt_handler_reg(hci_transport_event_handler_t event_handler)
  462. {
  463. uint32_t err_code;
  464. m_transport_event_handle = event_handler;
  465. err_code = hci_slip_evt_handler_register(slip_event_handle);
  466. APP_ERROR_CHECK(err_code);
  467. return (event_handler != NULL) ? NRF_SUCCESS : NRF_ERROR_NULL;
  468. }
  469. uint32_t hci_transport_tx_done_register(hci_transport_tx_done_handler_t event_handler)
  470. {
  471. uint32_t err_code;
  472. m_transport_tx_done_handle = event_handler;
  473. err_code = hci_slip_evt_handler_register(slip_event_handle);
  474. APP_ERROR_CHECK(err_code);
  475. return (event_handler != NULL) ? NRF_SUCCESS : NRF_ERROR_NULL;
  476. }
  477. /**@brief Function for handling the application packet retransmission timeout.
  478. *
  479. * This function is registered in the @ref app_timer module when a timer is created on
  480. * @ref hci_transport_open.
  481. *
  482. * @note This function must be executed in APP-LO context otherwise retransmission behaviour is
  483. * undefined, see @ref nrf51_system_integration_serialization.
  484. *
  485. * @param[in] p_context The timeout context.
  486. */
  487. void hci_transport_timeout_handle(void * p_context)
  488. {
  489. tx_sm_event_handle(TX_EVENT_TIMEOUT);
  490. }
  491. uint32_t hci_transport_open(void)
  492. {
  493. mp_tx_buffer = NULL;
  494. m_tx_buffer_length = 0;
  495. m_tx_retry_counter = 0;
  496. m_is_slip_decode_ready = false;
  497. m_tx_state = TX_STATE_IDLE;
  498. m_packet_expected_seq_number = INITIAL_ACK_NUMBER_EXPECTED;
  499. m_packet_transmit_seq_number = INITIAL_ACK_NUMBER_TX;
  500. m_tx_done_result_code = HCI_TRANSPORT_TX_DONE_FAILURE;
  501. uint32_t err_code = app_timer_create(&m_app_timer_id,
  502. APP_TIMER_MODE_REPEATED,
  503. hci_transport_timeout_handle);
  504. if (err_code != NRF_SUCCESS)
  505. {
  506. // @note: conduct required interface adjustment.
  507. return NRF_ERROR_INTERNAL;
  508. }
  509. err_code = hci_mem_pool_open();
  510. VERIFY_SUCCESS(err_code);
  511. err_code = hci_slip_open();
  512. VERIFY_SUCCESS(err_code);
  513. err_code = hci_mem_pool_rx_produce(RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer);
  514. if (err_code != NRF_SUCCESS)
  515. {
  516. // @note: conduct required interface adjustment.
  517. return NRF_ERROR_INTERNAL;
  518. }
  519. err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, RX_BUF_SIZE);
  520. return err_code;
  521. }
  522. uint32_t hci_transport_close(void)
  523. {
  524. uint32_t err_code;
  525. m_transport_tx_done_handle = NULL;
  526. m_transport_event_handle = NULL;
  527. err_code = hci_mem_pool_close();
  528. APP_ERROR_CHECK(err_code);
  529. err_code = hci_slip_close();
  530. APP_ERROR_CHECK(err_code);
  531. // @note: NRF_ERROR_NO_MEM is the only return value which should never be returned.
  532. err_code = app_timer_stop(m_app_timer_id);
  533. APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_NO_MEM);
  534. return NRF_SUCCESS;
  535. }
  536. uint32_t hci_transport_tx_alloc(uint8_t ** pp_memory)
  537. {
  538. const uint32_t err_code = hci_mem_pool_tx_alloc((void **)pp_memory);
  539. if (err_code == NRF_SUCCESS)
  540. {
  541. // @note: no need to validate pp_memory against null as validation has already been done
  542. // by hci_mem_pool_tx_alloc(...) and visible to us from the method return code.
  543. //lint -e(413) "Likely use of null pointer"
  544. *pp_memory += PKT_HDR_SIZE;
  545. }
  546. return err_code;
  547. }
  548. uint32_t hci_transport_tx_free(void)
  549. {
  550. return hci_mem_pool_tx_free();
  551. }
  552. /**@brief Function for constructing 1st byte of the packet header of the packet to be transmitted.
  553. *
  554. * @return 1st byte of the packet header of the packet to be transmitted
  555. */
  556. static __INLINE uint8_t tx_packet_byte_zero_construct(void)
  557. {
  558. const uint32_t value = DATA_INTEGRITY_MASK |
  559. RELIABLE_PKT_MASK |
  560. (packet_number_expected_get() << 3u) |
  561. packet_number_to_transmit_get();
  562. return (uint8_t) value;
  563. }
  564. /**@brief Function for handling the application packet write request in tx-idle state.
  565. */
  566. static uint32_t pkt_write_handle(void)
  567. {
  568. uint32_t err_code;
  569. // Set packet header fields.
  570. mp_tx_buffer -= PKT_HDR_SIZE;
  571. mp_tx_buffer[0] = tx_packet_byte_zero_construct();
  572. const uint16_t type_and_length_fields = ((m_tx_buffer_length << 4u) | PKT_TYPE_VENDOR_SPECIFIC);
  573. // @note: no use case for uint16_encode(...) return value.
  574. UNUSED_VARIABLE(uint16_encode(type_and_length_fields, &(mp_tx_buffer[1])));
  575. mp_tx_buffer[3] = header_checksum_calculate(mp_tx_buffer);
  576. // Calculate, append CRC to the packet and write it.
  577. const uint16_t crc = crc16_compute(mp_tx_buffer, (PKT_HDR_SIZE + m_tx_buffer_length), NULL);
  578. // @note: no use case for uint16_encode(...) return value.
  579. UNUSED_VARIABLE(uint16_encode(crc, &(mp_tx_buffer[PKT_HDR_SIZE + m_tx_buffer_length])));
  580. err_code = hci_slip_write(mp_tx_buffer, (m_tx_buffer_length + PKT_HDR_SIZE + PKT_CRC_SIZE));
  581. switch (err_code)
  582. {
  583. case NRF_SUCCESS:
  584. tx_sm_state_change(TX_STATE_ACTIVE);
  585. break;
  586. case NRF_ERROR_NO_MEM:
  587. tx_sm_state_change(TX_STATE_PENDING);
  588. err_code = NRF_SUCCESS;
  589. break;
  590. default:
  591. // No implementation needed.
  592. break;
  593. }
  594. return err_code;
  595. }
  596. uint32_t hci_transport_pkt_write(const uint8_t * p_buffer, uint16_t length)
  597. {
  598. uint32_t err_code;
  599. if (p_buffer)
  600. {
  601. switch (m_tx_state)
  602. {
  603. case TX_STATE_IDLE:
  604. mp_tx_buffer = (uint8_t *)p_buffer;
  605. m_tx_buffer_length = length;
  606. err_code = pkt_write_handle();
  607. break;
  608. default:
  609. err_code = NRF_ERROR_NO_MEM;
  610. break;
  611. }
  612. }
  613. else
  614. {
  615. err_code = NRF_ERROR_NULL;
  616. }
  617. return err_code;
  618. }
  619. uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length)
  620. {
  621. uint32_t err_code;
  622. if (pp_buffer != NULL && p_length != NULL)
  623. {
  624. uint32_t length = 0;
  625. if (m_is_slip_decode_ready)
  626. {
  627. m_is_slip_decode_ready = false;
  628. err_code = hci_mem_pool_rx_extract(pp_buffer, &length);
  629. length -= (PKT_HDR_SIZE + PKT_CRC_SIZE);
  630. *p_length = (uint16_t)length;
  631. *pp_buffer += PKT_HDR_SIZE;
  632. }
  633. else
  634. {
  635. err_code = NRF_ERROR_NO_MEM;
  636. }
  637. }
  638. else
  639. {
  640. err_code = NRF_ERROR_NULL;
  641. }
  642. return err_code;
  643. }
  644. uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer)
  645. {
  646. return (hci_mem_pool_rx_consume(p_buffer - PKT_HDR_SIZE));
  647. }