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/05/19 06:33:12 UTC

[01/12] incubator-mynewt-core git commit: BLE Host - Separate secure-read from secure-write

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 62f661927 -> 48df9b088


BLE Host - Separate secure-read from secure-write

Prior to this change, attributes required encryption, authentication,
and / or authorization; these were applied to both reads and writes.
Now security requirements can be different for reads vs. writes.


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

Branch: refs/heads/develop
Commit: c40856105ab97ea3fa976a270bf4df5ed20e1edf
Parents: 62f6619
Author: Christopher Collins <cc...@apache.org>
Authored: Tue May 17 20:23:36 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue May 17 20:23:36 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_att.h        | 17 ++++++----
 net/nimble/host/include/host/ble_gatt.h       |  9 +++--
 net/nimble/host/src/ble_att_svr.c             | 38 ++++++++++++++--------
 net/nimble/host/src/ble_gatts.c               | 33 ++++++++++++-------
 net/nimble/host/src/ble_hs_cfg.c              |  2 ++
 net/nimble/host/src/ble_l2cap_sm.c            | 11 ++-----
 net/nimble/host/src/test/ble_gatt_conn_test.c |  2 +-
 7 files changed, 68 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c4085610/net/nimble/host/include/host/ble_att.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_att.h b/net/nimble/host/include/host/ble_att.h
index 40fe616..2932124 100644
--- a/net/nimble/host/include/host/ble_att.h
+++ b/net/nimble/host/include/host/ble_att.h
@@ -72,13 +72,16 @@
 
 #define BLE_ATT_ATTR_MAX_LEN                512
 
-#define HA_FLAG_PERM_READ                   (1 << 0)
-#define HA_FLAG_PERM_WRITE                  (1 << 1)
-#define HA_FLAG_ENC_REQ                     (1 << 2)
-#define HA_FLAG_AUTHENTICATION_REQ          (1 << 3)
-#define HA_FLAG_AUTHORIZATION_REQ           (1 << 4)
-
-#define HA_FLAG_PERM_RW             (HA_FLAG_PERM_READ | HA_FLAG_PERM_WRITE)
+#define BLE_ATT_F_READ                      0x01
+#define BLE_ATT_F_WRITE                     0x02
+#define BLE_ATT_F_READ_ENC                  0x04
+#define BLE_ATT_F_READ_AUTHEN               0x08
+#define BLE_ATT_F_READ_AUTHOR               0x10
+#define BLE_ATT_F_WRITE_ENC                 0x20
+#define BLE_ATT_F_WRITE_AUTHEN              0x40
+#define BLE_ATT_F_WRITE_AUTHOR              0x80
+
+#define HA_FLAG_PERM_RW                     (BLE_ATT_F_READ | BLE_ATT_F_WRITE)
 
 #define BLE_ATT_ACCESS_OP_READ              1
 #define BLE_ATT_ACCESS_OP_WRITE             2

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c4085610/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index 7133fcb..7112210 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -166,9 +166,12 @@ typedef uint16_t ble_gatt_chr_flags;
 #define BLE_GATT_CHR_F_AUTH_SIGN_WRITE      0x0040
 #define BLE_GATT_CHR_F_RELIABLE_WRITE       0x0080
 #define BLE_GATT_CHR_F_AUX_WRITE            0x0100
-#define BLE_GATT_CHR_F_ENC_REQ              0x0200
-#define BLE_GATT_CHR_F_AUTHEN_REQ           0x0400
-#define BLE_GATT_CHR_F_AUTHOR_REQ           0x0800
+#define BLE_GATT_CHR_F_READ_ENC             0x0200
+#define BLE_GATT_CHR_F_READ_AUTHEN          0x0400
+#define BLE_GATT_CHR_F_READ_AUTHOR          0x0800
+#define BLE_GATT_CHR_F_WRITE_ENC            0x1000
+#define BLE_GATT_CHR_F_WRITE_AUTHEN         0x2000
+#define BLE_GATT_CHR_F_WRITE_AUTHOR         0x4000
 
 struct ble_gatt_chr_def {
     uint8_t *uuid128;   /* NULL if no more characteristics. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c4085610/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c b/net/nimble/host/src/ble_att_svr.c
index 7690a27..44cde96 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -234,17 +234,28 @@ ble_att_svr_get_sec_state(uint16_t conn_handle,
 }
 
 static int
-ble_att_svr_check_security(uint16_t conn_handle,
+ble_att_svr_check_security(uint16_t conn_handle, int is_read,
                            struct ble_att_svr_entry *entry,
                            uint8_t *out_att_err)
 {
     struct ble_gap_sec_state sec_state;
+    int author;
+    int authen;
+    int enc;
     int rc;
 
-    if (!(entry->ha_flags & (HA_FLAG_ENC_REQ |
-                             HA_FLAG_AUTHENTICATION_REQ |
-                             HA_FLAG_AUTHORIZATION_REQ))) {
+    if (is_read) {
+        enc = entry->ha_flags & BLE_ATT_F_READ_ENC;
+        authen = entry->ha_flags & BLE_ATT_F_READ_AUTHEN;
+        author = entry->ha_flags & BLE_ATT_F_READ_AUTHOR;
+    } else {
+        enc = entry->ha_flags & BLE_ATT_F_WRITE_ENC;
+        authen = entry->ha_flags & BLE_ATT_F_WRITE_AUTHEN;
+        author = entry->ha_flags & BLE_ATT_F_WRITE_AUTHOR;
+    }
 
+    /* Bail early if this operation doesn't require security. */
+    if (!enc && !authen && !author) {
         return 0;
     }
 
@@ -253,7 +264,7 @@ ble_att_svr_check_security(uint16_t conn_handle,
         return rc;
     }
 
-    if (entry->ha_flags & HA_FLAG_ENC_REQ && !sec_state.enc_enabled) {
+    if (enc && !sec_state.enc_enabled) {
         /* XXX: Check security database; if required key present, respond with
          * insufficient encryption error code.
          */
@@ -261,14 +272,12 @@ ble_att_svr_check_security(uint16_t conn_handle,
         return BLE_HS_ATT_ERR(*out_att_err);
     }
 
-    if (entry->ha_flags & HA_FLAG_AUTHENTICATION_REQ &&
-        !sec_state.authenticated) {
-
+    if (authen && !sec_state.authenticated) {
         *out_att_err = BLE_ATT_ERR_INSUFFICIENT_AUTHENT;
         return BLE_HS_ATT_ERR(*out_att_err);
     }
 
-    if (entry->ha_flags & HA_FLAG_AUTHORIZATION_REQ) {
+    if (author) {
         /* XXX: Prompt user for authorization. */
     }
 
@@ -287,13 +296,13 @@ ble_att_svr_read(uint16_t conn_handle,
     att_err = 0;    /* Silence gcc warning. */
 
     if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
-        if (!(entry->ha_flags & HA_FLAG_PERM_READ)) {
+        if (!(entry->ha_flags & BLE_ATT_F_READ)) {
             att_err = BLE_ATT_ERR_READ_NOT_PERMITTED;
             rc = BLE_HS_ENOTSUP;
             goto err;
         }
 
-        rc = ble_att_svr_check_security(conn_handle, entry, &att_err);
+        rc = ble_att_svr_check_security(conn_handle, 1, entry, &att_err);
         if (rc != 0) {
             goto err;
         }
@@ -352,14 +361,17 @@ ble_att_svr_write(uint16_t conn_handle, struct ble_att_svr_entry *entry,
     BLE_HS_DBG_ASSERT(!ble_hs_locked_by_cur_task());
 
     if (conn_handle != BLE_HS_CONN_HANDLE_NONE &&
-        !(entry->ha_flags & HA_FLAG_PERM_WRITE)) {
+        !(entry->ha_flags & BLE_ATT_F_WRITE)) {
 
         att_err = BLE_ATT_ERR_WRITE_NOT_PERMITTED;
         rc = BLE_HS_ENOTSUP;
         goto err;
     }
 
-    /* XXX: Check security. */
+    rc = ble_att_svr_check_security(conn_handle, 0, entry, &att_err);
+    if (rc != 0) {
+        goto err;
+    }
 
     BLE_HS_DBG_ASSERT(entry->ha_cb != NULL);
     rc = entry->ha_cb(conn_handle, entry->ha_handle_id,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c4085610/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index f6fad75..2f3d85e 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -145,19 +145,28 @@ ble_gatts_att_flags_from_chr_flags(ble_gatt_chr_flags chr_flags)
 
     att_flags = 0;
     if (chr_flags & BLE_GATT_CHR_F_READ) {
-        att_flags |= HA_FLAG_PERM_READ;
+        att_flags |= BLE_ATT_F_READ;
     }
     if (chr_flags & (BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_WRITE)) {
-        att_flags |= HA_FLAG_PERM_WRITE;
+        att_flags |= BLE_ATT_F_WRITE;
     }
-    if (chr_flags & BLE_GATT_CHR_F_ENC_REQ) {
-        att_flags |= HA_FLAG_ENC_REQ;
+    if (chr_flags & BLE_GATT_CHR_F_READ_ENC) {
+        att_flags |= BLE_ATT_F_READ_ENC;
     }
-    if (chr_flags & BLE_GATT_CHR_F_AUTHEN_REQ) {
-        att_flags |= HA_FLAG_AUTHENTICATION_REQ;
+    if (chr_flags & BLE_GATT_CHR_F_READ_AUTHEN) {
+        att_flags |= BLE_ATT_F_READ_AUTHEN;
     }
-    if (chr_flags & BLE_GATT_CHR_F_AUTHOR_REQ) {
-        att_flags |= HA_FLAG_AUTHORIZATION_REQ;
+    if (chr_flags & BLE_GATT_CHR_F_READ_AUTHOR) {
+        att_flags |= BLE_ATT_F_READ_AUTHOR;
+    }
+    if (chr_flags & BLE_GATT_CHR_F_WRITE_ENC) {
+        att_flags |= BLE_ATT_F_WRITE_ENC;
+    }
+    if (chr_flags & BLE_GATT_CHR_F_WRITE_AUTHEN) {
+        att_flags |= BLE_ATT_F_WRITE_AUTHEN;
+    }
+    if (chr_flags & BLE_GATT_CHR_F_WRITE_AUTHOR) {
+        att_flags |= BLE_ATT_F_WRITE_AUTHOR;
     }
 
     return att_flags;
@@ -364,7 +373,7 @@ ble_gatts_register_inc(struct ble_gatts_svc_entry *entry)
     BLE_HS_DBG_ASSERT(entry->handle != 0);
     BLE_HS_DBG_ASSERT(entry->end_group_handle != 0xffff);
 
-    rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_INCLUDE, HA_FLAG_PERM_READ,
+    rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_INCLUDE, BLE_ATT_F_READ,
                                      &handle, ble_gatts_inc_access, entry);
     if (rc != 0) {
         return rc;
@@ -613,7 +622,7 @@ ble_gatts_register_clt_cfg_dsc(uint16_t *att_handle)
         return rc;
     }
 
-    rc = ble_att_svr_register(uuid128, HA_FLAG_PERM_READ | HA_FLAG_PERM_WRITE,
+    rc = ble_att_svr_register(uuid128, BLE_ATT_F_READ | BLE_ATT_F_WRITE,
                               att_handle, ble_gatts_clt_cfg_access, NULL);
     if (rc != 0) {
         return rc;
@@ -644,7 +653,7 @@ ble_gatts_register_chr(const struct ble_gatt_chr_def *chr,
      * callback arg).
      */
     rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_CHARACTERISTIC,
-                                     HA_FLAG_PERM_READ, &def_handle,
+                                     BLE_ATT_F_READ, &def_handle,
                                      ble_gatts_chr_def_access, (void *)chr);
     if (rc != 0) {
         return rc;
@@ -754,7 +763,7 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
     /* Register service definition attribute (cast away const on callback
      * arg).
      */
-    rc = ble_att_svr_register_uuid16(uuid16, HA_FLAG_PERM_READ, out_handle,
+    rc = ble_att_svr_register_uuid16(uuid16, BLE_ATT_F_READ, out_handle,
                                      ble_gatts_svc_access, (void *)svc);
     if (rc != 0) {
         return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c4085610/net/nimble/host/src/ble_hs_cfg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_cfg.c b/net/nimble/host/src/ble_hs_cfg.c
index 9bc037e..6faf74e 100644
--- a/net/nimble/host/src/ble_hs_cfg.c
+++ b/net/nimble/host/src/ble_hs_cfg.c
@@ -57,6 +57,8 @@ const struct ble_hs_cfg ble_hs_cfg_dflt = {
     .sm_mitm = 0,
     .sm_sc = 0,
     .sm_keypress = 0,
+    .sm_our_key_dist = 0,
+    .sm_their_key_dist = 0,
 };
 
 struct ble_hs_cfg ble_hs_cfg;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c4085610/net/nimble/host/src/ble_l2cap_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm.c b/net/nimble/host/src/ble_l2cap_sm.c
index f4e8131..1fa05be 100644
--- a/net/nimble/host/src/ble_l2cap_sm.c
+++ b/net/nimble/host/src/ble_l2cap_sm.c
@@ -586,7 +586,7 @@ ble_l2cap_sm_extract_expired(struct ble_l2cap_sm_proc_list *dst_list)
             } else {
                 STAILQ_REMOVE_AFTER(&ble_l2cap_sm_procs, prev, next);
             }
-            STAILQ_INSERT_TAIL(dst_list, proc, next);
+            STAILQ_INSERT_HEAD(dst_list, proc, next);
         }
 
         prev = proc;
@@ -1819,13 +1819,10 @@ ble_l2cap_sm_rx_lt_key_req(struct hci_le_lt_key_req *evt)
          */
         bonding = 0;
         rc = ble_l2cap_sm_lt_key_req_stk_handle(proc, evt);
-        if (rc != 0) {
-            ble_l2cap_sm_proc_remove(proc, prev);
-        }
     } else {
         /* The request is unexpected.  Quietly ignore it. */
         bonding = 0;
-        proc = NULL;
+        rc = 0;
     }
 
     if (proc != NULL) {
@@ -1840,11 +1837,9 @@ ble_l2cap_sm_rx_lt_key_req(struct hci_le_lt_key_req *evt)
         } else {
             rc = ble_l2cap_sm_lt_key_req_ltk_handle(evt);
         }
-    } else if (proc != NULL) {
+    } else if (rc != 0) {
         ble_l2cap_sm_gap_event(proc, rc, 0);
         ble_l2cap_sm_proc_free(proc);
-    } else {
-        rc = BLE_HS_ENOENT;
     }
 
     return rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c4085610/net/nimble/host/src/test/ble_gatt_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_conn_test.c b/net/nimble/host/src/test/ble_gatt_conn_test.c
index 4cff95b..a319a05 100644
--- a/net/nimble/host/src/test/ble_gatt_conn_test.c
+++ b/net/nimble/host/src/test/ble_gatt_conn_test.c
@@ -380,7 +380,7 @@ TEST_CASE(ble_gatt_conn_test_disconnect)
     ble_hs_test_util_init();
 
     /*** Register an attribute to allow indicatations to be sent. */
-    rc = ble_att_svr_register(BLE_UUID16(0x1212), HA_FLAG_PERM_READ,
+    rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ,
                               &attr_handle,
                               ble_gatt_conn_test_attr_cb, NULL);
     TEST_ASSERT(rc == 0);


[11/12] incubator-mynewt-core git commit: Add license pointers for arduino 101 ld files.

Posted by cc...@apache.org.
Add license pointers for arduino 101 ld files.


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

Branch: refs/heads/develop
Commit: 9d7ab35fff73b686b255aa684f3e3e7ff780f404
Parents: c90e85c
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 20:42:34 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 20:42:34 2016 -0700

----------------------------------------------------------------------
 .rat-excludes | 2 ++
 LICENSE       | 2 ++
 2 files changed, 4 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9d7ab35f/.rat-excludes
----------------------------------------------------------------------
diff --git a/.rat-excludes b/.rat-excludes
index 96e6239..71786d1 100644
--- a/.rat-excludes
+++ b/.rat-excludes
@@ -29,6 +29,8 @@ nrf51822_peripherals.h
 nrf51_bitfields.h
 nrf51_deprecated.h
 system_nrf51.h
+boot-nrf51-arduino_101.ld
+nrf51-arduino_101.ld
 
 # Nordic nRF52 SDK - BSD License.
 nrf52pdk.ld

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9d7ab35f/LICENSE
----------------------------------------------------------------------
diff --git a/LICENSE b/LICENSE
index cb4462f..6bff31e 100644
--- a/LICENSE
+++ b/LICENSE
@@ -234,6 +234,8 @@ which are available under a BSD style license.  Relevant files are:
     * hw/bsp/nrf51dk-16kbram/boot-nrf51dk-16kbram.ld
     * hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram.ld
     * hw/bsp/nrf51dk-16kbram/nrf51dk-16kbram_no_boot.ld
+    * hw/bsp/nrf51-arduino_101/boot-nrf51-arduino_101.ld
+    * hw/bsp/nrf51-arduino_101/nrf51-arduino_101.ld
 
 This product bundles and partly derives from parts of the Nordic nRF52 SDK,
 which are available under a BSD style license.  Relevant files are:


[05/12] incubator-mynewt-core git commit: BLE Host - Use macro to check NIMBLE_OPT defines.

Posted by cc...@apache.org.
BLE Host - Use macro to check NIMBLE_OPT defines.

define NIMBLE_OPT(x)                           NIMBLE_OPT_ ## x
This macro exists to help catch bugs at compile time.  If code uses this
macro to check an option value, the compiler will complain when this
header is not included.  If the code checks the option symbol directly
without including this header, it will appear as though the option is
set to 0.


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

Branch: refs/heads/develop
Commit: 47a1ead602b788af00dc6a54dae2ee69ece2b206
Parents: 04ace8e
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 12:00:00 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 12:00:00 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/src/bletiny_priv.h              |  2 +-
 apps/bletiny/src/cmd.c                       |  2 +-
 apps/bletiny/src/main.c                      | 18 +++----
 net/nimble/host/src/ble_att_clt.c            | 48 ++++++++---------
 net/nimble/host/src/ble_att_svr.c            | 26 ++++-----
 net/nimble/host/src/ble_eddystone.c          |  4 +-
 net/nimble/host/src/ble_gap.c                | 30 +++++------
 net/nimble/host/src/ble_gattc.c              | 64 +++++++++++------------
 net/nimble/host/src/ble_gatts.c              |  2 +-
 net/nimble/host/src/ble_hs_adv.c             |  4 +-
 net/nimble/host/src/ble_hs_cfg.c             |  4 +-
 net/nimble/host/src/ble_hs_conn.c            | 22 ++++----
 net/nimble/host/src/ble_l2cap_sm.c           |  2 +-
 net/nimble/host/src/ble_l2cap_sm_alg.c       |  2 +-
 net/nimble/host/src/ble_l2cap_sm_cmd.c       |  2 +-
 net/nimble/host/src/ble_l2cap_sm_priv.h      |  4 +-
 net/nimble/host/src/test/ble_l2cap_sm_test.c |  4 +-
 net/nimble/include/nimble/nimble_opt.h       |  9 +++-
 18 files changed, 129 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/apps/bletiny/src/bletiny_priv.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny_priv.h b/apps/bletiny/src/bletiny_priv.h
index d27740f..fc85bc8 100644
--- a/apps/bletiny/src/bletiny_priv.h
+++ b/apps/bletiny/src/bletiny_priv.h
@@ -76,7 +76,7 @@ struct bletiny_conn {
     struct bletiny_svc_list svcs;
 };
 
-extern struct bletiny_conn bletiny_conns[NIMBLE_OPT_MAX_CONNECTIONS];
+extern struct bletiny_conn bletiny_conns[NIMBLE_OPT(MAX_CONNECTIONS)];
 extern int bletiny_num_conns;
 
 extern const char *bletiny_device_name;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 4510c3e..5015157 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1698,7 +1698,7 @@ cmd_write(int argc, char **argv)
 static int
 cmd_passkey(int argc, char **argv)
 {
-#if !NIMBLE_OPT_SM
+#if !NIMBLE_OPT(SM)
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 02f08fa..9c07f76 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -86,7 +86,7 @@ struct os_mempool default_mbuf_mpool;
 #define BLETINY_STACK_SIZE             (OS_STACK_ALIGN(288))
 #define BLETINY_TASK_PRIO              1
 
-#if NIMBLE_OPT_ROLE_CENTRAL
+#if NIMBLE_OPT(ROLE_CENTRAL)
 #define BLETINY_MAX_SVCS               32
 #define BLETINY_MAX_CHRS               64
 #define BLETINY_MAX_DSCS               64
@@ -105,13 +105,13 @@ bssnz_t os_stack_t bletiny_stack[BLETINY_STACK_SIZE];
 static struct log_handler bletiny_log_console_handler;
 struct log bletiny_log;
 
-struct bletiny_conn ble_shell_conns[NIMBLE_OPT_MAX_CONNECTIONS];
+struct bletiny_conn ble_shell_conns[NIMBLE_OPT(MAX_CONNECTIONS)];
 int bletiny_num_conns;
 
 void
 bletest_inc_adv_pkt_num(void) { }
 
-bssnz_t struct bletiny_conn bletiny_conns[NIMBLE_OPT_MAX_CONNECTIONS];
+bssnz_t struct bletiny_conn bletiny_conns[NIMBLE_OPT(MAX_CONNECTIONS)];
 int bletiny_num_conns;
 
 static void *bletiny_svc_mem;
@@ -386,7 +386,7 @@ bletiny_conn_add(struct ble_gap_conn_desc *desc)
 {
     struct bletiny_conn *conn;
 
-    assert(bletiny_num_conns < NIMBLE_OPT_MAX_CONNECTIONS);
+    assert(bletiny_num_conns < NIMBLE_OPT(MAX_CONNECTIONS));
 
     conn = bletiny_conns + bletiny_num_conns;
     bletiny_num_conns++;
@@ -416,7 +416,7 @@ bletiny_conn_delete_idx(int idx)
     /* This '#if' is not strictly necessary.  It is here to prevent a spurious
      * warning from being reported.
      */
-#if NIMBLE_OPT_MAX_CONNECTIONS > 1
+#if NIMBLE_OPT(MAX_CONNECTIONS) > 1
     int i;
     for (i = idx + 1; i < bletiny_num_conns; i++) {
         bletiny_conns[i - 1] = bletiny_conns[i];
@@ -1338,7 +1338,7 @@ bletiny_show_rssi(uint16_t conn_handle)
 int
 bletiny_sec_start(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT_SM
+#if !NIMBLE_OPT(SM)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1355,9 +1355,9 @@ bletiny_sec_restart(uint16_t conn_handle,
                     uint64_t rand_val,
                     int auth)
 {
-    #if !NIMBLE_OPT_SM
-        return BLE_HS_ENOTSUP;
-    #endif
+#if !NIMBLE_OPT(SM)
+    return BLE_HS_ENOTSUP;
+#endif
 
     int rc;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_att_clt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_clt.c b/net/nimble/host/src/ble_att_clt.c
index 5f628c6..a049f33 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -290,7 +290,7 @@ int
 ble_att_clt_tx_find_info(uint16_t conn_handle,
                          struct ble_att_find_info_req *req)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_INFO
+#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -372,7 +372,7 @@ ble_att_clt_parse_find_info_entry(struct os_mbuf **rxom, uint8_t rsp_format,
 int
 ble_att_clt_rx_find_info(uint16_t conn_handle, struct os_mbuf **om)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_INFO
+#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -441,7 +441,7 @@ ble_att_clt_tx_find_type_value(uint16_t conn_handle,
                                struct ble_att_find_type_value_req *req,
                                void *attribute_value, int value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_TYPE
+#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -488,7 +488,7 @@ ble_att_clt_parse_find_type_value_hinfo(
 int
 ble_att_clt_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_TYPE
+#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -560,7 +560,7 @@ ble_att_clt_tx_read_type(uint16_t conn_handle,
                          struct ble_att_read_type_req *req,
                          void *uuid128)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -607,7 +607,7 @@ ble_att_clt_parse_read_type_adata(struct os_mbuf **om, int data_len,
 int
 ble_att_clt_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -676,7 +676,7 @@ done:
 int
 ble_att_clt_tx_read(uint16_t conn_handle, struct ble_att_read_req *req)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ
+#if !NIMBLE_OPT(ATT_CLT_READ)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -703,7 +703,7 @@ ble_att_clt_tx_read(uint16_t conn_handle, struct ble_att_read_req *req)
 int
 ble_att_clt_rx_read(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ
+#if !NIMBLE_OPT(ATT_CLT_READ)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -758,7 +758,7 @@ int
 ble_att_clt_tx_read_blob(uint16_t conn_handle,
                          struct ble_att_read_blob_req *req)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_BLOB
+#if !NIMBLE_OPT(ATT_CLT_READ_BLOB)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -785,7 +785,7 @@ ble_att_clt_tx_read_blob(uint16_t conn_handle,
 int
 ble_att_clt_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_BLOB
+#if !NIMBLE_OPT(ATT_CLT_READ_BLOB)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -854,7 +854,7 @@ int
 ble_att_clt_tx_read_mult(uint16_t conn_handle, uint16_t *att_handles,
                          int num_att_handles)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_MULT
+#if !NIMBLE_OPT(ATT_CLT_READ_MULT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -881,7 +881,7 @@ ble_att_clt_tx_read_mult(uint16_t conn_handle, uint16_t *att_handles,
 int
 ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_MULT
+#if !NIMBLE_OPT(ATT_CLT_READ_MULT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -942,7 +942,7 @@ ble_att_clt_tx_read_group_type(uint16_t conn_handle,
                                struct ble_att_read_group_type_req *req,
                                void *uuid128)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_GROUP_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -997,7 +997,7 @@ ble_att_clt_parse_read_group_type_adata(
 int
 ble_att_clt_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_GROUP_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1101,7 +1101,7 @@ int
 ble_att_clt_tx_write_req(uint16_t conn_handle, struct ble_att_write_req *req,
                          void *value, uint16_t value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_WRITE
+#if !NIMBLE_OPT(ATT_CLT_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1117,7 +1117,7 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle,
                          struct ble_att_write_req *req,
                          void *value, uint16_t value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_WRITE_NO_RSP
+#if !NIMBLE_OPT(ATT_CLT_WRITE_NO_RSP)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1131,7 +1131,7 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle,
 int
 ble_att_clt_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_WRITE
+#if !NIMBLE_OPT(ATT_CLT_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1182,7 +1182,7 @@ ble_att_clt_tx_prep_write(uint16_t conn_handle,
                           struct ble_att_prep_write_cmd *req,
                           void *value, uint16_t value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_PREP_WRITE
+#if !NIMBLE_OPT(ATT_CLT_PREP_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1220,7 +1220,7 @@ ble_att_clt_tx_prep_write(uint16_t conn_handle,
 int
 ble_att_clt_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_PREP_WRITE
+#if !NIMBLE_OPT(ATT_CLT_PREP_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1287,7 +1287,7 @@ int
 ble_att_clt_tx_exec_write(uint16_t conn_handle,
                           struct ble_att_exec_write_req *req)
 {
-#if !NIMBLE_OPT_ATT_CLT_EXEC_WRITE
+#if !NIMBLE_OPT(ATT_CLT_EXEC_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1314,7 +1314,7 @@ ble_att_clt_tx_exec_write(uint16_t conn_handle,
 int
 ble_att_clt_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_EXEC_WRITE
+#if !NIMBLE_OPT(ATT_CLT_EXEC_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1370,7 +1370,7 @@ int
 ble_att_clt_tx_notify(uint16_t conn_handle, struct ble_att_notify_req *req,
                       void *value, uint16_t value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_NOTIFY
+#if !NIMBLE_OPT(ATT_CLT_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1437,7 +1437,7 @@ ble_att_clt_tx_indicate(uint16_t conn_handle,
                         struct ble_att_indicate_req *req,
                         void *value, uint16_t value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_INDICATE
+#if !NIMBLE_OPT(ATT_CLT_INDICATE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1466,7 +1466,7 @@ ble_att_clt_tx_indicate(uint16_t conn_handle,
 int
 ble_att_clt_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_CLT_INDICATE
+#if !NIMBLE_OPT(ATT_CLT_INDICATE)
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c b/net/nimble/host/src/ble_att_svr.c
index 44cde96..df42788 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -776,7 +776,7 @@ done:
 int
 ble_att_svr_rx_find_info(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_FIND_INFO
+#if !NIMBLE_OPT(ATT_SVR_FIND_INFO)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1088,7 +1088,7 @@ done:
 int
 ble_att_svr_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_FIND_TYPE
+#if !NIMBLE_OPT(ATT_SVR_FIND_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1269,7 +1269,7 @@ done:
 int
 ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_READ_TYPE
+#if !NIMBLE_OPT(ATT_SVR_READ_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1407,7 +1407,7 @@ done:
 int
 ble_att_svr_rx_read(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_READ
+#if !NIMBLE_OPT(ATT_SVR_READ)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1505,7 +1505,7 @@ done:
 int
 ble_att_svr_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_READ_BLOB
+#if !NIMBLE_OPT(ATT_SVR_READ_BLOB)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1663,7 +1663,7 @@ done:
 int
 ble_att_svr_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_READ_MULT
+#if !NIMBLE_OPT(ATT_SVR_READ_MULT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1966,7 +1966,7 @@ done:
 int
 ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_READ_GROUP_TYPE
+#if !NIMBLE_OPT(ATT_SVR_READ_GROUP_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2070,7 +2070,7 @@ done:
 int
 ble_att_svr_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_WRITE
+#if !NIMBLE_OPT(ATT_SVR_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2125,7 +2125,7 @@ done:
 int
 ble_att_svr_rx_write_no_rsp(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_WRITE_NO_RSP
+#if !NIMBLE_OPT(ATT_SVR_WRITE_NO_RSP)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2342,7 +2342,7 @@ ble_att_svr_prep_write(uint16_t conn_handle,
 int
 ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_PREP_WRITE
+#if !NIMBLE_OPT(ATT_SVR_PREP_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2517,7 +2517,7 @@ done:
 int
 ble_att_svr_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_EXEC_WRITE
+#if !NIMBLE_OPT(ATT_SVR_EXEC_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2589,7 +2589,7 @@ done:
 int
 ble_att_svr_rx_notify(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_NOTIFY
+#if !NIMBLE_OPT(ATT_SVR_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2666,7 +2666,7 @@ done:
 int
 ble_att_svr_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT_ATT_SVR_INDICATE
+#if !NIMBLE_OPT(ATT_SVR_INDICATE)
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_eddystone.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_eddystone.c b/net/nimble/host/src/ble_eddystone.c
index 98ce4ab..dbd8a71 100644
--- a/net/nimble/host/src/ble_eddystone.c
+++ b/net/nimble/host/src/ble_eddystone.c
@@ -117,7 +117,7 @@ ble_eddystone_set_adv_data_gen(struct ble_hs_adv_fields *adv_fields,
 int
 ble_eddystone_set_adv_data_uid(struct ble_hs_adv_fields *adv_fields, void *uid)
 {
-#if !NIMBLE_OPT_EDDYSTONE
+#if !NIMBLE_OPT(EDDYSTONE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -158,7 +158,7 @@ ble_eddystone_set_adv_data_url(struct ble_hs_adv_fields *adv_fields,
                                uint8_t url_scheme, char *url_body,
                                uint8_t url_body_len, uint8_t url_suffix)
 {
-#if !NIMBLE_OPT_EDDYSTONE
+#if !NIMBLE_OPT(EDDYSTONE)
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index 22c95d0..a51442c 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -512,7 +512,7 @@ ble_gap_conn_broken(struct ble_gap_snapshot *snap, int status)
 void
 ble_gap_rx_disconn_complete(struct hci_disconn_complete *evt)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return;
 #endif
 
@@ -551,7 +551,7 @@ ble_gap_rx_disconn_complete(struct hci_disconn_complete *evt)
 void
 ble_gap_rx_update_complete(struct hci_le_conn_upd_complete *evt)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return;
 #endif
 
@@ -709,7 +709,7 @@ ble_gap_accept_slave_conn(uint8_t addr_type, uint8_t *addr)
 void
 ble_gap_rx_adv_report(struct ble_hs_adv *adv)
 {
-#if !NIMBLE_OPT_ROLE_OBSERVER
+#if !NIMBLE_OPT(ROLE_OBSERVER)
     return;
 #endif
 
@@ -744,7 +744,7 @@ ble_gap_rx_adv_report(struct ble_hs_adv *adv)
 int
 ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -917,7 +917,7 @@ ble_gap_heartbeat(void)
 static int
 ble_gap_wl_busy(void)
 {
-#if !NIMBLE_OPT_WHITELIST
+#if !NIMBLE_OPT(WHITELIST)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -967,7 +967,7 @@ int
 ble_gap_wl_set(struct ble_gap_white_entry *white_list,
                uint8_t white_list_count)
 {
-#if !NIMBLE_OPT_WHITELIST
+#if !NIMBLE_OPT(WHITELIST)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1040,7 +1040,7 @@ ble_gap_adv_disable_tx(void)
 int
 ble_gap_adv_stop(void)
 {
-#if !NIMBLE_OPT_ADVERTISE
+#if !NIMBLE_OPT(ADVERTISE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1274,7 +1274,7 @@ ble_gap_adv_start(uint8_t discoverable_mode, uint8_t connectable_mode,
                   struct hci_adv_params *adv_params,
                   ble_gap_conn_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_ADVERTISE
+#if !NIMBLE_OPT(ADVERTISE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1395,7 +1395,7 @@ done:
 int
 ble_gap_adv_set_fields(struct ble_hs_adv_fields *adv_fields)
 {
-#if !NIMBLE_OPT_ADVERTISE
+#if !NIMBLE_OPT(ADVERTISE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1428,7 +1428,7 @@ ble_gap_adv_set_fields(struct ble_hs_adv_fields *adv_fields)
 int
 ble_gap_adv_rsp_set_fields(struct ble_hs_adv_fields *rsp_fields)
 {
-#if !NIMBLE_OPT_ADVERTISE
+#if !NIMBLE_OPT(ADVERTISE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1518,7 +1518,7 @@ ble_gap_disc(uint32_t duration_ms, uint8_t discovery_mode,
              uint8_t scan_type, uint8_t filter_policy,
              ble_gap_disc_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_ROLE_OBSERVER
+#if !NIMBLE_OPT(ROLE_OBSERVER)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1653,7 +1653,7 @@ ble_gap_conn_initiate(int addr_type, uint8_t *addr,
                       struct ble_gap_crt_params *params,
                       ble_gap_conn_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_ROLE_CENTRAL
+#if !NIMBLE_OPT(ROLE_CENTRAL)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1840,7 +1840,7 @@ ble_gap_tx_param_neg_reply(uint16_t conn_handle, uint8_t reject_reason)
 void
 ble_gap_rx_param_req(struct hci_le_conn_param_req *evt)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return;
 #endif
 
@@ -1926,7 +1926,7 @@ ble_gap_update_tx(uint16_t conn_handle, struct ble_gap_upd_params *params)
 int
 ble_gap_update_params(uint16_t conn_handle, struct ble_gap_upd_params *params)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1973,7 +1973,7 @@ done:
  * $security                                                                 *
  *****************************************************************************/
 
-#if NIMBLE_OPT_SM
+#if NIMBLE_OPT(SM)
 int
 ble_gap_security_initiate(uint16_t conn_handle)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index 7bfd2dd..916da80 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -1174,7 +1174,7 @@ int
 ble_gattc_disc_all_svcs(uint16_t conn_handle, ble_gatt_disc_svc_fn *cb,
                         void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_DISC_ALL_SVCS
+#if !NIMBLE_OPT(GATT_DISC_ALL_SVCS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1368,7 +1368,7 @@ int
 ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, void *service_uuid128,
                            ble_gatt_disc_svc_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_DISC_SVC_UUID
+#if !NIMBLE_OPT(GATT_DISC_SVC_UUID)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1675,7 +1675,7 @@ ble_gattc_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle,
                         uint16_t end_handle,
                         ble_gatt_disc_svc_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_FIND_INC_SVCS
+#if !NIMBLE_OPT(GATT_FIND_INC_SVCS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1900,7 +1900,7 @@ ble_gattc_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle,
                         uint16_t end_handle, ble_gatt_chr_fn *cb,
                         void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_DISC_ALL_CHRS
+#if !NIMBLE_OPT(GATT_DISC_ALL_CHRS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2135,7 +2135,7 @@ ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle,
                             uint16_t end_handle, void *uuid128,
                             ble_gatt_chr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_DISC_CHR_UUID
+#if !NIMBLE_OPT(GATT_DISC_CHR_UUID)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2333,7 +2333,7 @@ ble_gattc_disc_all_dscs(uint16_t conn_handle, uint16_t chr_def_handle,
                         uint16_t chr_end_handle,
                         ble_gatt_dsc_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_DISC_ALL_DSCS
+#if !NIMBLE_OPT(GATT_DISC_ALL_DSCS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2456,7 +2456,7 @@ int
 ble_gattc_read(uint16_t conn_handle, uint16_t attr_handle,
                ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_READ
+#if !NIMBLE_OPT(GATT_READ)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2603,7 +2603,7 @@ ble_gattc_read_by_uuid(uint16_t conn_handle, uint16_t start_handle,
                        uint16_t end_handle, void *uuid128,
                        ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_READ_UUID
+#if !NIMBLE_OPT(GATT_READ_UUID)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2781,7 +2781,7 @@ int
 ble_gattc_read_long(uint16_t conn_handle, uint16_t handle,
                     ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_READ_LONG
+#if !NIMBLE_OPT(GATT_READ_LONG)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2895,7 +2895,7 @@ ble_gattc_read_mult(uint16_t conn_handle, uint16_t *handles,
                     uint8_t num_handles, ble_gatt_attr_fn *cb,
                     void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_READ_MULT
+#if !NIMBLE_OPT(GATT_READ_MULT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2952,7 +2952,7 @@ int
 ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle, void *value,
                        uint16_t value_len)
 {
-#if !NIMBLE_OPT_GATT_WRITE_NO_RSP
+#if !NIMBLE_OPT(GATT_WRITE_NO_RSP)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3039,7 +3039,7 @@ int
 ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle, void *value,
                 uint16_t value_len, ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_WRITE
+#if !NIMBLE_OPT(GATT_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3264,7 +3264,7 @@ int
 ble_gattc_write_long(uint16_t conn_handle, uint16_t attr_handle, void *value,
                      uint16_t value_len, ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_WRITE_LONG
+#if !NIMBLE_OPT(GATT_WRITE_LONG)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3478,7 +3478,7 @@ ble_gattc_write_reliable(uint16_t conn_handle, struct ble_gatt_attr *attrs,
                          int num_attrs, ble_gatt_reliable_attr_fn *cb,
                          void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_WRITE_RELIABLE
+#if !NIMBLE_OPT(GATT_WRITE_RELIABLE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3529,7 +3529,7 @@ int
 ble_gattc_notify_custom(uint16_t conn_handle, uint16_t att_handle,
                         void *attr_data, uint16_t attr_data_len)
 {
-#if !NIMBLE_OPT_GATT_NOTIFY
+#if !NIMBLE_OPT(GATT_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3579,7 +3579,7 @@ err:
 int
 ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle)
 {
-#if !NIMBLE_OPT_GATT_NOTIFY
+#if !NIMBLE_OPT(GATT_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3675,7 +3675,7 @@ int
 ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle,
                    ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT_GATT_INDICATE
+#if !NIMBLE_OPT(GATT_INDICATE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3775,7 +3775,7 @@ void
 ble_gattc_rx_find_info_idata(uint16_t conn_handle,
                              struct ble_att_find_info_idata *idata)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_INFO
+#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
     return;
 #endif
 
@@ -3796,7 +3796,7 @@ ble_gattc_rx_find_info_idata(uint16_t conn_handle,
 void
 ble_gattc_rx_find_info_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_INFO
+#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
     return;
 #endif
 
@@ -3818,7 +3818,7 @@ void
 ble_gattc_rx_find_type_value_hinfo(uint16_t conn_handle,
                                    struct ble_att_find_type_value_hinfo *hinfo)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_TYPE
+#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
     return;
 #endif
 
@@ -3839,7 +3839,7 @@ ble_gattc_rx_find_type_value_hinfo(uint16_t conn_handle,
 void
 ble_gattc_rx_find_type_value_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT_ATT_CLT_FIND_TYPE
+#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
     return;
 #endif
 
@@ -3861,7 +3861,7 @@ void
 ble_gattc_rx_read_type_adata(uint16_t conn_handle,
                              struct ble_att_read_type_adata *adata)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
     return;
 #endif
 
@@ -3885,7 +3885,7 @@ ble_gattc_rx_read_type_adata(uint16_t conn_handle,
 void
 ble_gattc_rx_read_type_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
     return;
 #endif
 
@@ -3910,7 +3910,7 @@ void
 ble_gattc_rx_read_group_type_adata(uint16_t conn_handle,
                                    struct ble_att_read_group_type_adata *adata)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_GROUP_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
     return;
 #endif
 
@@ -3931,7 +3931,7 @@ ble_gattc_rx_read_group_type_adata(uint16_t conn_handle,
 void
 ble_gattc_rx_read_group_type_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_GROUP_TYPE
+#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
     return;
 #endif
 
@@ -3953,7 +3953,7 @@ void
 ble_gattc_rx_read_rsp(uint16_t conn_handle, int status, void *value,
                       int value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ
+#if !NIMBLE_OPT(ATT_CLT_READ)
     return;
 #endif
 
@@ -3978,7 +3978,7 @@ void
 ble_gattc_rx_read_blob_rsp(uint16_t conn_handle, int status,
                            void *value, int value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_BLOB
+#if !NIMBLE_OPT(ATT_CLT_READ_BLOB)
     return;
 #endif
 
@@ -4000,7 +4000,7 @@ void
 ble_gattc_rx_read_mult_rsp(uint16_t conn_handle, int status,
                            void *value, int value_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_READ_MULT
+#if !NIMBLE_OPT(ATT_CLT_READ_MULT)
     return;
 #endif
 
@@ -4020,7 +4020,7 @@ ble_gattc_rx_read_mult_rsp(uint16_t conn_handle, int status,
 void
 ble_gattc_rx_write_rsp(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT_ATT_CLT_WRITE
+#if !NIMBLE_OPT(ATT_CLT_WRITE)
     return;
 #endif
 
@@ -4042,7 +4042,7 @@ ble_gattc_rx_prep_write_rsp(uint16_t conn_handle, int status,
                             struct ble_att_prep_write_cmd *rsp,
                             void *attr_data, uint16_t attr_data_len)
 {
-#if !NIMBLE_OPT_ATT_CLT_PREP_WRITE
+#if !NIMBLE_OPT(ATT_CLT_PREP_WRITE)
     return;
 #endif
 
@@ -4066,7 +4066,7 @@ ble_gattc_rx_prep_write_rsp(uint16_t conn_handle, int status,
 void
 ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT_ATT_CLT_EXEC_WRITE
+#if !NIMBLE_OPT(ATT_CLT_EXEC_WRITE)
     return;
 #endif
 
@@ -4089,7 +4089,7 @@ ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status)
 void
 ble_gattc_rx_indicate_rsp(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT_ATT_CLT_INDICATE
+#if !NIMBLE_OPT(ATT_CLT_INDICATE)
     return;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 2f3d85e..cb71092 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -1111,7 +1111,7 @@ ble_gatts_chr_updated(uint16_t chr_def_handle)
 {
     struct ble_gatts_clt_cfg *clt_cfg;
     struct ble_hs_conn *conn;
-    uint16_t conn_handles[NIMBLE_OPT_MAX_CONNECTIONS];
+    uint16_t conn_handles[NIMBLE_OPT(MAX_CONNECTIONS)];
     int any_updates;
     int num_conns;
     int idx;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_hs_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_adv.c b/net/nimble/host/src/ble_hs_adv.c
index ef038a7..c56bb19 100644
--- a/net/nimble/host/src/ble_hs_adv.c
+++ b/net/nimble/host/src/ble_hs_adv.c
@@ -42,7 +42,7 @@ int
 ble_hs_adv_set_flat(uint8_t type, int data_len, void *data,
                     uint8_t *dst, uint8_t *dst_len, uint8_t max_len)
 {
-#if !NIMBLE_OPT_ADVERTISE
+#if !NIMBLE_OPT(ADVERTISE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -112,7 +112,7 @@ int
 ble_hs_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
                       uint8_t *dst, uint8_t *dst_len, uint8_t max_len)
 {
-#if !NIMBLE_OPT_ADVERTISE
+#if !NIMBLE_OPT(ADVERTISE)
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_hs_cfg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_cfg.c b/net/nimble/host/src/ble_hs_cfg.c
index 6faf74e..3cad0b6 100644
--- a/net/nimble/host/src/ble_hs_cfg.c
+++ b/net/nimble/host/src/ble_hs_cfg.c
@@ -26,8 +26,8 @@ const struct ble_hs_cfg ble_hs_cfg_dflt = {
     /** Connection settings. */
     .max_outstanding_pkts_per_conn = 5,
 
-#if NIMBLE_OPT_CONNECT
-    .max_connections = NIMBLE_OPT_MAX_CONNECTIONS,
+#if NIMBLE_OPT(CONNECT)
+    .max_connections = NIMBLE_OPT(MAX_CONNECTIONS),
     .max_conn_update_entries = 4,
 #else
     .max_connections = 0,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index 567da51..7afcce3 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -34,7 +34,7 @@ static os_membuf_t *ble_hs_conn_elem_mem;
 int
 ble_hs_conn_can_alloc(void)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return 0;
 #endif
 
@@ -46,7 +46,7 @@ ble_hs_conn_can_alloc(void)
 struct ble_l2cap_chan *
 ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return NULL;
 #endif
 
@@ -67,7 +67,7 @@ ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
 int
 ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -98,7 +98,7 @@ ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 struct ble_hs_conn *
 ble_hs_conn_alloc(void)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return NULL;
 #endif
 
@@ -135,7 +135,7 @@ ble_hs_conn_alloc(void)
     /* XXX: We should create the SM channel even if not configured.  We need it
      * to reject SM messages.
      */
-#if NIMBLE_OPT_SM
+#if NIMBLE_OPT(SM)
     chan = ble_l2cap_sm_create_chan();
     if (chan == NULL) {
         goto err;
@@ -174,7 +174,7 @@ ble_hs_conn_delete_chan(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 void
 ble_hs_conn_free(struct ble_hs_conn *conn)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return;
 #endif
 
@@ -202,7 +202,7 @@ ble_hs_conn_free(struct ble_hs_conn *conn)
 void
 ble_hs_conn_insert(struct ble_hs_conn *conn)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return;
 #endif
 
@@ -215,7 +215,7 @@ ble_hs_conn_insert(struct ble_hs_conn *conn)
 void
 ble_hs_conn_remove(struct ble_hs_conn *conn)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return;
 #endif
 
@@ -227,7 +227,7 @@ ble_hs_conn_remove(struct ble_hs_conn *conn)
 struct ble_hs_conn *
 ble_hs_conn_find(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return NULL;
 #endif
 
@@ -247,7 +247,7 @@ ble_hs_conn_find(uint16_t conn_handle)
 int
 ble_hs_conn_exists(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return 0;
 #endif
     return ble_hs_conn_find(conn_handle) != NULL;
@@ -259,7 +259,7 @@ ble_hs_conn_exists(uint16_t conn_handle)
 struct ble_hs_conn *
 ble_hs_conn_first(void)
 {
-#if !NIMBLE_OPT_CONNECT
+#if !NIMBLE_OPT(CONNECT)
     return NULL;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_l2cap_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm.c b/net/nimble/host/src/ble_l2cap_sm.c
index 1fa05be..6adc820 100644
--- a/net/nimble/host/src/ble_l2cap_sm.c
+++ b/net/nimble/host/src/ble_l2cap_sm.c
@@ -48,7 +48,7 @@
 #include "nimble/nimble_opt.h"
 #include "ble_hs_priv.h"
 
-#if NIMBLE_OPT_SM
+#if NIMBLE_OPT(SM)
 
 #define BLE_L2CAP_SM_PROC_STATE_NONE            ((uint8_t)-1)
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_l2cap_sm_alg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm_alg.c b/net/nimble/host/src/ble_l2cap_sm_alg.c
index 61a9825..df1002a 100644
--- a/net/nimble/host/src/ble_l2cap_sm_alg.c
+++ b/net/nimble/host/src/ble_l2cap_sm_alg.c
@@ -25,7 +25,7 @@
 #include "nimble/nimble_opt.h"
 #include "ble_hs_priv.h"
 
-#if NIMBLE_OPT_SM
+#if NIMBLE_OPT(SM)
 
 static mbedtls_aes_context ble_l2cap_sm_alg_ctxt;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_l2cap_sm_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm_cmd.c b/net/nimble/host/src/ble_l2cap_sm_cmd.c
index 14c5093..ced4c1c 100644
--- a/net/nimble/host/src/ble_l2cap_sm_cmd.c
+++ b/net/nimble/host/src/ble_l2cap_sm_cmd.c
@@ -24,7 +24,7 @@
 #include "nimble/nimble_opt.h"
 #include "ble_hs_priv.h"
 
-#if NIMBLE_OPT_SM
+#if NIMBLE_OPT(SM)
 
 static int
 ble_l2cap_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/ble_l2cap_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm_priv.h b/net/nimble/host/src/ble_l2cap_sm_priv.h
index f084f6e..e65d6d2 100644
--- a/net/nimble/host/src/ble_l2cap_sm_priv.h
+++ b/net/nimble/host/src/ble_l2cap_sm_priv.h
@@ -20,6 +20,8 @@
 #ifndef H_BLE_L2CAP_SM_
 #define H_BLE_L2CAP_SM_
 
+#include "nimble/nimble_opt.h"
+
 struct ble_gap_sec_state;
 struct hci_le_lt_key_req;
 
@@ -152,7 +154,7 @@ struct ble_l2cap_sm_sec_req {
 };
 
 
-#if NIMBLE_OPT_SM
+#if NIMBLE_OPT(SM)
 
 #ifdef BLE_HS_DEBUG
 void ble_l2cap_sm_dbg_set_next_pair_rand(uint8_t *next_pair_rand);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/host/src/test/ble_l2cap_sm_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_l2cap_sm_test.c b/net/nimble/host/src/test/ble_l2cap_sm_test.c
index a775cd5..7530144 100644
--- a/net/nimble/host/src/test/ble_l2cap_sm_test.c
+++ b/net/nimble/host/src/test/ble_l2cap_sm_test.c
@@ -27,7 +27,7 @@
 #include "host/ble_hs_test.h"
 #include "ble_hs_test_util.h"
 
-#if NIMBLE_OPT_SM
+#if NIMBLE_OPT(SM)
 
 int ble_l2cap_sm_test_gap_event;
 int ble_l2cap_sm_test_gap_status;
@@ -1915,7 +1915,7 @@ TEST_SUITE(ble_l2cap_sm_test_suite)
 int
 ble_l2cap_sm_test_all(void)
 {
-#if !NIMBLE_OPT_SM
+#if !NIMBLE_OPT(SM)
     return 0;
 #else
     ble_l2cap_sm_test_suite();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/47a1ead6/net/nimble/include/nimble/nimble_opt.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/nimble_opt.h b/net/nimble/include/nimble/nimble_opt.h
index 604db86..38fb5a7 100644
--- a/net/nimble/include/nimble/nimble_opt.h
+++ b/net/nimble/include/nimble/nimble_opt.h
@@ -58,7 +58,6 @@
 #define NIMBLE_OPT_SM                           1
 #endif
 
-
 /** HOST: Supported GATT procedures.  By default, all are enabled. */
 
 #ifndef NIMBLE_OPT_GATT_DISC_ALL_SVCS
@@ -367,6 +366,14 @@
 #define  BLE_LL_CFG_FEAT_EXT_SCAN_FILT          (0)
 #endif
 
+/**
+ * This macro exists to help catch bugs at compile time.  If code uses this
+ * macro to check an option value, the compiler will complain when this header
+ * is not included.  If the code checks the option symbol directly without
+ * including this header, it will appear as though the option is set to 0.
+ */
+#define NIMBLE_OPT(x)                           NIMBLE_OPT_ ## x
+
 /* Include automatically-generated settings. */
 #include "nimble/nimble_opt_auto.h"
 



[07/12] incubator-mynewt-core git commit: bletiny - Increase shell line length (128 to 256).

Posted by cc...@apache.org.
bletiny - Increase shell line length (128 to 256).

This is required for the encryption procedure ("sec restart").  To
initiate this procedure, the user needs to specify the ediv, rand, and
16-byte ltk on the command line.


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

Branch: refs/heads/develop
Commit: c16e6c12cfd50073c89ec64f3e8e775c51f9af3a
Parents: e404f06
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 16:08:33 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 16:08:33 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/src/main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c16e6c12/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 7e84792..d3d27f8 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -55,7 +55,7 @@
 #define BLE_LL_TASK_PRI         (OS_TASK_PRI_HIGHEST)
 
 #define SHELL_TASK_PRIO         (3)
-#define SHELL_MAX_INPUT_LEN     (128)
+#define SHELL_MAX_INPUT_LEN     (256)
 #define SHELL_TASK_STACK_SIZE   (OS_STACK_ALIGN(312))
 static bssnz_t os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
 


[12/12] incubator-mynewt-core git commit: BLE Host - Fix misordered fields in unit test.

Posted by cc...@apache.org.
BLE Host - Fix misordered fields in unit test.

Newer versions of gcc warn about the misordered fields.  This was
causing a unit test to fail to compile under Linux.


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

Branch: refs/heads/develop
Commit: 48df9b088c50cfc97d2518a823294ace761633d6
Parents: 9d7ab35
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 22:52:46 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 22:52:46 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/test/ble_gatt_conn_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/48df9b08/net/nimble/host/src/test/ble_gatt_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_conn_test.c b/net/nimble/host/src/test/ble_gatt_conn_test.c
index a319a05..7b6628f 100644
--- a/net/nimble/host/src/test/ble_gatt_conn_test.c
+++ b/net/nimble/host/src/test/ble_gatt_conn_test.c
@@ -471,7 +471,7 @@ TEST_CASE(ble_gatt_conn_test_disconnect)
 
     write_rel_arg.exp_conn_handle = 3;
     rc = ble_gattc_write_reliable(3,
-                                  ((struct ble_gatt_attr[]){{8, 0, NULL, 0}}),
+                                  ((struct ble_gatt_attr[]){{8, 0, 0, NULL}}),
                                   1, ble_gatt_conn_test_write_rel_cb,
                                   &write_rel_arg);
     TEST_ASSERT_FATAL(rc == 0);


[02/12] incubator-mynewt-core git commit: bleprph - Add characteristics which require enc.

Posted by cc...@apache.org.
bleprph - Add characteristics which require enc.


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

Branch: refs/heads/develop
Commit: 05401e1ebad4bbcf3b0bb9a5575ce5a15d8f567d
Parents: c408561
Author: Christopher Collins <cc...@apache.org>
Authored: Tue May 17 20:25:10 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue May 17 20:25:10 2016 -0700

----------------------------------------------------------------------
 apps/bleprph/src/bleprph.h  |   5 ++
 apps/bleprph/src/gatt_svr.c | 150 +++++++++++++++++++++++++++++++++++----
 apps/bleprph/src/keystore.c | 100 ++++++++++++++++++++++++++
 apps/bleprph/src/main.c     | 126 ++++++++++++++++++++++++++------
 4 files changed, 345 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/05401e1e/apps/bleprph/src/bleprph.h
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/bleprph.h b/apps/bleprph/src/bleprph.h
index e903fe0..94024d7 100644
--- a/apps/bleprph/src/bleprph.h
+++ b/apps/bleprph/src/bleprph.h
@@ -24,6 +24,11 @@
 
 void gatt_svr_init(void);
 
+int keystore_lookup(uint16_t ediv, uint64_t rand_num,
+                    void *out_ltk, int *out_authenticated);
+int keystore_add(uint16_t ediv, uint64_t rand_num, uint8_t *key,
+                 int authenticated);
+
 extern struct log bleprph_log;
 
 extern const char *bleprph_device_name;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/05401e1e/apps/bleprph/src/gatt_svr.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/gatt_svr.c b/apps/bleprph/src/gatt_svr.c
index 27a1433..aacc650 100644
--- a/apps/bleprph/src/gatt_svr.c
+++ b/apps/bleprph/src/gatt_svr.c
@@ -6,7 +6,7 @@
  * 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,
@@ -18,6 +18,7 @@
  */
 
 #include <assert.h>
+#include <stdio.h>
 #include <string.h>
 #include "bsp/bsp.h"
 #include "console/console.h"
@@ -31,6 +32,35 @@
 #define GATT_SVR_CHR_UNR_ALERT_STAT_UUID      0x2A45
 #define GATT_SVR_CHR_ALERT_NOT_CTRL_PT        0x2A44
 
+/**
+ * The vendor specific "bleprph" service consists of two characteristics:
+ *     o "read": a single-byte characteristic that can only be read of an
+ *       encryptted connection.
+ *     o "write": a single-byte characteristic that can always be read, but
+ *       can only be written over an encrypted connection.
+ */
+
+/* 59462f12-9543-9999-12c8-58b459a2712d */
+static const uint8_t gatt_svr_svc_bleprph[16] = {
+    0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
+    0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59
+};
+
+/* 5c3a659e-897e-45e1-b016-007107c96df6 */
+static const uint8_t gatt_svr_chr_bleprph_read[16] = {
+    0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
+    0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c
+};
+
+/* 5c3a659e-897e-45e1-b016-007107c96df7 */
+static const uint8_t gatt_svr_chr_bleprph_write[16] = {
+    0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
+    0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c
+};
+
+static uint8_t gatt_svr_nimble_test_read_val;
+static uint8_t gatt_svr_nimble_test_write_val;
+
 static int
 gatt_svr_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
                         union ble_gatt_access_ctxt *ctxt, void *arg);
@@ -42,6 +72,11 @@ gatt_svr_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle,
                           uint8_t op, union ble_gatt_access_ctxt *ctxt,
                           void *arg);
 
+static int
+gatt_svr_chr_access_bleprph(uint16_t conn_handle, uint16_t attr_handle,
+                                uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                                void *arg);
+
 static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
     {
         /*** Service: GAP. */
@@ -120,6 +155,26 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
     },
 
     {
+        /*** Service: bleprph. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = (void *)gatt_svr_svc_bleprph,
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            /*** Characteristic: Read. */
+            .uuid128 = (void *)gatt_svr_chr_bleprph_read,
+            .access_cb = gatt_svr_chr_access_bleprph,
+            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
+        }, {
+            /*** Characteristic: Write. */
+            .uuid128 = (void *)gatt_svr_chr_bleprph_write,
+            .access_cb = gatt_svr_chr_access_bleprph,
+            .flags = BLE_GATT_CHR_F_READ |
+                     BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
+        }, {
+            0, /* No more characteristics in this service. */
+        } },
+    },
+
+    {
         0, /* No more services. */
     },
 };
@@ -295,34 +350,101 @@ gatt_svr_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle,
     }
 }
 
+static int
+gatt_svr_chr_access_bleprph(uint16_t conn_handle, uint16_t attr_handle,
+                            uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                            void *arg)
+{
+    void *uuid128;
+    int rc;
+
+    uuid128 = ctxt->chr_access.chr->uuid128;
+
+    /* Determine which characteristic is being accessed by examining its
+     * 128-bit UUID.
+     */
+
+    if (memcmp(uuid128, gatt_svr_chr_bleprph_read, 16) == 0) {
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = &gatt_svr_nimble_test_read_val;
+        ctxt->chr_access.len = sizeof gatt_svr_nimble_test_read_val;
+        return 0;
+    }
+
+    if (memcmp(uuid128, gatt_svr_chr_bleprph_write, 16) == 0) {
+        switch (op) {
+        case BLE_GATT_ACCESS_OP_READ_CHR:
+            ctxt->chr_access.data = &gatt_svr_nimble_test_write_val;
+            ctxt->chr_access.len = sizeof gatt_svr_nimble_test_write_val;
+            return 0;
+
+        case BLE_GATT_ACCESS_OP_WRITE_CHR:
+            rc = gatt_svr_chr_write(op, ctxt,
+                                    sizeof gatt_svr_nimble_test_write_val,
+                                    sizeof gatt_svr_nimble_test_write_val,
+                                    &gatt_svr_nimble_test_write_val, NULL);
+            return rc;
+
+        default:
+            assert(0);
+            return BLE_ATT_ERR_UNLIKELY;
+        }
+    }
+
+    /* Unknown characteristic; the nimble stack should not have called this
+     * function.
+     */
+    assert(0);
+    return BLE_ATT_ERR_UNLIKELY;
+}
+
+static char *
+gatt_svr_uuid128_to_s(void *uuid128, char *dst)
+{
+    uint16_t uuid16;
+    uint8_t *u8p;
+
+    uuid16 = ble_uuid_128_to_16(uuid128);
+    if (uuid16 != 0) {
+        sprintf(dst, "0x%04x", uuid16);
+        return dst;
+    }
+
+    u8p = uuid128;
+
+    sprintf(dst,      "%02x%02x%02x%02x-", u8p[15], u8p[14], u8p[13], u8p[12]);
+    sprintf(dst + 9,  "%02x%02x-%02x%02x-", u8p[11], u8p[10], u8p[9], u8p[8]);
+    sprintf(dst + 19, "%02x%02x%02x%02x%02x%02x%02x%02x",
+            u8p[7], u8p[6], u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
+
+    return dst;
+}
+
 static void
 gatt_svr_register_cb(uint8_t op, union ble_gatt_register_ctxt *ctxt, void *arg)
 {
-    uint16_t uuid16;
+    char buf[40];
 
     switch (op) {
     case BLE_GATT_REGISTER_OP_SVC:
-        uuid16 = ble_uuid_128_to_16(ctxt->svc_reg.svc->uuid128);
-        assert(uuid16 != 0);
-        BLEPRPH_LOG(DEBUG, "registered service 0x%04x with handle=%d\n",
-                    uuid16, ctxt->svc_reg.handle);
+        BLEPRPH_LOG(DEBUG, "registered service %s with handle=%d\n",
+                    gatt_svr_uuid128_to_s(ctxt->svc_reg.svc->uuid128, buf),
+                    ctxt->svc_reg.handle);
         break;
 
     case BLE_GATT_REGISTER_OP_CHR:
-        uuid16 = ble_uuid_128_to_16(ctxt->chr_reg.chr->uuid128);
-        assert(uuid16 != 0);
-        BLEPRPH_LOG(DEBUG, "registering characteristic 0x%04x with "
+        BLEPRPH_LOG(DEBUG, "registering characteristic %s with "
                            "def_handle=%d val_handle=%d\n",
-                    uuid16, ctxt->chr_reg.def_handle,
+                    gatt_svr_uuid128_to_s(ctxt->chr_reg.chr->uuid128, buf),
+                    ctxt->chr_reg.def_handle,
                     ctxt->chr_reg.val_handle);
         break;
 
     case BLE_GATT_REGISTER_OP_DSC:
-        uuid16 = ble_uuid_128_to_16(ctxt->dsc_reg.dsc->uuid128);
-        assert(uuid16 != 0);
-        BLEPRPH_LOG(DEBUG, "registering descriptor 0x%04x with handle=%d "
+        BLEPRPH_LOG(DEBUG, "registering descriptor %s with handle=%d "
                            "chr_handle=%d\n",
-                    uuid16, ctxt->dsc_reg.dsc_handle,
+                    gatt_svr_uuid128_to_s(ctxt->dsc_reg.dsc->uuid128, buf),
+                    ctxt->dsc_reg.dsc_handle,
                     ctxt->dsc_reg.chr_def_handle);
         break;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/05401e1e/apps/bleprph/src/keystore.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/keystore.c b/apps/bleprph/src/keystore.c
new file mode 100644
index 0000000..5f3ff55
--- /dev/null
+++ b/apps/bleprph/src/keystore.c
@@ -0,0 +1,100 @@
+/**
+ * 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 "host/ble_hs.h"
+
+#define KEYSTORE_MAX_ENTRIES   4
+
+struct keystore_entry {
+    uint64_t rand_num;
+    uint16_t ediv;
+    uint8_t ltk[16];
+
+    unsigned authenticated:1;
+
+    /* XXX: authreq. */
+};
+
+static struct keystore_entry keystore_entries[KEYSTORE_MAX_ENTRIES];
+static int keystore_num_entries;
+
+/**
+ * Searches the database for a long-term key matching the specified criteria.
+ *
+ * @return                      0 if a key was found; else BLE_HS_ENOENT.
+ */
+int
+keystore_lookup(uint16_t ediv, uint64_t rand_num,
+                void *out_ltk, int *out_authenticated)
+{
+    struct keystore_entry *entry;
+    int i;
+
+    for (i = 0; i < keystore_num_entries; i++) {
+        entry = keystore_entries + i;
+
+        if (entry->ediv == ediv && entry->rand_num == rand_num) {
+            memcpy(out_ltk, entry->ltk, sizeof entry->ltk);
+            *out_authenticated = entry->authenticated;
+
+            return 0;
+        }
+    }
+
+    return BLE_HS_ENOENT;
+}
+
+/**
+ * Adds the specified key to the database.
+ *
+ * @return                      0 on success; BLE_HS_ENOMEM if the database is
+ *                                  full.
+ */
+int
+keystore_add(uint16_t ediv, uint64_t rand_num, uint8_t *ltk, int authenticated)
+{
+    struct keystore_entry *entry;
+
+    if (keystore_num_entries >= KEYSTORE_MAX_ENTRIES) {
+        return BLE_HS_ENOMEM;
+    }
+
+    entry = keystore_entries + keystore_num_entries;
+    keystore_num_entries++;
+
+    entry->ediv = ediv;
+    entry->rand_num = rand_num;
+    memcpy(entry->ltk, ltk, sizeof entry->ltk);
+    entry->authenticated = authenticated;
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/05401e1e/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 97a9367..8731c06 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -6,7 +6,7 @@
  * 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,
@@ -36,6 +36,7 @@
 #include "host/ble_att.h"
 #include "host/ble_gap.h"
 #include "host/ble_gatt.h"
+#include "host/ble_l2cap.h"
 #include "controller/ble_ll.h"
 
 #include "bleprph.h"
@@ -58,11 +59,10 @@ struct log bleprph_log;
 
 /** Priority of the nimble host and controller tasks. */
 #define BLE_LL_TASK_PRI             (OS_TASK_PRI_HIGHEST)
-#define BLEPRPH_BLE_HS_PRIO         (1)
 
 /** bleprph task settings. */
+#define BLEPRPH_TASK_PRIO           1
 #define BLEPRPH_STACK_SIZE          (OS_STACK_ALIGN(336))
-#define BLEPRPH_TASK_PRIO           (BLEPRPH_BLE_HS_PRIO + 1)
 
 struct os_eventq bleprph_evq;
 struct os_task bleprph_task;
@@ -84,7 +84,7 @@ uint8_t bleprph_reconnect_addr[6];
 uint8_t bleprph_pref_conn_params[8];
 uint8_t bleprph_gatt_service_changed[4];
 
-static int bleprph_on_connect(int event, int status,
+static int bleprph_gap_event(int event, int status,
                               struct ble_gap_conn_ctxt *ctxt, void *arg);
 
 /**
@@ -107,11 +107,16 @@ static void
 bleprph_print_conn_desc(struct ble_gap_conn_desc *desc)
 {
     BLEPRPH_LOG(INFO, "handle=%d peer_addr_type=%d peer_addr=",
-                desc->conn_handle, desc->peer_addr_type);
+                desc->conn_handle,
+                desc->peer_addr_type);
     bleprph_print_bytes(desc->peer_addr, 6);
-    BLEPRPH_LOG(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d",
-                desc->conn_itvl, desc->conn_latency,
-                desc->supervision_timeout);
+    BLEPRPH_LOG(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
+                      "encrypted=%d authenticated=%d",
+                desc->conn_itvl,
+                desc->conn_latency,
+                desc->supervision_timeout,
+                desc->sec_state.enc_enabled,
+                desc->sec_state.authenticated);
 }
 
 /**
@@ -138,19 +143,42 @@ bleprph_advertise(void)
 
     /* Begin advertising. */
     rc = ble_gap_adv_start(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_UND,
-                           NULL, 0, NULL, bleprph_on_connect, NULL);
+                           NULL, 0, NULL, bleprph_gap_event, NULL);
     if (rc != 0) {
         BLEPRPH_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc);
         return;
     }
 }
 
+/**
+ * The nimble host executes this callback when a GAP event occurs.  The
+ * application associates a GAP event callback with each connection that forms.
+ * bleprph uses the same callback for all connections.
+ *
+ * @param event                 The type of event being signalled.
+ * @param status                The error code associated with the event
+ *                                  (0 = success).
+ * @param ctxt                  Various information pertaining to the event.
+ * @param arg                   Application-specified argument; unuesd by
+ *                                  bleprph.
+ *
+ * @return                      0 if the application successfully handled the
+ *                                  event; nonzero on failure.  The semantics
+ *                                  of the return code is specific to the
+ *                                  particular GAP event being signalled.
+ */
 static int
-bleprph_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
-                   void *arg)
+bleprph_gap_event(int event, int status, struct ble_gap_conn_ctxt *ctxt,
+                  void *arg)
 {
+    int authenticated;
+    int rc;
+
     switch (event) {
     case BLE_GAP_EVENT_CONN:
+        /* A new connection has been established or an existing one has been
+         * terminated.
+         */
         BLEPRPH_LOG(INFO, "connection %s; status=%d ",
                     status == 0 ? "up" : "down", status);
         bleprph_print_conn_desc(ctxt->desc);
@@ -160,18 +188,69 @@ bleprph_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
             /* Connection terminated; resume advertising. */
             bleprph_advertise();
         }
-        break;
+        return 0;
 
     case BLE_GAP_EVENT_CONN_UPDATED:
+        /* The central has updated the connection parameters. */
         BLEPRPH_LOG(INFO, "connection updated; status=%d ", status);
         bleprph_print_conn_desc(ctxt->desc);
         BLEPRPH_LOG(INFO, "\n");
-        break;
+        return 0;
+
+    case BLE_GAP_EVENT_LTK_REQUEST:
+        /* 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",
+                    ctxt->ltk_params->ediv, ctxt->ltk_params->rand_num);
+
+        /* Perform a key lookup and populate the context object with the
+         * result.  The nimble stack will use this key if this function returns
+         * success.
+         */
+        rc = keystore_lookup(ctxt->ltk_params->ediv,
+                             ctxt->ltk_params->rand_num, ctxt->ltk_params->ltk,
+                             &authenticated);
+        if (rc == 0) {
+            ctxt->ltk_params->authenticated = authenticated;
+            BLEPRPH_LOG(INFO, "ltk=");
+            bleprph_print_bytes(ctxt->ltk_params->ltk,
+                                sizeof ctxt->ltk_params->ltk);
+            BLEPRPH_LOG(INFO, " authenticated=%d\n", authenticated);
+        } else {
+            BLEPRPH_LOG(INFO, "no matching ltk\n");
+        }
+
+        /* Indicate whether we were able to find an appropriate key. */
+        return rc;
+
+    case BLE_GAP_EVENT_KEY_EXCHANGE:
+        /* The central is sending us key information or vice-versa.  If the
+         * central is doing the sending, save the long-term key in the in-RAM
+         * database.  This permits bonding to occur on subsequent connections
+         * with this peer (as long as bleprph isn't restarted!).
+         */
+        if (ctxt->key_params->is_ours   &&
+            ctxt->key_params->ltk_valid &&
+            ctxt->key_params->ediv_rand_valid) {
+
+            rc = keystore_add(ctxt->key_params->ediv,
+                              ctxt->key_params->rand_val,
+                              ctxt->key_params->ltk,
+                              ctxt->desc->sec_state.authenticated);
+            if (rc != 0) {
+                BLEPRPH_LOG(INFO, "error persisting LTK; status=%d\n", rc);
+            }
+        }
+        return 0;
 
-    case BLE_GAP_EVENT_CONN_UPDATE_REQ:
-        BLEPRPH_LOG(INFO, "connection update request; status=%d ", status);
-        *ctxt->update.self_params = *ctxt->update.peer_params;
-        break;
+    case BLE_GAP_EVENT_SECURITY:
+        /* Encryption has been enabled or disabled for this connection. */
+        BLEPRPH_LOG(INFO, "security event; status=%d ", status);
+        bleprph_print_conn_desc(ctxt->desc);
+        BLEPRPH_LOG(INFO, "\n");
+        return 0;
     }
 
     return 0;
@@ -243,8 +322,8 @@ main(void)
     srand(seed);
 
     /* Initialize msys mbufs. */
-    rc = os_mempool_init(&bleprph_mbuf_mpool, MBUF_NUM_MBUFS, 
-                         MBUF_MEMBLOCK_SIZE, bleprph_mbuf_mpool_data, 
+    rc = os_mempool_init(&bleprph_mbuf_mpool, MBUF_NUM_MBUFS,
+                         MBUF_MEMBLOCK_SIZE, bleprph_mbuf_mpool_data,
                          "bleprph_mbuf_data");
     assert(rc == 0);
 
@@ -272,12 +351,15 @@ main(void)
     cfg = ble_hs_cfg_dflt;
     cfg.max_hci_bufs = 3;
     cfg.max_connections = 1;
-    cfg.max_attrs = 32;
-    cfg.max_services = 4;
+    cfg.max_attrs = 42;
+    cfg.max_services = 5;
     cfg.max_client_configs = 6;
     cfg.max_gattc_procs = 2;
     cfg.max_l2cap_chans = 3;
-    cfg.max_l2cap_sig_procs = 2;
+    cfg.max_l2cap_sig_procs = 1;
+    cfg.sm_bonding = 1;
+    cfg.sm_our_key_dist = BLE_L2CAP_SM_PAIR_KEY_DIST_ENC;
+    cfg.sm_their_key_dist = BLE_L2CAP_SM_PAIR_KEY_DIST_ENC;
 
     /* Initialize eventq */
     os_eventq_init(&bleprph_evq);


[03/12] incubator-mynewt-core git commit: bleprph - export GATT server data from header.

Posted by cc...@apache.org.
bleprph - export GATT server data from header.


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

Branch: refs/heads/develop
Commit: b54399551a9fd33a4eb54cefe467b63198d2d09f
Parents: 05401e1
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 10:11:41 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 10:11:41 2016 -0700

----------------------------------------------------------------------
 apps/bleprph/src/bleprph.h  | 26 +++++++++++++++++++-------
 apps/bleprph/src/gatt_svr.c | 13 +++----------
 apps/bleprph/src/main.c     | 18 ++++++++++++++++--
 3 files changed, 38 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b5439955/apps/bleprph/src/bleprph.h
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/bleprph.h b/apps/bleprph/src/bleprph.h
index 94024d7..1a9b150 100644
--- a/apps/bleprph/src/bleprph.h
+++ b/apps/bleprph/src/bleprph.h
@@ -22,13 +22,6 @@
 
 #include "log/log.h"
 
-void gatt_svr_init(void);
-
-int keystore_lookup(uint16_t ediv, uint64_t rand_num,
-                    void *out_ltk, int *out_authenticated);
-int keystore_add(uint16_t ediv, uint64_t rand_num, uint8_t *key,
-                 int authenticated);
-
 extern struct log bleprph_log;
 
 extern const char *bleprph_device_name;
@@ -45,4 +38,23 @@ extern uint8_t bleprph_gatt_service_changed[4];
 #define BLEPRPH_LOG(lvl, ...) \
     LOG_ ## lvl(&bleprph_log, BLEPRPH_LOG_MODULE, __VA_ARGS__)
 
+/** GATT server. */
+#define GATT_SVR_SVC_ALERT_UUID               0x1811
+#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID   0x2A47
+#define GATT_SVR_CHR_NEW_ALERT                0x2A46
+#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID   0x2A48
+#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID      0x2A45
+#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT        0x2A44
+extern const uint8_t gatt_svr_svc_bleprph[16];
+extern const uint8_t gatt_svr_chr_bleprph_read[16];
+extern const uint8_t gatt_svr_chr_bleprph_write[16];
+
+void gatt_svr_init(void);
+
+/** Keystore. */
+int keystore_lookup(uint16_t ediv, uint64_t rand_num,
+                    void *out_ltk, int *out_authenticated);
+int keystore_add(uint16_t ediv, uint64_t rand_num, uint8_t *key,
+                 int authenticated);
+
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b5439955/apps/bleprph/src/gatt_svr.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/gatt_svr.c b/apps/bleprph/src/gatt_svr.c
index aacc650..fe2a9ed 100644
--- a/apps/bleprph/src/gatt_svr.c
+++ b/apps/bleprph/src/gatt_svr.c
@@ -25,13 +25,6 @@
 #include "host/ble_hs.h"
 #include "bleprph.h"
 
-#define GATT_SVR_SVC_ALERT_UUID               0x1811
-#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID   0x2A47
-#define GATT_SVR_CHR_NEW_ALERT                0x2A46
-#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID   0x2A48
-#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID      0x2A45
-#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT        0x2A44
-
 /**
  * The vendor specific "bleprph" service consists of two characteristics:
  *     o "read": a single-byte characteristic that can only be read of an
@@ -41,19 +34,19 @@
  */
 
 /* 59462f12-9543-9999-12c8-58b459a2712d */
-static const uint8_t gatt_svr_svc_bleprph[16] = {
+const uint8_t gatt_svr_svc_bleprph[16] = {
     0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
     0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59
 };
 
 /* 5c3a659e-897e-45e1-b016-007107c96df6 */
-static const uint8_t gatt_svr_chr_bleprph_read[16] = {
+const uint8_t gatt_svr_chr_bleprph_read[16] = {
     0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
     0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c
 };
 
 /* 5c3a659e-897e-45e1-b016-007107c96df7 */
-static const uint8_t gatt_svr_chr_bleprph_write[16] = {
+const uint8_t gatt_svr_chr_bleprph_write[16] = {
     0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
     0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c
 };

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b5439955/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 8731c06..5037bc8 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -85,7 +85,7 @@ uint8_t bleprph_pref_conn_params[8];
 uint8_t bleprph_gatt_service_changed[4];
 
 static int bleprph_gap_event(int event, int status,
-                              struct ble_gap_conn_ctxt *ctxt, void *arg);
+                             struct ble_gap_conn_ctxt *ctxt, void *arg);
 
 /**
  * Utility function to log an array of bytes.
@@ -130,11 +130,25 @@ bleprph_advertise(void)
     struct ble_hs_adv_fields fields;
     int rc;
 
-    /* Set the advertisement data included in our advertisements. */
+    /**
+     *  Set the advertisement data included in our advertisements:
+     *     o Advertising tx power.
+     *     o Device name.
+     *     o 16-bit service UUIDs (alert notifications).
+     */
+
     memset(&fields, 0, sizeof fields);
+
+    fields.tx_pwr_lvl_is_present = 1;
+
     fields.name = (uint8_t *)bleprph_device_name;
     fields.name_len = strlen(bleprph_device_name);
     fields.name_is_complete = 1;
+
+    fields.uuids16 = (uint16_t[]){ GATT_SVR_SVC_ALERT_UUID };
+    fields.num_uuids16 = 1;
+    fields.uuids16_is_complete = 1;
+
     rc = ble_gap_adv_set_fields(&fields);
     if (rc != 0) {
         BLEPRPH_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc);


[06/12] incubator-mynewt-core git commit: bletiny - Copy bleprph's gatt_svr code.

Posted by cc...@apache.org.
bletiny - Copy bleprph's gatt_svr code.


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

Branch: refs/heads/develop
Commit: e404f064fce8784133f3f05a1090a02015d82779
Parents: 47a1ead
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 14:43:20 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 14:43:20 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/src/bletiny_priv.h |  20 +-
 apps/bletiny/src/gatt_svr.c     | 457 +++++++++++++++++++++++++++++++++++
 apps/bletiny/src/keystore.c     | 100 ++++++++
 apps/bletiny/src/main.c         | 119 ++++-----
 apps/bletiny/src/periph.c       | 344 --------------------------
 5 files changed, 621 insertions(+), 419 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e404f064/apps/bletiny/src/bletiny_priv.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny_priv.h b/apps/bletiny/src/bletiny_priv.h
index fc85bc8..279f96b 100644
--- a/apps/bletiny/src/bletiny_priv.h
+++ b/apps/bletiny/src/bletiny_priv.h
@@ -115,7 +115,6 @@ int parse_arg_uuid(char *name, uint8_t *dst_uuid128);
 int parse_err_too_few_args(char *cmd_name);
 int parse_arg_all(int argc, char **argv);
 int cmd_init(void);
-void periph_init(void);
 int nm_chr_access(uint16_t conn_handle, uint16_t attr_handle,
                   uint8_t op, union ble_gatt_access_ctxt *ctxt,
                   void *arg);
@@ -174,4 +173,23 @@ int bletiny_sec_restart(uint16_t conn_handle, uint8_t *ltk, uint16_t ediv,
 #define BLETINY_LOG(lvl, ...) \
     LOG_ ## lvl(&bletiny_log, BLETINY_LOG_MODULE, __VA_ARGS__)
 
+/** GATT server. */
+#define GATT_SVR_SVC_ALERT_UUID               0x1811
+#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID   0x2A47
+#define GATT_SVR_CHR_NEW_ALERT                0x2A46
+#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID   0x2A48
+#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID      0x2A45
+#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT        0x2A44
+extern const uint8_t gatt_svr_svc_bleprph[16];
+extern const uint8_t gatt_svr_chr_bleprph_read[16];
+extern const uint8_t gatt_svr_chr_bleprph_write[16];
+
+void gatt_svr_init(void);
+
+/** Keystore. */
+int keystore_lookup(uint16_t ediv, uint64_t rand_num,
+                    void *out_ltk, int *out_authenticated);
+int keystore_add(uint16_t ediv, uint64_t rand_num, uint8_t *key,
+                 int authenticated);
+
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e404f064/apps/bletiny/src/gatt_svr.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/gatt_svr.c b/apps/bletiny/src/gatt_svr.c
new file mode 100644
index 0000000..71df9b0
--- /dev/null
+++ b/apps/bletiny/src/gatt_svr.c
@@ -0,0 +1,457 @@
+/**
+ * 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.
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include "bsp/bsp.h"
+#include "console/console.h"
+#include "host/ble_hs.h"
+#include "bletiny_priv.h"
+
+/**
+ * The vendor specific "bleprph" service consists of two characteristics:
+ *     o "read": a single-byte characteristic that can only be read of an
+ *       encryptted connection.
+ *     o "write": a single-byte characteristic that can always be read, but
+ *       can only be written over an encrypted connection.
+ */
+
+/* 59462f12-9543-9999-12c8-58b459a2712d */
+const uint8_t gatt_svr_svc_bleprph[16] = {
+    0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
+    0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59
+};
+
+/* 5c3a659e-897e-45e1-b016-007107c96df6 */
+const uint8_t gatt_svr_chr_bleprph_read[16] = {
+    0xf6, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
+    0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c
+};
+
+/* 5c3a659e-897e-45e1-b016-007107c96df7 */
+const uint8_t gatt_svr_chr_bleprph_write[16] = {
+    0xf7, 0x6d, 0xc9, 0x07, 0x71, 0x00, 0x16, 0xb0,
+    0xe1, 0x45, 0x7e, 0x89, 0x9e, 0x65, 0x3a, 0x5c
+};
+
+static uint8_t gatt_svr_nimble_test_read_val;
+static uint8_t gatt_svr_nimble_test_write_val;
+
+static int
+gatt_svr_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
+                        union ble_gatt_access_ctxt *ctxt, void *arg);
+static int
+gatt_svr_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
+                         union ble_gatt_access_ctxt *ctxt, void *arg);
+static int
+gatt_svr_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle,
+                          uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                          void *arg);
+
+static int
+gatt_svr_chr_access_bleprph(uint16_t conn_handle, uint16_t attr_handle,
+                                uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                                void *arg);
+
+static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
+    {
+        /*** Service: GAP. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(BLE_GAP_SVC_UUID16),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            /*** Characteristic: Device Name. */
+            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_DEVICE_NAME),
+            .access_cb = gatt_svr_chr_access_gap,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            /*** Characteristic: Appearance. */
+            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_APPEARANCE),
+            .access_cb = gatt_svr_chr_access_gap,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            /*** Characteristic: Peripheral Privacy Flag. */
+            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG),
+            .access_cb = gatt_svr_chr_access_gap,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            /*** Characteristic: Reconnection Address. */
+            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_RECONNECT_ADDR),
+            .access_cb = gatt_svr_chr_access_gap,
+            .flags = BLE_GATT_CHR_F_WRITE,
+        }, {
+            /*** Characteristic: Peripheral Preferred Connection Parameters. */
+            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS),
+            .access_cb = gatt_svr_chr_access_gap,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            0, /* No more characteristics in this service. */
+        } },
+    },
+
+    {
+        /*** Service: GATT */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(BLE_GATT_SVC_UUID16),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(BLE_GATT_CHR_SERVICE_CHANGED_UUID16),
+            .access_cb = gatt_svr_chr_access_gatt,
+            .flags = BLE_GATT_CHR_F_INDICATE,
+        }, {
+            0, /* No more characteristics in this service. */
+        } },
+    },
+
+    {
+        /*** Alert Notification Service. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(GATT_SVR_SVC_ALERT_UUID),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID),
+            .access_cb = gatt_svr_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            .uuid128 = BLE_UUID16(GATT_SVR_CHR_NEW_ALERT),
+            .access_cb = gatt_svr_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_NOTIFY,
+        }, {
+            .uuid128 = BLE_UUID16(GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID),
+            .access_cb = gatt_svr_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            .uuid128 = BLE_UUID16(GATT_SVR_CHR_UNR_ALERT_STAT_UUID),
+            .access_cb = gatt_svr_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_NOTIFY,
+        }, {
+            .uuid128 = BLE_UUID16(GATT_SVR_CHR_ALERT_NOT_CTRL_PT),
+            .access_cb = gatt_svr_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_WRITE,
+        }, {
+            0, /* No more characteristics in this service. */
+        } },
+    },
+
+    {
+        /*** Service: bleprph. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = (void *)gatt_svr_svc_bleprph,
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            /*** Characteristic: Read. */
+            .uuid128 = (void *)gatt_svr_chr_bleprph_read,
+            .access_cb = gatt_svr_chr_access_bleprph,
+            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_READ_ENC,
+        }, {
+            /*** Characteristic: Write. */
+            .uuid128 = (void *)gatt_svr_chr_bleprph_write,
+            .access_cb = gatt_svr_chr_access_bleprph,
+            .flags = BLE_GATT_CHR_F_READ |
+                     BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_ENC,
+        }, {
+            0, /* No more characteristics in this service. */
+        } },
+    },
+
+    {
+        0, /* No more services. */
+    },
+};
+
+static int
+gatt_svr_chr_write(uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                   uint16_t min_len, uint16_t max_len, void *dst,
+                   uint16_t *len)
+{
+    assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
+    if (ctxt->chr_access.len < min_len ||
+        ctxt->chr_access.len > max_len) {
+        return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+    }
+    memcpy(dst, ctxt->chr_access.data, ctxt->chr_access.len);
+    if (len != NULL) {
+        *len = ctxt->chr_access.len;
+    }
+
+    return 0;
+}
+
+static int
+gatt_svr_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
+                        union ble_gatt_access_ctxt *ctxt, void *arg)
+{
+    uint16_t uuid16;
+
+    uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
+    assert(uuid16 != 0);
+
+    switch (uuid16) {
+    case BLE_GAP_CHR_UUID16_DEVICE_NAME:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)bletiny_device_name;
+        ctxt->chr_access.len = strlen(bletiny_device_name);
+        break;
+
+    case BLE_GAP_CHR_UUID16_APPEARANCE:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)&bletiny_appearance;
+        ctxt->chr_access.len = sizeof bletiny_appearance;
+        break;
+
+    case BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)&bletiny_privacy_flag;
+        ctxt->chr_access.len = sizeof bletiny_privacy_flag;
+        break;
+
+    case BLE_GAP_CHR_UUID16_RECONNECT_ADDR:
+        assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
+        if (ctxt->chr_access.len != sizeof bletiny_reconnect_addr) {
+            return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+        }
+        memcpy(bletiny_reconnect_addr, ctxt->chr_access.data,
+               sizeof bletiny_reconnect_addr);
+        break;
+
+    case BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)&bletiny_pref_conn_params;
+        ctxt->chr_access.len = sizeof bletiny_pref_conn_params;
+        break;
+
+    default:
+        assert(0);
+        break;
+    }
+
+    return 0;
+}
+
+static int
+gatt_svr_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
+                         union ble_gatt_access_ctxt *ctxt, void *arg)
+{
+    uint16_t uuid16;
+
+    uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
+    assert(uuid16 != 0);
+
+    switch (uuid16) {
+    case BLE_GATT_CHR_SERVICE_CHANGED_UUID16:
+        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
+            if (ctxt->chr_access.len != sizeof bletiny_gatt_service_changed) {
+                return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+            }
+            memcpy(bletiny_gatt_service_changed, ctxt->chr_access.data,
+                   sizeof bletiny_gatt_service_changed);
+        } else if (op == BLE_GATT_ACCESS_OP_READ_CHR) {
+            ctxt->chr_access.data = (void *)&bletiny_gatt_service_changed;
+            ctxt->chr_access.len = sizeof bletiny_gatt_service_changed;
+        }
+        break;
+
+    default:
+        assert(0);
+        break;
+    }
+
+    return 0;
+}
+
+#define GATT_SVR_NEW_ALERT_VAL_MAX_LEN    64
+
+static const uint8_t gatt_svr_new_alert_cat = 0x01; /* Simple alert. */
+static uint8_t gatt_svr_new_alert_val[GATT_SVR_NEW_ALERT_VAL_MAX_LEN];
+static uint16_t gatt_svr_new_alert_val_len;
+static const uint8_t gatt_svr_unr_alert_cat = 0x01; /* Simple alert. */
+static uint16_t gatt_svr_unr_alert_stat;
+static uint16_t gatt_svr_alert_not_ctrl_pt;
+
+static int
+gatt_svr_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle,
+                          uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                          void *arg)
+{
+    uint16_t uuid16;
+    int rc;
+
+    uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
+    assert(uuid16 != 0);
+
+    switch (uuid16) {
+    case GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)&gatt_svr_new_alert_cat;
+        ctxt->chr_access.len = sizeof gatt_svr_new_alert_cat;
+        return 0;
+
+    case GATT_SVR_CHR_NEW_ALERT:
+        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
+            rc = gatt_svr_chr_write(op, ctxt, 0, sizeof gatt_svr_new_alert_val,
+                                  gatt_svr_new_alert_val,
+                                  &gatt_svr_new_alert_val_len);
+            return rc;
+        } else if (op == BLE_GATT_ACCESS_OP_READ_CHR) {
+            ctxt->chr_access.data = (void *)&gatt_svr_new_alert_val;
+            ctxt->chr_access.len = sizeof gatt_svr_new_alert_val;
+            return 0;
+        }
+
+    case GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)&gatt_svr_unr_alert_cat;
+        ctxt->chr_access.len = sizeof gatt_svr_unr_alert_cat;
+        return 0;
+
+    case GATT_SVR_CHR_UNR_ALERT_STAT_UUID:
+        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
+            rc = gatt_svr_chr_write(op, ctxt, 2, 2, &gatt_svr_unr_alert_stat,
+                                    NULL);
+        } else {
+            ctxt->chr_access.data = (void *)&gatt_svr_unr_alert_stat;
+            ctxt->chr_access.len = sizeof gatt_svr_unr_alert_stat;
+            rc = 0;
+        }
+        return rc;
+
+    case GATT_SVR_CHR_ALERT_NOT_CTRL_PT:
+        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
+            rc = gatt_svr_chr_write(op, ctxt, 2, 2,
+                                    &gatt_svr_alert_not_ctrl_pt, NULL);
+        } else {
+            rc = BLE_ATT_ERR_UNLIKELY;
+        }
+        return rc;
+
+    default:
+        assert(0);
+        return BLE_ATT_ERR_UNLIKELY;
+    }
+}
+
+static int
+gatt_svr_chr_access_bleprph(uint16_t conn_handle, uint16_t attr_handle,
+                            uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                            void *arg)
+{
+    void *uuid128;
+    int rc;
+
+    uuid128 = ctxt->chr_access.chr->uuid128;
+
+    /* Determine which characteristic is being accessed by examining its
+     * 128-bit UUID.
+     */
+
+    if (memcmp(uuid128, gatt_svr_chr_bleprph_read, 16) == 0) {
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = &gatt_svr_nimble_test_read_val;
+        ctxt->chr_access.len = sizeof gatt_svr_nimble_test_read_val;
+        return 0;
+    }
+
+    if (memcmp(uuid128, gatt_svr_chr_bleprph_write, 16) == 0) {
+        switch (op) {
+        case BLE_GATT_ACCESS_OP_READ_CHR:
+            ctxt->chr_access.data = &gatt_svr_nimble_test_write_val;
+            ctxt->chr_access.len = sizeof gatt_svr_nimble_test_write_val;
+            return 0;
+
+        case BLE_GATT_ACCESS_OP_WRITE_CHR:
+            rc = gatt_svr_chr_write(op, ctxt,
+                                    sizeof gatt_svr_nimble_test_write_val,
+                                    sizeof gatt_svr_nimble_test_write_val,
+                                    &gatt_svr_nimble_test_write_val, NULL);
+            return rc;
+
+        default:
+            assert(0);
+            return BLE_ATT_ERR_UNLIKELY;
+        }
+    }
+
+    /* Unknown characteristic; the nimble stack should not have called this
+     * function.
+     */
+    assert(0);
+    return BLE_ATT_ERR_UNLIKELY;
+}
+
+static char *
+gatt_svr_uuid_to_s(void *uuid128, char *dst)
+{
+    uint16_t uuid16;
+    uint8_t *u8p;
+
+    uuid16 = ble_uuid_128_to_16(uuid128);
+    if (uuid16 != 0) {
+        sprintf(dst, "0x%04x", uuid16);
+        return dst;
+    }
+
+    u8p = uuid128;
+
+    sprintf(dst,      "%02x%02x%02x%02x-", u8p[15], u8p[14], u8p[13], u8p[12]);
+    sprintf(dst + 9,  "%02x%02x-%02x%02x-", u8p[11], u8p[10], u8p[9], u8p[8]);
+    sprintf(dst + 19, "%02x%02x%02x%02x%02x%02x%02x%02x",
+            u8p[7], u8p[6], u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
+
+    return dst;
+}
+
+static void
+gatt_svr_register_cb(uint8_t op, union ble_gatt_register_ctxt *ctxt, void *arg)
+{
+    char buf[40];
+
+    switch (op) {
+    case BLE_GATT_REGISTER_OP_SVC:
+        BLETINY_LOG(DEBUG, "registered service %s with handle=%d\n",
+                    gatt_svr_uuid_to_s(ctxt->svc_reg.svc->uuid128, buf),
+                    ctxt->svc_reg.handle);
+        break;
+
+    case BLE_GATT_REGISTER_OP_CHR:
+        BLETINY_LOG(DEBUG, "registering characteristic %s with "
+                           "def_handle=%d val_handle=%d\n",
+                    gatt_svr_uuid_to_s(ctxt->chr_reg.chr->uuid128, buf),
+                    ctxt->chr_reg.def_handle,
+                    ctxt->chr_reg.val_handle);
+        break;
+
+    case BLE_GATT_REGISTER_OP_DSC:
+        BLETINY_LOG(DEBUG, "registering descriptor %s with handle=%d "
+                           "chr_handle=%d\n",
+                    gatt_svr_uuid_to_s(ctxt->dsc_reg.dsc->uuid128, buf),
+                    ctxt->dsc_reg.dsc_handle,
+                    ctxt->dsc_reg.chr_def_handle);
+        break;
+
+    default:
+        assert(0);
+        break;
+    }
+}
+
+void
+gatt_svr_init(void)
+{
+    int rc;
+
+    rc = ble_gatts_register_svcs(gatt_svr_svcs, gatt_svr_register_cb, NULL);
+    assert(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e404f064/apps/bletiny/src/keystore.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/keystore.c b/apps/bletiny/src/keystore.c
new file mode 100644
index 0000000..5f3ff55
--- /dev/null
+++ b/apps/bletiny/src/keystore.c
@@ -0,0 +1,100 @@
+/**
+ * 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 "host/ble_hs.h"
+
+#define KEYSTORE_MAX_ENTRIES   4
+
+struct keystore_entry {
+    uint64_t rand_num;
+    uint16_t ediv;
+    uint8_t ltk[16];
+
+    unsigned authenticated:1;
+
+    /* XXX: authreq. */
+};
+
+static struct keystore_entry keystore_entries[KEYSTORE_MAX_ENTRIES];
+static int keystore_num_entries;
+
+/**
+ * Searches the database for a long-term key matching the specified criteria.
+ *
+ * @return                      0 if a key was found; else BLE_HS_ENOENT.
+ */
+int
+keystore_lookup(uint16_t ediv, uint64_t rand_num,
+                void *out_ltk, int *out_authenticated)
+{
+    struct keystore_entry *entry;
+    int i;
+
+    for (i = 0; i < keystore_num_entries; i++) {
+        entry = keystore_entries + i;
+
+        if (entry->ediv == ediv && entry->rand_num == rand_num) {
+            memcpy(out_ltk, entry->ltk, sizeof entry->ltk);
+            *out_authenticated = entry->authenticated;
+
+            return 0;
+        }
+    }
+
+    return BLE_HS_ENOENT;
+}
+
+/**
+ * Adds the specified key to the database.
+ *
+ * @return                      0 on success; BLE_HS_ENOMEM if the database is
+ *                                  full.
+ */
+int
+keystore_add(uint16_t ediv, uint64_t rand_num, uint8_t *ltk, int authenticated)
+{
+    struct keystore_entry *entry;
+
+    if (keystore_num_entries >= KEYSTORE_MAX_ENTRIES) {
+        return BLE_HS_ENOMEM;
+    }
+
+    entry = keystore_entries + keystore_num_entries;
+    keystore_num_entries++;
+
+    entry->ediv = ediv;
+    entry->rand_num = rand_num;
+    memcpy(entry->ltk, ltk, sizeof entry->ltk);
+    entry->authenticated = authenticated;
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e404f064/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 9c07f76..7e84792 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -96,8 +96,6 @@ struct os_mempool default_mbuf_mpool;
 #define BLETINY_MAX_DSCS               1
 #endif
 
-#define BLETINY_MAX_LTKS                4
-
 struct os_eventq bletiny_evq;
 struct os_task bletiny_task;
 bssnz_t os_stack_t bletiny_stack[BLETINY_STACK_SIZE];
@@ -130,9 +128,6 @@ uint8_t bletiny_reconnect_addr[6];
 uint8_t bletiny_pref_conn_params[8];
 uint8_t bletiny_gatt_service_changed[4];
 
-struct ble_gap_ltk_params bletiny_ltks[BLETINY_MAX_LTKS];
-int bletiny_num_ltks;
-
 #define XSTR(s) STR(s)
 #define STR(s) #s
 
@@ -878,54 +873,10 @@ bletiny_on_notify(uint16_t conn_handle, uint16_t attr_handle,
 }
 
 static int
-bletiny_ltk_lookup(struct ble_gap_ltk_params *ltk_params)
-{
-    struct ble_gap_ltk_params *ltk;
-    int i;
-
-    for (i = 0; i < bletiny_num_ltks; i++) {
-        ltk = bletiny_ltks + i;
-
-        if (ltk->ediv == ltk_params->ediv &&
-            ltk->rand_num == ltk_params->rand_num) {
-
-            memcpy(ltk_params->ltk, ltk->ltk, sizeof ltk_params->ltk);
-            ltk_params->authenticated = ltk->authenticated;
-
-            return 0;
-        }
-    }
-
-    return BLE_HS_ENOENT;
-}
-
-static int
-bletiny_ltk_add(uint16_t ediv, uint64_t rand_num, uint8_t *key,
-                int authenticated)
-{
-    struct ble_gap_ltk_params *ltk;
-
-    if (bletiny_num_ltks >= BLETINY_MAX_LTKS) {
-        return BLE_HS_ENOMEM;
-    }
-
-    ltk = bletiny_ltks + bletiny_num_ltks;
-    bletiny_num_ltks++;
-
-    ltk->ediv = ediv;
-    ltk->rand_num = rand_num;
-    memcpy(ltk->ltk, key, sizeof ltk->ltk);
-    ltk->authenticated = authenticated;
-
-    return 0;
-}
-
-/* XXX: LTK clear. */
-
-static int
-bletiny_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
+bletiny_gap_event(int event, int status, struct ble_gap_conn_ctxt *ctxt,
                    void *arg)
 {
+    int authenticated;
     int conn_idx;
     int rc;
 
@@ -967,22 +918,23 @@ bletiny_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
         *ctxt->update.self_params = *ctxt->update.peer_params;
         return 0;
 
-    case BLE_GAP_EVENT_SECURITY:
-        console_printf("security event; status=%d ", status);
-        bletiny_print_conn_desc(ctxt->desc);
-        console_printf("\n");
-        return 0;
-
-    case BLE_GAP_EVENT_PASSKEY_ACTION:
-        console_printf("passkey action event; status=%d ", status);
-        bletiny_print_passkey_action_parms(ctxt->passkey_action);
-        return 0;
-
     case BLE_GAP_EVENT_LTK_REQUEST:
+        /* 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",
                        ctxt->ltk_params->ediv, ctxt->ltk_params->rand_num);
-        rc = bletiny_ltk_lookup(ctxt->ltk_params);
+
+        /* Perform a key lookup and populate the context object with the
+         * result.  The nimble stack will use this key if this function returns
+         * success.
+         */
+        rc = keystore_lookup(ctxt->ltk_params->ediv,
+                             ctxt->ltk_params->rand_num, ctxt->ltk_params->ltk,
+                             &authenticated);
         if (rc == 0) {
+            ctxt->ltk_params->authenticated = authenticated;
             console_printf("ltk=");
             bletiny_print_bytes(ctxt->ltk_params->ltk,
                                 sizeof ctxt->ltk_params->ltk);
@@ -990,26 +942,45 @@ bletiny_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
         } else {
             console_printf("no matching ltk\n");
         }
+
+        /* Indicate whether we were able to find an appropriate key. */
         return rc;
 
     case BLE_GAP_EVENT_KEY_EXCHANGE:
         console_printf("key exchange event; status=%d ", status);
         bletiny_print_key_exchange_parms(ctxt->key_params);
 
+        /* The central is sending us key information or vice-versa.  If the
+         * central is doing the sending, save the long-term key in the in-RAM
+         * database.  This permits bonding to occur on subsequent connections
+         * with this peer (as long as bletiny isn't restarted!).
+         */
+
         if (ctxt->key_params->is_ours   &&
             ctxt->key_params->ltk_valid &&
             ctxt->key_params->ediv_rand_valid) {
 
-            rc = bletiny_ltk_add(ctxt->key_params->ediv,
-                                 ctxt->key_params->rand_val,
-                                 ctxt->key_params->ltk,
-                                 ctxt->desc->sec_state.authenticated);
+            rc = keystore_add(ctxt->key_params->ediv,
+                              ctxt->key_params->rand_val,
+                              ctxt->key_params->ltk,
+                              ctxt->desc->sec_state.authenticated);
             if (rc != 0) {
                 console_printf("error persisting LTK; status=%d\n", rc);
             }
         }
         return 0;
 
+    case BLE_GAP_EVENT_PASSKEY_ACTION:
+        console_printf("passkey action event; status=%d ", status);
+        bletiny_print_passkey_action_parms(ctxt->passkey_action);
+        return 0;
+
+    case BLE_GAP_EVENT_SECURITY:
+        console_printf("security event; status=%d ", status);
+        bletiny_print_conn_desc(ctxt->desc);
+        console_printf("\n");
+        return 0;
+
     default:
         return 0;
     }
@@ -1230,7 +1201,7 @@ bletiny_adv_start(int disc, int conn, uint8_t *peer_addr, int addr_type,
     int rc;
 
     rc = ble_gap_adv_start(disc, conn, peer_addr, addr_type, params,
-                           bletiny_on_connect, NULL);
+                           bletiny_gap_event, NULL);
     return rc;
 }
 
@@ -1240,7 +1211,7 @@ bletiny_conn_initiate(int addr_type, uint8_t *peer_addr,
 {
     int rc;
 
-    rc = ble_gap_conn_initiate(addr_type, peer_addr, NULL, bletiny_on_connect,
+    rc = ble_gap_conn_initiate(addr_type, peer_addr, NULL, bletiny_gap_event,
                                params);
     return rc;
 }
@@ -1499,11 +1470,11 @@ main(void)
     /* Initialize the BLE host. */
     cfg = ble_hs_cfg_dflt;
     cfg.max_hci_bufs = 3;
-    cfg.max_attrs = 32;
-    cfg.max_services = 4;
-    cfg.max_client_configs = 6;
+    cfg.max_attrs = 36;
+    cfg.max_services = 5;
+    cfg.max_client_configs = (NIMBLE_OPT(MAX_CONNECTIONS) + 1) * 3;
     cfg.max_gattc_procs = 2;
-    cfg.max_l2cap_chans = 3;
+    cfg.max_l2cap_chans = NIMBLE_OPT(MAX_CONNECTIONS) * 3;
     cfg.max_l2cap_sig_procs = 2;
 
     rc = ble_hs_init(&bletiny_evq, &cfg);
@@ -1522,7 +1493,7 @@ main(void)
     htole16(bletiny_pref_conn_params + 4, 0);
     htole16(bletiny_pref_conn_params + 6, BSWAP16(0x100));
 
-    periph_init();
+    gatt_svr_init();
 
     /* Start the OS */
     os_start();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e404f064/apps/bletiny/src/periph.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/periph.c b/apps/bletiny/src/periph.c
deleted file mode 100644
index d0f97e5..0000000
--- a/apps/bletiny/src/periph.c
+++ /dev/null
@@ -1,344 +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.
- */
-
-#include <assert.h>
-#include <string.h>
-#include "bsp/bsp.h"
-#include "console/console.h"
-#include "host/ble_hs.h"
-#include "bletiny_priv.h"
-
-#define CHR_F_FULL_ACCESS       (BLE_GATT_CHR_F_READ                |   \
-                                 BLE_GATT_CHR_F_WRITE_NO_RSP        |   \
-                                 BLE_GATT_CHR_F_WRITE               |   \
-                                 BLE_GATT_CHR_F_NOTIFY              |   \
-                                 BLE_GATT_CHR_F_INDICATE)
-
-#define PERIPH_SVC_ALERT_UUID               0x1811
-#define PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID   0x2A47
-#define PERIPH_CHR_NEW_ALERT                0x2A46
-#define PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID   0x2A48
-#define PERIPH_CHR_UNR_ALERT_STAT_UUID      0x2A45
-#define PERIPH_CHR_ALERT_NOT_CTRL_PT        0x2A44
-
-static int
-periph_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
-                      union ble_gatt_access_ctxt *ctxt, void *arg);
-static int
-periph_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
-                       union ble_gatt_access_ctxt *ctxt, void *arg);
-static int
-periph_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
-                        union ble_gatt_access_ctxt *ctxt, void *arg);
-
-static const struct ble_gatt_svc_def periph_svcs[] = {
-    [0] = {
-        /*** Service: GAP. */
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(BLE_GAP_SVC_UUID16),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            /*** Characteristic: Device Name. */
-            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_DEVICE_NAME),
-            .access_cb = periph_chr_access_gap,
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            /*** Characteristic: Appearance. */
-            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_APPEARANCE),
-            .access_cb = periph_chr_access_gap,
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            /*** Characteristic: Peripheral Privacy Flag. */
-            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG),
-            .access_cb = periph_chr_access_gap,
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            /*** Characteristic: Reconnection Address. */
-            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_RECONNECT_ADDR),
-            .access_cb = periph_chr_access_gap,
-            .flags = BLE_GATT_CHR_F_WRITE,
-        }, {
-            /*** Characteristic: Peripheral Preferred Connection Parameters. */
-            .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS),
-            .access_cb = periph_chr_access_gap,
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            0, /* No more characteristics in this service. */
-        } },
-    },
-
-    [1] = {
-        /*** Service: GATT */
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(BLE_GATT_SVC_UUID16),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(BLE_GATT_CHR_SERVICE_CHANGED_UUID16),
-            .access_cb = periph_chr_access_gatt,
-            .flags = BLE_GATT_CHR_F_INDICATE,
-        }, {
-            0, /* No more characteristics in this service. */
-        } },
-    },
-
-    [2] = {
-        /*** Alert Notification Service. */
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(PERIPH_SVC_ALERT_UUID),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID),
-            .access_cb = periph_chr_access_alert,
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            .uuid128 = BLE_UUID16(PERIPH_CHR_NEW_ALERT),
-            .access_cb = periph_chr_access_alert,
-            .flags = BLE_GATT_CHR_F_NOTIFY,
-        }, {
-            .uuid128 = BLE_UUID16(PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID),
-            .access_cb = periph_chr_access_alert,
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            .uuid128 = BLE_UUID16(PERIPH_CHR_UNR_ALERT_STAT_UUID),
-            .access_cb = periph_chr_access_alert,
-            .flags = BLE_GATT_CHR_F_NOTIFY,
-        }, {
-            .uuid128 = BLE_UUID16(PERIPH_CHR_ALERT_NOT_CTRL_PT),
-            .access_cb = periph_chr_access_alert,
-            .flags = BLE_GATT_CHR_F_WRITE,
-        }, {
-            0, /* No more characteristics in this service. */
-        } },
-    },
-
-    {
-        0, /* No more services. */
-    },
-};
-
-static int
-periph_chr_write(uint8_t op, union ble_gatt_access_ctxt *ctxt,
-                 uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len)
-{
-    assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
-    if (ctxt->chr_access.len < min_len ||
-        ctxt->chr_access.len > max_len) {
-        return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
-    }
-    memcpy(dst, ctxt->chr_access.data, ctxt->chr_access.len);
-    if (len != NULL) {
-        *len = ctxt->chr_access.len;
-    }
-
-    return 0;
-}
-
-static int
-periph_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
-                      union ble_gatt_access_ctxt *ctxt, void *arg)
-{
-    uint16_t uuid16;
-
-    uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
-    assert(uuid16 != 0);
-
-    switch (uuid16) {
-    case BLE_GAP_CHR_UUID16_DEVICE_NAME:
-        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
-        ctxt->chr_access.data = (void *)bletiny_device_name;
-        ctxt->chr_access.len = strlen(bletiny_device_name);
-        break;
-
-    case BLE_GAP_CHR_UUID16_APPEARANCE:
-        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
-        ctxt->chr_access.data = (void *)&bletiny_appearance;
-        ctxt->chr_access.len = sizeof bletiny_appearance;
-        break;
-
-    case BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG:
-        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
-        ctxt->chr_access.data = (void *)&bletiny_privacy_flag;
-        ctxt->chr_access.len = sizeof bletiny_privacy_flag;
-        break;
-
-    case BLE_GAP_CHR_UUID16_RECONNECT_ADDR:
-        assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
-        if (ctxt->chr_access.len != sizeof bletiny_reconnect_addr) {
-            return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
-        }
-        memcpy(bletiny_reconnect_addr, ctxt->chr_access.data,
-               sizeof bletiny_reconnect_addr);
-        break;
-
-    case BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS:
-        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
-        ctxt->chr_access.data = (void *)&bletiny_pref_conn_params;
-        ctxt->chr_access.len = sizeof bletiny_pref_conn_params;
-        break;
-
-    default:
-        assert(0);
-        break;
-    }
-
-    return 0;
-}
-
-static int
-periph_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
-                       union ble_gatt_access_ctxt *ctxt, void *arg)
-{
-    uint16_t uuid16;
-
-    uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
-    assert(uuid16 != 0);
-
-    switch (uuid16) {
-    case BLE_GATT_CHR_SERVICE_CHANGED_UUID16:
-        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
-            if (ctxt->chr_access.len != sizeof bletiny_gatt_service_changed) {
-                return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
-            }
-            memcpy(bletiny_gatt_service_changed, ctxt->chr_access.data,
-                   sizeof bletiny_gatt_service_changed);
-        } else if (op == BLE_GATT_ACCESS_OP_READ_CHR) {
-            ctxt->chr_access.data = (void *)&bletiny_gatt_service_changed;
-            ctxt->chr_access.len = sizeof bletiny_gatt_service_changed;
-        }
-        break;
-
-    default:
-        assert(0);
-        break;
-    }
-
-    return 0;
-}
-
-#define PERIPH_NEW_ALERT_VAL_MAX_LEN    64
-
-static const uint8_t periph_new_alert_cat = 0x01; /* Simple alert. */
-static uint8_t periph_new_alert_val[PERIPH_NEW_ALERT_VAL_MAX_LEN];
-static uint16_t periph_new_alert_val_len;
-static const uint8_t periph_unr_alert_cat = 0x01; /* Simple alert. */
-static uint16_t periph_unr_alert_stat;
-static uint16_t periph_alert_not_ctrl_pt;
-
-static int
-periph_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
-                        union ble_gatt_access_ctxt *ctxt, void *arg)
-{
-    uint16_t uuid16;
-    int rc;
-
-    uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
-    assert(uuid16 != 0);
-
-    switch (uuid16) {
-    case PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID:
-        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
-        ctxt->chr_access.data = (void *)&periph_new_alert_cat;
-        ctxt->chr_access.len = sizeof periph_new_alert_cat;
-        return 0;
-
-    case PERIPH_CHR_NEW_ALERT:
-        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
-            rc = periph_chr_write(op, ctxt, 0, sizeof periph_new_alert_val,
-                                  periph_new_alert_val,
-                                  &periph_new_alert_val_len);
-            return rc;
-        } else if (op == BLE_GATT_ACCESS_OP_READ_CHR) {
-            ctxt->chr_access.data = (void *)&periph_new_alert_val;
-            ctxt->chr_access.len = sizeof periph_new_alert_val;
-            return 0;
-        }
-
-    case PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID:
-        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
-        ctxt->chr_access.data = (void *)&periph_unr_alert_cat;
-        ctxt->chr_access.len = sizeof periph_unr_alert_cat;
-        return 0;
-
-    case PERIPH_CHR_UNR_ALERT_STAT_UUID:
-        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
-            rc = periph_chr_write(op, ctxt, 2, 2, &periph_unr_alert_stat, NULL);
-        } else {
-            ctxt->chr_access.data = (void *)&periph_unr_alert_stat;
-            ctxt->chr_access.len = sizeof periph_unr_alert_stat;
-            rc = 0;
-        }
-        return rc;
-
-    case PERIPH_CHR_ALERT_NOT_CTRL_PT:
-        if (op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
-            rc = periph_chr_write(op, ctxt, 2, 2, &periph_alert_not_ctrl_pt,
-                                  NULL);
-        } else {
-            rc = BLE_ATT_ERR_UNLIKELY;
-        }
-        return rc;
-
-    default:
-        assert(0);
-        return BLE_ATT_ERR_UNLIKELY;
-    }
-}
-
-static void
-periph_register_cb(uint8_t op, union ble_gatt_register_ctxt *ctxt, void *arg)
-{
-    uint16_t uuid16;
-
-    switch (op) {
-    case BLE_GATT_REGISTER_OP_SVC:
-        uuid16 = ble_uuid_128_to_16(ctxt->svc_reg.svc->uuid128);
-        assert(uuid16 != 0);
-        BLETINY_LOG(DEBUG, "registered service 0x%04x with handle=%d\n",
-                    uuid16, ctxt->svc_reg.handle);
-        break;
-
-    case BLE_GATT_REGISTER_OP_CHR:
-        uuid16 = ble_uuid_128_to_16(ctxt->chr_reg.chr->uuid128);
-        assert(uuid16 != 0);
-        BLETINY_LOG(DEBUG, "registering characteristic 0x%04x with "
-                           "def_handle=%d val_handle=%d\n",
-                    uuid16, ctxt->chr_reg.def_handle,
-                    ctxt->chr_reg.val_handle);
-        break;
-
-    case BLE_GATT_REGISTER_OP_DSC:
-        uuid16 = ble_uuid_128_to_16(ctxt->dsc_reg.dsc->uuid128);
-        assert(uuid16 != 0);
-        BLETINY_LOG(DEBUG, "registering descriptor 0x%04x with handle=%d "
-                           "chr_handle=%d\n",
-                    uuid16, ctxt->dsc_reg.dsc_handle,
-                    ctxt->dsc_reg.chr_def_handle);
-        break;
-
-    default:
-        assert(0);
-        break;
-    }
-}
-
-void
-periph_init(void)
-{
-    int rc;
-
-    rc = ble_gatts_register_svcs(periph_svcs, periph_register_cb, NULL);
-    assert(rc == 0);
-}



[08/12] incubator-mynewt-core git commit: bletiny - Various changes to reduce code size.

Posted by cc...@apache.org.
bletiny - Various changes to reduce code size.

These changes are necessary to fit a security-enabled bletiny image on
an nRF51dk.


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

Branch: refs/heads/develop
Commit: a12f64dacbd9b3ba2fa9ffcb2b76957b8389ff77
Parents: c16e6c1
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 16:09:49 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 16:10:33 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/pkg.yml            |   5 ++
 apps/bletiny/src/bletiny_priv.h |   1 -
 apps/bletiny/src/cmd.c          |  53 ++--------------
 apps/bletiny/src/main.c         | 119 ++++++++++-------------------------
 4 files changed, 43 insertions(+), 135 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a12f64da/apps/bletiny/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml
index 84e0bfa..97e4332 100644
--- a/apps/bletiny/pkg.yml
+++ b/apps/bletiny/pkg.yml
@@ -29,3 +29,8 @@ pkg.deps:
     - net/nimble/host
     - libs/console/full
     - libs/shell
+
+pkg.cflags:
+    # Reduce the log level from DEBUG to INFO.  This is necessary to fit
+    # bletiny on the nRF51dk.
+    - "-DLOG_LEVEL=2"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a12f64da/apps/bletiny/src/bletiny_priv.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny_priv.h b/apps/bletiny/src/bletiny_priv.h
index 279f96b..5ecf2e5 100644
--- a/apps/bletiny/src/bletiny_priv.h
+++ b/apps/bletiny/src/bletiny_priv.h
@@ -164,7 +164,6 @@ int bletiny_update_conn(uint16_t conn_handle,
 void bletiny_chrup(uint16_t attr_handle);
 int bletiny_l2cap_update(uint16_t conn_handle,
                           struct ble_l2cap_sig_update_params *params);
-int bletiny_show_rssi(uint16_t conn_handle);
 int bletiny_sec_start(uint16_t conn_handle);
 int bletiny_sec_restart(uint16_t conn_handle, uint8_t *ltk, uint16_t ediv,
                         uint64_t rand_val, int auth);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a12f64da/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 5015157..88ea88b 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -96,7 +96,7 @@ cmd_print_chr(struct bletiny_chr *chr)
 }
 
 static void
-cmd_print_svc(struct bletiny_svc *svc, int print_chrs)
+cmd_print_svc(struct bletiny_svc *svc)
 {
     struct bletiny_chr *chr;
 
@@ -105,10 +105,8 @@ cmd_print_svc(struct bletiny_svc *svc, int print_chrs)
     print_uuid(svc->svc.uuid128);
     console_printf("\n");
 
-    if (print_chrs) {
-        SLIST_FOREACH(chr, &svc->chrs, next) {
-            cmd_print_chr(chr);
-        }
+    SLIST_FOREACH(chr, &svc->chrs, next) {
+        cmd_print_chr(chr);
     }
 }
 
@@ -870,7 +868,7 @@ cmd_show_chr(int argc, char **argv)
         console_printf("\n");
 
         SLIST_FOREACH(svc, &conn->svcs, next) {
-            cmd_print_svc(svc, 1);
+            cmd_print_svc(svc);
         }
     }
 
@@ -894,53 +892,10 @@ cmd_show_conn(int argc, char **argv)
     return 0;
 }
 
-static int
-cmd_show_rssi(int argc, char **argv)
-{
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = parse_arg_uint16("conn", &rc);
-    if (rc != 0) {
-        return rc;
-    }
-
-    rc = bletiny_show_rssi(conn_handle);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-static int
-cmd_show_svc(int argc, char **argv)
-{
-    struct bletiny_conn *conn;
-    struct bletiny_svc *svc;
-    int i;
-
-    for (i = 0; i < bletiny_num_conns; i++) {
-        conn = bletiny_conns + i;
-
-        console_printf("CONNECTION: handle=%d addr=", conn->handle);
-        print_addr(conn->addr);
-        console_printf("\n");
-
-        SLIST_FOREACH(svc, &conn->svcs, next) {
-            cmd_print_svc(svc, 0);
-        }
-    }
-
-    return 0;
-}
-
 static struct cmd_entry cmd_show_entries[] = {
     { "addr", cmd_show_addr },
     { "chr", cmd_show_chr },
     { "conn", cmd_show_conn },
-    { "rssi", cmd_show_rssi },
-    { "svc", cmd_show_svc },
     { NULL, NULL }
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a12f64da/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index d3d27f8..42c93c9 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -103,12 +103,6 @@ bssnz_t os_stack_t bletiny_stack[BLETINY_STACK_SIZE];
 static struct log_handler bletiny_log_console_handler;
 struct log bletiny_log;
 
-struct bletiny_conn ble_shell_conns[NIMBLE_OPT(MAX_CONNECTIONS)];
-int bletiny_num_conns;
-
-void
-bletest_inc_adv_pkt_num(void) { }
-
 bssnz_t struct bletiny_conn bletiny_conns[NIMBLE_OPT(MAX_CONNECTIONS)];
 int bletiny_num_conns;
 
@@ -141,6 +135,10 @@ static void
 bletiny_print_error(char *msg, uint16_t conn_handle,
                     struct ble_gatt_error *error)
 {
+    if (msg == NULL) {
+        msg = "ERROR";
+    }
+
     console_printf("%s: conn_handle=%d status=%d att_handle=%d\n",
                    msg, conn_handle, error->status, error->att_handle);
 }
@@ -180,9 +178,9 @@ static void
 bletiny_print_key_exchange_parms(struct ble_gap_key_parms *key_params)
 {
     if (key_params->is_ours) {
-        console_printf("Our Keys Sent to Peer\n");
+        console_printf("keys; us --> peer\n");
     } else {
-        console_printf("Keys Received from Peer\n");
+        console_printf("keys; peer --> us\n");
     }
 
     if (key_params->ltk_valid) {
@@ -488,8 +486,9 @@ bletiny_svc_add(uint16_t conn_handle, struct ble_gatt_service *gatt_svc)
 
     conn = bletiny_conn_find(conn_handle);
     if (conn == NULL) {
-        console_printf("RECEIVED SERVICE FOR UNKNOWN CONNECTION; HANDLE=%d\n",
-                       conn_handle);
+        BLETINY_LOG(DEBUG, "RECEIVED SERVICE FOR UNKNOWN CONNECTION; "
+                           "HANDLE=%d\n",
+                    conn_handle);
         return NULL;
     }
 
@@ -501,7 +500,7 @@ bletiny_svc_add(uint16_t conn_handle, struct ble_gatt_service *gatt_svc)
 
     svc = os_memblock_get(&bletiny_svc_pool);
     if (svc == NULL) {
-        console_printf("OOM WHILE DISCOVERING SERVICE\n");
+        BLETINY_LOG(DEBUG, "OOM WHILE DISCOVERING SERVICE\n");
         return NULL;
     }
     memset(svc, 0, sizeof *svc);
@@ -572,15 +571,16 @@ bletiny_chr_add(uint16_t conn_handle,  uint16_t svc_start_handle,
 
     conn = bletiny_conn_find(conn_handle);
     if (conn == NULL) {
-        console_printf("RECEIVED SERVICE FOR UNKNOWN CONNECTION; HANDLE=%d\n",
-                       conn_handle);
+        BLETINY_LOG(DEBUG, "RECEIVED SERVICE FOR UNKNOWN CONNECTION; "
+                           "HANDLE=%d\n",
+                    conn_handle);
         return NULL;
     }
 
     svc = bletiny_svc_find(conn, svc_start_handle, NULL);
     if (svc == NULL) {
-        console_printf("CAN'T FIND SERVICE FOR DISCOVERED CHR; HANDLE=%d\n",
-                       conn_handle);
+        BLETINY_LOG(DEBUG, "CAN'T FIND SERVICE FOR DISCOVERED CHR; HANDLE=%d\n",
+                    conn_handle);
         return NULL;
     }
 
@@ -592,7 +592,7 @@ bletiny_chr_add(uint16_t conn_handle,  uint16_t svc_start_handle,
 
     chr = os_memblock_get(&bletiny_chr_pool);
     if (chr == NULL) {
-        console_printf("OOM WHILE DISCOVERING CHARACTERISTIC\n");
+        BLETINY_LOG(DEBUG, "OOM WHILE DISCOVERING CHARACTERISTIC\n");
         return NULL;
     }
     memset(chr, 0, sizeof *chr);
@@ -662,22 +662,24 @@ bletiny_dsc_add(uint16_t conn_handle, uint16_t chr_def_handle,
 
     conn = bletiny_conn_find(conn_handle);
     if (conn == NULL) {
-        console_printf("RECEIVED SERVICE FOR UNKNOWN CONNECTION; HANDLE=%d\n",
-                       conn_handle);
+        BLETINY_LOG(DEBUG, "RECEIVED SERVICE FOR UNKNOWN CONNECTION; "
+                           "HANDLE=%d\n",
+                    conn_handle);
         return NULL;
     }
 
     svc = bletiny_svc_find_range(conn, chr_def_handle);
     if (svc == NULL) {
-        console_printf("CAN'T FIND SERVICE FOR DISCOVERED DSC; HANDLE=%d\n",
-                       conn_handle);
+        BLETINY_LOG(DEBUG, "CAN'T FIND SERVICE FOR DISCOVERED DSC; HANDLE=%d\n",
+                    conn_handle);
         return NULL;
     }
 
     chr = bletiny_chr_find(svc, chr_def_handle, NULL);
     if (chr == NULL) {
-        console_printf("CAN'T FIND CHARACTERISTIC FOR DISCOVERED DSC; "
-                       "HANDLE=%d\n", conn_handle);
+        BLETINY_LOG(DEBUG, "CAN'T FIND CHARACTERISTIC FOR DISCOVERED DSC; "
+                           "HANDLE=%d\n",
+                    conn_handle);
         return NULL;
     }
 
@@ -705,37 +707,12 @@ bletiny_dsc_add(uint16_t conn_handle, uint16_t chr_def_handle,
     return dsc;
 }
 
-static void
-bletiny_start_auto_advertise(void)
-{
-    struct ble_hs_adv_fields fields;
-    int rc;
-
-    if (BLETINY_AUTO_DEVICE_NAME[0] != '\0') {
-        memset(&fields, 0, sizeof fields);
-
-        fields.name = (uint8_t *)BLETINY_AUTO_DEVICE_NAME;
-        fields.name_len = strlen(BLETINY_AUTO_DEVICE_NAME);
-        fields.name_is_complete = 1;
-        rc = bletiny_set_adv_data(&fields);
-        if (rc != 0) {
-            return;
-        }
-
-        rc = bletiny_adv_start(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_UND,
-                               NULL, 0, NULL);
-        if (rc != 0) {
-            return;
-        }
-    }
-}
-
 static int
 bletiny_on_mtu(uint16_t conn_handle, struct ble_gatt_error *error,
-                uint16_t mtu, void *arg)
+               uint16_t mtu, void *arg)
 {
     if (error != NULL) {
-        bletiny_print_error("ERROR EXCHANGING MTU", conn_handle, error);
+        bletiny_print_error(NULL, conn_handle, error);
     } else {
         console_printf("mtu exchange complete: conn_handle=%d mtu=%d\n",
                        conn_handle, mtu);
@@ -749,7 +726,7 @@ bletiny_on_disc_s(uint16_t conn_handle, struct ble_gatt_error *error,
                    struct ble_gatt_service *service, void *arg)
 {
     if (error != NULL) {
-        bletiny_print_error("ERROR DISCOVERING SERVICE", conn_handle, error);
+        bletiny_print_error(NULL, conn_handle, error);
     } else if (service != NULL) {
         bletiny_svc_add(conn_handle, service);
     } else {
@@ -768,8 +745,7 @@ bletiny_on_disc_c(uint16_t conn_handle, struct ble_gatt_error *error,
     svc_start_handle = (intptr_t)arg;
 
     if (error != NULL) {
-        bletiny_print_error("ERROR DISCOVERING CHARACTERISTIC", conn_handle,
-                             error);
+        bletiny_print_error(NULL, conn_handle, error);
     } else if (chr != NULL) {
         bletiny_chr_add(conn_handle, svc_start_handle, chr);
     } else {
@@ -785,8 +761,7 @@ bletiny_on_disc_d(uint16_t conn_handle, struct ble_gatt_error *error,
                    void *arg)
 {
     if (error != NULL) {
-        bletiny_print_error("ERROR DISCOVERING DESCRIPTOR", conn_handle,
-                             error);
+        bletiny_print_error(NULL, conn_handle, error);
     } else if (dsc != NULL) {
         bletiny_dsc_add(conn_handle, chr_def_handle, dsc);
     } else {
@@ -801,8 +776,7 @@ bletiny_on_read(uint16_t conn_handle, struct ble_gatt_error *error,
                  struct ble_gatt_attr *attr, void *arg)
 {
     if (error != NULL) {
-        bletiny_print_error("ERROR READING CHARACTERISTIC", conn_handle,
-                             error);
+        bletiny_print_error(NULL, conn_handle, error);
     } else if (attr != NULL) {
         console_printf("characteristic read; conn_handle=%d "
                        "attr_handle=%d len=%d value=", conn_handle,
@@ -821,8 +795,7 @@ bletiny_on_write(uint16_t conn_handle, struct ble_gatt_error *error,
                   struct ble_gatt_attr *attr, void *arg)
 {
     if (error != NULL) {
-        bletiny_print_error("ERROR WRITING CHARACTERISTIC", conn_handle,
-                             error);
+        bletiny_print_error(NULL, conn_handle, error);
     } else {
         console_printf("characteristic write complete; conn_handle=%d "
                        "attr_handle=%d len=%d value=", conn_handle,
@@ -842,8 +815,7 @@ bletiny_on_write_reliable(uint16_t conn_handle, struct ble_gatt_error *error,
     int i;
 
     if (error != NULL) {
-        bletiny_print_error("ERROR WRITING CHARACTERISTICS RELIABLY",
-                             conn_handle, error);
+        bletiny_print_error(NULL, conn_handle, error);
     } else {
         console_printf("characteristic write reliable complete; "
                        "conn_handle=%d", conn_handle);
@@ -874,7 +846,7 @@ bletiny_on_notify(uint16_t conn_handle, uint16_t attr_handle,
 
 static int
 bletiny_gap_event(int event, int status, struct ble_gap_conn_ctxt *ctxt,
-                   void *arg)
+                  void *arg)
 {
     int authenticated;
     int conn_idx;
@@ -896,13 +868,9 @@ bletiny_gap_event(int event, int status, struct ble_gap_conn_ctxt *ctxt,
                 }
             } else {
                 conn_idx = bletiny_conn_find_idx(ctxt->desc->conn_handle);
-                if (conn_idx == -1) {
-                    console_printf("UNKNOWN CONNECTION\n");
-                } else {
+                if (conn_idx != -1) {
                     bletiny_conn_delete_idx(conn_idx);
                 }
-
-                bletiny_start_auto_advertise();
             }
         }
         return 0;
@@ -1290,23 +1258,6 @@ bletiny_l2cap_update(uint16_t conn_handle,
 }
 
 int
-bletiny_show_rssi(uint16_t conn_handle)
-{
-    int8_t rssi;
-    int rc;
-
-    rc = ble_hci_util_read_rssi(conn_handle, &rssi);
-    if (rc != 0) {
-        console_printf("failure to read rssi; rc=%d\n", rc);
-        return rc;
-    }
-
-    console_printf("rssi=%d\n", rssi);
-
-    return 0;
-}
-
-int
 bletiny_sec_start(uint16_t conn_handle)
 {
 #if !NIMBLE_OPT(SM)
@@ -1353,8 +1304,6 @@ bletiny_task_handler(void *arg)
 
     ble_att_set_notify_cb(bletiny_on_notify, NULL);
 
-    bletiny_start_auto_advertise();
-
     while (1) {
         ev = os_eventq_get(&bletiny_evq);
         switch (ev->ev_type) {


[09/12] incubator-mynewt-core git commit: BLE Host - Fix typo in log message.

Posted by cc...@apache.org.
BLE Host - Fix typo in log message.


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

Branch: refs/heads/develop
Commit: 94ae8bd0de56525d9f2357de7a9b287de73edcc9
Parents: a12f64d
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 16:10:18 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 16:10:46 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_gap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/94ae8bd0/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index a51442c..5e5053b 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -248,7 +248,7 @@ ble_gap_log_adv(struct hci_adv_params *adv_params)
                ble_gap_slave.disc_mode, adv_params->peer_addr_type);
     BLE_HS_LOG_ADDR(INFO, adv_params->peer_addr);
     BLE_HS_LOG(INFO, " adv_type=%d adv_channel_map=%d own_addr_type=%d "
-                     "adv_filter_policy=%d adv_itvl_min=%d  adv_itvl_max=%d "
+                     "adv_filter_policy=%d adv_itvl_min=%d adv_itvl_max=%d "
                      "adv_data_len=%d",
                adv_params->adv_type,
                adv_params->adv_channel_map,


[04/12] incubator-mynewt-core git commit: BLE Host - Enable security manager by default.

Posted by cc...@apache.org.
BLE Host - Enable security manager by default.


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

Branch: refs/heads/develop
Commit: 04ace8e6c36bbc0863f785032e7abb65745becd6
Parents: b543995
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 10:15:40 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 10:15:40 2016 -0700

----------------------------------------------------------------------
 net/nimble/include/nimble/nimble_opt.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/04ace8e6/net/nimble/include/nimble/nimble_opt.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/nimble_opt.h b/net/nimble/include/nimble/nimble_opt.h
index 024f289..604db86 100644
--- a/net/nimble/include/nimble/nimble_opt.h
+++ b/net/nimble/include/nimble/nimble_opt.h
@@ -52,10 +52,10 @@
 #define NIMBLE_OPT_WHITELIST                    1
 #endif
 
-/** HOST / CONTROLLER: Security manager.  Disabled by default. */
+/** HOST / CONTROLLER: Security manager.  Enabled by default. */
 
 #ifndef NIMBLE_OPT_SM
-#define NIMBLE_OPT_SM                           0
+#define NIMBLE_OPT_SM                           1
 #endif
 
 


[10/12] incubator-mynewt-core git commit: bletiny - Increase mbuf count to support iPhone.

Posted by cc...@apache.org.
bletiny - Increase mbuf count to support iPhone.

The iPhone appears to send a bunch of packets in rapid succession
immediately after connecting.  The nimble host doesn't have a good way
to alleviate this problem at the moment, so the temporary fix is to
increase the number of mbufs.


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

Branch: refs/heads/develop
Commit: c90e85ce099a598835d2fde1ff5fcb392eef97aa
Parents: 94ae8bd
Author: Christopher Collins <cc...@apache.org>
Authored: Wed May 18 18:21:55 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed May 18 18:21:55 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/src/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c90e85ce/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 42c93c9..2545e24 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -72,7 +72,7 @@ uint8_t g_host_adv_len;
 static uint8_t bletiny_addr[6] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a};
 
 /* Create a mbuf pool of BLE mbufs */
-#define MBUF_NUM_MBUFS      (7)
+#define MBUF_NUM_MBUFS      (12)
 #define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
 #define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
 #define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
@@ -264,7 +264,7 @@ bletiny_print_adv_fields(struct ble_hs_adv_fields *fields)
     if (fields->name != NULL) {
         console_printf("    name(%scomplete)=",
                        fields->name_is_complete ? "" : "in");
-        console_printf("%*s\n", fields->name_len, fields->name);
+        console_write((char *)fields->name, fields->name_len);
     }
 
     if (fields->tx_pwr_lvl_is_present) {