improve saving bonding information
add devname into bonding data
This commit is contained in:
1 parent
68d9e7bac5
commit
887d6a6541
9 files changed
+150
-94
No files matched your search
@@ -35,7 +35,6 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "bluefruit.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* BLEAdvertisingData shared between ADV and ScanResponse
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "bluefruit.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
||||
@@ -281,10 +281,8 @@ err_t BLECharacteristic::begin(void)
|
||||
(void) ref_hdl; // not used
|
||||
}
|
||||
|
||||
// Currently Only register to Bluefruit if callback is installed
|
||||
// And The Characteristic must not be temporary memory i.e local variable
|
||||
if ( !_is_temp &&
|
||||
(_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb) )
|
||||
// Currently Only register to Bluefruit if The Characteristic is not temporary memory i.e local variable
|
||||
if ( !_is_temp )
|
||||
{
|
||||
(void) Bluefruit.Gatt._addCharacteristic(this);
|
||||
}
|
||||
@@ -304,11 +302,11 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
|
||||
{
|
||||
ble_gatts_evt_rw_authorize_request_t * authorize_request = &event->evt.gatts_evt.params.authorize_request;
|
||||
|
||||
ble_uuid_t request_uuid = (authorize_request->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) ?
|
||||
authorize_request->request.write.uuid : authorize_request->request.read.uuid;
|
||||
// Handle has the same offset for read & write request
|
||||
uint16_t req_handle = authorize_request->request.read.handle;
|
||||
|
||||
// skip if not our event
|
||||
if ( uuid == request_uuid )
|
||||
if ( _handles.value_handle == req_handle )
|
||||
{
|
||||
if ( (authorize_request->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE) && (_wr_authorize_cb != NULL))
|
||||
{
|
||||
@@ -334,11 +332,23 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
|
||||
}
|
||||
|
||||
// CCCD write
|
||||
if ( _cccd_wr_cb && (request->handle == _handles.cccd_handle) )
|
||||
if ( request->handle == _handles.cccd_handle )
|
||||
{
|
||||
uint16_t value;
|
||||
memcpy(&value, request->data, 2);
|
||||
_cccd_wr_cb(*this, value);
|
||||
// Invoke callback if set
|
||||
if (_cccd_wr_cb)
|
||||
{
|
||||
uint16_t value;
|
||||
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_defer(NULL, _adafruit_save_bond_cccd_dfr, event->evt.common_evt.conn_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "bluefruit.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
#define MAX_DESCIRPTORS 8
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "bluefruit.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
BLEGap::BLEGap(void)
|
||||
{
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "bluefruit.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
BLEScanner::BLEScanner(void)
|
||||
{
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
/**************************************************************************/
|
||||
|
||||
#include "bluefruit.h"
|
||||
#include "utility/AdaCallback.h"
|
||||
#include <Nffs.h>
|
||||
|
||||
#define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
|
||||
@@ -64,6 +63,8 @@ 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
|
||||
@@ -215,7 +216,6 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
|
||||
|
||||
ada_callback_init();
|
||||
|
||||
|
||||
NVIC_SetPriority(SD_EVT_IRQn, 6);
|
||||
NVIC_EnableIRQ(SD_EVT_IRQn);
|
||||
|
||||
@@ -533,6 +533,12 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
* or a few special one
|
||||
* - Connected event
|
||||
* - Advertising timeout (could be connected and advertising at the same time)
|
||||
*
|
||||
* Pairing procedure
|
||||
* - Connect -> SEC_PARAMS_REQUEST -> CONN_SEC_UPDATE -> AUTH_STATUS
|
||||
*
|
||||
* Reconnect to a paired device
|
||||
* - Connect -> SEC_INFO_REQUEST -> CONN_SEC_UPDATE
|
||||
*/
|
||||
if ( evt_conn_hdl == _conn_hdl ||
|
||||
evt->header.evt_id == BLE_GAP_EVT_CONNECTED ||
|
||||
@@ -572,10 +578,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_DISCONNECTED:
|
||||
// Note callback is invoked by BLEGap
|
||||
|
||||
// Save all configured cccd
|
||||
if (_bonded) _saveBondedCCCD();
|
||||
if (_bonded) _saveBondCCCD();
|
||||
|
||||
if (_disconnect_cb) ada_callback(NULL, _disconnect_cb, _conn_hdl);
|
||||
|
||||
@@ -660,7 +664,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
// Previously bonded --> secure by re-connection process
|
||||
// --> Load & Set Sys Attr (Apply Service Context)
|
||||
// Else Init Sys Attr
|
||||
_loadBondedCCCD(_bond_data.own_enc.master_id.ediv);
|
||||
_loadBondCCCD(_bond_data.own_enc.master_id.ediv);
|
||||
|
||||
// Consider Paired as Bonded
|
||||
_bonded = true;
|
||||
@@ -675,8 +679,8 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
|
||||
// Pairing/Bonding succeeded --> save encryption keys
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status)
|
||||
{
|
||||
_saveBondKeys();
|
||||
_bonded = true;
|
||||
ada_callback_defer(NULL, _adafruit_save_bond_key_dfr, _conn_hdl);
|
||||
}else
|
||||
{
|
||||
PRINT_HEX(status->auth_status);
|
||||
@@ -762,21 +766,61 @@ void AdafruitBluefruit::clearBonds(void)
|
||||
printBondDir();
|
||||
}
|
||||
|
||||
bool AdafruitBluefruit::_saveBondKeys(void)
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Saving Bond Data to Nffs in following layout
|
||||
* - _bond_data 80 bytes
|
||||
* - Name 32 bytes
|
||||
* - CCCD variable
|
||||
*------------------------------------------------------------------*/
|
||||
void _adafruit_save_bond_key_dfr(uint32_t conn_handle)
|
||||
{
|
||||
(void) conn_handle;
|
||||
Bluefruit._saveBondKeys();
|
||||
}
|
||||
|
||||
void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
|
||||
{
|
||||
(void) conn_handle;
|
||||
Bluefruit._saveBondCCCD();
|
||||
}
|
||||
|
||||
void AdafruitBluefruit::_saveBondKeys(void)
|
||||
{
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
|
||||
|
||||
VERIFY( Nffs.writeFile(filename, &_bond_data, sizeof(_bond_data)) );
|
||||
LOG_LV1(BOND, "Keys is saved to Nffs");
|
||||
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
|
||||
Gap.getPeerName(_conn_hdl, devname, CFG_MAX_DEVNAME_LEN);
|
||||
PRINT_STR(devname);
|
||||
|
||||
NffsFile file(filename, FS_ACCESS_WRITE);
|
||||
|
||||
VERIFY( file.exists(), );
|
||||
|
||||
bool result = true;
|
||||
|
||||
// write keys
|
||||
if ( !file.write((uint8_t*)&_bond_data, sizeof(_bond_data)) )
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
// write device name
|
||||
if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) )
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
LOG_LV1(BOND, result ? "Keys is saved to Nffs" : "Failed to save");
|
||||
printBondDir();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv)
|
||||
{
|
||||
VERIFY_STATIC(sizeof(_bond_data) == 80 );
|
||||
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME, ediv);
|
||||
|
||||
@@ -793,51 +837,9 @@ bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv)
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void AdafruitBluefruit::_loadBondedCCCD(uint16_t ediv)
|
||||
void AdafruitBluefruit::_saveBondCCCD(void)
|
||||
{
|
||||
bool loaded = false;
|
||||
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME "_cccd", ediv);
|
||||
|
||||
NffsFile file(filename, FS_ACCESS_READ);
|
||||
|
||||
if ( file.exists() )
|
||||
{
|
||||
uint16_t len = file.size();
|
||||
uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
|
||||
|
||||
if (sys_attr)
|
||||
{
|
||||
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_LV1(BOND, "Load CCCD from %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::_saveBondedCCCD(void)
|
||||
{
|
||||
if ( _bond_data.own_enc.master_id.ediv == 0xFFFF ) return;
|
||||
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);
|
||||
@@ -849,11 +851,16 @@ void AdafruitBluefruit::_saveBondedCCCD(void)
|
||||
{
|
||||
// save to file
|
||||
char filename[BOND_FILENAME_LEN];
|
||||
sprintf(filename, BOND_FILENAME "_cccd", _bond_data.own_enc.master_id.ediv);
|
||||
sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
|
||||
|
||||
Nffs.writeFile(filename, sys_attr, len);
|
||||
if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
|
||||
{
|
||||
LOG_LV1(BOND, "CCCD setting is saved to Nffs");
|
||||
}else
|
||||
{
|
||||
LOG_LV1(BOND, "Failed to save CCCD setting");
|
||||
}
|
||||
|
||||
LOG_LV1(BOND, "CCCD setting is saved to Nffs");
|
||||
}
|
||||
|
||||
printBondDir();
|
||||
@@ -861,6 +868,53 @@ void AdafruitBluefruit::_saveBondedCCCD(void)
|
||||
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_LV1(BOND, "Load CCCD from %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)
|
||||
{
|
||||
if (!_bonded)
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include "bluefruit_common.h"
|
||||
|
||||
#define CFG_ADV_BLINKY_INTERVAL 500
|
||||
#define CFG_MAX_DEVNAME_LEN 32
|
||||
|
||||
// Note chaning these parameters will affect APP_RAM_BASE
|
||||
// --> need to update RAM in feather52_s132.ld linker
|
||||
@@ -79,6 +80,8 @@
|
||||
#include "clients/BLEClientDis.h"
|
||||
#include "clients/BLEClientCts.h"
|
||||
|
||||
#include "utility/AdaCallback.h"
|
||||
|
||||
#define BLE_MAX_DATA_PER_MTU (GATT_MTU_SIZE_DEFAULT - 3)
|
||||
|
||||
extern "C"
|
||||
@@ -157,6 +160,9 @@ 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);
|
||||
|
||||
private:
|
||||
/*------------- BLE para -------------*/
|
||||
bool _prph_enabled;
|
||||
@@ -185,15 +191,17 @@ class AdafruitBluefruit
|
||||
BLEGap::connect_callback_t _connect_cb;
|
||||
BLEGap::disconnect_callback_t _disconnect_cb;
|
||||
|
||||
ble_gap_sec_params_t _sec_param;
|
||||
|
||||
// Shared keys with bonded device, size = 80 bytes
|
||||
struct
|
||||
{
|
||||
// Keys
|
||||
ble_gap_enc_key_t own_enc;
|
||||
ble_gap_enc_key_t peer_enc;
|
||||
ble_gap_id_key_t peer_id;
|
||||
} _bond_data;
|
||||
|
||||
ble_gap_sec_params_t _sec_param;
|
||||
enum { BOND_FILE_CCCD_OFFSET = sizeof(_bond_data) + CFG_MAX_DEVNAME_LEN };
|
||||
|
||||
COMMENT_OUT(
|
||||
uint8_t _auth_type;
|
||||
@@ -203,12 +211,8 @@ COMMENT_OUT(
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
*------------------------------------------------------------------*/
|
||||
bool _saveBondKeys(void);
|
||||
bool _loadBondKeys(uint16_t ediv);
|
||||
|
||||
void _saveBondedCCCD(void);
|
||||
void _loadBondedCCCD(uint16_t ediv);
|
||||
|
||||
void _loadBondCCCD(uint16_t ediv);
|
||||
void _ble_handler(ble_evt_t* evt);
|
||||
|
||||
friend void SD_EVT_IRQHandler(void);
|
||||
|
||||
@@ -61,8 +61,8 @@ typedef void (*adacb_worker_0arg_t) (void);
|
||||
typedef void (*adacb_worker_1arg_t) (uint32_t);
|
||||
typedef void (*adacb_worker_2arg_t) (uint32_t, uint32_t);
|
||||
typedef void (*adacb_worker_3arg_t) (uint32_t, uint32_t, uint32_t);
|
||||
typedef void (*adacb_worker_4arg_t) (uint32_t, uint32_t, uint32_t);
|
||||
typedef void (*adacb_worker_5arg_t) (uint32_t, uint32_t, uint32_t);
|
||||
typedef void (*adacb_worker_4arg_t) (uint32_t, uint32_t, uint32_t, uint32_t);
|
||||
typedef void (*adacb_worker_5arg_t) (uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* X Macros expansion for callback function
|
||||
@@ -124,25 +124,18 @@ ADA_CB_LOOKUP(ADA_CB_FUNC_XPAND)
|
||||
uint32_t arguments[] = { _ADA_CB_ARGS(__VA_ARGS__) };\
|
||||
memcpy(cb_data->arguments, arguments, 4*_count);\
|
||||
}\
|
||||
ada_callback_queue(cb_data);\
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
* Macro function is called by other module with all intended parameters.
|
||||
* The first parameter is malloced Pointer (NULL if not), so that callback could know to free memory
|
||||
*/
|
||||
#define ada_callback(_malloced, _func , ... ) \
|
||||
do { \
|
||||
_ada_cb_setup(_malloced, _func, ada_callback_type(_func), __VA_ARGS__);\
|
||||
ada_callback_queue(cb_data);\
|
||||
}while(0)
|
||||
#define ada_callback(_malloced, _func , ... ) _ada_cb_setup(_malloced, _func, ada_callback_type(_func), __VA_ARGS__)
|
||||
#define ada_callback_defer(_malloced, _func, ...) _ada_cb_setup(_malloced, _func, ADA_CB_DEFERRED_FUNC , __VA_ARGS__)
|
||||
|
||||
void ada_callback_init(void);
|
||||
void ada_callback_queue(ada_callback_t* cb_data);
|
||||
|
||||
#define ada_callback_defer(_malloced, _func, ...) \
|
||||
do { \
|
||||
_ada_cb_setup(_malloced, _func, ADA_CB_DEFERRED_FUNC, __VA_ARGS__);\
|
||||
ada_callback_queue(cb_data);\
|
||||
} while(0)
|
||||
|
||||
#endif /* ADACALLBACK_H_ */
|
||||
Reference in new issue
Block a user