Repository: incubator-mynewt-core Updated Branches: refs/heads/develop f3a009f49 -> 8f78cd3bc
bleprph/bletiny - use updated BLE host store. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/8f78cd3b Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/8f78cd3b Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/8f78cd3b Branch: refs/heads/develop Commit: 8f78cd3bcd7048f9d94c6aaed28f01e02a17b24f Parents: 62db4fe Author: Christopher Collins <[email protected]> Authored: Wed May 25 12:00:38 2016 -0700 Committer: Christopher Collins <[email protected]> Committed: Wed May 25 12:16:46 2016 -0700 ---------------------------------------------------------------------- apps/bleprph/src/store.c | 32 ++--- apps/bletiny/src/store.c | 289 +++++++++++++++++++++++++++++++++++------- 2 files changed, 258 insertions(+), 63 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8f78cd3b/apps/bleprph/src/store.c ---------------------------------------------------------------------- diff --git a/apps/bleprph/src/store.c b/apps/bleprph/src/store.c index 0439ee6..8e3e396 100644 --- a/apps/bleprph/src/store.c +++ b/apps/bleprph/src/store.c @@ -34,19 +34,19 @@ #include "bleprph.h" -#define STORE_MAX_OUR_LTKS 4 +#define STORE_MAX_SLV_LTKS 4 -static struct ble_store_value_ltk store_our_ltks[STORE_MAX_OUR_LTKS]; -static int store_num_our_ltks; +static struct ble_store_value_ltk store_slv_ltks[STORE_MAX_SLV_LTKS]; +static int store_num_slv_ltks; static int -store_find_our_ltk(struct ble_store_key_ltk *key_ltk) +store_find_slv_ltk(struct ble_store_key_ltk *key_ltk) { struct ble_store_value_ltk *cur; int i; - for (i = 0; i < store_num_our_ltks; i++) { - cur = store_our_ltks + i; + for (i = 0; i < store_num_slv_ltks; i++) { + cur = store_slv_ltks + i; if (cur->ediv == key_ltk->ediv && cur->rand_num == key_ltk->rand_num) { @@ -79,7 +79,7 @@ store_read(int obj_type, union ble_store_key *key, union ble_store_value *dst) int idx; switch (obj_type) { - case BLE_STORE_OBJ_TYPE_OUR_LTK: + case BLE_STORE_OBJ_TYPE_MST_LTK: /* An encryption procedure (bonding) is being attempted. The nimble * stack is asking us to look in our key database for a long-term key * corresponding to the specified ediv and random number. @@ -91,11 +91,11 @@ store_read(int obj_type, union ble_store_key *key, union ble_store_value *dst) * result. The nimble stack will use this key if this function returns * success. */ - idx = store_find_our_ltk(&key->ltk); + idx = store_find_slv_ltk(&key->ltk); if (idx == -1) { return BLE_HS_ENOENT; } - dst->ltk = store_our_ltks[idx]; + dst->ltk = store_slv_ltks[idx]; return 0; default: @@ -116,23 +116,23 @@ store_write(int obj_type, union ble_store_value *val) int idx; switch (obj_type) { - case BLE_STORE_OBJ_TYPE_OUR_LTK: - BLEPRPH_LOG(INFO, "persisting our ltk; "); + case BLE_STORE_OBJ_TYPE_MST_LTK: + BLEPRPH_LOG(INFO, "persisting slv ltk; "); store_print_ltk(&val->ltk); ble_store_key_from_value_ltk(&key_ltk, &val->ltk); - idx = store_find_our_ltk(&key_ltk); + idx = store_find_slv_ltk(&key_ltk); if (idx == -1) { - if (store_num_our_ltks >= STORE_MAX_OUR_LTKS) { + if (store_num_slv_ltks >= STORE_MAX_SLV_LTKS) { BLEPRPH_LOG(INFO, "error persisting LTK; too many entries\n"); return BLE_HS_ENOMEM; } - idx = store_num_our_ltks; - store_num_our_ltks++; + idx = store_num_slv_ltks; + store_num_slv_ltks++; } - store_our_ltks[idx] = val->ltk; + store_slv_ltks[idx] = val->ltk; return 0; default: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8f78cd3b/apps/bletiny/src/store.c ---------------------------------------------------------------------- diff --git a/apps/bletiny/src/store.c b/apps/bletiny/src/store.c index 904b328..2297466 100644 --- a/apps/bletiny/src/store.c +++ b/apps/bletiny/src/store.c @@ -35,19 +35,41 @@ #include "bletiny.h" -#define STORE_MAX_OUR_LTKS 4 +#define STORE_MAX_SLV_LTKS 4 +#define STORE_MAX_MST_LTKS 4 +#define STORE_MAX_CCCDS 16 -static struct ble_store_value_ltk store_our_ltks[STORE_MAX_OUR_LTKS]; -static int store_num_our_ltks; +static struct ble_store_value_ltk store_slv_ltks[STORE_MAX_SLV_LTKS]; +static int store_num_slv_ltks; + +static struct ble_store_value_ltk store_mst_ltks[STORE_MAX_MST_LTKS]; +static int store_num_mst_ltks; + +static struct ble_store_value_cccd store_cccds[STORE_MAX_CCCDS]; +static int store_num_cccds; + +/***************************************************************************** + * $ltk * + *****************************************************************************/ + +static void +store_print_ltk(struct ble_store_value_ltk *ltk) +{ + console_printf("ediv=%u rand=%llu authenticated=%d ", ltk->ediv, + ltk->rand_num, ltk->authenticated); + console_printf("ltk="); + print_bytes(ltk->key, 16); + console_printf("\n"); +} static int -store_find_our_ltk(struct ble_store_key_ltk *key_ltk) +store_find_slv_ltk(struct ble_store_key_ltk *key_ltk) { struct ble_store_value_ltk *cur; int i; - for (i = 0; i < store_num_our_ltks; i++) { - cur = store_our_ltks + i; + for (i = 0; i < store_num_slv_ltks; i++) { + cur = store_slv_ltks + i; if (cur->ediv == key_ltk->ediv && cur->rand_num == key_ltk->rand_num) { @@ -59,45 +81,226 @@ store_find_our_ltk(struct ble_store_key_ltk *key_ltk) return -1; } -static void -store_print_ltk(struct ble_store_value_ltk *ltk) +static int +store_read_slv_ltk(struct ble_store_key_ltk *key_ltk, + struct ble_store_value_ltk *value_ltk) { - console_printf("ediv=%u rand=%llu authenticated=%d ", ltk->ediv, - ltk->rand_num, ltk->authenticated); - console_printf("ltk="); - print_bytes(ltk->key, 16); - console_printf("\n"); + int idx; + + idx = store_find_slv_ltk(key_ltk); + if (idx == -1) { + return BLE_HS_ENOENT; + } + + *value_ltk = store_slv_ltks[idx]; + return 0; +} + +static int +store_write_slv_ltk(struct ble_store_value_ltk *value_ltk) +{ + struct ble_store_key_ltk key_ltk; + int idx; + + console_printf("persisting slv ltk; "); + store_print_ltk(value_ltk); + + ble_store_key_from_value_ltk(&key_ltk, value_ltk); + idx = store_find_slv_ltk(&key_ltk); + if (idx == -1) { + if (store_num_slv_ltks >= STORE_MAX_SLV_LTKS) { + console_printf("error persisting slv ltk; too many entries (%d)\n", + store_num_slv_ltks); + return BLE_HS_ENOMEM; + } + + idx = store_num_slv_ltks; + store_num_slv_ltks++; + } + + store_slv_ltks[idx] = *value_ltk; + return 0; +} + +static int +store_find_mst_ltk(struct ble_store_key_ltk *key_ltk) +{ + struct ble_store_value_ltk *cur; + int i; + + for (i = 0; i < store_num_mst_ltks; i++) { + cur = store_mst_ltks + i; + + if (cur->ediv == key_ltk->ediv && + cur->rand_num == key_ltk->rand_num) { + + return i; + } + } + + return -1; +} + +static int +store_read_mst_ltk(struct ble_store_key_ltk *key_ltk, + struct ble_store_value_ltk *value_ltk) +{ + int idx; + + idx = store_find_mst_ltk(key_ltk); + if (idx == -1) { + return BLE_HS_ENOENT; + } + + *value_ltk = store_mst_ltks[idx]; + return 0; +} + +static int +store_write_mst_ltk(struct ble_store_value_ltk *value_ltk) +{ + struct ble_store_key_ltk key_ltk; + int idx; + + console_printf("persisting mst ltk; "); + store_print_ltk(value_ltk); + + ble_store_key_from_value_ltk(&key_ltk, value_ltk); + idx = store_find_mst_ltk(&key_ltk); + if (idx == -1) { + if (store_num_mst_ltks >= STORE_MAX_MST_LTKS) { + console_printf("error persisting mst ltk; too many entries " + "(%d)\n", store_num_mst_ltks); + return BLE_HS_ENOMEM; + } + + idx = store_num_mst_ltks; + store_num_mst_ltks++; + } + + store_mst_ltks[idx] = *value_ltk; + return 0; +} + +/***************************************************************************** + * $cccd * + *****************************************************************************/ + +static int +store_find_cccd(struct ble_store_key_cccd *key) +{ + struct ble_store_value_cccd *cccd; + int skipped; + int i; + + skipped = 0; + for (i = 0; i < store_num_cccds; i++) { + cccd = store_cccds + i; + + if (key->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) { + if (cccd->peer_addr_type != key->peer_addr_type) { + continue; + } + + if (memcmp(cccd->peer_addr, key->peer_addr, 6) != 0) { + continue; + } + } + + if (key->chr_val_handle != 0) { + if (cccd->chr_val_handle != key->chr_val_handle) { + continue; + } + } + + if (key->idx > skipped) { + skipped++; + continue; + } + + return i; + } + + return -1; } +static int +store_read_cccd(struct ble_store_key_cccd *key_cccd, + struct ble_store_value_cccd *value_cccd) +{ + int idx; + + idx = store_find_cccd(key_cccd); + if (idx == -1) { + return BLE_HS_ENOENT; + } + + *value_cccd = store_cccds[idx]; + return 0; +} + +static int +store_write_cccd(struct ble_store_value_cccd *value_cccd) +{ + struct ble_store_key_cccd key_cccd; + int idx; + + ble_store_key_from_value_cccd(&key_cccd, value_cccd); + idx = store_find_cccd(&key_cccd); + if (idx == -1) { + if (store_num_cccds >= STORE_MAX_SLV_LTKS) { + console_printf("error persisting cccd; too many entries (%d)\n", + store_num_cccds); + return BLE_HS_ENOMEM; + } + + idx = store_num_cccds; + store_num_cccds++; + } + + store_cccds[idx] = *value_cccd; + return 0; +} + +/***************************************************************************** + * $api * + *****************************************************************************/ + /** - * Searches the database for a long-term key matching the specified criteria. + * Searches the database for an object matching the specified criteria. * * @return 0 if a key was found; else BLE_HS_ENOENT. */ int -store_read(int obj_type, union ble_store_key *key, union ble_store_value *dst) +store_read(int obj_type, union ble_store_key *key, + union ble_store_value *value) { - int idx; + int rc; switch (obj_type) { - case BLE_STORE_OBJ_TYPE_OUR_LTK: + case BLE_STORE_OBJ_TYPE_MST_LTK: /* An encryption procedure (bonding) is being attempted. The nimble * stack is asking us to look in our key database for a long-term key * corresponding to the specified ediv and random number. - */ - console_printf("looking up ltk with ediv=0x%02x rand=0x%llx\n", - key->ltk.ediv, key->ltk.rand_num); - - /* Perform a key lookup and populate the context object with the + * + * Perform a key lookup and populate the context object with the * result. The nimble stack will use this key if this function returns * success. */ - idx = store_find_our_ltk(&key->ltk); - if (idx == -1) { - return BLE_HS_ENOENT; - } - dst->ltk = store_our_ltks[idx]; - return 0; + console_printf("looking up slv ltk with ediv=0x%02x rand=0x%llx\n", + key->ltk.ediv, key->ltk.rand_num); + rc = store_read_slv_ltk(&key->ltk, &value->ltk); + return rc; + + case BLE_STORE_OBJ_TYPE_SLV_LTK: + console_printf("looking up mst ltk with ediv=0x%02x rand=0x%llx\n", + key->ltk.ediv, key->ltk.rand_num); + rc = store_read_mst_ltk(&key->ltk, &value->ltk); + return rc; + + case BLE_STORE_OBJ_TYPE_CCCD: + rc = store_read_cccd(&key->cccd, &value->cccd); + return rc; default: return BLE_HS_ENOTSUP; @@ -105,7 +308,7 @@ store_read(int obj_type, union ble_store_key *key, union ble_store_value *dst) } /** - * Adds the specified key to the database. + * Adds the specified object to the database. * * @return 0 on success; BLE_HS_ENOMEM if the database is * full. @@ -113,28 +316,20 @@ store_read(int obj_type, union ble_store_key *key, union ble_store_value *dst) int store_write(int obj_type, union ble_store_value *val) { - struct ble_store_key_ltk key_ltk; - int idx; + int rc; switch (obj_type) { - case BLE_STORE_OBJ_TYPE_OUR_LTK: - console_printf("persisting our ltk; "); - store_print_ltk(&val->ltk); - - ble_store_key_from_value_ltk(&key_ltk, &val->ltk); - idx = store_find_our_ltk(&key_ltk); - if (idx == -1) { - if (store_num_our_ltks >= STORE_MAX_OUR_LTKS) { - console_printf("error persisting LTK; too many entries\n"); - return BLE_HS_ENOMEM; - } + case BLE_STORE_OBJ_TYPE_MST_LTK: + rc = store_write_slv_ltk(&val->ltk); + return rc; - idx = store_num_our_ltks; - store_num_our_ltks++; - } + case BLE_STORE_OBJ_TYPE_SLV_LTK: + rc = store_write_mst_ltk(&val->ltk); + return rc; - store_our_ltks[idx] = val->ltk; - return 0; + case BLE_STORE_OBJ_TYPE_CCCD: + rc = store_write_cccd(&val->cccd); + return rc; default: return BLE_HS_ENOTSUP;
