forked from TheThingsArchive/croft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semtech.go
151 lines (134 loc) · 3.03 KB
/
semtech.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package lora
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"log"
"net"
"time"
)
const (
PUSH_DATA = iota
PUSH_ACK = iota
PULL_DATA = iota
PULL_ACK = iota
PULL_RESP = iota
)
type Conn struct {
Raw *net.UDPConn
}
type Message struct {
SourceAddr *net.UDPAddr
Conn *Conn
Header *MessageHeader
GatewayEui []byte
Payload interface{}
}
type MessageHeader struct {
ProtocolVersion byte
Token uint16
Identifier byte
}
type PushMessagePayload struct {
RXPK []*RXPK `json:"rxpk,omitempty"`
Stat *Stat `json:"stat,omitempty"`
}
type Stat struct {
Time string `json:"time"`
Lati float64 `json:"lati"`
Long float64 `json:"long"`
Alti float64 `json:"alti"`
Rxnb uint `json:"rxnb"`
Rxok uint `json:"rxok"`
Rxfw uint `json:"rxfw"`
Ackr float64 `json:"ackr"`
Dwnb uint `json:"dwnb"`
Txnb uint `json:"txnb"`
}
type RXPK struct {
Time time.Time `json:"time"`
Tmst uint `json:"tmst"`
Chan uint `json:"chan"`
Rfch uint `json:"rfch"`
Freq float64 `json:"freq"`
Stat int `json:"stat"`
Modu string `json:"modu"`
Datr string `json:"datr"`
Codr string `json:"codr"`
Rssi int `json:"rssi"`
Lsnr float64 `json:"lsnr"`
Size uint `json:"size"`
Data string `json:"data"`
}
type TXPX struct {
Imme bool `json:"imme"`
Freq float64 `json:"freq"`
Rfch uint `json:"rfch"`
Powe uint `json:"powe"`
Modu string `json:"modu"`
Datr uint `json:"datr"`
Fdev uint `json:"fdev"`
Size uint `json:"size"`
Data string `json:"data"`
}
func NewConn(r *net.UDPConn) *Conn {
return &Conn{r}
}
func (c *Conn) ReadMessage() (*Message, error) {
buf := make([]byte, 2048)
n, addr, err := c.Raw.ReadFromUDP(buf)
if err != nil {
log.Print("Error: ", err)
return nil, err
}
return c.parseMessage(addr, buf, n)
}
func (c *Conn) parseMessage(addr *net.UDPAddr, b []byte, n int) (*Message, error) {
var header MessageHeader
err := binary.Read(bytes.NewReader(b), binary.BigEndian, &header)
if err != nil {
return nil, err
}
msg := &Message{
SourceAddr: addr,
Conn: c,
Header: &header,
}
if header.Identifier == PUSH_DATA {
msg.GatewayEui = b[4:12]
var payload PushMessagePayload
err := json.Unmarshal(b[12:n], &payload)
if err != nil {
log.Printf("Parse message failed: %s\nMessage: %s", err.Error(), string(b[12:n]))
return nil, err
}
msg.Payload = payload
}
return msg, nil
}
func (m *Message) Ack() error {
ack := &MessageHeader{
ProtocolVersion: m.Header.ProtocolVersion,
Token: m.Header.Token,
Identifier: PUSH_ACK,
}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, ack)
if err != nil {
return err
}
_, err = m.Conn.Raw.WriteToUDP(buf.Bytes(), m.SourceAddr)
if err != nil {
return err
}
return nil
}
func (rxpk *RXPK) ParseData() (*PHYPayload, error) {
buf, err := base64.StdEncoding.DecodeString(rxpk.Data)
if err != nil {
log.Printf("Failed to decode base64 data: %s", err.Error())
return nil, err
}
return ParsePHYPayload(buf)
}