peer_manager_handler.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  41. * @file peer_manager_handler.h
  42. *
  43. * @defgroup pm_handler Peer Manager Standard Event Handlers
  44. * @ingroup peer_manager
  45. * @{
  46. * @brief Standard event handlers implementing some best practices for BLE security.
  47. */
  48. #ifndef PEER_MANAGER_HANDLER_H__
  49. #define PEER_MANAGER_HANDLER_H__
  50. #include "ble.h"
  51. #include "peer_manager.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**@brief Standard function for making Peer Manager calls based on Peer Manager events.
  56. *
  57. * This function does the following:
  58. * - Logs all PM events using @ref nrf_log, at different severity levels.
  59. * - Starts encryption if connected to an already bonded peer. This is affected by @ref
  60. * PM_HANDLER_SEC_DELAY_MS.
  61. * - Calls @ref app_error on fatal errors.
  62. *
  63. * @note In normal circumstances, this function should be called for every Peer Manager event.
  64. *
  65. * @param[in] p_pm_evt Peer Manager event to handle.
  66. */
  67. void pm_handler_on_pm_evt(pm_evt_t const * p_pm_evt);
  68. /**@brief Auxiliary standard function for logging Peer Manager events.
  69. *
  70. * This function logs all PM events using @ref nrf_log, at different severity levels. The
  71. * @ref PM_LOG_ENABLED and other @c PM_LOG_* configs control these log messages.
  72. *
  73. * @note This function is called internally by @ref pm_handler_on_pm_evt.
  74. *
  75. * @param[in] p_pm_evt Peer Manager event to log.
  76. */
  77. void pm_handler_pm_evt_log(pm_evt_t const * p_pm_evt);
  78. /**@brief Auxiliary standard function for maintaining room in flash based on Peer Manager events.
  79. *
  80. * This function does the following:
  81. * - Ranks peers by when they last connected.
  82. * - Garbage collects the flash when needed.
  83. * - Deletes the lowest ranked peer(s) when garbage collection is insufficient.
  84. *
  85. * @note See also @ref pm_handler_flash_clean_on_return.
  86. * @note In normal circumstances, this function should be called for every Peer Manager event.
  87. * @note This function is a supplement to, not a replacement for @ref pm_handler_on_pm_evt.
  88. *
  89. * @param[in] p_pm_evt Peer Manager event to handle.
  90. */
  91. void pm_handler_flash_clean(pm_evt_t const * p_pm_evt);
  92. /**@brief Function to call when a Peer Manager function returns @ref NRF_ERROR_STORAGE_FULL.
  93. *
  94. * @note This should only be used if @ref pm_handler_flash_clean is also used.
  95. */
  96. void pm_handler_flash_clean_on_return(void);
  97. /**@brief Auxiliary standard function for disconnecting when the connection could not be secured.
  98. *
  99. * This function disconnects whenever connection security fails, i.e. whenever it receives a
  100. * @ref PM_EVT_CONN_SEC_FAILED.
  101. *
  102. * @note In normal circumstances, this function should be called for every Peer Manager event.
  103. * @note This function is a supplement to, not a replacement for @ref pm_handler_on_pm_evt.
  104. *
  105. * @param[in] p_pm_evt Peer Manager event to handle.
  106. */
  107. void pm_handler_disconnect_on_sec_failure(pm_evt_t const * p_pm_evt);
  108. /**@brief Function for securing a connection when it is established.
  109. *
  110. * This function starts security when receiving a @ref BLE_GAP_EVT_CONNECTED event. This is
  111. * affected by @ref PM_HANDLER_SEC_DELAY_MS.
  112. *
  113. * @note In normal circumstances, this function should be called for every BLE event.
  114. *
  115. * @param[in] p_ble_evt BLE event to handle.
  116. */
  117. void pm_handler_secure_on_connection(ble_evt_t const * p_ble_evt);
  118. /**@brief Function for securing a connection if a GATT read or write operation lacks security.
  119. *
  120. * This function starts pairing if a GATTC procedure fails with insufficient encryption
  121. * or insufficient authentication. This is meant to delay performing pairing/bonding until
  122. * it is actually needed to access resources. This is affected by @ref PM_HANDLER_SEC_DELAY_MS.
  123. *
  124. * @note When using this handler, the failed GATTC operation must be retried by the user.
  125. * @note This does not work when using Write Without Response (@ref BLE_GATT_OP_WRITE_CMD) because
  126. * the server does not send any response, even on error. Instead, the write will be
  127. * silently dropped by the server.
  128. * @note In normal circumstances, this function should be called for every BLE event.
  129. *
  130. * @param[in] p_ble_evt BLE event to handle.
  131. */
  132. void pm_handler_secure_on_error(ble_evt_t const * p_ble_evt);
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. /** @}*/
  137. #endif // PEER_MANAGER_HANDLER_H__