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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
n = n - 4
|
n = len(p)
|
||||||
|
|
||||||
// finally, wait for the channel we got to return. this means that
|
// finally, wait for the channel we got to return. this means that
|
||||||
// the matching response frame was received, so we can parse it.
|
// the matching response frame was received, so we can parse it.
|
||||||
// TODO: add timeout.
|
|
||||||
|
|
||||||
var status *TxStatusFrame
|
var status *TxStatusFrame
|
||||||
select {
|
select {
|
||||||
case responseFrame := <-ch:
|
case responseFrame := <-ch:
|
||||||
status, err = ParseTxStatusFrame(responseFrame)
|
status, err = ParseTxStatusFrame(responseFrame)
|
||||||
case <-time.After(1 * time.Second):
|
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 {
|
func (c *Conn) Close() error {
|
||||||
// remove ourselves from the conn list.
|
// remove ourselves from the conn list.
|
||||||
|
delete(c.parent.conns, uint64(c.addr))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,7 +377,9 @@ func ParseDeviceString(dev string) (*Transport, error) {
|
||||||
|
|
||||||
addr, _ := strings.CutPrefix(dev, "tcp://")
|
addr, _ := strings.CutPrefix(dev, "tcp://")
|
||||||
|
|
||||||
|
// FIXME: use default port (9750) if port not provided.
|
||||||
conn, err := net.Dial("tcp", addr)
|
conn, err := net.Dial("tcp", addr)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,12 @@ package xbee
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func TestXBeeHardware(t *testing.T) {
|
func TestXBeeHardware(t *testing.T) {
|
||||||
// this test runs only if the environemnt variable
|
// this test runs only if the environemnt variable
|
||||||
// XBEE_DEVICE is set.
|
// XBEE_DEVICE is set.
|
||||||
|
@ -27,7 +27,6 @@ func TestXBeeHardware(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var sess *Session = nil
|
var sess *Session = nil
|
||||||
defer sess.Close()
|
|
||||||
// test connection.
|
// test connection.
|
||||||
t.Run("Connect to device", func(t *testing.T) {
|
t.Run("Connect to device", func(t *testing.T) {
|
||||||
dev, err := ParseDeviceString(devStr)
|
dev, err := ParseDeviceString(devStr)
|
||||||
|
@ -44,11 +43,11 @@ func TestXBeeHardware(t *testing.T) {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// now we should test sending a packet. and getting a response.
|
// now we should test sending a packet. and getting a response.
|
||||||
|
|
||||||
t.Run("Get Network ID", func(t *testing.T) {
|
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 {
|
if err != nil {
|
||||||
t.Errorf("ATCommand() error = %v", err)
|
t.Errorf("ATCommand() error = %v", err)
|
||||||
}
|
}
|
||||||
|
@ -58,17 +57,50 @@ func TestXBeeHardware(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Check NP", func(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 {
|
if err != nil {
|
||||||
t.Errorf("ATCommand() error = %v", err)
|
t.Errorf("ATCommand() error = %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
val := binary.BigEndian.Uint16(b)
|
val := binary.BigEndian.Uint16(b)
|
||||||
if val != 0x100 {
|
if val != 0x100 && val != 0x640 {
|
||||||
t.Errorf("NP response wrong, expected 0x100 got %x", val)
|
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