summaryrefslogtreecommitdiff
path: root/mestastic.go
diff options
context:
space:
mode:
authorMarin Ivanov <[email protected]>2025-09-03 20:59:24 +0300
committerMarin Ivanov <[email protected]>2025-09-03 20:59:24 +0300
commit220671ed46e10e4c67741286da493df1d45cdcfc (patch)
tree0711f179f4619959fd937c2929c6247ed8ab124a /mestastic.go
Initial release
Diffstat (limited to 'mestastic.go')
-rw-r--r--mestastic.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/mestastic.go b/mestastic.go
new file mode 100644
index 0000000..d76165b
--- /dev/null
+++ b/mestastic.go
@@ -0,0 +1,90 @@
+package main
+
+import (
+ "encoding/binary"
+
+ "github.com/meshnet-gophers/meshtastic-go"
+ pb "github.com/meshnet-gophers/meshtastic-go/meshtastic"
+ "google.golang.org/protobuf/proto"
+)
+
+const (
+ FlagViaMQTT = (1 << 4)
+ FlagWantACK = (1 << 3)
+)
+
+var (
+ broadcastID = meshtastic.BroadcastNodeID.Uint32()
+)
+
+var pkcChan = &pb.ChannelSettings{
+ ChannelNum: 0xFFFFFFFF,
+ Name: "[PKC]",
+ Psk: nil,
+}
+
+type MeshIf interface {
+ Open() error
+ ReadMeshPacket() (*pb.MeshPacket, uint64, error)
+ WriteMeshPacket(*pb.MeshPacket) error
+ Close() error
+}
+
+type RadioHeaderFlag uint8
+
+func (r RadioHeaderFlag) WantACK() uint8 {
+ return uint8(r>>3) & 1
+}
+
+func (r RadioHeaderFlag) MQTT() uint8 {
+ return uint8(r>>4) & 1
+}
+
+func (r RadioHeaderFlag) HopStart() uint8 {
+ return uint8(r>>5) & 0b111
+}
+
+func (r RadioHeaderFlag) HopLimit() uint8 {
+ return uint8(r & 0b111)
+}
+
+type PacketTruncated struct {
+ To uint32
+ From uint32
+ ID uint32
+}
+
+func buildRoutingReply(p *pb.MeshPacket, err pb.Routing_Error) *pb.MeshPacket {
+ cb, _ := proto.Marshal(&pb.Routing{
+ Variant: &pb.Routing_ErrorReason{ErrorReason: err},
+ })
+ return &pb.MeshPacket{
+ // Reversed
+ To: p.From,
+ From: p.To,
+
+ PkiEncrypted: p.PkiEncrypted,
+ PublicKey: p.PublicKey,
+
+ // (N)ACK
+ Priority: pb.MeshPacket_ACK,
+ HopLimit: p.HopStart,
+ Channel: p.Channel,
+ PayloadVariant: &pb.MeshPacket_Decoded{
+ Decoded: &pb.Data{
+ Portnum: pb.PortNum_ROUTING_APP,
+ RequestId: p.Id,
+ Payload: cb,
+ },
+ },
+ }
+}
+
+func buildPkcNonce(id uint32, extra uint32, from uint32) []byte {
+ var buf [13]byte // zero-init
+ binary.LittleEndian.PutUint32(buf[0:4], id)
+ binary.LittleEndian.PutUint32(buf[4:8], extra)
+ binary.LittleEndian.PutUint32(buf[8:12], from)
+ buf[12] = 0 // last byte is zero
+ return buf[:]
+}