* @brief Initiate a secure connection (pair/bond) with the server.\n * Called automatically when a characteristic or descriptor requires encryption or authentication to access it. * @return True on success. */
| 196 | * @return True on success. |
| 197 | */ |
| 198 | bool BLEClient::secureConnection() { |
| 199 | #if defined(CONFIG_BLUEDROID_ENABLED) |
| 200 | if (BLESecurity::m_authenticationComplete) { |
| 201 | return true; |
| 202 | } |
| 203 | if (!BLESecurity::m_securityEnabled) { |
| 204 | log_i("Security not enabled"); |
| 205 | return true; |
| 206 | } |
| 207 | if (!BLESecurity::startSecurity(m_peerAddress.getNative())) { |
| 208 | log_e("Failed to start security"); |
| 209 | return false; |
| 210 | } |
| 211 | BLESecurity::waitForAuthenticationComplete(); |
| 212 | return BLESecurity::m_authenticationComplete; |
| 213 | #endif |
| 214 | |
| 215 | #if defined(CONFIG_NIMBLE_ENABLED) |
| 216 | int retryCount = 1; |
| 217 | BLETaskData taskData(const_cast<BLEClient *>(this), BLE_HS_ENOTCONN); |
| 218 | m_pTaskData = &taskData; |
| 219 | |
| 220 | do { |
| 221 | if (BLESecurity::startSecurity(m_conn_id)) { |
| 222 | BLEUtils::taskWait(taskData, BLE_NPL_TIME_FOREVER); |
| 223 | } |
| 224 | |
| 225 | } while (taskData.m_flags == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--); |
| 226 | |
| 227 | m_pTaskData = nullptr; |
| 228 | |
| 229 | if (taskData.m_flags == 0) { |
| 230 | log_d("<< secureConnection: success"); |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | m_lastErr = taskData.m_flags; |
| 235 | log_e("secureConnection: failed rc=%d", taskData.m_flags); |
| 236 | return false; |
| 237 | #endif |
| 238 | } // secureConnection |
| 239 | |
| 240 | uint16_t BLEClient::getConnId() { |
| 241 | return m_conn_id; |
no test coverage detected