summaryrefslogtreecommitdiffstats
path: root/Src/PyCatcher/src/pyCatcherModel.py
blob: 4e17217474d678eb23aba0492d13aecfa3fd12b4 (plain) (blame)
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
import datetime
import gtk
import math
from cellIDDatabase import CellIDDBStatus
from cellIDDatabase import CIDDatabases
from rules import RuleResult

class EnumTranslator:
    CIDDBStatus = {
        0:'Confirmed.',
        1:'Approximated.',
        2:'Error.',
        3:'Not yet looked up.',
        4:'Not in database.'
    }
    CIDDB = {
        0:'None.',
        1:'Google.',
        2:'OpenCellID',
        3:'Local Cell DB'
    }

class BaseStationInformation:

    def __init__ (self):
        self.country = 'Nowhere'
        self.provider = 'Carry'
        self.arfcn = 0
        self.rxlev = 0
        self.times_scanned = 0
        self.system_info_t2 = []
        self.system_info_t2bis = []
        self.system_info_t2ter = []
        self.discovery_time = datetime.datetime.now().strftime('%T')
        self.found = False
        self.bsic = ''
        self.lac = 0
        self.cell = 0
        self.rules_report = {}
        self.evaluation_report = {}
        self.evaluation = 'NYE'
        self.evaluation_by = 'NYE'
        self.latitude = 0
        self.longitude = 0
        self.db_status = CellIDDBStatus.NOT_LOOKED_UP
        self.db_provider = CIDDatabases.NONE
               
    def get_list_model(self):
        return self.provider, str(self.arfcn), str(self.rxlev), str(self.cell),self.evaluation, self.discovery_time,0
    
    def get_neighbour_arfcn(self):
        if 1 < self.arfcn < 125:
            return self._parse_900()
        return []

    def si_to_bin(self, si):
        neighbours = si[3:19]
        bin_representation = ''
        for value in neighbours:
            bin_representation += str(bin(int(value, 16))[2:].zfill(8))
        return bin_representation

    def parse_bit_mask(self, si, offset):
        bin_representation = self.si_to_bin(si)
        neighbours = []
        for x in xrange(1,125):
            index = 0-x
            bit = bin_representation[index]
            if bit == '1':
                neighbours.append(abs(index) + offset)
        return neighbours


    def _parse_900(self):
        neighbours = self.parse_bit_mask(self.system_info_t2, 0)
        return map(int, neighbours)

    def _parse_1800(self):
        pass

    def _parse_900_ext(self):
        pass

    def create_report(self):
        report_params = '''------- Base Station Parameters -----------
Country: %s
Provider: %s
ARFCN: %s
rxlev: %s
BSIC: %s
LAC: %s
Cell ID: %s
Neighbours: %s
Latitude: %s
Longitude: %s
Database Status: %s
Database Provider: %s
Evaluation: %s\n
'''%(self.country,self.provider, self.arfcn, self.rxlev, self.bsic, self.lac,  self.cell, ', '.join(map(str,self.get_neighbour_arfcn())),self.latitude,self.longitude,EnumTranslator.CIDDBStatus[self.db_status], EnumTranslator.CIDDB[self.db_provider],self.evaluation)

        report_rules ='------- Rule Results -----------\n'
        for key in self.rules_report.keys():
            report_rules += str(key) + ': ' + str(self.rules_report[key]) + '\n'
        report_rules +='\n\n'

        report_evaluation ='------- Evaluation Report (' + self.evaluation_by + ')-----------\n'
        for key in self.evaluation_report.keys():
            report_evaluation += str(key) + ': ' + str(self.evaluation_report[key]) + '\n'
        report_evaluation +='\n\n'

        report_raw = '''------- Raw Information -----------
SystemInfo_2:       %s
SystemInfo_2ter:    %s
SystemInfo_2bis:    %s
'''%('  '.join(self.system_info_t2), '  '.join(self.system_info_t2ter), '  '.join(self.system_info_t2bis))


        return report_params + report_rules + report_evaluation + report_raw
    
class BaseStationInformationList:
    def __init__(self):
        self._base_station_list = []
        
    def add_station(self, base_station):
        base_station.found = True
        for item in self._base_station_list:
            #TODO: check if this works like i thought
            if item.arfcn == base_station.arfcn and item.bsic == base_station.bsic:
                item.discovery_time = datetime.datetime.now().strftime('%T')
                break
        else:
            self._base_station_list.append(base_station)
    
    def get_dot_code(self, band_filter, filters=None):
        preamble = r'digraph bsnetwork { '
        postamble = r'}'
        code = ''

        filtered_list = self._get_filtered_list(band_filter, filters)

        for station in filtered_list:
            if station.evaluation == RuleResult.OK:
                code += str(station.arfcn) + r' [style = filled, fillcolor = green]; '
            elif station.evaluation == RuleResult.WARNING:
                code += str(station.arfcn) + r' [style = filled, fillcolor = yellow]; '
            elif station.evaluation == RuleResult.CRITICAL:
                code += str(station.arfcn) + r' [style = filled, fillcolor = red]; '
            else:
                code += str(station.arfcn) + r' [style = filled, fillcolor = white]; '
            for neighbour in station.get_neighbour_arfcn():
                code += str(station.arfcn) + r' -> ' + str(neighbour) + r'; '
        #TODO: make printing the source a fixed option
        #print preamble + code + postamble
        return preamble + code + postamble
    
    def refill_store(self, store, band_filter, filters=None):
        store.clear()
        filtered_list = self._get_filtered_list(band_filter, filters)
        for item in filtered_list:
            store.append(item.get_list_model())

    def _get_unfiltered_list(self):
        return self._base_station_list

    def _get_filtered_list(self, band_filter, filters):
        filtered_list = []

        #only implemented for 900 band so far, so no distinction
        if band_filter.is_active:
            for item in self._base_station_list:
                if 0 < item.arfcn < 125:
                    filtered_list.append(item)
        else:
            for item in self._base_station_list:
                filtered_list.append(item)

        if filters is not None:
            for filter in filters:
                if filter.is_active:
                    filtered_list = filter.execute(filtered_list)

        return filtered_list


    def create_report(self, arfcn):
        for item in self._base_station_list:
            if item.arfcn == int(arfcn):
                return item.create_report()

    def evaluate(self, rules, evaluator):
        for station in self._base_station_list:
            rule_results = {}
            for rule in rules:
                if rule.is_active:
                    rule_results[rule.identifier] = rule.check(station.arfcn, self._base_station_list)
            station.rules_report = rule_results.copy()
            station.evaluation, station.evaluation_report = evaluator.evaluate(rule_results)
            station.evaluation_by = evaluator.identifier