This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newtmgr.git

commit 3f081ba15711d55d875aecbb0c7cced9f283983c
Author: Christopher Collins <ccoll...@apache.org>
AuthorDate: Wed Aug 9 14:25:33 2017 -0700

    nmxact - public and secure CoAP services.
---
 newtmgr/bll/bll_sesn.go   | 77 +++++++++++++++++++++--------------------------
 nmxact/bledefs/bledefs.go | 27 ++++++++++++-----
 nmxact/nmble/ble_sesn.go  | 41 +++++++++++++++----------
 nmxact/nmble/ble_util.go  | 12 ++++++++
 4 files changed, 90 insertions(+), 67 deletions(-)

diff --git a/newtmgr/bll/bll_sesn.go b/newtmgr/bll/bll_sesn.go
index 0e272dd..18b4372 100644
--- a/newtmgr/bll/bll_sesn.go
+++ b/newtmgr/bll/bll_sesn.go
@@ -97,8 +97,13 @@ func (s *BllSesn) connect() error {
        return nil
 }
 
-func findChr(profile *ble.Profile, svcUuid bledefs.BleUuid,
-       chrUuid bledefs.BleUuid) (*ble.Characteristic, error) {
+func findChr(profile *ble.Profile, chrId *bledefs.BleChrId) (
+       *ble.Characteristic, error) {
+
+       if chrId == nil {
+               return nil, fmt.Errorf("BLE session not configured with 
required " +
+                       "characteristic")
+       }
 
        for _, s := range profile.Services {
                uuid, err := UuidFromBllUuid(s.UUID)
@@ -106,14 +111,14 @@ func findChr(profile *ble.Profile, svcUuid 
bledefs.BleUuid,
                        return nil, err
                }
 
-               if bledefs.CompareUuids(uuid, svcUuid) == 0 {
+               if bledefs.CompareUuids(uuid, chrId.SvcUuid) == 0 {
                        for _, c := range s.Characteristics {
                                uuid, err := UuidFromBllUuid(c.UUID)
                                if err != nil {
                                        return nil, err
                                }
 
-                               if bledefs.CompareUuids(uuid, chrUuid) == 0 {
+                               if bledefs.CompareUuids(uuid, chrId.ChrUuid) == 
0 {
                                        return c, nil
                                }
                        }
@@ -130,49 +135,19 @@ func (s *BllSesn) discoverAll() error {
                return err
        }
 
-       nmpSvcUuid, _ := bledefs.ParseUuid(bledefs.NmpPlainSvcUuid)
-       nmpChrUuid, _ := bledefs.ParseUuid(bledefs.NmpPlainChrUuid)
-
-       ompSvcUuid, _ := bledefs.ParseUuid(bledefs.OmpUnsecSvcUuid)
-       ompReqChrUuid, _ := bledefs.ParseUuid(bledefs.OmpUnsecReqChrUuid)
-       ompRspChrUuid, _ := bledefs.ParseUuid(bledefs.OmpUnsecRspChrUuid)
-
-       unauthSvcUuid, _ := bledefs.ParseUuid(bledefs.UnauthSvcUuid)
-       unauthReqChrUuid, _ := bledefs.ParseUuid(bledefs.UnauthReqChrUuid)
-       unauthRspChrUuid, _ := bledefs.ParseUuid(bledefs.UnauthRspChrUuid)
-
-       switch s.cfg.MgmtProto {
-       case sesn.MGMT_PROTO_NMP:
-               s.nmpReqChr, err = findChr(p, nmpSvcUuid, nmpChrUuid)
-               if err != nil {
-                       return err
-               }
-               s.nmpRspChr = s.nmpReqChr
-
-       case sesn.MGMT_PROTO_OMP:
-               s.nmpReqChr, err = findChr(p, ompSvcUuid, ompReqChrUuid)
-               if err != nil {
-                       return err
-               }
-
-               s.nmpRspChr, err = findChr(p, ompSvcUuid, ompRspChrUuid)
-               if err != nil {
-                       return err
-               }
-
-       default:
-               return fmt.Errorf("invalid management protocol: %s", 
s.cfg.MgmtProto)
-       }
-
-       s.unauthReqChr, err = findChr(p, unauthSvcUuid, unauthReqChrUuid)
+       mgmtChrs, err := nmble.BuildMgmtChrs(s.cfg.MgmtProto)
        if err != nil {
                return err
        }
 
-       s.unauthRspChr, err = findChr(p, unauthSvcUuid, unauthRspChrUuid)
-       if err != nil {
-               return err
-       }
+       s.nmpReqChr, _ = findChr(p, mgmtChrs.NmpReqChr)
+       s.nmpRspChr, _ = findChr(p, mgmtChrs.NmpRspChr)
+       s.publicReqChr, _ = findChr(p, mgmtChrs.ResPublicReqChr)
+       s.publicRspChr, _ = findChr(p, mgmtChrs.ResPublicRspChr)
+       s.unauthReqChr, _ = findChr(p, mgmtChrs.ResUnauthReqChr)
+       s.unauthRspChr, _ = findChr(p, mgmtChrs.ResUnauthRspChr)
+       s.secureReqChr, _ = findChr(p, mgmtChrs.ResSecureReqChr)
+       s.secureRspChr, _ = findChr(p, mgmtChrs.ResSecureRspChr)
 
        return nil
 }
@@ -192,6 +167,14 @@ func (s *BllSesn) subscribe() error {
                }
        }
 
+       if s.publicRspChr != nil {
+               if err := s.cln.Subscribe(s.publicRspChr, false,
+                       onNotify); err != nil {
+
+                       return err
+               }
+       }
+
        if s.unauthRspChr != nil {
                if err := s.cln.Subscribe(s.unauthRspChr, false,
                        onNotify); err != nil {
@@ -200,6 +183,14 @@ func (s *BllSesn) subscribe() error {
                }
        }
 
+       if s.secureRspChr != nil {
+               if err := s.cln.Subscribe(s.secureRspChr, false,
+                       onNotify); err != nil {
+
+                       return err
+               }
+       }
+
        return nil
 }
 
diff --git a/nmxact/bledefs/bledefs.go b/nmxact/bledefs/bledefs.go
index fe0322a..780587e 100644
--- a/nmxact/bledefs/bledefs.go
+++ b/nmxact/bledefs/bledefs.go
@@ -33,6 +33,18 @@ const BLE_ATT_MTU_DFLT = 23
 
 const CccdUuid = 0x2902
 
+const PublicSvcUuid = "40e32721-9153-43a3-9ab3-ee7d4c84fcb2"
+const PublicReqChrUuid = "223387b6-63e6-4d16-8a58-988542253a54"
+const PublicRspChrUuid = "cb9564db-184c-4b9d-b221-6362679cad10"
+
+const UnauthSvcUuid = "0c08c213-98ed-4e43-a499-7e1137c39567"
+const UnauthReqChrUuid = "69b8a928-2ab2-487b-923e-54ce53a18bc1"
+const UnauthRspChrUuid = "bca10aea-5df1-4248-b72b-f52955ad9c88"
+
+const SecureSvcUuid = 0xfe18
+const SecureReqChrUuid = 0x1000
+const SecureRspChrUuid = 0x1001
+
 const NmpPlainSvcUuid = "8d53dc1d-1db7-4cd3-868b-8a527460aa84"
 const NmpPlainChrUuid = "da2e7828-fbce-4e01-ae9e-261174997c48"
 
@@ -40,13 +52,9 @@ const OmpUnsecSvcUuid = 
"ade3d529-c784-4f63-a987-eb69f70ee816"
 const OmpUnsecReqChrUuid = "ad7b334f-4637-4b86-90b6-9d787f03d218"
 const OmpUnsecRspChrUuid = "e9241982-4580-42c4-8831-95048216b256"
 
-const OmpSecSvcUuid = 0xfe18
-const OmpSecReqChrUuid = 0x1000
-const OmpSecRspChrUuid = 0x1001
-
-const UnauthSvcUuid = "0c08c213-98ed-4e43-a499-7e1137c39567"
-const UnauthReqChrUuid = "69b8a928-2ab2-487b-923e-54ce53a18bc1"
-const UnauthRspChrUuid = "bca10aea-5df1-4248-b72b-f52955ad9c88"
+const OmpSecSvcUuid = SecureSvcUuid
+const OmpSecReqChrUuid = SecureReqChrUuid
+const OmpSecRspChrUuid = SecureRspChrUuid
 
 type BleAddrType int
 
@@ -252,7 +260,6 @@ type BleUuid struct {
        // Set to 0 if the 128-bit UUID should be used.
        U16 BleUuid16
 
-       // Set to nil if the 16-bit UUID should be used.
        U128 BleUuid128
 }
 
@@ -283,6 +290,10 @@ func ParseUuid(uuidStr string) (BleUuid, error) {
        return bu, err
 }
 
+func NewBleUuid16(uuid16 uint16) BleUuid {
+       return BleUuid{BleUuid16(uuid16), BleUuid128{}}
+}
+
 func (bu *BleUuid) MarshalJSON() ([]byte, error) {
        if bu.U16 != 0 {
                return json.Marshal(bu.U16)
diff --git a/nmxact/nmble/ble_sesn.go b/nmxact/nmble/ble_sesn.go
index a35cebe..5f03153 100644
--- a/nmxact/nmble/ble_sesn.go
+++ b/nmxact/nmble/ble_sesn.go
@@ -23,6 +23,7 @@ import (
        "fmt"
        "sync"
 
+       log "github.com/Sirupsen/logrus"
        "github.com/runtimeco/go-coap"
 
        . "mynewt.apache.org/newtmgr/nmxact/bledefs"
@@ -33,8 +34,6 @@ import (
        "mynewt.apache.org/newtmgr/nmxact/sesn"
 )
 
-var dummyNotifyListener = NewNotifyListener()
-
 type BleSesn struct {
        cfg      sesn.SesnCfg
        bx       *BleXport
@@ -124,22 +123,25 @@ func (s *BleSesn) getChr(chrId *BleChrId) 
(*Characteristic, error) {
        return chr, nil
 }
 
-func (s *BleSesn) createNotifyListener(chrId *BleChrId) *NotifyListener {
-       chr, _ := s.getChr(chrId)
-       if chr == nil {
-               return dummyNotifyListener
-       }
+func (s *BleSesn) createNotifyListener(chrId *BleChrId) (
+       *NotifyListener, error) {
 
-       nl := s.conn.ListenForNotifications(chr)
-       if nl == nil {
-               return dummyNotifyListener
+       chr, err := s.getChr(chrId)
+       if err != nil {
+               return nil, err
        }
 
-       return nl
+       return s.conn.ListenForNotifications(chr), nil
 }
 
-func (s *BleSesn) notifyListen() {
-       nmpRspNl := s.createNotifyListener(s.mgmtChrs.NmpRspChr)
+func (s *BleSesn) notifyListenOnce(chrId *BleChrId,
+       dispatchCb func(b []byte)) {
+
+       nl, err := s.createNotifyListener(chrId)
+       if err != nil {
+               log.Debugf("error listening for notifications: %s", err.Error())
+               return
+       }
 
        s.wg.Add(1)
        go func() {
@@ -147,14 +149,14 @@ func (s *BleSesn) notifyListen() {
 
                for {
                        select {
-                       case <-nmpRspNl.ErrChan:
+                       case <-nl.ErrChan:
                                return
 
-                       case n, ok := <-nmpRspNl.NotifyChan:
+                       case n, ok := <-nl.NotifyChan:
                                if !ok {
                                        return
                                }
-                               s.txvr.DispatchNmpRsp(n.Data)
+                               dispatchCb(n.Data)
 
                        case <-s.stopChan:
                                return
@@ -163,6 +165,13 @@ func (s *BleSesn) notifyListen() {
        }()
 }
 
+func (s *BleSesn) notifyListen() {
+       s.notifyListenOnce(s.mgmtChrs.NmpRspChr, s.txvr.DispatchNmpRsp)
+       s.notifyListenOnce(s.mgmtChrs.ResPublicRspChr, s.txvr.DispatchCoap)
+       s.notifyListenOnce(s.mgmtChrs.ResUnauthRspChr, s.txvr.DispatchCoap)
+       s.notifyListenOnce(s.mgmtChrs.ResSecureRspChr, s.txvr.DispatchCoap)
+}
+
 func (s *BleSesn) openOnce() (bool, error) {
        if s.IsOpen() {
                return false, nmxutil.NewSesnAlreadyOpenError(
diff --git a/nmxact/nmble/ble_util.go b/nmxact/nmble/ble_util.go
index 470db01..8a11ec0 100644
--- a/nmxact/nmble/ble_util.go
+++ b/nmxact/nmble/ble_util.go
@@ -765,10 +765,18 @@ func BuildMgmtChrs(mgmtProto sesn.MgmtProto) 
(BleMgmtChrs, error) {
        ompReqChrUuid, _ := ParseUuid(OmpUnsecReqChrUuid)
        ompRspChrUuid, _ := ParseUuid(OmpUnsecRspChrUuid)
 
+       publicSvcUuid, _ := ParseUuid(PublicSvcUuid)
+       publicReqChrUuid, _ := ParseUuid(PublicReqChrUuid)
+       publicRspChrUuid, _ := ParseUuid(PublicRspChrUuid)
+
        unauthSvcUuid, _ := ParseUuid(UnauthSvcUuid)
        unauthReqChrUuid, _ := ParseUuid(UnauthReqChrUuid)
        unauthRspChrUuid, _ := ParseUuid(UnauthRspChrUuid)
 
+       secureSvcUuid := NewBleUuid16(SecureSvcUuid)
+       secureReqChrUuid := NewBleUuid16(SecureReqChrUuid)
+       secureRspChrUuid := NewBleUuid16(SecureRspChrUuid)
+
        switch mgmtProto {
        case sesn.MGMT_PROTO_NMP:
                mgmtChrs.NmpReqChr = &BleChrId{nmpSvcUuid, nmpChrUuid}
@@ -783,8 +791,12 @@ func BuildMgmtChrs(mgmtProto sesn.MgmtProto) (BleMgmtChrs, 
error) {
                        fmt.Errorf("invalid management protocol: %+v", 
mgmtProto)
        }
 
+       mgmtChrs.ResPublicReqChr = &BleChrId{publicSvcUuid, publicReqChrUuid}
+       mgmtChrs.ResPublicRspChr = &BleChrId{publicSvcUuid, publicRspChrUuid}
        mgmtChrs.ResUnauthReqChr = &BleChrId{unauthSvcUuid, unauthReqChrUuid}
        mgmtChrs.ResUnauthRspChr = &BleChrId{unauthSvcUuid, unauthRspChrUuid}
+       mgmtChrs.ResSecureReqChr = &BleChrId{secureSvcUuid, secureReqChrUuid}
+       mgmtChrs.ResSecureRspChr = &BleChrId{secureSvcUuid, secureRspChrUuid}
 
        return mgmtChrs, nil
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@mynewt.apache.org" <commits@mynewt.apache.org>.

Reply via email to