Skip to content

Commit 450a432

Browse files
committed
Refactor: Enhance BLESecure::removeBonding for specific device bond removal
This commit significantly refactors the `BLESecureClass::removeBonding(BLEDevice *device)` method to provide a functional implementation for deleting the bonding information of a specific, currently or recently connected LE device. Previously, this method was a placeholder that only disconnected the device and noted the need for a full implementation. The updated method now performs the following steps: 1. **Input Validation**: Checks if the provided `BLEDevice` object is valid and if it has a valid `hci_con_handle_t`. 2. **Device DB Index Lookup**: Uses `sm_le_device_index(handle)` to retrieve the internal index of the device within BTstack's LE Device Database. This requires the device's security context to be known to the Security Manager (SM). 3. **Address Information Retrieval**: If a valid DB index is found, `le_device_db_info()` is called to fetch the `bd_addr_t` (Bluetooth address) and `bd_addr_type_t` (address type) of the device from the LE Device Database (which, in the `arduino-pico` context, is typically a TLV store on flash). 4. **GAP-Level Bond Deletion**: Calls `gap_delete_bonding(addr_type, addr)` with the retrieved address and type. This is the appropriate high-level BTstack API function to request the deletion of an LE bond. It is expected to interact with both the LE Device Database and the Security Manager. 5. **Verification and Logging**: * Includes extensive `Serial.println()` messages to log the steps taken, including the device's DB index, address, and address type. * Calls `le_device_db_dump()` after `gap_delete_bonding` to allow observation of the LE Device DB state (though immediate changes to flash might not always be reflected without a re-scan or if the dump relies on a RAM cache). * Checks `le_device_db_count()` after the deletion attempt to log if the count of bonded devices has changed. 6. **Disconnection**: Calls `gap_disconnect(handle)` to disconnect the device after the bond removal attempt, maintaining a similar behavior to the original placeholder in this regard. 7. **Error Handling**: Provides log messages if the device is not found in the LE Device DB or if a valid LE address type cannot be retrieved. This enhancement aims to provide a more robust mechanism for managing individual device bonds within the `BLESecure` library, complementing the `clearAllBondings` function which targets all stored bonds. The effectiveness in completely preventing re-encryption still depends on the underlying behavior of BTstack's TLV flash backend and Security Manager regarding persistent key storage.
1 parent 688f8ac commit 450a432

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

src/BLESecure.cpp

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,35 +186,64 @@ bool BLESecureClass::bondWithDevice(BLEDevice *device)
186186
return result;
187187
}
188188

189-
bool BLESecureClass::removeBonding(BLEDevice *device)
190-
{
191-
if (!device)
189+
bool BLESecureClass::removeBonding(BLEDevice *device) {
190+
if (!device) {
191+
Serial.println("removeBonding: BLEDevice object is NULL.");
192192
return false;
193+
}
193194

194195
hci_con_handle_t handle = device->getHandle();
195-
if (handle == HCI_CON_HANDLE_INVALID)
196+
if (handle == HCI_CON_HANDLE_INVALID) {
197+
Serial.println("removeBonding: Invalid connection handle from BLEDevice.");
196198
return false;
199+
}
197200

201+
Serial.println("Attempting to remove bonding for specific device.");
198202
BluetoothLock b;
199203

200-
// Try to get the device index from the Security Manager
201204
int device_db_index = sm_le_device_index(handle);
202-
if (device_db_index < 0)
203-
{
204-
Serial.println("No bonding info found for this device");
205+
206+
if (device_db_index < 0) {
207+
Serial.print("removeBonding: Device not found in LE Device DB (sm_le_device_index returned ");
208+
Serial.print(device_db_index);
209+
Serial.println("). Cannot get address to remove bond. It might not be bonded or not connected.");
205210
return false;
206211
}
207212

208-
// We can't directly call le_device_db_remove as it might not be exposed
209-
// but we can log the information for debugging
210-
Serial.print("Found device at index: ");
213+
Serial.print("removeBonding: Found device in LE DB at index: ");
211214
Serial.println(device_db_index);
212-
Serial.println("Note: Full removeBonding() implementation requires access to le_device_db functions");
213215

214-
// For now, we'll just disconnect which should help in some cases
215-
gap_disconnect(handle);
216+
int addr_type_int;
217+
bd_addr_t addr;
218+
le_device_db_info(device_db_index, &addr_type_int, addr, NULL /* irk */);
219+
bd_addr_type_t current_addr_type = (bd_addr_type_t)addr_type_int;
216220

217-
return true;
221+
if (current_addr_type == BD_ADDR_TYPE_LE_PUBLIC || current_addr_type == BD_ADDR_TYPE_LE_RANDOM) {
222+
Serial.print("removeBonding: Calling gap_delete_bonding for AddrType: ");
223+
Serial.print(current_addr_type);
224+
Serial.print(", Addr: ");
225+
Serial.println(bd_addr_to_str(addr));
226+
227+
gap_delete_bonding(current_addr_type, addr);
228+
229+
Serial.println("removeBonding: gap_delete_bonding called. Verifying DB state:");
230+
le_device_db_dump();
231+
232+
int current_db_count = le_device_db_count();
233+
Serial.print("removeBonding: le_device_db_count() after gap_delete_bonding: ");
234+
Serial.println(current_db_count);
235+
236+
237+
Serial.println("removeBonding: Disconnecting device.");
238+
gap_disconnect(handle);
239+
240+
return true;
241+
} else {
242+
Serial.print("removeBonding: Could not retrieve a valid LE address type for device at DB index ");
243+
Serial.print(device_db_index);
244+
Serial.print(" (type was "); Serial.print(current_addr_type); Serial.println("). Bond not removed.");
245+
return false;
246+
}
218247
}
219248

220249
void BLESecureClass::clearAllBondings() {

0 commit comments

Comments
 (0)