This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
server.go
165 lines (144 loc) · 4.2 KB
/
server.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"log"
"net"
"time"
"github.com/thethingsnetwork/croft/lora"
"github.com/thethingsnetwork/server-shared"
)
func readUDPMessages(port int, messages chan interface{}) {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("0.0.0.0:%d", port))
if err != nil {
log.Fatalf("Failed to resolve address: %s", err.Error())
}
listener, err := net.ListenUDP("udp", addr)
if err != nil {
log.Fatalf("Failed to start UDP server: %s", err.Error())
}
defer listener.Close()
conn := lora.NewConn(listener)
for {
msg, err := conn.ReadMessage()
if err != nil {
log.Printf("Failed to read message: %s", err.Error())
continue
}
go handleMessage(msg, messages)
}
}
func handleMessage(msg *lora.Message, messages chan interface{}) {
log.Printf("Handling message from %v", msg.SourceAddr)
switch msg.Header.Identifier {
case lora.PUSH_DATA:
publishPushMessagePayloads(msg.GatewayEui, msg.Payload.(lora.PushMessagePayload), messages)
}
err := msg.Ack()
if err != nil {
log.Printf("Failed to send ACK: %s", err.Error())
}
}
func publishPushMessagePayloads(gatewayEui []byte, payload lora.PushMessagePayload, messages chan interface{}) {
if payload.Stat != nil {
stat, err := convertStat(gatewayEui, payload.Stat)
if err != nil {
log.Printf("Failed to convert Stat: %s", err.Error())
} else {
messages <- stat
}
}
if payload.RXPK != nil {
for _, rxpk := range payload.RXPK {
packet, err := convertRXPK(gatewayEui, rxpk)
if err != nil {
log.Printf("Failed to convert RXPK: %s", err.Error())
continue
}
messages <- packet
}
}
}
func convertStat(gatewayEui []byte, stat *lora.Stat) (*shared.GatewayStatus, error) {
t, err := time.Parse(time.RFC822, stat.Time)
if err != nil {
log.Printf("Failed to parse time %s: %s", stat.Time, err.Error())
t = time.Now()
}
return &shared.GatewayStatus{
Eui: fmt.Sprintf("%X", gatewayEui),
Time: t,
Latitude: &stat.Lati,
Longitude: &stat.Long,
Altitude: &stat.Alti,
RxCount: &stat.Rxnb,
RxOk: &stat.Rxok,
RxForwarded: &stat.Rxfw,
AckRatio: &stat.Ackr,
DatagramsReceived: &stat.Dwnb,
DatagramsSent: &stat.Txnb,
}, nil
}
func convertRXPK(gatewayEui []byte, rxpk *lora.RXPK) (*shared.RxPacket, error) {
data, err := rxpk.ParseData()
if err != nil {
log.Printf("Failed to parse RXPK: %s", err.Error())
return nil, err
}
networkKey, err := getNetworkKey(gatewayEui, data.DevAddr)
if err != nil {
log.Printf("Failed to get key (gateway %X, node %X): %s", gatewayEui, data.DevAddr, err.Error())
return nil, err
}
var key []byte
if data.FPort == 0 {
key = networkKey
} else {
key, err = getAppKey(gatewayEui, data.DevAddr)
if err != nil {
log.Printf("Failed to get key (gateway %X, node %X): %s", gatewayEui, data.DevAddr, err.Error())
return nil, err
}
}
ok, err := data.TestIntegrity(networkKey)
if err != nil {
log.Printf("Failed to test integrity: %s", err.Error())
return nil, err
}
if !ok {
return nil, errors.New("Integrity test failed")
}
payload, err := data.DecryptPayload(key)
if err != nil {
log.Printf("Error decrypting data: %s", err.Error())
return nil, err
}
return &shared.RxPacket{
GatewayEui: fmt.Sprintf("%X", gatewayEui),
NodeEui: fmtDevAddr(data.DevAddr),
Time: time.Now(), // rxpk.Time,
Frequency: &rxpk.Freq,
DataRate: rxpk.Datr,
Rssi: &rxpk.Rssi,
Snr: &rxpk.Lsnr,
RawData: rxpk.Data,
Data: base64.StdEncoding.EncodeToString(payload),
}, nil
}
func fmtDevAddr(devAddr uint32) string {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, devAddr)
return fmt.Sprintf("%X", buf.Bytes())
}
func getNetworkKey(gatewayEui []byte, devAddr uint32) ([]byte, error) {
// TODO: Implement fetching the right network key. Now returning Semtech's default key
key := []byte{0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}
return key, nil
}
func getAppKey(gatewayEui []byte, devAddr uint32) ([]byte, error) {
// TODO: Implement fetching the right application key
return getNetworkKey(gatewayEui, devAddr)
}