-
-
Notifications
You must be signed in to change notification settings - Fork 544
/
helpers.go
197 lines (173 loc) · 5.89 KB
/
helpers.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
package helpers
import (
"context"
"fmt"
"github.com/brocaar/chirpstack-network-server/v3/internal/backend/applicationserver"
"github.com/brocaar/chirpstack-network-server/v3/internal/storage"
"github.com/brocaar/lorawan"
"github.com/gofrs/uuid"
"github.com/brocaar/chirpstack-api/go/v3/as"
"github.com/brocaar/chirpstack-api/go/v3/common"
"github.com/brocaar/chirpstack-api/go/v3/gw"
"github.com/brocaar/lorawan/band"
"github.com/pkg/errors"
)
const defaultCodeRate = "4/5"
// GatewayIDGetter provides a GatewayId getter interface.
type GatewayIDGetter interface {
GetGatewayId() []byte
}
// DownlinkIDGetter provides a DownlinkId getter interface.
type DownlinkIDGetter interface {
GetDownlinkId() []byte
}
// DataRateGetter provides an interface for getting the data-rate.
type DataRateGetter interface {
GetModulation() common.Modulation
GetLoraModulationInfo() *gw.LoRaModulationInfo
GetFskModulationInfo() *gw.FSKModulationInfo
GetLrFhssModulationInfo() *gw.LRFHSSModulationInfo
}
// SetDownlinkTXInfoDataRate sets the DownlinkTXInfo data-rate.
func SetDownlinkTXInfoDataRate(txInfo *gw.DownlinkTXInfo, dr int, b band.Band) error {
dataRate, err := b.GetDataRate(dr)
if err != nil {
return errors.Wrap(err, "get data-rate error")
}
// note: LR-FHSS is uplink only, no need to handle it here.
switch dataRate.Modulation {
case band.LoRaModulation:
txInfo.Modulation = common.Modulation_LORA
txInfo.ModulationInfo = &gw.DownlinkTXInfo_LoraModulationInfo{
LoraModulationInfo: &gw.LoRaModulationInfo{
SpreadingFactor: uint32(dataRate.SpreadFactor),
Bandwidth: uint32(dataRate.Bandwidth),
CodeRate: defaultCodeRate,
PolarizationInversion: true,
},
}
case band.FSKModulation:
txInfo.Modulation = common.Modulation_FSK
txInfo.ModulationInfo = &gw.DownlinkTXInfo_FskModulationInfo{
FskModulationInfo: &gw.FSKModulationInfo{
Datarate: uint32(dataRate.BitRate),
FrequencyDeviation: uint32(dataRate.BitRate / 2), // see: https://github.com/brocaar/chirpstack-gateway-bridge/issues/16
},
}
default:
return fmt.Errorf("unexpected modulation: %s", dataRate.Modulation)
}
return nil
}
// SetUplinkTXInfoDataRate sets the UplinkTXInfo data-rate.
func SetUplinkTXInfoDataRate(txInfo *gw.UplinkTXInfo, dr int, b band.Band) error {
dataRate, err := b.GetDataRate(dr)
if err != nil {
return errors.Wrap(err, "get data-rate error")
}
switch dataRate.Modulation {
case band.LoRaModulation:
txInfo.Modulation = common.Modulation_LORA
txInfo.ModulationInfo = &gw.UplinkTXInfo_LoraModulationInfo{
LoraModulationInfo: &gw.LoRaModulationInfo{
SpreadingFactor: uint32(dataRate.SpreadFactor),
Bandwidth: uint32(dataRate.Bandwidth),
CodeRate: defaultCodeRate,
PolarizationInversion: true,
},
}
case band.FSKModulation:
txInfo.Modulation = common.Modulation_FSK
txInfo.ModulationInfo = &gw.UplinkTXInfo_FskModulationInfo{
FskModulationInfo: &gw.FSKModulationInfo{
Datarate: uint32(dataRate.BitRate),
},
}
case band.LRFHSSModulation:
txInfo.Modulation = common.Modulation_LR_FHSS
txInfo.ModulationInfo = &gw.UplinkTXInfo_LrFhssModulationInfo{
LrFhssModulationInfo: &gw.LRFHSSModulationInfo{
OperatingChannelWidth: uint32(dataRate.OccupiedChannelWidth),
CodeRate: dataRate.CodingRate,
// GridSteps: this value can't be derived from a DR?
},
}
default:
return fmt.Errorf("unknown modulation: %s", dataRate.Modulation)
}
return nil
}
// GetGatewayID returns the typed gateway ID.
func GetGatewayID(v GatewayIDGetter) lorawan.EUI64 {
var gatewayID lorawan.EUI64
copy(gatewayID[:], v.GetGatewayId())
return gatewayID
}
// GetUplinkID returns the typed message ID.
func GetUplinkID(v *gw.UplinkRXInfo) uuid.UUID {
var msgID uuid.UUID
if v != nil {
copy(msgID[:], v.GetUplinkId())
}
return msgID
}
// GetStatsID returns the typed stats ID.
func GetStatsID(v *gw.GatewayStats) uuid.UUID {
var statsID uuid.UUID
if v != nil {
copy(statsID[:], v.GetStatsId())
}
return statsID
}
// GetDownlinkID returns the types downlink ID.
func GetDownlinkID(v DownlinkIDGetter) uuid.UUID {
var downlinkID uuid.UUID
if b := v.GetDownlinkId(); b != nil {
copy(downlinkID[:], b)
}
return downlinkID
}
// GetDataRateIndex returns the data-rate index.
func GetDataRateIndex(uplink bool, v DataRateGetter, b band.Band) (int, error) {
var dr band.DataRate
switch v.GetModulation() {
case common.Modulation_LORA:
modInfo := v.GetLoraModulationInfo()
if modInfo == nil {
return 0, errors.New("lora_modulation_info must not be nil")
}
dr.Modulation = band.LoRaModulation
dr.SpreadFactor = int(modInfo.SpreadingFactor)
dr.Bandwidth = int(modInfo.Bandwidth)
case common.Modulation_FSK:
modInfo := v.GetFskModulationInfo()
if modInfo == nil {
return 0, errors.New("fsk_modulation_info must not be nil")
}
dr.Modulation = band.FSKModulation
dr.BitRate = int(modInfo.Datarate)
case common.Modulation_LR_FHSS:
modInfo := v.GetLrFhssModulationInfo()
if modInfo == nil {
return 0, errors.New("lr_fhss_modulation_info must not be nil")
}
dr.Modulation = band.LRFHSSModulation
dr.CodingRate = modInfo.CodeRate
dr.OccupiedChannelWidth = int(modInfo.OperatingChannelWidth)
default:
return 0, fmt.Errorf("unknown modulation: %s", v.GetModulation())
}
return b.GetDataRateIndex(uplink, dr)
}
// GetASClientForRoutingProfileID returns the AS client given a Routing Profile ID.
func GetASClientForRoutingProfileID(ctx context.Context, id uuid.UUID) (as.ApplicationServerServiceClient, error) {
rp, err := storage.GetRoutingProfile(ctx, storage.DB(), id)
if err != nil {
return nil, errors.Wrap(err, "get routing-profile error")
}
asClient, err := applicationserver.Pool().Get(rp.ASID, []byte(rp.CACert), []byte(rp.TLSCert), []byte(rp.TLSKey))
if err != nil {
return nil, errors.Wrap(err, "get application-server client error")
}
return asClient, nil
}