owlps/owlps-positioner/src/userinterface.cc

477 lines
16 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comte
* (Université de Franche-Comté), France.
*
* Copyright © Université de Franche-Comté 2007-2012.
*
* Corresponding author: Matteo Cypriani <mcy@lm7.fr>
*
***********************************************************************
*
* This software is governed by the CeCILL license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/or redistribute the software under the terms of the CeCILL
* license as circulated by CEA, CNRS and INRIA at the following URL:
* http://www.cecill.info
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided
* only with a limited warranty and the software's authors, the holder
* of the economic rights, and the successive licensors have only
* limited liability.
*
* In this respect, the user's attention is drawn to the risks
* associated with loading, using, modifying and/or developing or
* reproducing the software by the user in light of its specific status
* of free software, that may mean that it is complicated to manipulate,
* and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users
* are therefore encouraged to load and test the software's suitability
* as regards their requirements in conditions enabling the security of
* their systems and/or data to be ensured and, more generally, to use
* and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL license and that you accept its terms.
*
***********************************************************************
*/
#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 *** */
/* General options */
#define DEFAULT_CONFIG_FILE_NAME \
"/usr/local/etc/owlps/owlps-positioner.conf"
/* Positioning options */
#define DEFAULT_SS_SIMILARITY "mean"
#define DEFAULT_MESHING_GRAIN 0.5
#define DEFAULT_Z_MESHING_GRAIN 1
#define DEFAULT_SMALLEST_SS -99
/* Output options */
#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",
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",
po::value<string>(),
"CSV file to use for topology input (when"
" data-input.topology-medium = CSV).")
("data-input.waypoints-csv-file",
po::value<string>(),
"CSV file to use for waypoints input (when"
" data-input.topology-medium = CSV).")
("data-input.reference-points-medium",
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",
po::value<string>(),
"CSV file to use for input (when input.medium = CSV).")
("input.udp-port",
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, NSS.")
("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.")
("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.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 trilateration"
" 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.smallest-ss",
po::value<int>()->default_value(DEFAULT_SMALLEST_SS),
"Smallest possible value for a received signal strength, in dBm.")
("positioning.ss-similarity",
po::value<string>()->default_value(DEFAULT_SS_SIMILARITY),
"Algorithm to calculate the similarity, in the signal strength space,"
" between two measurements. Allowed: mean, interval, interval2.")
("positioning.generate-reference-points",
po::value<string>()->default_value("false"),
"Generate reference points from the (auto)calibration requests"
" received. Can be 'false', 'mesh' to generate reference points"
" regularly according to the meshing grain specified and the"
" positioning area defined, 'list' to generate reference points"
" from a list (cf. option generated-points-list) or 'both' to"
" generate points both from the list and following the meshing.")
("positioning.generate-multi-packet-reference-points",
po::value<bool>()->default_value(true),
"Generate several packets per reference point by trying to match the"
" packets in the real requests. If false, the SS values of the"
" requests are averaged and only one packet is generated.")
("positioning.generated-meshing-grain-x",
po::value<float>()->default_value(DEFAULT_MESHING_GRAIN),
"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(DEFAULT_MESHING_GRAIN),
"When generating reference points, this distance (in meters) will"
" separate each point to the next in Y.")
("positioning.generated-meshing-grain-z",
po::value<float>()->default_value(DEFAULT_Z_MESHING_GRAIN),
"When generating reference points, this parameter represents the"
" number of the floor. Currently, each increment of Y is a new"
" floor (full 3-D coordinates are not supported yet).")
("positioning.generated-points-list",
po::value<string>(),
"List of reference points to generate, if generate-reference-points"
" is set to 'list' or 'both' (string format:"
" \"(X1;Y1;Z1);(X2;Y2;Z2);...\" or \"X1;Y1;Z1;X2;Y2;Z2;...\").")
("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.calibration-requests-timeout",
po::value<unsigned int>()->default_value(0),
"Maximum age of a calibration request before to delete it"
" (0 = unlimited). See also the \"replay\" option.")
("positioning.unique-calibration-requests",
po::value<bool>()->default_value(true),
"Always delete existing former calibration requests when receiving"
" a new calibration request for an existing reference point.")
("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.nss.average-reference-points",
po::value<bool>()->default_value(false),
"With the NSS algorithm, for a given positioning request, average"
" all the calibration requests associated with a reference point"
" before to compute the SS similarity."
" The default is false, i.e the positioning request is compared"
" directly to each calibration request.")
("positioning.nss.ignore-ap-reference-points",
po::value<bool>()->default_value(false),
"With the NSS algorithm, try to avoid selecting the reference"
" points which are coordinates of an AP.")
;
file_options->add(options) ;
}
void UserInterface::fill_output_options()
{
po::options_description options("Output options") ;
options.add_options()
("output.2d-error",
po::value<bool>()->default_value(false),
"Compute the distance error in two dimensions instead of three"
" dimensions.")
("output.medium,O",
po::value< vector<string> >()->composing(),
"Medium to which the results will be written. You can specify"
" this option more than once."
" Allowed: Terminal, CSV, UDP, TCPEvAAL (EvAAL 2011 format)."
" 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.")
("replay",
po::value<bool>()->default_value(false),
"\"Replay\" mode. With this option enabled, the current time is the"
" emission timestamp of the most recent request; you will want to"
" enable this when reading inputs (requests) off-line to replay"
" scenarios, if time-related options are enabled (e.g."
" \"positioning.calibration-requests-timeout\"). To be useful, this"
" option requires the listeners' clocks to be synchronised.")
;
file_options->add(options) ;
}
void UserInterface::parse_options()
{
parse_command_line() ;
print_information() ;
if (! owl_run)
return ;
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()) ;
}
/**
* If information is printed, owl_run is set to false, to indicate that
* the program should stop.
*/
void UserInterface::print_information() const
{
// Print version if requested
if (Configuration::is_configured("version"))
{
cout
<< "This is OwlPS Positioner, part of the Owl"
<< " Positioning System project.\nVersion: " <<
#ifdef OWLPS_VERSION
OWLPS_VERSION
#else // OWLPS_VERSION
"unknown version"
#endif // OWLPS_VERSION
<< ".\n" ;
owl_run = false ; // Tell main() to exit
}
// Print usage if requested
if (Configuration::is_configured("help"))
{
cout << *cli_options ;
owl_run = false ; // Tell main() to exit
}
}
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()) ;
}