hci_transport.c 30 KB

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