blob: 846c87adf4c5c20ade9ebb1c823caacb3d0ad723 [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>
ptrkrysike9539c12015-07-06 08:34:22 +020034#include <endian.h>
35#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
77 info.mnc = (msg_elements[7]>>4); //take mnc
piotrdda22272014-08-04 11:31:54 +020078 boost::mutex::scoped_lock lock(extract_mutex);
ptrkrysike9539c12015-07-06 08:34:22 +020079 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;
piotrdda22272014-08-04 11:31:54 +020083 }
piotrdda22272014-08-04 11:31:54 +020084 }
85 else if(msg_elements[2]==0x1c){
piotrdda22272014-08-04 11:31:54 +020086 chan_info info;
ptrkrysike9539c12015-07-06 08:34:22 +020087 info.id = be16toh(header->arfcn); //take arfcn
piotrdda22272014-08-04 11:31:54 +020088 info.pwr_db = header->signal_dbm;
ptrkrysike9539c12015-07-06 08:34:22 +020089 info.lac = (msg_elements[6]<<8)+msg_elements[7]; //take lac
90 info.mnc = (msg_elements[5]>>4); //take mnc
piotrdda22272014-08-04 11:31:54 +020091
piotrdda22272014-08-04 11:31:54 +020092 boost::mutex::scoped_lock lock(extract_mutex);
ptrkrysike9539c12015-07-06 08:34:22 +020093 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;
piotrdda22272014-08-04 11:31:54 +020097 }
ptrkrysik09405382015-08-02 22:02:52 +020098 }
99 else if(msg_elements[2]==0x1a){ //System Information Type 2
100 memset(freq, 0, sizeof(freq));
ptrkrysike9539c12015-07-06 08:34:22 +0200101 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
ptrkrysik09405382015-08-02 22:02:52 +0200106 gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0xce, 0x01);
107 for(int arfcn=0; arfcn<sizeof(freq); arfcn++){
108 if(freq[arfcn].mask==0x01){
109 d_c0_channels[info.id].neighbour_cells.insert(arfcn);
110 }
111 }
112
113 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
114 d_c0_channels[info.id].copy_nonzero_elements(info);
115 } else {
116 d_c0_channels[info.id] = info;
117 }
118 }
119 else if(msg_elements[2]==0x02){ //System Information Type 2bis
120 memset(freq, 0, sizeof(freq));
121 chan_info info;
122 info.id = be16toh(header->arfcn); //take arfcn
123 info.pwr_db = header->signal_dbm;
124 boost::mutex::scoped_lock lock(extract_mutex);
125 //read neighbour cells
126 gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0xce, 0x01);
127 for(int arfcn=0; arfcn<sizeof(freq); arfcn++){
128 if(freq[arfcn].mask==0x01){
129 d_c0_channels[info.id].neighbour_cells.insert(arfcn);
130 }
131 }
132
133 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
134 d_c0_channels[info.id].copy_nonzero_elements(info);
135 } else {
136 d_c0_channels[info.id] = info;
137 }
138 }
139 else if(msg_elements[2]==0x03){ //System Information Type 2ter
140 memset(freq, 0, sizeof(freq));
141 chan_info info;
142 info.id = be16toh(header->arfcn); //take arfcn
143 info.pwr_db = header->signal_dbm;
144 boost::mutex::scoped_lock lock(extract_mutex);
145 //read neighbour cells
146 gsm48_decode_freq_list(freq, &msg_elements[3], 16, 0x8e, 0x01);
147 for(int arfcn=0; arfcn<sizeof(freq); arfcn++){
148 if(freq[arfcn].mask==0x01){
149 d_c0_channels[info.id].neighbour_cells.insert(arfcn);
150 }
151 }
ptrkrysike9539c12015-07-06 08:34:22 +0200152
153 if(d_c0_channels.find(info.id) != d_c0_channels.end()){
154 d_c0_channels[info.id].copy_nonzero_elements(info);
155 } else {
156 d_c0_channels[info.id] = info;
157 }
piotrdda22272014-08-04 11:31:54 +0200158 }
159 }
160
piotrdda22272014-08-04 11:31:54 +0200161 std::vector<int> extract_system_info_impl::get_chans()
162 {
ptrkrysike9539c12015-07-06 08:34:22 +0200163 std::vector<int> chans_ids;
164 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
165 chans_ids.push_back(i.second.id);
piotrdda22272014-08-04 11:31:54 +0200166 }
piotrdda22272014-08-04 11:31:54 +0200167 return chans_ids;
168 }
169
170 std::vector<int> extract_system_info_impl::get_lac()
171 {
ptrkrysike9539c12015-07-06 08:34:22 +0200172 std::vector<int> lacs;
173 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
174 lacs.push_back(i.second.lac);
piotrdda22272014-08-04 11:31:54 +0200175 }
ptrkrysike9539c12015-07-06 08:34:22 +0200176 return lacs;
piotrdda22272014-08-04 11:31:54 +0200177 }
ptrkrysike9539c12015-07-06 08:34:22 +0200178
piotrdda22272014-08-04 11:31:54 +0200179 std::vector<int> extract_system_info_impl::get_mnc()
180 {
ptrkrysike9539c12015-07-06 08:34:22 +0200181 std::vector<int> mncs;
182 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
183 mncs.push_back(i.second.mnc);
piotrdda22272014-08-04 11:31:54 +0200184 }
ptrkrysike9539c12015-07-06 08:34:22 +0200185 return mncs;
piotrdda22272014-08-04 11:31:54 +0200186 }
ptrkrysike9539c12015-07-06 08:34:22 +0200187
piotrdda22272014-08-04 11:31:54 +0200188 std::vector<int> extract_system_info_impl::get_cell_id()
189 {
ptrkrysike9539c12015-07-06 08:34:22 +0200190 std::vector<int> cell_ids;
191 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
192 cell_ids.push_back(i.second.cell_id);
piotrdda22272014-08-04 11:31:54 +0200193 }
ptrkrysike9539c12015-07-06 08:34:22 +0200194 return cell_ids;
piotrdda22272014-08-04 11:31:54 +0200195 }
196
197 std::vector<int> extract_system_info_impl::get_pwrs()
198 {
ptrkrysike9539c12015-07-06 08:34:22 +0200199 std::vector<int> pwrs;
200 BOOST_FOREACH(chan_info_map::value_type &i, d_c0_channels){
201 pwrs.push_back(i.second.pwr_db);
piotrdda22272014-08-04 11:31:54 +0200202 }
piotrdda22272014-08-04 11:31:54 +0200203 return pwrs;
204 }
ptrkrysike9539c12015-07-06 08:34:22 +0200205 std::vector<int> extract_system_info_impl::get_neighbours(int chan_id)
206 {
207 std::vector<int> neighbour_cells;
208 BOOST_FOREACH(int n, d_c0_channels[chan_id].neighbour_cells){
209 neighbour_cells.push_back(n);
210 }
211 return neighbour_cells;
212 }
piotrdda22272014-08-04 11:31:54 +0200213
214 void extract_system_info_impl::reset()
215 {
ptrkrysike9539c12015-07-06 08:34:22 +0200216 d_c0_channels.clear();
piotrdda22272014-08-04 11:31:54 +0200217 if(!empty_p(pmt::mp("bursts"))){
218 delete_head_blocking(pmt::mp("bursts"));
219 }
ptrkrysike9539c12015-07-06 08:34:22 +0200220 if(!empty_p(pmt::mp("msgs"))){
221 delete_head_blocking(pmt::mp("msgs"));
222 }
piotrdda22272014-08-04 11:31:54 +0200223 }
224
225 extract_system_info::sptr
226 extract_system_info::make()
227 {
228 return gnuradio::get_initial_sptr
229 (new extract_system_info_impl());
230 }
231
232 /*
233 * The private constructor
234 */
235 extract_system_info_impl::extract_system_info_impl()
236 : gr::block("extract_system_info",
237 gr::io_signature::make(0, 0, 0),
238 gr::io_signature::make(0, 0, 0)),
239 after_reset(false)
240 {
241 message_port_register_in(pmt::mp("bursts"));
242 set_msg_handler(pmt::mp("bursts"), boost::bind(&extract_system_info_impl::process_bursts, this, _1));
243 message_port_register_in(pmt::mp("msgs"));
244 set_msg_handler(pmt::mp("msgs"), boost::bind(&extract_system_info_impl::process_sysinfo, this, _1));
245 }
246
247 /*
248 * Our virtual destructor.
249 */
250 extract_system_info_impl::~extract_system_info_impl()
251 {
252 }
253
254
255 } /* namespace gsm */
256} /* namespace gr */
257