nrf_ble_gatt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * Copyright (c) 2016 - 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(NRF_BLE_GATT)
  42. #include "nrf_ble_gatt.h"
  43. #define NRF_LOG_MODULE_NAME nrf_ble_gatt
  44. #include "nrf_log.h"
  45. NRF_LOG_MODULE_REGISTER();
  46. #include "nrf_strerror.h"
  47. #define BLE_GAP_DATA_LENGTH_DEFAULT 27 //!< The stack's default data length.
  48. #define BLE_GAP_DATA_LENGTH_MAX 251 //!< Maximum data length.
  49. STATIC_ASSERT(NRF_SDH_BLE_GAP_DATA_LENGTH < 252);
  50. /**@brief Initialize a link's parameters to defaults. */
  51. static void link_init(nrf_ble_gatt_link_t * p_link)
  52. {
  53. p_link->att_mtu_desired = NRF_SDH_BLE_GATT_MAX_MTU_SIZE;
  54. p_link->att_mtu_effective = BLE_GATT_ATT_MTU_DEFAULT;
  55. p_link->att_mtu_exchange_pending = false;
  56. p_link->att_mtu_exchange_requested = false;
  57. #if !defined (S112) && !defined(S312) && !defined (S122)
  58. p_link->data_length_desired = NRF_SDH_BLE_GAP_DATA_LENGTH;
  59. p_link->data_length_effective = BLE_GAP_DATA_LENGTH_DEFAULT;
  60. #endif // !defined (S112) && !defined(S312) && !defined (S122)
  61. }
  62. /**@brief Start a data length update request procedure on a given connection. */
  63. #if !defined (S112) && !defined(S312) && !defined(S122)
  64. static ret_code_t data_length_update(uint16_t conn_handle, uint16_t data_length)
  65. {
  66. NRF_LOG_DEBUG("Updating data length to %u on connection 0x%x.",
  67. data_length, conn_handle);
  68. ble_gap_data_length_params_t const dlp =
  69. {
  70. .max_rx_octets = data_length,
  71. .max_tx_octets = data_length,
  72. .max_rx_time_us = BLE_GAP_DATA_LENGTH_AUTO,
  73. .max_tx_time_us = BLE_GAP_DATA_LENGTH_AUTO,
  74. };
  75. ble_gap_data_length_limitation_t dll = {0};
  76. ret_code_t err_code = sd_ble_gap_data_length_update(conn_handle, &dlp, &dll);
  77. if (err_code != NRF_SUCCESS)
  78. {
  79. NRF_LOG_ERROR("sd_ble_gap_data_length_update() (request) on connection 0x%x returned %s.",
  80. conn_handle, nrf_strerror_get(err_code));
  81. if ( (dll.tx_payload_limited_octets != 0)
  82. || (dll.rx_payload_limited_octets != 0))
  83. {
  84. NRF_LOG_ERROR("The requested TX/RX packet length is too long by %u/%u octets.",
  85. dll.tx_payload_limited_octets, dll.rx_payload_limited_octets);
  86. }
  87. if (dll.tx_rx_time_limited_us != 0)
  88. {
  89. NRF_LOG_ERROR("The requested combination of TX and RX packet lengths "
  90. "is too long by %u microseconds.",
  91. dll.tx_rx_time_limited_us);
  92. }
  93. }
  94. return err_code;
  95. }
  96. #endif // !defined (S112) && !defined(S312) && !defined (S122)
  97. /**@brief Handle a connected event.
  98. *
  99. * Begins an ATT MTU exchange procedure, followed by a data length update request as necessary.
  100. *
  101. * @param[in] p_gatt GATT structure.
  102. * @param[in] p_ble_evt Event received from the BLE stack.
  103. */
  104. static void on_connected_evt(nrf_ble_gatt_t * p_gatt, ble_evt_t const * p_ble_evt)
  105. {
  106. ret_code_t err_code;
  107. uint16_t conn_handle = p_ble_evt->evt.common_evt.conn_handle;
  108. nrf_ble_gatt_link_t * p_link = &p_gatt->links[conn_handle];
  109. // Update the link desired settings to reflect the current global settings.
  110. #if !defined (S112) && !defined(S312) && !defined(S122)
  111. p_link->data_length_desired = p_gatt->data_length;
  112. #endif // !defined (S112) && !defined(S312) && !defined (S122)
  113. switch (p_ble_evt->evt.gap_evt.params.connected.role)
  114. {
  115. #if !defined (S122)
  116. case BLE_GAP_ROLE_PERIPH:
  117. p_link->att_mtu_desired = p_gatt->att_mtu_desired_periph;
  118. break;
  119. #endif // !defined (S122)
  120. #if !defined (S112) && !defined(S312) && !defined(S113)
  121. case BLE_GAP_ROLE_CENTRAL:
  122. p_link->att_mtu_desired = p_gatt->att_mtu_desired_central;
  123. break;
  124. #endif // !defined (S112) && !defined(S312) && !defined(S113)
  125. default:
  126. // Ignore.
  127. break;
  128. }
  129. // Begin an ATT MTU exchange if necessary.
  130. if (p_link->att_mtu_desired > p_link->att_mtu_effective)
  131. {
  132. NRF_LOG_DEBUG("Requesting to update ATT MTU to %u bytes on connection 0x%x.",
  133. p_link->att_mtu_desired, conn_handle);
  134. err_code = sd_ble_gattc_exchange_mtu_request(conn_handle, p_link->att_mtu_desired);
  135. if (err_code == NRF_SUCCESS)
  136. {
  137. p_link->att_mtu_exchange_requested = true;
  138. }
  139. else if (err_code == NRF_ERROR_BUSY)
  140. {
  141. p_link->att_mtu_exchange_pending = true;
  142. NRF_LOG_DEBUG("sd_ble_gattc_exchange_mtu_request()"
  143. " on connection 0x%x returned busy, will retry.", conn_handle);
  144. }
  145. else
  146. {
  147. NRF_LOG_ERROR("sd_ble_gattc_exchange_mtu_request() returned %s.",
  148. nrf_strerror_get(err_code));
  149. }
  150. }
  151. #if !defined (S112) && !defined(S312) && !defined (S122)
  152. // Send a data length update request if necessary.
  153. if (p_link->data_length_desired > p_link->data_length_effective)
  154. {
  155. (void) data_length_update(conn_handle, p_link->data_length_desired);
  156. }
  157. #endif // !defined (S112) && !defined(S312) && !defined (S122)
  158. }
  159. static void on_disconnected_evt(nrf_ble_gatt_t * p_gatt, ble_evt_t const * p_ble_evt)
  160. {
  161. // Reset connection parameters.
  162. link_init(&p_gatt->links[p_ble_evt->evt.gap_evt.conn_handle]);
  163. }
  164. /**@brief Handle a BLE_GATTC_EVT_EXCHANGE_MTU_RSP event.
  165. *
  166. * @details The effective ATT MTU is set to the lowest between what we requested and the peer's
  167. * response. This events concludes the ATT MTU exchange. An event is sent to the user
  168. * and a data length update procedure is started if necessary.
  169. *
  170. * @param[in] p_gatt GATT structure.
  171. * @param[in] p_ble_evt Event received from the BLE stack.
  172. */
  173. static void on_exchange_mtu_rsp_evt(nrf_ble_gatt_t * p_gatt, ble_evt_t const * p_ble_evt)
  174. {
  175. uint16_t conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
  176. uint16_t server_rx_mtu = p_ble_evt->evt.gattc_evt.params.exchange_mtu_rsp.server_rx_mtu;
  177. nrf_ble_gatt_link_t * p_link = &p_gatt->links[conn_handle];
  178. // Determine the lowest MTU between our own desired MTU and the peer's.
  179. // The MTU may not be less than BLE_GATT_ATT_MTU_DEFAULT.
  180. p_link->att_mtu_effective = MIN(server_rx_mtu, p_link->att_mtu_desired);
  181. p_link->att_mtu_effective = MAX(p_link->att_mtu_effective, BLE_GATT_ATT_MTU_DEFAULT);
  182. NRF_LOG_DEBUG("ATT MTU updated to %u bytes on connection 0x%x (response).",
  183. p_link->att_mtu_effective, conn_handle);
  184. // Trigger an event indicating that the ATT MTU size has changed.
  185. // Send an event to the application only if an ATT MTU exchange was requested.
  186. if ((p_gatt->evt_handler != NULL) && (p_link->att_mtu_exchange_requested))
  187. {
  188. nrf_ble_gatt_evt_t const evt =
  189. {
  190. .evt_id = NRF_BLE_GATT_EVT_ATT_MTU_UPDATED,
  191. .conn_handle = conn_handle,
  192. .params.att_mtu_effective = p_link->att_mtu_effective,
  193. };
  194. p_gatt->evt_handler(p_gatt, &evt);
  195. }
  196. p_link->att_mtu_exchange_requested = false;
  197. p_link->att_mtu_exchange_pending = false;
  198. }
  199. /**@brief Handle a BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST event.
  200. *
  201. * @param[in] p_gatt GATT structure.
  202. * @param[in] p_ble_evt Event received from the BLE stack.
  203. */
  204. static void on_exchange_mtu_request_evt(nrf_ble_gatt_t * p_gatt, ble_evt_t const * p_ble_evt)
  205. {
  206. ret_code_t err_code;
  207. uint16_t conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
  208. uint16_t client_mtu = p_ble_evt->evt.gatts_evt.params.exchange_mtu_request.client_rx_mtu;
  209. nrf_ble_gatt_link_t * p_link = &p_gatt->links[conn_handle];
  210. NRF_LOG_DEBUG("Peer on connection 0x%x requested an ATT MTU of %u bytes.",
  211. conn_handle, client_mtu);
  212. client_mtu = MAX(client_mtu, BLE_GATT_ATT_MTU_DEFAULT);
  213. p_link->att_mtu_effective = MIN(client_mtu, p_link->att_mtu_desired);
  214. p_link->att_mtu_exchange_pending = false;
  215. NRF_LOG_DEBUG("Updating ATT MTU to %u bytes (desired: %u) on connection 0x%x.",
  216. p_link->att_mtu_effective, p_link->att_mtu_desired, conn_handle);
  217. err_code = sd_ble_gatts_exchange_mtu_reply(conn_handle, p_link->att_mtu_desired);
  218. if (err_code != NRF_SUCCESS)
  219. {
  220. NRF_LOG_ERROR("sd_ble_gatts_exchange_mtu_reply() returned %s.", nrf_strerror_get(err_code));
  221. }
  222. // If an ATT_MTU exchange was requested to the peer, defer sending
  223. // the data length update request and the event to the application until
  224. // the response for that request is received.
  225. if (p_link->att_mtu_exchange_requested)
  226. {
  227. return;
  228. }
  229. // The ATT MTU exchange has finished. Send an event to the application.
  230. if (p_gatt->evt_handler != NULL)
  231. {
  232. nrf_ble_gatt_evt_t const evt =
  233. {
  234. .evt_id = NRF_BLE_GATT_EVT_ATT_MTU_UPDATED,
  235. .conn_handle = conn_handle,
  236. .params.att_mtu_effective = p_link->att_mtu_effective,
  237. };
  238. p_gatt->evt_handler(p_gatt, &evt);
  239. }
  240. }
  241. /**@brief Handle a BLE_GAP_EVT_DATA_LENGTH_UPDATE event.
  242. *
  243. * @details Update the connection data length and send an event to the user.
  244. *
  245. * @param[in] p_gatt GATT structure.
  246. * @param[in] p_ble_evt Event received from the BLE stack.
  247. */
  248. #if !defined (S112) && !defined(S312) && !defined (S122)
  249. static void on_data_length_update_evt(nrf_ble_gatt_t * p_gatt, ble_evt_t const * p_ble_evt)
  250. {
  251. ble_gap_evt_t const gap_evt = p_ble_evt->evt.gap_evt;
  252. uint16_t const conn_handle = gap_evt.conn_handle;
  253. // Update the connection data length.
  254. // The SoftDevice only supports symmetric RX/TX data length settings.
  255. p_gatt->links[conn_handle].data_length_effective =
  256. gap_evt.params.data_length_update.effective_params.max_tx_octets;
  257. NRF_LOG_DEBUG("Data length updated to %u on connection 0x%0x.",
  258. p_gatt->links[conn_handle].data_length_effective,
  259. conn_handle);
  260. NRF_LOG_DEBUG("max_rx_octets: %u",
  261. gap_evt.params.data_length_update.effective_params.max_rx_octets);
  262. NRF_LOG_DEBUG("max_tx_octets: %u",
  263. gap_evt.params.data_length_update.effective_params.max_tx_octets);
  264. NRF_LOG_DEBUG("max_rx_time: %u",
  265. gap_evt.params.data_length_update.effective_params.max_rx_time_us);
  266. NRF_LOG_DEBUG("max_tx_time: %u",
  267. gap_evt.params.data_length_update.effective_params.max_tx_time_us);
  268. if (p_gatt->evt_handler != NULL)
  269. {
  270. nrf_ble_gatt_evt_t const evt =
  271. {
  272. .evt_id = NRF_BLE_GATT_EVT_DATA_LENGTH_UPDATED,
  273. .conn_handle = conn_handle,
  274. .params.data_length = p_gatt->links[conn_handle].data_length_effective,
  275. };
  276. p_gatt->evt_handler(p_gatt, &evt);
  277. }
  278. }
  279. /**@brief Handle a BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST event.
  280. *
  281. *@details Reply with a sd_ble_gap_data_length_update() call, using the minimum between the
  282. * link's preferred data length, and what requested by the peer.
  283. * The link preferred data length is set to the global preferred data length
  284. * upon connection and can be overridden by calling nrf_ble_gatt_data_length_set().
  285. * The default is NRF_SDH_BLE_GAP_DATA_LENGTH.
  286. *
  287. *@note The SoftDevice will not send any BLE_GAP_EVT_DATA_LENGTH_UPDATE events on this side.
  288. * Therefore, the connection data length is updated immediately and an event is sent
  289. * to the user.
  290. *
  291. * @param[in] p_gatt GATT structure.
  292. * @param[in] p_ble_evt Event received from the BLE stack.
  293. */
  294. static void on_data_length_update_request_evt(nrf_ble_gatt_t * p_gatt, ble_evt_t const * p_ble_evt)
  295. {
  296. ble_gap_evt_t const * p_gap_evt = &p_ble_evt->evt.gap_evt;
  297. nrf_ble_gatt_link_t * p_link = &p_gatt->links[p_gap_evt->conn_handle];
  298. // The SoftDevice only supports symmetric RX/TX data length settings.
  299. uint8_t const data_length_requested =
  300. p_gap_evt->params.data_length_update_request.peer_params.max_tx_octets;
  301. NRF_LOG_DEBUG("Peer on connection 0x%x requested a data length of %u bytes.",
  302. p_gap_evt->conn_handle, data_length_requested);
  303. uint8_t const data_length_effective = MIN(p_link->data_length_desired, data_length_requested);
  304. (void) data_length_update(p_gap_evt->conn_handle, data_length_effective);
  305. }
  306. #endif // !defined (S112) && !defined(S312) && !defined (S122)
  307. ret_code_t nrf_ble_gatt_init(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_handler_t evt_handler)
  308. {
  309. VERIFY_PARAM_NOT_NULL(p_gatt);
  310. p_gatt->evt_handler = evt_handler;
  311. p_gatt->att_mtu_desired_periph = NRF_SDH_BLE_GATT_MAX_MTU_SIZE;
  312. p_gatt->att_mtu_desired_central = NRF_SDH_BLE_GATT_MAX_MTU_SIZE;
  313. p_gatt->data_length = NRF_SDH_BLE_GAP_DATA_LENGTH;
  314. for (uint32_t i = 0; i < NRF_BLE_GATT_LINK_COUNT; i++)
  315. {
  316. link_init(&p_gatt->links[i]);
  317. }
  318. return NRF_SUCCESS;
  319. }
  320. ret_code_t nrf_ble_gatt_att_mtu_periph_set(nrf_ble_gatt_t * p_gatt, uint16_t desired_mtu)
  321. {
  322. VERIFY_PARAM_NOT_NULL(p_gatt);
  323. if ((desired_mtu < BLE_GATT_ATT_MTU_DEFAULT) || (desired_mtu > NRF_SDH_BLE_GATT_MAX_MTU_SIZE))
  324. {
  325. return NRF_ERROR_INVALID_PARAM;
  326. }
  327. p_gatt->att_mtu_desired_periph = desired_mtu;
  328. return NRF_SUCCESS;
  329. }
  330. ret_code_t nrf_ble_gatt_att_mtu_central_set(nrf_ble_gatt_t * p_gatt, uint16_t desired_mtu)
  331. {
  332. VERIFY_PARAM_NOT_NULL(p_gatt);
  333. if ((desired_mtu < BLE_GATT_ATT_MTU_DEFAULT) || (desired_mtu > NRF_SDH_BLE_GATT_MAX_MTU_SIZE))
  334. {
  335. return NRF_ERROR_INVALID_PARAM;
  336. }
  337. p_gatt->att_mtu_desired_central = desired_mtu;
  338. return NRF_SUCCESS;
  339. }
  340. uint16_t nrf_ble_gatt_eff_mtu_get(nrf_ble_gatt_t const * p_gatt, uint16_t conn_handle)
  341. {
  342. if ((p_gatt == NULL) || (conn_handle >= NRF_BLE_GATT_LINK_COUNT))
  343. {
  344. return 0;
  345. }
  346. return p_gatt->links[conn_handle].att_mtu_effective;
  347. }
  348. #if !defined (S112) && !defined(S312) && !defined (S122)
  349. ret_code_t nrf_ble_gatt_data_length_set(nrf_ble_gatt_t * p_gatt,
  350. uint16_t conn_handle,
  351. uint8_t data_length)
  352. {
  353. if (p_gatt == NULL)
  354. {
  355. return NRF_ERROR_NULL;
  356. }
  357. // Check early to avoid requesting an invalid data length for upcoming connections.
  358. if ( (data_length > BLE_GAP_DATA_LENGTH_MAX)
  359. || (data_length < BLE_GAP_DATA_LENGTH_DEFAULT))
  360. {
  361. return NRF_ERROR_INVALID_PARAM;
  362. }
  363. if (conn_handle == BLE_CONN_HANDLE_INVALID)
  364. {
  365. // Save value and request upon connection.
  366. p_gatt->data_length = data_length;
  367. return NRF_SUCCESS;
  368. }
  369. if (conn_handle >= NRF_BLE_GATT_LINK_COUNT)
  370. {
  371. return NRF_ERROR_INVALID_PARAM;
  372. }
  373. // Request data length on existing link.
  374. p_gatt->links[conn_handle].data_length_desired = data_length;
  375. return data_length_update(conn_handle, data_length);
  376. }
  377. ret_code_t nrf_ble_gatt_data_length_get(nrf_ble_gatt_t const * p_gatt,
  378. uint16_t conn_handle,
  379. uint8_t * p_data_length)
  380. {
  381. if ((p_gatt == NULL) || (p_data_length == NULL))
  382. {
  383. return NRF_ERROR_NULL;
  384. }
  385. if (conn_handle == BLE_CONN_HANDLE_INVALID)
  386. {
  387. *p_data_length = p_gatt->data_length;
  388. return NRF_SUCCESS;
  389. }
  390. if (conn_handle >= NRF_BLE_GATT_LINK_COUNT)
  391. {
  392. return NRF_ERROR_INVALID_PARAM;
  393. }
  394. *p_data_length = p_gatt->links[conn_handle].data_length_effective;
  395. return NRF_SUCCESS;
  396. }
  397. #endif // !defined (S112) && !defined(S312) && !defined(S122)
  398. void nrf_ble_gatt_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  399. {
  400. nrf_ble_gatt_t * p_gatt = (nrf_ble_gatt_t *)p_context;
  401. uint16_t conn_handle = p_ble_evt->evt.common_evt.conn_handle;
  402. if (conn_handle >= NRF_BLE_GATT_LINK_COUNT)
  403. {
  404. return;
  405. }
  406. switch (p_ble_evt->header.evt_id)
  407. {
  408. case BLE_GAP_EVT_CONNECTED:
  409. on_connected_evt(p_gatt, p_ble_evt);
  410. break;
  411. case BLE_GAP_EVT_DISCONNECTED:
  412. on_disconnected_evt(p_gatt, p_ble_evt);
  413. break;
  414. case BLE_GATTC_EVT_EXCHANGE_MTU_RSP:
  415. on_exchange_mtu_rsp_evt(p_gatt, p_ble_evt);
  416. break;
  417. case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST:
  418. on_exchange_mtu_request_evt(p_gatt, p_ble_evt);
  419. break;
  420. #if !defined (S112) && !defined(S312) && !defined (S122)
  421. case BLE_GAP_EVT_DATA_LENGTH_UPDATE:
  422. on_data_length_update_evt(p_gatt, p_ble_evt);
  423. break;
  424. case BLE_GAP_EVT_DATA_LENGTH_UPDATE_REQUEST:
  425. on_data_length_update_request_evt(p_gatt, p_ble_evt);
  426. break;
  427. #endif // !defined (S112) && !defined(S312) && !defined (S122)
  428. default:
  429. break;
  430. }
  431. if (p_gatt->links[conn_handle].att_mtu_exchange_pending)
  432. {
  433. ret_code_t err_code;
  434. err_code = sd_ble_gattc_exchange_mtu_request(conn_handle,
  435. p_gatt->links[conn_handle].att_mtu_desired);
  436. if (err_code == NRF_SUCCESS)
  437. {
  438. p_gatt->links[conn_handle].att_mtu_exchange_pending = false;
  439. p_gatt->links[conn_handle].att_mtu_exchange_requested = true;
  440. NRF_LOG_DEBUG("Requesting to update ATT MTU to %u bytes on connection 0x%x (retry).",
  441. p_gatt->links[conn_handle].att_mtu_desired, conn_handle);
  442. }
  443. else if (err_code != NRF_ERROR_BUSY)
  444. {
  445. NRF_LOG_ERROR("sd_ble_gattc_exchange_mtu_request() returned %s.",
  446. nrf_strerror_get(err_code));
  447. }
  448. }
  449. }
  450. #endif //NRF_BLE_GATT_ENABLED