Moved control channels decoder to libosmocore implementation
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5832d42..11d589d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -107,6 +107,7 @@
 find_package(CppUnit)
 find_package(Doxygen)
 find_package(Libosmocore)
+find_package(Libosmocoding)
 
 if(NOT GNURADIO_RUNTIME_FOUND)
     message(FATAL_ERROR "GnuRadio Runtime required to compile gr-gsm")
@@ -120,6 +121,9 @@
 if(NOT LIBOSMOCORE_FOUND)
     message(FATAL_ERROR "Libosmocore required to compile gr-gsm")
 endif()
+if(NOT LIBOSMOCODING_FOUND)
+    message(FATAL_ERROR "Libosmocoding required to compile gr-gsm")
+endif()
 
 ########################################################################
 # Setup doxygen option
diff --git a/cmake/Modules/FindLibosmocoding.cmake b/cmake/Modules/FindLibosmocoding.cmake
new file mode 100644
index 0000000..c3e4cbb
--- /dev/null
+++ b/cmake/Modules/FindLibosmocoding.cmake
@@ -0,0 +1,32 @@
+find_package(PkgConfig)
+pkg_check_modules(PC_libosmocoding libosmocoding)
+set(LIBOSMOCODING_DEFINITIONS ${PC_LIBOSMOCODING_CFLAGS_OTHER})
+
+find_path(
+        LIBOSMOCODING_INCLUDE_DIR
+        NAMES   osmocom/coding/gsm0503_coding.h
+        HINTS   ${PC_libosmocoding_INCLUDEDIR}
+                ${PC_libosmocoding_INCLUDE_DIRS}
+                ${CMAKE_INSTALL_PREFIX}/include
+        PATHS   /usr/local/include
+                /usr/include
+)
+
+find_library(
+        LIBOSMOCODING_LIBRARY
+        NAMES   libosmocoding osmocoding
+        HINTS   ${PC_libosmocoding_LIBDIR}
+                ${PC_libosmocoding_LIBRARY_DIRS}
+                ${CMAKE_INSTALL_PREFIX}/lib/
+                ${CMAKE_INSTALL_PREFIX}/lib64/
+        PATHS   /usr/local/lib
+                /usr/lib
+)
+
+
+set(LIBOSMOCODING_LIBRARIES ${LIBOSMOCODING_LIBRARY})
+set(LIBOSMOCODING_INCLUDE_DIRS ${LIBOSMOCODING_INCLUDE_DIR})
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(libosmocoding  DEFAULT_MSG LIBOSMOCODING_LIBRARY LIBOSMOCODING_INCLUDE_DIR)
+mark_as_advanced(LIBOSMOCODING_INCLUDE_DIR LIBOSMOCODING_LIBRARY )
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 660bbfd..03903f8 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -71,7 +71,7 @@
 
 
 add_library(grgsm SHARED ${grgsm_sources})
-target_link_libraries(grgsm ${Boost_LIBRARIES} ${GNURADIO_ALL_LIBRARIES} ${VOLK_LIBRARIES} ${LIBOSMOCORE_LIBRARIES}
+target_link_libraries(grgsm ${Boost_LIBRARIES} ${GNURADIO_ALL_LIBRARIES} ${VOLK_LIBRARIES} ${LIBOSMOCORE_LIBRARIES} ${LIBOSMOCODING_LIBRARIES} 
 # libraries required by plotting.h - have troubles to be installed by pybombs
 #    boost_iostreams
 #    boost_system
diff --git a/lib/decoding/control_channels_decoder_impl.cc b/lib/decoding/control_channels_decoder_impl.cc
index 532e255..ddea256 100644
--- a/lib/decoding/control_channels_decoder_impl.cc
+++ b/lib/decoding/control_channels_decoder_impl.cc
@@ -33,6 +33,25 @@
 namespace gr {
   namespace gsm {
 
+    static int ubits2sbits(ubit_t *ubits, sbit_t *sbits, int count)
+    {
+	    int i;
+
+	    for (i = 0; i < count; i++) {
+		    if (*ubits == 0x23) {
+			    ubits++;
+			    sbits++;
+			    continue;
+		    }
+		    if ((*ubits++) & 1)
+			    *sbits++ = -127;
+		    else
+			    *sbits++ = 127;
+	    }
+
+	    return count;
+    }
+
     control_channels_decoder::sptr
     control_channels_decoder::make()
     {
@@ -49,18 +68,6 @@
               gr::io_signature::make(0, 0, 0)),
               d_collected_bursts_num(0)
     {
-        //initialize de/interleaver
-        int j, k, B;
-        for (k = 0; k < CONV_SIZE; k++)
-        {
-            B = k % 4;
-            j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
-            interleave_trans[k] = B * 114 + j; //114=57 + 57
-        }
-        
-        //initialize decoder
-        FC_init(&fc_ctx, 40, 184);
-        
         //setup input/output ports
         message_port_register_in(pmt::mp("bursts"));
         set_msg_handler(pmt::mp("bursts"), boost::bind(&control_channels_decoder_impl::decode, this, _1));
@@ -73,7 +80,11 @@
 
     void control_channels_decoder_impl::decode(pmt::pmt_t msg)
     {
-        unsigned char iBLOCK[BLOCKS*iBLOCK_SIZE], hl, hn, conv_data[CONV_SIZE], decoded_data[PARITY_OUTPUT_SIZE];
+        ubit_t bursts_u[116 * 4];
+        sbit_t bursts_s[116 * 4];
+        uint8_t result[23];        
+		int n_errors, n_bits_total;
+    	
         d_bursts[d_collected_bursts_num] = msg;
         d_collected_bursts_num++;
         //get convecutive bursts
@@ -86,60 +97,22 @@
             {
                 pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
                 int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
-
-                for(int jj = 0; jj < 57; jj++)
-                {
-                    iBLOCK[ii*iBLOCK_SIZE+jj] = burst_bits[jj + 3];
-                    iBLOCK[ii*iBLOCK_SIZE+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
-                }
+                
+                memcpy(&bursts_u[ii*116], &burst_bits[3],58);
+                memcpy(&bursts_u[ii*116+58], &burst_bits[3+57+1+26],58);                
             }
-            //deinterleave
-            for (int k = 0; k < CONV_SIZE; k++)
-            {
-                conv_data[k] = iBLOCK[interleave_trans[k]];
-            }
-            //convolutional code decode
-            int errors = conv_decode(decoded_data, conv_data);
-            //std::cout << "Errors:" << errors << " " << parity_check(decoded_data) << std::endl;
-            // check parity
-            // If parity check error detected try to fix it.
+            //convert to soft bits
+            ubits2sbits(bursts_u, bursts_s, 116 * 4);
+            //decode
+            gsm0503_xcch_decode(result, bursts_s, &n_errors, &n_bits_total);
 
-            if (parity_check(decoded_data))
-            {
-                FC_init(&fc_ctx, 40, 184);
-                unsigned char crc_result[PARITY_OUTPUT_SIZE];
-                if (FC_check_crc(&fc_ctx, decoded_data, crc_result) == 0)
-                {
-                    //("error: sacch: parity error (errors=%d fn=%d)\n", errors, ctx->fn);
-                    //std::cout << "Uncorrectable errors!" << std::endl;
-                    errors = -1;
-                    return;
-                } else {
-                    //DEBUGF("Successfully corrected parity bits! (errors=%d fn=%d)\n", errors, ctx->fn);
-                    //std::cout << "Corrected some errors" << std::endl;
-                    memcpy(decoded_data, crc_result, PARITY_OUTPUT_SIZE);
-                    errors = 0;
-                }
-            } else {
-                //std::cout << "Everything correct" << std::endl;
-            }
-           //compress bits
-           unsigned char outmsg[28];
-           unsigned char sbuf_len=224;
-           int i, j, c, pos=0;
-           for(i = 0; i < sbuf_len; i += 8) {
-                for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
-                    c |= (!!decoded_data[i + j]) << j;
-                }
-                outmsg[pos++] = c & 0xff;
-           }
-
-           //send message with header of the first burst
+            //send to the output
             pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
             gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
             int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
             memcpy(header_plus_data, header, sizeof(gsmtap_hdr));
-            memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
+            
+            memcpy(header_plus_data+sizeof(gsmtap_hdr), result, DATA_BYTES);
             ((gsmtap_hdr*)header_plus_data)->type = GSMTAP_TYPE_UM;
             
             pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
diff --git a/lib/decoding/control_channels_decoder_impl.h b/lib/decoding/control_channels_decoder_impl.h
index 2fa3dce..542335b 100644
--- a/lib/decoding/control_channels_decoder_impl.h
+++ b/lib/decoding/control_channels_decoder_impl.h
@@ -26,6 +26,10 @@
 #include <grgsm/decoding/control_channels_decoder.h>
 #include "fire_crc.h"
 #include "cch.h"
+extern "C" {
+    #include <osmocom/coding/gsm0503_coding.h>
+    #include <osmocom/core/utils.h>
+}
 
 namespace gr {
   namespace gsm {
@@ -35,9 +39,8 @@
      private:
       unsigned int d_collected_bursts_num;
       pmt::pmt_t d_bursts[4];
-      unsigned short interleave_trans[CONV_SIZE];      
-      FC_CTX fc_ctx;      
-      void decode(pmt::pmt_t msg);
+     
+      void decode(pmt::pmt_t msg);      
      public:
       control_channels_decoder_impl();
       ~control_channels_decoder_impl();