move pairing event handle code to BLEGap

- add bond_init()
- move thing around
This commit is contained in:
hathach committed 2018-02-22 23:38:41 +07:00
1 parent 86ea7e7ae5
commit c4e25295e2
7 files changed
+349 -220

No files matched your search

+100 -139
View File
@@ -109,7 +109,6 @@ AdafruitBluefruit::AdafruitBluefruit(void)
_tx_power = 0;
_conn_hdl = BLE_CONN_HANDLE_INVALID;
_bonded = false;
_ppcp_min_conn = BLE_GAP_CONN_MIN_INTERVAL_DFLT;
_ppcp_max_conn = BLE_GAP_CONN_MAX_INTERVAL_DFLT;
@@ -125,20 +124,6 @@ COMMENT_OUT(
varclr(&_bond_data);
_bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv
_sec_param = (ble_gap_sec_params_t)
{
.bond = 1,
.mitm = 0,
.lesc = 0,
.keypress = 0,
.io_caps = BLE_GAP_IO_CAPS_NONE,
.oob = 0,
.min_key_size = 7,
.max_key_size = 16,
.kdist_own = { .enc = 1, .id = 1},
.kdist_peer = { .enc = 1, .id = 1},
};
}
void AdafruitBluefruit::configServiceChanged(bool changed)
@@ -464,9 +449,8 @@ err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
// Create Timer for led advertising blinky
_led_blink_th = xTimerCreate(NULL, ms2tick(CFG_ADV_BLINKY_INTERVAL/2), true, NULL, bluefruit_blinky_cb);
// Initialize nffs for bonding (it is safe to call nffs_pkg_init() multiple time)
Nffs.begin();
(void) Nffs.mkdir_p(CFG_BOND_NFFS_DIR);
// Initialize bonding
bond_init();
return ERROR_NONE;
}
@@ -597,7 +581,7 @@ uint16_t AdafruitBluefruit::connHandle(void)
bool AdafruitBluefruit::connPaired(void)
{
return _bonded;
return Gap.bonded(_conn_hdl);
}
uint16_t AdafruitBluefruit::connInterval(void)
@@ -944,109 +928,94 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
LOG_LV2("GAP", "Disconnect Reason 0x%02X", evt->evt.gap_evt.params.disconnected.reason);
_conn_hdl = BLE_CONN_HANDLE_INVALID;
_bonded = false;
break;
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
{
// Pairing in progress
varclr(&_bond_data);
_bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv
// case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
// // Pairing in progress
// varclr(&_bond_data);
// _bond_data.own_enc.master_id.ediv = 0xFFFF; // invalid value for ediv
//
// /* Step 1: Pairing/Bonding
// * - Central supplies its parameters
// * - We replies with our security parameters
// */
// // ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
// COMMENT_OUT(
// // Change security parameter according to authentication type
// if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
// {
// sec_para.mitm = 1;
// sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
// }
// )
//
// ble_gap_sec_keyset_t keyset =
// {
// .keys_own = {
// .p_enc_key = &_bond_data.own_enc,
// .p_id_key = NULL,
// .p_sign_key = NULL,
// .p_pk = NULL
// },
//
// .keys_peer = {
// .p_enc_key = &_bond_data.peer_enc,
// .p_id_key = &_bond_data.peer_id,
// .p_sign_key = NULL,
// .p_pk = NULL
// }
// };
//
// VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &_sec_param, &keyset), RETURN_VOID);
// break;
/* Step 1: Pairing/Bonding
* - Central supplies its parameters
* - We replies with our security parameters
*/
// ble_gap_sec_params_t* peer = &evt->evt.gap_evt.params.sec_params_request.peer_params;
COMMENT_OUT(
// Change security parameter according to authentication type
if ( _auth_type == BLE_GAP_AUTH_KEY_TYPE_PASSKEY)
{
sec_para.mitm = 1;
sec_para.io_caps = BLE_GAP_IO_CAPS_DISPLAY_ONLY;
}
)
// case BLE_GAP_EVT_SEC_INFO_REQUEST:
// {
// // Reconnection. If bonded previously, Central will ask for stored keys.
// // return security information. Otherwise NULL
// ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
//
// if ( bond_load_keys(sec_request->master_id.ediv, &_bond_data) )
// {
// sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond_data.own_enc.enc_info, &_bond_data.peer_id.id_info, NULL);
// } else
// {
// sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
// }
// }
// break;
ble_gap_sec_keyset_t keyset =
{
.keys_own = {
.p_enc_key = &_bond_data.own_enc,
.p_id_key = NULL,
.p_sign_key = NULL,
.p_pk = NULL
},
.keys_peer = {
.p_enc_key = &_bond_data.peer_enc,
.p_id_key = &_bond_data.peer_id,
.p_sign_key = NULL,
.p_pk = NULL
}
};
VERIFY_STATUS(sd_ble_gap_sec_params_reply(evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &_sec_param, &keyset), RETURN_VOID);
}
break;
case BLE_GAP_EVT_SEC_INFO_REQUEST:
{
// Reconnection. If bonded previously, Central will ask for stored keys.
// return security information. Otherwise NULL
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
if ( bond_load_keys(sec_request->master_id.ediv, &_bond_data) )
{
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &_bond_data.own_enc.enc_info, &_bond_data.peer_id.id_info, NULL);
} else
{
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, NULL, NULL, NULL);
}
}
break;
case BLE_GAP_EVT_PASSKEY_DISPLAY:
{
// ble_gap_evt_passkey_display_t const* passkey_display = &evt->evt.gap_evt.params.passkey_display;
//
// PRINT_INT(passkey_display->match_request);
// PRINT_BUFFER(passkey_display->passkey, 6);
// sd_ble_gap_auth_key_reply
}
break;
case BLE_GAP_EVT_CONN_SEC_UPDATE:
{
// Connection is secured aka Paired
COMMENT_OUT( ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &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(evt_conn_hdl, _bond_data.own_enc.master_id.ediv);
// Consider Paired as Bonded
_bonded = true;
}
break;
case BLE_GAP_EVT_AUTH_STATUS:
{
// Bonding process completed
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
// Pairing/Bonding succeeded --> save encryption keys
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
{
_bonded = true;
bond_save_keys(evt_conn_hdl, &_bond_data);
}else
{
PRINT_HEX(status->auth_status);
}
}
break;
// case BLE_GAP_EVT_CONN_SEC_UPDATE:
// {
// // Connection is secured aka Paired
// COMMENT_OUT( ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &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(evt_conn_hdl, _bond_data.own_enc.master_id.ediv);
//
// // Consider Paired as Bonded
// _bonded = true;
// }
// break;
//
// case BLE_GAP_EVT_AUTH_STATUS:
// {
// // Bonding process completed
// ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
//
// // Pairing/Bonding succeeded --> save encryption keys
// if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
// {
// _bonded = true;
// bond_save_keys(evt_conn_hdl, &_bond_data);
// }else
// {
// PRINT_HEX(status->auth_status);
// }
// }
// break;
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
@@ -1100,19 +1069,7 @@ void AdafruitBluefruit::_setConnLed (bool on_off)
*------------------------------------------------------------------*/
bool AdafruitBluefruit::requestPairing(void)
{
// skip if already bonded
if (_bonded) return true;
VERIFY_STATUS( sd_ble_gap_authenticate(_conn_hdl, &_sec_param ), false);
uint32_t start = millis();
// timeout in 30 seconds
while ( !_bonded && (start + 30000 > millis()) )
{
yield();
}
return _bonded;
return Gap.requestPairing(_conn_hdl);
}
void AdafruitBluefruit::clearBonds(void)
@@ -1122,14 +1079,18 @@ void AdafruitBluefruit::clearBonds(void)
void AdafruitBluefruit::_bledfu_get_bond_data(ble_gap_addr_t* addr, ble_gap_irk_t* irk, ble_gap_enc_key_t* enc_key)
{
if (!_bonded)
{
(*addr) = getPeerAddr();
}else
{
(*addr) = _bond_data.peer_id.id_addr_info;
(*irk) = _bond_data.peer_id.id_info;
(*addr) = getPeerAddr();
(*enc_key) = _bond_data.own_enc;
if ( connPaired() )
{
bond_data_t bdata;
if ( bond_load_keys( Gap._get_peer(_conn_hdl)->ediv, &bdata ) )
{
(*addr) = _bond_data.peer_id.id_addr_info;
(*irk) = _bond_data.peer_id.id_info;
(*enc_key) = _bond_data.own_enc;
}
}
}