-
Notifications
You must be signed in to change notification settings - Fork 15
/
smartnet.cc
616 lines (461 loc) · 15.7 KB
/
smartnet.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* Copyright 2011 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
/*
* GNU Radio C++ example creating dial tone
* ("the simplest thing that could possibly work")
*
* Send a tone each to the left and right channels of stereo audio
* output and let the user's brain sum them.
*
* GNU Radio makes extensive use of Boost shared pointers. Signal processing
* blocks are typically created by calling a "make" factory function, which
* returns an instance of the block as a typedef'd shared pointer that can
* be used in any way a regular pointer can. Shared pointers created this way
* keep track of their memory and free it at the right time, so the user
* doesn't need to worry about it (really).
*
*/
// Include header files for each block used in flowgraph
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm> // copy
#include <iterator>
#include <cstddef>
#include "logging_receiver_dsd.h"
#include "smartnet_crc.h"
#include "smartnet_deinterleave.h"
#include "talkgroup.h"
#include <osmosdr_source_c.h>
#include <osmosdr_sink_c.h>
#include <boost/program_options.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/tokenizer.hpp>
#include <filter/freq_xlating_fir_filter_ccf.h>
#include <filter/firdes.h>
#include <digital_fll_band_edge_cc.h>
#include <digital_clock_recovery_mm_ff.h>
#include <digital_binary_slicer_fb.h>
#include <gr_firdes.h>
#include <gr_fir_filter_ccf.h>
#include <gr_pll_freqdet_cf.h>
#include <gr_sig_source_f.h>
#include <gr_sig_source_c.h>
#include <gr_audio_sink.h>
#include <gr_correlate_access_code_tag_bb.h>
#include <gr_msg_queue.h>
#include <gr_message.h>
#include <gr_file_sink.h>
#include <gr_null_sink.h>
#include <gr_complex.h>
#include <gr_fir_filter_ccf.h>
#include <gr_top_block.h>
#include <gr_multiply_cc.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <ncurses.h>
#include <menu.h>
namespace po = boost::program_options;
using namespace std;
int lastcmd = 0;
long lastaddress = 0;
int thread_num=0;
double center_freq;
bool console = false;
gr_top_block_sptr tb;
osmosdr_source_c_sptr src;
vector<log_dsd_sptr> loggers;
unsigned int max_loggers = 6;
unsigned int num_loggers = 0;
vector<log_dsd_sptr> active_loggers;
vector<Talkgroup *> talkgroups;
vector<Talkgroup *> active_tg;
char **menu_choices;
char status[150];
ITEM **tg_menu_items;
WINDOW *active_tg_win;
WINDOW *tg_menu_win;
WINDOW *status_win;
MENU *tg_menu;
volatile sig_atomic_t exit_flag = 0;
void update_active_tg_win() {
werase(active_tg_win);
box(active_tg_win, 0, 0);
int i=0;
for(std::vector<Talkgroup *>::iterator it = active_tg.begin(); it != active_tg.end(); ++it) {
Talkgroup *tg = (Talkgroup *) *it;
/*s = tg->menu_string();
c = (char *) malloc((s.size() + 1) * sizeof(char));
//strncpy(c, s.c_str(), s.size());
//c[s.size()] = '\0';
strcpy(c, s.c_str());
*/
mvwprintw(active_tg_win,i*2+1,2,"TG: %s", tg->alpha_tag.c_str());
mvwprintw(active_tg_win,i*2+2,6,"%s ", tg->description.c_str());
mvwprintw(active_tg_win,i*2+1,40,"Num: %5lu", tg->number);
mvwprintw(active_tg_win,i*2+2,40,"Tag: %s", tg->tag.c_str());
mvwprintw(active_tg_win,i*2+2,60,"Group: %s", tg->group.c_str());
i++;
}
wrefresh(active_tg_win);
}
void update_status_win(char *c) {
wclear(status_win);
//wattron(status_win,A_REVERSE);
mvwprintw(status_win,0,2,"%s",c);
wrefresh(status_win);
}
void create_status_win() {
int startx, starty, width, height;
height = 1;
width = COLS;
starty = LINES-1;
startx = 0;
status_win = newwin(height, width, starty, startx);
}
void create_active_tg_win() {
int startx, starty, width, height;
height = 20;
width = COLS;
starty = 0;
startx = 0;
active_tg_win = newwin(height, width, starty, startx);
box(active_tg_win, 0, 0);
wrefresh(active_tg_win);
}
void create_tg_menu() {
std::string s;
int n_choices, i;
char *c;
//printw("%s\n", s.c_str());
menu_choices = new char*[talkgroups.size()];
//menu_choices = malloc(talkgroups.size(), sizeof(char *));
i=0;
for(std::vector<Talkgroup *>::iterator it = talkgroups.begin(); it != talkgroups.end(); ++it) {
Talkgroup *tg = (Talkgroup *) *it;
s = tg->menu_string();
c = (char *) malloc((s.size() + 1) * sizeof(char));
//strncpy(c, s.c_str(), s.size());
//c[s.size()] = '\0';
strcpy(c, s.c_str());
menu_choices[i] = c;
i++;
}
n_choices = talkgroups.size(); //ARRAY_SIZE(menu_choices);
tg_menu_items = (ITEM **) calloc(n_choices + 1, sizeof(ITEM *));
for (i=0; i < n_choices; ++i) {
tg_menu_items[i] = new_item(menu_choices[i], menu_choices[i]);
set_item_userptr(tg_menu_items[i], (void *) talkgroups[i]);
}
tg_menu = new_menu((ITEM **) tg_menu_items);
tg_menu_win = newwin(LINES - 11, COLS, 10, 0);
keypad(tg_menu_win, TRUE);
set_menu_win(tg_menu, tg_menu_win);
set_menu_sub(tg_menu, derwin(tg_menu_win, LINES - 15, COLS - 4, 2, 2));
set_menu_format(tg_menu, LINES - 14 , 1);
//set_menu_mark(tg_menu, " * ");
box(tg_menu_win,0,0);
menu_opts_off(tg_menu, O_SHOWDESC | O_ONEVALUE);
//menu_opts_off(tg_menu, O_ONEVALUE);
post_menu(tg_menu);
}
void exit_interupt(int sig){ // can be called asynchronously
exit_flag = 1; // set flag
}
void init_loggers(int num, float center_freq) {
// static loggers
for (int i = 0; i < num; i++) {
log_dsd_sptr log = make_log_dsd( center_freq, center_freq, 0, i);
loggers.push_back(log);
tb->connect(src, 0, log, 0);
}
}
float getfreq(int cmd) {
float freq;
if (cmd < 0x1b8) {
freq = float(cmd * 0.025 + 851.0125);
} else if (cmd < 0x230) {
freq = float(cmd * 0.025 + 851.0125 - 10.9875);
} else {
freq = 0;
}
return freq;
}
void parse_file(string filename) {
ifstream in(filename.c_str());
if (!in.is_open()) return;
boost::char_separator<char> sep(",");
typedef boost::tokenizer< boost::char_separator<char> > t_tokenizer;
vector< string > vec;
string line;
while (getline(in,line))
{
t_tokenizer tok(line, sep);
//Tokenizer tok(line);
vec.assign(tok.begin(),tok.end());
if (vec.size() < 8) continue;
Talkgroup *tg = new Talkgroup(atoi( vec[0].c_str()), vec[2].at(0),vec[3].c_str(),vec[4].c_str(),vec[5].c_str() ,vec[6].c_str(),atoi(vec[7].c_str()) );
talkgroups.push_back(tg);
}
}
void parse_status(int command, int address, int groupflag) {
int Value = address << 1 | (groupflag ? (1) : 0);
int GroupTimeout = Value & (0x1f);
Value >>= 5;
int ConnecTimeout = Value & (0x1f);
Value >>= 5;
int DispatchTimeout = Value & 0xf;
Value >>= 4;
int Power = Value & 1;
Value >>= 1;
int OpCode = Value;
if (console) {
sprintf(status, "Status: %d \tPower: %d \tDispatchTimeout: %d \tConnectTimeOut: %d \tGroupTimeOut: %d", OpCode, Power, DispatchTimeout, ConnecTimeout, GroupTimeout);
update_status_win(status);
}
}
float parse_message(string s) {
float retfreq = 0;
bool rxfound = false;
std::vector<std::string> x;
boost::split(x, s, boost::is_any_of(","), boost::token_compress_on);
long address = atoi( x[0].c_str() ) & 0xFFF0;
int groupflag = atoi( x[1].c_str() );
int command = atoi( x[2].c_str() );
char shell_command[200];
x.clear();
vector<string>().swap(x);
if (command < 0x2d0) {
if ( lastcmd == 0x308) {
// Channel Grant
if ( (address != 56016) && (address != 8176)) {
retfreq = getfreq(command);
//std::cout << "Channel Grant: " << lastaddress << " Address: " << address << " Command: " << command << " Last Command: " << lastcmd << std::endl;
}
} else {
// Call continuation
if ( (address != 56016) && (address != 8176)) {
retfreq = getfreq(command);
//std::cout << "Call Continue: " << lastaddress << " Address: " << address << " Command: " << command << " Last Command: " << lastcmd << std::endl;
}
}
}
if (command == 0x03c0) {
//parse_status(command, address,groupflag);
}
if (retfreq) {
for(vector<log_dsd_sptr>::iterator it = loggers.begin(); it != loggers.end();it++) {
log_dsd_sptr rx = *it;
if (rx->is_active() && (rx->lastupdate() > 4.0)) {
if (console) {
for(std::vector<Talkgroup *>::iterator tg_it = active_tg.begin(); tg_it != active_tg.end(); ++tg_it) {
Talkgroup *tg = (Talkgroup *) *tg_it;
if (tg->number == rx->get_talkgroup()) {
active_tg.erase(tg_it);
break;
}
}
update_active_tg_win();
}
sprintf(shell_command,"./encode-upload.sh %s > /dev/null 2>&1 &", rx->get_filename());
rx->deactivate();
num_loggers--;
system(shell_command);
}
}
for(vector<log_dsd_sptr>::iterator it = loggers.begin(); it != loggers.end(); ++it) {
log_dsd_sptr rx = *it;
if (rx->is_active())
{
if (rx->get_talkgroup() == address) {
if (rx->get_freq() != retfreq) {
if (console) {
sprintf(status, "Retuning TG: %Ld \tOld Freq: %g \tNew Freq: %g \t TG last update %d seconds ago",rx->get_talkgroup(),rx->get_freq(),retfreq,rx->lastupdate());
update_status_win(status);
}
rx->tune_offset(retfreq);
}
rx->unmute();
rxfound = true;
} else {
if (rx->get_freq() == retfreq) {
if (console) {
sprintf(status, "%g \t- Freq overlap: Existing TG %d \tNew TG %d \tTG Updated %d seconds ago",rx->get_freq(),rx->get_talkgroup(),address,rx->lastupdate());
update_status_win(status);
}
//cout << " !! Someone else is on my Channel - My TG: "<< rx->get_talkgroup() << " Freq: " <<rx->get_freq() << " Intruding TG: " << address << endl;
rx->mute();
}
}
}
}
if ((!rxfound)){
Talkgroup *rx_talkgroup = NULL;
bool record_tg = false;
for(std::vector<Talkgroup *>::iterator it = talkgroups.begin(); it != talkgroups.end(); ++it) {
Talkgroup *tg = (Talkgroup *) *it;
if (tg->number == address) {
rx_talkgroup = tg;
break;
}
}
if (rx_talkgroup) {
if (((rx_talkgroup->get_priority() == 1) && (num_loggers < max_loggers)) ||
((rx_talkgroup->get_priority() == 2) && (num_loggers < 4 )) ||
((rx_talkgroup->get_priority() == 3) && (num_loggers < 2 ))) {
record_tg = true;
if (console) {
active_tg.push_back(rx_talkgroup);
update_active_tg_win();
}
} else {
record_tg = false;
}
}
if (record_tg){
for(vector<log_dsd_sptr>::iterator it = loggers.begin(); it != loggers.end();it++) {
log_dsd_sptr rx = *it;
if (!rx->is_active())
{
num_loggers++;
rx->activate(retfreq, address,num_loggers);
break;
}
}
}
}
}
lastaddress = address;
lastcmd = command;
return retfreq;
}
int main(int argc, char **argv)
{
std::string device_addr;
double samp_rate, chan_freq, error;
int if_gain, bb_gain, rf_gain;
//setup the program options
po::options_description desc("Allowed options");
desc.add_options()
("help", "help message")
("arg", po::value<std::string>(&device_addr)->default_value(""), "the device arguments in string format")
("rate", po::value<double>(&samp_rate)->default_value(1e6), "the sample rate in samples per second")
("center", po::value<double>(¢er_freq)->default_value(10e6), "the center frequency in Hz")
("error", po::value<double>(&error)->default_value(0), "the Error in frequency in Hz")
("freq", po::value<double>(&chan_freq)->default_value(10e6), "the frequency in Hz of the trunking channel")
("rfgain", po::value<int>(&rf_gain)->default_value(14), "RF Gain")
("bbgain", po::value<int>(&bb_gain)->default_value(25), "BB Gain")
("ifgain", po::value<int>(&if_gain)->default_value(25), "IF Gain")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
//print the help message
if (vm.count("help")){
std::cout
<< boost::format("SmartNet Trunking Reciever %s") % desc << std::endl
<< "The tags sink demo block will print USRP source time stamps." << std::endl
<< "The tags source demo block will send bursts to the USRP sink." << std::endl
<< "Look at the USRP output on a scope to see the timed bursts." << std::endl
<< std::endl;
return ~0;
}
signal(SIGINT, exit_interupt);
parse_file("ChanList.csv");
tb = gr_make_top_block("smartnet");
src = osmosdr_make_source_c();
cout << "Setting sample rate to: " << samp_rate << endl;
src->set_sample_rate(samp_rate);
cout << "Tunning to " << center_freq - error << "hz" << endl;
src->set_center_freq(center_freq - error,0);
cout << "Setting RF gain to " << rf_gain << endl;
cout << "Setting BB gain to " << bb_gain << endl;
cout << "Setting IF gain to " << if_gain << endl;
src->set_gain(rf_gain);
src->set_if_gain(if_gain);
src->set_bb_gain(bb_gain);
float samples_per_second = samp_rate;
float syms_per_sec = 3600;
float gain_mu = 0.01;
float mu=0.5;
float omega_relative_limit = 0.3;
float offset = center_freq - chan_freq;
float clockrec_oversample = 3;
int decim = int(samples_per_second / (syms_per_sec * clockrec_oversample));
float sps = samples_per_second/decim/syms_per_sec;
const double pi = boost::math::constants::pi<double>();
cout << "Control channel offset: " << offset << endl;
cout << "Decim: " << decim << endl;
cout << "Samples per symbol: " << sps << endl;
init_loggers(max_loggers, center_freq);
gr_msg_queue_sptr queue = gr_make_msg_queue();
gr_freq_xlating_fir_filter_ccf_sptr prefilter = gr_make_freq_xlating_fir_filter_ccf(decim,
gr_firdes::low_pass(1, samp_rate, 10000, 12000),
offset,
samp_rate);
gr_pll_freqdet_cf_sptr pll_demod = gr_make_pll_freqdet_cf(2.0 / clockrec_oversample, 2*pi/clockrec_oversample,
-2*pi/clockrec_oversample);
digital_fll_band_edge_cc_sptr carriertrack = digital_make_fll_band_edge_cc(sps, 0.6, 64, 0.35);
digital_clock_recovery_mm_ff_sptr softbits = digital_make_clock_recovery_mm_ff(sps, 0.25 * gain_mu * gain_mu, mu, gain_mu, omega_relative_limit);
digital_binary_slicer_fb_sptr slicer = digital_make_binary_slicer_fb();
gr_correlate_access_code_tag_bb_sptr start_correlator = gr_make_correlate_access_code_tag_bb("10101100",0,"smartnet_preamble");
smartnet_deinterleave_sptr deinterleave = smartnet_make_deinterleave();
smartnet_crc_sptr crc = smartnet_make_crc(queue);
/* gr_null_sink_sptr nullsink = gr_make_null_sink(sizeof(u_char));
tb->connect(deinterleave,0,nullsink,0);*/
tb->connect(src,0,prefilter,0);
tb->connect(prefilter,0,carriertrack,0);
tb->connect(carriertrack, 0, pll_demod, 0);
tb->connect(pll_demod, 0, softbits, 0);
tb->connect(softbits, 0, slicer, 0);
tb->connect(slicer, 0, start_correlator, 0);
tb->connect(start_correlator, 0, deinterleave, 0);
tb->connect(deinterleave, 0, crc, 0);
tb->start();
parse_file("ChanList.csv");
if (console) {
initscr();
cbreak();
noecho();
nodelay(active_tg_win,TRUE);
create_active_tg_win();
create_status_win();
}
gr_message_sptr msg;
while (1) {
if(exit_flag){ // my action when signal set it 1
printf("\n Signal caught!\n");
tb->stop();
endwin();
return 0;
}
msg = queue->delete_head();
parse_message(msg->to_string());
msg.reset();
//delete(sentence);
}
endwin();
// Exit normally.
return 0;
}