Skip to content

Commit 4c78896

Browse files
nimble/host: Add support for new type of object in the storage.
Add support for BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT. This is used for storing values of Client Supported Features for clients with a trusted relationship.
1 parent b0f06d8 commit 4c78896

File tree

7 files changed

+494
-5
lines changed

7 files changed

+494
-5
lines changed

nimble/host/include/host/ble_store.h

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <inttypes.h>
3131
#include "nimble/ble.h"
32+
#include "../../host/src/ble_gatt_priv.h"
3233

3334
#ifdef __cplusplus
3435
extern "C" {
@@ -40,13 +41,16 @@ extern "C" {
4041
* @{
4142
*/
4243
/** Object type: Our security material. */
43-
#define BLE_STORE_OBJ_TYPE_OUR_SEC 1
44+
#define BLE_STORE_OBJ_TYPE_OUR_SEC 1
4445

4546
/** Object type: Peer security material. */
46-
#define BLE_STORE_OBJ_TYPE_PEER_SEC 2
47+
#define BLE_STORE_OBJ_TYPE_PEER_SEC 2
4748

4849
/** Object type: Client Characteristic Configuration Descriptor. */
49-
#define BLE_STORE_OBJ_TYPE_CCCD 3
50+
#define BLE_STORE_OBJ_TYPE_CCCD 3
51+
52+
/** Object type: Peer Client Supported Features. */
53+
#define BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT 4
5054

5155
/** @} */
5256

@@ -154,6 +158,33 @@ struct ble_store_value_cccd {
154158
unsigned value_changed:1;
155159
};
156160

161+
/**
162+
* Used as a key for lookups of stored client supported features of specific
163+
* peer. This struct corresponds to the BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT
164+
* store object type.
165+
*/
166+
struct ble_store_key_cl_sup_feat {
167+
/**
168+
* Key by peer identity address;
169+
* peer_addr=BLE_ADDR_NONE means don't key off peer.
170+
*/
171+
ble_addr_t peer_addr;
172+
173+
/** Number of results to skip; 0 means retrieve the first match. */
174+
uint8_t idx;
175+
};
176+
177+
/**
178+
* Represents a stored client supported features of specific peer. This struct
179+
* corresponds to the BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT store object type.
180+
*/
181+
struct ble_store_value_cl_sup_feat {
182+
/** The peer address associated with the stored supported features. */
183+
ble_addr_t peer_addr;
184+
/** Client supported features of a specific peer. */
185+
uint8_t peer_cl_sup_feat[BLE_GATT_CHR_CLI_SUP_FEAT_SZ];
186+
};
187+
157188
/**
158189
* Used as a key for store lookups. This union must be accompanied by an
159190
* object type code to indicate which field is valid.
@@ -163,6 +194,8 @@ union ble_store_key {
163194
struct ble_store_key_sec sec;
164195
/** Key for Client Characteristic Configuration Descriptor store lookups. */
165196
struct ble_store_key_cccd cccd;
197+
/** Key for Peer Client Supported Features store lookpus. */
198+
struct ble_store_key_cl_sup_feat feat;
166199
};
167200

168201
/**
@@ -174,6 +207,8 @@ union ble_store_value {
174207
struct ble_store_value_sec sec;
175208
/** Stored Client Characteristic Configuration Descriptor. */
176209
struct ble_store_value_cccd cccd;
210+
/** Stored Client Supported Features. */
211+
struct ble_store_value_cl_sup_feat feat;
177212
};
178213

179214
/** Represents an event associated with the BLE Store. */
@@ -556,6 +591,64 @@ int ble_store_write_cccd(const struct ble_store_value_cccd *value);
556591
*/
557592
int ble_store_delete_cccd(const struct ble_store_key_cccd *key);
558593

594+
/**
595+
* @brief Reads Client Supported Features value from a storage
596+
*
597+
* This function reads client supported features value from a storage based
598+
* on the provied key and stores the retrieved value in the specified output
599+
* structure.
600+
*
601+
* @param key A pointer to a 'ble_store_key_cl_sup_feat'
602+
* struct representing the key to identify
603+
* Client Supported Features value to be read.
604+
* @param out_value A pointer to a 'ble_store_value_cl_sup_feat'
605+
* struct to store the Client Supported
606+
* Features value read from a storage
607+
*
608+
* @return 0 if the Client Supported Features values was
609+
* successfully read and stored in the
610+
* 'out_value' structure;
611+
* Non-zero on error
612+
*/
613+
int
614+
ble_store_read_peer_cl_sup_feat(const struct ble_store_key_cl_sup_feat *key,
615+
struct ble_store_value_cl_sup_feat *out_value);
616+
617+
/**
618+
* @brief Writes a Client Supported Features value to a storage.
619+
*
620+
* This function writes a Client Supported Features value to a storage based on
621+
* the provided value
622+
*
623+
* @param value A pointer to a 'ble_store_value_cl_sup_feat'
624+
* structure representing the Client Supported
625+
* Features value to be written to a storage.
626+
*
627+
* @return 0 if the value was successfully written to
628+
* a storage;
629+
* Non-zero on error.
630+
*/
631+
int
632+
ble_store_write_peer_cl_sup_feat(const struct ble_store_value_cl_sup_feat
633+
*value);
634+
635+
/**
636+
* @brief Deletes a Client Supported Features value from a storage.
637+
*
638+
* This function deletes a Client Supported Features value from a storage based
639+
* on the provided key.
640+
*
641+
* @param key A pointer to a 'ble_store_key_cl_sup_feat'
642+
* structure identifying the Client Supported
643+
* Features value to be deleted from
644+
* a storage.
645+
*
646+
* @return 0 if the Client Supported Features value was
647+
* successfully written to a storage;
648+
* Non-zero on error.
649+
*/
650+
int ble_store_delete_peer_cl_sup_feat(const struct ble_store_key_cl_sup_feat
651+
*key);
559652

560653
/**
561654
* @brief Generates a storage key for a security material entry from its value.
@@ -587,6 +680,26 @@ void ble_store_key_from_value_sec(struct ble_store_key_sec *out_key,
587680
void ble_store_key_from_value_cccd(struct ble_store_key_cccd *out_key,
588681
const struct ble_store_value_cccd *value);
589682

683+
/**
684+
* @brief Generates a storage key for a Client Supported Features entry from
685+
* its value
686+
*
687+
* This function generates a storage key for a Client Supported Features value
688+
* entry based on the provided value.
689+
*
690+
* @param out_key A pointer to a 'ble_store_key_cl_sup_feat'
691+
* structure where the generated key will be
692+
* stored.
693+
* @param value A pointer to a 'ble_store_value_cl_sup_feat'
694+
* structure containing the Client Supported
695+
* Features value from which the key will be
696+
* generated.
697+
*/
698+
void
699+
ble_store_key_from_value_peer_cl_sup_feat(struct ble_store_key_cl_sup_feat
700+
*out_key,
701+
const struct ble_store_value_cl_sup_feat
702+
*value);
590703

591704
/**
592705
* @brief Generates a storage key from a value based on the object type.

nimble/host/src/ble_store.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,44 @@ ble_store_write_peer_sec(const struct ble_store_value_sec *value_sec)
249249
return 0;
250250
}
251251

252+
int
253+
ble_store_read_peer_cl_sup_feat(const struct ble_store_key_cl_sup_feat *key,
254+
struct ble_store_value_cl_sup_feat *out_value)
255+
{
256+
union ble_store_value *store_value;
257+
union ble_store_key *store_key;
258+
int rc;
259+
260+
store_key = (void *)key;
261+
store_value = (void *)out_value;
262+
rc = ble_store_read(BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT, store_key,
263+
store_value);
264+
return rc;
265+
}
266+
267+
int
268+
ble_store_write_peer_cl_sup_feat(const struct ble_store_value_cl_sup_feat
269+
*value)
270+
{
271+
union ble_store_value *store_value;
272+
int rc;
273+
274+
store_value = (void *)value;
275+
rc = ble_store_write(BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT, store_value);
276+
return rc;
277+
}
278+
279+
int
280+
ble_store_delete_peer_cl_sup_feat(const struct ble_store_key_cl_sup_feat *key)
281+
{
282+
union ble_store_key *store_key;
283+
int rc;
284+
285+
store_key = (void *)key;
286+
rc = ble_store_delete(BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT, store_key);
287+
return rc;
288+
}
289+
252290
int
253291
ble_store_read_cccd(const struct ble_store_key_cccd *key,
254292
struct ble_store_value_cccd *out_value)
@@ -302,6 +340,16 @@ ble_store_key_from_value_sec(struct ble_store_key_sec *out_key,
302340
out_key->idx = 0;
303341
}
304342

343+
void
344+
ble_store_key_from_value_peer_cl_sup_feat(struct ble_store_key_cl_sup_feat
345+
*out_key,
346+
const struct ble_store_value_cl_sup_feat
347+
*value)
348+
{
349+
out_key->peer_addr = value->peer_addr;
350+
out_key->idx = 0;
351+
}
352+
305353
void
306354
ble_store_key_from_value(int obj_type,
307355
union ble_store_key *out_key,
@@ -317,6 +365,10 @@ ble_store_key_from_value(int obj_type,
317365
ble_store_key_from_value_cccd(&out_key->cccd, &value->cccd);
318366
break;
319367

368+
case BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT:
369+
ble_store_key_from_value_peer_cl_sup_feat(&out_key->feat,
370+
&value->feat);
371+
320372
default:
321373
BLE_HS_DBG_ASSERT(0);
322374
break;
@@ -346,6 +398,10 @@ ble_store_iterate(int obj_type,
346398
key.cccd.peer_addr = *BLE_ADDR_ANY;
347399
pidx = &key.cccd.idx;
348400
break;
401+
case BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT:
402+
key.feat.peer_addr = *BLE_ADDR_ANY;
403+
pidx = &key.feat.idx;
404+
break;
349405
default:
350406
BLE_HS_DBG_ASSERT(0);
351407
return BLE_HS_EINVAL;
@@ -390,6 +446,7 @@ ble_store_clear(void)
390446
BLE_STORE_OBJ_TYPE_OUR_SEC,
391447
BLE_STORE_OBJ_TYPE_PEER_SEC,
392448
BLE_STORE_OBJ_TYPE_CCCD,
449+
BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT,
393450
};
394451
union ble_store_key key;
395452
int obj_type;

nimble/host/src/ble_store_util.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ ble_store_util_delete_peer(const ble_addr_t *peer_id_addr)
109109
return rc;
110110
}
111111

112+
memset(&key, 0, sizeof key);
113+
key.feat.peer_addr = *peer_id_addr;
114+
115+
rc = ble_store_util_delete_all(BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT, &key);
116+
if (rc != 0) {
117+
return rc;
118+
}
119+
112120
return 0;
113121
}
114122

@@ -192,6 +200,7 @@ ble_store_util_status_rr(struct ble_store_status_event *event, void *arg)
192200
switch (event->overflow.obj_type) {
193201
case BLE_STORE_OBJ_TYPE_OUR_SEC:
194202
case BLE_STORE_OBJ_TYPE_PEER_SEC:
203+
case BLE_STORE_OBJ_TYPE_PEER_CL_SUP_FEAT:
195204
return ble_gap_unpair_oldest_peer();
196205
case BLE_STORE_OBJ_TYPE_CCCD:
197206
/* Try unpairing oldest peer except current peer */

0 commit comments

Comments
 (0)