peer_manager_handler.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /**
  2. * Copyright (c) 2018 - 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 "peer_manager_handler.h"
  41. #include <stdint.h>
  42. #include "sdk_errors.h"
  43. #include "app_error.h"
  44. #include "peer_manager.h"
  45. #include "ble_gap.h"
  46. #include "ble_gattc.h"
  47. #include "ble_conn_state.h"
  48. #include "fds.h"
  49. #include "nrf_strerror.h"
  50. #include "sdk_config.h"
  51. #if PM_HANDLER_SEC_DELAY_MS > 0
  52. #include "app_timer.h"
  53. #endif
  54. #define NRF_LOG_MODULE_NAME peer_manager_handler
  55. #if PM_LOG_ENABLED
  56. #define NRF_LOG_LEVEL PM_LOG_LEVEL
  57. #define NRF_LOG_INFO_COLOR PM_LOG_INFO_COLOR
  58. #define NRF_LOG_DEBUG_COLOR PM_LOG_DEBUG_COLOR
  59. #else
  60. #define NRF_LOG_LEVEL 0
  61. #endif // PM_LOG_ENABLED
  62. #include "nrf_log.h"
  63. #include "nrf_log_ctrl.h"
  64. NRF_LOG_MODULE_REGISTER();
  65. #include "nrf_strerror.h"
  66. static const char * m_roles_str[] =
  67. {
  68. "Invalid Role",
  69. "Peripheral",
  70. "Central",
  71. };
  72. static const char * m_sec_procedure_str[] =
  73. {
  74. "Encryption",
  75. "Bonding",
  76. "Pairing",
  77. };
  78. #define PM_EVT_STR(_name) [_name] = STRINGIFY(_name)
  79. static const char * m_event_str[] =
  80. {
  81. PM_EVT_STR(PM_EVT_BONDED_PEER_CONNECTED),
  82. PM_EVT_STR(PM_EVT_CONN_SEC_START),
  83. PM_EVT_STR(PM_EVT_CONN_SEC_SUCCEEDED),
  84. PM_EVT_STR(PM_EVT_CONN_SEC_FAILED),
  85. PM_EVT_STR(PM_EVT_CONN_SEC_CONFIG_REQ),
  86. PM_EVT_STR(PM_EVT_CONN_SEC_PARAMS_REQ),
  87. PM_EVT_STR(PM_EVT_STORAGE_FULL),
  88. PM_EVT_STR(PM_EVT_ERROR_UNEXPECTED),
  89. PM_EVT_STR(PM_EVT_PEER_DATA_UPDATE_SUCCEEDED),
  90. PM_EVT_STR(PM_EVT_PEER_DATA_UPDATE_FAILED),
  91. PM_EVT_STR(PM_EVT_PEER_DELETE_SUCCEEDED),
  92. PM_EVT_STR(PM_EVT_PEER_DELETE_FAILED),
  93. PM_EVT_STR(PM_EVT_PEERS_DELETE_SUCCEEDED),
  94. PM_EVT_STR(PM_EVT_PEERS_DELETE_FAILED),
  95. PM_EVT_STR(PM_EVT_LOCAL_DB_CACHE_APPLIED),
  96. PM_EVT_STR(PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED),
  97. PM_EVT_STR(PM_EVT_SERVICE_CHANGED_IND_SENT),
  98. PM_EVT_STR(PM_EVT_SERVICE_CHANGED_IND_CONFIRMED),
  99. PM_EVT_STR(PM_EVT_SLAVE_SECURITY_REQ),
  100. PM_EVT_STR(PM_EVT_FLASH_GARBAGE_COLLECTED),
  101. PM_EVT_STR(PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED),
  102. };
  103. static const char * m_data_id_str[] =
  104. {
  105. "Outdated (0)",
  106. "Service changed pending flag",
  107. "Outdated (2)",
  108. "Outdated (3)",
  109. "Application data",
  110. "Remote database",
  111. "Peer rank",
  112. "Bonding data",
  113. "Local database",
  114. "Central address resolution",
  115. };
  116. static const char * m_data_action_str[] =
  117. {
  118. "Update",
  119. "Delete"
  120. };
  121. static void _conn_secure(uint16_t conn_handle, bool force)
  122. {
  123. ret_code_t err_code;
  124. if (!force)
  125. {
  126. pm_conn_sec_status_t status;
  127. err_code = pm_conn_sec_status_get(conn_handle, &status);
  128. if (err_code != BLE_ERROR_INVALID_CONN_HANDLE)
  129. {
  130. APP_ERROR_CHECK(err_code);
  131. }
  132. // If the link is already secured, don't initiate security procedure.
  133. if (status.encrypted)
  134. {
  135. NRF_LOG_DEBUG("Already encrypted, skipping security.");
  136. return;
  137. }
  138. }
  139. err_code = pm_conn_secure(conn_handle, false);
  140. if ((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_BUSY))
  141. {
  142. // Success.
  143. }
  144. else if (err_code == NRF_ERROR_TIMEOUT)
  145. {
  146. NRF_LOG_WARNING("pm_conn_secure() failed because an SMP timeout is preventing security on "\
  147. "the link. Disconnecting conn_handle %d.",
  148. conn_handle);
  149. err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  150. if (err_code != NRF_SUCCESS)
  151. {
  152. NRF_LOG_WARNING("sd_ble_gap_disconnect() returned %s on conn_handle %d.",
  153. nrf_strerror_get(err_code),
  154. conn_handle);
  155. }
  156. }
  157. else if (err_code == NRF_ERROR_INVALID_DATA)
  158. {
  159. NRF_LOG_WARNING("pm_conn_secure() failed because the stored data for conn_handle %d does "\
  160. "not have a valid key.",
  161. conn_handle);
  162. }
  163. else if (err_code == BLE_ERROR_INVALID_CONN_HANDLE)
  164. {
  165. NRF_LOG_WARNING("pm_conn_secure() failed because conn_handle %d is not a valid connection.",
  166. conn_handle);
  167. }
  168. else
  169. {
  170. NRF_LOG_ERROR("Asserting. pm_conn_secure() returned %s on conn_handle %d.",
  171. nrf_strerror_get(err_code),
  172. conn_handle);
  173. APP_ERROR_CHECK(err_code);
  174. }
  175. }
  176. #if PM_HANDLER_SEC_DELAY_MS > 0
  177. APP_TIMER_DEF(secure_delay_timer);
  178. typedef union
  179. {
  180. struct
  181. {
  182. uint16_t conn_handle;
  183. bool force;
  184. } values;
  185. void * p_void;
  186. } conn_secure_context_t;
  187. STATIC_ASSERT(sizeof(conn_secure_context_t) <= sizeof(void *), "conn_secure_context_t is too large.");
  188. static void _conn_secure(uint16_t conn_handle, bool force);
  189. static void delayed_conn_secure(void * context)
  190. {
  191. conn_secure_context_t sec_context = {.p_void = context};
  192. _conn_secure(sec_context.values.conn_handle, sec_context.values.force);
  193. }
  194. static void conn_secure(uint16_t conn_handle, bool force)
  195. {
  196. ret_code_t err_code;
  197. static bool created = false;
  198. if (!created)
  199. {
  200. err_code = app_timer_create(&secure_delay_timer,
  201. APP_TIMER_MODE_SINGLE_SHOT,
  202. delayed_conn_secure);
  203. APP_ERROR_CHECK(err_code);
  204. created = true;
  205. }
  206. conn_secure_context_t sec_context = {0};
  207. sec_context.values.conn_handle = conn_handle;
  208. sec_context.values.force = force;
  209. err_code = app_timer_start(secure_delay_timer,
  210. APP_TIMER_TICKS(PM_HANDLER_SEC_DELAY_MS),
  211. sec_context.p_void);
  212. APP_ERROR_CHECK(err_code);
  213. }
  214. #else
  215. static void conn_secure(uint16_t conn_handle, bool force)
  216. {
  217. _conn_secure(conn_handle, force);
  218. }
  219. #endif
  220. void pm_handler_on_pm_evt(pm_evt_t const * p_pm_evt)
  221. {
  222. pm_handler_pm_evt_log(p_pm_evt);
  223. if (p_pm_evt->evt_id == PM_EVT_BONDED_PEER_CONNECTED)
  224. {
  225. conn_secure(p_pm_evt->conn_handle, false);
  226. }
  227. else if (p_pm_evt->evt_id == PM_EVT_ERROR_UNEXPECTED)
  228. {
  229. NRF_LOG_ERROR("Asserting.");
  230. APP_ERROR_CHECK(p_pm_evt->params.error_unexpected.error);
  231. }
  232. }
  233. void pm_handler_flash_clean_on_return(void)
  234. {
  235. // Trigger the mechanism to make more room in flash.
  236. pm_evt_t storage_full_evt = {.evt_id = PM_EVT_STORAGE_FULL};
  237. pm_handler_flash_clean(&storage_full_evt);
  238. }
  239. static void rank_highest(pm_peer_id_t peer_id)
  240. {
  241. // Trigger a pm_peer_rank_highest() with internal bookkeeping.
  242. pm_evt_t connected_evt = {.evt_id = PM_EVT_BONDED_PEER_CONNECTED, .peer_id = peer_id};
  243. pm_handler_flash_clean(&connected_evt);
  244. }
  245. void pm_handler_flash_clean(pm_evt_t const * p_pm_evt)
  246. {
  247. ret_code_t err_code;
  248. static bool flash_cleaning = false; // Indicates whether garbage collection is currently being run.
  249. static bool flash_write_after_gc = false; // Indicates whether a successful write happened after the last garbage
  250. // collection. If this is false when flash is full, it means just a
  251. // garbage collection won't work, so some data should be deleted.
  252. #define RANK_QUEUE_SIZE 8 // Size of rank_queue.
  253. #define RANK_QUEUE_INIT() PM_PEER_ID_INVALID, // Initial value of rank_queue.
  254. //lint -save -e40 -e26 -esym(628,MACRO_REPEAT_8)
  255. static pm_peer_id_t rank_queue[8] = {MACRO_REPEAT(RANK_QUEUE_SIZE, RANK_QUEUE_INIT)}; // Queue of rank_highest calls that
  256. // failed because of full flash.
  257. //lint -restore
  258. static int rank_queue_wr = 0; // Write pointer for rank_queue.
  259. switch (p_pm_evt->evt_id)
  260. {
  261. case PM_EVT_BONDED_PEER_CONNECTED:
  262. err_code = pm_peer_rank_highest(p_pm_evt->peer_id);
  263. if ((err_code == NRF_ERROR_STORAGE_FULL) ||
  264. (err_code == NRF_ERROR_BUSY))
  265. {
  266. // Queue pm_peer_rank_highest() call and attempt to clean flash.
  267. rank_queue[rank_queue_wr] = p_pm_evt->peer_id;
  268. rank_queue_wr = (rank_queue_wr + 1) % RANK_QUEUE_SIZE;
  269. pm_handler_flash_clean_on_return();
  270. }
  271. else if ((err_code != NRF_ERROR_NOT_SUPPORTED) &&
  272. (err_code != NRF_ERROR_INVALID_PARAM) &&
  273. (err_code != NRF_ERROR_RESOURCES))
  274. {
  275. APP_ERROR_CHECK(err_code);
  276. }
  277. else
  278. {
  279. NRF_LOG_DEBUG("pm_peer_rank_highest() returned %s for peer id %d",
  280. nrf_strerror_get(err_code),
  281. p_pm_evt->peer_id);
  282. }
  283. break;
  284. case PM_EVT_CONN_SEC_START:
  285. break;
  286. case PM_EVT_CONN_SEC_SUCCEEDED:
  287. if ( (p_pm_evt->params.conn_sec_succeeded.procedure == PM_CONN_SEC_PROCEDURE_BONDING)
  288. || (p_pm_evt->params.conn_sec_succeeded.procedure == PM_CONN_SEC_PROCEDURE_ENCRYPTION))
  289. // PM_CONN_SEC_PROCEDURE_ENCRYPTION in case peer was not recognized at connection time.
  290. {
  291. rank_highest(p_pm_evt->peer_id);
  292. }
  293. break;
  294. case PM_EVT_CONN_SEC_FAILED:
  295. case PM_EVT_CONN_SEC_CONFIG_REQ:
  296. case PM_EVT_CONN_SEC_PARAMS_REQ:
  297. break;
  298. case PM_EVT_STORAGE_FULL:
  299. if (!flash_cleaning)
  300. {
  301. err_code = NRF_SUCCESS;
  302. NRF_LOG_INFO("Attempting to clean flash.");
  303. if (!flash_write_after_gc)
  304. {
  305. pm_peer_id_t peer_id_to_delete;
  306. err_code = pm_peer_ranks_get(NULL, NULL, &peer_id_to_delete, NULL);
  307. if (err_code == NRF_SUCCESS)
  308. {
  309. NRF_LOG_INFO("Deleting lowest ranked peer (peer_id: %d)", peer_id_to_delete);
  310. err_code = pm_peer_delete(peer_id_to_delete);
  311. APP_ERROR_CHECK(err_code);
  312. flash_write_after_gc = true;
  313. }
  314. if (err_code == NRF_ERROR_NOT_FOUND)
  315. {
  316. NRF_LOG_ERROR("There are no peers to delete.");
  317. }
  318. else if (err_code == NRF_ERROR_NOT_SUPPORTED)
  319. {
  320. NRF_LOG_WARNING("Peer ranks functionality is disabled, so no peers are deleted.");
  321. }
  322. else
  323. {
  324. APP_ERROR_CHECK(err_code);
  325. }
  326. }
  327. if (err_code == NRF_SUCCESS)
  328. {
  329. err_code = fds_gc();
  330. if (err_code == NRF_SUCCESS)
  331. {
  332. NRF_LOG_DEBUG("Running flash garbage collection.");
  333. flash_cleaning = true;
  334. }
  335. else if (err_code != FDS_ERR_NO_SPACE_IN_QUEUES)
  336. {
  337. APP_ERROR_CHECK(err_code);
  338. }
  339. }
  340. }
  341. break;
  342. case PM_EVT_ERROR_UNEXPECTED:
  343. break;
  344. case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
  345. flash_write_after_gc = true;
  346. break;
  347. case PM_EVT_PEER_DATA_UPDATE_FAILED:
  348. break;
  349. case PM_EVT_PEER_DELETE_SUCCEEDED:
  350. flash_write_after_gc = true;
  351. break;
  352. case PM_EVT_PEER_DELETE_FAILED:
  353. case PM_EVT_PEERS_DELETE_SUCCEEDED:
  354. case PM_EVT_PEERS_DELETE_FAILED:
  355. case PM_EVT_LOCAL_DB_CACHE_APPLIED:
  356. case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED:
  357. case PM_EVT_SERVICE_CHANGED_IND_SENT:
  358. case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED:
  359. case PM_EVT_SLAVE_SECURITY_REQ:
  360. break;
  361. case PM_EVT_FLASH_GARBAGE_COLLECTED:
  362. flash_cleaning = false;
  363. flash_write_after_gc = false;
  364. {
  365. // Reattempt queued pm_peer_rank_highest() calls.
  366. int rank_queue_rd = rank_queue_wr;
  367. for (int i = 0; i < RANK_QUEUE_SIZE; i++)
  368. {
  369. pm_peer_id_t peer_id = rank_queue[(i + rank_queue_rd) % RANK_QUEUE_SIZE];
  370. if (peer_id != PM_PEER_ID_INVALID)
  371. {
  372. rank_queue[(i + rank_queue_rd) % RANK_QUEUE_SIZE] = PM_PEER_ID_INVALID;
  373. rank_highest(peer_id);
  374. }
  375. }
  376. }
  377. break;
  378. case PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED:
  379. flash_cleaning = false;
  380. if (p_pm_evt->params.garbage_collection_failed.fds_error
  381. && (p_pm_evt->params.garbage_collection_failed.error == FDS_ERR_BUSY
  382. || p_pm_evt->params.garbage_collection_failed.error == FDS_ERR_OPERATION_TIMEOUT))
  383. {
  384. // Retry immediately if error is transient.
  385. pm_handler_flash_clean_on_return();
  386. }
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. void pm_handler_pm_evt_log(pm_evt_t const * p_pm_evt)
  393. {
  394. NRF_LOG_DEBUG("Event %s", m_event_str[p_pm_evt->evt_id]);
  395. switch (p_pm_evt->evt_id)
  396. {
  397. case PM_EVT_BONDED_PEER_CONNECTED:
  398. NRF_LOG_DEBUG("Previously bonded peer connected: role: %s, conn_handle: %d, peer_id: %d",
  399. m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)],
  400. p_pm_evt->conn_handle,
  401. p_pm_evt->peer_id);
  402. break;
  403. case PM_EVT_CONN_SEC_START:
  404. NRF_LOG_DEBUG("Connection security procedure started: role: %s, conn_handle: %d, procedure: %s",
  405. m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)],
  406. p_pm_evt->conn_handle,
  407. m_sec_procedure_str[p_pm_evt->params.conn_sec_start.procedure]);
  408. break;
  409. case PM_EVT_CONN_SEC_SUCCEEDED:
  410. NRF_LOG_INFO("Connection secured: role: %s, conn_handle: %d, procedure: %s",
  411. m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)],
  412. p_pm_evt->conn_handle,
  413. m_sec_procedure_str[p_pm_evt->params.conn_sec_start.procedure]);
  414. break;
  415. case PM_EVT_CONN_SEC_FAILED:
  416. NRF_LOG_INFO("Connection security failed: role: %s, conn_handle: 0x%x, procedure: %s, error: %d",
  417. m_roles_str[ble_conn_state_role(p_pm_evt->conn_handle)],
  418. p_pm_evt->conn_handle,
  419. m_sec_procedure_str[p_pm_evt->params.conn_sec_start.procedure],
  420. p_pm_evt->params.conn_sec_failed.error);
  421. break;
  422. case PM_EVT_CONN_SEC_CONFIG_REQ:
  423. NRF_LOG_DEBUG("Security configuration request");
  424. break;
  425. case PM_EVT_CONN_SEC_PARAMS_REQ:
  426. NRF_LOG_DEBUG("Security parameter request");
  427. break;
  428. case PM_EVT_STORAGE_FULL:
  429. NRF_LOG_WARNING("Flash storage is full");
  430. break;
  431. case PM_EVT_ERROR_UNEXPECTED:
  432. NRF_LOG_ERROR("Unexpected fatal error occurred: error: %s",
  433. nrf_strerror_get(p_pm_evt->params.error_unexpected.error));
  434. break;
  435. case PM_EVT_PEER_DATA_UPDATE_SUCCEEDED:
  436. NRF_LOG_DEBUG("Peer data updated in flash: peer_id: %d, data_id: %s, action: %s",
  437. p_pm_evt->peer_id,
  438. m_data_id_str[p_pm_evt->params.peer_data_update_succeeded.data_id],
  439. m_data_action_str[p_pm_evt->params.peer_data_update_succeeded.action]);
  440. break;
  441. case PM_EVT_PEER_DATA_UPDATE_FAILED:
  442. // This can happen if the SoftDevice is too busy with BLE operations.
  443. NRF_LOG_WARNING("Peer data updated failed: peer_id: %d, data_id: %s, action: %s, error: %s",
  444. p_pm_evt->peer_id,
  445. m_data_id_str[p_pm_evt->params.peer_data_update_failed.data_id],
  446. m_data_action_str[p_pm_evt->params.peer_data_update_succeeded.action],
  447. nrf_strerror_get(p_pm_evt->params.peer_data_update_failed.error));
  448. break;
  449. case PM_EVT_PEER_DELETE_SUCCEEDED:
  450. NRF_LOG_ERROR("Peer deleted successfully: peer_id: %d", p_pm_evt->peer_id);
  451. break;
  452. case PM_EVT_PEER_DELETE_FAILED:
  453. NRF_LOG_ERROR("Peer deletion failed: peer_id: %d, error: %s",
  454. p_pm_evt->peer_id,
  455. nrf_strerror_get(p_pm_evt->params.peer_delete_failed.error));
  456. break;
  457. case PM_EVT_PEERS_DELETE_SUCCEEDED:
  458. NRF_LOG_INFO("All peers deleted.");
  459. break;
  460. case PM_EVT_PEERS_DELETE_FAILED:
  461. NRF_LOG_ERROR("All peer deletion failed: error: %s",
  462. nrf_strerror_get(p_pm_evt->params.peers_delete_failed_evt.error));
  463. break;
  464. case PM_EVT_LOCAL_DB_CACHE_APPLIED:
  465. NRF_LOG_DEBUG("Previously stored local DB applied: conn_handle: %d, peer_id: %d",
  466. p_pm_evt->conn_handle,
  467. p_pm_evt->peer_id);
  468. break;
  469. case PM_EVT_LOCAL_DB_CACHE_APPLY_FAILED:
  470. // This can happen when the local DB has changed.
  471. NRF_LOG_WARNING("Local DB could not be applied: conn_handle: %d, peer_id: %d",
  472. p_pm_evt->conn_handle,
  473. p_pm_evt->peer_id);
  474. break;
  475. case PM_EVT_SERVICE_CHANGED_IND_SENT:
  476. NRF_LOG_DEBUG("Sending Service Changed indication.");
  477. break;
  478. case PM_EVT_SERVICE_CHANGED_IND_CONFIRMED:
  479. NRF_LOG_DEBUG("Service Changed indication confirmed.");
  480. break;
  481. case PM_EVT_SLAVE_SECURITY_REQ:
  482. NRF_LOG_DEBUG("Security Request received from peer.");
  483. break;
  484. case PM_EVT_FLASH_GARBAGE_COLLECTED:
  485. NRF_LOG_DEBUG("Flash garbage collection complete.");
  486. break;
  487. case PM_EVT_FLASH_GARBAGE_COLLECTION_FAILED:
  488. NRF_LOG_WARNING("Flash garbage collection failed with error %s.",
  489. nrf_strerror_get(p_pm_evt->params.garbage_collection_failed.error));
  490. break;
  491. default:
  492. NRF_LOG_WARNING("Unexpected PM event ID: 0x%x.", p_pm_evt->evt_id);
  493. break;
  494. }
  495. }
  496. void pm_handler_disconnect_on_sec_failure(pm_evt_t const * p_pm_evt)
  497. {
  498. ret_code_t err_code;
  499. if (p_pm_evt->evt_id == PM_EVT_CONN_SEC_FAILED)
  500. {
  501. NRF_LOG_WARNING("Disconnecting conn_handle %d.", p_pm_evt->conn_handle);
  502. err_code = sd_ble_gap_disconnect(p_pm_evt->conn_handle,
  503. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  504. if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_INVALID_CONN_HANDLE))
  505. {
  506. APP_ERROR_CHECK(err_code);
  507. }
  508. }
  509. }
  510. void pm_handler_secure_on_connection(ble_evt_t const * p_ble_evt)
  511. {
  512. switch (p_ble_evt->header.evt_id)
  513. {
  514. case BLE_GAP_EVT_CONNECTED:
  515. NRF_LOG_DEBUG("Connected, securing connection. conn_handle: %d", p_ble_evt->evt.gap_evt.conn_handle);
  516. conn_secure(p_ble_evt->evt.gap_evt.conn_handle, false);
  517. break;
  518. #if PM_HANDLER_SEC_DELAY_MS > 0
  519. case BLE_GAP_EVT_DISCONNECTED:
  520. {
  521. ret_code_t err_code = app_timer_stop(secure_delay_timer);
  522. APP_ERROR_CHECK(err_code);
  523. } break;
  524. #endif
  525. default:
  526. break;
  527. }
  528. }
  529. void pm_handler_secure_on_error(ble_evt_t const * p_ble_evt)
  530. {
  531. if ((p_ble_evt->header.evt_id >= BLE_GATTC_EVT_BASE) && (p_ble_evt->header.evt_id <= BLE_GATTC_EVT_LAST))
  532. {
  533. if ((p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION) ||
  534. (p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION))
  535. {
  536. NRF_LOG_INFO("GATTC procedure (evt id 0x%x) failed because it needs encryption. Bonding: conn_handle=%d",
  537. p_ble_evt->header.evt_id,
  538. p_ble_evt->evt.gattc_evt.conn_handle);
  539. conn_secure(p_ble_evt->evt.gattc_evt.conn_handle, true);
  540. }
  541. }
  542. }