2009-12-11 13:52:21 +01:00
|
|
|
#include "posutil.hh"
|
2010-06-23 17:32:35 +02:00
|
|
|
#include "posexcept.hh"
|
[Positioning] Some ReferencePoint code -> PosUtil
ReferencePoint::complete_with_dummy_measurements(
map<string, Measurement>,
map<string, Measurement>)
becomes PosUtil::complete_with_dummy_measurements(
unordered_map<string, Measurement>,
unordered_map<string, Measurement>)
ReferencePoint::compute_ss_square_distance(
map<string, Measurement>,
map<string, Measurement>)
becomes PosUtil::ss_square_distance(
unordered_map<string, Measurement>,
unordered_map<string, Measurement>)
map<string, Measurement> ReferencePoint::get_all_measurements_sorted()
is now private, returns an unordered_map instead of a map, and is
renamed ReferencePoint::get_all_measurements().
Note that we now use unordered_maps for these functions. The use of
sorted lists (maps) was useless, as stated in the commit message of
e9eab687 (Wed Jun 2 12:37:43 2010 +0200).
2011-05-09 15:19:01 +02:00
|
|
|
#include "stock.hh"
|
|
|
|
#include "measurement.hh"
|
2009-12-11 13:52:21 +01:00
|
|
|
|
2011-05-09 14:20:57 +02:00
|
|
|
#include <owlps.h>
|
2009-12-11 13:52:21 +01:00
|
|
|
|
2011-07-13 14:56:06 +02:00
|
|
|
#include <cmath>
|
|
|
|
|
[Positioning] Some ReferencePoint code -> PosUtil
ReferencePoint::complete_with_dummy_measurements(
map<string, Measurement>,
map<string, Measurement>)
becomes PosUtil::complete_with_dummy_measurements(
unordered_map<string, Measurement>,
unordered_map<string, Measurement>)
ReferencePoint::compute_ss_square_distance(
map<string, Measurement>,
map<string, Measurement>)
becomes PosUtil::ss_square_distance(
unordered_map<string, Measurement>,
unordered_map<string, Measurement>)
map<string, Measurement> ReferencePoint::get_all_measurements_sorted()
is now private, returns an unordered_map instead of a map, and is
renamed ReferencePoint::get_all_measurements().
Note that we now use unordered_maps for these functions. The use of
sorted lists (maps) was useless, as stated in the commit message of
e9eab687 (Wed Jun 2 12:37:43 2010 +0200).
2011-05-09 15:19:01 +02:00
|
|
|
using namespace std ;
|
|
|
|
using std::tr1::unordered_map ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-13 14:56:06 +02:00
|
|
|
/* *** Maths *** */
|
|
|
|
|
|
|
|
|
|
|
|
double PosUtil::rad2deg(const double &radians)
|
|
|
|
{
|
|
|
|
return radians * (180.0 / M_PI) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
[Positioning] Some ReferencePoint code -> PosUtil
ReferencePoint::complete_with_dummy_measurements(
map<string, Measurement>,
map<string, Measurement>)
becomes PosUtil::complete_with_dummy_measurements(
unordered_map<string, Measurement>,
unordered_map<string, Measurement>)
ReferencePoint::compute_ss_square_distance(
map<string, Measurement>,
map<string, Measurement>)
becomes PosUtil::ss_square_distance(
unordered_map<string, Measurement>,
unordered_map<string, Measurement>)
map<string, Measurement> ReferencePoint::get_all_measurements_sorted()
is now private, returns an unordered_map instead of a map, and is
renamed ReferencePoint::get_all_measurements().
Note that we now use unordered_maps for these functions. The use of
sorted lists (maps) was useless, as stated in the commit message of
e9eab687 (Wed Jun 2 12:37:43 2010 +0200).
2011-05-09 15:19:01 +02:00
|
|
|
/* *** 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 ;
|
|
|
|
}
|
|
|
|
|
2009-12-11 13:52:21 +01:00
|
|
|
|
2010-03-05 10:05:12 +01:00
|
|
|
|
2011-05-09 14:20:57 +02:00
|
|
|
/* *** Wi-Fi *** */
|
2010-03-05 10:05:12 +01:00
|
|
|
|
|
|
|
|
2010-01-16 12:23:07 +01:00
|
|
|
/**
|
2011-05-09 14:20:57 +02:00
|
|
|
* @param channel A IEEE 802.11 channel or frequency in MHz or in Hz.
|
2010-06-23 17:32:35 +02:00
|
|
|
* @return The frequency in Hz.
|
|
|
|
* @throw malformed_input_data if \em channel is not a valid channel or
|
|
|
|
* frequency value.
|
2009-12-11 13:52:21 +01:00
|
|
|
*/
|
2010-06-23 17:32:35 +02:00
|
|
|
unsigned long PosUtil::wifi_channel_to_hz(const unsigned long &channel)
|
2009-12-11 13:52:21 +01:00
|
|
|
{
|
|
|
|
switch (channel)
|
|
|
|
{
|
|
|
|
case 1:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_1:
|
|
|
|
case OWL_80211_HZ_CHANNEL_1:
|
|
|
|
return OWL_80211_HZ_CHANNEL_1 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 2:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_2:
|
|
|
|
case OWL_80211_HZ_CHANNEL_2:
|
|
|
|
return OWL_80211_HZ_CHANNEL_2 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 3:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_3:
|
|
|
|
case OWL_80211_HZ_CHANNEL_3:
|
|
|
|
return OWL_80211_HZ_CHANNEL_3 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 4:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_4:
|
|
|
|
case OWL_80211_HZ_CHANNEL_4:
|
|
|
|
return OWL_80211_HZ_CHANNEL_4 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 5:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_5:
|
|
|
|
case OWL_80211_HZ_CHANNEL_5:
|
|
|
|
return OWL_80211_HZ_CHANNEL_5 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 6:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_6:
|
|
|
|
case OWL_80211_HZ_CHANNEL_6:
|
|
|
|
return OWL_80211_HZ_CHANNEL_6 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 7:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_7:
|
|
|
|
case OWL_80211_HZ_CHANNEL_7:
|
|
|
|
return OWL_80211_HZ_CHANNEL_7 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 8:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_8:
|
|
|
|
case OWL_80211_HZ_CHANNEL_8:
|
|
|
|
return OWL_80211_HZ_CHANNEL_8 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 9:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_9:
|
|
|
|
case OWL_80211_HZ_CHANNEL_9:
|
|
|
|
return OWL_80211_HZ_CHANNEL_9 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 10:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_10:
|
|
|
|
case OWL_80211_HZ_CHANNEL_10:
|
|
|
|
return OWL_80211_HZ_CHANNEL_10 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 11:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_11:
|
|
|
|
case OWL_80211_HZ_CHANNEL_11:
|
|
|
|
return OWL_80211_HZ_CHANNEL_11 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 12:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_12:
|
|
|
|
case OWL_80211_HZ_CHANNEL_12:
|
|
|
|
return OWL_80211_HZ_CHANNEL_12 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 13:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_13:
|
|
|
|
case OWL_80211_HZ_CHANNEL_13:
|
|
|
|
return OWL_80211_HZ_CHANNEL_13 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
case 14:
|
2011-05-09 14:20:57 +02:00
|
|
|
case OWL_80211_MHZ_CHANNEL_14:
|
|
|
|
case OWL_80211_HZ_CHANNEL_14:
|
|
|
|
return OWL_80211_HZ_CHANNEL_14 ;
|
2009-12-11 13:52:21 +01:00
|
|
|
}
|
|
|
|
|
2010-06-23 17:32:35 +02:00
|
|
|
// Error: wrong channel value
|
|
|
|
throw bad_channel(channel) ;
|
2009-12-11 13:52:21 +01:00
|
|
|
}
|
2011-07-21 14:37:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* *** 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 ;
|
|
|
|
}
|