From e99af632fc50813b61f29ccfc38f5f6f69fcb40f Mon Sep 17 00:00:00 2001 From: saji <9110284+kschamplin@users.noreply.github.com> Date: Wed, 10 May 2023 01:13:31 -0500 Subject: [PATCH] xbee: add session test --- xbee/session_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 xbee/session_test.go diff --git a/xbee/session_test.go b/xbee/session_test.go new file mode 100644 index 0000000..85f7b19 --- /dev/null +++ b/xbee/session_test.go @@ -0,0 +1,74 @@ +/* +Package xbee provides communication and configuration of Digi XBee products + +(and other Digi products that are similar such as the XLR Pro). It provides +a net.Conn-like interface as well as AT commands for configuration. The most +common usage of the package is with a Session, which provides +*/ +package xbee + +import ( + "encoding/binary" + "os" + "testing" + + "golang.org/x/exp/slog" +) + + +func TestXBeeHardware(t *testing.T) { + // this test runs only if the environemnt variable + // XBEE_DEVICE is set. + + devStr, ok := os.LookupEnv("XBEE_DEVICE") + + if !ok { + t.Skip("No XBee device provided") + } + + var sess *Session = nil + defer sess.Close() + // test connection. + t.Run("Connect to device", func(t *testing.T) { + dev, err := ParseDeviceString(devStr) + if err != nil { + t.Errorf("ParseDeviceString() error = %v", err) + } + sess, err = NewSession(dev, slog.With("type", dev.Type())) + if err != nil { + t.Errorf("NewSession() error = %v", err) + } + // err = sess.Close() + // if err != nil { + // t.Errorf("Session.Close() error = %v", err) + // } + + }) + + // 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) + if err != nil { + t.Errorf("ATCommand() error = %v", err) + } + if len(b) != 2 { + t.Errorf("reponse length mismatch: expected 2 got %d", len(b)) + } + }) + + t.Run("Check NP", func(t *testing.T) { + 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) + } + }) + + +}