-
Notifications
You must be signed in to change notification settings - Fork 0
/
uplink.go
154 lines (130 loc) · 4.47 KB
/
uplink.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
// 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 (
"time"
pb_broker "github.com/TheThingsNetwork/api/broker"
"github.com/TheThingsNetwork/api/logfields"
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/router/gateway"
"github.com/TheThingsNetwork/ttn/core/types"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/brocaar/lorawan"
)
func (r *router) HandleUplink(gatewayID string, uplink *pb.UplinkMessage) (err error) {
ctx := r.Ctx.WithField("GatewayID", gatewayID).WithFields(logfields.ForMessage(uplink))
start := time.Now()
var gateway *gateway.Gateway
r.RegisterReceived(uplink)
defer func() {
if err != nil {
uplink.Trace = uplink.Trace.WithEvent(trace.DropEvent, "reason", err)
ctx.WithError(err).Warn("Could not handle uplink")
} else {
r.RegisterHandled(uplink)
}
if gateway != nil && gateway.MonitorStream != nil {
gateway.MonitorStream.Send(uplink)
}
}()
r.status.uplink.Mark(1)
uplink.Trace = uplink.Trace.WithEvent(trace.ReceiveEvent, "gateway", gatewayID)
// LoRaWAN: Unmarshal
var phyPayload lorawan.PHYPayload
err = phyPayload.UnmarshalBinary(uplink.Payload)
if err != nil {
return err
}
if phyPayload.MHDR.MType == lorawan.JoinRequest {
joinRequestPayload, ok := phyPayload.MACPayload.(*lorawan.JoinRequestPayload)
if !ok {
return errors.NewErrInvalidArgument("Join Request", "does not contain a JoinRequest payload")
}
devEUI := types.DevEUI(joinRequestPayload.DevEUI)
appEUI := types.AppEUI(joinRequestPayload.AppEUI)
ctx.WithFields(ttnlog.Fields{
"DevEUI": devEUI,
"AppEUI": appEUI,
}).Debug("Handle Uplink as Activation")
r.HandleActivation(gatewayID, &pb.DeviceActivationRequest{
Payload: uplink.Payload,
DevEUI: devEUI,
AppEUI: appEUI,
ProtocolMetadata: uplink.ProtocolMetadata,
GatewayMetadata: uplink.GatewayMetadata,
Trace: uplink.Trace.WithEvent("handle uplink as activation"),
})
return nil
}
if phyPayload.MHDR.MType != lorawan.UnconfirmedDataUp && phyPayload.MHDR.MType != lorawan.ConfirmedDataUp {
ctx.Warn("Accidentally received non-uplink message")
return nil
}
if lorawan := uplink.ProtocolMetadata.GetLoRaWAN(); lorawan != nil {
ctx = ctx.WithField("Modulation", lorawan.Modulation.String())
if lorawan.Modulation == pb_lorawan.Modulation_LORA {
ctx = ctx.WithField("DataRate", lorawan.DataRate)
} else {
ctx = ctx.WithField("BitRate", lorawan.BitRate)
}
}
ctx = ctx.WithFields(ttnlog.Fields{
"Frequency": uplink.GatewayMetadata.Frequency,
"RSSI": uplink.GatewayMetadata.RSSI,
"SNR": uplink.GatewayMetadata.SNR,
})
macPayload, ok := phyPayload.MACPayload.(*lorawan.MACPayload)
if !ok {
return errors.NewErrInvalidArgument("Uplink", "does not contain a MAC payload")
}
devAddr := types.DevAddr(macPayload.FHDR.DevAddr)
ctx = ctx.WithFields(ttnlog.Fields{
"DevAddr": devAddr,
"FCnt": macPayload.FHDR.FCnt,
})
gateway = r.getGateway(gatewayID)
if err = gateway.HandleUplink(uplink); err != nil {
return err
}
var downlinkOptions []*pb_broker.DownlinkOption
if gateway.Schedule.IsActive() {
downlinkOptions = r.buildDownlinkOptions(uplink, false, gateway)
uplink.Trace = uplink.Trace.WithEvent(trace.BuildDownlinkEvent,
"options", len(downlinkOptions),
)
}
ctx = ctx.WithField("DownlinkOptions", len(downlinkOptions))
// Find Broker
brokers, err := r.Discovery.GetAllBrokersForDevAddr(devAddr)
if err != nil {
return err
}
if len(brokers) == 0 {
ctx.Debug("No brokers to forward message to")
uplink.Trace = uplink.Trace.WithEvent(trace.DropEvent, "reason", "no brokers")
return nil
}
ctx = ctx.WithField("NumBrokers", len(brokers))
uplink.Trace = uplink.Trace.WithEvent(trace.ForwardEvent,
"brokers", len(brokers),
)
// Forward to all brokers
for _, broker := range brokers {
broker, err := r.getBroker(broker)
if err != nil {
continue
}
broker.uplink <- &pb_broker.UplinkMessage{
Payload: uplink.Payload,
ProtocolMetadata: uplink.ProtocolMetadata,
GatewayMetadata: uplink.GatewayMetadata,
DownlinkOptions: downlinkOptions,
Trace: uplink.Trace,
}
}
ctx.WithField("Duration", time.Now().Sub(start)).Info("Handled uplink")
return nil
}