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/08/21 18:10:12 UTC

[mynewt-newtmgr] 01/02: nmxact - Fragment outgoing CoAP messages.

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 b5708d015a401c4a145e0f90290b6ca8f18649d1
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Mon Aug 21 11:05:25 2017 -0700

    nmxact - Fragment outgoing CoAP messages.
---
 nmxact/nmble/ble_oic_svr.go | 21 ++++++++++++++++--
 nmxact/nmble/ble_sesn.go    |  8 +++++++
 nmxact/nmble/ble_xport.go   | 53 +++++++++++++++++++++++++++++++++++----------
 3 files changed, 68 insertions(+), 14 deletions(-)

diff --git a/nmxact/nmble/ble_oic_svr.go b/nmxact/nmble/ble_oic_svr.go
index b892d3d..6dc609f 100644
--- a/nmxact/nmble/ble_oic_svr.go
+++ b/nmxact/nmble/ble_oic_svr.go
@@ -41,6 +41,12 @@ func (b *BleOicSvr) Rx(access BleGattAccess) uint8 {
 		return 0
 	}
 
+	s := b.x.findSesn(access.ConnHandle)
+	if s == nil {
+		// The sender is no longer connected.
+		return ERR_CODE_ATT_UNLIKELY
+	}
+
 	data, err := ml.MarshalBinary()
 	if err != nil {
 		return ERR_CODE_ATT_UNLIKELY
@@ -51,8 +57,19 @@ func (b *BleOicSvr) Rx(access BleGattAccess) uint8 {
 		return ERR_CODE_ATT_UNLIKELY
 	}
 
-	if err := NotifyXact(b.x, access.ConnHandle, valHandle, data); err != nil {
-		return ERR_CODE_ATT_UNLIKELY
+	mtu := s.MtuOut()
+	for off := 0; off < len(data); off += mtu {
+		chunkEnd := off + mtu
+		if chunkEnd > len(data) {
+			chunkEnd = len(data)
+		}
+		chunk := data[off:chunkEnd]
+
+		if err := NotifyXact(b.x, access.ConnHandle, valHandle,
+			chunk); err != nil {
+
+			return ERR_CODE_ATT_UNLIKELY
+		}
 	}
 
 	return 0
diff --git a/nmxact/nmble/ble_sesn.go b/nmxact/nmble/ble_sesn.go
index 2355c30..705c76b 100644
--- a/nmxact/nmble/ble_sesn.go
+++ b/nmxact/nmble/ble_sesn.go
@@ -91,6 +91,8 @@ func (s *BleSesn) disconnectListen() {
 		// If the session is being closed, unblock the close() call.
 		defer s.closeBlocker.Unblock(nil)
 
+		s.bx.removeSesn(s.conn.connHandle)
+
 		// Block until disconnect.
 		err := <-s.conn.DisconnectChan()
 		nmxutil.Assert(!s.IsOpen())
@@ -251,6 +253,10 @@ func (s *BleSesn) Open() error {
 		}
 	}
 
+	if err != nil {
+		s.bx.addSesn(s.conn.connHandle, s)
+	}
+
 	return err
 }
 
@@ -273,6 +279,8 @@ func (s *BleSesn) OpenConnected(
 	// Listen for incoming notifications in the background.
 	s.notifyListen()
 
+	s.bx.addSesn(connHandle, s)
+
 	return nil
 }
 
diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go
index 79a1f2c..03b158e 100644
--- a/nmxact/nmble/ble_xport.go
+++ b/nmxact/nmble/ble_xport.go
@@ -114,20 +114,22 @@ type BleXport struct {
 	master       nmxutil.SingleResource
 	slave        nmxutil.SingleResource
 	randAddr     *BleAddr
-	stateMtx     sync.Mutex
+	mtx          sync.Mutex
 	scanner      *BleScanner
 	advertiser   *Advertiser
 	cm           ChrMgr
+	sesns        map[uint16]*BleSesn
 }
 
 func NewBleXport(cfg XportCfg) (*BleXport, error) {
 	bx := &BleXport{
+		cfg:          cfg,
 		d:            NewDispatcher(),
 		shutdownChan: make(chan bool),
 		readyBcast:   nmxutil.Bcaster{},
 		master:       nmxutil.NewSingleResource(),
 		slave:        nmxutil.NewSingleResource(),
-		cfg:          cfg,
+		sesns:        map[uint16]*BleSesn{},
 	}
 
 	return bx, nil
@@ -258,7 +260,7 @@ func (bx *BleXport) shutdown(restart bool, err error) {
 
 	log.Debugf("Shutting down BLE transport")
 
-	bx.stateMtx.Lock()
+	bx.mtx.Lock()
 
 	var fullyStarted bool
 	var already bool
@@ -278,13 +280,15 @@ func (bx *BleXport) shutdown(restart bool, err error) {
 		bx.state = BLE_XPORT_STATE_STOPPING
 	}
 
-	bx.stateMtx.Unlock()
+	bx.mtx.Unlock()
 
 	if already {
 		// Shutdown already in progress.
 		return
 	}
 
+	bx.sesns = map[uint16]*BleSesn{}
+
 	// Indicate error to all clients who are waiting for the master resource.
 	log.Debugf("Aborting BLE master")
 	bx.master.Abort(err)
@@ -321,22 +325,22 @@ func (bx *BleXport) shutdown(restart bool, err error) {
 func (bx *BleXport) blockUntilReady() error {
 	var ch chan interface{}
 
-	bx.stateMtx.Lock()
+	bx.mtx.Lock()
 	switch bx.state {
 	case BLE_XPORT_STATE_STARTED:
 		// Already started; don't block.
-		bx.stateMtx.Unlock()
+		bx.mtx.Unlock()
 		return nil
 
 	case BLE_XPORT_STATE_DORMANT:
 		// Not in the process of starting; the user will be waiting forever.
-		bx.stateMtx.Unlock()
+		bx.mtx.Unlock()
 		return fmt.Errorf("Attempt to use BLE transport without starting it")
 
 	default:
 		ch = bx.readyBcast.Listen()
 	}
-	bx.stateMtx.Unlock()
+	bx.mtx.Unlock()
 
 	itf := <-ch
 	if itf == nil {
@@ -347,15 +351,15 @@ func (bx *BleXport) blockUntilReady() error {
 }
 
 func (bx *BleXport) getState() BleXportState {
-	bx.stateMtx.Lock()
-	defer bx.stateMtx.Unlock()
+	bx.mtx.Lock()
+	defer bx.mtx.Unlock()
 
 	return bx.state
 }
 
 func (bx *BleXport) setStateFrom(from BleXportState, to BleXportState) bool {
-	bx.stateMtx.Lock()
-	defer bx.stateMtx.Unlock()
+	bx.mtx.Lock()
+	defer bx.mtx.Unlock()
 
 	if bx.state != from {
 		return false
@@ -659,3 +663,28 @@ func (bx *BleXport) ReleaseSlave() {
 func (bx *BleXport) StopWaitingForSlave(token interface{}, err error) {
 	bx.slave.StopWaiting(token, err)
 }
+
+func (bx *BleXport) addSesn(connHandle uint16, s *BleSesn) {
+	bx.mtx.Lock()
+	defer bx.mtx.Unlock()
+
+	bx.sesns[connHandle] = s
+}
+
+func (bx *BleXport) removeSesn(connHandle uint16) *BleSesn {
+	bx.mtx.Lock()
+	defer bx.mtx.Unlock()
+
+	s := bx.sesns[connHandle]
+	if s != nil {
+		delete(bx.sesns, connHandle)
+	}
+	return s
+}
+
+func (bx *BleXport) findSesn(connHandle uint16) *BleSesn {
+	bx.mtx.Lock()
+	defer bx.mtx.Unlock()
+
+	return bx.sesns[connHandle]
+}

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