You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2015/12/03 19:55:14 UTC

[3/3] incubator-mynewt-larva git commit: Rename ble_hs_att_* to ble_att_*

Rename ble_hs_att_* to ble_att_*

Identifiers are getting too long.


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

Branch: refs/heads/master
Commit: 31f0c10b69827333e42d2c071d19ab6dcb592d68
Parents: 8598e84
Author: Christopher Collins <cc...@gmail.com>
Authored: Thu Dec 3 10:54:52 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Thu Dec 3 10:54:52 2015 -0800

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_gatt.h        |    4 +-
 net/nimble/host/include/host/ble_hs_test.h     |    4 +-
 net/nimble/host/src/ble_att.c                  |  118 ++
 net/nimble/host/src/ble_att.h                  |  166 +++
 net/nimble/host/src/ble_att_clt.c              |  511 ++++++++
 net/nimble/host/src/ble_att_cmd.c              |  482 ++++++++
 net/nimble/host/src/ble_att_cmd.h              |  249 ++++
 net/nimble/host/src/ble_att_svr.c              | 1226 ++++++++++++++++++
 net/nimble/host/src/ble_gatt.c                 |   14 +-
 net/nimble/host/src/ble_hs.c                   |    6 +-
 net/nimble/host/src/ble_hs_att.c               |  118 --
 net/nimble/host/src/ble_hs_att.h               |  171 ---
 net/nimble/host/src/ble_hs_att_clt.c           |  514 --------
 net/nimble/host/src/ble_hs_att_cmd.c           |  482 --------
 net/nimble/host/src/ble_hs_att_cmd.h           |  249 ----
 net/nimble/host/src/ble_hs_att_svr.c           | 1230 -------------------
 net/nimble/host/src/ble_hs_conn.c              |    6 +-
 net/nimble/host/src/ble_hs_conn.h              |    4 +-
 net/nimble/host/src/test/ble_host_hci_test.c   |    4 +-
 net/nimble/host/src/test/ble_hs_att_clt_test.c |   70 +-
 net/nimble/host/src/test/ble_hs_att_svr_test.c |  386 +++---
 net/nimble/host/src/test/ble_hs_conn_test.c    |   10 +-
 net/nimble/host/src/test/ble_hs_test.c         |    4 +-
 23 files changed, 3008 insertions(+), 3020 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index 50ed37d..d1d6084 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -3,10 +3,10 @@
 
 #include <inttypes.h>
 struct ble_hs_conn;
-struct ble_hs_att_error_rsp;
+struct ble_att_error_rsp;
 
 void ble_gatt_rx_error(struct ble_hs_conn *conn,
-                       struct ble_hs_att_error_rsp *rsp);
+                       struct ble_att_error_rsp *rsp);
 void ble_gatt_wakeup(void);
 void ble_gatt_rx_mtu(struct ble_hs_conn *conn, uint16_t chan_mtu);
 int ble_gatt_mtu(uint16_t conn_handle);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/include/host/ble_hs_test.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs_test.h b/net/nimble/host/include/host/ble_hs_test.h
index 874d92d..520c051 100644
--- a/net/nimble/host/include/host/ble_hs_test.h
+++ b/net/nimble/host/include/host/ble_hs_test.h
@@ -22,8 +22,8 @@ struct os_mbuf;
 void ble_hs_test_pkt_txed(struct os_mbuf *om);
 
 int l2cap_test_all(void);
-int ble_hs_att_svr_test_all(void);
-int ble_hs_att_clt_test_all(void);
+int ble_att_svr_test_all(void);
+int ble_att_clt_test_all(void);
 int ble_host_hci_test_all(void);
 int ble_hs_conn_test_all(void);
 int ble_gap_test_all(void);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/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
new file mode 100644
index 0000000..3d2dc7a
--- /dev/null
+++ b/net/nimble/host/src/ble_att.c
@@ -0,0 +1,118 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stddef.h>
+#include <errno.h>
+#include "ble_l2cap.h"
+#include "ble_att_cmd.h"
+#include "ble_att.h"
+
+/** 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);
+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[] = {
+    { BLE_ATT_OP_MTU_REQ,              ble_att_svr_rx_mtu },
+    { BLE_ATT_OP_MTU_RSP,              ble_att_clt_rx_mtu },
+    { BLE_ATT_OP_FIND_INFO_REQ,        ble_att_svr_rx_find_info },
+    { BLE_ATT_OP_FIND_INFO_RSP,        ble_att_clt_rx_find_info },
+    { BLE_ATT_OP_FIND_TYPE_VALUE_REQ,  ble_att_svr_rx_find_type_value },
+    { BLE_ATT_OP_READ_TYPE_REQ,        ble_att_svr_rx_read_type },
+    { BLE_ATT_OP_READ_REQ,             ble_att_svr_rx_read },
+    { BLE_ATT_OP_WRITE_REQ,            ble_att_svr_rx_write },
+};
+
+#define BLE_ATT_RX_DISPATCH_SZ \
+    (sizeof ble_att_rx_dispatch / sizeof ble_att_rx_dispatch[0])
+
+static struct ble_att_rx_dispatch_entry *
+ble_att_rx_dispatch_entry_find(uint8_t op)
+{
+    struct ble_att_rx_dispatch_entry *entry;
+    int i;
+
+    for (i = 0; i < BLE_ATT_RX_DISPATCH_SZ; i++) {
+        entry = ble_att_rx_dispatch + i;
+        if (entry->bde_op == op) {
+            return entry;
+        }
+
+        if (entry->bde_op > op) {
+            break;
+        }
+    }
+
+    return NULL;
+}
+
+static int
+ble_att_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+           struct os_mbuf *om)
+{
+    struct ble_att_rx_dispatch_entry *entry;
+    uint8_t op;
+    int rc;
+
+    rc = os_mbuf_copydata(om, 0, 1, &op);
+    if (rc != 0) {
+        return EMSGSIZE;
+    }
+
+    entry = ble_att_rx_dispatch_entry_find(op);
+    if (entry == NULL) {
+        return EINVAL;
+    }
+
+    rc = entry->bde_fn(conn, chan, om);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+void
+ble_att_set_peer_mtu(struct ble_l2cap_chan *chan, uint16_t peer_mtu)
+{
+    if (peer_mtu < BLE_ATT_MTU_DFLT) {
+        peer_mtu = BLE_ATT_MTU_DFLT;
+    }
+
+    chan->blc_peer_mtu = peer_mtu;
+}
+
+struct ble_l2cap_chan *
+ble_att_create_chan(void)
+{
+    struct ble_l2cap_chan *chan;
+
+    chan = ble_l2cap_chan_alloc();
+    if (chan == NULL) {
+        return NULL;
+    }
+
+    chan->blc_cid = BLE_L2CAP_CID_ATT;
+    chan->blc_my_mtu = BLE_ATT_MTU_DFLT;
+    chan->blc_default_mtu = BLE_ATT_MTU_DFLT;
+    chan->blc_rx_fn = ble_att_rx;
+
+    return chan;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/src/ble_att.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att.h b/net/nimble/host/src/ble_att.h
new file mode 100644
index 0000000..c14fca0
--- /dev/null
+++ b/net/nimble/host/src/ble_att.h
@@ -0,0 +1,166 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef H_BLE_HS_ATT_
+#define H_BLE_HS_ATT_
+
+#include <inttypes.h>
+#include "os/queue.h"
+struct os_mbuf;
+struct ble_hs_conn;
+struct ble_l2cap_chan;
+struct ble_att_find_info_req;
+struct ble_att_error_rsp;
+struct ble_att_mtu_cmd;
+struct ble_att_read_req;
+struct ble_att_read_group_type_req;
+struct ble_att_read_group_type_rsp;
+
+#define BLE_ATT_MTU_DFLT         23  /* Also the minimum. */
+#define BLE_ATT_MTU_MAX          256 /* XXX: I'm making this up! */
+
+#define BLE_ATT_ERR_INVALID_HANDLE       0x01
+#define BLE_ATT_ERR_INVALID_PDU          0x04
+#define BLE_ATT_ERR_REQ_NOT_SUPPORTED    0x06
+#define BLE_ATT_ERR_ATTR_NOT_FOUND       0x0a
+#define BLE_ATT_ERR_UNLIKELY             0x0e
+#define BLE_ATT_ERR_INSUFFICIENT_RES     0x11
+
+union ble_att_svr_handle_arg {
+    struct {
+        void *attr_data;
+        int attr_len;
+    } aha_read;
+
+    struct {
+        struct os_mbuf *om;
+        int attr_len;
+    } aha_write;
+};
+
+struct ble_att_svr_entry;
+
+/**
+ * Called from ble_att_svr_walk().  Called on each entry in the 
+ * ble_att_svr_list.
+ *
+ * @param Contains the current ble_att being iterated through
+ * @param The user supplied argument to ble_att_svr_walk()
+ *
+ * @return 0 on continue, 1 on stop
+ */
+typedef int (*ble_att_svr_walk_func_t)(struct ble_att_svr_entry *entry,
+                                       void *arg);
+
+/**
+ * Handles a host attribute request.
+ *
+ * @param entry                 The host attribute being requested.
+ * @param op                    The operation being performed on the attribute.
+ * @param arg                   The request data associated with that host
+ *                                  attribute.
+ *
+ * @return                      0 on success;
+ *                              One of the BLE_ATT_ERR_[...] codes on
+ *                                  failure.
+ */
+typedef int ble_att_svr_handle_func(struct ble_att_svr_entry *entry,
+                                    uint8_t op,
+                                    union ble_att_svr_handle_arg *arg);
+
+#define HA_FLAG_PERM_READ            (1 << 0)
+#define HA_FLAG_PERM_WRITE           (1 << 1) 
+#define HA_FLAG_PERM_RW              (1 << 2)
+#define HA_FLAG_ENC_REQ              (1 << 3)
+#define HA_FLAG_AUTHENTICATION_REQ   (1 << 4)
+#define HA_FLAG_AUTHORIZATION_REQ    (1 << 5)
+
+struct ble_att_svr_entry {
+    STAILQ_ENTRY(ble_att_svr_entry) ha_next;
+
+    uint8_t ha_uuid[16];
+    uint8_t ha_flags;
+    uint8_t ha_pad1;
+    uint16_t ha_handle_id;
+    ble_att_svr_handle_func *ha_fn;
+};
+
+#define HA_OPCODE_METHOD_START (0)
+#define HA_OPCODE_METHOD_END (5)
+#define HA_OPCODE_COMMAND_FLAG (1 << 6) 
+#define HA_OPCODE_AUTH_SIG_FLAG (1 << 7) 
+
+struct ble_att_clt_entry {
+    SLIST_ENTRY(ble_att_clt_entry) bhac_next;
+    uint16_t bhac_handle_id;
+    uint8_t bhac_uuid[16];
+};
+
+SLIST_HEAD(ble_att_clt_entry_list, ble_att_clt_entry);
+
+/*** @gen */
+struct ble_l2cap_chan *ble_att_create_chan(void);
+void ble_att_set_peer_mtu(struct ble_l2cap_chan *chan, uint16_t peer_mtu);
+
+/*** @svr */
+int ble_att_svr_register(uint8_t *uuid, uint8_t flags, uint16_t *handle_id,
+                         ble_att_svr_handle_func *fn);
+
+
+int ble_att_svr_rx_mtu(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                       struct os_mbuf *om);
+int ble_att_svr_rx_find_info(struct ble_hs_conn *conn,
+                             struct ble_l2cap_chan *chan,
+                             struct os_mbuf *om);
+int ble_att_svr_rx_find_type_value(struct ble_hs_conn *conn,
+                                   struct ble_l2cap_chan *chan,
+                                   struct os_mbuf *om);
+int ble_att_svr_rx_read_type(struct ble_hs_conn *conn,
+                             struct ble_l2cap_chan *chan,
+                             struct os_mbuf *om);
+int ble_att_svr_rx_read(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                        struct os_mbuf *om);
+int ble_att_svr_rx_write(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                         struct os_mbuf *om);
+int ble_att_svr_init(void);
+
+/*** @clt */
+void ble_att_clt_entry_list_free(struct ble_att_clt_entry_list *list);
+int ble_att_clt_entry_insert(struct ble_hs_conn *conn, uint16_t handle_id,
+                             uint8_t *uuid);
+uint16_t ble_att_clt_find_entry_uuid128(struct ble_hs_conn *conn,
+                                        void *uuid128);
+uint16_t ble_att_clt_find_entry_uuid16(struct ble_hs_conn *conn,
+                                       uint16_t uuid16);
+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 *om);
+int ble_att_clt_tx_read(struct ble_hs_conn *conn,
+                        struct ble_att_read_req *req);
+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 *om);
+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_rsp(struct ble_hs_conn *conn,
+                                       struct ble_l2cap_chan *chan,
+                                       struct os_mbuf *om);
+int ble_att_clt_init(void);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/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
new file mode 100644
index 0000000..6fe6a57
--- /dev/null
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -0,0 +1,511 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include "os/os_mempool.h"
+#include "nimble/ble.h"
+#include "host/ble_gatt.h"
+#include "host/ble_hs.h"
+#include "ble_hs_uuid.h"
+#include "ble_hs_conn.h"
+#include "ble_att_cmd.h"
+#include "ble_att.h"
+
+#define BLE_ATT_CLT_NUM_ENTRIES  128
+static void *ble_att_clt_entry_mem;
+static struct os_mempool ble_att_clt_entry_pool;
+
+static struct ble_att_clt_entry *
+ble_att_clt_entry_alloc(void)
+{
+    struct ble_att_clt_entry *entry;
+
+    entry = os_memblock_get(&ble_att_clt_entry_pool);
+    if (entry != NULL) {
+        memset(entry, 0, sizeof *entry);
+    }
+
+    return entry;
+}
+
+static void
+ble_att_clt_entry_free(struct ble_att_clt_entry *entry)
+{
+    int rc;
+
+    rc = os_memblock_put(&ble_att_clt_entry_pool, entry);
+    assert(rc == 0);
+}
+
+void
+ble_att_clt_entry_list_free(struct ble_att_clt_entry_list *list)
+{
+    struct ble_att_clt_entry *entry;
+
+    while ((entry = SLIST_FIRST(list)) != NULL) {
+        SLIST_REMOVE_HEAD(list, bhac_next);
+        ble_att_clt_entry_free(entry);
+    }
+}
+
+int
+ble_att_clt_entry_insert(struct ble_hs_conn *conn, uint16_t handle_id,
+                         uint8_t *uuid)
+{
+    struct ble_att_clt_entry *entry;
+    struct ble_att_clt_entry *prev;
+    struct ble_att_clt_entry *cur;
+
+    /* XXX: Probably need to lock a semaphore here. */
+
+    entry = ble_att_clt_entry_alloc();
+    if (entry == NULL) {
+        return ENOMEM;
+    }
+
+    entry->bhac_handle_id = handle_id;
+    memcpy(entry->bhac_uuid, uuid, sizeof entry->bhac_uuid);
+
+    prev = NULL;
+    SLIST_FOREACH(cur, &conn->bhc_att_clt_list, bhac_next) {
+        if (cur->bhac_handle_id == handle_id) {
+            return EEXIST;
+        }
+        if (cur->bhac_handle_id > handle_id) {
+            break;
+        }
+
+        prev = cur;
+    }
+
+    if (prev == NULL) {
+        SLIST_INSERT_HEAD(&conn->bhc_att_clt_list, entry, bhac_next);
+    } else {
+        SLIST_INSERT_AFTER(prev, entry, bhac_next);
+    }
+
+    return 0;
+}
+
+uint16_t
+ble_att_clt_find_entry_uuid128(struct ble_hs_conn *conn, void *uuid128)
+{
+    struct ble_att_clt_entry *entry;
+    int rc;
+
+    SLIST_FOREACH(entry, &conn->bhc_att_clt_list, bhac_next) {
+        rc = memcmp(entry->bhac_uuid, uuid128, 16);
+        if (rc == 0) {
+            return entry->bhac_handle_id;
+        }
+    }
+
+    return 0;
+}
+
+static int
+ble_att_clt_prep_req(struct ble_hs_conn *conn, struct ble_l2cap_chan **chan,
+                     struct os_mbuf **txom, uint16_t initial_sz)
+{
+    void *buf;
+    int rc;
+
+    *chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    assert(*chan != NULL);
+
+    *txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (*txom == NULL) {
+        rc = ENOMEM;
+        goto err;
+    }
+
+    buf = os_mbuf_extend(*txom, initial_sz);
+    if (buf == NULL) {
+        rc = ENOMEM;
+        goto err;
+    }
+
+    /* The caller expects the initial buffer to be at the start of the mbuf. */
+    assert(buf == (*txom)->om_data);
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(*txom);
+    *txom = NULL;
+    return rc;
+}
+
+uint16_t
+ble_att_clt_find_entry_uuid16(struct ble_hs_conn *conn, uint16_t uuid16)
+{
+    uint8_t uuid128[16];
+    uint16_t handle_id;
+    int rc;
+
+    rc = ble_hs_uuid_from_16bit(uuid16, uuid128);
+    if (rc != 0) {
+        return 0;
+    }
+
+    handle_id = ble_att_clt_find_entry_uuid128(conn, uuid128);
+    return handle_id;
+}
+
+int
+ble_att_clt_tx_mtu(struct ble_hs_conn *conn, struct ble_att_mtu_cmd *req)
+{
+    struct ble_l2cap_chan *chan;
+    struct os_mbuf *txom;
+    int rc;
+
+    txom = NULL;
+
+    if (req->bhamc_mtu < BLE_ATT_MTU_DFLT) {
+        rc = EINVAL;
+        goto err;
+    }
+
+    rc = ble_att_clt_prep_req(conn, &chan, &txom, BLE_ATT_MTU_CMD_SZ);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_att_mtu_req_write(txom->om_data, txom->om_len, req);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_att_clt_rx_mtu(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                   struct os_mbuf *om)
+{
+    struct ble_att_mtu_cmd rsp;
+    int rc;
+
+    /* XXX: Pull up om */
+
+    rc = ble_att_mtu_cmd_parse(om->om_data, om->om_len, &rsp);
+    if (rc != 0) {
+        return rc;
+    }
+
+    ble_att_set_peer_mtu(chan, rsp.bhamc_mtu);
+
+    ble_gatt_rx_mtu(conn, ble_l2cap_chan_mtu(chan));
+
+    return 0;
+}
+
+int
+ble_att_clt_tx_find_info(struct ble_hs_conn *conn,
+                         struct ble_att_find_info_req *req)
+{
+    struct ble_l2cap_chan *chan;
+    struct os_mbuf *txom;
+    int rc;
+
+    txom = NULL;
+
+    if (req->bhafq_start_handle == 0 ||
+        req->bhafq_start_handle > req->bhafq_end_handle) {
+
+        rc = EINVAL;
+        goto err;
+    }
+
+    rc = ble_att_clt_prep_req(conn, &chan, &txom,
+                                 BLE_ATT_FIND_INFO_REQ_SZ);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_att_find_info_req_write(txom->om_data, txom->om_len, req);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_att_clt_rx_find_info(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                         struct os_mbuf *om)
+{
+    struct ble_att_find_info_rsp rsp;
+    uint16_t handle_id;
+    uint16_t uuid16;
+    uint8_t uuid128[16];
+    int off;
+    int rc;
+
+    /* XXX: Pull up om */
+
+    rc = ble_att_find_info_rsp_parse(om->om_data, om->om_len, &rsp);
+    if (rc != 0) {
+        return rc;
+    }
+
+    handle_id = 0;
+    off = BLE_ATT_FIND_INFO_RSP_MIN_SZ;
+    while (off < OS_MBUF_PKTHDR(om)->omp_len) {
+        rc = os_mbuf_copydata(om, off, 2, &handle_id);
+        if (rc != 0) {
+            rc = EINVAL;
+            goto done;
+        }
+        off += 2;
+        handle_id = le16toh(&handle_id);
+
+        switch (rsp.bhafp_format) {
+        case BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT:
+            rc = os_mbuf_copydata(om, off, 2, &uuid16);
+            if (rc != 0) {
+                rc = EINVAL;
+                goto done;
+            }
+            off += 2;
+            uuid16 = le16toh(&uuid16);
+
+            rc = ble_hs_uuid_from_16bit(uuid16, uuid128);
+            if (rc != 0) {
+                rc = EINVAL;
+                goto done;
+            }
+            break;
+
+        case BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT:
+            rc = os_mbuf_copydata(om, off, 16, &uuid128);
+            if (rc != 0) {
+                rc = EINVAL;
+                goto done;
+            }
+            off += 16;
+            break;
+
+        default:
+            rc = EINVAL;
+            goto done;
+        }
+
+        rc = ble_att_clt_entry_insert(conn, handle_id, uuid128);
+        if (rc != 0) {
+            return rc;
+        }
+    }
+
+    rc = 0;
+
+done:
+    ble_gatt_rx_find_info(conn, -rc, handle_id);
+    return rc;
+}
+
+int
+ble_att_clt_tx_read(struct ble_hs_conn *conn, struct ble_att_read_req *req)
+{
+    struct ble_l2cap_chan *chan;
+    struct os_mbuf *txom;
+    int rc;
+
+    txom = NULL;
+
+    if (req->bharq_handle == 0) {
+        rc = EINVAL;
+        goto err;
+    }
+
+    rc = ble_att_clt_prep_req(conn, &chan, &txom, BLE_ATT_READ_REQ_SZ);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_att_read_req_write(txom->om_data, txom->om_len, req);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_att_clt_tx_read_group_type(struct ble_hs_conn *conn,
+                               struct ble_att_read_group_type_req *req,
+                               void *uuid128)
+{
+    struct ble_l2cap_chan *chan;
+    struct os_mbuf *txom;
+    int rc;
+
+    txom = NULL;
+
+    if (req->bhagq_start_handle == 0 ||
+        req->bhagq_start_handle > req->bhagq_end_handle) {
+
+        rc = EINVAL;
+        goto err;
+    }
+
+    rc = ble_att_clt_prep_req(conn, &chan, &txom,
+                                 BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_att_read_group_type_req_write(txom->om_data, txom->om_len,
+                                              req);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_hs_uuid_append(txom, uuid128);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+struct ble_att_clt_adata {
+    uint16_t att_handle;
+    uint16_t end_group_handle;
+    void *value;
+};
+
+static int
+ble_att_clt_parse_attribute_data(struct os_mbuf *om, int data_len,
+                                 struct ble_att_clt_adata *adata)
+{
+    /* XXX: Pull up om */
+
+    adata->att_handle = le16toh(om->om_data + 0);
+    adata->end_group_handle = le16toh(om->om_data + 2);
+    adata->value = om->om_data + 6;
+
+    return 0;
+}
+
+int
+ble_att_clt_rx_read_group_type_rsp(struct ble_hs_conn *conn,
+                                   struct ble_l2cap_chan *chan,
+                                   struct os_mbuf *om)
+{
+    struct ble_att_read_group_type_rsp rsp;
+    struct ble_att_clt_adata adata;
+    int rc;
+
+    /* XXX: Pull up om */
+
+    rc = ble_att_read_group_type_rsp_parse(om->om_data, om->om_len, &rsp);
+    if (rc != 0) {
+        return rc;
+    }
+
+    /* XXX: Verify group handle is valid. */
+
+    while (OS_MBUF_PKTHDR(om)->omp_len > 0) {
+        rc = ble_att_clt_parse_attribute_data(om, rsp.bhagp_length, &adata);
+        if (rc != 0) {
+            break;
+        }
+
+        /* Save attribute mapping? */
+
+        /* XXX: Pass adata to GATT callback. */
+
+        os_mbuf_adj(om, rsp.bhagp_length);
+    }
+
+    return 0;
+}
+
+int
+ble_att_clt_init(void)
+{
+    int rc;
+
+    free(ble_att_clt_entry_mem);
+    ble_att_clt_entry_mem = malloc(
+        OS_MEMPOOL_BYTES(BLE_ATT_CLT_NUM_ENTRIES,
+                         sizeof (struct ble_att_clt_entry)));
+    if (ble_att_clt_entry_mem == NULL) {
+        rc = ENOMEM;
+        goto err;
+    }
+
+    rc = os_mempool_init(&ble_att_clt_entry_pool,
+                         BLE_ATT_CLT_NUM_ENTRIES,
+                         sizeof (struct ble_att_clt_entry),
+                         ble_att_clt_entry_mem,
+                         "ble_att_clt_entry_pool");
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    free(ble_att_clt_entry_mem);
+    ble_att_clt_entry_mem = NULL;
+
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/src/ble_att_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_cmd.c b/net/nimble/host/src/ble_att_cmd.c
new file mode 100644
index 0000000..72306dd
--- /dev/null
+++ b/net/nimble/host/src/ble_att_cmd.c
@@ -0,0 +1,482 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <string.h>
+#include "os/os.h"
+#include "nimble/ble.h"
+#include "ble_hs_uuid.h"
+#include "ble_l2cap.h"
+#include "ble_att_cmd.h"
+
+int
+ble_att_error_rsp_parse(void *payload, int len,
+                           struct ble_att_error_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_ERROR_RSP_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_ERROR_RSP) {
+        return EINVAL;
+    }
+
+    rsp->bhaep_req_op = u8ptr[1];
+    rsp->bhaep_handle = le16toh(u8ptr + 2);
+    rsp->bhaep_error_code = u8ptr[4];
+
+    return 0;
+}
+
+int
+ble_att_error_rsp_write(void *payload, int len,
+                           struct ble_att_error_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_ERROR_RSP_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_ERROR_RSP;
+    u8ptr[1] = rsp->bhaep_req_op;
+    htole16(u8ptr + 2, rsp->bhaep_handle);
+    u8ptr[4] = rsp->bhaep_error_code;
+
+    return 0;
+}
+
+int
+ble_att_mtu_cmd_parse(void *payload, int len,
+                         struct ble_att_mtu_cmd *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_MTU_CMD_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_MTU_REQ &&
+        u8ptr[0] != BLE_ATT_OP_MTU_RSP) {
+
+        return EINVAL;
+    }
+
+    cmd->bhamc_mtu = le16toh(u8ptr + 1);
+
+    return 0;
+}
+
+int
+ble_att_mtu_req_write(void *payload, int len,
+                         struct ble_att_mtu_cmd *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_MTU_CMD_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_MTU_REQ;
+    htole16(u8ptr + 1, cmd->bhamc_mtu);
+
+    return 0;
+}
+
+int
+ble_att_mtu_rsp_write(void *payload, int len,
+                         struct ble_att_mtu_cmd *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_MTU_CMD_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_MTU_RSP;
+    htole16(u8ptr + 1, cmd->bhamc_mtu);
+
+    return 0;
+}
+
+int
+ble_att_find_info_req_parse(void *payload, int len,
+                               struct ble_att_find_info_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_FIND_INFO_REQ_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_FIND_INFO_REQ) {
+        return EINVAL;
+    }
+
+    req->bhafq_start_handle = le16toh(u8ptr + 1);
+    req->bhafq_end_handle = le16toh(u8ptr + 3);
+
+    return 0;
+}
+
+int
+ble_att_find_info_req_write(void *payload, int len,
+                               struct ble_att_find_info_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_FIND_INFO_REQ_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_FIND_INFO_REQ;
+    htole16(u8ptr + 1, req->bhafq_start_handle);
+    htole16(u8ptr + 3, req->bhafq_end_handle);
+
+    return 0;
+}
+
+int
+ble_att_find_info_rsp_parse(void *payload, int len,
+                               struct ble_att_find_info_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_FIND_INFO_RSP_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_FIND_INFO_RSP) {
+        return EINVAL;
+    }
+
+    rsp->bhafp_format = u8ptr[1];
+
+    return 0;
+}
+
+int
+ble_att_find_info_rsp_write(void *payload, int len,
+                               struct ble_att_find_info_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_FIND_INFO_RSP_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_FIND_INFO_RSP;
+    u8ptr[1] = rsp->bhafp_format;
+
+    return 0;
+}
+
+int
+ble_att_find_type_value_req_parse(
+    void *payload, int len, struct ble_att_find_type_value_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_FIND_TYPE_VALUE_REQ_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_FIND_TYPE_VALUE_REQ) {
+        return EINVAL;
+    }
+
+    req->bhavq_start_handle = le16toh(u8ptr + 1);
+    req->bhavq_end_handle = le16toh(u8ptr + 3);
+    req->bhavq_attr_type = le16toh(u8ptr + 5);
+
+    return 0;
+}
+
+int
+ble_att_find_type_value_req_write(
+    void *payload, int len, struct ble_att_find_type_value_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_FIND_TYPE_VALUE_REQ_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_FIND_TYPE_VALUE_REQ;
+    htole16(u8ptr + 1, req->bhavq_start_handle);
+    htole16(u8ptr + 3, req->bhavq_end_handle);
+    htole16(u8ptr + 5, req->bhavq_attr_type);
+
+    return 0;
+}
+
+int
+ble_att_read_type_req_parse(void *payload, int len,
+                               struct ble_att_read_type_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_TYPE_REQ_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_READ_TYPE_REQ) {
+        return EINVAL;
+    }
+
+    req->bhatq_start_handle = le16toh(u8ptr + 1);
+    req->bhatq_end_handle = le16toh(u8ptr + 3);
+
+    return 0;
+}
+
+int
+ble_att_read_type_req_write(void *payload, int len,
+                               struct ble_att_read_type_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_TYPE_REQ_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_READ_TYPE_REQ;
+    htole16(u8ptr + 1, req->bhatq_start_handle);
+    htole16(u8ptr + 3, req->bhatq_end_handle);
+
+    return 0;
+}
+
+int
+ble_att_read_type_rsp_parse(void *payload, int len,
+                               struct ble_att_read_type_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_TYPE_RSP_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    rsp->bhatp_len = u8ptr[1];
+
+    return 0;
+}
+
+int
+ble_att_read_type_rsp_write(void *payload, int len,
+                               struct ble_att_read_type_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_TYPE_RSP_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_READ_TYPE_RSP;
+    u8ptr[1] = rsp->bhatp_len;
+
+    return 0;
+}
+
+int
+ble_att_read_req_parse(void *payload, int len,
+                          struct ble_att_read_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_REQ_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_READ_REQ) {
+        return EINVAL;
+    }
+
+    req->bharq_handle = le16toh(u8ptr + 1);
+
+    return 0;
+}
+
+int
+ble_att_read_req_write(void *payload, int len,
+                          struct ble_att_read_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_REQ_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_READ_REQ;
+    htole16(u8ptr + 1, req->bharq_handle);
+
+    return 0;
+}
+
+int
+ble_att_read_group_type_req_parse(
+    void *payload, int len, struct ble_att_read_group_type_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_READ_GROUP_TYPE_REQ) {
+        return EINVAL;
+    }
+
+    req->bhagq_start_handle = le16toh(u8ptr + 1);
+    req->bhagq_end_handle = le16toh(u8ptr + 3);
+
+    return 0;
+}
+
+int
+ble_att_read_group_type_req_write(
+    void *payload, int len, struct ble_att_read_group_type_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_READ_GROUP_TYPE_REQ;
+    htole16(u8ptr + 1, req->bhagq_start_handle);
+    htole16(u8ptr + 3, req->bhagq_end_handle);
+
+    return 0;
+}
+
+int
+ble_att_read_group_type_rsp_parse(
+    void *payload, int len, struct ble_att_read_group_type_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_READ_GROUP_TYPE_RSP) {
+        return EINVAL;
+    }
+
+    rsp->bhagp_length = u8ptr[1];
+
+    return 0;
+}
+
+int
+ble_att_read_group_type_rsp_write(
+    void *payload, int len, struct ble_att_read_group_type_rsp *rsp)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_READ_GROUP_TYPE_RSP;
+    u8ptr[1] = rsp->bhagp_length;
+
+    return 0;
+}
+
+int
+ble_att_write_req_parse(void *payload, int len,
+                           struct ble_att_write_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_WRITE_REQ_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    if (u8ptr[0] != BLE_ATT_OP_WRITE_REQ) {
+        return EINVAL;
+    }
+
+    req->bhawq_handle = le16toh(u8ptr + 1);
+
+    return 0;
+}
+
+int
+ble_att_write_req_write(void *payload, int len,
+                           struct ble_att_write_req *req)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_ATT_WRITE_REQ_MIN_SZ) {
+        return EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_ATT_OP_WRITE_REQ;
+    htole16(u8ptr + 1, req->bhawq_handle);
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/src/ble_att_cmd.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_cmd.h b/net/nimble/host/src/ble_att_cmd.h
new file mode 100644
index 0000000..062a057
--- /dev/null
+++ b/net/nimble/host/src/ble_att_cmd.h
@@ -0,0 +1,249 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <inttypes.h>
+struct ble_l2cap_chan;
+
+#define BLE_ATT_OP_ERROR_RSP             0x01
+#define BLE_ATT_OP_MTU_REQ               0x02
+#define BLE_ATT_OP_MTU_RSP               0x03
+#define BLE_ATT_OP_FIND_INFO_REQ         0x04
+#define BLE_ATT_OP_FIND_INFO_RSP         0x05
+#define BLE_ATT_OP_FIND_TYPE_VALUE_REQ   0x06
+#define BLE_ATT_OP_FIND_TYPE_VALUE_RSP   0x07
+#define BLE_ATT_OP_READ_TYPE_REQ         0x08
+#define BLE_ATT_OP_READ_TYPE_RSP         0x09
+#define BLE_ATT_OP_READ_REQ              0x0a
+#define BLE_ATT_OP_READ_RSP              0x0b
+#define BLE_ATT_OP_READ_GROUP_TYPE_REQ   0x10
+#define BLE_ATT_OP_READ_GROUP_TYPE_RSP   0x11
+#define BLE_ATT_OP_WRITE_REQ             0x12
+#define BLE_ATT_OP_WRITE_RSP             0x13
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Request Opcode In Error            | 1                 |
+ * | Attribute Handle In Error          | 2                 |
+ * | Error Code                         | 1                 |
+ */
+#define BLE_ATT_ERROR_RSP_SZ             5
+struct ble_att_error_rsp {
+    uint8_t bhaep_req_op;
+    uint16_t bhaep_handle;
+    uint8_t bhaep_error_code;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Server Rx MTU                      | 2                 |
+ */
+#define BLE_ATT_MTU_CMD_SZ               3
+struct ble_att_mtu_cmd {
+    uint16_t bhamc_mtu;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Starting Handle                    | 2                 |
+ * | Ending Handle                      | 2                 |
+ */
+#define BLE_ATT_FIND_INFO_REQ_SZ         5
+struct ble_att_find_info_req {
+    uint16_t bhafq_start_handle;
+    uint16_t bhafq_end_handle;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Format                             | 1                 |
+ * | Information Data                   | 4 to (ATT_MTU-2)  |
+ */
+#define BLE_ATT_FIND_INFO_RSP_MIN_SZ     2
+struct ble_att_find_info_rsp {
+    uint8_t bhafp_format;
+    /* Followed by information data. */
+};
+
+#define BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT   1
+#define BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT  2
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Starting Handle                    | 2                 |
+ * | Ending Handle                      | 2                 |
+ * | Attribute Type                     | 2                 |
+ * | Attribute Value                    | 0 to (ATT_MTU-7)  |
+ */
+#define BLE_ATT_FIND_TYPE_VALUE_REQ_MIN_SZ   7
+struct ble_att_find_type_value_req {
+    uint16_t bhavq_start_handle;
+    uint16_t bhavq_end_handle;
+    uint16_t bhavq_attr_type;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Information Data                   | 4 to (ATT_MTU-1)  |
+ */
+#define BLE_ATT_FIND_TYPE_VALUE_RSP_MIN_SZ   1
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Starting Handle                    | 2                 |
+ * | Ending Handle                      | 2                 |
+ * | Attribute Type                     | 2 or 16           |
+ */
+#define BLE_ATT_READ_TYPE_REQ_MIN_SZ     7
+#define BLE_ATT_READ_TYPE_REQ_SZ_16      7
+#define BLE_ATT_READ_TYPE_REQ_SZ_128     21
+struct ble_att_read_type_req {
+    uint16_t bhatq_start_handle;
+    uint16_t bhatq_end_handle;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Length                             | 1                 |
+ * | Attribute Data List                | 2 to (ATT_MTU-2)  |
+ */
+#define BLE_ATT_READ_TYPE_RSP_MIN_SZ     2
+struct ble_att_read_type_rsp {
+    uint8_t bhatp_len;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Attribute Handle                   | 2                 |
+ */
+#define BLE_ATT_READ_REQ_SZ              3
+struct ble_att_read_req {
+    uint16_t bharq_handle;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Attribute Value                    | 0 to (ATT_MTU-1)  |
+ */
+#define BLE_ATT_READ_RSP_MIN_SZ          1
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Starting Handle                    | 2                 |
+ * | Ending Handle                      | 2                 |
+ * | Attribute Group Type               | 2 or 16           |
+ */
+#define BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ  5
+#define BLE_ATT_READ_GROUP_TYPE_REQ_SZ_16    7
+#define BLE_ATT_READ_GROUP_TYPE_REQ_SZ_128   21
+struct ble_att_read_group_type_req {
+    uint16_t bhagq_start_handle;
+    uint16_t bhagq_end_handle;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Length                             | 1                 |
+ * | Attribute Data List                | 2 to (ATT_MTU-2)  |
+ */
+#define BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ  2
+struct ble_att_read_group_type_rsp {
+    uint8_t bhagp_length;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | Attribute Opcode                   | 1                 |
+ * | Attribute Handle                   | 2                 |
+ * | Attribute Value                    | 0 to (ATT_MTU-3)  |
+ */
+#define BLE_ATT_WRITE_REQ_MIN_SZ         3
+struct ble_att_write_req {
+    uint16_t bhawq_handle;
+};
+
+#define BLE_ATT_WRITE_RSP_SZ             1
+
+int ble_att_error_rsp_parse(void *payload, int len,
+                               struct ble_att_error_rsp *rsp);
+int ble_att_error_rsp_write(void *payload, int len,
+                               struct ble_att_error_rsp *rsp);
+int ble_att_mtu_cmd_parse(void *payload, int len,
+                             struct ble_att_mtu_cmd *cmd);
+int ble_att_mtu_req_write(void *payload, int len,
+                             struct ble_att_mtu_cmd *cmd);
+int ble_att_mtu_rsp_write(void *payload, int len,
+                             struct ble_att_mtu_cmd *cmd);
+int ble_att_find_info_req_parse(void *payload, int len,
+                                   struct ble_att_find_info_req *req);
+int ble_att_find_info_req_write(void *payload, int len,
+                                   struct ble_att_find_info_req *req);
+int ble_att_find_info_rsp_parse(void *payload, int len,
+                                   struct ble_att_find_info_rsp *rsp);
+int ble_att_find_info_rsp_write(void *payload, int len,
+                                   struct ble_att_find_info_rsp *rsp);
+int ble_att_find_type_value_req_parse(
+    void *payload, int len, struct ble_att_find_type_value_req *req);
+int ble_att_find_type_value_req_write(
+    void *payload, int len, struct ble_att_find_type_value_req *req);
+int ble_att_read_req_parse(void *payload, int len,
+                              struct ble_att_read_req *req);
+int ble_att_read_req_write(void *payload, int len,
+                              struct ble_att_read_req *req);
+int ble_att_read_type_req_parse(void *payload, int len,
+                                   struct ble_att_read_type_req *req);
+int ble_att_read_type_req_write(void *payload, int len,
+                                   struct ble_att_read_type_req *req);
+int ble_att_read_type_rsp_parse(void *payload, int len,
+                                   struct ble_att_read_type_rsp *rsp);
+int ble_att_read_type_rsp_write(void *payload, int len,
+                                   struct ble_att_read_type_rsp *rsp);
+int ble_att_read_group_type_req_parse(
+    void *payload, int len, struct ble_att_read_group_type_req *req);
+int ble_att_read_group_type_req_write(
+    void *payload, int len, struct ble_att_read_group_type_req *req);
+int ble_att_read_group_type_rsp_parse(
+    void *payload, int len, struct ble_att_read_group_type_rsp *rsp);
+int ble_att_read_group_type_rsp_write(
+    void *payload, int len, struct ble_att_read_group_type_rsp *rsp);
+int ble_att_write_req_parse(void *payload, int len,
+                               struct ble_att_write_req *req);
+int ble_att_write_req_write(void *payload, int len,
+                               struct ble_att_write_req *req);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/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
new file mode 100644
index 0000000..39c8cc2
--- /dev/null
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -0,0 +1,1226 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include "os/os.h"
+#include "nimble/ble.h"
+#include "host/ble_hs.h"
+#include "ble_l2cap.h"
+#include "ble_hs_conn.h"
+#include "ble_hs_uuid.h"
+#include "ble_att_cmd.h"
+#include "ble_att.h"
+
+static STAILQ_HEAD(, ble_att_svr_entry) ble_att_svr_list;
+static uint16_t ble_att_svr_id;
+
+static struct os_mutex ble_att_svr_list_mutex;
+
+#define BLE_ATT_SVR_NUM_ENTRIES          32
+static void *ble_att_svr_entry_mem;
+static struct os_mempool ble_att_svr_entry_pool;
+
+/**
+ * Locks the host attribute list.
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+static void
+ble_att_svr_list_lock(void)
+{
+    int rc;
+
+    rc = os_mutex_pend(&ble_att_svr_list_mutex, OS_WAIT_FOREVER);
+    assert(rc == 0 || rc == OS_NOT_STARTED);
+}
+
+/**
+ * Unlocks the host attribute list
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+static void
+ble_att_svr_list_unlock(void)
+{
+    int rc;
+
+    rc = os_mutex_release(&ble_att_svr_list_mutex);
+    assert(rc == 0 || rc == OS_NOT_STARTED);
+}
+
+static struct ble_att_svr_entry *
+ble_att_svr_entry_alloc(void)
+{
+    struct ble_att_svr_entry *entry;
+
+    entry = os_memblock_get(&ble_att_svr_entry_pool);
+    if (entry != NULL) {
+        memset(entry, 0, sizeof *entry);
+    }
+
+    return entry;
+}
+
+#if 0
+static void
+ble_att_svr_entry_free(struct ble_att_svr_entry *entry)
+{
+    int rc;
+
+    rc = os_memblock_put(&ble_att_svr_entry_pool, entry);
+    assert(rc == 0);
+}
+#endif
+
+/**
+ * Allocate the next handle id and return it.
+ *
+ * @return A new 16-bit handle ID.
+ */
+static uint16_t
+ble_att_svr_next_id(void)
+{
+    /* Rollover is fatal. */
+    assert(ble_att_svr_id != UINT16_MAX);
+
+    return (++ble_att_svr_id);
+}
+
+/**
+ * Register a host attribute with the BLE stack.
+ *
+ * @param ha                    A filled out ble_att structure to register
+ * @param handle_id             A pointer to a 16-bit handle ID, which will be
+ *                                  the handle that is allocated.
+ * @param fn                    The callback function that gets executed when
+ *                                  the attribute is operated on.
+ *
+ * @return 0 on success, non-zero error code on failure.
+ */
+int
+ble_att_svr_register(uint8_t *uuid, uint8_t flags, uint16_t *handle_id,
+                     ble_att_svr_handle_func *fn)
+{
+    struct ble_att_svr_entry *entry;
+
+    entry = ble_att_svr_entry_alloc();
+    if (entry == NULL) {
+        return ENOMEM;
+    }
+
+    memcpy(&entry->ha_uuid, uuid, sizeof entry->ha_uuid);
+    entry->ha_flags = flags;
+    entry->ha_handle_id = ble_att_svr_next_id();
+    entry->ha_fn = fn;
+
+    ble_att_svr_list_lock();
+    STAILQ_INSERT_TAIL(&ble_att_svr_list, entry, ha_next);
+    ble_att_svr_list_unlock();
+
+    if (handle_id != NULL) {
+        *handle_id = entry->ha_handle_id;
+    }
+
+    return 0;
+}
+
+/**
+ * Walk the host attribute list, calling walk_func on each entry with argument.
+ * If walk_func wants to stop iteration, it returns 1.  To continue iteration
+ * it returns 0.
+ *
+ * @param walk_func The function to call for each element in the host attribute
+ *                  list.
+ * @param arg       The argument to provide to walk_func
+ * @param ha_ptr    On input: Indicates the starting point of the walk;
+ *                      null means start at the beginning of the list,
+ *                      non-null means start at the following entry.
+ *                  On output: Indicates the last ble_att element processed,
+ *                      or NULL if the entire list has been processed.
+ *
+ * @return 1 on stopped, 0 on fully processed and an error code otherwise.
+ */
+int
+ble_att_svr_walk(ble_att_svr_walk_func_t walk_func, void *arg,
+                 struct ble_att_svr_entry **ha_ptr)
+{
+    struct ble_att_svr_entry *ha;
+    int rc;
+
+    assert(ha_ptr != NULL);
+
+    ble_att_svr_list_lock();
+
+    if (*ha_ptr == NULL) {
+        ha = STAILQ_FIRST(&ble_att_svr_list);
+    } else {
+        ha = STAILQ_NEXT(*ha_ptr, ha_next);
+    }
+
+    while (ha != NULL) {
+        rc = walk_func(ha, arg);
+        if (rc == 1) {
+            *ha_ptr = ha;
+            goto done;
+        }
+
+        ha = STAILQ_NEXT(ha, ha_next);
+    }
+
+    rc = 0;
+
+done:
+    ble_att_svr_list_unlock();
+    return rc;
+}
+
+static int
+ble_att_svr_match_handle(struct ble_att_svr_entry *ha, void *arg)
+{
+    if (ha->ha_handle_id == *(uint16_t *) arg) {
+        return (1);
+    } else {
+        return (0);
+    }
+}
+
+
+/**
+ * Find a host attribute by handle id.
+ *
+ * @param handle_id             The handle_id to search for
+ * @param ha_ptr                A pointer to a pointer to put the matching host
+ *                                  attr into.
+ *
+ * @return                      0 on success; ENOENT on not found.
+ */
+int
+ble_att_svr_find_by_handle(uint16_t handle_id,
+                           struct ble_att_svr_entry **ha_ptr)
+{
+    int rc;
+
+    rc = ble_att_svr_walk(ble_att_svr_match_handle, &handle_id, ha_ptr);
+    if (rc == 1) {
+        /* Found a matching handle */
+        return 0;
+    } else {
+        /* Not found */
+        return ENOENT;
+    }
+}
+
+static int
+ble_att_svr_match_uuid(struct ble_att_svr_entry *ha, void *arg)
+{
+    uint8_t *uuid;
+
+    uuid = arg;
+
+    if (memcmp(ha->ha_uuid, uuid, sizeof ha->ha_uuid) == 0) {
+        return (1);
+    } else {
+        return (0);
+    }
+}
+
+/**
+ * Find a host attribute by UUID.
+ *
+ * @param uuid                  The ble_uuid_t to search for
+ * @param ha_ptr                A pointer to a pointer to put the matching host
+ *                                  attr into.
+ *
+ * @return                      0 on success; ENOENT on not found.
+ */
+int
+ble_att_svr_find_by_uuid(uint8_t *uuid,
+                         struct ble_att_svr_entry **ha_ptr)
+{
+    int rc;
+
+    rc = ble_att_svr_walk(ble_att_svr_match_uuid, uuid, ha_ptr);
+    if (rc == 1) {
+        /* Found a matching handle */
+        return 0;
+    } else {
+        /* No match */
+        return ENOENT;
+    }
+}
+
+static int
+ble_att_svr_tx_error_rsp(struct ble_l2cap_chan *chan, uint8_t req_op,
+                         uint16_t handle, uint8_t error_code)
+{
+    struct ble_att_error_rsp rsp;
+    struct os_mbuf *txom;
+    void *dst;
+    int rc;
+
+    txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (txom == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    dst = os_mbuf_extend(txom, BLE_ATT_ERROR_RSP_SZ);
+    if (dst == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    rsp.bhaep_req_op = req_op;
+    rsp.bhaep_handle = handle;
+    rsp.bhaep_error_code = error_code;
+
+    rc = ble_att_error_rsp_write(dst, BLE_ATT_ERROR_RSP_SZ, &rsp);
+    assert(rc == 0);
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+static int
+ble_att_svr_tx_mtu_rsp(struct ble_l2cap_chan *chan, uint8_t op, uint16_t mtu)
+{
+    struct ble_att_mtu_cmd cmd;
+    struct os_mbuf *txom;
+    void *dst;
+    int rc;
+
+    assert(op == BLE_ATT_OP_MTU_REQ || op == BLE_ATT_OP_MTU_RSP);
+    assert(mtu >= BLE_ATT_MTU_DFLT);
+
+    txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (txom == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    dst = os_mbuf_extend(txom, BLE_ATT_MTU_CMD_SZ);
+    if (dst == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    cmd.bhamc_mtu = mtu;
+
+    rc = ble_att_mtu_rsp_write(dst, BLE_ATT_MTU_CMD_SZ, &cmd);
+    assert(rc == 0);
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    chan->blc_flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_att_svr_rx_mtu(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                   struct os_mbuf *om)
+{
+    struct ble_att_mtu_cmd cmd;
+    int rc;
+
+    /* XXX: Pull up om. */
+
+    rc = ble_att_mtu_cmd_parse(om->om_data, om->om_len, &cmd);
+    assert(rc == 0);
+
+    ble_att_set_peer_mtu(chan, cmd.bhamc_mtu);
+    rc = ble_att_svr_tx_mtu_rsp(chan, BLE_ATT_OP_MTU_RSP,
+                                   chan->blc_my_mtu);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+/**
+ * Fills the supplied mbuf with the variable length Information Data field of a
+ * Find Information ATT response.
+ *
+ * @param req                   The Find Information request being responded
+ *                                  to.
+ * @param om                    The destination mbuf where the Information
+ *                                  Data field gets written.
+ * @param mtu                   The ATT L2CAP channel MTU.
+ * @param format                On success, the format field of the response
+ *                                  gets stored here.  One of:
+ *                                     o BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT
+ *                                     o BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT
+ *
+ * @return                      0 on success; an ATT error code on failure.
+ */
+static int
+ble_att_svr_fill_info(struct ble_att_find_info_req *req, struct os_mbuf *om,
+                      uint16_t mtu, uint8_t *format)
+{
+    struct ble_att_svr_entry *ha;
+    uint16_t handle_id;
+    uint16_t uuid16;
+    int num_entries;
+    int rsp_sz;
+    int rc;
+
+    *format = 0;
+    num_entries = 0;
+    rc = 0;
+
+    ble_att_svr_list_lock();
+
+    STAILQ_FOREACH(ha, &ble_att_svr_list, ha_next) {
+        if (ha->ha_handle_id > req->bhafq_end_handle) {
+            rc = 0;
+            goto done;
+        }
+        if (ha->ha_handle_id >= req->bhafq_start_handle) {
+            uuid16 = ble_hs_uuid_16bit(ha->ha_uuid);
+
+            if (*format == 0) {
+                if (uuid16 != 0) {
+                    *format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT;
+                } else {
+                    *format = BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT;
+                }
+            }
+
+            switch (*format) {
+            case BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT:
+                if (uuid16 == 0) {
+                    rc = 0;
+                    goto done;
+                }
+
+                rsp_sz = OS_MBUF_PKTHDR(om)->omp_len + 4;
+                if (rsp_sz > mtu) {
+                    rc = 0;
+                    goto done;
+                }
+
+                htole16(&handle_id, ha->ha_handle_id);
+                rc = os_mbuf_append(om, &handle_id, 2);
+                if (rc != 0) {
+                    goto done;
+                }
+
+                htole16(&uuid16, uuid16);
+                rc = os_mbuf_append(om, &uuid16, 2);
+                if (rc != 0) {
+                    goto done;
+                }
+                break;
+
+            case BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT:
+                if (uuid16 != 0) {
+                    rc = 0;
+                    goto done;
+                }
+
+                rsp_sz = OS_MBUF_PKTHDR(om)->omp_len + 18;
+                if (rsp_sz > mtu) {
+                    rc = 0;
+                    goto done;
+                }
+
+                htole16(&handle_id, ha->ha_handle_id);
+                rc = os_mbuf_append(om, &handle_id, 2);
+                if (rc != 0) {
+                    goto done;
+                }
+
+                rc = os_mbuf_append(om, &ha->ha_uuid,
+                                    16);
+                if (rc != 0) {
+                    goto done;
+                }
+                break;
+
+            default:
+                assert(0);
+                break;
+            }
+
+            num_entries++;
+        }
+    }
+
+done:
+    ble_att_svr_list_unlock();
+
+    if (rc == 0 && num_entries == 0) {
+        return ENOENT;
+    } else {
+        return rc;
+    }
+}
+
+int
+ble_att_svr_rx_find_info(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                         struct os_mbuf *rxom)
+{
+    struct ble_att_find_info_req req;
+    struct ble_att_find_info_rsp rsp;
+    struct os_mbuf *txom;
+    void *buf;
+    int rc;
+
+    txom = NULL;
+
+    /* XXX: Pull up rxom. */
+
+    rc = ble_att_find_info_req_parse(rxom->om_data, rxom->om_len, &req);
+    if (rc != 0) {
+        req.bhafq_start_handle = 0;
+        rc = BLE_ATT_ERR_INVALID_PDU;
+        goto err;
+    }
+
+    /* Tx error response if start handle is greater than end handle or is equal
+     * to 0 (Vol. 3, Part F, 3.4.3.1).
+     */
+    if (req.bhafq_start_handle > req.bhafq_end_handle ||
+        req.bhafq_start_handle == 0) {
+
+        rc = BLE_ATT_ERR_INVALID_HANDLE;
+        goto err;
+    }
+
+    txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (txom == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    /* Write the response base at the start of the buffer.  The format field is
+     * unknown at this point; it will be filled in later.
+     */
+    buf = os_mbuf_extend(txom, BLE_ATT_FIND_INFO_RSP_MIN_SZ);
+    if (buf == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    rc = ble_att_find_info_rsp_write(buf, BLE_ATT_FIND_INFO_RSP_MIN_SZ,
+                                        &rsp);
+    assert(rc == 0);
+
+    /* Write the variable length Information Data field, populating the format
+     * field as appropriate.
+     */
+    rc = ble_att_svr_fill_info(&req, txom, ble_l2cap_chan_mtu(chan),
+                                  txom->om_data + 1);
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_ATTR_NOT_FOUND;
+        goto err;
+    }
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    ble_att_svr_tx_error_rsp(chan, BLE_ATT_OP_FIND_INFO_REQ,
+                                req.bhafq_start_handle, rc);
+
+    return rc;
+}
+
+/**
+ * Processes a single non-matching attribute entry while filling a
+ * Find-By-Type-Value-Response.
+ *
+ * @param om                    The response mbuf.
+ * @param first                 Pointer to the first matching handle ID in the
+ *                                  current group of IDs.  0 if there is not a
+ *                                  current group.
+ * @param prev                  Pointer to the most recent matching handle ID
+ *                                  in the current group of IDs.  0 if there is
+ *                                  not a current group.
+ * @param mtu                   The ATT L2CAP channel MTU.
+ *
+ * @return                      0 if the response should be sent;
+ *                              EAGAIN if the entry was successfully processed
+ *                                  and subsequent entries can be inspected.
+ *                              Other nonzero on error.
+ */
+static int
+ble_att_svr_fill_type_value_no_match(struct os_mbuf *om, uint16_t *first,
+                                     uint16_t *prev, int mtu)
+{
+    uint16_t u16;
+    int rsp_sz;
+    int rc;
+
+    /* If there is no current group, then there is nothing to do. */
+    if (*first == 0) {
+        return EAGAIN;
+    }
+
+    rsp_sz = OS_MBUF_PKTHDR(om)->omp_len + 4;
+    if (rsp_sz > mtu) {
+        return 0;
+    }
+
+    u16 = *first;
+    htole16(&u16, u16);
+    rc = os_mbuf_append(om, &u16, 2);
+    if (rc != 0) {
+        return ENOMEM;
+    }
+
+    u16 = *prev;
+    htole16(&u16, u16);
+    rc = os_mbuf_append(om, &u16, 2);
+    if (rc != 0) {
+        return ENOMEM;
+    }
+
+    *first = 0;
+    *prev = 0;
+
+    return EAGAIN;
+}
+
+/**
+ * Processes a single matching attribute entry while filling a
+ * Find-By-Type-Value-Response.
+ *
+ * @param om                    The response mbuf.
+ * @param first                 Pointer to the first matching handle ID in the
+ *                                  current group of IDs.  0 if there is not a
+ *                                  current group.
+ * @param prev                  Pointer to the most recent matching handle ID
+ *                                  in the current group of IDs.  0 if there is
+ *                                  not a current group.
+ * @param handle_id             The matching handle ID to process.
+ * @param mtu                   The ATT L2CAP channel MTU.
+ *
+ * @return                      0 if the response should be sent;
+ *                              EAGAIN if the entry was successfully processed
+ *                                  and subsequent entries can be inspected.
+ *                              Other nonzero on error.
+ */
+static int
+ble_att_svr_fill_type_value_match(struct os_mbuf *om, uint16_t *first,
+                                  uint16_t *prev, uint16_t handle_id,
+                                  int mtu)
+{
+    int rc;
+
+    /* If this is the start of a group, record it as the first ID and keep
+     * searching.
+     */
+    if (*first == 0) {
+        *first = handle_id;
+        *prev = handle_id;
+        return EAGAIN;
+    }
+
+    /* If this is the continuation of a group, keep searching. */
+    if (handle_id == *prev + 1) {
+        *prev = handle_id;
+        return EAGAIN;
+    }
+
+    /* Otherwise, this handle is not a part of the previous group.  Write the
+     * previous group to the response, and remember this ID as the start of the
+     * next group.
+     */
+    rc = ble_att_svr_fill_type_value_no_match(om, first, prev, mtu);
+    *first = handle_id;
+    *prev = handle_id;
+    return rc;
+}
+
+/**
+ * Fills the supplied mbuf with the variable length Handles-Information-List
+ * field of a Find-By-Type-Value ATT response.
+ *
+ * @param req                   The Find-By-Type-Value-Request being responded
+ *                                  to.
+ * @param rxom                  The mbuf containing the received request.
+ * @param txom                  The destination mbuf where the
+ *                                  Handles-Information-List field gets
+ *                                  written.
+ * @param mtu                   The ATT L2CAP channel MTU.
+ *
+ * @return                      0 on success; an ATT error code on failure.
+ */
+static int
+ble_att_svr_fill_type_value(struct ble_att_find_type_value_req *req,
+                            struct os_mbuf *rxom, struct os_mbuf *txom,
+                            uint16_t mtu)
+{
+    union ble_att_svr_handle_arg arg;
+    struct ble_att_svr_entry *ha;
+    uint16_t uuid16;
+    uint16_t first;
+    uint16_t prev;
+    int any_entries;
+    int match;
+    int rc;
+
+    first = 0;
+    prev = 0;
+    rc = 0;
+
+    ble_att_svr_list_lock();
+
+    /* Iterate through the attribute list, keeping track of the current
+     * matching group.  For each attribute entry, determine if data needs to be
+     * written to the response.
+     */
+    STAILQ_FOREACH(ha, &ble_att_svr_list, ha_next) {
+        match = 0;
+
+        if (ha->ha_handle_id > req->bhavq_end_handle) {
+            break;
+        }
+
+        if (ha->ha_handle_id >= req->bhavq_start_handle) {
+            /* Compare the attribute type and value to the request fields to
+             * determine if this attribute matches.
+             */
+            uuid16 = ble_hs_uuid_16bit(ha->ha_uuid);
+            if (uuid16 == req->bhavq_attr_type) {
+                rc = ha->ha_fn(ha, BLE_ATT_OP_READ_REQ, &arg);
+                if (rc != 0) {
+                    rc = BLE_ATT_ERR_UNLIKELY;
+                    goto done;
+                }
+                rc = os_mbuf_memcmp(rxom,
+                                    BLE_ATT_FIND_TYPE_VALUE_REQ_MIN_SZ,
+                                    arg.aha_read.attr_data,
+                                    arg.aha_read.attr_len);
+                if (rc == 0) {
+                    match = 1;
+                }
+            }
+        }
+
+        if (match) {
+            rc = ble_att_svr_fill_type_value_match(txom, &first, &prev,
+                                                      ha->ha_handle_id, mtu);
+        } else {
+            rc = ble_att_svr_fill_type_value_no_match(txom, &first, &prev,
+                                                         mtu);
+        }
+
+        if (rc == 0) {
+            goto done;
+        }
+        if (rc != EAGAIN) {
+            rc = BLE_ATT_ERR_UNLIKELY;
+            goto done;
+        }
+    }
+
+    /* Process one last non-matching ID in case a group was in progress when
+     * the end of the attribute list was reached.
+     */
+    rc = ble_att_svr_fill_type_value_no_match(txom, &first, &prev, mtu);
+    if (rc == EAGAIN) {
+        rc = 0;
+    } else if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+    }
+
+done:
+    ble_att_svr_list_unlock();
+
+    any_entries = OS_MBUF_PKTHDR(txom)->omp_len >
+                  BLE_ATT_FIND_TYPE_VALUE_RSP_MIN_SZ;
+    if (rc == 0 && !any_entries) {
+        return BLE_ATT_ERR_ATTR_NOT_FOUND;
+    } else {
+        return rc;
+    }
+}
+
+int
+ble_att_svr_rx_find_type_value(struct ble_hs_conn *conn,
+                               struct ble_l2cap_chan *chan,
+                               struct os_mbuf *rxom)
+{
+    struct ble_att_find_type_value_req req;
+    struct os_mbuf *txom;
+    uint8_t *buf;
+    int rc;
+
+    txom = NULL;
+
+    /* XXX: Pull up rx_om. */
+
+    rc = ble_att_find_type_value_req_parse(rxom->om_data, rxom->om_len,
+                                              &req);
+    assert(rc == 0);
+
+    /* Tx error response if start handle is greater than end handle or is equal
+     * to 0 (Vol. 3, Part F, 3.4.3.3).
+     */
+    if (req.bhavq_start_handle > req.bhavq_end_handle ||
+        req.bhavq_start_handle == 0) {
+
+        rc = BLE_ATT_ERR_INVALID_HANDLE;
+        goto err;
+    }
+
+    txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (txom == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    /* Write the response base at the start of the buffer. */
+    buf = os_mbuf_extend(txom, BLE_ATT_FIND_TYPE_VALUE_RSP_MIN_SZ);
+    if (buf == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+    buf[0] = BLE_ATT_OP_FIND_TYPE_VALUE_RSP;
+
+    /* Write the variable length Information Data field. */
+    rc = ble_att_svr_fill_type_value(&req, rxom, txom,
+                                        ble_l2cap_chan_mtu(chan));
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    ble_att_svr_tx_error_rsp(chan, BLE_ATT_OP_FIND_TYPE_VALUE_REQ,
+                            req.bhavq_start_handle, rc);
+    return rc;
+}
+
+static int
+ble_att_svr_tx_read_type_rsp(struct ble_hs_conn *conn,
+                             struct ble_l2cap_chan *chan,
+                             struct ble_att_read_type_req *req,
+                             uint8_t *uuid128)
+{
+    struct ble_att_read_type_rsp rsp;
+    union ble_att_svr_handle_arg arg;
+    struct ble_att_svr_entry *entry;
+    struct os_mbuf *txom;
+    uint8_t *dptr;
+    int txomlen;
+    int prev_attr_len;
+    int attr_len;
+    int rc;
+
+    txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (txom == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    /* Allocate space for the respose base, but don't fill in the fields.  They
+     * get filled in at the end, when we know the value of the length field.
+     */
+    dptr = os_mbuf_extend(txom,
+                          BLE_ATT_READ_TYPE_RSP_MIN_SZ);
+    if (dptr == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    /* Find all matching attributes, writing a record for each. */
+    prev_attr_len = 0;
+    entry = NULL;
+    while (1) {
+        rc = ble_att_svr_find_by_uuid(uuid128, &entry);
+        if (rc == ENOENT) {
+            break;
+        } else if (rc != 0) {
+            rc = BLE_ATT_ERR_UNLIKELY;
+            goto err;
+        }
+
+        if (entry->ha_handle_id > req->bhatq_end_handle) {
+            break;
+        }
+
+        if (entry->ha_handle_id >= req->bhatq_start_handle &&
+            entry->ha_handle_id <= req->bhatq_end_handle) {
+
+            rc = entry->ha_fn(entry, BLE_ATT_OP_READ_REQ, &arg);
+            if (rc != 0) {
+                rc = BLE_ATT_ERR_UNLIKELY;
+                goto err;
+            }
+
+            if (arg.aha_read.attr_len > ble_l2cap_chan_mtu(chan) - 4) {
+                attr_len = ble_l2cap_chan_mtu(chan) - 4;
+            } else {
+                attr_len = arg.aha_read.attr_len;
+            }
+
+            if (prev_attr_len == 0) {
+                prev_attr_len = attr_len;
+            } else if (prev_attr_len != attr_len) {
+                break;
+            }
+
+            txomlen = OS_MBUF_PKTHDR(txom)->omp_len + 2 + attr_len;
+            if (txomlen > ble_l2cap_chan_mtu(chan)) {
+                break;
+            }
+
+            dptr = os_mbuf_extend(txom, 2 + attr_len);
+            if (dptr == NULL) {
+                rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+                goto err;
+            }
+
+            htole16(dptr + 0, entry->ha_handle_id);
+            memcpy(dptr + 2, arg.aha_read.attr_data, attr_len);
+        }
+    }
+
+    if (prev_attr_len == 0) {
+        /* No matching attributes. */
+        rc = BLE_ATT_ERR_ATTR_NOT_FOUND;
+        goto err;
+    }
+
+    /* Fill the response base. */
+    rsp.bhatp_len = prev_attr_len;
+    rc = ble_att_read_type_rsp_write(txom->om_data, txom->om_len, &rsp);
+    assert(rc == 0);
+
+    /* Transmit the response. */
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    ble_att_svr_tx_error_rsp(chan, BLE_ATT_OP_READ_TYPE_REQ,
+                                req->bhatq_start_handle, rc);
+    return rc;
+}
+
+int
+ble_att_svr_rx_read_type(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                         struct os_mbuf *rx_om)
+{
+    struct ble_att_read_type_req req;
+    uint16_t uuid16;
+    uint8_t uuid128[16];
+    int rc;
+
+    /* XXX: Pull up mbuf. */
+
+    rc = ble_att_read_type_req_parse(rx_om->om_data, rx_om->om_len, &req);
+    if (rc != 0) {
+        goto done;
+    }
+
+    switch (rx_om->om_len) {
+    case BLE_ATT_READ_TYPE_REQ_SZ_16:
+        uuid16 = le16toh(rx_om->om_data + 5);
+        rc = ble_hs_uuid_from_16bit(uuid16, uuid128);
+        if (rc != 0) {
+            goto done;
+        }
+        break;
+
+    case BLE_ATT_READ_TYPE_REQ_SZ_128:
+        memcpy(uuid128, rx_om->om_data + 5, 16);
+        break;
+
+    default:
+        rc = EMSGSIZE;
+        goto done;
+    }
+
+    rc = ble_att_svr_tx_read_type_rsp(conn, chan, &req, uuid128);
+    if (rc != 0) {
+        goto done;
+    }
+
+    rc = 0;
+
+done:
+    os_mbuf_free_chain(rx_om);
+    return rc;
+}
+
+static int
+ble_att_svr_tx_read_rsp(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                        void *attr_data, int attr_len)
+{
+    struct os_mbuf *txom;
+    uint16_t data_len;
+    uint8_t op;
+    int rc;
+
+    txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (txom == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    op = BLE_ATT_OP_READ_RSP;
+    rc = os_mbuf_append(txom, &op, 1);
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    /* Vol. 3, part F, 3.2.9; don't send more than ATT_MTU-1 bytes of data. */
+    if (attr_len > ble_l2cap_chan_mtu(chan) - 1) {
+        data_len = ble_l2cap_chan_mtu(chan) - 1;
+    } else {
+        data_len = attr_len;
+    }
+
+    rc = os_mbuf_append(txom, attr_data, data_len);
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    rc = ble_l2cap_tx(chan, txom);
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_att_svr_rx_read(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                    struct os_mbuf *om)
+{
+    union ble_att_svr_handle_arg arg;
+    struct ble_att_svr_entry *entry;
+    struct ble_att_read_req req;
+    uint8_t buf[BLE_ATT_READ_REQ_SZ];
+    int rc;
+
+    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
+    if (rc != 0) {
+        req.bharq_handle = 0;
+        rc = BLE_ATT_ERR_INVALID_PDU;
+        goto err;
+    }
+
+    rc = ble_att_read_req_parse(buf, sizeof buf, &req);
+    assert(rc == 0);
+
+    entry = NULL;
+    rc = ble_att_svr_find_by_handle(req.bharq_handle, &entry);
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_INVALID_HANDLE;
+        goto err;
+    }
+
+    if (entry->ha_fn == NULL) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    rc = entry->ha_fn(entry, BLE_ATT_OP_READ_REQ, &arg);
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    rc = ble_att_svr_tx_read_rsp(conn, chan, arg.aha_read.attr_data,
+                                arg.aha_read.attr_len);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    ble_att_svr_tx_error_rsp(chan, BLE_ATT_OP_READ_REQ,
+                            req.bharq_handle, rc);
+    return rc;
+}
+
+static int
+ble_att_svr_tx_write_rsp(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
+{
+    struct os_mbuf *txom;
+    uint8_t *dst;
+    int rc;
+
+    txom = os_mbuf_get_pkthdr(&ble_hs_mbuf_pool, 0);
+    if (txom == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    dst = os_mbuf_extend(txom, BLE_ATT_WRITE_RSP_SZ);
+    if (dst == NULL) {
+        rc = BLE_ATT_ERR_INSUFFICIENT_RES;
+        goto err;
+    }
+
+    *dst = BLE_ATT_OP_WRITE_RSP;
+    rc = ble_l2cap_tx(chan, txom);
+    txom = NULL;
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_att_svr_rx_write(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
+                     struct os_mbuf *om)
+{
+    union ble_att_svr_handle_arg arg;
+    struct ble_att_svr_entry *entry;
+    struct ble_att_write_req req;
+    uint8_t buf[BLE_ATT_WRITE_REQ_MIN_SZ];
+    int rc;
+
+    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_att_write_req_parse(buf, sizeof buf, &req);
+    assert(rc == 0);
+
+    entry = NULL;
+    rc = ble_att_svr_find_by_handle(req.bhawq_handle, &entry);
+    if (rc != 0) {
+        rc = BLE_ATT_ERR_INVALID_HANDLE;
+        goto send_err;
+    }
+
+    if (entry->ha_fn == NULL) {
+        rc = BLE_ATT_ERR_UNLIKELY;
+        goto send_err;
+    }
+
+    arg.aha_write.om = om;
+    arg.aha_write.attr_len = OS_MBUF_PKTHDR(om)->omp_len;
+    rc = entry->ha_fn(entry, BLE_ATT_OP_WRITE_REQ, &arg);
+    if (rc != 0) {
+        goto send_err;
+    }
+
+    rc = ble_att_svr_tx_write_rsp(conn, chan);
+    if (rc != 0) {
+        goto send_err;
+    }
+
+    return 0;
+
+send_err:
+    ble_att_svr_tx_error_rsp(chan, BLE_ATT_OP_WRITE_REQ,
+                                req.bhawq_handle, rc);
+    return rc;
+}
+
+int
+ble_att_svr_init(void)
+{
+    int rc;
+
+    STAILQ_INIT(&ble_att_svr_list);
+
+    rc = os_mutex_init(&ble_att_svr_list_mutex);
+    if (rc != 0) {
+        goto err;
+    }
+
+    free(ble_att_svr_entry_mem);
+    ble_att_svr_entry_mem = malloc(
+        OS_MEMPOOL_BYTES(BLE_ATT_SVR_NUM_ENTRIES,
+                         sizeof (struct ble_att_svr_entry)));
+    if (ble_att_svr_entry_mem == NULL) {
+        rc = ENOMEM;
+        goto err;
+    }
+
+    rc = os_mempool_init(&ble_att_svr_entry_pool,
+                         BLE_ATT_SVR_NUM_ENTRIES,
+                         sizeof (struct ble_att_svr_entry),
+                         ble_att_svr_entry_mem,
+                         "ble_att_svr_entry_pool");
+    if (rc != 0) {
+        goto err;
+    }
+
+    return 0;
+
+err:
+    free(ble_att_svr_entry_mem);
+    ble_att_svr_entry_mem = NULL;
+
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/src/ble_gatt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatt.c b/net/nimble/host/src/ble_gatt.c
index c5c2764..6b52b96 100644
--- a/net/nimble/host/src/ble_gatt.c
+++ b/net/nimble/host/src/ble_gatt.c
@@ -23,8 +23,8 @@
 #include "host/ble_gatt.h"
 #include "host/ble_hs.h"
 #include "ble_hs_conn.h"
-#include "ble_hs_att_cmd.h"
-#include "ble_hs_att.h"
+#include "ble_att_cmd.h"
+#include "ble_att.h"
 
 struct ble_gatt_entry {
     STAILQ_ENTRY(ble_gatt_entry) next;
@@ -211,7 +211,7 @@ ble_gatt_new_entry(uint16_t conn_handle, struct ble_gatt_entry **entry)
 static int
 ble_gatt_kick_mtu(struct ble_gatt_entry *entry)
 {
-    struct ble_hs_att_mtu_cmd req;
+    struct ble_att_mtu_cmd req;
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
     int rc;
@@ -225,7 +225,7 @@ ble_gatt_kick_mtu(struct ble_gatt_entry *entry)
     assert(chan != NULL);
 
     req.bhamc_mtu = chan->blc_my_mtu;
-    rc = ble_hs_att_clt_tx_mtu(conn, &req);
+    rc = ble_att_clt_tx_mtu(conn, &req);
     if (rc != 0) {
         return rc;
     }
@@ -236,7 +236,7 @@ ble_gatt_kick_mtu(struct ble_gatt_entry *entry)
 static int
 ble_gatt_kick_find_info(struct ble_gatt_entry *entry)
 {
-    struct ble_hs_att_find_info_req req;
+    struct ble_att_find_info_req req;
     struct ble_hs_conn *conn;
     int rc;
 
@@ -247,7 +247,7 @@ ble_gatt_kick_find_info(struct ble_gatt_entry *entry)
 
     req.bhafq_start_handle = entry->find_info.next_handle;
     req.bhafq_end_handle = entry->find_info.end_handle;
-    rc = ble_hs_att_clt_tx_find_info(conn, &req);
+    rc = ble_att_clt_tx_find_info(conn, &req);
     if (rc != 0) {
         return rc;
     }
@@ -288,7 +288,7 @@ ble_gatt_wakeup(void)
 }
 
 void
-ble_gatt_rx_error(struct ble_hs_conn *conn, struct ble_hs_att_error_rsp *rsp)
+ble_gatt_rx_error(struct ble_hs_conn *conn, struct ble_att_error_rsp *rsp)
 {
     struct ble_gatt_entry *entry;
     struct ble_gatt_entry *prev;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 916cc1a..519e3e7 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -21,7 +21,7 @@
 #include "host/host_hci.h"
 #include "host/ble_gatt.h"
 #include "host/ble_hs.h"
-#include "ble_hs_att.h"
+#include "ble_att.h"
 #include "ble_hs_conn.h"
 #include "ble_hs_ack.h"
 #include "ble_hs_hci_batch.h"
@@ -319,12 +319,12 @@ ble_hs_init(uint8_t prio)
         goto err;
     }
 
-    rc = ble_hs_att_svr_init();
+    rc = ble_att_svr_init();
     if (rc != 0) {
         goto err;
     }
 
-    rc = ble_hs_att_clt_init();
+    rc = ble_att_clt_init();
     if (rc != 0) {
         goto err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/31f0c10b/net/nimble/host/src/ble_hs_att.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_att.c b/net/nimble/host/src/ble_hs_att.c
deleted file mode 100644
index 7d20934..0000000
--- a/net/nimble/host/src/ble_hs_att.c
+++ /dev/null
@@ -1,118 +0,0 @@
-/**
- * Copyright (c) 2015 Runtime Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stddef.h>
-#include <errno.h>
-#include "ble_l2cap.h"
-#include "ble_hs_att_cmd.h"
-#include "ble_hs_att.h"
-
-/** Dispatch table for incoming ATT requests.  Sorted by op code. */
-typedef int ble_hs_att_rx_fn(struct ble_hs_conn *conn,
-                                 struct ble_l2cap_chan *chan,
-                                 struct os_mbuf *om);
-struct ble_hs_att_rx_dispatch_entry {
-    uint8_t bde_op;
-    ble_hs_att_rx_fn *bde_fn;
-};
-
-static struct ble_hs_att_rx_dispatch_entry ble_hs_att_rx_dispatch[] = {
-    { BLE_HS_ATT_OP_MTU_REQ,              ble_hs_att_svr_rx_mtu },
-    { BLE_HS_ATT_OP_MTU_RSP,              ble_hs_att_clt_rx_mtu },
-    { BLE_HS_ATT_OP_FIND_INFO_REQ,        ble_hs_att_svr_rx_find_info },
-    { BLE_HS_ATT_OP_FIND_INFO_RSP,        ble_hs_att_clt_rx_find_info },
-    { BLE_HS_ATT_OP_FIND_TYPE_VALUE_REQ,  ble_hs_att_svr_rx_find_type_value },
-    { BLE_HS_ATT_OP_READ_TYPE_REQ,        ble_hs_att_svr_rx_read_type },
-    { BLE_HS_ATT_OP_READ_REQ,             ble_hs_att_svr_rx_read },
-    { BLE_HS_ATT_OP_WRITE_REQ,            ble_hs_att_svr_rx_write },
-};
-
-#define BLE_HS_ATT_RX_DISPATCH_SZ \
-    (sizeof ble_hs_att_rx_dispatch / sizeof ble_hs_att_rx_dispatch[0])
-
-static struct ble_hs_att_rx_dispatch_entry *
-ble_hs_att_rx_dispatch_entry_find(uint8_t op)
-{
-    struct ble_hs_att_rx_dispatch_entry *entry;
-    int i;
-
-    for (i = 0; i < BLE_HS_ATT_RX_DISPATCH_SZ; i++) {
-        entry = ble_hs_att_rx_dispatch + i;
-        if (entry->bde_op == op) {
-            return entry;
-        }
-
-        if (entry->bde_op > op) {
-            break;
-        }
-    }
-
-    return NULL;
-}
-
-static int
-ble_hs_att_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-              struct os_mbuf *om)
-{
-    struct ble_hs_att_rx_dispatch_entry *entry;
-    uint8_t op;
-    int rc;
-
-    rc = os_mbuf_copydata(om, 0, 1, &op);
-    if (rc != 0) {
-        return EMSGSIZE;
-    }
-
-    entry = ble_hs_att_rx_dispatch_entry_find(op);
-    if (entry == NULL) {
-        return EINVAL;
-    }
-
-    rc = entry->bde_fn(conn, chan, om);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
-}
-
-void
-ble_hs_att_set_peer_mtu(struct ble_l2cap_chan *chan, uint16_t peer_mtu)
-{
-    if (peer_mtu < BLE_HS_ATT_MTU_DFLT) {
-        peer_mtu = BLE_HS_ATT_MTU_DFLT;
-    }
-
-    chan->blc_peer_mtu = peer_mtu;
-}
-
-struct ble_l2cap_chan *
-ble_hs_att_create_chan(void)
-{
-    struct ble_l2cap_chan *chan;
-
-    chan = ble_l2cap_chan_alloc();
-    if (chan == NULL) {
-        return NULL;
-    }
-
-    chan->blc_cid = BLE_L2CAP_CID_ATT;
-    chan->blc_my_mtu = BLE_HS_ATT_MTU_DFLT;
-    chan->blc_default_mtu = BLE_HS_ATT_MTU_DFLT;
-    chan->blc_rx_fn = ble_hs_att_rx;
-
-    return chan;
-}