gatts_cache_manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 "gatts_cache_manager.h"
  43. #include <string.h>
  44. #include "ble_gap.h"
  45. #include "ble_err.h"
  46. #include "peer_manager_types.h"
  47. #include "peer_manager_internal.h"
  48. #include "peer_database.h"
  49. #include "peer_data_storage.h"
  50. #include "id_manager.h"
  51. #define NRF_LOG_MODULE_NAME peer_manager_gscm
  52. #if PM_LOG_ENABLED
  53. #define NRF_LOG_LEVEL PM_LOG_LEVEL
  54. #define NRF_LOG_INFO_COLOR PM_LOG_INFO_COLOR
  55. #define NRF_LOG_DEBUG_COLOR PM_LOG_DEBUG_COLOR
  56. #else
  57. #define NRF_LOG_LEVEL 0
  58. #endif // PM_LOG_ENABLED
  59. #include "nrf_log.h"
  60. #include "nrf_log_ctrl.h"
  61. NRF_LOG_MODULE_REGISTER();
  62. #include "nrf_strerror.h"
  63. #if !defined(PM_SERVICE_CHANGED_ENABLED) || (PM_SERVICE_CHANGED_ENABLED == 1)
  64. // The number of registered event handlers.
  65. #define GSCM_EVENT_HANDLERS_CNT (sizeof(m_evt_handlers) / sizeof(m_evt_handlers[0]))
  66. // GATTS Cache Manager event handler in Peer Manager.
  67. extern void pm_gscm_evt_handler(pm_evt_t * p_gcm_evt);
  68. // GATTS Cache Manager events' handlers.
  69. // The number of elements in this array is GSCM_EVENT_HANDLERS_CNT.
  70. static pm_evt_handler_internal_t m_evt_handlers[] =
  71. {
  72. pm_gscm_evt_handler
  73. };
  74. #endif
  75. // Syntactic sugar, two spoons.
  76. #define SYS_ATTR_SYS (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS)
  77. #define SYS_ATTR_USR (BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
  78. #define SYS_ATTR_BOTH (SYS_ATTR_SYS | SYS_ATTR_USR)
  79. static bool m_module_initialized;
  80. static pm_peer_id_t m_current_sc_store_peer_id;
  81. /**@brief Function for resetting the module variable(s) of the GSCM module.
  82. */
  83. static void internal_state_reset()
  84. {
  85. m_module_initialized = false;
  86. m_current_sc_store_peer_id = PM_PEER_ID_INVALID;
  87. // If PM_SERVICE_CHANGED_ENABLED is 0, this variable is unused.
  88. UNUSED_VARIABLE(m_current_sc_store_peer_id);
  89. }
  90. #if !defined(PM_SERVICE_CHANGED_ENABLED) || (PM_SERVICE_CHANGED_ENABLED == 1)
  91. static void evt_send(pm_evt_t * p_gscm_evt)
  92. {
  93. p_gscm_evt->conn_handle = im_conn_handle_get(p_gscm_evt->peer_id);
  94. for (uint32_t i = 0; i < GSCM_EVENT_HANDLERS_CNT; i++)
  95. {
  96. m_evt_handlers[i](p_gscm_evt);
  97. }
  98. }
  99. //lint -save -e550
  100. /**@brief Function for storing service_changed_pending = true to flash for all peers, in sequence.
  101. *
  102. * This function aborts if it gets @ref NRF_ERROR_BUSY when trying to store. A subsequent call will
  103. * continue where the last call was aborted.
  104. */
  105. static void service_changed_pending_set(void)
  106. {
  107. NRF_PM_DEBUG_CHECK(m_module_initialized);
  108. ret_code_t err_code;
  109. // Use a uint32_t to enforce 4-byte alignment.
  110. static const uint32_t service_changed_pending = true;
  111. //lint -save -e65 -e64
  112. pm_peer_data_const_t peer_data =
  113. {
  114. .data_id = PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING,
  115. .length_words = PM_SC_STATE_N_WORDS(),
  116. .p_service_changed_pending = (bool*)&service_changed_pending,
  117. };
  118. //lint -restore
  119. while (m_current_sc_store_peer_id != PM_PEER_ID_INVALID)
  120. {
  121. err_code = pds_peer_data_store(m_current_sc_store_peer_id, &peer_data, NULL);
  122. if (err_code != NRF_SUCCESS)
  123. {
  124. pm_evt_t evt = {.peer_id = m_current_sc_store_peer_id};
  125. if (err_code == NRF_ERROR_BUSY)
  126. {
  127. // Do nothing.
  128. }
  129. else if (err_code == NRF_ERROR_STORAGE_FULL)
  130. {
  131. evt.evt_id = PM_EVT_STORAGE_FULL;
  132. evt_send(&evt);
  133. }
  134. else
  135. {
  136. NRF_LOG_ERROR("pds_peer_data_store() returned %s while storing service changed"\
  137. "state for peer id %d.",
  138. nrf_strerror_get(err_code),
  139. m_current_sc_store_peer_id);
  140. evt.evt_id = PM_EVT_ERROR_UNEXPECTED;
  141. evt.params.error_unexpected.error = err_code;
  142. evt_send(&evt);
  143. }
  144. break;
  145. }
  146. m_current_sc_store_peer_id = pds_next_peer_id_get(m_current_sc_store_peer_id);
  147. }
  148. }
  149. //lint -restore
  150. /**@brief Event handler for events from the Peer Database module.
  151. * This function is extern in Peer Database.
  152. *
  153. * @param[in] p_event The event that has happened with peer id and flags.
  154. */
  155. void gscm_pdb_evt_handler(pm_evt_t * p_event)
  156. {
  157. if (m_current_sc_store_peer_id != PM_PEER_ID_INVALID)
  158. {
  159. service_changed_pending_set();
  160. }
  161. }
  162. #endif
  163. ret_code_t gscm_init()
  164. {
  165. NRF_PM_DEBUG_CHECK(!m_module_initialized);
  166. internal_state_reset();
  167. m_module_initialized = true;
  168. return NRF_SUCCESS;
  169. }
  170. ret_code_t gscm_local_db_cache_update(uint16_t conn_handle)
  171. {
  172. NRF_PM_DEBUG_CHECK(m_module_initialized);
  173. pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle);
  174. ret_code_t err_code;
  175. if (peer_id == PM_PEER_ID_INVALID)
  176. {
  177. return BLE_ERROR_INVALID_CONN_HANDLE;
  178. }
  179. else
  180. {
  181. pm_peer_data_t peer_data;
  182. uint16_t n_bufs = 1;
  183. bool retry_with_bigger_buffer = false;
  184. do
  185. {
  186. retry_with_bigger_buffer = false;
  187. err_code = pdb_write_buf_get(peer_id, PM_PEER_DATA_ID_GATT_LOCAL, n_bufs++, &peer_data);
  188. if (err_code == NRF_SUCCESS)
  189. {
  190. pm_peer_data_local_gatt_db_t * p_local_gatt_db = peer_data.p_local_gatt_db;
  191. p_local_gatt_db->flags = SYS_ATTR_BOTH;
  192. err_code = sd_ble_gatts_sys_attr_get(conn_handle, &p_local_gatt_db->data[0], &p_local_gatt_db->len, p_local_gatt_db->flags);
  193. if (err_code == NRF_SUCCESS)
  194. {
  195. err_code = pdb_write_buf_store(peer_id, PM_PEER_DATA_ID_GATT_LOCAL, peer_id);
  196. }
  197. else
  198. {
  199. if (err_code == NRF_ERROR_DATA_SIZE)
  200. {
  201. // The sys attributes are bigger than the requested write buffer.
  202. retry_with_bigger_buffer = true;
  203. }
  204. else if (err_code == NRF_ERROR_NOT_FOUND)
  205. {
  206. // There are no sys attributes in the GATT db, so nothing needs to be stored.
  207. err_code = NRF_SUCCESS;
  208. }
  209. ret_code_t err_code_release = pdb_write_buf_release(peer_id, PM_PEER_DATA_ID_GATT_LOCAL);
  210. if (err_code_release != NRF_SUCCESS)
  211. {
  212. NRF_LOG_ERROR("Did another thread manipulate PM_PEER_DATA_ID_GATT_LOCAL for "\
  213. "peer_id %d at the same time? pdb_write_buf_release() returned %s.",
  214. peer_id,
  215. nrf_strerror_get(err_code_release));
  216. err_code = NRF_ERROR_INTERNAL;
  217. }
  218. }
  219. }
  220. else if (err_code == NRF_ERROR_INVALID_PARAM)
  221. {
  222. // The sys attributes are bigger than the entire write buffer.
  223. err_code = NRF_ERROR_DATA_SIZE;
  224. }
  225. } while (retry_with_bigger_buffer);
  226. }
  227. return err_code;
  228. }
  229. ret_code_t gscm_local_db_cache_apply(uint16_t conn_handle)
  230. {
  231. NRF_PM_DEBUG_CHECK(m_module_initialized);
  232. pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle);
  233. ret_code_t err_code;
  234. pm_peer_data_flash_t peer_data;
  235. uint8_t const * p_sys_attr_data = NULL;
  236. uint16_t sys_attr_len = 0;
  237. uint32_t sys_attr_flags = (SYS_ATTR_BOTH);
  238. bool all_attributes_applied = true;
  239. if (peer_id != PM_PEER_ID_INVALID)
  240. {
  241. err_code = pdb_peer_data_ptr_get(peer_id, PM_PEER_DATA_ID_GATT_LOCAL, &peer_data);
  242. if (err_code == NRF_SUCCESS)
  243. {
  244. pm_peer_data_local_gatt_db_t const * p_local_gatt_db;
  245. p_local_gatt_db = peer_data.p_local_gatt_db;
  246. p_sys_attr_data = p_local_gatt_db->data;
  247. sys_attr_len = p_local_gatt_db->len;
  248. sys_attr_flags = p_local_gatt_db->flags;
  249. }
  250. }
  251. do
  252. {
  253. err_code = sd_ble_gatts_sys_attr_set(conn_handle, p_sys_attr_data, sys_attr_len, sys_attr_flags);
  254. if (err_code == NRF_ERROR_NO_MEM)
  255. {
  256. err_code = NRF_ERROR_BUSY;
  257. }
  258. else if (err_code == NRF_ERROR_INVALID_STATE)
  259. {
  260. err_code = NRF_SUCCESS;
  261. }
  262. else if (err_code == NRF_ERROR_INVALID_DATA)
  263. {
  264. all_attributes_applied = false;
  265. if (sys_attr_flags & SYS_ATTR_USR)
  266. {
  267. // Try setting only system attributes.
  268. sys_attr_flags = SYS_ATTR_SYS;
  269. }
  270. else if (p_sys_attr_data || sys_attr_len)
  271. {
  272. // Try reporting that none exist.
  273. p_sys_attr_data = NULL;
  274. sys_attr_len = 0;
  275. sys_attr_flags = SYS_ATTR_BOTH;
  276. }
  277. else
  278. {
  279. NRF_LOG_ERROR("sd_ble_gatts_sys_attr_set() returned NRF_ERROR_INVALID_DATA for NULL "\
  280. "pointer which should never happen. conn_handle: %d",
  281. conn_handle);
  282. err_code = NRF_ERROR_INTERNAL;
  283. }
  284. }
  285. } while (err_code == NRF_ERROR_INVALID_DATA);
  286. if (!all_attributes_applied)
  287. {
  288. err_code = NRF_ERROR_INVALID_DATA;
  289. }
  290. return err_code;
  291. }
  292. #if !defined(PM_SERVICE_CHANGED_ENABLED) || (PM_SERVICE_CHANGED_ENABLED == 1)
  293. void gscm_local_database_has_changed(void)
  294. {
  295. NRF_PM_DEBUG_CHECK(m_module_initialized);
  296. m_current_sc_store_peer_id = pds_next_peer_id_get(PM_PEER_ID_INVALID);
  297. service_changed_pending_set();
  298. }
  299. bool gscm_service_changed_ind_needed(uint16_t conn_handle)
  300. {
  301. ret_code_t err_code;
  302. bool service_changed_state;
  303. pm_peer_data_flash_t peer_data;
  304. peer_data.p_service_changed_pending = &service_changed_state;
  305. pm_peer_id_t peer_id = im_peer_id_get_by_conn_handle(conn_handle);
  306. err_code = pdb_peer_data_ptr_get(peer_id, PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING, &peer_data);
  307. if (err_code != NRF_SUCCESS)
  308. {
  309. return false;
  310. }
  311. return *peer_data.p_service_changed_pending;
  312. }
  313. ret_code_t gscm_service_changed_ind_send(uint16_t conn_handle)
  314. {
  315. static uint16_t start_handle;
  316. const uint16_t end_handle = 0xFFFF;
  317. ret_code_t err_code;
  318. err_code = sd_ble_gatts_initial_user_handle_get(&start_handle);
  319. if (err_code != NRF_SUCCESS)
  320. {
  321. NRF_LOG_ERROR("sd_ble_gatts_initial_user_handle_get() returned %s which should not happen.",
  322. nrf_strerror_get(err_code));
  323. return NRF_ERROR_INTERNAL;
  324. }
  325. do
  326. {
  327. err_code = sd_ble_gatts_service_changed(conn_handle, start_handle, end_handle);
  328. if (err_code == BLE_ERROR_INVALID_ATTR_HANDLE)
  329. {
  330. start_handle += 1;
  331. }
  332. } while (err_code == BLE_ERROR_INVALID_ATTR_HANDLE);
  333. return err_code;
  334. }
  335. void gscm_db_change_notification_done(pm_peer_id_t peer_id)
  336. {
  337. NRF_PM_DEBUG_CHECK(m_module_initialized);
  338. // Use a uint32_t to enforce 4-byte alignment.
  339. static const uint32_t service_changed_pending = false;
  340. //lint -save -e65 -e64
  341. pm_peer_data_const_t peer_data =
  342. {
  343. .data_id = PM_PEER_DATA_ID_SERVICE_CHANGED_PENDING,
  344. .length_words = PM_SC_STATE_N_WORDS(),
  345. .p_service_changed_pending = (bool*)&service_changed_pending,
  346. };
  347. //lint -restore
  348. // Don't need to check return code, because all error conditions can be ignored.
  349. //lint -save -e550
  350. (void) pds_peer_data_store(peer_id, &peer_data, NULL);
  351. //lint -restore
  352. }
  353. #endif
  354. #endif // NRF_MODULE_ENABLED(PEER_MANAGER)