Skip to content

Commit 957486e

Browse files
pan-paul-szczepanek-arm
authored andcommitted
BLE: Move traces out of header file to avoid collisions.
This change required the creation of the implementation files of SecurityDb classes.
1 parent 6adaefd commit 957486e

File tree

9 files changed

+698
-453
lines changed

9 files changed

+698
-453
lines changed

connectivity/FEATURE_BLE/source/cordio/source/PalAttClientImpl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "att_api.h"
2828
#include "att_defs.h"
2929

30+
#include "mbed-trace/mbed_trace.h"
31+
3032
#define TRACE_GROUP "BLAT"
3133

3234
namespace ble {

connectivity/FEATURE_BLE/source/generic/FileSecurityDb.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ FileSecurityDb::~FileSecurityDb()
9090
fclose(_db_file);
9191
}
9292

93+
FileSecurityDb::entry_t* FileSecurityDb::as_entry(entry_handle_t db_handle) {
94+
entry_t* entry = reinterpret_cast<entry_t*>(db_handle);
95+
if (!entry) {
96+
tr_error("Invalid security DB handle used");
97+
}
98+
return entry;
99+
}
100+
93101
FILE* FileSecurityDb::open_db_file(const char *db_path)
94102
{
95103
if (!db_path) {

connectivity/FEATURE_BLE/source/generic/FileSecurityDb.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include <stdio.h>
2525

2626
#include "SecurityDb.h"
27-
#include "mbed-trace/mbed_trace.h"
28-
29-
#define TRACE_GROUP "BLDB"
3027

3128
namespace ble {
3229

@@ -40,13 +37,7 @@ class FileSecurityDb : public SecurityDb {
4037
size_t file_offset;
4138
};
4239

43-
static entry_t* as_entry(entry_handle_t db_handle) {
44-
entry_t* entry = reinterpret_cast<entry_t*>(db_handle);
45-
if (!entry) {
46-
tr_error("Invalid security DB handle used");
47-
}
48-
return entry;
49-
}
40+
static entry_t* as_entry(entry_handle_t db_handle);
5041

5142
template<class T>
5243
void db_read(T *value, long int offset) {

connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ KVStoreSecurityDb::~KVStoreSecurityDb()
6262
{
6363
}
6464

65+
KVStoreSecurityDb::entry_t *KVStoreSecurityDb::as_entry(entry_handle_t db_handle) {
66+
entry_t* entry = reinterpret_cast<entry_t*>(db_handle);
67+
if (!entry) {
68+
tr_error("Invalid security DB handle used");
69+
}
70+
return entry;
71+
}
72+
6573
bool KVStoreSecurityDb::open_db()
6674
{
6775
uint8_t version = 0;

connectivity/FEATURE_BLE/source/generic/KVStoreSecurityDb.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@
2525
#include "mbed_error.h"
2626

2727
#include "SecurityDb.h"
28-
#include "mbed-trace/mbed_trace.h"
2928

3029
#define STR_EXPAND(tok) #tok
3130
#define STR(tok) STR_EXPAND(tok)
3231

33-
#define TRACE_GROUP "BLDB"
34-
3532
namespace ble {
3633

3734
/** Filesystem implementation */
@@ -66,14 +63,6 @@ class KVStoreSecurityDb : public SecurityDb {
6663
static constexpr char DB_VERSION[DB_KEY_SIZE] = { 'v','e','r' };
6764
static constexpr char DB_RESTORE[DB_KEY_SIZE] = { 'r','e','s' };
6865

69-
static entry_t* as_entry(entry_handle_t db_handle) {
70-
entry_t* entry = reinterpret_cast<entry_t*>(db_handle);
71-
if (!entry) {
72-
tr_error("Invalid security DB handle used");
73-
}
74-
return entry;
75-
}
76-
7766
template<class T>
7867
static void db_read(T *value, const char* key) {
7968
char db_key[DB_FULL_KEY_SIZE];
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2021 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "MemorySecurityDb.h"
19+
20+
#include "mbed-trace/mbed_trace.h"
21+
#include "common/ble_trace_helpers.h"
22+
23+
#define TRACE_GROUP "BLDB"
24+
25+
#define _STR(x) #x
26+
#define STR(x) _STR(x)
27+
28+
29+
namespace ble {
30+
31+
MemorySecurityDb::entry_t *MemorySecurityDb::as_entry(entry_handle_t db_handle)
32+
{
33+
entry_t* entry = reinterpret_cast<entry_t*>(db_handle);
34+
if (!entry) {
35+
tr_error("Invalid security DB handle used");
36+
}
37+
return entry;
38+
}
39+
40+
41+
MemorySecurityDb::MemorySecurityDb() : SecurityDb()
42+
{
43+
tr_info("Using memory security DB (capacity " STR(BLE_SECURITY_DATABASE_MAX_ENTRIES) " entries) - no persistence across reset");
44+
}
45+
46+
47+
SecurityDistributionFlags_t *MemorySecurityDb::get_distribution_flags(
48+
entry_handle_t db_handle
49+
) {
50+
return reinterpret_cast<SecurityDistributionFlags_t*>(db_handle);
51+
}
52+
53+
/* local keys */
54+
55+
/* set */
56+
void MemorySecurityDb::set_entry_local_ltk(
57+
entry_handle_t db_handle,
58+
const ltk_t &ltk
59+
) {
60+
entry_t *entry = as_entry(db_handle);
61+
if (entry) {
62+
tr_info("Write DB entry %d: local ltk %s", get_index(db_handle), to_string(ltk));
63+
entry->flags.ltk_sent = true;
64+
entry->local_keys.ltk = ltk;
65+
}
66+
}
67+
68+
void MemorySecurityDb::set_entry_local_ediv_rand(
69+
entry_handle_t db_handle,
70+
const ediv_t &ediv,
71+
const rand_t &rand
72+
) {
73+
entry_t *entry = as_entry(db_handle);
74+
if (entry) {
75+
tr_info("Write DB entry %d: local ediv %s rand %s", get_index(db_handle), to_string(ediv), to_string(rand));
76+
entry->local_keys.ediv = ediv;
77+
entry->local_keys.rand = rand;
78+
}
79+
}
80+
81+
void MemorySecurityDb::set_entry_peer_ltk(
82+
entry_handle_t db_handle,
83+
const ltk_t &ltk
84+
) {
85+
entry_t *entry = as_entry(db_handle);
86+
if (entry) {
87+
tr_info("Write DB entry %d: peer ltk %s", get_index(db_handle), to_string(ltk));
88+
entry->peer_keys.ltk = ltk;
89+
entry->flags.ltk_stored = true;
90+
}
91+
}
92+
93+
void MemorySecurityDb::set_entry_peer_ediv_rand(
94+
entry_handle_t db_handle,
95+
const ediv_t &ediv,
96+
const rand_t &rand
97+
) {
98+
entry_t *entry = as_entry(db_handle);
99+
if (entry) {
100+
tr_info("Write DB entry %d: peer ediv %s rand %s", get_index(db_handle), to_string(ediv), to_string(rand));
101+
entry->peer_keys.ediv = ediv;
102+
entry->peer_keys.rand = rand;
103+
}
104+
}
105+
106+
void MemorySecurityDb::set_entry_peer_irk(
107+
entry_handle_t db_handle,
108+
const irk_t &irk
109+
) {
110+
entry_t *entry = as_entry(db_handle);
111+
if (entry) {
112+
tr_info("Write DB entry %d: peer irk %s", get_index(db_handle), to_string(irk));
113+
entry->peer_identity.irk = irk;
114+
entry->flags.irk_stored = true;
115+
}
116+
}
117+
118+
void MemorySecurityDb::set_entry_peer_bdaddr(
119+
entry_handle_t db_handle,
120+
bool address_is_public,
121+
const address_t &peer_address
122+
) {
123+
entry_t *entry = as_entry(db_handle);
124+
if (entry) {
125+
tr_info("Write DB entry %d: %s peer address %s", get_index(db_handle), address_is_public? "public" : "private", to_string(peer_address));
126+
entry->peer_identity.identity_address = peer_address;
127+
entry->peer_identity.identity_address_is_public = address_is_public;
128+
}
129+
}
130+
131+
void MemorySecurityDb::set_entry_peer_csrk(
132+
entry_handle_t db_handle,
133+
const csrk_t &csrk
134+
) {
135+
entry_t *entry = as_entry(db_handle);
136+
if (entry) {
137+
tr_info("Write DB entry %d: peer csrk %s", get_index(db_handle), to_string(csrk));
138+
entry->flags.csrk_stored = true;
139+
entry->peer_signing.csrk = csrk;
140+
}
141+
}
142+
143+
void MemorySecurityDb::set_entry_peer_sign_counter(
144+
entry_handle_t db_handle,
145+
sign_count_t sign_counter
146+
) {
147+
entry_t *entry = as_entry(db_handle);
148+
if (entry) {
149+
tr_info("Write DB entry %d: sign counter %lu", get_index(db_handle), sign_counter);
150+
entry->peer_signing.counter = sign_counter;
151+
}
152+
}
153+
154+
uint8_t MemorySecurityDb::get_entry_count() {
155+
return BLE_SECURITY_DATABASE_MAX_ENTRIES;
156+
}
157+
158+
SecurityDistributionFlags_t *MemorySecurityDb::get_entry_handle_by_index(uint8_t index) {
159+
if (index < BLE_SECURITY_DATABASE_MAX_ENTRIES) {
160+
return &_entries[index].flags;
161+
} else {
162+
return nullptr;
163+
}
164+
}
165+
166+
void MemorySecurityDb::reset_entry(entry_handle_t db_entry) {
167+
auto *entry = reinterpret_cast<entry_t*>(db_entry);
168+
*entry = entry_t();
169+
}
170+
171+
SecurityEntryIdentity_t *MemorySecurityDb::read_in_entry_peer_identity(entry_handle_t db_entry) {
172+
auto *entry = reinterpret_cast<entry_t*>(db_entry);
173+
return &entry->peer_identity;
174+
}
175+
176+
SecurityEntryKeys_t *MemorySecurityDb::read_in_entry_peer_keys(entry_handle_t db_entry) {
177+
auto *entry = reinterpret_cast<entry_t*>(db_entry);
178+
return &entry->peer_keys;
179+
}
180+
181+
SecurityEntryKeys_t *MemorySecurityDb::read_in_entry_local_keys(entry_handle_t db_entry) {
182+
auto *entry = reinterpret_cast<entry_t*>(db_entry);
183+
return &entry->local_keys;
184+
}
185+
186+
SecurityEntrySigning_t *MemorySecurityDb::read_in_entry_peer_signing(entry_handle_t db_entry) {
187+
auto *entry = reinterpret_cast<entry_t*>(db_entry);
188+
return &entry->peer_signing;
189+
}
190+
191+
uint8_t MemorySecurityDb::get_index(const entry_handle_t db_handle) const
192+
{
193+
return reinterpret_cast<entry_t*>(db_handle) - _entries;
194+
}
195+
196+
} /* namespace ble */

0 commit comments

Comments
 (0)