-
Notifications
You must be signed in to change notification settings - Fork 8
/
xbee.go
107 lines (91 loc) · 2.63 KB
/
xbee.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package xbee
import (
"context"
"net"
"strings"
"github.com/aperturerobotics/bifrost/transport"
pconn "github.com/aperturerobotics/bifrost/transport/common/kcp"
"github.com/aperturerobotics/bifrost/transport/xbee/xbserial"
"github.com/aperturerobotics/bifrost/util/scrc"
"github.com/blang/semver"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/tarm/serial"
)
// Version is the version of the xbee implementation.
var Version = semver.MustParse("0.0.1")
// TransportType is the transport type identifier for this transport.
const TransportType = "xbee"
// ControllerID is the xbee controller ID.
const ControllerID = "bifrost/xbee"
// Link represents a xbee-based connection/link.
type Link = pconn.Link
// XBee implements a XBee transport.
type XBee struct {
*pconn.Transport
xbs *xbserial.XBeeSerial
}
// NewXBee builds a new XBee transport, opening the serial device.
func NewXBee(
ctx context.Context,
le *logrus.Entry,
opts *Config,
pKey crypto.PrivKey,
c transport.TransportHandler,
) (*XBee, error) {
le = le.
WithField("device-path", opts.GetDevicePath()).
WithField("device-baud", opts.GetDeviceBaud())
le.Debug("opening device")
sp, err := serial.OpenPort(&serial.Config{
Name: opts.GetDevicePath(),
Baud: int(opts.GetDeviceBaud()),
// ReadTimeout: time.Millisecond * 500,
})
if err != nil {
return nil, errors.Wrap(err, "open xbee serial")
}
xbs := xbserial.NewXBeeSerial(le, sp)
go func() {
_ = xbs.ReadPump()
}()
le.Debug("reading device address")
pc, err := xbserial.NewPacketConn(ctx, xbs)
if err != nil {
sp.Close()
return nil, err
}
uuid := scrc.Crc64([]byte(
strings.Join([]string{ControllerID, pc.LocalAddr().String()}, "/"),
))
le.
WithField("device-address", pc.LocalAddr().String()).
Info("opened xbee device successfully")
conn := pconn.New(
le,
uuid,
pc,
pKey,
func(addr string) (net.Addr, error) {
return xbserial.ParseXBeeAddr(addr)
},
c,
opts.GetPacketOpts(),
)
return &XBee{Transport: conn, xbs: xbs}, nil
}
// MatchTransportType checks if the given transport type ID matches this transport.
// If returns true, the transport controller will call DialPeer with that tptaddr.
// E.x.: "udp-quic" or "ws"
func (c *XBee) MatchTransportType(transportType string) bool {
return transportType == TransportType
}
// Execute executes the transport as configured, returning any fatal error.
func (c *XBee) Execute(ctx context.Context) error {
return c.Transport.Execute(ctx)
}
// _ is a type assertion.
var _ transport.Transport = ((*XBee)(nil))
// _ is a type assertion
var _ transport.TransportDialer = ((*XBee)(nil))