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/be58d00d Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/be58d00d Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/be58d00d Branch: refs/heads/develop Commit: be58d00d0d85812ea9ae2f3949e6ab446faf3d32 Parents: 6a0f5a8 Author: Åukasz Rymanowski <lukasz.rymanow...@codecoup.pl> Authored: Sun Feb 12 15:07:58 2017 +0100 Committer: Åukasz Rymanowski <lukasz.rymanow...@codecoup.pl> Committed: Fri Mar 3 12:40:41 2017 +0100 ---------------------------------------------------------------------- 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/be58d00d/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/be58d00d/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/be58d00d/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 *