3 from cStringIO import StringIO
6 from dbparse import DBParser, create_rules, create_collections
12 bands, power, countries = p.parse(file('db.txt'))
13 rules = create_rules(countries)
14 collections = create_collections(countries)
19 /* This is the DB data for the CRDA database */
21 /* Each regulatory rule defined has a set of frequency ranges with
22 * an attached power rule. */
23 drop table if exists reg_rule;
24 CREATE TABLE reg_rule (
25 reg_rule_id INTEGER PRIMARY KEY NOT NULL,
26 freq_range_id INTEGER NOT NULL default '0',
27 power_rule_id INTEGER NOT NULL default '0'
30 /* Frequency ranges */
31 drop table if exists freq_range;
32 CREATE TABLE freq_range (
33 freq_range_id INTEGER PRIMARY KEY NOT NULL,
34 start_freq_khz INTEGER NOT NULL default '0',
35 end_freq_khz INTEGER NOT NULL default '0',
36 max_bandwidth_khz INTEGER NOT NULL default '0',
37 modulation_cap INTEGER NOT NULL default '0',
38 misc_restrictions INTEGER NOT NULL default '0'
41 /* Power rule. Each power rule can be attached to a frequency range.
42 * We use mili to avoid floating point in the kernel.
43 * EIRP and IR is given in mili Bells with respect to 1 mili Watt, so mbm
44 * Antenna gain is given in mili Bells with respect to the isotropic, so mbi.
46 * Note: a dipole antenna has a gain of 2.14 dBi, so 2140 mBi)
48 drop table if exists power_rule;
49 CREATE TABLE power_rule (
50 power_rule_id INTEGER PRIMARY KEY NOT NULL,
51 environment_cap char(1) NOT NULL,
52 max_antenna_gain_mbi INTEGER NOT NULL default '0',
53 max_ir_ptmp_mbm INTEGER NOT NULL default '0',
54 max_ir_ptp_mbm INTEGER NOT NULL default '0',
55 max_eirp_pmtp_mbm INTEGER NOT NULL default '0',
56 max_eirp_ptp_mbm INTEGER NOT NULL default '0'
59 /* Each collection has a list of rules attached it. The reg_collection_id is what we use
60 * to group together a bunch of rules into a group. */
61 drop table if exists reg_rules_collection;
62 CREATE TABLE reg_rules_collection (
63 entry_id INTEGER PRIMARY KEY NOT NULL,
64 reg_collection_id INTEGER NOT NULL default '0',
65 reg_rule_id INTEGER NOT NULL default '0'
68 /* ISO3166-alpha2 <--> regulatory collection id mapping. Each country can have
69 * more than one collection id. This allows for large flexibility on how
70 * we can group together in collections rule ids. Right now we group together
71 * common rules into bands groups (2 GHz and 5 GHz for now) */
72 drop table if exists reg_country;
73 CREATE TABLE reg_country (
74 reg_country_id INTEGER PRIMARY KEY NOT NULL,
75 alpha2 char(2) NOT NULL,
76 reg_collection_id INTEGER NOT NULL default '0'
82 for power_rule_id, pr in power.iteritems():
85 pr = [int(v * 1000) for v in pr[1:]]
86 power_rules[power_rule_id] = power_rule
87 print 'INSERT INTO power_rule (power_rule_id, environment_cap, max_antenna_gain_mbi, max_ir_ptmp_mbm, max_ir_ptp_mbm, max_eirp_pmtp_mbm, max_eirp_ptp_mbm) VALUES',
88 print "(%d, '%s', %d, %d, %d, %d, %d);" % (power_rule, environ, pr[0], pr[1], pr[2], pr[3], pr[4])
92 for freq_range_id, fr in bands.iteritems():
94 freq_ranges[freq_range_id] = freq_range
96 fr = [int(f * 1000) for f in fr[:3]]
97 print 'INSERT INTO freq_range (freq_range_id, start_freq_khz, end_freq_khz, max_bandwidth_khz, modulation_cap) VALUES',
98 print '(%d, %d, %d, %d, %d);' % (freq_range, fr[0], fr[1], fr[2], fl)
103 for freq_range_id, power_rule_id in rules:
105 reg_rules[(freq_range_id, power_rule_id)] = reg_rule
106 print 'INSERT INTO reg_rule (reg_rule_id, freq_range_id, power_rule_id) VALUES',
107 print '(%d, %d, %d);' % (reg_rule, freq_ranges[freq_range_id], power_rules[power_rule_id])
110 reg_rules_collections = {}
113 for coll in collections:
115 reg_rules_collections[coll] = reg_rule_coll
118 print 'INSERT INTO reg_rules_collection (entry_id, reg_collection_id, reg_rule_id) VALUES',
119 print '(%d, %d, %d);' % (reg_rule_entry, reg_rule_coll, reg_rules[c])
122 for alpha2, coll in countries.iteritems():
124 print 'INSERT INTO reg_country (reg_country_id, alpha2, reg_collection_id) VALUES',
125 print "(%d, '%s', %d);" % (country_id, alpha2, reg_rules_collections[coll])