nmxact - conn_find

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

Branch: refs/heads/master
Commit: e94d645a8d76b4dae829c1aa2706c78cd38c7709
Parents: 06c80b7
Author: Christopher Collins <[email protected]>
Authored: Wed Apr 5 19:09:32 2017 -0700
Committer: Christopher Collins <[email protected]>
Committed: Wed Apr 5 19:09:32 2017 -0700

----------------------------------------------------------------------
 nmxact/bledefs/bledefs.go | 12 +++++++
 nmxact/nmble/ble_act.go   | 51 +++++++++++++++++++++++++++-
 nmxact/nmble/ble_fsm.go   | 31 +++++++++++++----
 nmxact/nmble/ble_proto.go | 76 ++++++++++++++++++++++++++++--------------
 nmxact/nmble/ble_util.go  |  8 +++++
 nmxact/nmble/ble_xport.go | 20 +++++++++++
 nmxact/nmble/dispatch.go  |  2 ++
 7 files changed, 168 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e94d645a/nmxact/bledefs/bledefs.go
----------------------------------------------------------------------
diff --git a/nmxact/bledefs/bledefs.go b/nmxact/bledefs/bledefs.go
index 3bb2772..570ddb0 100644
--- a/nmxact/bledefs/bledefs.go
+++ b/nmxact/bledefs/bledefs.go
@@ -268,3 +268,15 @@ type BleAdvReport struct {
 }
 
 type BleAdvPredicate func(adv BleAdvReport) bool
+
+type BleConnDesc struct {
+       ConnHandle      uint16
+       OwnIdAddrType   BleAddrType
+       OwnIdAddr       BleAddr
+       OwnOtaAddrType  BleAddrType
+       OwnOtaAddr      BleAddr
+       PeerIdAddrType  BleAddrType
+       PeerIdAddr      BleAddr
+       PeerOtaAddrType BleAddrType
+       PeerOtaAddr     BleAddr
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e94d645a/nmxact/nmble/ble_act.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_act.go b/nmxact/nmble/ble_act.go
index ff6dff9..7cd05a8 100644
--- a/nmxact/nmble/ble_act.go
+++ b/nmxact/nmble/ble_act.go
@@ -276,7 +276,7 @@ func exchangeMtu(x *BleXport, bl *BleListener, r 
*BleExchangeMtuReq) (
                                                MSG_TYPE_MTU_CHANGE_EVT,
                                                msg.Status)
                                } else {
-                                       return msg.Mtu, nil
+                                       return int(msg.Mtu), nil
                                }
 
                        default:
@@ -363,6 +363,55 @@ func scanCancel(x *BleXport, bl *BleListener, r 
*BleScanCancelReq) error {
        }
 }
 
+func connFind(x *BleXport, bl *BleListener, r *BleConnFindReq) (
+       BleConnDesc, error) {
+
+       const msgType = MSG_TYPE_CONN_FIND
+
+       j, err := json.Marshal(r)
+       if err != nil {
+               return BleConnDesc{}, err
+       }
+
+       if err := x.Tx(j); err != nil {
+               return BleConnDesc{}, err
+       }
+
+       for {
+               select {
+               case err := <-bl.ErrChan:
+                       return BleConnDesc{}, err
+
+               case bm := <-bl.BleChan:
+                       switch msg := bm.(type) {
+                       case *BleConnFindRsp:
+                               bl.Acked = true
+                               if msg.Status != 0 {
+                                       return BleConnDesc{},
+                                               StatusError(MSG_OP_RSP, 
msgType, msg.Status)
+                               }
+
+                               return BleConnDesc{
+                                       ConnHandle:      msg.ConnHandle,
+                                       OwnIdAddrType:   msg.OwnIdAddrType,
+                                       OwnIdAddr:       msg.OwnIdAddr,
+                                       OwnOtaAddrType:  msg.OwnOtaAddrType,
+                                       OwnOtaAddr:      msg.OwnOtaAddr,
+                                       PeerIdAddrType:  msg.PeerIdAddrType,
+                                       PeerIdAddr:      msg.PeerIdAddr,
+                                       PeerOtaAddrType: msg.PeerOtaAddrType,
+                                       PeerOtaAddr:     msg.PeerOtaAddr,
+                               }, nil
+
+                       default:
+                       }
+
+               case <-bl.AfterTimeout(x.RspTimeout()):
+                       return BleConnDesc{}, BhdTimeoutError(msgType)
+               }
+       }
+}
+
 // Asks the controller to generate a random address.  This is done when the
 // transport is starting up, and therefore does not require the transport to be
 // synced.  Only the transport should call this function.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e94d645a/nmxact/nmble/ble_fsm.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_fsm.go b/nmxact/nmble/ble_fsm.go
index dc94ca9..8c374b8 100644
--- a/nmxact/nmble/ble_fsm.go
+++ b/nmxact/nmble/ble_fsm.go
@@ -60,7 +60,7 @@ type BleFsm struct {
        params BleFsmParams
 
        peerDev         *BleDev
-       connHandle      int
+       connHandle      uint16
        nmpSvc          *BleSvc
        nmpReqChr       *BleChr
        nmpRspChr       *BleChr
@@ -203,6 +203,27 @@ func (bf *BleFsm) action(
        return nil
 }
 
+func (bf *BleFsm) logConnection() {
+       desc, err := bf.params.Bx.connFind(bf.connHandle)
+       if err != nil {
+               return
+       }
+
+       log.Debugf("BLE connection attempt succeeded; "+
+               "conn_handle=%d "+
+               "own_id_addr=%s,%s own_ota_addr=%s,%s "+
+               "peer_id_addr=%s,%s peer_ota_addr=%s,%s",
+               desc.ConnHandle,
+               BleAddrTypeToString(desc.OwnIdAddrType),
+               desc.OwnIdAddr.String(),
+               BleAddrTypeToString(desc.OwnOtaAddrType),
+               desc.OwnOtaAddr.String(),
+               BleAddrTypeToString(desc.PeerIdAddrType),
+               desc.PeerIdAddr.String(),
+               BleAddrTypeToString(desc.PeerOtaAddrType),
+               desc.PeerOtaAddr.String())
+}
+
 func calcDisconnectType(state BleSesnState) BleFsmDisconnectType {
        switch state {
        case SESN_STATE_EXCHANGING_MTU:
@@ -282,10 +303,8 @@ func (bf *BleFsm) connectListen(seq BleSeq) error {
                                case *BleConnectEvt:
                                        if msg.Status == 0 {
                                                bl.Acked = true
-                                               log.Debugf("BLE connection 
attempt succeeded; "+
-                                                       "peer=%d handle=%d", 
bf.peerDev.String(),
-                                                       msg.ConnHandle)
                                                bf.connHandle = msg.ConnHandle
+                                               bf.logConnection()
                                                if err := bf.nmpRspListen(); 
err != nil {
                                                        bf.connChan <- err
                                                        return
@@ -310,7 +329,7 @@ func (bf *BleFsm) connectListen(seq BleSeq) error {
                                        } else {
                                                log.Debugf("BLE ATT MTU 
updated; from=%d to=%d",
                                                        bf.attMtu, msg.Mtu)
-                                               bf.attMtu = msg.Mtu
+                                               bf.attMtu = int(msg.Mtu)
                                        }
 
                                case *BleDisconnectEvt:
@@ -334,7 +353,7 @@ func (bf *BleFsm) nmpRspListen() error {
                Op:         MSG_OP_EVT,
                Type:       MSG_TYPE_NOTIFY_RX_EVT,
                Seq:        BLE_SEQ_NONE,
-               ConnHandle: bf.connHandle,
+               ConnHandle: int(bf.connHandle),
        }
 
        bl, err := bf.addBleListener(base)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e94d645a/nmxact/nmble/ble_proto.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_proto.go b/nmxact/nmble/ble_proto.go
index 41afd51..f955fab 100644
--- a/nmxact/nmble/ble_proto.go
+++ b/nmxact/nmble/ble_proto.go
@@ -233,6 +233,8 @@ const (
        MSG_TYPE_SCAN                      = 15
        MSG_TYPE_SCAN_CANCEL               = 16
        MSG_TYPE_SET_PREFERRED_MTU         = 17
+       MSG_TYPE_SECURITY_INITIATE         = 18
+       MSG_TYPE_CONN_FIND                 = 19
 
        MSG_TYPE_SYNC_EVT       = 2049
        MSG_TYPE_CONNECT_EVT    = 2050
@@ -243,6 +245,7 @@ const (
        MSG_TYPE_NOTIFY_RX_EVT  = 2055
        MSG_TYPE_MTU_CHANGE_EVT = 2056
        MSG_TYPE_SCAN_EVT       = 2057
+       MSG_TYPE_ENC_CHANGE_EVT = 2058
 )
 
 var MsgOpStringMap = map[MsgOp]string{
@@ -267,6 +270,7 @@ var MsgTypeStringMap = map[MsgType]string{
        MSG_TYPE_SCAN:              "scan",
        MSG_TYPE_SCAN_CANCEL:       "scan_cancel",
        MSG_TYPE_SET_PREFERRED_MTU: "set_preferred_mtu",
+       MSG_TYPE_CONN_FIND:         "conn_find",
 
        MSG_TYPE_SYNC_EVT:       "sync_evt",
        MSG_TYPE_CONNECT_EVT:    "connect_evt",
@@ -276,6 +280,7 @@ var MsgTypeStringMap = map[MsgType]string{
        MSG_TYPE_NOTIFY_RX_EVT:  "notify_rx_evt",
        MSG_TYPE_MTU_CHANGE_EVT: "mtu_change_evt",
        MSG_TYPE_SCAN_EVT:       "scan_evt",
+       MSG_TYPE_ENC_CHANGE_EVT: "enc_change_evt",
 }
 
 type BleHdr struct {
@@ -346,16 +351,8 @@ type BleConnectEvt struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       Status          int         `json:"status"`
-       ConnHandle      int         `json:"conn_handle"`
-       OwnIdAddrType   BleAddrType `json:"own_id_addr_type"`
-       OwnIdAddr       BleAddr     `json:"own_id_addr"`
-       OwnOtaAddrType  BleAddrType `json:"own_ota_addr_type"`
-       OwnOtaAddr      BleAddr     `json:"own_ota_addr"`
-       PeerIdAddrType  BleAddrType `json:"peer_id_addr_type"`
-       PeerIdAddr      BleAddr     `json:"peer_id_addr"`
-       PeerOtaAddrType BleAddrType `json:"peer_ota_addr_type"`
-       PeerOtaAddr     BleAddr     `json:"peer_ota_addr"`
+       Status     int    `json:"status"`
+       ConnHandle uint16 `json:"conn_handle"`
 }
 
 type BleTerminateReq struct {
@@ -364,8 +361,8 @@ type BleTerminateReq struct {
        Type MsgType `json:"type"`
        Seq  BleSeq  `json:"seq"`
 
-       ConnHandle int `json:"conn_handle"`
-       HciReason  int `json:"hci_reason"`
+       ConnHandle uint16 `json:"conn_handle"`
+       HciReason  int    `json:"hci_reason"`
 }
 
 type BleTerminateRsp struct {
@@ -402,8 +399,8 @@ type BleDisconnectEvt struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       Reason     int `json:"reason"`
-       ConnHandle int `json:"conn_handle"`
+       Reason     int    `json:"reason"`
+       ConnHandle uint16 `json:"conn_handle"`
 }
 
 type BleDiscSvcUuidReq struct {
@@ -413,7 +410,7 @@ type BleDiscSvcUuidReq struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       ConnHandle int     `json:"conn_handle"`
+       ConnHandle uint16  `json:"conn_handle"`
        Uuid       BleUuid `json:"svc_uuid"`
 }
 
@@ -445,7 +442,7 @@ type BleDiscChrUuidReq struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       ConnHandle  int     `json:"conn_handle"`
+       ConnHandle  uint16  `json:"conn_handle"`
        StartHandle int     `json:"start_handle"`
        EndHandle   int     `json:"end_handle"`
        Uuid        BleUuid `json:"chr_uuid"`
@@ -458,9 +455,9 @@ type BleDiscAllChrsReq struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       ConnHandle  int `json:"conn_handle"`
-       StartHandle int `json:"start_handle"`
-       EndHandle   int `json:"end_handle"`
+       ConnHandle  uint16 `json:"conn_handle"`
+       StartHandle int    `json:"start_handle"`
+       EndHandle   int    `json:"end_handle"`
 }
 
 type BleDiscAllChrsRsp struct {
@@ -522,7 +519,7 @@ type BleWriteCmdReq struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       ConnHandle int      `json:"conn_handle"`
+       ConnHandle uint16   `json:"conn_handle"`
        AttrHandle int      `json:"attr_handle"`
        Data       BleBytes `json:"data"`
 }
@@ -554,7 +551,7 @@ type BleNotifyRxEvt struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       ConnHandle int      `json:"conn_handle"`
+       ConnHandle uint16   `json:"conn_handle"`
        AttrHandle int      `json:"attr_handle"`
        Indication bool     `json:"indication"`
        Data       BleBytes `json:"data"`
@@ -567,7 +564,7 @@ type BleExchangeMtuReq struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       ConnHandle int `json:"conn_handle"`
+       ConnHandle uint16 `json:"conn_handle"`
 }
 
 type BleExchangeMtuRsp struct {
@@ -587,9 +584,9 @@ type BleMtuChangeEvt struct {
        Seq  BleSeq  `json:"seq"`
 
        // Mandatory
-       Status     int `json:"status"`
-       ConnHandle int `json:"conn_handle"`
-       Mtu        int `json:"mtu"`
+       Status     int    `json:"status"`
+       ConnHandle uint16 `json:"conn_handle"`
+       Mtu        uint16 `json:"mtu"`
 }
 
 type BleGenRandAddrReq struct {
@@ -716,6 +713,35 @@ type BleSetPreferredMtuRsp struct {
        Status int `json:"status"`
 }
 
+type BleConnFindReq struct {
+       // Header
+       Op   MsgOp   `json:"op"`
+       Type MsgType `json:"type"`
+       Seq  BleSeq  `json:"seq"`
+
+       // Mandatory
+       ConnHandle uint16 `json:"conn_handle"`
+}
+
+type BleConnFindRsp struct {
+       // Header
+       Op   MsgOp   `json:"op"`
+       Type MsgType `json:"type"`
+       Seq  BleSeq  `json:"seq"`
+
+       // Mandatory
+       Status          int         `json:"status"`
+       ConnHandle      uint16      `json:"conn_handle"`
+       OwnIdAddrType   BleAddrType `json:"own_id_addr_type"`
+       OwnIdAddr       BleAddr     `json:"own_id_addr"`
+       OwnOtaAddrType  BleAddrType `json:"own_ota_addr_type"`
+       OwnOtaAddr      BleAddr     `json:"own_ota_addr"`
+       PeerIdAddrType  BleAddrType `json:"peer_id_addr_type"`
+       PeerIdAddr      BleAddr     `json:"peer_id_addr"`
+       PeerOtaAddrType BleAddrType `json:"peer_ota_addr_type"`
+       PeerOtaAddr     BleAddr     `json:"peer_ota_addr"`
+}
+
 func ErrCodeToString(e int) string {
        var s string
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e94d645a/nmxact/nmble/ble_util.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_util.go b/nmxact/nmble/ble_util.go
index a61618f..57bc324 100644
--- a/nmxact/nmble/ble_util.go
+++ b/nmxact/nmble/ble_util.go
@@ -196,3 +196,11 @@ func NewBleSetPreferredMtuReq() *BleSetPreferredMtuReq {
                Seq:  NextSeq(),
        }
 }
+
+func NewBleConnFindReq() *BleConnFindReq {
+       return &BleConnFindReq{
+               Op:   MSG_OP_REQ,
+               Type: MSG_TYPE_CONN_FIND,
+               Seq:  NextSeq(),
+       }
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e94d645a/nmxact/nmble/ble_xport.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go
index 4f44577..e575d78 100644
--- a/nmxact/nmble/ble_xport.go
+++ b/nmxact/nmble/ble_xport.go
@@ -138,6 +138,26 @@ func (bx *BleXport) genRandAddr() (BleAddr, error) {
        return genRandAddr(bx, bl, r)
 }
 
+func (bx *BleXport) connFind(connHandle uint16) (BleConnDesc, error) {
+       r := NewBleConnFindReq()
+       r.ConnHandle = connHandle
+
+       base := BleMsgBase{
+               Op:         -1,
+               Type:       -1,
+               Seq:        r.Seq,
+               ConnHandle: -1,
+       }
+
+       bl := NewBleListener()
+       if err := bx.Bd.AddListener(base, bl); err != nil {
+               return BleConnDesc{}, err
+       }
+       defer bx.Bd.RemoveListener(base)
+
+       return connFind(bx, bl, r)
+}
+
 func (bx *BleXport) setRandAddr(addr BleAddr) error {
        r := NewBleSetRandAddrReq()
        r.Addr = addr

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e94d645a/nmxact/nmble/dispatch.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/dispatch.go b/nmxact/nmble/dispatch.go
index 06d2847..40ec8f2 100644
--- a/nmxact/nmble/dispatch.go
+++ b/nmxact/nmble/dispatch.go
@@ -99,6 +99,7 @@ func connCancelRspCtor() BleMsg      { return 
&BleConnCancelRsp{} }
 func scanRspCtor() BleMsg            { return &BleScanRsp{} }
 func scanCancelRspCtor() BleMsg      { return &BleScanCancelRsp{} }
 func setPreferredMtuRspCtor() BleMsg { return &BleSetPreferredMtuRsp{} }
+func connFindRspCtor() BleMsg        { return &BleConnFindRsp{} }
 
 func syncEvtCtor() BleMsg       { return &BleSyncEvt{} }
 func connectEvtCtor() BleMsg    { return &BleConnectEvt{} }
@@ -125,6 +126,7 @@ var msgCtorMap = map[OpTypePair]msgCtor{
        {MSG_OP_RSP, MSG_TYPE_SCAN}:              scanRspCtor,
        {MSG_OP_RSP, MSG_TYPE_SCAN_CANCEL}:       scanCancelRspCtor,
        {MSG_OP_RSP, MSG_TYPE_SET_PREFERRED_MTU}: setPreferredMtuRspCtor,
+       {MSG_OP_RSP, MSG_TYPE_CONN_FIND}:         connFindRspCtor,
 
        {MSG_OP_EVT, MSG_TYPE_SYNC_EVT}:       syncEvtCtor,
        {MSG_OP_EVT, MSG_TYPE_CONNECT_EVT}:    connectEvtCtor,

Reply via email to