cvb
Fork of nrf51-sdk by
source/nordic_sdk/components/ble/common/ble_conn_state.c@0:bc2961fa1ef0, 2016-04-06 (annotated)
- Committer:
- Jonathan Austin
- Date:
- Wed Apr 06 23:55:04 2016 +0100
- Revision:
- 0:bc2961fa1ef0
Synchronized with git rev 90647e3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jonathan Austin |
0:bc2961fa1ef0 | 1 | /* |
Jonathan Austin |
0:bc2961fa1ef0 | 2 | * Copyright (c) Nordic Semiconductor ASA |
Jonathan Austin |
0:bc2961fa1ef0 | 3 | * All rights reserved. |
Jonathan Austin |
0:bc2961fa1ef0 | 4 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
Jonathan Austin |
0:bc2961fa1ef0 | 6 | * are permitted provided that the following conditions are met: |
Jonathan Austin |
0:bc2961fa1ef0 | 7 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
Jonathan Austin |
0:bc2961fa1ef0 | 9 | * list of conditions and the following disclaimer. |
Jonathan Austin |
0:bc2961fa1ef0 | 10 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
Jonathan Austin |
0:bc2961fa1ef0 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
Jonathan Austin |
0:bc2961fa1ef0 | 13 | * other materials provided with the distribution. |
Jonathan Austin |
0:bc2961fa1ef0 | 14 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
Jonathan Austin |
0:bc2961fa1ef0 | 16 | * contributors to this software may be used to endorse or promote products |
Jonathan Austin |
0:bc2961fa1ef0 | 17 | * derived from this software without specific prior written permission. |
Jonathan Austin |
0:bc2961fa1ef0 | 18 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 19 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
Jonathan Austin |
0:bc2961fa1ef0 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Jonathan Austin |
0:bc2961fa1ef0 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Jonathan Austin |
0:bc2961fa1ef0 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
Jonathan Austin |
0:bc2961fa1ef0 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Jonathan Austin |
0:bc2961fa1ef0 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Jonathan Austin |
0:bc2961fa1ef0 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
Jonathan Austin |
0:bc2961fa1ef0 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Jonathan Austin |
0:bc2961fa1ef0 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Jonathan Austin |
0:bc2961fa1ef0 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Jonathan Austin |
0:bc2961fa1ef0 | 30 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 31 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 32 | |
Jonathan Austin |
0:bc2961fa1ef0 | 33 | #include "ble_conn_state.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 34 | #include <stdbool.h> |
Jonathan Austin |
0:bc2961fa1ef0 | 35 | #include <stdint.h> |
Jonathan Austin |
0:bc2961fa1ef0 | 36 | #include <string.h> |
Jonathan Austin |
0:bc2961fa1ef0 | 37 | #include "ble.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 38 | #include "sdk_mapped_flags.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 39 | #include "app_error.h" |
Jonathan Austin |
0:bc2961fa1ef0 | 40 | |
Jonathan Austin |
0:bc2961fa1ef0 | 41 | |
Jonathan Austin |
0:bc2961fa1ef0 | 42 | #if defined(__CC_ARM) |
Jonathan Austin |
0:bc2961fa1ef0 | 43 | #pragma push |
Jonathan Austin |
0:bc2961fa1ef0 | 44 | #pragma anon_unions |
Jonathan Austin |
0:bc2961fa1ef0 | 45 | #elif defined(__ICCARM__) |
Jonathan Austin |
0:bc2961fa1ef0 | 46 | #pragma language=extended |
Jonathan Austin |
0:bc2961fa1ef0 | 47 | #elif defined(__GNUC__) |
Jonathan Austin |
0:bc2961fa1ef0 | 48 | /* anonymous unions are enabled by default */ |
Jonathan Austin |
0:bc2961fa1ef0 | 49 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 50 | |
Jonathan Austin |
0:bc2961fa1ef0 | 51 | |
Jonathan Austin |
0:bc2961fa1ef0 | 52 | #define BLE_CONN_STATE_N_DEFAULT_FLAGS 5 /**< The number of flags kept for each connection, excluding user flags. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 53 | #define BLE_CONN_STATE_N_FLAGS (BLE_CONN_STATE_N_DEFAULT_FLAGS + BLE_CONN_STATE_N_USER_FLAGS) /**< The number of flags kept for each connection, including user flags. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 54 | |
Jonathan Austin |
0:bc2961fa1ef0 | 55 | |
Jonathan Austin |
0:bc2961fa1ef0 | 56 | /**@brief Structure containing all the flag collections maintained by the Connection State module. |
Jonathan Austin |
0:bc2961fa1ef0 | 57 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 58 | typedef struct |
Jonathan Austin |
0:bc2961fa1ef0 | 59 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 60 | sdk_mapped_flags_t valid_flags; /**< Flags indicating which connection handles are valid. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 61 | sdk_mapped_flags_t connected_flags; /**< Flags indicating which connections are connected, since disconnected connection handles will not immediately be invalidated. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 62 | sdk_mapped_flags_t central_flags; /**< Flags indicating in which connections the local device is the central. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 63 | sdk_mapped_flags_t encrypted_flags; /**< Flags indicating which connections are encrypted. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 64 | sdk_mapped_flags_t mitm_protected_flags; /**< Flags indicating which connections have encryption with protection from man-in-the-middle attacks. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 65 | sdk_mapped_flags_t user_flags[BLE_CONN_STATE_N_USER_FLAGS]; /**< Flags that can be reserved by the user. The flags will be cleared when a connection is invalidated, otherwise, the user is wholly responsible for the flag states. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 66 | } ble_conn_state_flag_collections_t; |
Jonathan Austin |
0:bc2961fa1ef0 | 67 | |
Jonathan Austin |
0:bc2961fa1ef0 | 68 | |
Jonathan Austin |
0:bc2961fa1ef0 | 69 | /**@brief Structure containing the internal state of the Connection State module. |
Jonathan Austin |
0:bc2961fa1ef0 | 70 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 71 | typedef struct |
Jonathan Austin |
0:bc2961fa1ef0 | 72 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 73 | uint16_t acquired_flags; /**< Bitmap for keeping track of which user flags have been acquired. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 74 | uint16_t valid_conn_handles[SDK_MAPPED_FLAGS_N_KEYS]; /**< List of connection handles used as keys for the sdk_mapped_flags module. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 75 | union |
Jonathan Austin |
0:bc2961fa1ef0 | 76 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 77 | ble_conn_state_flag_collections_t flags; /**< Flag collections kept by the Connection State module. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 78 | sdk_mapped_flags_t flag_array[BLE_CONN_STATE_N_FLAGS]; /**< Flag collections as array to allow use of @ref sdk_mapped_flags_bulk_update_by_key() when setting all flags. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 79 | }; |
Jonathan Austin |
0:bc2961fa1ef0 | 80 | } ble_conn_state_t; |
Jonathan Austin |
0:bc2961fa1ef0 | 81 | |
Jonathan Austin |
0:bc2961fa1ef0 | 82 | |
Jonathan Austin |
0:bc2961fa1ef0 | 83 | #if defined(__CC_ARM) |
Jonathan Austin |
0:bc2961fa1ef0 | 84 | #pragma pop |
Jonathan Austin |
0:bc2961fa1ef0 | 85 | #elif defined(__ICCARM__) |
Jonathan Austin |
0:bc2961fa1ef0 | 86 | /* leave anonymous unions enabled */ |
Jonathan Austin |
0:bc2961fa1ef0 | 87 | #elif defined(__GNUC__) |
Jonathan Austin |
0:bc2961fa1ef0 | 88 | /* anonymous unions are enabled by default */ |
Jonathan Austin |
0:bc2961fa1ef0 | 89 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 90 | |
Jonathan Austin |
0:bc2961fa1ef0 | 91 | |
Jonathan Austin |
0:bc2961fa1ef0 | 92 | static ble_conn_state_t m_bcs = {0}; /**< Instantiation of the internal state. */ |
Jonathan Austin |
0:bc2961fa1ef0 | 93 | |
Jonathan Austin |
0:bc2961fa1ef0 | 94 | |
Jonathan Austin |
0:bc2961fa1ef0 | 95 | /**@brief Function for resetting all internal memory to the values it had at initialization. |
Jonathan Austin |
0:bc2961fa1ef0 | 96 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 97 | void bcs_internal_state_reset(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 98 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 99 | memset( &m_bcs, 0, sizeof(ble_conn_state_t) ); |
Jonathan Austin |
0:bc2961fa1ef0 | 100 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 101 | |
Jonathan Austin |
0:bc2961fa1ef0 | 102 | |
Jonathan Austin |
0:bc2961fa1ef0 | 103 | /**@brief Function for activating a connection record. |
Jonathan Austin |
0:bc2961fa1ef0 | 104 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 105 | * @param p_record The record to activate. |
Jonathan Austin |
0:bc2961fa1ef0 | 106 | * @param conn_handle The connection handle to copy into the record. |
Jonathan Austin |
0:bc2961fa1ef0 | 107 | * @param role The role of the connection. |
Jonathan Austin |
0:bc2961fa1ef0 | 108 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 109 | * @return whether the record was activated successfully. |
Jonathan Austin |
0:bc2961fa1ef0 | 110 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 111 | static bool record_activate(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 112 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 113 | uint16_t available_index = sdk_mapped_flags_first_key_index_get(~m_bcs.flags.valid_flags); |
Jonathan Austin |
0:bc2961fa1ef0 | 114 | |
Jonathan Austin |
0:bc2961fa1ef0 | 115 | if (available_index != SDK_MAPPED_FLAGS_INVALID_INDEX) |
Jonathan Austin |
0:bc2961fa1ef0 | 116 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 117 | m_bcs.valid_conn_handles[available_index] = conn_handle; |
Jonathan Austin |
0:bc2961fa1ef0 | 118 | sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 119 | &m_bcs.flags.connected_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 120 | conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 121 | 1); |
Jonathan Austin |
0:bc2961fa1ef0 | 122 | sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 123 | &m_bcs.flags.valid_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 124 | conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 125 | 1); |
Jonathan Austin |
0:bc2961fa1ef0 | 126 | |
Jonathan Austin |
0:bc2961fa1ef0 | 127 | return true; |
Jonathan Austin |
0:bc2961fa1ef0 | 128 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 129 | |
Jonathan Austin |
0:bc2961fa1ef0 | 130 | return false; |
Jonathan Austin |
0:bc2961fa1ef0 | 131 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 132 | |
Jonathan Austin |
0:bc2961fa1ef0 | 133 | |
Jonathan Austin |
0:bc2961fa1ef0 | 134 | /**@brief Function for marking a connection record as invalid and resetting the values. |
Jonathan Austin |
0:bc2961fa1ef0 | 135 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 136 | * @param p_record The record to invalidate. |
Jonathan Austin |
0:bc2961fa1ef0 | 137 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 138 | static void record_invalidate(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 139 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 140 | sdk_mapped_flags_bulk_update_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 141 | m_bcs.flag_array, |
Jonathan Austin |
0:bc2961fa1ef0 | 142 | BLE_CONN_STATE_N_FLAGS, |
Jonathan Austin |
0:bc2961fa1ef0 | 143 | conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 144 | 0); |
Jonathan Austin |
0:bc2961fa1ef0 | 145 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 146 | |
Jonathan Austin |
0:bc2961fa1ef0 | 147 | |
Jonathan Austin |
0:bc2961fa1ef0 | 148 | /**@brief Function for marking a connection as disconnected. See @ref BLE_CONN_STATUS_DISCONNECTED. |
Jonathan Austin |
0:bc2961fa1ef0 | 149 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 150 | * @param p_record The record of the connection to set as disconnected. |
Jonathan Austin |
0:bc2961fa1ef0 | 151 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 152 | static void record_set_disconnected(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 153 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 154 | sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 155 | &m_bcs.flags.connected_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 156 | conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 157 | 0); |
Jonathan Austin |
0:bc2961fa1ef0 | 158 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 159 | |
Jonathan Austin |
0:bc2961fa1ef0 | 160 | |
Jonathan Austin |
0:bc2961fa1ef0 | 161 | /**@brief Function for invalidating records with a @ref BLE_CONN_STATUS_DISCONNECTED |
Jonathan Austin |
0:bc2961fa1ef0 | 162 | * connection status |
Jonathan Austin |
0:bc2961fa1ef0 | 163 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 164 | static void record_purge_disconnected() |
Jonathan Austin |
0:bc2961fa1ef0 | 165 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 166 | sdk_mapped_flags_key_list_t disconnected_list; |
Jonathan Austin |
0:bc2961fa1ef0 | 167 | |
Jonathan Austin |
0:bc2961fa1ef0 | 168 | disconnected_list = sdk_mapped_flags_key_list_get( |
Jonathan Austin |
0:bc2961fa1ef0 | 169 | m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 170 | (~m_bcs.flags.connected_flags) & (m_bcs.flags.valid_flags)); |
Jonathan Austin |
0:bc2961fa1ef0 | 171 | |
Jonathan Austin |
0:bc2961fa1ef0 | 172 | for (int i = 0; i < disconnected_list.len; i++) |
Jonathan Austin |
0:bc2961fa1ef0 | 173 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 174 | record_invalidate(disconnected_list.flag_keys[i]); |
Jonathan Austin |
0:bc2961fa1ef0 | 175 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 176 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 177 | |
Jonathan Austin |
0:bc2961fa1ef0 | 178 | |
Jonathan Austin |
0:bc2961fa1ef0 | 179 | /**@brief Function for checking if a user flag has been acquired. |
Jonathan Austin |
0:bc2961fa1ef0 | 180 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 181 | * @param[in] flag_id Which flag to check. |
Jonathan Austin |
0:bc2961fa1ef0 | 182 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 183 | * @return Whether the flag has been acquired. |
Jonathan Austin |
0:bc2961fa1ef0 | 184 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 185 | static bool user_flag_is_acquired(ble_conn_state_user_flag_id_t flag_id) |
Jonathan Austin |
0:bc2961fa1ef0 | 186 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 187 | return ((m_bcs.acquired_flags & (1 << flag_id)) != 0); |
Jonathan Austin |
0:bc2961fa1ef0 | 188 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 189 | |
Jonathan Austin |
0:bc2961fa1ef0 | 190 | |
Jonathan Austin |
0:bc2961fa1ef0 | 191 | /**@brief Function for marking a user flag as acquired. |
Jonathan Austin |
0:bc2961fa1ef0 | 192 | * |
Jonathan Austin |
0:bc2961fa1ef0 | 193 | * @param[in] flag_id Which flag to mark. |
Jonathan Austin |
0:bc2961fa1ef0 | 194 | */ |
Jonathan Austin |
0:bc2961fa1ef0 | 195 | static void user_flag_acquire(ble_conn_state_user_flag_id_t flag_id) |
Jonathan Austin |
0:bc2961fa1ef0 | 196 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 197 | m_bcs.acquired_flags |= (1 << flag_id); |
Jonathan Austin |
0:bc2961fa1ef0 | 198 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 199 | |
Jonathan Austin |
0:bc2961fa1ef0 | 200 | |
Jonathan Austin |
0:bc2961fa1ef0 | 201 | void ble_conn_state_init(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 202 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 203 | bcs_internal_state_reset(); |
Jonathan Austin |
0:bc2961fa1ef0 | 204 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 205 | |
Jonathan Austin |
0:bc2961fa1ef0 | 206 | |
Jonathan Austin |
0:bc2961fa1ef0 | 207 | void ble_conn_state_on_ble_evt(ble_evt_t * p_ble_evt) |
Jonathan Austin |
0:bc2961fa1ef0 | 208 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 209 | switch (p_ble_evt->header.evt_id) |
Jonathan Austin |
0:bc2961fa1ef0 | 210 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 211 | case BLE_GAP_EVT_CONNECTED: |
Jonathan Austin |
0:bc2961fa1ef0 | 212 | record_purge_disconnected(); |
Jonathan Austin |
0:bc2961fa1ef0 | 213 | |
Jonathan Austin |
0:bc2961fa1ef0 | 214 | if ( !record_activate(p_ble_evt->evt.gap_evt.conn_handle) ) |
Jonathan Austin |
0:bc2961fa1ef0 | 215 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 216 | // No more records available. Should not happen. |
Jonathan Austin |
0:bc2961fa1ef0 | 217 | APP_ERROR_HANDLER(NRF_ERROR_NO_MEM); |
Jonathan Austin |
0:bc2961fa1ef0 | 218 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 219 | else |
Jonathan Austin |
0:bc2961fa1ef0 | 220 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 221 | #if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110) |
Jonathan Austin |
0:bc2961fa1ef0 | 222 | bool is_central = false; |
Jonathan Austin |
0:bc2961fa1ef0 | 223 | #elif defined(TARGET_MCU_NRF51_16K_S120) || defined(TARGET_MCU_NRF51_32K_S120) |
Jonathan Austin |
0:bc2961fa1ef0 | 224 | bool is_central = true; |
Jonathan Austin |
0:bc2961fa1ef0 | 225 | #else |
Jonathan Austin |
0:bc2961fa1ef0 | 226 | bool is_central = |
Jonathan Austin |
0:bc2961fa1ef0 | 227 | (p_ble_evt->evt.gap_evt.params.connected.role == BLE_GAP_ROLE_CENTRAL); |
Jonathan Austin |
0:bc2961fa1ef0 | 228 | #endif |
Jonathan Austin |
0:bc2961fa1ef0 | 229 | |
Jonathan Austin |
0:bc2961fa1ef0 | 230 | sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 231 | &m_bcs.flags.central_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 232 | p_ble_evt->evt.gap_evt.conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 233 | is_central); |
Jonathan Austin |
0:bc2961fa1ef0 | 234 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 235 | |
Jonathan Austin |
0:bc2961fa1ef0 | 236 | break; |
Jonathan Austin |
0:bc2961fa1ef0 | 237 | |
Jonathan Austin |
0:bc2961fa1ef0 | 238 | case BLE_GAP_EVT_DISCONNECTED: |
Jonathan Austin |
0:bc2961fa1ef0 | 239 | record_set_disconnected(p_ble_evt->evt.gap_evt.conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 240 | break; |
Jonathan Austin |
0:bc2961fa1ef0 | 241 | |
Jonathan Austin |
0:bc2961fa1ef0 | 242 | case BLE_GAP_EVT_CONN_SEC_UPDATE: |
Jonathan Austin |
0:bc2961fa1ef0 | 243 | sdk_mapped_flags_update_by_key( |
Jonathan Austin |
0:bc2961fa1ef0 | 244 | m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 245 | &m_bcs.flags.encrypted_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 246 | p_ble_evt->evt.gap_evt.conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 247 | (p_ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv > 1)); |
Jonathan Austin |
0:bc2961fa1ef0 | 248 | sdk_mapped_flags_update_by_key( |
Jonathan Austin |
0:bc2961fa1ef0 | 249 | m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 250 | &m_bcs.flags.mitm_protected_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 251 | p_ble_evt->evt.gap_evt.conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 252 | (p_ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv > 2)); |
Jonathan Austin |
0:bc2961fa1ef0 | 253 | break; |
Jonathan Austin |
0:bc2961fa1ef0 | 254 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 255 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 256 | |
Jonathan Austin |
0:bc2961fa1ef0 | 257 | |
Jonathan Austin |
0:bc2961fa1ef0 | 258 | bool ble_conn_state_valid(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 259 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 260 | return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 261 | m_bcs.flags.valid_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 262 | conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 263 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 264 | |
Jonathan Austin |
0:bc2961fa1ef0 | 265 | |
Jonathan Austin |
0:bc2961fa1ef0 | 266 | uint8_t ble_conn_state_role(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 267 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 268 | uint8_t role = BLE_GAP_ROLE_INVALID; |
Jonathan Austin |
0:bc2961fa1ef0 | 269 | |
Jonathan Austin |
0:bc2961fa1ef0 | 270 | if ( sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, m_bcs.flags.valid_flags, conn_handle) ) |
Jonathan Austin |
0:bc2961fa1ef0 | 271 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 272 | bool central = sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 273 | m_bcs.flags.central_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 274 | conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 275 | |
Jonathan Austin |
0:bc2961fa1ef0 | 276 | role = central ? BLE_GAP_ROLE_CENTRAL : BLE_GAP_ROLE_PERIPH; |
Jonathan Austin |
0:bc2961fa1ef0 | 277 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 278 | |
Jonathan Austin |
0:bc2961fa1ef0 | 279 | return role; |
Jonathan Austin |
0:bc2961fa1ef0 | 280 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 281 | |
Jonathan Austin |
0:bc2961fa1ef0 | 282 | |
Jonathan Austin |
0:bc2961fa1ef0 | 283 | ble_conn_state_status_t ble_conn_state_status(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 284 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 285 | ble_conn_state_status_t conn_status = BLE_CONN_STATUS_INVALID; |
Jonathan Austin |
0:bc2961fa1ef0 | 286 | bool valid = sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 287 | m_bcs.flags.valid_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 288 | conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 289 | |
Jonathan Austin |
0:bc2961fa1ef0 | 290 | if (valid) |
Jonathan Austin |
0:bc2961fa1ef0 | 291 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 292 | bool connected = sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 293 | m_bcs.flags.connected_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 294 | conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 295 | |
Jonathan Austin |
0:bc2961fa1ef0 | 296 | conn_status = connected ? BLE_CONN_STATUS_CONNECTED : BLE_CONN_STATUS_DISCONNECTED; |
Jonathan Austin |
0:bc2961fa1ef0 | 297 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 298 | |
Jonathan Austin |
0:bc2961fa1ef0 | 299 | return conn_status; |
Jonathan Austin |
0:bc2961fa1ef0 | 300 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 301 | |
Jonathan Austin |
0:bc2961fa1ef0 | 302 | |
Jonathan Austin |
0:bc2961fa1ef0 | 303 | bool ble_conn_state_encrypted(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 304 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 305 | return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 306 | m_bcs.flags.encrypted_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 307 | conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 308 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 309 | |
Jonathan Austin |
0:bc2961fa1ef0 | 310 | |
Jonathan Austin |
0:bc2961fa1ef0 | 311 | bool ble_conn_state_mitm_protected(uint16_t conn_handle) |
Jonathan Austin |
0:bc2961fa1ef0 | 312 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 313 | return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 314 | m_bcs.flags.mitm_protected_flags, |
Jonathan Austin |
0:bc2961fa1ef0 | 315 | conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 316 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 317 | |
Jonathan Austin |
0:bc2961fa1ef0 | 318 | |
Jonathan Austin |
0:bc2961fa1ef0 | 319 | uint32_t ble_conn_state_n_connections(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 320 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 321 | return sdk_mapped_flags_n_flags_set(m_bcs.flags.connected_flags); |
Jonathan Austin |
0:bc2961fa1ef0 | 322 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 323 | |
Jonathan Austin |
0:bc2961fa1ef0 | 324 | |
Jonathan Austin |
0:bc2961fa1ef0 | 325 | uint32_t ble_conn_state_n_centrals(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 326 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 327 | return sdk_mapped_flags_n_flags_set((m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
Jonathan Austin |
0:bc2961fa1ef0 | 328 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 329 | |
Jonathan Austin |
0:bc2961fa1ef0 | 330 | |
Jonathan Austin |
0:bc2961fa1ef0 | 331 | uint32_t ble_conn_state_n_peripherals(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 332 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 333 | return sdk_mapped_flags_n_flags_set((~m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
Jonathan Austin |
0:bc2961fa1ef0 | 334 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 335 | |
Jonathan Austin |
0:bc2961fa1ef0 | 336 | |
Jonathan Austin |
0:bc2961fa1ef0 | 337 | sdk_mapped_flags_key_list_t ble_conn_state_conn_handles(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 338 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 339 | return sdk_mapped_flags_key_list_get(m_bcs.valid_conn_handles, m_bcs.flags.valid_flags); |
Jonathan Austin |
0:bc2961fa1ef0 | 340 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 341 | |
Jonathan Austin |
0:bc2961fa1ef0 | 342 | |
Jonathan Austin |
0:bc2961fa1ef0 | 343 | sdk_mapped_flags_key_list_t ble_conn_state_central_handles(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 344 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 345 | return sdk_mapped_flags_key_list_get(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 346 | (m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
Jonathan Austin |
0:bc2961fa1ef0 | 347 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 348 | |
Jonathan Austin |
0:bc2961fa1ef0 | 349 | |
Jonathan Austin |
0:bc2961fa1ef0 | 350 | sdk_mapped_flags_key_list_t ble_conn_state_periph_handles(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 351 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 352 | return sdk_mapped_flags_key_list_get(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 353 | (~m_bcs.flags.central_flags) & (m_bcs.flags.connected_flags)); |
Jonathan Austin |
0:bc2961fa1ef0 | 354 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 355 | |
Jonathan Austin |
0:bc2961fa1ef0 | 356 | |
Jonathan Austin |
0:bc2961fa1ef0 | 357 | ble_conn_state_user_flag_id_t ble_conn_state_user_flag_acquire(void) |
Jonathan Austin |
0:bc2961fa1ef0 | 358 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 359 | for (ble_conn_state_user_flag_id_t i = BLE_CONN_STATE_USER_FLAG0; |
Jonathan Austin |
0:bc2961fa1ef0 | 360 | i < BLE_CONN_STATE_N_USER_FLAGS; |
Jonathan Austin |
0:bc2961fa1ef0 | 361 | i++) |
Jonathan Austin |
0:bc2961fa1ef0 | 362 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 363 | if ( !user_flag_is_acquired(i) ) |
Jonathan Austin |
0:bc2961fa1ef0 | 364 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 365 | user_flag_acquire(i); |
Jonathan Austin |
0:bc2961fa1ef0 | 366 | return i; |
Jonathan Austin |
0:bc2961fa1ef0 | 367 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 368 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 369 | |
Jonathan Austin |
0:bc2961fa1ef0 | 370 | return BLE_CONN_STATE_USER_FLAG_INVALID; |
Jonathan Austin |
0:bc2961fa1ef0 | 371 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 372 | |
Jonathan Austin |
0:bc2961fa1ef0 | 373 | |
Jonathan Austin |
0:bc2961fa1ef0 | 374 | bool ble_conn_state_user_flag_get(uint16_t conn_handle, ble_conn_state_user_flag_id_t flag_id) |
Jonathan Austin |
0:bc2961fa1ef0 | 375 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 376 | if (user_flag_is_acquired(flag_id)) |
Jonathan Austin |
0:bc2961fa1ef0 | 377 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 378 | return sdk_mapped_flags_get_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 379 | m_bcs.flags.user_flags[flag_id], |
Jonathan Austin |
0:bc2961fa1ef0 | 380 | conn_handle); |
Jonathan Austin |
0:bc2961fa1ef0 | 381 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 382 | else |
Jonathan Austin |
0:bc2961fa1ef0 | 383 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 384 | return false; |
Jonathan Austin |
0:bc2961fa1ef0 | 385 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 386 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 387 | |
Jonathan Austin |
0:bc2961fa1ef0 | 388 | |
Jonathan Austin |
0:bc2961fa1ef0 | 389 | void ble_conn_state_user_flag_set(uint16_t conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 390 | ble_conn_state_user_flag_id_t flag_id, |
Jonathan Austin |
0:bc2961fa1ef0 | 391 | bool value) |
Jonathan Austin |
0:bc2961fa1ef0 | 392 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 393 | if (user_flag_is_acquired(flag_id)) |
Jonathan Austin |
0:bc2961fa1ef0 | 394 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 395 | sdk_mapped_flags_update_by_key(m_bcs.valid_conn_handles, |
Jonathan Austin |
0:bc2961fa1ef0 | 396 | &m_bcs.flags.user_flags[flag_id], |
Jonathan Austin |
0:bc2961fa1ef0 | 397 | conn_handle, |
Jonathan Austin |
0:bc2961fa1ef0 | 398 | value); |
Jonathan Austin |
0:bc2961fa1ef0 | 399 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 400 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 401 | |
Jonathan Austin |
0:bc2961fa1ef0 | 402 | |
Jonathan Austin |
0:bc2961fa1ef0 | 403 | sdk_mapped_flags_t ble_conn_state_user_flag_collection(ble_conn_state_user_flag_id_t flag_id) |
Jonathan Austin |
0:bc2961fa1ef0 | 404 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 405 | if ( user_flag_is_acquired(flag_id) ) |
Jonathan Austin |
0:bc2961fa1ef0 | 406 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 407 | return m_bcs.flags.user_flags[flag_id]; |
Jonathan Austin |
0:bc2961fa1ef0 | 408 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 409 | else |
Jonathan Austin |
0:bc2961fa1ef0 | 410 | { |
Jonathan Austin |
0:bc2961fa1ef0 | 411 | return 0; |
Jonathan Austin |
0:bc2961fa1ef0 | 412 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 413 | } |
Jonathan Austin |
0:bc2961fa1ef0 | 414 |