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/03/28 22:42:02 UTC
[65/67] [abbrv] incubator-mynewt-newtmgr git commit: newtmgr - use
newtmgr repo, not newt repo.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/dispatch.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/dispatch.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/dispatch.go
deleted file mode 100644
index fb25f98..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/dispatch.go
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package omp
-
-import (
- log "github.com/Sirupsen/logrus"
-
- "mynewt.apache.org/newt/nmxact/nmp"
-)
-
-type OmpDispatcher struct {
- nd *nmp.NmpDispatcher
- reassembler *Reassembler
-}
-
-func NewOmpDispatcher() *OmpDispatcher {
- return &OmpDispatcher{
- nd: nmp.NewNmpDispatcher(),
- reassembler: NewReassembler(),
- }
-}
-
-func (od *OmpDispatcher) AddListener(seq uint8, rl *nmp.NmpListener) error {
- return od.nd.AddListener(seq, rl)
-}
-
-func (od *OmpDispatcher) RemoveListener(seq uint8) *nmp.NmpListener {
- return od.nd.RemoveListener(seq)
-}
-
-func (od *OmpDispatcher) FakeRxError(seq uint8, err error) error {
- return od.nd.FakeRxError(seq, err)
-}
-
-// Returns true if the response was dispatched.
-func (om *OmpDispatcher) Dispatch(data []byte) bool {
- tm := om.reassembler.RxFrag(data)
- if tm == nil {
- return false
- }
-
- r, err := DecodeOmpTcp(tm)
- if err != nil {
- log.Printf("OMP decode failure: %s", err.Error())
- return false
- }
-
- return om.nd.DispatchRsp(r)
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/frag.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/frag.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/frag.go
deleted file mode 100644
index caf7f48..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/frag.go
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package omp
-
-import (
- log "github.com/Sirupsen/logrus"
- "github.com/runtimeco/go-coap"
-)
-
-type Reassembler struct {
- cur []byte
-}
-
-func NewReassembler() *Reassembler {
- return &Reassembler{}
-}
-
-func (r *Reassembler) RxFrag(frag []byte) *coap.TcpMessage {
- r.cur = append(r.cur, frag...)
-
- var tm *coap.TcpMessage
- var err error
- tm, r.cur, err = coap.PullTcp(r.cur)
- if err != nil {
- log.Debugf("received invalid CoAP-TCP packet: %s", err.Error())
- return nil
- }
-
- return tm
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/omp.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/omp.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/omp.go
deleted file mode 100644
index 5bed071..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/omp/omp.go
+++ /dev/null
@@ -1,125 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package omp
-
-import (
- "encoding/hex"
- "fmt"
-
- log "github.com/Sirupsen/logrus"
- "github.com/fatih/structs"
- "github.com/runtimeco/go-coap"
- "github.com/ugorji/go/codec"
-
- "mynewt.apache.org/newt/nmxact/nmp"
-)
-
-// OIC wrapping adds this many bytes to an NMP message.
-const OMP_MSG_OVERHEAD = 13
-
-type OicMsg struct {
- Hdr []byte `codec:"_h"`
-}
-
-/*
- * Not able to install custom decoder for indefite length objects with the
- * codec. So we need to decode the whole response, and then re-encode the
- * newtmgr response part.
- */
-func DecodeOmpTcp(tm *coap.TcpMessage) (nmp.NmpRsp, error) {
- // Ignore non-responses.
- if tm.Code == coap.GET || tm.Code == coap.PUT {
- return nil, nil
- }
-
- if tm.Code != coap.Created && tm.Code != coap.Deleted &&
- tm.Code != coap.Valid && tm.Code != coap.Changed &&
- tm.Code != coap.Content {
- return nil, fmt.Errorf(
- "OMP response specifies unexpected code: %d (%s)", int(tm.Code),
- tm.Code.String())
- }
-
- var om OicMsg
- err := codec.NewDecoderBytes(tm.Payload, new(codec.CborHandle)).Decode(&om)
- if err != nil {
- return nil, fmt.Errorf("Invalid incoming cbor: %s", err.Error())
- }
- if om.Hdr == nil {
- return nil, fmt.Errorf("Invalid incoming OMP response; NMP header" +
- "missing")
- }
-
- hdr, err := nmp.DecodeNmpHdr(om.Hdr)
- if err != nil {
- return nil, err
- }
-
- rsp, err := nmp.DecodeRspBody(hdr, tm.Payload)
- if err != nil {
- return nil, fmt.Errorf("Error decoding OMP response: %s", err.Error())
- }
- if rsp == nil {
- return nil, nil
- }
-
- return rsp, nil
-}
-
-func EncodeOmpTcp(nmr *nmp.NmpMsg) ([]byte, error) {
- req := coap.TcpMessage{
- Message: coap.Message{
- Type: coap.Confirmable,
- Code: coap.PUT,
- },
- }
- req.SetPathString("/omgr")
-
- payload := []byte{}
- enc := codec.NewEncoderBytes(&payload, new(codec.CborHandle))
-
- fields := structs.Fields(nmr.Body)
- m := make(map[string]interface{}, len(fields))
- for _, f := range fields {
- if cname := f.Tag("codec"); cname != "" {
- m[cname] = f.Value()
- }
- }
-
- // Add the NMP heder to the OMP response map.
- hbytes := nmr.Hdr.Bytes()
- m["_h"] = hbytes
-
- if err := enc.Encode(m); err != nil {
- return nil, err
- }
- req.Payload = payload
-
- data, err := req.MarshalBinary()
- if err != nil {
- return nil, fmt.Errorf("Failed to encode: %s\n", err.Error())
- }
-
- log.Debugf("Serialized OMP request:\n"+
- "Hdr %+v:\n%s\nPayload:%s\nData:\n%s",
- nmr.Hdr, hex.Dump(hbytes), hex.Dump(req.Payload), hex.Dump(data))
-
- return data, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn.go
deleted file mode 100644
index f5bbd3c..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn.go
+++ /dev/null
@@ -1,79 +0,0 @@
-package sesn
-
-import (
- "time"
-
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/nmxutil"
-)
-
-type TxOptions struct {
- Timeout time.Duration
- Tries int
-}
-
-func (opt *TxOptions) AfterTimeout() <-chan time.Time {
- if opt.Timeout == 0 {
- return nil
- } else {
- return time.After(opt.Timeout)
- }
-}
-
-// Represents a communication session with a specific peer. The particulars
-// vary according to protocol and transport. Several Sesn instances can use the
-// same Xport.
-type Sesn interface {
- // Initiates communication with the peer. For connection-oriented
- // transports, this creates a connection.
- // Returns:
- // * nil: success.
- // * nmxutil.SesnAlreadyOpenError: session already open.
- // * other error
- Open() error
-
- // Ends communication with the peer. For connection-oriented transports,
- // this closes the connection.
- // * nil: success.
- // * nmxutil.SesnClosedError: session not open.
- // * other error
- Close() error
-
- // Indicates whether the session is currently open.
- IsOpen() bool
-
- // Retrieves the maximum data payload for outgoing NMP requests.
- MtuOut() int
-
- // Retrieves the maximum data payload for incoming NMP responses.
- MtuIn() int
-
- EncodeNmpMsg(msg *nmp.NmpMsg) ([]byte, error)
-
- // Performs a blocking transmit a single NMP message and listens for the
- // response.
- // * nil: success.
- // * nmxutil.SesnClosedError: session not open.
- // * other error
- TxNmpOnce(m *nmp.NmpMsg, opt TxOptions) (nmp.NmpRsp, error)
-
- // Stops a receive operation in progress. This must be called from a
- // separate thread, as sesn receive operations are blocking.
- AbortRx(nmpSeq uint8) error
-}
-
-func TxNmp(s Sesn, m *nmp.NmpMsg, o TxOptions) (nmp.NmpRsp, error) {
- retries := o.Tries - 1
- for i := 0; ; i++ {
- r, err := s.TxNmpOnce(m, o)
- if err == nil {
- return r, nil
- }
-
- if (!nmxutil.IsNmpTimeout(err) && !nmxutil.IsXportTimeout(err)) ||
- i >= retries {
-
- return nil, err
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn_cfg.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn_cfg.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn_cfg.go
deleted file mode 100644
index 9e07c71..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/sesn/sesn_cfg.go
+++ /dev/null
@@ -1,65 +0,0 @@
-package sesn
-
-import (
- "time"
-
- "mynewt.apache.org/newt/nmxact/bledefs"
-)
-
-type MgmtProto int
-
-const (
- MGMT_PROTO_NMP MgmtProto = iota
- MGMT_PROTO_OMP
-)
-
-type BleOnCloseFn func(s Sesn, peer bledefs.BleDev, err error)
-
-// Specifies the BLE peer to connect to.
-type BlePeerSpec struct {
- // This is filled in if you know the address of the peer to connect to.
- Dev bledefs.BleDev
-
- // Otherwise, we must scan for a peer to connect to. This points to a
- // function that indicates whether we should connect to the sender of the
- // specified advertisement. This function gets called each time an
- // incoming advertisement is received. If it returns true, the session
- // will connect to the sender of the corresponding advertisement. Set this
- // to nil if you populate the Dev field.
- ScanPred bledefs.BleAdvPredicate
-}
-
-func BlePeerSpecDev(dev bledefs.BleDev) BlePeerSpec {
- return BlePeerSpec{Dev: dev}
-}
-
-func BlePeerSpecName(name string) BlePeerSpec {
- return BlePeerSpec{
- ScanPred: func(r bledefs.BleAdvReport) bool {
- return r.Name == name
- },
- }
-}
-
-type SesnCfgBle struct {
- OwnAddrType bledefs.BleAddrType
- PeerSpec BlePeerSpec
- CloseTimeout time.Duration
- OnCloseCb BleOnCloseFn
-}
-
-type SesnCfg struct {
- // Used with all transport types.
- MgmtProto MgmtProto
-
- // Only used with BLE transports.
- Ble SesnCfgBle
-}
-
-func NewSesnCfg() SesnCfg {
- return SesnCfg{
- Ble: SesnCfgBle{
- CloseTimeout: 5 * time.Second,
- },
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/cmd.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/cmd.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/cmd.go
deleted file mode 100644
index f42b2df..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/cmd.go
+++ /dev/null
@@ -1,65 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package xact
-
-import (
- "fmt"
-
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-type Result interface {
- Status() int
-}
-
-type Cmd interface {
- // Transmits request and listens for response; blocking.
- Run(s sesn.Sesn) (Result, error)
- Abort() error
-
- TxOptions() sesn.TxOptions
- SetTxOptions(opt sesn.TxOptions)
-}
-
-type CmdBase struct {
- txOptions sesn.TxOptions
- curNmpSeq uint8
- curSesn sesn.Sesn
- abortErr error
-}
-
-func (c *CmdBase) TxOptions() sesn.TxOptions {
- return c.txOptions
-}
-
-func (c *CmdBase) SetTxOptions(opt sesn.TxOptions) {
- c.txOptions = opt
-}
-
-func (c *CmdBase) Abort() error {
- if c.curSesn != nil {
- if err := c.curSesn.AbortRx(c.curNmpSeq); err != nil {
- return err
- }
- }
-
- c.abortErr = fmt.Errorf("Command aborted")
- return nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/config.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/config.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/config.go
deleted file mode 100644
index a0d2f70..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/config.go
+++ /dev/null
@@ -1,88 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-//////////////////////////////////////////////////////////////////////////////
-// $read //
-//////////////////////////////////////////////////////////////////////////////
-
-type ConfigReadCmd struct {
- CmdBase
- Name string
-}
-
-func NewConfigReadCmd() *ConfigReadCmd {
- return &ConfigReadCmd{}
-}
-
-type ConfigReadResult struct {
- Rsp *nmp.ConfigReadRsp
-}
-
-func newConfigReadResult() *ConfigReadResult {
- return &ConfigReadResult{}
-}
-
-func (r *ConfigReadResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *ConfigReadCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewConfigReadReq()
- r.Name = c.Name
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.ConfigReadRsp)
-
- res := newConfigReadResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $write //
-//////////////////////////////////////////////////////////////////////////////
-
-type ConfigWriteCmd struct {
- CmdBase
- Name string
- Val string
-}
-
-func NewConfigWriteCmd() *ConfigWriteCmd {
- return &ConfigWriteCmd{}
-}
-
-type ConfigWriteResult struct {
- Rsp *nmp.ConfigWriteRsp
-}
-
-func newConfigWriteResult() *ConfigWriteResult {
- return &ConfigWriteResult{}
-}
-
-func (r *ConfigWriteResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *ConfigWriteCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewConfigWriteReq()
- r.Name = c.Name
- r.Val = c.Val
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.ConfigWriteRsp)
-
- res := newConfigWriteResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/crash.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/crash.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/crash.go
deleted file mode 100644
index 002e277..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/crash.go
+++ /dev/null
@@ -1,87 +0,0 @@
-package xact
-
-import (
- "fmt"
- "sort"
-
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-type CrashType int
-
-const (
- CRASH_TYPE_DIV0 CrashType = iota
- CRASH_TYPE_JUMP0
- CRASH_TYPE_REF0
- CRASH_TYPE_ASSERT
- CRASH_TYPE_WDOG
-)
-
-var CrashTypeNameMap = map[CrashType]string{
- CRASH_TYPE_DIV0: "div0",
- CRASH_TYPE_JUMP0: "jump0",
- CRASH_TYPE_REF0: "ref0",
- CRASH_TYPE_ASSERT: "assert",
- CRASH_TYPE_WDOG: "wdog",
-}
-
-func CrashTypeToString(ct CrashType) string {
- return CrashTypeNameMap[ct]
-}
-
-func CrashTypeFromString(s string) (CrashType, error) {
- for k, v := range CrashTypeNameMap {
- if s == v {
- return k, nil
- }
- }
-
- return CrashType(0), fmt.Errorf("invalid crash type: %s", s)
-}
-
-func CrashTypeNames() []string {
- names := make([]string, 0, len(CrashTypeNameMap))
- for _, v := range CrashTypeNameMap {
- names = append(names, v)
- }
-
- sort.Strings(names)
- return names
-}
-
-type CrashCmd struct {
- CmdBase
- CrashType CrashType
-}
-
-func NewCrashCmd() *CrashCmd {
- return &CrashCmd{}
-}
-
-type CrashResult struct {
- Rsp *nmp.CrashRsp
-}
-
-func newCrashResult() *CrashResult {
- return &CrashResult{}
-}
-
-func (r *CrashResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *CrashCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewCrashReq()
- r.CrashType = CrashTypeToString(c.CrashType)
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.CrashRsp)
-
- res := newCrashResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/datetime.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/datetime.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/datetime.go
deleted file mode 100644
index 09738f5..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/datetime.go
+++ /dev/null
@@ -1,84 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-///////////////////////////////////////////////////////////////////////////////
-// $read //
-///////////////////////////////////////////////////////////////////////////////
-
-type DateTimeReadCmd struct {
- CmdBase
-}
-
-func NewDateTimeReadCmd() *DateTimeReadCmd {
- return &DateTimeReadCmd{}
-}
-
-type DateTimeReadResult struct {
- Rsp *nmp.DateTimeReadRsp
-}
-
-func newDateTimeReadResult() *DateTimeReadResult {
- return &DateTimeReadResult{}
-}
-
-func (r *DateTimeReadResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *DateTimeReadCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewDateTimeReadReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.DateTimeReadRsp)
-
- res := newDateTimeReadResult()
- res.Rsp = srsp
- return res, nil
-}
-
-///////////////////////////////////////////////////////////////////////////////
-// $write //
-///////////////////////////////////////////////////////////////////////////////
-
-type DateTimeWriteCmd struct {
- CmdBase
- DateTime string
-}
-
-func NewDateTimeWriteCmd() *DateTimeWriteCmd {
- return &DateTimeWriteCmd{}
-}
-
-type DateTimeWriteResult struct {
- Rsp *nmp.DateTimeWriteRsp
-}
-
-func newDateTimeWriteResult() *DateTimeWriteResult {
- return &DateTimeWriteResult{}
-}
-
-func (r *DateTimeWriteResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *DateTimeWriteCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewDateTimeWriteReq()
- r.DateTime = c.DateTime
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.DateTimeWriteRsp)
-
- res := newDateTimeWriteResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/echo.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/echo.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/echo.go
deleted file mode 100644
index 0be55b4..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/echo.go
+++ /dev/null
@@ -1,42 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-type EchoCmd struct {
- CmdBase
- Payload string
-}
-
-func NewEchoCmd() *EchoCmd {
- return &EchoCmd{}
-}
-
-type EchoResult struct {
- Rsp *nmp.EchoRsp
-}
-
-func newEchoResult() *EchoResult {
- return &EchoResult{}
-}
-
-func (r *EchoResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *EchoCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewEchoReq()
- r.Payload = c.Payload
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.EchoRsp)
-
- res := newEchoResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/fs.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/fs.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/fs.go
deleted file mode 100644
index 9f150fa..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/fs.go
+++ /dev/null
@@ -1,178 +0,0 @@
-package xact
-
-import (
- "fmt"
-
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-//////////////////////////////////////////////////////////////////////////////
-// $download //
-//////////////////////////////////////////////////////////////////////////////
-
-type FsDownloadProgressCb func(c *FsDownloadCmd, r *nmp.FsDownloadRsp)
-type FsDownloadCmd struct {
- CmdBase
- Name string
- ProgressCb FsDownloadProgressCb
-}
-
-func NewFsDownloadCmd() *FsDownloadCmd {
- return &FsDownloadCmd{}
-}
-
-type FsDownloadResult struct {
- Rsps []*nmp.FsDownloadRsp
-}
-
-func newFsDownloadResult() *FsDownloadResult {
- return &FsDownloadResult{}
-}
-
-func (r *FsDownloadResult) Status() int {
- rsp := r.Rsps[len(r.Rsps)-1]
- return rsp.Rc
-}
-
-func (c *FsDownloadCmd) Run(s sesn.Sesn) (Result, error) {
- res := newFsDownloadResult()
- off := 0
-
- for {
- r := nmp.NewFsDownloadReq()
- r.Name = c.Name
- r.Off = uint32(off)
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- frsp := rsp.(*nmp.FsDownloadRsp)
- res.Rsps = append(res.Rsps, frsp)
-
- if frsp.Rc != 0 {
- break
- }
-
- if c.ProgressCb != nil {
- c.ProgressCb(c, frsp)
- }
-
- off = int(frsp.Off) + len(frsp.Data)
- if off >= int(frsp.Len) {
- break
- }
- }
-
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $upload //
-//////////////////////////////////////////////////////////////////////////////
-
-type FsUploadProgressCb func(c *FsUploadCmd, r *nmp.FsUploadRsp)
-type FsUploadCmd struct {
- CmdBase
- Name string
- Data []byte
- ProgressCb FsUploadProgressCb
-}
-
-func NewFsUploadCmd() *FsUploadCmd {
- return &FsUploadCmd{}
-}
-
-type FsUploadResult struct {
- Rsps []*nmp.FsUploadRsp
-}
-
-func newFsUploadResult() *FsUploadResult {
- return &FsUploadResult{}
-}
-
-func (r *FsUploadResult) Status() int {
- rsp := r.Rsps[len(r.Rsps)-1]
- return rsp.Rc
-}
-
-func buildFsUploadReq(name string, fileSz int, chunk []byte,
- off int) *nmp.FsUploadReq {
-
- r := nmp.NewFsUploadReq()
-
- if r.Off == 0 {
- r.Len = uint32(fileSz)
- }
- r.Name = name
- r.Off = uint32(off)
- r.Data = chunk
-
- return r
-}
-
-func nextFsUploadReq(s sesn.Sesn, name string, data []byte, off int) (
- *nmp.FsUploadReq, error) {
-
- // First, build a request without data to determine how much data could
- // fit.
- empty := buildFsUploadReq(name, len(data), nil, off)
- emptyEnc, err := s.EncodeNmpMsg(empty.Msg())
- if err != nil {
- return nil, err
- }
-
- room := s.MtuOut() - len(emptyEnc)
- if room <= 0 {
- return nil, fmt.Errorf("Cannot create image upload request; " +
- "MTU too low to fit any image data")
- }
-
- // Assume all the unused space can hold image data. This assumption may
- // not be valid for some encodings (e.g., CBOR uses variable length fields
- // to encodes byte string lengths).
- r := buildFsUploadReq(name, len(data), data[off:off+room], off)
- enc, err := s.EncodeNmpMsg(r.Msg())
- if err != nil {
- return nil, err
- }
-
- oversize := len(enc) - s.MtuOut()
- if oversize > 0 {
- // Request too big. Reduce the amount of image data.
- r = buildFsUploadReq(name, len(data), data[off:off+room-oversize], off)
- }
-
- return r, nil
-}
-
-func (c *FsUploadCmd) Run(s sesn.Sesn) (Result, error) {
- res := newFsUploadResult()
-
- for off := 0; off < len(c.Data); {
- r, err := nextFsUploadReq(s, c.Name, c.Data, off)
- if err != nil {
- return nil, err
- }
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- crsp := rsp.(*nmp.FsUploadRsp)
-
- off = int(crsp.Off)
-
- if c.ProgressCb != nil {
- c.ProgressCb(c, crsp)
- }
-
- res.Rsps = append(res.Rsps, crsp)
- if crsp.Rc != 0 {
- break
- }
- }
-
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/image.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/image.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/image.go
deleted file mode 100644
index 1f236a4..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/image.go
+++ /dev/null
@@ -1,332 +0,0 @@
-package xact
-
-import (
- "fmt"
-
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-//////////////////////////////////////////////////////////////////////////////
-// $upload //
-//////////////////////////////////////////////////////////////////////////////
-
-type ImageUploadProgressFn func(c *ImageUploadCmd, r *nmp.ImageUploadRsp)
-type ImageUploadCmd struct {
- CmdBase
- Data []byte
- ProgressCb ImageUploadProgressFn
-}
-
-type ImageUploadResult struct {
- Rsps []*nmp.ImageUploadRsp
-}
-
-func NewImageUploadCmd() *ImageUploadCmd {
- return &ImageUploadCmd{}
-}
-
-func newImageUploadResult() *ImageUploadResult {
- return &ImageUploadResult{}
-}
-
-func (r *ImageUploadResult) Status() int {
- rsp := r.Rsps[len(r.Rsps)-1]
- return rsp.Rc
-}
-
-func buildImageUploadReq(imageSz int, chunk []byte,
- off int) *nmp.ImageUploadReq {
-
- r := nmp.NewImageUploadReq()
-
- if r.Off == 0 {
- r.Len = uint32(imageSz)
- }
- r.Off = uint32(off)
- r.Data = chunk
-
- return r
-}
-
-func nextImageUploadReq(s sesn.Sesn, data []byte, off int) (
- *nmp.ImageUploadReq, error) {
-
- // First, build a request without data to determine how much data could
- // fit.
- empty := buildImageUploadReq(len(data), nil, off)
- emptyEnc, err := s.EncodeNmpMsg(empty.Msg())
- if err != nil {
- return nil, err
- }
-
- room := s.MtuOut() - len(emptyEnc)
- if room <= 0 {
- return nil, fmt.Errorf("Cannot create image upload request; " +
- "MTU too low to fit any image data")
- }
-
- // Assume all the unused space can hold image data. This assumption may
- // not be valid for some encodings (e.g., CBOR uses variable length fields
- // to encodes byte string lengths).
- r := buildImageUploadReq(len(data), data[off:off+room], off)
- enc, err := s.EncodeNmpMsg(r.Msg())
- if err != nil {
- return nil, err
- }
-
- oversize := len(enc) - s.MtuOut()
- if oversize > 0 {
- // Request too big. Reduce the amount of image data.
- r = buildImageUploadReq(len(data), data[off:off+room-oversize], off)
- }
-
- return r, nil
-}
-
-func (c *ImageUploadCmd) Run(s sesn.Sesn) (Result, error) {
- res := newImageUploadResult()
-
- for off := 0; off < len(c.Data); {
- r, err := nextImageUploadReq(s, c.Data, off)
- if err != nil {
- return nil, err
- }
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- irsp := rsp.(*nmp.ImageUploadRsp)
-
- off = int(irsp.Off)
-
- if c.ProgressCb != nil {
- c.ProgressCb(c, irsp)
- }
-
- res.Rsps = append(res.Rsps, irsp)
- if irsp.Rc != 0 {
- break
- }
- }
-
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $state read //
-//////////////////////////////////////////////////////////////////////////////
-
-type ImageStateReadCmd struct {
- CmdBase
-}
-
-type ImageStateReadResult struct {
- Rsp *nmp.ImageStateRsp
-}
-
-func NewImageStateReadCmd() *ImageStateReadCmd {
- return &ImageStateReadCmd{}
-}
-
-func newImageStateReadResult() *ImageStateReadResult {
- return &ImageStateReadResult{}
-}
-
-func (r *ImageStateReadResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *ImageStateReadCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewImageStateReadReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.ImageStateRsp)
-
- res := newImageStateReadResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $state write //
-//////////////////////////////////////////////////////////////////////////////
-
-type ImageStateWriteCmd struct {
- CmdBase
- Hash []byte
- Confirm bool
-}
-
-type ImageStateWriteResult struct {
- Rsp *nmp.ImageStateRsp
-}
-
-func NewImageStateWriteCmd() *ImageStateWriteCmd {
- return &ImageStateWriteCmd{}
-}
-
-func newImageStateWriteResult() *ImageStateWriteResult {
- return &ImageStateWriteResult{}
-}
-
-func (r *ImageStateWriteResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *ImageStateWriteCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewImageStateWriteReq()
- r.Hash = c.Hash
- r.Confirm = c.Confirm
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.ImageStateRsp)
-
- res := newImageStateWriteResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $corelist //
-//////////////////////////////////////////////////////////////////////////////
-
-type CoreListCmd struct {
- CmdBase
-}
-
-type CoreListResult struct {
- Rsp *nmp.CoreListRsp
-}
-
-func NewCoreListCmd() *CoreListCmd {
- return &CoreListCmd{}
-}
-
-func newCoreListResult() *CoreListResult {
- return &CoreListResult{}
-}
-
-func (r *CoreListResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *CoreListCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewCoreListReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.CoreListRsp)
-
- res := newCoreListResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $coreload //
-//////////////////////////////////////////////////////////////////////////////
-
-type CoreLoadProgressFn func(c *CoreLoadCmd, r *nmp.CoreLoadRsp)
-type CoreLoadCmd struct {
- CmdBase
- ProgressCb CoreLoadProgressFn
-}
-
-type CoreLoadResult struct {
- Rsps []*nmp.CoreLoadRsp
-}
-
-func NewCoreLoadCmd() *CoreLoadCmd {
- return &CoreLoadCmd{}
-}
-
-func newCoreLoadResult() *CoreLoadResult {
- return &CoreLoadResult{}
-}
-
-func (r *CoreLoadResult) Status() int {
- rsp := r.Rsps[len(r.Rsps)-1]
- return rsp.Rc
-}
-
-func (c *CoreLoadCmd) Run(s sesn.Sesn) (Result, error) {
- res := newCoreLoadResult()
- off := 0
-
- for {
- r := nmp.NewCoreLoadReq()
- r.Off = uint32(off)
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- irsp := rsp.(*nmp.CoreLoadRsp)
-
- if c.ProgressCb != nil {
- c.ProgressCb(c, irsp)
- }
-
- res.Rsps = append(res.Rsps, irsp)
- if irsp.Rc != 0 {
- break
- }
-
- if len(irsp.Data) == 0 {
- // Download complete.
- break
- }
-
- off = int(irsp.Off) + len(irsp.Data)
- }
-
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $coreerase //
-//////////////////////////////////////////////////////////////////////////////
-
-type CoreEraseCmd struct {
- CmdBase
-}
-
-type CoreEraseResult struct {
- Rsp *nmp.CoreEraseRsp
-}
-
-func NewCoreEraseCmd() *CoreEraseCmd {
- return &CoreEraseCmd{}
-}
-
-func newCoreEraseResult() *CoreEraseResult {
- return &CoreEraseResult{}
-}
-
-func (r *CoreEraseResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *CoreEraseCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewCoreEraseReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.CoreEraseRsp)
-
- res := newCoreEraseResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/log.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/log.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/log.go
deleted file mode 100644
index d40d523..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/log.go
+++ /dev/null
@@ -1,202 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-//////////////////////////////////////////////////////////////////////////////
-// $read //
-//////////////////////////////////////////////////////////////////////////////
-
-type LogShowCmd struct {
- CmdBase
- Name string
- Timestamp int64
- Index uint32
-}
-
-func NewLogShowCmd() *LogShowCmd {
- return &LogShowCmd{}
-}
-
-type LogShowResult struct {
- Rsp *nmp.LogShowRsp
-}
-
-func newLogShowResult() *LogShowResult {
- return &LogShowResult{}
-}
-
-func (r *LogShowResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *LogShowCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewLogShowReq()
- r.Name = c.Name
- r.Timestamp = c.Timestamp
- r.Index = c.Index
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.LogShowRsp)
-
- res := newLogShowResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $list //
-//////////////////////////////////////////////////////////////////////////////
-
-type LogListCmd struct {
- CmdBase
-}
-
-func NewLogListCmd() *LogListCmd {
- return &LogListCmd{}
-}
-
-type LogListResult struct {
- Rsp *nmp.LogListRsp
-}
-
-func newLogListResult() *LogListResult {
- return &LogListResult{}
-}
-
-func (r *LogListResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *LogListCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewLogListReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.LogListRsp)
-
- res := newLogListResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $module list //
-//////////////////////////////////////////////////////////////////////////////
-
-type LogModuleListCmd struct {
- CmdBase
-}
-
-func NewLogModuleListCmd() *LogModuleListCmd {
- return &LogModuleListCmd{}
-}
-
-type LogModuleListResult struct {
- Rsp *nmp.LogModuleListRsp
-}
-
-func newLogModuleListResult() *LogModuleListResult {
- return &LogModuleListResult{}
-}
-
-func (r *LogModuleListResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *LogModuleListCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewLogModuleListReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.LogModuleListRsp)
-
- res := newLogModuleListResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $level list //
-//////////////////////////////////////////////////////////////////////////////
-
-type LogLevelListCmd struct {
- CmdBase
-}
-
-func NewLogLevelListCmd() *LogLevelListCmd {
- return &LogLevelListCmd{}
-}
-
-type LogLevelListResult struct {
- Rsp *nmp.LogLevelListRsp
-}
-
-func newLogLevelListResult() *LogLevelListResult {
- return &LogLevelListResult{}
-}
-
-func (r *LogLevelListResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *LogLevelListCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewLogLevelListReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.LogLevelListRsp)
-
- res := newLogLevelListResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $clear //
-//////////////////////////////////////////////////////////////////////////////
-
-type LogClearCmd struct {
- CmdBase
-}
-
-func NewLogClearCmd() *LogClearCmd {
- return &LogClearCmd{}
-}
-
-type LogClearResult struct {
- Rsp *nmp.LogClearRsp
-}
-
-func newLogClearResult() *LogClearResult {
- return &LogClearResult{}
-}
-
-func (r *LogClearResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *LogClearCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewLogClearReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.LogClearRsp)
-
- res := newLogClearResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/mpstat.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/mpstat.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/mpstat.go
deleted file mode 100644
index cc2784c..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/mpstat.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-type MempoolStatCmd struct {
- CmdBase
-}
-
-func NewMempoolStatCmd() *MempoolStatCmd {
- return &MempoolStatCmd{}
-}
-
-type MempoolStatResult struct {
- Rsp *nmp.MempoolStatRsp
-}
-
-func newMempoolStatResult() *MempoolStatResult {
- return &MempoolStatResult{}
-}
-
-func (r *MempoolStatResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *MempoolStatCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewMempoolStatReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.MempoolStatRsp)
-
- res := newMempoolStatResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/reset.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/reset.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/reset.go
deleted file mode 100644
index c7adf83..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/reset.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-type ResetCmd struct {
- CmdBase
- Payload string
-}
-
-func NewResetCmd() *ResetCmd {
- return &ResetCmd{}
-}
-
-type ResetResult struct {
- Rsp *nmp.ResetRsp
-}
-
-func newResetResult() *ResetResult {
- return &ResetResult{}
-}
-
-func (r *ResetResult) Status() int {
- return 0
-}
-
-func (c *ResetCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewResetReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.ResetRsp)
-
- res := newResetResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/run.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/run.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/run.go
deleted file mode 100644
index ff1e1e9..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/run.go
+++ /dev/null
@@ -1,86 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-//////////////////////////////////////////////////////////////////////////////
-// $test //
-//////////////////////////////////////////////////////////////////////////////
-
-type RunTestCmd struct {
- CmdBase
- Testname string
- Token string
-}
-
-func NewRunTestCmd() *RunTestCmd {
- return &RunTestCmd{}
-}
-
-type RunTestResult struct {
- Rsp *nmp.RunTestRsp
-}
-
-func newRunTestResult() *RunTestResult {
- return &RunTestResult{}
-}
-
-func (r *RunTestResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *RunTestCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewRunTestReq()
- r.Testname = c.Testname
- r.Token = c.Token
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.RunTestRsp)
-
- res := newRunTestResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $list //
-//////////////////////////////////////////////////////////////////////////////
-
-type RunListCmd struct {
- CmdBase
-}
-
-func NewRunListCmd() *RunListCmd {
- return &RunListCmd{}
-}
-
-type RunListResult struct {
- Rsp *nmp.RunListRsp
-}
-
-func newRunListResult() *RunListResult {
- return &RunListResult{}
-}
-
-func (r *RunListResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *RunListCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewRunListReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.RunListRsp)
-
- res := newRunListResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/stat.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/stat.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/stat.go
deleted file mode 100644
index 6aa4dc3..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/stat.go
+++ /dev/null
@@ -1,84 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-//////////////////////////////////////////////////////////////////////////////
-// $read //
-//////////////////////////////////////////////////////////////////////////////
-
-type StatReadCmd struct {
- CmdBase
- Name string
-}
-
-func NewStatReadCmd() *StatReadCmd {
- return &StatReadCmd{}
-}
-
-type StatReadResult struct {
- Rsp *nmp.StatReadRsp
-}
-
-func newStatReadResult() *StatReadResult {
- return &StatReadResult{}
-}
-
-func (r *StatReadResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *StatReadCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewStatReadReq()
- r.Name = c.Name
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.StatReadRsp)
-
- res := newStatReadResult()
- res.Rsp = srsp
- return res, nil
-}
-
-//////////////////////////////////////////////////////////////////////////////
-// $list //
-//////////////////////////////////////////////////////////////////////////////
-
-type StatListCmd struct {
- CmdBase
-}
-
-func NewStatListCmd() *StatListCmd {
- return &StatListCmd{}
-}
-
-type StatListResult struct {
- Rsp *nmp.StatListRsp
-}
-
-func newStatListResult() *StatListResult {
- return &StatListResult{}
-}
-
-func (r *StatListResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *StatListCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewStatListReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.StatListRsp)
-
- res := newStatListResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/taskstat.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/taskstat.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/taskstat.go
deleted file mode 100644
index 9ec7774..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/taskstat.go
+++ /dev/null
@@ -1,40 +0,0 @@
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-type TaskStatCmd struct {
- CmdBase
-}
-
-func NewTaskStatCmd() *TaskStatCmd {
- return &TaskStatCmd{}
-}
-
-type TaskStatResult struct {
- Rsp *nmp.TaskStatRsp
-}
-
-func newTaskStatResult() *TaskStatResult {
- return &TaskStatResult{}
-}
-
-func (r *TaskStatResult) Status() int {
- return r.Rsp.Rc
-}
-
-func (c *TaskStatCmd) Run(s sesn.Sesn) (Result, error) {
- r := nmp.NewTaskStatReq()
-
- rsp, err := txReq(s, r.Msg(), &c.CmdBase)
- if err != nil {
- return nil, err
- }
- srsp := rsp.(*nmp.TaskStatRsp)
-
- res := newTaskStatResult()
- res.Rsp = srsp
- return res, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/xact.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/xact.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/xact.go
deleted file mode 100644
index 2cdf150..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xact/xact.go
+++ /dev/null
@@ -1,47 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package xact
-
-import (
- "mynewt.apache.org/newt/nmxact/nmp"
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-func txReq(s sesn.Sesn, m *nmp.NmpMsg, c *CmdBase) (
- nmp.NmpRsp, error) {
-
- if c.abortErr != nil {
- return nil, c.abortErr
- }
-
- c.curNmpSeq = m.Hdr.Seq
- c.curSesn = s
- defer func() {
- c.curNmpSeq = 0
- c.curSesn = nil
- }()
-
- rsp, err := sesn.TxNmp(s, m, c.TxOptions())
- if err != nil {
- return nil, err
- }
-
- return rsp, nil
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xport/xport.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xport/xport.go b/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xport/xport.go
deleted file mode 100644
index 3cdf8b5..0000000
--- a/newtmgr/vendor/mynewt.apache.org/newt/nmxact/xport/xport.go
+++ /dev/null
@@ -1,15 +0,0 @@
-package xport
-
-import (
- "mynewt.apache.org/newt/nmxact/sesn"
-)
-
-type RxFn func(data []byte)
-
-type Xport interface {
- Start() error
- Stop() error
- BuildSesn(cfg sesn.SesnCfg) (sesn.Sesn, error)
-
- Tx(data []byte) error
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/bledefs/bledefs.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/bledefs/bledefs.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/bledefs/bledefs.go
new file mode 100644
index 0000000..3bb2772
--- /dev/null
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/bledefs/bledefs.go
@@ -0,0 +1,270 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package bledefs
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "strconv"
+ "strings"
+)
+
+const BLE_ATT_ATTR_MAX_LEN = 512
+
+type BleAddrType int
+
+const (
+ BLE_ADDR_TYPE_PUBLIC BleAddrType = 0
+ BLE_ADDR_TYPE_RANDOM = 1
+ BLE_ADDR_TYPE_RPA_PUB = 2
+ BLE_ADDR_TYPE_RPA_RND = 3
+)
+
+var BleAddrTypeStringMap = map[BleAddrType]string{
+ BLE_ADDR_TYPE_PUBLIC: "public",
+ BLE_ADDR_TYPE_RANDOM: "random",
+ BLE_ADDR_TYPE_RPA_PUB: "rpa_pub",
+ BLE_ADDR_TYPE_RPA_RND: "rpa_rnd",
+}
+
+func BleAddrTypeToString(addrType BleAddrType) string {
+ s := BleAddrTypeStringMap[addrType]
+ if s == "" {
+ panic(fmt.Sprintf("Invalid BleAddrType: %d", int(addrType)))
+ }
+
+ return s
+}
+
+func BleAddrTypeFromString(s string) (BleAddrType, error) {
+ for addrType, name := range BleAddrTypeStringMap {
+ if s == name {
+ return addrType, nil
+ }
+ }
+
+ return BleAddrType(0), fmt.Errorf("Invalid BleAddrType string: %s", s)
+}
+
+func (a BleAddrType) MarshalJSON() ([]byte, error) {
+ return json.Marshal(BleAddrTypeToString(a))
+}
+
+func (a *BleAddrType) UnmarshalJSON(data []byte) error {
+ var err error
+
+ var s string
+ if err := json.Unmarshal(data, &s); err != nil {
+ return err
+ }
+
+ *a, err = BleAddrTypeFromString(s)
+ return err
+}
+
+type BleAddr struct {
+ Bytes [6]byte
+}
+
+func ParseBleAddr(s string) (BleAddr, error) {
+ ba := BleAddr{}
+
+ toks := strings.Split(strings.ToLower(s), ":")
+ if len(toks) != 6 {
+ return ba, fmt.Errorf("invalid BLE addr string: %s", s)
+ }
+
+ for i, t := range toks {
+ u64, err := strconv.ParseUint(t, 16, 8)
+ if err != nil {
+ return ba, err
+ }
+ ba.Bytes[i] = byte(u64)
+ }
+
+ return ba, nil
+}
+
+func (ba *BleAddr) String() string {
+ var buf bytes.Buffer
+ buf.Grow(len(ba.Bytes) * 3)
+
+ for i, b := range ba.Bytes {
+ if i != 0 {
+ buf.WriteString(":")
+ }
+ fmt.Fprintf(&buf, "%02x", b)
+ }
+
+ return buf.String()
+}
+
+func (ba *BleAddr) MarshalJSON() ([]byte, error) {
+ return json.Marshal(ba.String())
+}
+
+func (ba *BleAddr) UnmarshalJSON(data []byte) error {
+ var s string
+ if err := json.Unmarshal(data, &s); err != nil {
+ return err
+ }
+
+ var err error
+ *ba, err = ParseBleAddr(s)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+type BleDev struct {
+ AddrType BleAddrType
+ Addr BleAddr
+}
+
+func (bd *BleDev) String() string {
+ return fmt.Sprintf("%s,%s",
+ BleAddrTypeToString(bd.AddrType),
+ bd.Addr.String())
+}
+
+type BleScanFilterPolicy int
+
+const (
+ BLE_SCAN_FILT_NO_WL BleScanFilterPolicy = 0
+ BLE_SCAN_FILT_USE_WL = 1
+ BLE_SCAN_FILT_NO_WL_INITA = 2
+ BLE_SCAN_FILT_USE_WL_INITA = 3
+)
+
+var BleScanFilterPolicyStringMap = map[BleScanFilterPolicy]string{
+ BLE_SCAN_FILT_NO_WL: "no_wl",
+ BLE_SCAN_FILT_USE_WL: "use_wl",
+ BLE_SCAN_FILT_NO_WL_INITA: "no_wl_inita",
+ BLE_SCAN_FILT_USE_WL_INITA: "use_wl_inita",
+}
+
+func BleScanFilterPolicyToString(filtPolicy BleScanFilterPolicy) string {
+ s := BleScanFilterPolicyStringMap[filtPolicy]
+ if s == "" {
+ panic(fmt.Sprintf("Invalid BleScanFilterPolicy: %d", int(filtPolicy)))
+ }
+
+ return s
+}
+
+func BleScanFilterPolicyFromString(s string) (BleScanFilterPolicy, error) {
+ for filtPolicy, name := range BleScanFilterPolicyStringMap {
+ if s == name {
+ return filtPolicy, nil
+ }
+ }
+
+ return BleScanFilterPolicy(0),
+ fmt.Errorf("Invalid BleScanFilterPolicy string: %s", s)
+}
+
+func (a BleScanFilterPolicy) MarshalJSON() ([]byte, error) {
+ return json.Marshal(BleScanFilterPolicyToString(a))
+}
+
+func (a *BleScanFilterPolicy) UnmarshalJSON(data []byte) error {
+ var err error
+
+ var s string
+ if err := json.Unmarshal(data, &s); err != nil {
+ return err
+ }
+
+ *a, err = BleScanFilterPolicyFromString(s)
+ return err
+}
+
+type BleAdvEventType int
+
+const (
+ BLE_ADV_EVENT_IND BleAdvEventType = 0
+ BLE_ADV_EVENT_DIRECT_IND_HD = 1
+ BLE_ADV_EVENT_SCAN_IND = 2
+ BLE_ADV_EVENT_NONCONN_IND = 3
+ BLE_ADV_EVENT_DIRECT_IND_LD = 4
+)
+
+var BleAdvEventTypeStringMap = map[BleAdvEventType]string{
+ BLE_ADV_EVENT_IND: "ind",
+ BLE_ADV_EVENT_DIRECT_IND_HD: "direct_ind_hd",
+ BLE_ADV_EVENT_SCAN_IND: "scan_ind",
+ BLE_ADV_EVENT_NONCONN_IND: "nonconn_ind",
+ BLE_ADV_EVENT_DIRECT_IND_LD: "direct_ind_ld",
+}
+
+func BleAdvEventTypeToString(advEventType BleAdvEventType) string {
+ s := BleAdvEventTypeStringMap[advEventType]
+ if s == "" {
+ panic(fmt.Sprintf("Invalid BleAdvEventType: %d", int(advEventType)))
+ }
+
+ return s
+}
+
+func BleAdvEventTypeFromString(s string) (BleAdvEventType, error) {
+ for advEventType, name := range BleAdvEventTypeStringMap {
+ if s == name {
+ return advEventType, nil
+ }
+ }
+
+ return BleAdvEventType(0),
+ fmt.Errorf("Invalid BleAdvEventType string: %s", s)
+}
+
+func (a BleAdvEventType) MarshalJSON() ([]byte, error) {
+ return json.Marshal(BleAdvEventTypeToString(a))
+}
+
+func (a *BleAdvEventType) UnmarshalJSON(data []byte) error {
+ var err error
+
+ var s string
+ if err := json.Unmarshal(data, &s); err != nil {
+ return err
+ }
+
+ *a, err = BleAdvEventTypeFromString(s)
+ return err
+}
+
+type BleAdvReport struct {
+ // These fields are always present.
+ EventType BleAdvEventType
+ Sender BleDev
+ Rssi int8
+ Data []byte
+
+ // These fields are only present if the sender included them in its
+ // advertisement.
+ Flags uint8 // 0 if not present.
+ Name string // "" if not present.
+ NameIsComplete bool // false if not present.
+}
+
+type BleAdvPredicate func(adv BleAdvReport) bool
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_act.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_act.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_act.go
new file mode 100644
index 0000000..2aa6678
--- /dev/null
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_act.go
@@ -0,0 +1,363 @@
+package nmble
+
+import (
+ "encoding/json"
+
+ "mynewt.apache.org/newtmgr/nmxact/nmxutil"
+)
+
+// Blocking
+func connect(x *BleXport, connChan chan error, r *BleConnectReq) error {
+ j, err := json.Marshal(r)
+ if err != nil {
+ return err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return err
+ }
+
+ err = <-connChan
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// Blocking
+func terminate(x *BleXport, bl *BleListener, r *BleTerminateReq) error {
+ j, err := json.Marshal(r)
+ if err != nil {
+ return err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return err
+ }
+
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleTerminateRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return StatusError(MSG_OP_RSP,
+ MSG_TYPE_TERMINATE,
+ msg.Status)
+ } else {
+ return nil
+ }
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return BhdTimeoutError(MSG_TYPE_TERMINATE)
+ }
+ }
+}
+
+func connCancel(x *BleXport, bl *BleListener, r *BleConnCancelReq) error {
+ j, err := json.Marshal(r)
+ if err != nil {
+ return err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return err
+ }
+
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleConnCancelRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return StatusError(MSG_OP_RSP,
+ MSG_TYPE_CONN_CANCEL,
+ msg.Status)
+ } else {
+ return nil
+ }
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return BhdTimeoutError(MSG_TYPE_TERMINATE)
+ }
+ }
+}
+
+// Blocking.
+func discSvcUuid(x *BleXport, bl *BleListener, r *BleDiscSvcUuidReq) (
+ *BleSvc, error) {
+
+ j, err := json.Marshal(r)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return nil, err
+ }
+
+ var svc *BleSvc
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return nil, err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleDiscSvcUuidRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return nil, StatusError(MSG_OP_RSP,
+ MSG_TYPE_DISC_SVC_UUID,
+ msg.Status)
+ }
+
+ case *BleDiscSvcEvt:
+ switch msg.Status {
+ case 0:
+ svc = &msg.Svc
+ case ERR_CODE_EDONE:
+ if svc == nil {
+ return nil, nmxutil.FmtBleHostError(
+ msg.Status,
+ "Peer doesn't support required service: %s",
+ r.Uuid.String())
+ }
+ return svc, nil
+ default:
+ return nil, StatusError(MSG_OP_EVT,
+ MSG_TYPE_DISC_SVC_EVT,
+ msg.Status)
+ }
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return nil, BhdTimeoutError(MSG_TYPE_DISC_SVC_UUID)
+ }
+ }
+}
+
+// Blocking.
+func discAllChrs(x *BleXport, bl *BleListener, r *BleDiscAllChrsReq) (
+ []*BleChr, error) {
+
+ j, err := json.Marshal(r)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return nil, err
+ }
+
+ chrs := []*BleChr{}
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return nil, err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleDiscAllChrsRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return nil, StatusError(MSG_OP_RSP,
+ MSG_TYPE_DISC_ALL_CHRS,
+ msg.Status)
+ }
+
+ case *BleDiscChrEvt:
+ switch msg.Status {
+ case 0:
+ chrs = append(chrs, &msg.Chr)
+ case ERR_CODE_EDONE:
+ return chrs, nil
+ default:
+ return nil, StatusError(MSG_OP_EVT,
+ MSG_TYPE_DISC_CHR_EVT,
+ msg.Status)
+ }
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return nil, BhdTimeoutError(MSG_TYPE_DISC_ALL_CHRS)
+ }
+ }
+}
+
+// Blocking.
+func writeCmd(x *BleXport, bl *BleListener, r *BleWriteCmdReq) error {
+ j, err := json.Marshal(r)
+ if err != nil {
+ return err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return err
+ }
+
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleWriteCmdRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return StatusError(MSG_OP_RSP,
+ MSG_TYPE_WRITE_CMD,
+ msg.Status)
+ } else {
+ return nil
+ }
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return BhdTimeoutError(MSG_TYPE_WRITE_CMD)
+ }
+ }
+}
+
+// Blocking.
+func exchangeMtu(x *BleXport, bl *BleListener, r *BleExchangeMtuReq) (
+ int, error) {
+
+ j, err := json.Marshal(r)
+ if err != nil {
+ return 0, err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return 0, err
+ }
+
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return 0, err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleExchangeMtuRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return 0, StatusError(MSG_OP_RSP,
+ MSG_TYPE_EXCHANGE_MTU,
+ msg.Status)
+ }
+
+ case *BleMtuChangeEvt:
+ if msg.Status != 0 {
+ return 0, StatusError(MSG_OP_EVT,
+ MSG_TYPE_MTU_CHANGE_EVT,
+ msg.Status)
+ } else {
+ return msg.Mtu, nil
+ }
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return 0, BhdTimeoutError(MSG_TYPE_EXCHANGE_MTU)
+ }
+ }
+}
+
+type scanFn func(evt *BleScanEvt)
+
+func scan(x *BleXport, bl *BleListener, r *BleScanReq,
+ abortChan chan struct{}, scanCb scanFn) error {
+
+ j, err := json.Marshal(r)
+ if err != nil {
+ return err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return err
+ }
+
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleScanRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return StatusError(MSG_OP_RSP, MSG_TYPE_SCAN, msg.Status)
+ }
+
+ case *BleScanEvt:
+ scanCb(msg)
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return BhdTimeoutError(MSG_TYPE_EXCHANGE_MTU)
+
+ case <-abortChan:
+ return nil
+ }
+ }
+}
+
+func scanCancel(x *BleXport, bl *BleListener, r *BleScanCancelReq) error {
+ j, err := json.Marshal(r)
+ if err != nil {
+ return err
+ }
+
+ if err := x.Tx(j); err != nil {
+ return err
+ }
+
+ for {
+ select {
+ case err := <-bl.ErrChan:
+ return err
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleScanCancelRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ return StatusError(MSG_OP_RSP, MSG_TYPE_SCAN, msg.Status)
+ }
+ return nil
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(x.rspTimeout):
+ return BhdTimeoutError(MSG_TYPE_EXCHANGE_MTU)
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go
new file mode 100644
index 0000000..9c96526
--- /dev/null
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go
@@ -0,0 +1,766 @@
+package nmble
+
+import (
+ "encoding/hex"
+ "fmt"
+ "sync"
+ "time"
+
+ log "github.com/Sirupsen/logrus"
+
+ . "mynewt.apache.org/newtmgr/nmxact/bledefs"
+ "mynewt.apache.org/newtmgr/nmxact/nmp"
+ "mynewt.apache.org/newtmgr/nmxact/nmxutil"
+ "mynewt.apache.org/newtmgr/nmxact/sesn"
+)
+
+type BleSesnState int32
+
+const DFLT_ATT_MTU = 23
+
+const (
+ SESN_STATE_UNCONNECTED BleSesnState = 0
+ SESN_STATE_SCANNING = 1
+ SESN_STATE_CONNECTING = 2
+ SESN_STATE_CONNECTED = 3
+ SESN_STATE_EXCHANGING_MTU = 4
+ SESN_STATE_EXCHANGED_MTU = 5
+ SESN_STATE_DISCOVERING_SVC = 6
+ SESN_STATE_DISCOVERED_SVC = 7
+ SESN_STATE_DISCOVERING_CHR = 8
+ SESN_STATE_DISCOVERED_CHR = 9
+ SESN_STATE_TERMINATING = 10
+ SESN_STATE_CONN_CANCELLING = 11
+)
+
+type BleRxNmpFn func(data []byte)
+type BleDisconnectFn func(peer BleDev, err error)
+
+type BleFsmParams struct {
+ Bx *BleXport
+ OwnAddrType BleAddrType
+ PeerSpec sesn.BlePeerSpec
+ SvcUuid BleUuid
+ ReqChrUuid BleUuid
+ RspChrUuid BleUuid
+ RxNmpCb BleRxNmpFn
+ DisconnectCb BleDisconnectFn
+}
+
+type BleFsm struct {
+ bx *BleXport
+ ownAddrType BleAddrType
+ peerSpec sesn.BlePeerSpec
+ peerDev *BleDev
+ svcUuid BleUuid
+ reqChrUuid BleUuid
+ rspChrUuid BleUuid
+ rxNmpCb BleRxNmpFn
+ disconnectCb BleDisconnectFn
+
+ connHandle int
+ nmpSvc *BleSvc
+ nmpReqChr *BleChr
+ nmpRspChr *BleChr
+ attMtu int
+ connChan chan error
+
+ mtx sync.Mutex
+ lastStateChange time.Time
+
+ // These variables must be protected by the mutex.
+ bls map[*BleListener]struct{}
+ state BleSesnState
+}
+
+func NewBleFsm(p BleFsmParams) *BleFsm {
+ bf := &BleFsm{
+ bx: p.Bx,
+ peerSpec: p.PeerSpec,
+ ownAddrType: p.OwnAddrType,
+ svcUuid: p.SvcUuid,
+ reqChrUuid: p.ReqChrUuid,
+ rspChrUuid: p.RspChrUuid,
+ rxNmpCb: p.RxNmpCb,
+ disconnectCb: p.DisconnectCb,
+
+ bls: map[*BleListener]struct{}{},
+ attMtu: DFLT_ATT_MTU,
+ }
+
+ return bf
+}
+
+func (bf *BleFsm) disconnectError(reason int) error {
+ str := fmt.Sprintf("BLE peer disconnected; "+
+ "reason=\"%s\" (%d) peer=%s handle=%d",
+ ErrCodeToString(reason), reason, bf.peerDev.String(), bf.connHandle)
+ return nmxutil.NewBleSesnDisconnectError(reason, str)
+}
+
+func (bf *BleFsm) closedError(msg string) error {
+ return nmxutil.NewSesnClosedError(fmt.Sprintf(
+ "%s; state=%d last-state-change=%s",
+ msg, bf.getState(), bf.lastStateChange))
+}
+
+func (bf *BleFsm) getState() BleSesnState {
+ bf.mtx.Lock()
+ defer bf.mtx.Unlock()
+
+ return bf.state
+}
+
+func (bf *BleFsm) setState(toState BleSesnState) {
+ bf.mtx.Lock()
+ defer bf.mtx.Unlock()
+
+ bf.state = toState
+ bf.lastStateChange = time.Now()
+}
+
+func (bf *BleFsm) transitionState(fromState BleSesnState,
+ toState BleSesnState) error {
+
+ bf.mtx.Lock()
+ defer bf.mtx.Unlock()
+
+ if bf.state != fromState {
+ return fmt.Errorf(
+ "Can't set BleFsm state to %d; current state != required "+
+ "value: %d",
+ toState, fromState)
+ }
+
+ bf.state = toState
+ return nil
+}
+
+func (bf *BleFsm) addBleListener(base BleMsgBase) (*BleListener, error) {
+ bl := NewBleListener()
+
+ bf.mtx.Lock()
+ bf.bls[bl] = struct{}{}
+ bf.mtx.Unlock()
+
+ if err := bf.bx.Bd.AddListener(base, bl); err != nil {
+ delete(bf.bls, bl)
+ return nil, err
+ }
+
+ return bl, nil
+}
+
+func (bf *BleFsm) addBleSeqListener(seq int) (*BleListener, error) {
+ base := BleMsgBase{
+ Op: -1,
+ Type: -1,
+ Seq: seq,
+ ConnHandle: -1,
+ }
+ bl, err := bf.addBleListener(base)
+ if err != nil {
+ return nil, err
+ }
+
+ return bl, nil
+}
+
+func (bf *BleFsm) removeBleListener(base BleMsgBase) {
+ bl := bf.bx.Bd.RemoveListener(base)
+ if bl != nil {
+ bf.mtx.Lock()
+ delete(bf.bls, bl)
+ bf.mtx.Unlock()
+ }
+}
+
+func (bf *BleFsm) removeBleSeqListener(seq int) {
+ base := BleMsgBase{
+ Op: -1,
+ Type: -1,
+ Seq: seq,
+ ConnHandle: -1,
+ }
+
+ bf.removeBleListener(base)
+}
+
+func (bf *BleFsm) action(
+ preState BleSesnState,
+ inState BleSesnState,
+ postState BleSesnState,
+ cb func() error) error {
+
+ if err := bf.transitionState(preState, inState); err != nil {
+ return err
+ }
+
+ if err := cb(); err != nil {
+ bf.setState(preState)
+ return err
+ }
+
+ bf.setState(postState)
+ return nil
+}
+
+func (bf *BleFsm) connectListen(seq int) error {
+ bf.connChan = make(chan error, 1)
+
+ bl, err := bf.addBleSeqListener(seq)
+ if err != nil {
+ return err
+ }
+
+ go func() {
+ defer bf.removeBleSeqListener(seq)
+ for {
+ select {
+ case <-bl.ErrChan:
+ return
+
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleConnectRsp:
+ bl.Acked = true
+ if msg.Status != 0 {
+ str := fmt.Sprintf("BLE connection attempt failed; "+
+ "status=%s (%d) peer=%s",
+ ErrCodeToString(msg.Status), msg.Status,
+ bf.peerDev.String())
+ log.Debugf(str)
+ bf.connChan <- nmxutil.NewBleHostError(msg.Status, str)
+ return
+ }
+
+ 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
+ if err := bf.nmpRspListen(); err != nil {
+ bf.connChan <- err
+ return
+ }
+ bf.connChan <- nil
+ } else {
+ str := fmt.Sprintf("BLE connection attempt failed; "+
+ "status=%s (%d) peer=%s",
+ ErrCodeToString(msg.Status), msg.Status,
+ bf.peerDev.String())
+ log.Debugf(str)
+ bf.connChan <- nmxutil.NewBleHostError(msg.Status, str)
+ return
+ }
+
+ case *BleMtuChangeEvt:
+ if msg.Status != 0 {
+ err := StatusError(MSG_OP_EVT,
+ MSG_TYPE_MTU_CHANGE_EVT,
+ msg.Status)
+ log.Debugf(err.Error())
+ } else {
+ log.Debugf("BLE ATT MTU updated; from=%d to=%d",
+ bf.attMtu, msg.Mtu)
+ bf.attMtu = msg.Mtu
+ }
+
+ case *BleDisconnectEvt:
+ err := bf.disconnectError(msg.Reason)
+ log.Debugf(err.Error())
+
+ bf.mtx.Lock()
+ bls := make([]*BleListener, 0, len(bf.bls))
+ for bl, _ := range bf.bls {
+ bls = append(bls, bl)
+ }
+ bf.mtx.Unlock()
+
+ for _, bl := range bls {
+ bl.ErrChan <- err
+ }
+
+ bf.setState(SESN_STATE_UNCONNECTED)
+ peer := *bf.peerDev
+ bf.peerDev = nil
+ bf.disconnectCb(peer, err)
+ return
+
+ default:
+ }
+
+ case <-bl.AfterTimeout(bf.bx.rspTimeout):
+ bf.connChan <- BhdTimeoutError(MSG_TYPE_CONNECT)
+ }
+ }
+ }()
+ return nil
+}
+
+func (bf *BleFsm) nmpRspListen() error {
+ base := BleMsgBase{
+ Op: MSG_OP_EVT,
+ Type: MSG_TYPE_NOTIFY_RX_EVT,
+ Seq: -1,
+ ConnHandle: bf.connHandle,
+ }
+
+ bl, err := bf.addBleListener(base)
+ if err != nil {
+ return err
+ }
+
+ go func() {
+ defer bf.removeBleListener(base)
+ for {
+ select {
+ case <-bl.ErrChan:
+ // The session encountered an error; stop listening.
+ return
+ case bm := <-bl.BleChan:
+ switch msg := bm.(type) {
+ case *BleNotifyRxEvt:
+ if bf.nmpRspChr != nil &&
+ msg.AttrHandle == bf.nmpRspChr.ValHandle {
+
+ bf.rxNmpCb(msg.Data.Bytes)
+ }
+
+ default:
+ }
+ }
+ }
+ }()
+ return nil
+}
+
+func (bf *BleFsm) connect() error {
+ r := NewBleConnectReq()
+ r.OwnAddrType = bf.ownAddrType
+ r.PeerAddrType = bf.peerDev.AddrType
+ r.PeerAddr = bf.peerDev.Addr
+
+ if err := bf.connectListen(r.Seq); err != nil {
+ return err
+ }
+
+ if err := connect(bf.bx, bf.connChan, r); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) scan() error {
+ r := NewBleScanReq()
+ r.OwnAddrType = bf.ownAddrType
+ r.DurationMs = 15000
+ r.FilterPolicy = BLE_SCAN_FILT_NO_WL
+ r.Limited = false
+ r.Passive = false
+ r.FilterDuplicates = true
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ abortChan := make(chan struct{}, 1)
+
+ // This function gets called for each incoming advertisement.
+ scanCb := func(evt *BleScanEvt) {
+ r := BleAdvReport{
+ EventType: evt.EventType,
+ Sender: BleDev{
+ AddrType: evt.AddrType,
+ Addr: evt.Addr,
+ },
+ Rssi: evt.Rssi,
+ Data: evt.Data.Bytes,
+
+ Flags: evt.DataFlags,
+ Name: evt.DataName,
+ NameIsComplete: evt.DataNameIsComplete,
+ }
+
+ // Ask client if we should connect to this advertiser.
+ if bf.peerSpec.ScanPred(r) {
+ bf.peerDev = &r.Sender
+ abortChan <- struct{}{}
+ }
+ }
+
+ if err := scan(bf.bx, bl, r, abortChan, scanCb); err != nil {
+ return err
+ }
+
+ // Scanning still in progress; cancel the operation.
+ return bf.scanCancel()
+}
+
+func (bf *BleFsm) scanCancel() error {
+ r := NewBleScanCancelReq()
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ if err := scanCancel(bf.bx, bl, r); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) terminateSetState() error {
+ bf.mtx.Lock()
+ defer bf.mtx.Unlock()
+
+ switch bf.state {
+ case SESN_STATE_UNCONNECTED,
+ SESN_STATE_CONNECTING,
+ SESN_STATE_CONN_CANCELLING:
+ return fmt.Errorf("BLE terminate failed; not connected")
+ case SESN_STATE_TERMINATING:
+ return fmt.Errorf(
+ "BLE terminate failed; session already being closed")
+ default:
+ bf.state = SESN_STATE_TERMINATING
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) terminate() error {
+ if err := bf.terminateSetState(); err != nil {
+ return err
+ }
+
+ r := NewBleTerminateReq()
+ r.ConnHandle = bf.connHandle
+ r.HciReason = ERR_CODE_HCI_REM_USER_CONN_TERM
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ if err := terminate(bf.bx, bl, r); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) connCancel() error {
+ if err := bf.transitionState(
+ SESN_STATE_CONNECTING,
+ SESN_STATE_CONN_CANCELLING); err != nil {
+
+ return fmt.Errorf("BLE connect cancel failed; not connecting")
+ }
+
+ r := NewBleConnCancelReq()
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ if err := connCancel(bf.bx, bl, r); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) discSvcUuid() error {
+ r := NewBleDiscSvcUuidReq()
+ r.ConnHandle = bf.connHandle
+ r.Uuid = bf.svcUuid
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ bf.nmpSvc, err = discSvcUuid(bf.bx, bl, r)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) discAllChrs() error {
+ r := NewBleDiscAllChrsReq()
+ r.ConnHandle = bf.connHandle
+ r.StartHandle = bf.nmpSvc.StartHandle
+ r.EndHandle = bf.nmpSvc.EndHandle
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ chrs, err := discAllChrs(bf.bx, bl, r)
+ if err != nil {
+ return err
+ }
+
+ for _, c := range chrs {
+ if CompareUuids(bf.reqChrUuid, c.Uuid) == 0 {
+ bf.nmpReqChr = c
+ }
+ if CompareUuids(bf.rspChrUuid, c.Uuid) == 0 {
+ bf.nmpRspChr = c
+ }
+ }
+
+ if bf.nmpReqChr == nil {
+ return fmt.Errorf(
+ "Peer doesn't support required characteristic: %s",
+ bf.reqChrUuid.String())
+ }
+
+ if bf.nmpRspChr == nil {
+ return fmt.Errorf(
+ "Peer doesn't support required characteristic: %s",
+ bf.rspChrUuid.String())
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) exchangeMtu() error {
+ r := NewBleExchangeMtuReq()
+ r.ConnHandle = bf.connHandle
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ mtu, err := exchangeMtu(bf.bx, bl, r)
+ if err != nil {
+ return err
+ }
+
+ bf.attMtu = mtu
+ return nil
+}
+
+func (bf *BleFsm) writeCmd(data []byte) error {
+ r := NewBleWriteCmdReq()
+ r.ConnHandle = bf.connHandle
+ r.AttrHandle = bf.nmpReqChr.ValHandle
+ r.Data.Bytes = data
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ if err := writeCmd(bf.bx, bl, r); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (bf *BleFsm) subscribe() error {
+ r := NewBleWriteCmdReq()
+ r.ConnHandle = bf.connHandle
+ r.AttrHandle = bf.nmpRspChr.ValHandle + 1
+ r.Data.Bytes = []byte{1, 0}
+
+ bl, err := bf.addBleSeqListener(r.Seq)
+ if err != nil {
+ return err
+ }
+ defer bf.removeBleSeqListener(r.Seq)
+
+ if err := writeCmd(bf.bx, bl, r); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// Tries to populate the FSM's peerDev field. This function succeeds if the
+// client specified the address of the peer to connect to.
+func (bf *BleFsm) tryFillPeerDev() bool {
+ // The peer spec contains one of:
+ // * Peer address;
+ // * Predicate function to call during scanning.
+ // If a peer address is specified, fill in the peer field now so the
+ // scanning step can be skipped. Otherwise, the peer field gets populated
+ // during scanning.
+ if bf.peerSpec.ScanPred == nil {
+ bf.peerDev = &bf.peerSpec.Dev
+ return true
+ }
+
+ return false
+}
+
+func (bf *BleFsm) Start() error {
+ if bf.getState() != SESN_STATE_UNCONNECTED {
+ return nmxutil.NewSesnAlreadyOpenError(
+ "Attempt to open an already-open BLE session")
+ }
+
+ for {
+ state := bf.getState()
+ switch state {
+ case SESN_STATE_UNCONNECTED:
+ var err error
+
+ // Determine if we can immediately initiate a connection, or if we
+ // need to scan for a peer first. If the client specified a peer
+ // address, or if we have already successfully scanned, we initiate
+ // a connection now. Otherwise, we need to scan to determine which
+ // peer meets the specified scan criteria.
+ bf.tryFillPeerDev()
+ if bf.peerDev == nil {
+ // Peer not inferred yet. Initiate scan.
+ cb := func() error { return bf.scan() }
+ err = bf.action(
+ SESN_STATE_UNCONNECTED,
+ SESN_STATE_SCANNING,
+ SESN_STATE_UNCONNECTED,
+ cb)
+ } else {
+ // We already know the address we want to connect to. Initiate
+ // a connection.
+ cb := func() error { return bf.connect() }
+ err = bf.action(
+ SESN_STATE_UNCONNECTED,
+ SESN_STATE_CONNECTING,
+ SESN_STATE_CONNECTED,
+ cb)
+ }
+
+ if err != nil {
+ return err
+ }
+
+ case SESN_STATE_CONNECTED:
+ cb := func() error { return bf.exchangeMtu() }
+ err := bf.action(
+ SESN_STATE_CONNECTED,
+ SESN_STATE_EXCHANGING_MTU,
+ SESN_STATE_EXCHANGED_MTU,
+ cb)
+ if err != nil {
+ return err
+ }
+
+ case SESN_STATE_EXCHANGED_MTU:
+ cb := func() error { return bf.discSvcUuid() }
+ err := bf.action(
+ SESN_STATE_EXCHANGED_MTU,
+ SESN_STATE_DISCOVERING_SVC,
+ SESN_STATE_DISCOVERED_SVC,
+ cb)
+ if err != nil {
+ return err
+ }
+
+ case SESN_STATE_DISCOVERED_SVC:
+ cb := func() error {
+ return bf.discAllChrs()
+ }
+
+ err := bf.action(
+ SESN_STATE_DISCOVERED_SVC,
+ SESN_STATE_DISCOVERING_CHR,
+ SESN_STATE_DISCOVERED_CHR,
+ cb)
+ if err != nil {
+ return err
+ }
+
+ if err := bf.subscribe(); err != nil {
+ return err
+ }
+
+ case SESN_STATE_DISCOVERED_CHR:
+ /* Open complete. */
+ return nil
+
+ case SESN_STATE_CONNECTING,
+ SESN_STATE_DISCOVERING_SVC,
+ SESN_STATE_DISCOVERING_CHR,
+ SESN_STATE_TERMINATING:
+ return fmt.Errorf("BleFsm already being opened")
+ }
+ }
+}
+
+// @return bool true if stop complete;
+// false if disconnect is now pending.
+func (bf *BleFsm) Stop() (bool, error) {
+ state := bf.getState()
+
+ switch state {
+ case SESN_STATE_UNCONNECTED,
+ SESN_STATE_TERMINATING,
+ SESN_STATE_CONN_CANCELLING:
+
+ return false,
+ bf.closedError("Attempt to close an unopened BLE session")
+
+ case SESN_STATE_CONNECTING:
+ if err := bf.connCancel(); err != nil {
+ return false, err
+ }
+ return true, nil
+
+ default:
+ if err := bf.terminate(); err != nil {
+ return false, err
+ }
+ return false, nil
+ }
+}
+
+func (bf *BleFsm) IsOpen() bool {
+ return bf.getState() == SESN_STATE_DISCOVERED_CHR
+}
+
+func (bf *BleFsm) TxNmp(payload []byte, nl *nmp.NmpListener,
+ timeout time.Duration) (nmp.NmpRsp, error) {
+
+ log.Debugf("Tx NMP request: %s", hex.Dump(payload))
+ if err := bf.writeCmd(payload); err != nil {
+ return nil, err
+ }
+
+ // Now wait for NMP response.
+ for {
+ select {
+ case err := <-nl.ErrChan:
+ return nil, err
+ case rsp := <-nl.RspChan:
+ // Only accept NMP responses if the session is still open. This is
+ // to help prevent race conditions in client code.
+ if bf.IsOpen() {
+ return rsp, nil
+ }
+ case <-nl.AfterTimeout(timeout):
+ return nil, nmxutil.NewNmpTimeoutError("NMP timeout")
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/15498bdc/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go
new file mode 100644
index 0000000..365efc6
--- /dev/null
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go
@@ -0,0 +1,198 @@
+package nmble
+
+import (
+ "fmt"
+ "sync"
+ "time"
+
+ . "mynewt.apache.org/newtmgr/nmxact/bledefs"
+ "mynewt.apache.org/newtmgr/nmxact/nmp"
+ "mynewt.apache.org/newtmgr/nmxact/omp"
+ "mynewt.apache.org/newtmgr/nmxact/sesn"
+ "mynewt.apache.org/newt/util"
+)
+
+type BleOicSesn struct {
+ bf *BleFsm
+ nls map[*nmp.NmpListener]struct{}
+ od *omp.OmpDispatcher
+ closeTimeout time.Duration
+ onCloseCb sesn.BleOnCloseFn
+
+ closeChan chan error
+ mx sync.Mutex
+}
+
+func NewBleOicSesn(bx *BleXport, cfg sesn.SesnCfg) *BleOicSesn {
+ bos := &BleOicSesn{
+ nls: map[*nmp.NmpListener]struct{}{},
+ od: omp.NewOmpDispatcher(),
+ closeTimeout: cfg.Ble.CloseTimeout,
+ onCloseCb: cfg.Ble.OnCloseCb,
+ }
+
+ svcUuid, err := ParseUuid(NmpOicSvcUuid)
+ if err != nil {
+ panic(err.Error())
+ }
+
+ reqChrUuid, err := ParseUuid(NmpOicReqChrUuid)
+ if err != nil {
+ panic(err.Error())
+ }
+
+ rspChrUuid, err := ParseUuid(NmpOicRspChrUuid)
+ if err != nil {
+ panic(err.Error())
+ }
+
+ bos.bf = NewBleFsm(BleFsmParams{
+ Bx: bx,
+ OwnAddrType: cfg.Ble.OwnAddrType,
+ PeerSpec: cfg.Ble.PeerSpec,
+ SvcUuid: svcUuid,
+ ReqChrUuid: reqChrUuid,
+ RspChrUuid: rspChrUuid,
+ RxNmpCb: func(d []byte) { bos.onRxNmp(d) },
+ DisconnectCb: func(p BleDev, e error) { bos.onDisconnect(p, e) },
+ })
+
+ return bos
+}
+
+func (bos *BleOicSesn) addNmpListener(seq uint8) (*nmp.NmpListener, error) {
+ nl := nmp.NewNmpListener()
+ bos.nls[nl] = struct{}{}
+
+ if err := bos.od.AddListener(seq, nl); err != nil {
+ delete(bos.nls, nl)
+ return nil, err
+ }
+
+ return nl, nil
+}
+
+func (bos *BleOicSesn) removeNmpListener(seq uint8) {
+ listener := bos.od.RemoveListener(seq)
+ if listener != nil {
+ delete(bos.nls, listener)
+ }
+}
+
+// Returns true if a new channel was assigned.
+func (bos *BleOicSesn) setCloseChan() bool {
+ bos.mx.Lock()
+ defer bos.mx.Unlock()
+
+ if bos.closeChan != nil {
+ return false
+ }
+
+ bos.closeChan = make(chan error, 1)
+ return true
+}
+
+func (bos *BleOicSesn) clearCloseChan() {
+ bos.mx.Lock()
+ defer bos.mx.Unlock()
+
+ bos.closeChan = nil
+}
+
+func (bos *BleOicSesn) AbortRx(seq uint8) error {
+ return bos.od.FakeRxError(seq, fmt.Errorf("Rx aborted"))
+}
+
+func (bos *BleOicSesn) Open() error {
+ return bos.bf.Start()
+}
+
+func (bos *BleOicSesn) Close() error {
+ if !bos.setCloseChan() {
+ return bos.bf.closedError(
+ "Attempt to close an unopened BLE session")
+ }
+ defer bos.clearCloseChan()
+
+ done, err := bos.bf.Stop()
+ if err != nil {
+ return err
+ }
+
+ if done {
+ // Close complete.
+ return nil
+ }
+
+ // Block until close completes or timeout.
+ select {
+ case <-bos.closeChan:
+ case <-time.After(bos.closeTimeout):
+ }
+
+ return nil
+}
+
+func (bos *BleOicSesn) IsOpen() bool {
+ return bos.bf.IsOpen()
+}
+
+func (bos *BleOicSesn) onRxNmp(data []byte) {
+ bos.od.Dispatch(data)
+}
+
+func (bos *BleOicSesn) onDisconnect(peer BleDev, err error) {
+ for nl, _ := range bos.nls {
+ nl.ErrChan <- err
+ }
+
+ // If the session is being closed, unblock the close() call.
+ if bos.closeChan != nil {
+ bos.closeChan <- err
+ }
+ if bos.onCloseCb != nil {
+ bos.onCloseCb(bos, peer, err)
+ }
+}
+
+func (bos *BleOicSesn) EncodeNmpMsg(m *nmp.NmpMsg) ([]byte, error) {
+ return omp.EncodeOmpTcp(m)
+}
+
+// Blocking.
+func (bos *BleOicSesn) TxNmpOnce(m *nmp.NmpMsg, opt sesn.TxOptions) (
+ nmp.NmpRsp, error) {
+
+ if !bos.IsOpen() {
+ return nil, bos.bf.closedError(
+ "Attempt to transmit over closed BLE session")
+ }
+
+ nl, err := bos.addNmpListener(m.Hdr.Seq)
+ if err != nil {
+ return nil, err
+ }
+ defer bos.removeNmpListener(m.Hdr.Seq)
+
+ b, err := bos.EncodeNmpMsg(m)
+ if err != nil {
+ return nil, err
+ }
+
+ return bos.bf.TxNmp(b, nl, opt.Timeout)
+}
+
+func (bos *BleOicSesn) MtuIn() int {
+ return bos.bf.attMtu -
+ NOTIFY_CMD_BASE_SZ -
+ omp.OMP_MSG_OVERHEAD -
+ nmp.NMP_HDR_SIZE
+}
+
+func (bos *BleOicSesn) MtuOut() int {
+ mtu := bos.bf.attMtu -
+ WRITE_CMD_BASE_SZ -
+ omp.OMP_MSG_OVERHEAD -
+ nmp.NMP_HDR_SIZE
+ return util.IntMin(mtu, BLE_ATT_ATTR_MAX_LEN)
+}