You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/03/23 05:38:28 UTC

[2/4] incubator-mynewt-core git commit: interim sm commit - basic pair req/rsp.

interim sm commit - basic pair req/rsp.


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

Branch: refs/heads/ble-host-sm
Commit: 64f622b23d5941cc05d8132a45130dc6527d5689
Parents: ef426a9
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Mar 22 15:47:30 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Mar 22 21:37:26 2016 -0700

----------------------------------------------------------------------
 net/nimble/host/src/ble_l2cap_priv.h   |   1 -
 net/nimble/host/src/ble_l2cap_sig.c    |   3 +-
 net/nimble/host/src/ble_l2cap_sm.c     |  88 +++++---------------
 net/nimble/host/src/ble_l2cap_sm.h     |  27 ++++++
 net/nimble/host/src/ble_l2cap_sm_cmd.c | 125 ++++++++++++++++++++++++++++
 5 files changed, 176 insertions(+), 68 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/64f622b2/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 70c7bb4..6f6438a 100644
--- a/net/nimble/host/src/ble_l2cap_priv.h
+++ b/net/nimble/host/src/ble_l2cap_priv.h
@@ -70,7 +70,6 @@ struct ble_l2cap_sig_update_rsp {
 #define BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT  0x0000
 #define BLE_L2CAP_SIG_UPDATE_RSP_RESULT_REJECT  0x0001
 
-
 #define BLE_L2CAP_CID_ATT   4
 #define BLE_L2CAP_CID_SIG   5
 #define BLE_L2CAP_CID_SM    6

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/64f622b2/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 cc8fb46..112d541 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -434,8 +434,7 @@ ble_l2cap_sig_update_req_rx(uint16_t conn_handle,
         return BLE_HS_ENOTSUP;
     }
 
-    rc = ble_l2cap_sig_update_req_parse((*om)->om_data, (*om)->om_len,
-                                        &req);
+    rc = ble_l2cap_sig_update_req_parse((*om)->om_data, (*om)->om_len, &req);
     assert(rc == 0);
 
     params.itvl_min = req.itvl_min;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/64f622b2/net/nimble/host/src/ble_l2cap_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm.c b/net/nimble/host/src/ble_l2cap_sm.c
index d52d587..368ff36 100644
--- a/net/nimble/host/src/ble_l2cap_sm.c
+++ b/net/nimble/host/src/ble_l2cap_sm.c
@@ -63,33 +63,6 @@ ble_l2cap_sm_dispatch_get(uint8_t op)
 }
 
 /**
- * Lock restrictions:
- *     o Caller locks ble_hs_conn.
- */
-static int
-ble_l2cap_sm_conn_chan_find(uint16_t conn_handle,
-                            struct ble_hs_conn **out_conn,
-                            struct ble_l2cap_chan **out_chan)
-{
-    int rc;
-
-    rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
-                                         out_conn, out_chan);
-    return rc;
-}
-
-static int
-ble_l2cap_sm_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
-                struct os_mbuf *txom)
-{
-    int rc;
-
-    STATS_INC(ble_l2cap_stats, sm_tx);
-    rc = ble_l2cap_tx(conn, chan, txom);
-    return rc;
-}
-
-/**
  * Lock restrictions: None.
  */
 static int
@@ -101,53 +74,39 @@ ble_l2cap_sm_rx_noop(uint16_t conn_handle, uint8_t op, struct os_mbuf **om)
 static int
 ble_l2cap_sm_pair_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om)
 {
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-    struct os_mbuf *txom;
-    uint8_t *u;
+    struct ble_l2cap_sm_pair_cmd req;
+    struct ble_l2cap_sm_pair_cmd rsp;
     int rc;
 
-    txom = NULL;
-
-    txom = ble_hs_misc_pkthdr();
-    if (txom == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-
-    u = os_mbuf_extend(txom, 2);
-    if (u == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-
-    /* Op. */
-    u[0] = BLE_L2CAP_SM_OP_PAIR_FAILED;
-
-    /* Reason: Pairing not supported. */
-    u[1] = 5;
-
-    ble_hs_conn_lock();
-
-    rc = ble_l2cap_sm_conn_chan_find(conn_handle, &conn, &chan);
+    rc = ble_hs_misc_pullup_base(om, BLE_L2CAP_SM_PAIR_CMD_SZ);
     if (rc != 0) {
-        rc = BLE_HS_EUNKNOWN;
-    } else {
-        rc = ble_l2cap_sm_tx(conn, chan, txom);
-        txom = NULL;
+        return rc;
     }
 
-    ble_hs_conn_unlock();
+    rc = ble_l2cap_sm_pair_cmd_parse((*om)->om_data, (*om)->om_len, &req);
+    assert(rc == 0);
 
+    BLE_HS_LOG(DEBUG, "rxed sm pair req; io_cap=0x%02x oob_data_flag=%d "
+                      "authreq=0x%02x max_enc_key_size=%d "
+                      "init_key_dist=0x%02x resp_key_dist=0x%02x\n",
+               req.io_cap, req.oob_data_flag, req.authreq,
+               req.max_enc_key_size, req.init_key_dist, req.resp_key_dist);
+
+    /* Op. */
+    rsp.io_cap = 3;
+    rsp.oob_data_flag = 0;
+    rsp.authreq = 0;
+    rsp.max_enc_key_size = 16;
+    rsp.init_key_dist = 0;
+    rsp.resp_key_dist = 0;
+
+    rc = ble_l2cap_sm_pair_cmd_tx(conn_handle, 0, &rsp);
+    /* XXX: Update SM state machine, notify application, etc. */
     if (rc != 0) {
-        goto err;
+        return rc;
     }
 
     return 0;
-
-err:
-    os_mbuf_free_chain(txom);
-    return rc;
 }
 
 /**
@@ -164,7 +123,6 @@ ble_l2cap_sm_rx(uint16_t conn_handle, struct os_mbuf **om)
     STATS_INC(ble_l2cap_stats, sm_rx);
     BLE_HS_LOG(DEBUG, "L2CAP - rxed security manager msg: ");
     ble_hs_misc_log_mbuf(*om);
-    BLE_HS_LOG(DEBUG, "\n");
 
     rc = ble_hs_misc_pullup_base(om, 1);
     if (rc != 0) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/64f622b2/net/nimble/host/src/ble_l2cap_sm.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm.h b/net/nimble/host/src/ble_l2cap_sm.h
index e2099f6..0adadb0 100644
--- a/net/nimble/host/src/ble_l2cap_sm.h
+++ b/net/nimble/host/src/ble_l2cap_sm.h
@@ -22,6 +22,33 @@
 
 #define BLE_L2CAP_SM_MTU            65
 
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x01/0x02 [req/rsp])         | 1                 |
+ * | IO Capability                      | 1                 |
+ * | OOB data flag                      | 1                 |
+ * | AuthReq                            | 1                 |
+ * | Maximum Encryption Key Size        | 1                 |
+ * | Initiator Key Distribution         | 1                 |
+ * | Responder Key Distribution         | 1                 |
+ */
+#define BLE_L2CAP_SM_PAIR_CMD_SZ         6
+struct ble_l2cap_sm_pair_cmd {
+    uint8_t io_cap;
+    uint8_t oob_data_flag;
+    uint8_t authreq;
+    uint8_t max_enc_key_size;
+    uint8_t init_key_dist;
+    uint8_t resp_key_dist;
+};
+
 struct ble_l2cap_chan *ble_l2cap_sm_create_chan(void);
 
+int ble_l2cap_sm_pair_cmd_parse(void *payload, int len,
+                                struct ble_l2cap_sm_pair_cmd *cmd);
+int ble_l2cap_sm_pair_cmd_write(void *payload, int len, int is_req,
+                                struct ble_l2cap_sm_pair_cmd *cmd);
+int ble_l2cap_sm_pair_cmd_tx(uint16_t conn_handle, int is_req,
+                             struct ble_l2cap_sm_pair_cmd *cmd);
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/64f622b2/net/nimble/host/src/ble_l2cap_sm_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sm_cmd.c b/net/nimble/host/src/ble_l2cap_sm_cmd.c
new file mode 100644
index 0000000..0489e30
--- /dev/null
+++ b/net/nimble/host/src/ble_l2cap_sm_cmd.c
@@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include "console/console.h"
+#include "nimble/ble.h"
+#include "ble_hs_priv.h"
+
+static int
+ble_l2cap_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    STATS_INC(ble_l2cap_stats, sm_tx);
+
+    ble_hs_conn_lock();
+
+    rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
+                                         &conn, &chan);
+    if (rc == 0) {
+        rc = ble_l2cap_tx(conn, chan, txom);
+    }
+
+    ble_hs_conn_unlock();
+
+    return rc;
+}
+
+int
+ble_l2cap_sm_pair_cmd_parse(void *payload, int len,
+                            struct ble_l2cap_sm_pair_cmd *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_L2CAP_SM_PAIR_CMD_SZ) {
+        return BLE_HS_EBADDATA;
+    }
+
+    u8ptr = payload;
+
+    cmd->io_cap = u8ptr[0];
+    cmd->oob_data_flag = u8ptr[1];
+    cmd->authreq = u8ptr[2];
+    cmd->max_enc_key_size = u8ptr[3];
+    cmd->init_key_dist = u8ptr[4];
+    cmd->resp_key_dist = u8ptr[5];
+
+    return 0;
+}
+
+int
+ble_l2cap_sm_pair_cmd_write(void *payload, int len, int is_req,
+                            struct ble_l2cap_sm_pair_cmd *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < 1 + BLE_L2CAP_SM_PAIR_CMD_SZ) {
+        return BLE_HS_EBADDATA;
+    }
+
+    u8ptr = payload;
+
+    u8ptr[0] = is_req ? BLE_L2CAP_SM_OP_PAIR_REQ : BLE_L2CAP_SM_OP_PAIR_RSP;
+    u8ptr[1] = cmd->io_cap;
+    u8ptr[2] = cmd->oob_data_flag;
+    u8ptr[3] = cmd->authreq;
+    u8ptr[4] = cmd->max_enc_key_size;
+    u8ptr[5] = cmd->init_key_dist;
+    u8ptr[6] = cmd->resp_key_dist;
+
+    return 0;
+}
+
+int
+ble_l2cap_sm_pair_cmd_tx(uint16_t conn_handle, int is_req,
+                         struct ble_l2cap_sm_pair_cmd *cmd)
+{
+    struct os_mbuf *txom;
+    void *v;
+    int rc;
+
+    txom = ble_hs_misc_pkthdr();
+    if (txom == NULL) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    v = os_mbuf_extend(txom, 1 + BLE_L2CAP_SM_PAIR_CMD_SZ);
+    if (v == NULL) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    rc = ble_l2cap_sm_pair_cmd_write(v, 1 + BLE_L2CAP_SIG_UPDATE_REQ_SZ,
+                                     is_req, cmd);
+    assert(rc == 0);
+
+    rc = ble_l2cap_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+