ble_link_ctx_manager.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) 2017 - 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. /** @file
  41. *
  42. * @defgroup ble_link_ctx_manager BLE Link Context Manager
  43. * @{
  44. * @ingroup ble_sdk_lib
  45. * @brief Storage for link-related data.
  46. *
  47. * @details BLE Link Context Manager can be used as a simple storage for link-related data.
  48. * Each link context data is uniquely identified within the storage by its connection
  49. * handle and can be retrieved from it by using this handle.
  50. *
  51. */
  52. #ifndef BLE_LINK_CTX_MANAGER_H__
  53. #define BLE_LINK_CTX_MANAGER_H__
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #include <stdint.h>
  58. #include "ble_conn_state.h"
  59. #include "sdk_errors.h"
  60. /**@brief Macro for defining a blcm_link_ctx_storage instance.
  61. *
  62. * @param[in] _name Name of the instance.
  63. * @param[in] _max_clients Maximum number of clients connected at a time.
  64. * @param[in] _link_ctx_size Context size in bytes for a single link.
  65. */
  66. #define BLE_LINK_CTX_MANAGER_DEF(_name, _max_clients, _link_ctx_size) \
  67. STATIC_ASSERT((_max_clients) <= BLE_CONN_STATE_MAX_CONNECTIONS); \
  68. static uint32_t CONCAT_2(_name, _ctx_data_pool)[(_max_clients)*BYTES_TO_WORDS(_link_ctx_size)]; \
  69. static blcm_link_ctx_storage_t _name = \
  70. { \
  71. .p_ctx_data_pool = CONCAT_2(_name, _ctx_data_pool), \
  72. .max_links_cnt = (_max_clients), \
  73. .link_ctx_size = sizeof(CONCAT_2(_name, _ctx_data_pool))/(_max_clients) \
  74. }
  75. /**
  76. * @brief Type of description that is used for registry of all current connections.
  77. */
  78. typedef struct
  79. {
  80. void * const p_ctx_data_pool; /**< Pointer to links context memory pool. */
  81. uint8_t const max_links_cnt; /**< Maximum number of concurrent links. */
  82. uint16_t const link_ctx_size; /**< Context size in bytes for a single link (word-aligned). */
  83. } blcm_link_ctx_storage_t;
  84. /**
  85. * @brief Function for getting the link context from the link registry.
  86. *
  87. * This function finds the link context in the registry. The link to find is identified by the
  88. * connection handle within the registry.
  89. *
  90. * The context is preserved for the lifetime of the connection. When a new connection occurs, the
  91. * value of its context is undefined, and should be initialized.
  92. *
  93. * @param[in] p_link_ctx_storage Pointer to the link storage descriptor.
  94. * @param[in] conn_handle Connection whose context to find.
  95. * @param[out] pp_ctx_data Pointer to data with context for the connection.
  96. *
  97. * @retval NRF_ERROR_NULL If \p p_link_ctx_storage is NULL or contains a NULL pointer, or if
  98. * \p pp_ctx_data is NULL.
  99. * @retval NRF_ERROR_INVALID_PARAM If \p p_link_ctx_storage::link_ctx_size is not multiple of word
  100. * size.
  101. * @retval NRF_ERROR_NOT_FOUND If \p conn_handle does not refer to an active or recently
  102. * disconnected link.
  103. * @retval NRF_ERROR_NO_MEM If there is not enough memory to store context for the given
  104. * connection handle. This can happen if the number of links is
  105. * greater than \p p_link_ctx_storage::max_links_cnt.
  106. * @retval NRF_SUCCESS If the operation was successful.
  107. */
  108. ret_code_t blcm_link_ctx_get(blcm_link_ctx_storage_t const * const p_link_ctx_storage,
  109. uint16_t const conn_handle,
  110. void ** const pp_ctx_data);
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #endif // BLE_LINK_CTX_MANAGER_H__
  115. /** @} */