-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
pf_config.go
308 lines (261 loc) · 9.1 KB
/
pf_config.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
package gateway
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"regexp"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/brocaar/loraserver/api/gw"
"github.com/brocaar/lorawan"
"github.com/brocaar/lorawan/band"
)
// radioBandwidthPerChannelBandwidth defines the bandwidth that a single radio
// can cover per channel bandwidth
var radioBandwidthPerChannelBandwidth = map[int]int{
500000: 1100000, // 500kHz channel
250000: 1000000, // 250kHz channel
125000: 925000, // 125kHz channel
}
// defaultRadioBandwidth defines the radio bandwidth in case the channel
// bandwidth does not match any of the above values.
const defaultRadioBandwidth = 925000
// radioCount defines the number of radios available
const radioCount = 2
// channelCount defines the number of available channels
const channelCount = 8
// jsonCommentRegexp matches json comments
var jsonCommentRegexp = regexp.MustCompile(`/\*.*\*/`)
// radioConfig contains the radio configuration.
type radioConfig struct {
Enable bool
Freq int
}
// multiSFChannelConfig contains the configuration for a multi spreading-factor
// channel.
type multiSFChannelConfig struct {
Enable bool
Radio int
IF int
Freq int
}
// LoRaSTDChannelConfig contains the configuration for a single
// spreading-factor LoRa channel.
type loRaSTDChannelConfig struct {
Enable bool
Radio int
IF int
Bandwidth int
SpreadFactor int
Freq int
}
// fskChannelConfig contains the configuratio for a FSK channel.
type fskChannelConfig struct {
Enable bool
Radio int
IF int
Bandwidth int
DataRate int
Freq int
}
// configFile represents a packet-forwarder JSON config file.
type configFile struct {
SX1301Conf map[string]interface{} `json:"SX1301_conf"`
GatewayConf map[string]interface{} `json:"gateway_conf"`
}
// gatewayConfiguration contains the radio configuration for a gateway.
type gatewayConfiguration struct {
Radios [radioCount]radioConfig
MultiSFChannels [channelCount]multiSFChannelConfig
LoRaSTDChannelConfig loRaSTDChannelConfig
FSKChannelConfig fskChannelConfig
}
// channelByMinRadioCenterFreqency implements sort.Interface for []*gw.Channel.
// The sorting is based on the center frequency of the radio when placing the
// channel exactly on the left side of the available radio bandwidth.
type channelByMinRadioCenterFrequency []gw.Channel
func (c channelByMinRadioCenterFrequency) Len() int { return len(c) }
func (c channelByMinRadioCenterFrequency) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c channelByMinRadioCenterFrequency) Less(i, j int) bool {
return c.minRadioCenterFreq(i) < c.minRadioCenterFreq(j)
}
func (c channelByMinRadioCenterFrequency) minRadioCenterFreq(i int) int {
channelBandwidth := int(c[i].Bandwidth * 1000)
radioBandwidth, ok := radioBandwidthPerChannelBandwidth[channelBandwidth]
if !ok {
radioBandwidth = defaultRadioBandwidth
}
return int(c[i].Frequency) - (channelBandwidth / 2) + (radioBandwidth / 2)
}
// getGatewayConfig transforms the given GatewayConfigurationPacket into a
// gatewayConfiguration object. It will determine the configuration for the
// radios and their center frequencies and the channels assigned to each radio.
func getGatewayConfig(conf gw.GatewayConfigPacket) (gatewayConfiguration, error) {
var gc gatewayConfiguration
var multiSFCounter int
// make sure the channels are sorted by the minimum radio center frequency
sort.Sort(channelByMinRadioCenterFrequency(conf.Channels))
// define the radios and their center frequency
for _, c := range conf.Channels {
channelBandwidth := c.Bandwidth * 1000
channelMax := c.Frequency + (channelBandwidth / 2)
radioBandwidth, ok := radioBandwidthPerChannelBandwidth[channelBandwidth]
if !ok {
radioBandwidth = defaultRadioBandwidth
}
minRadioCenterFreq := c.Frequency - (channelBandwidth / 2) + (radioBandwidth / 2)
for i, r := range gc.Radios {
// the radio is not defined yet, use it
if !r.Enable {
gc.Radios[i].Enable = true
gc.Radios[i].Freq = minRadioCenterFreq
break
}
if channelMax <= r.Freq+(radioBandwidth/2) {
break
}
}
}
// assign channels
for _, c := range conf.Channels {
var radio int
channelBandwidth := int(c.Bandwidth * 1000)
channelMin := c.Frequency - (channelBandwidth / 2)
channelMax := c.Frequency + (channelBandwidth / 2)
radioBandwidth, ok := radioBandwidthPerChannelBandwidth[channelBandwidth]
if !ok {
radioBandwidth = defaultRadioBandwidth
}
// get the radio covering the channel frequency
for i, r := range gc.Radios {
if channelMin >= r.Freq-(radioBandwidth/2) && channelMax <= r.Freq+(radioBandwidth/2) {
radio = i
break
}
}
if c.Modulation == band.FSKModulation {
// FSK channel
if gc.FSKChannelConfig.Enable {
return gc, errors.New("gateway: fsk channel already configured")
}
gc.FSKChannelConfig = fskChannelConfig{
Enable: true,
Radio: radio,
IF: c.Frequency - gc.Radios[radio].Freq,
Bandwidth: c.Bandwidth,
DataRate: c.Bitrate,
Freq: c.Frequency,
}
} else if c.Modulation == band.LoRaModulation && len(c.SpreadingFactors) == 1 {
// LoRa STD (single SF) channel
if gc.LoRaSTDChannelConfig.Enable {
return gc, errors.New("gateway: lora std channel already configured")
}
gc.LoRaSTDChannelConfig = loRaSTDChannelConfig{
Enable: true,
Radio: radio,
IF: c.Frequency - gc.Radios[radio].Freq,
Bandwidth: channelBandwidth,
SpreadFactor: c.SpreadingFactors[0],
Freq: c.Frequency,
}
} else if c.Modulation == band.LoRaModulation {
// LoRa multi-SF channels
if multiSFCounter >= channelCount {
return gc, errors.New("gateway: exceeded maximum number of multi-sf channels")
}
gc.MultiSFChannels[multiSFCounter] = multiSFChannelConfig{
Enable: true,
Radio: radio,
IF: c.Frequency - gc.Radios[radio].Freq,
Freq: c.Frequency,
}
multiSFCounter++
} else {
return gc, fmt.Errorf("gateway: invalid modulation: %s", c.Modulation)
}
}
return gc, nil
}
func loadConfigFile(filePath string) (configFile, error) {
var out configFile
b, err := ioutil.ReadFile(filePath)
if err != nil {
return out, errors.Wrap(err, "read file error")
}
// remove comments from json
b = jsonCommentRegexp.ReplaceAll(b, []byte{})
if err = json.Unmarshal(b, &out); err != nil {
return out, errors.Wrap(err, "unmarshal config json error")
}
return out, nil
}
// mergeConfig merges the new configuration into the given configuration.
// Unfortunately we have to do this as the packet-forwarder sees these keys
// as complete overrides (it does not just update the leaves).
// We want to remain the other configuration (e.g. which radio chip is used,
// calibration values that are board specific).
// This is not pretty but it works.
func mergeConfig(mac lorawan.EUI64, config configFile, newConfig gatewayConfiguration) error {
// update radios
for i, r := range newConfig.Radios {
key := fmt.Sprintf("radio_%d", i)
radio, ok := config.SX1301Conf[key].(map[string]interface{})
if !ok {
return fmt.Errorf("expected %s to be of type map[string]interface{}, got %T", key, config.SX1301Conf[key])
}
radio["enable"] = r.Enable
radio["freq"] = r.Freq
}
// update multi SF channels
for i, c := range newConfig.MultiSFChannels {
key := fmt.Sprintf("chan_multiSF_%d", i)
channel, ok := config.SX1301Conf[key].(map[string]interface{})
if !ok {
return fmt.Errorf("expected %s to be of type map[string]interface{}, got %T", key, config.SX1301Conf[key])
}
channel["enable"] = c.Enable
channel["radio"] = c.Radio
channel["if"] = c.IF
}
// update LoRa std channel
channel, ok := config.SX1301Conf["chan_Lora_std"].(map[string]interface{})
if !ok {
return fmt.Errorf("expected chan_Lora_std to be of type map[string]interface{}, got %T", config.SX1301Conf["chan_Lora_std"])
}
channel["enable"] = newConfig.LoRaSTDChannelConfig.Enable
channel["radio"] = newConfig.LoRaSTDChannelConfig.Radio
channel["if"] = newConfig.LoRaSTDChannelConfig.IF
channel["bandwidth"] = newConfig.LoRaSTDChannelConfig.Bandwidth
channel["spread_factor"] = newConfig.LoRaSTDChannelConfig.SpreadFactor
// update FSK channel
channel, ok = config.SX1301Conf["chan_FSK"].(map[string]interface{})
if !ok {
return fmt.Errorf("expected chan_FSK to be of type map[string]interface{}, got %T", config.SX1301Conf["chan_FSK"])
}
channel["enable"] = newConfig.FSKChannelConfig.Enable
channel["radio"] = newConfig.FSKChannelConfig.Radio
channel["if"] = newConfig.FSKChannelConfig.IF
channel["bandwidth"] = newConfig.FSKChannelConfig.Bandwidth
channel["datarate"] = newConfig.FSKChannelConfig.DataRate
// update gateway mac / ID
config.GatewayConf["gateway_ID"] = mac.String()
return nil
}
func invokePFRestart(cmd string) error {
parts := strings.Fields(cmd)
if len(parts) == 0 {
return errors.New("gateway: no packet-forwarder restart command configured")
}
var args []string
if len(parts) > 1 {
args = parts[1:len(parts)]
}
_, err := exec.Command(parts[0], args...).Output()
if err != nil {
return errors.Wrap(err, "execute command error")
}
return nil
}