piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 2 | /* |
| 3 | * @file |
| 4 | * @author Piotr Krysik <ptrkrysik@gmail.com> |
| 5 | * @section LICENSE |
| 6 | * |
| 7 | * Gr-gsm is free software; you can redistribute it and/or modify |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3, or (at your option) |
| 10 | * any later version. |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 11 | * |
| 12 | * Gr-gsm is distributed in the hope that it will be useful, |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 16 | * |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
ptrkrysik | 529895b | 2014-12-02 18:07:38 +0100 | [diff] [blame] | 18 | * along with gr-gsm; see the file COPYING. If not, write to |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 19 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include <gnuradio/io_signature.h> |
ptrkrysik | 3be74a7 | 2014-12-13 10:11:00 +0100 | [diff] [blame] | 28 | #include <grgsm/gsmtap.h> |
ptrkrysik | 402c1fa | 2014-12-03 22:09:29 +0100 | [diff] [blame] | 29 | #include <unistd.h> |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 30 | #include <map> |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 31 | #include <iterator> |
| 32 | #include <algorithm> |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 33 | #include <iostream> |
David Holm | 41b63f2 | 2015-08-22 16:52:42 +0200 | [diff] [blame] | 34 | #include <grgsm/endian.h> |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 35 | #include <boost/foreach.hpp> |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 36 | extern "C" { |
| 37 | #include <osmocom/gsm/gsm48_ie.h> |
| 38 | } |
| 39 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 40 | |
ptrkrysik | 402c1fa | 2014-12-03 22:09:29 +0100 | [diff] [blame] | 41 | #include "extract_system_info_impl.h" |
| 42 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 43 | namespace gr { |
| 44 | namespace gsm { |
| 45 | boost::mutex extract_mutex; |
| 46 | void extract_system_info_impl::process_bursts(pmt::pmt_t msg) |
| 47 | { |
ptrkrysik | a31a481 | 2014-12-15 09:10:01 +0100 | [diff] [blame] | 48 | pmt::pmt_t burst_plus_header_blob = pmt::cdr(msg); |
| 49 | gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(burst_plus_header_blob); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 50 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 51 | chan_info info; |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 52 | info.id = be16toh(header->arfcn); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 53 | info.pwr_db = header->signal_dbm; |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 54 | |
| 55 | boost::mutex::scoped_lock lock(extract_mutex); |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 56 | |
| 57 | if(d_c0_channels.find(info.id) != d_c0_channels.end()){ |
| 58 | d_c0_channels[info.id].copy_nonzero_elements(info); |
| 59 | } else { |
| 60 | d_c0_channels[info.id] = info; |
| 61 | } |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void extract_system_info_impl::process_sysinfo(pmt::pmt_t msg){ |
ptrkrysik | a31a481 | 2014-12-15 09:10:01 +0100 | [diff] [blame] | 65 | pmt::pmt_t message_plus_header_blob = pmt::cdr(msg); |
| 66 | uint8_t * message_plus_header = (uint8_t *)pmt::blob_data(message_plus_header_blob); |
| 67 | gsmtap_hdr * header = (gsmtap_hdr *)message_plus_header; |
| 68 | uint8_t * msg_elements = (uint8_t *)(message_plus_header+sizeof(gsmtap_hdr)); |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 69 | struct gsm_sysinfo_freq freq[1024]; |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 70 | |
| 71 | if(msg_elements[2]==0x1b){ |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 72 | chan_info info; |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 73 | info.id = be16toh(header->arfcn); //take arfcn |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 74 | info.pwr_db = header->signal_dbm; |
ptrkrysik | a31a481 | 2014-12-15 09:10:01 +0100 | [diff] [blame] | 75 | info.cell_id = (msg_elements[3]<<8)+msg_elements[4]; //take cell id |
| 76 | info.lac = (msg_elements[8]<<8)+msg_elements[9]; //take lac |
| 77 | info.mnc = (msg_elements[7]>>4); //take mnc |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 78 | boost::mutex::scoped_lock lock(extract_mutex); |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 79 | if(d_c0_channels.find(info.id) != d_c0_channels.end()){ |
| 80 | d_c0_channels[info.id].copy_nonzero_elements(info); |
| 81 | } else { |
| 82 | d_c0_channels[info.id] = info; |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 83 | } |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 84 | } |
| 85 | else if(msg_elements[2]==0x1c){ |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 86 | chan_info info; |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 87 | info.id = be16toh(header->arfcn); //take arfcn |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 88 | info.pwr_db = header->signal_dbm; |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 89 | info.lac = (msg_elements[6]<<8)+msg_elements[7]; //take lac |
| 90 | info.mnc = (msg_elements[5]>>4); //take mnc |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 91 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 92 | boost::mutex::scoped_lock lock(extract_mutex); |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 93 | if(d_c0_channels.find(info.id) != d_c0_channels.end()){ |
| 94 | d_c0_channels[info.id].copy_nonzero_elements(info); |
| 95 | } else { |
| 96 | d_c0_channels[info.id] = info; |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 97 | } |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 98 | } |
| 99 | else if(msg_elements[2]==0x1a){ //System Information Type 2 |
| 100 | memset(freq, 0, sizeof(freq)); |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 101 | chan_info info; |
| 102 | info.id = be16toh(header->arfcn); //take arfcn |
| 103 | info.pwr_db = header->signal_dbm; |
| 104 | boost::mutex::scoped_lock lock(extract_mutex); |
| 105 | //read neighbour cells |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 106 | gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0xce, 0x01); |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 107 | |
| 108 | if(d_c0_channels.find(info.id) != d_c0_channels.end()){ |
| 109 | d_c0_channels[info.id].copy_nonzero_elements(info); |
| 110 | } else { |
| 111 | d_c0_channels[info.id] = info; |
| 112 | } |
ptrkrysik | 5e37674 | 2015-08-06 10:02:04 +0200 | [diff] [blame] | 113 | |
| 114 | for(int arfcn=0; arfcn<sizeof(freq); arfcn++){ |
| 115 | if(freq[arfcn].mask==0x01){ |
| 116 | d_c0_channels[info.id].neighbour_cells.insert(arfcn); |
| 117 | } |
| 118 | } |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 119 | } |
| 120 | else if(msg_elements[2]==0x02){ //System Information Type 2bis |
| 121 | memset(freq, 0, sizeof(freq)); |
| 122 | chan_info info; |
| 123 | info.id = be16toh(header->arfcn); //take arfcn |
| 124 | info.pwr_db = header->signal_dbm; |
| 125 | boost::mutex::scoped_lock lock(extract_mutex); |
| 126 | //read neighbour cells |
| 127 | gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0xce, 0x01); |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 128 | if(d_c0_channels.find(info.id) != d_c0_channels.end()){ |
| 129 | d_c0_channels[info.id].copy_nonzero_elements(info); |
| 130 | } else { |
| 131 | d_c0_channels[info.id] = info; |
| 132 | } |
ptrkrysik | 5e37674 | 2015-08-06 10:02:04 +0200 | [diff] [blame] | 133 | |
| 134 | for(int arfcn=0; arfcn<sizeof(freq); arfcn++){ |
| 135 | if(freq[arfcn].mask==0x01){ |
| 136 | d_c0_channels[info.id].neighbour_cells.insert(arfcn); |
| 137 | } |
| 138 | } |
ptrkrysik | 0940538 | 2015-08-02 22:02:52 +0200 | [diff] [blame] | 139 | } |
| 140 | else if(msg_elements[2]==0x03){ //System Information Type 2ter |
| 141 | memset(freq, 0, sizeof(freq)); |
| 142 | chan_info info; |
| 143 | info.id = be16toh(header->arfcn); //take arfcn |
| 144 | info.pwr_db = header->signal_dbm; |
| 145 | boost::mutex::scoped_lock lock(extract_mutex); |
| 146 | //read neighbour cells |
| 147 | gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0x8e, 0x01); |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 148 | if(d_c0_channels.find(info.id) != d_c0_channels.end()){ |
| 149 | d_c0_channels[info.id].copy_nonzero_elements(info); |
| 150 | } else { |
| 151 | d_c0_channels[info.id] = info; |
| 152 | } |
ptrkrysik | 5e37674 | 2015-08-06 10:02:04 +0200 | [diff] [blame] | 153 | |
| 154 | for(int arfcn=0; arfcn<sizeof(freq); arfcn++){ |
| 155 | if(freq[arfcn].mask==0x01){ |
| 156 | d_c0_channels[info.id].neighbour_cells.insert(arfcn); |
| 157 | } |
| 158 | } |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 162 | std::vector<int> extract_system_info_impl::get_chans() |
| 163 | { |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 164 | std::vector<int> chans_ids; |
| 165 | BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){ |
| 166 | chans_ids.push_back(i.second.id); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 167 | } |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 168 | return chans_ids; |
| 169 | } |
| 170 | |
| 171 | std::vector<int> extract_system_info_impl::get_lac() |
| 172 | { |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 173 | std::vector<int> lacs; |
| 174 | BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){ |
| 175 | lacs.push_back(i.second.lac); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 176 | } |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 177 | return lacs; |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 178 | } |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 179 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 180 | std::vector<int> extract_system_info_impl::get_mnc() |
| 181 | { |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 182 | std::vector<int> mncs; |
| 183 | BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){ |
| 184 | mncs.push_back(i.second.mnc); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 185 | } |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 186 | return mncs; |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 187 | } |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 188 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 189 | std::vector<int> extract_system_info_impl::get_cell_id() |
| 190 | { |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 191 | std::vector<int> cell_ids; |
| 192 | BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){ |
| 193 | cell_ids.push_back(i.second.cell_id); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 194 | } |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 195 | return cell_ids; |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | std::vector<int> extract_system_info_impl::get_pwrs() |
| 199 | { |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 200 | std::vector<int> pwrs; |
| 201 | BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){ |
| 202 | pwrs.push_back(i.second.pwr_db); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 203 | } |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 204 | return pwrs; |
| 205 | } |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 206 | std::vector<int> extract_system_info_impl::get_neighbours(int chan_id) |
| 207 | { |
| 208 | std::vector<int> neighbour_cells; |
| 209 | BOOST_FOREACH(int n, d_c0_channels[chan_id].neighbour_cells){ |
| 210 | neighbour_cells.push_back(n); |
| 211 | } |
| 212 | return neighbour_cells; |
| 213 | } |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 214 | |
| 215 | void extract_system_info_impl::reset() |
| 216 | { |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 217 | d_c0_channels.clear(); |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 218 | if(!empty_p(pmt::mp("bursts"))){ |
| 219 | delete_head_blocking(pmt::mp("bursts")); |
| 220 | } |
ptrkrysik | e9539c1 | 2015-07-06 08:34:22 +0200 | [diff] [blame] | 221 | if(!empty_p(pmt::mp("msgs"))){ |
| 222 | delete_head_blocking(pmt::mp("msgs")); |
| 223 | } |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | extract_system_info::sptr |
| 227 | extract_system_info::make() |
| 228 | { |
| 229 | return gnuradio::get_initial_sptr |
| 230 | (new extract_system_info_impl()); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * The private constructor |
| 235 | */ |
| 236 | extract_system_info_impl::extract_system_info_impl() |
| 237 | : gr::block("extract_system_info", |
| 238 | gr::io_signature::make(0, 0, 0), |
| 239 | gr::io_signature::make(0, 0, 0)), |
| 240 | after_reset(false) |
| 241 | { |
| 242 | message_port_register_in(pmt::mp("bursts")); |
| 243 | set_msg_handler(pmt::mp("bursts"), boost::bind(&extract_system_info_impl::process_bursts, this, _1)); |
| 244 | message_port_register_in(pmt::mp("msgs")); |
| 245 | set_msg_handler(pmt::mp("msgs"), boost::bind(&extract_system_info_impl::process_sysinfo, this, _1)); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Our virtual destructor. |
| 250 | */ |
| 251 | extract_system_info_impl::~extract_system_info_impl() |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | |
| 256 | } /* namespace gsm */ |
| 257 | } /* namespace gr */ |
| 258 | |