Skip to content

Commit a05d467

Browse files
committed
enhance bonding management
- seperate bond folder for prph and central - use mac address if couldn't get peer name when saving bond data
1 parent 203d9bd commit a05d467

File tree

9 files changed

+146
-73
lines changed

9 files changed

+146
-73
lines changed

libraries/Bluefruit52Lib/src/BLECentral.cpp

Lines changed: 6 additions & 0 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
* Constructor
@@ -137,6 +138,11 @@ void BLECentral::setDisconnectCallback( BLEGap::disconnect_callback_t fp)
137138
_disconnect_cb = fp;
138139
}
139140

141+
void BLECentral::clearBonds(void)
142+
{
143+
bond_clear_cntr();
144+
}
145+
140146
/**
141147
* Event is forwarded from Bluefruit Poll() method
142148
* @param event

libraries/Bluefruit52Lib/src/BLECentral.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class BLECentral
7171
bool connected (uint16_t conn_handle); // If connected to a specific peripheral
7272
bool connected (void); // If connected to any peripherals
7373

74+
void clearBonds (void);
75+
7476
/*------------- Callbacks -------------*/
7577
void setConnectCallback ( BLEGap::connect_callback_t fp);
7678
void setDisconnectCallback( BLEGap::disconnect_callback_t fp);

libraries/Bluefruit52Lib/src/BLEGap.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
337337
peer->bonded = true;
338338
peer->ediv = peer->bond_data->own_enc.master_id.ediv;
339339

340-
// TODO Skip bonding for central, implement re-establishment connection later.
341-
if ( peer->role == BLE_GAP_ROLE_PERIPH )
342-
{
343-
bond_save_keys(conn_hdl, peer->bond_data);
344-
}
340+
bond_save_keys(peer->role, conn_hdl, peer->bond_data);
345341
}else
346342
{
347343
PRINT_HEX(status->auth_status);
@@ -361,7 +357,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
361357
bond_data_t bdata;
362358
varclr(&bdata);
363359

364-
if ( bond_load_keys(sec_req->master_id.ediv, &bdata) )
360+
if ( bond_load_keys(peer->role, sec_req->master_id.ediv, &bdata) )
365361
{
366362
sd_ble_gap_sec_info_reply(evt->evt.gap_evt.conn_handle, &bdata.own_enc.enc_info, &bdata.peer_id.id_info, NULL);
367363

@@ -379,7 +375,7 @@ void BLEGap::_eventHandler(ble_evt_t* evt)
379375
// Previously bonded --> secure by re-connection process
380376
// --> Load & Set Sys Attr (Apply Service Context)
381377
// Else Init Sys Attr
382-
bond_load_cccd(conn_hdl, peer->ediv);
378+
bond_load_cccd(peer->role, conn_hdl, peer->ediv);
383379

384380
// Paired is Bonded (as we always save keys)
385381
peer->bonded = true;

libraries/Bluefruit52Lib/src/BLEGap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class BLEGap
124124
ble_gap_sec_params_t _sec_param;
125125

126126
friend class AdafruitBluefruit;
127-
friend class BLEGatt;
127+
// friend class BLEGatt;
128128
};
129129

130130
#endif /* BLEGAP_H_ */

libraries/Bluefruit52Lib/src/BLEGatt.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
146146
// Save CCCD if paired
147147
if ( Bluefruit.connPaired() && (evt_id == BLE_GATTS_EVT_WRITE) && (req_handle == chr->handles().cccd_handle) )
148148
{
149-
bond_save_cccd(evt_conn_hdl, Bluefruit.Gap._get_peer(evt_conn_hdl)->ediv);
149+
BLEGap::gap_peer_t* peer = Bluefruit.Gap._get_peer(evt_conn_hdl);
150+
bond_save_cccd( peer->role, evt_conn_hdl, peer->ediv);
150151
}
151152
}
152153
}

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -731,34 +731,12 @@ void AdafruitBluefruit::printInfo(void)
731731
Serial.println();
732732

733733
/*------------- List the paried device -------------*/
734-
Serial.printf(title_fmt, "Paired Devices");
734+
Serial.printf(title_fmt, "Peripheral Paired Devices\n");
735+
bond_print_list(BLE_GAP_ROLE_PERIPH);
735736
Serial.println();
736737

737-
NffsDir dir(CFG_BOND_NFFS_DIR);
738-
NffsDirEntry dirEntry;
739-
while( dir.read(&dirEntry) )
740-
{
741-
if ( !dirEntry.isDirectory() )
742-
{
743-
char name[64];
744-
dirEntry.getName(name, sizeof(name));
745-
746-
Serial.printf(" %s : ", name);
747-
748-
// open file to read device name
749-
NffsFile file(CFG_BOND_NFFS_DIR, dirEntry, FS_ACCESS_READ);
750-
751-
varclr(name);
752-
753-
file.seek(BOND_FILE_DEVNAME_OFFSET);
754-
if ( file.read(name, CFG_MAX_DEVNAME_LEN) )
755-
{
756-
Serial.println(name);
757-
}
758-
759-
file.close();
760-
}
761-
}
738+
Serial.printf(title_fmt, "Central Paired Devices\n");
739+
bond_print_list(BLE_GAP_ROLE_CENTRAL);
762740
Serial.println();
763741

764742
Serial.println();
@@ -984,6 +962,6 @@ bool AdafruitBluefruit::requestPairing(void)
984962

985963
void AdafruitBluefruit::clearBonds(void)
986964
{
987-
bond_clear();
965+
bond_clear_prph();
988966
}
989967

libraries/Bluefruit52Lib/src/services/BLEDfu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt
139139
{
140140
bond_data_t bdata;
141141

142-
if ( bond_load_keys( Bluefruit.Gap._get_peer(conn_hdl)->ediv, &bdata ) )
142+
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap._get_peer(conn_hdl)->ediv, &bdata ) )
143143
{
144144
peer_data->addr = bdata.peer_id.id_addr_info;
145145
peer_data->irk = bdata.peer_id.id_info;

libraries/Bluefruit52Lib/src/utility/bonding.cpp

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
#define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
4343

4444
#if CFG_DEBUG >= 2
45-
#define printBondDir() dbgPrintDir(CFG_BOND_NFFS_DIR)
45+
#define printBondDir(role) dbgPrintDir( role == BLE_GAP_ROLE_PERIPH ? BOND_DIR_PRPH : BOND_DIR_CNTR )
4646
#else
47-
#define printBondDir()
47+
#define printBondDir(role)
4848
#endif
4949

5050
/*------------------------------------------------------------------*/
@@ -58,16 +58,24 @@ void bond_init(void)
5858
{
5959
// Initialize nffs for bonding (it is safe to call nffs_pkg_init() multiple time)
6060
Nffs.begin();
61-
(void) Nffs.mkdir_p(CFG_BOND_NFFS_DIR);
61+
62+
(void) Nffs.mkdir_p(BOND_DIR_PRPH);
63+
(void) Nffs.mkdir_p(BOND_DIR_CNTR);
6264
}
6365

6466
/*------------------------------------------------------------------*/
6567
/* Keys
6668
*------------------------------------------------------------------*/
67-
static void bond_save_keys_dfr(uint16_t conn_hdl, bond_data_t* bdata)
69+
static void bond_save_keys_dfr(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
6870
{
69-
char filename[BOND_FILENAME_LEN];
70-
sprintf(filename, BOND_FILENAME, bdata->own_enc.master_id.ediv);
71+
char filename[BOND_FNAME_LEN];
72+
if ( role == BLE_GAP_ROLE_PERIPH )
73+
{
74+
sprintf(filename, BOND_FNAME_PRPH, bdata->own_enc.master_id.ediv);
75+
}else
76+
{
77+
sprintf(filename, BOND_FNAME_CNTR, bdata->own_enc.master_id.ediv);
78+
}
7179

7280
char devname[CFG_MAX_DEVNAME_LEN] = { 0 };
7381
Bluefruit.Gap.getPeerName(conn_hdl, devname, CFG_MAX_DEVNAME_LEN);
@@ -84,12 +92,15 @@ static void bond_save_keys_dfr(uint16_t conn_hdl, bond_data_t* bdata)
8492
result = false;
8593
}
8694

87-
// write device name
88-
if ( strlen(devname) && !file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN) )
95+
// If couldn't get devname use peer mac address
96+
if ( !strlen(devname) )
8997
{
90-
result = false;
98+
uint8_t* mac = bdata->peer_id.id_addr_info.addr;
99+
sprintf(devname, "%02X:%02X:%02X:%02X:%02X:%02X", mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);
91100
}
92101

102+
file.write((uint8_t*) devname, CFG_MAX_DEVNAME_LEN);
103+
93104
file.close();
94105

95106
if (result)
@@ -100,24 +111,30 @@ static void bond_save_keys_dfr(uint16_t conn_hdl, bond_data_t* bdata)
100111
LOG_LV1("BOND", "Failed to save keys for \"%s\"", devname);
101112
}
102113

103-
printBondDir();
114+
printBondDir(role);
104115
}
105116

106-
void bond_save_keys(uint16_t conn_hdl, bond_data_t* bdata)
117+
void bond_save_keys(uint8_t role, uint16_t conn_hdl, bond_data_t* bdata)
107118
{
108119
uint8_t* buf = (uint8_t*) rtos_malloc( sizeof(bond_data_t) );
109120
VERIFY(buf, );
110121

111122
memcpy(buf, bdata, sizeof(bond_data_t));
112123

113124
// queue to execute in Ada Callback thread
114-
ada_callback(buf, bond_save_keys_dfr, conn_hdl, buf);
125+
ada_callback(buf, bond_save_keys_dfr, role, conn_hdl, buf);
115126
}
116127

117-
bool bond_load_keys(uint16_t ediv, bond_data_t* bdata)
128+
bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
118129
{
119-
char filename[BOND_FILENAME_LEN];
120-
sprintf(filename, BOND_FILENAME, ediv);
130+
char filename[BOND_FNAME_LEN];
131+
if ( role == BLE_GAP_ROLE_PERIPH )
132+
{
133+
sprintf(filename, BOND_FNAME_PRPH, ediv);
134+
}else
135+
{
136+
sprintf(filename, BOND_FNAME_CNTR, ediv);
137+
}
121138

122139
bool result = (Nffs.readFile(filename, bdata, sizeof(bond_data_t)) > 0);
123140

@@ -136,7 +153,7 @@ bool bond_load_keys(uint16_t ediv, bond_data_t* bdata)
136153
/*------------------------------------------------------------------*/
137154
/* CCCD
138155
*------------------------------------------------------------------*/
139-
static void bond_save_cccd_dfr (uint16_t conn_hdl, uint16_t ediv)
156+
static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
140157
{
141158
uint16_t len=0;
142159
sd_ble_gatts_sys_attr_get(conn_hdl, NULL, &len, SVC_CONTEXT_FLAG);
@@ -147,8 +164,14 @@ static void bond_save_cccd_dfr (uint16_t conn_hdl, uint16_t ediv)
147164
if ( ERROR_NONE == sd_ble_gatts_sys_attr_get(conn_hdl, sys_attr, &len, SVC_CONTEXT_FLAG) )
148165
{
149166
// save to file
150-
char filename[BOND_FILENAME_LEN];
151-
sprintf(filename, BOND_FILENAME, ediv);
167+
char filename[BOND_FNAME_LEN];
168+
if ( role == BLE_GAP_ROLE_PERIPH )
169+
{
170+
sprintf(filename, BOND_FNAME_PRPH, ediv);
171+
}else
172+
{
173+
sprintf(filename, BOND_FNAME_CNTR, ediv);
174+
}
152175

153176
if ( Nffs.writeFile(filename, sys_attr, len, BOND_FILE_CCCD_OFFSET) )
154177
{
@@ -161,24 +184,30 @@ static void bond_save_cccd_dfr (uint16_t conn_hdl, uint16_t ediv)
161184
}
162185

163186
rtos_free(sys_attr);
164-
printBondDir();
187+
printBondDir(role);
165188
}
166189

167-
void bond_save_cccd(uint16_t cond_hdl, uint16_t ediv)
190+
void bond_save_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv)
168191
{
169192
VERIFY( ediv != 0xFFFF, );
170193

171194
// queue to execute in Ada Callback thread
172-
ada_callback(NULL, bond_save_cccd_dfr, cond_hdl, ediv);
195+
ada_callback(NULL, bond_save_cccd_dfr, role, cond_hdl, ediv);
173196
}
174197

175198

176-
bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv)
199+
bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv)
177200
{
178201
bool loaded = false;
179202

180-
char filename[BOND_FILENAME_LEN];
181-
sprintf(filename, BOND_FILENAME, ediv);
203+
char filename[BOND_FNAME_LEN];
204+
if ( role == BLE_GAP_ROLE_PERIPH )
205+
{
206+
sprintf(filename, BOND_FNAME_PRPH, ediv);
207+
}else
208+
{
209+
sprintf(filename, BOND_FNAME_CNTR, ediv);
210+
}
182211

183212
NffsFile file(filename, FS_ACCESS_READ);
184213

@@ -222,16 +251,68 @@ bool bond_load_cccd(uint16_t cond_hdl, uint16_t ediv)
222251
return loaded;
223252
}
224253

254+
void bond_print_list(uint8_t role)
255+
{
256+
const char* dpath = (role == BLE_GAP_ROLE_PERIPH ? BOND_DIR_PRPH : BOND_DIR_CNTR);
257+
258+
NffsDir dir(dpath);
259+
NffsDirEntry dirEntry;
260+
261+
while( dir.read(&dirEntry) )
262+
{
263+
if ( !dirEntry.isDirectory() )
264+
{
265+
char name[64];
266+
dirEntry.getName(name, sizeof(name));
267+
268+
cprintf(" %s : ", name);
269+
270+
// open file to read device name
271+
NffsFile file(dpath, dirEntry, FS_ACCESS_READ);
272+
273+
varclr(name);
274+
275+
file.seek(BOND_FILE_DEVNAME_OFFSET);
276+
if ( file.read(name, CFG_MAX_DEVNAME_LEN) )
277+
{
278+
cprintf(name);
279+
}
280+
281+
cprintf("\n");
282+
file.close();
283+
}
284+
}
285+
cprintf("\n");
286+
}
287+
225288
/*------------------------------------------------------------------*/
226289
/* DELETE
227290
*------------------------------------------------------------------*/
228-
void bond_clear(void)
291+
void bond_clear_prph(void)
229292
{
230293
// Detele bonds dir
231-
Nffs.remove(CFG_BOND_NFFS_DIR);
294+
Nffs.remove(BOND_DIR_PRPH);
232295

233296
// Create an empty one
234-
Nffs.mkdir_p(CFG_BOND_NFFS_DIR);
297+
(void) Nffs.mkdir_p(BOND_DIR_PRPH);
298+
}
299+
300+
void bond_clear_cntr(void)
301+
{
302+
// Detele bonds dir
303+
Nffs.remove(BOND_DIR_CNTR);
304+
305+
// Create an empty one
306+
(void) Nffs.mkdir_p(BOND_DIR_CNTR);
307+
}
308+
309+
310+
void bond_clear_all(void)
311+
{
312+
// Detele bonds dir
313+
Nffs.remove(BOND_DIR_ROOT);
235314

236-
printBondDir();
315+
// Create an empty one for prph and central
316+
(void) Nffs.mkdir_p(BOND_DIR_PRPH);
317+
(void) Nffs.mkdir_p(BOND_DIR_CNTR);
237318
}

0 commit comments

Comments
 (0)