Skip to content

Commit

Permalink
Fix connection issues and encoding scheme (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krajiyah authored Feb 28, 2020
1 parent 591ceb9 commit 2056cf2
Show file tree
Hide file tree
Showing 26 changed files with 1,265 additions and 1,137 deletions.
8 changes: 6 additions & 2 deletions examples/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type exampleListener struct {
c *client.BLEClient
}

func (l *exampleListener) OnConnected(addr string, attempts int, rssi int) {
fmt.Printf("Client connected to server (%s) after %d attempts with rssi %d", addr, attempts, rssi)
func (l *exampleListener) OnConnected(addr string, rssi int) {
fmt.Printf("Client connected to server (%s) with rssi %d", addr, rssi)
for {
time.Sleep(time.Second * 1)
fmt.Println("Attempting to write to extra write char...")
Expand All @@ -49,6 +49,10 @@ func (l *exampleListener) OnTimeSync() {
fmt.Println("Client has syncronized time with server.")
}

func (l *exampleListener) OnInternalError(err error) {
fmt.Println("Internal Error: " + err.Error())
}

func main() {
if Name == "" || BLESecret == "" || BLEClientAddr == "" || BLEServerAddr == "" {
fmt.Println("please compile this with BLESecret, BLEClientAddr, and BLEServerAddr as ldflag")
Expand Down
6 changes: 2 additions & 4 deletions examples/forwarder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ type myListener struct{}
func (l myListener) OnServerStatusChanged(s models.BLEServerStatus, err error) {}
func (l myListener) OnClientStateMapChanged(m map[string]models.BLEClientState) {}
func (l myListener) OnClientLog(r models.ClientLogRequest) {}
func (l myListener) OnConnectionError(err error) {}
func (l myListener) OnReadOrWriteError(err error) {}
func (l myListener) OnError(err error) {}
func (l myListener) OnConnected(addr string, attempts int, rssi int) {}
func (l myListener) OnInternalError(err error) {}
func (l myListener) OnConnected(addr string, rssi int) {}
func (l myListener) OnDisconnected() {}
func (l myListener) OnTimeSync() {}

Expand Down
8 changes: 2 additions & 6 deletions examples/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (l myServerListener) OnClientStateMapChanged(c *models.ConnectionGraph, r *
func (l myServerListener) OnClientLog(r models.ClientLogRequest) {
fmt.Println(fmt.Sprintf("Client pushed log entry: %+v", r))
}
func (l myServerListener) OnReadOrWriteError(err error) {
func (l myServerListener) OnInternalError(err error) {
fmt.Println(fmt.Sprintf("There was an error in handling a read or write operations from a characteristic: %s", err))
}

Expand Down Expand Up @@ -66,12 +66,8 @@ func main() {
}},
}
fmt.Println("Starting BLE server...")
serv, err := server.NewBLEServer(Name, BLESecret, bluetoothAddress, myServerListener{}, moreReadChars, moreWriteChars)
_, _, err := server.NewBLEServer(Name, BLESecret, bluetoothAddress, nil, myServerListener{}, moreReadChars, moreWriteChars)
if err != nil {
fmt.Println("Ooops! Something went wrong with setting up ble server: " + err.Error())
}
err = serv.Run()
if err != nil {
fmt.Println("Ooops! Something went wrong with running the ble server: " + err.Error())
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/RyanCarrier/dijkstra v1.0.0
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013
github.com/currantlabs/ble v0.0.0-20171229162446-c1d21c164cf8
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.7.1
github.com/go-ble/ble v0.0.0-20200120171844-0a73a9da88eb
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/albertorestifo/dijkstra v0.0.0-20160910063646-aba76f725f72/go.mod h1:
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013 h1:/P9/RL0xgWE+ehnCUUN5h3RpG3dmoMCOONO1CCvq23Y=
github.com/bradfitz/slice v0.0.0-20180809154707-2b758aa73013/go.mod h1:pccXHIvs3TV/TUqSNyEvF99sxjX2r4FFRIyw6TZY9+w=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/currantlabs/ble v0.0.0-20171229162446-c1d21c164cf8 h1:eo7L0zxxFowLpF4FNlLijrAMVNlq9h8sicNwMfzauM8=
github.com/currantlabs/ble v0.0.0-20171229162446-c1d21c164cf8/go.mod h1:MGpIf7cfnYPFaMIcD8LoSgCr8Jsa4rUcV5Nb9temsYw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9rTHJQ=
Expand Down
80 changes: 80 additions & 0 deletions internal/testutils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package internal

import (
"bytes"
"context"
"errors"
"time"

"github.com/Krajiyah/ble-sdk/pkg/models"
"github.com/Krajiyah/ble-sdk/pkg/util"
"github.com/go-ble/ble"
)

type TestListener struct {
Rssi int
}

func (l *TestListener) OnDisconnected() {}
func (l *TestListener) OnTimeSync() {}
func (l *TestListener) OnInternalError(error) {}
func (l *TestListener) OnConnected(_ string, r int) {
l.Rssi = r
}

type DummyAdv struct {
Address ble.Addr
Rssi int
Expand Down Expand Up @@ -49,3 +66,66 @@ func GetTestServices(charUUIDs []string) []*ble.Service {
}
return []*ble.Service{&ble.Service{u, chars, 0, 0}}
}

type TestConnection struct {
srcAddr string
connectedAddr string
toConnectAddr string
rssiMap *models.RssiMap
mockedReadValue map[string]*bytes.Buffer
mockedWriteValue map[string]*bytes.Buffer
}

func NewTestConnection(addr string, toConnectAddr string, rm *models.RssiMap) *TestConnection {
return &TestConnection{toConnectAddr: toConnectAddr, srcAddr: addr, rssiMap: rm, mockedReadValue: map[string]*bytes.Buffer{}, mockedWriteValue: map[string]*bytes.Buffer{}}
}

func (c *TestConnection) Context() context.Context { return context.TODO() }

func (c *TestConnection) SetToConnectAddr(addr string) { c.toConnectAddr = addr }

func (c *TestConnection) SetConnectedAddr(addr string) { c.connectedAddr = addr }

func (c *TestConnection) GetMockedReadBuffer(uuid string) *bytes.Buffer {
if val, ok := c.mockedReadValue[uuid]; ok {
return val
}
buff := bytes.NewBuffer([]byte{})
c.mockedReadValue[uuid] = buff
return buff
}

func (c *TestConnection) GetMockedWriteBufferData(uuid string) []byte {
return c.mockedWriteValue[uuid].Bytes()
}

func (c *TestConnection) GetConnectedAddr() string { return c.connectedAddr }
func (c *TestConnection) GetRssiMap() *models.RssiMap { return c.rssiMap }
func (c *TestConnection) Connect(ble.AdvFilter) error {
c.connectedAddr = c.toConnectAddr
return nil
}
func (c *TestConnection) Dial(a string) error {
c.connectedAddr = a
return nil
}
func (c *TestConnection) ScanForDuration(time.Duration, func(ble.Advertisement)) error {
return nil
}
func (c *TestConnection) Scan(fn func(ble.Advertisement)) error {
for addr, rssi := range c.rssiMap.GetAll()[c.srcAddr] {
fn(DummyAdv{DummyAddr{addr}, rssi, false})
}
return nil
}
func (c *TestConnection) ReadValue(uuid string) ([]byte, error) {
val, ok := c.mockedReadValue[uuid]
if ok {
return val.Bytes(), nil
}
return nil, errors.New("UUID not in mocked read value: " + uuid)
}
func (c *TestConnection) WriteValue(uuid string, data []byte) error {
c.mockedWriteValue[uuid] = bytes.NewBuffer(data)
return nil
}
Loading

0 comments on commit 2056cf2

Please sign in to comment.