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 2017/03/03 17:36:16 UTC

[01/26] incubator-mynewt-core git commit: bletiny: Add missing returns in L2CAP connect/disconnect

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop e3241525f -> b2ea1dc2c


bletiny: Add missing returns in L2CAP connect/disconnect

Fix missing error returns.


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

Branch: refs/heads/develop
Commit: b552c1636787546611dd683f6efeddd73377dd4f
Parents: 316c18a
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Feb 22 17:04:10 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Wed Mar 1 12:10:48 2017 +0100

----------------------------------------------------------------------
 apps/bletiny/src/cmd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b552c163/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index 8904ea2..47ab379 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1226,6 +1226,7 @@ cmd_l2cap_connect(int argc, char **argv)
     if (rc != 0) {
         console_printf("invalid 'conn' parameter\n");
         help_cmd_uint16("conn");
+        return rc;
     }
 
     psm = parse_arg_uint16("psm", &rc);
@@ -1265,13 +1266,14 @@ cmd_l2cap_disconnect(int argc, char **argv)
     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 0;
+        return rc;
     }
 
     return bletiny_l2cap_disconnect(conn, idx);


[09/26] incubator-mynewt-core git commit: nibmle/l2cap: Clear LE CoC channel on ACL drop

Posted by cc...@apache.org.
nibmle/l2cap: Clear LE CoC channel on ACL drop

When ACL is disconnected before L2CAP is disconnected we need to
make sure proper cleaning is done and application is notified
about channel disconnection.

Therefore with this patch sending DISCONNECTED EVENT is moved to
function doing free of COC specific data in the channel


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

Branch: refs/heads/develop
Commit: e488b9df404e1dc45ce6494d66fbe965d66a1758
Parents: 93ac0df
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 21:45:29 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_coc.c | 14 ++++++++++++++
 net/nimble/host/src/ble_l2cap_sig.c | 16 ----------------
 2 files changed, 14 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e488b9df/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index 12d79d6..ec01fec 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -276,6 +276,18 @@ ble_l2cap_coc_create_srv_chan(uint16_t conn_handle, uint16_t psm,
     return 0;
 }
 
+static void
+ble_l2cap_event_coc_disconnected(struct ble_l2cap_chan *chan)
+{
+    struct ble_l2cap_event event = { };
+
+    event.type = BLE_L2CAP_EVENT_COC_DISCONNECTED;
+    event.disconnect.conn_handle = chan->conn_handle;
+    event.disconnect.chan = chan;
+
+    chan->cb(&event, chan->cb_arg);
+}
+
 void
 ble_l2cap_coc_cleanup_chan(struct ble_l2cap_chan *chan)
 {
@@ -284,6 +296,8 @@ ble_l2cap_coc_cleanup_chan(struct ble_l2cap_chan *chan)
             return;
     }
 
+    ble_l2cap_event_coc_disconnected(chan);
+
     os_mbuf_free_chain(chan->coc_rx.sdu);
     os_mbuf_free_chain(chan->coc_tx.sdu);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e488b9df/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 8da2dad..b77fa3b 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -585,18 +585,6 @@ ble_l2cap_event_coc_connected(struct ble_l2cap_chan *chan, uint16_t status)
     chan->cb(&event, chan->cb_arg);
 }
 
-static void
-ble_l2cap_event_coc_disconnected(struct ble_l2cap_chan *chan)
-{
-    struct ble_l2cap_event event = { };
-
-    event.type = BLE_L2CAP_EVENT_COC_DISCONNECTED;
-    event.disconnect.conn_handle = chan->conn_handle;
-    event.disconnect.chan = chan;
-
-    chan->cb(&event, chan->cb_arg);
-}
-
 static int
 ble_l2cap_event_coc_accept(struct ble_l2cap_chan *chan, uint16_t peer_sdu_size)
 {
@@ -891,8 +879,6 @@ ble_l2cap_sig_disc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
     rsp->dcid = htole16(chan->scid);
     rsp->scid = htole16(chan->dcid);
 
-    ble_l2cap_event_coc_disconnected(chan);
-
     ble_hs_conn_delete_chan(conn, chan);
     ble_hs_unlock();
 
@@ -922,8 +908,6 @@ ble_l2cap_sig_coc_disconnect_cb(struct ble_l2cap_sig_proc *proc, int status)
         goto done;
     }
 
-    ble_l2cap_event_coc_disconnected(chan);
-
 done:
     ble_hs_lock();
     conn = ble_hs_conn_find(chan->conn_handle);


[06/26] incubator-mynewt-core git commit: nimble/l2cap: Fix handling scid/dcid in the channel.

Posted by cc...@apache.org.
nimble/l2cap: Fix handling scid/dcid in the channel.

This patch makes sure that each l2cap channel has corretly
set scid and dcid. In the same time dcid is use when sending
data.


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

Branch: refs/heads/develop
Commit: 509cd5fb6bffd3cc1e682ae247b28687cbac2354
Parents: 5c449bb
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Thu Feb 16 06:31:43 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:41 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_att.c        | 1 +
 net/nimble/host/src/ble_l2cap.c      | 2 +-
 net/nimble/host/src/ble_l2cap_priv.h | 2 +-
 net/nimble/host/src/ble_l2cap_sig.c  | 1 +
 net/nimble/host/src/ble_sm.c         | 1 +
 5 files changed, 5 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/509cd5fb/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
index ff1eebf..329fcec 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -561,6 +561,7 @@ ble_att_create_chan(void)
     }
 
     chan->scid = BLE_L2CAP_CID_ATT;
+    chan->dcid = BLE_L2CAP_CID_ATT;
     chan->my_mtu = ble_att_preferred_mtu_val;
     chan->rx_fn = ble_att_rx;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/509cd5fb/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index e94a234..09ca9dc 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -385,7 +385,7 @@ ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
 {
     int rc;
 
-    txom = ble_l2cap_prepend_hdr(txom, chan->scid, OS_MBUF_PKTLEN(txom));
+    txom = ble_l2cap_prepend_hdr(txom, chan->dcid, OS_MBUF_PKTLEN(txom));
     if (txom == NULL) {
         return BLE_HS_ENOMEM;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/509cd5fb/net/nimble/host/src/ble_l2cap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index cc4e0ed..81e350d 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -66,6 +66,7 @@ typedef int ble_l2cap_rx_fn(uint16_t conn_handle, struct os_mbuf **rxom);
 
 struct ble_l2cap_chan {
     SLIST_ENTRY(ble_l2cap_chan) next;
+    uint16_t dcid;
     uint16_t scid;
     uint16_t my_mtu;
     uint16_t peer_mtu;      /* 0 if not exchanged. */
@@ -78,7 +79,6 @@ struct ble_l2cap_chan {
 
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
     uint16_t conn_handle;
-    uint16_t dcid;
     uint16_t psm;
     struct ble_l2cap_coc_endpoint coc_rx;
     struct ble_l2cap_coc_endpoint coc_tx;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/509cd5fb/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 40cda11..94bb271 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -1060,6 +1060,7 @@ ble_l2cap_sig_create_chan(void)
     }
 
     chan->scid = BLE_L2CAP_CID_SIG;
+    chan->dcid = BLE_L2CAP_CID_SIG;
     chan->my_mtu = BLE_L2CAP_SIG_MTU;
     chan->rx_fn = ble_l2cap_sig_rx;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/509cd5fb/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index bb2c1aa..2a880dc 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -2512,6 +2512,7 @@ ble_sm_create_chan(void)
     }
 
     chan->scid = BLE_L2CAP_CID_SM;
+    chan->dcid = BLE_L2CAP_CID_SM;
     chan->my_mtu = BLE_SM_MTU;
     chan->rx_fn = ble_sm_rx;
 


[08/26] incubator-mynewt-core git commit: bletiny: Add debug log for CoC accept

Posted by cc...@apache.org.
bletiny: Add debug log for CoC accept


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

Branch: refs/heads/develop
Commit: 3d756b3fb2a91a6865b8c1ddb7f66242ebb0289b
Parents: be58d00
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Thu Feb 16 06:29:56 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:41 2017 +0100

----------------------------------------------------------------------
 apps/bletiny/src/main.c | 3 +++
 1 file changed, 3 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3d756b3f/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index b8f1e51..442a264 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1643,6 +1643,9 @@ bletiny_l2cap_coc_accept(uint16_t conn_handle, uint16_t peer_mtu,
 {
     struct os_mbuf *sdu_rx;
 
+    console_printf("LE CoC accepting, chan: 0x%08lx, peer_mtu %d\n",
+                       (uint32_t) chan, peer_mtu);
+
     sdu_rx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
     if (!sdu_rx) {
         return BLE_HS_ENOMEM;


[16/26] incubator-mynewt-core git commit: nimble/l2cap: Remove not needed **om from ble_l2cap_rx_fn

Posted by cc...@apache.org.
nimble/l2cap: Remove not needed **om from ble_l2cap_rx_fn

Since this callback function has ble_l2cap_chan as a parameter,
we don't need to provide there os_mbuf with data as this is already
in chan->rx_buf


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

Branch: refs/heads/develop
Commit: 9b3899a0000e5c0425b097a536e98eee800e0c2e
Parents: 27bc9ed
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Thu Feb 2 15:00:25 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_att.c               |  6 +++++-
 net/nimble/host/src/ble_hs_hci_evt.c        |  7 ++-----
 net/nimble/host/src/ble_l2cap.c             |  7 +++----
 net/nimble/host/src/ble_l2cap_coc.c         |  2 +-
 net/nimble/host/src/ble_l2cap_priv.h        |  3 +--
 net/nimble/host/src/ble_l2cap_sig.c         |  8 ++++++--
 net/nimble/host/src/ble_sm.c                | 14 +++++++++-----
 net/nimble/host/test/src/ble_hs_test_util.c |  7 ++-----
 net/nimble/host/test/src/ble_l2cap_test.c   |  2 +-
 9 files changed, 30 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/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
index 7876e0d..7fe095b 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -461,11 +461,12 @@ ble_att_chan_mtu(const struct ble_l2cap_chan *chan)
 }
 
 static int
-ble_att_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
+ble_att_rx(struct ble_l2cap_chan *chan)
 {
     const struct ble_att_rx_dispatch_entry *entry;
     uint8_t op;
     uint16_t conn_handle;
+    struct os_mbuf **om;
     int rc;
 
     conn_handle = ble_l2cap_get_conn_handle(chan);
@@ -473,6 +474,9 @@ ble_att_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
         return BLE_HS_ENOTCONN;
     }
 
+    om = &chan->rx_buf;
+    BLE_HS_DBG_ASSERT(*om != NULL);
+
     rc = os_mbuf_copydata(*om, 0, 1, &op);
     if (rc != 0) {
         return BLE_HS_EMSGSIZE;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/src/ble_hs_hci_evt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_evt.c b/net/nimble/host/src/ble_hs_hci_evt.c
index 7f868a6..cd98f7f 100644
--- a/net/nimble/host/src/ble_hs_hci_evt.c
+++ b/net/nimble/host/src/ble_hs_hci_evt.c
@@ -612,7 +612,6 @@ ble_hs_hci_evt_acl_process(struct os_mbuf *om)
     struct hci_data_hdr hci_hdr;
     struct ble_hs_conn *conn;
     ble_l2cap_rx_fn *rx_cb;
-    struct os_mbuf *rx_buf;
     uint16_t conn_handle;
     int reject_cid;
     int rc;
@@ -648,7 +647,7 @@ ble_hs_hci_evt_acl_process(struct os_mbuf *om)
         reject_cid = -1;
     } else {
         /* Forward ACL data to L2CAP. */
-        rc = ble_l2cap_rx(conn, &hci_hdr, om, &rx_cb, &rx_buf, &reject_cid);
+        rc = ble_l2cap_rx(conn, &hci_hdr, om, &rx_cb, &reject_cid);
         om = NULL;
     }
 
@@ -658,10 +657,8 @@ ble_hs_hci_evt_acl_process(struct os_mbuf *om)
     case 0:
         /* Final fragment received. */
         BLE_HS_DBG_ASSERT(rx_cb != NULL);
-        BLE_HS_DBG_ASSERT(rx_buf != NULL);
-        rc = rx_cb(conn->bhc_rx_chan, &rx_buf);
+        rc = rx_cb(conn->bhc_rx_chan);
         ble_l2cap_forget_rx(conn, conn->bhc_rx_chan);
-        os_mbuf_free_chain(rx_buf);
         break;
 
     case BLE_HS_EAGAIN:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index d909b78..306eac1 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -173,6 +173,7 @@ void
 ble_l2cap_forget_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {
     conn->bhc_rx_chan = NULL;
+    os_mbuf_free_chain(chan->rx_buf);
     chan->rx_buf = NULL;
     chan->rx_len = 0;
 }
@@ -209,7 +210,7 @@ ble_l2cap_append_rx(struct ble_l2cap_chan *chan, struct os_mbuf *frag)
 static int
 ble_l2cap_rx_payload(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                      struct os_mbuf *om,
-                     ble_l2cap_rx_fn **out_rx_cb, struct os_mbuf **out_rx_buf)
+                     ble_l2cap_rx_fn **out_rx_cb)
 {
     int len_diff;
     int rc;
@@ -231,7 +232,6 @@ ble_l2cap_rx_payload(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
     } else if (len_diff == 0) {
         /* All fragments received. */
         *out_rx_cb = chan->rx_fn;
-        *out_rx_buf = chan->rx_buf;
         rc = 0;
     } else {
         /* More fragments remain. */
@@ -299,7 +299,6 @@ ble_l2cap_rx(struct ble_hs_conn *conn,
              struct hci_data_hdr *hci_hdr,
              struct os_mbuf *om,
              ble_l2cap_rx_fn **out_rx_cb,
-             struct os_mbuf **out_rx_buf,
              int *out_reject_cid)
 {
     struct ble_l2cap_chan *chan;
@@ -367,7 +366,7 @@ ble_l2cap_rx(struct ble_hs_conn *conn,
         goto err;
     }
 
-    rc = ble_l2cap_rx_payload(conn, chan, om, out_rx_cb, out_rx_buf);
+    rc = ble_l2cap_rx_payload(conn, chan, om, out_rx_cb);
     om = NULL;
     if (rc != 0) {
         goto err;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index 178370d..ac1b6fa 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -117,7 +117,7 @@ ble_l2cap_coc_srv_find(uint16_t psm)
 }
 
 static int
-ble_l2cap_coc_rx_fn(struct ble_l2cap_chan *chan, struct os_mbuf **rxom)
+ble_l2cap_coc_rx_fn(struct ble_l2cap_chan *chan)
 {
     return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/src/ble_l2cap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index 64ffa22..5db63c3 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -62,7 +62,7 @@ extern struct os_mempool ble_l2cap_chan_pool;
 
 typedef uint8_t ble_l2cap_chan_flags;
 
-typedef int ble_l2cap_rx_fn(struct ble_l2cap_chan *chan, struct os_mbuf **rxom);
+typedef int ble_l2cap_rx_fn(struct ble_l2cap_chan *chan);
 
 struct ble_l2cap_chan {
     SLIST_ENTRY(ble_l2cap_chan) next;
@@ -113,7 +113,6 @@ int ble_l2cap_rx(struct ble_hs_conn *conn,
                  struct hci_data_hdr *hci_hdr,
                  struct os_mbuf *om,
                  ble_l2cap_rx_fn **out_rx_cb,
-                 struct os_mbuf **out_rx_buf,
                  int *out_reject_cid);
 int ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                  struct os_mbuf *txom);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 93d73ed..abc00ae 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -1012,13 +1012,17 @@ ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan)
  *****************************************************************************/
 
 static int
-ble_l2cap_sig_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
+ble_l2cap_sig_rx(struct ble_l2cap_chan *chan)
 {
     struct ble_l2cap_sig_hdr hdr;
     ble_l2cap_sig_rx_fn *rx_cb;
-    uint16_t conn_handle = chan->conn_handle;
+    uint16_t conn_handle;
+    struct os_mbuf **om;
     int rc;
 
+    conn_handle = chan->conn_handle;
+    om = &chan->rx_buf;
+
     STATS_INC(ble_l2cap_stats, sig_rx);
     BLE_HS_LOG(DEBUG, "L2CAP - rxed signalling msg: ");
     ble_hs_log_mbuf(*om);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index 603d99c..ef2f41b 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -2326,12 +2326,13 @@ ble_sm_unbond(uint8_t peer_id_addr_type, const uint8_t *peer_id_addr)
 }
 
 static int
-ble_sm_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
+ble_sm_rx(struct ble_l2cap_chan *chan)
 {
     struct ble_sm_result res;
     ble_sm_rx_fn *rx_cb;
     uint8_t op;
     uint16_t conn_handle;
+    struct os_mbuf *om;
     int rc;
 
     STATS_INC(ble_l2cap_stats, sm_rx);
@@ -2341,19 +2342,22 @@ ble_sm_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
         return BLE_HS_ENOTCONN;
     }
 
-    rc = os_mbuf_copydata(*om, 0, 1, &op);
+    om = chan->rx_buf;
+    BLE_HS_DBG_ASSERT(om != NULL);
+
+    rc = os_mbuf_copydata(om, 0, 1, &op);
     if (rc != 0) {
         return BLE_HS_EBADDATA;
     }
 
     /* Strip L2CAP SM header from the front of the mbuf. */
-    os_mbuf_adj(*om, 1);
+    os_mbuf_adj(om, 1);
 
     rx_cb = ble_sm_dispatch_get(op);
     if (rx_cb != NULL) {
         memset(&res, 0, sizeof res);
 
-        rx_cb(conn_handle, om, &res);
+        rx_cb(conn_handle, &om, &res);
         ble_sm_process_result(conn_handle, &res);
         rc = res.app_status;
     } else {
@@ -2491,7 +2495,7 @@ ble_sm_init(void)
  * simple
  */
 static int
-ble_sm_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
+ble_sm_rx(struct ble_l2cap_chan *chan)
 {
     struct ble_sm_pair_fail *cmd;
     struct os_mbuf *txom;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.c b/net/nimble/host/test/src/ble_hs_test_util.c
index 9fa2d3d..d084c39 100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -934,7 +934,6 @@ ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
 {
     struct ble_hs_conn *conn;
     ble_l2cap_rx_fn *rx_cb;
-    struct os_mbuf *rx_buf;
     int reject_cid;
     int rc;
 
@@ -942,7 +941,7 @@ ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
 
     conn = ble_hs_conn_find(conn_handle);
     if (conn != NULL) {
-        rc = ble_l2cap_rx(conn, hci_hdr, om, &rx_cb, &rx_buf, &reject_cid);
+        rc = ble_l2cap_rx(conn, hci_hdr, om, &rx_cb, &reject_cid);
     } else {
         os_mbuf_free_chain(om);
     }
@@ -953,10 +952,8 @@ ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
         rc = BLE_HS_ENOTCONN;
     } else if (rc == 0) {
         TEST_ASSERT_FATAL(rx_cb != NULL);
-        TEST_ASSERT_FATAL(rx_buf != NULL);
-        rc = rx_cb(conn->bhc_rx_chan, &rx_buf);
+        rc = rx_cb(conn->bhc_rx_chan);
         ble_l2cap_forget_rx(conn, conn->bhc_rx_chan);
-        os_mbuf_free_chain(rx_buf);
     } else if (rc == BLE_HS_EAGAIN) {
         /* More fragments on the way. */
         rc = 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9b3899a0/net/nimble/host/test/src/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_l2cap_test.c b/net/nimble/host/test/src/ble_l2cap_test.c
index e68971d..3fc7876 100644
--- a/net/nimble/host/test/src/ble_l2cap_test.c
+++ b/net/nimble/host/test/src/ble_l2cap_test.c
@@ -94,7 +94,7 @@ ble_l2cap_test_util_verify_tx_update_conn(
 }
 
 static int
-ble_l2cap_test_util_dummy_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
+ble_l2cap_test_util_dummy_rx(struct ble_l2cap_chan *chan)
 {
     return 0;
 }


[12/26] incubator-mynewt-core git commit: nimble/l2cap: Add helper to clean L2CAP LE CoC channel

Posted by cc...@apache.org.
nimble/l2cap: Add helper to clean L2CAP LE CoC channel

With this patch we make sure that when CoC is used, outstanding
os_mbufs are freed when channel is closing


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

Branch: refs/heads/develop
Commit: fbceba584c5cd31f2b1253527023469b0a262162
Parents: 98e2cb9
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 15:23:09 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap.c          |  1 +
 net/nimble/host/src/ble_l2cap_coc.c      | 12 ++++++++++++
 net/nimble/host/src/ble_l2cap_coc_priv.h |  2 ++
 3 files changed, 15 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fbceba58/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index b0ab23d..046a136 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -79,6 +79,7 @@ ble_l2cap_chan_free(struct ble_l2cap_chan *chan)
     }
 
     os_mbuf_free_chain(chan->rx_buf);
+    ble_l2cap_coc_cleanup_chan(chan);
 
     rc = os_memblock_put(&ble_l2cap_chan_pool, chan);
     BLE_HS_DBG_ASSERT_EVAL(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fbceba58/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index b549ba7..9794a00 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -277,6 +277,18 @@ ble_l2cap_coc_create_srv_chan(uint16_t conn_handle, uint16_t psm,
 }
 
 void
+ble_l2cap_coc_cleanup_chan(struct ble_l2cap_chan *chan)
+{
+    /* PSM 0 is used for fixed channels. */
+    if (chan->psm == 0) {
+            return;
+    }
+
+    os_mbuf_free_chain(chan->coc_rx.sdu);
+    os_mbuf_free_chain(chan->coc_tx.sdu);
+}
+
+void
 ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
                                 uint16_t credits)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fbceba58/net/nimble/host/src/ble_l2cap_coc_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc_priv.h b/net/nimble/host/src/ble_l2cap_coc_priv.h
index 88380ea..36ab4dc 100644
--- a/net/nimble/host/src/ble_l2cap_coc_priv.h
+++ b/net/nimble/host/src/ble_l2cap_coc_priv.h
@@ -62,6 +62,7 @@ struct ble_l2cap_chan * ble_l2cap_coc_chan_alloc(uint16_t conn_handle,
                                                  struct os_mbuf *sdu_rx,
                                                  ble_l2cap_event_fn *cb,
                                                  void *cb_arg);
+void ble_l2cap_coc_cleanup_chan(struct ble_l2cap_chan *chan);
 void ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
                                     uint16_t credits);
 void ble_l2cap_coc_recv_ready(struct ble_l2cap_chan *chan,
@@ -70,6 +71,7 @@ void ble_l2cap_coc_recv_ready(struct ble_l2cap_chan *chan,
 #define ble_l2cap_coc_init()                                    0
 #define ble_l2cap_coc_create_server(psm, mtu, cb, cb_arg)       BLE_HS_ENOTSUP
 #define ble_l2cap_coc_recv_ready(chan, sdu_rx)
+#define ble_l2cap_coc_cleanup_chan(chan)
 #endif
 
 #ifdef __cplusplus


[14/26] incubator-mynewt-core git commit: nimble/l2cap: Add helper to create L2CAP channel for LE CoC

Posted by cc...@apache.org.
nimble/l2cap: Add helper to create L2CAP channel for LE CoC

With this patch, creating L2CAP channel for LE CoC purposes is moved
to ble_l2cap_coc.c. It is because in that file we want to have
all the logic related to CoC e.g. credits, available servers, data handling


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

Branch: refs/heads/develop
Commit: eb576bc2036d1322cbc1fb216dea8fa0153968d9
Parents: 98ebb51
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Thu Feb 2 11:27:14 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_coc.c      | 57 ++++++++++++++++++++++++++-
 net/nimble/host/src/ble_l2cap_coc_priv.h | 11 ++++--
 net/nimble/host/src/ble_l2cap_sig.c      | 43 +++++---------------
 3 files changed, 73 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/eb576bc2/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index 6286d43..46b903b 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -87,7 +87,7 @@ ble_l2cap_coc_create_server(uint16_t psm, uint16_t mtu,
     return 0;
 }
 
-uint16_t
+static uint16_t
 ble_l2cap_coc_get_cid(void)
 {
     static uint16_t next_cid = BLE_L2CAP_COC_CID_START;
@@ -100,7 +100,7 @@ ble_l2cap_coc_get_cid(void)
     return next_cid++;
 }
 
-struct ble_l2cap_coc_srv *
+static struct ble_l2cap_coc_srv *
 ble_l2cap_coc_srv_find(uint16_t psm)
 {
     struct ble_l2cap_coc_srv *cur, *srv;
@@ -116,6 +116,59 @@ ble_l2cap_coc_srv_find(uint16_t psm)
     return srv;
 }
 
+static int
+ble_l2cap_coc_rx_fn(uint16_t conn_handle, struct os_mbuf **rxom)
+{
+    return 0;
+}
+
+struct ble_l2cap_chan *
+ble_l2cap_coc_chan_alloc(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
+                         struct os_mbuf *sdu_rx, ble_l2cap_event_fn *cb,
+                         void *cb_arg)
+{
+    struct ble_l2cap_chan *chan;
+
+    chan = ble_l2cap_chan_alloc();
+    if (!chan) {
+        return NULL;
+    }
+
+    chan->conn_handle = conn_handle;
+    chan->psm = psm;
+    chan->cb = cb;
+    chan->cb_arg = cb_arg;
+    chan->scid = ble_l2cap_coc_get_cid();
+    chan->my_mtu = BLE_L2CAP_COC_MTU;
+    chan->rx_fn = ble_l2cap_coc_rx_fn;
+    chan->coc_rx.mtu = mtu;
+    chan->coc_rx.credits = 10; /* FIXME Calculate it */
+    chan->coc_rx.sdu = sdu_rx;
+
+    return chan;
+}
+
+int
+ble_l2cap_coc_create_srv_chan(uint16_t conn_handle, uint16_t psm,
+                              struct ble_l2cap_chan **chan)
+{
+    struct ble_l2cap_coc_srv *srv;
+
+    /* Check if there is server registered on this PSM */
+    srv = ble_l2cap_coc_srv_find(psm);
+    if (!srv) {
+        return BLE_HS_ENOTSUP;
+    }
+
+    *chan = ble_l2cap_coc_chan_alloc(conn_handle, psm, srv->mtu, NULL, srv->cb,
+                                     srv->cb_arg);
+    if (!*chan) {
+        return BLE_HS_ENOMEM;
+    }
+
+    return 0;
+}
+
 int
 ble_l2cap_coc_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/eb576bc2/net/nimble/host/src/ble_l2cap_coc_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc_priv.h b/net/nimble/host/src/ble_l2cap_coc_priv.h
index 31e81e7..63c593e 100644
--- a/net/nimble/host/src/ble_l2cap_coc_priv.h
+++ b/net/nimble/host/src/ble_l2cap_coc_priv.h
@@ -23,9 +23,9 @@
 #include <inttypes.h>
 #include "syscfg/syscfg.h"
 #include "os/queue.h"
+#include "os/os_mbuf.h"
 #include "host/ble_l2cap.h"
 #include "ble_l2cap_sig_priv.h"
-
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -52,10 +52,15 @@ struct ble_l2cap_coc_srv {
 
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
 int ble_l2cap_coc_init(void);
-uint16_t ble_l2cap_coc_get_cid(void);
 int ble_l2cap_coc_create_server(uint16_t psm, uint16_t mtu,
                                 ble_l2cap_event_fn *cb, void *cb_arg);
-struct ble_l2cap_coc_srv * ble_l2cap_coc_srv_find(uint16_t psm);
+int ble_l2cap_coc_create_srv_chan(uint16_t conn_handle, uint16_t psm,
+                                  struct ble_l2cap_chan **chan);
+struct ble_l2cap_chan * ble_l2cap_coc_chan_alloc(uint16_t conn_handle,
+                                                 uint16_t psm, uint16_t mtu,
+                                                 struct os_mbuf *sdu_rx,
+                                                 ble_l2cap_event_fn *cb,
+                                                 void *cb_arg);
 #else
 #define ble_l2cap_coc_init()                                    0
 #define ble_l2cap_coc_create_server(psm, mtu, cb, cb_arg)       BLE_HS_ENOTSUP

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/eb576bc2/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 7919a89..d6219d3 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -551,6 +551,8 @@ static int
 ble_l2cap_sig_ble_hs_err2coc_err(uint16_t ble_hs_err)
 {
     switch (ble_hs_err) {
+    case BLE_HS_ENOTSUP:
+        return BLE_L2CAP_COC_ERR_UNKNOWN_LE_PSM;
     case BLE_HS_ENOMEM:
         return BLE_L2CAP_COC_ERR_NO_RESOURCES;
     case BLE_HS_EAUTHEN:
@@ -631,8 +633,7 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
     struct ble_l2cap_sig_le_con_req *req;
     struct os_mbuf *txom;
     struct ble_l2cap_sig_le_con_rsp *rsp;
-    struct ble_l2cap_coc_srv *srv;
-    struct ble_l2cap_chan *chan;
+    struct ble_l2cap_chan *chan = NULL;
     struct ble_hs_conn *conn;
     uint16_t scid;
 
@@ -657,13 +658,6 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
     ble_hs_lock();
     conn = ble_hs_conn_find_assert(conn_handle);
 
-    /* Check if there is server registered on this PSM */
-    srv = ble_l2cap_coc_srv_find(le16toh(req->psm));
-    if (!srv) {
-        rsp->result = htole16(BLE_L2CAP_COC_ERR_UNKNOWN_LE_PSM);
-        goto failed;
-    }
-
     /* Verify CID */
     scid = le16toh(req->scid);
     if (scid < BLE_L2CAP_COC_CID_START || scid > BLE_L2CAP_COC_CID_END) {
@@ -672,19 +666,15 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
         goto failed;
     }
 
-    chan = ble_l2cap_chan_alloc();
-    if (!chan) {
-        rsp->result = htole16(BLE_L2CAP_COC_ERR_NO_RESOURCES);
+    rc = ble_l2cap_coc_create_srv_chan(conn_handle, le16toh(req->psm), &chan);
+    if (rc != 0) {
+        uint16_t coc_err = ble_l2cap_sig_ble_hs_err2coc_err(rc);
+        rsp->result = htole16(coc_err);
         goto failed;
     }
 
-    chan->cb = srv->cb;
-    chan->cb_arg = srv->cb_arg;
-    chan->conn_handle = conn_handle;
-    chan->dcid = scid;
-    chan->my_mtu = BLE_L2CAP_COC_MTU;
-
     /* Fill up remote configuration. Note MPS is the L2CAP MTU*/
+    chan->dcid = scid;
     chan->peer_mtu = le16toh(req->mps);
     chan->coc_tx.credits = le16toh(req->credits);
     chan->coc_tx.mtu = le16toh(req->mtu);
@@ -697,10 +687,6 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
         goto failed;
     }
 
-    chan->scid = ble_l2cap_coc_get_cid();
-    chan->coc_rx.mtu = srv->mtu;
-    chan->coc_rx.credits = 10; //FIXME Calculate it
-
     rsp->dcid = htole16(chan->scid);
     rsp->credits = htole16(chan->coc_rx.credits);
     rsp->mps = htole16(chan->my_mtu);
@@ -795,7 +781,7 @@ ble_l2cap_sig_coc_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
     struct ble_l2cap_sig_proc *proc;
     struct os_mbuf *txom;
     struct ble_l2cap_sig_le_con_req *req;
-    struct ble_l2cap_chan *chan;
+    struct ble_l2cap_chan *chan = NULL;
     int rc;
 
     if (!sdu_rx || !cb) {
@@ -810,7 +796,7 @@ ble_l2cap_sig_coc_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
         return BLE_HS_ENOTCONN;
     }
 
-    chan = ble_l2cap_chan_alloc();
+    chan = ble_l2cap_coc_chan_alloc(conn_handle, psm, mtu, sdu_rx, cb, cb_arg);
     if (!chan) {
         ble_hs_unlock();
         return BLE_HS_ENOMEM;
@@ -823,15 +809,6 @@ ble_l2cap_sig_coc_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
         return BLE_HS_ENOMEM;
     }
 
-    chan->scid = ble_l2cap_coc_get_cid();
-    chan->my_mtu = BLE_L2CAP_COC_MTU;
-    chan->coc_rx.credits = 10;
-    chan->coc_rx.mtu = mtu;
-    chan->coc_rx.sdu = sdu_rx;
-    chan->cb = cb;
-    chan->cb_arg = cb_arg;
-    chan->conn_handle = conn_handle;
-
     proc->op = BLE_L2CAP_SIG_PROC_OP_CONNECT;
     proc->id = ble_l2cap_sig_next_id();
     proc->conn_handle = conn_handle;


[10/26] incubator-mynewt-core git commit: nimble/l2cap: Refactor handling L2CAP reject command

Posted by cc...@apache.org.
nimble/l2cap: Refactor handling L2CAP reject command

With this patch L2CAP reject command uses a new way of preparing
command and sending it


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

Branch: refs/heads/develop
Commit: ba9de55afb41bf1c1b837ae93041953aa2f59493
Parents: a443035
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Tue Feb 28 12:14:46 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig_cmd.c  | 40 +++++----------------------
 net/nimble/host/src/ble_l2cap_sig_priv.h |  1 +
 2 files changed, 8 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ba9de55a/net/nimble/host/src/ble_l2cap_sig_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_cmd.c b/net/nimble/host/src/ble_l2cap_sig_cmd.c
index 189efd7..e6a7209 100644
--- a/net/nimble/host/src/ble_l2cap_sig_cmd.c
+++ b/net/nimble/host/src/ble_l2cap_sig_cmd.c
@@ -94,48 +94,22 @@ ble_l2cap_sig_hdr_write(void *payload, uint16_t len,
     BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_HDR_SZ);
     ble_l2cap_sig_hdr_swap(payload, src);
 }
-static void
-ble_l2cap_sig_reject_swap(struct ble_l2cap_sig_reject *dst,
-                          struct ble_l2cap_sig_reject *src)
-{
-    dst->reason = TOFROMLE16(src->reason);
-}
-
-static void
-ble_l2cap_sig_reject_write(void *payload, uint16_t len,
-                           struct ble_l2cap_sig_reject *src,
-                           void *data, int data_len)
-{
-    uint8_t *u8ptr;
-
-    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_REJECT_MIN_SZ + data_len);
-
-    ble_l2cap_sig_reject_swap(payload, src);
-
-    u8ptr = payload;
-    u8ptr += BLE_L2CAP_SIG_REJECT_MIN_SZ;
-    memcpy(u8ptr, data, data_len);
-}
 
 int
 ble_l2cap_sig_reject_tx(uint16_t conn_handle, uint8_t id, uint16_t reason,
                         void *data, int data_len)
 {
-    struct ble_l2cap_sig_reject cmd;
+    struct ble_l2cap_sig_reject *cmd;
     struct os_mbuf *txom;
-    void *payload_buf;
-    int rc;
 
-    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_REJECT, id,
-                                BLE_L2CAP_SIG_REJECT_MIN_SZ + data_len, &txom,
-                                &payload_buf);
-    if (rc != 0) {
-        return rc;
+    cmd = ble_l2cap_sig_cmd_get(BLE_L2CAP_SIG_OP_REJECT, id,
+                           sizeof(*cmd) + data_len, &txom);
+    if (!cmd) {
+        return BLE_HS_ENOMEM;
     }
 
-    cmd.reason = reason;
-    ble_l2cap_sig_reject_write(payload_buf, txom->om_len, &cmd,
-                               data, data_len);
+    cmd->reason = htole16(reason);
+    memcpy(cmd->data, data, data_len);
 
     STATS_INC(ble_l2cap_stats, sig_rx);
     return ble_l2cap_sig_tx(conn_handle, txom);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ba9de55a/net/nimble/host/src/ble_l2cap_sig_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_priv.h b/net/nimble/host/src/ble_l2cap_sig_priv.h
index f089dcb..ad3b846 100644
--- a/net/nimble/host/src/ble_l2cap_sig_priv.h
+++ b/net/nimble/host/src/ble_l2cap_sig_priv.h
@@ -39,6 +39,7 @@ struct ble_l2cap_sig_hdr {
 #define BLE_L2CAP_SIG_REJECT_MIN_SZ         2
 struct ble_l2cap_sig_reject {
     uint16_t reason;
+    uint8_t data[0];
 } __attribute__((packed));
 
 #define BLE_L2CAP_SIG_UPDATE_REQ_SZ         8


[07/26] incubator-mynewt-core git commit: bletiny: Add error print when L2CAP LE CoC disconnect fails

Posted by cc...@apache.org.
bletiny: Add error print when L2CAP LE CoC disconnect fails


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

Branch: refs/heads/develop
Commit: 5c449bb18c87b87e9c00df6a324c0ef87e6dadeb
Parents: 3d756b3
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Mar 1 11:16:15 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:41 2017 +0100

----------------------------------------------------------------------
 apps/bletiny/src/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5c449bb1/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index 442a264..c0177c0 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -1740,6 +1740,7 @@ bletiny_l2cap_disconnect(uint16_t conn_handle, uint16_t idx)
     struct bletiny_conn *conn;
     struct bletiny_l2cap_coc *coc;
     int i;
+    int rc = 0;
 
     conn = bletiny_conn_find(conn_handle);
     assert(conn != NULL);
@@ -1753,9 +1754,12 @@ bletiny_l2cap_disconnect(uint16_t conn_handle, uint16_t idx)
     }
     assert(coc != NULL);
 
-    ble_l2cap_disconnect(coc->chan);
+    rc = ble_l2cap_disconnect(coc->chan);
+    if (rc) {
+        console_printf("Could not disconnect channel rc=%d\n", rc);
+    }
 
-    return 0;
+    return rc;
 #endif
 }
 


[23/26] incubator-mynewt-core git commit: nimble/l2cap: Fix for possible memory leak

Posted by cc...@apache.org.
nimble/l2cap: Fix for possible memory leak

With this patch we remove allocated memory for proc in case we could
not allocate signaling request later on.


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

Branch: refs/heads/develop
Commit: 34e648624d8b005a1aec4bd6ecb1b6a6a3b3cafa
Parents: 44df175
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Mar 1 09:24:35 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/34e64862/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index a33ddef..444a8f6 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -990,7 +990,8 @@ ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan)
     req = ble_l2cap_sig_cmd_get(BLE_L2CAP_SIG_OP_DISCONN_REQ, proc->id,
                                 sizeof(*req), &txom);
     if (!req) {
-        return BLE_HS_ENOMEM;
+        rc = BLE_HS_ENOMEM;
+        goto done;
     }
 
     req->dcid = htole16(chan->dcid);
@@ -998,6 +999,7 @@ ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan)
 
     rc = ble_l2cap_sig_tx(proc->conn_handle, txom);
 
+done:
     ble_l2cap_sig_process_status(proc, rc);
 
     return rc;


[17/26] incubator-mynewt-core git commit: nimble/l2cap: Handle REJECT CMD on L2CAP LE CoC connection create req

Posted by cc...@apache.org.
nimble/l2cap: Handle REJECT CMD on L2CAP LE CoC connection create req

With this patch, if legacy device response with REJECT CMD on
LE Create Base Connection request, application will be corretly
notified.


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

Branch: refs/heads/develop
Commit: 44df175e480d0935346edf5b5b8be3bfa03f3304
Parents: e488b9d
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 21:49:47 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_coc.c |  5 ++++
 net/nimble/host/src/ble_l2cap_sig.c | 39 +++++++++++++++++++++++++++++++-
 2 files changed, 43 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/44df175e/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index ec01fec..f7ecef4 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -281,6 +281,11 @@ ble_l2cap_event_coc_disconnected(struct ble_l2cap_chan *chan)
 {
     struct ble_l2cap_event event = { };
 
+    /* FIXME */
+    if (!chan->cb) {
+        return;
+    }
+
     event.type = BLE_L2CAP_EVENT_COC_DISCONNECTED;
     event.disconnect.conn_handle = chan->conn_handle;
     event.disconnect.chan = chan;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/44df175e/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index b77fa3b..a33ddef 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -92,6 +92,7 @@ typedef int ble_l2cap_sig_rx_fn(uint16_t conn_handle,
 static ble_l2cap_sig_rx_fn ble_l2cap_sig_rx_noop;
 static ble_l2cap_sig_rx_fn ble_l2cap_sig_update_req_rx;
 static ble_l2cap_sig_rx_fn ble_l2cap_sig_update_rsp_rx;
+static ble_l2cap_sig_rx_fn ble_l2cap_sig_rx_reject;
 
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
 static ble_l2cap_sig_rx_fn ble_l2cap_sig_coc_req_rx;
@@ -108,7 +109,7 @@ static ble_l2cap_sig_rx_fn ble_l2cap_sig_le_credits_rx;
 #endif
 
 static ble_l2cap_sig_rx_fn * const ble_l2cap_sig_dispatch[] = {
-    [BLE_L2CAP_SIG_OP_REJECT]               = ble_l2cap_sig_rx_noop,
+    [BLE_L2CAP_SIG_OP_REJECT]               = ble_l2cap_sig_rx_reject,
     [BLE_L2CAP_SIG_OP_CONNECT_RSP]          = ble_l2cap_sig_rx_noop,
     [BLE_L2CAP_SIG_OP_CONFIG_RSP]           = ble_l2cap_sig_rx_noop,
     [BLE_L2CAP_SIG_OP_DISCONN_REQ]          = ble_l2cap_sig_disc_req_rx,
@@ -613,6 +614,15 @@ ble_l2cap_sig_coc_connect_cb(struct ble_l2cap_sig_proc *proc, int status)
     }
 
     ble_l2cap_event_coc_connected(chan, status);
+
+    if (status) {
+        /* Normally in channel free we send disconnected event to application.
+         * However in case on error during creation connection we send connected
+         * event with error status. To avoid additional disconnected event lets
+         * clear callbacks since we don't needed it anymore.*/
+        chan->cb = NULL;
+        ble_l2cap_chan_free(chan);
+    }
 }
 
 static int
@@ -1037,6 +1047,33 @@ ble_l2cap_sig_le_credits(struct ble_l2cap_chan *chan, uint16_t credits)
     return ble_l2cap_sig_tx(chan->conn_handle, txom);
 }
 #endif
+
+static int
+ble_l2cap_sig_rx_reject(uint16_t conn_handle,
+                        struct ble_l2cap_sig_hdr *hdr,
+                        struct os_mbuf **om)
+{
+    struct ble_l2cap_sig_proc *proc;
+    proc = ble_l2cap_sig_proc_extract(conn_handle,
+                                         BLE_L2CAP_SIG_PROC_OP_CONNECT,
+                                         hdr->identifier);
+   if (!proc) {
+       return 0;
+   }
+
+   switch (proc->id) {
+#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
+       case BLE_L2CAP_SIG_PROC_OP_CONNECT:
+           ble_l2cap_sig_coc_connect_cb(proc, BLE_HS_EREJECT);
+           break;
+#endif
+       default:
+           break;
+   }
+
+   ble_l2cap_sig_proc_free(proc);
+   return 0;
+}
 /*****************************************************************************
  * $misc                                                                     *
  *****************************************************************************/


[13/26] incubator-mynewt-core git commit: nimble/l2cap: Refactor update parameters handling

Posted by cc...@apache.org.
nimble/l2cap: Refactor update parameters handling

With this patch l2cap update parameters is handled in similar way as
other signaling commands.


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

Branch: refs/heads/develop
Commit: 98ebb5133ac345d8a9a93b335722e88b68373673
Parents: 506e373
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Thu Feb 2 10:21:57 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig.c         | 55 +++++++++-----
 net/nimble/host/src/ble_l2cap_sig_cmd.c     | 94 ------------------------
 net/nimble/host/src/ble_l2cap_sig_priv.h    | 17 +----
 net/nimble/host/test/src/ble_hs_test_util.c | 42 ++++++++++-
 net/nimble/host/test/src/ble_l2cap_test.c   | 20 ++++-
 5 files changed, 98 insertions(+), 130 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98ebb513/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 9292137..7919a89 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -336,7 +336,9 @@ ble_l2cap_sig_update_req_rx(uint16_t conn_handle,
                             struct ble_l2cap_sig_hdr *hdr,
                             struct os_mbuf **om)
 {
-    struct ble_l2cap_sig_update_req req;
+    struct ble_l2cap_sig_update_req *req;
+    struct os_mbuf *txom;
+    struct ble_l2cap_sig_update_rsp *rsp;
     struct ble_gap_upd_params params;
     ble_hs_conn_flags_t conn_flags;
     uint16_t l2cap_result;
@@ -361,12 +363,12 @@ ble_l2cap_sig_update_req_rx(uint16_t conn_handle,
         return BLE_HS_EREJECT;
     }
 
-    ble_l2cap_sig_update_req_parse((*om)->om_data, (*om)->om_len, &req);
+    req = (struct ble_l2cap_sig_update_req *)(*om)->om_data;
 
-    params.itvl_min = req.itvl_min;
-    params.itvl_max = req.itvl_max;
-    params.latency = req.slave_latency;
-    params.supervision_timeout = req.timeout_multiplier;
+    params.itvl_min = le16toh(req->itvl_min);
+    params.itvl_max = le16toh(req->itvl_max);
+    params.latency = le16toh(req->slave_latency);
+    params.supervision_timeout = le16toh(req->timeout_multiplier);
     params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
     params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
 
@@ -383,11 +385,19 @@ ble_l2cap_sig_update_req_rx(uint16_t conn_handle,
         l2cap_result = BLE_L2CAP_SIG_UPDATE_RSP_RESULT_REJECT;
     }
 
+    rsp = ble_l2cap_sig_cmd_get(BLE_L2CAP_SIG_OP_UPDATE_RSP, hdr->identifier,
+                                sizeof(*rsp), &txom);
+    if (!rsp) {
+        /* No memory for response, lest allow to timeout on remote side */
+        return 0;
+    }
+
+    rsp->result = htole16(l2cap_result);
+
     /* Send L2CAP response. */
-    rc = ble_l2cap_sig_update_rsp_tx(conn_handle, hdr->identifier,
-                                         l2cap_result);
+    ble_l2cap_sig_tx(conn_handle, txom);
 
-    return rc;
+    return 0;
 }
 
 static int
@@ -395,7 +405,7 @@ ble_l2cap_sig_update_rsp_rx(uint16_t conn_handle,
                             struct ble_l2cap_sig_hdr *hdr,
                             struct os_mbuf **om)
 {
-    struct ble_l2cap_sig_update_rsp rsp;
+    struct ble_l2cap_sig_update_rsp *rsp;
     struct ble_l2cap_sig_proc *proc;
     int cb_status;
     int rc;
@@ -413,9 +423,9 @@ ble_l2cap_sig_update_rsp_rx(uint16_t conn_handle,
         goto done;
     }
 
-    ble_l2cap_sig_update_rsp_parse((*om)->om_data, (*om)->om_len, &rsp);
+    rsp = (struct ble_l2cap_sig_update_rsp *)(*om)->om_data;
 
-    switch (rsp.result) {
+    switch (le16toh(rsp->result)) {
     case BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT:
         cb_status = 0;
         rc = 0;
@@ -443,7 +453,8 @@ ble_l2cap_sig_update(uint16_t conn_handle,
                      struct ble_l2cap_sig_update_params *params,
                      ble_l2cap_sig_update_fn *cb, void *cb_arg)
 {
-    struct ble_l2cap_sig_update_req req;
+    struct os_mbuf *txom;
+    struct ble_l2cap_sig_update_req *req;
     struct ble_l2cap_sig_proc *proc;
     struct ble_l2cap_chan *chan;
     struct ble_hs_conn *conn;
@@ -481,12 +492,20 @@ ble_l2cap_sig_update(uint16_t conn_handle,
     proc->update.cb = cb;
     proc->update.cb_arg = cb_arg;
 
-    req.itvl_min = params->itvl_min;
-    req.itvl_max = params->itvl_max;
-    req.slave_latency = params->slave_latency;
-    req.timeout_multiplier = params->timeout_multiplier;
+    req = ble_l2cap_sig_cmd_get(BLE_L2CAP_SIG_OP_UPDATE_REQ, proc->id,
+                                sizeof(*req), &txom);
+    if (!req) {
+        STATS_INC(ble_l2cap_stats, update_fail);
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
 
-    rc = ble_l2cap_sig_update_req_tx(conn_handle, proc->id, &req);
+    req->itvl_min = htole16(params->itvl_min);
+    req->itvl_max = htole16(params->itvl_max);
+    req->slave_latency = htole16(params->slave_latency);
+    req->timeout_multiplier = htole16(params->timeout_multiplier);
+
+    rc = ble_l2cap_sig_tx(conn_handle, txom);
 
 done:
     ble_l2cap_sig_process_status(proc, rc);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98ebb513/net/nimble/host/src/ble_l2cap_sig_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_cmd.c b/net/nimble/host/src/ble_l2cap_sig_cmd.c
index f7e68c8..189efd7 100644
--- a/net/nimble/host/src/ble_l2cap_sig_cmd.c
+++ b/net/nimble/host/src/ble_l2cap_sig_cmd.c
@@ -158,99 +158,6 @@ ble_l2cap_sig_reject_invalid_cid_tx(uint16_t conn_handle, uint8_t id,
                                  &data, sizeof data);
 }
 
-static void
-ble_l2cap_sig_update_req_swap(struct ble_l2cap_sig_update_req *dst,
-                              struct ble_l2cap_sig_update_req *src)
-{
-    dst->itvl_min = TOFROMLE16(src->itvl_min);
-    dst->itvl_max = TOFROMLE16(src->itvl_max);
-    dst->slave_latency = TOFROMLE16(src->slave_latency);
-    dst->timeout_multiplier = TOFROMLE16(src->timeout_multiplier);
-}
-
-void
-ble_l2cap_sig_update_req_parse(void *payload, int len,
-                               struct ble_l2cap_sig_update_req *dst)
-{
-    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_REQ_SZ);
-    ble_l2cap_sig_update_req_swap(dst, payload);
-}
-
-void
-ble_l2cap_sig_update_req_write(void *payload, int len,
-                               struct ble_l2cap_sig_update_req *src)
-{
-    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_REQ_SZ);
-    ble_l2cap_sig_update_req_swap(payload, src);
-}
-
-int
-ble_l2cap_sig_update_req_tx(uint16_t conn_handle, uint8_t id,
-                            struct ble_l2cap_sig_update_req *req)
-{
-    struct os_mbuf *txom;
-    void *payload_buf;
-    int rc;
-
-    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_REQ, id,
-                                BLE_L2CAP_SIG_UPDATE_REQ_SZ, &txom,
-                                &payload_buf);
-    if (rc != 0) {
-        return rc;
-    }
-
-    ble_l2cap_sig_update_req_write(payload_buf, BLE_L2CAP_SIG_UPDATE_REQ_SZ,
-                                   req);
-
-    return ble_l2cap_sig_tx(conn_handle, txom);
-}
-
-static void
-ble_l2cap_sig_update_rsp_swap(struct ble_l2cap_sig_update_rsp *dst,
-                              struct ble_l2cap_sig_update_rsp *src)
-{
-    dst->result = TOFROMLE16(src->result);
-}
-
-void
-ble_l2cap_sig_update_rsp_parse(void *payload, int len,
-                               struct ble_l2cap_sig_update_rsp *dst)
-{
-    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_RSP_SZ);
-    ble_l2cap_sig_update_rsp_swap(dst, payload);
-}
-
-void
-ble_l2cap_sig_update_rsp_write(void *payload, int len,
-                               struct ble_l2cap_sig_update_rsp *src)
-{
-    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_RSP_SZ);
-    ble_l2cap_sig_update_rsp_swap(payload, src);
-}
-
-int
-ble_l2cap_sig_update_rsp_tx(uint16_t conn_handle, uint8_t id, uint16_t result)
-{
-    struct ble_l2cap_sig_update_rsp rsp;
-    struct os_mbuf *txom;
-    void *payload_buf;
-    int rc;
-
-    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_RSP, id,
-                                BLE_L2CAP_SIG_UPDATE_RSP_SZ, &txom,
-                                &payload_buf);
-    if (rc != 0) {
-        return rc;
-    }
-
-    rsp.result = result;
-    ble_l2cap_sig_update_rsp_write(payload_buf, BLE_L2CAP_SIG_UPDATE_RSP_SZ,
-                                   &rsp);
-
-    return ble_l2cap_sig_tx(conn_handle, txom);
-}
-
-#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
 void *
 ble_l2cap_sig_cmd_get(uint8_t opcode, uint8_t id, uint16_t len,
                       struct os_mbuf **txom)
@@ -275,4 +182,3 @@ ble_l2cap_sig_cmd_get(uint8_t opcode, uint8_t id, uint16_t len,
 
     return hdr->data;
 }
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98ebb513/net/nimble/host/src/ble_l2cap_sig_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_priv.h b/net/nimble/host/src/ble_l2cap_sig_priv.h
index 48bff7e..f089dcb 100644
--- a/net/nimble/host/src/ble_l2cap_sig_priv.h
+++ b/net/nimble/host/src/ble_l2cap_sig_priv.h
@@ -92,28 +92,15 @@ void ble_l2cap_sig_hdr_write(void *payload, uint16_t len,
 int ble_l2cap_sig_reject_tx(uint16_t conn_handle,
                             uint8_t id, uint16_t reason,
                             void *data, int data_len);
-void ble_l2cap_sig_update_req_parse(void *payload, int len,
-                                    struct ble_l2cap_sig_update_req *req);
-void ble_l2cap_sig_update_req_write(void *payload, int len,
-                                    struct ble_l2cap_sig_update_req *src);
-int ble_l2cap_sig_update_req_tx(uint16_t conn_handle, uint8_t id,
-                                struct ble_l2cap_sig_update_req *req);
-void ble_l2cap_sig_update_rsp_parse(void *payload, int len,
-                                    struct ble_l2cap_sig_update_rsp *cmd);
-void ble_l2cap_sig_update_rsp_write(void *payload, int len,
-                                    struct ble_l2cap_sig_update_rsp *src);
-int ble_l2cap_sig_update_rsp_tx(uint16_t conn_handle, uint8_t id,
-                                uint16_t result);
 int ble_l2cap_sig_reject_invalid_cid_tx(uint16_t conn_handle, uint8_t id,
                                         uint16_t src_cid, uint16_t dst_cid);
 int ble_l2cap_sig_tx(uint16_t conn_handle, struct os_mbuf *txom);
+void *ble_l2cap_sig_cmd_get(uint8_t opcode, uint8_t id, uint16_t len,
+                            struct os_mbuf **txom);
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
 int ble_l2cap_sig_coc_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
                               struct os_mbuf *sdu_rx,
                               ble_l2cap_event_fn *cb, void *cb_arg);
-void *ble_l2cap_sig_cmd_get(uint8_t opcode, uint8_t id, uint16_t len,
-                            struct os_mbuf **txom);
-
 int ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan);
 #else
 #define ble_l2cap_sig_coc_connect(conn_handle, psm, mtu, sdu_rx, cb, cb_arg) \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98ebb513/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.c b/net/nimble/host/test/src/ble_hs_test_util.c
index cd9e1b2..ceabd1c 100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -1764,6 +1764,24 @@ ble_hs_test_util_verify_tx_l2cap_sig_hdr(uint8_t op, uint8_t id,
     return om;
 }
 
+static void
+ble_l2cap_test_update_req_swap(struct ble_l2cap_sig_update_req *dst,
+                               struct ble_l2cap_sig_update_req *src)
+{
+    dst->itvl_min = le16toh(src->itvl_min);
+    dst->itvl_max = le16toh(src->itvl_max);
+    dst->slave_latency = le16toh(src->slave_latency);
+    dst->timeout_multiplier = le16toh(src->timeout_multiplier);
+}
+
+static void
+ble_l2cap_test_update_req_parse(void *payload, int len,
+                               struct ble_l2cap_sig_update_req *dst)
+{
+    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_REQ_SZ);
+    ble_l2cap_test_update_req_swap(dst, payload);
+}
+
 /**
  * @return                      The L2CAP sig identifier in the request.
  */
@@ -1783,7 +1801,7 @@ ble_hs_test_util_verify_tx_l2cap_update_req(
                                                   &hdr);
 
     /* Verify payload. */
-    ble_l2cap_sig_update_req_parse(om->om_data, om->om_len, &req);
+    ble_l2cap_test_update_req_parse(om->om_data, om->om_len, &req);
     TEST_ASSERT(req.itvl_min == params->itvl_min);
     TEST_ASSERT(req.itvl_max == params->itvl_max);
     TEST_ASSERT(req.slave_latency == params->slave_latency);
@@ -1792,6 +1810,26 @@ ble_hs_test_util_verify_tx_l2cap_update_req(
     return hdr.identifier;
 }
 
+static void
+ble_l2cap_sig_update_rsp_parse(void *payload, int len,
+                               struct ble_l2cap_sig_update_rsp *dst)
+{
+    struct ble_l2cap_sig_update_rsp *src = payload;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_RSP_SZ);
+    dst->result = le16toh(src->result);
+}
+
+static void
+ble_l2cap_test_update_rsp_write(void *payload, int len,
+                               struct ble_l2cap_sig_update_rsp *src)
+{
+    struct ble_l2cap_sig_update_rsp *dst = payload;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_RSP_SZ);
+    dst->result = htole16(src->result);
+}
+
 int
 ble_hs_test_util_rx_l2cap_update_rsp(uint16_t conn_handle,
                                      uint8_t id, uint16_t result)
@@ -1811,7 +1849,7 @@ ble_hs_test_util_rx_l2cap_update_rsp(uint16_t conn_handle,
     TEST_ASSERT_FATAL(rc == 0);
 
     rsp.result = result;
-    ble_l2cap_sig_update_rsp_write(v, BLE_L2CAP_SIG_UPDATE_RSP_SZ, &rsp);
+    ble_l2cap_test_update_rsp_write(v, BLE_L2CAP_SIG_UPDATE_RSP_SZ, &rsp);
 
     rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SIG,
                                               &hci_hdr, om);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98ebb513/net/nimble/host/test/src/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_l2cap_test.c b/net/nimble/host/test/src/ble_l2cap_test.c
index f4f16bc..2958a6c 100644
--- a/net/nimble/host/test/src/ble_l2cap_test.c
+++ b/net/nimble/host/test/src/ble_l2cap_test.c
@@ -44,6 +44,24 @@ ble_l2cap_test_util_init(void)
 }
 
 static void
+ble_l2cap_test_update_req_swap(struct ble_l2cap_sig_update_req *dst,
+                              struct ble_l2cap_sig_update_req *src)
+{
+    dst->itvl_min = le16toh(src->itvl_min);
+    dst->itvl_max = le16toh(src->itvl_max);
+    dst->slave_latency = le16toh(src->slave_latency);
+    dst->timeout_multiplier = le16toh(src->timeout_multiplier);
+}
+
+static void
+ble_l2cap_test_update_req_write(void *payload, int len,
+                               struct ble_l2cap_sig_update_req *src)
+{
+    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_REQ_SZ);
+    ble_l2cap_test_update_req_swap(payload, src);
+}
+
+static void
 ble_l2cap_test_util_rx_update_req(uint16_t conn_handle, uint8_t id,
                                   struct ble_l2cap_sig_update_params *params)
 {
@@ -65,7 +83,7 @@ ble_l2cap_test_util_rx_update_req(uint16_t conn_handle, uint8_t id,
     req.itvl_max = params->itvl_max;
     req.slave_latency = params->slave_latency;
     req.timeout_multiplier = params->timeout_multiplier;
-    ble_l2cap_sig_update_req_write(v, BLE_L2CAP_SIG_UPDATE_REQ_SZ, &req);
+    ble_l2cap_test_update_req_write(v, BLE_L2CAP_SIG_UPDATE_REQ_SZ, &req);
 
     ble_hs_test_util_set_ack(
         ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,


[18/26] incubator-mynewt-core git commit: nimble/l2cap: Add handling receiving SDU over L2CAP LE CoC

Posted by cc...@apache.org.
nimble/l2cap: Add handling receiving SDU over L2CAP LE CoC

With this patch nimble can receive full SDU from remote device.
Once it is done, callback with SDU is called to application.


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

Branch: refs/heads/develop
Commit: 98e2cb9d3c8cbb75be0c6462aa09391d8df31cbd
Parents: 754c445
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 15:20:50 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap.c          |   2 +-
 net/nimble/host/src/ble_l2cap_coc.c      | 136 ++++++++++++++++++++++++++
 net/nimble/host/src/ble_l2cap_coc_priv.h |   4 +
 3 files changed, 141 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98e2cb9d/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index e048ce9..b0ab23d 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -166,7 +166,7 @@ ble_l2cap_send(struct ble_l2cap_chan *chan, struct os_mbuf *sdu)
 void
 ble_l2cap_recv_ready(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_rx)
 {
-    /*TODO In here we going to update sdu_rx buffer */
+    ble_l2cap_coc_recv_ready(chan, sdu_rx);
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98e2cb9d/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index 8d5f71f..b549ba7 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -28,6 +28,8 @@
 
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
 
+#define BLE_L2CAP_SDU_SIZE              2
+
 STAILQ_HEAD(ble_l2cap_coc_srv_list, ble_l2cap_coc_srv);
 
 static struct ble_l2cap_coc_srv_list ble_l2cap_coc_srvs;
@@ -116,9 +118,115 @@ ble_l2cap_coc_srv_find(uint16_t psm)
     return srv;
 }
 
+static void
+ble_l2cap_event_coc_received_data(struct ble_l2cap_chan *chan,
+                                  struct os_mbuf *om)
+{
+    struct ble_l2cap_event event;
+
+    event.type = BLE_L2CAP_EVENT_COC_DATA_RECEIVED;
+    event.receive.chan = chan;
+    event.receive.sdu_rx = om;
+
+    chan->cb(&event, chan->cb_arg);
+}
+
 static int
 ble_l2cap_coc_rx_fn(struct ble_l2cap_chan *chan)
 {
+    int rc;
+    struct os_mbuf **om;
+    struct ble_l2cap_coc_endpoint *rx;
+    uint16_t om_total;
+
+    /* Create a shortcut to rx_buf */
+    om = &chan->rx_buf;
+    BLE_HS_DBG_ASSERT(*om != NULL);
+
+    /* Create a shortcut to rx endpoint */
+    rx = &chan->coc_rx;
+
+    om_total = OS_MBUF_PKTLEN(*om);
+    rc = ble_hs_mbuf_pullup_base(om, om_total);
+    if (rc != 0) {
+        return rc;
+    }
+
+    /* Fist LE frame */
+    if (OS_MBUF_PKTLEN(rx->sdu) == 0) {
+        uint16_t sdu_len;
+
+        sdu_len = get_le16((*om)->om_data);
+        if (sdu_len > rx->mtu) {
+            /* TODO Disconnect?*/
+            BLE_HS_LOG(INFO, "error: sdu_len > rx->mtu (%d>%d)\n",
+                       sdu_len, rx->mtu);
+            return BLE_HS_EBADDATA;
+        }
+
+        BLE_HS_LOG(DEBUG, "sdu_len=%d, received LE frame=%d, credits=%d\n",
+                   sdu_len, om_total, rx->credits);
+
+        os_mbuf_adj(*om , BLE_L2CAP_SDU_SIZE);
+
+        rc = os_mbuf_appendfrom(rx->sdu, *om, 0, om_total - BLE_L2CAP_SDU_SIZE);
+        if (rc != 0) {
+            /* FIXME: User shall give us big enough buffer.
+             * need to handle it better
+             */
+            BLE_HS_LOG(INFO, "Could not append data rc=%d\n", rc);
+            assert(0);
+        }
+
+        /* In RX case data_offset keeps incoming SDU len */
+        rx->data_offset = sdu_len;
+
+    } else {
+        BLE_HS_LOG(DEBUG, "Continuation...received %d\n", (*om)->om_len);
+
+        rc  = os_mbuf_appendfrom(rx->sdu, *om, 0, om_total);
+        if (rc != 0) {
+            /* FIXME: need to handle it better */
+            BLE_HS_LOG(DEBUG, "Could not append data rc=%d\n", rc);
+            assert(0);
+        }
+    }
+
+    rx->credits--;
+
+    if (OS_MBUF_PKTLEN(rx->sdu) == rx->data_offset) {
+        struct os_mbuf *sdu_rx = rx->sdu;
+
+        /* Lets get back control to os_mbuf to application.
+         * Since it this callback application might want to set new sdu
+         * we need to prepare space for this. Therefore we need sdu_rx
+         */
+
+        rx->sdu = NULL;
+        rx->data_offset = 0;
+
+        ble_l2cap_event_coc_received_data(chan, sdu_rx);
+
+        goto done;
+    }
+
+    /* If we did not received full SDU and credits are 0 it means
+     * that remote was sending us not fully filled up LE frames.
+     * However, we still have buffer to for next LE Frame so lets give one more
+     * credit to peer so it can send us full SDU
+     */
+    if (rx->credits == 0 && rx->sdu) {
+        /* Remote did not send full SDU. Lets give him one more credits to do
+         * so since we have still buffer to handle it
+         */
+        rx->credits = 1;
+        ble_l2cap_sig_le_credits(chan, rx->credits);
+    }
+
+done:
+    BLE_HS_LOG(DEBUG, "Received sdu_len=%d, credits left=%d\n",
+               OS_MBUF_PKTLEN(rx->sdu), rx->credits);
+
     return 0;
 }
 
@@ -200,6 +308,34 @@ ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
     ble_hs_unlock();
 }
 
+void
+ble_l2cap_coc_recv_ready(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_rx)
+{
+    struct ble_hs_conn *conn;
+    struct ble_l2cap_chan *c;
+
+    chan->coc_rx.sdu = sdu_rx;
+
+    ble_hs_lock();
+    conn = ble_hs_conn_find_assert(chan->conn_handle);
+    c = ble_hs_conn_chan_find_by_scid(conn, chan->scid);
+    if (!c) {
+        ble_hs_unlock();
+        return;
+    }
+
+    /* FIXME 10 is hardcoded - make it better.
+     * We want to back only that much credits which remote side is missing
+     * to be able to send complete SDU.
+     */
+    if (chan->coc_rx.credits < 10) {
+        ble_l2cap_sig_le_credits(chan, 10 - chan->coc_rx.credits);
+        chan->coc_rx.credits = 10;
+    }
+
+    ble_hs_unlock();
+}
+
 int
 ble_l2cap_coc_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/98e2cb9d/net/nimble/host/src/ble_l2cap_coc_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc_priv.h b/net/nimble/host/src/ble_l2cap_coc_priv.h
index 21f9ec2..88380ea 100644
--- a/net/nimble/host/src/ble_l2cap_coc_priv.h
+++ b/net/nimble/host/src/ble_l2cap_coc_priv.h
@@ -39,6 +39,7 @@ struct ble_l2cap_chan;
 struct ble_l2cap_coc_endpoint {
     uint16_t mtu;
     uint16_t credits;
+    uint16_t data_offset;
     struct os_mbuf *sdu;
 };
 
@@ -63,9 +64,12 @@ struct ble_l2cap_chan * ble_l2cap_coc_chan_alloc(uint16_t conn_handle,
                                                  void *cb_arg);
 void ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
                                     uint16_t credits);
+void ble_l2cap_coc_recv_ready(struct ble_l2cap_chan *chan,
+                              struct os_mbuf *sdu_rx);
 #else
 #define ble_l2cap_coc_init()                                    0
 #define ble_l2cap_coc_create_server(psm, mtu, cb, cb_arg)       BLE_HS_ENOTSUP
+#define ble_l2cap_coc_recv_ready(chan, sdu_rx)
 #endif
 
 #ifdef __cplusplus


[24/26] incubator-mynewt-core git commit: nimble/l2cap: Fix hanlding broken ACL during L2CAP procedure

Posted by cc...@apache.org.
nimble/l2cap: Fix hanlding broken ACL during L2CAP procedure

With this patch we make sure that all processing procedures are
free and user is notified about broken link


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

Branch: refs/heads/develop
Commit: 38ae570452466f55cd15f7b03b78b25babcd1c66
Parents: 34e6486
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Mar 1 09:26:17 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/38ae5704/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 444a8f6..439acfe 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -1200,17 +1200,26 @@ ble_l2cap_sig_conn_broken(uint16_t conn_handle, int reason)
 {
     struct ble_l2cap_sig_proc *proc;
 
-    /* If there was a connection update in progress, indicate to the
-     * application that it did not complete.
-     */
-
-    proc = ble_l2cap_sig_proc_extract(conn_handle,
-                                      BLE_L2CAP_SIG_PROC_OP_UPDATE, 0);
+    /* Report a failure for each timed out procedure. */
+    while ((proc = STAILQ_FIRST(&ble_l2cap_sig_procs)) != NULL) {
+        switch(proc->op) {
+            case BLE_L2CAP_SIG_PROC_OP_UPDATE:
+                ble_l2cap_sig_update_call_cb(proc, reason);
+                break;
+#if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
+            case BLE_L2CAP_SIG_PROC_OP_CONNECT:
+                ble_l2cap_sig_coc_connect_cb(proc, reason);
+            break;
+            case BLE_L2CAP_SIG_PROC_OP_DISCONNECT:
+                ble_l2cap_sig_coc_disconnect_cb(proc, reason);
+            break;
+#endif
+            }
 
-    if (proc != NULL) {
-        ble_l2cap_sig_update_call_cb(proc, reason);
-        ble_l2cap_sig_proc_free(proc);
+            STAILQ_REMOVE_HEAD(&ble_l2cap_sig_procs, next);
+            ble_l2cap_sig_proc_free(proc);
     }
+
 }
 
 /**


[21/26] incubator-mynewt-core git commit: nimble/l2cap: Fix handling bad data in L2CAP update parameters response

Posted by cc...@apache.org.
nimble/l2cap: Fix handling bad data in L2CAP update parameters response

Returning error will cause sending L2CAP REJECT on that response and
this is incorrect. This patch fixes that.


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

Branch: refs/heads/develop
Commit: a4430354b14e932312210adcf6b2830070abb9cd
Parents: eb576bc
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Thu Feb 2 11:32:25 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a4430354/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index d6219d3..560311d 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -438,7 +438,7 @@ ble_l2cap_sig_update_rsp_rx(uint16_t conn_handle,
 
     default:
         cb_status = BLE_HS_EBADDATA;
-        rc = BLE_HS_EBADDATA;
+        rc = 0;
         break;
     }
 


[02/26] incubator-mynewt-core git commit: bletiny: Refactor buffer handling in the application

Posted by cc...@apache.org.
bletiny: Refactor buffer handling in the application

With this patch we prepare os_mbuf_pool which is used later by application
to get sdu buffer


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

Branch: refs/heads/develop
Commit: 6a0f5a8f002eed9e66791b8c4a9b96c3945f81db
Parents: b552c16
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Feb 22 17:06:53 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 10:08:17 2017 +0100

----------------------------------------------------------------------
 apps/bletiny/src/main.c | 32 ++++++++++++++++++++++----------
 1 file changed, 22 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6a0f5a8f/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index c468666..52cf7e6 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -73,6 +73,8 @@
 
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM)
 #define BLETINY_COC_MTU               (256)
+/* We use same pool for incoming and outgoing sdu */
+#define BLETINY_COC_BUF_COUNT         (3 * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM))
 #endif
 
 struct log bletiny_log;
@@ -94,7 +96,8 @@ static void *bletiny_coc_conn_mem;
 static struct os_mempool bletiny_coc_conn_pool;
 
 static void *bletiny_sdu_coc_mem;
-static struct os_mempool bletiny_sdu_coc_pool;
+struct os_mbuf_pool sdu_os_mbuf_pool;
+static struct os_mempool sdu_coc_mbuf_mempool;
 #endif
 
 static struct os_callout bletiny_tx_timer;
@@ -1624,7 +1627,14 @@ bletiny_l2cap_coc_remove(uint16_t conn_handle, struct ble_l2cap_chan *chan)
 static void
 bletiny_l2cap_coc_recv(struct ble_l2cap_chan *chan, struct os_mbuf *sdu)
 {
-    console_printf("LE CoC SDU received, chan: 0x%08lx\n", (uint32_t) chan);
+    console_printf("LE CoC SDU received, chan: 0x%08lx, data len %d\n",
+                   (uint32_t) chan, OS_MBUF_PKTLEN(sdu));
+
+    os_mbuf_free_chain(sdu);
+    sdu = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
+    assert(sdu != NULL);
+
+    ble_l2cap_recv_ready(chan, sdu);
 }
 
 static int
@@ -1633,9 +1643,9 @@ bletiny_l2cap_coc_accept(uint16_t conn_handle, uint16_t peer_mtu,
 {
     struct os_mbuf *sdu_rx;
 
-    sdu_rx = os_memblock_get(&bletiny_sdu_coc_pool);
+    sdu_rx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
     if (!sdu_rx) {
-            return BLE_HS_ENOMEM;
+        return BLE_HS_ENOMEM;
     }
 
     ble_l2cap_recv_ready(chan, sdu_rx);
@@ -1707,7 +1717,7 @@ bletiny_l2cap_connect(uint16_t conn_handle, uint16_t psm)
 
     struct os_mbuf *sdu_rx;
 
-    sdu_rx = os_memblock_get(&bletiny_sdu_coc_pool);
+    sdu_rx = os_mbuf_get_pkthdr(&sdu_os_mbuf_pool, 0);
     assert(sdu_rx != NULL);
 
     return ble_l2cap_connect(conn_handle, psm, BLETINY_COC_MTU, sdu_rx,
@@ -1793,16 +1803,18 @@ main(void)
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
     /* For testing we want to support all the available channels */
     bletiny_sdu_coc_mem = malloc(
-        OS_MEMPOOL_BYTES(BLETINY_COC_MTU * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM),
-                         sizeof (struct os_mbuf)));
+        OS_MEMPOOL_BYTES(BLETINY_COC_BUF_COUNT, BLETINY_COC_MTU));
     assert(bletiny_sdu_coc_mem != NULL);
 
-    rc = os_mempool_init(&bletiny_sdu_coc_pool,
-                         BLETINY_COC_MTU * MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM),
-                         sizeof (struct os_mbuf), bletiny_sdu_coc_mem,
+    rc = os_mempool_init(&sdu_coc_mbuf_mempool, BLETINY_COC_BUF_COUNT,
+                         BLETINY_COC_MTU, bletiny_sdu_coc_mem,
                          "bletiny_coc_sdu_pool");
     assert(rc == 0);
 
+    rc = os_mbuf_pool_init(&sdu_os_mbuf_pool, &sdu_coc_mbuf_mempool,
+                           BLETINY_COC_MTU, BLETINY_COC_BUF_COUNT);
+    assert(rc == 0);
+
     bletiny_coc_conn_mem = malloc(
         OS_MEMPOOL_BYTES(MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM),
                          sizeof (struct bletiny_l2cap_coc)));


[20/26] incubator-mynewt-core git commit: nimble/l2cap: Add suppport to send data over L2CAP LE CoC

Posted by cc...@apache.org.
nimble/l2cap: Add suppport to send data over L2CAP LE CoC


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

Branch: refs/heads/develop
Commit: 93ac0dfaf68e8b5614413b03b0ca787dc9f6142b
Parents: fbceba5
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 15:26:25 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap.c          |   3 +-
 net/nimble/host/src/ble_l2cap_coc.c      | 124 ++++++++++++++++++++++++++
 net/nimble/host/src/ble_l2cap_coc_priv.h |   2 +
 3 files changed, 127 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93ac0dfa/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 046a136..b19a348 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -160,8 +160,7 @@ int ble_l2cap_disconnect(struct ble_l2cap_chan *chan)
 int
 ble_l2cap_send(struct ble_l2cap_chan *chan, struct os_mbuf *sdu)
 {
-    /*TODO Implement */
-    return BLE_HS_ENOTSUP;
+    return ble_l2cap_coc_send(chan, sdu);
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93ac0dfa/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index 9794a00..12d79d6 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -288,6 +288,108 @@ ble_l2cap_coc_cleanup_chan(struct ble_l2cap_chan *chan)
     os_mbuf_free_chain(chan->coc_tx.sdu);
 }
 
+static int
+ble_l2cap_coc_continue_tx(struct ble_l2cap_chan *chan)
+{
+    struct ble_l2cap_coc_endpoint *tx;
+    uint16_t len;
+    uint16_t left_to_send;
+    struct os_mbuf *txom;
+    struct ble_hs_conn *conn;
+    uint16_t sdu_size_offset;
+    int rc;
+
+    /* If there is no data to send, just return success */
+    tx = &chan->coc_tx;
+    if (!tx->sdu) {
+        return 0;
+    }
+
+    while (tx->credits) {
+        sdu_size_offset = 0;
+
+        BLE_HS_LOG(DEBUG, "Available credits %d\n", tx->credits);
+
+        /* lets calculate data we are going to send */
+        left_to_send = OS_MBUF_PKTLEN(tx->sdu) - tx->data_offset;
+
+        if (tx->data_offset == 0) {
+            sdu_size_offset = BLE_L2CAP_SDU_SIZE;
+            left_to_send += sdu_size_offset;
+        }
+
+        /* Take into account peer MTU */
+        len = min(left_to_send, chan->peer_mtu);
+
+        /* Prepare packet */
+        txom = ble_hs_mbuf_l2cap_pkt();
+        if (!txom) {
+            BLE_HS_LOG(DEBUG, "Could not prepare l2cap packet len %d, err=%d",
+                                                                   len, rc);
+
+            rc = BLE_HS_ENOMEM;
+            goto failed;
+        }
+
+        if (tx->data_offset == 0) {
+            /* First packet needs SDU len first. Left to send */
+            uint16_t l = htole16(OS_MBUF_PKTLEN(tx->sdu));
+
+            BLE_HS_LOG(DEBUG, "Sending SDU len=%d\n", OS_MBUF_PKTLEN(tx->sdu));
+            rc = os_mbuf_append(txom, &l, sizeof(uint16_t));
+            if (rc) {
+                BLE_HS_LOG(DEBUG, "Could not append data rc=%d", rc);
+                goto failed;
+            }
+        }
+
+        /* In data_offset we keep track on what we already sent. Need to remember
+         * that for first packet we need to decrease data size by 2 bytes for sdu
+         * size
+         */
+        rc = os_mbuf_appendfrom(txom, tx->sdu, tx->data_offset,
+                                len - sdu_size_offset);
+        if (rc) {
+            BLE_HS_LOG(DEBUG, "Could not append data rc=%d", rc);
+           goto failed;
+        }
+
+        ble_hs_lock();
+        conn = ble_hs_conn_find_assert(chan->conn_handle);
+        rc = ble_l2cap_tx(conn, chan, txom);
+        ble_hs_unlock();
+
+        if (rc) {
+          /* txom is consumed by l2cap */
+          txom = NULL;
+          goto failed;
+        } else {
+            tx->credits --;
+            tx->data_offset += len - sdu_size_offset;
+        }
+
+        BLE_HS_LOG(DEBUG, "Sent %d bytes, credits=%d, to send %d bytes \n",
+                  len, tx->credits, OS_MBUF_PKTLEN(tx->sdu)- tx->data_offset );
+
+        if (tx->data_offset == OS_MBUF_PKTLEN(tx->sdu)) {
+                BLE_HS_LOG(DEBUG, "Complete package sent");
+                os_mbuf_free_chain(tx->sdu);
+                tx->sdu = 0;
+                tx->data_offset = 0;
+                break;
+        }
+    }
+
+    return 0;
+
+failed:
+    os_mbuf_free_chain(tx->sdu);
+    tx->sdu = NULL;
+    os_mbuf_free_chain(txom);
+
+    return rc;
+}
+
 void
 ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
                                 uint16_t credits)
@@ -317,6 +419,8 @@ ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
     }
 
     chan->coc_tx.credits += credits;
+    ble_l2cap_coc_continue_tx(chan);
+
     ble_hs_unlock();
 }
 
@@ -349,6 +453,26 @@ ble_l2cap_coc_recv_ready(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_rx)
 }
 
 int
+ble_l2cap_coc_send(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_tx)
+{
+    struct ble_l2cap_coc_endpoint *tx;
+
+    tx = &chan->coc_tx;
+
+    if (tx->sdu) {
+        return BLE_HS_EBUSY;
+    }
+
+    tx->sdu = sdu_tx;
+
+    if (OS_MBUF_PKTLEN(sdu_tx) > tx->mtu) {
+        return BLE_HS_EBADDATA;
+    }
+
+    return ble_l2cap_coc_continue_tx(chan);
+}
+
+int
 ble_l2cap_coc_init(void)
 {
     STAILQ_INIT(&ble_l2cap_coc_srvs);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/93ac0dfa/net/nimble/host/src/ble_l2cap_coc_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc_priv.h b/net/nimble/host/src/ble_l2cap_coc_priv.h
index 36ab4dc..4fe75e9 100644
--- a/net/nimble/host/src/ble_l2cap_coc_priv.h
+++ b/net/nimble/host/src/ble_l2cap_coc_priv.h
@@ -67,11 +67,13 @@ void ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
                                     uint16_t credits);
 void ble_l2cap_coc_recv_ready(struct ble_l2cap_chan *chan,
                               struct os_mbuf *sdu_rx);
+int ble_l2cap_coc_send(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_tx);
 #else
 #define ble_l2cap_coc_init()                                    0
 #define ble_l2cap_coc_create_server(psm, mtu, cb, cb_arg)       BLE_HS_ENOTSUP
 #define ble_l2cap_coc_recv_ready(chan, sdu_rx)
 #define ble_l2cap_coc_cleanup_chan(chan)
+#define ble_l2cap_coc_send(chan, sdu_tx)                        BLE_HS_ENOTSUP
 #endif
 
 #ifdef __cplusplus


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

Posted by cc...@apache.org.
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: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 15:07:58 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@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
  *


[19/26] incubator-mynewt-core git commit: nimble/l2cap: Improve L2CAP LE CoC connection handling

Posted by cc...@apache.org.
nimble/l2cap: Improve L2CAP LE CoC connection handling

With this patch we make sure remote does not use already used CID


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

Branch: refs/heads/develop
Commit: 6963daf878259509b367e9a538302429bb5da0da
Parents: 38ae570
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Mar 1 11:42:43 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6963daf8/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 439acfe..1840b0a 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -658,14 +658,19 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
     ble_hs_lock();
     conn = ble_hs_conn_find_assert(conn_handle);
 
-    /* Verify CID */
+    /* Verify CID. Note, scid in the request is dcid for out local channel */
     scid = le16toh(req->scid);
     if (scid < BLE_L2CAP_COC_CID_START || scid > BLE_L2CAP_COC_CID_END) {
-        /*FIXME: Check if SCID is not already used */
         rsp->result = htole16(BLE_L2CAP_COC_ERR_INVALID_SOURCE_CID);
         goto failed;
     }
 
+    chan = ble_hs_conn_chan_find_by_dcid(conn, scid);
+    if (chan) {
+        rsp->result = htole16(BLE_L2CAP_COC_ERR_SOURCE_CID_ALREADY_USED);
+        goto failed;
+    }
+
     rc = ble_l2cap_coc_create_srv_chan(conn_handle, le16toh(req->psm), &chan);
     if (rc != 0) {
         uint16_t coc_err = ble_l2cap_sig_ble_hs_err2coc_err(rc);


[05/26] incubator-mynewt-core git commit: nimble/l2cap: Memset response in L2CAP LE CoC connect request

Posted by cc...@apache.org.
nimble/l2cap: Memset response in L2CAP LE CoC connect request

This is in order to not send trash in case of error response.


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

Branch: refs/heads/develop
Commit: ced3e8b2469a914562abbbb3c8711629fd69f846
Parents: 509cd5f
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Feb 22 17:15:35 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:41 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig.c | 2 ++
 1 file changed, 2 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ced3e8b2/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 94bb271..a1a3399 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -631,6 +631,8 @@ ble_l2cap_sig_coc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
         return 0;
     }
 
+    memset(rsp, 0, sizeof(*rsp));
+
     req = (struct ble_l2cap_sig_le_con_req *)(*om)->om_data;
 
     ble_hs_lock();


[26/26] incubator-mynewt-core git commit: This closes #190.

Posted by cc...@apache.org.
This closes #190.

Merge remote-tracking branch 'rymanluk/l2cap_coc' into develop

* rymanluk/l2cap_coc: (25 commits)
  nimble/l2cap: Add initial credits calculations
  nimble/l2cap: Improve L2CAP LE CoC connection handling
  nimble/l2cap: Fix hanlding broken ACL during L2CAP procedure
  nimble/l2cap: Fix for possible memory leak
  nimble/l2cap: Handle REJECT CMD on L2CAP LE CoC connection create req
  nibmle/l2cap: Clear LE CoC channel on ACL drop
  nimble/l2cap: Add suppport to send data over L2CAP LE CoC
  nimble/l2cap: Add helper to clean L2CAP LE CoC channel
  nimble/l2cap: Add handling receiving SDU over L2CAP LE CoC
  nimble/l2cap: Add LE credits update handling
  nimble/l2cap: Remove not needed **om from ble_l2cap_rx_fn
  nimble/l2cap: Make ble_l2cap_chan available in ble_l2cap_rx_fn
  nimble/l2cap: Remove not needed function ble_l2cap_sig_init_cmd
  nimble/l2cap: Refactor handling L2CAP reject command
  nimble/l2cap: Fix handling bad data in L2CAP update parameters response
  nimble/l2cap: Add helper to create L2CAP channel for LE CoC
  nimble/l2cap: Refactor update parameters handling
  nimble/l2cap: Fix L2CAP LE CoC disconnection handling
  nimble/l2cap: Memset response in L2CAP LE CoC connect request
  nimble/l2cap: Fix handling scid/dcid in the channel.
  ...


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

Branch: refs/heads/develop
Commit: b2ea1dc2caeb0706bad7d30e83488f48b56e1a3f
Parents: e324152 750707b
Author: Christopher Collins <cc...@apache.org>
Authored: Fri Mar 3 09:18:06 2017 -0800
Committer: Christopher Collins <cc...@apache.org>
Committed: Fri Mar 3 09:18:06 2017 -0800

----------------------------------------------------------------------
 apps/bletiny/src/bletiny.h                  |   1 +
 apps/bletiny/src/cmd.c                      |  54 +++-
 apps/bletiny/src/main.c                     | 118 ++++++-
 net/nimble/host/include/host/ble_l2cap.h    |   1 +
 net/nimble/host/src/ble_att.c               |  19 +-
 net/nimble/host/src/ble_att_priv.h          |   2 +-
 net/nimble/host/src/ble_gap.c               |   3 +-
 net/nimble/host/src/ble_hs_conn.c           |  32 +-
 net/nimble/host/src/ble_hs_conn_priv.h      |   6 +-
 net/nimble/host/src/ble_hs_hci_evt.c        |   8 +-
 net/nimble/host/src/ble_hs_misc.c           |   2 +-
 net/nimble/host/src/ble_l2cap.c             |  33 +-
 net/nimble/host/src/ble_l2cap_coc.c         | 383 ++++++++++++++++++++++-
 net/nimble/host/src/ble_l2cap_coc_priv.h    |  21 +-
 net/nimble/host/src/ble_l2cap_priv.h        |  11 +-
 net/nimble/host/src/ble_l2cap_sig.c         | 269 ++++++++++------
 net/nimble/host/src/ble_l2cap_sig_cmd.c     | 168 +---------
 net/nimble/host/src/ble_l2cap_sig_priv.h    |  26 +-
 net/nimble/host/src/ble_sm.c                |  31 +-
 net/nimble/host/src/ble_sm_priv.h           |   2 +-
 net/nimble/host/test/src/ble_hs_conn_test.c |   6 +-
 net/nimble/host/test/src/ble_hs_test_util.c |  50 ++-
 net/nimble/host/test/src/ble_l2cap_test.c   |  22 +-
 23 files changed, 912 insertions(+), 356 deletions(-)
----------------------------------------------------------------------



[03/26] incubator-mynewt-core git commit: nimble/l2cap: Fix L2CAP LE CoC disconnection handling

Posted by cc...@apache.org.
nimble/l2cap: Fix L2CAP LE CoC disconnection handling

This patch fixes mess around scid/dcid on L2CAP disconnection
request


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

Branch: refs/heads/develop
Commit: 506e3738f7cd4155b5126fa7427536b81613536f
Parents: ced3e8b
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Wed Feb 22 17:37:39 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:41 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/506e3738/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index a1a3399..9292137 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -877,15 +877,21 @@ ble_l2cap_sig_disc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
 
     req = (struct ble_l2cap_sig_disc_req *) (*om)->om_data;
 
+    /* Let's find matching channel. Note that destination CID in the request
+     * is from peer perspective. It is source CID from nimble perspective 
+     */
     chan = ble_hs_conn_chan_find(conn, le16toh(req->dcid));
-    if (!chan || (le16toh(req->scid) != chan->scid)) {
+    if (!chan || (le16toh(req->scid) != chan->dcid)) {
         os_mbuf_free_chain(txom);
         ble_hs_unlock();
         return 0;
     }
 
-    rsp->dcid = htole16(chan->dcid);
-    rsp->scid = htole16(chan->scid);
+    /* Note that in the response destination CID is form peer perspective and
+     * it is source CID from nimble perspective.
+     */
+    rsp->dcid = htole16(chan->scid);
+    rsp->scid = htole16(chan->dcid);
 
     ble_l2cap_event_coc_disconnected(chan);
 


[22/26] incubator-mynewt-core git commit: nimble/l2cap: Add LE credits update handling

Posted by cc...@apache.org.
nimble/l2cap: Add LE credits update handling

This patch add support to send singaling request to update LE creadits
as well as handle remote device sending us LE credits update.

Note, that this patch introduces also helper function to find channel
by scid and dcid


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

Branch: refs/heads/develop
Commit: 754c4456a022f89135bedc80807c755950d2d201
Parents: 9b3899a
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Sun Feb 12 15:30:21 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_att.c               |  2 +-
 net/nimble/host/src/ble_hs_conn.c           | 23 ++++++++++-
 net/nimble/host/src/ble_hs_conn_priv.h      |  4 +-
 net/nimble/host/src/ble_hs_misc.c           |  2 +-
 net/nimble/host/src/ble_l2cap.c             |  2 +-
 net/nimble/host/src/ble_l2cap_coc.c         | 32 +++++++++++++++
 net/nimble/host/src/ble_l2cap_coc_priv.h    |  2 +
 net/nimble/host/src/ble_l2cap_sig.c         | 50 +++++++++++++++++++++++-
 net/nimble/host/src/ble_l2cap_sig_priv.h    |  6 +++
 net/nimble/host/test/src/ble_hs_conn_test.c |  6 +--
 10 files changed, 119 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/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
index 7fe095b..0eaed34 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -545,7 +545,7 @@ ble_att_set_preferred_mtu(uint16_t mtu)
 
     i = 0;
     while ((conn = ble_hs_conn_find_by_idx(i)) != NULL) {
-        chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+        chan = ble_hs_conn_chan_find_by_scid(conn, BLE_L2CAP_CID_ATT);
         BLE_HS_DBG_ASSERT(chan != NULL);
 
         if (!(chan->flags & BLE_L2CAP_CHAN_F_TXED_MTU)) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index a371ace..cef0e3a 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -50,7 +50,7 @@ ble_hs_conn_can_alloc(void)
 }
 
 struct ble_l2cap_chan *
-ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
+ble_hs_conn_chan_find_by_scid(struct ble_hs_conn *conn, uint16_t cid)
 {
 #if !NIMBLE_BLE_CONNECT
     return NULL;
@@ -70,6 +70,27 @@ ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
     return NULL;
 }
 
+struct ble_l2cap_chan *
+ble_hs_conn_chan_find_by_dcid(struct ble_hs_conn *conn, uint16_t cid)
+{
+#if !NIMBLE_BLE_CONNECT
+    return NULL;
+#endif
+
+    struct ble_l2cap_chan *chan;
+
+    SLIST_FOREACH(chan, &conn->bhc_channels, next) {
+        if (chan->dcid == cid) {
+            return chan;
+        }
+        if (chan->dcid > cid) {
+            return NULL;
+        }
+    }
+
+    return NULL;
+}
+
 int
 ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_hs_conn_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn_priv.h b/net/nimble/host/src/ble_hs_conn_priv.h
index 24e2e48..4c51646 100644
--- a/net/nimble/host/src/ble_hs_conn_priv.h
+++ b/net/nimble/host/src/ble_hs_conn_priv.h
@@ -84,7 +84,9 @@ struct ble_hs_conn *ble_hs_conn_find_by_addr(const ble_addr_t *addr);
 struct ble_hs_conn *ble_hs_conn_find_by_idx(int idx);
 int ble_hs_conn_exists(uint16_t conn_handle);
 struct ble_hs_conn *ble_hs_conn_first(void);
-struct ble_l2cap_chan *ble_hs_conn_chan_find(struct ble_hs_conn *conn,
+struct ble_l2cap_chan *ble_hs_conn_chan_find_by_scid(struct ble_hs_conn *conn,
+                                             uint16_t cid);
+struct ble_l2cap_chan *ble_hs_conn_chan_find_by_dcid(struct ble_hs_conn *conn,
                                              uint16_t cid);
 int ble_hs_conn_chan_insert(struct ble_hs_conn *conn,
                             struct ble_l2cap_chan *chan);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_hs_misc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_misc.c b/net/nimble/host/src/ble_hs_misc.c
index 578c7e4..f06bfae 100644
--- a/net/nimble/host/src/ble_hs_misc.c
+++ b/net/nimble/host/src/ble_hs_misc.c
@@ -39,7 +39,7 @@ ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
         chan = NULL;
         rc = BLE_HS_ENOTCONN;
     } else {
-        chan = ble_hs_conn_chan_find(conn, cid);
+        chan = ble_hs_conn_chan_find_by_scid(conn, cid);
         if (chan == NULL) {
             rc = BLE_HS_ENOTCONN;
         } else {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 306eac1..e048ce9 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -320,7 +320,7 @@ ble_l2cap_rx(struct ble_hs_conn *conn,
         /* Strip L2CAP header from the front of the mbuf. */
         os_mbuf_adj(om, BLE_L2CAP_HDR_SZ);
 
-        chan = ble_hs_conn_chan_find(conn, l2cap_hdr.cid);
+        chan = ble_hs_conn_chan_find_by_scid(conn, l2cap_hdr.cid);
         if (chan == NULL) {
             rc = BLE_HS_ENOENT;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index ac1b6fa..8d5f71f 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -168,6 +168,38 @@ ble_l2cap_coc_create_srv_chan(uint16_t conn_handle, uint16_t psm,
     return 0;
 }
 
+void
+ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
+                                uint16_t credits)
+{
+    struct ble_hs_conn *conn;
+    struct ble_l2cap_chan *chan;
+
+    /* remote updated its credits */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(conn_handle);
+    if (!conn) {
+        ble_hs_unlock();
+        return;
+    }
+
+    chan = ble_hs_conn_chan_find_by_dcid(conn, dcid);
+    if (!chan) {
+        ble_hs_unlock();
+        return;
+    }
+
+    if (chan->coc_tx.credits + credits > 0xFFFF) {
+        BLE_HS_LOG(INFO, "LE CoC credits overflow...disconnecting\n");
+        ble_l2cap_sig_disconnect(chan);
+        ble_hs_unlock();
+        return;
+    }
+
+    chan->coc_tx.credits += credits;
+    ble_hs_unlock();
+}
+
 int
 ble_l2cap_coc_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_l2cap_coc_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc_priv.h b/net/nimble/host/src/ble_l2cap_coc_priv.h
index 63c593e..21f9ec2 100644
--- a/net/nimble/host/src/ble_l2cap_coc_priv.h
+++ b/net/nimble/host/src/ble_l2cap_coc_priv.h
@@ -61,6 +61,8 @@ struct ble_l2cap_chan * ble_l2cap_coc_chan_alloc(uint16_t conn_handle,
                                                  struct os_mbuf *sdu_rx,
                                                  ble_l2cap_event_fn *cb,
                                                  void *cb_arg);
+void ble_l2cap_coc_le_credits_update(uint16_t conn_handle, uint16_t dcid,
+                                    uint16_t credits);
 #else
 #define ble_l2cap_coc_init()                                    0
 #define ble_l2cap_coc_create_server(psm, mtu, cb, cb_arg)       BLE_HS_ENOTSUP

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index abc00ae..8da2dad 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -98,11 +98,13 @@ static ble_l2cap_sig_rx_fn ble_l2cap_sig_coc_req_rx;
 static ble_l2cap_sig_rx_fn ble_l2cap_sig_coc_rsp_rx;
 static ble_l2cap_sig_rx_fn ble_l2cap_sig_disc_rsp_rx;
 static ble_l2cap_sig_rx_fn ble_l2cap_sig_disc_req_rx;
+static ble_l2cap_sig_rx_fn ble_l2cap_sig_le_credits_rx;
 #else
 #define ble_l2cap_sig_coc_req_rx    ble_l2cap_sig_rx_noop
 #define ble_l2cap_sig_coc_rsp_rx    ble_l2cap_sig_rx_noop
 #define ble_l2cap_sig_disc_rsp_rx   ble_l2cap_sig_rx_noop
 #define ble_l2cap_sig_disc_req_rx   ble_l2cap_sig_rx_noop
+#define ble_l2cap_sig_le_credits_rx   ble_l2cap_sig_rx_noop
 #endif
 
 static ble_l2cap_sig_rx_fn * const ble_l2cap_sig_dispatch[] = {
@@ -120,7 +122,7 @@ static ble_l2cap_sig_rx_fn * const ble_l2cap_sig_dispatch[] = {
     [BLE_L2CAP_SIG_OP_UPDATE_RSP]           = ble_l2cap_sig_update_rsp_rx,
     [BLE_L2CAP_SIG_OP_CREDIT_CONNECT_REQ]   = ble_l2cap_sig_coc_req_rx,
     [BLE_L2CAP_SIG_OP_CREDIT_CONNECT_RSP]   = ble_l2cap_sig_coc_rsp_rx,
-    [BLE_L2CAP_SIG_OP_FLOW_CTRL_CREDIT]     = ble_l2cap_sig_rx_noop,
+    [BLE_L2CAP_SIG_OP_FLOW_CTRL_CREDIT]     = ble_l2cap_sig_le_credits_rx,
 };
 
 static uint8_t ble_l2cap_sig_cur_id;
@@ -876,7 +878,7 @@ ble_l2cap_sig_disc_req_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
     /* Let's find matching channel. Note that destination CID in the request
      * is from peer perspective. It is source CID from nimble perspective 
      */
-    chan = ble_hs_conn_chan_find(conn, le16toh(req->dcid));
+    chan = ble_hs_conn_chan_find_by_scid(conn, le16toh(req->dcid));
     if (!chan || (le16toh(req->scid) != chan->dcid)) {
         os_mbuf_free_chain(txom);
         ble_hs_unlock();
@@ -1006,6 +1008,50 @@ ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan)
 
     return rc;
 }
+
+static int
+ble_l2cap_sig_le_credits_rx(uint16_t conn_handle, struct ble_l2cap_sig_hdr *hdr,
+                            struct os_mbuf **om)
+{
+    struct ble_l2cap_sig_le_credits *req;
+    int rc;
+
+    rc = ble_hs_mbuf_pullup_base(om, sizeof(*req));
+    if (rc != 0) {
+        return 0;
+    }
+
+    req = (struct ble_l2cap_sig_le_credits *) (*om)->om_data;
+
+    /* Ignore when peer sends zero credits */
+    if (req->credits == 0) {
+            return 0;
+    }
+
+    ble_l2cap_coc_le_credits_update(conn_handle, le16toh(req->scid),
+                                    le16toh(req->credits));
+
+    return 0;
+}
+
+int
+ble_l2cap_sig_le_credits(struct ble_l2cap_chan *chan, uint16_t credits)
+{
+    struct ble_l2cap_sig_le_credits *cmd;
+    struct os_mbuf *txom;
+
+    cmd = ble_l2cap_sig_cmd_get(BLE_L2CAP_SIG_OP_FLOW_CTRL_CREDIT,
+                                ble_l2cap_sig_next_id(), sizeof(*cmd), &txom);
+
+    if (!cmd) {
+        return BLE_HS_ENOMEM;
+    }
+
+    cmd->scid = htole16(chan->scid);
+    cmd->credits = htole16(credits);
+
+    return ble_l2cap_sig_tx(chan->conn_handle, txom);
+}
 #endif
 /*****************************************************************************
  * $misc                                                                     *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/src/ble_l2cap_sig_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_priv.h b/net/nimble/host/src/ble_l2cap_sig_priv.h
index 0677405..49d096c 100644
--- a/net/nimble/host/src/ble_l2cap_sig_priv.h
+++ b/net/nimble/host/src/ble_l2cap_sig_priv.h
@@ -84,6 +84,11 @@ struct ble_l2cap_sig_disc_rsp {
     uint16_t scid;
 } __attribute__((packed));
 
+struct ble_l2cap_sig_le_credits {
+    uint16_t scid;
+    uint16_t credits;
+} __attribute__((packed));
+
 int ble_l2cap_sig_init_cmd(uint8_t op, uint8_t id, uint8_t payload_len,
                            struct os_mbuf **out_om, void **out_payload_buf);
 void ble_l2cap_sig_hdr_parse(void *payload, uint16_t len,
@@ -103,6 +108,7 @@ int ble_l2cap_sig_coc_connect(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
                               struct os_mbuf *sdu_rx,
                               ble_l2cap_event_fn *cb, void *cb_arg);
 int ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan);
+int ble_l2cap_sig_le_credits(struct ble_l2cap_chan *chan, uint16_t credits);
 #else
 #define ble_l2cap_sig_coc_connect(conn_handle, psm, mtu, sdu_rx, cb, cb_arg) \
                                                                 BLE_HS_ENOTSUP

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/754c4456/net/nimble/host/test/src/ble_hs_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_conn_test.c b/net/nimble/host/test/src/ble_hs_conn_test.c
index 8ae4971..87f9b46 100644
--- a/net/nimble/host/test/src/ble_hs_conn_test.c
+++ b/net/nimble/host/test/src/ble_hs_conn_test.c
@@ -77,7 +77,7 @@ TEST_CASE(ble_hs_conn_test_direct_connect_success)
     TEST_ASSERT(conn->bhc_handle == 2);
     TEST_ASSERT(memcmp(conn->bhc_peer_addr.val, addr.val, 6) == 0);
 
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    chan = ble_hs_conn_chan_find_by_scid(conn, BLE_L2CAP_CID_ATT);
     TEST_ASSERT_FATAL(chan != NULL);
     TEST_ASSERT(chan->my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
     TEST_ASSERT(chan->peer_mtu == 0);
@@ -131,7 +131,7 @@ TEST_CASE(ble_hs_conn_test_direct_connectable_success)
     TEST_ASSERT(conn->bhc_handle == 2);
     TEST_ASSERT(memcmp(conn->bhc_peer_addr.val, addr.val, 6) == 0);
 
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    chan = ble_hs_conn_chan_find_by_scid(conn, BLE_L2CAP_CID_ATT);
     TEST_ASSERT_FATAL(chan != NULL);
     TEST_ASSERT(chan->my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
     TEST_ASSERT(chan->peer_mtu == 0);
@@ -192,7 +192,7 @@ TEST_CASE(ble_hs_conn_test_undirect_connectable_success)
     TEST_ASSERT(conn->bhc_handle == 2);
     TEST_ASSERT(memcmp(conn->bhc_peer_addr.val, addr.val, 6) == 0);
 
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    chan = ble_hs_conn_chan_find_by_scid(conn, BLE_L2CAP_CID_ATT);
     TEST_ASSERT_FATAL(chan != NULL);
     TEST_ASSERT(chan->my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
     TEST_ASSERT(chan->peer_mtu == 0);


[15/26] incubator-mynewt-core git commit: nimble/l2cap: Make ble_l2cap_chan available in ble_l2cap_rx_fn

Posted by cc...@apache.org.
nimble/l2cap: Make ble_l2cap_chan available in ble_l2cap_rx_fn

Each L2CAP channel has its data handler function. Till now we had
function per CID. In order to handle COC we need to have possibility
to handle more channels in same data handler function. For this reason
we need to have pointer to ble_l2cap_chan to which data are comming.

This patch does it.


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

Branch: refs/heads/develop
Commit: 27bc9edf37d344ba67110ee80af148b1861392c6
Parents: d9f788b
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Thu Feb 2 14:44:47 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/include/host/ble_l2cap.h    |  1 +
 net/nimble/host/src/ble_att.c               | 12 +++++++++---
 net/nimble/host/src/ble_att_priv.h          |  2 +-
 net/nimble/host/src/ble_gap.c               |  3 +--
 net/nimble/host/src/ble_hs_conn.c           |  9 +++++----
 net/nimble/host/src/ble_hs_conn_priv.h      |  2 +-
 net/nimble/host/src/ble_hs_hci_evt.c        |  3 ++-
 net/nimble/host/src/ble_l2cap.c             | 16 +++++++++++++---
 net/nimble/host/src/ble_l2cap_coc.c         |  5 ++---
 net/nimble/host/src/ble_l2cap_priv.h        |  7 ++++---
 net/nimble/host/src/ble_l2cap_sig.c         |  7 ++++---
 net/nimble/host/src/ble_l2cap_sig_priv.h    |  2 +-
 net/nimble/host/src/ble_sm.c                | 20 ++++++++++++++++----
 net/nimble/host/src/ble_sm_priv.h           |  2 +-
 net/nimble/host/test/src/ble_hs_test_util.c |  3 ++-
 net/nimble/host/test/src/ble_l2cap_test.c   |  4 ++--
 16 files changed, 65 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/include/host/ble_l2cap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_l2cap.h b/net/nimble/host/include/host/ble_l2cap.h
index 604d080..b3b03b9 100644
--- a/net/nimble/host/include/host/ble_l2cap.h
+++ b/net/nimble/host/include/host/ble_l2cap.h
@@ -174,6 +174,7 @@ struct ble_l2cap_event {
 
 typedef int ble_l2cap_event_fn(struct ble_l2cap_event *event, void *arg);
 
+uint16_t ble_l2cap_get_conn_handle(struct ble_l2cap_chan *chan);
 int ble_l2cap_create_server(uint16_t psm, uint16_t mtu,
                             ble_l2cap_event_fn *cb, void *cb_arg);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/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
index 329fcec..7876e0d 100644
--- a/net/nimble/host/src/ble_att.c
+++ b/net/nimble/host/src/ble_att.c
@@ -461,12 +461,18 @@ ble_att_chan_mtu(const struct ble_l2cap_chan *chan)
 }
 
 static int
-ble_att_rx(uint16_t conn_handle, struct os_mbuf **om)
+ble_att_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
 {
     const struct ble_att_rx_dispatch_entry *entry;
     uint8_t op;
+    uint16_t conn_handle;
     int rc;
 
+    conn_handle = ble_l2cap_get_conn_handle(chan);
+    if (!conn_handle) {
+        return BLE_HS_ENOTCONN;
+    }
+
     rc = os_mbuf_copydata(*om, 0, 1, &op);
     if (rc != 0) {
         return BLE_HS_EMSGSIZE;
@@ -551,11 +557,11 @@ ble_att_set_preferred_mtu(uint16_t mtu)
 }
 
 struct ble_l2cap_chan *
-ble_att_create_chan(void)
+ble_att_create_chan(uint16_t conn_handle)
 {
     struct ble_l2cap_chan *chan;
 
-    chan = ble_l2cap_chan_alloc();
+    chan = ble_l2cap_chan_alloc(conn_handle);
     if (chan == NULL) {
         return NULL;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_att_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_priv.h b/net/nimble/host/src/ble_att_priv.h
index 22ed62e..af05684 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -159,7 +159,7 @@ SLIST_HEAD(ble_att_clt_entry_list, ble_att_clt_entry);
 
 /*** @gen */
 
-struct ble_l2cap_chan *ble_att_create_chan(void);
+struct ble_l2cap_chan *ble_att_create_chan(uint16_t conn_handle);
 void ble_att_conn_chan_find(uint16_t conn_handle, struct ble_hs_conn **out_conn,
                             struct ble_l2cap_chan **out_chan);
 void ble_att_inc_tx_stat(uint8_t att_op);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index 92c3d12..0a09d4c 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -1151,10 +1151,9 @@ ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt)
     }
 
     /* We verified that there is a free connection when the procedure began. */
-    conn = ble_hs_conn_alloc();
+    conn = ble_hs_conn_alloc(evt->connection_handle);
     BLE_HS_DBG_ASSERT(conn != NULL);
 
-    conn->bhc_handle = evt->connection_handle;
     conn->bhc_itvl = evt->conn_itvl;
     conn->bhc_latency = evt->conn_latency;
     conn->bhc_supervision_timeout = evt->supervision_timeout;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index 02676fa..a371ace 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -102,7 +102,7 @@ ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 }
 
 struct ble_hs_conn *
-ble_hs_conn_alloc(void)
+ble_hs_conn_alloc(uint16_t conn_handle)
 {
 #if !NIMBLE_BLE_CONNECT
     return NULL;
@@ -117,10 +117,11 @@ ble_hs_conn_alloc(void)
         goto err;
     }
     memset(conn, 0, sizeof *conn);
+    conn->bhc_handle = conn_handle;
 
     SLIST_INIT(&conn->bhc_channels);
 
-    chan = ble_att_create_chan();
+    chan = ble_att_create_chan(conn_handle);
     if (chan == NULL) {
         goto err;
     }
@@ -129,7 +130,7 @@ ble_hs_conn_alloc(void)
         goto err;
     }
 
-    chan = ble_l2cap_sig_create_chan();
+    chan = ble_l2cap_sig_create_chan(conn_handle);
     if (chan == NULL) {
         goto err;
     }
@@ -141,7 +142,7 @@ ble_hs_conn_alloc(void)
     /* Create the SM channel even if not configured. We need it to reject SM
      * messages.
      */
-    chan = ble_sm_create_chan();
+    chan = ble_sm_create_chan(conn_handle);
     if (chan == NULL) {
         goto err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_hs_conn_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn_priv.h b/net/nimble/host/src/ble_hs_conn_priv.h
index b58a0df..24e2e48 100644
--- a/net/nimble/host/src/ble_hs_conn_priv.h
+++ b/net/nimble/host/src/ble_hs_conn_priv.h
@@ -74,7 +74,7 @@ struct ble_hs_conn_addrs {
 };
 
 int ble_hs_conn_can_alloc(void);
-struct ble_hs_conn *ble_hs_conn_alloc(void);
+struct ble_hs_conn *ble_hs_conn_alloc(uint16_t conn_handle);
 void ble_hs_conn_free(struct ble_hs_conn *conn);
 void ble_hs_conn_insert(struct ble_hs_conn *conn);
 void ble_hs_conn_remove(struct ble_hs_conn *conn);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_hs_hci_evt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_evt.c b/net/nimble/host/src/ble_hs_hci_evt.c
index a2d7a52..7f868a6 100644
--- a/net/nimble/host/src/ble_hs_hci_evt.c
+++ b/net/nimble/host/src/ble_hs_hci_evt.c
@@ -659,7 +659,8 @@ ble_hs_hci_evt_acl_process(struct os_mbuf *om)
         /* Final fragment received. */
         BLE_HS_DBG_ASSERT(rx_cb != NULL);
         BLE_HS_DBG_ASSERT(rx_buf != NULL);
-        rc = rx_cb(conn_handle, &rx_buf);
+        rc = rx_cb(conn->bhc_rx_chan, &rx_buf);
+        ble_l2cap_forget_rx(conn, conn->bhc_rx_chan);
         os_mbuf_free_chain(rx_buf);
         break;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 09ca9dc..d909b78 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -52,7 +52,7 @@ STATS_NAME_START(ble_l2cap_stats)
 STATS_NAME_END(ble_l2cap_stats)
 
 struct ble_l2cap_chan *
-ble_l2cap_chan_alloc(void)
+ble_l2cap_chan_alloc(uint16_t conn_handle)
 {
     struct ble_l2cap_chan *chan;
 
@@ -62,6 +62,7 @@ ble_l2cap_chan_alloc(void)
     }
 
     memset(chan, 0, sizeof *chan);
+    chan->conn_handle = conn_handle;
 
     STATS_INC(ble_l2cap_stats, chan_create);
 
@@ -126,6 +127,16 @@ ble_l2cap_prepend_hdr(struct os_mbuf *om, uint16_t cid, uint16_t len)
     return om;
 }
 
+uint16_t
+ble_l2cap_get_conn_handle(struct ble_l2cap_chan *chan)
+{
+    if (!chan) {
+        return 0;
+    }
+
+    return chan->conn_handle;
+}
+
 int
 ble_l2cap_create_server(uint16_t psm, uint16_t mtu,
                         ble_l2cap_event_fn *cb, void *cb_arg)
@@ -158,7 +169,7 @@ ble_l2cap_recv_ready(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_rx)
     /*TODO In here we going to update sdu_rx buffer */
 }
 
-static void
+void
 ble_l2cap_forget_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {
     conn->bhc_rx_chan = NULL;
@@ -221,7 +232,6 @@ ble_l2cap_rx_payload(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
         /* All fragments received. */
         *out_rx_cb = chan->rx_fn;
         *out_rx_buf = chan->rx_buf;
-        ble_l2cap_forget_rx(conn, chan);
         rc = 0;
     } else {
         /* More fragments remain. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index 46b903b..178370d 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -117,7 +117,7 @@ ble_l2cap_coc_srv_find(uint16_t psm)
 }
 
 static int
-ble_l2cap_coc_rx_fn(uint16_t conn_handle, struct os_mbuf **rxom)
+ble_l2cap_coc_rx_fn(struct ble_l2cap_chan *chan, struct os_mbuf **rxom)
 {
     return 0;
 }
@@ -129,12 +129,11 @@ ble_l2cap_coc_chan_alloc(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
 {
     struct ble_l2cap_chan *chan;
 
-    chan = ble_l2cap_chan_alloc();
+    chan = ble_l2cap_chan_alloc(conn_handle);
     if (!chan) {
         return NULL;
     }
 
-    chan->conn_handle = conn_handle;
     chan->psm = psm;
     chan->cb = cb;
     chan->cb_arg = cb_arg;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_l2cap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index 81e350d..64ffa22 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -62,10 +62,11 @@ extern struct os_mempool ble_l2cap_chan_pool;
 
 typedef uint8_t ble_l2cap_chan_flags;
 
-typedef int ble_l2cap_rx_fn(uint16_t conn_handle, struct os_mbuf **rxom);
+typedef int ble_l2cap_rx_fn(struct ble_l2cap_chan *chan, struct os_mbuf **rxom);
 
 struct ble_l2cap_chan {
     SLIST_ENTRY(ble_l2cap_chan) next;
+    uint16_t conn_handle;
     uint16_t dcid;
     uint16_t scid;
     uint16_t my_mtu;
@@ -78,7 +79,6 @@ struct ble_l2cap_chan {
     ble_l2cap_rx_fn *rx_fn;
 
 #if MYNEWT_VAL(BLE_L2CAP_COC_MAX_NUM) != 0
-    uint16_t conn_handle;
     uint16_t psm;
     struct ble_l2cap_coc_endpoint coc_rx;
     struct ble_l2cap_coc_endpoint coc_tx;
@@ -104,7 +104,7 @@ int ble_l2cap_parse_hdr(struct os_mbuf *om, int off,
 struct os_mbuf *ble_l2cap_prepend_hdr(struct os_mbuf *om, uint16_t cid,
                                       uint16_t len);
 
-struct ble_l2cap_chan *ble_l2cap_chan_alloc(void);
+struct ble_l2cap_chan *ble_l2cap_chan_alloc(uint16_t conn_handle);
 void ble_l2cap_chan_free(struct ble_l2cap_chan *chan);
 
 bool ble_l2cap_is_mtu_req_sent(const struct ble_l2cap_chan *chan);
@@ -118,6 +118,7 @@ int ble_l2cap_rx(struct ble_hs_conn *conn,
 int ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                  struct os_mbuf *txom);
 
+void ble_l2cap_forget_rx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan);
 int ble_l2cap_init(void);
 
 #ifdef __cplusplus

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 560311d..93d73ed 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -1012,10 +1012,11 @@ ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan)
  *****************************************************************************/
 
 static int
-ble_l2cap_sig_rx(uint16_t conn_handle, struct os_mbuf **om)
+ble_l2cap_sig_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
 {
     struct ble_l2cap_sig_hdr hdr;
     ble_l2cap_sig_rx_fn *rx_cb;
+    uint16_t conn_handle = chan->conn_handle;
     int rc;
 
     STATS_INC(ble_l2cap_stats, sig_rx);
@@ -1054,11 +1055,11 @@ ble_l2cap_sig_rx(uint16_t conn_handle, struct os_mbuf **om)
 }
 
 struct ble_l2cap_chan *
-ble_l2cap_sig_create_chan(void)
+ble_l2cap_sig_create_chan(uint16_t conn_handle)
 {
     struct ble_l2cap_chan *chan;
 
-    chan = ble_l2cap_chan_alloc();
+    chan = ble_l2cap_chan_alloc(conn_handle);
     if (chan == NULL) {
         return NULL;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_l2cap_sig_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_priv.h b/net/nimble/host/src/ble_l2cap_sig_priv.h
index ad3b846..0677405 100644
--- a/net/nimble/host/src/ble_l2cap_sig_priv.h
+++ b/net/nimble/host/src/ble_l2cap_sig_priv.h
@@ -111,7 +111,7 @@ int ble_l2cap_sig_disconnect(struct ble_l2cap_chan *chan);
 
 void ble_l2cap_sig_conn_broken(uint16_t conn_handle, int reason);
 int32_t ble_l2cap_sig_timer(void);
-struct ble_l2cap_chan *ble_l2cap_sig_create_chan(void);
+struct ble_l2cap_chan *ble_l2cap_sig_create_chan(uint16_t conn_handle);
 int ble_l2cap_sig_init(void);
 
 #ifdef __cplusplus

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index 2a880dc..603d99c 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -2326,15 +2326,21 @@ ble_sm_unbond(uint8_t peer_id_addr_type, const uint8_t *peer_id_addr)
 }
 
 static int
-ble_sm_rx(uint16_t conn_handle, struct os_mbuf **om)
+ble_sm_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
 {
     struct ble_sm_result res;
     ble_sm_rx_fn *rx_cb;
     uint8_t op;
+    uint16_t conn_handle;
     int rc;
 
     STATS_INC(ble_l2cap_stats, sm_rx);
 
+    conn_handle = ble_l2cap_get_conn_handle(chan);
+    if (!conn_handle) {
+        return BLE_HS_ENOTCONN;
+    }
+
     rc = os_mbuf_copydata(*om, 0, 1, &op);
     if (rc != 0) {
         return BLE_HS_EBADDATA;
@@ -2485,10 +2491,16 @@ ble_sm_init(void)
  * simple
  */
 static int
-ble_sm_rx(uint16_t handle, struct os_mbuf **om)
+ble_sm_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
 {
     struct ble_sm_pair_fail *cmd;
     struct os_mbuf *txom;
+    uint16_t handle;
+
+    handle = ble_l2cap_get_conn_handle(chan);
+    if (!handle) {
+        return BLE_HS_ENOTCONN;
+    }
 
     cmd = ble_sm_cmd_get(BLE_SM_OP_PAIR_FAIL, sizeof(*cmd), &txom);
     if (cmd == NULL) {
@@ -2502,11 +2514,11 @@ ble_sm_rx(uint16_t handle, struct os_mbuf **om)
 #endif
 
 struct ble_l2cap_chan *
-ble_sm_create_chan(void)
+ble_sm_create_chan(uint16_t conn_handle)
 {
     struct ble_l2cap_chan *chan;
 
-    chan = ble_l2cap_chan_alloc();
+    chan = ble_l2cap_chan_alloc(conn_handle);
     if (chan == NULL) {
         return NULL;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_priv.h b/net/nimble/host/src/ble_sm_priv.h
index eb4f2da..86c31da 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -419,7 +419,7 @@ int ble_sm_init(void);
 
 #endif
 
-struct ble_l2cap_chan *ble_sm_create_chan(void);
+struct ble_l2cap_chan *ble_sm_create_chan(uint16_t handle);
 void *ble_sm_cmd_get(uint8_t opcode, size_t len, struct os_mbuf **txom);
 int ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.c b/net/nimble/host/test/src/ble_hs_test_util.c
index 0dc1b18..9fa2d3d 100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -954,7 +954,8 @@ ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
     } else if (rc == 0) {
         TEST_ASSERT_FATAL(rx_cb != NULL);
         TEST_ASSERT_FATAL(rx_buf != NULL);
-        rc = rx_cb(conn_handle, &rx_buf);
+        rc = rx_cb(conn->bhc_rx_chan, &rx_buf);
+        ble_l2cap_forget_rx(conn, conn->bhc_rx_chan);
         os_mbuf_free_chain(rx_buf);
     } else if (rc == BLE_HS_EAGAIN) {
         /* More fragments on the way. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/27bc9edf/net/nimble/host/test/src/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_l2cap_test.c b/net/nimble/host/test/src/ble_l2cap_test.c
index 9c11607..e68971d 100644
--- a/net/nimble/host/test/src/ble_l2cap_test.c
+++ b/net/nimble/host/test/src/ble_l2cap_test.c
@@ -94,7 +94,7 @@ ble_l2cap_test_util_verify_tx_update_conn(
 }
 
 static int
-ble_l2cap_test_util_dummy_rx(uint16_t conn_handle, struct os_mbuf **om)
+ble_l2cap_test_util_dummy_rx(struct ble_l2cap_chan *chan, struct os_mbuf **om)
 {
     return 0;
 }
@@ -113,7 +113,7 @@ ble_l2cap_test_util_create_conn(uint16_t conn_handle, uint8_t *addr,
     conn = ble_hs_conn_find(conn_handle);
     TEST_ASSERT_FATAL(conn != NULL);
 
-    chan = ble_l2cap_chan_alloc();
+    chan = ble_l2cap_chan_alloc(conn_handle);
     TEST_ASSERT_FATAL(chan != NULL);
 
     chan->scid = BLE_L2CAP_TEST_CID;


[25/26] incubator-mynewt-core git commit: nimble/l2cap: Add initial credits calculations

Posted by cc...@apache.org.
nimble/l2cap: Add initial credits calculations

With this patch we calculate initial credits in that way that
peer device is able to send full SDU by fill up fully LE frames.

If it happens that peer is not filling up LE Frames, there is mechanism
to handling it already in receiving function


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

Branch: refs/heads/develop
Commit: 750707ba60915ea182d416c8289324d16af8fa64
Parents: 6963daf
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Fri Mar 3 11:30:45 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:43 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_coc.c  | 16 ++++++++++------
 net/nimble/host/src/ble_l2cap_priv.h |  1 +
 2 files changed, 11 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/750707ba/net/nimble/host/src/ble_l2cap_coc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_coc.c b/net/nimble/host/src/ble_l2cap_coc.c
index f7ecef4..fbfbf31 100644
--- a/net/nimble/host/src/ble_l2cap_coc.c
+++ b/net/nimble/host/src/ble_l2cap_coc.c
@@ -249,9 +249,14 @@ ble_l2cap_coc_chan_alloc(uint16_t conn_handle, uint16_t psm, uint16_t mtu,
     chan->my_mtu = BLE_L2CAP_COC_MTU;
     chan->rx_fn = ble_l2cap_coc_rx_fn;
     chan->coc_rx.mtu = mtu;
-    chan->coc_rx.credits = 10; /* FIXME Calculate it */
     chan->coc_rx.sdu = sdu_rx;
 
+    /* Number of credits should allow to send full SDU with on given
+     * L2CAP MTU
+     */
+    chan->coc_rx.credits = (mtu + (chan->my_mtu - 1) / 2) / chan->my_mtu;
+
+    chan->initial_credits = chan->coc_rx.credits;
     return chan;
 }
 
@@ -459,13 +464,12 @@ ble_l2cap_coc_recv_ready(struct ble_l2cap_chan *chan, struct os_mbuf *sdu_rx)
         return;
     }
 
-    /* FIXME 10 is hardcoded - make it better.
-     * We want to back only that much credits which remote side is missing
+    /* We want to back only that much credits which remote side is missing
      * to be able to send complete SDU.
      */
-    if (chan->coc_rx.credits < 10) {
-        ble_l2cap_sig_le_credits(chan, 10 - chan->coc_rx.credits);
-        chan->coc_rx.credits = 10;
+    if (chan->coc_rx.credits < c->initial_credits) {
+        ble_l2cap_sig_le_credits(chan, c->initial_credits - chan->coc_rx.credits);
+        chan->coc_rx.credits = c->initial_credits;
     }
 
     ble_hs_unlock();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/750707ba/net/nimble/host/src/ble_l2cap_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_priv.h b/net/nimble/host/src/ble_l2cap_priv.h
index 5db63c3..1d035e8 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -82,6 +82,7 @@ struct ble_l2cap_chan {
     uint16_t psm;
     struct ble_l2cap_coc_endpoint coc_rx;
     struct ble_l2cap_coc_endpoint coc_tx;
+    uint16_t initial_credits;
     ble_l2cap_event_fn *cb;
     void *cb_arg;
 #endif


[11/26] incubator-mynewt-core git commit: nimble/l2cap: Remove not needed function ble_l2cap_sig_init_cmd

Posted by cc...@apache.org.
nimble/l2cap: Remove not needed function ble_l2cap_sig_init_cmd

After refactor this function is not needed anymore in the host code.
The only user of it is unitest and this patch fixes that.


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

Branch: refs/heads/develop
Commit: d9f788b49df395538cc2081eabe38161a3165ca7
Parents: ba9de55
Author: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Authored: Tue Feb 28 13:23:41 2017 +0100
Committer: \u0141ukasz Rymanowski <lu...@codecoup.pl>
Committed: Fri Mar 3 12:40:42 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_sig_cmd.c     | 34 ----------------------
 net/nimble/host/test/src/ble_hs_test_util.c | 22 ++++-----------
 net/nimble/host/test/src/ble_l2cap_test.c   | 36 ++++++------------------
 3 files changed, 13 insertions(+), 79 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9f788b4/net/nimble/host/src/ble_l2cap_sig_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig_cmd.c b/net/nimble/host/src/ble_l2cap_sig_cmd.c
index e6a7209..4381be5 100644
--- a/net/nimble/host/src/ble_l2cap_sig_cmd.c
+++ b/net/nimble/host/src/ble_l2cap_sig_cmd.c
@@ -21,40 +21,6 @@
 #include "ble_hs_priv.h"
 
 int
-ble_l2cap_sig_init_cmd(uint8_t op, uint8_t id, uint8_t payload_len,
-                       struct os_mbuf **out_om, void **out_payload_buf)
-{
-    struct ble_l2cap_sig_hdr hdr;
-    struct os_mbuf *txom;
-    void *v;
-
-    *out_om = NULL;
-    *out_payload_buf = NULL;
-
-    txom = ble_hs_mbuf_l2cap_pkt();
-    if (txom == NULL) {
-        return BLE_HS_ENOMEM;
-    }
-
-    v = os_mbuf_extend(txom, BLE_L2CAP_SIG_HDR_SZ + payload_len);
-    if (v == NULL) {
-        os_mbuf_free(txom);
-        return BLE_HS_ENOMEM;
-    }
-
-    hdr.op = op;
-    hdr.identifier = id;
-    hdr.length = TOFROMLE16(payload_len);
-
-    ble_l2cap_sig_hdr_write(v, BLE_L2CAP_SIG_HDR_SZ, &hdr);
-
-    *out_om = txom;
-    *out_payload_buf = (uint8_t *)v + BLE_L2CAP_SIG_HDR_SZ;
-
-    return 0;
-}
-
-int
 ble_l2cap_sig_tx(uint16_t conn_handle, struct os_mbuf *txom)
 {
     struct ble_l2cap_chan *chan;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9f788b4/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.c b/net/nimble/host/test/src/ble_hs_test_util.c
index ceabd1c..0dc1b18 100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -1820,36 +1820,24 @@ ble_l2cap_sig_update_rsp_parse(void *payload, int len,
     dst->result = le16toh(src->result);
 }
 
-static void
-ble_l2cap_test_update_rsp_write(void *payload, int len,
-                               struct ble_l2cap_sig_update_rsp *src)
-{
-    struct ble_l2cap_sig_update_rsp *dst = payload;
-
-    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_RSP_SZ);
-    dst->result = htole16(src->result);
-}
-
 int
 ble_hs_test_util_rx_l2cap_update_rsp(uint16_t conn_handle,
                                      uint8_t id, uint16_t result)
 {
-    struct ble_l2cap_sig_update_rsp rsp;
+    struct ble_l2cap_sig_update_rsp *rsp;
     struct hci_data_hdr hci_hdr;
     struct os_mbuf *om;
-    void *v;
     int rc;
 
     hci_hdr = BLE_HS_TEST_UTIL_L2CAP_HCI_HDR(
         2, BLE_HCI_PB_FIRST_FLUSH,
         BLE_L2CAP_HDR_SZ + BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_RSP_SZ);
 
-    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_RSP, id,
-                                BLE_L2CAP_SIG_UPDATE_RSP_SZ, &om, &v);
-    TEST_ASSERT_FATAL(rc == 0);
+    rsp = ble_l2cap_sig_cmd_get(BLE_L2CAP_SIG_OP_UPDATE_RSP, id,
+                                BLE_L2CAP_SIG_UPDATE_RSP_SZ, &om);
+    TEST_ASSERT_FATAL(rsp != NULL);
 
-    rsp.result = result;
-    ble_l2cap_test_update_rsp_write(v, BLE_L2CAP_SIG_UPDATE_RSP_SZ, &rsp);
+    rsp->result = htole16(result);
 
     rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SIG,
                                               &hci_hdr, om);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9f788b4/net/nimble/host/test/src/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_l2cap_test.c b/net/nimble/host/test/src/ble_l2cap_test.c
index 2958a6c..9c11607 100644
--- a/net/nimble/host/test/src/ble_l2cap_test.c
+++ b/net/nimble/host/test/src/ble_l2cap_test.c
@@ -44,46 +44,26 @@ ble_l2cap_test_util_init(void)
 }
 
 static void
-ble_l2cap_test_update_req_swap(struct ble_l2cap_sig_update_req *dst,
-                              struct ble_l2cap_sig_update_req *src)
-{
-    dst->itvl_min = le16toh(src->itvl_min);
-    dst->itvl_max = le16toh(src->itvl_max);
-    dst->slave_latency = le16toh(src->slave_latency);
-    dst->timeout_multiplier = le16toh(src->timeout_multiplier);
-}
-
-static void
-ble_l2cap_test_update_req_write(void *payload, int len,
-                               struct ble_l2cap_sig_update_req *src)
-{
-    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_REQ_SZ);
-    ble_l2cap_test_update_req_swap(payload, src);
-}
-
-static void
 ble_l2cap_test_util_rx_update_req(uint16_t conn_handle, uint8_t id,
                                   struct ble_l2cap_sig_update_params *params)
 {
-    struct ble_l2cap_sig_update_req req;
+    struct ble_l2cap_sig_update_req *req;
     struct hci_data_hdr hci_hdr;
     struct os_mbuf *om;
-    void *v;
     int rc;
 
     hci_hdr = BLE_HS_TEST_UTIL_L2CAP_HCI_HDR(
         2, BLE_HCI_PB_FIRST_FLUSH,
         BLE_L2CAP_HDR_SZ + BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_REQ_SZ);
 
-    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_REQ, id,
-                                BLE_L2CAP_SIG_UPDATE_REQ_SZ, &om, &v);
-    TEST_ASSERT_FATAL(rc == 0);
+    req = ble_l2cap_sig_cmd_get(BLE_L2CAP_SIG_OP_UPDATE_REQ, id,
+                                BLE_L2CAP_SIG_UPDATE_REQ_SZ, &om);
+    TEST_ASSERT_FATAL(req != NULL);
 
-    req.itvl_min = params->itvl_min;
-    req.itvl_max = params->itvl_max;
-    req.slave_latency = params->slave_latency;
-    req.timeout_multiplier = params->timeout_multiplier;
-    ble_l2cap_test_update_req_write(v, BLE_L2CAP_SIG_UPDATE_REQ_SZ, &req);
+    req->itvl_min = htole16(params->itvl_min);
+    req->itvl_max = htole16(params->itvl_max);
+    req->slave_latency = htole16(params->slave_latency);
+    req->timeout_multiplier = htole16(params->timeout_multiplier);
 
     ble_hs_test_util_set_ack(
         ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,