gotelem/xbee/session.go

423 lines
11 KiB
Go
Raw Normal View History

2023-05-08 02:00:34 +00:00
/*
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
*/
2023-04-29 15:58:56 +00:00
package xbee
import (
"bufio"
2023-05-10 18:58:46 +00:00
"encoding/binary"
2023-05-10 17:55:51 +00:00
"errors"
2023-04-29 15:58:56 +00:00
"fmt"
"io"
"net"
2023-05-10 17:55:51 +00:00
"runtime"
"strconv"
2023-05-10 17:55:51 +00:00
"strings"
"sync"
2023-05-11 00:02:57 +00:00
"time"
2023-04-29 15:58:56 +00:00
"log/slog"
"go.bug.st/serial"
2023-04-29 15:58:56 +00:00
)
2023-05-08 02:00:34 +00:00
// TODO: implement net.Conn for Session/Conn. We are missing LocalAddr, RemoteAddr,
// and Deadline related methods.
2023-05-07 13:39:10 +00:00
2023-05-10 18:58:46 +00:00
// XBeeAddr is an XBee device address.
type XBeeAddr uint64
2023-05-11 00:02:57 +00:00
func (addr XBeeAddr) String() string {
2023-05-10 18:58:46 +00:00
return fmt.Sprintf("%X", uint64(addr))
}
func (addr XBeeAddr) Network() string {
return "xbee"
}
2023-05-08 02:00:34 +00:00
// Session represents a connection to a locally-attached XBee. The connection can be through
// serial/USB or TCP/IP depending on what is supported by the device.
2023-05-10 17:55:51 +00:00
// Session implements the net.Conn interface, so it can be used anywhere a net.Conn can be used.
// This also means that deadlines can be set.
2023-05-07 13:39:10 +00:00
type Session struct {
ioDev io.ReadWriteCloser
ct connTrack
2023-05-08 02:00:34 +00:00
log slog.Logger
2023-04-29 15:58:56 +00:00
// this buffer is used for storing data that must be read at some point.
2023-05-08 02:00:34 +00:00
rxBuf *bufio.ReadWriter
2023-04-29 15:58:56 +00:00
writeLock sync.Mutex // prevents multiple writers from accessing the port at once.
2023-05-08 02:00:34 +00:00
// conns contain a map of addresses to connections. This means that there
// can only be one direct connection to a device. This is pretty reasonable IMO.
// but needs to be documented very clearly.
conns map[uint64]*Conn
2023-05-13 15:38:35 +00:00
// local address
lAddr XBeeAddr
2023-04-29 15:58:56 +00:00
}
2023-05-08 02:00:34 +00:00
// NewSession takes an IO device and a logger and returns a new XBee session.
2023-05-07 13:39:10 +00:00
func NewSession(dev io.ReadWriteCloser, baseLog *slog.Logger) (*Session, error) {
sess := &Session{
2023-05-08 02:00:34 +00:00
ioDev: dev,
log: *baseLog,
ct: *NewConnTrack(),
2023-04-29 15:58:56 +00:00
}
// setup io readwriter with a pipe.
rd, wr := io.Pipe()
// this is for reading data *only* - writes are different! it's a
// readWriter because the goroutine that runs scan continuously (and handles all other packets)
// will write to the buffer when new Rx packets come in, and we can read out from application code.
sess.rxBuf = bufio.NewReadWriter(bufio.NewReader(rd), bufio.NewWriter(wr))
// start the rx handler in the background. we close it later by closing the serial port.
go sess.rxHandler()
2023-05-13 15:38:35 +00:00
// now we should get the local address cached so LocalAddr is fast.
sh, err := sess.ATCommand([2]byte{'S', 'H'}, nil, false)
if err != nil {
return sess, errors.New("error getting SH")
}
sl, err := sess.ATCommand([2]byte{'S', 'L'}, nil, false)
if err != nil {
return sess, errors.New("error getting SL")
}
addr := append(sh, sl...)
sess.lAddr = XBeeAddr(binary.BigEndian.Uint64(addr))
2023-04-29 15:58:56 +00:00
return sess, nil
}
// before we can write `Read(p []byte)` we have to have a goroutine that takes the input from
// the serial port and parses it out - if it's data, we push the data to a buffer for
// the application to read the bytes on its own.
//
// if it's a different kind of packet, we do custom functionality (free the conntrack, update
// local status, etc)
2023-05-07 13:39:10 +00:00
func (sess *Session) rxHandler() {
2023-04-29 15:58:56 +00:00
// we wrap the serial port read line in a bufio.scanner using our custom split function.
2023-05-07 13:39:10 +00:00
scan := bufio.NewScanner(sess.ioDev)
2023-04-29 15:58:56 +00:00
scan.Split(xbeeFrameSplit)
2023-05-13 15:38:35 +00:00
sess.log.Debug("starting rx handler", "device", sess.ioDev)
2023-05-08 02:00:34 +00:00
// scan.Scan() will return false when there's EOF, i.e the io device is closed.
// this is activated by sess.Close()
2023-04-29 15:58:56 +00:00
for scan.Scan() {
data, err := parseFrame(scan.Bytes())
2023-05-13 15:38:35 +00:00
sess.log.Debug("got an api frame", "data", data)
2023-04-29 15:58:56 +00:00
if err != nil {
2023-05-08 02:00:34 +00:00
sess.log.Warn("error parsing frame", "error", err, "data", data)
2023-04-29 15:58:56 +00:00
continue
}
switch XBeeCmd(data[0]) {
case RxPktType:
// we parse the data, and push it to the rx buffer.
2023-05-07 05:00:35 +00:00
//TODO: if we have multiple remotes on the network, we need to track them here.
2023-04-29 15:58:56 +00:00
frame, err := ParseRxFrame(data)
if err != nil {
2023-05-08 02:00:34 +00:00
sess.log.Warn("error parsing rx packet", "error", err, "data", data)
2023-04-29 15:58:56 +00:00
break //continue?
}
2023-05-11 04:56:45 +00:00
// write it to either the connection or the default buffer.
if c, ok := sess.conns[frame.Source]; ok {
_, err = c.rxBuf.Write(frame.Payload)
} else {
_, err = sess.rxBuf.Write(frame.Payload)
}
2023-04-29 15:58:56 +00:00
if err != nil {
2023-05-08 02:00:34 +00:00
sess.log.Warn("error writing data", "error", err, "payload", frame.Payload)
2023-04-29 15:58:56 +00:00
}
2023-05-07 05:00:35 +00:00
case TxStatusType, ATCmdResponseType, RemoteCmdRespType:
// we hand the frame back via the channel. we directly find the ID since it's always
2023-04-29 15:58:56 +00:00
// the second byte.
idx := data[1]
err := sess.ct.ClearMark(idx, data)
if err != nil {
// we got a rogue packet lol
2023-05-08 02:00:34 +00:00
sess.log.Warn("rogue frame ID", "id", idx, "error", err)
2023-04-29 15:58:56 +00:00
}
default:
// we don't know what to do with it.
2023-05-08 02:00:34 +00:00
sess.log.Info("unhandled packet type", "type", data[0], "id", data[1])
2023-04-29 15:58:56 +00:00
}
}
2023-05-01 14:49:47 +00:00
// if we get here, the serial port has closed. this is fine.
2023-05-08 02:00:34 +00:00
sess.log.Debug("closing rx handler", "err", scan.Err())
2023-04-29 15:58:56 +00:00
}
// This implements io.Reader for the UART Session.
2023-05-07 13:39:10 +00:00
func (sess *Session) Read(p []byte) (int, error) {
2023-04-29 15:58:56 +00:00
// Since we have an rx buffer, we just read from that and return the results.
return sess.rxBuf.Read(p)
}
2023-05-08 02:00:34 +00:00
// Write sends a message to all XBees listening on the network. To send a message to a specific
// XBee, use Dial() to get a Conn
func (sess *Session) Write(p []byte) (int, error) {
return sess.writeAddr(p, 0xFFFF)
}
2023-05-11 04:56:45 +00:00
// internal function used to write data to a specific address.
// The Write() call uses 0xFFFF (broadcast address).
2023-05-08 02:00:34 +00:00
func (sess *Session) writeAddr(p []byte, dest uint64) (n int, err error) {
2023-04-29 15:58:56 +00:00
idx, ch, err := sess.ct.GetMark()
if err != nil {
return
}
2023-05-07 05:00:35 +00:00
2023-04-29 15:58:56 +00:00
wf := &TxFrame{
Id: idx,
2023-05-08 02:00:34 +00:00
Destination: dest,
2023-04-29 15:58:56 +00:00
Payload: p,
}
sess.writeLock.Lock()
2023-05-08 02:00:34 +00:00
n, err = writeXBeeFrame(sess.ioDev, wf.Bytes())
2023-05-03 15:38:22 +00:00
sess.writeLock.Unlock()
2023-04-29 15:58:56 +00:00
if err != nil {
return
}
n = len(p)
2023-04-29 15:58:56 +00:00
// finally, wait for the channel we got to return. this means that
// the matching response frame was received, so we can parse it.
2023-05-11 04:56:45 +00:00
var status *TxStatusFrame
select {
case responseFrame := <-ch:
status, err = ParseTxStatusFrame(responseFrame)
case <-time.After(1 * time.Second):
return 0, errors.New("timeout waiting for response")
2023-05-11 04:56:45 +00:00
}
2023-04-29 15:58:56 +00:00
// this is a tx status frame bytes, so lets parse it out.
if err != nil {
return
}
if status.Status != 0 {
err = fmt.Errorf("tx failed 0x%x", status.Status)
}
return
}
// sends a local AT command. If `queued` is true, the command is not immediately applied;
// instead, an AC command must be set to apply the queued changes. `queued` does not
// affect query-type commands, which always return right away.
// the AT command is an interface.
2023-05-13 15:38:35 +00:00
func (sess *Session) ATCommand(cmd [2]byte, data []byte, queued bool) (payload []byte, err error) {
2023-04-29 15:58:56 +00:00
// we must encode the command, and then create the actual packet.
// then we send the packet, and wait for the response
// TODO: how to handle multiple-response-packet AT commands?
// (mainly Node Discovery ND)
// get a mark for the frame
idx, ch, err := sess.ct.GetMark()
if err != nil {
2023-05-03 15:38:22 +00:00
return nil, err
2023-04-29 15:58:56 +00:00
}
2023-05-03 15:38:22 +00:00
rawData := encodeATCommand(cmd, data, idx, queued)
2023-04-29 15:58:56 +00:00
sess.writeLock.Lock()
2023-05-07 13:39:10 +00:00
_, err = writeXBeeFrame(sess.ioDev, rawData)
2023-04-29 15:58:56 +00:00
sess.writeLock.Unlock()
if err != nil {
2023-05-03 15:38:22 +00:00
return nil, fmt.Errorf("error writing xbee frame: %w", err)
2023-04-29 15:58:56 +00:00
}
2023-05-11 04:56:45 +00:00
var resp *ATCmdResponse
2023-05-11 00:02:57 +00:00
select {
2023-05-11 04:56:45 +00:00
case b := <-ch:
resp, err = ParseATCmdResponse(b)
2023-05-11 00:02:57 +00:00
case <-time.After(1 * time.Second):
return nil, errors.New("timeout waiting for response frame")
}
2023-05-11 04:56:45 +00:00
2023-04-29 15:58:56 +00:00
if err != nil {
2023-05-03 15:38:22 +00:00
return nil, err
2023-04-29 15:58:56 +00:00
}
if resp.Status != 0 {
2023-05-07 05:00:35 +00:00
return resp.Data, fmt.Errorf("AT command failed: %v", resp.Status)
2023-04-29 15:58:56 +00:00
}
2023-05-07 05:00:35 +00:00
return resp.Data, nil
2023-04-29 15:58:56 +00:00
}
// Does this need to exist?
2023-05-07 13:39:10 +00:00
func (sess *Session) GetStatus() {
2023-04-29 15:58:56 +00:00
panic("TODO: implement")
}
// Implement the io.Closer.
2023-05-07 13:39:10 +00:00
func (sess *Session) Close() error {
return sess.ioDev.Close()
2023-04-29 15:58:56 +00:00
}
2023-05-03 05:29:02 +00:00
2023-05-10 18:58:46 +00:00
func (sess *Session) LocalAddr() XBeeAddr {
2023-05-13 15:38:35 +00:00
return sess.lAddr
2023-05-10 18:58:46 +00:00
}
func (sess *Session) RemoteAddr() XBeeAddr {
return 0xFFFF
}
2023-05-11 04:56:45 +00:00
func (sess *Session) Dial(addr uint64) (conn *Conn, err error) {
if _, exist := sess.conns[addr]; exist {
return nil, errors.New("address already in use")
}
rd, wr := io.Pipe()
conn.rxBuf = bufio.NewReadWriter(bufio.NewReader(rd), bufio.NewWriter(wr))
conn.addr = XBeeAddr(addr)
conn.parent = sess
// add it to the list
sess.conns[addr] = conn
return
}
2023-05-11 00:02:57 +00:00
/*
The session implements a io.Writer and io.Reader, but does not
have a way of connecting to a specific XBee by default. To do this, we would
need to either pass an address to the write and read methods (breaking io.ReadWriter),
or add another command. Rather than do that, we can make a "Conn" class, which represents
a single connection to a device on the network.
*/
2023-05-08 02:00:34 +00:00
// Conn is a connection to a specific remote XBee. Conn allows for the user to
// contact one Xbee for point-to-point communications. This enables ACK packets
// for reliable transmission.
type Conn struct {
parent *Session
2023-05-11 00:02:57 +00:00
addr XBeeAddr
2023-05-11 04:56:45 +00:00
// data is written here by session rxHandler
rxBuf *bufio.ReadWriter
2023-05-08 02:00:34 +00:00
}
2023-05-11 04:56:45 +00:00
func (c *Conn) Write(p []byte) (n int, err error) {
2023-05-11 00:02:57 +00:00
return c.parent.writeAddr(p, uint64(c.addr))
}
2023-05-11 04:56:45 +00:00
func (c *Conn) Read(p []byte) (n int, err error) {
return c.rxBuf.Read(p)
}
2023-05-11 00:02:57 +00:00
func (c *Conn) Close() error {
2023-05-11 04:56:45 +00:00
// remove ourselves from the conn list.
delete(c.parent.conns, uint64(c.addr))
2023-05-11 00:02:57 +00:00
return nil
}
func (c *Conn) GetRSSI() int {
return 0
2023-05-08 02:00:34 +00:00
}
2023-05-10 17:55:51 +00:00
/*
Transport represents a connection that an XBee can use.
it's mostly a helper struct to parse URIs. It can parse the following formats:
2023-05-10 17:55:51 +00:00
tcp://192.168.4.5:8340
COM1
/dev/ttyUSB0:115200
for network devices, a port is optional. If it is not specified it will
default to 2616. The colon after a serial port sets the baud rate.
It will default to 9600 if not specified.
*/
type Transport struct {
io.ReadWriteCloser
devType string
}
func (xbt *Transport) Type() string {
return xbt.devType
}
// parseDeviceString parses the device parameter and sets up the associated
// device. The device is returned in an xbeeTransport which also stores
// the underlying type of the device with Type() string
func ParseDeviceString(dev string) (*Transport, error) {
xbt := &Transport{}
parseSerial := func(s string) (serial.Port, error) {
path, bRate, found := strings.Cut(dev, ":")
mode := &serial.Mode{
BaudRate: 9600,
}
if found {
b, err := strconv.Atoi(bRate)
if err != nil {
return nil, err
}
mode.BaudRate = b
}
return serial.Open(path, mode)
}
// actually parse the path
if strings.HasPrefix(dev, "tcp://") {
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
}
xbt.ReadWriteCloser = conn
xbt.devType = "tcp"
} else if strings.HasPrefix(dev, "COM") && runtime.GOOS == "windows" {
sDev, err := parseSerial(dev)
if err != nil {
return nil, err
}
xbt.ReadWriteCloser = sDev
xbt.devType = "serialWin"
} else if strings.HasPrefix(dev, "/") && runtime.GOOS != "windows" {
sDev, err := parseSerial(dev)
if err != nil {
return nil, err
}
xbt.ReadWriteCloser = sDev
xbt.devType = "serial"
} else {
return nil, errors.New("could not parse device path")
}
return xbt, nil
}