blob: 73ce992d77f2f12b79dfebe36c04eaa4a9986633 [file] [log] [blame]
piotrdda22272014-08-04 11:31:54 +02001/* -*- c++ -*- */
ptrkrysik529895b2014-12-02 18:07:38 +01002/*
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
piotrdda22272014-08-04 11:31:54 +02008 * 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.
ptrkrysik529895b2014-12-02 18:07:38 +010011 *
12 * Gr-gsm is distributed in the hope that it will be useful,
piotrdda22272014-08-04 11:31:54 +020013 * 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.
ptrkrysik529895b2014-12-02 18:07:38 +010016 *
piotrdda22272014-08-04 11:31:54 +020017 * You should have received a copy of the GNU General Public License
ptrkrysik529895b2014-12-02 18:07:38 +010018 * along with gr-gsm; see the file COPYING. If not, write to
piotrdda22272014-08-04 11:31:54 +020019 * 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>
ptrkrysik3be74a72014-12-13 10:11:00 +010028#include <grgsm/gsmtap.h>
ptrkrysik402c1fa2014-12-03 22:09:29 +010029#include <unistd.h>
ptrkrysike9539c12015-07-06 08:34:22 +020030#include <map>
piotrdda22272014-08-04 11:31:54 +020031#include <iterator>
32#include <algorithm>
piotrdda22272014-08-04 11:31:54 +020033#include <iostream>
David Holm41b63f22015-08-22 16:52:42 +020034#include <grgsm/endian.h>
ptrkrysike9539c12015-07-06 08:34:22 +020035#include <boost/foreach.hpp>
ptrkrysik09405382015-08-02 22:02:52 +020036extern "C" {
37 #include <osmocom/gsm/gsm48_ie.h>
38}
39
piotrdda22272014-08-04 11:31:54 +020040
ptrkrysik402c1fa2014-12-03 22:09:29 +010041#include "extract_system_info_impl.h"
42
piotrdda22272014-08-04 11:31:54 +020043namespace gr {
44 namespace gsm {
45 boost::mutex extract_mutex;
46 void extract_system_info_impl::process_bursts(pmt::pmt_t msg)
47 {
ptrkrysika31a4812014-12-15 09:10:01 +010048 pmt::pmt_t burst_plus_header_blob = pmt::cdr(msg);
49 gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(burst_plus_header_blob);
piotrdda22272014-08-04 11:31:54 +020050
piotrdda22272014-08-04 11:31:54 +020051 chan_info info;
ptrkrysike9539c12015-07-06 08:34:22 +020052 info.id = be16toh(header->arfcn);
piotrdda22272014-08-04 11:31:54 +020053 info.pwr_db = header->signal_dbm;
piotrdda22272014-08-04 11:31:54 +020054
55 boost::mutex::scoped_lock lock(extract_mutex);
ptrkrysike9539c12015-07-06 08:34:22 +020056
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 }
piotrdda22272014-08-04 11:31:54 +020062 }
63
64 void extract_system_info_impl::process_sysinfo(pmt::pmt_t msg){
ptrkrysika31a4812014-12-15 09:10:01 +010065 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));
ptrkrysik09405382015-08-02 22:02:52 +020069 struct gsm_sysinfo_freq freq[1024];
piotrdda22272014-08-04 11:31:54 +020070
71 if(msg_elements[2]==0x1b){
piotrdda22272014-08-04 11:31:54 +020072 chan_info info;
ptrkrysike9539c12015-07-06 08:34:22 +020073 info.id = be16toh(header->arfcn); //take arfcn
piotrdda22272014-08-04 11:31:54 +020074 info.pwr_db = header->signal_dbm;
ptrkrysika31a4812014-12-15 09:10:01 +010075 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
Roman Khassrafa1dd7ee2015-09-26 17:20:49 +020077 info.mcc = ((msg_elements[5] & 0xF) * 100) + (((msg_elements[5] & 0xF0) >> 4) * 10) + ((msg_elements[6] & 0xF)); // take mcc
78 info.mnc = (msg_elements[7] & 0xF) * 10 + (msg_elements[7]>>4); //take mnc
Roman Khassraf9f4feb52015-09-26 18:44:56 +020079 info.ccch_conf = (msg_elements[10] & 0x7); // ccch_conf
80
piotrdda22272014-08-04 11:31:54 +020081 boost::mutex::scoped_lock lock(extract_mutex);
ptrkrysike9539c12015-07-06 08:34:22 +020082 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
83 d_c0_channels[info.id].copy_nonzero_elements(info);
84 } else {
85 d_c0_channels[info.id] = info;
piotrdda22272014-08-04 11:31:54 +020086 }
piotrdda22272014-08-04 11:31:54 +020087 }
88 else if(msg_elements[2]==0x1c){
piotrdda22272014-08-04 11:31:54 +020089 chan_info info;
ptrkrysike9539c12015-07-06 08:34:22 +020090 info.id = be16toh(header->arfcn); //take arfcn
piotrdda22272014-08-04 11:31:54 +020091 info.pwr_db = header->signal_dbm;
ptrkrysike9539c12015-07-06 08:34:22 +020092 info.lac = (msg_elements[6]<<8)+msg_elements[7]; //take lac
Roman Khassrafa1dd7ee2015-09-26 17:20:49 +020093 info.mcc = ((msg_elements[3] & 0xF) * 100) + (((msg_elements[3] & 0xF0) >> 4) * 10) + ((msg_elements[3] & 0xF)); // take mcc
94 info.mnc = (msg_elements[5] & 0xF) * 10 + (msg_elements[5]>>4); //take mnc
95
piotrdda22272014-08-04 11:31:54 +020096 boost::mutex::scoped_lock lock(extract_mutex);
ptrkrysike9539c12015-07-06 08:34:22 +020097 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
98 d_c0_channels[info.id].copy_nonzero_elements(info);
99 } else {
100 d_c0_channels[info.id] = info;
piotrdda22272014-08-04 11:31:54 +0200101 }
ptrkrysik09405382015-08-02 22:02:52 +0200102 }
103 else if(msg_elements[2]==0x1a){ //System Information Type 2
104 memset(freq, 0, sizeof(freq));
ptrkrysike9539c12015-07-06 08:34:22 +0200105 chan_info info;
106 info.id = be16toh(header->arfcn); //take arfcn
107 info.pwr_db = header->signal_dbm;
108 boost::mutex::scoped_lock lock(extract_mutex);
109 //read neighbour cells
ptrkrysik09405382015-08-02 22:02:52 +0200110 gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0xce, 0x01);
ptrkrysik09405382015-08-02 22:02:52 +0200111
112 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
113 d_c0_channels[info.id].copy_nonzero_elements(info);
114 } else {
115 d_c0_channels[info.id] = info;
116 }
ptrkrysik5e376742015-08-06 10:02:04 +0200117
118 for(int arfcn=0; arfcn<sizeof(freq); arfcn++){
119 if(freq[arfcn].mask==0x01){
120 d_c0_channels[info.id].neighbour_cells.insert(arfcn);
121 }
122 }
ptrkrysik09405382015-08-02 22:02:52 +0200123 }
124 else if(msg_elements[2]==0x02){ //System Information Type 2bis
125 memset(freq, 0, sizeof(freq));
126 chan_info info;
127 info.id = be16toh(header->arfcn); //take arfcn
128 info.pwr_db = header->signal_dbm;
129 boost::mutex::scoped_lock lock(extract_mutex);
130 //read neighbour cells
131 gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0xce, 0x01);
ptrkrysik09405382015-08-02 22:02:52 +0200132 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
133 d_c0_channels[info.id].copy_nonzero_elements(info);
134 } else {
135 d_c0_channels[info.id] = info;
136 }
ptrkrysik5e376742015-08-06 10:02:04 +0200137
138 for(int arfcn=0; arfcn<sizeof(freq); arfcn++){
139 if(freq[arfcn].mask==0x01){
140 d_c0_channels[info.id].neighbour_cells.insert(arfcn);
141 }
142 }
ptrkrysik09405382015-08-02 22:02:52 +0200143 }
144 else if(msg_elements[2]==0x03){ //System Information Type 2ter
145 memset(freq, 0, sizeof(freq));
146 chan_info info;
147 info.id = be16toh(header->arfcn); //take arfcn
148 info.pwr_db = header->signal_dbm;
149 boost::mutex::scoped_lock lock(extract_mutex);
150 //read neighbour cells
151 gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0x8e, 0x01);
ptrkrysike9539c12015-07-06 08:34:22 +0200152 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
153 d_c0_channels[info.id].copy_nonzero_elements(info);
154 } else {
155 d_c0_channels[info.id] = info;
156 }
ptrkrysik5e376742015-08-06 10:02:04 +0200157
158 for(int arfcn=0; arfcn<sizeof(freq); arfcn++){
159 if(freq[arfcn].mask==0x01){
160 d_c0_channels[info.id].neighbour_cells.insert(arfcn);
161 }
162 }
piotrdda22272014-08-04 11:31:54 +0200163 }
164 }
165
piotrdda22272014-08-04 11:31:54 +0200166 std::vector<int> extract_system_info_impl::get_chans()
167 {
ptrkrysike9539c12015-07-06 08:34:22 +0200168 std::vector<int> chans_ids;
169 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
170 chans_ids.push_back(i.second.id);
piotrdda22272014-08-04 11:31:54 +0200171 }
piotrdda22272014-08-04 11:31:54 +0200172 return chans_ids;
173 }
174
175 std::vector<int> extract_system_info_impl::get_lac()
176 {
ptrkrysike9539c12015-07-06 08:34:22 +0200177 std::vector<int> lacs;
178 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
179 lacs.push_back(i.second.lac);
piotrdda22272014-08-04 11:31:54 +0200180 }
ptrkrysike9539c12015-07-06 08:34:22 +0200181 return lacs;
piotrdda22272014-08-04 11:31:54 +0200182 }
ptrkrysike9539c12015-07-06 08:34:22 +0200183
Roman Khassrafa1dd7ee2015-09-26 17:20:49 +0200184 std::vector<int> extract_system_info_impl::get_mcc()
185 {
186 std::vector<int> mccs;
187 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
188 mccs.push_back(i.second.mcc);
189 }
190 return mccs;
191 }
192
piotrdda22272014-08-04 11:31:54 +0200193 std::vector<int> extract_system_info_impl::get_mnc()
194 {
ptrkrysike9539c12015-07-06 08:34:22 +0200195 std::vector<int> mncs;
196 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
197 mncs.push_back(i.second.mnc);
piotrdda22272014-08-04 11:31:54 +0200198 }
ptrkrysike9539c12015-07-06 08:34:22 +0200199 return mncs;
piotrdda22272014-08-04 11:31:54 +0200200 }
ptrkrysike9539c12015-07-06 08:34:22 +0200201
piotrdda22272014-08-04 11:31:54 +0200202 std::vector<int> extract_system_info_impl::get_cell_id()
203 {
ptrkrysike9539c12015-07-06 08:34:22 +0200204 std::vector<int> cell_ids;
205 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
206 cell_ids.push_back(i.second.cell_id);
piotrdda22272014-08-04 11:31:54 +0200207 }
ptrkrysike9539c12015-07-06 08:34:22 +0200208 return cell_ids;
piotrdda22272014-08-04 11:31:54 +0200209 }
210
211 std::vector<int> extract_system_info_impl::get_pwrs()
212 {
ptrkrysike9539c12015-07-06 08:34:22 +0200213 std::vector<int> pwrs;
214 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
215 pwrs.push_back(i.second.pwr_db);
piotrdda22272014-08-04 11:31:54 +0200216 }
piotrdda22272014-08-04 11:31:54 +0200217 return pwrs;
218 }
Roman Khassrafa1dd7ee2015-09-26 17:20:49 +0200219
Roman Khassraf9f4feb52015-09-26 18:44:56 +0200220 std::vector<int> extract_system_info_impl::get_ccch_conf()
221 {
222 std::vector<int> ccch_confs;
223 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
224 ccch_confs.push_back(i.second.ccch_conf);
225 }
226 return ccch_confs;
227 }
228
ptrkrysike9539c12015-07-06 08:34:22 +0200229 std::vector<int> extract_system_info_impl::get_neighbours(int chan_id)
230 {
231 std::vector<int> neighbour_cells;
232 BOOST_FOREACH(int n, d_c0_channels[chan_id].neighbour_cells){
233 neighbour_cells.push_back(n);
234 }
235 return neighbour_cells;
236 }
piotrdda22272014-08-04 11:31:54 +0200237
238 void extract_system_info_impl::reset()
239 {
ptrkrysike9539c12015-07-06 08:34:22 +0200240 d_c0_channels.clear();
piotrdda22272014-08-04 11:31:54 +0200241 if(!empty_p(pmt::mp("bursts"))){
242 delete_head_blocking(pmt::mp("bursts"));
243 }
ptrkrysike9539c12015-07-06 08:34:22 +0200244 if(!empty_p(pmt::mp("msgs"))){
245 delete_head_blocking(pmt::mp("msgs"));
246 }
piotrdda22272014-08-04 11:31:54 +0200247 }
248
249 extract_system_info::sptr
250 extract_system_info::make()
251 {
252 return gnuradio::get_initial_sptr
253 (new extract_system_info_impl());
254 }
255
256 /*
257 * The private constructor
258 */
259 extract_system_info_impl::extract_system_info_impl()
260 : gr::block("extract_system_info",
261 gr::io_signature::make(0, 0, 0),
262 gr::io_signature::make(0, 0, 0)),
263 after_reset(false)
264 {
265 message_port_register_in(pmt::mp("bursts"));
266 set_msg_handler(pmt::mp("bursts"), boost::bind(&extract_system_info_impl::process_bursts, this, _1));
267 message_port_register_in(pmt::mp("msgs"));
268 set_msg_handler(pmt::mp("msgs"), boost::bind(&extract_system_info_impl::process_sysinfo, this, _1));
269 }
270
271 /*
272 * Our virtual destructor.
273 */
274 extract_system_info_impl::~extract_system_info_impl()
275 {
276 }
277
278
279 } /* namespace gsm */
280} /* namespace gr */
281