aboutsummaryrefslogtreecommitdiff
path: root/example/main.go
diff options
context:
space:
mode:
authorNoah Stride <[email protected]>2024-02-05 15:32:49 +0000
committerGitHub <[email protected]>2024-02-05 05:32:49 -1000
commit598d6e45b2a79b169056c5abe7a6266aa6d5cb23 (patch)
tree5a628024bd06a73fda221712a87eaf20b30ccf29 /example/main.go
parent95a38401baeb4df14d701405609a3dd26ffd747f (diff)
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
Diffstat (limited to 'example/main.go')
-rw-r--r--example/main.go46
1 files changed, 46 insertions, 0 deletions
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()
+}