improve bonding with on-the-fly semaphore
enhance central bond
This commit is contained in:
1 parent
e81f60cd2e
commit
f3136abcda
6 files changed
+87
-23
No files matched your search
@@ -168,19 +168,34 @@ bool BLEGap::requestPairing(uint16_t conn_hdl)
|
|||||||
{
|
{
|
||||||
gap_peer_t* peer = &_peers[conn_hdl];
|
gap_peer_t* peer = &_peers[conn_hdl];
|
||||||
|
|
||||||
// skip if already bonded
|
// skip if already paired
|
||||||
if ( peer->paired ) return true;
|
if ( peer->paired ) return true;
|
||||||
|
|
||||||
|
// Check to see if we did bonded with current prph
|
||||||
|
if ( peer->role == BLE_GAP_ROLE_CENTRAL )
|
||||||
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()) )
|
|
||||||
{
|
{
|
||||||
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;
|
return peer->paired;
|
||||||
}
|
}
|
||||||
@@ -372,15 +387,22 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case BLE_GAP_EVT_CONN_SEC_UPDATE:
|
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;
|
||||||
|
|
||||||
|
// Connection is secured (paired)
|
||||||
|
if ( !( conn_sec->sec_mode.sm == 1 && conn_sec->sec_mode.lv == 1) )
|
||||||
|
{
|
||||||
// Previously bonded --> secure by re-connection process
|
// Previously bonded --> secure by re-connection process
|
||||||
// --> Load & Set Sys Attr (Apply Service Context)
|
// --> Load & Set Sys Attr (Apply Service Context)
|
||||||
// Else Init Sys Attr
|
// Else Init Sys Attr
|
||||||
bond_load_cccd(peer->role, conn_hdl, peer->ediv);
|
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;
|
break;
|
||||||
|
|
||||||
case BLE_GAP_EVT_PASSKEY_DISPLAY:
|
case BLE_GAP_EVT_PASSKEY_DISPLAY:
|
||||||
|
|||||||
@@ -105,8 +105,11 @@ class BLEGap
|
|||||||
SemaphoreHandle_t hvn_tx_sem;
|
SemaphoreHandle_t hvn_tx_sem;
|
||||||
SemaphoreHandle_t wrcmd_tx_sem;
|
SemaphoreHandle_t wrcmd_tx_sem;
|
||||||
|
|
||||||
SemaphoreHandle_t indicate_confirm_sem;
|
|
||||||
bool hvc_received;
|
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;
|
||||||
|
|
||||||
gap_peer_t* _get_peer(uint16_t conn_hdl) { return &_peers[conn_hdl]; }
|
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);
|
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(conn_hdl);
|
||||||
|
|
||||||
// hvi confirm semaphore is created on the fly
|
// 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);
|
vSemaphoreDelete(peer->hvc_sem);
|
||||||
peer->indicate_confirm_sem = NULL;
|
peer->hvc_sem = NULL;
|
||||||
|
|
||||||
return peer->hvc_received;
|
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);
|
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);
|
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;
|
peer->hvc_received = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -248,7 +248,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
|||||||
|
|
||||||
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
|
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;
|
peer->hvc_received = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -731,13 +731,13 @@ void AdafruitBluefruit::printInfo(void)
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
/*------------- List the paried device -------------*/
|
/*------------- 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);
|
bond_print_list(BLE_GAP_ROLE_PERIPH);
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
Serial.printf(title_fmt, "Central Paired Devices\n");
|
Serial.printf(title_fmt, "Central Paired Devices");
|
||||||
bond_print_list(BLE_GAP_ROLE_CENTRAL);
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
bond_print_list(BLE_GAP_ROLE_CENTRAL);
|
||||||
|
|
||||||
Serial.println();
|
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);
|
sprintf(filename, BOND_FNAME_PRPH, bdata->own_enc.master_id.ediv);
|
||||||
}else
|
}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 };
|
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();
|
file.close();
|
||||||
|
|
||||||
|
PRINT_HEX(bdata->own_enc.master_id.ediv);
|
||||||
|
PRINT_HEX(bdata->peer_enc.master_id.ediv);
|
||||||
|
|
||||||
|
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s", devname, filename);
|
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");
|
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
|
/* DELETE
|
||||||
*------------------------------------------------------------------*/
|
*------------------------------------------------------------------*/
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ 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);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* BONDING_H_ */
|
#endif /* BONDING_H_ */
|
||||||
Reference in new issue
Block a user