Skip to content

Commit

Permalink
Add sqlite fix and parser error ptrkrysik#478 @Darkbat91
Browse files Browse the repository at this point in the history
  • Loading branch information
bkerler committed Aug 13, 2021
1 parent 4493100 commit 1d3348a
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions apps/grgsm_scanner
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,21 @@ class channel_info(object):
return "ARFCN: %4u, Freq: %6.1fM, CID: %5u, LAC: %5u, MCC: %3u, MNC: %3u, Pwr: %3i, NCC: %d, BCC: %d" % (
self.arfcn, self.freq / 1e6, self.cid, self.lac, self.mcc, self.mnc, self.power, self.ncc, self.bcc)

def sqlite_file(filename, debug = False):
import sqlite3 # Avoid pulling in sqlite3 when not saving
if debug:
print("Saving to SQLite database in %s" % filename)
conn = sqlite3.connect(filename)
conn.execute("CREATE TABLE IF NOT EXISTS towers(time, arfcin, freq, cell_id, lac, mcc, mnc, ccch_conf, power, neighbour_list, cell_arfcn_list);")
return conn

def do_scan(samp_rate, band, speed, ppm, gain, args, prn = None, debug = False):
signallist = []
channels_num = int(samp_rate / 0.2e6)
conn = None

if sqlite is not None:
conn = sqlite_file(sqlite, debug)
for arfcn_range in gsm.arfcn.get_arfcn_ranges(band):
first_arfcn = arfcn_range[0]
last_arfcn = arfcn_range[1]
Expand Down Expand Up @@ -366,6 +378,15 @@ def do_scan(samp_rate, band, speed, ppm, gain, args, prn = None, debug = False):
neighbour_list, cell_arfcn_list, receiver.get_ncc(), receiver.get_bcc())
found_list.append(info)

if conn:
now = time.time()
conn.execute("INSERT INTO towers(time, arfcin, freq, cell_id, lac, mcc, mnc, ccch_conf, power, neighbour_list, cell_arfcn_list) "+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
(now, grgsm.arfcn.downlink2arfcn(found_freqs[i]), found_freqs[i],
cell_ids[i], lacs[i], mccs[i], mncs[i], ccch_confs[i], powers[i],
str(neighbour_list), str(cell_arfcn_list)))
conn.commit()

scanner = None


Expand All @@ -381,6 +402,10 @@ def do_scan(samp_rate, band, speed, ppm, gain, args, prn = None, debug = False):
signallist.extend(found_list)

current_freq += channels_num * 0.2e6

if sqlite is not None:
conn.close()

return signallist

def argument_parser():
Expand All @@ -405,6 +430,9 @@ def argument_parser():
help="If set, verbose information output is printed: ccch configuration, cell ARFCN's, neighbour ARFCN's")
parser.add_option("-d", "--debug", action="store_true",
help="Print additional debug messages")
parser.add_option("-w", "--sqlite", dest="sqlite", default=None, type="string", help="Save observed tower values to specified SQLite file")
parser.add_option("-r", "--recurse", dest="recurse", action='store_true', help="Loop continously to monitor for change, most helpful with the --sqlite directive")


"""
Dont forget: sudo sysctl kernel.shmmni=32000
Expand All @@ -420,22 +448,25 @@ def main(options = None):
sys.exit(0)

if options.band not in gsm.arfcn.get_bands():
parser.error("Invalid GSM band\n")
argument_parser().error("Invalid GSM band\n")

if options.speed < 0 or options.speed > 5:
parser.error("Invalid scan speed.\n")
argument_parser().error("Invalid scan speed.\n")

if (options.samp_rate / 0.2e6) % 2 != 0:
parser.error("Invalid sample rate. Sample rate must be an even numer * 0.2e6")
argument_parser().error("Invalid sample rate. Sample rate must be an even numer * 0.2e6")

def printfunc(found_list):
for info in sorted(found_list):
print(info)
if options.verbose:
print(info.get_verbose_info())
print("")
do_scan(options.samp_rate, options.band, options.speed,
options.ppm, options.gain, options.args, prn = printfunc, debug = options.debug)
while True:
do_scan(options.samp_rate, options.band, options.speed,
options.ppm, options.gain, options.sqlite, options.args, prn = printfunc, debug = options.debug)
if not options.recurse:
break

if __name__ == '__main__':
main()

0 comments on commit 1d3348a

Please sign in to comment.