xbee: cleanup, fix session test, add parseDeviceString test
This commit is contained in:
parent
08ab0e3509
commit
de57b38958
|
@ -177,18 +177,17 @@ func (sess *Session) writeAddr(p []byte, dest uint64) (n int, err error) {
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
n = n - 4
|
||||
n = len(p)
|
||||
|
||||
// finally, wait for the channel we got to return. this means that
|
||||
// the matching response frame was received, so we can parse it.
|
||||
// TODO: add timeout.
|
||||
|
||||
var status *TxStatusFrame
|
||||
select {
|
||||
case responseFrame := <-ch:
|
||||
status, err = ParseTxStatusFrame(responseFrame)
|
||||
case <-time.After(1 * time.Second):
|
||||
return n, errors.New("timeout waiting for response")
|
||||
return 0, errors.New("timeout waiting for response")
|
||||
}
|
||||
|
||||
|
||||
|
@ -321,7 +320,7 @@ func (c *Conn) Read(p []byte) (n int, err error) {
|
|||
|
||||
func (c *Conn) Close() error {
|
||||
// remove ourselves from the conn list.
|
||||
|
||||
delete(c.parent.conns, uint64(c.addr))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -378,7 +377,9 @@ func ParseDeviceString(dev string) (*Transport, error) {
|
|||
|
||||
addr, _ := strings.CutPrefix(dev, "tcp://")
|
||||
|
||||
// FIXME: use default port (9750) if port not provided.
|
||||
conn, err := net.Dial("tcp", addr)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -10,12 +10,12 @@ package xbee
|
|||
import (
|
||||
"encoding/binary"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
|
||||
func TestXBeeHardware(t *testing.T) {
|
||||
// this test runs only if the environemnt variable
|
||||
// XBEE_DEVICE is set.
|
||||
|
@ -27,7 +27,6 @@ func TestXBeeHardware(t *testing.T) {
|
|||
}
|
||||
|
||||
var sess *Session = nil
|
||||
defer sess.Close()
|
||||
// test connection.
|
||||
t.Run("Connect to device", func(t *testing.T) {
|
||||
dev, err := ParseDeviceString(devStr)
|
||||
|
@ -48,7 +47,7 @@ func TestXBeeHardware(t *testing.T) {
|
|||
// now we should test sending a packet. and getting a response.
|
||||
|
||||
t.Run("Get Network ID", func(t *testing.T) {
|
||||
b, err := sess.ATCommand([2]rune{'I','D'}, nil, false)
|
||||
b, err := sess.ATCommand([2]rune{'I', 'D'}, nil, false)
|
||||
if err != nil {
|
||||
t.Errorf("ATCommand() error = %v", err)
|
||||
}
|
||||
|
@ -58,17 +57,50 @@ func TestXBeeHardware(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("Check NP", func(t *testing.T) {
|
||||
b, err := sess.ATCommand([2]rune{'N','P'}, nil, false)
|
||||
b, err := sess.ATCommand([2]rune{'N', 'P'}, nil, false)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("ATCommand() error = %v", err)
|
||||
}
|
||||
|
||||
val := binary.BigEndian.Uint16(b)
|
||||
if val != 0x100 {
|
||||
t.Errorf("NP response wrong, expected 0x100 got %x", val)
|
||||
if val != 0x100 && val != 0x640 {
|
||||
t.Errorf("NP response wrong, expected 0x100 or 0x640 got 0x%X", val)
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
func TestParseDeviceString(t *testing.T) {
|
||||
type args struct {
|
||||
dev string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *Transport
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "invalid stuff",
|
||||
args: args{
|
||||
dev: "blah",
|
||||
},
|
||||
want: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
// TODO: moar tests!
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseDeviceString(tt.args.dev)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ParseDeviceString() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("ParseDeviceString() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue