owlps/owlps-positioner/src/inputmedium.cc

207 lines
6.1 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 "inputmedium.hh"
#include "calibrationrequest.hh"
#include "stock.hh"
#include "configuration.hh"
#include "posexcept.hh"
using namespace std ;
/* *** Constructors *** */
InputMedium::InputMedium():
current_line_nb(0)
{
current_request = new Request() ;
}
InputMedium::~InputMedium()
{
delete current_request ;
}
/* *** Operations *** */
/**
* Reads a Request, increments current_line_nb, updates #current_request
* and returns it.
*
* The input medium should be ready to be read before precessing
* requests; otherwise, #current_request is \link Request::clear()
* cleared\endlink and a blank Request is returned.
*
* @return The Request read, or an empty Request in case of error or
* EOF (note that when casted to bool, an empty Request is \em false,
* see Request::operator bool()).
*/
const Request& InputMedium::get_next_request()
{
clear_current_request() ;
if (! fill_current_request())
{
clear_current_request() ;
return *current_request ;
}
current_request->received_now() ;
return *current_request ;
}
void InputMedium::clear_current_request()
{
if (dynamic_cast<CalibrationRequest*>(current_request) == NULL)
current_request->clear() ;
else
{
delete current_request ;
current_request = NULL ;
current_request = new Request() ;
}
}
/**
* The \em position argument can be changed by this function.
*/
void InputMedium::
fill_calibration_request_data(const string &mac_mobile, Point3D &position,
const Direction &direction, uint8_t type)
{
// We set the real coordinates (if found) for non-calibration requests
if (type != OWL_REQUEST_CALIBRATION &&
type != OWL_REQUEST_AUTOCALIBRATION)
{
if (position)
current_request->set_real_position(position) ;
return ;
}
if (position)
{
// Update the AP's coordinates if allowed (and if the mobile is
// an AP, of course)
if (Configuration::
bool_value("positioning.update-ap-coordinates-online"))
{
if (type == OWL_REQUEST_AUTOCALIBRATION &&
Configuration::bool_value("positioning.accept-new-aps"))
{
AccessPoint &transmitter =
const_cast<AccessPoint&>(
Stock::find_create_ap(mac_mobile)) ;
transmitter.set_coordinates(position) ;
}
else
{
try
{
AccessPoint &transmitter =
const_cast<AccessPoint&>(Stock::get_ap(mac_mobile)) ;
transmitter.set_coordinates(position) ;
}
catch (element_not_found &e)
{
// The mobile is not an AP or the AP does not exist
}
}
}
}
else if (type == OWL_REQUEST_AUTOCALIBRATION)
{
// If an autocalibration request does not contain the coordinates
// of the AP, we use the current coordinates of the AP as
// ReferencePoint.
try
{
AccessPoint &transmitter =
const_cast<AccessPoint&>(Stock::get_ap(mac_mobile)) ;
position = transmitter.get_coordinates() ;
}
catch (element_not_found &e)
{
// The mobile is not an AP or the AP does not exist
}
}
const ReferencePoint &reference_point =
Stock::find_create_reference_point(position) ;
current_request_to_calibration_request(
&reference_point, direction, type) ;
}
void InputMedium::
current_request_to_calibration_request(
const ReferencePoint *const reference_point,
const Direction &direction,
const uint_fast8_t request_type)
{
CalibrationRequest *calibration_request =
dynamic_cast<CalibrationRequest*>(current_request) ;
if (calibration_request != NULL)
{
calibration_request->set_reference_point(reference_point) ;
calibration_request->set_direction(direction) ;
calibration_request->set_type(request_type) ;
return ;
}
Request *tmp = current_request ;
current_request = NULL ;
current_request =
new CalibrationRequest(*tmp,
const_cast<ReferencePoint*>(reference_point),
direction, request_type) ;
delete tmp ;
}