forked from lorabasics/basicstation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_config.py
210 lines (178 loc) · 7.47 KB
/
router_config.py
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
# --- Revised 3-Clause BSD License ---
# Copyright (C) 2016-2019, SEMTECH (International) AG.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL SEMTECH BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from typing import Any,List,Mapping,MutableMapping,Optional
from pathlib import Path
import yaml
import copy
import logging
import pprint
import struct
from id6 import Id6, Eui
logger = logging.getLogger('ts2pktfwd')
REGION_CONFIG_KEYWORDS = [ 'upchannels', 'DRs' ]
STATION_CONFIG_KEYWORDS = [ 'JoinEui', 'NetID', 'bcning', 'regionid' ] # and more ..
class Region:
def __init__(self, o:Mapping[str,Any]):
self.name = o['name']
self.config = o['config']
for kw in REGION_CONFIG_KEYWORDS:
assert kw in self.config, 'Missing region config key: %s' % (kw)
def __str__(self):
return 'Region:%s' % (self.name)
class RouterConfig:
def __init__(self, routerid:Id6, config:MutableMapping[str,Any]):
self.routerid = routerid
station = config['station']
self.station = station
for kw in STATION_CONFIG_KEYWORDS:
assert kw in station, 'Missing station config key in router config: %s' % (kw)
regionid = station['regionid']
if regionid not in regionid2region:
raise Exception('Inexisting or invalid regionid in router configuration of %s' % (routerid))
region = regionid2region[station['regionid']]
station['DRs'] = region.config['DRs']
station['upchannels'] = region.config['upchannels']
self.region = region
logger.debug('%s: station config:\n%s' % (self, pprint.pformat(self.station)))
self.dr2sfbw = DR2SFBW(station)
self.sfbw2dr = SFBW2DR(station)
self.RxDelay = 1
region = station['region']
if region == 'EU863':
self.RX2DR = 0
self.RX2Freq = 869525000
elif region == 'US902':
self.RX2DR = 8
self.RX2Freq = 923300000
else:
raise Exception('Unsupported region: %s' % (region))
pktfwd = config['pktfwd']
self.pktfwd = pktfwd
if 'gateway_ID' not in pktfwd:
self.pktfwd['gateway_ID'] = self.routerid.id
else:
self.pktfwd['gateway_ID'] = struct.unpack('>q', bytes.fromhex(pktfwd['gateway_ID']))[0]
def get_station_config_message(self) -> MutableMapping[str,Any]:
return copy.deepcopy(self.station)
def get_pktfwd_gateway_ID(self) -> int:
return self.pktfwd['gateway_ID']
def get_hwspec(self) -> str:
return self.station['hwspec']
def get_regionid(self) -> Any:
return self.station['regionid']
def __str__(self) -> str:
return 'RouterConfig:%s' % (self.routerid)
def DR2SFBW(rconfig) -> Mapping[int,str]:
return { i:'SF%dBW%d' % (t[0], t[1]) for i,t in enumerate(rconfig['DRs']) }
def SFBW2DR(rconfig) -> Mapping[int,str]:
return { 'SF%dBW%d' % (t[0], t[1]):i for i,t in enumerate(rconfig['DRs']) }
routerid2config = {} # type:Mapping[Id6,RouterConfig]
regionid2region = {} # type:Mapping[Id6,Region]
def ini(paths:List[str]) -> None:
for s in paths:
p = Path(s)
if not p.is_dir():
raise Exception('Not a directory: %s' % (s))
for s in paths:
p = Path(s)
f = p.joinpath('regions.yaml')
if f.exists():
regions = yaml.load(f.read_text(), Loader=yaml.SafeLoader)
for regionid,o in regions.items():
regionid2region[regionid] = Region(o)
logger.info('router_config.ini: loaded regions from %s.' % (f))
break
for s in paths:
p = Path(s)
for f in p.glob('router-*.yaml'):
name = f.name
if name.endswith('.yaml'):
try:
routerid = Id6(name[:-5])
if routerid.cat == 'router':
rc = RouterConfig(routerid, yaml.load(f.read_text(), Loader=yaml.SafeLoader))
routerid2config[routerid] = rc
logger.info('router_config.ini: loaded router configuration from %s.' % (f))
else:
logger.info('router_config.ini: ignore file %s.' % (f))
except:
logger.info('router_config.ini: ignore file %s.' % (f), exc_info=True)
pass
def get_router_config(routerid:Id6) -> RouterConfig:
if routerid not in routerid2config:
raise Exception('No configuration found for router %s' % (routerid))
return routerid2config[routerid]
ROUTER_CONFIG_EU863_TRACKNET8_AS_YAML = '''
JoinEui: null
NetID: null
bcning: null
config: {}
freq_range: [863000000, 870000000]
hwspec: sx1301/1
max_eirp: 16
protocol: 1
region: EU863
regionid: 1000
sx1301_conf:
- chan_FSK: {enable: false}
chan_Lora_std: {enable: false}
chan_multiSF_0: {enable: true, if: -375000, radio: 0}
chan_multiSF_1: {enable: true, if: -175000, radio: 0}
chan_multiSF_2: {enable: true, if: 25000, radio: 0}
chan_multiSF_3: {enable: true, if: 375000, radio: 0}
chan_multiSF_4: {enable: true, if: -237500, radio: 1}
chan_multiSF_5: {enable: true, if: 237500, radio: 1}
chan_multiSF_6: {enable: false}
chan_multiSF_7: {enable: false}
radio_0: {enable: true, freq: 868475000}
radio_1: {enable: true, freq: 869287500}
'''
ROUTER_CONFIG_US902_BLOCK0_AS_YAML = '''
JoinEui: null
NetID: null
bcning: null
config: {}
freq_range: [902000000, 928000000]
hwspec: sx1301/1
max_eirp: 30.0
protocol: 1
region: US902
regionid: 1001
sx1301_conf:
- chan_FSK: {enable: false}
chan_Lora_std: {bandwidth: 500000, enable: true, if: 300000, radio: 0, spread_factor: 8}
chan_multiSF_0: {enable: true, if: -400000, radio: 0}
chan_multiSF_1: {enable: true, if: -200000, radio: 0}
chan_multiSF_2: {enable: true, if: 0, radio: 0}
chan_multiSF_3: {enable: true, if: 200000, radio: 0}
chan_multiSF_4: {enable: true, if: 400000, radio: 0}
chan_multiSF_5: {enable: true, if: -200000, radio: 1}
chan_multiSF_6: {enable: true, if: 0, radio: 1}
chan_multiSF_7: {enable: true, if: 200000, radio: 1}
radio_0: {enable: true, freq: 902700000}
radio_1: {enable: true, freq: 903500000}
'''