Tutorials/k0SDR/Tutorial04a: spectrum_console.cpp

File spectrum_console.cpp, 5.4 KB (added by nilanjan, 5 years ago)
Line 
1// Compile syntax:
2// > g++ spectrum_console.cpp -lboost_program_options-mt -loml2 -o spectrum_console
3
4//
5// Copyright 2010-2011 Ettus Research LLC
6//
7// This program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
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.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19//
20
21#include <boost/program_options.hpp>
22#include <boost/thread/thread.hpp> //gets time
23//#include <boost/lexical_cast.hpp>
24#include <boost/format.hpp>
25#include <boost/algorithm/string.hpp>
26#include <iostream>
27#include <fstream>
28#include "fcntl.h"
29#include "unistd.h"
30//#include <time.h>
31
32namespace po = boost::program_options;
33
34
35
36bool xml_getElement(std::string element, std::string str, std::string &element_value)
37{
38
39  std::string tag;
40  size_t start_pos, end_pos;
41
42  tag = "<"+element+">";
43  start_pos = str.find(tag);
44  if (start_pos == std::string::npos)
45    return false;
46
47  start_pos += tag.length();
48   
49  tag = "</"+element+">";
50  end_pos = str.find(tag);
51 
52  element_value.assign (str, start_pos, end_pos-start_pos);
53  return true;
54 
55}
56
57int main(int argc, char *argv[]){
58
59  //variables to be set by po
60  std::string ant, subdev, ref, wave_type, freq, rate, oml_format;
61  std::string gain, rx_node_name, num_bins, time_duration;
62  double bw, wave_freq;
63
64  FILE *fp_txusrp, *fp_rxusrp[20];
65  char line[8192];
66  std::string tx_cmd, rx_cmd;
67
68  //setup the program options
69  po::options_description desc("Allowed options");
70  desc.add_options()
71    ("help", "help message")
72    ("node", po::value<std::string>(&rx_node_name)->default_value("node18-18"), "specify nodex-y. Use comma-delimited list for multiple nodes (no space): ex. node13-13,node18-18")
73    ("freq", po::value<std::string>(&freq)->default_value("5e9"), "RF center frequency (CF) in Hz. Use a single value for same CF for all nodes OR a comma-delimited list (no space) to specify unique CF per node: 2.43e9,5.2e9")
74
75    ("rate", po::value<std::string>(&rate)->default_value("16e6"), "rate of incoming samples (sps)")
76    ("gain", po::value<std::string>(&gain)->default_value("15"), "gain for RF gain in dBm")
77    //("ant", po::value<std::string>(&ant), "daughterboard antenna selection")
78    //("subdev", po::value<std::string>(&subdev), "daughterboard subdevice specification")
79    //("bw", po::value<double>(&bw), "daughterboard IF filter bandwidth in Hz")
80    ("time", po::value<std::string>(&time_duration)->default_value("9999999"), "time duration to run for in seconds")
81    ("oml", po::value<std::string>(&oml_format)->default_value("file"), "file - record values to oml text file on the node, oml:3003 - record values to oml server")
82    ("num-bins", po::value<std::string>(&num_bins)->default_value("64"), "the number of FFT points")
83    ;
84   
85  po::variables_map vm;
86  po::store(po::parse_command_line(argc, argv, desc), vm);
87  po::notify(vm);
88 
89  //print the help message
90  if (vm.count("help")){
91    std::cout << boost::format("%s") % desc << std::endl;
92    return ~0;
93  }
94
95  // Split rx node names and freq into list
96  std::vector<std::string> node_list;
97  std::vector<std::string> freq_list;
98  boost::split(node_list, rx_node_name, boost::is_any_of(",") );
99  boost::split(freq_list, freq,         boost::is_any_of(",") );
100
101  // Error check node and freq list
102  if (freq_list.size() > node_list.size() )
103  {
104    std::cout << "ERR: frequency list is greater than node list" << std::endl;
105    return ~0;
106  }
107
108  if (freq_list.size() == 1)
109  {
110    freq_list.resize( node_list.size() );
111    for (int i = 0 ; i < freq_list.size(); freq_list.at(i) = freq , ++i );
112  } 
113
114  //for (int i=0; i < node_list.size(); std::cout << node_list.at(i) << std::endl, i++)
115
116  for (int i = 0 ; i < node_list.size() ; i++)
117  {
118    rx_node_name = node_list.at(i);
119    freq = freq_list.at(i);
120    rx_cmd = " ssh root@" + rx_node_name + " \"/root/uhd/host/build/examples/spectrum"; 
121    rx_cmd += " --freq " + freq;
122    rx_cmd += " --rate " + rate;
123    rx_cmd += " --num-bins " +  num_bins;
124    rx_cmd += " --gain " + gain; 
125    rx_cmd += " --time " + time_duration;
126    rx_cmd += " --oml " + oml_format;
127    rx_cmd += " \"";
128   
129    //printf("%s\n", rx_cmd.c_str());
130
131    /* Open the command for reading. */
132    fp_rxusrp[i] = popen( rx_cmd.c_str(), "r");
133    if (fp_rxusrp == NULL) {
134      std::cout << "Failed to run rx_peakpower\n";
135      exit;
136    }
137    //fcntl(fileno(fp_rxusrp[i]), F_SETFL, O_NONBLOCK);
138  } // end for-loop
139
140  // Give application some time to set up
141  sleep(3);
142 
143  /* Read the output a line at a time - output it. */
144  while ( fgets(line, sizeof(line)-1, fp_rxusrp[0]) != NULL ) {
145    std::string str(line);
146    std::cout << "["<<rx_node_name<<"]" << str;
147  }
148
149
150  for (int i = 0 ; i < node_list.size() ; i++)
151  {
152    pclose(fp_rxusrp[i]); 
153  }
154 
155
156  std::cout << std::endl << "Done!" << std::endl << std::endl;
157 
158  return 0;
159}
160
161