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/04/05 01:44:34 UTC
[5/6] incubator-mynewt-newtmgr git commit: nmxact - Add ble_loop
example.
nmxact - Add ble_loop example.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/commit/2f603eac
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/2f603eac
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/2f603eac
Branch: refs/heads/master
Commit: 2f603eac7ab7ef428fe8b83a3050c6ef8007ef2c
Parents: 2d55058
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Apr 4 18:08:33 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Apr 4 18:10:25 2017 -0700
----------------------------------------------------------------------
nmxact/build-examples.sh | 1 +
nmxact/example/ble_loop/ble_loop.go | 104 +++++++++++++++++++++++++++++++
2 files changed, 105 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/2f603eac/nmxact/build-examples.sh
----------------------------------------------------------------------
diff --git a/nmxact/build-examples.sh b/nmxact/build-examples.sh
index 7898948..a751f5f 100755
--- a/nmxact/build-examples.sh
+++ b/nmxact/build-examples.sh
@@ -1,6 +1,7 @@
#!/bin/sh
read -r -d '' exdirs <<-'EOS'
+ ble_loop
ble_plain
serial_plain
EOS
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/2f603eac/nmxact/example/ble_loop/ble_loop.go
----------------------------------------------------------------------
diff --git a/nmxact/example/ble_loop/ble_loop.go b/nmxact/example/ble_loop/ble_loop.go
new file mode 100644
index 0000000..c76b11c
--- /dev/null
+++ b/nmxact/example/ble_loop/ble_loop.go
@@ -0,0 +1,104 @@
+/**
+ * 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 main
+
+import (
+ "fmt"
+ "os"
+
+ "mynewt.apache.org/newtmgr/nmxact/bledefs"
+ "mynewt.apache.org/newtmgr/nmxact/nmble"
+ "mynewt.apache.org/newtmgr/nmxact/sesn"
+ "mynewt.apache.org/newtmgr/nmxact/xact"
+)
+
+func main() {
+ // Initialize the BLE transport.
+ params := nmble.NewXportCfg()
+ params.SockPath = "/tmp/blehostd-uds"
+ params.BlehostdPath = "blehostd.elf"
+ params.DevPath = "/dev/cu.usbmodem142111"
+
+ x, err := nmble.NewBleXport(params)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "error creating BLE transport: %s\n",
+ err.Error())
+ os.Exit(1)
+ }
+
+ // Start the BLE transport.
+ if err := x.Start(); err != nil {
+ fmt.Fprintf(os.Stderr, "error starting BLE transport: %s\n",
+ err.Error())
+ os.Exit(1)
+ }
+ defer x.Stop()
+
+ // Prepare a BLE session:
+ // * Plain NMP (not tunnelled over OIC).
+ // * We use a random address.
+ // * Peer has name "nimble-bleprph".
+ sc := sesn.NewSesnCfg()
+ sc.MgmtProto = sesn.MGMT_PROTO_NMP
+ sc.Ble.OwnAddrType = bledefs.BLE_ADDR_TYPE_RANDOM
+ sc.Ble.PeerSpec = sesn.BlePeerSpecName("nimble-bleprph")
+
+ s, err := x.BuildSesn(sc)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "error creating BLE session: %s\n", err.Error())
+ os.Exit(1)
+ }
+
+ // Repeatedly:
+ // * Connect to peer if unconnected.
+ // * Send an echo command to peer.
+ //
+ // If blehostd crashes or the controller is unplugged, nmxact should
+ // recover on the next connect attempt.
+ for {
+ if !s.IsOpen() {
+ // Connect to the peer (open the session).
+ if err := s.Open(); err != nil {
+ fmt.Fprintf(os.Stderr, "error starting BLE session: %s\n",
+ err.Error())
+ continue
+ }
+ }
+
+ // Send an echo command to the peer.
+ c := xact.NewEchoCmd()
+ c.Payload = "hello"
+
+ res, err := c.Run(s)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "error executing echo command: %s\n",
+ err.Error())
+ continue
+ }
+
+ if res.Status() != 0 {
+ fmt.Printf("Peer responded negatively to echo command; status=%d\n",
+ res.Status())
+ }
+
+ eres := res.(*xact.EchoResult)
+ fmt.Printf("Peer echoed back: %s\n", eres.Rsp.Payload)
+ }
+}