gatt_cache_manager.c 28 KB

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