This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
downlink.go
337 lines (290 loc) · 10.9 KB
/
downlink.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package router
import (
"fmt"
"math"
"strings"
"time"
pb_broker "github.com/TheThingsNetwork/api/broker"
pb_gateway "github.com/TheThingsNetwork/api/gateway"
"github.com/TheThingsNetwork/api/logfields"
pb_protocol "github.com/TheThingsNetwork/api/protocol"
pb_lorawan "github.com/TheThingsNetwork/api/protocol/lorawan"
pb "github.com/TheThingsNetwork/api/router"
"github.com/TheThingsNetwork/api/trace"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/core/band"
"github.com/TheThingsNetwork/ttn/core/router/gateway"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/TheThingsNetwork/ttn/utils/toa"
"github.com/spf13/viper"
)
func (r *router) SubscribeDownlink(gatewayID string, subscriptionID string) (<-chan *pb.DownlinkMessage, error) {
ctx := r.Ctx.WithFields(ttnlog.Fields{
"GatewayID": gatewayID,
})
gateway := r.getGateway(gatewayID)
if fromSchedule := gateway.Schedule.Subscribe(subscriptionID); fromSchedule != nil {
if token := gateway.Token(); gatewayID != "" && token != "" {
r.Discovery.AddGatewayID(gatewayID, token)
}
toGateway := make(chan *pb.DownlinkMessage)
go func() {
ctx.Debug("Activate downlink")
for message := range fromSchedule {
ctx.WithFields(logfields.ForMessage(message)).Debug("Send downlink")
toGateway <- message
if gateway.MonitorStream != nil {
clone := *message // There can be multiple subscribers
clone.Trace = clone.Trace.WithEvent(trace.SendEvent)
gateway.MonitorStream.Send(&clone)
}
}
ctx.Debug("Deactivate downlink")
close(toGateway)
}()
return toGateway, nil
}
return nil, errors.NewErrInternal(fmt.Sprintf("Already subscribed to downlink for %s", gatewayID))
}
func (r *router) UnsubscribeDownlink(gatewayID string, subscriptionID string) error {
gateway := r.getGateway(gatewayID)
if token := gateway.Token(); gatewayID != "" && token != "" {
r.Discovery.RemoveGatewayID(gatewayID, token)
}
gateway.Schedule.Stop(subscriptionID)
return nil
}
func (r *router) HandleDownlink(downlink *pb_broker.DownlinkMessage) (err error) {
var gateway *gateway.Gateway
r.RegisterReceived(downlink)
defer func() {
if err != nil {
downlink.Trace = downlink.Trace.WithEvent(trace.DropEvent, "reason", err)
if gateway != nil && gateway.MonitorStream != nil {
gateway.MonitorStream.Send(downlink)
}
} else {
r.RegisterHandled(downlink)
}
}()
r.status.downlink.Mark(1)
downlink.Trace = downlink.Trace.WithEvent(trace.ReceiveEvent)
option := downlink.DownlinkOption
// NOTE: This option is intentionally undocumented.
if maxTxPower := int32(viper.GetInt("lorawan.max-tx-power")); maxTxPower != 0 &&
option.GatewayConfiguration.Power > maxTxPower {
option.GatewayConfiguration.Power = maxTxPower
}
downlinkMessage := &pb.DownlinkMessage{
Payload: downlink.Payload,
ProtocolConfiguration: option.ProtocolConfiguration,
GatewayConfiguration: option.GatewayConfiguration,
Trace: downlink.Trace,
}
identifier := option.Identifier
if r.Component != nil && r.Component.Identity != nil {
identifier = strings.TrimPrefix(option.Identifier, fmt.Sprintf("%s:", r.Component.Identity.ID))
}
gateway = r.getGateway(downlink.DownlinkOption.GatewayID)
return gateway.HandleDownlink(identifier, downlinkMessage)
}
// buildDownlinkOption builds a DownlinkOption with default values
func (r *router) buildDownlinkOption(gatewayID string, band band.FrequencyPlan) *pb_broker.DownlinkOption {
dataRate, _ := types.ConvertDataRate(band.DataRates[band.RX2DataRate])
return &pb_broker.DownlinkOption{
GatewayID: gatewayID,
ProtocolConfiguration: pb_protocol.TxConfiguration{Protocol: &pb_protocol.TxConfiguration_LoRaWAN{LoRaWAN: &pb_lorawan.TxConfiguration{
Modulation: pb_lorawan.Modulation_LORA,
DataRate: dataRate.String(),
CodingRate: "4/5",
}}},
GatewayConfiguration: pb_gateway.TxConfiguration{
RfChain: 0,
PolarizationInversion: true,
Frequency: uint64(band.RX2Frequency),
Power: int32(band.DefaultTXPower),
},
}
}
func (r *router) buildDownlinkOptions(uplink *pb.UplinkMessage, isActivation bool, gateway *gateway.Gateway) (downlinkOptions []*pb_broker.DownlinkOption) {
var options []*pb_broker.DownlinkOption
gatewayStatus, _ := gateway.Status.Get() // This just returns empty if non-existing
lorawanMetadata := uplink.ProtocolMetadata.GetLoRaWAN()
if lorawanMetadata == nil {
return // We can't handle any other protocols than LoRaWAN yet
}
frequencyPlan := gatewayStatus.FrequencyPlan
if frequencyPlan == "" {
frequencyPlan = band.Guess(uplink.GatewayMetadata.Frequency)
}
band, err := band.Get(frequencyPlan)
if err != nil {
return // We can't handle this frequency plan
}
if frequencyPlan == "EU_863_870" && isActivation {
band.RX2DataRate = 0
}
dataRate, err := lorawanMetadata.GetLoRaWANDataRate()
if err != nil {
return
}
// Configuration for RX2
buildRX2 := func() (*pb_broker.DownlinkOption, error) {
option := r.buildDownlinkOption(gateway.ID, band)
if frequencyPlan == "EU_863_870" {
option.GatewayConfiguration.Power = 27 // The EU RX2 frequency allows up to 27dBm
}
if isActivation {
option.GatewayConfiguration.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.JoinAcceptDelay2/1000)
} else {
option.GatewayConfiguration.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.ReceiveDelay2/1000)
}
option.ProtocolConfiguration.GetLoRaWAN().CodingRate = lorawanMetadata.CodingRate
return option, nil
}
if option, err := buildRX2(); err == nil {
options = append(options, option)
}
// Configuration for RX1
buildRX1 := func() (*pb_broker.DownlinkOption, error) {
option := r.buildDownlinkOption(gateway.ID, band)
if isActivation {
option.GatewayConfiguration.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.JoinAcceptDelay1/1000)
} else {
option.GatewayConfiguration.Timestamp = uplink.GatewayMetadata.Timestamp + uint32(band.ReceiveDelay1/1000)
}
option.ProtocolConfiguration.GetLoRaWAN().CodingRate = lorawanMetadata.CodingRate
freq, err := band.GetRX1Frequency(int(uplink.GatewayMetadata.Frequency))
if err != nil {
return nil, err
}
option.GatewayConfiguration.Frequency = uint64(freq)
upDR, err := band.GetDataRate(dataRate)
if err != nil {
return nil, err
}
downDR, err := band.GetRX1DataRate(upDR, 0)
if err != nil {
return nil, err
}
if err := option.ProtocolConfiguration.GetLoRaWAN().SetDataRate(band.DataRates[downDR]); err != nil {
return nil, err
}
option.GatewayConfiguration.FrequencyDeviation = uint32(option.ProtocolConfiguration.GetLoRaWAN().BitRate / 2)
return option, nil
}
if option, err := buildRX1(); err == nil {
options = append(options, option)
}
computeDownlinkScores(gateway, uplink, options)
for _, option := range options {
// Add router ID to downlink option
if r.Component != nil && r.Component.Identity != nil {
option.Identifier = fmt.Sprintf("%s:%s", r.Component.Identity.ID, option.Identifier)
}
// Filter all illegal options
if option.Score < 1000 {
downlinkOptions = append(downlinkOptions, option)
}
}
return
}
// Calculating the score for each downlink option; lower is better, 0 is best
// If a score is over 1000, it may should not be used as feasible option.
// TODO: The weights of these parameters should be optimized. I'm sure someone
// can do some computer simulations to find the right values.
func computeDownlinkScores(gateway *gateway.Gateway, uplink *pb.UplinkMessage, options []*pb_broker.DownlinkOption) {
gatewayStatus, _ := gateway.Status.Get() // This just returns empty if non-existing
frequencyPlan := gatewayStatus.FrequencyPlan
if frequencyPlan == "" {
frequencyPlan = band.Guess(uplink.GatewayMetadata.Frequency)
}
gatewayRx, _ := gateway.Utilization.Get()
for _, option := range options {
// Invalid if no LoRaWAN
conf := option.GetProtocolConfiguration()
lorawan := conf.GetLoRaWAN()
if lorawan == nil {
option.Score = 1000
continue
}
var time time.Duration
if lorawan.Modulation == pb_lorawan.Modulation_LORA {
// Calculate max ToA
time, _ = toa.ComputeLoRa(
51+13, // Max MACPayload plus LoRaWAN header, TODO: What is the length we should use?
lorawan.DataRate,
lorawan.CodingRate,
)
}
if lorawan.Modulation == pb_lorawan.Modulation_FSK {
// Calculate max ToA
time, _ = toa.ComputeFSK(
51+13, // Max MACPayload plus LoRaWAN header, TODO: What is the length we should use?
int(lorawan.BitRate),
)
}
// Invalid if time is zero
if time == 0 {
option.Score = 1000
continue
}
timeScore := math.Min(time.Seconds()*5, 10) // 2 seconds will be 10 (max)
signalScore := 0.0 // Between 0 and 20 (lower is better)
{
// Prefer high SNR
if uplink.GatewayMetadata.SNR < 5 {
signalScore += 10
}
// Prefer good RSSI
signalScore += math.Min(float64(uplink.GatewayMetadata.RSSI*-0.1), 10)
}
utilizationScore := 0.0 // Between 0 and 40 (lower is better) will be over 100 if forbidden
{
// Avoid gateways that do more Rx
utilizationScore += math.Min(gatewayRx*50, 20) / 2 // 40% utilization = 10 (max)
// Avoid busy channels
freq := option.GatewayConfiguration.Frequency
channelRx, channelTx := gateway.Utilization.GetChannel(freq)
utilizationScore += math.Min((channelTx+channelRx)*200, 20) / 2 // 10% utilization = 10 (max)
// European Duty Cycle
if frequencyPlan == "EU_863_870" {
var duty float64
switch {
case freq >= 863000000 && freq < 868000000:
duty = 0.01 // g 863.0 – 868.0 MHz 1%
case freq >= 868000000 && freq < 868600000:
duty = 0.01 // g1 868.0 – 868.6 MHz 1%
case freq >= 868700000 && freq < 869200000:
duty = 0.001 // g2 868.7 – 869.2 MHz 0.1%
case freq >= 869400000 && freq < 869650000:
duty = 0.1 // g3 869.4 – 869.65 MHz 10%
case freq >= 869700000 && freq < 870000000:
duty = 0.01 // g4 869.7 – 870.0 MHz 1%
default:
utilizationScore += 100 // Transmissions on this frequency are forbidden
}
if channelTx > duty {
utilizationScore += 100 // Transmissions on this frequency are forbidden
}
if duty > 0 {
utilizationScore += math.Min(time.Seconds()/duty/100, 20) // Impact on duty-cycle (in order to prefer RX2 for SF9BW125)
}
}
}
scheduleScore := 0.0 // Between 0 and 30 (lower is better) will be over 100 if forbidden
{
id, conflicts := gateway.Schedule.GetOption(option.GatewayConfiguration.Timestamp, uint32(time/1000))
option.Identifier = id
if conflicts >= 100 {
scheduleScore += 100
} else {
scheduleScore += math.Min(float64(conflicts*10), 30) // max 30
}
}
option.Score = uint32((timeScore + signalScore + utilizationScore + scheduleScore) * 10)
}
}