gatts_cache_manager.c 12 KB

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