You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2017/10/30 21:56:14 UTC

[GitHub] mkiiskila closed pull request #48: mtu improvements when tx'ing

mkiiskila closed pull request #48: mtu improvements when tx'ing
URL: https://github.com/apache/mynewt-newtmgr/pull/48
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/nmxact/mtech_lora/listen.go b/nmxact/mtech_lora/listen.go
index 0bc6976..64dc945 100644
--- a/nmxact/mtech_lora/listen.go
+++ b/nmxact/mtech_lora/listen.go
@@ -48,6 +48,7 @@ func TypeKey(msgType string) ListenerKey {
 
 type Listener struct {
 	MsgChan chan []byte
+	MtuChan chan int
 	ErrChan chan error
 	TmoChan chan time.Time
 	Acked   bool
@@ -62,6 +63,7 @@ type Listener struct {
 func NewListener() *Listener {
 	return &Listener{
 		MsgChan: make(chan []byte, 16),
+		MtuChan: make(chan int, 1),
 		ErrChan: make(chan error, 1),
 		TmoChan: make(chan time.Time, 1),
 
@@ -97,6 +99,13 @@ func (ll *Listener) Close() {
 		}
 	}
 
+	close(ll.MtuChan)
+	for {
+		if _, ok := <-ll.MtuChan; !ok {
+			break
+		}
+	}
+
 	close(ll.ErrChan)
 	for {
 		if _, ok := <-ll.ErrChan; !ok {
diff --git a/nmxact/mtech_lora/mtech_lora_sesn.go b/nmxact/mtech_lora/mtech_lora_sesn.go
index a4f94b5..9bde337 100644
--- a/nmxact/mtech_lora/mtech_lora_sesn.go
+++ b/nmxact/mtech_lora/mtech_lora_sesn.go
@@ -27,6 +27,7 @@ import (
 	"strings"
 	"sync"
 
+	log "github.com/Sirupsen/logrus"
 	"github.com/joaojeronimo/go-crc16"
 	"github.com/runtimeco/go-coap"
 	"github.com/ugorji/go/codec"
@@ -43,6 +44,7 @@ type LoraSesn struct {
 	cfg      sesn.SesnCfg
 	txvr     *mgmt.Transceiver
 	isOpen   bool
+	mtu      int
 	xport    *LoraXport
 	listener *Listener
 	wg       sync.WaitGroup
@@ -70,6 +72,7 @@ func NewLoraSesn(cfg sesn.SesnCfg, lx *LoraXport) (*LoraSesn, error) {
 	s := &LoraSesn{
 		cfg:   cfg,
 		xport: lx,
+		mtu:   0,
 	}
 
 	return s, nil
@@ -110,6 +113,14 @@ func (s *LoraSesn) Open() error {
 				if ok {
 					s.txvr.DispatchCoap(msg)
 				}
+			case mtu, ok := <-s.listener.MtuChan:
+				if ok {
+					if s.mtu != mtu {
+						log.Debugf("Setting mtu for %s %d",
+							s.cfg.Lora.Addr, mtu)
+					}
+					s.mtu = mtu
+				}
 			case <-s.stopChan:
 				return
 			}
@@ -137,23 +148,37 @@ func (s *LoraSesn) Close() error {
 	return nil
 }
 
+func (s *LoraSesn) Mtu() int {
+	if s.cfg.Lora.SegSz != 0 {
+		return s.cfg.Lora.SegSz
+	}
+	if s.mtu != 0 {
+		return s.mtu
+	}
+	return s.xport.minMtu()
+}
+
 func (s *LoraSesn) IsOpen() bool {
 	return s.isOpen
 }
 
 func (s *LoraSesn) MtuIn() int {
-	return MAX_PACKET_SIZE - omp.OMP_MSG_OVERHEAD - nmp.NMP_HDR_SIZE
+	return MAX_PACKET_SIZE_IN - omp.OMP_MSG_OVERHEAD - nmp.NMP_HDR_SIZE
 }
 
 func (s *LoraSesn) MtuOut() int {
-	return MAX_PACKET_SIZE - omp.OMP_MSG_OVERHEAD - nmp.NMP_HDR_SIZE
+	// We want image upload to use chunk size which fits inside a single
+	// lora segment, when possible. If the datarate is low enough, then we have
+	// to fragment, but try to avoid it if possible.
+	mtu := MAX_PACKET_SIZE_OUT
+	if s.mtu > mtu {
+		mtu = s.mtu
+	}
+	return mtu - omp.OMP_MSG_OVERHEAD - nmp.NMP_HDR_SIZE
 }
 
 func (s *LoraSesn) sendFragments(b []byte) error {
-	segSz := s.xport.minMtu()
-	if segSz < s.cfg.Lora.SegSz {
-		segSz = s.cfg.Lora.SegSz
-	}
+	segSz := s.Mtu()
 	crc := crc16.Crc16(b)
 	idx := 0
 	for off := 0; off < len(b); {
diff --git a/nmxact/mtech_lora/mtech_lora_xport.go b/nmxact/mtech_lora/mtech_lora_xport.go
index 082faf8..4abfd29 100644
--- a/nmxact/mtech_lora/mtech_lora_xport.go
+++ b/nmxact/mtech_lora/mtech_lora_xport.go
@@ -64,7 +64,24 @@ type LoraData struct {
 	Port int    `codec:"port"`
 }
 
-const MAX_PACKET_SIZE = 2048
+type LoraPacketSent struct {
+	DataRate string `codec:"datr"`
+}
+
+/*
+ * This maps datarate string into a max mtu we can use
+ */
+var LoraDataRateMapUS = map[string]int{
+	"SF12BW500": 33,
+	"SF11BW500": 109,
+	"SF10BW500": 222,
+	"SF9BW500":  222,
+	"SF8BW500":  222,
+	"SF7BW500":  222,
+}
+
+const MAX_PACKET_SIZE_IN = 2048
+const MAX_PACKET_SIZE_OUT = 128
 const UDP_RX_PORT = 1784
 const UDP_TX_PORT = 1786
 const OIC_LORA_PORT = 0xbb
@@ -146,6 +163,21 @@ func (lx *LoraXport) reass(dev string, data []byte) {
 	}
 }
 
+func (lx *LoraXport) dataRateSeen(dev string, dataRate string) {
+	lx.Lock()
+	defer lx.Unlock()
+
+	_, l := lx.listenMap.FindListener(dev, "rx")
+	if l == nil {
+		return
+	}
+	mtu, ok := LoraDataRateMapUS[dataRate]
+	if !ok {
+		mtu = lx.minMtu()
+	}
+	l.MtuChan <- mtu
+}
+
 /*
  * lora/00-13-50-04-04-50-13-00/up
  */
@@ -187,6 +219,18 @@ func (lx *LoraXport) processData(data string) {
 			return
 		}
 		lx.reass(splitHdr[1], dec)
+	case "packet_sent":
+		var sent LoraPacketSent
+
+		log.Debugf("loraxport rx: %s", data)
+		pload := []byte(splitMsg[1])
+		err := codec.NewDecoderBytes(pload, new(codec.JsonHandle)).Decode(&sent)
+		if err != nil {
+			log.Debugf("loraxport rx: error decoding json: %v", err)
+			return
+		}
+
+		lx.dataRateSeen(splitHdr[1], sent.DataRate)
 	}
 }
 
@@ -224,7 +268,7 @@ func (lx *LoraXport) Start() error {
 	lx.started = true
 
 	go func() {
-		data := make([]byte, MAX_PACKET_SIZE*4/3+512)
+		data := make([]byte, MAX_PACKET_SIZE_IN*4/3+512)
 		for {
 			nr, _, err := lx.rxConn.ReadFromUDP(data)
 			if err != nil {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services