39
39
#include " bonding.h"
40
40
#include " bluefruit.h"
41
41
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
+
42
50
/* ------------------------------------------------------------------*/
43
51
/* Bond Key is saved in following layout
44
52
* - 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
99
107
file.write (namelen + 1 );
100
108
file.write ((uint8_t *) devname, namelen + 1 );
101
109
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 ());
103
111
104
112
file.close ();
105
113
}
@@ -131,7 +139,7 @@ bool bond_load_keys(uint8_t role, uint16_t ediv, bond_data_t* bdata)
131
139
file.read (bdata, keylen);
132
140
file.close ();
133
141
134
- LOG_LV2 ( " BOND " , " Load Keys from file %s" , filename);
142
+ BOND_LOG ( " Loaded keys from file %s" , filename);
135
143
136
144
return true ;
137
145
}
@@ -163,8 +171,7 @@ static void bond_save_cccd_dfr (uint8_t role, uint16_t conn_hdl, uint16_t ediv)
163
171
file.write ((uint8_t ) len);
164
172
file.write (sys_attr, len);
165
173
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);
168
175
169
176
file.close ();
170
177
}
@@ -183,35 +190,37 @@ bool bond_load_cccd(uint8_t role, uint16_t cond_hdl, uint16_t ediv)
183
190
{
184
191
bool loaded = false ;
185
192
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 )
193
194
{
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);
196
197
197
- uint16_t len = file. read ( );
198
+ PRINT_STR (filename );
198
199
199
- if ( len )
200
- {
201
- uint8_t sys_attr[len];
200
+ File file (filename, FILE_READ, InternalFS);
202
201
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
204
206
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 )
206
209
{
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
+ }
210
219
}
211
220
}
212
- }
213
221
214
- file.close ();
222
+ file.close ();
223
+ }
215
224
216
225
if ( !loaded )
217
226
{
@@ -233,18 +242,20 @@ void bond_print_list(uint8_t role)
233
242
{
234
243
file.seek (file.read () + file.position ()); // skip key
235
244
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);
242
250
251
+ cprintf (" %s : %s (%d bytes)\n " , file.name (), devname, file.size ());
252
+ }
243
253
file.close ();
244
254
}
245
255
246
256
cprintf (" \n " );
247
257
258
+ file.close ();
248
259
dir.close ();
249
260
}
250
261
@@ -259,7 +270,7 @@ bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
259
270
while ( (file = dir.openNextFile (FILE_READ)) )
260
271
{
261
272
// Read bond data of each stored file
262
- uint8_t keylen = file.read ();
273
+ int keylen = file.read ();
263
274
if ( keylen == sizeof (bond_data_t ) )
264
275
{
265
276
file.read ((uint8_t *) bdata, keylen);
@@ -280,6 +291,7 @@ bool bond_find_cntr(ble_gap_addr_t* addr, bond_data_t* bdata)
280
291
if ( found ) break ;
281
292
}
282
293
294
+ file.close ();
283
295
dir.close ();
284
296
285
297
return found;
0 commit comments