piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 1 | /* -*- c++ -*- */ |
| 2 | /* |
| 3 | * Copyright 2014 <+YOU OR YOUR COMPANY+>. |
| 4 | * |
| 5 | * This is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3, or (at your option) |
| 8 | * any later version. |
| 9 | * |
| 10 | * This software is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this software; see the file COPYING. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
| 25 | #include <gnuradio/io_signature.h> |
| 26 | #include <gsm/gsmtap.h> |
| 27 | #include <iterator> |
| 28 | #include <algorithm> |
| 29 | #include "extract_system_info_impl.h" |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <iostream> |
| 33 | |
| 34 | namespace gr { |
| 35 | namespace gsm { |
| 36 | boost::mutex extract_mutex; |
| 37 | void extract_system_info_impl::process_bursts(pmt::pmt_t msg) |
| 38 | { |
| 39 | pmt::pmt_t burst = pmt::cdr(msg); |
| 40 | int8_t * burst_elements = (int8_t *)pmt::blob_data(burst); |
| 41 | size_t burst_len=pmt::blob_length(burst); |
| 42 | |
| 43 | pmt::pmt_t header_blob = pmt::car(msg); |
| 44 | gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_blob); |
| 45 | chan_info info; |
| 46 | info.id = header->arfcn; |
| 47 | info.pwr_db = header->signal_dbm; |
| 48 | |
| 49 | std::set<chan_info, compare_id>::iterator iter = d_c0_channels.find(info); |
| 50 | |
| 51 | boost::mutex::scoped_lock lock(extract_mutex); |
| 52 | if(iter != d_c0_channels.end()){ |
| 53 | info.lac = iter->lac; |
| 54 | info.cell_id = iter->cell_id; |
| 55 | info.mnc = iter->mnc; |
| 56 | d_c0_channels.erase(iter); |
| 57 | d_c0_channels.insert(info); |
| 58 | } |
| 59 | d_c0_channels.insert(info); |
| 60 | } |
| 61 | |
| 62 | void extract_system_info_impl::process_sysinfo(pmt::pmt_t msg){ |
| 63 | pmt::pmt_t msg_blob = pmt::cdr(msg); |
| 64 | uint8_t * msg_elements = (uint8_t *)pmt::blob_data(msg_blob); |
| 65 | |
| 66 | if(msg_elements[2]==0x1b){ |
| 67 | //wyciągnij arfcn |
| 68 | pmt::pmt_t header_blob = pmt::car(msg); |
| 69 | gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_blob); |
| 70 | chan_info info; |
| 71 | info.id = header->arfcn; |
| 72 | info.pwr_db = header->signal_dbm; |
| 73 | info.cell_id = (msg_elements[3]<<8)+msg_elements[4]; //wyciągnij cell id |
| 74 | info.lac = (msg_elements[8]<<8)+msg_elements[9]; //wyciągnij lac |
| 75 | info.mnc = (msg_elements[7]>>4); //wyciągnij id operatora |
| 76 | |
| 77 | std::set<chan_info, compare_id>::iterator iter = d_c0_channels.find(info); |
| 78 | boost::mutex::scoped_lock lock(extract_mutex); |
| 79 | if(iter != d_c0_channels.end()){ |
| 80 | d_c0_channels.erase(iter); |
| 81 | } |
| 82 | d_c0_channels.insert(info); |
| 83 | } |
| 84 | else if(msg_elements[2]==0x1c){ |
| 85 | pmt::pmt_t header_blob = pmt::car(msg); |
| 86 | gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(header_blob); |
| 87 | chan_info info; |
| 88 | info.id = header->arfcn; |
| 89 | info.pwr_db = header->signal_dbm; |
| 90 | info.lac = (msg_elements[6]<<8)+msg_elements[7]; //wyciągnij lac |
| 91 | info.mnc = (msg_elements[5]>>4); //wyciągnij id operatora |
| 92 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 93 | std::set<chan_info, compare_id>::iterator iter = d_c0_channels.find(info); |
| 94 | boost::mutex::scoped_lock lock(extract_mutex); |
| 95 | if(iter != d_c0_channels.end()){ |
| 96 | d_c0_channels.erase(iter); |
| 97 | if(iter->cell_id!=0){ |
| 98 | info.cell_id=iter->cell_id; |
| 99 | } |
| 100 | } |
| 101 | d_c0_channels.insert(info); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void extract_system_info_impl::show(){ |
| 106 | std::vector<chan_info> chans(d_c0_channels.begin(), d_c0_channels.end()); |
| 107 | std::vector<chan_info>::iterator iter; |
| 108 | //std::sort(chans.begin(), chans.end(), compare_pwr()); |
| 109 | for(iter=chans.begin(); iter != chans.end(); ++iter){ |
| 110 | std::cout << static_cast<int>((*iter).id) << "(" << static_cast<int>((*iter).pwr_db) << ") "; |
| 111 | } |
| 112 | std::cout << std::endl; |
| 113 | } |
| 114 | |
| 115 | std::vector<int> extract_system_info_impl::get_chans() |
| 116 | { |
| 117 | std::vector<chan_info> chans(d_c0_channels.begin(), d_c0_channels.end()); |
| 118 | std::vector<int> chans_ids(chans.size(),-1); |
| 119 | //std::sort(chans.begin(), chans.end(), compare_pwr()); |
| 120 | |
| 121 | for(int ii; ii < chans.size(); ++ii){ |
| 122 | chans_ids[ii] = chans[ii].id; |
| 123 | } |
| 124 | |
| 125 | return chans_ids; |
| 126 | } |
| 127 | |
| 128 | std::vector<int> extract_system_info_impl::get_lac() |
| 129 | { |
| 130 | std::vector<chan_info> chans(d_c0_channels.begin(), d_c0_channels.end()); |
| 131 | std::vector<int> chans_ids(chans.size(),-1); |
| 132 | //std::sort(chans.begin(), chans.end(), compare_pwr()); |
| 133 | |
| 134 | for(int ii; ii < chans.size(); ++ii){ |
| 135 | chans_ids[ii] = chans[ii].lac; |
| 136 | } |
| 137 | |
| 138 | return chans_ids; |
| 139 | } |
| 140 | std::vector<int> extract_system_info_impl::get_mnc() |
| 141 | { |
| 142 | std::vector<chan_info> chans(d_c0_channels.begin(), d_c0_channels.end()); |
| 143 | std::vector<int> chans_ids(chans.size(),-1); |
| 144 | //std::sort(chans.begin(), chans.end(), compare_pwr()); |
| 145 | |
| 146 | for(int ii; ii < chans.size(); ++ii){ |
| 147 | chans_ids[ii] = chans[ii].mnc; |
| 148 | } |
| 149 | |
| 150 | return chans_ids; |
| 151 | } |
| 152 | std::vector<int> extract_system_info_impl::get_cell_id() |
| 153 | { |
| 154 | std::vector<chan_info> chans(d_c0_channels.begin(), d_c0_channels.end()); |
| 155 | std::vector<int> chans_ids(chans.size(),-1); |
| 156 | //std::sort(chans.begin(), chans.end(), compare_pwr()); |
| 157 | |
| 158 | for(int ii; ii < chans.size(); ++ii){ |
| 159 | chans_ids[ii] = chans[ii].cell_id; |
| 160 | } |
| 161 | |
| 162 | return chans_ids; |
| 163 | } |
| 164 | |
| 165 | std::vector<int> extract_system_info_impl::get_pwrs() |
| 166 | { |
| 167 | std::vector<chan_info> chans(d_c0_channels.begin(), d_c0_channels.end()); |
| 168 | std::vector<int> pwrs(chans.size(),-1); |
| 169 | //std::sort(chans.begin(), chans.end(), compare_pwr()); |
| 170 | |
| 171 | for(int ii; ii < chans.size(); ++ii){ |
| 172 | pwrs[ii] = chans[ii].pwr_db; |
| 173 | } |
| 174 | |
| 175 | return pwrs; |
| 176 | } |
| 177 | |
| 178 | void extract_system_info_impl::reset() |
| 179 | { |
Piotr K | 66bb3cd | 2014-08-13 19:04:57 +0200 | [diff] [blame] | 180 | std::set<chan_info, compare_id>::iterator iter; |
| 181 | |
| 182 | chan_info info; |
| 183 | |
| 184 | for(iter = d_c0_channels.begin(); iter != d_c0_channels.end(); iter++){ |
| 185 | info.id = iter->id; |
| 186 | info.cell_id = iter->cell_id; //wyciągnij cell id |
| 187 | info.lac = iter->lac; //wyciągnij lac |
| 188 | info.mnc = iter->mnc; |
| 189 | info.pwr_db = -111; |
| 190 | d_c0_channels.erase(iter); |
| 191 | d_c0_channels.insert(info); |
| 192 | } |
| 193 | // d_c0_channels.clear(); |
| 194 | |
piotr | dda2227 | 2014-08-04 11:31:54 +0200 | [diff] [blame] | 195 | if(!empty_p(pmt::mp("bursts"))){ |
| 196 | delete_head_blocking(pmt::mp("bursts")); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | extract_system_info::sptr |
| 201 | extract_system_info::make() |
| 202 | { |
| 203 | return gnuradio::get_initial_sptr |
| 204 | (new extract_system_info_impl()); |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * The private constructor |
| 209 | */ |
| 210 | extract_system_info_impl::extract_system_info_impl() |
| 211 | : gr::block("extract_system_info", |
| 212 | gr::io_signature::make(0, 0, 0), |
| 213 | gr::io_signature::make(0, 0, 0)), |
| 214 | after_reset(false) |
| 215 | { |
| 216 | message_port_register_in(pmt::mp("bursts")); |
| 217 | set_msg_handler(pmt::mp("bursts"), boost::bind(&extract_system_info_impl::process_bursts, this, _1)); |
| 218 | message_port_register_in(pmt::mp("msgs")); |
| 219 | set_msg_handler(pmt::mp("msgs"), boost::bind(&extract_system_info_impl::process_sysinfo, this, _1)); |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Our virtual destructor. |
| 224 | */ |
| 225 | extract_system_info_impl::~extract_system_info_impl() |
| 226 | { |
| 227 | } |
| 228 | |
| 229 | |
| 230 | } /* namespace gsm */ |
| 231 | } /* namespace gr */ |
| 232 | |