improve bonding with on-the-fly semaphore
enhance central bond
This commit is contained in:
1 parent
e81f60cd2e
commit
f3136abcda
6 files changed
+101
-37
No files matched your search
@@ -168,20 +168,35 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
|
||||
{
|
||||
gap_peer_t* peer = &_peers[conn_hdl];
|
||||
|
||||
// skip if already bonded
|
||||
// skip if already paired
|
||||
if ( peer->paired ) return true;
|
||||
|
||||
|
||||
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
|
||||
uint32_t start = millis();
|
||||
|
||||
// timeout in 30 seconds
|
||||
while ( ! ((volatile bool) peer->paired) && (start + 30000 > millis()) )
|
||||
// Check to see if we did bonded with current prph
|
||||
if ( peer->role == BLE_GAP_ROLE_CENTRAL )
|
||||
{
|
||||
yield();
|
||||
bond_data_t bdata;
|
||||
|
||||
if ( bond_find_cntr(&peer->addr, &bdata) )
|
||||
{
|
||||
LOG_LV2("BOND", "Load Keys from file " BOND_FNAME_CNTR, bdata.peer_enc.master_id.ediv);
|
||||
VERIFY_STATUS( sd_ble_gap_encrypt(conn_hdl, &bdata.peer_enc.master_id, &bdata.peer_enc.enc_info), false);
|
||||
}else
|
||||
{
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
|
||||
}
|
||||
}else
|
||||
{
|
||||
VERIFY_STATUS( sd_ble_gap_authenticate(conn_hdl, &_sec_param ), false);
|
||||
}
|
||||
|
||||
// Wait for pairing process using on-the-fly semaphore
|
||||
peer->pair_sem = xSemaphoreCreateBinary();
|
||||
|
||||
xSemaphoreTake(peer->pair_sem, portMAX_DELAY);
|
||||
|
||||
vSemaphoreDelete(peer->pair_sem);
|
||||
peer->pair_sem = NULL;
|
||||
|
||||
return peer->paired;
|
||||
}
|
||||
|
||||
@@ -372,15 +387,22 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONN_SEC_UPDATE:
|
||||
// Connection is secured aka Paired
|
||||
{
|
||||
const ble_gap_conn_sec_t* conn_sec = &evt->evt.gap_evt.params.conn_sec_update.conn_sec;
|
||||
|
||||
// Previously bonded --> secure by re-connection process
|
||||
// --> Load & Set Sys Attr (Apply Service Context)
|
||||
// Else Init Sys Attr
|
||||
bond_load_cccd(peer->role, conn_hdl, peer->ediv);
|
||||
// Connection is secured (paired)
|
||||
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
|
||||
{
|
||||
// Previously bonded --> secure by re-connection process
|
||||
// --> Load & Set Sys Attr (Apply Service Context)
|
||||
// Else Init Sys Attr
|
||||
bond_load_cccd(peer->role, conn_hdl, peer->ediv);
|
||||
|
||||
// Paired is Bonded (as we always save keys)
|
||||
peer->paired = true;
|
||||
peer->paired = true;
|
||||
}
|
||||
|
||||
if (peer->pair_sem) xSemaphoreGive(peer->pair_sem);
|
||||
}
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_PASSKEY_DISPLAY:
|
||||
|
||||
@@ -105,8 +105,11 @@ class BLEGap
|
||||
SemaphoreHandle_t hvn_tx_sem;
|
||||
SemaphoreHandle_t wrcmd_tx_sem;
|
||||
|
||||
SemaphoreHandle_t indicate_confirm_sem;
|
||||
bool hvc_received;
|
||||
|
||||
// On-demand semaphore that are created on the fly
|
||||
SemaphoreHandle_t hvc_sem;
|
||||
SemaphoreHandle_t pair_sem;
|
||||
} gap_peer_t;
|
||||
|
||||
gap_peer_t* _get_peer(uint16_t conn_hdl) { return &_peers[conn_hdl]; }
|
||||
|
||||
@@ -80,12 +80,12 @@ bool BLEGatt::waitForIndicateConfirm(uint16_t conn_hdl)
|
||||
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(conn_hdl);
|
||||
|
||||
// hvi confirm semaphore is created on the fly
|
||||
peer->indicate_confirm_sem = xSemaphoreCreateBinary();
|
||||
peer->hvc_sem = xSemaphoreCreateBinary();
|
||||
|
||||
xSemaphoreTake(peer->indicate_confirm_sem, portMAX_DELAY);
|
||||
xSemaphoreTake(peer->hvc_sem, portMAX_DELAY);
|
||||
|
||||
vSemaphoreDelete(peer->indicate_confirm_sem);
|
||||
peer->indicate_confirm_sem = NULL;
|
||||
vSemaphoreDelete(peer->hvc_sem);
|
||||
peer->hvc_sem = NULL;
|
||||
|
||||
return peer->hvc_received;
|
||||
}
|
||||
@@ -237,7 +237,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
LOG_LV2("GATTS", "Confirm received handle = 0x%04X", evt->evt.gatts_evt.params.hvc.handle);
|
||||
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
|
||||
|
||||
if ( peer->indicate_confirm_sem ) xSemaphoreGive(peer->indicate_confirm_sem);
|
||||
if ( peer->hvc_sem ) xSemaphoreGive(peer->hvc_sem);
|
||||
peer->hvc_received = true;
|
||||
}
|
||||
break;
|
||||
@@ -248,7 +248,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
|
||||
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
|
||||
|
||||
if ( peer->indicate_confirm_sem ) xSemaphoreGive(peer->indicate_confirm_sem);
|
||||
if ( peer->hvc_sem ) xSemaphoreGive(peer->hvc_sem);
|
||||
peer->hvc_received = false;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -731,13 +731,13 @@ void AdafruitBluefruit::printInfo(void)
|
||||
Serial.println();
|
||||
|
||||
/*------------- List the paried device -------------*/
|
||||
Serial.printf(title_fmt, "Peripheral Paired Devices\n");
|
||||
Serial.printf(title_fmt, "Peripheral Paired Devices");
|
||||
Serial.println();
|
||||
bond_print_list(BLE_GAP_ROLE_PERIPH);
|
||||
Serial.println();
|
||||
|
||||
Serial.printf(title_fmt, "Central Paired Devices\n");
|
||||
bond_print_list(BLE_GAP_ROLE_CENTRAL);
|
||||
Serial.printf(title_fmt, "Central Paired Devices");
|
||||
Serial.println();
|
||||
bond_print_list(BLE_GAP_ROLE_CENTRAL);
|
||||
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bda
|
||||
sprintf(filename, BOND_FNAME_PRPH, bdata->own_enc.master_id.ediv);
|
||||
}else
|
||||
{
|
||||
sprintf(filename, BOND_FNAME_CNTR, bdata->own_enc.master_id.ediv);
|
||||
sprintf(filename, BOND_FNAME_CNTR, bdata->peer_enc.master_id.ediv);
|
||||
}
|
||||
|
||||
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
|
||||
@@ -103,6 +103,10 @@ static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bda
|
||||
|
||||
file.close();
|
||||
|
||||
PRINT_HEX(bdata->own_enc.master_id.ediv);
|
||||
PRINT_HEX(bdata->peer_enc.master_id.ediv);
|
||||
|
||||
|
||||
if (result)
|
||||
{
|
||||
LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename);
|
||||
@@ -285,6 +289,39 @@ void bond_print_list(uint8_t role)
|
||||
cprintf("\n");
|
||||
}
|
||||
|
||||
|
||||
bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
NffsDir dir(BOND_DIR_CNTR);
|
||||
NffsDirEntry dirEntry;
|
||||
|
||||
while( dir.read(&dirEntry) && !found )
|
||||
{
|
||||
// Read bond data of each stored file
|
||||
if ( !dirEntry.isDirectory() )
|
||||
{
|
||||
NffsFile file(BOND_DIR_CNTR, dirEntry, FS_ACCESS_READ);
|
||||
if ( file.read( (uint8_t*)bdata, sizeof(bond_data_t)) )
|
||||
{
|
||||
if ( !memcmp(addr->addr, bdata->peer_id.id_addr_info.addr, 6) )
|
||||
{
|
||||
// Compare static address
|
||||
found = true;
|
||||
}else if ( addr->addr_type == BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE )
|
||||
{
|
||||
// Resolving private address
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* DELETE
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
@@ -61,18 +61,20 @@ enum
|
||||
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
|
||||
};
|
||||
|
||||
void bond_init(void);
|
||||
void bond_clear_prph(void);
|
||||
void bond_clear_cntr(void);
|
||||
void bond_clear_all(void);
|
||||
void bond_init(void);
|
||||
void bond_clear_prph(void);
|
||||
void bond_clear_cntr(void);
|
||||
void bond_clear_all(void);
|
||||
|
||||
void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata);
|
||||
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata);
|
||||
void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata);
|
||||
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata);
|
||||
|
||||
void bond_save_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
|
||||
bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
|
||||
void bond_save_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
|
||||
bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv);
|
||||
|
||||
void bond_print_list(uint8_t role);
|
||||
void bond_print_list(uint8_t role);
|
||||
|
||||
bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user