Skip to content

Commit 5c59868

Browse files
templates for reading and writing to avoid repetition
1 parent 49db7e2 commit 5c59868

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

features/FEATURE_BLE/ble/generic/FileSecurityDb.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ class FileSecurityDb : public SecurityDb {
4040
return reinterpret_cast<entry_t*>(db_handle);
4141
}
4242

43+
template<class T>
44+
void db_read(T *value, long int offset) {
45+
fseek(_db_file, offset, SEEK_SET);
46+
fread(value, sizeof(T), 1, _db_file);
47+
}
48+
49+
template<class T>
50+
void db_write(T *value, long int offset) {
51+
fseek(_db_file, offset, SEEK_SET);
52+
fwrite(value, sizeof(T), 1, _db_file);
53+
}
54+
4355
public:
4456
FileSecurityDb(FILE *db_file);
4557
virtual ~FileSecurityDb();

features/FEATURE_BLE/source/generic/FileSecurityDb.cpp

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ void FileSecurityDb::set_entry_local_ltk(
163163

164164
entry->flags.ltk_sent = true;
165165

166-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_LTK, SEEK_SET);
167-
fwrite(&ltk, sizeof(ltk_t), 1, _db_file);
166+
db_write(&ltk, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_LTK);
168167
}
169168

170169
void FileSecurityDb::set_entry_local_ediv_rand(
@@ -177,10 +176,8 @@ void FileSecurityDb::set_entry_local_ediv_rand(
177176
return;
178177
}
179178

180-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_EDIV, SEEK_SET);
181-
fwrite(&ediv, sizeof(ediv_t), 1, _db_file);
182-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_RAND, SEEK_SET);
183-
fwrite(&rand, sizeof(rand_t), 1, _db_file);
179+
db_write(&ediv, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_EDIV);
180+
db_write(&rand, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS_RAND);
184181
}
185182

186183
/* peer's keys */
@@ -198,8 +195,7 @@ void FileSecurityDb::set_entry_peer_ltk(
198195

199196
entry->flags.ltk_stored = true;
200197

201-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_LTK, SEEK_SET);
202-
fwrite(&ltk, sizeof(ltk_t), 1, _db_file);
198+
db_write(&ltk, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_LTK);
203199
}
204200

205201
void FileSecurityDb::set_entry_peer_ediv_rand(
@@ -212,10 +208,8 @@ void FileSecurityDb::set_entry_peer_ediv_rand(
212208
return;
213209
}
214210

215-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_EDIV, SEEK_SET);
216-
fwrite(&ediv, sizeof(ediv_t), 1, _db_file);
217-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_RAND, SEEK_SET);
218-
fwrite(&rand, sizeof(rand_t), 1, _db_file);
211+
db_write(&ediv, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_EDIV);
212+
db_write(&rand, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS_RAND);
219213
}
220214

221215
void FileSecurityDb::set_entry_peer_irk(
@@ -229,8 +223,7 @@ void FileSecurityDb::set_entry_peer_irk(
229223

230224
entry->flags.irk_stored = true;
231225

232-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_IRK, SEEK_SET);
233-
fwrite(&irk, sizeof(irk_t), 1, _db_file);
226+
db_write(&irk, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_IRK);
234227
}
235228

236229
void FileSecurityDb::set_entry_peer_bdaddr(
@@ -243,10 +236,8 @@ void FileSecurityDb::set_entry_peer_bdaddr(
243236
return;
244237
}
245238

246-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS, SEEK_SET);
247-
fwrite(&peer_address, sizeof(address_t), 1, _db_file);
248-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS_IS_PUBLIC, SEEK_SET);
249-
fwrite(&address_is_public, sizeof(bool), 1, _db_file);
239+
db_write(&peer_address, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS);
240+
db_write(&address_is_public, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY_ADDRESS_IS_PUBLIC);
250241
}
251242

252243
void FileSecurityDb::set_entry_peer_csrk(
@@ -260,9 +251,7 @@ void FileSecurityDb::set_entry_peer_csrk(
260251

261252
entry->flags.csrk_stored = true;
262253

263-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING, SEEK_SET);
264-
/* only write in the csrk */
265-
fwrite(&csrk, sizeof(csrk_t), 1, _db_file);
254+
db_write(&csrk, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING);
266255
}
267256

268257
void FileSecurityDb::set_entry_peer_sign_counter(
@@ -290,14 +279,9 @@ void FileSecurityDb::restore() {
290279
}
291280
}
292281

293-
fseek(_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
294-
fread(&_local_identity, sizeof(_local_identity), 1, _db_file);
295-
296-
fseek(_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
297-
fread(&_local_csrk, sizeof(_local_csrk), 1, _db_file);
298-
299-
fseek(_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
300-
fread(&_local_sign_counter, sizeof(_local_sign_counter), 1, _db_file);
282+
db_read(&_local_identity, DB_OFFSET_LOCAL_IDENTITY);
283+
db_read(&_local_csrk, DB_OFFSET_LOCAL_CSRK);
284+
db_read(&_local_sign_counter, DB_OFFSET_LOCAL_SIGN_COUNT);
301285

302286
fseek(_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
303287
/* we read the entries partially and fill the offsets ourselves*/
@@ -313,13 +297,11 @@ void FileSecurityDb::sync(entry_handle_t db_handle) {
313297
return;
314298
}
315299

316-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING_COUNT, SEEK_SET);
317-
fwrite(&entry->peer_sign_counter, sizeof(sign_count_t), 1, _db_file);
300+
db_write(&entry->peer_sign_counter, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING_COUNT);
318301
}
319302

320303
void FileSecurityDb::set_restore(bool reload) {
321-
fseek(_db_file, DB_OFFSET_RESTORE, SEEK_SET);
322-
fwrite(&reload, sizeof(bool), 1, _db_file);
304+
db_write(&reload, DB_OFFSET_RESTORE);
323305
}
324306

325307
/* helper functions */
@@ -358,42 +340,50 @@ SecurityEntryIdentity_t* FileSecurityDb::read_in_entry_peer_identity(entry_handl
358340
if (!entry) {
359341
return NULL;
360342
}
361-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY, SEEK_SET);
362-
fread(&_buffer, sizeof(SecurityEntryIdentity_t), 1, _db_file);
363-
return reinterpret_cast<SecurityEntryIdentity_t*>(_buffer);
343+
344+
SecurityEntryIdentity_t* identity = reinterpret_cast<SecurityEntryIdentity_t*>(_buffer);
345+
db_read(identity, entry->file_offset + DB_STORE_OFFSET_PEER_IDENTITY);
346+
347+
return identity;
364348
};
365349

366350
SecurityEntryKeys_t* FileSecurityDb::read_in_entry_peer_keys(entry_handle_t db_entry) {
367351
entry_t *entry = as_entry(db_entry);
368352
if (!entry) {
369353
return NULL;
370354
}
371-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS, SEEK_SET);
372-
fread(&_buffer, sizeof(SecurityEntryKeys_t), 1, _db_file);
373-
return reinterpret_cast<SecurityEntryKeys_t*>(_buffer);
355+
356+
SecurityEntryKeys_t* keys = reinterpret_cast<SecurityEntryKeys_t*>(_buffer);
357+
db_read(keys, entry->file_offset + DB_STORE_OFFSET_PEER_KEYS);
358+
359+
return keys;
374360
};
375361

376362
SecurityEntryKeys_t* FileSecurityDb::read_in_entry_local_keys(entry_handle_t db_entry) {
377363
entry_t *entry = as_entry(db_entry);
378364
if (!entry) {
379365
return NULL;
380366
}
381-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS, SEEK_SET);
382-
fread(&_buffer, sizeof(SecurityEntryKeys_t), 1, _db_file);
383-
return reinterpret_cast<SecurityEntryKeys_t*>(_buffer);
367+
368+
SecurityEntryKeys_t* keys = reinterpret_cast<SecurityEntryKeys_t*>(_buffer);
369+
db_read(keys, entry->file_offset + DB_STORE_OFFSET_LOCAL_KEYS);
370+
371+
return keys;
384372
};
385373

386374
SecurityEntrySigning_t* FileSecurityDb::read_in_entry_peer_signing(entry_handle_t db_entry) {
387375
entry_t *entry = as_entry(db_entry);
388376
if (!entry) {
389377
return NULL;
390378
}
391-
fseek(_db_file, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING, SEEK_SET);
392379

393380
/* only read in the csrk */
394-
fread(&_buffer, sizeof(csrk_t), 1, _db_file);
395-
SecurityEntrySigning_t* signing = reinterpret_cast<SecurityEntrySigning_t*>(_buffer);
381+
csrk_t* csrk = reinterpret_cast<csrk_t*>(_buffer);
382+
db_read(csrk, entry->file_offset + DB_STORE_OFFSET_PEER_SIGNING);
383+
384+
396385
/* use the counter held in memory */
386+
SecurityEntrySigning_t* signing = reinterpret_cast<SecurityEntrySigning_t*>(_buffer);
397387
signing->counter = entry->peer_sign_counter;
398388

399389
return signing;

0 commit comments

Comments
 (0)