/
push_data.go
278 lines (240 loc) · 9.38 KB
/
push_data.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package packets
import (
"encoding/binary"
"encoding/json"
"regexp"
"strconv"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"github.com/brocaar/loraserver/api/common"
"github.com/brocaar/loraserver/api/gw"
"github.com/brocaar/lorawan"
)
// loRaDataRateRegex contains a regexp for parsing the data-rate string.
var loRaDataRateRegex = regexp.MustCompile(`SF(\d+)BW(\d+)`)
// PushDataPacket type is used by the gateway mainly to forward the RF packets
// received, and associated metadata, to the server.
type PushDataPacket struct {
ProtocolVersion uint8
RandomToken uint16
GatewayMAC lorawan.EUI64
Payload PushDataPayload
}
// MarshalBinary encodes the packet into binary form compatible with the
// Semtech UDP protocol.
func (p PushDataPacket) MarshalBinary() ([]byte, error) {
pb, err := json.Marshal(&p.Payload)
if err != nil {
return nil, err
}
out := make([]byte, 4, len(pb)+12)
out[0] = p.ProtocolVersion
binary.LittleEndian.PutUint16(out[1:3], p.RandomToken)
out[3] = byte(PushData)
out = append(out, p.GatewayMAC[0:len(p.GatewayMAC)]...)
out = append(out, pb...)
return out, nil
}
// GetGatewayStats returns the gw.GatewayStats object (if the packet contains stats).
func (p PushDataPacket) GetGatewayStats() (*gw.GatewayStats, error) {
if p.Payload.Stat == nil {
return nil, nil
}
stats := gw.GatewayStats{
GatewayId: p.GatewayMAC[:],
RxPacketsReceived: p.Payload.Stat.RXNb,
RxPacketsReceivedOk: p.Payload.Stat.RXOK,
TxPacketsEmitted: p.Payload.Stat.TXNb,
TxPacketsReceived: p.Payload.Stat.DWNb,
}
// time
ts, err := ptypes.TimestampProto(time.Time(p.Payload.Stat.Time))
if err != nil {
return nil, errors.Wrap(err, "timestamp proto error")
}
stats.Time = ts
// location
if p.Payload.Stat.Lati != nil && p.Payload.Stat.Long != nil && p.Payload.Stat.Alti != nil {
stats.Location = &common.Location{
Latitude: *p.Payload.Stat.Lati,
Longitude: *p.Payload.Stat.Long,
Altitude: float64(*p.Payload.Stat.Alti),
Source: common.LocationSource_GPS,
}
}
return &stats, nil
}
// GetUplinkFrames returns a slice of gw.UplinkFrame.
func (p PushDataPacket) GetUplinkFrames(FakeRxInfoTime bool) ([]gw.UplinkFrame, error) {
var frames []gw.UplinkFrame
for i := range p.Payload.RXPK {
if len(p.Payload.RXPK[i].RSig) == 0 {
frame, err := getUplinkFrame(p.GatewayMAC[:], p.Payload.RXPK[i], FakeRxInfoTime)
if err != nil {
return nil, errors.Wrap(err, "gateway: get uplink frame error")
}
frames = append(frames, frame)
} else {
for j := range p.Payload.RXPK[i].RSig {
frame, err := getUplinkFrame(p.GatewayMAC[:], p.Payload.RXPK[i], FakeRxInfoTime)
if err != nil {
return nil, errors.Wrap(err, "gateway: get uplink frame error")
}
frame = setUplinkFrameRSig(frame, p.Payload.RXPK[i], p.Payload.RXPK[i].RSig[j])
frames = append(frames, frame)
}
}
}
return frames, nil
}
func setUplinkFrameRSig(frame gw.UplinkFrame, rxPK RXPK, rSig RSig) gw.UplinkFrame {
frame.RxInfo.Antenna = uint32(rSig.Ant)
frame.RxInfo.Channel = uint32(rSig.Chan)
frame.RxInfo.Rssi = int32(rSig.RSSIC)
frame.RxInfo.LoraSnr = rSig.LSNR
if len(rSig.ETime) != 0 {
frame.RxInfo.FineTimestampType = gw.FineTimestampType_ENCRYPTED
frame.RxInfo.FineTimestamp = &gw.UplinkRXInfo_EncryptedFineTimestamp{
EncryptedFineTimestamp: &gw.EncryptedFineTimestamp{
EncryptedNs: rSig.ETime,
AesKeyIndex: uint32(rxPK.AESK),
},
}
}
return frame
}
func getUplinkFrame(gatewayID []byte, rxpk RXPK, FakeRxInfoTime bool) (gw.UplinkFrame, error) {
frame := gw.UplinkFrame{
PhyPayload: rxpk.Data,
TxInfo: &gw.UplinkTXInfo{
Frequency: uint32(rxpk.Freq * 1000000),
},
RxInfo: &gw.UplinkRXInfo{
GatewayId: gatewayID,
Rssi: int32(rxpk.RSSI),
LoraSnr: rxpk.LSNR,
Channel: uint32(rxpk.Chan),
RfChain: uint32(rxpk.RFCh),
Board: uint32(rxpk.Brd),
Context: make([]byte, 4),
},
}
// Context
binary.BigEndian.PutUint32(frame.RxInfo.Context, rxpk.Tmst)
// Time.
if rxpk.Time != nil && !time.Time(*rxpk.Time).IsZero() {
ts, err := ptypes.TimestampProto(time.Time(*rxpk.Time))
if err != nil {
return frame, errors.Wrap(err, "gateway: timestamp proto error")
}
frame.RxInfo.Time = ts
} else if FakeRxInfoTime {
ts, _ := ptypes.TimestampProto(time.Now().UTC())
frame.RxInfo.Time = ts
}
// Time since GPS epoch
if rxpk.Tmms != nil {
d := time.Duration(*rxpk.Tmms) * time.Millisecond
frame.RxInfo.TimeSinceGpsEpoch = ptypes.DurationProto(d)
}
// LoRa data-rate
if rxpk.DatR.LoRa != "" {
frame.TxInfo.Modulation = common.Modulation_LORA
match := loRaDataRateRegex.FindStringSubmatch(rxpk.DatR.LoRa)
// parse e.g. SF12BW250 into separate variables
if len(match) != 3 {
return frame, errors.New("gateway: could not parse LoRa data-rate")
}
// cast variables to ints
sf, err := strconv.Atoi(match[1])
if err != nil {
return frame, errors.Wrap(err, "gateway: could not convert sf to int")
}
bw, err := strconv.Atoi(match[2])
if err != nil {
return frame, errors.Wrap(err, "gateway: could not parse bandwidth to int")
}
frame.TxInfo.ModulationInfo = &gw.UplinkTXInfo_LoraModulationInfo{
LoraModulationInfo: &gw.LoRaModulationInfo{
Bandwidth: uint32(bw),
SpreadingFactor: uint32(sf),
CodeRate: rxpk.CodR,
},
}
}
// FSK data-rate
if rxpk.DatR.FSK != 0 {
frame.TxInfo.Modulation = common.Modulation_FSK
frame.TxInfo.ModulationInfo = &gw.UplinkTXInfo_FskModulationInfo{
FskModulationInfo: &gw.FSKModulationInfo{
Bitrate: uint32(rxpk.DatR.FSK),
},
}
}
return frame, nil
}
// UnmarshalBinary decodes the packet from Semtech UDP binary form.
func (p *PushDataPacket) UnmarshalBinary(data []byte) error {
if len(data) < 13 {
return errors.New("gateway: at least 13 bytes are expected")
}
if data[3] != byte(PushData) {
return errors.New("gateway: identifier mismatch (PUSH_DATA expected)")
}
if !protocolSupported(data[0]) {
return ErrInvalidProtocolVersion
}
p.ProtocolVersion = data[0]
p.RandomToken = binary.LittleEndian.Uint16(data[1:3])
for i := 0; i < 8; i++ {
p.GatewayMAC[i] = data[4+i]
}
return json.Unmarshal(data[12:], &p.Payload)
}
// PushDataPayload represents the upstream JSON data structure.
type PushDataPayload struct {
RXPK []RXPK `json:"rxpk,omitempty"`
Stat *Stat `json:"stat,omitempty"`
}
// Stat contains the status of the gateway.
type Stat struct {
Time ExpandedTime `json:"time"` // UTC 'system' time of the gateway, ISO 8601 'expanded' format (e.g 2014-01-12 08:59:28 GMT)
Lati *float64 `json:"lati"` // GPS latitude of the gateway in degree (float, N is +)
Long *float64 `json:"long"` // GPS latitude of the gateway in degree (float, E is +)
Alti *int32 `json:"alti"` // GPS altitude of the gateway in meter RX (integer)
RXNb uint32 `json:"rxnb"` // Number of radio packets received (unsigned integer)
RXOK uint32 `json:"rxok"` // Number of radio packets received with a valid PHY CRC
RXFW uint32 `json:"rxfw"` // Number of radio packets forwarded (unsigned integer)
ACKR float64 `json:"ackr"` // Percentage of upstream datagrams that were acknowledged
DWNb uint32 `json:"dwnb"` // Number of downlink datagrams received (unsigned integer)
TXNb uint32 `json:"txnb"` // Number of packets emitted (unsigned integer)
}
// RXPK contain a RF packet and associated metadata.
type RXPK struct {
Time *CompactTime `json:"time"` // UTC time of pkt RX, us precision, ISO 8601 'compact' format (e.g. 2013-03-31T16:21:17.528002Z)
Tmms *int64 `json:"tmms"` // GPS time of pkt RX, number of milliseconds since 06.Jan.1980
Tmst uint32 `json:"tmst"` // Internal timestamp of "RX finished" event (32b unsigned)
AESK uint8 `json:"aesk"` //AES key index used for encrypting fine timestamps
Chan uint8 `json:"chan"` // Concentrator "IF" channel used for RX (unsigned integer)
RFCh uint8 `json:"rfch"` // Concentrator "RF chain" used for RX (unsigned integer)
Stat int8 `json:"stat"` // CRC status: 1 = OK, -1 = fail, 0 = no CRC
Freq float64 `json:"freq"` // RX central frequency in MHz (unsigned float, Hz precision)
Brd uint32 `json:"brd"` // Concentrator board used for RX (unsigned integer)
RSSI int16 `json:"rssi"` // RSSI in dBm (signed integer, 1 dB precision)
Size uint16 `json:"size"` // RF packet payload size in bytes (unsigned integer)
DatR DatR `json:"datr"` // LoRa datarate identifier (eg. SF12BW500) || FSK datarate (unsigned, in bits per second)
Modu string `json:"modu"` // Modulation identifier "LORA" or "FSK"
CodR string `json:"codr"` // LoRa ECC coding rate identifier
LSNR float64 `json:"lsnr"` // Lora SNR ratio in dB (signed float, 0.1 dB precision)
Data []byte `json:"data"` // Base64 encoded RF packet payload, padded
RSig []RSig `json:"rsig"` // Received signal information, per antenna (Optional)
}
// RSig contains the received signal information per antenna.
type RSig struct {
Ant uint8 `json:"ant"` // Antenna number on which signal has been received
Chan uint8 `json:"chan"` // Concentrator "IF" channel used for RX (unsigned integer)
RSSIC int16 `json:"rssic"` // RSSI in dBm of the channel (signed integer, 1 dB precision)
LSNR float64 `json:"lsnr"` // Lora SNR ratio in dB (signed float, 0.1 dB precision)
ETime []byte `json:"etime"` // Encrypted 'main' fine timestamp, ns precision [0..999999999] (Optional)
}