rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* @file |
Piotr Krysik | a6268a5 | 2017-08-23 16:02:19 +0200 | [diff] [blame] | 3 | * @author (C) 2015 by Pieter Robyns <pieter.robyns@uhasselt.be> |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 4 | * @section LICENSE |
Martin Jesper Low Madsen | f3105d9 | 2015-06-24 13:33:56 +0200 | [diff] [blame] | 5 | * |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 6 | * Gr-gsm is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3, or (at your option) |
| 9 | * any later version. |
Martin Jesper Low Madsen | f3105d9 | 2015-06-24 13:33:56 +0200 | [diff] [blame] | 10 | * |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 11 | * Gr-gsm is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Martin Jesper Low Madsen | f3105d9 | 2015-06-24 13:33:56 +0200 | [diff] [blame] | 15 | * |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with gr-gsm; see the file COPYING. If not, write to |
| 18 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 19 | * Boston, MA 02110-1301, USA. |
Martin Jesper Low Madsen | f3105d9 | 2015-06-24 13:33:56 +0200 | [diff] [blame] | 20 | * |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <gnuradio/io_signature.h> |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 28 | #include <grgsm/gsmtap.h> |
Martin Jesper Low Madsen | f3105d9 | 2015-06-24 13:33:56 +0200 | [diff] [blame] | 29 | #include <grgsm/endian.h> |
rpp | 11b549e | 2015-06-11 10:25:19 +0200 | [diff] [blame] | 30 | #include <boost/algorithm/clamp.hpp> |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 31 | #include "cx_channel_hopper_impl.h" |
| 32 | |
| 33 | namespace gr { |
| 34 | namespace gsm { |
| 35 | |
| 36 | cx_channel_hopper::sptr |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 37 | cx_channel_hopper::make(const std::vector<int> &ma, int maio, int hsn) |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 38 | { |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 39 | return gnuradio::get_initial_sptr |
| 40 | (new cx_channel_hopper_impl(ma, maio, hsn)); |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /* |
| 44 | * The private constructor |
| 45 | */ |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 46 | cx_channel_hopper_impl::cx_channel_hopper_impl(const std::vector<int> &ma, int maio, int hsn) |
| 47 | : gr::block("cx_channel_hopper", |
| 48 | gr::io_signature::make(0, 0, 0), |
| 49 | gr::io_signature::make(0, 0, 0)), |
| 50 | d_ma(ma), |
| 51 | d_maio(maio), |
| 52 | d_hsn(hsn) |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 53 | { |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 54 | d_narfcn = ma.size(); |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 55 | |
rpp | 11b549e | 2015-06-11 10:25:19 +0200 | [diff] [blame] | 56 | // Check user input for GSM 05.02, p16 compliance |
| 57 | if(d_narfcn < 1 || d_narfcn > 64) { |
| 58 | std::cerr << "warning: clamping number of RFCNs in the MA (" << d_narfcn << "), which should be 1 <= N <= 64." << std::endl; |
| 59 | d_narfcn = boost::algorithm::clamp(d_narfcn, 1, 64); |
| 60 | d_ma.resize(d_narfcn); |
| 61 | } |
| 62 | |
| 63 | if(d_maio < 0 || d_maio >= d_narfcn) { |
| 64 | std::cerr << "warning: clamping MAIO (" << d_maio << "), which should be 0 <= MAIO < N." << std::endl; |
| 65 | d_maio = boost::algorithm::clamp(d_maio, 0, d_narfcn - 1); |
| 66 | } |
| 67 | |
| 68 | if(d_hsn < 0 || d_hsn > 63) { |
| 69 | std::cerr << "warning: clamping HSN (" << d_hsn << "), which should be 0 <= HSN < 64." << std::endl; |
| 70 | d_hsn = boost::algorithm::clamp(d_hsn, 0, 63); |
| 71 | } |
| 72 | |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 73 | message_port_register_in(pmt::mp("CX")); |
| 74 | set_msg_handler(pmt::mp("CX"), boost::bind(&cx_channel_hopper_impl::assemble_bursts, this, _1)); |
| 75 | message_port_register_out(pmt::mp("bursts")); |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Our virtual destructor. |
| 80 | */ |
| 81 | cx_channel_hopper_impl::~cx_channel_hopper_impl() |
| 82 | { |
| 83 | } |
| 84 | |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 85 | /** |
Martin Jesper Low Madsen | f3105d9 | 2015-06-24 13:33:56 +0200 | [diff] [blame] | 86 | * Random number table used for calculating the |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 87 | * hopping sequence. Defined in GSM 05.02. |
| 88 | */ |
| 89 | unsigned char RNTABLE[114] = { |
| 90 | 48, 98, 63, 1, 36, 95, 78, 102, 94, 73, \ |
| 91 | 0, 64, 25, 81, 76, 59, 124, 23, 104, 100, \ |
| 92 | 101, 47, 118, 85, 18, 56, 96, 86, 54, 2, \ |
| 93 | 80, 34, 127, 13, 6, 89, 57, 103, 12, 74, \ |
| 94 | 55, 111, 75, 38, 109, 71, 112, 29, 11, 88, \ |
| 95 | 87, 19, 3, 68, 110, 26, 33, 31, 8, 45, \ |
| 96 | 82, 58, 40, 107, 32, 5, 106, 92, 62, 67, \ |
| 97 | 77, 108, 122, 37, 60, 66, 121, 42, 51, 126, \ |
| 98 | 117, 114, 4, 90, 43, 52, 53, 113, 120, 72, \ |
| 99 | 16, 49, 7, 79, 119, 61, 22, 84, 9, 97, \ |
| 100 | 91, 15, 21, 24, 46, 39, 93, 105, 65, 70, \ |
| 101 | 125, 99, 17, 123 \ |
| 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Slow Frequency Hopping (SFH) MAI calculation based |
| 106 | * on airprobe-hopping by Bogdan Diaconescu. |
| 107 | */ |
| 108 | int cx_channel_hopper_impl::calculate_ma_sfh(int maio, int hsn, int n, int fn) |
| 109 | { |
| 110 | int mai = 0; |
| 111 | int s = 0; |
| 112 | int nbin = floor(log2(n) + 1); |
| 113 | int t1 = fn / 1326; |
| 114 | int t2 = fn % 26; |
| 115 | int t3 = fn % 51; |
| 116 | |
| 117 | if (hsn == 0) |
| 118 | mai = (fn + maio) % n; |
| 119 | else { |
| 120 | int t1r = t1 % 64; |
| 121 | int m = t2 + RNTABLE[(hsn ^ t1r) + t3]; |
| 122 | int mprim = m % (1 << nbin); |
| 123 | int tprim = t3 % (1 << nbin); |
| 124 | |
| 125 | if (mprim < n) |
| 126 | s = mprim; |
| 127 | else |
| 128 | s = (mprim + tprim) % n; |
| 129 | |
| 130 | mai = (s + maio) % n; |
| 131 | } |
| 132 | |
| 133 | return (mai); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Given MA, MAIO, HSN, and FN, decide which frames |
| 138 | * to forward to the demapper. |
| 139 | */ |
| 140 | void cx_channel_hopper_impl::assemble_bursts(pmt::pmt_t msg) |
| 141 | { |
| 142 | pmt::pmt_t header_plus_burst = pmt::cdr(msg); |
| 143 | gsmtap_hdr *header = (gsmtap_hdr *)pmt::blob_data(header_plus_burst); |
| 144 | |
| 145 | uint32_t frame_nr = be32toh(header->frame_number); |
Piotr Krysik | 938cda4 | 2016-07-14 11:44:45 +0200 | [diff] [blame] | 146 | uint16_t frame_ca = be16toh(header->arfcn) & 0x3FFF; //change highest bits to '0' |
| 147 | //in order to leave only ARFCN number |
Your Name | a7976a3 | 2016-07-12 17:23:13 +0430 | [diff] [blame] | 148 | |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 149 | int mai = calculate_ma_sfh(d_maio, d_hsn, d_narfcn, frame_nr); |
| 150 | |
rpp | 11b549e | 2015-06-11 10:25:19 +0200 | [diff] [blame] | 151 | if(d_ma[mai] == (int)frame_ca) { |
rpp | 267214a | 2015-06-10 11:49:55 +0200 | [diff] [blame] | 152 | message_port_pub(pmt::mp("bursts"), msg); |
| 153 | } |
| 154 | } |
| 155 | |
rpp | 4f0f3b5 | 2015-06-10 10:23:30 +0200 | [diff] [blame] | 156 | } /* namespace gsm */ |
| 157 | } /* namespace gr */ |