owlps/owlps-positioning/src/posutil.cc

191 lines
4.8 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS).
* OwlPS is a project of the University of Franche-Comté
* (Université de Franche-Comté), France.
*/
#include "posutil.hh"
#include "posexcept.hh"
#include "stock.hh"
#include "measurement.hh"
#include <owlps.h>
#include <cmath>
using namespace std ;
using std::tr1::unordered_map ;
/* *** Maths *** */
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(-98) ; // FIXME: should be the smallest possible value
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
* compute_ss_square_distance().
*/
float PosUtil::ss_square_distance(
unordered_map<string, Measurement> &measurements1,
unordered_map<string, Measurement> &measurements2)
{
assert(measurements1.size() == measurements2.size()) ;
float distance = 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()) ;
distance += i1->second.ss_square_distance(
i2->second) ;
}
return distance ;
}
/* *** 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 ;
}