complete move all bond function to bonding.cpp
This commit is contained in:
1 parent
4fab39f26a
commit
dcbecb8b5f
6 files changed
+219
-144
No files matched your search
@@ -406,14 +406,6 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
|
||||
memcpy(&value, request->data, 2);
|
||||
_cccd_wr_cb(*this, value);
|
||||
}
|
||||
|
||||
// TODO could move later
|
||||
// Save CCCD if bonded to file whenever changed
|
||||
extern void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle);
|
||||
if ( Bluefruit.connPaired() )
|
||||
{
|
||||
ada_callback(NULL, _adafruit_save_bond_cccd_dfr, conn_hdl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -394,14 +394,6 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
|
||||
memcpy(&value, request->data, 2);
|
||||
_cccd_wr_cb(*this, value);
|
||||
}
|
||||
|
||||
// TODO could move later
|
||||
// Save CCCD if bonded to file whenever changed
|
||||
extern void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle);
|
||||
if ( Bluefruit.connPaired() )
|
||||
{
|
||||
ada_callback(NULL, _adafruit_save_bond_cccd_dfr, event->evt.common_evt.conn_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "bluefruit.h"
|
||||
#include "utility/bonding.h"
|
||||
|
||||
|
||||
BLEGatt::BLEGatt(void)
|
||||
@@ -95,7 +96,8 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle;
|
||||
const uint16_t evt_id = evt->header.evt_id;
|
||||
|
||||
// Server Service TODO multiple peripherals
|
||||
/*------------- Server service -------------*/
|
||||
// TODO multiple peripherals
|
||||
// if ( evt_conn_hdl == Bluefruit.connHandle() )
|
||||
{
|
||||
if ( evt_id == BLE_GAP_EVT_DISCONNECTED || evt_id == BLE_GAP_EVT_CONNECTED )
|
||||
@@ -113,7 +115,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
}
|
||||
}
|
||||
|
||||
// Server Characteristics
|
||||
/*------------- Server Characteristics -------------*/
|
||||
// TODO multiple prph connection
|
||||
if ( evt_conn_hdl == Bluefruit.connHandle() )
|
||||
{
|
||||
@@ -127,11 +129,11 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
|
||||
// Handle has the same offset for read & write request
|
||||
req_handle = evt->evt.gatts_evt.params.authorize_request.request.read.handle;
|
||||
break;
|
||||
break;
|
||||
|
||||
case BLE_GATTS_EVT_WRITE:
|
||||
req_handle = evt->evt.gatts_evt.params.write.handle;
|
||||
break;
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
@@ -140,11 +142,17 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
if ((req_handle != BLE_GATT_HANDLE_INVALID) && (req_handle == chr->handles().value_handle || req_handle == chr->handles().cccd_handle ))
|
||||
{
|
||||
chr->_eventHandler(evt);
|
||||
|
||||
// Save CCCD if paired
|
||||
if ( Bluefruit.connPaired() && (evt_id == BLE_GATTS_EVT_WRITE) && (req_handle == chr->handles().cccd_handle) )
|
||||
{
|
||||
bond_save_cccd(evt_conn_hdl, Bluefruit._bond_data.own_enc.master_id.ediv);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Client Characteristics
|
||||
/*------------- Client Characteristics -------------*/
|
||||
for(int i=0; i<_client.chr_count; i++)
|
||||
{
|
||||
if ( evt_conn_hdl == _client.chr_list[i]->connHandle() )
|
||||
|
||||
@@ -64,8 +64,6 @@ extern "C"
|
||||
void adafruit_ble_task(void* arg);
|
||||
void adafruit_soc_task(void* arg);
|
||||
|
||||
void _adafruit_save_bond_key_dfr(uint32_t conn_handle);
|
||||
|
||||
#if CFG_DEBUG >= 2
|
||||
#define printBondDir() dbgPrintDir(CFG_BOND_NFFS_DIR)
|
||||
#else
|
||||
@@ -954,9 +952,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
// Save all configured cccd
|
||||
if (_bonded) _saveBondCCCD();
|
||||
|
||||
if (_disconnect_cb) ada_callback(NULL, _disconnect_cb, _conn_hdl, evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
LOG_LV2("GAP", "Disconnect Reason 0x%02X", evt->evt.gap_evt.params.disconnected.reason);
|
||||
@@ -1012,7 +1007,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
// 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 ( _loadBondKeys(sec_request->master_id.ediv) )
|
||||
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
|
||||
@@ -1039,10 +1034,10 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
// 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
|
||||
_loadBondCCCD(_bond_data.own_enc.master_id.ediv);
|
||||
// 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;
|
||||
@@ -1157,11 +1152,11 @@ void AdafruitBluefruit::clearBonds(void)
|
||||
// Bluefruit._saveBondKeys();
|
||||
//}
|
||||
|
||||
void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
|
||||
{
|
||||
(void) conn_handle;
|
||||
Bluefruit._saveBondCCCD();
|
||||
}
|
||||
//void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
|
||||
//{
|
||||
// (void) conn_handle;
|
||||
// Bluefruit._saveBondCCCD();
|
||||
//}
|
||||
|
||||
//void AdafruitBluefruit::_saveBondKeys(void)
|
||||
//{
|
||||
@@ -1201,103 +1196,103 @@ void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
|
||||
// printBondDir();
|
||||
//}
|
||||
|
||||
bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv)
|
||||
{
|
||||
VERIFY_STATIC(sizeof(_bond_data) == 80 );
|
||||
//bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv)
|
||||
//{
|
||||
// VERIFY_STATIC(sizeof(_bond_data) == 80 );
|
||||
//
|
||||
// char filename[BOND_FILENAME_LEN];
|
||||
// sprintf(filename, BOND_FILENAME, ediv);
|
||||
//
|
||||
// bool result = (Nffs.readFile(filename, &_bond_data, sizeof(_bond_data)) > 0);
|
||||
//
|
||||
// if ( result )
|
||||
// {
|
||||
// LOG_LV2("BOND", "Load Keys from file %s", filename);
|
||||
// }else
|
||||
// {
|
||||
// LOG_LV1("BOND", "Keys not found");
|
||||
// }
|
||||
//
|
||||
// return result;
|
||||
//}
|
||||
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, ediv);
|
||||
//void AdafruitBluefruit::_saveBondCCCD(void)
|
||||
//{
|
||||
// VERIFY( _bond_data.own_enc.master_id.ediv != 0xFFFF, );
|
||||
//
|
||||
// uint16_t len=0;
|
||||
// sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
|
||||
//
|
||||
// uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
|
||||
// VERIFY( sys_attr, );
|
||||
//
|
||||
// if ( ERROR_NONE == sd_ble_gatts_sys_attr_get(_conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG) )
|
||||
// {
|
||||
// // save to file
|
||||
// char filename[BOND_FILENAME_LEN];
|
||||
// sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
|
||||
//
|
||||
// if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
|
||||
// {
|
||||
// LOG_LV2("BOND", "CCCD setting is saved to file %s", filename);
|
||||
// }else
|
||||
// {
|
||||
// LOG_LV1("BOND", "Failed to save CCCD setting");
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// printBondDir();
|
||||
//
|
||||
// rtos_free(sys_attr);
|
||||
//}
|
||||
|
||||
bool result = (Nffs.readFile(filename, &_bond_data, sizeof(_bond_data)) > 0);
|
||||
|
||||
if ( result )
|
||||
{
|
||||
LOG_LV2("BOND", "Load Keys from file %s", filename);
|
||||
}else
|
||||
{
|
||||
LOG_LV1("BOND", "Keys not found");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::_saveBondCCCD(void)
|
||||
{
|
||||
VERIFY( _bond_data.own_enc.master_id.ediv != 0xFFFF, );
|
||||
|
||||
uint16_t len=0;
|
||||
sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
|
||||
|
||||
uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
|
||||
VERIFY( sys_attr, );
|
||||
|
||||
if ( ERROR_NONE == sd_ble_gatts_sys_attr_get(_conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG) )
|
||||
{
|
||||
// save to file
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
|
||||
|
||||
if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
|
||||
{
|
||||
LOG_LV2("BOND", "CCCD setting is saved to file %s", filename);
|
||||
}else
|
||||
{
|
||||
LOG_LV1("BOND", "Failed to save CCCD setting");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printBondDir();
|
||||
|
||||
rtos_free(sys_attr);
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::_loadBondCCCD(uint16_t ediv)
|
||||
{
|
||||
bool loaded = false;
|
||||
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, ediv);
|
||||
|
||||
NffsFile file(filename, FS_ACCESS_READ);
|
||||
|
||||
if ( file.exists() )
|
||||
{
|
||||
int32_t len = file.size() - BOND_FILE_CCCD_OFFSET;
|
||||
|
||||
if ( len )
|
||||
{
|
||||
uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
|
||||
|
||||
if (sys_attr)
|
||||
{
|
||||
file.seek(BOND_FILE_CCCD_OFFSET);
|
||||
|
||||
if ( file.read(sys_attr, len ) )
|
||||
{
|
||||
if (ERROR_NONE == sd_ble_gatts_sys_attr_set(_conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) )
|
||||
{
|
||||
loaded = true;
|
||||
|
||||
LOG_LV2("BOND", "Load CCCD from file %s", filename);
|
||||
}else
|
||||
{
|
||||
LOG_LV1("BOND", "CCCD setting not found");
|
||||
}
|
||||
}
|
||||
|
||||
rtos_free(sys_attr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
if ( !loaded )
|
||||
{
|
||||
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
|
||||
}
|
||||
}
|
||||
//void AdafruitBluefruit::_loadBondCCCD(uint16_t ediv)
|
||||
//{
|
||||
// bool loaded = false;
|
||||
//
|
||||
// char filename[BOND_FILENAME_LEN];
|
||||
// sprintf(filename, BOND_FILENAME, ediv);
|
||||
//
|
||||
// NffsFile file(filename, FS_ACCESS_READ);
|
||||
//
|
||||
// if ( file.exists() )
|
||||
// {
|
||||
// int32_t len = file.size() - BOND_FILE_CCCD_OFFSET;
|
||||
//
|
||||
// if ( len )
|
||||
// {
|
||||
// uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
|
||||
//
|
||||
// if (sys_attr)
|
||||
// {
|
||||
// file.seek(BOND_FILE_CCCD_OFFSET);
|
||||
//
|
||||
// if ( file.read(sys_attr, len ) )
|
||||
// {
|
||||
// if (ERROR_NONE == sd_ble_gatts_sys_attr_set(_conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) )
|
||||
// {
|
||||
// loaded = true;
|
||||
//
|
||||
// LOG_LV2("BOND", "Load CCCD from file %s", filename);
|
||||
// }else
|
||||
// {
|
||||
// LOG_LV1("BOND", "CCCD setting not found");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// rtos_free(sys_attr);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// file.close();
|
||||
//
|
||||
// if ( !loaded )
|
||||
// {
|
||||
// sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
|
||||
// }
|
||||
//}
|
||||
|
||||
void AdafruitBluefruit::_bledfu_get_bond_data(ble_gap_addr_t* addr, ble_gap_irk_t* irk, ble_gap_enc_key_t* enc_key)
|
||||
{
|
||||
|
||||
@@ -196,8 +196,16 @@ class AdafruitBluefruit
|
||||
void _setConnLed (bool on_off);
|
||||
void _bledfu_get_bond_data(ble_gap_addr_t* addr, ble_gap_irk_t* irk, ble_gap_enc_key_t* enc_key);
|
||||
|
||||
void _saveBondKeys(void);
|
||||
void _saveBondCCCD(void);
|
||||
// void _saveBondKeys(void);
|
||||
// void _saveBondCCCD(void);
|
||||
|
||||
// Shared keys with bonded device, size = 80 bytes
|
||||
bond_data_t _bond_data;
|
||||
enum
|
||||
{
|
||||
BOND_FILE_DEVNAME_OFFSET = sizeof(_bond_data),
|
||||
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
|
||||
};
|
||||
|
||||
private:
|
||||
/*------------- SoftDevice Configuration -------------*/
|
||||
@@ -235,15 +243,6 @@ class AdafruitBluefruit
|
||||
|
||||
ble_gap_sec_params_t _sec_param;
|
||||
|
||||
// Shared keys with bonded device, size = 80 bytes
|
||||
bond_data_t _bond_data;
|
||||
|
||||
enum
|
||||
{
|
||||
BOND_FILE_DEVNAME_OFFSET = sizeof(_bond_data),
|
||||
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
|
||||
};
|
||||
|
||||
COMMENT_OUT(
|
||||
uint8_t _auth_type;
|
||||
char _pin[BLE_GAP_PASSKEY_LEN];
|
||||
@@ -252,8 +251,8 @@ COMMENT_OUT(
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
*------------------------------------------------------------------*/
|
||||
bool _loadBondKeys(uint16_t ediv);
|
||||
void _loadBondCCCD(uint16_t ediv);
|
||||
// bool _loadBondKeys(uint16_t ediv);
|
||||
// void _loadBondCCCD(uint16_t ediv);
|
||||
void _ble_handler(ble_evt_t* evt);
|
||||
|
||||
friend void SD_EVT_IRQHandler(void);
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
#define printBondDir()
|
||||
#endif
|
||||
|
||||
#define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
@@ -104,17 +105,105 @@ void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata)
|
||||
ada_callback(buf, bond_save_keys_dfr, conn_hdl, buf);
|
||||
}
|
||||
|
||||
static void bond_save_cccd_dfr (uint16_t conn_hdl, uint16_t ediv)
|
||||
{
|
||||
uint16_t len=0;
|
||||
sd_ble_gatts_sys_attr_get(conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
|
||||
|
||||
uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
|
||||
VERIFY( sys_attr, );
|
||||
|
||||
if ( ERROR_NONE == sd_ble_gatts_sys_attr_get(conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG) )
|
||||
{
|
||||
// save to file
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, ediv);
|
||||
|
||||
if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
|
||||
{
|
||||
LOG_LV2("BOND", "CCCD setting is saved to file %s", filename);
|
||||
}else
|
||||
{
|
||||
LOG_LV1("BOND", "Failed to save CCCD setting");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
rtos_free(sys_attr);
|
||||
printBondDir();
|
||||
}
|
||||
|
||||
void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv)
|
||||
{
|
||||
VERIFY( ediv != 0xFFFF, );
|
||||
|
||||
// queue to execute in Ada Callback thread
|
||||
ada_callback(NULL, bond_save_cccd_dfr, cond_hdl, ediv);
|
||||
}
|
||||
|
||||
bool bond_load_keys(uint16_t ediv, bond_data_t* bdata)
|
||||
{
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, ediv);
|
||||
|
||||
bool result = (Nffs.readFile(filename, bdata, sizeof(bond_data_t)) > 0);
|
||||
|
||||
if ( result )
|
||||
{
|
||||
LOG_LV2("BOND", "Load Keys from file %s", filename);
|
||||
}else
|
||||
{
|
||||
LOG_LV1("BOND", "Keys not found");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv)
|
||||
{
|
||||
bool loaded = false;
|
||||
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, ediv);
|
||||
|
||||
NffsFile file(filename, FS_ACCESS_READ);
|
||||
|
||||
if ( file.exists() )
|
||||
{
|
||||
int32_t len = file.size() - BOND_FILE_CCCD_OFFSET;
|
||||
|
||||
if ( len )
|
||||
{
|
||||
uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
|
||||
|
||||
if (sys_attr)
|
||||
{
|
||||
file.seek(BOND_FILE_CCCD_OFFSET);
|
||||
|
||||
if ( file.read(sys_attr, len ) )
|
||||
{
|
||||
if (ERROR_NONE == sd_ble_gatts_sys_attr_set(cond_hdl, sys_attr, len, SVC_CONTEXT_FLAG) )
|
||||
{
|
||||
loaded = true;
|
||||
|
||||
LOG_LV2("BOND", "Load CCCD from file %s", filename);
|
||||
}else
|
||||
{
|
||||
LOG_LV1("BOND", "CCCD setting not found");
|
||||
}
|
||||
}
|
||||
|
||||
rtos_free(sys_attr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
if ( !loaded )
|
||||
{
|
||||
sd_ble_gatts_sys_attr_set(cond_hdl, NULL, 0, 0);
|
||||
}
|
||||
|
||||
return loaded;
|
||||
}
|
||||
Reference in new issue
Block a user