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/02/02 04:42:22 UTC

[1/5] incubator-mynewt-larva git commit: Protect connection list with a mutex

Repository: incubator-mynewt-larva
Updated Branches:
  refs/heads/master 7674b1bf8 -> da8f3e038


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 22e371b..c45012e 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -55,6 +55,9 @@ struct ble_gatts_clt_cfg {
 static struct ble_gatts_clt_cfg *ble_gatts_clt_cfgs;
 static int ble_gatts_num_cfgable_chrs;
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_svc_access(uint16_t conn_handle, uint16_t attr_handle,
                      uint8_t *uuid128, uint8_t op,
@@ -81,6 +84,9 @@ ble_gatts_svc_access(uint16_t conn_handle, uint16_t attr_handle,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_inc_access(uint16_t conn_handle, uint16_t attr_handle,
                      uint8_t *uuid128, uint8_t op,
@@ -111,6 +117,9 @@ ble_gatts_inc_access(uint16_t conn_handle, uint16_t attr_handle,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static uint16_t
 ble_gatts_chr_clt_cfg_allowed(const struct ble_gatt_chr_def *chr)
 {
@@ -127,6 +136,9 @@ ble_gatts_chr_clt_cfg_allowed(const struct ble_gatt_chr_def *chr)
     return flags;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static uint8_t
 ble_gatts_att_flags_from_chr_flags(ble_gatt_chr_flags chr_flags)
 {
@@ -143,6 +155,9 @@ ble_gatts_att_flags_from_chr_flags(ble_gatt_chr_flags chr_flags)
     return att_flags;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static uint8_t
 ble_gatts_chr_properties(const struct ble_gatt_chr_def *chr)
 {
@@ -180,6 +195,9 @@ ble_gatts_chr_properties(const struct ble_gatt_chr_def *chr)
     return properties;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_chr_def_access(uint16_t conn_handle, uint16_t attr_handle,
                          uint8_t *uuid128, uint8_t op,
@@ -211,6 +229,9 @@ ble_gatts_chr_def_access(uint16_t conn_handle, uint16_t attr_handle,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_chr_is_sane(const struct ble_gatt_chr_def *chr)
 {
@@ -227,6 +248,9 @@ ble_gatts_chr_is_sane(const struct ble_gatt_chr_def *chr)
     return 1;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static uint8_t
 ble_gatts_chr_op(uint8_t att_op)
 {
@@ -243,6 +267,9 @@ ble_gatts_chr_op(uint8_t att_op)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gatts_chr_val_access(uint16_t conn_handle, uint16_t attr_handle,
                          uint8_t *uuid128, uint8_t att_op,
@@ -279,7 +306,9 @@ ble_gatts_chr_val_access(uint16_t conn_handle, uint16_t attr_handle,
     return 0;
 }
 
-
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_find_svc(const struct ble_gatt_svc_def *svc)
 {
@@ -294,6 +323,9 @@ ble_gatts_find_svc(const struct ble_gatt_svc_def *svc)
     return -1;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_svc_incs_satisfied(const struct ble_gatt_svc_def *svc)
 {
@@ -315,6 +347,9 @@ ble_gatts_svc_incs_satisfied(const struct ble_gatt_svc_def *svc)
     return 1;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_register_inc(struct ble_gatts_svc_entry *entry)
 {
@@ -333,6 +368,9 @@ ble_gatts_register_inc(struct ble_gatts_svc_entry *entry)
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_clt_cfg_find_idx(struct ble_gatts_clt_cfg *cfgs,
                            uint16_t chr_def_handle)
@@ -350,6 +388,9 @@ ble_gatts_clt_cfg_find_idx(struct ble_gatts_clt_cfg *cfgs,
     return -1;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static struct ble_gatts_clt_cfg *
 ble_gatts_clt_cfg_find(struct ble_gatts_clt_cfg *cfgs,
                        uint16_t chr_def_handle)
@@ -364,35 +405,33 @@ ble_gatts_clt_cfg_find(struct ble_gatts_clt_cfg *cfgs,
     }
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static int
-ble_gatts_clt_cfg_access(uint16_t conn_handle, uint16_t attr_handle,
-                         uint8_t *uuid128, uint8_t op,
-                         struct ble_att_svr_access_ctxt *ctxt,
-                         void *arg)
+ble_gatts_clt_cfg_access_locked(struct ble_hs_conn *conn, uint16_t attr_handle,
+                                uint8_t *uuid128, uint8_t op,
+                                struct ble_att_svr_access_ctxt *ctxt,
+                                void *arg)
 {
     struct ble_gatts_clt_cfg *clt_cfg;
-    struct ble_hs_conn *conn;
     uint16_t chr_def_handle;
     uint16_t flags;
 
     static uint8_t buf[2];
 
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn == NULL) {
-        return BLE_ATT_ERR_UNLIKELY;
-    }
-
     /* We always register the client characteristics descriptor with handle
      * (chr_def + 2).
      */
     chr_def_handle = attr_handle - 2;
     if (chr_def_handle > attr_handle) {
+        /* Attribute handle wrapped somehow. */
         return BLE_ATT_ERR_UNLIKELY;
     }
 
     clt_cfg = ble_gatts_clt_cfg_find(conn->bhc_gatt_svr.clt_cfgs,
                                      attr_handle - 2);
-    if (clt_cfg == 0) {
+    if (clt_cfg == NULL) {
         return BLE_ATT_ERR_UNLIKELY;
     }
 
@@ -424,6 +463,36 @@ ble_gatts_clt_cfg_access(uint16_t conn_handle, uint16_t attr_handle,
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
+static int
+ble_gatts_clt_cfg_access(uint16_t conn_handle, uint16_t attr_handle,
+                         uint8_t *uuid128, uint8_t op,
+                         struct ble_att_svr_access_ctxt *ctxt,
+                         void *arg)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    conn = ble_hs_conn_find(conn_handle);
+    if (conn == NULL) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+    } else {
+        rc = ble_gatts_clt_cfg_access_locked(conn, attr_handle, uuid128, op,
+                                             ctxt, arg);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
+/**
+ * Lock restrictions: None.
+ */
 static uint8_t
 ble_gatts_dsc_op(uint8_t att_op)
 {
@@ -440,6 +509,9 @@ ble_gatts_dsc_op(uint8_t att_op)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gatts_dsc_access(uint16_t conn_handle, uint16_t attr_handle,
                      uint8_t *uuid128, uint8_t att_op,
@@ -470,6 +542,9 @@ ble_gatts_dsc_access(uint16_t conn_handle, uint16_t attr_handle,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_dsc_is_sane(const struct ble_gatt_dsc_def *dsc)
 {
@@ -484,6 +559,9 @@ ble_gatts_dsc_is_sane(const struct ble_gatt_dsc_def *dsc)
     return 1;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_register_dsc(const struct ble_gatt_dsc_def *dsc,
                        const struct ble_gatt_chr_def *chr,
@@ -516,6 +594,9 @@ ble_gatts_register_dsc(const struct ble_gatt_dsc_def *dsc,
 
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_register_clt_cfg_dsc(uint16_t *att_handle)
 {
@@ -536,6 +617,9 @@ ble_gatts_register_clt_cfg_dsc(uint16_t *att_handle)
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_register_chr(const struct ble_gatt_chr_def *chr,
                        ble_gatt_register_fn *register_cb, void *cb_arg)
@@ -602,6 +686,9 @@ ble_gatts_register_chr(const struct ble_gatt_chr_def *chr,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_svc_type_to_uuid(uint8_t svc_type, uint16_t *out_uuid16)
 {
@@ -619,6 +706,9 @@ ble_gatts_svc_type_to_uuid(uint8_t svc_type, uint16_t *out_uuid16)
     }
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_svc_is_sane(const struct ble_gatt_svc_def *svc)
 {
@@ -635,6 +725,9 @@ ble_gatts_svc_is_sane(const struct ble_gatt_svc_def *svc)
     return 1;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
                        uint16_t *out_handle,
@@ -699,6 +792,9 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_register_round(int *out_num_registered, ble_gatt_register_fn *cb,
                          void *cb_arg)
@@ -742,6 +838,9 @@ ble_gatts_register_round(int *out_num_registered, ble_gatt_register_fn *cb,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gatts_register_svcs(const struct ble_gatt_svc_def *svcs,
                         ble_gatt_register_fn *cb, void *cb_arg)
@@ -770,6 +869,9 @@ ble_gatts_register_svcs(const struct ble_gatt_svc_def *svcs,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 void
 ble_gatts_conn_deinit(struct ble_gatts_conn *gatts_conn)
 {
@@ -783,12 +885,18 @@ ble_gatts_conn_deinit(struct ble_gatts_conn *gatts_conn)
     }
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_clt_cfg_size(void)
 {
     return ble_gatts_num_cfgable_chrs * sizeof (struct ble_gatts_clt_cfg);
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gatts_clt_cfg_init(void)
 {
@@ -852,6 +960,9 @@ ble_gatts_clt_cfg_init(void)
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gatts_conn_init(struct ble_gatts_conn *gatts_conn)
 {
@@ -881,6 +992,9 @@ ble_gatts_conn_init(struct ble_gatts_conn *gatts_conn)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 void
 ble_gatts_send_notifications(struct ble_hs_conn *conn)
 {
@@ -888,7 +1002,6 @@ ble_gatts_send_notifications(struct ble_hs_conn *conn)
     int rc;
     int i;
 
-
     /* Iterate through each configurable characteristic.  If a characteristic
      * has been updated, try to send an indication or notification
      * (never both).
@@ -920,6 +1033,9 @@ ble_gatts_send_notifications(struct ble_hs_conn *conn)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_gatts_chr_updated(uint16_t chr_def_handle)
 {
@@ -935,7 +1051,8 @@ ble_gatts_chr_updated(uint16_t chr_def_handle)
         return;
     }
 
-    /* XXX: Lock connection list. */
+    ble_hs_conn_lock();
+
     for (conn = ble_hs_conn_first();
          conn != NULL;
          conn = SLIST_NEXT(conn, bhc_next)) {
@@ -951,14 +1068,22 @@ ble_gatts_chr_updated(uint16_t chr_def_handle)
             ble_gatts_send_notifications(conn);
         }
     }
+
+    ble_hs_conn_unlock();
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_gatts_free_mem(void)
 {
     free(ble_gatts_clt_cfg_mem);
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gatts_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 a21c10a..6ceb017 100644
--- a/net/nimble/host/src/ble_hs_adv.c
+++ b/net/nimble/host/src/ble_hs_adv.c
@@ -101,6 +101,8 @@ ble_hs_adv_set_array32(uint8_t type, uint8_t num_elems, uint32_t *elems,
 /**
  * Sets the significant part of the data in outgoing advertisements.
  *
+ * Lock restrictions: None.
+ *
  * @return                      0 on success;  on failure.
  */
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 1ed3849..9598053 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -31,12 +31,53 @@ static struct os_mempool ble_hs_conn_pool;
 
 static os_membuf_t *ble_hs_conn_elem_mem;
 
+static struct os_mutex ble_hs_conn_mutex;
+
+void
+ble_hs_conn_lock(void)
+{
+    struct os_task *owner;
+    int rc;
+
+    owner = ble_hs_conn_mutex.mu_owner;
+    if (owner != NULL) {
+        assert(owner != os_sched_get_current_task());
+    }
+
+    rc = os_mutex_pend(&ble_hs_conn_mutex, 0xffffffff);
+    assert(rc == 0 || rc == OS_NOT_STARTED);
+}
+
+void
+ble_hs_conn_unlock(void)
+{
+    int rc;
+
+    rc = os_mutex_release(&ble_hs_conn_mutex);
+    assert(rc == 0 || rc == OS_NOT_STARTED);
+}
+
+int
+ble_hs_conn_locked_by_cur_task(void)
+{
+    struct os_task *owner;
+
+    owner = ble_hs_conn_mutex.mu_owner;
+    return owner != NULL && owner == os_sched_get_current_task();
+}
+
+/**
+ * Lock restrictions: none.
+ */
 int
 ble_hs_conn_can_alloc(void)
 {
     return ble_hs_conn_pool.mp_num_free >= 1;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 struct ble_l2cap_chan *
 ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
 {
@@ -54,6 +95,10 @@ ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
     return NULL;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex if connection has been
+ * inserted.
+ */
 int
 ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {
@@ -81,6 +126,9 @@ ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
     return 0;
 }
 
+/**
+ * Lock restrictions: none.
+ */
 struct ble_hs_conn *
 ble_hs_conn_alloc(void)
 {
@@ -128,6 +176,9 @@ err:
     return NULL;
 }
 
+/**
+ * Lock restrictions: none.
+ */
 static void
 ble_hs_conn_delete_chan(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {
@@ -139,6 +190,9 @@ ble_hs_conn_delete_chan(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
     ble_l2cap_chan_free(chan);
 }
 
+/**
+ * Lock restrictions: none.
+ */
 void
 ble_hs_conn_free(struct ble_hs_conn *conn)
 {
@@ -161,19 +215,32 @@ ble_hs_conn_free(struct ble_hs_conn *conn)
     assert(rc == 0);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_hs_conn_insert(struct ble_hs_conn *conn)
 {
+    ble_hs_conn_lock();
+
     assert(ble_hs_conn_find(conn->bhc_handle) == NULL);
     SLIST_INSERT_HEAD(&ble_hs_conns, conn, bhc_next);
+
+    ble_hs_conn_unlock();
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 void
 ble_hs_conn_remove(struct ble_hs_conn *conn)
 {
     SLIST_REMOVE(&ble_hs_conns, conn, ble_hs_conn, bhc_next);
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 struct ble_hs_conn *
 ble_hs_conn_find(uint16_t conn_handle)
 {
@@ -189,8 +256,24 @@ ble_hs_conn_find(uint16_t conn_handle)
 }
 
 /**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
+int
+ble_hs_conn_exists(uint16_t conn_handle)
+{
+    struct ble_hs_conn *conn;
+
+    ble_hs_conn_lock();
+    conn = ble_hs_conn_find(conn_handle);
+    ble_hs_conn_unlock();
+
+    return conn != NULL;
+}
+
+/**
  * Retrieves the first connection in the list.
- * XXX: This is likely temporary.
+ *
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
  */
 struct ble_hs_conn *
 ble_hs_conn_first(void)
@@ -198,12 +281,18 @@ ble_hs_conn_first(void)
     return SLIST_FIRST(&ble_hs_conns);
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static void
 ble_hs_conn_txable_transition(struct ble_hs_conn *conn)
 {
     ble_gattc_connection_txable(conn->bhc_handle);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_hs_conn_rx_num_completed_pkts(uint16_t handle, uint16_t num_pkts)
 {
@@ -211,25 +300,30 @@ ble_hs_conn_rx_num_completed_pkts(uint16_t handle, uint16_t num_pkts)
     int could_tx;
     int can_tx;
 
-    conn = ble_hs_conn_find(handle);
-    if (conn == NULL) {
-        return;
-    }
+    ble_hs_conn_lock();
 
-    could_tx = ble_hs_conn_can_tx(conn);
+    conn = ble_hs_conn_find(handle);
+    if (conn != NULL) {
+        could_tx = ble_hs_conn_can_tx(conn);
 
-    if (num_pkts > conn->bhc_outstanding_pkts) {
-        num_pkts = conn->bhc_outstanding_pkts;
-    }
-    conn->bhc_outstanding_pkts -= num_pkts;
+        if (num_pkts > conn->bhc_outstanding_pkts) {
+            num_pkts = conn->bhc_outstanding_pkts;
+        }
+        conn->bhc_outstanding_pkts -= num_pkts;
 
-    can_tx = ble_hs_conn_can_tx(conn);
+        can_tx = ble_hs_conn_can_tx(conn);
 
-    if (!could_tx && can_tx) {
-        ble_hs_conn_txable_transition(conn);
+        if (!could_tx && can_tx) {
+            ble_hs_conn_txable_transition(conn);
+        }
     }
+
+    ble_hs_conn_unlock();
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_hs_conn_can_tx(struct ble_hs_conn *conn)
 {
@@ -238,6 +332,9 @@ ble_hs_conn_can_tx(struct ble_hs_conn *conn)
                 ble_hs_cfg.max_outstanding_pkts_per_conn;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_hs_conn_free_mem(void)
 {
@@ -245,6 +342,9 @@ ble_hs_conn_free_mem(void)
     ble_hs_conn_elem_mem = NULL;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int 
 ble_hs_conn_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_hs_conn.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.h b/net/nimble/host/src/ble_hs_conn.h
index bdea85c..ab78df4 100644
--- a/net/nimble/host/src/ble_hs_conn.h
+++ b/net/nimble/host/src/ble_hs_conn.h
@@ -53,12 +53,16 @@ struct ble_hs_conn {
     void *bhc_cb_arg;
 };
 
+void ble_hs_conn_lock(void);
+void ble_hs_conn_unlock(void);
+int ble_hs_conn_locked_by_cur_task(void);
 int ble_hs_conn_can_alloc(void);
 struct ble_hs_conn *ble_hs_conn_alloc(void);
 void ble_hs_conn_free(struct ble_hs_conn *conn);
 void ble_hs_conn_insert(struct ble_hs_conn *conn);
 void ble_hs_conn_remove(struct ble_hs_conn *conn);
 struct ble_hs_conn *ble_hs_conn_find(uint16_t conn_handle);
+int ble_hs_conn_exists(uint16_t conn_handle);
 struct ble_hs_conn *ble_hs_conn_first(void);
 struct ble_l2cap_chan *ble_hs_conn_chan_find(struct ble_hs_conn *conn,
                                              uint16_t cid);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 008f5bc..348b1fc 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -34,6 +34,9 @@ static struct os_mempool ble_l2cap_chan_pool;
 
 static void *ble_l2cap_chan_mem;
 
+/**
+ * Lock restrictions: None.
+ */
 struct ble_l2cap_chan *
 ble_l2cap_chan_alloc(void)
 {
@@ -49,6 +52,9 @@ ble_l2cap_chan_alloc(void)
     return chan;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 void
 ble_l2cap_chan_free(struct ble_l2cap_chan *chan)
 {
@@ -62,6 +68,9 @@ ble_l2cap_chan_free(struct ble_l2cap_chan *chan)
     assert(rc == 0);
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 uint16_t
 ble_l2cap_chan_mtu(struct ble_l2cap_chan *chan)
 {
@@ -83,6 +92,9 @@ ble_l2cap_chan_mtu(struct ble_l2cap_chan *chan)
     return mtu;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_l2cap_parse_hdr(struct os_mbuf *om, int off,
                     struct ble_l2cap_hdr *l2cap_hdr)
@@ -100,6 +112,9 @@ ble_l2cap_parse_hdr(struct os_mbuf *om, int off,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 struct os_mbuf *
 ble_l2cap_prepend_hdr(struct os_mbuf *om, uint16_t cid, uint16_t len)
 {
@@ -118,19 +133,34 @@ ble_l2cap_prepend_hdr(struct os_mbuf *om, uint16_t cid, uint16_t len)
     return om;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static void
-ble_l2cap_discard_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
+ble_l2cap_forget_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {
     conn->bhc_rx_chan = NULL;
-
-    os_mbuf_free_chain(chan->blc_rx_buf);
     chan->blc_rx_buf = NULL;
     chan->blc_rx_len = 0;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
+static void
+ble_l2cap_discard_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
+{
+    os_mbuf_free_chain(chan->blc_rx_buf);
+    ble_l2cap_forget_rx(conn, chan);
+}
+
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static int
 ble_l2cap_rx_payload(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                     struct os_mbuf *om)
+                     struct os_mbuf *om,
+                     ble_l2cap_rx_fn **out_rx_cb, struct os_mbuf **out_rx_buf)
 {
     int len_diff;
     int rc;
@@ -149,20 +179,27 @@ ble_l2cap_rx_payload(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
         rc = BLE_HS_EBADDATA;
     } else if (len_diff == 0) {
         /* All fragments received. */
-        rc = chan->blc_rx_fn(conn, chan, &chan->blc_rx_buf);
-        ble_l2cap_discard_rx(conn, chan);
+        *out_rx_cb = chan->blc_rx_fn;
+        *out_rx_buf = chan->blc_rx_buf;
+        ble_l2cap_forget_rx(conn, chan);
+        rc = 0;
     } else {
         /* More fragments remain. */
-        rc = 0;
+        rc = BLE_HS_EAGAIN;
     }
 
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_l2cap_rx(struct ble_hs_conn *conn,
              struct hci_data_hdr *hci_hdr,
-             struct os_mbuf *om)
+             struct os_mbuf *om,
+             ble_l2cap_rx_fn **out_rx_cb,
+             struct os_mbuf **out_rx_buf)
 {
     struct ble_l2cap_chan *chan;
     struct ble_l2cap_hdr l2cap_hdr;
@@ -211,7 +248,7 @@ ble_l2cap_rx(struct ble_hs_conn *conn,
         goto err;
     }
 
-    rc = ble_l2cap_rx_payload(conn, chan, om);
+    rc = ble_l2cap_rx_payload(conn, chan, om, out_rx_cb, out_rx_buf);
     om = NULL;
     if (rc != 0) {
         goto err;
@@ -227,6 +264,8 @@ err:
 /**
  * Transmits the L2CAP payload contained in the specified mbuf.  The supplied
  * mbuf is consumed, regardless of the outcome of the function call.
+ * 
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
  *
  * @param chan                  The L2CAP channel to transmit over.
  * @param om                    The data to transmit.
@@ -258,6 +297,9 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_l2cap_free_mem(void)
 {
@@ -265,6 +307,9 @@ ble_l2cap_free_mem(void)
     ble_l2cap_chan_mem = NULL;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_l2cap_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_l2cap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index 4f8940b..ec5f67f 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -66,8 +66,7 @@ struct ble_l2cap_hdr
 
 struct ble_l2cap_chan;
 
-typedef int ble_l2cap_rx_fn(struct ble_hs_conn *conn,
-                            struct ble_l2cap_chan *chan,
+typedef int ble_l2cap_rx_fn(uint16_t conn_handle,
                             struct os_mbuf **om);
 
 typedef int ble_l2cap_tx_fn(struct ble_hs_conn *conn,
@@ -133,9 +132,11 @@ void ble_l2cap_chan_free(struct ble_l2cap_chan *chan);
 uint16_t ble_l2cap_chan_mtu(struct ble_l2cap_chan *chan);
 
 
-int ble_l2cap_rx(struct ble_hs_conn *connection,
+int ble_l2cap_rx(struct ble_hs_conn *conn,
                  struct hci_data_hdr *hci_hdr,
-                 struct os_mbuf *om);
+                 struct os_mbuf *om,
+                 ble_l2cap_rx_fn **out_rx_cb,
+                 struct os_mbuf **out_rx_buf);
 int ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                  struct os_mbuf *om);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 43d0125..07afcd7 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -75,21 +75,17 @@ static struct os_callout_func ble_l2cap_sig_heartbeat_timer;
 
 typedef int ble_l2cap_sig_kick_fn(struct ble_l2cap_sig_proc *proc);
 
-typedef int ble_l2cap_sig_rx_fn(struct ble_hs_conn *conn,
-                                struct ble_l2cap_chan *chan,
+typedef int ble_l2cap_sig_rx_fn(uint16_t conn_handle,
                                 struct ble_l2cap_sig_hdr *hdr,
                                 struct os_mbuf **om);
 
-static int ble_l2cap_sig_rx_noop(struct ble_hs_conn *conn,
-                                 struct ble_l2cap_chan *chan,
+static int ble_l2cap_sig_rx_noop(uint16_t conn_handle,
                                  struct ble_l2cap_sig_hdr *hdr,
                                  struct os_mbuf **om);
-static int ble_l2cap_sig_update_req_rx(struct ble_hs_conn *conn,
-                                       struct ble_l2cap_chan *chan,
+static int ble_l2cap_sig_update_req_rx(uint16_t conn_handle,
                                        struct ble_l2cap_sig_hdr *hdr,
                                        struct os_mbuf **om);
-static int ble_l2cap_sig_update_rsp_rx(struct ble_hs_conn *conn,
-                                       struct ble_l2cap_chan *chan,
+static int ble_l2cap_sig_update_rsp_rx(uint16_t conn_handle,
                                        struct ble_l2cap_sig_hdr *hdr,
                                        struct os_mbuf **om);
 
@@ -127,6 +123,8 @@ static STAILQ_HEAD(, ble_l2cap_sig_proc) ble_l2cap_sig_list;
 
 /**
  * Ensures all procedure entries are in a valid state.
+ *
+ * Lock restrictions: None.
  */
 static void
 ble_l2cap_sig_assert_sanity(void)
@@ -152,6 +150,34 @@ ble_l2cap_sig_assert_sanity(void)
 #endif
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
+static int
+ble_l2cap_sig_conn_chan_find(uint16_t conn_handle,
+                             struct ble_hs_conn **out_conn,
+                             struct ble_l2cap_chan **out_chan)
+{
+    ble_hs_conn_lock();
+
+    *out_conn = ble_hs_conn_find(conn_handle);
+    if (*out_conn != NULL) {
+        *out_chan = ble_hs_conn_chan_find(*out_conn, BLE_L2CAP_CID_SIG);
+        assert(*out_chan != NULL);
+    }
+
+    ble_hs_conn_unlock();
+
+    if (*out_conn == NULL) {
+        return BLE_HS_ENOTCONN;
+    } else {
+        return 0;
+    }
+}
+
+/**
+ * Lock restrictions: None.
+ */
 static uint8_t
 ble_l2cap_sig_next_id(void)
 {
@@ -164,6 +190,9 @@ ble_l2cap_sig_next_id(void)
     return ble_l2cap_sig_cur_id;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static ble_l2cap_sig_rx_fn *
 ble_l2cap_sig_dispatch_get(uint8_t op)
 {
@@ -174,6 +203,9 @@ ble_l2cap_sig_dispatch_get(uint8_t op)
     return ble_l2cap_sig_dispatch[op];
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static ble_l2cap_sig_kick_fn *
 ble_l2cap_sig_kick_get(uint8_t op)
 {
@@ -186,6 +218,8 @@ ble_l2cap_sig_kick_get(uint8_t op)
 
 /**
  * Determines if the specified proc entry's "pending" flag can be set.
+ *
+ * Lock restrictions: None.
  */
 static int
 ble_l2cap_sig_proc_can_pend(struct ble_l2cap_sig_proc *proc)
@@ -198,6 +232,8 @@ ble_l2cap_sig_proc_can_pend(struct ble_l2cap_sig_proc *proc)
 /**
  * Allocates a proc entry.
  *
+ * Lock restrictions: None.
+ *
  * @return                      An entry on success; null on failure.
  */
 static struct ble_l2cap_sig_proc *
@@ -215,6 +251,8 @@ ble_l2cap_sig_proc_alloc(void)
 
 /**
  * Frees the specified proc entry.  No-op if passed a null pointer.
+ *
+ * Lock restrictions: None.
  */
 static void
 ble_l2cap_sig_proc_free(struct ble_l2cap_sig_proc *proc)
@@ -230,6 +268,8 @@ ble_l2cap_sig_proc_free(struct ble_l2cap_sig_proc *proc)
 /**
  * Removes the specified proc entry from the global list without freeing it.
  *
+ * Lock restrictions: None.
+ *
  * @param proc                  The proc to remove.
  * @param prev                  The proc that is previous to "proc" in the
  *                                  list; null if "proc" is the list head.
@@ -250,6 +290,8 @@ ble_l2cap_sig_proc_remove(struct ble_l2cap_sig_proc *proc,
 /**
  * Removes and frees the speicifed proc entry.
  *
+ * Lock restrictions: None.
+ *
  * @param proc                  The proc to remove and free.
  * @param prev                  The proc that is previous to "proc" in the
  *                                  list; null if "proc" is the list head.
@@ -262,20 +304,13 @@ ble_l2cap_sig_proc_remove_free(struct ble_l2cap_sig_proc *proc,
     ble_l2cap_sig_proc_free(proc);
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_l2cap_sig_new_proc(uint16_t conn_handle, uint8_t op,
                        struct ble_l2cap_sig_proc **out_proc)
 {
-    struct ble_hs_conn *conn;
-
-    *out_proc = NULL;
-
-    /* Ensure we have a connection with the specified handle. */
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn == NULL) {
-        return BLE_HS_ENOTCONN;
-    }
-
     *out_proc = ble_l2cap_sig_proc_alloc();
     if (*out_proc == NULL) {
         return BLE_HS_ENOMEM;
@@ -284,6 +319,7 @@ ble_l2cap_sig_new_proc(uint16_t conn_handle, uint8_t op,
     memset(*out_proc, 0, sizeof **out_proc);
     (*out_proc)->op = op;
     (*out_proc)->conn_handle = conn_handle;
+    (*out_proc)->tx_time = os_time_get();
 
     STAILQ_INSERT_TAIL(&ble_l2cap_sig_list, *out_proc, next);
 
@@ -293,6 +329,8 @@ ble_l2cap_sig_new_proc(uint16_t conn_handle, uint8_t op,
 /**
  * Tests if a proc entry fits the specified criteria.
  *
+ * Lock restrictions: None.
+ *
  * @param proc                  The procedure to test.
  * @param conn_handle           The connection handle to match against.
  * @param op                    The op code to match against/
@@ -331,6 +369,8 @@ ble_l2cap_sig_proc_matches(struct ble_l2cap_sig_proc *proc,
 /**
  * Searched the global proc list for an entry that fits the specified criteria.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection handle to match against.
  * @param op                    The op code to match against.
  * @param id                    The identifier to match against.
@@ -369,6 +409,8 @@ ble_l2cap_sig_proc_find(uint16_t conn_handle, uint8_t op, uint8_t id,
 /**
  * Sets the specified proc entry's "pending" flag (i.e., indicates that the
  * L2CAP sig procedure is stalled until it transmits its next request.
+ *
+ * Lock restrictions: None.
  */
 static void
 ble_l2cap_sig_proc_set_pending(struct ble_l2cap_sig_proc *proc)
@@ -383,6 +425,8 @@ ble_l2cap_sig_proc_set_pending(struct ble_l2cap_sig_proc *proc)
 /**
  * Sets the specified proc entry's "expecting" flag (i.e., indicates that the
  * L2CAP sig procedure is stalled until it receives a response.
+ *
+ * Lock restrictions: None.
  */
 static void
 ble_l2cap_sig_proc_set_expecting(struct ble_l2cap_sig_proc *proc,
@@ -397,9 +441,11 @@ ble_l2cap_sig_proc_set_expecting(struct ble_l2cap_sig_proc *proc,
     STAILQ_INSERT_TAIL(&ble_l2cap_sig_list, proc, next);
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
-ble_l2cap_sig_rx_noop(struct ble_hs_conn *conn,
-                      struct ble_l2cap_chan *chan,
+ble_l2cap_sig_rx_noop(uint16_t conn_handle,
                       struct ble_l2cap_sig_hdr *hdr,
                       struct os_mbuf **om)
 {
@@ -410,22 +456,31 @@ ble_l2cap_sig_rx_noop(struct ble_hs_conn *conn,
  * $update                                                                   *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_l2cap_sig_update_call_cb(struct ble_l2cap_sig_proc *proc, int status)
 {
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->update.cb != NULL) {
         proc->update.cb(status, proc->update.cb_arg);
     }
 }
 
-static int
-ble_l2cap_sig_update_req_rx(struct ble_hs_conn *conn,
-                            struct ble_l2cap_chan *chan,
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
+int
+ble_l2cap_sig_update_req_rx(uint16_t conn_handle,
                             struct ble_l2cap_sig_hdr *hdr,
                             struct os_mbuf **om)
 {
     struct ble_l2cap_sig_update_req req;
     struct ble_gap_conn_upd_params params;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
     uint16_t l2cap_result;
     int rc;
 
@@ -434,27 +489,38 @@ ble_l2cap_sig_update_req_rx(struct ble_hs_conn *conn,
         return BLE_HS_ENOMEM;
     }
 
-    rc = ble_l2cap_sig_update_req_parse((*om)->om_data, (*om)->om_len, &req);
-    assert(rc == 0);
+    ble_hs_conn_lock();
+    rc = ble_l2cap_sig_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_l2cap_sig_update_req_parse((*om)->om_data, (*om)->om_len,
+                                            &req);
+        assert(rc == 0);
 
-    /* Only a master can process an update request. */
-    if (!(conn->bhc_flags & BLE_HS_CONN_F_MASTER)) {
-        ble_l2cap_sig_reject_tx(conn, chan, hdr->identifier,
-                                BLE_L2CAP_ERR_CMD_NOT_UNDERSTOOD);
-        return BLE_HS_ENOTSUP;
+        /* Only a master can process an update request. */
+        if (!(conn->bhc_flags & BLE_HS_CONN_F_MASTER)) {
+            ble_l2cap_sig_reject_tx(conn, chan, hdr->identifier,
+                                    BLE_L2CAP_ERR_CMD_NOT_UNDERSTOOD);
+            rc = BLE_HS_ENOTSUP;
+        } else {
+            params.itvl_min = req.itvl_min;
+            params.itvl_max = req.itvl_max;
+            params.latency = req.slave_latency;
+            params.supervision_timeout = req.timeout_multiplier;
+            params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
+            params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
+        }
     }
+    ble_hs_conn_unlock();
 
-    params.itvl_min = req.itvl_min;
-    params.itvl_max = req.itvl_max;
-    params.latency = req.slave_latency;
-    params.supervision_timeout = req.timeout_multiplier;
-    params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
-    params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
+    if (rc != 0) {
+        return rc;
+    }
 
-    rc = ble_gap_conn_rx_l2cap_update_req(conn, &params);
+    /* Ask application if slave's connection parameters are acceptable. */
+    rc = ble_gap_conn_rx_l2cap_update_req(conn_handle, &params);
     if (rc == 0) {
         /* Application agrees to accept parameters; schedule update. */
-        rc = ble_gap_conn_update_params(conn->bhc_handle, &params);
+        rc = ble_gap_conn_update_params(conn_handle, &params);
         if (rc != 0) {
             return rc;
         }
@@ -464,18 +530,22 @@ ble_l2cap_sig_update_req_rx(struct ble_hs_conn *conn,
     }
 
     /* Send L2CAP response. */
-    rc = ble_l2cap_sig_update_rsp_tx(conn, chan, hdr->identifier,
-                                     l2cap_result);
-    if (rc != 0) {
-        return rc;
+    ble_hs_conn_lock();
+    rc = ble_l2cap_sig_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_l2cap_sig_update_rsp_tx(conn, chan, hdr->identifier,
+                                         l2cap_result);
     }
+    ble_hs_conn_unlock();
 
-    return 0;
+    return rc;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
-ble_l2cap_sig_update_rsp_rx(struct ble_hs_conn *conn,
-                            struct ble_l2cap_chan *chan,
+ble_l2cap_sig_update_rsp_rx(uint16_t conn_handle,
                             struct ble_l2cap_sig_hdr *hdr,
                             struct os_mbuf **om)
 {
@@ -485,7 +555,7 @@ ble_l2cap_sig_update_rsp_rx(struct ble_hs_conn *conn,
     int cb_status;
     int rc;
 
-    proc = ble_l2cap_sig_proc_find(conn->bhc_handle,
+    proc = ble_l2cap_sig_proc_find(conn_handle,
                                    BLE_L2CAP_SIG_PROC_OP_UPDATE,
                                    hdr->identifier, 1, &prev);
     if (proc ==  NULL) {
@@ -531,6 +601,9 @@ done:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_l2cap_sig_update_kick(struct ble_l2cap_sig_proc *proc)
 {
@@ -539,36 +612,43 @@ ble_l2cap_sig_update_kick(struct ble_l2cap_sig_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         /* Not connected; abort the proedure. */
-        ble_l2cap_sig_update_call_cb(proc, BLE_HS_ENOTCONN);
-        return BLE_HS_EDONE;
-    }
+        rc = BLE_HS_ENOTCONN;
+    } else if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) {
+        /* Only the slave can initiate the L2CAP connection update
+         * procedure.
+         */
+        rc = BLE_HS_EINVAL;
+    } else {
+        chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_SIG);
+        assert(chan != NULL);
 
-    /* Only the slave can initiate the L2CAP connection update procedure. */
-    if (conn->bhc_flags & BLE_HS_CONN_F_MASTER) {
-        ble_l2cap_sig_update_call_cb(proc, BLE_HS_EINVAL);
-        return BLE_HS_EDONE;
-    }
+        proc->id = ble_l2cap_sig_next_id();
+        req.itvl_min = proc->update.params.itvl_min;
+        req.itvl_max = proc->update.params.itvl_max;
+        req.slave_latency = proc->update.params.slave_latency;
+        req.timeout_multiplier = proc->update.params.timeout_multiplier;
 
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_SIG);
-    assert(chan != NULL);
+        rc = ble_l2cap_sig_update_req_tx(conn, chan, proc->id, &req);
+    }
 
-    proc->id = ble_l2cap_sig_next_id();
-    req.itvl_min = proc->update.params.itvl_min;
-    req.itvl_max = proc->update.params.itvl_max;
-    req.slave_latency = proc->update.params.slave_latency;
-    req.timeout_multiplier = proc->update.params.timeout_multiplier;
+    ble_hs_conn_unlock();
 
-    rc = ble_l2cap_sig_update_req_tx(conn, chan, proc->id, &req);
-    if (rc != 0) {
-        return rc;
+    if (rc == 0) {
+        return 0;
+    } else {
+        ble_l2cap_sig_update_call_cb(proc, rc);
+        return BLE_HS_EDONE;
     }
-
-    return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_l2cap_sig_update(uint16_t conn_handle,
                      struct ble_l2cap_sig_update_params *params,
@@ -592,12 +672,16 @@ ble_l2cap_sig_update(uint16_t conn_handle,
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
-ble_l2cap_sig_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                 struct os_mbuf **om)
+ble_l2cap_sig_rx(uint16_t conn_handle, struct os_mbuf **om)
 {
-    ble_l2cap_sig_rx_fn *rx_cb;
     struct ble_l2cap_sig_hdr hdr;
+    struct ble_l2cap_chan *chan;
+    ble_l2cap_sig_rx_fn *rx_cb;
+    struct ble_hs_conn *conn;
     int rc;
 
     console_printf("L2CAP - rxed signalling msg: ");
@@ -616,21 +700,30 @@ ble_l2cap_sig_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
     os_mbuf_adj(*om, BLE_L2CAP_SIG_HDR_SZ);
 
     if (OS_MBUF_PKTLEN(*om) != hdr.length) {
-        /* XXX: Send reject? */
         return BLE_HS_EBADDATA;
     }
 
     rx_cb = ble_l2cap_sig_dispatch_get(hdr.op);
     if (rx_cb == NULL) {
-        ble_l2cap_sig_reject_tx(conn, chan, hdr.identifier,
-                                BLE_L2CAP_ERR_CMD_NOT_UNDERSTOOD);
-        return BLE_HS_ENOTSUP;
+        ble_hs_conn_lock();
+        rc = ble_l2cap_sig_conn_chan_find(conn_handle, &conn, &chan);
+        if (rc == 0) {
+            ble_l2cap_sig_reject_tx(conn, chan, hdr.identifier,
+                                    BLE_L2CAP_ERR_CMD_NOT_UNDERSTOOD);
+        }
+        ble_hs_conn_unlock();
+
+        rc = BLE_HS_ENOTSUP;
+    } else {
+        rc = rx_cb(conn_handle, &hdr, om);
     }
 
-    rc = rx_cb(conn, chan, &hdr, om);
     return rc;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 struct ble_l2cap_chan *
 ble_l2cap_sig_create_chan(void)
 {
@@ -659,6 +752,8 @@ ble_l2cap_sig_create_chan(void)
  * seconds are aborted, and their corresponding connection is terminated.
  *
  * Called by the heartbeat timer; executed every second.
+ *
+ * Lock restrictions: None.
  */
 static void
 ble_l2cap_sig_heartbeat(void *unused)
@@ -689,6 +784,9 @@ ble_l2cap_sig_heartbeat(void *unused)
     assert(rc == 0);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_l2cap_sig_wakeup(void)
 {
@@ -740,6 +838,9 @@ ble_l2cap_sig_wakeup(void)
     ble_l2cap_sig_assert_sanity();
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_l2cap_sig_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/host_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci.c b/net/nimble/host/src/host_hci.c
index 863125f..4401405 100644
--- a/net/nimble/host/src/host_hci.c
+++ b/net/nimble/host/src/host_hci.c
@@ -662,39 +662,52 @@ host_hci_data_hdr_strip(struct os_mbuf *om, struct hci_data_hdr *hdr)
 int
 host_hci_data_rx(struct os_mbuf *om)
 {
-    struct ble_hs_conn *connection;
     struct hci_data_hdr hci_hdr;
+    struct ble_hs_conn *conn;
+    ble_l2cap_rx_fn *rx_cb;
+    struct os_mbuf *rx_buf;
     uint16_t handle;
     int rc;
 
     rc = host_hci_data_hdr_strip(om, &hci_hdr);
-    if (rc != 0) {
-        goto done;
+    if (rc == 0) {
+        console_printf("host_hci_data_rx(): handle=%u pb=%x len=%u data=",
+                       BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc), 
+                       BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc), 
+                       hci_hdr.hdh_len);
+        ble_hs_misc_log_mbuf(om);
+
+        if (hci_hdr.hdh_len != OS_MBUF_PKTHDR(om)->omp_len) {
+            rc = BLE_HS_EBADDATA;
+        } else {
+            handle = BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc);
+
+            ble_hs_conn_lock();
+
+            conn = ble_hs_conn_find(handle);
+            if (conn == NULL) {
+                rc = BLE_HS_ENOTCONN;
+            } else {
+                rc = ble_l2cap_rx(conn, &hci_hdr, om, &rx_cb, &rx_buf);
+                om = NULL;
+            }
+
+            ble_hs_conn_unlock();
+        }
     }
 
-    console_printf("host_hci_data_rx(): handle=%u pb=%x len=%u data=",
-                   BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc), 
-                   BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc), 
-                   hci_hdr.hdh_len);
-    ble_hs_misc_log_mbuf(om);
-
-    if (hci_hdr.hdh_len != OS_MBUF_PKTHDR(om)->omp_len) {
-        rc = BLE_HS_EMSGSIZE;
-        goto done;
-    }
+    os_mbuf_free_chain(om);
 
-    handle = BLE_HCI_DATA_HANDLE(hci_hdr.hdh_handle_pb_bc);
-    connection = ble_hs_conn_find(handle);
-    if (connection == NULL) {
-        rc = BLE_HS_ENOTCONN;
-        goto done;
+    if (rc == 0) {
+        assert(rx_cb != NULL);
+        assert(rx_buf != NULL);
+        rc = rx_cb(handle, &rx_buf);
+        os_mbuf_free_chain(rx_buf);
+    } else if (rc == BLE_HS_EAGAIN) {
+        /* More fragments on the way. */
+        rc = 0;
     }
 
-    rc = ble_l2cap_rx(connection, &hci_hdr, om);
-    om = NULL;
-
-done:
-    os_mbuf_free_chain(om);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 8344086..e727d10 100644
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ b/net/nimble/host/src/test/ble_gap_test.c
@@ -99,7 +99,7 @@ ble_gap_test_util_connect_cb(int event, int status,
     case BLE_GAP_EVENT_ADV_STOP_FAILURE:
         ble_gap_test_conn_event = event;
         ble_gap_test_conn_status = status;
-        ble_gap_test_conn_desc = ctxt->desc;
+        ble_gap_test_conn_desc = *ctxt->desc;
         ble_gap_test_conn_arg = arg;
         break;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 902653c..dcc4991 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ b/net/nimble/host/src/test/ble_hs_test_util.c
@@ -156,6 +156,29 @@ ble_hs_test_util_rx_le_ack(uint16_t ocf, uint8_t status)
 }
 
 int
+ble_hs_test_util_l2cap_rx(struct ble_hs_conn *conn,
+                          struct hci_data_hdr *hci_hdr,
+                          struct os_mbuf *om)
+{
+    ble_l2cap_rx_fn *rx_cb;
+    struct os_mbuf *rx_buf;
+    int rc;
+
+    rc = ble_l2cap_rx(conn, hci_hdr, om, &rx_cb, &rx_buf);
+    if (rc == 0) {
+        assert(rx_cb != NULL);
+        assert(rx_buf != NULL);
+        rc = rx_cb(conn->bhc_handle, &rx_buf);
+        os_mbuf_free_chain(rx_buf);
+    } else if (rc == BLE_HS_EAGAIN) {
+        /* More fragments on the way. */
+        rc = 0;
+    }
+
+    return rc;
+}
+
+int
 ble_hs_test_util_l2cap_rx_payload_flat(struct ble_hs_conn *conn,
                                        struct ble_l2cap_chan *chan,
                                        const void *data, int len)
@@ -180,8 +203,7 @@ ble_hs_test_util_l2cap_rx_payload_flat(struct ble_hs_conn *conn,
                                    BLE_HCI_PB_FIRST_FLUSH, 0);
     hci_hdr.hdh_len = OS_MBUF_PKTHDR(om)->omp_len;
 
-    rc = ble_l2cap_rx(conn, &hci_hdr, om);
-
+    rc = ble_hs_test_util_l2cap_rx(conn, &hci_hdr, om);
     return rc;
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/test/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.h b/net/nimble/host/src/test/ble_hs_test_util.h
index 6a11151..55b4707 100644
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ b/net/nimble/host/src/test/ble_hs_test_util.h
@@ -44,6 +44,9 @@ void ble_hs_test_util_rx_ack(uint16_t opcode, uint8_t status);
 void ble_hs_test_util_rx_ack_param(uint16_t opcode, uint8_t status,
                                    void *param, int param_len);
 void ble_hs_test_util_rx_le_ack(uint16_t ocf, uint8_t status);
+int ble_hs_test_util_l2cap_rx(struct ble_hs_conn *conn,
+                              struct hci_data_hdr *hci_hdr,
+                              struct os_mbuf *om);
 void ble_hs_test_util_rx_le_ack_param(uint16_t ocf, uint8_t status,
                                       void *param, int param_len);
 int ble_hs_test_util_l2cap_rx_payload_flat(struct ble_hs_conn *conn,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/test/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_l2cap_test.c b/net/nimble/host/src/test/ble_l2cap_test.c
index 52aa836..07ce5c3 100644
--- a/net/nimble/host/src/test/ble_l2cap_test.c
+++ b/net/nimble/host/src/test/ble_l2cap_test.c
@@ -89,7 +89,7 @@ ble_l2cap_test_util_rx_update_req(struct ble_hs_conn *conn, uint8_t id,
         v, BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_REQ_SZ, &sig_hdr, &req);
     TEST_ASSERT_FATAL(rc == 0);
 
-    rc = ble_l2cap_rx(conn, &hci_hdr, om);
+    rc = ble_hs_test_util_l2cap_rx(conn, &hci_hdr, om);
     TEST_ASSERT_FATAL(rc == 0);
 }
 
@@ -128,7 +128,7 @@ ble_l2cap_test_util_rx_update_rsp(struct ble_hs_conn *conn,
         v, BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_RSP_SZ, &sig_hdr, &rsp);
     TEST_ASSERT_FATAL(rc == 0);
 
-    rc = ble_l2cap_rx(conn, &hci_hdr, om);
+    rc = ble_hs_test_util_l2cap_rx(conn, &hci_hdr, om);
     return rc;
 }
 
@@ -215,8 +215,7 @@ ble_l2cap_test_util_verify_tx_update_conn(
 }
 
 static int
-ble_l2cap_test_util_dummy_rx(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan, struct os_mbuf **om)
+ble_l2cap_test_util_dummy_rx(uint16_t conn_handle, struct os_mbuf **om)
 {
     return 0;
 }
@@ -266,7 +265,7 @@ ble_l2cap_test_util_rx_first_frag(struct ble_hs_conn *conn,
     hci_len = sizeof hci_hdr + l2cap_frag_len;
     hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(conn->bhc_handle,
                                           BLE_HCI_PB_FIRST_FLUSH, hci_len);
-    rc = ble_l2cap_rx(conn, &hci_hdr, om);
+    rc = ble_hs_test_util_l2cap_rx(conn, &hci_hdr, om);
     return rc;
 }
 
@@ -286,7 +285,7 @@ ble_l2cap_test_util_rx_next_frag(struct ble_hs_conn *conn, uint16_t hci_len)
 
     hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(conn->bhc_handle,
                                           BLE_HCI_PB_MIDDLE, hci_len);
-    rc = ble_l2cap_rx(conn, &hci_hdr, om);
+    rc = ble_hs_test_util_l2cap_rx(conn, &hci_hdr, om);
     return rc;
 }
 
@@ -339,7 +338,7 @@ TEST_CASE(ble_l2cap_test_case_bad_header)
     ble_l2cap_test_util_init();
 
     conn = ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                      NULL, NULL);
+                                           NULL, NULL);
 
     rc = ble_l2cap_test_util_rx_first_frag(conn, 14, 1234, 10);
     TEST_ASSERT(rc == BLE_HS_ENOENT);
@@ -370,7 +369,7 @@ TEST_CASE(ble_l2cap_test_case_frag_single)
     om = ble_l2cap_prepend_hdr(om, 0, 5);
     TEST_ASSERT_FATAL(om != NULL);
 
-    rc = ble_l2cap_rx(conn, &hci_hdr, om);
+    rc = ble_hs_test_util_l2cap_rx(conn, &hci_hdr, om);
     TEST_ASSERT(rc == BLE_HS_EBADDATA);
 
     /*** Packet consisting of three fragments. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/test/ble_os_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_os_test.c b/net/nimble/host/src/test/ble_os_test.c
index 7fede98..8bbc155 100644
--- a/net/nimble/host/src/test/ble_os_test.c
+++ b/net/nimble/host/src/test/ble_os_test.c
@@ -75,9 +75,9 @@ ble_gap_direct_connect_test_connect_cb(int event, int status,
 
     TEST_ASSERT(event == BLE_GAP_EVENT_CONN);
     TEST_ASSERT(status == 0);
-    TEST_ASSERT(ctxt->desc.conn_handle == 2);
-    TEST_ASSERT(ctxt->desc.peer_addr_type == BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(memcmp(ctxt->desc.peer_addr, ble_os_test_peer_addr, 6) == 0);
+    TEST_ASSERT(ctxt->desc->conn_handle == 2);
+    TEST_ASSERT(ctxt->desc->peer_addr_type == BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(memcmp(ctxt->desc->peer_addr, ble_os_test_peer_addr, 6) == 0);
 
     return 0;
 }
@@ -241,7 +241,7 @@ ble_gap_terminate_cb(int event, int status,
     }
 
     disconn_handle = arg;
-    *disconn_handle = ctxt->desc.conn_handle;
+    *disconn_handle = ctxt->desc->conn_handle;
 
     return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/project/bleshell/src/main.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/main.c b/project/bleshell/src/main.c
index 221b496..e23ebd7 100755
--- a/project/bleshell/src/main.c
+++ b/project/bleshell/src/main.c
@@ -838,18 +838,18 @@ bleshell_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
     switch (event) {
     case BLE_GAP_EVENT_CONN:
         bleshell_printf("connection complete; status=%d ", status);
-        bleshell_print_conn_desc(&ctxt->desc);
+        bleshell_print_conn_desc(ctxt->desc);
         bleshell_printf("\n");
 
         if (status == 0) {
-            bleshell_conn_add(&ctxt->desc);
+            bleshell_conn_add(ctxt->desc);
         } else {
-            if (ctxt->desc.conn_handle == BLE_HS_CONN_HANDLE_NONE) {
+            if (ctxt->desc->conn_handle == BLE_HS_CONN_HANDLE_NONE) {
                 if (status == BLE_HS_HCI_ERR(BLE_ERR_UNK_CONN_ID)) {
                     bleshell_printf("connection procedure cancelled.\n");
                 }
             } else {
-                conn_idx = bleshell_conn_find_idx(ctxt->desc.conn_handle);
+                conn_idx = bleshell_conn_find_idx(ctxt->desc->conn_handle);
                 if (conn_idx == -1) {
                     bleshell_printf("UNKNOWN CONNECTION\n");
                 } else {
@@ -862,7 +862,7 @@ bleshell_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
 
     case BLE_GAP_EVENT_CONN_UPDATED:
         bleshell_printf("connection updated; status=%d ", status);
-        bleshell_print_conn_desc(&ctxt->desc);
+        bleshell_print_conn_desc(ctxt->desc);
         bleshell_printf("\n");
         break;
 


[3/5] incubator-mynewt-larva git commit: Protect connection list with a mutex

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 f51b81a..34fb537 100644
--- a/net/nimble/host/src/ble_gap_conn.c
+++ b/net/nimble/host/src/ble_gap_conn.c
@@ -97,13 +97,13 @@ static const struct ble_gap_conn_crt_params ble_gap_conn_params_dflt = {
 };
 
 static const struct hci_adv_params ble_gap_adv_params_dflt = {
-        .adv_itvl_min = 0,
-        .adv_itvl_max = 0,
-        .adv_type = BLE_HCI_ADV_TYPE_ADV_IND,
-        .own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC,
-        .peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC,
-        .adv_channel_map = BLE_HCI_ADV_CHANMASK_DEF,
-        .adv_filter_policy = BLE_HCI_ADV_FILT_DEF,
+    .adv_itvl_min = 0,
+    .adv_itvl_max = 0,
+    .adv_type = BLE_HCI_ADV_TYPE_ADV_IND,
+    .own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC,
+    .peer_addr_type = BLE_HCI_ADV_PEER_ADDR_PUBLIC,
+    .adv_channel_map = BLE_HCI_ADV_CHANMASK_DEF,
+    .adv_filter_policy = BLE_HCI_ADV_FILT_DEF,
 };
 
 /**
@@ -203,101 +203,37 @@ static ble_hci_sched_tx_fn * const ble_gap_conn_dispatch_adv_dir[] = {
 static struct os_callout_func ble_gap_conn_master_timer;
 static struct os_callout_func ble_gap_conn_slave_timer;
 
+struct ble_gap_conn_snap {
+    struct ble_gap_conn_desc desc;
+    ble_gap_conn_fn *cb;
+    void *cb_arg;
+};
+
 /*****************************************************************************
- * $misc                                                                     *
+ * $callbacks                                                                *
  *****************************************************************************/
 
-static struct ble_gap_conn_update_entry *
-ble_gap_conn_update_find(uint16_t conn_handle)
-{
-    struct ble_gap_conn_update_entry *entry;
-
-    SLIST_FOREACH(entry, &ble_gap_conn_update_entries, next) {
-        if (entry->conn_handle == conn_handle) {
-            return entry;
-        }
-    }
-
-    return NULL;
-}
-
-static struct ble_gap_conn_update_entry *
-ble_gap_conn_update_entry_alloc(uint16_t conn_handle,
-                                struct ble_gap_conn_upd_params *params,
-                                int state)
-{
-    struct ble_gap_conn_update_entry *entry;
-
-#ifdef BLE_HS_DEBUG
-    assert(ble_gap_conn_update_find(conn_handle) == NULL);
-#endif
-
-    entry = os_memblock_get(&ble_gap_conn_update_pool);
-    if (entry == NULL) {
-        return NULL;
-    }
-
-    memset(entry, 0, sizeof *entry);
-    entry->conn_handle = conn_handle;
-    entry->params = *params;
-    entry->state = state;
-
-    SLIST_INSERT_HEAD(&ble_gap_conn_update_entries, entry, next);
-
-    return entry;
-}
-
-static void
-ble_gap_conn_update_entry_free(struct ble_gap_conn_update_entry *entry)
-{
-    int rc;
-
-    rc = os_memblock_put(&ble_gap_conn_update_pool, entry);
-    assert(rc == 0);
-}
-
-static void
-ble_gap_conn_fill_desc(struct ble_hs_conn *conn,
-                       struct ble_gap_conn_desc *desc)
-{
-    desc->conn_handle = conn->bhc_handle;
-    desc->peer_addr_type = conn->bhc_addr_type;
-    memcpy(desc->peer_addr, conn->bhc_addr, sizeof desc->peer_addr);
-    desc->conn_itvl = conn->bhc_itvl;
-    desc->conn_latency = conn->bhc_latency;
-    desc->supervision_timeout = conn->bhc_supervision_timeout;
-}
-
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
-ble_gap_conn_call_conn_cb(int event, int status, struct ble_hs_conn *conn,
+ble_gap_conn_call_conn_cb(int event, int status,
+                          struct ble_gap_conn_snap *snap,
                           struct ble_gap_conn_upd_params *self_params,
                           struct ble_gap_conn_upd_params *peer_params)
 {
     struct ble_gap_conn_ctxt ctxt;
-    ble_gap_conn_fn *cb;
-    void *cb_arg;
     int rc;
 
-    memset(&ctxt, 0, sizeof ctxt);
-
-    if (conn != NULL) {
-        ble_gap_conn_fill_desc(conn, &ctxt.desc);
-        cb = conn->bhc_cb;
-        cb_arg = conn->bhc_cb_arg;
-    } else {
-        ctxt.desc.conn_handle = BLE_HS_CONN_HANDLE_NONE;
-        ctxt.desc.peer_addr_type = ble_gap_conn_master.conn.addr_type;
-        memcpy(ctxt.desc.peer_addr, ble_gap_conn_master.conn.addr,
-               sizeof ctxt.desc.peer_addr);
-        cb = ble_gap_conn_master.conn.cb;
-        cb_arg = ble_gap_conn_master.conn.cb_arg;
-    }
+    assert(!ble_hs_conn_locked_by_cur_task());
 
+    memset(&ctxt, 0, sizeof ctxt);
+    ctxt.desc = &snap->desc;
     ctxt.self_params = self_params;
     ctxt.peer_params = peer_params;
 
-    if (cb != NULL) {
-        rc = cb(event, status, &ctxt, cb_arg);
+    if (snap->cb != NULL) {
+        rc = snap->cb(event, status, &ctxt, snap->cb_arg);
     } else {
         if (event == BLE_GAP_EVENT_CONN_UPDATE_REQ) {
             /* Just copy peer parameters back into reply. */
@@ -309,143 +245,221 @@ ble_gap_conn_call_conn_cb(int event, int status, struct ble_hs_conn *conn,
     return rc;
 }
 
-static void
-ble_gap_conn_call_slave_cb(int event, int status)
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
+static int
+ble_gap_conn_call_master_conn_cb(int event, int status)
 {
     struct ble_gap_conn_ctxt ctxt;
+    struct ble_gap_conn_desc desc;
+    int rc;
 
-    if (ble_gap_conn_slave.cb == NULL) {
-        return;
+    assert(!ble_hs_conn_locked_by_cur_task());
+
+    if (ble_gap_conn_master.conn.cb == NULL) {
+        return 0;
     }
 
     memset(&ctxt, 0, sizeof ctxt);
-    ctxt.desc.conn_handle = BLE_HS_CONN_HANDLE_NONE;
-    ctxt.desc.peer_addr_type = ble_gap_conn_slave.dir_addr_type;
-    memcpy(ctxt.desc.peer_addr, ble_gap_conn_slave.dir_addr,
-           sizeof ctxt.desc.peer_addr);
 
-    ble_gap_conn_slave.cb(event, status, &ctxt, ble_gap_conn_slave.cb_arg);
+    desc.conn_handle = BLE_HS_CONN_HANDLE_NONE;
+    desc.peer_addr_type = ble_gap_conn_master.conn.addr_type;
+    memcpy(desc.peer_addr, ble_gap_conn_master.conn.addr,
+           sizeof desc.peer_addr);
+    ctxt.desc = &desc;
+
+    rc = ble_gap_conn_master.conn.cb(event, status, &ctxt,
+                                     ble_gap_conn_master.conn.cb_arg);
+    return rc;
 }
 
 /**
- * Notifies the application of a connection event if a callback is configured.
- *
- * @param status                The HCI status of the connection attempt.
- * @param conn                  The connection this notification concerns;
- *                                  null if a connection was never created.
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
-ble_gap_conn_notify_connect(int status, struct ble_hs_conn *conn)
-{
-    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN, status, conn, NULL, NULL);
-}
-
-static void
-ble_gap_conn_notify_term_failure(int status, struct ble_hs_conn *conn)
-{
-    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_TERM_FAILURE, status, conn,
-                              NULL, NULL);
-}
-
-static void
-ble_gap_conn_notify_disc(struct ble_hs_adv *adv,
-                         struct ble_hs_adv_fields *fields)
+ble_gap_conn_call_master_disc_cb(int event, int status, struct ble_hs_adv *adv,
+                                 struct ble_hs_adv_fields *fields)
 {
     struct ble_gap_disc_desc desc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (ble_gap_conn_master.disc.cb == NULL) {
         return;
     }
 
-    desc.event_type = adv->event_type;
-    desc.addr_type = adv->addr_type;
-    desc.length_data = adv->length_data;
-    desc.rssi = adv->rssi;
-    memcpy(desc.addr, adv->addr, sizeof adv->addr);
-    desc.data = adv->data;
-    desc.fields = fields;
+    if (adv != NULL) {
+        desc.event_type = adv->event_type;
+        desc.addr_type = adv->addr_type;
+        desc.length_data = adv->length_data;
+        desc.rssi = adv->rssi;
+        memcpy(desc.addr, adv->addr, sizeof adv->addr);
+        desc.data = adv->data;
+        desc.fields = fields;
+    } else {
+        memset(&desc, 0, sizeof desc);
+    }
 
-    ble_gap_conn_master.disc.cb(BLE_GAP_EVENT_DISC_SUCCESS, 0, &desc,
+    ble_gap_conn_master.disc.cb(event, status, &desc,
                                 ble_gap_conn_master.disc.cb_arg);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
-ble_gap_conn_notify_master_conn_failure(int status)
+ble_gap_conn_call_slave_cb(int event, int status)
 {
-    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN, status, NULL, NULL, NULL);
+    struct ble_gap_conn_ctxt ctxt;
+    struct ble_gap_conn_desc desc;
+
+    assert(!ble_hs_conn_locked_by_cur_task());
+
+    if (ble_gap_conn_slave.cb == NULL) {
+        return;
+    }
+
+    memset(&ctxt, 0, sizeof ctxt);
+    desc.conn_handle = BLE_HS_CONN_HANDLE_NONE;
+    desc.peer_addr_type = ble_gap_conn_slave.dir_addr_type;
+    memcpy(desc.peer_addr, ble_gap_conn_slave.dir_addr,
+           sizeof desc.peer_addr);
+    ctxt.desc = &desc;
+
+    ble_gap_conn_slave.cb(event, status, &ctxt, ble_gap_conn_slave.cb_arg);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
-ble_gap_conn_notify_master_cancel_failure(int status)
+ble_gap_conn_call_wl_cb(int status)
 {
-    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CANCEL_FAILURE, status, NULL,
-                              NULL, NULL);
+    assert(!ble_hs_conn_locked_by_cur_task());
+
+    if (ble_gap_conn_wl.cb == NULL) {
+        return;
+    }
+
+    ble_gap_conn_wl.cb(status, ble_gap_conn_wl.cb_arg);
 }
 
-static void
-ble_gap_conn_notify_master_disc_failure(int status)
+/*****************************************************************************
+ * $misc                                                                     *
+ *****************************************************************************/
+
+/**
+ * Lock restrictions: None.
+ */
+static struct ble_gap_conn_update_entry *
+ble_gap_conn_update_find(uint16_t conn_handle)
 {
-    struct ble_gap_disc_desc desc;
+    struct ble_gap_conn_update_entry *entry;
 
-    if (ble_gap_conn_master.disc.cb == NULL) {
-        return;
+    SLIST_FOREACH(entry, &ble_gap_conn_update_entries, next) {
+        if (entry->conn_handle == conn_handle) {
+            return entry;
+        }
     }
 
-    memset(&desc, 0, sizeof desc);
-    ble_gap_conn_master.disc.cb(BLE_GAP_EVENT_DISC_FINISHED, status, &desc,
-                                ble_gap_conn_master.disc.cb_arg);
+    return NULL;
 }
 
-static void
-ble_gap_conn_notify_master_term_failure(int status, uint16_t conn_handle)
+/**
+ * Lock restrictions: None.
+ */
+static struct ble_gap_conn_update_entry *
+ble_gap_conn_update_entry_alloc(uint16_t conn_handle,
+                                struct ble_gap_conn_upd_params *params,
+                                int state)
 {
-    struct ble_hs_conn *conn;
+    struct ble_gap_conn_update_entry *entry;
 
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn == NULL) {
-        /* XXX: Stat. */
-        return;
+#ifdef BLE_HS_DEBUG
+    assert(ble_gap_conn_update_find(conn_handle) == NULL);
+#endif
+
+    entry = os_memblock_get(&ble_gap_conn_update_pool);
+    if (entry == NULL) {
+        return NULL;
     }
 
-    ble_gap_conn_notify_term_failure(status, conn);
+    memset(entry, 0, sizeof *entry);
+    entry->conn_handle = conn_handle;
+    entry->params = *params;
+    entry->state = state;
+
+    SLIST_INSERT_HEAD(&ble_gap_conn_update_entries, entry, next);
+
+    return entry;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static void
-ble_gap_conn_notify_adv_finished(void)
+ble_gap_conn_update_entry_free(struct ble_gap_conn_update_entry *entry)
 {
-    ble_gap_conn_call_slave_cb(BLE_GAP_EVENT_ADV_FINISHED, 0);
+    int rc;
+
+    rc = os_memblock_put(&ble_gap_conn_update_pool, entry);
+    assert(rc == 0);
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static void
-ble_gap_conn_notify_slave_adv_cancel_failure(int status)
+ble_gap_conn_fill_desc(struct ble_hs_conn *conn,
+                       struct ble_gap_conn_desc *desc)
 {
-    ble_gap_conn_call_slave_cb(BLE_GAP_EVENT_ADV_STOP_FAILURE, status);
+    desc->conn_handle = conn->bhc_handle;
+    desc->peer_addr_type = conn->bhc_addr_type;
+    memcpy(desc->peer_addr, conn->bhc_addr, sizeof desc->peer_addr);
+    desc->conn_itvl = conn->bhc_itvl;
+    desc->conn_latency = conn->bhc_latency;
+    desc->supervision_timeout = conn->bhc_supervision_timeout;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static void
-ble_gap_conn_notify_wl(int status)
+ble_gap_conn_to_snap(struct ble_hs_conn *conn, struct ble_gap_conn_snap *snap)
 {
-    if (ble_gap_conn_wl.cb == NULL) {
-        return;
-    }
-
-    ble_gap_conn_wl.cb(status, ble_gap_conn_wl.cb_arg);
+    ble_gap_conn_fill_desc(conn, &snap->desc);
+    snap->cb = conn->bhc_cb;
+    snap->cb_arg = conn->bhc_cb_arg;
 }
 
-static void
-ble_gap_conn_notify_update(struct ble_gap_conn_update_entry *entry, int status)
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
+static int
+ble_gap_conn_find_snap(uint16_t handle, struct ble_gap_conn_snap *snap)
 {
     struct ble_hs_conn *conn;
 
-    conn = ble_hs_conn_find(entry->conn_handle);
-    if (conn == NULL) {
-        return;
+    ble_hs_conn_lock();
+
+    conn = ble_hs_conn_find(handle);
+    if (conn != NULL) {
+        ble_gap_conn_to_snap(conn, snap);
     }
 
-    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN_UPDATED, status, conn, NULL,
-                              NULL);
+    ble_hs_conn_unlock();
+
+    if (conn == NULL) {
+        return BLE_HS_ENOENT;
+    } else {
+        return 0;
+    }
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_gap_conn_master_reset_state(void)
 {
@@ -453,6 +467,9 @@ ble_gap_conn_master_reset_state(void)
     ble_gap_conn_master.op = BLE_GAP_CONN_OP_NULL;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_gap_conn_slave_reset_state(void)
 {
@@ -461,9 +478,29 @@ ble_gap_conn_slave_reset_state(void)
 }
 
 /**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
+static void
+ble_gap_conn_notify_update(struct ble_gap_conn_update_entry *entry, int status)
+{
+    struct ble_gap_conn_snap snap;
+    int rc;
+
+    rc = ble_gap_conn_find_snap(entry->conn_handle, &snap);
+    if (rc != 0) {
+        return;
+    }
+
+    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN_UPDATED, status, &snap, NULL,
+                              NULL);
+}
+
+/**
  * Called when an error is encountered while the master-connection-fsm is
  * active.  Resets the state machine, clears the HCI ack callback, and notifies
  * the host task that the next hci_batch item can be processed.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gap_conn_master_failed(int status)
@@ -479,11 +516,12 @@ ble_gap_conn_master_failed(int status)
 
     switch (old_proc) {
     case BLE_GAP_CONN_OP_M_DISC:
-        ble_gap_conn_notify_master_disc_failure(status);
+        ble_gap_conn_call_master_disc_cb(BLE_GAP_EVENT_DISC_FINISHED, status,
+                                         NULL, NULL);
         break;
 
     case BLE_GAP_CONN_OP_M_CONN:
-        ble_gap_conn_notify_master_conn_failure(status);
+        ble_gap_conn_call_master_conn_cb(BLE_GAP_EVENT_CONN, status);
         break;
 
     default:
@@ -494,6 +532,8 @@ ble_gap_conn_master_failed(int status)
 /**
  * Called when an error is encountered while the slave-connection-fsm is
  * active.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gap_conn_slave_failed(int event, int status)
@@ -502,13 +542,19 @@ ble_gap_conn_slave_failed(int event, int status)
     ble_gap_conn_slave_reset_state();
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_wl_failed(int status)
 {
     ble_gap_conn_wl.op = BLE_GAP_CONN_OP_NULL;
-    ble_gap_conn_notify_wl(status);
+    ble_gap_conn_call_wl_cb(status);
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_gap_conn_update_entry_remove_free(struct ble_gap_conn_update_entry *entry)
 {
@@ -517,6 +563,9 @@ ble_gap_conn_update_entry_remove_free(struct ble_gap_conn_update_entry *entry)
     ble_gap_conn_update_entry_free(entry);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_update_failed(struct ble_gap_conn_update_entry *entry, int status)
 {
@@ -524,6 +573,9 @@ ble_gap_conn_update_failed(struct ble_gap_conn_update_entry *entry, int status)
     ble_gap_conn_update_entry_remove_free(entry);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_connection_broken(uint16_t conn_handle)
 {
@@ -537,56 +589,84 @@ ble_gap_conn_connection_broken(uint16_t conn_handle)
     ble_gattc_connection_broken(conn_handle);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_gap_conn_rx_disconn_complete(struct hci_disconn_complete *evt)
 {
+    struct ble_gap_conn_snap snap;
     struct ble_hs_conn *conn;
+    int rc;
 
     if (evt->status == 0) {
         /* Find the connection that this event refers to. */
+        ble_hs_conn_lock();
         conn = ble_hs_conn_find(evt->connection_handle);
-        if (conn == NULL) {
-            return;
+        if (conn != NULL) {
+            ble_gap_conn_to_snap(conn, &snap);
+
+            ble_gap_conn_connection_broken(evt->connection_handle);
+            ble_hs_conn_remove(conn);
+            ble_hs_conn_free(conn);
+        }
+        ble_hs_conn_unlock();
+
+        if (conn != NULL) {
+            ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN, BLE_HS_ENOTCONN,
+                                      &snap, NULL, NULL);
         }
-        ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN, BLE_HS_ENOTCONN, conn,
-                                  NULL, NULL);
-        ble_gap_conn_connection_broken(evt->connection_handle);
-        ble_hs_conn_remove(conn);
-        ble_hs_conn_free(conn);
     } else {
-        ble_gap_conn_notify_master_term_failure(BLE_HS_HCI_ERR(evt->status),
-                                                evt->connection_handle);
+        rc = ble_gap_conn_find_snap(evt->connection_handle, &snap);
+        if (rc == 0) {
+            ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_TERM_FAILURE,
+                                      BLE_HS_HCI_ERR(evt->status), &snap,
+                                      NULL, NULL);
+        }
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_gap_conn_rx_update_complete(struct hci_le_conn_upd_complete *evt)
 {
     struct ble_gap_conn_update_entry *entry;
+    struct ble_gap_conn_snap snap;
     struct ble_hs_conn *conn;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(evt->connection_handle);
-    if (conn == NULL) {
-        return;
-    }
+    if (conn != NULL) {
+        entry = ble_gap_conn_update_find(evt->connection_handle);
+        if (entry != NULL) {
+            ble_gap_conn_update_entry_remove_free(entry);
+        }
 
-    entry = ble_gap_conn_update_find(evt->connection_handle);
-    if (entry != NULL) {
-        ble_gap_conn_update_entry_remove_free(entry);
-    }
+        if (evt->status == 0) {
+            conn->bhc_itvl = evt->conn_itvl;
+            conn->bhc_latency = evt->conn_latency;
+            conn->bhc_supervision_timeout = evt->supervision_timeout;
+        }
 
-    if (evt->status == 0) {
-        conn->bhc_itvl = evt->conn_itvl;
-        conn->bhc_latency = evt->conn_latency;
-        conn->bhc_supervision_timeout = evt->supervision_timeout;
+        ble_gap_conn_to_snap(conn, &snap);
     }
 
-    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN_UPDATED,
-                              BLE_HS_HCI_ERR(evt->status), conn, NULL, NULL);
+    ble_hs_conn_unlock();
+
+    if (conn != NULL) {
+        ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN_UPDATED,
+                                  BLE_HS_HCI_ERR(evt->status), &snap,
+                                  NULL, NULL);
+    }
 }
 
 /**
  * Tells you if the BLE host is in the process of creating a master connection.
+ * 
+ * Lock restrictions: None.
  */
 int
 ble_gap_conn_master_in_progress(void)
@@ -596,6 +676,8 @@ ble_gap_conn_master_in_progress(void)
 
 /**
  * Tells you if the BLE host is in the process of creating a slave connection.
+ * 
+ * Lock restrictions: None.
  */
 int
 ble_gap_conn_slave_in_progress(void)
@@ -605,6 +687,8 @@ ble_gap_conn_slave_in_progress(void)
 
 /**
  * Tells you if the BLE host is in the process of updating a connection.
+ * 
+ * Lock restrictions: None.
  *
  * @param conn_handle           The connection to test, or
  *                                  BLE_HS_CONN_HANDLE_NONE to check all
@@ -626,6 +710,9 @@ ble_gap_conn_update_in_progress(uint16_t conn_handle)
     return entry != NULL;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gap_conn_currently_advertising(void)
 {
@@ -648,6 +735,9 @@ ble_gap_conn_currently_advertising(void)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_master_enqueue(uint8_t state, int in_progress,
                             ble_hci_sched_tx_fn *hci_tx_cb, void *cb_arg)
@@ -672,6 +762,8 @@ ble_gap_conn_master_enqueue(uint8_t state, int in_progress,
  * "connection complete" event from the controller.  If the master connection
  * FSM is in a state that can accept this event, and the peer device address is
  * valid, the master FSM is reset and success is returned.
+ * 
+ * Lock restrictions: None.
  *
  * @param addr_type             The address type of the peer; one of the
  *                                  following values:
@@ -718,6 +810,8 @@ ble_gap_conn_accept_master_conn(uint8_t addr_type, uint8_t *addr)
  * FSM is in a state that can accept this event, and the peer device address is
  * valid, the master FSM is reset and success is returned.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @param addr_type             The address type of the peer; one of the
  *                                  following values:
  *                                  o    BLE_HCI_ADV_PEER_ADDR_PUBLIC
@@ -758,6 +852,9 @@ ble_gap_conn_accept_slave_conn(uint8_t addr_type, uint8_t *addr)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_gap_conn_rx_adv_report(struct ble_hs_adv *adv)
 {
@@ -782,15 +879,19 @@ ble_gap_conn_rx_adv_report(struct ble_hs_adv *adv)
         return;
     }
 
-    ble_gap_conn_notify_disc(adv, &fields);
+    ble_gap_conn_call_master_disc_cb(BLE_GAP_EVENT_DISC_SUCCESS, 0, adv,
+                                     &fields);
 }
 
 /**
  * Processes an incoming connection-complete HCI event.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 int
 ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt)
 {
+    struct ble_gap_conn_snap snap;
     struct ble_hs_conn *conn;
     int status;
     int rc;
@@ -798,19 +899,29 @@ ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt)
     /* Determine if this event refers to a completed connection or a connection
      * in progress.
      */
+    ble_hs_conn_lock();
     conn = ble_hs_conn_find(evt->connection_handle);
 
     /* Apply the event to the existing connection if it exists. */
     if (conn != NULL) {
         /* XXX: Does this ever happen? */
         if (evt->status != 0) {
+            ble_gap_conn_to_snap(conn, &snap);
+
             ble_gap_conn_connection_broken(evt->connection_handle);
 
             ble_hs_conn_remove(conn);
-            ble_gap_conn_notify_connect(evt->status, conn);
             ble_hs_conn_free(conn);
         }
+    }
+
+    ble_hs_conn_unlock();
 
+    if (conn != NULL) {
+        if (evt->status != 0) {
+            ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN, evt->status, &snap,
+                                      NULL, NULL);
+        }
         return 0;
     }
 
@@ -879,12 +990,17 @@ ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt)
         ble_gap_conn_slave_reset_state();
     }
 
+    ble_gap_conn_to_snap(conn, &snap);
+
     ble_hs_conn_insert(conn);
-    ble_gap_conn_notify_connect(0, conn);
+    ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN, 0, &snap, NULL, NULL);
 
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_master_timer_exp(void *arg)
 {
@@ -904,6 +1020,9 @@ ble_gap_conn_master_timer_exp(void *arg)
 
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_slave_timer_exp(void *arg)
 {
@@ -915,6 +1034,9 @@ ble_gap_conn_slave_timer_exp(void *arg)
  * $white list                                                               *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gap_conn_wl_busy(void)
 {
@@ -935,6 +1057,9 @@ ble_gap_conn_wl_busy(void)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_wl_enqueue(uint8_t state, int in_progress,
                         ble_hci_sched_tx_fn *hci_tx_cb, void *cb_arg)
@@ -954,6 +1079,9 @@ ble_gap_conn_wl_enqueue(uint8_t state, int in_progress,
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_wl_ack_add(struct ble_hci_ack *ack, void *arg)
 {
@@ -972,10 +1100,13 @@ ble_gap_conn_wl_ack_add(struct ble_hci_ack *ack, void *arg)
     } else {
         /* Success. */
         ble_gap_conn_wl.op = BLE_GAP_CONN_OP_NULL;
-        ble_gap_conn_notify_wl(0);
+        ble_gap_conn_call_wl_cb(0);
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_wl_tx_add(void *arg)
 {
@@ -1000,6 +1131,9 @@ ble_gap_conn_wl_tx_add(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_wl_ack_clear(struct ble_hci_ack *ack, void *arg)
 {
@@ -1015,6 +1149,9 @@ ble_gap_conn_wl_ack_clear(struct ble_hci_ack *ack, void *arg)
                             ble_gap_conn_wl_tx_add, NULL);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_wl_tx_clear(void *arg)
 {
@@ -1034,6 +1171,9 @@ ble_gap_conn_wl_tx_clear(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 int
 ble_gap_conn_wl_set(struct ble_gap_white_entry *white_list,
                     uint8_t white_list_count, ble_gap_wl_fn *cb, void *cb_arg)
@@ -1073,18 +1213,25 @@ ble_gap_conn_wl_set(struct ble_gap_white_entry *white_list,
  * $stop advertise                                                           *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_adv_ack_disable(struct ble_hci_ack *ack, void *arg)
 {
     if (ack->bha_status == 0) {
         /* Advertising should now be aborted. */
-        ble_gap_conn_notify_adv_finished();
+        ble_gap_conn_call_slave_cb(BLE_GAP_EVENT_ADV_FINISHED, 0);
         ble_gap_conn_slave_reset_state();
     } else {
-        ble_gap_conn_notify_slave_adv_cancel_failure(ack->bha_status);
+        ble_gap_conn_call_slave_cb(BLE_GAP_EVENT_ADV_STOP_FAILURE,
+                                   ack->bha_status);
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_adv_disable_tx(void *arg)
 {
@@ -1093,13 +1240,17 @@ ble_gap_conn_adv_disable_tx(void *arg)
     ble_hci_ack_set_callback(ble_gap_conn_adv_ack_disable, NULL);
     rc = host_hci_cmd_le_set_adv_enable(0);
     if (rc != BLE_ERR_SUCCESS) {
-        ble_gap_conn_notify_slave_adv_cancel_failure(BLE_HS_HCI_ERR(rc));
+        ble_gap_conn_call_slave_cb(BLE_GAP_EVENT_ADV_STOP_FAILURE,
+                                   BLE_HS_HCI_ERR(rc));
         return 1;
     }
 
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gap_conn_adv_stop(void)
 {
@@ -1122,6 +1273,9 @@ ble_gap_conn_adv_stop(void)
  * $advertise                                                                *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_gap_conn_adv_itvls(uint8_t disc_mode, uint8_t conn_mode,
                        uint16_t *out_itvl_min, uint16_t *out_itvl_max)
@@ -1150,6 +1304,9 @@ ble_gap_conn_adv_itvls(uint8_t disc_mode, uint8_t conn_mode,
     }
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static ble_hci_sched_tx_fn *
 ble_gap_conn_adv_get_dispatch(void)
 {
@@ -1169,6 +1326,9 @@ ble_gap_conn_adv_get_dispatch(void)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_adv_next_state(void)
 {
@@ -1185,6 +1345,9 @@ ble_gap_conn_adv_next_state(void)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_adv_ack(struct ble_hci_ack *ack, void *arg)
 {
@@ -1195,6 +1358,9 @@ ble_gap_conn_adv_ack(struct ble_hci_ack *ack, void *arg)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_adv_enable_tx(void *arg)
 {
@@ -1211,6 +1377,9 @@ ble_gap_conn_adv_enable_tx(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_adv_rsp_data_tx(void *arg)
 {
@@ -1228,6 +1397,9 @@ ble_gap_conn_adv_rsp_data_tx(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_adv_data_tx(void *arg)
 {
@@ -1286,6 +1458,9 @@ ble_gap_conn_adv_data_tx(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_adv_power_ack(struct ble_hci_ack *ack, void *arg)
 {
@@ -1320,6 +1495,9 @@ ble_gap_conn_adv_power_ack(struct ble_hci_ack *ack, void *arg)
     ble_gap_conn_adv_next_state();
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_adv_power_tx(void *arg)
 {
@@ -1336,6 +1514,9 @@ ble_gap_conn_adv_power_tx(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_adv_params_tx(void *arg)
 {
@@ -1375,6 +1556,9 @@ ble_gap_conn_adv_params_tx(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_gap_conn_adv_initiate(void)
 {
@@ -1393,6 +1577,8 @@ ble_gap_conn_adv_initiate(void)
  * Enables the specified discoverable mode and connectable mode, and initiates
  * the advertising process.
  *
+ * Lock restrictions: None.
+ *
  * @param discoverable_mode     One of the following constants:
  *                                  o BLE_GAP_DISC_MODE_NON
  *                                      (non-discoverable; 3.C.9.2.2).
@@ -1499,6 +1685,9 @@ ble_gap_conn_adv_start(uint8_t discoverable_mode, uint8_t connectable_mode,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gap_conn_set_adv_fields(struct ble_hs_adv_fields *adv_fields)
 {
@@ -1518,6 +1707,9 @@ ble_gap_conn_set_adv_fields(struct ble_hs_adv_fields *adv_fields)
  * $discovery procedures                                                     *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_disc_ack_disable(struct ble_hci_ack *ack, void *arg)
 {
@@ -1531,6 +1723,9 @@ ble_gap_conn_disc_ack_disable(struct ble_hci_ack *ack, void *arg)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_disc_tx_disable(void *arg)
 {
@@ -1550,6 +1745,9 @@ ble_gap_conn_disc_tx_disable(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_disc_ack_enable(struct ble_hci_ack *ack, void *arg)
 {
@@ -1563,6 +1761,9 @@ ble_gap_conn_disc_ack_enable(struct ble_hci_ack *ack, void *arg)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_disc_tx_enable(void *arg)
 {
@@ -1581,6 +1782,9 @@ ble_gap_conn_disc_tx_enable(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_disc_ack_params(struct ble_hci_ack *ack, void *arg)
 {
@@ -1596,6 +1800,9 @@ ble_gap_conn_disc_ack_params(struct ble_hci_ack *ack, void *arg)
                                 ble_gap_conn_disc_tx_enable, NULL);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_disc_tx_params(void *arg)
 {
@@ -1623,6 +1830,8 @@ ble_gap_conn_disc_tx_params(void *arg)
  * Performs the Limited or General Discovery Procedures, as described in
  * vol. 3, part C, section 9.2.5 / 9.2.6.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      0 on success; nonzero on failure.
  */
 int
@@ -1682,6 +1891,8 @@ ble_gap_conn_disc(uint32_t duration_ms, uint8_t discovery_mode,
 /**
  * Processes an HCI acknowledgement (either command status or command complete)
  * while a master connection is being established.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gap_conn_create_ack(struct ble_hci_ack *ack, void *arg)
@@ -1697,6 +1908,9 @@ ble_gap_conn_create_ack(struct ble_hci_ack *ack, void *arg)
     ble_gap_conn_master.state = BLE_GAP_CONN_STATE_M_ACKED;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_create_tx(void *arg)
 {
@@ -1751,6 +1965,8 @@ ble_gap_conn_create_tx(void *arg)
  *                                  o BLE_HCI_CONN_PEER_ADDR_RANDOM_IDENT
  *                                  o BLE_GAP_ADDR_TYPE_WL
  * @param addr                  The address of the peer to connect to.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  *
  * @return                      0 on success; nonzero on failure.
  */
@@ -1802,46 +2018,70 @@ ble_gap_conn_initiate(int addr_type, uint8_t *addr,
  * $terminate connection procedure                                           *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_terminate_ack(struct ble_hci_ack *ack, void *arg)
 {
+    struct ble_gap_conn_snap snap;
     uint16_t handle;
+    int rc;
 
     if (ack->bha_status != 0) {
         handle = (uintptr_t)arg;
-        ble_gap_conn_notify_master_term_failure(ack->bha_status, handle);
+        rc = ble_gap_conn_find_snap(handle, &snap);
+        if (rc == 0) {
+            ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_TERM_FAILURE,
+                                      ack->bha_status, &snap, NULL, NULL);
+        }
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_terminate_tx(void *arg)
 {
+    struct ble_gap_conn_snap snap;
     uint16_t handle;
+    int status;
     int rc;
 
     handle = (uintptr_t)arg;
 
     ble_hci_ack_set_callback(ble_gap_conn_terminate_ack, arg);
 
-    rc = host_hci_cmd_disconnect(handle, BLE_ERR_REM_USER_CONN_TERM);
-    if (rc != 0) {
-        ble_gap_conn_notify_master_term_failure(rc, handle);
+    status = host_hci_cmd_disconnect(handle, BLE_ERR_REM_USER_CONN_TERM);
+    if (status != 0) {
+        rc = ble_gap_conn_find_snap(handle, &snap);
+        if (rc == 0) {
+            /* XXX: We need to notify the application when there is no
+             * connection as well.
+             */
+            ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_TERM_FAILURE,
+                                      status, &snap, NULL, NULL);
+        }
     }
 
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 int
-ble_gap_conn_terminate(uint16_t handle)
+ble_gap_conn_terminate(uint16_t conn_handle)
 {
     int rc;
 
-    if (ble_hs_conn_find(handle) == NULL) {
+    if (!ble_hs_conn_exists(conn_handle)) {
         return BLE_HS_ENOENT;
     }
 
     rc = ble_hci_sched_enqueue(ble_gap_conn_terminate_tx,
-                               (void *)(uintptr_t)handle);
+                               (void *)(uintptr_t)conn_handle);
     if (rc != 0) {
         return rc;
     }
@@ -1853,14 +2093,21 @@ ble_gap_conn_terminate(uint16_t handle)
  * $cancel                                                                   *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_cancel_ack(struct ble_hci_ack *ack, void *arg)
 {
     if (ack->bha_status != 0) {
-        ble_gap_conn_notify_master_cancel_failure(ack->bha_status);
+        ble_gap_conn_call_master_conn_cb(BLE_GAP_EVENT_CANCEL_FAILURE,
+                                    ack->bha_status);
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_cancel_tx(void *arg)
 {
@@ -1870,12 +2117,15 @@ ble_gap_conn_cancel_tx(void *arg)
 
     rc = host_hci_cmd_le_create_conn_cancel();
     if (rc != 0) {
-        ble_gap_conn_notify_master_cancel_failure(rc);
+        ble_gap_conn_call_master_conn_cb(BLE_GAP_EVENT_CANCEL_FAILURE, rc);
     }
 
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gap_conn_cancel(void)
 {
@@ -1897,6 +2147,9 @@ ble_gap_conn_cancel(void)
  * $update connection parameters                                             *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_gap_conn_param_neg_reply_ack(struct ble_hci_ack *ack, void *arg)
 {
@@ -1908,6 +2161,9 @@ ble_gap_conn_param_neg_reply_ack(struct ble_hci_ack *ack, void *arg)
     ble_gap_conn_update_entry_remove_free(entry);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_param_reply_ack(struct ble_hci_ack *ack, void *arg)
 {
@@ -1924,6 +2180,9 @@ ble_gap_conn_param_reply_ack(struct ble_hci_ack *ack, void *arg)
     }
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 void
 ble_gap_conn_rx_param_req(struct hci_le_conn_param_req *evt)
 {
@@ -1931,7 +2190,7 @@ ble_gap_conn_rx_param_req(struct hci_le_conn_param_req *evt)
     struct hci_conn_param_neg_reply neg_reply;
     struct hci_conn_param_reply pos_reply;
     struct ble_gap_conn_upd_params peer_params;
-    struct ble_hs_conn *conn;
+    struct ble_gap_conn_snap snap;
     int rc;
 
     entry = ble_gap_conn_update_find(evt->connection_handle);
@@ -1942,8 +2201,8 @@ ble_gap_conn_rx_param_req(struct hci_le_conn_param_req *evt)
         ble_gap_conn_update_entry_remove_free(entry);
     }
 
-    conn = ble_hs_conn_find(evt->connection_handle);
-    if (conn == NULL) {
+    rc = ble_gap_conn_find_snap(evt->connection_handle, &snap);
+    if (rc != 0) {
         return;
     }
 
@@ -1963,7 +2222,7 @@ ble_gap_conn_rx_param_req(struct hci_le_conn_param_req *evt)
         goto err;
     }
 
-    rc = ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN_UPDATE_REQ, 0, conn,
+    rc = ble_gap_conn_call_conn_cb(BLE_GAP_EVENT_CONN_UPDATE_REQ, 0, &snap,
                                    &entry->params, &peer_params);
     if (rc != 0) {
         goto err;
@@ -1998,6 +2257,9 @@ err:
     host_hci_cmd_le_conn_param_neg_reply(&neg_reply);
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static void
 ble_gap_conn_update_ack(struct ble_hci_ack *ack, void *arg)
 {
@@ -2015,6 +2277,9 @@ ble_gap_conn_update_ack(struct ble_hci_ack *ack, void *arg)
     entry->state = BLE_GAP_CONN_STATE_U_UPDATE_ACKED;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 static int
 ble_gap_conn_update_tx(void *arg)
 {
@@ -2044,6 +2309,9 @@ ble_gap_conn_update_tx(void *arg)
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 int
 ble_gap_conn_update_params(uint16_t conn_handle,
                            struct ble_gap_conn_upd_params *params)
@@ -2056,7 +2324,7 @@ ble_gap_conn_update_params(uint16_t conn_handle,
         return BLE_HS_EALREADY;
     }
 
-    if (ble_hs_conn_find(conn_handle) == NULL) {
+    if (!ble_hs_conn_exists(conn_handle)) {
         return BLE_HS_ENOENT;
     }
 
@@ -2080,19 +2348,27 @@ ble_gap_conn_update_params(uint16_t conn_handle,
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 int
-ble_gap_conn_rx_l2cap_update_req(struct ble_hs_conn *conn,
+ble_gap_conn_rx_l2cap_update_req(uint16_t conn_handle,
                                  struct ble_gap_conn_upd_params *params)
 {
     struct ble_gap_conn_ctxt ctxt;
+    struct ble_gap_conn_snap snap;
     int rc;
 
-    if (conn->bhc_cb != NULL) {
-        ble_gap_conn_fill_desc(conn, &ctxt.desc);
+    rc = ble_gap_conn_find_snap(conn_handle, &snap);
+    if (rc != 0) {
+        return rc;
+    }
+
+    if (snap.cb != NULL) {
+        ctxt.desc = &snap.desc;
         ctxt.peer_params = params;
         ctxt.self_params = NULL;
-        rc = conn->bhc_cb(BLE_GAP_EVENT_L2CAP_UPDATE_REQ, 0, &ctxt,
-                          conn->bhc_cb_arg);
+        rc = snap.cb(BLE_GAP_EVENT_L2CAP_UPDATE_REQ, 0, &ctxt, snap.cb_arg);
     } else {
         rc = 0;
     }
@@ -2104,11 +2380,17 @@ ble_gap_conn_rx_l2cap_update_req(struct ble_hs_conn *conn,
  * $init                                                                     *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: None.
+ */
 static void
 ble_gap_conn_free_mem(void)
 {
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_gap_conn_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_gap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap_priv.h b/net/nimble/host/src/ble_gap_priv.h
index bcea4a1..19d304a 100644
--- a/net/nimble/host/src/ble_gap_priv.h
+++ b/net/nimble/host/src/ble_gap_priv.h
@@ -34,7 +34,7 @@ int ble_gap_conn_rx_conn_complete(struct hci_le_conn_complete *evt);
 void ble_gap_conn_rx_disconn_complete(struct hci_disconn_complete *evt);
 void ble_gap_conn_rx_update_complete(struct hci_le_conn_upd_complete *evt);
 void ble_gap_conn_rx_param_req(struct hci_le_conn_param_req *evt);
-int ble_gap_conn_rx_l2cap_update_req(struct ble_hs_conn *conn,
+int ble_gap_conn_rx_l2cap_update_req(uint16_t conn_handle,
                                      struct ble_gap_conn_upd_params *params);
 int ble_gap_conn_master_in_progress(void);
 int ble_gap_conn_slave_in_progress(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_gatt_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatt_priv.h b/net/nimble/host/src/ble_gatt_priv.h
index e744e7b..d76dee9 100644
--- a/net/nimble/host/src/ble_gatt_priv.h
+++ b/net/nimble/host/src/ble_gatt_priv.h
@@ -48,31 +48,31 @@ int ble_gattc_notify(struct ble_hs_conn *conn, uint16_t chr_val_handle);
 
 void ble_gattc_rx_err(uint16_t conn_handle, struct ble_att_error_rsp *rsp);
 void ble_gattc_wakeup(void);
-void ble_gattc_rx_mtu(struct ble_hs_conn *conn, int status, uint16_t chan_mtu);
-void ble_gattc_rx_read_type_adata(struct ble_hs_conn *conn,
+void ble_gattc_rx_mtu(uint16_t conn_handle, int status, uint16_t chan_mtu);
+void ble_gattc_rx_read_type_adata(uint16_t conn_handle,
                                   struct ble_att_read_type_adata *adata);
-void ble_gattc_rx_read_type_complete(struct ble_hs_conn *conn, int status);
-void ble_gattc_rx_read_rsp(struct ble_hs_conn *conn, int status, void *value,
+void ble_gattc_rx_read_type_complete(uint16_t conn_handle, int status);
+void ble_gattc_rx_read_rsp(uint16_t conn_handle, int status, void *value,
                            int value_len);
-void ble_gattc_rx_read_blob_rsp(struct ble_hs_conn *conn, int status,
+void ble_gattc_rx_read_blob_rsp(uint16_t conn_handle, int status,
                                 void *value, int value_len);
-void ble_gattc_rx_read_mult_rsp(struct ble_hs_conn *conn, int status,
+void ble_gattc_rx_read_mult_rsp(uint16_t conn_handle, int status,
                                 void *value, int value_len);
 void ble_gattc_rx_read_group_type_adata(
-    struct ble_hs_conn *conn, struct ble_att_read_group_type_adata *adata);
-void ble_gattc_rx_read_group_type_complete(struct ble_hs_conn *conn, int rc);
+    uint16_t conn_handle, struct ble_att_read_group_type_adata *adata);
+void ble_gattc_rx_read_group_type_complete(uint16_t conn_handle, int rc);
 void ble_gattc_rx_find_type_value_hinfo(
-    struct ble_hs_conn *conn, struct ble_att_find_type_value_hinfo *hinfo);
-void ble_gattc_rx_find_type_value_complete(struct ble_hs_conn *conn, int rc);
-void ble_gattc_rx_write_rsp(struct ble_hs_conn *conn);
-void ble_gattc_rx_prep_write_rsp(struct ble_hs_conn *conn, int status,
+    uint16_t conn_handle, struct ble_att_find_type_value_hinfo *hinfo);
+void ble_gattc_rx_find_type_value_complete(uint16_t conn_handle, int status);
+void ble_gattc_rx_write_rsp(uint16_t conn_handle);
+void 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);
-void ble_gattc_rx_exec_write_rsp(struct ble_hs_conn *conn, int status);
-void ble_gattc_rx_indicate_rsp(struct ble_hs_conn *conn);
-void ble_gattc_rx_find_info_idata(struct ble_hs_conn *conn,
+void ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status);
+void ble_gattc_rx_indicate_rsp(uint16_t conn_handle);
+void ble_gattc_rx_find_info_idata(uint16_t conn_handle,
                                   struct ble_att_find_info_idata *idata);
-void ble_gattc_rx_find_info_complete(struct ble_hs_conn *conn, int status);
+void ble_gattc_rx_find_info_complete(uint16_t conn_handle, int status);
 void ble_gattc_connection_txable(uint16_t conn_handle);
 void ble_gattc_connection_broken(uint16_t conn_handle);
 



[5/5] incubator-mynewt-larva git commit: Bring centtest and prphtest projects up to date.

Posted by cc...@apache.org.
Bring centtest and prphtest projects up to date.


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

Branch: refs/heads/master
Commit: da8f3e03883e693aecc8cc706f8f1afaffc10f6a
Parents: cf46c07
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Feb 1 19:39:02 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Feb 1 19:41:01 2016 -0800

----------------------------------------------------------------------
 project/centtest/src/main.c | 13 ++++++-------
 project/prphtest/src/main.c | 11 ++++++-----
 2 files changed, 12 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/da8f3e03/project/centtest/src/main.c
----------------------------------------------------------------------
diff --git a/project/centtest/src/main.c b/project/centtest/src/main.c
index 4134b19..c4f3372 100755
--- a/project/centtest/src/main.c
+++ b/project/centtest/src/main.c
@@ -187,13 +187,13 @@ centtest_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
     case BLE_GAP_EVENT_CONN:
         console_printf("connection complete; handle=%d status=%d "
                        "peer_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
-                       ctxt->desc.conn_handle, status,
-                       ctxt->desc.peer_addr[0], ctxt->desc.peer_addr[1],
-                       ctxt->desc.peer_addr[2], ctxt->desc.peer_addr[3],
-                       ctxt->desc.peer_addr[4], ctxt->desc.peer_addr[5]);
+                       ctxt->desc->conn_handle, status,
+                       ctxt->desc->peer_addr[0], ctxt->desc->peer_addr[1],
+                       ctxt->desc->peer_addr[2], ctxt->desc->peer_addr[3],
+                       ctxt->desc->peer_addr[4], ctxt->desc->peer_addr[5]);
 
         if (status == 0) {
-            ble_gattc_disc_all_svcs(ctxt->desc.conn_handle,
+            ble_gattc_disc_all_svcs(ctxt->desc->conn_handle,
                                     centtest_on_disc_s, NULL);
         }
 
@@ -225,9 +225,8 @@ centtest_task_handler(void *arg)
     g_centtest_state = 0;
     g_next_os_time = os_time_get();
     
-    //rc = ble_gap_conn_disc(20000, BLE_GAP_DISC_MODE_GEN);
     rc = ble_gap_conn_initiate(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
-                               centtest_slv_addr,
+                               centtest_slv_addr, NULL,
                                centtest_on_connect, NULL);
     assert(rc == 0);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/da8f3e03/project/prphtest/src/main.c
----------------------------------------------------------------------
diff --git a/project/prphtest/src/main.c b/project/prphtest/src/main.c
index b66fb6b..8a8dd1a 100755
--- a/project/prphtest/src/main.c
+++ b/project/prphtest/src/main.c
@@ -30,6 +30,7 @@
 #include "host/ble_att.h"
 #include "host/ble_gap.h"
 #include "host/ble_gatt.h"
+#include "host/ble_hs_adv.h"
 #include "controller/ble_ll.h"
 
 /* Init all tasks */
@@ -203,10 +204,10 @@ prphtest_on_connect(int event, int status, struct ble_gap_conn_ctxt *ctxt,
     case BLE_GAP_EVENT_CONN:
         console_printf("connection event; handle=%d status=%d "
                        "peer_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
-                       ctxt->desc.conn_handle, status,
-                       ctxt->desc.peer_addr[0], ctxt->desc.peer_addr[1],
-                       ctxt->desc.peer_addr[2], ctxt->desc.peer_addr[3],
-                       ctxt->desc.peer_addr[4], ctxt->desc.peer_addr[5]);
+                       ctxt->desc->conn_handle, status,
+                       ctxt->desc->peer_addr[0], ctxt->desc->peer_addr[1],
+                       ctxt->desc->peer_addr[2], ctxt->desc->peer_addr[3],
+                       ctxt->desc->peer_addr[4], ctxt->desc->peer_addr[5]);
         break;
 
     default:
@@ -258,7 +259,7 @@ prphtest_task_handler(void *arg)
     assert(rc == 0);
 
     rc = ble_gap_conn_adv_start(BLE_GAP_DISC_MODE_GEN, BLE_GAP_CONN_MODE_UND,
-                                NULL, 0, prphtest_on_connect, NULL);
+                                NULL, 0, NULL, prphtest_on_connect, NULL);
     assert(rc == 0);
 
     while (1) {


[4/5] incubator-mynewt-larva git commit: Protect connection list with a mutex

Posted by cc...@apache.org.
Protect connection list with a mutex


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

Branch: refs/heads/master
Commit: cf46c07f926c44aebbb3f79fb001af7008cda424
Parents: 7674b1b
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Feb 1 11:56:20 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Feb 1 19:40:59 2016 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gap.h      |    2 +-
 net/nimble/host/src/ble_att.c               |   60 +-
 net/nimble/host/src/ble_att_clt.c           |  218 +++--
 net/nimble/host/src/ble_att_priv.h          |   87 +-
 net/nimble/host/src/ble_att_svr.c           |  357 +++++--
 net/nimble/host/src/ble_gap_conn.c          |  710 ++++++++++----
 net/nimble/host/src/ble_gap_priv.h          |    2 +-
 net/nimble/host/src/ble_gatt_priv.h         |   32 +-
 net/nimble/host/src/ble_gattc.c             | 1141 ++++++++++++++--------
 net/nimble/host/src/ble_gatts.c             |  153 ++-
 net/nimble/host/src/ble_hs_adv.c            |    2 +
 net/nimble/host/src/ble_hs_conn.c           |  126 ++-
 net/nimble/host/src/ble_hs_conn.h           |    4 +
 net/nimble/host/src/ble_l2cap.c             |   63 +-
 net/nimble/host/src/ble_l2cap_priv.h        |    9 +-
 net/nimble/host/src/ble_l2cap_sig.c         |  249 +++--
 net/nimble/host/src/host_hci.c              |   59 +-
 net/nimble/host/src/test/ble_gap_test.c     |    2 +-
 net/nimble/host/src/test/ble_hs_test_util.c |   26 +-
 net/nimble/host/src/test/ble_hs_test_util.h |    3 +
 net/nimble/host/src/test/ble_l2cap_test.c   |   15 +-
 net/nimble/host/src/test/ble_os_test.c      |    8 +-
 project/bleshell/src/main.c                 |   10 +-
 23 files changed, 2323 insertions(+), 1015 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 3b8705e..3ea1e17 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -126,7 +126,7 @@ struct ble_gap_conn_upd_params {
 };
 
 struct ble_gap_conn_ctxt {
-    struct ble_gap_conn_desc desc;
+    struct ble_gap_conn_desc *desc;
     struct ble_gap_conn_upd_params *peer_params;
     struct ble_gap_conn_upd_params *self_params;
 };

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_att.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att.c b/net/nimble/host/src/ble_att.c
index 219a66c..7567297 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -16,7 +16,9 @@
 
 #include <stddef.h>
 #include <errno.h>
+#include <assert.h>
 #include "ble_hs_priv.h"
+#include "ble_hs_conn.h"
 #include "ble_l2cap_priv.h"
 #include "ble_att_cmd.h"
 #include "ble_att_priv.h"
@@ -24,15 +26,13 @@
 static uint16_t ble_att_preferred_mtu;
 
 /** Dispatch table for incoming ATT requests.  Sorted by op code. */
-typedef int ble_att_rx_fn(struct ble_hs_conn *conn,
-                          struct ble_l2cap_chan *chan,
-                          struct os_mbuf **om);
+typedef int ble_att_rx_fn(uint16_t conn_handle, struct os_mbuf **om);
 struct ble_att_rx_dispatch_entry {
     uint8_t bde_op;
     ble_att_rx_fn *bde_fn;
 };
 
-static struct ble_att_rx_dispatch_entry ble_att_rx_dispatch[] = {
+static const struct ble_att_rx_dispatch_entry ble_att_rx_dispatch[] = {
     { BLE_ATT_OP_ERROR_RSP,            ble_att_clt_rx_error },
     { BLE_ATT_OP_MTU_REQ,              ble_att_svr_rx_mtu },
     { BLE_ATT_OP_MTU_RSP,              ble_att_clt_rx_mtu },
@@ -64,10 +64,13 @@ static struct ble_att_rx_dispatch_entry ble_att_rx_dispatch[] = {
 #define BLE_ATT_RX_DISPATCH_SZ \
     (sizeof ble_att_rx_dispatch / sizeof ble_att_rx_dispatch[0])
 
-static struct ble_att_rx_dispatch_entry *
+/**
+ * Lock restrictions: None.
+ */
+static const struct ble_att_rx_dispatch_entry *
 ble_att_rx_dispatch_entry_find(uint8_t op)
 {
-    struct ble_att_rx_dispatch_entry *entry;
+    const struct ble_att_rx_dispatch_entry *entry;
     int i;
 
     for (i = 0; i < BLE_ATT_RX_DISPATCH_SZ; i++) {
@@ -84,11 +87,31 @@ ble_att_rx_dispatch_entry_find(uint8_t op)
     return NULL;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
+int
+ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
+                       struct ble_l2cap_chan **out_chan)
+{
+    *out_conn = ble_hs_conn_find(conn_handle);
+    if (*out_conn == NULL) {
+        return BLE_HS_ENOTCONN;
+    }
+
+    *out_chan = ble_hs_conn_chan_find(*out_conn, BLE_L2CAP_CID_ATT);
+    assert(*out_chan != NULL);
+
+    return 0;
+}
+
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static int
-ble_att_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-           struct os_mbuf **om)
+ble_att_rx(uint16_t conn_handle, struct os_mbuf **om)
 {
-    struct ble_att_rx_dispatch_entry *entry;
+    const struct ble_att_rx_dispatch_entry *entry;
     uint8_t op;
     int rc;
 
@@ -102,7 +125,7 @@ ble_att_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
         return BLE_HS_EINVAL;
     }
 
-    rc = entry->bde_fn(conn, chan, om);
+    rc = entry->bde_fn(conn_handle, om);
     if (rc != 0) {
         return rc;
     }
@@ -110,6 +133,9 @@ ble_att_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 void
 ble_att_set_notify_cb(ble_att_svr_notify_fn *cb, void *cb_arg)
 {
@@ -117,6 +143,9 @@ ble_att_set_notify_cb(ble_att_svr_notify_fn *cb, void *cb_arg)
     ble_att_svr_notify_cb_arg = cb_arg;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 void
 ble_att_set_peer_mtu(struct ble_l2cap_chan *chan, uint16_t peer_mtu)
 {
@@ -127,6 +156,9 @@ ble_att_set_peer_mtu(struct ble_l2cap_chan *chan, uint16_t peer_mtu)
     chan->blc_peer_mtu = peer_mtu;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 int
 ble_att_set_preferred_mtu(uint16_t mtu)
 {
@@ -144,6 +176,9 @@ ble_att_set_preferred_mtu(uint16_t mtu)
     return 0;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 struct ble_l2cap_chan *
 ble_att_create_chan(void)
 {
@@ -164,6 +199,8 @@ ble_att_create_chan(void)
 
 /**
  * Allocates an mbuf for use as an ATT request or response.
+ *
+ * Lock restrictions: None.
  */
 struct os_mbuf *
 ble_att_get_pkthdr(void)
@@ -181,6 +218,9 @@ ble_att_get_pkthdr(void)
     return om;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 void
 ble_att_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 d977e51..b9de412 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -27,6 +27,9 @@
 #include "ble_att_cmd.h"
 #include "ble_att_priv.h"
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static int
 ble_att_clt_init_req(struct ble_hs_conn *conn, struct ble_l2cap_chan **chan,
                      struct os_mbuf **txom, uint16_t initial_sz)
@@ -67,6 +70,9 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static int
 ble_att_clt_append_blob(struct ble_l2cap_chan *chan, struct os_mbuf *txom,
                         void *blob, int blob_len)
@@ -102,9 +108,11 @@ ble_att_clt_append_blob(struct ble_l2cap_chan *chan, struct os_mbuf *txom,
  * $error response                                                           *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_error(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                     struct os_mbuf **om)
+ble_att_clt_rx_error(uint16_t conn_handle, struct os_mbuf **om)
 {
     struct ble_att_error_rsp rsp;
     int rc;
@@ -119,7 +127,7 @@ ble_att_clt_rx_error(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
         return rc;
     }
 
-    ble_gattc_rx_err(conn->bhc_handle, &rsp);
+    ble_gattc_rx_err(conn_handle, &rsp);
 
     return 0;
 }
@@ -128,6 +136,9 @@ ble_att_clt_rx_error(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
  * $mtu exchange                                                             *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_mtu(struct ble_hs_conn *conn, struct ble_att_mtu_cmd *req)
 {
@@ -165,28 +176,45 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_mtu(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                   struct os_mbuf **om)
+ble_att_clt_rx_mtu(uint16_t conn_handle, struct os_mbuf **om)
 {
     struct ble_att_mtu_cmd rsp;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    uint16_t mtu;
     int rc;
 
+    mtu = 0;
+
     *om = os_mbuf_pullup(*om, BLE_ATT_MTU_CMD_SZ);
     if (*om == NULL) {
         rc = BLE_HS_ENOMEM;
-        goto done;
-    }
+    } else {
+        rc = ble_att_mtu_cmd_parse((*om)->om_data, (*om)->om_len, &rsp);
+        assert(rc == 0);
 
-    rc = ble_att_mtu_cmd_parse((*om)->om_data, (*om)->om_len, &rsp);
-    assert(rc == 0);
+        ble_hs_conn_lock();
 
-    ble_att_set_peer_mtu(chan, rsp.bamc_mtu);
+        conn = ble_hs_conn_find(conn_handle);
+        if (conn == NULL) {
+            rc = BLE_HS_ENOTCONN;
+        } else {
+            chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+            assert(chan != NULL);
 
-    rc = 0;
+            ble_att_set_peer_mtu(chan, rsp.bamc_mtu);
+            mtu = ble_l2cap_chan_mtu(chan);
+            rc = 0;
+        }
 
-done:
-    ble_gattc_rx_mtu(conn, rc, ble_l2cap_chan_mtu(chan));
+        ble_hs_conn_unlock();
+    }
+
+    ble_gattc_rx_mtu(conn_handle, rc, mtu);
     return rc;
 }
 
@@ -194,6 +222,9 @@ done:
  * $find information                                                         *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_find_info(struct ble_hs_conn *conn,
                          struct ble_att_find_info_req *req)
@@ -232,9 +263,11 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_find_info(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **om)
+ble_att_clt_rx_find_info(uint16_t conn_handle, struct os_mbuf **om)
 {
     struct ble_att_find_info_rsp rsp;
     struct ble_att_find_info_idata idata;
@@ -299,7 +332,7 @@ ble_att_clt_rx_find_info(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
             goto done;
         }
 
-        ble_gattc_rx_find_info_idata(conn, &idata);
+        ble_gattc_rx_find_info_idata(conn_handle, &idata);
     }
 
     rc = 0;
@@ -307,7 +340,7 @@ ble_att_clt_rx_find_info(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
 done:
     *om = rxom;
 
-    ble_gattc_rx_find_info_complete(conn, rc);
+    ble_gattc_rx_find_info_complete(conn_handle, rc);
     return rc;
 }
 
@@ -315,6 +348,9 @@ done:
  * $find by type value                                                       *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_find_type_value(struct ble_hs_conn *conn,
                                struct ble_att_find_type_value_req *req,
@@ -376,10 +412,11 @@ ble_att_clt_parse_find_type_value_hinfo(
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_find_type_value(struct ble_hs_conn *conn,
-                               struct ble_l2cap_chan *chan,
-                               struct os_mbuf **rxom)
+ble_att_clt_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     struct ble_att_find_type_value_hinfo hinfo;
     int rc;
@@ -398,12 +435,12 @@ ble_att_clt_rx_find_type_value(struct ble_hs_conn *conn,
             break;
         }
 
-        ble_gattc_rx_find_type_value_hinfo(conn, &hinfo);
+        ble_gattc_rx_find_type_value_hinfo(conn_handle, &hinfo);
         os_mbuf_adj(*rxom, BLE_ATT_FIND_TYPE_VALUE_HINFO_BASE_SZ);
     }
 
     /* Notify GATT client that the full response has been parsed. */
-    ble_gattc_rx_find_type_value_complete(conn, rc);
+    ble_gattc_rx_find_type_value_complete(conn_handle, rc);
 
     return 0;
 }
@@ -412,6 +449,9 @@ ble_att_clt_rx_find_type_value(struct ble_hs_conn *conn,
  * $read by type                                                             *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_read_type(struct ble_hs_conn *conn,
                          struct ble_att_read_type_req *req,
@@ -457,6 +497,9 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_att_clt_parse_read_type_adata(struct os_mbuf **om, int data_len,
                                   struct ble_att_read_type_adata *adata)
@@ -477,9 +520,11 @@ ble_att_clt_parse_read_type_adata(struct os_mbuf **om, int data_len,
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_read_type(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom)
+ble_att_clt_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     struct ble_att_read_type_adata adata;
     struct ble_att_read_type_rsp rsp;
@@ -504,13 +549,13 @@ ble_att_clt_rx_read_type(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
             goto done;
         }
 
-        ble_gattc_rx_read_type_adata(conn, &adata);
+        ble_gattc_rx_read_type_adata(conn_handle, &adata);
         os_mbuf_adj(*rxom, rsp.batp_length);
     }
 
 done:
     /* Notify GATT that the response is done being parsed. */
-    ble_gattc_rx_read_type_complete(conn, rc);
+    ble_gattc_rx_read_type_complete(conn_handle, rc);
     return rc;
 
 }
@@ -519,6 +564,9 @@ done:
  * $read                                                                     *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_read(struct ble_hs_conn *conn, struct ble_att_read_req *req)
 {
@@ -554,9 +602,11 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_read(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                    struct os_mbuf **rxom)
+ble_att_clt_rx_read(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     void *value;
     int value_len;
@@ -583,7 +633,7 @@ ble_att_clt_rx_read(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
     rc = 0;
 
 done:
-    ble_gattc_rx_read_rsp(conn, rc, value, value_len);
+    ble_gattc_rx_read_rsp(conn_handle, rc, value, value_len);
     return rc;
 }
 
@@ -591,6 +641,9 @@ done:
  * $read blob                                                                *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_read_blob(struct ble_hs_conn *conn,
                          struct ble_att_read_blob_req *req)
@@ -627,9 +680,11 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_read_blob(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom)
+ble_att_clt_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     void *value;
     int value_len;
@@ -656,7 +711,7 @@ ble_att_clt_rx_read_blob(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
 
 done:
     /* Pass the Attribute Value field to GATT. */
-    ble_gattc_rx_read_blob_rsp(conn, rc, value, value_len);
+    ble_gattc_rx_read_blob_rsp(conn_handle, rc, value, value_len);
     return rc;
 }
 
@@ -664,6 +719,9 @@ done:
  * $read multiple                                                            *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_read_mult(struct ble_hs_conn *conn,
                          uint16_t *handles, int num_handles)
@@ -713,9 +771,11 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_read_mult(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom)
+ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     void *value;
     int value_len;
@@ -742,7 +802,7 @@ ble_att_clt_rx_read_mult(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
 
 done:
     /* Pass the Attribute Value field to GATT. */
-    ble_gattc_rx_read_mult_rsp(conn, rc, value, value_len);
+    ble_gattc_rx_read_mult_rsp(conn_handle, rc, value, value_len);
     return rc;
 }
 
@@ -750,6 +810,9 @@ done:
  * $read by group type                                                       *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_read_group_type(struct ble_hs_conn *conn,
                                struct ble_att_read_group_type_req *req,
@@ -795,6 +858,9 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: None.
+ */
 static int
 ble_att_clt_parse_read_group_type_adata(
     struct os_mbuf **om, int data_len,
@@ -817,13 +883,14 @@ ble_att_clt_parse_read_group_type_adata(
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_read_group_type(struct ble_hs_conn *conn,
-                               struct ble_l2cap_chan *chan,
-                               struct os_mbuf **rxom)
+ble_att_clt_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-    struct ble_att_read_group_type_rsp rsp;
     struct ble_att_read_group_type_adata adata;
+    struct ble_att_read_group_type_rsp rsp;
     int rc;
 
     *rxom = os_mbuf_pullup(*rxom, BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ);
@@ -847,13 +914,13 @@ ble_att_clt_rx_read_group_type(struct ble_hs_conn *conn,
             goto done;
         }
 
-        ble_gattc_rx_read_group_type_adata(conn, &adata);
+        ble_gattc_rx_read_group_type_adata(conn_handle, &adata);
         os_mbuf_adj(*rxom, rsp.bagp_length);
     }
 
 done:
     /* Notify GATT that the response is done being parsed. */
-    ble_gattc_rx_read_group_type_complete(conn, rc);
+    ble_gattc_rx_read_group_type_complete(conn_handle, rc);
     return rc;
 }
 
@@ -861,6 +928,9 @@ done:
  * $write                                                                    *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 static int
 ble_att_clt_tx_write_req_or_cmd(struct ble_hs_conn *conn,
                                 struct ble_att_write_req *req,
@@ -903,6 +973,9 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_write_req(struct ble_hs_conn *conn,
                          struct ble_att_write_req *req,
@@ -918,6 +991,9 @@ ble_att_clt_tx_write_req(struct ble_hs_conn *conn,
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_write_cmd(struct ble_hs_conn *conn,
                          struct ble_att_write_req *req,
@@ -933,13 +1009,14 @@ ble_att_clt_tx_write_cmd(struct ble_hs_conn *conn,
     return 0;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_write(struct ble_hs_conn *conn,
-                     struct ble_l2cap_chan *chan,
-                     struct os_mbuf **rxom)
+ble_att_clt_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     /* No payload. */
-    ble_gattc_rx_write_rsp(conn);
+    ble_gattc_rx_write_rsp(conn_handle);
     return 0;
 }
 
@@ -947,6 +1024,9 @@ ble_att_clt_rx_write(struct ble_hs_conn *conn,
  * $prepare write request                                                    *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_prep_write(struct ble_hs_conn *conn,
                           struct ble_att_prep_write_cmd *req,
@@ -1002,10 +1082,11 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_prep_write(struct ble_hs_conn *conn,
-                          struct ble_l2cap_chan *chan,
-                          struct os_mbuf **rxom)
+ble_att_clt_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     struct ble_att_prep_write_cmd rsp;
     int rc;
@@ -1028,12 +1109,12 @@ ble_att_clt_rx_prep_write(struct ble_hs_conn *conn,
     /* Strip the base from the front of the response. */
     os_mbuf_adj(*rxom, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
 
-    ble_gattc_rx_prep_write_rsp(conn, 0, &rsp, (*rxom)->om_data,
+    ble_gattc_rx_prep_write_rsp(conn_handle, 0, &rsp, (*rxom)->om_data,
                                 (*rxom)->om_len);
     return 0;
 
 err:
-    ble_gattc_rx_prep_write_rsp(conn, rc, NULL, NULL, 0);
+    ble_gattc_rx_prep_write_rsp(conn_handle, rc, NULL, NULL, 0);
     return rc;
 }
 
@@ -1041,6 +1122,9 @@ err:
  * $execute write request                                                    *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_exec_write(struct ble_hs_conn *conn,
                           struct ble_att_exec_write_req *req)
@@ -1077,26 +1161,23 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_exec_write(struct ble_hs_conn *conn,
-                          struct ble_l2cap_chan *chan,
-                          struct os_mbuf **rxom)
+ble_att_clt_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     int rc;
 
     *rxom = os_mbuf_pullup(*rxom, BLE_ATT_EXEC_WRITE_RSP_SZ);
     if (*rxom == NULL) {
         rc = BLE_HS_EBADDATA;
-        goto done;
+    } else {
+        rc = ble_att_exec_write_rsp_parse((*rxom)->om_data, (*rxom)->om_len);
+        assert(rc == 0);
     }
 
-    rc = ble_att_exec_write_rsp_parse((*rxom)->om_data, (*rxom)->om_len);
-    assert(rc == 0);
-
-    rc = 0;
-
-done:
-    ble_gattc_rx_exec_write_rsp(conn, rc);
+    ble_gattc_rx_exec_write_rsp(conn_handle, rc);
     return rc;
 }
 
@@ -1104,6 +1185,9 @@ done:
  * $handle value notification                                                *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_notify(struct ble_hs_conn *conn, struct ble_att_notify_req *req,
                       void *value, uint16_t value_len)
@@ -1149,6 +1233,9 @@ err:
  * $handle value indication                                                  *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
 ble_att_clt_tx_indicate(struct ble_hs_conn *conn,
                         struct ble_att_indicate_req *req,
@@ -1192,12 +1279,13 @@ err:
     return rc;
 }
 
+/**
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
+ */
 int
-ble_att_clt_rx_indicate(struct ble_hs_conn *conn,
-                        struct ble_l2cap_chan *chan,
-                        struct os_mbuf **rxom)
+ble_att_clt_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom)
 {
     /* No payload. */
-    ble_gattc_rx_indicate_rsp(conn);
+    ble_gattc_rx_indicate_rsp(conn_handle);
     return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/net/nimble/host/src/ble_att_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_priv.h b/net/nimble/host/src/ble_att_priv.h
index 384ad20..879ef47 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -89,6 +89,8 @@ SLIST_HEAD(ble_att_clt_entry_list, ble_att_clt_entry);
 /*** @gen */
 
 struct ble_l2cap_chan *ble_att_create_chan(void);
+int ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
+                           struct ble_l2cap_chan **out_chan);
 void ble_att_set_peer_mtu(struct ble_l2cap_chan *chan, uint16_t peer_mtu);
 struct os_mbuf *ble_att_get_pkthdr(void);
 void ble_att_init(void);
@@ -101,41 +103,29 @@ extern void *ble_att_svr_notify_cb_arg;
 
 int ble_att_svr_find_by_uuid(uint8_t *uuid, struct ble_att_svr_entry **ha_ptr);
 uint16_t ble_att_svr_prev_handle(void);
-int ble_att_svr_rx_mtu(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                       struct os_mbuf **rxom);
-int ble_att_svr_rx_find_info(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
-                             struct os_mbuf **rxom);
-int ble_att_svr_rx_find_type_value(struct ble_hs_conn *conn,
-                                   struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_mtu(uint16_t conn_handle, struct os_mbuf **om);
+int ble_att_svr_rx_find_info(uint16_t conn_handle, struct os_mbuf **rxom);
+int ble_att_svr_rx_find_type_value(uint16_t conn_handle,
                                    struct os_mbuf **rxom);
-int ble_att_svr_rx_read_type(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_read_type(uint16_t conn_handle,
                              struct os_mbuf **rxom);
-int ble_att_svr_rx_read_group_type(struct ble_hs_conn *conn,
-                                   struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_read_group_type(uint16_t conn_handle,
                                    struct os_mbuf **rxom);
-int ble_att_svr_rx_read(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_read(uint16_t conn_handle,
                         struct os_mbuf **rxom);
-int ble_att_svr_rx_read_blob(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_read_blob(uint16_t conn_handle,
                              struct os_mbuf **rxom);
-int ble_att_svr_rx_read_mult(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_read_mult(uint16_t conn_handle,
                              struct os_mbuf **rxom);
-int ble_att_svr_rx_write(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_write(uint16_t conn_handle,
                          struct os_mbuf **rxom);
-int ble_att_svr_rx_prep_write(struct ble_hs_conn *conn,
-                              struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_prep_write(uint16_t conn_handle,
                               struct os_mbuf **rxom);
-int ble_att_svr_rx_exec_write(struct ble_hs_conn *conn,
-                              struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_exec_write(uint16_t conn_handle,
                               struct os_mbuf **rxom);
-int ble_att_svr_rx_notify(struct ble_hs_conn *conn,
-                          struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_notify(uint16_t conn_handle,
                           struct os_mbuf **rxom);
-int ble_att_svr_rx_indicate(struct ble_hs_conn *conn,
-                            struct ble_l2cap_chan *chan,
+int ble_att_svr_rx_indicate(uint16_t conn_handle,
                             struct os_mbuf **rxom);
 void ble_att_svr_prep_clear(struct ble_att_svr_conn *basc);
 int ble_att_svr_read_handle(struct ble_hs_conn *conn, uint16_t attr_handle,
@@ -173,48 +163,35 @@ struct ble_att_read_group_type_adata {
     uint8_t *value;
 };
 
-int ble_att_clt_rx_error(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom);
+int ble_att_clt_rx_error(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_mtu(struct ble_hs_conn *conn,
                        struct ble_att_mtu_cmd *req);
-int ble_att_clt_rx_mtu(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                       struct os_mbuf **rxom);
+int ble_att_clt_rx_mtu(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_read(struct ble_hs_conn *conn,
                         struct ble_att_read_req *req);
-int ble_att_clt_rx_read(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                        struct os_mbuf **rxom);
+int ble_att_clt_rx_read(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_read_blob(struct ble_hs_conn *conn,
                              struct ble_att_read_blob_req *req);
-int ble_att_clt_rx_read_blob(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
-                             struct os_mbuf **rxom);
+int ble_att_clt_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_read_mult(struct ble_hs_conn *conn,
                              uint16_t *handles, int num_handles);
-int ble_att_clt_rx_read_mult(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
-                             struct os_mbuf **rxom);
+int ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_read_type(struct ble_hs_conn *conn,
                              struct ble_att_read_type_req *req,
                              void *uuid128);
-int ble_att_clt_rx_read_type(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
-                             struct os_mbuf **rxom);
+int ble_att_clt_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_read_group_type(struct ble_hs_conn *conn,
                                    struct ble_att_read_group_type_req *req,
                                    void *uuid128);
-int ble_att_clt_rx_read_group_type(struct ble_hs_conn *conn,
-                                   struct ble_l2cap_chan *chan,
+int ble_att_clt_rx_read_group_type(uint16_t conn_handle,
                                    struct os_mbuf **rxom);
 int ble_att_clt_tx_find_info(struct ble_hs_conn *conn,
                              struct ble_att_find_info_req *req);
-int ble_att_clt_rx_find_info(struct ble_hs_conn *conn,
-                             struct ble_l2cap_chan *chan,
-                             struct os_mbuf **rxom);
+int ble_att_clt_rx_find_info(uint16_t conn_handle, struct os_mbuf **om);
 int ble_att_clt_tx_find_type_value(struct ble_hs_conn *conn,
                                    struct ble_att_find_type_value_req *req,
                                    void *attribute_value, int value_len);
-int ble_att_clt_rx_find_type_value(struct ble_hs_conn *conn,
-                                   struct ble_l2cap_chan *chan,
+int ble_att_clt_rx_find_type_value(uint16_t conn_handle,
                                    struct os_mbuf **rxom);
 int ble_att_clt_tx_write_req(struct ble_hs_conn *conn,
                              struct ble_att_write_req *req,
@@ -225,25 +202,17 @@ int ble_att_clt_tx_write_cmd(struct ble_hs_conn *conn,
 int ble_att_clt_tx_prep_write(struct ble_hs_conn *conn,
                               struct ble_att_prep_write_cmd *req,
                               void *value, uint16_t value_len);
-int ble_att_clt_rx_prep_write(struct ble_hs_conn *conn,
-                              struct ble_l2cap_chan *chan,
-                              struct os_mbuf **rxom);
+int ble_att_clt_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_exec_write(struct ble_hs_conn *conn,
                               struct ble_att_exec_write_req *req);
-int ble_att_clt_rx_exec_write(struct ble_hs_conn *conn,
-                              struct ble_l2cap_chan *chan,
-                              struct os_mbuf **rxom);
-int ble_att_clt_rx_write(struct ble_hs_conn *conn,
-                         struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom);
+int ble_att_clt_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom);
+int ble_att_clt_rx_write(uint16_t conn_handle, struct os_mbuf **rxom);
 int ble_att_clt_tx_notify(struct ble_hs_conn *conn,
                           struct ble_att_notify_req *req,
                           void *value, uint16_t value_len);
 int ble_att_clt_tx_indicate(struct ble_hs_conn *conn,
                             struct ble_att_indicate_req *req,
                             void *value, uint16_t value_len);
-int ble_att_clt_rx_indicate(struct ble_hs_conn *conn,
-                            struct ble_l2cap_chan *chan,
-                            struct os_mbuf **rxom);
+int ble_att_clt_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 9ff872f..e1c80be 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -524,34 +524,41 @@ err:
 }
 
 int
-ble_att_svr_rx_mtu(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                   struct os_mbuf **om)
+ble_att_svr_rx_mtu(uint16_t conn_handle, struct os_mbuf **om)
 {
     struct ble_att_mtu_cmd cmd;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
     uint8_t att_err;
     int rc;
 
-    *om = os_mbuf_pullup(*om, BLE_ATT_MTU_CMD_SZ);
-    if (*om == NULL) {
-        att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
+    ble_hs_conn_lock();
 
-    rc = ble_att_mtu_cmd_parse((*om)->om_data, (*om)->om_len, &cmd);
-    assert(rc == 0);
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        *om = os_mbuf_pullup(*om, BLE_ATT_MTU_CMD_SZ);
+        if (*om == NULL) {
+            att_err = BLE_ATT_ERR_INSUFFICIENT_RES;
+            rc = BLE_HS_ENOMEM;
+        } else {
+            rc = ble_att_mtu_cmd_parse((*om)->om_data, (*om)->om_len, &cmd);
+            assert(rc == 0);
 
-    ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
-    rc = ble_att_svr_tx_mtu_rsp(conn, chan, BLE_ATT_OP_MTU_RSP,
-                                chan->blc_my_mtu, &att_err);
-    if (rc != 0) {
-        goto err;
+            ble_att_set_peer_mtu(chan, cmd.bamc_mtu);
+        }
+
+        if (rc == 0) {
+            rc = ble_att_svr_tx_mtu_rsp(conn, chan, BLE_ATT_OP_MTU_RSP,
+                                        chan->blc_my_mtu, &att_err);
+        }
+        if (rc != 0) {
+            ble_att_svr_tx_error_rsp(conn, chan, BLE_ATT_OP_MTU_REQ, 0,
+                                     att_err);
+        }
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    ble_att_svr_tx_error_rsp(conn, chan, BLE_ATT_OP_MTU_REQ, 0, att_err);
     return rc;
 }
 
@@ -727,9 +734,10 @@ err:
     return rc;
 }
 
-int
-ble_att_svr_rx_find_info(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_find_info_locked(struct ble_hs_conn *conn,
+                                struct ble_l2cap_chan *chan,
+                                struct os_mbuf **rxom)
 {
     struct ble_att_find_info_req req;
     uint16_t err_handle;
@@ -773,6 +781,25 @@ err:
     return rc;
 }
 
+int
+ble_att_svr_rx_find_info(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_find_info_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 /**
  * Processes a single non-matching attribute entry while filling a
  * Find-By-Type-Value-Response.
@@ -1042,10 +1069,10 @@ err:
     return rc;
 }
 
-int
-ble_att_svr_rx_find_type_value(struct ble_hs_conn *conn,
-                               struct ble_l2cap_chan *chan,
-                               struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_find_type_value_locked(struct ble_hs_conn *conn,
+                                      struct ble_l2cap_chan *chan,
+                                      struct os_mbuf **rxom)
 {
     struct ble_att_find_type_value_req req;
     uint16_t err_handle;
@@ -1090,6 +1117,25 @@ err:
     return rc;
 }
 
+int
+ble_att_svr_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_find_type_value_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 static int
 ble_att_svr_tx_read_type_rsp(struct ble_hs_conn *conn,
                              struct ble_l2cap_chan *chan,
@@ -1217,9 +1263,10 @@ done:
     return rc;
 }
 
-int
-ble_att_svr_rx_read_type(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_read_type_locked(struct ble_hs_conn *conn,
+                                struct ble_l2cap_chan *chan,
+                                struct os_mbuf **rxom)
 {
     struct ble_att_read_type_req req;
     uint16_t err_handle;
@@ -1285,6 +1332,25 @@ err:
     return rc;
 }
 
+int
+ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_read_type_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 /**
  * @return                      0 on success; nonzero on failure.
  */
@@ -1394,9 +1460,10 @@ err:
     return rc;
 }
 
-int
-ble_att_svr_rx_read(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                    struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_read_locked(struct ble_hs_conn *conn,
+                           struct ble_l2cap_chan *chan,
+                           struct os_mbuf **rxom)
 {
     struct ble_att_svr_access_ctxt ctxt;
     struct ble_att_svr_entry *entry;
@@ -1461,8 +1528,28 @@ err:
 }
 
 int
-ble_att_svr_rx_read_blob(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom)
+ble_att_svr_rx_read(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_read_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
+static int
+ble_att_svr_rx_read_blob_locked(struct ble_hs_conn *conn,
+                                struct ble_l2cap_chan *chan,
+                                struct os_mbuf **rxom)
 {
     struct ble_att_svr_access_ctxt ctxt;
     struct ble_att_svr_entry *entry;
@@ -1533,6 +1620,25 @@ err:
     return rc;
 }
 
+int
+ble_att_svr_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_read_blob_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 static int
 ble_att_svr_tx_read_mult(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                          struct os_mbuf **rxom, uint8_t *att_err,
@@ -1624,9 +1730,10 @@ err:
     return rc;
 }
 
-int
-ble_att_svr_rx_read_mult(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                         struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_read_mult_locked(struct ble_hs_conn *conn,
+                                struct ble_l2cap_chan *chan,
+                                struct os_mbuf **rxom)
 {
     uint16_t err_handle;
     uint8_t att_err;
@@ -1666,6 +1773,25 @@ err:
     return rc;
 }
 
+int
+ble_att_svr_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_read_mult_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 static int
 ble_att_svr_is_valid_group_type(uint8_t *uuid128)
 {
@@ -1921,10 +2047,10 @@ done:
 }
 
 
-int
-ble_att_svr_rx_read_group_type(struct ble_hs_conn *conn,
-                               struct ble_l2cap_chan *chan,
-                               struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_read_group_type_locked(struct ble_hs_conn *conn,
+                                      struct ble_l2cap_chan *chan,
+                                      struct os_mbuf **rxom)
 {
     struct ble_att_read_group_type_req req;
     uint8_t uuid128[16];
@@ -1988,6 +2114,25 @@ err:
     return rc;
 }
 
+int
+ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_read_group_type_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 static int
 ble_att_svr_tx_write_rsp(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                          uint8_t *att_err)
@@ -2025,9 +2170,10 @@ err:
     return rc;
 }
 
-int
-ble_att_svr_rx_write(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                     struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_write_locked(struct ble_hs_conn *conn,
+                            struct ble_l2cap_chan *chan,
+                            struct os_mbuf **rxom)
 {
     struct ble_att_svr_access_ctxt ctxt;
     struct ble_att_svr_entry *entry;
@@ -2089,6 +2235,25 @@ err:
 }
 
 int
+ble_att_svr_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_write_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
+int
 ble_att_svr_write_local(uint16_t attr_handle, void *data, uint16_t data_len)
 {
     struct ble_att_svr_access_ctxt ctxt;
@@ -2271,10 +2436,10 @@ ble_att_svr_prep_write(struct ble_hs_conn *conn, uint16_t *err_handle)
     return 0;
 }
 
-int
-ble_att_svr_rx_prep_write(struct ble_hs_conn *conn,
-                          struct ble_l2cap_chan *chan,
-                          struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_prep_write_locked(struct ble_hs_conn *conn,
+                                 struct ble_l2cap_chan *chan,
+                                 struct os_mbuf **rxom)
 {
     struct ble_att_prep_write_cmd req;
     struct ble_att_prep_entry *prep_entry;
@@ -2387,6 +2552,25 @@ err:
     return rc;
 }
 
+int
+ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_prep_write_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 /**
  * @return                      0 on success; nonzero on failure.
  */
@@ -2429,10 +2613,10 @@ err:
     return rc;
 }
 
-int
-ble_att_svr_rx_exec_write(struct ble_hs_conn *conn,
-                          struct ble_l2cap_chan *chan,
-                          struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_exec_write_locked(struct ble_hs_conn *conn,
+                                 struct ble_l2cap_chan *chan,
+                                 struct os_mbuf **rxom)
 {
     struct ble_att_exec_write_req req;
     uint16_t err_handle;
@@ -2482,9 +2666,28 @@ err:
 }
 
 int
-ble_att_svr_rx_notify(struct ble_hs_conn *conn,
-                      struct ble_l2cap_chan *chan,
-                      struct os_mbuf **rxom)
+ble_att_svr_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_exec_write_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
+static int
+ble_att_svr_rx_notify_locked(struct ble_hs_conn *conn,
+                             struct ble_l2cap_chan *chan,
+                             struct os_mbuf **rxom)
 {
     struct ble_att_notify_req req;
     uint16_t attr_len;
@@ -2525,6 +2728,25 @@ ble_att_svr_rx_notify(struct ble_hs_conn *conn,
     return 0;
 }
 
+int
+ble_att_svr_rx_notify(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_notify_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 /**
  * @return                      0 on success; nonzero on failure.
  */
@@ -2564,10 +2786,10 @@ err:
     return rc;
 }
 
-int
-ble_att_svr_rx_indicate(struct ble_hs_conn *conn,
-                        struct ble_l2cap_chan *chan,
-                        struct os_mbuf **rxom)
+static int
+ble_att_svr_rx_indicate_locked(struct ble_hs_conn *conn,
+                               struct ble_l2cap_chan *chan,
+                               struct os_mbuf **rxom)
 {
     struct ble_att_indicate_req req;
     uint16_t attr_len;
@@ -2619,6 +2841,25 @@ done:
     return rc;
 }
 
+int
+ble_att_svr_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(conn_handle, &conn, &chan);
+    if (rc == 0) {
+        rc = ble_att_svr_rx_indicate_locked(conn, chan, rxom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
 static void
 ble_att_svr_free_mem(void)
 {



[2/5] incubator-mynewt-larva git commit: Protect connection list with a mutex

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/cf46c07f/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 28f5a0d..e472f84 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -258,95 +258,72 @@ static void ble_gattc_indicate_err(struct ble_gattc_proc *proc, int status,
  */
 static int
 ble_gattc_find_inc_svcs_rx_adata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_read_type_adata *adata);
 static int
 ble_gattc_find_inc_svcs_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn,
                                     int status);
 static int
 ble_gattc_find_inc_svcs_rx_read_rsp(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn,
                                     int status, void *value, int value_len);
 static int
 ble_gattc_disc_all_chrs_rx_adata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_read_type_adata *adata);
 static int
 ble_gattc_disc_all_chrs_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn,
                                     int status);
 static int
 ble_gattc_disc_chr_uuid_rx_adata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_read_type_adata *adata);
 static int
 ble_gattc_disc_chr_uuid_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn,
                                     int status);
 static int
-ble_gattc_read_rx_read_rsp(struct ble_gattc_proc *proc,
-                           struct ble_hs_conn *conn, int status,
+ble_gattc_read_rx_read_rsp(struct ble_gattc_proc *proc, int status,
                            void *value, int value_len);
 static int
-ble_gattc_read_long_rx_read_rsp(struct ble_gattc_proc *proc,
-                                struct ble_hs_conn *conn, int status,
+ble_gattc_read_long_rx_read_rsp(struct ble_gattc_proc *proc, int status,
                                 void *value, int value_len);
 static int
 ble_gattc_read_uuid_rx_adata(struct ble_gattc_proc *proc,
-                             struct ble_hs_conn *conn,
                              struct ble_att_read_type_adata *adata);
 static int
-ble_gattc_read_uuid_rx_complete(struct ble_gattc_proc *proc,
-                                struct ble_hs_conn *conn,
-                                int status);
+ble_gattc_read_uuid_rx_complete(struct ble_gattc_proc *proc, int status);
 static int
 ble_gattc_write_long_rx_prep(struct ble_gattc_proc *proc,
-                             struct ble_hs_conn *conn,
                              int status, struct ble_att_prep_write_cmd *rsp,
                              void *attr_data, uint16_t attr_len);
 static int
-ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc,
-                             struct ble_hs_conn *conn, int status);
+ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc, int status);
 
 static int
-ble_gattc_write_reliable_rx_prep(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
-                                 int status,
+ble_gattc_write_reliable_rx_prep(struct ble_gattc_proc *proc, int status,
                                  struct ble_att_prep_write_cmd *rsp,
                                  void *attr_data, uint16_t attr_len);
 static int
-ble_gattc_write_reliable_rx_exec(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn, int status);
+ble_gattc_write_reliable_rx_exec(struct ble_gattc_proc *proc, int status);
 
 
 typedef int ble_gattc_rx_adata_fn(struct ble_gattc_proc *proc,
-                                  struct ble_hs_conn *conn,
                                   struct ble_att_read_type_adata *adata);
 struct ble_gattc_rx_adata_entry {
     uint8_t op;
     ble_gattc_rx_adata_fn *cb;
 };
 
-typedef int ble_gattc_rx_complete_fn(struct ble_gattc_proc *proc,
-                                     struct ble_hs_conn *conn,
-                                     int rc);
+typedef int ble_gattc_rx_complete_fn(struct ble_gattc_proc *proc, int status);
 struct ble_gattc_rx_complete_entry {
     uint8_t op;
     ble_gattc_rx_complete_fn *cb;
 };
 
-typedef int ble_gattc_rx_attr_fn(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn, int status,
+typedef int ble_gattc_rx_attr_fn(struct ble_gattc_proc *proc, int status,
                                  void *value, int value_len);
 struct ble_gattc_rx_attr_entry {
     uint8_t op;
     ble_gattc_rx_attr_fn *cb;
 };
 
-typedef int ble_gattc_rx_prep_fn(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
-                                 int status,
+typedef int ble_gattc_rx_prep_fn(struct ble_gattc_proc *proc, int status,
                                  struct ble_att_prep_write_cmd *rsp,
                                  void *attr_data, uint16_t attr_len);
 
@@ -355,8 +332,7 @@ struct ble_gattc_rx_prep_entry {
     ble_gattc_rx_prep_fn *cb;
 };
 
-typedef int ble_gattc_rx_exec_fn(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn, int status);
+typedef int ble_gattc_rx_exec_fn(struct ble_gattc_proc *proc, int status);
 
 struct ble_gattc_rx_exec_entry {
     uint8_t op;
@@ -482,6 +458,8 @@ static STAILQ_HEAD(, ble_gattc_proc) ble_gattc_list;
 
 /**
  * Ensures all procedure entries are in a valid state.
+ *
+ * Lock restrictions: None.
  */
 static void
 ble_gattc_assert_sanity(void)
@@ -511,6 +489,9 @@ ble_gattc_assert_sanity(void)
  * $rx entry                                                                 *
  *****************************************************************************/
 
+/**
+ * Lock restrictions: None.
+ */
 static const void *
 ble_gattc_rx_entry_find_(uint8_t op, const void *rx_entries, int num_entries)
 {
@@ -535,6 +516,8 @@ ble_gattc_rx_entry_find_(uint8_t op, const void *rx_entries, int num_entries)
 /**
  * Searches an array of RX entries for one with the specified op code.  This is
  * defined as a macro so that it works with any array type.
+ * 
+ * Lock restrictions: None.
  *
  * @param op_arg                The GATT procedure op code to search for.
  * @param rx_entries            The array to search.
@@ -551,6 +534,8 @@ ble_gattc_rx_entry_find_(uint8_t op, const void *rx_entries, int num_entries)
 
 /**
  * Allocates a proc entry.
+ * 
+ * Lock restrictions: None.
  *
  * @return                      An entry on success; null on failure.
  */
@@ -569,6 +554,8 @@ ble_gattc_proc_alloc(void)
 
 /**
  * Frees the specified proc entry.  No-op if passed a null pointer.
+ * 
+ * Lock restrictions: None.
  */
 static void
 ble_gattc_proc_free(struct ble_gattc_proc *proc)
@@ -583,6 +570,8 @@ ble_gattc_proc_free(struct ble_gattc_proc *proc)
 
 /**
  * Removes the specified proc entry from the global list without freeing it.
+ * 
+ * Lock restrictions: None.
  *
  * @param proc                  The proc to remove.
  * @param prev                  The proc that is previous to "proc" in the
@@ -602,6 +591,8 @@ ble_gattc_proc_remove(struct ble_gattc_proc *proc, struct ble_gattc_proc *prev)
 
 /**
  * Removes and frees the speicifed proc entry.
+ * 
+ * Lock restrictions: None.
  *
  * @param proc                  The proc to remove and free.
  * @param prev                  The proc that is previous to "proc" in the
@@ -617,6 +608,8 @@ ble_gattc_proc_remove_free(struct ble_gattc_proc *proc,
 
 /**
  * Tests if a proc entry fits the specified criteria.
+ * 
+ * Lock restrictions: None.
  *
  * @param proc                  The procedure to test.
  * @param conn_handle           The connection handle to match against.
@@ -650,6 +643,8 @@ ble_gattc_proc_matches(struct ble_gattc_proc *proc, uint16_t conn_handle,
 
 /**
  * Searched the global proc list for an entry that fits the specified criteria.
+ * 
+ * Lock restrictions: None.
  *
  * @param conn_handle           The connection handle to match against.
  * @param op                    The op code to match against, or
@@ -687,6 +682,8 @@ ble_gattc_proc_find(uint16_t conn_handle, uint8_t op, int expecting_only,
 /**
  * Sets the specified proc entry's "pending" flag (i.e., indicates that the
  * GATT procedure is stalled until it transmits its next ATT request.
+ *
+ * Lock restrictions: None.
  */
 static void
 ble_gattc_proc_set_pending(struct ble_gattc_proc *proc)
@@ -701,6 +698,8 @@ ble_gattc_proc_set_pending(struct ble_gattc_proc *proc)
 /**
  * Sets the specified proc entry's "expecting" flag (i.e., indicates that the
  * GATT procedure is stalled until it receives an ATT response.
+ * 
+ * Lock restrictions: None.
  */
 static void
 ble_gattc_proc_set_expecting(struct ble_gattc_proc *proc,
@@ -719,6 +718,8 @@ ble_gattc_proc_set_expecting(struct ble_gattc_proc *proc,
  * Creates a new proc entry and sets its fields to the specified values.  The
  * entry is automatically inserted into the global proc list, and its "pending"
  * flag is set.
+ * 
+ * Lock restrictions: None.
  *
  * @param conn_handle           The handle of the connection associated with
  *                                  the GATT procedure.
@@ -733,16 +734,6 @@ static int
 ble_gattc_new_proc(uint16_t conn_handle, uint8_t op,
                    struct ble_gattc_proc **out_proc)
 {
-    struct ble_hs_conn *conn;
-
-    *out_proc = NULL;
-
-    /* Ensure we have a connection with the specified handle. */
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn == NULL) {
-        return BLE_HS_ENOTCONN;
-    }
-
     *out_proc = ble_gattc_proc_alloc();
     if (*out_proc == NULL) {
         return BLE_HS_ENOMEM;
@@ -759,6 +750,8 @@ ble_gattc_new_proc(uint16_t conn_handle, uint8_t op,
 
 /**
  * Determines if the specified proc entry's "pending" flag can be set.
+ * 
+ * Lock restrictions: None.
  */
 static int
 ble_gattc_proc_can_pend(struct ble_gattc_proc *proc)
@@ -775,6 +768,8 @@ ble_gattc_proc_can_pend(struct ble_gattc_proc *proc)
  * transmission fails.  A tx can be postponed if the failure was caused by
  * congestion or memory exhaustion.  All other failures cannot be postponed,
  * and the procedure should be aborted entirely.
+ * 
+ * Lock restrictions: None.
  *
  * @param proc                  The proc entry to check for postponement.
  * @param rc                    The return code of the previous tx attempt.
@@ -804,6 +799,8 @@ ble_gattc_tx_postpone_chk(struct ble_gattc_proc *proc, int rc)
 
 /**
  * Retrieves the dispatch entry with the specified op code.
+ * 
+ * Lock restrictions: None.
  */
 static const struct ble_gattc_dispatch_entry *
 ble_gattc_dispatch_get(uint8_t op)
@@ -822,6 +819,8 @@ ble_gattc_dispatch_get(uint8_t op)
  * seconds are aborted, and their corresponding connection is terminated.
  *
  * Called by the heartbeat timer; executed every second.
+ * 
+ * Lock restrictions: None.
  */
 static void
 ble_gattc_heartbeat(void *unused)
@@ -855,6 +854,8 @@ ble_gattc_heartbeat(void *unused)
  * Returns a pointer to a GATT error object with the specified fields.  The
  * returned object is statically allocated, so this function is not reentrant.
  * This function should only ever be called by the ble_hs task.
+ * 
+ * Lock restrictions: None.
  */
 struct ble_gatt_error *
 ble_gattc_error(int status, uint16_t att_handle)
@@ -878,6 +879,8 @@ ble_gattc_error(int status, uint16_t att_handle)
  * Calls an mtu-exchange proc's callback with the specified parameters.  If the
  * proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -887,6 +890,8 @@ ble_gattc_mtu_cb(struct ble_gattc_proc *proc, int status, uint16_t att_handle,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->mtu.cb == NULL) {
         rc = 0;
     } else {
@@ -900,6 +905,8 @@ ble_gattc_mtu_cb(struct ble_gattc_proc *proc, int status, uint16_t att_handle,
 
 /**
  * Triggers a pending transmit for the specified mtu-exchange proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_mtu_kick(struct ble_gattc_proc *proc)
@@ -909,39 +916,41 @@ ble_gattc_mtu_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
-    assert(chan != NULL);
+    } else {
+        chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+        assert(chan != NULL);
 
-    if (chan->blc_flags & BLE_L2CAP_CHAN_F_TXED_MTU) {
-        rc = BLE_HS_EALREADY;
-        goto err;
+        if (chan->blc_flags & BLE_L2CAP_CHAN_F_TXED_MTU) {
+            rc = BLE_HS_EALREADY;
+        } else {
+            req.bamc_mtu = chan->blc_my_mtu;
+            rc = ble_att_clt_tx_mtu(conn, &req);
+        }
     }
 
-    req.bamc_mtu = chan->blc_my_mtu;
-    rc = ble_att_clt_tx_mtu(conn, &req);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_hs_conn_unlock();
 
-    return 0;
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
+        ble_gattc_mtu_cb(proc, rc, 0, 0);
+        return BLE_HS_EDONE;
     }
-
-    ble_gattc_mtu_cb(proc, rc, 0, 0);
-    return BLE_HS_EDONE;
 }
 
 /**
  * Handles an incoming ATT error response for the specified mtu-exchange proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_mtu_err(struct ble_gattc_proc *proc, int status, uint16_t att_handle)
@@ -952,9 +961,11 @@ ble_gattc_mtu_err(struct ble_gattc_proc *proc, int status, uint16_t att_handle)
 /**
  * Handles an incoming ATT exchange mtu response for the specified mtu-exchange
  * proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_mtu_rx_rsp(struct ble_gattc_proc *proc, struct ble_hs_conn *conn,
+ble_gattc_mtu_rx_rsp(struct ble_gattc_proc *proc,
                      int status, uint16_t chan_mtu)
 {
     ble_gattc_mtu_cb(proc, status, 0, chan_mtu);
@@ -963,6 +974,8 @@ ble_gattc_mtu_rx_rsp(struct ble_gattc_proc *proc, struct ble_hs_conn *conn,
 
 /**
  * Initiates GATT procedure: Exchange MTU.
+ * 
+ * Lock restrictions: None.
  *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
@@ -995,6 +1008,8 @@ ble_gattc_exchange_mtu(uint16_t conn_handle, ble_gatt_mtu_fn *cb, void *cb_arg)
 /**
  * Calls a discover-all-services proc's callback with the specified parameters.
  * If the proc has no callback, this function is a no-op.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
@@ -1006,6 +1021,8 @@ ble_gattc_disc_all_svcs_cb(struct ble_gattc_proc *proc,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->disc_all_svcs.cb == NULL) {
         rc = 0;
     } else {
@@ -1019,6 +1036,8 @@ ble_gattc_disc_all_svcs_cb(struct ble_gattc_proc *proc,
 
 /**
  * Triggers a pending transmit for the specified discover-all-services proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_all_svcs_kick(struct ble_gattc_proc *proc)
@@ -1028,36 +1047,39 @@ ble_gattc_disc_all_svcs_kick(struct ble_gattc_proc *proc)
     uint8_t uuid128[16];
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    rc = ble_uuid_16_to_128(BLE_ATT_UUID_PRIMARY_SERVICE, uuid128);
-    assert(rc == 0);
+    } else {
+        rc = ble_uuid_16_to_128(BLE_ATT_UUID_PRIMARY_SERVICE, uuid128);
+        assert(rc == 0);
 
-    req.bagq_start_handle = proc->disc_all_svcs.prev_handle + 1;
-    req.bagq_end_handle = 0xffff;
-    rc = ble_att_clt_tx_read_group_type(conn, &req, uuid128);
-    if (rc != 0) {
-        goto err;
+        req.bagq_start_handle = proc->disc_all_svcs.prev_handle + 1;
+        req.bagq_end_handle = 0xffff;
+        rc = ble_att_clt_tx_read_group_type(conn, &req, uuid128);
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_disc_all_svcs_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
+        ble_gattc_disc_all_svcs_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * discover-all-services proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_disc_all_svcs_err(struct ble_gattc_proc *proc, int status,
@@ -1074,10 +1096,11 @@ ble_gattc_disc_all_svcs_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming attribute data entry from a read-group-type response for
  * the specified discover-all-services proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_all_svcs_rx_adata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_read_group_type_adata *adata)
 {
     struct ble_gatt_service service;
@@ -1127,10 +1150,11 @@ done:
 /**
  * Handles a notification that an incoming read-group-type response has been
  * fully processed.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_disc_all_svcs_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn, int status)
+ble_gattc_disc_all_svcs_rx_complete(struct ble_gattc_proc *proc, int status)
 {
     if (status != 0 || proc->disc_all_svcs.prev_handle == 0xffff) {
         /* Error or all svcs discovered. */
@@ -1145,6 +1169,8 @@ ble_gattc_disc_all_svcs_rx_complete(struct ble_gattc_proc *proc,
 
 /**
  * Initiates GATT procedure: Discover All Primary Services.
+ * 
+ * Lock restrictions: None.
  *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
@@ -1178,6 +1204,8 @@ ble_gattc_disc_all_svcs(uint16_t conn_handle, ble_gatt_disc_svc_fn *cb,
 /**
  * Calls a discover-service-by-uuid proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
@@ -1189,6 +1217,8 @@ ble_gattc_disc_svc_uuid_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->disc_svc_uuid.cb == NULL) {
         rc = 0;
     } else {
@@ -1202,6 +1232,8 @@ ble_gattc_disc_svc_uuid_cb(struct ble_gattc_proc *proc, int status,
 
 /**
  * Triggers a pending transmit for the specified discover-service-by-uuid proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_svc_uuid_kick(struct ble_gattc_proc *proc)
@@ -1210,36 +1242,40 @@ ble_gattc_disc_svc_uuid_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    req.bavq_start_handle = proc->disc_svc_uuid.prev_handle + 1;
-    req.bavq_end_handle = 0xffff;
-    req.bavq_attr_type = BLE_ATT_UUID_PRIMARY_SERVICE;
+    } else {
+        req.bavq_start_handle = proc->disc_svc_uuid.prev_handle + 1;
+        req.bavq_end_handle = 0xffff;
+        req.bavq_attr_type = BLE_ATT_UUID_PRIMARY_SERVICE;
 
-    rc = ble_att_clt_tx_find_type_value(conn, &req,
-                                        proc->disc_svc_uuid.service_uuid, 16);
-    if (rc != 0) {
-        goto err;
+        rc = ble_att_clt_tx_find_type_value(conn, &req,
+                                            proc->disc_svc_uuid.service_uuid,
+                                            16);
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_disc_svc_uuid_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
+        ble_gattc_disc_svc_uuid_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * discover-service-by-uuid proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_disc_svc_uuid_err(struct ble_gattc_proc *proc, int status,
@@ -1256,10 +1292,11 @@ ble_gattc_disc_svc_uuid_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming "handles info" entry from a find-type-value response for
  * the specified discover-service-by-uuid proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_svc_uuid_rx_hinfo(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_find_type_value_hinfo *hinfo)
 {
     struct ble_gatt_service service;
@@ -1291,10 +1328,11 @@ done:
 /**
  * Handles a notification that a find-type-value response has been fully
  * processed for the specified discover-service-by-uuid proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_disc_svc_uuid_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn, int status)
+ble_gattc_disc_svc_uuid_rx_complete(struct ble_gattc_proc *proc, int status)
 {
     if (status != 0 || proc->disc_svc_uuid.prev_handle == 0xffff) {
         /* Error or all svcs discovered. */
@@ -1309,6 +1347,8 @@ ble_gattc_disc_svc_uuid_rx_complete(struct ble_gattc_proc *proc,
 
 /**
  * Initiates GATT procedure: Discover Primary Service by Service UUID.
+ * 
+ * Lock restrictions: None.
  *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
@@ -1345,6 +1385,8 @@ ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, void *service_uuid128,
  * Calls a find-included-services proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -1355,6 +1397,8 @@ ble_gattc_find_inc_svcs_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->find_inc_svcs.cb == NULL) {
         rc = 0;
     } else {
@@ -1368,6 +1412,8 @@ ble_gattc_find_inc_svcs_cb(struct ble_gattc_proc *proc, int status,
 
 /**
  * Triggers a pending transmit for the specified find-included-services proc.
+ * 
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_find_inc_svcs_kick(struct ble_gattc_proc *proc)
@@ -1378,44 +1424,48 @@ ble_gattc_find_inc_svcs_kick(struct ble_gattc_proc *proc)
     uint8_t uuid128[16];
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    if (proc->find_inc_svcs.cur_start == 0) {
-        /* Find the next included service. */
-        read_type_req.batq_start_handle = proc->find_inc_svcs.prev_handle + 1;
-        read_type_req.batq_end_handle = proc->find_inc_svcs.end_handle;
+    } else {
+        if (proc->find_inc_svcs.cur_start == 0) {
+            /* Find the next included service. */
+            read_type_req.batq_start_handle =
+                proc->find_inc_svcs.prev_handle + 1;
+            read_type_req.batq_end_handle = proc->find_inc_svcs.end_handle;
 
-        rc = ble_uuid_16_to_128(BLE_ATT_UUID_INCLUDE, uuid128);
-        assert(rc == 0);
+            rc = ble_uuid_16_to_128(BLE_ATT_UUID_INCLUDE, uuid128);
+            assert(rc == 0);
 
-        rc = ble_att_clt_tx_read_type(conn, &read_type_req, uuid128);
-    } else {
-        /* Read the UUID of the previously found service. */
-        read_req.barq_handle = proc->find_inc_svcs.cur_start;
-        rc = ble_att_clt_tx_read(conn, &read_req);
-    }
-    if (rc != 0) {
-        goto err;
+            rc = ble_att_clt_tx_read_type(conn, &read_type_req, uuid128);
+        } else {
+            /* Read the UUID of the previously found service. */
+            read_req.barq_handle = proc->find_inc_svcs.cur_start;
+            rc = ble_att_clt_tx_read(conn, &read_req);
+        }
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_find_inc_svcs_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
+        ble_gattc_find_inc_svcs_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * find-included-services proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_find_inc_svcs_err(struct ble_gattc_proc *proc, int status,
@@ -1434,10 +1484,11 @@ ble_gattc_find_inc_svcs_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming read-response for the specified find-included-services
  * proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_find_inc_svcs_rx_read_rsp(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn, int status,
+ble_gattc_find_inc_svcs_rx_read_rsp(struct ble_gattc_proc *proc, int status,
                                     void *value, int value_len)
 {
     struct ble_gatt_service service;
@@ -1483,10 +1534,11 @@ done:
 /**
  * Handles an incoming "attribute data" entry from a read-by-type response for
  * the specified find-included-services proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_find_inc_svcs_rx_adata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_read_type_adata *adata)
 {
     struct ble_gatt_service service;
@@ -1551,10 +1603,11 @@ done:
 /**
  * Handles a notification that a read-by-type response has been fully
  * processed for the specified find-included-services proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_find_inc_svcs_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn, int status)
+ble_gattc_find_inc_svcs_rx_complete(struct ble_gattc_proc *proc, int status)
 {
     if (status != 0 || proc->find_inc_svcs.prev_handle == 0xffff) {
         /* Error or all svcs discovered. */
@@ -1570,6 +1623,8 @@ ble_gattc_find_inc_svcs_rx_complete(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Find Included Services.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param start_handle          The handle to begin the search at (generally
@@ -1609,6 +1664,8 @@ ble_gattc_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle,
  * Calls a discover-all-characteristics proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -1618,6 +1675,8 @@ ble_gattc_disc_all_chrs_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->disc_all_chrs.cb == NULL) {
         rc = 0;
     } else {
@@ -1632,6 +1691,8 @@ ble_gattc_disc_all_chrs_cb(struct ble_gattc_proc *proc, int status,
 /**
  * Triggers a pending transmit for the specified discover-all-characteristics
  * proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_all_chrs_kick(struct ble_gattc_proc *proc)
@@ -1641,37 +1702,40 @@ ble_gattc_disc_all_chrs_kick(struct ble_gattc_proc *proc)
     uint8_t uuid128[16];
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    rc = ble_uuid_16_to_128(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
-    assert(rc == 0);
+    } else {
+        rc = ble_uuid_16_to_128(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
+        assert(rc == 0);
 
-    req.batq_start_handle = proc->disc_all_chrs.prev_handle + 1;
-    req.batq_end_handle = proc->disc_all_chrs.end_handle;
+        req.batq_start_handle = proc->disc_all_chrs.prev_handle + 1;
+        req.batq_end_handle = proc->disc_all_chrs.end_handle;
 
-    rc = ble_att_clt_tx_read_type(conn, &req, uuid128);
-    if (rc != 0) {
-        goto err;
+        rc = ble_att_clt_tx_read_type(conn, &req, uuid128);
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_disc_all_chrs_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
+        ble_gattc_disc_all_chrs_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * discover-all-characteristics proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_disc_all_chrs_err(struct ble_gattc_proc *proc, int status,
@@ -1688,10 +1752,11 @@ ble_gattc_disc_all_chrs_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming "attribute data" entry from a read-by-type response for
  * the specified discover-all-characteristics proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_all_chrs_rx_adata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_read_type_adata *adata)
 {
     struct ble_gatt_chr chr;
@@ -1745,10 +1810,11 @@ done:
 /**
  * Handles a notification that a read-by-type response has been fully
  * processed for the specified discover-all-characteristics proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_disc_all_chrs_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn, int status)
+ble_gattc_disc_all_chrs_rx_complete(struct ble_gattc_proc *proc, int status)
 {
     if (status != 0 ||
         proc->disc_all_chrs.prev_handle == proc->disc_all_chrs.end_handle) {
@@ -1766,6 +1832,8 @@ ble_gattc_disc_all_chrs_rx_complete(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Discover All Characteristics of a Service.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param start_handle          The handle to begin the search at (generally
@@ -1805,6 +1873,8 @@ ble_gattc_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle,
  * Calls a discover-characteristic-by-uuid proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -1814,6 +1884,8 @@ ble_gattc_disc_chr_uuid_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->disc_chr_uuid.cb == NULL) {
         rc = 0;
     } else {
@@ -1828,6 +1900,8 @@ ble_gattc_disc_chr_uuid_cb(struct ble_gattc_proc *proc, int status,
 /**
  * Triggers a pending transmit for the specified
  * discover-characteristic-by-uuid proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_chr_uuid_kick(struct ble_gattc_proc *proc)
@@ -1837,37 +1911,40 @@ ble_gattc_disc_chr_uuid_kick(struct ble_gattc_proc *proc)
     uint8_t uuid128[16];
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    rc = ble_uuid_16_to_128(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
-    assert(rc == 0);
+    } else {
+        rc = ble_uuid_16_to_128(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
+        assert(rc == 0);
 
-    req.batq_start_handle = proc->disc_chr_uuid.prev_handle + 1;
-    req.batq_end_handle = proc->disc_chr_uuid.end_handle;
+        req.batq_start_handle = proc->disc_chr_uuid.prev_handle + 1;
+        req.batq_end_handle = proc->disc_chr_uuid.end_handle;
 
-    rc = ble_att_clt_tx_read_type(conn, &req, uuid128);
-    if (rc != 0) {
-        goto err;
+        rc = ble_att_clt_tx_read_type(conn, &req, uuid128);
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_disc_chr_uuid_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
+        ble_gattc_disc_chr_uuid_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * discover-characteristic-by-uuid proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_disc_chr_uuid_err(struct ble_gattc_proc *proc, int status,
@@ -1884,10 +1961,11 @@ ble_gattc_disc_chr_uuid_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming "attribute data" entry from a read-by-type response for
  * the specified discover-characteristics-by-uuid proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_chr_uuid_rx_adata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_read_type_adata *adata)
 {
     struct ble_gatt_chr chr;
@@ -1946,10 +2024,11 @@ done:
 /**
  * Handles a notification that a read-by-type response has been fully
  * processed for the specified discover-characteristics-by-uuid proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_disc_chr_uuid_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn, int status)
+ble_gattc_disc_chr_uuid_rx_complete(struct ble_gattc_proc *proc, int status)
 {
     if (status != 0 ||
         proc->disc_chr_uuid.prev_handle == proc->disc_chr_uuid.end_handle) {
@@ -1967,6 +2046,8 @@ ble_gattc_disc_chr_uuid_rx_complete(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Discover Characteristics by UUID.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param start_handle          The handle to begin the search at (generally
@@ -2009,6 +2090,8 @@ ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle,
  * Calls a discover-all-descriptors proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -2018,6 +2101,8 @@ ble_gattc_disc_all_dscs_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->disc_all_dscs.cb == NULL) {
         rc = 0;
     } else {
@@ -2032,6 +2117,8 @@ ble_gattc_disc_all_dscs_cb(struct ble_gattc_proc *proc, int status,
 
 /**
  * Triggers a pending transmit for the specified discover-all-descriptors proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_all_dscs_kick(struct ble_gattc_proc *proc)
@@ -2040,34 +2127,38 @@ ble_gattc_disc_all_dscs_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
+    } else {
 
-    req.bafq_start_handle = proc->disc_all_dscs.prev_handle + 1;
-    req.bafq_end_handle = proc->disc_all_dscs.end_handle;
+        req.bafq_start_handle = proc->disc_all_dscs.prev_handle + 1;
+        req.bafq_end_handle = proc->disc_all_dscs.end_handle;
 
-    rc = ble_att_clt_tx_find_info(conn, &req);
-    if (rc != 0) {
-        goto err;
+        rc = ble_att_clt_tx_find_info(conn, &req);
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_disc_all_dscs_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
+        ble_gattc_disc_all_dscs_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * discover-all-descriptors proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_disc_all_dscs_err(struct ble_gattc_proc *proc, int status,
@@ -2084,10 +2175,11 @@ ble_gattc_disc_all_dscs_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming "information data" entry from a find-information
  * response for the specified discover-all-descriptors proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_disc_all_dscs_rx_idata(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  struct ble_att_find_info_idata *idata)
 {
     struct ble_gatt_dsc dsc;
@@ -2117,10 +2209,11 @@ done:
 /**
  * Handles a notification that a find-information response has been fully
  * processed for the specified discover-all-descriptors proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_disc_all_dscs_rx_complete(struct ble_gattc_proc *proc,
-                                    struct ble_hs_conn *conn, int status)
+ble_gattc_disc_all_dscs_rx_complete(struct ble_gattc_proc *proc, int status)
 {
     if (status != 0 ||
         proc->disc_all_dscs.prev_handle == proc->disc_all_dscs.end_handle) {
@@ -2138,6 +2231,8 @@ ble_gattc_disc_all_dscs_rx_complete(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Discover All Characteristic Descriptors.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param chr_def_handle        The handle of the characteristic definition
@@ -2178,6 +2273,8 @@ ble_gattc_disc_all_dscs(uint16_t conn_handle, uint16_t chr_def_handle,
  * Calls a read-characteristic proc's callback with the specified parameters.
  * If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -2187,6 +2284,8 @@ ble_gattc_read_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->read.cb == NULL) {
         rc = 0;
     } else {
@@ -2201,6 +2300,8 @@ ble_gattc_read_cb(struct ble_gattc_proc *proc, int status,
 /**
  * Triggers a pending transmit for the specified read-characteristic-value
  * proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_read_kick(struct ble_gattc_proc *proc)
@@ -2209,32 +2310,35 @@ ble_gattc_read_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
+    } else {
+        req.barq_handle = proc->read.handle;
+        rc = ble_att_clt_tx_read(conn, &req);
     }
 
-    req.barq_handle = proc->read.handle;
-    rc = ble_att_clt_tx_read(conn, &req);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_hs_conn_unlock();
 
-    return 0;
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
+        ble_gattc_read_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
     }
-
-    ble_gattc_read_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * read-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_read_err(struct ble_gattc_proc *proc, int status,
@@ -2246,10 +2350,11 @@ ble_gattc_read_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming read-response for the specified
  * read-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_read_rx_read_rsp(struct ble_gattc_proc *proc,
-                           struct ble_hs_conn *conn, int status,
+ble_gattc_read_rx_read_rsp(struct ble_gattc_proc *proc, int status,
                            void *value, int value_len)
 {
     struct ble_gatt_attr attr;
@@ -2268,6 +2373,8 @@ ble_gattc_read_rx_read_rsp(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Read Characteristic Value.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the characteristic value to read.
@@ -2302,6 +2409,8 @@ ble_gattc_read(uint16_t conn_handle, uint16_t attr_handle,
  * Calls a read-using-characteristic-uuid proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -2311,6 +2420,8 @@ ble_gattc_read_uuid_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->read_uuid.cb == NULL) {
         rc = 0;
     } else {
@@ -2325,6 +2436,8 @@ ble_gattc_read_uuid_cb(struct ble_gattc_proc *proc, int status,
 /**
  * Triggers a pending transmit for the specified read-using-characteristic-uuid
  * proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_read_uuid_kick(struct ble_gattc_proc *proc)
@@ -2333,33 +2446,36 @@ ble_gattc_read_uuid_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
+    } else {
+        req.batq_start_handle = proc->read_uuid.prev_handle + 1;
+        req.batq_end_handle = proc->read_uuid.end_handle;
+        rc = ble_att_clt_tx_read_type(conn, &req, proc->read_uuid.uuid128);
     }
 
-    req.batq_start_handle = proc->read_uuid.prev_handle + 1;
-    req.batq_end_handle = proc->read_uuid.end_handle;
-    rc = ble_att_clt_tx_read_type(conn, &req, proc->read_uuid.uuid128);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_hs_conn_unlock();
 
-    return 0;
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
+        ble_gattc_read_uuid_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
     }
-
-    ble_gattc_read_uuid_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * read-using-characteristic-uuid proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_read_uuid_err(struct ble_gattc_proc *proc, int status,
@@ -2375,10 +2491,11 @@ ble_gattc_read_uuid_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming "attribute data" entry from a read-by-type response for
  * the specified read-using-characteristic-uuid proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_read_uuid_rx_adata(struct ble_gattc_proc *proc,
-                             struct ble_hs_conn *conn,
                              struct ble_att_read_type_adata *adata)
 {
     struct ble_gatt_attr attr;
@@ -2402,11 +2519,11 @@ ble_gattc_read_uuid_rx_adata(struct ble_gattc_proc *proc,
 /**
  * Handles a notification that a read-by-type response has been fully
  * processed for the specified read-using-characteristic-uuid proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_read_uuid_rx_complete(struct ble_gattc_proc *proc,
-                                struct ble_hs_conn *conn,
-                                int status)
+ble_gattc_read_uuid_rx_complete(struct ble_gattc_proc *proc, int status)
 {
     if (status != 0 ||
         proc->read_uuid.prev_handle == proc->read_uuid.end_handle) {
@@ -2424,6 +2541,8 @@ ble_gattc_read_uuid_rx_complete(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Read Using Characteristic UUID.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param start_handle          The first handle to search (generally the
@@ -2464,6 +2583,8 @@ ble_gattc_read_by_uuid(uint16_t conn_handle, uint16_t start_handle,
  * Calls a read-long-characteristic proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -2473,6 +2594,8 @@ ble_gattc_read_long_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->read_long.cb == NULL) {
         rc = 0;
     } else {
@@ -2486,6 +2609,8 @@ ble_gattc_read_long_cb(struct ble_gattc_proc *proc, int status,
 
 /**
  * Triggers a pending transmit for the specified read-long-characteristic proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_read_long_kick(struct ble_gattc_proc *proc)
@@ -2495,38 +2620,41 @@ ble_gattc_read_long_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    if (proc->read_long.offset == 0) {
-        read_req.barq_handle = proc->read_long.handle;
-        rc = ble_att_clt_tx_read(conn, &read_req);
     } else {
-        blob_req.babq_handle = proc->read_long.handle;
-        blob_req.babq_offset = proc->read_long.offset;
-        rc = ble_att_clt_tx_read_blob(conn, &blob_req);
-    }
-    if (rc != 0) {
-        goto err;
+        if (proc->read_long.offset == 0) {
+            read_req.barq_handle = proc->read_long.handle;
+            rc = ble_att_clt_tx_read(conn, &read_req);
+        } else {
+            blob_req.babq_handle = proc->read_long.handle;
+            blob_req.babq_offset = proc->read_long.offset;
+            rc = ble_att_clt_tx_read_blob(conn, &blob_req);
+        }
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_read_long_cb(proc, rc, 0, NULL);
-    return BLE_HS_EDONE;
+        ble_gattc_read_long_cb(proc, rc, 0, NULL);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * read-long-characteristic proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_read_long_err(struct ble_gattc_proc *proc, int status,
@@ -2538,14 +2666,17 @@ ble_gattc_read_long_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming read-response for the specified
  * read-long-characteristic-values proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_read_long_rx_read_rsp(struct ble_gattc_proc *proc,
-                                struct ble_hs_conn *conn, int status,
+ble_gattc_read_long_rx_read_rsp(struct ble_gattc_proc *proc, int status,
                                 void *value, int value_len)
 {
     struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
     struct ble_gatt_attr attr;
+    uint16_t mtu;
     int rc;
 
     attr.handle = proc->read_long.handle;
@@ -2560,10 +2691,20 @@ ble_gattc_read_long_rx_read_rsp(struct ble_gattc_proc *proc,
     }
 
     /* Determine if this is the end of the attribute value. */
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
-    assert(chan != NULL);
+    ble_hs_conn_lock();
+
+    rc = ble_att_conn_chan_find(proc->conn_handle, &conn, &chan);
+    if (rc == 0) {
+        mtu = ble_l2cap_chan_mtu(chan);
+    }
+
+    ble_hs_conn_unlock();
+
+    if (rc != 0) {
+        return 1;
+    }
 
-    if (value_len < ble_l2cap_chan_mtu(chan) - 1) {
+    if (value_len < mtu - 1) {
         ble_gattc_read_long_cb(proc, 0, 0, NULL);
         return 1;
     } else {
@@ -2578,6 +2719,8 @@ ble_gattc_read_long_rx_read_rsp(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Read Long Characteristic Values.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param handle                The handle of the characteristic value to read.
@@ -2613,6 +2756,8 @@ ble_gattc_read_long(uint16_t conn_handle, uint16_t handle,
  * Calls a read-multiple-characteristics proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -2623,6 +2768,8 @@ ble_gattc_read_mult_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->read_mult.cb == NULL) {
         rc = 0;
     } else {
@@ -2640,6 +2787,8 @@ ble_gattc_read_mult_cb(struct ble_gattc_proc *proc, int status,
 /**
  * Triggers a pending transmit for the specified read-multiple-characteristics
  * proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_read_mult_kick(struct ble_gattc_proc *proc)
@@ -2647,32 +2796,35 @@ ble_gattc_read_mult_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
+    } else {
+        rc = ble_att_clt_tx_read_mult(conn, proc->read_mult.handles,
+                                      proc->read_mult.num_handles);
     }
 
-    rc = ble_att_clt_tx_read_mult(conn, proc->read_mult.handles,
-                                  proc->read_mult.num_handles);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_hs_conn_unlock();
 
-    return 0;
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
+        ble_gattc_read_mult_cb(proc, rc, 0, NULL, 0);
+        return BLE_HS_EDONE;
     }
-
-    ble_gattc_read_mult_cb(proc, rc, 0, NULL, 0);
-    return BLE_HS_EDONE;
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * read-multiple-characteristics proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_read_mult_err(struct ble_gattc_proc *proc, int status,
@@ -2684,10 +2836,11 @@ ble_gattc_read_mult_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming read-multiple-response for the specified
  * read-multiple-characteristic-values proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_read_mult_rx_read_mult_rsp(struct ble_gattc_proc *proc,
-                                     struct ble_hs_conn *conn, int status,
+ble_gattc_read_mult_rx_read_mult_rsp(struct ble_gattc_proc *proc, int status,
                                      void *value, int value_len)
 {
     ble_gattc_read_mult_cb(proc, status, 0, value, value_len);
@@ -2701,6 +2854,8 @@ ble_gattc_read_mult_rx_read_mult_rsp(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Read Multiple Characteristic Values.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param handles               An array of 16-bit attribute handles to read.
@@ -2738,6 +2893,8 @@ ble_gattc_read_mult(uint16_t conn_handle, uint16_t *handles,
  * Calls a write-characteristic-value proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -2747,6 +2904,8 @@ ble_gattc_write_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->write.cb == NULL) {
         rc = 0;
     } else {
@@ -2760,6 +2919,8 @@ ble_gattc_write_cb(struct ble_gattc_proc *proc, int status,
 
 /**
  * Triggers a pending transmit for the specified write-without-response proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_write_no_rsp_kick(struct ble_gattc_proc *proc)
@@ -2768,37 +2929,40 @@ ble_gattc_write_no_rsp_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
+    } else {
+        req.bawq_handle = proc->write.attr.handle;
+        rc = ble_att_clt_tx_write_cmd(conn, &req, proc->write.attr.value,
+                                      proc->write.attr.value_len);
     }
 
-    req.bawq_handle = proc->write.attr.handle;
-    rc = ble_att_clt_tx_write_cmd(conn, &req, proc->write.attr.value,
-                                  proc->write.attr.value_len);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_hs_conn_unlock();
 
-    /* No response expected; call callback immediately and return 'done' to
-     * indicate the proc should be freed.
-     */
-    ble_gattc_write_cb(proc, 0, 0);
-    return BLE_HS_EDONE;
+    if (rc == 0) {
+        /* No response expected; call callback immediately and return 'done' to
+         * indicate the proc should be freed.
+         */
+        ble_gattc_write_cb(proc, 0, 0);
+        return BLE_HS_EDONE;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
+        ble_gattc_write_cb(proc, rc, 0);
+        return BLE_HS_EDONE;
     }
-
-    ble_gattc_write_cb(proc, rc, 0);
-    return BLE_HS_EDONE;
 }
 
 /**
  * Initiates GATT procedure: Write Without Response.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the characteristic value to write
@@ -2838,6 +3002,8 @@ ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle, void *value,
 /**
  * Triggers a pending transmit for the specified write-characteristic-value
  * proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_write_kick(struct ble_gattc_proc *proc)
@@ -2846,33 +3012,36 @@ ble_gattc_write_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
+    } else {
+        req.bawq_handle = proc->write.attr.handle;
+        rc = ble_att_clt_tx_write_req(conn, &req, proc->write.attr.value,
+                                      proc->write.attr.value_len);
     }
 
-    req.bawq_handle = proc->write.attr.handle;
-    rc = ble_att_clt_tx_write_req(conn, &req, proc->write.attr.value,
-                                  proc->write.attr.value_len);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_hs_conn_unlock();
 
-    return 0;
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
+        ble_gattc_write_cb(proc, rc, 0);
+        return BLE_HS_EDONE;
     }
-
-    ble_gattc_write_cb(proc, rc, 0);
-    return BLE_HS_EDONE;
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * write-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_write_err(struct ble_gattc_proc *proc, int status,
@@ -2884,9 +3053,11 @@ ble_gattc_write_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming write-response for the specified
  * write-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_write_rx_rsp(struct ble_gattc_proc *proc, struct ble_hs_conn *conn)
+ble_gattc_write_rx_rsp(struct ble_gattc_proc *proc)
 {
     ble_gattc_write_cb(proc, 0, 0);
 
@@ -2897,6 +3068,8 @@ ble_gattc_write_rx_rsp(struct ble_gattc_proc *proc, struct ble_hs_conn *conn)
 /**
  * Initiates GATT procedure: Write Characteristic Value.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the characteristic value to write
@@ -2937,6 +3110,8 @@ ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle, void *value,
  * Calls a write-long-characteristic-value proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -2946,6 +3121,8 @@ ble_gattc_write_long_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->write_long.cb == NULL) {
         rc = 0;
     } else {
@@ -2961,6 +3138,8 @@ ble_gattc_write_long_cb(struct ble_gattc_proc *proc, int status,
 /**
  * Triggers a pending transmit for the specified
  * write-long-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_write_long_kick(struct ble_gattc_proc *proc)
@@ -2972,54 +3151,57 @@ ble_gattc_write_long_kick(struct ble_gattc_proc *proc)
     int max_sz;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    if (proc->write_long.attr.offset < proc->write_long.attr.value_len) {
-        chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
-        assert(chan != NULL);
-
-        max_sz = ble_l2cap_chan_mtu(chan) - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
-        if (proc->write_long.attr.offset + max_sz >
-            proc->write_long.attr.value_len) {
+    } else {
+        if (proc->write_long.attr.offset < proc->write_long.attr.value_len) {
+            chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+            assert(chan != NULL);
+
+            max_sz = ble_l2cap_chan_mtu(chan) - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+            if (proc->write_long.attr.offset + max_sz >
+                proc->write_long.attr.value_len) {
+
+                proc->write_long.length = proc->write_long.attr.value_len -
+                                          proc->write_long.attr.offset;
+            } else {
+                proc->write_long.length = max_sz;
+            }
 
-            proc->write_long.length = proc->write_long.attr.value_len -
-                                      proc->write_long.attr.offset;
+            prep_req.bapc_handle = proc->write_long.attr.handle;
+            prep_req.bapc_offset = proc->write_long.attr.offset;
+            rc = ble_att_clt_tx_prep_write(conn, &prep_req,
+                                           proc->write_long.attr.value +
+                                               proc->write_long.attr.offset,
+                                           proc->write_long.length);
         } else {
-            proc->write_long.length = max_sz;
+            exec_req.baeq_flags = BLE_ATT_EXEC_WRITE_F_CONFIRM;
+            rc = ble_att_clt_tx_exec_write(conn, &exec_req);
         }
-
-        prep_req.bapc_handle = proc->write_long.attr.handle;
-        prep_req.bapc_offset = proc->write_long.attr.offset;
-        rc = ble_att_clt_tx_prep_write(conn, &prep_req,
-                                       proc->write_long.attr.value +
-                                           proc->write_long.attr.offset,
-                                       proc->write_long.length);
-    } else {
-        exec_req.baeq_flags = BLE_ATT_EXEC_WRITE_F_CONFIRM;
-        rc = ble_att_clt_tx_exec_write(conn, &exec_req);
-    }
-    if (rc != 0) {
-        goto err;
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_write_long_cb(proc, rc, 0);
-    return BLE_HS_EDONE;
+        ble_gattc_write_long_cb(proc, rc, 0);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * write-long-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_write_long_err(struct ble_gattc_proc *proc, int status,
@@ -3031,10 +3213,11 @@ ble_gattc_write_long_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming prepare-write-response for the specified
  * write-long-cahracteristic-values proc.
+ *
+ * Lock restrictions: None.
  */
 static int
 ble_gattc_write_long_rx_prep(struct ble_gattc_proc *proc,
-                             struct ble_hs_conn *conn,
                              int status, struct ble_att_prep_write_cmd *rsp,
                              void *attr_data, uint16_t attr_len)
 {
@@ -3082,10 +3265,11 @@ err:
 /**
  * Handles an incoming execute-write-response for the specified
  * write-long-characteristic-values proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc,
-                             struct ble_hs_conn *conn, int status)
+ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc, int status)
 {
     ble_gattc_write_long_cb(proc, status, 0);
     return 1;
@@ -3094,6 +3278,8 @@ ble_gattc_write_long_rx_exec(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Write Long Characteristic Values.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the characteristic value to write
@@ -3135,6 +3321,8 @@ ble_gattc_write_long(uint16_t conn_handle, uint16_t attr_handle, void *value,
  * Calls a write-long-characteristic-value proc's callback with the specified
  * parameters.  If the proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -3144,6 +3332,8 @@ ble_gattc_write_reliable_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->write_reliable.cb == NULL) {
         rc = 0;
     } else {
@@ -3160,6 +3350,8 @@ ble_gattc_write_reliable_cb(struct ble_gattc_proc *proc, int status,
 /**
  * Triggers a pending transmit for the specified
  * write-reliable-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_write_reliable_kick(struct ble_gattc_proc *proc)
@@ -3171,41 +3363,44 @@ ble_gattc_write_reliable_kick(struct ble_gattc_proc *proc)
     int attr_idx;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
-
-    attr_idx = proc->write_reliable.cur_attr;
-    if (attr_idx < proc->write_reliable.num_attrs) {
-        attr = proc->write_reliable.attrs + attr_idx;
-        prep_req.bapc_handle = attr->handle;
-        prep_req.bapc_offset = 0;
-        rc = ble_att_clt_tx_prep_write(conn, &prep_req, attr->value,
-                                       attr->value_len);
     } else {
-        exec_req.baeq_flags = BLE_ATT_EXEC_WRITE_F_CONFIRM;
-        rc = ble_att_clt_tx_exec_write(conn, &exec_req);
-    }
-    if (rc != 0) {
-        goto err;
+        attr_idx = proc->write_reliable.cur_attr;
+        if (attr_idx < proc->write_reliable.num_attrs) {
+            attr = proc->write_reliable.attrs + attr_idx;
+            prep_req.bapc_handle = attr->handle;
+            prep_req.bapc_offset = 0;
+            rc = ble_att_clt_tx_prep_write(conn, &prep_req, attr->value,
+                                           attr->value_len);
+        } else {
+            exec_req.baeq_flags = BLE_ATT_EXEC_WRITE_F_CONFIRM;
+            rc = ble_att_clt_tx_exec_write(conn, &exec_req);
+        }
     }
 
-    return 0;
+    ble_hs_conn_unlock();
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
-    }
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-    ble_gattc_write_reliable_cb(proc, rc, 0);
-    return BLE_HS_EDONE;
+        ble_gattc_write_reliable_cb(proc, rc, 0);
+        return BLE_HS_EDONE;
+    }
 }
 
 /**
  * Handles an incoming ATT error response for the specified
  * write-reliable-characteristic-value proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_write_reliable_err(struct ble_gattc_proc *proc, int status,
@@ -3217,10 +3412,11 @@ ble_gattc_write_reliable_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming prepare-write-response for the specified
  * write-reliable-cahracteristic-values proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_write_reliable_rx_prep(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn,
                                  int status,
                                  struct ble_att_prep_write_cmd *rsp,
                                  void *attr_data, uint16_t attr_len)
@@ -3271,10 +3467,11 @@ err:
 /**
  * Handles an incoming execute-write-response for the specified
  * write-reliable-characteristic-values proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_write_reliable_rx_exec(struct ble_gattc_proc *proc,
-                                 struct ble_hs_conn *conn, int status)
+ble_gattc_write_reliable_rx_exec(struct ble_gattc_proc *proc, int status)
 {
     ble_gattc_write_reliable_cb(proc, status, 0);
     return 1;
@@ -3283,6 +3480,8 @@ ble_gattc_write_reliable_rx_exec(struct ble_gattc_proc *proc,
 /**
  * Initiates GATT procedure: Write Long Characteristic Values.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the characteristic value to write
@@ -3322,6 +3521,8 @@ ble_gattc_write_reliable(uint16_t conn_handle, struct ble_gatt_attr *attrs,
 
 /**
  * Sends an attribute notification.
+ *
+ * Lock restrictions: Caller must lock ble_hs_conn mutex.
  */
 int
 ble_gattc_notify(struct ble_hs_conn *conn, uint16_t chr_val_handle)
@@ -3352,6 +3553,8 @@ ble_gattc_notify(struct ble_hs_conn *conn, uint16_t chr_val_handle)
  * Calls an indication proc's callback with the specified parameters.  If the
  * proc has no callback, this function is a no-op.
  *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
+ *
  * @return                      The return code of the callback (or 0 if there
  *                                  is no callback).
  */
@@ -3361,6 +3564,8 @@ ble_gattc_indicate_cb(struct ble_gattc_proc *proc, int status,
 {
     int rc;
 
+    assert(!ble_hs_conn_locked_by_cur_task());
+
     if (proc->indicate.cb == NULL) {
         rc = 0;
     } else {
@@ -3374,6 +3579,8 @@ ble_gattc_indicate_cb(struct ble_gattc_proc *proc, int status,
 
 /**
  * Triggers a pending transmit for the specified indication proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
 ble_gattc_indicate_kick(struct ble_gattc_proc *proc)
@@ -3383,40 +3590,42 @@ ble_gattc_indicate_kick(struct ble_gattc_proc *proc)
     struct ble_hs_conn *conn;
     int rc;
 
+    ble_hs_conn_lock();
+
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn == NULL) {
         rc = BLE_HS_ENOTCONN;
-        goto err;
-    }
+    } else {
+        rc = ble_att_svr_read_handle(NULL, proc->indicate.attr.handle, &ctxt,
+                                     NULL);
+        if (rc == 0) {
+            proc->indicate.attr.value = ctxt.attr_data;
+            proc->indicate.attr.value_len = ctxt.data_len;
 
-    rc = ble_att_svr_read_handle(NULL, proc->indicate.attr.handle, &ctxt,
-                                 NULL);
-    if (rc != 0) {
-        goto err;
+            req.baiq_handle = proc->indicate.attr.handle;
+            rc = ble_att_clt_tx_indicate(conn, &req, proc->indicate.attr.value,
+                                         proc->indicate.attr.value_len);
+        }
     }
-    proc->indicate.attr.value = ctxt.attr_data;
-    proc->indicate.attr.value_len = ctxt.data_len;
 
-    req.baiq_handle = proc->indicate.attr.handle;
-    rc = ble_att_clt_tx_indicate(conn, &req, proc->indicate.attr.value,
-                                 proc->indicate.attr.value_len);
-    if (rc != 0) {
-        goto err;
-    }
+    ble_hs_conn_unlock();
 
-    return 0;
+    if (rc == 0) {
+        return 0;
+    } else {
+        if (ble_gattc_tx_postpone_chk(proc, rc)) {
+            return BLE_HS_EAGAIN;
+        }
 
-err:
-    if (ble_gattc_tx_postpone_chk(proc, rc)) {
-        return BLE_HS_EAGAIN;
+        ble_gattc_indicate_cb(proc, rc, 0);
+        return BLE_HS_EDONE;
     }
-
-    ble_gattc_indicate_cb(proc, rc, 0);
-    return BLE_HS_EDONE;
 }
 
 /**
  * Handles an incoming ATT error response for the specified indication proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static void
 ble_gattc_indicate_err(struct ble_gattc_proc *proc, int status,
@@ -3428,20 +3637,30 @@ ble_gattc_indicate_err(struct ble_gattc_proc *proc, int status,
 /**
  * Handles an incoming handle-value-confirmation for the specified indication
  * proc.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 static int
-ble_gattc_indicate_rx_rsp(struct ble_gattc_proc *proc,
-                          struct ble_hs_conn *conn)
+ble_gattc_indicate_rx_rsp(struct ble_gattc_proc *proc)
 {
+    struct ble_hs_conn *conn;
+
     /* Now that the confirmation has been received, we can send any subsequent
      * indication.
      */
-    conn->bhc_gatt_svr.flags &= ~BLE_GATTS_CONN_F_INDICATION_TXED;
+    ble_hs_conn_lock();
 
-    ble_gattc_indicate_cb(proc, 0, 0);
+    conn = ble_hs_conn_find(proc->conn_handle);
+    if (conn != NULL) {
+        conn->bhc_gatt_svr.flags &= ~BLE_GATTS_CONN_F_INDICATION_TXED;
+
+        ble_gattc_indicate_cb(proc, 0, 0);
+
+        /* Send the next indication if one is pending. */
+        ble_gatts_send_notifications(conn);
+    }
 
-    /* Send the next indication if one is pending. */
-    ble_gatts_send_notifications(conn);
+    ble_hs_conn_unlock();
 
     /* The indicate operation only has a single request / response exchange. */
     return 1;
@@ -3449,6 +3668,8 @@ ble_gattc_indicate_rx_rsp(struct ble_gattc_proc *proc,
 
 /**
  * Sends an attribute indication.
+ *
+ * Lock restrictions: None.
  */
 int
 ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle,
@@ -3477,6 +3698,8 @@ ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle,
 /**
  * Initiates GATT procedure: Read Characteristic Descriptors.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the descriptor to read.
@@ -3498,6 +3721,8 @@ ble_gattc_read_dsc(uint16_t conn_handle, uint16_t attr_handle,
 /**
  * Initiates GATT procedure: Read Long Characteristic Descriptors.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the descriptor to read.
@@ -3519,6 +3744,8 @@ ble_gattc_read_long_dsc(uint16_t conn_handle, uint16_t attr_handle,
 /**
  * Initiates GATT procedure: Write Characteristic Descriptors.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the descriptor to write.
@@ -3544,6 +3771,8 @@ ble_gattc_write_dsc(uint16_t conn_handle, uint16_t attr_handle, void *value,
 /**
  * Initiates GATT procedure: Write Long Characteristic Descriptors.
  *
+ * Lock restrictions: None.
+ *
  * @param conn_handle           The connection over which to execute the
  *                                  procedure.
  * @param attr_handle           The handle of the descriptor to write.
@@ -3569,6 +3798,8 @@ ble_gattc_write_long_dsc(uint16_t conn_handle, uint16_t attr_handle,
 /**
  * Dispatches an incoming ATT error-response to the appropriate active GATT
  * procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
 ble_gattc_rx_err(uint16_t conn_handle, struct ble_att_error_rsp *rsp)
@@ -3597,9 +3828,11 @@ ble_gattc_rx_err(uint16_t conn_handle, struct ble_att_error_rsp *rsp)
 /**
  * Dispatches an incoming ATT exchange-mtu-response to the appropriate active
  * GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_mtu(struct ble_hs_conn *conn, int status, uint16_t chan_mtu)
+ble_gattc_rx_mtu(uint16_t conn_handle, int status, uint16_t chan_mtu)
 {
     struct ble_gattc_proc *proc;
     struct ble_gattc_proc *prev;
@@ -3607,13 +3840,13 @@ ble_gattc_rx_mtu(struct ble_hs_conn *conn, int status, uint16_t chan_mtu)
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_MTU, 1, &prev);
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_MTU, 1, &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
     }
 
-    rc = ble_gattc_mtu_rx_rsp(proc, conn, status, chan_mtu);
+    rc = ble_gattc_mtu_rx_rsp(proc, status, chan_mtu);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     }
@@ -3622,9 +3855,11 @@ ble_gattc_rx_mtu(struct ble_hs_conn *conn, int status, uint16_t chan_mtu)
 /**
  * Dispatches an incoming "information data" entry from a
  * find-information-response to the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_find_info_idata(struct ble_hs_conn *conn,
+ble_gattc_rx_find_info_idata(uint16_t conn_handle,
                              struct ble_att_find_info_idata *idata)
 {
     struct ble_gattc_proc *proc;
@@ -3633,14 +3868,14 @@ ble_gattc_rx_find_info_idata(struct ble_hs_conn *conn,
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_DISC_ALL_DSCS, 1,
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_DISC_ALL_DSCS, 1,
                                &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
     }
 
-    rc = ble_gattc_disc_all_dscs_rx_idata(proc, conn, idata);
+    rc = ble_gattc_disc_all_dscs_rx_idata(proc, idata);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     } else {
@@ -3656,9 +3891,11 @@ ble_gattc_rx_find_info_idata(struct ble_hs_conn *conn,
 /**
  * Dispatches an incoming notification of the end of a
  * find-information-response to the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_find_info_complete(struct ble_hs_conn *conn, int status)
+ble_gattc_rx_find_info_complete(uint16_t conn_handle, int status)
 {
     struct ble_gattc_proc *proc;
     struct ble_gattc_proc *prev;
@@ -3666,14 +3903,14 @@ ble_gattc_rx_find_info_complete(struct ble_hs_conn *conn, int status)
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_DISC_ALL_DSCS, 1,
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_DISC_ALL_DSCS, 1,
                                &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
     }
 
-    rc = ble_gattc_disc_all_dscs_rx_complete(proc, conn, status);
+    rc = ble_gattc_disc_all_dscs_rx_complete(proc, status);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     }
@@ -3682,9 +3919,11 @@ ble_gattc_rx_find_info_complete(struct ble_hs_conn *conn, int status)
 /**
  * Dispatches an incoming "handles info" entry from a
  * find-by-type-value-response to the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_find_type_value_hinfo(struct ble_hs_conn *conn,
+ble_gattc_rx_find_type_value_hinfo(uint16_t conn_handle,
                                    struct ble_att_find_type_value_hinfo *hinfo)
 {
     struct ble_gattc_proc *proc;
@@ -3693,14 +3932,14 @@ ble_gattc_rx_find_type_value_hinfo(struct ble_hs_conn *conn,
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_DISC_SVC_UUID, 1,
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_DISC_SVC_UUID, 1,
                                &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
     }
 
-    rc = ble_gattc_disc_svc_uuid_rx_hinfo(proc, conn, hinfo);
+    rc = ble_gattc_disc_svc_uuid_rx_hinfo(proc, hinfo);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     } else {
@@ -3716,9 +3955,11 @@ ble_gattc_rx_find_type_value_hinfo(struct ble_hs_conn *conn,
 /**
  * Dispatches an incoming notification of the end of a
  * find-by-type-value-response to the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_find_type_value_complete(struct ble_hs_conn *conn, int status)
+ble_gattc_rx_find_type_value_complete(uint16_t conn_handle, int status)
 {
     struct ble_gattc_proc *proc;
     struct ble_gattc_proc *prev;
@@ -3726,14 +3967,14 @@ ble_gattc_rx_find_type_value_complete(struct ble_hs_conn *conn, int status)
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_DISC_SVC_UUID, 1,
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_DISC_SVC_UUID, 1,
                                &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
     }
 
-    rc = ble_gattc_disc_svc_uuid_rx_complete(proc, conn, status);
+    rc = ble_gattc_disc_svc_uuid_rx_complete(proc, status);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     }
@@ -3742,9 +3983,11 @@ ble_gattc_rx_find_type_value_complete(struct ble_hs_conn *conn, int status)
 /**
  * Dispatches an incoming "attribute data" entry from a read-by-type-response
  * to the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_read_type_adata(struct ble_hs_conn *conn,
+ble_gattc_rx_read_type_adata(uint16_t conn_handle,
                              struct ble_att_read_type_adata *adata)
 {
     const struct ble_gattc_rx_adata_entry *rx_entry;
@@ -3754,7 +3997,7 @@ ble_gattc_rx_read_type_adata(struct ble_hs_conn *conn,
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_NONE, 1, &prev);
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_NONE, 1, &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
@@ -3767,7 +4010,7 @@ ble_gattc_rx_read_type_adata(struct ble_hs_conn *conn,
         return;
     }
 
-    rc = rx_entry->cb(proc, conn, adata);
+    rc = rx_entry->cb(proc, adata);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     } else {
@@ -3783,9 +4026,11 @@ ble_gattc_rx_read_type_adata(struct ble_hs_conn *conn,
 /**
  * Dispatches an incoming notification of the end of a read-by-type-response to
  * the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_read_type_complete(struct ble_hs_conn *conn, int status)
+ble_gattc_rx_read_type_complete(uint16_t conn_handle, int status)
 {
     const struct ble_gattc_rx_complete_entry *rx_entry;
     struct ble_gattc_proc *proc;
@@ -3794,7 +4039,7 @@ ble_gattc_rx_read_type_complete(struct ble_hs_conn *conn, int status)
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_NONE, 1, &prev);
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_NONE, 1, &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
@@ -3807,7 +4052,7 @@ ble_gattc_rx_read_type_complete(struct ble_hs_conn *conn, int status)
         return;
     }
 
-    rc = rx_entry->cb(proc, conn, status);
+    rc = rx_entry->cb(proc, status);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     }
@@ -3816,9 +4061,11 @@ ble_gattc_rx_read_type_complete(struct ble_hs_conn *conn, int status)
 /**
  * Dispatches an incoming "attribute data" entry from a
  * read-by-group-type-response to the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_read_group_type_adata(struct ble_hs_conn *conn,
+ble_gattc_rx_read_group_type_adata(uint16_t conn_handle,
                                    struct ble_att_read_group_type_adata *adata)
 {
     struct ble_gattc_proc *proc;
@@ -3827,14 +4074,14 @@ ble_gattc_rx_read_group_type_adata(struct ble_hs_conn *conn,
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_DISC_ALL_SVCS, 1,
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_DISC_ALL_SVCS, 1,
                                &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
     }
 
-    rc = ble_gattc_disc_all_svcs_rx_adata(proc, conn, adata);
+    rc = ble_gattc_disc_all_svcs_rx_adata(proc, adata);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     } else {
@@ -3850,9 +4097,11 @@ ble_gattc_rx_read_group_type_adata(struct ble_hs_conn *conn,
 /**
  * Dispatches an incoming notification of the end of a
  * read-by-group-type-response to the appropriate active GATT procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_read_group_type_complete(struct ble_hs_conn *conn, int status)
+ble_gattc_rx_read_group_type_complete(uint16_t conn_handle, int status)
 {
     struct ble_gattc_proc *proc;
     struct ble_gattc_proc *prev;
@@ -3860,14 +4109,14 @@ ble_gattc_rx_read_group_type_complete(struct ble_hs_conn *conn, int status)
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_DISC_ALL_SVCS, 1,
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_DISC_ALL_SVCS, 1,
                                &prev);
     if (proc == NULL) {
         /* Not expecting a response from this device. */
         return;
     }
 
-    rc = ble_gattc_disc_all_svcs_rx_complete(proc, conn, status);
+    rc = ble_gattc_disc_all_svcs_rx_complete(proc, status);
     if (rc != 0) {
         ble_gattc_proc_remove_free(proc, prev);
     }
@@ -3876,9 +4125,11 @@ ble_gattc_rx_read_group_type_complete(struct ble_hs_conn *conn, int status)
 /**
  * Dispatches an incoming ATT read-response to the appropriate active GATT
  * procedure.
+ *
+ * Lock restrictions: Caller must NOT lock ble_hs_conn mutex.
  */
 void
-ble_gattc_rx_read_rsp(struct ble_hs_conn *conn, int status, void *value,
+ble_gattc_rx_read_rsp(uint16_t conn_handle, int status, void *value,
                       int value_len)
 {
     const struct ble_gattc_rx_attr_entry *rx_entry;
@@ -3888,7 +4139,7 @@ ble_gattc_rx_read_rsp(struct ble_hs_conn *conn, int status, void *value,
 
     ble_gattc_assert_sanity();
 
-    proc = ble_gattc_proc_find(conn->bhc_handle, BLE_GATT_OP_NONE, 1, &prev);
+    proc = ble_gattc_proc_find(conn_handle, BLE_GATT_OP_NONE, 1, &prev);
     if (proc == NULL) {
      

<TRUNCATED>