owlps/owlps-positioning/src/userinterface.cc

362 lines
11 KiB
C++

#include "userinterface.hh"
#include "configuration.hh"
#include <owlps.h>
#include <iostream>
#include <fstream>
#include <boost/program_options.hpp>
using namespace std ;
namespace po = boost::program_options ;
/* *** Default value definitions *** */
#define DEFAULT_CONFIG_FILE_NAME \
"/usr/local/etc/owlps/owlps-positioning.conf"
#define DEFAULT_TCPEVAAL_HOST "127.0.0.1"
#define DEFAULT_TCPEVAAL_PORT 4444
/* *** Constructors *** */
/**
* \em argc and \em argv are (normally) the ones passed to the main()
* function.
* @param argc Number of arguments passed to the program.
* @param argv Values of the arguments.
*/
UserInterface::UserInterface(const int argc, char **argv)
{
assert(argv) ;
cli_argument_values = argv ;
cli_argument_count = argc ;
try
{
cli_options = new po::options_description("General options") ;
file_options = new po::options_description("Parameters") ;
}
catch (bad_alloc &e)
{
throw ;
}
fill_options() ;
parse_options() ;
}
UserInterface::~UserInterface()
{
delete cli_options ;
delete file_options ;
}
/* *** Operations *** */
void UserInterface::fill_options()
{
fill_cli_options() ;
fill_file_options() ;
// File options are also accepted on the command line
cli_options->add(*file_options) ;
}
void UserInterface::fill_cli_options()
{
cli_options->add_options()
("help,h", "Print help.")
("version,V", "Print version information.")
("config-file,f", po::value<string>()
->default_value(DEFAULT_CONFIG_FILE_NAME),
"Alternative configuration file.")
("verbose,v", "Verbose mode.")
; // End of options
}
void UserInterface::fill_file_options()
{
fill_data_input_options() ;
fill_input_options() ;
fill_log_options() ;
fill_positioning_options() ;
fill_output_options() ;
fill_misc_options() ;
}
void UserInterface::fill_data_input_options()
{
po::options_description options("Data input options") ;
options.add_options()
("data-input.ap-medium", po::value< vector<string> >()
->composing(),
"Medium from which access points are read. You can specify this"
" option more than once. Allowed: CSV.")
("data-input.ap-csv-file", po::value<string>(),
"CSV file to use for access points input (when"
" data-input.ap-medium = CSV).")
("data-input.mobile-medium", po::value< vector<string> >()
->composing(),
"Medium from which mobiles are read. You can specify this"
" option more than once. Allowed: CSV.")
("data-input.mobile-csv-file", po::value<string>(),
"CSV file to use for mobiles input (when"
" data-input.mobile-medium = CSV).")
("data-input.topology-medium,T", po::value< vector<string> >()
->composing(),
"Medium from which topology (buildings, areas and waypoints) is"
" read. You can specify this option more than once. Allowed: CSV.")
("data-input.areas-csv-file,A", po::value<string>(),
"CSV file to use for topology input (when"
" data-input.topology-medium = CSV).")
("data-input.waypoints-csv-file,W", po::value<string>(),
"CSV file to use for waypoints input (when"
" data-input.topology-medium = CSV).")
("data-input.reference-points-medium,R", po::value< vector<string> >()
->composing(),
"Medium from which reference points are read. You can specify this"
" option more than once. Allowed: CSV.")
("data-input.reference-points-csv-file", po::value<string>(),
"CSV file to use for reference points input (when"
" data-input.reference-points-medium = CSV).")
;
file_options->add(options) ;
}
void UserInterface::fill_input_options()
{
po::options_description options("Input options") ;
options.add_options()
("input.medium,I", po::value<string>(),
"Medium from which requests are read. Allowed: CSV, UDP.")
("input.csv-file,C", po::value<string>(),
"CSV file to use for input (when input.medium = CSV).")
("input.udp-port,p", po::value<int>()
->default_value(OWL_DEFAULT_AGGREGATION_PORT),
"Port on which the UDP socket listens (when input.medium = UDP).")
;
file_options->add(options) ;
}
void UserInterface::fill_log_options()
{
po::options_description options("Log options") ;
options.add_options()
("log.medium,L", po::value< vector<string> >()->composing(),
"Medium to which the requests will be logged. You can specify"
" this option more than once. Allowed: none, CSV. The `none`"
" value completely disables logging.")
("log.csv-file", po::value<string>(),
"CSV file to use for logging (when log.medium = CSV).")
;
file_options->add(options) ;
}
void UserInterface::fill_positioning_options()
{
po::options_description options("Positioning options") ;
options.add_options()
("positioning.algorithm,a", po::value< vector<string> >()->composing(),
"Algorithms used to compute positions. You can specify"
"this option more than once (but at least once). Allowed: Real, FBCM,"
" FRBHMBasic, InterlinkNetworks, RADAR.")
("positioning.area-start", po::value<string>(),
"Coordinates of the first point of the deployment area; this is"
" used to delimit the area in which the MinMax multilateration"
" method tests points and in which the reference points are"
" generated, if the corresponding options are activated"
" (string format: \"X;Y;Z\").")
("positioning.area-stop", po::value<string>(),
"Coordinates of the last point of the deployment area"
" (string format: \"X;Y;Z\").")
("positioning.radar-average-reference-points",
po::value<bool>()->default_value(false),
"With the RADAR algorithm, for a given positioning request, average"
" all the calibration requests associated with a reference point"
" before to compute the SS distance."
" The default is false, i.e the positioning request is compared"
" directly to each calibration request.")
("positioning.generate-reference-points",
po::value<bool>()->default_value(false),
"Generate reference points from the (auto)calibration requests"
" received.")
("positioning.generated-meshing-grain-x",
po::value<float>()->default_value(0.5),
"When generating reference points, this distance (in meters) will"
" separate each point to the next in X.")
("positioning.generated-meshing-grain-y",
po::value<float>()->default_value(0.5),
"When generating reference points, this distance (in meters) will"
" separate each point to the next in Y.")
("positioning.radar-ignore-ap-reference-points",
po::value<bool>()->default_value(false),
"With the RADAR algorithm, try to avoid selecting the reference"
" points which are coordinates of an AP.")
("positioning.accept-new-calibration-requests",
po::value<bool>()->default_value(false),
"Add the calibration requests received during the run-time to"
" the calibration requests' list; this is required for the"
" self-calibration. If unactivated, the calibration requests"
" are handled as positioning requests (default is unactivated,"
" for security purposes).")
("positioning.position-calibration-requests",
po::value<bool>()->default_value(false),
"When accept-new-calibration-requests is activated, allow the"
" calibration requests to be positioned as normal requests."
" The default is to add them to the calibration requests' list"
" without position them.")
("positioning.accept-new-aps",
po::value<bool>()->default_value(false),
"When receiving requests, add unknown APs (APs which are not"
" declared in the APs' configuration file) to the APs' list"
" (default is false, for security purposes).")
("positioning.update-ap-coordinates-online",
po::value<bool>()->default_value(false),
"Allow AP's coordinates to be updated when a calibration request"
" with new coordinates is received from the AP (default is false,"
" for security purposes).")
("positioning.accept-new-mobiles",
po::value<bool>()->default_value(false),
"When receiving requests, add unknown mobiles (mobiles which are not"
" declared in the mobiles' configuration file) to the mobiles' list.")
;
file_options->add(options) ;
}
void UserInterface::fill_output_options()
{
po::options_description options("Output options") ;
options.add_options()
("output.medium,O", po::value< vector<string> >()->composing(),
"Medium to which the results will be wrote. You can specify"
" this option more than once."
" Allowed: Terminal, CSV, UDP, TCPEvAAL."
" If this option is absent, the results are printed on the terminal.")
("output.csv-file", po::value<string>(),
"CSV file to use for output (when output.medium = CSV).")
("output.udp-host", po::value<string>(),
"Host to which the UDP data is sent (when output.medium = UDP).")
("output.udp-port", po::value<int>()
->default_value(OWL_DEFAULT_RESULT_PORT),
"Port on which the UDP data is sent (when output.medium = UDP).")
("output.tcpevaal-host", po::value<string>()
->default_value(DEFAULT_TCPEVAAL_HOST),
"Host to which the TCP data is sent (when output.medium = TCPEvAAL).")
("output.tcpevaal-port", po::value<int>()
->default_value(DEFAULT_TCPEVAAL_PORT),
"Port on which the TCP data is sent (when output.medium = TCPEvAAL).")
;
file_options->add(options) ;
}
void UserInterface::fill_misc_options()
{
po::options_description options("Miscellaneous options") ;
options.add_options()
("flush-output-files", po::value<bool>()->default_value(true),
"Flush output text files after each line.")
;
file_options->add(options) ;
}
void UserInterface::parse_options()
{
parse_command_line() ;
print_information_and_exit_if_requested() ;
parse_file() ;
po::notify(Configuration::getw_configuration()) ;
}
void UserInterface::parse_command_line() const
{
po::store(po::parse_command_line(cli_argument_count,
cli_argument_values,
*cli_options),
Configuration::getw_configuration()) ;
}
void UserInterface::print_information_and_exit_if_requested() const
{
bool exit_program = false ;
// Print version if requested
if (Configuration::is_configured("version"))
{
cout
<< "This is OwlPS Positioning, part of the Open Wireless"
<< " Positioning System project.\nVersion: " <<
#ifdef OWLPS_VERSION
OWLPS_VERSION
#else // OWLPS_VERSION
"unknown version"
#endif // OWLPS_VERSION
<< ".\n" ;
exit_program = true ;
}
// Print usage if requested
if (Configuration::is_configured("help"))
{
cout << *cli_options ;
exit_program = true ;
}
if (exit_program)
exit(0) ;
}
void UserInterface::parse_file() const
{
string config_file_name = Configuration::string_value("config-file") ;
ifstream config_file(config_file_name.c_str()) ;
if (! config_file)
{
cerr
<< "Warning! Error opening input configuration file « "
<< config_file_name
<< " »! Using command line and default values…"
<< endl ;
return ;
}
po::store(po::parse_config_file(config_file, *file_options),
Configuration::getw_configuration()) ;
}