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 04:59:57 UTC

[2/2] incubator-mynewt-larva git commit: Add GATT characteristic commands to ble shell app.

Add GATT characteristic commands to ble shell app.


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

Branch: refs/heads/master
Commit: bf84ccc0f97b5ccc93c3fdce1f851277cf0fd1d3
Parents: dc72efa
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Jan 18 19:56:35 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Jan 18 19:59:33 2016 -0800

----------------------------------------------------------------------
 project/bleshell/src/bleshell_priv.h |  13 ++++
 project/bleshell/src/cmd.c           |  89 ++++++++++++++++++++++++--
 project/bleshell/src/main.c          | 103 +++++++++++++++++++++++++++++-
 project/bleshell/src/parse.c         |  17 ++++-
 4 files changed, 213 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/bf84ccc0/project/bleshell/src/bleshell_priv.h
----------------------------------------------------------------------
diff --git a/project/bleshell/src/bleshell_priv.h b/project/bleshell/src/bleshell_priv.h
index 942f3d8..de4aa72 100644
--- a/project/bleshell/src/bleshell_priv.h
+++ b/project/bleshell/src/bleshell_priv.h
@@ -19,9 +19,18 @@ struct kv_pair {
     int val;
 };
 
+struct bleshell_chr {
+    STAILQ_ENTRY(bleshell_chr) next;
+    struct ble_gatt_chr chr;
+};
+
+STAILQ_HEAD(bleshell_chr_list, bleshell_chr);
+
 struct bleshell_svc {
     STAILQ_ENTRY(bleshell_svc) next;
     struct ble_gatt_service svc;
+
+    struct bleshell_chr_list chrs;
 };
 
 STAILQ_HEAD(bleshell_svc_list, bleshell_svc);
@@ -42,7 +51,9 @@ void print_uuid(void *uuid128);
 struct cmd_entry *parse_cmd_find(struct cmd_entry *cmds, char *name);
 struct kv_pair *parse_kv_find(struct kv_pair *kvs, char *name);
 char *parse_arg_find(char *key);
+int parse_arg_int_bounds(char *name, int min, int max);
 int parse_arg_int(char *name);
+uint16_t parse_arg_uint16(char *name);
 int parse_arg_kv(char *name, struct kv_pair *kvs);
 int parse_arg_mac(char *name, uint8_t *dst);
 int parse_err_too_few_args(char *cmd_name);
@@ -50,6 +61,8 @@ 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_all_chrs(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/bf84ccc0/project/bleshell/src/cmd.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/cmd.c b/project/bleshell/src/cmd.c
index e187d13..38acb23 100644
--- a/project/bleshell/src/cmd.c
+++ b/project/bleshell/src/cmd.c
@@ -39,6 +39,34 @@ cmd_exec(struct cmd_entry *cmds, int argc, char **argv)
     return 0;
 }
 
+static void
+cmd_print_chr(struct bleshell_chr *chr)
+{
+    console_printf("        def_handle=%d val_handle=%d properties=0x%02x "
+                   "uuid=",
+                   chr->chr.decl_handle, chr->chr.value_handle,
+                   chr->chr.properties);
+    print_uuid(chr->chr.uuid128);
+    console_printf("\n");
+}
+
+static void
+cmd_print_svc(struct bleshell_svc *svc, int print_chrs)
+{
+    struct bleshell_chr *chr;
+
+    console_printf("    start=%d end=%d uuid=", svc->svc.start_handle,
+                   svc->svc.end_handle);
+    print_uuid(svc->svc.uuid128);
+    console_printf("\n");
+
+    if (print_chrs) {
+        STAILQ_FOREACH(chr, &svc->chrs, next) {
+            cmd_print_chr(chr);
+        }
+    }
+}
+
 /*****************************************************************************
  * $advertise                                                                *
  *****************************************************************************/
@@ -166,6 +194,38 @@ cmd_conn(int argc, char **argv)
  *****************************************************************************/
 
 static int
+cmd_disc_all_chrs(int argc, char **argv)
+{
+    uint16_t start_handle;
+    uint16_t conn_handle;
+    uint16_t end_handle;
+    int rc;
+
+    conn_handle = parse_arg_uint16("conn");
+    if (conn_handle == -1) {
+        return -1;
+    }
+
+    start_handle = parse_arg_uint16("start");
+    if (start_handle == -1) {
+        return -1;
+    }
+
+    end_handle = parse_arg_uint16("end");
+    if (end_handle == -1) {
+        return -1;
+    }
+
+    rc = bleshell_disc_all_chrs(conn_handle, start_handle, end_handle);
+    if (rc != 0) {
+        console_printf("error discovering services; rc=%d\n", rc);
+        return rc;
+    }
+
+    return 0;
+}
+
+static int
 cmd_disc_svc(int argc, char **argv)
 {
     int conn_handle;
@@ -186,6 +246,7 @@ cmd_disc_svc(int argc, char **argv)
 }
 
 static struct cmd_entry cmd_disc_entries[] = {
+    { "all_chrs", cmd_disc_all_chrs },
     { "svc", cmd_disc_svc },
     { NULL, NULL }
 };
@@ -218,6 +279,28 @@ cmd_show_addr(int argc, char **argv)
 }
 
 static int
+cmd_show_chr(int argc, char **argv)
+{
+    struct bleshell_conn *conn;
+    struct bleshell_svc *svc;
+    int i;
+
+    for (i = 0; i < bleshell_num_conns; i++) {
+        conn = bleshell_conns + i;
+
+        console_printf("CONNECTION: handle=%d addr=", conn->handle);
+        print_addr(conn->addr);
+        console_printf("\n");
+
+        STAILQ_FOREACH(svc, &conn->svcs, next) {
+            cmd_print_svc(svc, 1);
+        }
+    }
+
+    return 0;
+}
+
+static int
 cmd_show_conn(int argc, char **argv)
 {
     struct bleshell_conn *conn;
@@ -249,10 +332,7 @@ cmd_show_svc(int argc, char **argv)
         console_printf("\n");
 
         STAILQ_FOREACH(svc, &conn->svcs, next) {
-            console_printf("    start=%d end=%d uuid=", svc->svc.start_handle,
-                           svc->svc.end_handle);
-            print_uuid(svc->svc.uuid128);
-            console_printf("\n");
+            cmd_print_svc(svc, 0);
         }
     }
 
@@ -261,6 +341,7 @@ cmd_show_svc(int argc, char **argv)
 
 static struct cmd_entry cmd_show_entries[] = {
     { "addr", cmd_show_addr },
+    { "chr", cmd_show_chr },
     { "conn", cmd_show_conn },
     { "svc", cmd_show_svc },
     { NULL, NULL }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/bf84ccc0/project/bleshell/src/main.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/main.c b/project/bleshell/src/main.c
index 4a6e0fb..ddf71f8 100755
--- a/project/bleshell/src/main.c
+++ b/project/bleshell/src/main.c
@@ -39,7 +39,7 @@
 #define HOST_TASK_PRIO          (1)
 
 #define SHELL_TASK_PRIO         (3) 
-#define SHELL_TASK_STACK_SIZE   (OS_STACK_ALIGN(256))
+#define SHELL_TASK_STACK_SIZE   (OS_STACK_ALIGN(384))
 os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
 
 /* For LED toggling */
@@ -69,10 +69,11 @@ struct os_mempool g_mbuf_mempool;
 os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE];
 
 /* BLESHELL variables */
-#define BLESHELL_STACK_SIZE             (256)
+#define BLESHELL_STACK_SIZE             (128)
 #define BLESHELL_TASK_PRIO              (HOST_TASK_PRIO + 1)
 
-#define BLESHELL_MAX_SVCS               16
+#define BLESHELL_MAX_SVCS               8
+#define BLESHELL_MAX_CHRS               8
 
 uint32_t g_next_os_time;
 int g_bleshell_state;
@@ -92,6 +93,9 @@ int bleshell_num_conns;
 static void *bleshell_svc_mem;
 static struct os_mempool bleshell_svc_pool;
 
+static void *bleshell_chr_mem;
+static struct os_mempool bleshell_chr_pool;
+
 static int
 bleshell_conn_find_idx(uint16_t handle)
 {
@@ -178,11 +182,61 @@ bleshell_svc_add(uint16_t conn_handle, struct ble_gatt_service *gatt_svc)
     }
 
     svc->svc = *gatt_svc;
+    STAILQ_INIT(&svc->chrs);
+
     STAILQ_INSERT_TAIL(&conn->svcs, svc, next);
 
     return svc;
 }
 
+static struct bleshell_svc *
+bleshell_svc_find(struct bleshell_conn *conn, uint16_t svc_start_handle)
+{
+    struct bleshell_svc *svc;
+
+    STAILQ_FOREACH(svc, &conn->svcs, next) {
+        if (svc->svc.start_handle == svc_start_handle) {
+            return svc;
+        }
+    }
+
+    return NULL;
+}
+
+static struct bleshell_chr *
+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 *chr;
+
+    conn = bleshell_conn_find(conn_handle);
+    if (conn == NULL) {
+        console_printf("RECEIVED SERVICE FOR UNKNOWN CONNECTION; HANDLE=%d\n",
+                       conn_handle);
+        return NULL;
+    }
+
+    svc = bleshell_svc_find(conn, svc_start_handle);
+    if (svc == NULL) {
+        console_printf("CAN'T FIND SERVICE FOR DISCOVERED CHR; HANDLE=%d\n",
+                       conn_handle);
+        return NULL;
+    }
+
+    chr = os_memblock_get(&bleshell_chr_pool);
+    if (chr == NULL) {
+        console_printf("OOM WHILE DISCOVERING CHARACTERISTIC\n");
+        return NULL;
+    }
+
+    chr->chr = *gatt_chr;
+    STAILQ_INSERT_TAIL(&svc->chrs, chr, next);
+
+    return chr;
+}
+
 static int
 bleshell_on_disc_s(uint16_t conn_handle, struct ble_gatt_error *error,
                    struct ble_gatt_service *service, void *arg)
@@ -200,6 +254,27 @@ bleshell_on_disc_s(uint16_t conn_handle, struct ble_gatt_error *error,
     return 0;
 }
 
+int
+bleshell_on_disc_c(uint16_t conn_handle, struct ble_gatt_error *error,
+                   struct ble_gatt_chr *chr, void *arg)
+{
+    intmax_t *svc_start_handle;
+
+    svc_start_handle = arg;
+
+    if (error != NULL) {
+        console_printf("ERROR DISCOVERING CHARACTERISTIC: conn_handle=%d "
+                       "status=%d att_handle=%d\n",
+                       conn_handle, error->status, error->att_handle);
+    } else if (chr != NULL) {
+        bleshell_chr_add(conn_handle, *svc_start_handle, chr);
+    } else {
+        /* Service discovery complete. */
+    }
+
+    return 0;
+}
+
 static void
 bleshell_on_connect(int event, int status, struct ble_gap_conn_desc *desc,
                     void *arg)
@@ -231,6 +306,19 @@ bleshell_on_connect(int event, int status, struct ble_gap_conn_desc *desc,
 }
 
 int
+bleshell_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle,
+                       uint16_t end_handle)
+{
+    intmax_t svc_start_handle;
+    int rc;
+
+    svc_start_handle = start_handle;
+    rc = ble_gattc_disc_all_chrs(conn_handle, start_handle, end_handle,
+                                 bleshell_on_disc_c, &svc_start_handle);
+    return rc;
+}
+
+int
 bleshell_disc_svcs(uint16_t conn_handle)
 {
     int rc;
@@ -360,6 +448,15 @@ main(void)
                          "bleshell_svc_pool");
     assert(rc == 0);
 
+    bleshell_chr_mem = malloc(
+        OS_MEMPOOL_BYTES(BLESHELL_MAX_CHRS, sizeof (struct bleshell_chr)));
+    assert(bleshell_chr_mem != NULL);
+
+    rc = os_mempool_init(&bleshell_chr_pool, BLESHELL_MAX_CHRS,
+                         sizeof (struct bleshell_chr), bleshell_chr_mem,
+                         "bleshell_chr_pool");
+    assert(rc == 0);
+
     os_task_init(&bleshell_task, "bleshell", bleshell_task_handler,
                  NULL, BLESHELL_TASK_PRIO, OS_WAIT_FOREVER,
                  bleshell_stack, BLESHELL_STACK_SIZE);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/bf84ccc0/project/bleshell/src/parse.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/parse.c b/project/bleshell/src/parse.c
index 04afb83..9f6e91a 100644
--- a/project/bleshell/src/parse.c
+++ b/project/bleshell/src/parse.c
@@ -97,7 +97,7 @@ parse_arg_find(char *key)
 }
 
 int
-parse_arg_int(char *name)
+parse_arg_int_bounds(char *name, int min, int max)
 {
     char *endptr;
     char *sval;
@@ -110,12 +110,25 @@ parse_arg_int(char *name)
 
     lval = strtol(sval, &endptr, 0);
     if (sval[0] != '\0' && *endptr == '\0' &&
-        lval >= INT_MIN && lval <= INT_MAX) {
+        lval >= min && lval <= max) {
 
         return lval;
     }
 
     return -1;
+
+}
+
+int
+parse_arg_int(char *name)
+{
+    return parse_arg_int_bounds(name, INT_MIN, INT_MAX);
+}
+
+uint16_t
+parse_arg_uint16(char *name)
+{
+    return parse_arg_int_bounds(name, 0, UINT16_MAX);
 }
 
 int