forked from brocaar/chirpstack-application-server
/
gwping.go
248 lines (208 loc) · 6.06 KB
/
gwping.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
package gwping
import (
"context"
"crypto/rand"
"database/sql"
"fmt"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/gomodule/redigo/redis"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/lora-app-server/internal/config"
"github.com/brocaar/lora-app-server/internal/storage"
"github.com/brocaar/loraserver/api/as"
"github.com/brocaar/loraserver/api/ns"
"github.com/brocaar/lorawan"
)
const (
micLookupExpire = time.Second * 10
micLookupTempl = "lora:as:gwping:%s"
)
// SendPingLoop is a never returning function sending the gateway pings.
func SendPingLoop() {
for {
if err := sendGatewayPing(); err != nil {
log.Errorf("send gateway ping error: %s", err)
}
time.Sleep(time.Second)
}
}
// HandleReceivedPing handles a ping received by one or multiple gateways.
func HandleReceivedPing(req *as.HandleProprietaryUplinkRequest) error {
var mic lorawan.MIC
copy(mic[:], req.Mic)
id, err := getPingLookup(mic)
if err != nil {
return errors.Wrap(err, "get ping lookup error")
}
if err = deletePingLookup(mic); err != nil {
log.Errorf("delete ping lookup error: %s", err)
}
ping, err := storage.GetGatewayPing(config.C.PostgreSQL.DB, id)
if err != nil {
return errors.Wrap(err, "get gateway ping error")
}
err = storage.Transaction(config.C.PostgreSQL.DB, func(tx sqlx.Ext) error {
for _, rx := range req.RxInfo {
var mac lorawan.EUI64
copy(mac[:], rx.GatewayId)
// ignore pings received by the sending gateway
if ping.GatewayMAC == mac {
continue
}
var receivedAt *time.Time
if rx.Time != nil {
ts, err := ptypes.Timestamp(rx.Time)
if err != nil {
return err
}
receivedAt = &ts
}
pingRX := storage.GatewayPingRX{
PingID: id,
GatewayMAC: mac,
ReceivedAt: receivedAt,
RSSI: int(rx.Rssi),
LoRaSNR: rx.LoraSnr,
}
if rx.Location != nil {
pingRX.Location = storage.GPSPoint{
Latitude: rx.Location.Latitude,
Longitude: rx.Location.Longitude,
}
pingRX.Altitude = rx.Location.Altitude
}
err := storage.CreateGatewayPingRX(tx, &pingRX)
if err != nil {
return errors.Wrap(err, "create gateway ping rx error")
}
}
return nil
})
if err != nil {
return errors.Wrap(err, "transaction error")
}
return nil
}
// sendGatewayPing selects the next gateway to ping, creates the "ping"
// frame and sends this frame to the network-server for transmission.
func sendGatewayPing() error {
return storage.Transaction(config.C.PostgreSQL.DB, func(tx sqlx.Ext) error {
gw, err := getGatewayForPing(tx)
if err != nil {
return errors.Wrap(err, "get gateway for ping error")
}
if gw == nil {
return nil
}
n, err := storage.GetNetworkServer(config.C.PostgreSQL.DB, gw.NetworkServerID)
if err != nil {
return errors.Wrap(err, "get network-server error")
}
ping := storage.GatewayPing{
GatewayMAC: gw.MAC,
Frequency: n.GatewayDiscoveryTXFrequency,
DR: n.GatewayDiscoveryDR,
}
err = storage.CreateGatewayPing(tx, &ping)
if err != nil {
return errors.Wrap(err, "create gateway ping error")
}
var mic lorawan.MIC
if _, err = rand.Read(mic[:]); err != nil {
return errors.Wrap(err, "read random bytes error")
}
err = CreatePingLookup(mic, ping.ID)
if err != nil {
return errors.Wrap(err, "store mic lookup error")
}
err = sendPing(mic, n, ping)
if err != nil {
return errors.Wrap(err, "send ping error")
}
gw.LastPingID = &ping.ID
gw.LastPingSentAt = &ping.CreatedAt
err = storage.UpdateGateway(tx, gw)
if err != nil {
return errors.Wrap(err, "update gateway error")
}
return nil
})
}
// getGatewayForPing returns the next gateway for sending a ping. If no gateway
// matches the filter criteria, nil is returned.
func getGatewayForPing(tx sqlx.Ext) (*storage.Gateway, error) {
var gw storage.Gateway
err := sqlx.Get(tx, &gw, `
select
g.*
from gateway g
inner join network_server ns
on ns.id = g.network_server_id
where
ns.gateway_discovery_enabled = true
and g.ping = true
and (g.last_ping_sent_at is null or g.last_ping_sent_at <= (now() - (interval '24 hours' / ns.gateway_discovery_interval)))
order by last_ping_sent_at
limit 1
for update`,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, errors.Wrap(err, "select error")
}
return &gw, nil
}
func sendPing(mic lorawan.MIC, n storage.NetworkServer, ping storage.GatewayPing) error {
nsClient, err := config.C.NetworkServer.Pool.Get(n.Server, []byte(n.CACert), []byte(n.TLSCert), []byte(n.TLSKey))
if err != nil {
return errors.Wrap(err, "get network-server client error")
}
_, err = nsClient.SendProprietaryPayload(context.Background(), &ns.SendProprietaryPayloadRequest{
Mic: mic[:],
GatewayMacs: [][]byte{ping.GatewayMAC[:]},
PolarizationInversion: false,
Frequency: uint32(ping.Frequency),
Dr: uint32(ping.DR),
})
if err != nil {
return errors.Wrap(err, "send proprietary payload error")
}
log.WithFields(log.Fields{
"gateway_mac": ping.GatewayMAC,
"id": ping.ID,
}).Info("gateway ping sent to network-server")
return nil
}
// CreatePingLookup creates an automatically expiring MIC to ping id lookup.
func CreatePingLookup(mic lorawan.MIC, id int64) error {
c := config.C.Redis.Pool.Get()
defer c.Close()
_, err := redis.String(c.Do("PSETEX", fmt.Sprintf(micLookupTempl, mic), int64(micLookupExpire)/int64(time.Millisecond), id))
if err != nil {
return errors.Wrap(err, "set mic lookup error")
}
return nil
}
func getPingLookup(mic lorawan.MIC) (int64, error) {
c := config.C.Redis.Pool.Get()
defer c.Close()
id, err := redis.Int64(c.Do("GET", fmt.Sprintf(micLookupTempl, mic)))
if err != nil {
return 0, errors.Wrap(err, "get ping lookup error")
}
return id, nil
}
func deletePingLookup(mic lorawan.MIC) error {
c := config.C.Redis.Pool.Get()
defer c.Close()
_, err := redis.Int(c.Do("DEL", fmt.Sprintf(micLookupTempl, mic)))
if err != nil {
return errors.Wrap(err, "delete ping lookup error")
}
return nil
}