gatt_cache_manager.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /**
  2. * Copyright (c) 2015 - 2018, 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(PEER_MANAGER)
  42. #include "gatt_cache_manager.h"
  43. #include "ble_gap.h"
  44. #include "ble_err.h"
  45. #include "ble_conn_state.h"
  46. #include "peer_manager_types.h"
  47. #include "peer_manager_internal.h"
  48. #include "id_manager.h"
  49. #include "gatts_cache_manager.h"
  50. #include "peer_data_storage.h"
  51. #include "peer_database.h"
  52. #include "nrf_mtx.h"
  53. #define NRF_LOG_MODULE_NAME peer_manager_gcm
  54. #if PM_LOG_ENABLED
  55. #define NRF_LOG_LEVEL PM_LOG_LEVEL
  56. #define NRF_LOG_INFO_COLOR PM_LOG_INFO_COLOR
  57. #define NRF_LOG_DEBUG_COLOR PM_LOG_DEBUG_COLOR
  58. #else
  59. #define NRF_LOG_LEVEL 0
  60. #endif // PM_LOG_ENABLED
  61. #include "nrf_log.h"
  62. #include "nrf_log_ctrl.h"
  63. NRF_LOG_MODULE_REGISTER();
  64. #include "nrf_strerror.h"
  65. // The number of registered event handlers.
  66. #define GCM_EVENT_HANDLERS_CNT (sizeof(m_evt_handlers) / sizeof(m_evt_handlers[0]))
  67. // GATT Cache Manager event handler in Peer Manager.
  68. extern void pm_gcm_evt_handler(pm_evt_t * p_gcm_evt);
  69. // GATT Cache Manager events' handlers.
  70. // The number of elements in this array is GCM_EVENT_HANDLERS_CNT.
  71. static pm_evt_handler_internal_t m_evt_handlers[] =
  72. {
  73. pm_gcm_evt_handler
  74. };
  75. static bool m_module_initialized;
  76. static nrf_mtx_t m_db_update_in_progress_mutex; /**< Mutex indicating whether a local DB write operation is ongoing. */
  77. static ble_conn_state_user_flag_id_t m_flag_local_db_update_pending; /**< Flag ID for flag collection to keep track of which connections need a local DB update procedure. */
  78. static ble_conn_state_user_flag_id_t m_flag_local_db_apply_pending; /**< Flag ID for flag collection to keep track of which connections need a local DB apply procedure. */
  79. static ble_conn_state_user_flag_id_t m_flag_service_changed_pending; /**< Flag ID for flag collection to keep track of which connections need to be sent a service changed indication. */
  80. static ble_conn_state_user_flag_id_t m_flag_service_changed_sent; /**< Flag ID for flag collection to keep track of which connections have been sent a service changed indication and are waiting for a handle value confirmation. */
  81. static ble_conn_state_user_flag_id_t m_flag_car_update_pending; /**< Flag ID for flag collection to keep track of which connections need to have their Central Address Resolution value stored. */
  82. #ifdef PM_SERVICE_CHANGED_ENABLED
  83. STATIC_ASSERT(PM_SERVICE_CHANGED_ENABLED || !NRF_SDH_BLE_SERVICE_CHANGED,
  84. "PM_SERVICE_CHANGED_ENABLED should be enabled if NRF_SDH_BLE_SERVICE_CHANGED is enabled.");
  85. #else
  86. #define PM_SERVICE_CHANGED_ENABLED 1
  87. #endif
  88. /**@brief Function for resetting the module variable(s) of the GSCM module.
  89. *
  90. * @param[out] The instance to reset.
  91. */
  92. static void internal_state_reset()
  93. {
  94. m_module_initialized = false;
  95. }
  96. static void evt_send(pm_evt_t * p_gcm_evt)
  97. {
  98. p_gcm_evt->peer_id = im_peer_id_get_by_conn_handle(p_gcm_evt->conn_handle);
  99. for (uint32_t i = 0; i < GCM_EVENT_HANDLERS_CNT; i++)
  100. {
  101. m_evt_handlers[i](p_gcm_evt);
  102. }
  103. }
  104. /**@brief Function for checking a write event for whether a CCCD was written during the write
  105. * operation.
  106. *
  107. * @param[in] p_write_evt The parameters of the write event.
  108. *
  109. * @return Whether the write was on a CCCD.
  110. */
  111. static bool cccd_written(ble_gatts_evt_write_t const * p_write_evt)
  112. {
  113. return ( (p_write_evt->op == BLE_GATTS_OP_WRITE_REQ)
  114. && (p_write_evt->uuid.type == BLE_UUID_TYPE_BLE)
  115. && (p_write_evt->uuid.uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG)
  116. );
  117. }
  118. /**@brief Function for sending an PM_EVT_ERROR_UNEXPECTED event.
  119. *
  120. * @param[in] conn_handle The connection handle the event pertains to.
  121. * @param[in] err_code The unexpected error that occurred.
  122. */
  123. static void send_unexpected_error(uint16_t conn_handle, ret_code_t err_code)
  124. {
  125. pm_evt_t error_evt =
  126. {
  127. .evt_id = PM_EVT_ERROR_UNEXPECTED,
  128. .conn_handle = conn_handle,
  129. .params =
  130. {
  131. .error_unexpected =
  132. {
  133. .error = err_code,
  134. .fds_error = false
  135. }
  136. }
  137. };
  138. evt_send(&error_evt);
  139. }
  140. /**@brief Function for performing the local DB update procedure in an event context, where no return
  141. * code can be given.
  142. *
  143. * @details This function will do the procedure, and check the result, set a flag if needed, and
  144. * send an event if needed.
  145. *
  146. * @param[in] conn_handle The connection to perform the procedure on.
  147. */
  148. static void local_db_apply_in_evt(uint16_t conn_handle)
  149. {
  150. bool set_procedure_as_pending = false;
  151. ret_code_t err_code;
  152. pm_evt_t event =
  153. {
  154. .conn_handle = conn_handle,
  155. };
  156. if (conn_handle == BLE_CONN_HANDLE_INVALID)
  157. {
  158. return;
  159. }
  160. err_code = gscm_local_db_cache_apply(conn_handle);
  161. switch (err_code)
  162. {
  163. case NRF_SUCCESS:
  164. event.evt_id = PM_EVT_LOCAL_DB_CACHE_APPLIED;
  165. evt_send(&event);
  166. break;
  167. case NRF_ERROR_BUSY:
  168. set_procedure_as_pending = true;
  169. break;
  170. case NRF_ERROR_INVALID_DATA:
  171. event.evt_id = PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED;
  172. NRF_LOG_WARNING("The local database has changed, so some subscriptions to notifications "\
  173. "and indications could not be restored for conn_handle %d",
  174. conn_handle);
  175. evt_send(&event);
  176. break;
  177. case BLE_ERROR_INVALID_CONN_HANDLE:
  178. /* Do nothing */
  179. break;
  180. default:
  181. NRF_LOG_ERROR("gscm_local_db_cache_apply() returned %s which should not happen. "\
  182. "conn_handle: %d",
  183. nrf_strerror_get(err_code),
  184. conn_handle);
  185. send_unexpected_error(conn_handle, err_code);
  186. break;
  187. }
  188. ble_conn_state_user_flag_set(conn_handle, m_flag_local_db_apply_pending, set_procedure_as_pending);
  189. }
  190. /**@brief Function for asynchronously starting a DB update procedure.
  191. *
  192. * @note This procedure can only be started asynchronously.
  193. *
  194. * @param[in] conn_handle The connection to perform the procedure on.
  195. * @param[in] update Whether to perform the procedure.
  196. */
  197. static __INLINE void local_db_update(uint16_t conn_handle, bool update)
  198. {
  199. ble_conn_state_user_flag_set(conn_handle, m_flag_local_db_update_pending, update);
  200. }
  201. /**@brief Function for performing the local DB update procedure in an event context, where no return
  202. * code can be given.
  203. *
  204. * @details This function will do the procedure, and check the result, set a flag if needed, and
  205. * send an event if needed.
  206. *
  207. * @param[in] conn_handle The connection to perform the procedure on.
  208. */
  209. static bool local_db_update_in_evt(uint16_t conn_handle)
  210. {
  211. bool set_procedure_as_pending = false;
  212. bool success = false;
  213. ret_code_t err_code = gscm_local_db_cache_update(conn_handle);
  214. switch (err_code)
  215. {
  216. case NRF_SUCCESS:
  217. success = true;
  218. break;
  219. case BLE_ERROR_INVALID_CONN_HANDLE:
  220. /* Do nothing */
  221. break;
  222. case NRF_ERROR_BUSY:
  223. set_procedure_as_pending = true;
  224. break;
  225. case NRF_ERROR_STORAGE_FULL:
  226. {
  227. pm_evt_t event =
  228. {
  229. .evt_id = PM_EVT_STORAGE_FULL,
  230. .conn_handle = conn_handle,
  231. };
  232. NRF_LOG_WARNING("Flash full. Could not store data for conn_handle: %d", conn_handle);
  233. evt_send(&event);
  234. break;
  235. }
  236. default:
  237. NRF_LOG_ERROR("gscm_local_db_cache_update() returned %s for conn_handle: %d",
  238. nrf_strerror_get(err_code),
  239. conn_handle);
  240. send_unexpected_error(conn_handle, err_code);
  241. break;
  242. }
  243. local_db_update(conn_handle, set_procedure_as_pending);
  244. return success;
  245. }
  246. #if PM_SERVICE_CHANGED_ENABLED
  247. /**@brief Function for sending a service changed indication in an event context, where no return
  248. * code can be given.
  249. *
  250. * @details This function will do the procedure, and check the result, set a flag if needed, and
  251. * send an event if needed.
  252. *
  253. * @param[in] conn_handle The connection to perform the procedure on.
  254. */
  255. static void service_changed_send_in_evt(uint16_t conn_handle)
  256. {
  257. bool sc_pending_state = true;
  258. bool sc_sent_state = false;
  259. ret_code_t err_code = gscm_service_changed_ind_send(conn_handle);
  260. switch (err_code)
  261. {
  262. case NRF_SUCCESS:
  263. {
  264. pm_evt_t event =
  265. {
  266. .evt_id = PM_EVT_SERVICE_CHANGED_IND_SENT,
  267. .conn_handle = conn_handle,
  268. };
  269. sc_sent_state = true;
  270. evt_send(&event);
  271. break;
  272. }
  273. case NRF_ERROR_BUSY:
  274. // Do nothing.
  275. break;
  276. case NRF_ERROR_INVALID_STATE:
  277. // CCCDs not enabled. Drop indication.
  278. // Fallthrough.
  279. case NRF_ERROR_NOT_SUPPORTED:
  280. // Service changed not supported. Drop indication.
  281. sc_pending_state = false;
  282. gscm_db_change_notification_done(im_peer_id_get_by_conn_handle(conn_handle));
  283. break;
  284. case BLE_ERROR_GATTS_SYS_ATTR_MISSING:
  285. local_db_apply_in_evt(conn_handle);
  286. break;
  287. case BLE_ERROR_INVALID_CONN_HANDLE:
  288. // Do nothing.
  289. break;
  290. default:
  291. NRF_LOG_ERROR("gscm_service_changed_ind_send() returned %s for conn_handle: %d",
  292. nrf_strerror_get(err_code),
  293. conn_handle);
  294. send_unexpected_error(conn_handle, err_code);
  295. break;
  296. }
  297. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, sc_pending_state);
  298. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_sent, sc_sent_state);
  299. }
  300. #endif
  301. static void apply_pending_handle(uint16_t conn_handle, void * p_context)
  302. {
  303. UNUSED_PARAMETER(p_context);
  304. local_db_apply_in_evt(conn_handle);
  305. }
  306. static __INLINE void apply_pending_flags_check(void)
  307. {
  308. UNUSED_RETURN_VALUE(ble_conn_state_for_each_set_user_flag(m_flag_local_db_apply_pending,
  309. apply_pending_handle,
  310. NULL));
  311. }
  312. static void db_update_pending_handle(uint16_t conn_handle, void * p_context)
  313. {
  314. UNUSED_PARAMETER(p_context);
  315. if (nrf_mtx_trylock(&m_db_update_in_progress_mutex))
  316. {
  317. if (local_db_update_in_evt(conn_handle))
  318. {
  319. // Successfully started writing to flash.
  320. return;
  321. }
  322. else
  323. {
  324. nrf_mtx_unlock(&m_db_update_in_progress_mutex);
  325. }
  326. }
  327. }
  328. #if PM_SERVICE_CHANGED_ENABLED
  329. static void sc_send_pending_handle(uint16_t conn_handle, void * p_context)
  330. {
  331. UNUSED_PARAMETER(p_context);
  332. if (!ble_conn_state_user_flag_get(conn_handle, m_flag_service_changed_sent))
  333. {
  334. service_changed_send_in_evt(conn_handle);
  335. }
  336. }
  337. static __INLINE void service_changed_pending_flags_check(void)
  338. {
  339. UNUSED_RETURN_VALUE(ble_conn_state_for_each_set_user_flag(m_flag_service_changed_pending,
  340. sc_send_pending_handle,
  341. NULL));
  342. }
  343. static void service_changed_needed(uint16_t conn_handle)
  344. {
  345. if (gscm_service_changed_ind_needed(conn_handle))
  346. {
  347. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, true);
  348. }
  349. }
  350. #endif
  351. static void car_update_pending_handle(uint16_t conn_handle, void * p_context)
  352. {
  353. UNUSED_PARAMETER(p_context);
  354. ble_uuid_t car_uuid;
  355. memset(&car_uuid, 0, sizeof(ble_uuid_t));
  356. car_uuid.uuid = BLE_UUID_GAP_CHARACTERISTIC_CAR;
  357. car_uuid.type = BLE_UUID_TYPE_BLE;
  358. ble_gattc_handle_range_t const car_handle_range = {1, 0xFFFF};
  359. ret_code_t err_code = sd_ble_gattc_char_value_by_uuid_read(conn_handle, &car_uuid, &car_handle_range);
  360. UNUSED_RETURN_VALUE(err_code);
  361. }
  362. static void car_update_needed(uint16_t conn_handle)
  363. {
  364. pm_peer_data_t peer_data;
  365. if (pds_peer_data_read(im_peer_id_get_by_conn_handle(conn_handle),
  366. PM_PEER_DATA_ID_CENTRAL_ADDR_RES,
  367. &peer_data,
  368. NULL) == NRF_ERROR_NOT_FOUND)
  369. {
  370. ble_conn_state_user_flag_set(conn_handle, m_flag_car_update_pending, true);
  371. }
  372. }
  373. static __INLINE void update_pending_flags_check(void)
  374. {
  375. uint32_t count = ble_conn_state_for_each_set_user_flag(m_flag_local_db_update_pending,
  376. db_update_pending_handle,
  377. NULL);
  378. if (count == 0)
  379. {
  380. count = ble_conn_state_for_each_set_user_flag(m_flag_car_update_pending,
  381. car_update_pending_handle,
  382. NULL);
  383. UNUSED_RETURN_VALUE(count);
  384. }
  385. }
  386. /**@brief Callback function for events from the ID Manager module.
  387. * This function is registered in the ID Manager module.
  388. *
  389. * @param[in] p_event The event from the ID Manager module.
  390. */
  391. void gcm_im_evt_handler(pm_evt_t * p_event)
  392. {
  393. switch (p_event->evt_id)
  394. {
  395. case PM_EVT_BONDED_PEER_CONNECTED:
  396. local_db_apply_in_evt(p_event->conn_handle);
  397. #if (PM_SERVICE_CHANGED_ENABLED == 1)
  398. service_changed_needed(p_event->conn_handle);
  399. #endif
  400. car_update_needed(p_event->conn_handle);
  401. update_pending_flags_check();
  402. break;
  403. default:
  404. break;
  405. }
  406. }
  407. /**@brief Callback function for events from the Peer Database module.
  408. * This handler is extern in Peer Database.
  409. *
  410. * @param[in] p_event The event from the Security Dispatcher module.
  411. */
  412. void gcm_pdb_evt_handler(pm_evt_t * p_event)
  413. {
  414. if ( p_event->evt_id == PM_EVT_PEER_DATA_UPDATE_SUCCEEDED
  415. && p_event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE)
  416. {
  417. switch (p_event->params.peer_data_update_succeeded.data_id)
  418. {
  419. case PM_PEER_DATA_ID_BONDING:
  420. {
  421. uint16_t conn_handle = im_conn_handle_get(p_event->peer_id);
  422. if (conn_handle != BLE_CONN_HANDLE_INVALID)
  423. {
  424. local_db_update(conn_handle, true);
  425. car_update_needed(conn_handle);
  426. }
  427. break;
  428. }
  429. #if PM_SERVICE_CHANGED_ENABLED
  430. case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING:
  431. {
  432. ret_code_t err_code;
  433. pm_peer_data_flash_t peer_data;
  434. err_code = pdb_peer_data_ptr_get(p_event->peer_id,
  435. PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING,
  436. &peer_data);
  437. if (err_code == NRF_SUCCESS)
  438. {
  439. if (*peer_data.p_service_changed_pending)
  440. {
  441. uint16_t conn_handle = im_conn_handle_get(p_event->peer_id);
  442. if (conn_handle != BLE_CONN_HANDLE_INVALID)
  443. {
  444. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, true);
  445. service_changed_pending_flags_check();
  446. }
  447. }
  448. }
  449. break;
  450. }
  451. #endif
  452. case PM_PEER_DATA_ID_GATT_LOCAL:
  453. if (m_db_update_in_progress_mutex == NRF_MTX_LOCKED)
  454. {
  455. nrf_mtx_unlock(&m_db_update_in_progress_mutex);
  456. }
  457. // Expecting a call to update_pending_flags_check() immediately.
  458. break;
  459. default:
  460. /* No action */
  461. break;
  462. }
  463. }
  464. update_pending_flags_check();
  465. }
  466. ret_code_t gcm_init()
  467. {
  468. NRF_PM_DEBUG_CHECK(!m_module_initialized);
  469. internal_state_reset();
  470. m_flag_local_db_update_pending = ble_conn_state_user_flag_acquire();
  471. m_flag_local_db_apply_pending = ble_conn_state_user_flag_acquire();
  472. m_flag_service_changed_pending = ble_conn_state_user_flag_acquire();
  473. m_flag_service_changed_sent = ble_conn_state_user_flag_acquire();
  474. m_flag_car_update_pending = ble_conn_state_user_flag_acquire();
  475. if ((m_flag_local_db_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  476. || (m_flag_local_db_apply_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  477. || (m_flag_service_changed_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  478. || (m_flag_service_changed_sent == BLE_CONN_STATE_USER_FLAG_INVALID)
  479. || (m_flag_car_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  480. )
  481. {
  482. NRF_LOG_ERROR("Could not acquire conn_state user flags. Increase "\
  483. "BLE_CONN_STATE_USER_FLAG_COUNT in the ble_conn_state module.");
  484. return NRF_ERROR_INTERNAL;
  485. }
  486. nrf_mtx_init(&m_db_update_in_progress_mutex);
  487. m_module_initialized = true;
  488. return NRF_SUCCESS;
  489. }
  490. void store_car_value(uint16_t conn_handle, bool car_value)
  491. {
  492. // Use a uint32_t to enforce 4-byte alignment.
  493. static const uint32_t car_value_true = true;
  494. static const uint32_t car_value_false = false;
  495. pm_peer_data_const_t peer_data =
  496. {
  497. .data_id = PM_PEER_DATA_ID_CENTRAL_ADDR_RES,
  498. .length_words = 1,
  499. };
  500. peer_data.p_central_addr_res = car_value ? &car_value_true : &car_value_false;
  501. ret_code_t err_code = pds_peer_data_store(im_peer_id_get_by_conn_handle(conn_handle), &peer_data, NULL);
  502. if (err_code != NRF_SUCCESS)
  503. {
  504. NRF_LOG_WARNING("CAR char value couldn't be stored (error: %s). Reattempt will happen on the next connection.", nrf_strerror_get(err_code));
  505. }
  506. }
  507. /**@brief Callback function for BLE events from the SoftDevice.
  508. *
  509. * @param[in] p_ble_evt The BLE event from the SoftDevice.
  510. */
  511. void gcm_ble_evt_handler(ble_evt_t const * p_ble_evt)
  512. {
  513. uint16_t conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
  514. switch (p_ble_evt->header.evt_id)
  515. {
  516. case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  517. local_db_apply_in_evt(conn_handle);
  518. break;
  519. #if PM_SERVICE_CHANGED_ENABLED
  520. case BLE_GATTS_EVT_SC_CONFIRM:
  521. {
  522. pm_evt_t event =
  523. {
  524. .evt_id = PM_EVT_SERVICE_CHANGED_IND_CONFIRMED,
  525. .peer_id = im_peer_id_get_by_conn_handle(conn_handle),
  526. .conn_handle = conn_handle,
  527. };
  528. gscm_db_change_notification_done(event.peer_id);
  529. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_sent, false);
  530. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, false);
  531. evt_send(&event);
  532. break;
  533. }
  534. #endif
  535. case BLE_GATTS_EVT_WRITE:
  536. if (cccd_written(&p_ble_evt->evt.gatts_evt.params.write))
  537. {
  538. local_db_update(conn_handle, true);
  539. update_pending_flags_check();
  540. }
  541. break;
  542. case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
  543. {
  544. bool car_value = false;
  545. conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
  546. ble_conn_state_user_flag_set(conn_handle, m_flag_car_update_pending, false);
  547. if (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND)
  548. {
  549. // Store 0.
  550. }
  551. else if (p_ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS)
  552. {
  553. NRF_LOG_WARNING("Unexpected GATT status while getting CAR char value: 0x%x",
  554. p_ble_evt->evt.gattc_evt.gatt_status);
  555. // Store 0.
  556. }
  557. else
  558. {
  559. if (p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp.count != 1)
  560. {
  561. NRF_LOG_WARNING("Multiple (%d) CAR characteristics found, using the first.",
  562. p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp.count);
  563. }
  564. if (p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp.value_len != 1)
  565. {
  566. NRF_LOG_WARNING("Unexpected CAR characteristic value length (%d), store 0.",
  567. p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp.value_len);
  568. // Store 0.
  569. }
  570. else
  571. {
  572. car_value = *p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp.handle_value;
  573. }
  574. }
  575. store_car_value(conn_handle, car_value);
  576. break;
  577. }
  578. }
  579. apply_pending_flags_check();
  580. #if PM_SERVICE_CHANGED_ENABLED
  581. service_changed_pending_flags_check();
  582. #endif
  583. }
  584. ret_code_t gcm_local_db_cache_update(uint16_t conn_handle)
  585. {
  586. NRF_PM_DEBUG_CHECK(m_module_initialized);
  587. local_db_update(conn_handle, true);
  588. update_pending_flags_check();
  589. return NRF_SUCCESS;
  590. }
  591. #if PM_SERVICE_CHANGED_ENABLED
  592. void gcm_local_database_has_changed(void)
  593. {
  594. gscm_local_database_has_changed();
  595. ble_conn_state_conn_handle_list_t conn_handles = ble_conn_state_conn_handles();
  596. for (uint16_t i = 0; i < conn_handles.len; i++)
  597. {
  598. if (im_peer_id_get_by_conn_handle(conn_handles.conn_handles[i]) == PM_PEER_ID_INVALID)
  599. {
  600. ble_conn_state_user_flag_set(conn_handles.conn_handles[i], m_flag_service_changed_pending, true);
  601. }
  602. }
  603. service_changed_pending_flags_check();
  604. }
  605. #endif
  606. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)