You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/07/15 04:30:04 UTC

[7/9] incubator-mynewt-core git commit: bletiny / bleprph - Use RAM persistence layer pkg.

bletiny / bleprph - Use RAM persistence layer pkg.


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/cf067658
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/cf067658
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/cf067658

Branch: refs/heads/develop
Commit: cf0676581926f1777ae58a851dc494085734cc11
Parents: db4ca38
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Jul 14 19:34:36 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Jul 14 21:26:10 2016 -0700

----------------------------------------------------------------------
 apps/bleprph/pkg.yml       |   1 +
 apps/bleprph/src/bleprph.h |   8 +-
 apps/bleprph/src/main.c    |   7 +-
 apps/bletiny/pkg.yml       |   1 +
 apps/bletiny/src/bletiny.h |   5 -
 apps/bletiny/src/main.c    |   7 +-
 apps/bletiny/src/store.c   | 399 ----------------------------------------
 7 files changed, 13 insertions(+), 415 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf067658/apps/bleprph/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/pkg.yml b/apps/bleprph/pkg.yml
index df57ac2..0cef560 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

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf067658/apps/bleprph/src/bleprph.h
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/bleprph.h b/apps/bleprph/src/bleprph.h
index 6a7ffd4..0dba232 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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf067658/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index f73f2bf..4342dc8 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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf067658/apps/bletiny/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml
index 3b7ca3d..9fab33e 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
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf067658/apps/bletiny/src/bletiny.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny.h b/apps/bletiny/src/bletiny.h
index cb5206b..4910121 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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf067658/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 7c898ff..b397b65 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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/cf067658/apps/bletiny/src/store.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/store.c b/apps/bletiny/src/store.c
deleted file mode 100644
index 2cf4682..0000000
--- 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;
-    }
-}