Skip to content

Commit

Permalink
NimBLE: Added support of enc adv data in ble_store
Browse files Browse the repository at this point in the history
  • Loading branch information
IshaESP committed Jun 28, 2023
1 parent 2f98b89 commit 75f611c
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 1 deletion.
48 changes: 47 additions & 1 deletion nimble/host/include/host/ble_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ extern "C" {
#define BLE_STORE_OBJ_TYPE_PEER_SEC 2
#define BLE_STORE_OBJ_TYPE_CCCD 3

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
#define BLE_STORE_OBJ_TYPE_ENC_ADV_DATA 4
#endif

/** Failed to persist record; insufficient storage capacity. */
#define BLE_STORE_EVENT_OVERFLOW 1

Expand Down Expand Up @@ -112,13 +116,44 @@ struct ble_store_value_cccd {
unsigned value_changed:1;
};

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
/**
* Used as a key for lookups of encrypted advertising data. This struct corresponds
* to the BLE_STORE_OBJ_TYPE_ENC_ADV_DATA store object type.
*/
struct ble_store_key_ead {
/**
* Key by peer identity address;
* peer_addr=BLE_ADDR_NONE means don't key off peer.
*/
ble_addr_t peer_addr;

/** Number of results to skip; 0 means retrieve the first match. */
uint8_t idx;
};

/**
* Represents a stored encrypted advertising data. This struct corresponds
* to the BLE_STORE_OBJ_TYPE_ENC_ADV_DATA store object type.
*/
struct ble_store_value_ead {
ble_addr_t peer_addr;
unsigned km_present : 1;
struct key_material *km;
};
#endif

/**
* Used as a key for store lookups. This union must be accompanied by an
* object type code to indicate which field is valid.
*/
union ble_store_key {
struct ble_store_key_sec sec;
struct ble_store_key_cccd cccd;
#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
struct ble_store_key_ead ead;
#endif

};

/**
Expand All @@ -128,6 +163,10 @@ union ble_store_key {
union ble_store_value {
struct ble_store_value_sec sec;
struct ble_store_value_cccd cccd;
#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
struct ble_store_value_ead ead;
#endif

};

struct ble_store_status_event {
Expand Down Expand Up @@ -298,7 +337,14 @@ void ble_store_key_from_value_sec(struct ble_store_key_sec *out_key,
const struct ble_store_value_sec *value);
void ble_store_key_from_value_cccd(struct ble_store_key_cccd *out_key,
const struct ble_store_value_cccd *value);

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
int ble_store_read_ead(const struct ble_store_key_ead *key,
struct ble_store_value_ead *out_value);
int ble_store_write_ead(const struct ble_store_value_ead *value);
int ble_store_delete_ead(const struct ble_store_key_ead *key);
void ble_store_key_from_value_ead(struct ble_store_key_ead *out_key,
const struct ble_store_value_ead *value);
#endif
void ble_store_key_from_value(int obj_type,
union ble_store_key *out_key,
const union ble_store_value *value);
Expand Down
63 changes: 63 additions & 0 deletions nimble/host/src/ble_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,53 @@ ble_store_key_from_value_sec(struct ble_store_key_sec *out_key,
out_key->idx = 0;
}

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
int
ble_store_read_ead(const struct ble_store_key_ead *key,
struct ble_store_value_ead *out_value)
{
union ble_store_value *store_value;
union ble_store_key *store_key;
int rc;

store_key = (void *)key;
store_value = (void *)out_value;

rc = ble_store_read(BLE_STORE_OBJ_TYPE_ENC_ADV_DATA, store_key, store_value);
return rc;
}

int
ble_store_write_ead(const struct ble_store_value_ead *value)
{
union ble_store_value *store_value;
int rc;

store_value = (void *)value;
rc = ble_store_write(BLE_STORE_OBJ_TYPE_ENC_ADV_DATA, store_value);
return rc;
}

int
ble_store_delete_ead(const struct ble_store_key_ead *key)
{
union ble_store_key *store_key;
int rc;

store_key = (void *)key;
rc = ble_store_delete(BLE_STORE_OBJ_TYPE_ENC_ADV_DATA, store_key);
return rc;
}

void
ble_store_key_from_value_ead(struct ble_store_key_ead *out_key,
const struct ble_store_value_ead *value)
{
out_key->peer_addr = value->peer_addr;
out_key->idx = 0;
}
#endif

void
ble_store_key_from_value(int obj_type,
union ble_store_key *out_key,
Expand All @@ -317,6 +364,12 @@ ble_store_key_from_value(int obj_type,
ble_store_key_from_value_cccd(&out_key->cccd, &value->cccd);
break;

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
case BLE_STORE_OBJ_TYPE_ENC_ADV_DATA:
ble_store_key_from_value_ead(&out_key->ead, &value->ead);
break;
#endif

default:
BLE_HS_DBG_ASSERT(0);
break;
Expand Down Expand Up @@ -346,6 +399,13 @@ ble_store_iterate(int obj_type,
key.cccd.peer_addr = *BLE_ADDR_ANY;
pidx = &key.cccd.idx;
break;
#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
case BLE_STORE_OBJ_TYPE_ENC_ADV_DATA:
key.ead.peer_addr = *BLE_ADDR_ANY;
pidx = &key.ead.idx;
break;
#endif

default:
BLE_HS_DBG_ASSERT(0);
return BLE_HS_EINVAL;
Expand Down Expand Up @@ -390,6 +450,9 @@ ble_store_clear(void)
BLE_STORE_OBJ_TYPE_OUR_SEC,
BLE_STORE_OBJ_TYPE_PEER_SEC,
BLE_STORE_OBJ_TYPE_CCCD,
#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
BLE_STORE_OBJ_TYPE_ENC_ADV_DATA,
#endif
};
union ble_store_key key;
int obj_type;
Expand Down
10 changes: 10 additions & 0 deletions nimble/host/src/ble_store_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ ble_store_util_delete_peer(const ble_addr_t *peer_id_addr)
return rc;
}

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
memset(&key, 0, sizeof key);
key.ead.peer_addr = *peer_id_addr;

rc = ble_store_util_delete_all(BLE_STORE_OBJ_TYPE_ENC_ADV_DATA, &key);
if (rc != 0) {
return rc;
}
#endif

return 0;
}

Expand Down
133 changes: 133 additions & 0 deletions nimble/host/store/config/src/ble_store_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ struct ble_store_value_cccd
ble_store_config_cccds[MYNEWT_VAL(BLE_STORE_MAX_CCCDS)];
int ble_store_config_num_cccds;

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
struct ble_store_value_ead
ble_store_config_eads[MYNEWT_VAL(BLE_STORE_MAX_EADS)];
int ble_store_config_num_eads;
#endif

/*****************************************************************************
* $sec *
*****************************************************************************/
Expand Down Expand Up @@ -394,6 +400,112 @@ ble_store_config_write_cccd(const struct ble_store_value_cccd *value_cccd)
return 0;
}

/*****************************************************************************
* $ead *
*****************************************************************************/
#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
static int
ble_store_config_find_ead(const struct ble_store_key_ead *key)
{
struct ble_store_value_ead *ead;
int skipped;
int i;

skipped = 0;
for (i = 0; i < ble_store_config_num_eads; i++) {
ead = ble_store_config_eads + i;

if (ble_addr_cmp(&key->peer_addr, BLE_ADDR_ANY)) {
if (ble_addr_cmp(&ead->peer_addr, &key->peer_addr)) {
continue;
}
}

if (key->idx > skipped) {
skipped++;
continue;
}

return i;
}

return -1;
}

static int
ble_store_config_delete_ead(const struct ble_store_key_ead *key_ead)
{
int idx;
int rc;

idx = ble_store_config_find_ead(key_ead);
if (idx == -1) {
return BLE_HS_ENOENT;
}

rc = ble_store_config_delete_obj(ble_store_config_eads,
sizeof *ble_store_config_eads,
idx,
&ble_store_config_num_eads);
if (rc != 0) {
return rc;
}

rc = ble_store_config_persist_eads();
if (rc != 0) {
return rc;
}

return 0;
}

static int
ble_store_config_read_ead(const struct ble_store_key_ead *key_ead,
struct ble_store_value_ead *value_ead)
{
int idx;

idx = ble_store_config_find_ead(key_ead);
if (idx == -1) {
return BLE_HS_ENOENT;
}

*value_ead = ble_store_config_eads[idx];
return 0;
}

static int
ble_store_config_write_ead(const struct ble_store_value_ead *value_ead)
{
struct ble_store_key_ead key_ead;
int idx;
int rc;

ble_store_key_from_value_ead(&key_ead, value_ead);
idx = ble_store_config_find_ead(&key_ead);

if (idx == -1) {
if (ble_store_config_num_eads >= MYNEWT_VAL(BLE_STORE_MAX_CCCDS)) {
BLE_HS_LOG(DEBUG, "error persisting ead; too many entries (%d)\n",
ble_store_config_num_eads);
return BLE_HS_ESTORE_CAP;
}

idx = ble_store_config_num_eads;
ble_store_config_num_eads++;
}

ble_store_config_eads[idx] = *value_ead;

rc = ble_store_config_persist_eads();
if (rc != 0) {
return rc;
}

return 0;
}
#endif

/*****************************************************************************
* $api *
*****************************************************************************/
Expand Down Expand Up @@ -436,6 +548,12 @@ ble_store_config_read(int obj_type, const union ble_store_key *key,
rc = ble_store_config_read_cccd(&key->cccd, &value->cccd);
return rc;

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
case BLE_STORE_OBJ_TYPE_ENC_ADV_DATA:
rc = ble_store_config_read_ead(&key->ead, &value->ead);
return rc;
#endif

default:
return BLE_HS_ENOTSUP;
}
Expand Down Expand Up @@ -465,6 +583,12 @@ ble_store_config_write(int obj_type, const union ble_store_value *val)
rc = ble_store_config_write_cccd(&val->cccd);
return rc;

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
case BLE_STORE_OBJ_TYPE_ENC_ADV_DATA:
rc = ble_store_config_write_ead(&val->ead);
return rc;
#endif

default:
return BLE_HS_ENOTSUP;
}
Expand All @@ -488,6 +612,12 @@ ble_store_config_delete(int obj_type, const union ble_store_key *key)
rc = ble_store_config_delete_cccd(&key->cccd);
return rc;

#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
case BLE_STORE_OBJ_TYPE_ENC_ADV_DATA:
rc = ble_store_config_write_ead(&key->ead);
return rc;
#endif

default:
return BLE_HS_ENOTSUP;
}
Expand All @@ -507,6 +637,9 @@ ble_store_config_init(void)
ble_store_config_num_our_secs = 0;
ble_store_config_num_peer_secs = 0;
ble_store_config_num_cccds = 0;
#if MYNEWT_VAL(BLE_ENC_ADV_DATA)
ble_store_config_num_eads = 0;
#endif

ble_store_config_conf_init();
}
Loading

0 comments on commit 75f611c

Please sign in to comment.