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/07 20:14:23 UTC

[mynewt-newtmgr] 01/06: nmxact - CoAP fixed resources (server)

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 e63d52560576910597f176fa0d99d314296092e4
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Aug 4 15:29:48 2017 -0700

    nmxact - CoAP fixed resources (server)
---
 nmxact/example/ble_adv/ble_adv | Bin 13981316 -> 14030132 bytes
 nmxact/nmble/temp.go           |  44 +++++++++++++++++++++++++++
 nmxact/oic/resmgr.go           |  67 +++++++++++++++++++++++++++++++----------
 3 files changed, 95 insertions(+), 16 deletions(-)

diff --git a/nmxact/example/ble_adv/ble_adv b/nmxact/example/ble_adv/ble_adv
index 62b93a0..d57f915 100755
Binary files a/nmxact/example/ble_adv/ble_adv and b/nmxact/example/ble_adv/ble_adv differ
diff --git a/nmxact/nmble/temp.go b/nmxact/nmble/temp.go
new file mode 100644
index 0000000..2c19fb5
--- /dev/null
+++ b/nmxact/nmble/temp.go
@@ -0,0 +1,44 @@
+package nmble
+
+import (
+	"github.com/runtimeco/go-coap"
+
+	. "mynewt.apache.org/newtmgr/nmxact/bledefs"
+	"mynewt.apache.org/newtmgr/nmxact/nmxutil"
+	"mynewt.apache.org/newtmgr/nmxact/oic"
+)
+
+func GwService(x *BleXport) (BleSvc, error) {
+	svcUuid, _ := ParseUuid(UnauthSvcUuid)
+	reqChrUuid, _ := ParseUuid(UnauthReqChrUuid)
+	rspChrUuid, _ := ParseUuid(UnauthRspChrUuid)
+
+	resources := []oic.Resource{
+		oic.NewFixedResource(
+			"mynewt.yourmom",
+			map[string]interface{}{"yourmom": "fat"},
+			func(val map[string]interface{}) coap.COAPCode {
+				return coap.Changed
+			},
+		)}
+
+	return GenCoapService(x, svcUuid, reqChrUuid, rspChrUuid, resources)
+}
+
+func SetAllServices(x *BleXport) error {
+	gwSvc, err := GwService(x)
+	if err != nil {
+		return nmxutil.NewXportError(err.Error())
+	}
+
+	svcs := []BleSvc{
+		GapService("gwadv"),
+		GattService(),
+		gwSvc,
+	}
+	if err := x.cm.SetServices(x, svcs); err != nil {
+		return nmxutil.NewXportError(err.Error())
+	}
+
+	return nil
+}
diff --git a/nmxact/oic/resmgr.go b/nmxact/oic/resmgr.go
index 467204d..c6d9e6d 100644
--- a/nmxact/oic/resmgr.go
+++ b/nmxact/oic/resmgr.go
@@ -5,34 +5,35 @@ import (
 
 	log "github.com/Sirupsen/logrus"
 	"github.com/runtimeco/go-coap"
+
+	"mynewt.apache.org/newtmgr/nmxact/nmxutil"
 )
 
-type ResWriteFn func(uri string, data []byte) coap.COAPCode
-type ResReadFn func(uri string, data []byte) (coap.COAPCode, []byte)
+type ResGetFn func(uri string) (coap.COAPCode, []byte)
+type ResPutFn func(uri string, data []byte) coap.COAPCode
 
 type Resource struct {
-	Name    string
-	WriteCb ResWriteFn
-	ReadCb  ResReadFn
+	Uri   string
+	GetCb ResGetFn
+	PutCb ResPutFn
 }
 
 type ResMgr struct {
-	nameResMap map[string]Resource
+	uriResMap map[string]Resource
 }
 
 func NewResMgr() ResMgr {
 	return ResMgr{
-		nameResMap: map[string]Resource{},
+		uriResMap: map[string]Resource{},
 	}
 }
 
 func (rm *ResMgr) Add(r Resource) error {
-	if _, ok := rm.nameResMap[r.Name]; ok {
-		return fmt.Errorf("Registration of duplicate CoAP resource: %s",
-			r.Name)
+	if _, ok := rm.uriResMap[r.Uri]; ok {
+		return fmt.Errorf("Registration of duplicate CoAP resource: %s", r.Uri)
 	}
 
-	rm.nameResMap[r.Name] = r
+	rm.uriResMap[r.Uri] = r
 	return nil
 }
 
@@ -44,7 +45,7 @@ func (rm *ResMgr) Access(m coap.Message) (coap.COAPCode, []byte) {
 	}
 	path := paths[0]
 
-	r, ok := rm.nameResMap[path]
+	r, ok := rm.uriResMap[path]
 	if !ok {
 		log.Debugf("Incoming CoAP message specifies unknown resource: %s", path)
 		return coap.NotFound, nil
@@ -52,17 +53,17 @@ func (rm *ResMgr) Access(m coap.Message) (coap.COAPCode, []byte) {
 
 	switch m.Code() {
 	case coap.GET:
-		if r.ReadCb == nil {
+		if r.GetCb == nil {
 			return coap.MethodNotAllowed, nil
 		} else {
-			return r.ReadCb(path, m.Payload())
+			return r.GetCb(path)
 		}
 
 	case coap.PUT:
-		if r.WriteCb == nil {
+		if r.PutCb == nil {
 			return coap.MethodNotAllowed, nil
 		} else {
-			return r.WriteCb(path, m.Payload()), nil
+			return r.PutCb(path, m.Payload()), nil
 		}
 
 	default:
@@ -71,3 +72,37 @@ func (rm *ResMgr) Access(m coap.Message) (coap.COAPCode, []byte) {
 		return coap.MethodNotAllowed, nil
 	}
 }
+
+type FixedResourceWriteFn func(val map[string]interface{}) coap.COAPCode
+
+func NewFixedResource(uri string, val map[string]interface{},
+	writeCb FixedResourceWriteFn) Resource {
+
+	return Resource{
+		Uri: uri,
+
+		GetCb: func(uri string) (coap.COAPCode, []byte) {
+			b, err := nmxutil.EncodeCborMap(val)
+			if err != nil {
+				return coap.InternalServerError, nil
+			}
+			return coap.Content, b
+		},
+
+		PutCb: func(uri string, data []byte) coap.COAPCode {
+			m, err := nmxutil.DecodeCborMap(data)
+			if err != nil {
+				return coap.BadRequest
+			}
+
+			code := writeCb(m)
+			if code == coap.Created ||
+				code == coap.Deleted ||
+				code == coap.Changed {
+
+				val = m
+			}
+			return code
+		},
+	}
+}

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