-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbparser.js
More file actions
265 lines (244 loc) · 6.44 KB
/
dbparser.js
File metadata and controls
265 lines (244 loc) · 6.44 KB
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
'use strict'
/** @typedef {[string, Country][]} RegDB */
const parseDB = (function () {
const FLAG = {
'NO-OFDM': 1<<0,
'NO-CCK': 1<<1,
'NO-INDOOR': 1<<2,
'NO-OUTDOOR': 1<<3,
'DFS': 1<<4,
'PTP-ONLY': 1<<5,
'PTMP-ONLY': 1<<6,
'NO-IR': 1<<7,
// hole at bit 8
// hole at bit 9. FIXME: Where is NO-HT40 defined?
'NO-HT40': 1<<10,
'AUTO-BW': 1<<11,
}
class DFSRegion {
static Unknown = new DFSRegion('', 0)
static FCC = new DFSRegion('DFS-FCC', 1)
static ETSI = new DFSRegion('DFS-ETSI', 2)
static JP = new DFSRegion('DFS-JP', 3)
/**
* @param {string} name
* @param {number} value
*/
constructor (name, value) {
/** @type {string} */
this.name = name
/** @type {number} */
this.value = value
}
/**
* @param {string} str
* @returns {DFSRegion}
*/
static parse (str) {
switch (str) {
case '': return DFSRegion.Unknown
case 'DFS-FCC': return DFSRegion.FCC
case 'DFS-ETSI': return DFSRegion.ETSI
case 'DFS-JP': return DFSRegion.JP
default: throw new Error(`Unknown DFS region: ${str}`)
}
}
toString () {
return this.name
}
valueOf () {
return this.value
}
}
class FreqBand extends Interval {
/**
* @param {number} start
* @param {number} end
* @param {number} bw
* @param {string?} comments
*/
constructor (start, end, bw, comments) {
super(start, end)
/** @type {number} */
this.bw = bw
/** @type {string?} */
this.comments = comments
}
/**
* @param {string} str
* @param {string?} comments
* @returns {FreqBand}
*/
static parse (str, comments) {
const [_, start, end, bw] = str.match(/^([^-]*)-([^@]*)@(.*)$/)
return new FreqBand(parseFloat(start), parseFloat(end), parseFloat(bw),
comments)
}
/**
* @param {Channel} channel
* @returns {boolean}
*/
hasChannel (channel) {
return this.start <= channel.start && channel.end <= this.end &&
channel.bw <= this.bw
}
}
class PowerRestriction {
/**
* @param {number} max_ant_gain
* @param {number} max_eirp
* @param {string?} comments
*/
constructor (max_ant_gain, max_eirp, comments) {
/** @type {number} */
this.max_ant_gain = max_ant_gain
/** @type {number} */
this.max_eirp = max_eirp
/** @type {string?} */
this.comments = comments
}
/**
* @param {string} str
* @returns {PowerRestriction}
*/
static parse (str) {
let dbm = parseInt(str)
if (str.endsWith('mW')) {
dbm = 10 * Math.log10(dbm)
}
return new PowerRestriction(0, dbm)
}
/**
* @param {boolean?} usemW
* @returns {string}
*/
toString (usemW) {
return usemW ? `${(10 ** (this.max_eirp / 10)).toFixed()} mW` :
`${this.max_eirp.toFixed()} dBm`
}
/**
* @param {boolean?} usemW
* @returns {number}
*/
valueOf (usemW) {
return (usemW ? 10 ** (this.max_eirp / 10) : this.max_eirp).toFixed()
}
}
class Permission {
/**
* @param {FreqBand} freqband
* @param {PowerRestriction} power
* @param {string[]?} flags
* @param {string?} wmmrule
*/
constructor (freqband, power, flags, wmmrule) {
/** @type {FreqBand} */
this.freqband = freqband
/** @type {PowerRestriction} */
this.power = power
/** @type {string[]?} */
this.flags = flags
if (this.flags) {
this.flags.sort()
}
/** @type {string?} */
this.wmmrule = wmmrule
}
/**
* @param {string} str
* @param {string?} comments
* @returns {Permission}
*/
static parse (str, comments) {
const flags = str.split(',').map(s => s.trim())
const freqband = flags.shift().slice(1, -1)
const power = flags.shift().slice(1, -1)
let wmmrule = null
for (let i = 0; i < flags.length; i++) {
if (flags[i].startsWith('wmmrule=')) {
wmmrule = flags[i].slice(8)
flags.splice(i, 1)
break
}
}
return new Permission(
FreqBand.parse(freqband, comments),
PowerRestriction.parse(power), flags, wmmrule)
}
toString () {
return `${this.freqband} ${this.power} ${this.wmmrule}`
}
/**
* @param {Channel} channel
* @returns {boolean}
*/
hasChannel (channel) {
return this.freqband.hasChannel(channel)
}
}
class Country {
/**
* @param {DFSRegion} dfs_region
* @param {Permission[]} permissions
* @param {string?} comments
*/
constructor (dfs_region, permissions, comments) {
/** @type {DFSRegion} */
this.dfs_region = dfs_region
/** @type {Permission[]} */
this.permissions = permissions
/** @type {string?} */
this.comments = comments
}
/**
* @param {Channel} channel
* @returns {Permission?}
*/
hasChannel (channel) {
return this.permissions.find(p => p.hasChannel(channel))
}
}
/**
* @param {string} str
* @returns {RegDB}
*/
return function (str) {
const lines = str.split('\n')
/** @type {RegDB} */
const countries = []
/** @type {string[]} */
let comments = []
/** @type {string?} */
let country = null
/** @type {string?} */
let dfs_region = null
/** @type {Permission[]} */
let permissions = []
/** @type {string?} */
let country_comments = null
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim()
if (!line) {
} else if (line.startsWith('#')) {
comments.push(line.slice(1 + line.startsWith('# ')))
} else if (!lines[i].startsWith('\t')) {
if (country) {
// store old block
countries.push([
country, new Country(dfs_region, permissions, country_comments)])
}
// process new block
const header = line.split(':')
country = header[0].startsWith('country') ? header[0].slice(8) : null
dfs_region = header[1]?.trim()
permissions = []
country_comments = comments.join('\n') || null
comments = []
} else if (line.startsWith('(')) {
permissions.push(Permission.parse(line, comments.join('\n') || null))
comments = []
}
}
return countries
}
})()