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 2015/12/14 23:33:52 UTC

[3/4] incubator-mynewt-larva git commit: Move ble_gap.c to ble_gap_conn.c.

Move ble_gap.c to ble_gap_conn.c.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/eea8a48c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/eea8a48c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/eea8a48c

Branch: refs/heads/master
Commit: eea8a48c597ac6cc46475ffe94a8652eb5942a71
Parents: 6d66ff8
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Dec 14 12:12:33 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Dec 14 14:33:33 2015 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h      |   8 +-
 net/nimble/host/src/ble_gap.c               | 115 -----
 net/nimble/host/src/ble_gap_conn.c          | 557 +++++++++++++----------
 net/nimble/host/src/test/ble_gap_test.c     |   4 +-
 net/nimble/host/src/test/ble_hs_conn_test.c |   6 +-
 net/nimble/host/src/test/ble_hs_test_util.c |   2 +-
 project/hostctlrtest/src/main.c             |  14 +-
 7 files changed, 327 insertions(+), 379 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/eea8a48c/net/nimble/host/include/host/ble_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gap.h b/net/nimble/host/include/host/ble_gap.h
index 144ba87..88ad01e 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -28,9 +28,9 @@ struct ble_gap_connect_desc {
 
 typedef void ble_gap_connect_fn(struct ble_gap_connect_desc *desc, void *arg);
 
-void ble_gap_set_connect_cb(ble_gap_connect_fn *cb, void *arg);
-int ble_gap_general_discovery(void);
-int ble_gap_direct_connection_establishment(uint8_t addr_type, uint8_t *addr);
-int ble_gap_directed_connectable(uint8_t addr_type, uint8_t *addr);
+void ble_gap_conn_set_cb(ble_gap_connect_fn *cb, void *arg);
+int ble_gap_conn_gen_disc(void);
+int ble_gap_conn_direct_connectable(int addr_type, uint8_t *addr);
+int ble_gap_conn_direct_connect(int addr_type, uint8_t *addr);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/eea8a48c/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
deleted file mode 100644
index c19906f..0000000
--- a/net/nimble/host/src/ble_gap.c
+++ /dev/null
@@ -1,115 +0,0 @@
-/**
- * Copyright (c) 2015 Runtime Inc.
- *
- * Licensed 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 <stddef.h>
-#include <errno.h>
-#include <string.h>
-#include "ble_hs_priv.h"
-#include "host/ble_gap.h"
-#include "ble_gap_conn.h"
-
-/**
- * Configures the connection event callback.  The callback is executed whenever
- * any of the following events occurs:
- *     o Connection creation succeeds.
- *     o Connection creation fails.
- *     o Connection establishment fails.
- *     o Established connection broken.
- */
-void
-ble_gap_set_connect_cb(ble_gap_connect_fn *cb, void *arg)
-{
-    ble_gap_conn_cb = cb;
-    ble_gap_conn_arg = arg;
-}
-
-/**
- * Performs the General Discovery Procedure, as described in
- * vol. 3, part C, section 9.2.6.
- *
- * @return                      0 on success; nonzero on failure.
- */
-int
-ble_gap_general_discovery(void)
-{
-#if 0
-    struct ble_hs_hci_batch_entry *entry;
-
-    entry = ble_hs_hci_batch_entry_alloc();
-    if (entry == NULL) {
-        return BLE_HS_ENOMEM;
-    }
-
-    entry->bhb_type = BLE_HS_HCI_BATCH_TYPE_GENERAL_DISCOVERY;
-
-    ble_hs_hci_batch_enqueue(entry);
-
-#endif
-    return 0;
-}
-
-
-/**
- * Performs the Direct Connection Establishment Procedure, as described in
- * vol. 3, part C, section 9.3.8.
- *
- * @param addr_type             The peer's address type; one of:
- *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC
- *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM
- *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC_IDENT
- *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM_IDENT
- * @param addr                  The address of the peer to connect to.
- *
- * @return                      0 on success; nonzero on failure.
- */
-int
-ble_gap_direct_connection_establishment(uint8_t addr_type, uint8_t *addr)
-{
-    int rc;
-
-    rc = ble_gap_conn_direct_connect(addr_type, addr);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-/**
- * Enables Directed Connectable Mode, as described in vol. 3, part C, section
- * 9.3.3.
- *
- * @param addr_type             The peer's address type; one of:
- *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC
- *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM
- *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC_IDENT
- *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM_IDENT
- * @param addr                  The address of the peer to connect to.
- *
- * @return                      0 on success; nonzero on failure.
- */
-int
-ble_gap_directed_connectable(uint8_t addr_type, uint8_t *addr)
-{
-    int rc;
-
-    rc = ble_gap_conn_direct_advertise(addr_type, addr);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/eea8a48c/net/nimble/host/src/ble_gap_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap_conn.c b/net/nimble/host/src/ble_gap_conn.c
index 1b158a4..ed1f094 100644
--- a/net/nimble/host/src/ble_gap_conn.c
+++ b/net/nimble/host/src/ble_gap_conn.c
@@ -28,19 +28,21 @@
 
 #define BLE_GAP_CONN_STATE_IDLE                             0
 
-#define BLE_GAP_CONN_STATE_MASTER_GEN_DISC_UNACKED          1
-#define BLE_GAP_CONN_STATE_MASTER_GEN_DISC_PARAMS_ACKED     2
-#define BLE_GAP_CONN_STATE_MASTER_GEN_DISC_ENABLE_ACKED     3
-
-#define BLE_GAP_CONN_STATE_MASTER_DIRECT_PENDING            4
-#define BLE_GAP_CONN_STATE_MASTER_DIRECT_UNACKED            5
-#define BLE_GAP_CONN_STATE_MASTER_DIRECT_ACKED              6
-
-#define BLE_GAP_CONN_STATE_SLAVE_PENDING                    1
-#define BLE_GAP_CONN_STATE_SLAVE_PARAMS                     2
-#define BLE_GAP_CONN_STATE_SLAVE_PARAMS_ACKED               3
-#define BLE_GAP_CONN_STATE_SLAVE_ENABLE                     4
-#define BLE_GAP_CONN_STATE_SLAVE_ENABLE_ACKED               5
+#define BLE_GAP_CONN_STATE_M_GEN_DISC_PENDING          1
+#define BLE_GAP_CONN_STATE_M_GEN_DISC_PARAMS           2
+#define BLE_GAP_CONN_STATE_M_GEN_DISC_PARAMS_ACKED     3
+#define BLE_GAP_CONN_STATE_M_GEN_DISC_ENABLE           4
+#define BLE_GAP_CONN_STATE_M_GEN_DISC_ENABLE_ACKED     5
+
+#define BLE_GAP_CONN_STATE_M_DIRECT_PENDING            6
+#define BLE_GAP_CONN_STATE_M_DIRECT_UNACKED            7
+#define BLE_GAP_CONN_STATE_M_DIRECT_ACKED              8
+
+#define BLE_GAP_CONN_STATE_S_PENDING                    1
+#define BLE_GAP_CONN_STATE_S_PARAMS                     2
+#define BLE_GAP_CONN_STATE_S_PARAMS_ACKED               3
+#define BLE_GAP_CONN_STATE_S_ENABLE                     4
+#define BLE_GAP_CONN_STATE_S_ENABLE_ACKED               5
 
 /** 30 ms. */
 #define BLE_GAP_ADV_FAST_INTERVAL1_MIN      (30 * 1000 / BLE_HCI_ADV_ITVL)
@@ -54,8 +56,6 @@
 /** 11.25 ms. */
 #define BLE_GAP_SCAN_SLOW_WINDOW1           (11.25 * 1000 / BLE_HCI_SCAN_ITVL)
 
-static void ble_gap_conn_slave_ack(struct ble_hci_ack *ack, void *arg);
-
 ble_gap_connect_fn *ble_gap_conn_cb;
 void *ble_gap_conn_arg;
 
@@ -64,6 +64,25 @@ static int ble_gap_conn_slave_state;
 static uint8_t ble_gap_conn_master_addr[BLE_DEV_ADDR_LEN];
 static uint8_t ble_gap_conn_slave_addr[BLE_DEV_ADDR_LEN];
 
+/*****************************************************************************
+ * @misc                                                                     *
+ *****************************************************************************/
+
+/**
+ * Configures the connection event callback.  The callback is executed whenever
+ * any of the following events occurs:
+ *     o Connection creation succeeds.
+ *     o Connection creation fails.
+ *     o Connection establishment fails.
+ *     o Established connection broken.
+ */
+void
+ble_gap_conn_set_cb(ble_gap_connect_fn *cb, void *arg)
+{
+    ble_gap_conn_cb = cb;
+    ble_gap_conn_arg = arg;
+}
+
 /**
  * Calls the connect callback if one is configured.
  *
@@ -116,182 +135,191 @@ ble_gap_conn_slave_failed(uint8_t status)
     ble_gap_conn_notify_app(status, NULL);
 }
 
-/**
- * Processes an HCI acknowledgement (either command status or command complete)
- * while a master connection is being established.
- */
-static void
-ble_gap_conn_master_ack(struct ble_hci_ack *ack, void *arg)
+int
+ble_gap_conn_rx_disconn_complete(struct hci_disconn_complete *evt)
 {
-    int rc;
+    struct ble_hs_conn *conn;
 
-    if (ack->bha_status != 0) {
-        ble_gap_conn_master_failed(ack->bha_status);
-        return;
+    conn = ble_hs_conn_find(evt->connection_handle);
+    if (conn == NULL) {
+        return BLE_HS_ENOENT;
     }
 
-    switch (ble_gap_conn_master_state) {
-    /*** General discovery. */
-    case BLE_GAP_CONN_STATE_MASTER_GEN_DISC_UNACKED:
-        ble_gap_conn_master_state =
-            BLE_GAP_CONN_STATE_MASTER_GEN_DISC_PARAMS_ACKED;
-        ble_hci_ack_set_callback(ble_gap_conn_master_ack, NULL);
-        rc = host_hci_cmd_le_set_scan_enable(1, 0);
-        if (rc != BLE_ERR_SUCCESS) {
-            ble_gap_conn_master_failed(rc);
-        }
-        break;
+    if (evt->status == 0) {
+        ble_hs_conn_remove(conn);
+        ble_gap_conn_notify_app(evt->reason, conn);
+        ble_hs_conn_free(conn);
+    } else {
+        /* XXX: Ensure we have a disconnect operation in progress. */
+        ble_gap_conn_notify_app(evt->status, conn);
+    }
 
-    case BLE_GAP_CONN_STATE_MASTER_GEN_DISC_PARAMS_ACKED:
-        ble_gap_conn_master_state =
-            BLE_GAP_CONN_STATE_MASTER_GEN_DISC_ENABLE_ACKED;
-        break;
+    return 0;
+}
 
-    /*** Direct connect. */
-    case BLE_GAP_CONN_STATE_MASTER_DIRECT_UNACKED:
-        ble_gap_conn_master_state = BLE_GAP_CONN_STATE_MASTER_DIRECT_ACKED;
-        break;
+/**
+ * Tells you if the BLE host is in the process of creating a master connection.
+ */
+int
+ble_gap_conn_master_in_progress(void)
+{
+    return ble_gap_conn_master_state != BLE_GAP_CONN_STATE_IDLE;
+}
 
-    default:
-        assert(0);
-        break;
-    }
+/**
+ * Tells you if the BLE host is in the process of creating a slave connection.
+ */
+int
+ble_gap_conn_slave_in_progress(void)
+{
+    return ble_gap_conn_slave_state != BLE_GAP_CONN_STATE_IDLE;
 }
 
 static int
-ble_gap_conn_direct_advertise_tx_enable(void *arg)
+ble_gap_conn_accept_new_conn(uint8_t *addr)
 {
-    int rc;
-
-    rc = host_hci_cmd_le_set_adv_enable(1);
-    if (rc != BLE_ERR_SUCCESS) {
-        goto err;
+    switch (ble_gap_conn_master_state) {
+    case BLE_GAP_CONN_STATE_M_DIRECT_ACKED:
+        if (memcmp(ble_gap_conn_master_addr, addr, BLE_DEV_ADDR_LEN) == 0) {
+            ble_gap_conn_master_state = BLE_GAP_CONN_STATE_IDLE;
+            return 0;
+        }
     }
 
-    ble_hci_ack_set_callback(ble_gap_conn_slave_ack, NULL);
-    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_ENABLE;
-
-    return 0;
+    switch (ble_gap_conn_slave_state) {
+    case BLE_GAP_CONN_STATE_S_ENABLE_ACKED:
+        if (memcmp(ble_gap_conn_slave_addr, addr, BLE_DEV_ADDR_LEN) == 0) {
+            ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_IDLE;
+            return 0;
+        }
+        break;
+    }
 
-err:
-    ble_gap_conn_slave_failed(rc);
-    return 1;
+    return BLE_HS_ENOENT;
 }
 
 /**
- * Processes an HCI acknowledgement (either command status or command complete)
- * while a slave connection is being established.
+ * Processes an incoming connection-complete HCI event.
  */
-static void
-ble_gap_conn_slave_ack(struct ble_hci_ack *ack, void *arg)
+int
+ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt)
 {
+    struct ble_hs_conn *conn;
     int rc;
 
-    if (ack->bha_status != BLE_ERR_SUCCESS) {
-        rc = ack->bha_status;
-        goto err;
-    }
+    /* Determine if this event refers to a completed connection or a connection
+     * in progress.
+     */
+    conn = ble_hs_conn_find(evt->connection_handle);
 
-    switch (ble_gap_conn_slave_state) {
-    case BLE_GAP_CONN_STATE_SLAVE_PARAMS:
-        ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_PARAMS_ACKED;
-        rc = ble_hci_sched_enqueue(ble_gap_conn_direct_advertise_tx_enable,
-                                   NULL);
-        if (rc != 0) {
-            goto err;
+    /* Apply the event to the existing connection if it exists. */
+    if (conn != NULL) {
+        if (evt->status != 0) {
+            ble_hs_conn_remove(conn);
+            ble_gap_conn_notify_app(evt->status, conn);
+            ble_hs_conn_free(conn);
         }
-        break;
 
-    case BLE_GAP_CONN_STATE_SLAVE_ENABLE:
-        ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_ENABLE_ACKED;
-        break;
+        return 0;
+    }
 
-    default:
-        assert(0);
-        break;
+    /* This event refers to a new connection. */
+    rc = ble_gap_conn_accept_new_conn(evt->peer_addr);
+    if (rc != 0) {
+        return BLE_HS_ENOENT;
+    }
+
+    if (evt->status != BLE_ERR_SUCCESS) {
+        return 0;
+    }
+
+    /* XXX: Ensure device address is expected. */
+    /* XXX: Ensure event fields are acceptable. */
+
+    conn = ble_hs_conn_alloc();
+    if (conn == NULL) {
+        /* XXX: Ensure this never happens. */
+        ble_gap_conn_notify_app(BLE_ERR_MEM_CAPACITY, NULL);
+        return BLE_HS_ENOMEM;
     }
 
-    return;
+    conn->bhc_handle = evt->connection_handle;
+    memcpy(conn->bhc_addr, evt->peer_addr, sizeof conn->bhc_addr);
+
+    ble_hs_conn_insert(conn);
+
+    ble_gap_conn_notify_app(0, conn);
 
-err:
-    ble_gap_conn_slave_failed(rc);
+    return 0;
 }
 
-#if 0
-int
-ble_gap_conn_general_discovery(void)
+/*****************************************************************************
+ * @general discovery procedure                                              *
+ *****************************************************************************/
+
+static void
+ble_gap_conn_gen_disc_ack_enable(struct ble_hci_ack *ack, void *arg)
 {
-    int rc;
+    assert(ble_gap_conn_master_state == BLE_GAP_CONN_STATE_M_GEN_DISC_ENABLE);
 
-    /* Make sure no master connection attempt is already in progress. */
-    if (ble_gap_conn_master_in_progress()) {
-        rc = BLE_HS_EALREADY;
-        goto err;
+    if (ack->bha_status != 0) {
+        ble_gap_conn_master_failed(ack->bha_status);
+    } else {
+        ble_gap_conn_master_state = BLE_GAP_CONN_STATE_M_GEN_DISC_ENABLE_ACKED;
     }
+}
 
-    rc = host_hci_cmd_le_set_scan_params(BLE_HCI_SCAN_TYPE_ACTIVE,
-                                         BLE_GAP_SCAN_SLOW_INTERVAL1,
-                                         BLE_GAP_SCAN_SLOW_WINDOW1,
-                                         BLE_HCI_ADV_OWN_ADDR_PUBLIC,
-                                         BLE_HCI_SCAN_FILT_NO_WL);
-    if (rc != 0) {
-        rc = BLE_HS_EINVAL;
-        goto err;
-    }
+static void
+ble_gap_conn_gen_disc_ack_params(struct ble_hci_ack *ack, void *arg)
+{
+    int rc;
 
-    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_MASTER_DIRECT_UNACKED;
-    ble_hci_ack_set_callback(ble_gap_conn_master_ack, NULL);
+    assert(ble_gap_conn_master_state == BLE_GAP_CONN_STATE_M_GEN_DISC_PARAMS);
 
-    return 0;
+    if (ack->bha_status != 0) {
+        ble_gap_conn_master_failed(ack->bha_status);
+        return;
+    }
+
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_M_GEN_DISC_PARAMS_ACKED;
+    ble_hci_ack_set_callback(ble_gap_conn_gen_disc_ack_enable, NULL);
 
-err:
-    ble_gap_conn_notify_app(rc, NULL);
-    return rc;
+    rc = host_hci_cmd_le_set_scan_enable(1, 0);
+    if (rc != BLE_ERR_SUCCESS) {
+        ble_gap_conn_master_failed(rc);
+    }
 }
-#endif
+
 
 static int
-ble_gap_conn_direct_connect_tx(void *arg)
+ble_gap_conn_gen_disc_tx_params(void *arg)
 {
-    struct hci_create_conn hcc;
     int rc;
 
-    hcc.scan_itvl = 0x0010;
-    hcc.scan_window = 0x0010;
-    hcc.filter_policy = BLE_HCI_CONN_FILT_NO_WL;
-    hcc.peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC;
-    memcpy(hcc.peer_addr, ble_gap_conn_master_addr, sizeof hcc.peer_addr);
-    hcc.own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC;
-    hcc.conn_itvl_min = 24;
-    hcc.conn_itvl_max = 40;
-    hcc.conn_latency = 0;
-    hcc.supervision_timeout = 0x0100; // XXX
-    hcc.min_ce_len = 0x0010; // XXX
-    hcc.max_ce_len = 0x0300; // XXX
-
-    rc = host_hci_cmd_le_create_connection(&hcc);
+    rc = host_hci_cmd_le_set_scan_params(BLE_HCI_SCAN_TYPE_ACTIVE,
+                                         BLE_GAP_SCAN_SLOW_INTERVAL1,
+                                         BLE_GAP_SCAN_SLOW_WINDOW1,
+                                         BLE_HCI_ADV_OWN_ADDR_PUBLIC,
+                                         BLE_HCI_SCAN_FILT_NO_WL);
     if (rc != 0) {
-        goto err;
+        ble_gap_conn_master_failed(rc);
+        return rc;
     }
 
-    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_MASTER_DIRECT_UNACKED;
-    ble_hci_ack_set_callback(ble_gap_conn_master_ack, NULL);
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_M_GEN_DISC_PARAMS;
+    ble_hci_ack_set_callback(ble_gap_conn_gen_disc_ack_params, NULL);
 
     return 0;
-
-err:
-    ble_gap_conn_master_failed(rc);
-    return 1;
 }
 
 /**
- * Initiates a connection using the GAP Direct Connection Establishment
- * Procedure.
+ * Performs the General Discovery Procedure, as described in
+ * vol. 3, part C, section 9.2.6.
  *
- * @return 0 on success; nonzero on failure.
+ * @return                      0 on success; nonzero on failure.
  */
 int
-ble_gap_conn_direct_connect(int addr_type, uint8_t *addr)
+ble_gap_conn_gen_disc(void)
 {
     int rc;
 
@@ -300,10 +328,10 @@ ble_gap_conn_direct_connect(int addr_type, uint8_t *addr)
         return BLE_HS_EALREADY;
     }
 
-    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_MASTER_DIRECT_PENDING;
-    memcpy(ble_gap_conn_master_addr, addr, BLE_DEV_ADDR_LEN);
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_M_GEN_DISC_PENDING;
+    memset(ble_gap_conn_master_addr, 0, BLE_DEV_ADDR_LEN);
 
-    rc = ble_hci_sched_enqueue(ble_gap_conn_direct_connect_tx, NULL);
+    rc = ble_hci_sched_enqueue(ble_gap_conn_gen_disc_tx_params, NULL);
     if (rc != 0) {
         ble_gap_conn_master_state = BLE_GAP_CONN_STATE_IDLE;
         return rc;
@@ -312,43 +340,103 @@ ble_gap_conn_direct_connect(int addr_type, uint8_t *addr)
     return 0;
 }
 
+/*****************************************************************************
+ * @directed connectable mode                                                *
+ *****************************************************************************/
+
+static void
+ble_gap_conn_direct_connectable_ack_enable(struct ble_hci_ack *ack, void *arg)
+{
+    assert(ble_gap_conn_slave_state == BLE_GAP_CONN_STATE_S_ENABLE);
+
+    if (ack->bha_status != BLE_ERR_SUCCESS) {
+        ble_gap_conn_slave_failed(ack->bha_status);
+    } else {
+        ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_S_ENABLE_ACKED;
+    }
+}
+
+static int
+ble_gap_conn_direct_connectable_tx_enable(void *arg)
+{
+    int rc;
+
+    rc = host_hci_cmd_le_set_adv_enable(1);
+    if (rc != BLE_ERR_SUCCESS) {
+        ble_gap_conn_slave_failed(rc);
+        return 1;
+    }
+
+    ble_hci_ack_set_callback(ble_gap_conn_direct_connectable_ack_enable, NULL);
+    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_S_ENABLE;
+
+    return 0;
+}
+
+static void
+ble_gap_conn_direct_connectable_ack_params(struct ble_hci_ack *ack, void *arg)
+{
+    int rc;
+
+    assert(ble_gap_conn_slave_state == BLE_GAP_CONN_STATE_S_PARAMS);
+
+    if (ack->bha_status != BLE_ERR_SUCCESS) {
+        ble_gap_conn_slave_failed(ack->bha_status);
+        return;
+    }
+
+    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_S_PARAMS_ACKED;
+
+    rc = ble_hci_sched_enqueue(ble_gap_conn_direct_connectable_tx_enable,
+                               NULL);
+    if (rc != 0) {
+        ble_gap_conn_slave_failed(rc);
+        return;
+    }
+}
+
 static int
-ble_gap_conn_direct_advertise_tx_params(void *arg)
+ble_gap_conn_direct_connectable_tx_params(void *arg)
 {
     struct hci_adv_params hap;
     int rc;
 
     hap.adv_itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
     hap.adv_itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MAX;
-    hap.adv_type = BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_HD;
+    hap.adv_type = BLE_HCI_ADV_TYPE_ADV_IND;
     hap.own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC;
     hap.peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC;
     memcpy(hap.peer_addr, ble_gap_conn_slave_addr, sizeof hap.peer_addr);
     hap.adv_channel_map = BLE_HCI_ADV_CHANMASK_DEF;
     hap.adv_filter_policy = BLE_HCI_ADV_FILT_DEF;
 
+    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_S_PARAMS;
+    ble_hci_ack_set_callback(ble_gap_conn_direct_connectable_ack_params, NULL);
+
     rc = host_hci_cmd_le_set_adv_params(&hap);
     if (rc != 0) {
-        goto err;
+        ble_gap_conn_slave_failed(rc);
+        return 1;
     }
 
-    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_PARAMS;
-    ble_hci_ack_set_callback(ble_gap_conn_slave_ack, NULL);
-
     return 0;
-
-err:
-    ble_gap_conn_slave_failed(rc);
-    return 1;
 }
 
 /**
- * Enables the GAP Directed Connectable Mode.
+ * Enables Directed Connectable Mode, as described in vol. 3, part C, section
+ * 9.3.3.
+ *
+ * @param addr_type             The peer's address type; one of:
+ *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC
+ *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM
+ *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC_IDENT
+ *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM_IDENT
+ * @param addr                  The address of the peer to connect to.
  *
- * @return 0 on success; nonzero on failure.
+ * @return                      0 on success; nonzero on failure.
  */
 int
-ble_gap_conn_direct_advertise(int addr_type, uint8_t *addr)
+ble_gap_conn_direct_connectable(int addr_type, uint8_t *addr)
 {
     int rc;
 
@@ -357,134 +445,109 @@ ble_gap_conn_direct_advertise(int addr_type, uint8_t *addr)
         return BLE_HS_EALREADY;
     }
 
-    rc = ble_hci_sched_enqueue(ble_gap_conn_direct_advertise_tx_params, NULL);
+    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_S_PENDING;
+    memcpy(ble_gap_conn_slave_addr, addr, BLE_DEV_ADDR_LEN);
+
+    rc = ble_hci_sched_enqueue(ble_gap_conn_direct_connectable_tx_params,
+                               NULL);
     if (rc != 0) {
         return rc;
     }
 
-    ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_SLAVE_PENDING;
-    memcpy(ble_gap_conn_slave_addr, addr, BLE_DEV_ADDR_LEN);
-
     return 0;
 }
 
-static int
-ble_gap_conn_accept_new_conn(uint8_t *addr)
+/*****************************************************************************
+ * @direct connection establishment procedure                                *
+ *****************************************************************************/
+
+/**
+ * Processes an HCI acknowledgement (either command status or command complete)
+ * while a master connection is being established.
+ */
+static void
+ble_gap_conn_direct_connect_ack(struct ble_hci_ack *ack, void *arg)
 {
-    switch (ble_gap_conn_master_state) {
-    case BLE_GAP_CONN_STATE_MASTER_DIRECT_ACKED:
-        if (memcmp(ble_gap_conn_master_addr, addr, BLE_DEV_ADDR_LEN) == 0) {
-            ble_gap_conn_master_state = BLE_GAP_CONN_STATE_IDLE;
-            return 0;
-        }
-    }
+    assert(ble_gap_conn_master_state ==
+           BLE_GAP_CONN_STATE_M_DIRECT_UNACKED);
 
-    switch (ble_gap_conn_slave_state) {
-    case BLE_GAP_CONN_STATE_SLAVE_ENABLE_ACKED:
-        if (memcmp(ble_gap_conn_slave_addr, addr, BLE_DEV_ADDR_LEN) == 0) {
-            ble_gap_conn_slave_state = BLE_GAP_CONN_STATE_IDLE;
-            return 0;
-        }
-        break;
+    if (ack->bha_status != 0) {
+        ble_gap_conn_master_failed(ack->bha_status);
+        return;
     }
 
-    return BLE_HS_ENOENT;
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_M_DIRECT_ACKED;
 }
 
-/**
- * Processes an incoming connection-complete HCI event.
- */
-int
-ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt)
+static int
+ble_gap_conn_direct_connect_tx(void *arg)
 {
-    struct ble_hs_conn *conn;
+    struct hci_create_conn hcc;
     int rc;
 
-    /* Determine if this event refers to a completed connection or a connection
-     * in progress.
-     */
-    conn = ble_hs_conn_find(evt->connection_handle);
-
-    /* Apply the event to the existing connection if it exists. */
-    if (conn != NULL) {
-        if (evt->status != 0) {
-            ble_hs_conn_remove(conn);
-            ble_gap_conn_notify_app(evt->status, conn);
-            ble_hs_conn_free(conn);
-        }
+    hcc.scan_itvl = 0x0010;
+    hcc.scan_window = 0x0010;
+    hcc.filter_policy = BLE_HCI_CONN_FILT_NO_WL;
+    hcc.peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC;
+    memcpy(hcc.peer_addr, ble_gap_conn_master_addr, sizeof hcc.peer_addr);
+    hcc.own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC;
+    hcc.conn_itvl_min = 24;
+    hcc.conn_itvl_max = 40;
+    hcc.conn_latency = 0;
+    hcc.supervision_timeout = 0x0100; // XXX
+    hcc.min_ce_len = 0x0010; // XXX
+    hcc.max_ce_len = 0x0300; // XXX
 
-        return 0;
-    }
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_M_DIRECT_UNACKED;
+    ble_hci_ack_set_callback(ble_gap_conn_direct_connect_ack, NULL);
 
-    /* This event refers to a new connection. */
-    rc = ble_gap_conn_accept_new_conn(evt->peer_addr);
+    rc = host_hci_cmd_le_create_connection(&hcc);
     if (rc != 0) {
-        return BLE_HS_ENOENT;
-    }
-
-    if (evt->status != BLE_ERR_SUCCESS) {
-        return 0;
-    }
-
-    /* XXX: Ensure device address is expected. */
-    /* XXX: Ensure event fields are acceptable. */
-
-    conn = ble_hs_conn_alloc();
-    if (conn == NULL) {
-        /* XXX: Ensure this never happens. */
-        ble_gap_conn_notify_app(BLE_ERR_MEM_CAPACITY, NULL);
-        return BLE_HS_ENOMEM;
+        ble_gap_conn_master_failed(rc);
+        return 1;
     }
 
-    conn->bhc_handle = evt->connection_handle;
-    memcpy(conn->bhc_addr, evt->peer_addr, sizeof conn->bhc_addr);
-
-    ble_hs_conn_insert(conn);
-
-    ble_gap_conn_notify_app(0, conn);
-
     return 0;
 }
 
+/**
+ * Performs the Direct Connection Establishment Procedure, as described in
+ * vol. 3, part C, section 9.3.8.
+ *
+ * @param addr_type             The peer's address type; one of:
+ *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC
+ *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM
+ *                                  o BLE_HCI_ADV_PEER_ADDR_PUBLIC_IDENT
+ *                                  o BLE_HCI_ADV_PEER_ADDR_RANDOM_IDENT
+ * @param addr                  The address of the peer to connect to.
+ *
+ * @return                      0 on success; nonzero on failure.
+ */
 int
-ble_gap_conn_rx_disconn_complete(struct hci_disconn_complete *evt)
+ble_gap_conn_direct_connect(int addr_type, uint8_t *addr)
 {
-    struct ble_hs_conn *conn;
+    int rc;
 
-    conn = ble_hs_conn_find(evt->connection_handle);
-    if (conn == NULL) {
-        return BLE_HS_ENOENT;
+    /* Make sure no master connection attempt is already in progress. */
+    if (ble_gap_conn_master_in_progress()) {
+        return BLE_HS_EALREADY;
     }
 
-    if (evt->status == 0) {
-        ble_hs_conn_remove(conn);
-        ble_gap_conn_notify_app(evt->reason, conn);
-        ble_hs_conn_free(conn);
-    } else {
-        /* XXX: Ensure we have a disconnect operation in progress. */
-        ble_gap_conn_notify_app(evt->status, conn);
+    ble_gap_conn_master_state = BLE_GAP_CONN_STATE_M_DIRECT_PENDING;
+    memcpy(ble_gap_conn_master_addr, addr, BLE_DEV_ADDR_LEN);
+
+    rc = ble_hci_sched_enqueue(ble_gap_conn_direct_connect_tx, NULL);
+    if (rc != 0) {
+        ble_gap_conn_master_state = BLE_GAP_CONN_STATE_IDLE;
+        return rc;
     }
 
     return 0;
 }
 
-/**
- * Tells you if the BLE host is in the process of creating a master connection.
- */
-int
-ble_gap_conn_master_in_progress(void)
-{
-    return ble_gap_conn_master_state != BLE_GAP_CONN_STATE_IDLE;
-}
-
-/**
- * Tells you if the BLE host is in the process of creating a slave connection.
- */
-int
-ble_gap_conn_slave_in_progress(void)
-{
-    return ble_gap_conn_slave_state != BLE_GAP_CONN_STATE_IDLE;
-}
+/*****************************************************************************
+ * @init                                                                     *
+ *****************************************************************************/
 
 int
 ble_gap_conn_init(void)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/eea8a48c/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
index cbc9ae0..d6920db 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -83,7 +83,7 @@ ble_gap_test_task_handler(void *arg)
      * proper arguments.
      */
     cb_called = 0;
-    ble_gap_set_connect_cb(ble_gap_test_connect_cb, &cb_called);
+    ble_gap_conn_set_cb(ble_gap_test_connect_cb, &cb_called);
 
     /* Make sure there are no created connections and no connections in
      * progress.
@@ -91,7 +91,7 @@ ble_gap_test_task_handler(void *arg)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate a direct connection. */
-    ble_gap_direct_connection_establishment(0, addr);
+    ble_gap_conn_direct_connect(0, addr);
     TEST_ASSERT(ble_hs_conn_first() == NULL);
     TEST_ASSERT(!cb_called);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/eea8a48c/net/nimble/host/src/test/ble_hs_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_conn_test.c b/net/nimble/host/src/test/ble_hs_conn_test.c
index dab15de..e40814e 100644
--- a/net/nimble/host/src/test/ble_hs_conn_test.c
+++ b/net/nimble/host/src/test/ble_hs_conn_test.c
@@ -45,7 +45,7 @@ TEST_CASE(ble_hs_conn_test_master_direct_success)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate connection. */
-    rc = ble_gap_direct_connection_establishment(0, addr);
+    rc = ble_gap_conn_direct_connect(0, addr);
     TEST_ASSERT(rc == 0);
 
     ble_hci_sched_wakeup();
@@ -91,7 +91,7 @@ TEST_CASE(ble_hs_conn_test_master_direct_hci_errors)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate connection. */
-    rc = ble_gap_direct_connection_establishment(0, addr);
+    rc = ble_gap_conn_direct_connect(0, addr);
     TEST_ASSERT(rc == 0);
 
     ble_hci_sched_wakeup();
@@ -136,7 +136,7 @@ TEST_CASE(ble_hs_conn_test_slave_direct_success)
     TEST_ASSERT(ble_hs_conn_first() == NULL);
 
     /* Initiate advertising. */
-    rc = ble_gap_directed_connectable(0, addr);
+    rc = ble_gap_conn_direct_connectable(0, addr);
     TEST_ASSERT(rc == 0);
 
     ble_hci_sched_wakeup();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/eea8a48c/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
index eed81fd..4cf71e0 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -65,7 +65,7 @@ ble_hs_test_util_create_conn(uint16_t handle, uint8_t *addr)
     struct ble_hs_conn *conn;
     int rc;
 
-    rc = ble_gap_direct_connection_establishment(0, addr);
+    rc = ble_gap_conn_direct_connect(0, addr);
     TEST_ASSERT(rc == 0);
 
     ble_hci_sched_wakeup();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/eea8a48c/project/hostctlrtest/src/main.c
----------------------------------------------------------------------
diff --git a/project/hostctlrtest/src/main.c b/project/hostctlrtest/src/main.c
index 1276283..17a1008 100755
--- a/project/hostctlrtest/src/main.c
+++ b/project/hostctlrtest/src/main.c
@@ -57,8 +57,8 @@ uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
 uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
 uint8_t g_host_adv_len;
 
-//static uint8_t hostctlrtest_slv_addr[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
-static uint8_t hostctlrtest_slv_addr[6] = {0x82, 0x6a, 0xd0, 0x48, 0xb4, 0xb0};
+static uint8_t hostctlrtest_slv_addr[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
+//static uint8_t hostctlrtest_slv_addr[6] = {0x82, 0x6a, 0xd0, 0x48, 0xb4, 0xb0};
 static uint8_t hostctlrtest_mst_addr[6] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a};
 
 /* Create a mbuf pool of BLE mbufs */
@@ -342,17 +342,17 @@ hostctlrtest_task_handler(void *arg)
     g_hostctlrtest_state = 0;
     g_next_os_time = os_time_get();
     
-    ble_gap_set_connect_cb(hostctlrtest_on_connect, NULL);
+    ble_gap_conn_set_cb(hostctlrtest_on_connect, NULL);
 
 #if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_ADVERTISER
     hostctlrtest_register_attrs();
     console_printf("ble_gap_directed_connectable\n");
-    rc = ble_gap_directed_connectable(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
-                                      hostctlrtest_mst_addr);
+    rc = ble_gap_conn_direct_connectable(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
+                                         hostctlrtest_mst_addr);
 #else
     console_printf("ble_gap_direct_connection_establishment\n");
-    rc = ble_gap_direct_connection_establishment(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
-                                                 hostctlrtest_slv_addr);
+    rc = ble_gap_conn_direct_connect(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
+                                     hostctlrtest_slv_addr);
 #endif
     assert(rc == 0);