-
Notifications
You must be signed in to change notification settings - Fork 0
/
activation.go
172 lines (152 loc) · 4.65 KB
/
activation.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
// 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"
"sync"
"time"
pb_broker "github.com/TheThingsNetwork/ttn/api/broker"
pb_protocol "github.com/TheThingsNetwork/ttn/api/protocol"
pb_lorawan "github.com/TheThingsNetwork/ttn/api/protocol/lorawan"
pb "github.com/TheThingsNetwork/ttn/api/router"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/core/band"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/apex/log"
)
func (r *router) HandleActivation(gatewayID string, activation *pb.DeviceActivationRequest) (res *pb.DeviceActivationResponse, err error) {
ctx := r.Ctx.WithFields(log.Fields{
"GatewayID": gatewayID,
"AppEUI": *activation.AppEui,
"DevEUI": *activation.DevEui,
})
start := time.Now()
defer func() {
if err != nil {
activation.Trace = activation.Trace.WithEvent(trace.DropEvent, "reason", err)
ctx.WithError(err).Warn("Could not handle activation")
} else {
ctx.WithField("Duration", time.Now().Sub(start)).Info("Handled activation")
}
}()
r.status.activations.Mark(1)
activation.Trace = activation.Trace.WithEvent(trace.ReceiveEvent, "gateway", gatewayID)
gateway := r.getGateway(gatewayID)
gateway.LastSeen = time.Now()
uplink := &pb.UplinkMessage{
Payload: activation.Payload,
ProtocolMetadata: activation.ProtocolMetadata,
GatewayMetadata: activation.GatewayMetadata,
Trace: activation.Trace,
}
if err = gateway.HandleUplink(uplink); err != nil {
return nil, err
}
if !gateway.Schedule.IsActive() {
return nil, errors.NewErrInternal(fmt.Sprintf("Gateway %s not available for downlink", gatewayID))
}
downlinkOptions := r.buildDownlinkOptions(uplink, true, gateway)
activation.Trace = uplink.Trace.WithEvent(trace.BuildDownlinkEvent,
"options", len(downlinkOptions),
)
// Find Broker
brokers, err := r.Discovery.GetAll("broker")
if err != nil {
return nil, err
}
// Prepare request
request := &pb_broker.DeviceActivationRequest{
Payload: activation.Payload,
DevEui: activation.DevEui,
AppEui: activation.AppEui,
ProtocolMetadata: activation.ProtocolMetadata,
GatewayMetadata: activation.GatewayMetadata,
ActivationMetadata: &pb_protocol.ActivationMetadata{
Protocol: &pb_protocol.ActivationMetadata_Lorawan{
Lorawan: &pb_lorawan.ActivationMetadata{
AppEui: activation.AppEui,
DevEui: activation.DevEui,
},
},
},
DownlinkOptions: downlinkOptions,
Trace: activation.Trace,
}
// Prepare LoRaWAN activation
status, err := gateway.Status.Get()
if err != nil {
return nil, err
}
region := status.Region
if region == "" {
region = band.Guess(uplink.GatewayMetadata.Frequency)
}
band, err := band.Get(region)
if err != nil {
return nil, err
}
lorawan := request.ActivationMetadata.GetLorawan()
lorawan.Rx1DrOffset = 0
lorawan.Rx2Dr = uint32(band.RX2DataRate)
lorawan.RxDelay = uint32(band.ReceiveDelay1.Seconds())
if band.CFList != nil {
lorawan.CfList = new(pb_lorawan.CFList)
for _, freq := range band.CFList {
lorawan.CfList.Freq = append(lorawan.CfList.Freq, freq)
}
}
ctx = ctx.WithField("NumBrokers", len(brokers))
request.Trace = request.Trace.WithEvent(trace.ForwardEvent,
"brokers", len(brokers),
)
// Forward to all brokers and collect responses
var wg sync.WaitGroup
responses := make(chan *pb_broker.DeviceActivationResponse, len(brokers))
for _, broker := range brokers {
broker, err := r.getBroker(broker)
if err != nil {
continue
}
// Do async request
wg.Add(1)
go func() {
res, err := broker.client.Activate(r.Component.GetContext(""), request)
if err == nil && res != nil {
responses <- res
}
wg.Done()
}()
}
// Make sure to close channel when all requests are done
go func() {
wg.Wait()
close(responses)
}()
var gotFirst bool
for res := range responses {
if gotFirst {
ctx.Warn("Duplicate Activation Response")
} else {
gotFirst = true
downlink := &pb_broker.DownlinkMessage{
Payload: res.Payload,
Message: res.Message,
DownlinkOption: res.DownlinkOption,
Trace: res.Trace,
}
err := r.HandleDownlink(downlink)
if err != nil {
ctx.Warn("Could not send downlink for Activation")
gotFirst = false // try again
}
}
}
// Activation not accepted by any broker
if !gotFirst {
ctx.Debug("Activation not accepted at this gateway")
return nil, errors.New("Activation not accepted at this Gateway")
}
// Activation accepted by (at least one) broker
ctx.Debug("Activation accepted")
return &pb.DeviceActivationResponse{}, nil
}