summaryrefslogtreecommitdiff
path: root/gsm-tvoid/src/python/gsm_scan.py
blob: fab6ce82786f344e76c0cd8fa27eff7c26f49a59 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
#!/usr/bin/env python
# TODO:
#	* Add status info to window (frequency, offset, etc)
#	* Put direct frequency tuning back
#	* Add rate-limited file reads (throttle?)
#	* Make console only version
#	* Reset burst_stats on retune
#	* Add better option checking
#	* Wideband (multi-channel) processing (usrp and/or file input)
#	* Automatic beacon scan (quick scan RSSI, then check for BCCH)
#	* AGC

import sys

#nasty hack for testing
for extdir in ['../lib','../lib/.libs']:
	if extdir not in sys.path:
		sys.path.append(extdir)

from gnuradio import gr, gru, blks
from gnuradio import usrp
from gnuradio import eng_notation
from gnuradio.eng_option import eng_option
from gnuradio.wxgui import stdgui, fftsink, waterfallsink, scopesink, form, slider
from optparse import OptionParser
from math import pi
import wx
import gsm

####################
class burst_callback(gr.feval_ll):
	def __init__(self, fg):
		gr.feval_ll.__init__(self)
		self.fg = fg
		self.offset_mean_num = 10		#number of FCCH offsets to average
		self.offset_vals = []
		
####################
	def eval(self, x):
		#print "burst_callback: eval(",x,")\n";
		try:
			if gsm.BURST_CB_SYNC_OFFSET == x:
				#print "burst_callback: SYNC_OFFSET\n";
				if self.fg.options.tuning.count("o"):
					last_offset = self.fg.burst.last_freq_offset()
					self.fg.offset -= last_offset
					#print "burst_callback: SYNC_OFFSET:", last_offset, " ARFCN: ", self.fg.channel, "\n";
					self.fg.set_channel(self.fg.channel)

			elif gsm.BURST_CB_ADJ_OFFSET == x:
				last_offset = self.fg.burst.last_freq_offset()

				self.offset_vals.append(last_offset)
				count =  len(self.offset_vals)
				#print "burst_callback: ADJ_OFFSET:", last_offset, ", count=",count,"\n";

				if count >= self.offset_mean_num:
					sum = 0.0
					while len(self.offset_vals):
						sum += self.offset_vals.pop(0)

					self.fg.mean_offset = sum / self.offset_mean_num

					#print "burst_callback: mean offset:", self.fg.mean_offset, "\n";

					#retune if greater than 100 Hz
					if abs(self.fg.mean_offset) > 100.0:	
						#print "burst_callback: mean offset adjust:", self.fg.mean_offset, "\n";
						if self.fg.options.tuning.count("o"):	
							#print "burst_callback: tuning.\n";
							self.fg.offset -= self.fg.mean_offset
							self.fg.set_channel(self.fg.channel)

			elif gsm.BURST_CB_TUNE == x:
				#print "burst_callback: BURST_CB_TUNE: ARFCN: ", self.fg.burst.next_arfcn, "\n";
				if self.fg.options.tuning.count("h"):
					#print "burst_callback: tuning.\n";
					self.fg.set_channel(self.fg.burst.next_arfcn)

			return 0

		except Exception, e:
			print >> sys.stderr, "burst_callback: Exception: ", e


####################
def pick_subdevice(u):
	if u.db[0][0].dbid() >= 0:
		return (0, 0)
	if u.db[1][0].dbid() >= 0:
		return (1, 0)
	return (0, 0)

####################
def get_freq_from_arfcn(chan,region):

	#P/E/R-GSM 900
	if chan >= 0 and chan <= 124:
		freq = 890 + 0.2*chan + 45

	#GSM 850
	elif chan >= 128 and chan <= 251:
		freq = 824.2 + 0.2*(chan - 128) + 45
		
	#GSM 450
	elif chan >= 259 and chan <= 293:
		freq = 450.6 + 0.2*(chan - 259) + 10
		
	#GSM 480
	elif chan >= 306 and chan <= 340:
		freq = 479 + 0.2*(chan - 306) + 10
		
	#DCS 1800
	elif region is "e" and chan >= 512 and chan <= 885:
		freq = 1710.2 + 0.2*(chan - 512) + 95
		
	#DCS 1900
	elif region is "u" and chan >= 512 and chan <= 810:
		freq = 1850.2 + 0.2*(chan - 512) + 80

	#E/R-GSM 900
	elif chan >= 955 and chan <= 1023:
		freq = 890 + 0.2*(chan - 1024) + 45

	else:
		freq = 0

	return freq * 1e6


####################
class app_flow_graph(stdgui.gui_flow_graph):

	def __init__(self, frame, panel, vbox, argv):
		stdgui.gui_flow_graph.__init__(self)

		self.frame = frame
		self.panel = panel
		self.parse_options()
		self.setup_flowgraph()	
		self.setup_print_options()
		self._build_gui(vbox)

		#some non-gui wxwindows handlers
		self.t1 = wx.Timer(self.frame)
		self.t1.Start(5000,0)
		self.frame.Bind(wx.EVT_TIMER, self.on_tick)

		#bind the idle routing for message_queue processing	
		self.frame.Bind(wx.EVT_IDLE, self.on_idle)
		
		#tune
		self.set_channel(self.channel)
	
		#giddyup
		self.status_msg = "Started."


####################
	def parse_options(self):
		parser = OptionParser(option_class=eng_option)

		#view options
		parser.add_option("-S", "--scopes", type="string", default="I",
							help="Select scopes to display. (N)one, (I)nput,(F)ilter,(d)emod,(c)locked,(b)urst [default=%default]")
		parser.add_option("-p", "--print-console", type="string", default="s",
							help="What to print on console. [default=%default]\n" +
							"(n)othing, (e)verything, (s)tatus, (a)ll Types, (k)nown, (u)nknown, \n" +
							"TS(0), (F)CCH, (S)CH, (N)ormal, (D)ummy\n" +
							"Usefull (b)its, All TS (B)its, (C)orrelation bits, he(x) raw burst data, \n" +
							"(d)ecoded hex for gsmdecode")
				

		#decoder options
		parser.add_option("-D", "--decoder", type="string", default="c",
							help="Select decoder block to use. (c)omplex,(f)loat [default=%default]")
		parser.add_option("-E", "--equalizer", type="string", default="none",
							help="Type of equalizer to use.  none, fixed-dfe, fixed-linear [default=%default]")
		parser.add_option("-t", "--timing", type="string", default="cn",
							help="Type of timing techniques to use. [default=%default] \n" +
							"(n)one, (c)orrelation track, (q)uarter bit, (f)ull04 ")

		#tuning options
		parser.add_option("-T", "--tuning", type="string", default="oh",
							help="Type of tuning to perform. [default=%default] \n" +
							"(n)one, (o)ffset adjustment, (h)opping ")
		parser.add_option("-o", "--offset", type="eng_float", default=0.0,
							help="Tuning offset frequency")
		
		#file options
		parser.add_option("-I", "--inputfile", type="string", default=None,
							help="Select a capture file to read")
		parser.add_option("-O", "--outputfile", type="string", default=None,
							help="Filename to save burst output")
		parser.add_option("-l", "--fileloop", action="store_true", dest="fileloop", default=False,
							help="Continuously loop data from input file")
		
		#usrp options
		parser.add_option("-d", "--decim", type="int", default=112,
							help="Set USRP decimation rate to DECIM [default=%default]")
		parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
							help="Select USRP Rx side A or B (default=first one with a daughterboard)")
		parser.add_option("--fusb-block-size", type="int", default=0,
							help="Set USRP blocksize")
		parser.add_option("--fusb-nblocks", type="int", default=0,
							help="Set USRP block buffers")
		parser.add_option("--realtime",action="store_true", dest="realtime",
							help="Use realtime scheduling.")
		parser.add_option("-C", "--clock-offset", type="eng_float", default=0.0,
							help="Sample clock offset frequency")

		#FIXME: gain not working?
		parser.add_option("-g", "--gain", type="eng_float", default=None,
							help="Set gain in dB (default is midpoint)")
		parser.add_option("-c", "--channel", type="int", default=1,
							help="Tune to GSM ARFCN.")
		parser.add_option("-r", "--region", type="string", default="u",
							help="Frequency bands to use for channels.  (u)s or (e)urope [default=%default]")

		#testing options
		parser.add_option("--test-hop-speed",action="store_true", dest="test_hop_speed",
							help="Test hopping speed.")
		parser.add_option("--hopgood", type="int", default=658,
							help="Good ARFCN [default=%default]")
		parser.add_option("--hopbad", type="int", default=655,
							help="Emtpy ARFCN [default=%default]")

		(options, args) = parser.parse_args()
		if (len(args) != 0):
			parser.print_help()
			sys.exit(1)

#	   if (options.inputfile and ( options.freq or options.rx_subdev_spec or options.gain)):
#		   print "datafile option cannot be used with USRP options."
#		   sys.exit(1)

		if options.test_hop_speed:
			options.tuning = 'h'	#hopping only, no offset
			options.channel = options.hopgood

		self.options = options
		self.scopes = options.scopes
		self.region = options.region
		self.channel = options.channel
		self.offset = options.offset

####################
	def	setup_print_options(self):
		options = self.options

		self.print_status = options.print_console.count('s')

		if options.print_console.count('e'):
			self.print_status = 1

		#console print options
		popts = 0

		if options.print_console.count('d'):
			popts |= gsm.PRINT_GSM_DECODE

		if options.print_console.count('s'):
			popts |= gsm.PRINT_STATE

		if options.print_console.count('e'):
			popts |= gsm.PRINT_EVERYTHING

		if options.print_console.count('a'):
			popts |= gsm.PRINT_ALL_TYPES

		if options.print_console.count('k'):
			popts |= gsm.PRINT_KNOWN

		if options.print_console.count('u'):
			popts |= gsm.PRINT_UNKNOWN

		if options.print_console.count('0'):
			popts |= gsm.PRINT_TS0

		if options.print_console.count('F'):
			popts |= gsm.PRINT_FCCH

		if options.print_console.count('S'):
			popts |= gsm.PRINT_SCH

		if options.print_console.count('N'):
			popts |= gsm.PRINT_NORMAL

		if options.print_console.count('D'):
			popts |= gsm.PRINT_DUMMY

		if options.print_console.count('x'):
			popts |= gsm.PRINT_BITS | gsm.PRINT_HEX

		if options.print_console.count('B'):
			popts |= gsm.PRINT_BITS | gsm.PRINT_ALL_BITS

		elif options.print_console.count('b'):
			popts |= gsm.PRINT_BITS

		elif options.print_console.count('C'):
			popts |= gsm.PRINT_BITS | gsm.PRINT_CORR_BITS

		#print  "Print flags: 0x%8.8x\n" %(popts)
		
		self.burst.d_print_options = popts	


####################
	def setup_usrp(self):
		options = self.options
		
		#set resonable defaults if no user prefs set
		if options.realtime:
			if options.fusb_block_size == 0:
				options.fusb_block_size = gr.prefs().get_long('fusb', 'rt_block_size', 1024)
			if options.fusb_nblocks == 0:
				options.fusb_nblocks    = gr.prefs().get_long('fusb', 'rt_nblocks', 16)
		else:
			if options.fusb_block_size == 0:
				options.fusb_block_size = gr.prefs().get_long('fusb', 'block_size', 4096)
			if options.fusb_nblocks == 0:
				options.fusb_nblocks    = gr.prefs().get_long('fusb', 'nblocks', 16)
		
		print >> sys.stderr, "fusb_block_size =", options.fusb_block_size
		print >> sys.stderr, "fusb_nblocks    =", options.fusb_nblocks

			
		self.ursp = usrp.source_c(decim_rate=options.decim,fusb_block_size=options.fusb_block_size,fusb_nblocks=options.fusb_nblocks)
			
		if options.rx_subdev_spec is None:
			options.rx_subdev_spec = pick_subdevice(self.ursp)
		
		self.ursp.set_mux(usrp.determine_rx_mux_value(self.ursp, options.rx_subdev_spec))

		# determine the daughterboard subdevice
		self.subdev = usrp.selected_subdev(self.ursp, options.rx_subdev_spec)
		input_rate = self.ursp.adc_freq() / self.ursp.decim_rate()

		# set initial values
		if options.gain is None:
			# if no gain was specified, use the mid-point in dB
			g = self.subdev.gain_range()
			options.gain = float(g[0]+g[1])/2

		self.set_gain(options.gain)
	
		self.source = self.ursp

####################
	def setup_timing(self):
		options = self.options
		clock_rate = 64e6

		if options.clock_offset:
			clock_rate = 64e6 + options.clock_offset
		elif options.channel:		
			#calculate actual clock rate based on frequency offset (assumes shared clock for sampling and tuning) 
			f = get_freq_from_arfcn(options.channel,options.region)
			if f:
				percent_offset = options.offset / get_freq_from_arfcn(options.channel,options.region)
			else:
				percent_offset = 0.0
				
			clock_rate += clock_rate * percent_offset
			print >> sys.stderr, "% offset = ", percent_offset, "clock = ", clock_rate
			
		self.clock_rate = clock_rate
		self.input_rate = clock_rate / options.decim		#TODO: what about usrp value?
		self.gsm_symb_rate = 1625000.0 / 6.0
		self.sps = self.input_rate / self.gsm_symb_rate
	
####################
	def setup_filter(self):
		#test w/o filter (for buffer latency)
		#self.filter = self.source
		#return
		
		options = self.options

		# configure channel filter
		filter_cutoff	= 145e3		#135,417Hz is GSM bandwidth 
		filter_t_width	= 10e3

		#Only DSP adjust for offset on datafile, adjust tuner for USRP
		#TODO: see if we can change this offset at runtime based on freq detection
		if options.inputfile:
			offset = self.offset
		else:
			offset = 0.0

		filter_taps = gr.firdes.low_pass(1.0, self.input_rate, filter_cutoff, filter_t_width, gr.firdes.WIN_HAMMING)
		self.filter = gr.freq_xlating_fir_filter_ccf(1, filter_taps, offset, self.input_rate)

		self.connect(self.source, self.filter)

####################
	def setup_f_flowgraph(self):
		# configure demodulator
		# adjust the phase gain for sampling rate
		self.demod = gr.quadrature_demod_cf(self.sps);
		
		#configure clock recovery
		gain_mu = 0.01
		gain_omega = .25 * gain_mu * gain_mu		# critically damped
		self.clocker = gr.clock_recovery_mm_ff(	self.sps, 
												gain_omega,
												0.5,			#mu
												gain_mu,
												0.5)			#omega_relative_limit, 

		self.burst = gsm.burst_ff(self.burst_cb)
		self.connect(self.filter, self.demod, self.clocker, self.burst)
	
####################
	def setup_c_flowgraph(self):
			#use the sink version if burst scope not selected
# 			if self.scopes.count("b"):
# 				self.burst = gsm.burst_cf(self.burst_cb,self.input_rate)
# 			else:
# 				self.burst = gsm.burst_sink_c(self.burst_cb,self.input_rate)

			self.burst = gsm.burst_cf(self.burst_cb,self.input_rate)
			
			self.connect(self.filter, self.burst)	

####################
	def setup_scopes(self):			
		#Input FFT
		if self.scopes.count("I"):
			self.input_fft_scope = fftsink.fft_sink_c (self, self.panel, fft_size=1024, sample_rate=self.input_rate)
			self.connect(self.source, self.input_fft_scope)

		#Filter FFT
		if self.scopes.count("F"):
			self.filter_fft_scope = fftsink.fft_sink_c (self, self.panel, fft_size=1024, sample_rate=self.input_rate)
			self.connect(self.filter, self.filter_fft_scope)

		#Burst Scope
		if self.scopes.count("b"):
			self.burst_scope = scopesink.scope_sink_f(self, self.panel, sample_rate=self.gsm_symb_rate,v_scale=1)
			self.connect(self.v2s, self.burst_scope)
							
		#burst_f options
		if self.options.decoder.count("f"):
			if self.scopes.count("d"):
				self.demod_scope = scopesink.scope_sink_f(self, self.panel, sample_rate=self.input_rate)
				self.connect(self.demod, self.demod_scope)

			if self.scopes.count("c"):
				#f_flowgraph
				self.clocked_scope = scopesink.scope_sink_f(self, self.panel, sample_rate=self.gsm_symb_rate,v_scale=1)
				self.connect(self.clocker, self.clocked_scope)
				#for testing: f_flowgraph2
				#self.clocked_scope = scopesink.scope_sink_c(self, self.panel, sample_rate=self.gsm_symb_rate,v_scale=1)
				#self.connect(self.clocker, self.clocked_scope)
				#self.connect((self.clocker,1),(self.clocked_scope,1))

####################
	def configure_burst_decoder(self):
		options = self.options
		
		# equalizer
		eq_types = {'none': gsm.EQ_NONE, 'fixed-dfe': gsm.EQ_FIXED_DFE, 'fixed-linear': gsm.EQ_FIXED_LINEAR }
		self.burst.d_equalizer_type = eq_types[options.equalizer]

		# timing
		topts = 0

		if options.timing.count('c'):
			topts |= gsm.CLK_CORR_TRACK
		
		if options.timing.count('q'):
			topts |= gsm.QB_QUARTER
		
		elif options.timing.count('f'):
			topts |= gsm.QB_FULL04
		
		self.burst.d_clock_options = topts

		#test modes
		testopts = 0
		
		if options.test_hop_speed:
			testopts |= gsm.OPT_TEST_HOP_SPEED
			self.burst.d_hop_good_arfcn = options.hopgood
			self.burst.d_hop_bad_arfcn = options.hopbad
			
			print "!!!!! Enabling Hop Speed Testing (good=%d, bad=%d) !!!!!" % (options.hopgood,options.hopbad)

		self.burst.d_test_options = testopts
		#print "Test Options: 0x%8.8x" % (self.burst.d_test_options)
		
		

####################
	def setup_flowgraph(self):

		options = self.options
		
		# Attempt to enable realtime scheduling
		if options.realtime:
			r = gr.enable_realtime_scheduling()
			if r == gr.RT_OK:
				options.realtime = True
				print >> sys.stderr, "Realtime scheduling ENABLED"
			else:
				options.realtime = False
				print >> sys.stderr, "Realtime scheduling FAILED"

		self.setup_timing()
		
		# Setup our input source
		if options.inputfile:
			self.using_usrp = False
			print >> sys.stderr, "Reading data from: " + options.inputfile
			self.source = gr.file_source(gr.sizeof_gr_complex, options.inputfile, options.fileloop)
		else:
			self.using_usrp = True
			self.setup_usrp()

		self.setup_filter()

		#create a tuner callback
		self.mean_offset = 0.0		#this is set by tuner callback
		self.burst_cb = burst_callback(self)

		# Setup flow based on decoder selection
		if options.decoder.count("c"):
			self.setup_c_flowgraph()
		elif options.decoder.count("f"):
			self.setup_f_flowgraph()

		self.configure_burst_decoder()
		
		#Hookup a vector-stream converter if we want burst output
		if self.scopes.count("b") or options.outputfile:
			self.v2s = gr.vector_to_stream(gr.sizeof_float,142)		#burst output is 142 (USEFUL_BITS)
			self.connect(self.burst, self.v2s)
#		else:
#			self.burst_sink = gr.null_sink(gr.sizeof_float)
#			self.connect(self.v2s, self.burst_sink)


		#Output file
		if options.outputfile:
			self.filesink = gr.file_sink(gr.sizeof_float, options.outputfile)
			self.connect(self.v2s, self.filesink)

		self.setup_scopes()			

####################		
	def set_status_msg(self, msg):
		self.frame.GetStatusBar().SetStatusText(msg, 0)

####################
	def _build_gui(self, vbox):

		if self.scopes.count("I"):
			vbox.Add(self.input_fft_scope.win, 5, wx.EXPAND)

		if self.scopes.count("F"):
			vbox.Add(self.filter_fft_scope.win, 5, wx.EXPAND)

		if self.scopes.count("d"):
			vbox.Add(self.demod_scope.win, 5, wx.EXPAND)

		if self.scopes.count("c"):
			vbox.Add(self.clocked_scope.win, 5, wx.EXPAND)
 
		if self.scopes.count("b"):
			vbox.Add(self.burst_scope.win, 5, wx.EXPAND)
		
		# add control area at the bottom
		if self.using_usrp:

			def _form_set_freq(kv):
				return self.set_freq(kv['freq'])

			self.usrpform = usrpform = form.form()
			hbox = wx.BoxSizer(wx.HORIZONTAL)

			hbox.Add((5,0), 0, 0)
			g = self.subdev.gain_range()
			usrpform['gain'] = form.slider_field(parent=self.panel, sizer=hbox, label="Gain",
											   weight=3,
											   min=int(g[0]), max=int(g[1]),
											   callback=self.set_gain)

			hbox.Add((5,0), 0, 0)
			usrpform['chan'] = form.slider_field(	parent=self.panel, sizer=hbox, label="Channel",
												weight=3,
												min=0, max=1023,
												value=self.channel,
												callback=self.set_channel)

			vbox.Add(hbox, 0, wx.EXPAND)
		

####################
	def set_freq(self, freq):
		#TODO: for wideband processing, determine if the desired freq is within our current sample range.
		#		If so, use the frequency translator to tune.  Tune the USRP otherwise.
		#		Maybe have a flag to force tuning the USRP?
		
		if not self.using_usrp:
			#if reading from file just adjust for offset in the freq translator
			if self.print_status:
				print >> sys.stderr, "Setting filter center freq to offset: ", self.offset, "\n"
			
			self.filter.set_center_freq(self.offset) 
			return True
	
		freq = freq - self.offset

		r = self.ursp.tune(0, self.subdev, freq)

		if r:
			self.status_msg = '%f' % (freq/1e6)
			return True
		else:
			self.status_msg = "Failed to set frequency (%f)" % (freq/1e6)
			return False

####################
	def set_gain(self, gain):

		if not self.using_usrp:
			return False

		self.subdev.set_gain(gain)

####################
	def set_channel(self, chan):

		self.chan = chan
		
		freq = get_freq_from_arfcn(chan,self.region)

		if freq:
			self.set_freq(freq)
		else:
			self.status_msg = "Invalid Channel"

####################
	def print_stats(self):
		out = sys.stderr
		
		n_total = self.burst.d_total_count
		n_unknown = self.burst.d_unknown_count
		n_known = n_total - n_unknown
		
		print >> out, "======== STATS ========="
		print >> out, 'freq_offset:    ',self.offset
		print >> out, 'mean_offset:    ',self.mean_offset
		print >> out, 'sync_loss_count:',self.burst.d_sync_loss_count
		print >> out, 'total_bursts:   ',n_total
		print >> out, 'fcch_count:     ',self.burst.d_fcch_count
		print >> out, 'part_sch_count: ',self.burst.d_part_sch_count
		print >> out, 'sch_count:      ',self.burst.d_sch_count
		print >> out, 'normal_count:   ',self.burst.d_normal_count
		print >> out, 'dummy_count:    ',self.burst.d_dummy_count
		print >> out, 'unknown_count:  ',self.burst.d_unknown_count
		print >> out, 'known_count:    ',n_known
		if n_total:
			print >> out, '%known:         ', 100.0 * n_known / n_total

		#timing
		if self.options.decoder.count("c"):
			omega = self.burst.get_omega()
		else:
			omega = self.clocker.omega()

		percent_sps = omega / self.sps 
		print >> out, 'omega:          %f (%f / %f)' % (omega,self.sps,percent_sps)
		
		print >> out, ""		
				
####################
	def on_tick(self, evt):
			
		if self.print_status:
			self.print_stats()

####################
	def on_idle(self, event):
		#We can't update this while in the tune functions since they can be invoked by callbaks and the GUI croaks...
		#FIXME: This is icky.
		self.set_status_msg(self.status_msg)
		#print "Idle.\n";

####################
def main():
	app = stdgui.stdapp(app_flow_graph, "GSM Scanner", nstatus=1)
	app.MainLoop()

####################
if __name__ == '__main__':
	main()
personal git repositories of Harald Welte. Your mileage may vary