Repository: incubator-mynewt-core Updated Branches: refs/heads/develop b6fa07ad9 -> 2328ac6bf
bleprph/bletiny - adjust for BLE host changes. 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/2328ac6b Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/2328ac6b Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/2328ac6b Branch: refs/heads/develop Commit: 2328ac6bf5f247d97094f0515df1f49f60b24024 Parents: cc094c5 Author: Christopher Collins <[email protected]> Authored: Wed May 25 15:20:48 2016 -0700 Committer: Christopher Collins <[email protected]> Committed: Wed May 25 15:21:22 2016 -0700 ---------------------------------------------------------------------- apps/bleprph/src/store.c | 252 ++++++++++++++++++++++++++++++++++-------- apps/bletiny/src/store.c | 98 +++++++++------- 2 files changed, 261 insertions(+), 89 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2328ac6b/apps/bleprph/src/store.c ---------------------------------------------------------------------- diff --git a/apps/bleprph/src/store.c b/apps/bleprph/src/store.c index 8e3e396..e80a625 100644 --- a/apps/bleprph/src/store.c +++ b/apps/bleprph/src/store.c @@ -30,73 +30,241 @@ #include <inttypes.h> #include <string.h> +#include "console/console.h" #include "host/ble_hs.h" #include "bleprph.h" #define STORE_MAX_SLV_LTKS 4 +#define STORE_MAX_CCCDS 16 static struct ble_store_value_ltk store_slv_ltks[STORE_MAX_SLV_LTKS]; static int store_num_slv_ltks; +static struct ble_store_value_cccd store_cccds[STORE_MAX_CCCDS]; +static int store_num_cccds; + +/***************************************************************************** + * $ltk * + *****************************************************************************/ + +static void +store_print_value_ltk(struct ble_store_value_ltk *ltk) +{ + BLEPRPH_LOG(INFO, "ediv=%u rand=%llu authenticated=%d ", ltk->ediv, + ltk->rand_num, ltk->authenticated); + BLEPRPH_LOG(INFO, "ltk="); + print_bytes(ltk->key, 16); + BLEPRPH_LOG(INFO, "\n"); +} + +static void +store_print_key_ltk(struct ble_store_key_ltk *key_ltk) +{ + if (key_ltk->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) { + BLEPRPH_LOG(INFO, "peer_addr_type=%d peer_addr=", + key_ltk->peer_addr_type); + print_bytes(key_ltk->peer_addr, 6); + BLEPRPH_LOG(INFO, " "); + } + if (key_ltk->ediv_present) { + BLEPRPH_LOG(INFO, "ediv=0x%02x ", key_ltk->ediv); + } + if (key_ltk->rand_num_present) { + BLEPRPH_LOG(INFO, "rand=0x%llx ", key_ltk->rand_num); + } +} + static int -store_find_slv_ltk(struct ble_store_key_ltk *key_ltk) +store_find_ltk(struct ble_store_key_ltk *key_ltk, + struct ble_store_value_ltk *value_ltks, int num_value_ltks) { struct ble_store_value_ltk *cur; int i; - for (i = 0; i < store_num_slv_ltks; i++) { - cur = store_slv_ltks + i; + for (i = 0; i < num_value_ltks; i++) { + cur = value_ltks + i; + + if (key_ltk->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) { + if (cur->peer_addr_type != key_ltk->peer_addr_type) { + continue; + } + + if (memcmp(cur->peer_addr, key_ltk->peer_addr, + sizeof cur->peer_addr) != 0) { + continue; + } + } - if (cur->ediv == key_ltk->ediv && - cur->rand_num == key_ltk->rand_num) { + if (key_ltk->ediv_present && cur->ediv != key_ltk->ediv) { + continue; + } - return i; + if (key_ltk->rand_num_present && cur->rand_num != key_ltk->rand_num) { + continue; } + + return i; } 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) { - BLEPRPH_LOG(INFO, "ediv=%u rand=%llu authenticated=%d ", ltk->ediv, - ltk->rand_num, ltk->authenticated); - BLEPRPH_LOG(INFO, "ltk="); - print_bytes(ltk->key, 16); - BLEPRPH_LOG(INFO, "\n"); + int idx; + + idx = store_find_ltk(key_ltk, store_slv_ltks, store_num_slv_ltks); + 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; + + BLEPRPH_LOG(INFO, "persisting slv ltk; "); + store_print_value_ltk(value_ltk); + + ble_store_key_from_value_ltk(&key_ltk, value_ltk); + idx = store_find_ltk(&key_ltk, store_slv_ltks, store_num_slv_ltks); + if (idx == -1) { + if (store_num_slv_ltks >= STORE_MAX_SLV_LTKS) { + BLEPRPH_LOG(INFO, "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; +} + +/***************************************************************************** + * $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) { + BLEPRPH_LOG(INFO, "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_MST_LTK: + case BLE_STORE_OBJ_TYPE_SLV_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. - */ - BLEPRPH_LOG(INFO, "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_slv_ltk(&key->ltk); - if (idx == -1) { - return BLE_HS_ENOENT; - } - dst->ltk = store_slv_ltks[idx]; - return 0; + BLEPRPH_LOG(INFO, "looking up slv ltk; "); + store_print_key_ltk(&key->ltk); + BLEPRPH_LOG(INFO, "\n"); + rc = store_read_slv_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; @@ -104,7 +272,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. @@ -112,28 +280,16 @@ 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_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_slv_ltk(&key_ltk); - if (idx == -1) { - 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_slv_ltks; - store_num_slv_ltks++; - } + case BLE_STORE_OBJ_TYPE_SLV_LTK: + rc = store_write_slv_ltk(&val->ltk); + return rc; - store_slv_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; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2328ac6b/apps/bletiny/src/store.c ---------------------------------------------------------------------- diff --git a/apps/bletiny/src/store.c b/apps/bletiny/src/store.c index 2297466..b3403ae 100644 --- a/apps/bletiny/src/store.c +++ b/apps/bletiny/src/store.c @@ -53,7 +53,7 @@ static int store_num_cccds; *****************************************************************************/ static void -store_print_ltk(struct ble_store_value_ltk *ltk) +store_print_value_ltk(struct ble_store_value_ltk *ltk) { console_printf("ediv=%u rand=%llu authenticated=%d ", ltk->ediv, ltk->rand_num, ltk->authenticated); @@ -62,20 +62,53 @@ store_print_ltk(struct ble_store_value_ltk *ltk) console_printf("\n"); } +static void +store_print_key_ltk(struct ble_store_key_ltk *key_ltk) +{ + if (key_ltk->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) { + console_printf("peer_addr_type=%d peer_addr=", + key_ltk->peer_addr_type); + print_bytes(key_ltk->peer_addr, 6); + console_printf(" "); + } + if (key_ltk->ediv_present) { + console_printf("ediv=0x%02x ", key_ltk->ediv); + } + if (key_ltk->rand_num_present) { + console_printf("rand=0x%llx ", key_ltk->rand_num); + } +} + static int -store_find_slv_ltk(struct ble_store_key_ltk *key_ltk) +store_find_ltk(struct ble_store_key_ltk *key_ltk, + struct ble_store_value_ltk *value_ltks, int num_value_ltks) { struct ble_store_value_ltk *cur; int i; - for (i = 0; i < store_num_slv_ltks; i++) { - cur = store_slv_ltks + i; + for (i = 0; i < num_value_ltks; i++) { + cur = value_ltks + i; - if (cur->ediv == key_ltk->ediv && - cur->rand_num == key_ltk->rand_num) { + if (key_ltk->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) { + if (cur->peer_addr_type != key_ltk->peer_addr_type) { + continue; + } - return i; + if (memcmp(cur->peer_addr, key_ltk->peer_addr, + sizeof cur->peer_addr) != 0) { + continue; + } } + + if (key_ltk->ediv_present && cur->ediv != key_ltk->ediv) { + continue; + } + + if (key_ltk->rand_num_present && cur->rand_num != key_ltk->rand_num) { + continue; + } + + return i; } return -1; @@ -87,7 +120,7 @@ store_read_slv_ltk(struct ble_store_key_ltk *key_ltk, { int idx; - idx = store_find_slv_ltk(key_ltk); + idx = store_find_ltk(key_ltk, store_slv_ltks, store_num_slv_ltks); if (idx == -1) { return BLE_HS_ENOENT; } @@ -103,10 +136,10 @@ store_write_slv_ltk(struct ble_store_value_ltk *value_ltk) int idx; console_printf("persisting slv ltk; "); - store_print_ltk(value_ltk); + store_print_value_ltk(value_ltk); ble_store_key_from_value_ltk(&key_ltk, value_ltk); - idx = store_find_slv_ltk(&key_ltk); + idx = store_find_ltk(&key_ltk, store_slv_ltks, store_num_slv_ltks); if (idx == -1) { if (store_num_slv_ltks >= STORE_MAX_SLV_LTKS) { console_printf("error persisting slv ltk; too many entries (%d)\n", @@ -123,31 +156,12 @@ store_write_slv_ltk(struct ble_store_value_ltk *value_ltk) } 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) + struct ble_store_value_ltk *value_ltk) { int idx; - idx = store_find_mst_ltk(key_ltk); + idx = store_find_ltk(key_ltk, store_mst_ltks, store_num_mst_ltks); if (idx == -1) { return BLE_HS_ENOENT; } @@ -163,10 +177,10 @@ store_write_mst_ltk(struct ble_store_value_ltk *value_ltk) int idx; console_printf("persisting mst ltk; "); - store_print_ltk(value_ltk); + store_print_value_ltk(value_ltk); ble_store_key_from_value_ltk(&key_ltk, value_ltk); - idx = store_find_mst_ltk(&key_ltk); + idx = store_find_ltk(&key_ltk, store_mst_ltks, store_num_mst_ltks); if (idx == -1) { if (store_num_mst_ltks >= STORE_MAX_MST_LTKS) { console_printf("error persisting mst ltk; too many entries " @@ -287,15 +301,17 @@ store_read(int obj_type, union ble_store_key *key, * result. The nimble stack will use this key if this function returns * success. */ - 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); + console_printf("looking up mst ltk; "); + store_print_key_ltk(&key->ltk); + console_printf("\n"); + rc = store_read_mst_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); + console_printf("looking up slv ltk; "); + store_print_key_ltk(&key->ltk); + console_printf("\n"); + rc = store_read_slv_ltk(&key->ltk, &value->ltk); return rc; case BLE_STORE_OBJ_TYPE_CCCD: @@ -320,11 +336,11 @@ store_write(int obj_type, union ble_store_value *val) switch (obj_type) { case BLE_STORE_OBJ_TYPE_MST_LTK: - rc = store_write_slv_ltk(&val->ltk); + rc = store_write_mst_ltk(&val->ltk); return rc; case BLE_STORE_OBJ_TYPE_SLV_LTK: - rc = store_write_mst_ltk(&val->ltk); + rc = store_write_slv_ltk(&val->ltk); return rc; case BLE_STORE_OBJ_TYPE_CCCD:
