Skip to content

Commit 751195f

Browse files
committed
fix a bonding issue with load cccd
1 parent 608cde9 commit 751195f

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

libraries/Bluefruit52Lib/src/utility/bonding.cpp

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
#include "bonding.h"
4040
#include "bluefruit.h"
4141

42+
#define BOND_DEBUG 0
43+
44+
#if (CFG_DEBUG == 1 && BOND_DEBUG == 1) || (CFG_DEBUG >= 2)
45+
#define BOND_LOG(...) LOG_LV1("BOND", __VA_ARGS__)
46+
#else
47+
#define BOND_LOG(...)
48+
#endif
49+
4250
/*------------------------------------------------------------------*/
4351
/* Bond Key is saved in following layout
4452
* - Bond Data : 80 bytes
@@ -99,7 +107,7 @@ static void bond_save_keys_dfr (uint8_t role, uint16_t conn_hdl, bond_data_t* bd
99107
file.write(namelen + 1);
100108
file.write((uint8_t*) devname, namelen + 1);
101109

102-
LOG_LV2("BOND", "Keys for \"%s\" is saved to file %s ( %d bytes )", devname, filename, file.size());
110+
BOND_LOG("Saved keys for \"%s\" to file %s ( %d bytes )", devname, filename, file.size());
103111

104112
file.close();
105113
}
@@ -131,7 +139,7 @@ bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
131139
file.read(bdata, keylen);
132140
file.close();
133141

134-
LOG_LV2("BOND", "Load Keys from file %s", filename);
142+
BOND_LOG("Loaded keys from file %s", filename);
135143

136144
return true;
137145
}
@@ -163,8 +171,7 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
163171
file.write((uint8_t) len);
164172
file.write(sys_attr, len);
165173

166-
LOG_LV2("BOND", "CCCD setting is saved to file %s ( offset = %d, len = %d bytes )", filename, file.size() - (len + 1),
167-
len);
174+
BOND_LOG("Saved CCCD setting to file %s ( offset = %d, len = %d bytes )", filename, file.size() - (len + 1), len);
168175

169176
file.close();
170177
}
@@ -183,35 +190,37 @@ bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv)
183190
{
184191
bool loaded = false;
185192

186-
char filename[BOND_FNAME_LEN];
187-
get_fname(filename, role, ediv);
188-
189-
File file(filename, FILE_READ, InternalFS);
190-
VERIFY(file);
191-
192-
if ( file )
193+
if ( ediv != 0xFFFF )
193194
{
194-
file.seek(file.read() + file.position()); // skip key
195-
file.seek(file.read() + file.position()); // skip name
195+
char filename[BOND_FNAME_LEN];
196+
get_fname(filename, role, ediv);
196197

197-
uint16_t len = file.read();
198+
PRINT_STR(filename);
198199

199-
if ( len )
200-
{
201-
uint8_t sys_attr[len];
200+
File file(filename, FILE_READ, InternalFS);
202201

203-
file.read(sys_attr, len);
202+
if ( file )
203+
{
204+
file.seek(file.read() + file.position()); // skip key
205+
file.seek(file.read() + file.position()); // skip name
204206

205-
if ( ERROR_NONE == sd_ble_gatts_sys_attr_set(cond_hdl, sys_attr, len, SVC_CONTEXT_FLAG) )
207+
int len = file.read();
208+
if ( len > 0 )
206209
{
207-
loaded = true;
208-
LOG_LV2("BOND", "Load CCCD from file %s ( offset = %d, len = %d bytes )", filename, file.size() - (len + 1),
209-
len);
210+
uint8_t sys_attr[len];
211+
212+
file.read(sys_attr, len);
213+
214+
if ( ERROR_NONE == sd_ble_gatts_sys_attr_set(cond_hdl, sys_attr, len, SVC_CONTEXT_FLAG) )
215+
{
216+
loaded = true;
217+
BOND_LOG("Loaded CCCD from file %s ( offset = %d, len = %d bytes )", filename, file.size() - (len + 1), len);
218+
}
210219
}
211220
}
212-
}
213221

214-
file.close();
222+
file.close();
223+
}
215224

216225
if ( !loaded )
217226
{
@@ -233,18 +242,20 @@ void bond_print_list(uint8_t role)
233242
{
234243
file.seek(file.read() + file.position()); // skip key
235244

236-
uint8_t len = file.read();
237-
238-
char devname[len];
239-
file.read(devname, len);
240-
241-
cprintf(" %s : %s (%d bytes)\n", file.name(), devname, file.size());
245+
int len = file.read();
246+
if ( len > 0 )
247+
{
248+
char devname[len];
249+
file.read(devname, len);
242250

251+
cprintf(" %s : %s (%d bytes)\n", file.name(), devname, file.size());
252+
}
243253
file.close();
244254
}
245255

246256
cprintf("\n");
247257

258+
file.close();
248259
dir.close();
249260
}
250261

@@ -259,7 +270,7 @@ bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
259270
while ( (file = dir.openNextFile(FILE_READ)) )
260271
{
261272
// Read bond data of each stored file
262-
uint8_t keylen = file.read();
273+
int keylen = file.read();
263274
if ( keylen == sizeof(bond_data_t) )
264275
{
265276
file.read((uint8_t*) bdata, keylen);
@@ -280,6 +291,7 @@ bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
280291
if ( found ) break;
281292
}
282293

294+
file.close();
283295
dir.close();
284296

285297
return found;

libraries/Bluefruit52Lib/src/utility/bonding.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838

3939
#include "bluefruit_common.h"
4040

41-
#define BOND_DIR_PRPH "/adafruit/bond_prph"
42-
#define BOND_DIR_CNTR "/adafruit/bond_cntr"
41+
#define BOND_DIR_PRPH "/adafruit/bond_prph"
42+
#define BOND_DIR_CNTR "/adafruit/bond_cntr"
4343

44-
#define BOND_FNAME_PRPH BOND_DIR_PRPH "/%04x"
45-
#define BOND_FNAME_CNTR BOND_DIR_CNTR "/%04x"
44+
#define BOND_FNAME_PRPH BOND_DIR_PRPH "/%04x"
45+
#define BOND_FNAME_CNTR BOND_DIR_CNTR "/%04x"
4646

4747
// Shared keys with bonded device, size = 80 bytes
4848
typedef struct

0 commit comments

Comments
 (0)