make dump.c dump in the db.txt format
[platform/upstream/crda.git] / db2sql.py
1 #!/usr/bin/env python
2
3 from cStringIO import StringIO
4 import struct
5 import sha
6 from dbparse import DBParser, create_rules, create_collections
7
8 MAGIC = 0x52474442
9 VERSION = 19
10
11 p = DBParser()
12 bands, power, countries = p.parse(file('db.txt'))
13 rules = create_rules(countries)
14 collections = create_collections(countries)
15
16 print """
17 use regulatory;
18
19 /* This is the DB data for the CRDA database */
20
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'
28 );
29
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'
39 );
40
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.
45  *
46  * Note: a dipole antenna has a gain of 2.14 dBi, so 2140 mBi)
47  */
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'
57 );
58
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'
66 );
67
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'
77 );
78 """
79
80 power_rules = {}
81 power_rule = 0
82 for power_rule_id, pr in power.iteritems():
83     power_rule += 1
84     environ = pr[0]
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])
89
90 freq_ranges = {}
91 freq_range = 0
92 for freq_range_id, fr in bands.iteritems():
93     freq_range += 1
94     freq_ranges[freq_range_id] = freq_range
95     fl = fr[3]
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)
99
100
101 reg_rules = {}
102 reg_rule = 0
103 for freq_range_id, power_rule_id in rules:
104     reg_rule += 1
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])
108
109
110 reg_rules_collections = {}
111 reg_rule_coll = 0
112 reg_rule_entry = 0
113 for coll in collections:
114     reg_rule_coll += 1
115     reg_rules_collections[coll] = reg_rule_coll
116     for c in coll:
117         reg_rule_entry += 1
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])
120
121 country_id = 0
122 for alpha2, coll in countries.iteritems():
123     country_id += 1
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])