This commit is contained in:
saji 2023-05-18 22:34:52 -05:00
parent 47a818a451
commit ac5ccaa964
2 changed files with 63 additions and 32 deletions

View file

@ -45,14 +45,16 @@ type testThing func(cCtx *cli.Context, broker *gotelem.Broker) (err error)
type service interface {
fmt.Stringer
Start(cCtx *cli.Context, broker *gotelem.Broker) (err error)
Start(cCtx *cli.Context, broker *gotelem.Broker, logger *slog.Logger) (err error)
Status()
}
// this variable stores all the hanlders. It has some basic ones, but also
// can be extended on certain platforms (see cli/socketcan.go)
// or if certain features are present (see sqlite.go)
var serveThings = []testThing{}
var serveThings = []service{
&XBeeService{},
}
func serve(cCtx *cli.Context) error {
// TODO: output both to stderr and a file.
@ -66,16 +68,6 @@ func serve(cCtx *cli.Context) error {
// can logger.
go CanDump(broker, logger.WithGroup("candump"), done)
if cCtx.String("device") != "" {
logger.Info("using xbee device")
transport, err := xbee.ParseDeviceString(cCtx.String("device"))
if err != nil {
logger.Error("failed to open device string", "err", err)
os.Exit(1)
}
go XBeeSend(broker, logger.WithGroup("xbee"), done, transport)
}
if cCtx.String("can") != "" {
logger.Info("using can device")
go canHandler(broker, logger.With("device", cCtx.String("can")), done, cCtx.String("can"))
@ -103,7 +95,6 @@ func serve(cCtx *cli.Context) error {
}
}
func tcpSvc(ctx *cli.Context, broker *gotelem.Broker) error {
// TODO: extract port/ip from cli context.
ln, err := net.Listen("tcp", ":8082")
@ -230,36 +221,60 @@ func CanDump(broker *gotelem.Broker, l *slog.Logger, done <-chan struct{}) {
}
}
func XBeeSend(broker *gotelem.Broker, l *slog.Logger, done <-chan struct{}, trspt *xbee.Transport) {
rxCh := broker.Subscribe("xbee")
l.Info("starting xbee send routine")
type XBeeService struct {
session *xbee.Session
}
xb, err := xbee.NewSession(trspt, l.With("device", trspt.Type()))
if err != nil {
l.Error("failed to start xbee session", "err", err)
func (x *XBeeService) String() string {
return "hello"
}
func (x *XBeeService) Status() {
}
func (x *XBeeService) Start(cCtx *cli.Context,
broker *gotelem.Broker, logger *slog.Logger,
) (err error) {
if cCtx.String("xbee") == "" {
logger.Info("not using xbee")
return
}
transport, err := xbee.ParseDeviceString(cCtx.String("device"))
if err != nil {
logger.Error("failed to open xbee string", "err", err)
return
}
logger.Info("using xbee device", "transport", transport)
rxCh := broker.Subscribe("xbee")
l.Info("connected to local xbee", "addr", xb.LocalAddr())
x.session, err = xbee.NewSession(transport, logger.With("device", transport.Type()))
if err != nil {
logger.Error("failed to start xbee session", "err", err)
return
}
logger.Info("connected to local xbee", "addr", x.session.LocalAddr())
for {
select {
case <-done:
xb.Close()
case <-cCtx.Done():
x.session.Close()
return
case msg := <-rxCh:
// TODO: take can message and send it over CAN.
l.Info("got msg", "msg", msg)
logger.Info("got msg", "msg", msg)
buf := make([]byte, 0)
buf = binary.BigEndian.AppendUint32(buf, msg.Id)
buf = append(buf, msg.Data...)
_, err := xb.Write(buf)
_, err := x.session.Write(buf)
if err != nil {
l.Warn("error writing to xbee", "err", err)
logger.Warn("error writing to xbee", "err", err)
}
}
}
}
return
}

View file

@ -6,6 +6,7 @@ import (
"github.com/kschamplin/gotelem"
"github.com/kschamplin/gotelem/socketcan"
"github.com/urfave/cli/v2"
"golang.org/x/exp/slog"
)
// this file adds socketCAN commands and functionality when building on linux.
@ -26,16 +27,29 @@ func init() {
serveFlags = append(serveFlags, canDevFlag)
// add services for server
serveThings = append(serveThings, socketCANService)
serveThings = append(serveThings, &socketCANService{})
// add can subcommand/actions
// TODO: make socketcan utility commands.
subCmds = append(subCmds, socketCANCmd)
}
// FIXME: add logging back in since it's missing rn
func socketCANService(cCtx *cli.Context, broker *gotelem.Broker) (err error) {
type socketCANService struct {
}
func (s *socketCANService) Status() {
return
}
func (s *socketCANService) String() string {
return ""
}
func (s *socketCANService) Start(cCtx *cli.Context, broker *gotelem.Broker, logger *slog.Logger) (err error) {
rxCh := broker.Subscribe("socketCAN")
sock, err := socketcan.NewCanSocket(cCtx.String("can"))
if err != nil {
@ -46,7 +60,10 @@ func socketCANService(cCtx *cli.Context, broker *gotelem.Broker) (err error) {
go func() {
for {
pkt, _ := sock.Recv()
pkt, err := sock.Recv()
if err != nil {
logger.Warn("error receiving CAN packet", "err", err)
}
rxCan <- *pkt
}
@ -71,7 +88,6 @@ var socketCANCmd = &cli.Command{
Usage: "SocketCAN utilities",
Description: `
Various helper utilties for CAN bus on sockets.
`,
Flags: []cli.Flag{
canDevFlag,