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 { type service interface {
fmt.Stringer fmt.Stringer
Start(cCtx *cli.Context, broker *gotelem.Broker) (err error) Start(cCtx *cli.Context, broker *gotelem.Broker, logger *slog.Logger) (err error)
Status() Status()
} }
// this variable stores all the hanlders. It has some basic ones, but also // this variable stores all the hanlders. It has some basic ones, but also
// can be extended on certain platforms (see cli/socketcan.go) // can be extended on certain platforms (see cli/socketcan.go)
// or if certain features are present (see sqlite.go) // or if certain features are present (see sqlite.go)
var serveThings = []testThing{} var serveThings = []service{
&XBeeService{},
}
func serve(cCtx *cli.Context) error { func serve(cCtx *cli.Context) error {
// TODO: output both to stderr and a file. // TODO: output both to stderr and a file.
@ -66,16 +68,6 @@ func serve(cCtx *cli.Context) error {
// can logger. // can logger.
go CanDump(broker, logger.WithGroup("candump"), done) 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") != "" { if cCtx.String("can") != "" {
logger.Info("using can device") logger.Info("using can device")
go canHandler(broker, logger.With("device", cCtx.String("can")), done, cCtx.String("can")) 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 { func tcpSvc(ctx *cli.Context, broker *gotelem.Broker) error {
// TODO: extract port/ip from cli context. // TODO: extract port/ip from cli context.
ln, err := net.Listen("tcp", ":8082") 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) { type XBeeService struct {
rxCh := broker.Subscribe("xbee") session *xbee.Session
l.Info("starting xbee send routine") }
xb, err := xbee.NewSession(trspt, l.With("device", trspt.Type())) func (x *XBeeService) String() string {
if err != nil { return "hello"
l.Error("failed to start xbee session", "err", err) }
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 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 { for {
select { select {
case <-done: case <-cCtx.Done():
xb.Close() x.session.Close()
return return
case msg := <-rxCh: case msg := <-rxCh:
// TODO: take can message and send it over CAN. // 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 := make([]byte, 0)
buf = binary.BigEndian.AppendUint32(buf, msg.Id) buf = binary.BigEndian.AppendUint32(buf, msg.Id)
buf = append(buf, msg.Data...) buf = append(buf, msg.Data...)
_, err := xb.Write(buf) _, err := x.session.Write(buf)
if err != nil { 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"
"github.com/kschamplin/gotelem/socketcan" "github.com/kschamplin/gotelem/socketcan"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"golang.org/x/exp/slog"
) )
// this file adds socketCAN commands and functionality when building on linux. // this file adds socketCAN commands and functionality when building on linux.
@ -26,16 +27,29 @@ func init() {
serveFlags = append(serveFlags, canDevFlag) serveFlags = append(serveFlags, canDevFlag)
// add services for server // add services for server
serveThings = append(serveThings, socketCANService) serveThings = append(serveThings, &socketCANService{})
// add can subcommand/actions // add can subcommand/actions
// TODO: make socketcan utility commands. // TODO: make socketcan utility commands.
subCmds = append(subCmds, socketCANCmd)
} }
// FIXME: add logging back in since it's missing rn // 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") rxCh := broker.Subscribe("socketCAN")
sock, err := socketcan.NewCanSocket(cCtx.String("can")) sock, err := socketcan.NewCanSocket(cCtx.String("can"))
if err != nil { if err != nil {
@ -46,10 +60,13 @@ func socketCANService(cCtx *cli.Context, broker *gotelem.Broker) (err error) {
go func() { go func() {
for { for {
pkt, _ := sock.Recv() pkt, err := sock.Recv()
if err != nil {
logger.Warn("error receiving CAN packet", "err", err)
}
rxCan <- *pkt rxCan <- *pkt
} }
}() }()
for { for {
@ -70,8 +87,7 @@ var socketCANCmd = &cli.Command{
Name: "can", Name: "can",
Usage: "SocketCAN utilities", Usage: "SocketCAN utilities",
Description: ` Description: `
Various helper utilties for CAN bus on sockets. Various helper utilties for CAN bus on sockets.
`, `,
Flags: []cli.Flag{ Flags: []cli.Flag{
canDevFlag, canDevFlag,