summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Collins <ccollins@apache.org>2016-07-14 19:34:36 -0700
committerChristopher Collins <ccollins@apache.org>2016-07-14 21:26:10 -0700
commitcf0676581926f1777ae58a851dc494085734cc11 (patch)
tree4e788e8f8b20b73af0aca5c38ee2dc1c420bb453
parentdb4ca38aad14bec43325580a49343f3f45b8414e (diff)
bletiny / bleprph - Use RAM persistence layer pkg.
-rw-r--r--apps/bleprph/pkg.yml1
-rw-r--r--apps/bleprph/src/bleprph.h8
-rwxr-xr-xapps/bleprph/src/main.c7
-rw-r--r--apps/bletiny/pkg.yml1
-rw-r--r--apps/bletiny/src/bletiny.h5
-rwxr-xr-xapps/bletiny/src/main.c7
-rw-r--r--apps/bletiny/src/store.c399
7 files changed, 13 insertions, 415 deletions
diff --git a/apps/bleprph/pkg.yml b/apps/bleprph/pkg.yml
index df57ac26..0cef5609 100644
--- a/apps/bleprph/pkg.yml
+++ b/apps/bleprph/pkg.yml
@@ -27,5 +27,6 @@ pkg.deps:
- sys/log
- net/nimble/controller
- net/nimble/host
+ - net/nimble/host/store/ram
- libs/console/full
- libs/baselibc
diff --git a/apps/bleprph/src/bleprph.h b/apps/bleprph/src/bleprph.h
index 6a7ffd4a..0dba232c 100644
--- a/apps/bleprph/src/bleprph.h
+++ b/apps/bleprph/src/bleprph.h
@@ -21,8 +21,7 @@
#define H_BLEPRPH_
#include "log/log.h"
-union ble_store_value;
-union ble_store_key;
+struct ble_hs_cfg;
extern struct log bleprph_log;
@@ -53,11 +52,6 @@ extern const uint8_t gatt_svr_chr_bleprph_write[16];
void gatt_svr_init(void);
-/** Store. */
-int 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);
-
/** Misc. */
void print_bytes(const uint8_t *bytes, int len);
void print_addr(const void *addr);
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index f73f2bf7..4342dc8e 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -41,6 +41,9 @@
#include "host/ble_sm.h"
#include "controller/ble_ll.h"
+/* RAM persistence layer. */
+#include "store/ram/ble_store_ram.h"
+
#include "bleprph.h"
#define BSWAP16(x) ((uint16_t)(((x) << 8) | (((x) & 0xff00) >> 8)))
@@ -342,8 +345,8 @@ main(void)
cfg.sm_bonding = 1;
cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
- cfg.store_read_cb = store_read;
- cfg.store_write_cb = store_write;
+ cfg.store_read_cb = ble_store_ram_read;
+ cfg.store_write_cb = ble_store_ram_write;
/* Initialize eventq */
os_eventq_init(&bleprph_evq);
diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml
index 3b7ca3d4..9fab33e5 100644
--- a/apps/bletiny/pkg.yml
+++ b/apps/bletiny/pkg.yml
@@ -27,6 +27,7 @@ pkg.deps:
- sys/log
- net/nimble/controller
- net/nimble/host
+ - net/nimble/host/store/ram
- libs/console/full
- libs/shell
diff --git a/apps/bletiny/src/bletiny.h b/apps/bletiny/src/bletiny.h
index cb5206b1..49101215 100644
--- a/apps/bletiny/src/bletiny.h
+++ b/apps/bletiny/src/bletiny.h
@@ -201,11 +201,6 @@ extern const uint8_t gatt_svr_chr_bleprph_write[16];
void gatt_svr_init(void);
-/** Store. */
-int 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);
-
/** Misc. */
void print_bytes(const uint8_t *bytes, int len);
void print_addr(const void *addr);
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 7c898ffb..b397b65f 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -47,6 +47,9 @@
#include "host/ble_sm.h"
#include "controller/ble_ll.h"
+/* RAM persistence layer. */
+#include "store/ram/ble_store_ram.h"
+
/* XXX: An app should not include private headers from a library. The bletiny
* app uses some of nimble's internal details for logging.
*/
@@ -1655,8 +1658,8 @@ main(void)
cfg.max_gattc_procs = 2;
cfg.max_l2cap_chans = NIMBLE_OPT(MAX_CONNECTIONS) * 3;
cfg.max_l2cap_sig_procs = 2;
- cfg.store_read_cb = store_read;
- cfg.store_write_cb = store_write;
+ cfg.store_read_cb = ble_store_ram_read;
+ cfg.store_write_cb = ble_store_ram_write;
rc = ble_hs_init(&bletiny_evq, &cfg);
assert(rc == 0);
diff --git a/apps/bletiny/src/store.c b/apps/bletiny/src/store.c
deleted file mode 100644
index 2cf46828..00000000
--- a/apps/bletiny/src/store.c
+++ /dev/null
@@ -1,399 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-/**
- * This file implements a simple in-RAM key database for long-term keys. A key
- * is inserted into the database immediately after a successful pairing
- * procedure. A key is retrieved from the database when the central performs
- * the encryption procedure (bonding).
- *
- * As this database is only stored in RAM, its contents are lost if bleprph is
- * restarted.
- */
-
-#include <inttypes.h>
-#include <string.h>
-#include <assert.h>
-
-#include "console/console.h"
-#include "host/ble_hs.h"
-
-#include "bletiny.h"
-
-#define STORE_MAX_SLV_LTKS 4
-#define STORE_MAX_MST_LTKS 4
-#define STORE_MAX_CCCDS 16
-
-static struct ble_store_value_sec store_our_secs[STORE_MAX_SLV_LTKS];
-static int store_num_our_secs;
-
-static struct ble_store_value_sec store_peer_secs[STORE_MAX_MST_LTKS];
-static int store_num_peer_secs;
-
-static struct ble_store_value_cccd store_cccds[STORE_MAX_CCCDS];
-static int store_num_cccds;
-
-/*****************************************************************************
- * $sec *
- *****************************************************************************/
-
-static void
-store_print_value_sec(struct ble_store_value_sec *sec)
-{
- if (sec->ltk_present) {
- console_printf("ediv=%u rand=%llu authenticated=%d ltk=",
- sec->ediv, sec->rand_num, sec->authenticated);
- print_bytes(sec->ltk, 16);
- console_printf(" ");
- }
- if (sec->irk_present) {
- console_printf("irk=");
- print_bytes(sec->irk, 16);
- console_printf(" ");
- }
- if (sec->csrk_present) {
- console_printf("csrk=");
- print_bytes(sec->csrk, 16);
- console_printf(" ");
- }
-
- console_printf("\n");
-}
-
-static void
-store_print_key_sec(struct ble_store_key_sec *key_sec)
-{
- if (key_sec->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) {
- console_printf("peer_addr_type=%d peer_addr=",
- key_sec->peer_addr_type);
- print_bytes(key_sec->peer_addr, 6);
- console_printf(" ");
- }
- if (key_sec->ediv_rand_present) {
- console_printf("ediv=0x%02x rand=0x%llx ",
- key_sec->ediv, key_sec->rand_num);
- }
-}
-
-static void
-store_log_sec_lookup(int obj_type, struct ble_store_key_sec *key_sec)
-{
- char *obj_name;
-
- if (key_sec->peer_addr_type == BLE_STORE_ADDR_TYPE_NONE &&
- !key_sec->ediv_rand_present) {
-
- return;
- }
-
- switch (obj_type) {
- case BLE_STORE_OBJ_TYPE_PEER_SEC:
- obj_name = "peer sec";
- break;
- case BLE_STORE_OBJ_TYPE_OUR_SEC:
- obj_name = "our sec";
- break;
- default:
- assert(0);
- return;
- }
-
- console_printf("looking up %s; ", obj_name);
- store_print_key_sec(key_sec);
- console_printf("\n");
-}
-
-static int
-store_find_sec(struct ble_store_key_sec *key_sec,
- struct ble_store_value_sec *value_secs, int num_value_secs)
-{
- struct ble_store_value_sec *cur;
- int skipped;
- int i;
-
- skipped = 0;
-
- for (i = 0; i < num_value_secs; i++) {
- cur = value_secs + i;
-
- if (key_sec->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) {
- if (cur->peer_addr_type != key_sec->peer_addr_type) {
- continue;
- }
-
- if (memcmp(cur->peer_addr, key_sec->peer_addr,
- sizeof cur->peer_addr) != 0) {
- continue;
- }
- }
-
- if (key_sec->ediv_rand_present) {
- if (cur->ediv != key_sec->ediv) {
- continue;
- }
-
- if (cur->rand_num != key_sec->rand_num) {
- continue;
- }
- }
-
- if (key_sec->idx > skipped) {
- skipped++;
- continue;
- }
-
- return i;
- }
-
- return -1;
-}
-
-static int
-store_read_our_sec(struct ble_store_key_sec *key_sec,
- struct ble_store_value_sec *value_sec)
-{
- int idx;
-
- idx = store_find_sec(key_sec, store_our_secs, store_num_our_secs);
- if (idx == -1) {
- return BLE_HS_ENOENT;
- }
-
- *value_sec = store_our_secs[idx];
- return 0;
-}
-
-static int
-store_write_our_sec(struct ble_store_value_sec *value_sec)
-{
- struct ble_store_key_sec key_sec;
- int idx;
-
- console_printf("persisting our sec; ");
- store_print_value_sec(value_sec);
-
- ble_store_key_from_value_sec(&key_sec, value_sec);
- idx = store_find_sec(&key_sec, store_our_secs, store_num_our_secs);
- if (idx == -1) {
- if (store_num_our_secs >= STORE_MAX_SLV_LTKS) {
- console_printf("error persisting our sec; too many entries (%d)\n",
- store_num_our_secs);
- return BLE_HS_ENOMEM;
- }
-
- idx = store_num_our_secs;
- store_num_our_secs++;
- }
-
- store_our_secs[idx] = *value_sec;
- return 0;
-}
-
-static int
-store_read_peer_sec(struct ble_store_key_sec *key_sec,
- struct ble_store_value_sec *value_sec)
-{
- int idx;
-
- idx = store_find_sec(key_sec, store_peer_secs, store_num_peer_secs);
- if (idx == -1) {
- return BLE_HS_ENOENT;
- }
-
- *value_sec = store_peer_secs[idx];
- return 0;
-}
-
-static int
-store_write_peer_sec(struct ble_store_value_sec *value_sec)
-{
- struct ble_store_key_sec key_sec;
- int idx;
-
- console_printf("persisting peer sec; ");
- store_print_value_sec(value_sec);
-
- ble_store_key_from_value_sec(&key_sec, value_sec);
- idx = store_find_sec(&key_sec, store_peer_secs, store_num_peer_secs);
- if (idx == -1) {
- if (store_num_peer_secs >= STORE_MAX_MST_LTKS) {
- console_printf("error persisting peer sec; too many entries "
- "(%d)\n", store_num_peer_secs);
- return BLE_HS_ENOMEM;
- }
-
- idx = store_num_peer_secs;
- store_num_peer_secs++;
- }
-
- store_peer_secs[idx] = *value_sec;
- 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 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 *value)
-{
- int rc;
-
- switch (obj_type) {
- case BLE_STORE_OBJ_TYPE_PEER_SEC:
- /* 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.
- *
- * Perform a key lookup and populate the context object with the
- * result. The nimble stack will use this key if this function returns
- * success.
- */
- store_log_sec_lookup(BLE_STORE_OBJ_TYPE_PEER_SEC, &key->sec);
- rc = store_read_peer_sec(&key->sec, &value->sec);
- return rc;
-
- case BLE_STORE_OBJ_TYPE_OUR_SEC:
- store_log_sec_lookup(BLE_STORE_OBJ_TYPE_OUR_SEC, &key->sec);
- rc = store_read_our_sec(&key->sec, &value->sec);
- return rc;
-
- case BLE_STORE_OBJ_TYPE_CCCD:
- rc = store_read_cccd(&key->cccd, &value->cccd);
- return rc;
-
- default:
- return BLE_HS_ENOTSUP;
- }
-}
-
-/**
- * Adds the specified object to the database.
- *
- * @return 0 on success; BLE_HS_ENOMEM if the database is
- * full.
- */
-int
-store_write(int obj_type, union ble_store_value *val)
-{
- int rc;
-
- switch (obj_type) {
- case BLE_STORE_OBJ_TYPE_PEER_SEC:
- rc = store_write_peer_sec(&val->sec);
- return rc;
-
- case BLE_STORE_OBJ_TYPE_OUR_SEC:
- rc = store_write_our_sec(&val->sec);
- return rc;
-
- case BLE_STORE_OBJ_TYPE_CCCD:
- rc = store_write_cccd(&val->cccd);
- return rc;
-
- default:
- return BLE_HS_ENOTSUP;
- }
-}