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/01/19 23:27:36 UTC

[2/4] incubator-mynewt-larva git commit: Sort services and characteristics by handle.

Sort services and characteristics by handle.


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

Branch: refs/heads/master
Commit: a2b4dd5e4422482ff229db1506e472e7a1f4ad94
Parents: 2da25ba
Author: Christopher Collins <cc...@gmail.com>
Authored: Tue Jan 19 12:11:52 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Tue Jan 19 14:23:32 2016 -0800

----------------------------------------------------------------------
 project/bleshell/src/bleshell_priv.h |  11 ++-
 project/bleshell/src/cmd.c           |  98 ++++++++++++++++++----
 project/bleshell/src/main.c          | 131 +++++++++++++++++++++++++-----
 project/bleshell/src/periph.c        |  74 +++++++++--------
 4 files changed, 242 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a2b4dd5e/project/bleshell/src/bleshell_priv.h
----------------------------------------------------------------------
diff --git a/project/bleshell/src/bleshell_priv.h b/project/bleshell/src/bleshell_priv.h
index efa5813..6ecfc65 100644
--- a/project/bleshell/src/bleshell_priv.h
+++ b/project/bleshell/src/bleshell_priv.h
@@ -20,20 +20,20 @@ struct kv_pair {
 };
 
 struct bleshell_chr {
-    STAILQ_ENTRY(bleshell_chr) next;
+    SLIST_ENTRY(bleshell_chr) next;
     struct ble_gatt_chr chr;
 };
 
-STAILQ_HEAD(bleshell_chr_list, bleshell_chr);
+SLIST_HEAD(bleshell_chr_list, bleshell_chr);
 
 struct bleshell_svc {
-    STAILQ_ENTRY(bleshell_svc) next;
+    SLIST_ENTRY(bleshell_svc) next;
     struct ble_gatt_service svc;
 
     struct bleshell_chr_list chrs;
 };
 
-STAILQ_HEAD(bleshell_svc_list, bleshell_svc);
+SLIST_HEAD(bleshell_svc_list, bleshell_svc);
 
 struct bleshell_conn {
     uint16_t handle;
@@ -63,10 +63,13 @@ int parse_arg_all(int argc, char **argv);
 int cmd_init(void);
 void periph_init(void);
 int bleshell_disc_svcs(uint16_t conn_handle);
+int bleshell_disc_svc_by_uuid(uint16_t conn_handle, uint8_t *uuid128);
 int bleshell_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle,
                            uint16_t end_handle);
 int bleshell_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle,
                                uint16_t end_handle, uint8_t *uuid128);
+int bleshell_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle,
+                           uint16_t end_handle);
 int bleshell_adv_start(int disc, int conn, uint8_t *peer_addr, int addr_type);
 int bleshell_adv_stop(void);
 int bleshell_conn_initiate(int addr_type, uint8_t *peer_addr);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a2b4dd5e/project/bleshell/src/cmd.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/cmd.c b/project/bleshell/src/cmd.c
index ca1b6b6..fe89767 100644
--- a/project/bleshell/src/cmd.c
+++ b/project/bleshell/src/cmd.c
@@ -61,12 +61,36 @@ cmd_print_svc(struct bleshell_svc *svc, int print_chrs)
     console_printf("\n");
 
     if (print_chrs) {
-        STAILQ_FOREACH(chr, &svc->chrs, next) {
+        SLIST_FOREACH(chr, &svc->chrs, next) {
             cmd_print_chr(chr);
         }
     }
 }
 
+static int
+cmd_parse_conn_start_end(uint16_t *out_conn, uint16_t *out_start,
+                         uint16_t *out_end)
+{
+    int rc;
+
+    *out_conn = parse_arg_uint16("conn", &rc);
+    if (rc != 0) {
+        return rc;
+    }
+
+    *out_start = parse_arg_uint16("start", &rc);
+    if (rc != 0) {
+        return rc;
+    }
+
+    *out_end = parse_arg_uint16("end", &rc);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
 /*****************************************************************************
  * $advertise                                                                *
  *****************************************************************************/
@@ -202,17 +226,7 @@ cmd_disc_chr(int argc, char **argv)
     uint8_t uuid128[16];
     int rc;
 
-    conn_handle = parse_arg_uint16("conn", &rc);
-    if (rc != 0) {
-        return rc;
-    }
-
-    start_handle = parse_arg_uint16("start", &rc);
-    if (rc != 0) {
-        return rc;
-    }
-
-    end_handle = parse_arg_uint16("end", &rc);
+    rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle);
     if (rc != 0) {
         return rc;
     }
@@ -237,6 +251,7 @@ cmd_disc_chr(int argc, char **argv)
 static int
 cmd_disc_svc(int argc, char **argv)
 {
+    uint8_t uuid128[16];
     int conn_handle;
     int rc;
 
@@ -245,7 +260,15 @@ cmd_disc_svc(int argc, char **argv)
         return rc;
     }
 
-    rc = bleshell_disc_svcs(conn_handle);
+    rc = parse_arg_uuid("uuid", uuid128);
+    if (rc == 0) {
+        rc = bleshell_disc_svc_by_uuid(conn_handle, uuid128);
+    } else if (rc == ENOENT) {
+        rc = bleshell_disc_svcs(conn_handle);
+    } else  {
+        return rc;
+    }
+
     if (rc != 0) {
         console_printf("error discovering services; rc=%d\n", rc);
         return rc;
@@ -274,6 +297,50 @@ cmd_disc(int argc, char **argv)
 }
 
 /*****************************************************************************
+ * $find                                                                     *
+ *****************************************************************************/
+
+static int
+cmd_find_inc_svcs(int argc, char **argv)
+{
+    uint16_t start_handle;
+    uint16_t conn_handle;
+    uint16_t end_handle;
+    int rc;
+
+    rc = cmd_parse_conn_start_end(&conn_handle, &start_handle, &end_handle);
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = bleshell_find_inc_svcs(conn_handle, start_handle, end_handle);
+    if (rc != 0) {
+        console_printf("error finding included services; rc=%d\n", rc);
+        return rc;
+    }
+
+    return 0;
+}
+
+static struct cmd_entry cmd_find_entries[] = {
+    { "inc_svcs", cmd_find_inc_svcs },
+    { NULL, NULL }
+};
+
+static int
+cmd_find(int argc, char **argv)
+{
+    int rc;
+
+    rc = cmd_exec(cmd_find_entries, argc, argv);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+/*****************************************************************************
  * $show                                                                     *
  *****************************************************************************/
 
@@ -301,7 +368,7 @@ cmd_show_chr(int argc, char **argv)
         print_addr(conn->addr);
         console_printf("\n");
 
-        STAILQ_FOREACH(svc, &conn->svcs, next) {
+        SLIST_FOREACH(svc, &conn->svcs, next) {
             cmd_print_svc(svc, 1);
         }
     }
@@ -340,7 +407,7 @@ cmd_show_svc(int argc, char **argv)
         print_addr(conn->addr);
         console_printf("\n");
 
-        STAILQ_FOREACH(svc, &conn->svcs, next) {
+        SLIST_FOREACH(svc, &conn->svcs, next) {
             cmd_print_svc(svc, 0);
         }
     }
@@ -400,6 +467,7 @@ static struct cmd_entry cmd_b_entries[] = {
     { "adv", cmd_adv },
     { "conn", cmd_conn },
     { "disc", cmd_disc },
+    { "find", cmd_find },
     { "show", cmd_show },
     { "set", cmd_set },
     { NULL, NULL }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a2b4dd5e/project/bleshell/src/main.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/main.c b/project/bleshell/src/main.c
index 1229afd..130ef90 100755
--- a/project/bleshell/src/main.c
+++ b/project/bleshell/src/main.c
@@ -136,7 +136,7 @@ bleshell_conn_add(struct ble_gap_conn_desc *desc)
     conn->handle = desc->conn_handle;
     conn->addr_type = desc->peer_addr_type;
     memcpy(conn->addr, desc->peer_addr, 6);
-    STAILQ_INIT(&conn->svcs);
+    SLIST_INIT(&conn->svcs);
 
     return conn;
 }
@@ -151,8 +151,8 @@ bleshell_conn_delete_idx(int idx)
     assert(idx >= 0 && idx < bleshell_num_conns);
 
     conn = bleshell_conns + idx;
-    while ((svc = STAILQ_FIRST(&conn->svcs)) != NULL) {
-        STAILQ_REMOVE_HEAD(&conn->svcs, next);
+    while ((svc = SLIST_FIRST(&conn->svcs)) != NULL) {
+        SLIST_REMOVE_HEAD(&conn->svcs, next);
         os_memblock_put(&bleshell_svc_pool, svc);
     }
 
@@ -163,23 +163,52 @@ bleshell_conn_delete_idx(int idx)
 }
 
 static struct bleshell_svc *
-bleshell_svc_find(struct bleshell_conn *conn, uint16_t svc_start_handle)
+bleshell_svc_find_prev(struct bleshell_conn *conn, uint16_t svc_start_handle)
 {
+    struct bleshell_svc *prev;
     struct bleshell_svc *svc;
 
-    STAILQ_FOREACH(svc, &conn->svcs, next) {
-        if (svc->svc.start_handle == svc_start_handle) {
-            return svc;
+    prev = NULL;
+    SLIST_FOREACH(svc, &conn->svcs, next) {
+        if (svc->svc.start_handle >= svc_start_handle) {
+            break;
         }
+
+        prev = svc;
+    }
+
+    return prev;
+}
+
+static struct bleshell_svc *
+bleshell_svc_find(struct bleshell_conn *conn, uint16_t svc_start_handle,
+                  struct bleshell_svc **out_prev)
+{
+    struct bleshell_svc *prev;
+    struct bleshell_svc *svc;
+
+    prev = bleshell_svc_find_prev(conn, svc_start_handle);
+    if (prev == NULL) {
+        svc = SLIST_FIRST(&conn->svcs);
+    } else {
+        svc = SLIST_NEXT(prev, next);
+    }
+
+    if (svc != NULL && svc->svc.start_handle != svc_start_handle) {
+        svc = NULL;
     }
 
-    return NULL;
+    if (out_prev != NULL) {
+        *out_prev = prev;
+    }
+    return svc;
 }
 
 static struct bleshell_svc *
 bleshell_svc_add(uint16_t conn_handle, struct ble_gatt_service *gatt_svc)
 {
     struct bleshell_conn *conn;
+    struct bleshell_svc *prev;
     struct bleshell_svc *svc;
 
     conn = bleshell_conn_find(conn_handle);
@@ -189,7 +218,7 @@ bleshell_svc_add(uint16_t conn_handle, struct ble_gatt_service *gatt_svc)
         return NULL;
     }
 
-    svc = bleshell_svc_find(conn, gatt_svc->start_handle);
+    svc = bleshell_svc_find(conn, gatt_svc->start_handle, &prev);
     if (svc != NULL) {
         /* Service already discovered. */
         return svc;
@@ -200,28 +229,60 @@ bleshell_svc_add(uint16_t conn_handle, struct ble_gatt_service *gatt_svc)
         console_printf("OOM WHILE DISCOVERING SERVICE\n");
         return NULL;
     }
+    memset(svc, 0, sizeof *svc);
 
     svc->svc = *gatt_svc;
-    STAILQ_INIT(&svc->chrs);
+    SLIST_INIT(&svc->chrs);
 
-    STAILQ_INSERT_TAIL(&conn->svcs, svc, next);
+    if (prev == NULL) {
+        SLIST_INSERT_HEAD(&conn->svcs, svc, next);
+    } else {
+        SLIST_NEXT(prev, next) = svc;
+    }
 
     return svc;
 }
 
 static struct bleshell_chr *
-bleshell_chr_find(struct bleshell_conn *conn, struct bleshell_svc *svc,
-                  uint16_t chr_def_handle)
+bleshell_chr_find_prev(struct bleshell_svc *svc, uint16_t chr_def_handle)
 {
+    struct bleshell_chr *prev;
     struct bleshell_chr *chr;
 
-    STAILQ_FOREACH(chr, &svc->chrs, next) {
-        if (chr->chr.decl_handle == chr_def_handle) {
-            return chr;
+    prev = NULL;
+    SLIST_FOREACH(chr, &svc->chrs, next) {
+        if (chr->chr.decl_handle >= chr_def_handle) {
+            break;
         }
+
+        prev = chr;
+    }
+
+    return prev;
+}
+
+static struct bleshell_chr *
+bleshell_chr_find(struct bleshell_svc *svc, uint16_t chr_def_handle,
+                  struct bleshell_chr **out_prev)
+{
+    struct bleshell_chr *prev;
+    struct bleshell_chr *chr;
+
+    prev = bleshell_chr_find_prev(svc, chr_def_handle);
+    if (prev == NULL) {
+        chr = SLIST_FIRST(&svc->chrs);
+    } else {
+        chr = SLIST_NEXT(prev, next);
+    }
+
+    if (chr != NULL && chr->chr.decl_handle != chr_def_handle) {
+        chr = NULL;
     }
 
-    return NULL;
+    if (out_prev != NULL) {
+        *out_prev = prev;
+    }
+    return chr;
 }
 
 
@@ -230,8 +291,9 @@ bleshell_chr_add(uint16_t conn_handle,  uint16_t svc_start_handle,
                  struct ble_gatt_chr *gatt_chr)
 {
     struct bleshell_conn *conn;
-    struct bleshell_svc *svc;
+    struct bleshell_chr *prev;
     struct bleshell_chr *chr;
+    struct bleshell_svc *svc;
 
     conn = bleshell_conn_find(conn_handle);
     if (conn == NULL) {
@@ -240,14 +302,14 @@ bleshell_chr_add(uint16_t conn_handle,  uint16_t svc_start_handle,
         return NULL;
     }
 
-    svc = bleshell_svc_find(conn, svc_start_handle);
+    svc = bleshell_svc_find(conn, svc_start_handle, NULL);
     if (svc == NULL) {
         console_printf("CAN'T FIND SERVICE FOR DISCOVERED CHR; HANDLE=%d\n",
                        conn_handle);
         return NULL;
     }
 
-    chr = bleshell_chr_find(conn, svc, gatt_chr->decl_handle);
+    chr = bleshell_chr_find(svc, gatt_chr->decl_handle, &prev);
     if (chr != NULL) {
         /* Characteristic already discovered. */
         return chr;
@@ -258,9 +320,15 @@ bleshell_chr_add(uint16_t conn_handle,  uint16_t svc_start_handle,
         console_printf("OOM WHILE DISCOVERING CHARACTERISTIC\n");
         return NULL;
     }
+    memset(chr, 0, sizeof *chr);
 
     chr->chr = *gatt_chr;
-    STAILQ_INSERT_TAIL(&svc->chrs, chr, next);
+
+    if (prev == NULL) {
+        SLIST_INSERT_HEAD(&svc->chrs, chr, next);
+    } else {
+        SLIST_NEXT(prev, next) = chr;
+    }
 
     return chr;
 }
@@ -370,6 +438,27 @@ bleshell_disc_svcs(uint16_t conn_handle)
 }
 
 int
+bleshell_disc_svc_by_uuid(uint16_t conn_handle, uint8_t *uuid128)
+{
+    int rc;
+
+    rc = ble_gattc_disc_svc_by_uuid(conn_handle, uuid128,
+                                    bleshell_on_disc_s, NULL);
+    return rc;
+}
+
+int
+bleshell_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle,
+                       uint16_t end_handle)
+{
+    int rc;
+
+    rc = ble_gattc_find_inc_svcs(conn_handle, start_handle, end_handle,
+                                 bleshell_on_disc_s, NULL);
+    return rc;
+}
+
+int
 bleshell_adv_stop(void)
 {
     int rc;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/a2b4dd5e/project/bleshell/src/periph.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/periph.c b/project/bleshell/src/periph.c
index 8146171..02b8d7b 100644
--- a/project/bleshell/src/periph.c
+++ b/project/bleshell/src/periph.c
@@ -13,38 +13,48 @@ static int
 periph_gatt_cb(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
                union ble_gatt_access_ctxt *ctxt, void *arg);
 
-static const struct ble_gatt_svc_def periph_svcs[] = { {
-    /*** Service 0x1234. */
-    .type = BLE_GATT_SVC_TYPE_PRIMARY,
-    .uuid128 = BLE_UUID16(PERIPH_SVC1_UUID),
-    .characteristics = (struct ble_gatt_chr_def[]) { {
-        /*** Characteristic 0x1111. */
-        .uuid128 = BLE_UUID16(PERIPH_CHR1_UUID),
-        .access_cb = periph_gatt_cb,
-        .flags = 0,
-    }, {
-        /*** Characteristic 0x1112. */
-        .uuid128 = BLE_UUID16(PERIPH_CHR2_UUID),
-        .access_cb = periph_gatt_cb,
-        .flags = 0,
-    }, {
-        .uuid128 = NULL, /* No more characteristics in this service. */
-    } },
-}, {
-    /*** Service 0x5678. */
-    .type = BLE_GATT_SVC_TYPE_PRIMARY,
-    .uuid128 = BLE_UUID16(PERIPH_SVC2_UUID),
-    .characteristics = (struct ble_gatt_chr_def[]) { {
-        /*** Characteristic 0x5555. */
-        .uuid128 = BLE_UUID16(PERIPH_CHR3_UUID),
-        .access_cb = periph_gatt_cb,
-        .flags = 0,
-    }, {
-        .uuid128 = NULL, /* No more characteristics in this service. */
-    } },
-}, {
-    .type = BLE_GATT_SVC_TYPE_END, /* No more services. */
-}, };
+static const struct ble_gatt_svc_def periph_svcs[] = {
+    [0] = {
+        /*** Service 0x1234. */
+        .type = BLE_GATT_SVC_TYPE_SECONDARY,
+        .uuid128 = BLE_UUID16(PERIPH_SVC1_UUID),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            /*** Characteristic 0x1111. */
+            .uuid128 = BLE_UUID16(PERIPH_CHR1_UUID),
+            .access_cb = periph_gatt_cb,
+            .flags = 0,
+        }, {
+            /*** Characteristic 0x1112. */
+            .uuid128 = BLE_UUID16(PERIPH_CHR2_UUID),
+            .access_cb = periph_gatt_cb,
+            .flags = 0,
+        }, {
+            .uuid128 = NULL, /* No more characteristics in this service. */
+        } },
+    },
+
+    [1] = {
+        /*** Service 0x5678. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(PERIPH_SVC2_UUID),
+        .includes = (const struct ble_gatt_svc_def *[]) {
+            &periph_svcs[0],
+            NULL,
+        },
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            /*** Characteristic 0x5555. */
+            .uuid128 = BLE_UUID16(PERIPH_CHR3_UUID),
+            .access_cb = periph_gatt_cb,
+            .flags = 0,
+        }, {
+            .uuid128 = NULL, /* No more characteristics in this service. */
+        } },
+    },
+
+    {
+        .type = BLE_GATT_SVC_TYPE_END, /* No more services. */
+    },
+};
 
 static int
 periph_gatt_cb(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,