You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2017/03/07 00:05:17 UTC

[22/50] incubator-mynewt-core git commit: bletiny: Add support to send data over L2CAP LE CoC

bletiny: Add support to send data over L2CAP LE CoC

With this patch user can send random data to specified CoC
channel with command:
 l2cap send conn=<conn_handle> idx=<coc idx> bytes=<num of bytes>


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

Branch: refs/heads/1_0_0_dev
Commit: c26d8c651f1a915be9dec0450f7d2040538115a4
Parents: 9bc81b1
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 15:07:58 2017 +0100
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Mar 6 15:50:37 2017 -0800

----------------------------------------------------------------------
 apps/bletiny/src/bletiny.h |  1 +
 apps/bletiny/src/cmd.c     | 50 +++++++++++++++++++++++++++
 apps/bletiny/src/main.c    | 75 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 126 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c26d8c65/apps/bletiny/src/bletiny.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny.h b/apps/bletiny/src/bletiny.h
index 8d8ac64..cdaff14 100644
--- a/apps/bletiny/src/bletiny.h
+++ b/apps/bletiny/src/bletiny.h
@@ -193,6 +193,7 @@ int bletiny_rssi(uint16_t conn_handle, int8_t *out_rssi);
 int bletiny_l2cap_create_srv(uint16_t psm);
 int bletiny_l2cap_connect(uint16_t conn, uint16_t psm);
 int bletiny_l2cap_disconnect(uint16_t conn, uint16_t idx);
+int bletiny_l2cap_send(uint16_t conn, uint16_t idx, uint16_t bytes);
 #define BLETINY_LOG_MODULE  (LOG_MODULE_PERUSER + 0)
 #define BLETINY_LOG(lvl, ...) \
     LOG_ ## lvl(&bletiny_log, BLETINY_LOG_MODULE, __VA_ARGS__)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c26d8c65/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 47ab379..45debc5 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1279,6 +1279,55 @@ cmd_l2cap_disconnect(int argc, char **argv)
     return bletiny_l2cap_disconnect(conn, idx);
 }
 
+static void
+bletiny_l2cap_send_help(void)
+{
+    console_printf("Available l2cap send commands: \n");
+    console_printf("\thelp\n");
+    console_printf("Available l2cap disconnect params: \n");
+    help_cmd_uint16("conn");
+    help_cmd_uint16("idx");
+    help_cmd_uint16("bytes");
+    console_printf("\n Use 'b show coc' to get conn and idx parameters.\n");
+    console_printf("bytes stands for number of bytes to send .\n");
+}
+
+static int
+cmd_l2cap_send(int argc, char **argv)
+{
+    uint16_t conn;
+    uint16_t idx;
+    uint16_t bytes;
+    int rc;
+
+    if (argc > 1 && strcmp(argv[1], "help") == 0) {
+        bletiny_l2cap_send_help();
+        return 0;
+    }
+    conn = parse_arg_uint16("conn", &rc);
+    if (rc != 0) {
+       console_printf("invalid 'conn' parameter\n");
+       help_cmd_uint16("conn");
+       return rc;
+    }
+
+    idx = parse_arg_uint16("idx", &rc);
+    if (rc != 0) {
+       console_printf("invalid 'idx' parameter\n");
+       help_cmd_uint16("idx");
+       return rc;
+    }
+
+    bytes = parse_arg_uint16("bytes", &rc);
+    if (rc != 0) {
+       console_printf("invalid 'bytes' parameter\n");
+       help_cmd_uint16("bytes");
+       return rc;
+    }
+
+    return bletiny_l2cap_send(conn, idx, bytes);
+}
+
 static const struct cmd_entry cmd_l2cap_entries[];
 
 static int
@@ -1298,6 +1347,7 @@ static const struct cmd_entry cmd_l2cap_entries[] = {
     { "create_srv", cmd_l2cap_create_srv },
     { "connect", cmd_l2cap_connect },
     { "disconnect", cmd_l2cap_disconnect },
+    { "send", cmd_l2cap_send },
     { "help", cmd_l2cap_help },
     { NULL, NULL }
 };

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c26d8c65/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 52cf7e6..b8f1e51 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1756,6 +1756,81 @@ bletiny_l2cap_disconnect(uint16_t conn_handle, uint16_t idx)
 #endif
 }
 
+int
+bletiny_l2cap_send(uint16_t conn_handle, uint16_t idx, uint16_t bytes)
+{
+#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) == 0
+    console_printf("BLE L2CAP LE COC not supported.");
+    console_printf(" Configure nimble host to enable it\n");
+    return 0;
+#else
+
+    struct bletiny_conn *conn;
+    struct bletiny_l2cap_coc *coc;
+    struct os_mbuf *sdu_tx;
+    uint8_t b[] = {0x00, 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88, 0x99};
+    int i;
+    int rc;
+
+    console_printf("conn=%d, idx=%d, bytes=%d\n", conn_handle, idx, bytes);
+
+    conn = bletiny_conn_find(conn_handle);
+    if (conn == NULL) {
+        console_printf("conn=%d does not exist\n", conn_handle);
+        return 0;
+    }
+
+    i = 0;
+    SLIST_FOREACH(coc, &conn->coc_list, next) {
+        if (i == idx) {
+            break;
+        }
+        i++;
+    }
+    if (coc == NULL) {
+        console_printf("Are you sure your channel exist?\n");
+        return 0;
+    }
+
+    sdu_tx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
+    if (sdu_tx == NULL) {
+        console_printf("No memory in the test sdu pool\n");
+        return 0;
+    }
+
+    /* For the testing purpose we fill up buffer with known data, easy
+     * to validate on other side. In this loop we add as many full chunks as we
+     * can
+     */
+    for (i = 0; i < bytes / sizeof(b); i++) {
+        rc = os_mbuf_append(sdu_tx, b, sizeof(b));
+        if (rc) {
+            console_printf("Cannot append data %i !\n", i);
+            os_mbuf_free_chain(sdu_tx);
+            return rc;
+        }
+    }
+
+    /* Here we add the rest < sizeof(b) */
+    rc = os_mbuf_append(sdu_tx, b, bytes - (sizeof(b) * i));
+    if (rc) {
+        console_printf("Cannot append data %i !\n", i);
+        os_mbuf_free_chain(sdu_tx);
+        return rc;
+    }
+
+    rc = ble_l2cap_send(coc->chan, sdu_tx);
+    if (rc) {
+        console_printf("Could not send data rc=%d\n", rc);
+        if (rc == BLE_HS_EBUSY) {
+            os_mbuf_free_chain(sdu_tx);
+        }
+    }
+
+    return rc;
+
+#endif
+}
 /**
  * main
  *