Merge branch 'development' into ptrkrysik/trx

# Conflicts:
#	swig/grgsm_swig.i
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 199b63f..511d459 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -59,7 +59,7 @@
     ESCAPE_QUOTES
 @ONLY)
 
-add_sources(constants.cc)
+list(APPEND grgsm_sources ${CMAKE_CURRENT_BINARY_DIR}/constants.cc)
 #########################################################################
 # Subdirecories
 #########################################################################
diff --git a/lib/decoding/CMakeLists.txt b/lib/decoding/CMakeLists.txt
index 0305a07..cacfc55 100644
--- a/lib/decoding/CMakeLists.txt
+++ b/lib/decoding/CMakeLists.txt
@@ -17,6 +17,11 @@
 # the Free Software Foundation, Inc., 51 Franklin Street,
 # Boston, MA 02110-1301, USA.
 
+add_subdirectory(osmocom/coding)
+add_subdirectory(openbts)
+
+add_subdirectory(osmocom/coding)
+add_subdirectory(openbts)
 
 add_sources(
     control_channels_decoder_impl.cc
@@ -24,5 +29,3 @@
     sch.c
 )
 
-add_subdirectory(osmocom/coding)
-add_subdirectory(openbts)
diff --git a/lib/decoding/openbts/CMakeLists.txt b/lib/decoding/openbts/CMakeLists.txt
index eb857bf..18977b1 100644
--- a/lib/decoding/openbts/CMakeLists.txt
+++ b/lib/decoding/openbts/CMakeLists.txt
@@ -25,4 +25,3 @@
     GSM503Tables.cpp
     ViterbiR204.cpp
 )
-
diff --git a/lib/decoding/osmocom/coding/CMakeLists.txt b/lib/decoding/osmocom/coding/CMakeLists.txt
index 615a2f6..8e68577 100644
--- a/lib/decoding/osmocom/coding/CMakeLists.txt
+++ b/lib/decoding/osmocom/coding/CMakeLists.txt
@@ -17,7 +17,7 @@
 # the Free Software Foundation, Inc., 51 Franklin Street,
 # Boston, MA 02110-1301, USA.
 
-add_sources(APPEND grgsm_sources
+add_sources(
     gsm0503_conv.c
     gsm0503_coding.c
     gsm0503_interleaving.c
diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt
index a31ea2f..c7c7cae 100644
--- a/python/CMakeLists.txt
+++ b/python/CMakeLists.txt
@@ -45,6 +45,7 @@
 
 set(GR_TEST_TARGET_DEPS gr-gsm)
 set(GR_TEST_PYTHON_DIRS ${CMAKE_BINARY_DIR}/swig:${CMAKE_BINARY_DIR}/python/misc_utils)
+GR_ADD_TEST(qa_arfcn ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_arfcn.py)
 GR_ADD_TEST(qa_decryption ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_decryption.py)
 GR_ADD_TEST(qa_burst_printer ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_burst_printer.py)
 GR_ADD_TEST(qa_message_printer ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_message_printer.py)
@@ -54,7 +55,6 @@
 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)
 #GR_ADD_TEST(qa_msg_to_tag ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_msg_to_tag.py)
 #GR_ADD_TEST(qa_controlled_fractional_resampler_cc ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_controlled_fractional_resampler_cc.py)
 #GR_ADD_TEST(qa_uplink_downlink_splitter ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_uplink_downlink_splitter.py)
diff --git a/python/demapping/CMakeLists.txt b/python/demapping/CMakeLists.txt
index fd6e112..9f0efb4 100644
--- a/python/demapping/CMakeLists.txt
+++ b/python/demapping/CMakeLists.txt
@@ -17,7 +17,8 @@
 # the Free Software Foundation, Inc., 51 Franklin Street,
 # Boston, MA 02110-1301, USA.
 
-install(FILES
+GR_PYTHON_INSTALL(
+    FILES
     gsm_bcch_ccch_demapper.py
     gsm_bcch_ccch_sdcch4_demapper.py
     gsm_sdcch8_demapper.py DESTINATION ${GR_PYTHON_DIR}/grgsm
diff --git a/python/misc_utils/CMakeLists.txt b/python/misc_utils/CMakeLists.txt
index ffba1b7..1accda9 100644
--- a/python/misc_utils/CMakeLists.txt
+++ b/python/misc_utils/CMakeLists.txt
@@ -17,8 +17,10 @@
 # the Free Software Foundation, Inc., 51 Franklin Street,
 # Boston, MA 02110-1301, USA.
 
-install(FILES
+GR_PYTHON_INSTALL(
+    FILES
     arfcn.py
     clock_offset_corrector_tagged.py
-    hier_block.py DESTINATION ${GR_PYTHON_DIR}/grgsm
+    hier_block.py 
+    fn_time.py DESTINATION ${GR_PYTHON_DIR}/grgsm
 )
diff --git a/python/misc_utils/arfcn.py b/python/misc_utils/arfcn.py
old mode 100755
new mode 100644
diff --git a/python/misc_utils/fn_time.py b/python/misc_utils/fn_time.py
new file mode 100644
index 0000000..71e93db
--- /dev/null
+++ b/python/misc_utils/fn_time.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @file
+# @author Piotr Krysik <ptrkrysik@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 math import floor, ceil
+from random import uniform
+
+__hyper_frame = 26*51*2048
+__symb_rate = 13.0e6/48.0
+__ts_period = 156.25/__symb_rate
+__frame_period = 8*__ts_period
+
+#The functions below are crucial part of synchronization between
+#transmitter and receiver.
+
+#fnmod_delta computes difference between two frame numbers modulo
+#__hyper_frame. The result is correct if difference between the frame
+#numbers is not bigger than __hyper_frame/2.
+def fnmod_delta(fn1, fn2):
+    h2 = __hyper_frame/2
+    delta = (fn1%__hyper_frame)-(fn2%__hyper_frame)
+  
+    if delta >= h2:
+        delta = delta - __hyper_frame
+    elif delta < -h2:
+        delta = delta + __hyper_frame
+    
+    return delta
+  
+#fn_time_delta computes difference between reference frame number and a second frame number. It also computes timestamp of the second frame number. The full list of parameters is following:
+#* fn_ref - reference frame number modulo __hyper_frame
+#* time_ref - precise timestamp of the first sample in the frame fn_ref
+#* fn_x - second frame number modulo __hyper_frame,
+#* time_hint - coarse time for fn_x that is used as a hint to avoid ambiguities caused by modulo operation applied to frame numbers. The correct are values from range <time2_precise-__hiperframe/2, time2_precise+__hiperframe/2> (where time2_precise is exact time of the first sample in frame fn_x)
+#* ts_num - number of timeslot in the frame
+
+def fn_time_delta(fn_ref, time_ref, fn_x, time_hint=None, ts_num=None):
+    if time_hint is None:
+        time_hint = time_ref
+        
+    if ts_num is None:
+        ts_num = 0
+
+    time_diff = time_hint-time_ref
+    frames_diff = int(round(time_diff/__frame_period))
+    fn_ref = (fn_ref + frames_diff)
+    fn_delta = fnmod_delta(fn_x,fn_ref)+frames_diff
+    time2_precise = fn_delta*__frame_period+time_ref+ts_num*__ts_period
+    
+    return fn_delta, time2_precise
+
+
+if __name__ == "__main__":
+    fn1 = 10000
+    time1 = 10
+    for fn2 in xrange(__hyper_frame/2+fn1-10,__hyper_frame/2*10+fn1+10,10):
+        time2 = time1 + (fn2-fn1)*__frame_period 
+        error = uniform(-6200,6200)
+        time2_err = time2+error
+        fn_delta, time2_precise = fn_time_delta(fn1, time1, fn2, time2_err)
+        if fn_delta != fn2-fn1:
+            print "dupa:", fn2, error#, 'fn_delta:'+str(fn_delta), time2, error, frames_diff_h4, (time2-time1)/(__hyper_frame*__frame_period), time_diff_h4_prev, time_diff
+        time_diff = time2 - time2_precise
+        if time_diff > 0.1:
+            print "dupa"
+
diff --git a/python/receiver/CMakeLists.txt b/python/receiver/CMakeLists.txt
index 07fe2a4..8ab902b 100644
--- a/python/receiver/CMakeLists.txt
+++ b/python/receiver/CMakeLists.txt
@@ -17,7 +17,8 @@
 # the Free Software Foundation, Inc., 51 Franklin Street,
 # Boston, MA 02110-1301, USA.
 
-install(FILES
+GR_PYTHON_INSTALL(
+    FILES
     gsm_input.py
     fcch_burst_tagger.py
     sch_detector.py