Skip to content

Commit dcbecb8

Browse files
committed
complete move all bond function to bonding.cpp
1 parent 4fab39f commit dcbecb8

File tree

6 files changed

+219
-144
lines changed

6 files changed

+219
-144
lines changed

libraries/BLEHomekit/src/HAPCharacteristic.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,6 @@ void HAPCharacteristic::_eventHandler(ble_evt_t* event)
406406
memcpy(&value, request->data, 2);
407407
_cccd_wr_cb(*this, value);
408408
}
409-
410-
// TODO could move later
411-
// Save CCCD if bonded to file whenever changed
412-
extern void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle);
413-
if ( Bluefruit.connPaired() )
414-
{
415-
ada_callback(NULL, _adafruit_save_bond_cccd_dfr, conn_hdl);
416-
}
417409
}
418410
}
419411

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,6 @@ void BLECharacteristic::_eventHandler(ble_evt_t* event)
394394
memcpy(&value, request->data, 2);
395395
_cccd_wr_cb(*this, value);
396396
}
397-
398-
// TODO could move later
399-
// Save CCCD if bonded to file whenever changed
400-
extern void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle);
401-
if ( Bluefruit.connPaired() )
402-
{
403-
ada_callback(NULL, _adafruit_save_bond_cccd_dfr, event->evt.common_evt.conn_handle);
404-
}
405397
}
406398
}
407399
break;

libraries/Bluefruit52Lib/src/BLEGatt.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
/**************************************************************************/
3636

3737
#include "bluefruit.h"
38+
#include "utility/bonding.h"
3839

3940

4041
BLEGatt::BLEGatt(void)
@@ -95,7 +96,8 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
9596
const uint16_t evt_conn_hdl = evt->evt.common_evt.conn_handle;
9697
const uint16_t evt_id = evt->header.evt_id;
9798

98-
// Server Service TODO multiple peripherals
99+
/*------------- Server service -------------*/
100+
// TODO multiple peripherals
99101
// if ( evt_conn_hdl == Bluefruit.connHandle() )
100102
{
101103
if ( evt_id == BLE_GAP_EVT_DISCONNECTED || evt_id == BLE_GAP_EVT_CONNECTED )
@@ -113,7 +115,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
113115
}
114116
}
115117

116-
// Server Characteristics
118+
/*------------- Server Characteristics -------------*/
117119
// TODO multiple prph connection
118120
if ( evt_conn_hdl == Bluefruit.connHandle() )
119121
{
@@ -127,11 +129,11 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
127129
case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
128130
// Handle has the same offset for read & write request
129131
req_handle = evt->evt.gatts_evt.params.authorize_request.request.read.handle;
130-
break;
132+
break;
131133

132134
case BLE_GATTS_EVT_WRITE:
133135
req_handle = evt->evt.gatts_evt.params.write.handle;
134-
break;
136+
break;
135137

136138
default: break;
137139
}
@@ -140,11 +142,17 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
140142
if ((req_handle != BLE_GATT_HANDLE_INVALID) && (req_handle == chr->handles().value_handle || req_handle == chr->handles().cccd_handle ))
141143
{
142144
chr->_eventHandler(evt);
145+
146+
// Save CCCD if paired
147+
if ( Bluefruit.connPaired() && (evt_id == BLE_GATTS_EVT_WRITE) && (req_handle == chr->handles().cccd_handle) )
148+
{
149+
bond_save_cccd(evt_conn_hdl, Bluefruit._bond_data.own_enc.master_id.ediv);
150+
}
143151
}
144152
}
145153
}
146154

147-
// Client Characteristics
155+
/*------------- Client Characteristics -------------*/
148156
for(int i=0; i<_client.chr_count; i++)
149157
{
150158
if ( evt_conn_hdl == _client.chr_list[i]->connHandle() )

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 105 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ extern "C"
6464
void adafruit_ble_task(void* arg);
6565
void adafruit_soc_task(void* arg);
6666

67-
void _adafruit_save_bond_key_dfr(uint32_t conn_handle);
68-
6967
#if CFG_DEBUG >= 2
7068
#define printBondDir() dbgPrintDir(CFG_BOND_NFFS_DIR)
7169
#else
@@ -954,9 +952,6 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
954952
break;
955953

956954
case BLE_GAP_EVT_DISCONNECTED:
957-
// Save all configured cccd
958-
if (_bonded) _saveBondCCCD();
959-
960955
if (_disconnect_cb) ada_callback(NULL, _disconnect_cb, _conn_hdl, evt->evt.gap_evt.params.disconnected.reason);
961956

962957
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)
10121007
// return security information. Otherwise NULL
10131008
ble_gap_evt_sec_info_request_t* sec_request = (ble_gap_evt_sec_info_request_t*) &evt->evt.gap_evt.params.sec_info_request;
10141009

1015-
if ( _loadBondKeys(sec_request->master_id.ediv) )
1010+
if ( bond_load_keys(sec_request->master_id.ediv, &_bond_data) )
10161011
{
10171012
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);
10181013
} else
@@ -1039,10 +1034,10 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
10391034
// Connection is secured aka Paired
10401035
COMMENT_OUT( ble_gap_conn_sec_t* conn_sec = (ble_gap_conn_sec_t*) &evt->evt.gap_evt.params.conn_sec_update.conn_sec; )
10411036

1042-
// Previously bonded --> secure by re-connection process
1043-
// --> Load & Set Sys Attr (Apply Service Context)
1044-
// Else Init Sys Attr
1045-
_loadBondCCCD(_bond_data.own_enc.master_id.ediv);
1037+
// Previously bonded --> secure by re-connection process
1038+
// --> Load & Set Sys Attr (Apply Service Context)
1039+
// Else Init Sys Attr
1040+
bond_load_cccd(evt_conn_hdl, _bond_data.own_enc.master_id.ediv);
10461041

10471042
// Consider Paired as Bonded
10481043
_bonded = true;
@@ -1157,11 +1152,11 @@ void AdafruitBluefruit::clearBonds(void)
11571152
// Bluefruit._saveBondKeys();
11581153
//}
11591154

1160-
void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
1161-
{
1162-
(void) conn_handle;
1163-
Bluefruit._saveBondCCCD();
1164-
}
1155+
//void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
1156+
//{
1157+
// (void) conn_handle;
1158+
// Bluefruit._saveBondCCCD();
1159+
//}
11651160

11661161
//void AdafruitBluefruit::_saveBondKeys(void)
11671162
//{
@@ -1201,103 +1196,103 @@ void _adafruit_save_bond_cccd_dfr(uint32_t conn_handle)
12011196
// printBondDir();
12021197
//}
12031198

1204-
bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv)
1205-
{
1206-
VERIFY_STATIC(sizeof(_bond_data) == 80 );
1207-
1208-
char filename[BOND_FILENAME_LEN];
1209-
sprintf(filename, BOND_FILENAME, ediv);
1210-
1211-
bool result = (Nffs.readFile(filename, &_bond_data, sizeof(_bond_data)) > 0);
1212-
1213-
if ( result )
1214-
{
1215-
LOG_LV2("BOND", "Load Keys from file %s", filename);
1216-
}else
1217-
{
1218-
LOG_LV1("BOND", "Keys not found");
1219-
}
1220-
1221-
return result;
1222-
}
1223-
1224-
void AdafruitBluefruit::_saveBondCCCD(void)
1225-
{
1226-
VERIFY( _bond_data.own_enc.master_id.ediv != 0xFFFF, );
1227-
1228-
uint16_t len=0;
1229-
sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
1230-
1231-
uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
1232-
VERIFY( sys_attr, );
1233-
1234-
if ( ERROR_NONE == sd_ble_gatts_sys_attr_get(_conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG) )
1235-
{
1236-
// save to file
1237-
char filename[BOND_FILENAME_LEN];
1238-
sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
1239-
1240-
if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
1241-
{
1242-
LOG_LV2("BOND", "CCCD setting is saved to file %s", filename);
1243-
}else
1244-
{
1245-
LOG_LV1("BOND", "Failed to save CCCD setting");
1246-
}
1247-
1248-
}
1249-
1250-
printBondDir();
1251-
1252-
rtos_free(sys_attr);
1253-
}
1254-
1255-
void AdafruitBluefruit::_loadBondCCCD(uint16_t ediv)
1256-
{
1257-
bool loaded = false;
1258-
1259-
char filename[BOND_FILENAME_LEN];
1260-
sprintf(filename, BOND_FILENAME, ediv);
1261-
1262-
NffsFile file(filename, FS_ACCESS_READ);
1263-
1264-
if ( file.exists() )
1265-
{
1266-
int32_t len = file.size() - BOND_FILE_CCCD_OFFSET;
1267-
1268-
if ( len )
1269-
{
1270-
uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
1271-
1272-
if (sys_attr)
1273-
{
1274-
file.seek(BOND_FILE_CCCD_OFFSET);
1275-
1276-
if ( file.read(sys_attr, len ) )
1277-
{
1278-
if (ERROR_NONE == sd_ble_gatts_sys_attr_set(_conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) )
1279-
{
1280-
loaded = true;
1281-
1282-
LOG_LV2("BOND", "Load CCCD from file %s", filename);
1283-
}else
1284-
{
1285-
LOG_LV1("BOND", "CCCD setting not found");
1286-
}
1287-
}
1288-
1289-
rtos_free(sys_attr);
1290-
}
1291-
}
1292-
}
1199+
//bool AdafruitBluefruit::_loadBondKeys(uint16_t ediv)
1200+
//{
1201+
// VERIFY_STATIC(sizeof(_bond_data) == 80 );
1202+
//
1203+
// char filename[BOND_FILENAME_LEN];
1204+
// sprintf(filename, BOND_FILENAME, ediv);
1205+
//
1206+
// bool result = (Nffs.readFile(filename, &_bond_data, sizeof(_bond_data)) > 0);
1207+
//
1208+
// if ( result )
1209+
// {
1210+
// LOG_LV2("BOND", "Load Keys from file %s", filename);
1211+
// }else
1212+
// {
1213+
// LOG_LV1("BOND", "Keys not found");
1214+
// }
1215+
//
1216+
// return result;
1217+
//}
12931218

1294-
file.close();
1219+
//void AdafruitBluefruit::_saveBondCCCD(void)
1220+
//{
1221+
// VERIFY( _bond_data.own_enc.master_id.ediv != 0xFFFF, );
1222+
//
1223+
// uint16_t len=0;
1224+
// sd_ble_gatts_sys_attr_get(_conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
1225+
//
1226+
// uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
1227+
// VERIFY( sys_attr, );
1228+
//
1229+
// if ( ERROR_NONE == sd_ble_gatts_sys_attr_get(_conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG) )
1230+
// {
1231+
// // save to file
1232+
// char filename[BOND_FILENAME_LEN];
1233+
// sprintf(filename, BOND_FILENAME, _bond_data.own_enc.master_id.ediv);
1234+
//
1235+
// if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
1236+
// {
1237+
// LOG_LV2("BOND", "CCCD setting is saved to file %s", filename);
1238+
// }else
1239+
// {
1240+
// LOG_LV1("BOND", "Failed to save CCCD setting");
1241+
// }
1242+
//
1243+
// }
1244+
//
1245+
// printBondDir();
1246+
//
1247+
// rtos_free(sys_attr);
1248+
//}
12951249

1296-
if ( !loaded )
1297-
{
1298-
sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
1299-
}
1300-
}
1250+
//void AdafruitBluefruit::_loadBondCCCD(uint16_t ediv)
1251+
//{
1252+
// bool loaded = false;
1253+
//
1254+
// char filename[BOND_FILENAME_LEN];
1255+
// sprintf(filename, BOND_FILENAME, ediv);
1256+
//
1257+
// NffsFile file(filename, FS_ACCESS_READ);
1258+
//
1259+
// if ( file.exists() )
1260+
// {
1261+
// int32_t len = file.size() - BOND_FILE_CCCD_OFFSET;
1262+
//
1263+
// if ( len )
1264+
// {
1265+
// uint8_t* sys_attr = (uint8_t*) rtos_malloc( len );
1266+
//
1267+
// if (sys_attr)
1268+
// {
1269+
// file.seek(BOND_FILE_CCCD_OFFSET);
1270+
//
1271+
// if ( file.read(sys_attr, len ) )
1272+
// {
1273+
// if (ERROR_NONE == sd_ble_gatts_sys_attr_set(_conn_hdl, sys_attr, len, SVC_CONTEXT_FLAG) )
1274+
// {
1275+
// loaded = true;
1276+
//
1277+
// LOG_LV2("BOND", "Load CCCD from file %s", filename);
1278+
// }else
1279+
// {
1280+
// LOG_LV1("BOND", "CCCD setting not found");
1281+
// }
1282+
// }
1283+
//
1284+
// rtos_free(sys_attr);
1285+
// }
1286+
// }
1287+
// }
1288+
//
1289+
// file.close();
1290+
//
1291+
// if ( !loaded )
1292+
// {
1293+
// sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
1294+
// }
1295+
//}
13011296

13021297
void AdafruitBluefruit::_bledfu_get_bond_data(ble_gap_addr_t* addr, ble_gap_irk_t* irk, ble_gap_enc_key_t* enc_key)
13031298
{

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,16 @@ class AdafruitBluefruit
196196
void _setConnLed (bool on_off);
197197
void _bledfu_get_bond_data(ble_gap_addr_t* addr, ble_gap_irk_t* irk, ble_gap_enc_key_t* enc_key);
198198

199-
void _saveBondKeys(void);
200-
void _saveBondCCCD(void);
199+
// void _saveBondKeys(void);
200+
// void _saveBondCCCD(void);
201+
202+
// Shared keys with bonded device, size = 80 bytes
203+
bond_data_t _bond_data;
204+
enum
205+
{
206+
BOND_FILE_DEVNAME_OFFSET = sizeof(_bond_data),
207+
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
208+
};
201209

202210
private:
203211
/*------------- SoftDevice Configuration -------------*/
@@ -235,15 +243,6 @@ class AdafruitBluefruit
235243

236244
ble_gap_sec_params_t _sec_param;
237245

238-
// Shared keys with bonded device, size = 80 bytes
239-
bond_data_t _bond_data;
240-
241-
enum
242-
{
243-
BOND_FILE_DEVNAME_OFFSET = sizeof(_bond_data),
244-
BOND_FILE_CCCD_OFFSET = BOND_FILE_DEVNAME_OFFSET + CFG_MAX_DEVNAME_LEN
245-
};
246-
247246
COMMENT_OUT(
248247
uint8_t _auth_type;
249248
char _pin[BLE_GAP_PASSKEY_LEN];
@@ -252,8 +251,8 @@ COMMENT_OUT(
252251
/*------------------------------------------------------------------*/
253252
/* INTERNAL USAGE ONLY
254253
*------------------------------------------------------------------*/
255-
bool _loadBondKeys(uint16_t ediv);
256-
void _loadBondCCCD(uint16_t ediv);
254+
// bool _loadBondKeys(uint16_t ediv);
255+
// void _loadBondCCCD(uint16_t ediv);
257256
void _ble_handler(ble_evt_t* evt);
258257

259258
friend void SD_EVT_IRQHandler(void);

0 commit comments

Comments
 (0)