gatt_cache_manager.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /**
  2. * Copyright (c) 2015 - 2019, 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. static ble_conn_state_user_flag_id_t m_flag_car_handle_queried; /**< Flag ID for flag collection to keep track of which connections are pending Central Address Resolution handle reply. */
  83. static ble_conn_state_user_flag_id_t m_flag_car_value_queried; /**< Flag ID for flag collection to keep track of which connections are pending Central Address Resolution value reply. */
  84. #ifdef PM_SERVICE_CHANGED_ENABLED
  85. STATIC_ASSERT(PM_SERVICE_CHANGED_ENABLED || !NRF_SDH_BLE_SERVICE_CHANGED,
  86. "PM_SERVICE_CHANGED_ENABLED should be enabled if NRF_SDH_BLE_SERVICE_CHANGED is enabled.");
  87. #else
  88. #define PM_SERVICE_CHANGED_ENABLED 1
  89. #endif
  90. /**@brief Function for resetting the module variable(s) of the GSCM module.
  91. *
  92. * @param[out] The instance to reset.
  93. */
  94. static void internal_state_reset()
  95. {
  96. m_module_initialized = false;
  97. }
  98. static void evt_send(pm_evt_t * p_gcm_evt)
  99. {
  100. p_gcm_evt->peer_id = im_peer_id_get_by_conn_handle(p_gcm_evt->conn_handle);
  101. for (uint32_t i = 0; i < GCM_EVENT_HANDLERS_CNT; i++)
  102. {
  103. m_evt_handlers[i](p_gcm_evt);
  104. }
  105. }
  106. /**@brief Function for checking a write event for whether a CCCD was written during the write
  107. * operation.
  108. *
  109. * @param[in] p_write_evt The parameters of the write event.
  110. *
  111. * @return Whether the write was on a CCCD.
  112. */
  113. static bool cccd_written(ble_gatts_evt_write_t const * p_write_evt)
  114. {
  115. return ( (p_write_evt->op == BLE_GATTS_OP_WRITE_REQ)
  116. && (p_write_evt->uuid.type == BLE_UUID_TYPE_BLE)
  117. && (p_write_evt->uuid.uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG)
  118. );
  119. }
  120. /**@brief Function for sending an PM_EVT_ERROR_UNEXPECTED event.
  121. *
  122. * @param[in] conn_handle The connection handle the event pertains to.
  123. * @param[in] err_code The unexpected error that occurred.
  124. */
  125. static void send_unexpected_error(uint16_t conn_handle, ret_code_t err_code)
  126. {
  127. pm_evt_t error_evt =
  128. {
  129. .evt_id = PM_EVT_ERROR_UNEXPECTED,
  130. .conn_handle = conn_handle,
  131. .params =
  132. {
  133. .error_unexpected =
  134. {
  135. .error = err_code,
  136. .fds_error = false
  137. }
  138. }
  139. };
  140. evt_send(&error_evt);
  141. }
  142. /**@brief Function for performing the local DB update procedure in an event context, where no return
  143. * code can be given.
  144. *
  145. * @details This function will do the procedure, and check the result, set a flag if needed, and
  146. * send an event if needed.
  147. *
  148. * @param[in] conn_handle The connection to perform the procedure on.
  149. */
  150. static void local_db_apply_in_evt(uint16_t conn_handle)
  151. {
  152. bool set_procedure_as_pending = false;
  153. ret_code_t err_code;
  154. pm_evt_t event =
  155. {
  156. .conn_handle = conn_handle,
  157. };
  158. if (conn_handle == BLE_CONN_HANDLE_INVALID)
  159. {
  160. return;
  161. }
  162. err_code = gscm_local_db_cache_apply(conn_handle);
  163. switch (err_code)
  164. {
  165. case NRF_SUCCESS:
  166. event.evt_id = PM_EVT_LOCAL_DB_CACHE_APPLIED;
  167. evt_send(&event);
  168. break;
  169. case NRF_ERROR_BUSY:
  170. set_procedure_as_pending = true;
  171. break;
  172. case NRF_ERROR_INVALID_DATA:
  173. event.evt_id = PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED;
  174. NRF_LOG_WARNING("The local database has changed, so some subscriptions to notifications "\
  175. "and indications could not be restored for conn_handle %d",
  176. conn_handle);
  177. evt_send(&event);
  178. break;
  179. case BLE_ERROR_INVALID_CONN_HANDLE:
  180. /* Do nothing */
  181. break;
  182. default:
  183. NRF_LOG_ERROR("gscm_local_db_cache_apply() returned %s which should not happen. "\
  184. "conn_handle: %d",
  185. nrf_strerror_get(err_code),
  186. conn_handle);
  187. send_unexpected_error(conn_handle, err_code);
  188. break;
  189. }
  190. ble_conn_state_user_flag_set(conn_handle, m_flag_local_db_apply_pending, set_procedure_as_pending);
  191. }
  192. /**@brief Function for asynchronously starting a DB update procedure.
  193. *
  194. * @note This procedure can only be started asynchronously.
  195. *
  196. * @param[in] conn_handle The connection to perform the procedure on.
  197. * @param[in] update Whether to perform the procedure.
  198. */
  199. static __INLINE void local_db_update(uint16_t conn_handle, bool update)
  200. {
  201. ble_conn_state_user_flag_set(conn_handle, m_flag_local_db_update_pending, update);
  202. }
  203. /**@brief Function for performing the local DB update procedure in an event context, where no return
  204. * code can be given.
  205. *
  206. * @details This function will do the procedure, and check the result, set a flag if needed, and
  207. * send an event if needed.
  208. *
  209. * @param[in] conn_handle The connection to perform the procedure on.
  210. */
  211. static bool local_db_update_in_evt(uint16_t conn_handle)
  212. {
  213. bool set_procedure_as_pending = false;
  214. bool success = false;
  215. ret_code_t err_code = gscm_local_db_cache_update(conn_handle);
  216. switch (err_code)
  217. {
  218. case NRF_SUCCESS:
  219. success = true;
  220. break;
  221. case BLE_ERROR_INVALID_CONN_HANDLE:
  222. /* Do nothing */
  223. break;
  224. case NRF_ERROR_BUSY:
  225. set_procedure_as_pending = true;
  226. break;
  227. case NRF_ERROR_STORAGE_FULL:
  228. {
  229. pm_evt_t event =
  230. {
  231. .evt_id = PM_EVT_STORAGE_FULL,
  232. .conn_handle = conn_handle,
  233. };
  234. NRF_LOG_WARNING("Flash full. Could not store data for conn_handle: %d", conn_handle);
  235. evt_send(&event);
  236. break;
  237. }
  238. default:
  239. NRF_LOG_ERROR("gscm_local_db_cache_update() returned %s for conn_handle: %d",
  240. nrf_strerror_get(err_code),
  241. conn_handle);
  242. send_unexpected_error(conn_handle, err_code);
  243. break;
  244. }
  245. local_db_update(conn_handle, set_procedure_as_pending);
  246. return success;
  247. }
  248. #if PM_SERVICE_CHANGED_ENABLED
  249. /**@brief Function for getting the value of the CCCD for the service changed characteristic.
  250. *
  251. * @details This function will search all system handles consecutively.
  252. *
  253. * @param[in] conn_handle The connection to check.
  254. * @param[out] p_cccd The CCCD value of the service changed characteristic for this link.
  255. *
  256. * @return Any error from @ref sd_ble_gatts_value_get.
  257. */
  258. static ret_code_t service_changed_cccd(uint16_t conn_handle, uint16_t * p_cccd)
  259. {
  260. bool sc_found = false;
  261. uint16_t end_handle;
  262. ret_code_t err_code = sd_ble_gatts_initial_user_handle_get(&end_handle);
  263. ASSERT(err_code == NRF_SUCCESS);
  264. for (uint16_t handle = 1; handle < end_handle; handle++)
  265. {
  266. uint16_t uuid;
  267. ble_gatts_value_t value = {.p_value = (uint8_t *)&uuid, .len = 2, .offset = 0};
  268. err_code = sd_ble_gatts_value_get(conn_handle, handle, &value);
  269. if (err_code != NRF_SUCCESS)
  270. {
  271. return err_code;
  272. }
  273. else if (!sc_found && (uuid == BLE_UUID_GATT_CHARACTERISTIC_SERVICE_CHANGED))
  274. {
  275. sc_found = true;
  276. }
  277. else if (sc_found && (uuid == BLE_UUID_DESCRIPTOR_CLIENT_CHAR_CONFIG))
  278. {
  279. value.p_value = (uint8_t *)p_cccd;
  280. return sd_ble_gatts_value_get(conn_handle, ++handle, &value);
  281. }
  282. }
  283. return NRF_ERROR_NOT_FOUND;
  284. }
  285. /**@brief Function for sending a service changed indication in an event context, where no return
  286. * code can be given.
  287. *
  288. * @details This function will do the procedure, and check the result, set a flag if needed, and
  289. * send an event if needed.
  290. *
  291. * @param[in] conn_handle The connection to perform the procedure on.
  292. */
  293. static void service_changed_send_in_evt(uint16_t conn_handle)
  294. {
  295. bool sc_pending_state = true;
  296. bool sc_sent_state = false;
  297. ret_code_t err_code = gscm_service_changed_ind_send(conn_handle);
  298. switch (err_code)
  299. {
  300. case NRF_SUCCESS:
  301. {
  302. pm_evt_t event =
  303. {
  304. .evt_id = PM_EVT_SERVICE_CHANGED_IND_SENT,
  305. .conn_handle = conn_handle,
  306. };
  307. sc_sent_state = true;
  308. evt_send(&event);
  309. break;
  310. }
  311. case NRF_ERROR_BUSY:
  312. // Do nothing.
  313. break;
  314. case NRF_ERROR_INVALID_STATE:
  315. {
  316. uint16_t cccd;
  317. err_code = service_changed_cccd(conn_handle, &cccd);
  318. if ((err_code == NRF_SUCCESS) && cccd)
  319. {
  320. // Possible ATT_MTU exchange ongoing.
  321. // Do nothing, treat as busy.
  322. break;
  323. }
  324. else
  325. {
  326. if (err_code != NRF_SUCCESS)
  327. {
  328. NRF_LOG_DEBUG("Unexpected error when looking for service changed CCCD: %s",
  329. nrf_strerror_get(err_code));
  330. }
  331. // CCCDs not enabled or an error happened. Drop indication.
  332. // Fallthrough.
  333. }
  334. }
  335. // Sometimes fallthrough.
  336. case NRF_ERROR_NOT_SUPPORTED:
  337. // Service changed not supported. Drop indication.
  338. sc_pending_state = false;
  339. gscm_db_change_notification_done(im_peer_id_get_by_conn_handle(conn_handle));
  340. break;
  341. case BLE_ERROR_GATTS_SYS_ATTR_MISSING:
  342. local_db_apply_in_evt(conn_handle);
  343. break;
  344. case BLE_ERROR_INVALID_CONN_HANDLE:
  345. // Do nothing.
  346. break;
  347. default:
  348. NRF_LOG_ERROR("gscm_service_changed_ind_send() returned %s for conn_handle: %d",
  349. nrf_strerror_get(err_code),
  350. conn_handle);
  351. send_unexpected_error(conn_handle, err_code);
  352. break;
  353. }
  354. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, sc_pending_state);
  355. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_sent, sc_sent_state);
  356. }
  357. #endif
  358. static void apply_pending_handle(uint16_t conn_handle, void * p_context)
  359. {
  360. UNUSED_PARAMETER(p_context);
  361. local_db_apply_in_evt(conn_handle);
  362. }
  363. static __INLINE void apply_pending_flags_check(void)
  364. {
  365. UNUSED_RETURN_VALUE(ble_conn_state_for_each_set_user_flag(m_flag_local_db_apply_pending,
  366. apply_pending_handle,
  367. NULL));
  368. }
  369. static void db_update_pending_handle(uint16_t conn_handle, void * p_context)
  370. {
  371. UNUSED_PARAMETER(p_context);
  372. if (nrf_mtx_trylock(&m_db_update_in_progress_mutex))
  373. {
  374. if (local_db_update_in_evt(conn_handle))
  375. {
  376. // Successfully started writing to flash.
  377. return;
  378. }
  379. else
  380. {
  381. nrf_mtx_unlock(&m_db_update_in_progress_mutex);
  382. }
  383. }
  384. }
  385. #if PM_SERVICE_CHANGED_ENABLED
  386. static void sc_send_pending_handle(uint16_t conn_handle, void * p_context)
  387. {
  388. UNUSED_PARAMETER(p_context);
  389. if (!ble_conn_state_user_flag_get(conn_handle, m_flag_service_changed_sent))
  390. {
  391. service_changed_send_in_evt(conn_handle);
  392. }
  393. }
  394. static __INLINE void service_changed_pending_flags_check(void)
  395. {
  396. UNUSED_RETURN_VALUE(ble_conn_state_for_each_set_user_flag(m_flag_service_changed_pending,
  397. sc_send_pending_handle,
  398. NULL));
  399. }
  400. static void service_changed_needed(uint16_t conn_handle)
  401. {
  402. if (gscm_service_changed_ind_needed(conn_handle))
  403. {
  404. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, true);
  405. }
  406. }
  407. #endif
  408. static void car_update_pending_handle(uint16_t conn_handle, void * p_context)
  409. {
  410. UNUSED_PARAMETER(p_context);
  411. ble_uuid_t car_uuid;
  412. memset(&car_uuid, 0, sizeof(ble_uuid_t));
  413. car_uuid.uuid = BLE_UUID_GAP_CHARACTERISTIC_CAR;
  414. car_uuid.type = BLE_UUID_TYPE_BLE;
  415. ble_gattc_handle_range_t const car_handle_range = {1, 0xFFFF};
  416. ret_code_t err_code = sd_ble_gattc_char_value_by_uuid_read(conn_handle, &car_uuid, &car_handle_range);
  417. if (err_code == NRF_SUCCESS)
  418. {
  419. ble_conn_state_user_flag_set(conn_handle, m_flag_car_handle_queried, true);
  420. }
  421. }
  422. static void car_update_needed(uint16_t conn_handle)
  423. {
  424. pm_peer_data_t peer_data;
  425. if (pds_peer_data_read(im_peer_id_get_by_conn_handle(conn_handle),
  426. PM_PEER_DATA_ID_CENTRAL_ADDR_RES,
  427. &peer_data,
  428. NULL) == NRF_ERROR_NOT_FOUND)
  429. {
  430. ble_conn_state_user_flag_set(conn_handle, m_flag_car_update_pending, true);
  431. }
  432. }
  433. static __INLINE void update_pending_flags_check(void)
  434. {
  435. uint32_t count = ble_conn_state_for_each_set_user_flag(m_flag_local_db_update_pending,
  436. db_update_pending_handle,
  437. NULL);
  438. if (count == 0)
  439. {
  440. count = ble_conn_state_for_each_set_user_flag(m_flag_car_update_pending,
  441. car_update_pending_handle,
  442. NULL);
  443. UNUSED_RETURN_VALUE(count);
  444. }
  445. }
  446. /**@brief Callback function for events from the ID Manager module.
  447. * This function is registered in the ID Manager module.
  448. *
  449. * @param[in] p_event The event from the ID Manager module.
  450. */
  451. void gcm_im_evt_handler(pm_evt_t * p_event)
  452. {
  453. switch (p_event->evt_id)
  454. {
  455. case PM_EVT_BONDED_PEER_CONNECTED:
  456. local_db_apply_in_evt(p_event->conn_handle);
  457. #if (PM_SERVICE_CHANGED_ENABLED == 1)
  458. service_changed_needed(p_event->conn_handle);
  459. #endif
  460. car_update_needed(p_event->conn_handle);
  461. update_pending_flags_check();
  462. break;
  463. default:
  464. break;
  465. }
  466. }
  467. /**@brief Callback function for events from the Peer Database module.
  468. * This handler is extern in Peer Database.
  469. *
  470. * @param[in] p_event The event from the Security Dispatcher module.
  471. */
  472. void gcm_pdb_evt_handler(pm_evt_t * p_event)
  473. {
  474. if ( p_event->evt_id == PM_EVT_PEER_DATA_UPDATE_SUCCEEDED
  475. && p_event->params.peer_data_update_succeeded.action == PM_PEER_DATA_OP_UPDATE)
  476. {
  477. switch (p_event->params.peer_data_update_succeeded.data_id)
  478. {
  479. case PM_PEER_DATA_ID_BONDING:
  480. {
  481. uint16_t conn_handle = im_conn_handle_get(p_event->peer_id);
  482. if (conn_handle != BLE_CONN_HANDLE_INVALID)
  483. {
  484. local_db_update(conn_handle, true);
  485. car_update_needed(conn_handle);
  486. }
  487. break;
  488. }
  489. #if PM_SERVICE_CHANGED_ENABLED
  490. case PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING:
  491. {
  492. ret_code_t err_code;
  493. pm_peer_data_flash_t peer_data;
  494. err_code = pdb_peer_data_ptr_get(p_event->peer_id,
  495. PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING,
  496. &peer_data);
  497. if (err_code == NRF_SUCCESS)
  498. {
  499. if (*peer_data.p_service_changed_pending)
  500. {
  501. uint16_t conn_handle = im_conn_handle_get(p_event->peer_id);
  502. if (conn_handle != BLE_CONN_HANDLE_INVALID)
  503. {
  504. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, true);
  505. service_changed_pending_flags_check();
  506. }
  507. }
  508. }
  509. break;
  510. }
  511. #endif
  512. case PM_PEER_DATA_ID_GATT_LOCAL:
  513. if (m_db_update_in_progress_mutex == NRF_MTX_LOCKED)
  514. {
  515. nrf_mtx_unlock(&m_db_update_in_progress_mutex);
  516. }
  517. // Expecting a call to update_pending_flags_check() immediately.
  518. break;
  519. default:
  520. /* No action */
  521. break;
  522. }
  523. }
  524. update_pending_flags_check();
  525. }
  526. ret_code_t gcm_init()
  527. {
  528. NRF_PM_DEBUG_CHECK(!m_module_initialized);
  529. internal_state_reset();
  530. m_flag_local_db_update_pending = ble_conn_state_user_flag_acquire();
  531. m_flag_local_db_apply_pending = ble_conn_state_user_flag_acquire();
  532. m_flag_service_changed_pending = ble_conn_state_user_flag_acquire();
  533. m_flag_service_changed_sent = ble_conn_state_user_flag_acquire();
  534. m_flag_car_update_pending = ble_conn_state_user_flag_acquire();
  535. m_flag_car_handle_queried = ble_conn_state_user_flag_acquire();
  536. m_flag_car_value_queried = ble_conn_state_user_flag_acquire();
  537. if ((m_flag_local_db_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  538. || (m_flag_local_db_apply_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  539. || (m_flag_service_changed_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  540. || (m_flag_service_changed_sent == BLE_CONN_STATE_USER_FLAG_INVALID)
  541. || (m_flag_car_update_pending == BLE_CONN_STATE_USER_FLAG_INVALID)
  542. || (m_flag_car_handle_queried == BLE_CONN_STATE_USER_FLAG_INVALID)
  543. || (m_flag_car_value_queried == BLE_CONN_STATE_USER_FLAG_INVALID)
  544. )
  545. {
  546. NRF_LOG_ERROR("Could not acquire conn_state user flags. Increase "\
  547. "BLE_CONN_STATE_USER_FLAG_COUNT in the ble_conn_state module.");
  548. return NRF_ERROR_INTERNAL;
  549. }
  550. nrf_mtx_init(&m_db_update_in_progress_mutex);
  551. m_module_initialized = true;
  552. return NRF_SUCCESS;
  553. }
  554. void store_car_value(uint16_t conn_handle, bool car_value)
  555. {
  556. // Use a uint32_t to enforce 4-byte alignment.
  557. static const uint32_t car_value_true = true;
  558. static const uint32_t car_value_false = false;
  559. pm_peer_data_const_t peer_data =
  560. {
  561. .data_id = PM_PEER_DATA_ID_CENTRAL_ADDR_RES,
  562. .length_words = 1,
  563. };
  564. ble_conn_state_user_flag_set(conn_handle, m_flag_car_update_pending, false);
  565. peer_data.p_central_addr_res = car_value ? &car_value_true : &car_value_false;
  566. ret_code_t err_code = pds_peer_data_store(im_peer_id_get_by_conn_handle(conn_handle), &peer_data, NULL);
  567. if (err_code != NRF_SUCCESS)
  568. {
  569. NRF_LOG_WARNING("CAR char value couldn't be stored (error: %s). Reattempt will happen on the next connection.", nrf_strerror_get(err_code));
  570. }
  571. }
  572. /**@brief Callback function for BLE events from the SoftDevice.
  573. *
  574. * @param[in] p_ble_evt The BLE event from the SoftDevice.
  575. */
  576. void gcm_ble_evt_handler(ble_evt_t const * p_ble_evt)
  577. {
  578. uint16_t conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
  579. switch (p_ble_evt->header.evt_id)
  580. {
  581. case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  582. local_db_apply_in_evt(conn_handle);
  583. break;
  584. #if PM_SERVICE_CHANGED_ENABLED
  585. case BLE_GATTS_EVT_SC_CONFIRM:
  586. {
  587. pm_evt_t event =
  588. {
  589. .evt_id = PM_EVT_SERVICE_CHANGED_IND_CONFIRMED,
  590. .peer_id = im_peer_id_get_by_conn_handle(conn_handle),
  591. .conn_handle = conn_handle,
  592. };
  593. gscm_db_change_notification_done(event.peer_id);
  594. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_sent, false);
  595. ble_conn_state_user_flag_set(conn_handle, m_flag_service_changed_pending, false);
  596. evt_send(&event);
  597. break;
  598. }
  599. #endif
  600. case BLE_GATTS_EVT_WRITE:
  601. if (cccd_written(&p_ble_evt->evt.gatts_evt.params.write))
  602. {
  603. local_db_update(conn_handle, true);
  604. update_pending_flags_check();
  605. }
  606. break;
  607. case BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP:
  608. {
  609. bool handle_found = false;
  610. conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
  611. const ble_gattc_evt_char_val_by_uuid_read_rsp_t * p_val = &p_ble_evt->evt.gattc_evt.params.char_val_by_uuid_read_rsp;
  612. if (!ble_conn_state_user_flag_get(conn_handle, m_flag_car_handle_queried))
  613. {
  614. break;
  615. }
  616. ble_conn_state_user_flag_set(conn_handle, m_flag_car_handle_queried, false);
  617. if (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_ATTRIBUTE_NOT_FOUND)
  618. {
  619. // Store 0.
  620. }
  621. else if (p_ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS)
  622. {
  623. NRF_LOG_WARNING("Unexpected GATT status while getting CAR char value: 0x%x",
  624. p_ble_evt->evt.gattc_evt.gatt_status);
  625. // Store 0.
  626. }
  627. else
  628. {
  629. if (p_val->count != 1)
  630. {
  631. NRF_LOG_WARNING("Multiple (%d) CAR characteristics found, using the first.",
  632. p_val->count);
  633. }
  634. if (p_val->value_len != 1)
  635. {
  636. NRF_LOG_WARNING("Unexpected CAR characteristic value length (%d), store 0.",
  637. p_val->value_len);
  638. // Store 0.
  639. }
  640. else
  641. {
  642. ret_code_t err_code = sd_ble_gattc_read(conn_handle, *(uint16_t*)p_val->handle_value, 0);
  643. if (err_code == NRF_SUCCESS)
  644. {
  645. handle_found = true;
  646. ble_conn_state_user_flag_set(conn_handle, m_flag_car_value_queried, true);
  647. }
  648. }
  649. }
  650. if (!handle_found)
  651. {
  652. store_car_value(conn_handle, false);
  653. }
  654. break;
  655. }
  656. case BLE_GATTC_EVT_READ_RSP:
  657. {
  658. bool car_value = false;
  659. conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
  660. const ble_gattc_evt_read_rsp_t * p_val = &p_ble_evt->evt.gattc_evt.params.read_rsp;
  661. if (!ble_conn_state_user_flag_get(conn_handle, m_flag_car_value_queried))
  662. {
  663. break;
  664. }
  665. ble_conn_state_user_flag_set(conn_handle, m_flag_car_value_queried, false);
  666. if (p_ble_evt->evt.gattc_evt.gatt_status != BLE_GATT_STATUS_SUCCESS)
  667. {
  668. NRF_LOG_WARNING("Unexpected GATT status while getting CAR char value: 0x%x",
  669. p_ble_evt->evt.gattc_evt.gatt_status);
  670. // Store 0.
  671. }
  672. else
  673. {
  674. if (p_val->len != 1)
  675. {
  676. NRF_LOG_WARNING("Unexpected CAR characteristic value length (%d), store 0.",
  677. p_val->len);
  678. // Store 0.
  679. }
  680. else
  681. {
  682. car_value = *p_val->data;
  683. }
  684. }
  685. store_car_value(conn_handle, car_value);
  686. }
  687. }
  688. apply_pending_flags_check();
  689. #if PM_SERVICE_CHANGED_ENABLED
  690. service_changed_pending_flags_check();
  691. #endif
  692. }
  693. ret_code_t gcm_local_db_cache_update(uint16_t conn_handle)
  694. {
  695. NRF_PM_DEBUG_CHECK(m_module_initialized);
  696. local_db_update(conn_handle, true);
  697. update_pending_flags_check();
  698. return NRF_SUCCESS;
  699. }
  700. #if PM_SERVICE_CHANGED_ENABLED
  701. void gcm_local_database_has_changed(void)
  702. {
  703. gscm_local_database_has_changed();
  704. ble_conn_state_conn_handle_list_t conn_handles = ble_conn_state_conn_handles();
  705. for (uint16_t i = 0; i < conn_handles.len; i++)
  706. {
  707. if (im_peer_id_get_by_conn_handle(conn_handles.conn_handles[i]) == PM_PEER_ID_INVALID)
  708. {
  709. ble_conn_state_user_flag_set(conn_handles.conn_handles[i], m_flag_service_changed_pending, true);
  710. }
  711. }
  712. service_changed_pending_flags_check();
  713. }
  714. #endif
  715. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)