From 598d6e45b2a79b169056c5abe7a6266aa6d5cb23 Mon Sep 17 00:00:00 2001 From: Noah Stride Date: Mon, 5 Feb 2024 15:32:49 +0000 Subject: Refactor Meshtastic "Stream" Protocol handling and implement TCP listener (#5) * Start hacking on a "StreamConn" * Tidy up write side * Write basic send/receive test * Add support for "wake" Start2 spam * Add test case for reply * Add TCP listener to meshtastic stream conn * Very ugly basic impl that supports `meshtastic --nodes` * Support graceful disconnection command from client * Refactor handling for handleToRadioWantConfigID into it's own method * Send FromRadio messages to clients * Refactor client logic into own type * Fix up serial support for new client * Fix eample * Remove datadump * Make TCP listener optional * Add locking for reading/writing from the connection * Explain knownDevices * Properly close streamConn in example --- example/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 example/main.go (limited to 'example/main.go') diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..3986791 --- /dev/null +++ b/example/main.go @@ -0,0 +1,46 @@ +package main + +import ( + pb "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go/meshtastic" + "context" + "github.com/charmbracelet/log" + "github.com/crypto-smoke/meshtastic-go/transport" + "github.com/crypto-smoke/meshtastic-go/transport/serial" + "google.golang.org/protobuf/proto" + "os" + "os/signal" +) + +func main() { + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancel() + + log.SetLevel(log.DebugLevel) + + ports := serial.GetPorts() + serialConn, err := serial.Connect(ports[0]) + if err != nil { + panic(err) + } + streamConn, err := transport.NewClientStreamConn(serialConn) + if err != nil { + panic(err) + } + defer func() { + if err := streamConn.Close(); err != nil { + panic(err) + } + }() + + client := transport.NewClient(streamConn, false) + client.Handle(new(pb.MeshPacket), func(msg proto.Message) { + pkt := msg.(*pb.MeshPacket) + log.Info("Received message from radio", "msg", pkt) + }) + if client.Connect() != nil { + panic("Failed to connect to the radio") + } + + log.Info("Waiting for interrupt signal") + <-ctx.Done() +} -- cgit v1.2.3