Merge pull request #119 from romankh/gr-gsm-apps

App for RTL-SDR single channel capture
diff --git a/apps/airprobe_rtlsdr_capture.py b/apps/airprobe_rtlsdr_capture.py
new file mode 100755
index 0000000..b744bf2
--- /dev/null
+++ b/apps/airprobe_rtlsdr_capture.py
@@ -0,0 +1,251 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @file
+# @author Roman Khassraf <rkhassraf@gmail.com>
+# @section LICENSE
+#
+# Gr-gsm is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# Gr-gsm is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with gr-gsm; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+#
+
+from gnuradio import blocks
+from gnuradio import eng_notation
+from gnuradio import gr
+from gnuradio.eng_option import eng_option
+from gnuradio.filter import firdes
+from math import pi
+from optparse import OptionParser
+
+import grgsm
+import osmosdr
+import pmt
+import signal
+import sys
+
+
+class airprobe_rtlsdr_capture(gr.top_block):
+
+    def __init__(self, fc, gain, samp_rate, ppm, arfcn, cfile=None, burst_file=None, band=None, verbose=False):
+
+        gr.top_block.__init__(self, "Airprobe RTL-SDR Capture")
+                
+        ##################################################
+        # Parameters
+        ##################################################
+        self.fc = fc
+        self.gain = gain
+        self.samp_rate = samp_rate
+        self.ppm = ppm
+        self.arfcn = arfcn
+        self.cfile = cfile
+        self.burst_file = burst_file
+        self.band = band
+        self.verbose = verbose
+        self.shiftoff = shiftoff = 400e3
+        
+        ##################################################
+        # Processing Blocks
+        ##################################################
+
+        self.rtlsdr_source = osmosdr.source( args="numchan=" + str(1) + " " + "" )
+        self.rtlsdr_source.set_sample_rate(samp_rate)
+        self.rtlsdr_source.set_center_freq(fc - shiftoff, 0)
+        self.rtlsdr_source.set_freq_corr(ppm, 0)
+        self.rtlsdr_source.set_dc_offset_mode(2, 0)
+        self.rtlsdr_source.set_iq_balance_mode(2, 0)
+        self.rtlsdr_source.set_gain_mode(True, 0)
+        self.rtlsdr_source.set_gain(gain, 0)
+        self.rtlsdr_source.set_if_gain(20, 0)
+        self.rtlsdr_source.set_bb_gain(20, 0)
+        self.rtlsdr_source.set_antenna("", 0)
+        self.rtlsdr_source.set_bandwidth(250e3+abs(shiftoff), 0)
+        self.blocks_rotator = blocks.rotator_cc(-2*pi*shiftoff/samp_rate)
+
+        if self.verbose or self.burst_file:
+            self.gsm_receiver = grgsm.receiver(4, ([self.arfcn]), ([]))
+            self.gsm_input = grgsm.gsm_input(
+                ppm=0,
+                osr=4,
+                fc=fc,
+                samp_rate_in=samp_rate,
+            )
+            self.gsm_clock_offset_control = grgsm.clock_offset_control(fc-shiftoff)
+
+        if self.burst_file:
+            self.gsm_burst_file_sink = grgsm.burst_file_sink(self.burst_file)
+
+        if self.cfile:
+            self.blocks_file_sink = blocks.file_sink(gr.sizeof_gr_complex*1, self.cfile, False)
+            self.blocks_file_sink.set_unbuffered(False)
+
+        if self.verbose:
+            self.gsm_bursts_printer_0 = grgsm.bursts_printer(pmt.intern(""),
+                                                             False, False, False, False)
+
+        ##################################################
+        # Connections
+        ##################################################
+
+        self.connect((self.rtlsdr_source, 0), (self.blocks_rotator, 0))
+
+        if self.cfile:
+            self.connect((self.blocks_rotator, 0), (self.blocks_file_sink, 0))
+
+        if self.verbose or self.burst_file:
+            self.connect((self.gsm_input, 0), (self.gsm_receiver, 0))
+            self.connect((self.blocks_rotator, 0), (self.gsm_input, 0))
+            self.msg_connect(self.gsm_clock_offset_control, "ppm", self.gsm_input, "ppm_in")
+            self.msg_connect(self.gsm_receiver, "measurements", self.gsm_clock_offset_control, "measurements")
+
+            if self.burst_file:
+                self.msg_connect(self.gsm_receiver, "C0", self.gsm_burst_file_sink, "in")
+            if self.verbose:
+                self.msg_connect(self.gsm_receiver, "C0", self.gsm_bursts_printer_0, "bursts")
+
+    def get_fc(self):
+        return self.fc
+
+    def set_fc(self, fc):
+        self.fc = fc
+        if self.verbose or self.burst_file:
+            self.gsm_input.set_fc(self.fc)
+            
+    def get_arfcn(self):
+        return self.arfcn
+
+    def set_arfcn(self, arfcn):
+        self.arfcn = arfcn
+        if self.verbose or self.burst_file:
+            self.gsm_receiver.set_cell_allocation([self.arfcn])
+            if options.band:
+                new_freq = grgsm.arfcn.arfcn2downlink(self.arfcn, self.band)
+            else:
+                for band in grgsm.arfcn.get_bands():
+                    if grgsm.arfcn.is_valid_arfcn(arfcn, band):
+                        new_freq = grgsm.arfcn.arfcn2downlink(arfcn, band)
+                        break
+            self.set_fc(new_freq)
+
+    def get_gain(self):
+        return self.gain
+
+    def set_gain(self, gain):
+        self.gain = gain
+
+    def get_samp_rate(self):
+        return self.samp_rate
+
+    def set_samp_rate(self, samp_rate):
+        self.samp_rate = samp_rate
+        self.rtlsdr_source.set_sample_rate(self.samp_rate)
+        if self.verbose or self.burst_file:
+            self.gsm_input.set_samp_rate_in(self.samp_rate)
+
+    def get_ppm(self):
+        return self.ppm
+
+    def set_ppm(self, ppm):
+        self.ppm = ppm
+        self.set_ppm_slider(self.ppm)
+
+if __name__ == '__main__':
+
+    parser = OptionParser(option_class=eng_option, usage="%prog [options]",
+                          description="RTL-SDR capturing app of gr-gsm.")
+
+    parser.add_option("-f", "--fc", dest="fc", type="eng_float", 
+                      help="Set frequency [default=%default]")
+    
+    parser.add_option("-a", "--arfcn", dest="arfcn", type="intx", 
+                      help="Set ARFCN instead of frequency. In some cases you may have to provide the GSM band also")
+    
+    parser.add_option("-g", "--gain", dest="gain", type="eng_float", 
+                      default=eng_notation.num_to_str(30),
+                      help="Set gain [default=%default]")
+    
+    parser.add_option("-s", "--samp-rate", dest="samp_rate", type="eng_float", 
+                      default=eng_notation.num_to_str(2000000.052982),
+                      help="Set samp_rate [default=%default]")
+    
+    parser.add_option("-p", "--ppm", dest="ppm", type="intx", default=0,
+                      help="Set ppm [default=%default]")
+    
+    parser.add_option("-b", "--burst-file", dest="burst_file", 
+                      help="File where the captured bursts are saved")
+    
+    parser.add_option("-c", "--cfile", dest="cfile", 
+                      help="File where the captured data are saved")
+    
+    bands_list = ", ".join(grgsm.arfcn.get_bands())
+    parser.add_option("--band", dest="band", 
+                      help="Specify the GSM band for the frequency.\nAvailable bands are: " + bands_list + ".\nIf no band is specified, it will be determined automatically, defaulting to 0." )
+    
+    parser.add_option("-v", "--verbose", action="store_true", 
+                      help="If set, the captured bursts are printed to stdout")
+    
+    (options, args) = parser.parse_args()
+    
+    if options.cfile is None and options.burst_file is None:
+        parser.error("Please provide a cfile or a burst file (or both) to save the captured data\n")
+        
+    if (options.fc is None and options.arfcn is None) or (options.fc is not None and options.arfcn is not None):
+        parser.error("You have to provide either a frequency or an ARFCN (but not both).\n")
+        
+    arfcn = 0
+    fc = 939.4e6
+    if options.arfcn:
+        if options.band:
+            if options.band not in grgsm.arfcn.get_bands():
+                parser.error("Invalid GSM band\n")
+            elif not grgsm.arfcn.is_valid_arfcn(options.arfcn, options.band):
+                parser.error("ARFCN is not valid in the specified band\n")
+            else:
+                arfcn = options.arfcn
+                fc = grgsm.arfcn.arfcn2downlink(arfcn, options.band)
+        else:
+            arfcn = options.arfcn
+            for band in grgsm.arfcn.get_bands():
+                if grgsm.arfcn.is_valid_arfcn(arfcn, band):
+                    fc = grgsm.arfcn.arfcn2downlink(arfcn, band)
+                    break
+    elif options.fc:
+        fc = options.fc
+        if options.band:
+            if options.band not in grgsm.arfcn.get_bands():
+                parser.error("Invalid GSM band\n")
+            elif not grgsm.arfcn.is_valid_downlink(options.fc, options.band):
+                parser.error("Frequency is not valid in the specified band\n")
+            else:
+                arfcn = grgsm.arfcn.downlink2arfcn(options.fc, options.band)
+        else:
+            for band in grgsm.arfcn.get_bands():
+                if grgsm.arfcn.is_valid_downlink(options.fc, band):
+                    arfcn = grgsm.arfcn.downlink2arfcn(options.fc, band)
+                    break
+     
+    tb = airprobe_rtlsdr_capture(fc=fc, gain=options.gain, samp_rate=options.samp_rate,
+                         ppm=options.ppm, arfcn=arfcn, cfile=options.cfile, 
+                         burst_file=options.burst_file, band=options.band, verbose=options.verbose)
+    
+    def signal_handler(signal, frame):
+        tb.stop()
+        tb.wait()        
+        sys.exit(0)
+        
+    signal.signal(signal.SIGINT, signal_handler)
+
+    tb.start()
+    tb.wait()
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index 95cbd91..78cc646 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -36,6 +36,7 @@
     receiver/sch_detector.py
     receiver/fcch_detector.py 
     receiver/chirpz.py
+    misc_utils/arfcn.py
     misc_utils/clock_offset_corrector.py DESTINATION ${GR_PYTHON_DIR}/grgsm
 )
 
@@ -55,3 +56,5 @@
 GR_ADD_TEST(qa_burst_sdcch_subslot_filter ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_burst_sdcch_subslot_filter.py)
 GR_ADD_TEST(qa_burst_fnr_filter ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_burst_fnr_filter.py)
 GR_ADD_TEST(qa_dummy_burst_filter ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_dummy_burst_filter.py)
+GR_ADD_TEST(qa_arfcn ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_arfcn.py)
+
diff --git a/python/__init__.py b/python/__init__.py
index 41adfe8..31c0bc5 100644
--- a/python/__init__.py
+++ b/python/__init__.py
@@ -51,6 +51,7 @@
 from fcch_detector import fcch_detector
 from clock_offset_corrector import clock_offset_corrector
 from gsm_input import gsm_input
+import arfcn
 
 
 #
diff --git a/python/misc_utils/arfcn.py b/python/misc_utils/arfcn.py
new file mode 100644
index 0000000..afe8ab6
--- /dev/null
+++ b/python/misc_utils/arfcn.py
@@ -0,0 +1,126 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @file
+# @author Roman Khassraf <rkhassraf@gmail.com>
+# @section LICENSE
+#
+# Gr-gsm is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# Gr-gsm is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with gr-gsm; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+#
+#
+
+import collections
+
+
+# first uplink freq, first arfcn, last arfcn, downlink frequence distance
+# entries are ordered by relevance
+__band_conf = collections.OrderedDict([
+    ('P-GSM', {'first_freq': 890.2e6, 'first_arfcn': 1, 'last_arfcn': 124, 'downlink_dist': 45e6}),
+    ('DCS1800', {'first_freq': 1710.2e6, 'first_arfcn': 512, 'last_arfcn': 885, 'downlink_dist': 95e6}),
+    ('PCS1900', {'first_freq': 1850.2e6, 'first_arfcn': 512, 'last_arfcn': 810, 'downlink_dist': 80e6}),
+    ('E-GSM', {'first_freq': 880.2e6, 'first_arfcn': 975, 'last_arfcn': 1023, 'downlink_dist': 45e6}),           
+    ('R-GSM', {'first_freq': 876.2e6, 'first_arfcn': 955, 'last_arfcn': 1023, 'downlink_dist': 45e6}),
+    ('GSM450',{'first_freq': 450.6e6, 'first_arfcn': 259, 'last_arfcn': 293, 'downlink_dist': 10e6}),
+    ('GSM480', {'first_freq': 479e6, 'first_arfcn': 306, 'last_arfcn': 340, 'downlink_dist': 10e6}),
+    ('GSM850', {'first_freq': 824.2e6, 'first_arfcn': 128, 'last_arfcn': 251, 'downlink_dist': 45e6})
+    ])
+
+__chan_spacing = 2e5
+
+
+def get_bands():
+    return __band_conf.keys()
+
+
+def is_valid_arfcn(arfcn, band):
+    """
+    Returns True if arfcn is valid in the given band, else False
+    """
+    if band in __band_conf:
+        conf = __band_conf.get(band)
+        first_arfcn = conf['first_arfcn']
+        last_arfcn = conf['last_arfcn']
+        if first_arfcn <= arfcn <= last_arfcn:
+            return True
+    return False
+
+
+def is_valid_uplink(freq, band):
+    """
+    Returns True if the given frequency is a valid uplink frequency in the given band
+    """
+    if band in __band_conf:
+        conf = __band_conf.get(band)
+        first_freq = arfcn2uplink(conf['first_arfcn'], band)
+        last_freq = arfcn2uplink(conf['last_arfcn'], band)
+    if first_freq is None or last_freq is None:
+        return False
+    if first_freq <= freq <= last_freq:
+        return True
+    return False
+
+
+def is_valid_downlink(freq, band):
+    """
+    Returns True if the given frequency is a valid downlink frequency in the given band
+    """
+    if band in __band_conf:
+        conf = __band_conf.get(band)
+        first_freq = arfcn2downlink(conf['first_arfcn'], band)
+        last_freq = arfcn2downlink(conf['last_arfcn'], band)
+    if first_freq is None or last_freq is None:
+        return False
+    if first_freq <= freq <= last_freq:
+        return True
+    return False 
+
+
+def arfcn2uplink(arfcn, band):
+    if band in __band_conf and is_valid_arfcn(arfcn, band):
+
+        conf = __band_conf.get(band)
+        freq_start = conf['first_freq']
+        offset = conf['first_arfcn']
+        # if (band == 'E-GSM' or band == 'R-GSM') and arfcn > 124:
+        #     offset = 1024
+        f = freq_start + (__chan_spacing * (arfcn - offset))
+        return round(f, 1)
+    return None
+
+
+def arfcn2downlink(arfcn, band):
+    if band in __band_conf and is_valid_arfcn(arfcn, band):
+        conf = __band_conf.get(band)
+        distance = conf['downlink_dist']
+        return round(arfcn2uplink(arfcn, band) + distance, 1)
+    return None
+
+
+def uplink2arfcn(freq, band):
+    if band in __band_conf and is_valid_uplink(freq, band):
+        conf = __band_conf.get(band)
+        freq_start = conf['first_freq']
+        offset = conf['first_arfcn']
+        return int(round(offset + ((freq - freq_start) / __chan_spacing), 0))
+    return None
+
+
+def downlink2arfcn(freq, band):
+    if band in __band_conf and is_valid_downlink(freq, band):
+        conf = __band_conf.get(band)
+        distance = conf['downlink_dist']
+        freq_uplink = freq - distance
+        return int(round(uplink2arfcn(freq_uplink, band), 0))
+    return None
diff --git a/python/qa_arfcn.py b/python/qa_arfcn.py
new file mode 100755
index 0000000..64775ff
--- /dev/null
+++ b/python/qa_arfcn.py
@@ -0,0 +1,268 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @file
+# @author Roman Khassraf <rkhassraf@gmail.com>
+# @section LICENSE
+# 
+# Gr-gsm is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# Gr-gsm is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with gr-gsm; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 
+# 
+
+from gnuradio import gr, gr_unittest, blocks
+import grgsm
+import os
+import sys
+
+
+class qa_arfcn (gr_unittest.TestCase):
+   
+    def test_001_is_valid_arfcn(self):
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(259, 'GSM450'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(277, 'GSM450'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(293, 'GSM450'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(258, 'GSM450'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(294, 'GSM450'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(306, 'GSM480'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(323, 'GSM480'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(340, 'GSM480'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(305, 'GSM480'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(341, 'GSM480'))
+                
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(128, 'GSM850'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(199, 'GSM850'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(251, 'GSM850'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(127, 'GSM480'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(251, 'GSM480'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(1, 'P-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(63, 'P-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(124, 'P-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(0, 'P-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(125, 'P-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(975, 'E-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(999, 'E-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(1023, 'E-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(974, 'E-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(1024, 'E-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(955, 'R-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(989, 'R-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(1023, 'R-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(954, 'R-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(1024, 'R-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(512, 'DCS1800'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(732, 'DCS1800'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(885, 'DCS1800'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(511, 'DCS1800'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(886, 'DCS1800'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(512, 'PCS1900'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(691, 'PCS1900'))
+        self.assertTrue(grgsm.arfcn.is_valid_arfcn(810, 'PCS1900'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(511, 'PCS1900'))
+        self.assertFalse(grgsm.arfcn.is_valid_arfcn(811, 'PCS1900'))
+
+
+    def test_002_is_valid_uplink(self):
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(450.6e6, 'GSM450'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(457.4e6, 'GSM450'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(450.4e6, 'GSM450'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(457.6e6, 'GSM450'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(479e6, 'GSM480'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(485.8e6, 'GSM480'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(478.8e6, 'GSM480'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(486e6, 'GSM480'))
+                
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(824.2e6, 'GSM850'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(848.8e6, 'GSM850'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(824e6, 'GSM850'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(849e6, 'GSM850'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(890.2e6, 'P-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(914.8e6, 'P-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(890e6, 'P-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(915e6, 'P-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(880.2e6, 'E-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(889.8e6, 'E-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(880e6, 'E-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(890e6, 'E-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(876.2e6, 'R-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(889.8e6, 'R-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(876e6, 'R-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(890e6, 'R-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(1710.2e6, 'DCS1800'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(1784.8e6, 'DCS1800'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(1710e6, 'DCS1800'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(1785e6, 'DCS1800'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(1850.2e6, 'PCS1900'))
+        self.assertTrue(grgsm.arfcn.is_valid_uplink(1909.8e6, 'PCS1900'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(1850e6, 'PCS1900'))
+        self.assertFalse(grgsm.arfcn.is_valid_uplink(1910e6, 'PCS1900'))
+
+
+    def test_003_is_valid_downlink(self):
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(460.6e6, 'GSM450'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(467.4e6, 'GSM450'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(460.4e6, 'GSM450'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(467.6e6, 'GSM450'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(489e6, 'GSM480'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(495.8e6, 'GSM480'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(488.8e6, 'GSM480'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(496e6, 'GSM480'))
+                
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(869.2e6, 'GSM850'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(893.8e6, 'GSM850'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(869e6, 'GSM850'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(894e6, 'GSM850'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(935.2e6, 'P-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(959.8e6, 'P-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(935e6, 'P-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(960e6, 'P-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(925.2e6, 'E-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(934.8e6, 'E-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(925e6, 'E-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(935e6, 'E-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(921.2e6, 'R-GSM'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(934.8e6, 'R-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(921e6, 'R-GSM'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(935e6, 'R-GSM'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(1805.2e6, 'DCS1800'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(1879.8e6, 'DCS1800'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(1805e6, 'DCS1800'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(1880e6, 'DCS1800'))
+        
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(1930.2e6, 'PCS1900'))
+        self.assertTrue(grgsm.arfcn.is_valid_downlink(1989.8e6, 'PCS1900'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(1930e6, 'PCS1900'))
+        self.assertFalse(grgsm.arfcn.is_valid_downlink(1990e6, 'PCS1900'))
+        
+    def test_004_arfcn2uplink(self):
+        self.assertEqual(450.6e6, grgsm.arfcn.arfcn2uplink(259, 'GSM450'))
+        self.assertEqual(457.4e6, grgsm.arfcn.arfcn2uplink(293, 'GSM450'))
+        
+        self.assertEqual(479e6, grgsm.arfcn.arfcn2uplink(306, 'GSM480'))
+        self.assertEqual(485.8e6, grgsm.arfcn.arfcn2uplink(340, 'GSM480'))
+        
+        self.assertEqual(824.2e6, grgsm.arfcn.arfcn2uplink(128, 'GSM850'))
+        self.assertEqual(848.8e6, grgsm.arfcn.arfcn2uplink(251, 'GSM850'))
+        
+        self.assertEqual(890.2e6, grgsm.arfcn.arfcn2uplink(1, 'P-GSM'))
+        self.assertEqual(914.8e6, grgsm.arfcn.arfcn2uplink(124, 'P-GSM'))
+        
+        self.assertEqual(880.2e6, grgsm.arfcn.arfcn2uplink(975, 'E-GSM'))
+        self.assertEqual(889.8e6, grgsm.arfcn.arfcn2uplink(1023, 'E-GSM'))
+        
+        self.assertEqual(876.2e6, grgsm.arfcn.arfcn2uplink(955, 'R-GSM'))
+        self.assertEqual(889.8e6, grgsm.arfcn.arfcn2uplink(1023, 'R-GSM'))
+        
+        self.assertEqual(1710.2e6, grgsm.arfcn.arfcn2uplink(512, 'DCS1800'))
+        self.assertEqual(1784.8e6, grgsm.arfcn.arfcn2uplink(885, 'DCS1800'))
+        
+        self.assertEqual(1850.2e6, grgsm.arfcn.arfcn2uplink(512, 'PCS1900'))
+        self.assertEqual(1909.8e6, grgsm.arfcn.arfcn2uplink(810, 'PCS1900'))
+        
+    def test_005_arfcn2downlink(self):
+        self.assertEqual(460.6e6, grgsm.arfcn.arfcn2downlink(259, 'GSM450'))
+        self.assertEqual(467.4e6, grgsm.arfcn.arfcn2downlink(293, 'GSM450'))
+        
+        self.assertEqual(489e6, grgsm.arfcn.arfcn2downlink(306, 'GSM480'))
+        self.assertEqual(495.8e6, grgsm.arfcn.arfcn2downlink(340, 'GSM480'))
+        
+        self.assertEqual(869.2e6, grgsm.arfcn.arfcn2downlink(128, 'GSM850'))
+        self.assertEqual(893.8e6, grgsm.arfcn.arfcn2downlink(251, 'GSM850'))
+        
+        self.assertEqual(935.2e6, grgsm.arfcn.arfcn2downlink(1, 'P-GSM'))
+        self.assertEqual(959.8e6, grgsm.arfcn.arfcn2downlink(124, 'P-GSM'))
+        
+        self.assertEqual(925.2e6, grgsm.arfcn.arfcn2downlink(975, 'E-GSM'))
+        self.assertEqual(934.8e6, grgsm.arfcn.arfcn2downlink(1023, 'E-GSM'))
+        
+        self.assertEqual(921.2e6, grgsm.arfcn.arfcn2downlink(955, 'R-GSM'))
+        self.assertEqual(934.8e6, grgsm.arfcn.arfcn2downlink(1023, 'R-GSM'))
+        
+        self.assertEqual(1805.2e6, grgsm.arfcn.arfcn2downlink(512, 'DCS1800'))
+        self.assertEqual(1879.8e6, grgsm.arfcn.arfcn2downlink(885, 'DCS1800'))
+        
+        self.assertEqual(1930.2e6, grgsm.arfcn.arfcn2downlink(512, 'PCS1900'))
+        self.assertEqual(1989.8e6, grgsm.arfcn.arfcn2downlink(810, 'PCS1900'))
+        
+    def test_006_uplink2arfcn(self):
+        self.assertEqual(259, grgsm.arfcn.uplink2arfcn(450.6e6, 'GSM450'))
+        self.assertEqual(293, grgsm.arfcn.uplink2arfcn(457.4e6, 'GSM450'))
+        
+        self.assertEqual(306, grgsm.arfcn.uplink2arfcn(479e6, 'GSM480'))
+        self.assertEqual(340, grgsm.arfcn.uplink2arfcn(485.8e6, 'GSM480'))
+        
+        self.assertEqual(128, grgsm.arfcn.uplink2arfcn(824.2e6, 'GSM850'))
+        self.assertEqual(251, grgsm.arfcn.uplink2arfcn(848.8e6, 'GSM850'))
+        
+        self.assertEqual(1, grgsm.arfcn.uplink2arfcn(890.2e6, 'P-GSM'))
+        self.assertEqual(124, grgsm.arfcn.uplink2arfcn(914.8e6, 'P-GSM'))
+        
+        self.assertEqual(975, grgsm.arfcn.uplink2arfcn(880.2e6, 'E-GSM'))
+        self.assertEqual(1023, grgsm.arfcn.uplink2arfcn(889.8e6, 'E-GSM'))
+        
+        self.assertEqual(955, grgsm.arfcn.uplink2arfcn(876.2e6, 'R-GSM'))
+        self.assertEqual(1023, grgsm.arfcn.uplink2arfcn(889.8e6, 'R-GSM'))
+        
+        self.assertEqual(512, grgsm.arfcn.uplink2arfcn(1710.2e6, 'DCS1800'))
+        self.assertEqual(885, grgsm.arfcn.uplink2arfcn(1784.8e6, 'DCS1800'))
+        
+        self.assertEqual(512, grgsm.arfcn.uplink2arfcn(1850.2e6, 'PCS1900'))
+        self.assertEqual(810, grgsm.arfcn.uplink2arfcn(1909.8e6, 'PCS1900'))
+        
+    def test_007_downlink2arfcn(self):
+        self.assertEqual(259, grgsm.arfcn.downlink2arfcn(460.6e6, 'GSM450'))
+        self.assertEqual(293, grgsm.arfcn.downlink2arfcn(467.4e6, 'GSM450'))
+        
+        self.assertEqual(306, grgsm.arfcn.downlink2arfcn(489e6, 'GSM480'))
+        self.assertEqual(340, grgsm.arfcn.downlink2arfcn(495.8e6, 'GSM480'))
+        
+        self.assertEqual(128, grgsm.arfcn.downlink2arfcn(869.2e6, 'GSM850'))
+        self.assertEqual(251, grgsm.arfcn.downlink2arfcn(893.8e6, 'GSM850'))
+        
+        self.assertEqual(1, grgsm.arfcn.downlink2arfcn(935.2e6, 'P-GSM'))
+        self.assertEqual(124, grgsm.arfcn.downlink2arfcn(959.8e6, 'P-GSM'))
+        
+        self.assertEqual(975, grgsm.arfcn.downlink2arfcn(925.2e6, 'E-GSM'))
+        self.assertEqual(1023, grgsm.arfcn.downlink2arfcn(934.8e6, 'E-GSM'))
+        
+        self.assertEqual(955, grgsm.arfcn.downlink2arfcn(921.2e6, 'R-GSM'))
+        self.assertEqual(1023, grgsm.arfcn.downlink2arfcn(934.8e6, 'R-GSM'))
+        
+        self.assertEqual(512, grgsm.arfcn.downlink2arfcn(1805.2e6, 'DCS1800'))
+        self.assertEqual(885, grgsm.arfcn.downlink2arfcn(1879.8e6, 'DCS1800'))
+        
+        self.assertEqual(512, grgsm.arfcn.downlink2arfcn(1930.2e6, 'PCS1900'))
+        self.assertEqual(810, grgsm.arfcn.downlink2arfcn(1989.8e6, 'PCS1900'))
+        
+        
+if __name__ == '__main__':
+    gr_unittest.run(qa_arfcn, "qa_arfcn.xml")
+