owlps/owlps-positioner/src/posutil.cc

236 lines
6.8 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 "posutil.hh"
#include "posexcept.hh"
#include "stock.hh"
#include "configuration.hh"
#include "measurement.hh"
#include <owlps.h>
#include <cmath>
using namespace std ;
using std::tr1::unordered_map ;
/* *** Maths *** */
double PosUtil::deg2rad(const double &degrees)
{
return degrees / (180.0 / M_PI) ;
}
double PosUtil::rad2deg(const double &radians)
{
return radians * (180.0 / M_PI) ;
}
/* *** Measurements *** */
/**
* APs that have not captured a Request must not have too much weight in
* the computation. Thus, in the measurements lists we compare, we add
* missing APs with a very low SS value.
*
* Both lists can be completed, depending of the APs they contain.
* Both lists must initially contain at least one measurement.
*/
void PosUtil::complete_with_dummy_measurements(
unordered_map<string, Measurement> &measurements1,
unordered_map<string, Measurement> &measurements2)
{
assert(! measurements1.empty()) ;
assert(! measurements2.empty()) ;
Measurement dummy ;
dummy.add_ss(1, Configuration::int_value("positioning.smallest-ss")) ;
for (unordered_map<string, Measurement>::const_iterator i =
measurements1.begin() ; i != measurements1.end() ; ++i)
if (measurements2.find(i->first) == measurements2.end())
{
dummy.set_ap(&Stock::get_ap(i->first)) ;
measurements2[i->first] = dummy ;
}
for (unordered_map<string, Measurement>::const_iterator i =
measurements2.begin() ; i != measurements2.end() ; ++i)
if (measurements1.find(i->first) == measurements1.end())
{
dummy.set_ap(&Stock::get_ap(i->first)) ;
measurements1[i->first] = dummy ;
}
}
/**
* Both lists must have the same size and contain the same keys:
* you should call complete_with_dummy_measurements() before this
* function.
*
* The similarity between the two lists is computed by averaging the
* similarities between the elements of both lists, two by two, calling
* Measurement::similarity().
*/
float PosUtil::similarity(
unordered_map<string, Measurement> &measurements1,
unordered_map<string, Measurement> &measurements2)
{
assert(! measurements1.empty()) ;
assert(measurements1.size() == measurements2.size()) ;
float similarity = 0 ;
for (unordered_map<string, Measurement>::const_iterator i1 =
measurements1.begin() ; i1 != measurements1.end() ; ++i1)
{
unordered_map<string, Measurement>::const_iterator i2 =
measurements2.find(i1->first) ;
assert(i2 != measurements2.end()) ;
similarity += i1->second.similarity(i2->second) ;
}
return similarity / measurements1.size() ;
}
/* *** Wi-Fi *** */
/**
* @param channel A IEEE 802.11 channel or frequency in MHz or in Hz.
* @return The frequency in Hz.
* @throw malformed_input_data if \em channel is not a valid channel or
* frequency value.
*/
unsigned long PosUtil::wifi_channel_to_hz(const unsigned long &channel)
{
switch (channel)
{
case 1:
case OWL_80211_MHZ_CHANNEL_1:
case OWL_80211_HZ_CHANNEL_1:
return OWL_80211_HZ_CHANNEL_1 ;
case 2:
case OWL_80211_MHZ_CHANNEL_2:
case OWL_80211_HZ_CHANNEL_2:
return OWL_80211_HZ_CHANNEL_2 ;
case 3:
case OWL_80211_MHZ_CHANNEL_3:
case OWL_80211_HZ_CHANNEL_3:
return OWL_80211_HZ_CHANNEL_3 ;
case 4:
case OWL_80211_MHZ_CHANNEL_4:
case OWL_80211_HZ_CHANNEL_4:
return OWL_80211_HZ_CHANNEL_4 ;
case 5:
case OWL_80211_MHZ_CHANNEL_5:
case OWL_80211_HZ_CHANNEL_5:
return OWL_80211_HZ_CHANNEL_5 ;
case 6:
case OWL_80211_MHZ_CHANNEL_6:
case OWL_80211_HZ_CHANNEL_6:
return OWL_80211_HZ_CHANNEL_6 ;
case 7:
case OWL_80211_MHZ_CHANNEL_7:
case OWL_80211_HZ_CHANNEL_7:
return OWL_80211_HZ_CHANNEL_7 ;
case 8:
case OWL_80211_MHZ_CHANNEL_8:
case OWL_80211_HZ_CHANNEL_8:
return OWL_80211_HZ_CHANNEL_8 ;
case 9:
case OWL_80211_MHZ_CHANNEL_9:
case OWL_80211_HZ_CHANNEL_9:
return OWL_80211_HZ_CHANNEL_9 ;
case 10:
case OWL_80211_MHZ_CHANNEL_10:
case OWL_80211_HZ_CHANNEL_10:
return OWL_80211_HZ_CHANNEL_10 ;
case 11:
case OWL_80211_MHZ_CHANNEL_11:
case OWL_80211_HZ_CHANNEL_11:
return OWL_80211_HZ_CHANNEL_11 ;
case 12:
case OWL_80211_MHZ_CHANNEL_12:
case OWL_80211_HZ_CHANNEL_12:
return OWL_80211_HZ_CHANNEL_12 ;
case 13:
case OWL_80211_MHZ_CHANNEL_13:
case OWL_80211_HZ_CHANNEL_13:
return OWL_80211_HZ_CHANNEL_13 ;
case 14:
case OWL_80211_MHZ_CHANNEL_14:
case OWL_80211_HZ_CHANNEL_14:
return OWL_80211_HZ_CHANNEL_14 ;
}
// Error: wrong channel value
throw bad_channel(channel) ;
}
/* *** Strings *** */
string PosUtil::int_to_mac(const uint32_t source)
{
uint8_t bytes[6] ;
memset(bytes, 0, 6) ;
for (int i = 0 ; i < 4 ; ++i)
bytes[i+2] = reinterpret_cast<const uint8_t*>(&source)[i] ;
char mac_cstr[OWL_ETHER_ADDR_STRLEN] ;
owl_mac_bytes_to_string_r(bytes, mac_cstr) ;
string mac(mac_cstr) ;
to_upper(mac) ;
return mac ;
}