owlps/owlps-positioning/src/measurement.cc

182 lines
3.5 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 "measurement.hh"
#include "posexcept.hh"
using namespace std ;
/* *** Constructors *** */
Measurement::Measurement(const AccessPoint *_ap):
ap(const_cast<AccessPoint*>(_ap)), average_ss(0)
{
ss_list.reserve(10) ;
}
Measurement::Measurement(const AccessPoint *_ap,
const vector<ss_t> &_ss_list):
ap(const_cast<AccessPoint*>(_ap)), ss_list(_ss_list)
{
ss_list.reserve(10) ;
update_average_ss() ;
}
/**
* Note that values pointed by #ap are not deleted.
*/
Measurement::~Measurement()
{
ss_list.clear() ;
}
/* *** Write accessors *** */
/**
* @param ss_dbm The signal strength to add to #ss_list (in dBm).
*/
void Measurement::add_ss(const ss_t &ss_dbm)
{
double total_ss_mwatts =
pow(10, static_cast<double>(ss_dbm) / 10.0) + // New SS in mW
(ss_list.size() * pow(10, average_ss / 10.0)) ; // Other values in mW
ss_list.push_back(ss_dbm) ; // Add new value (dBm)
// Update average converting back in dBm
average_ss = 10.0 * log10(total_ss_mwatts / ss_list.size()) ;
}
void Measurement::set_ss_list(const std::vector<ss_t> &_ss_list)
{
ss_list = _ss_list ;
update_average_ss() ;
}
void Measurement::add_ss_list(const std::vector<ss_t> &_ss_list)
{
ss_list.insert(ss_list.end(), _ss_list.begin(), _ss_list.end()) ;
update_average_ss() ;
}
/**
* Merge consists of adding the SS values of \em source to #ss_list. It
* is possible only if the #ap of the two Measurement are identical.
* @throw cannot_merge if the AP of the two Measurement are different.
*/
void Measurement::merge(const Measurement &source)
{
if (ap != source.ap)
throw cannot_merge(
"error when trying to merge measurements, APs are different") ;
add_ss_list(source.ss_list) ;
}
/**
* - #ap is not deleted, only initialised to NULL.
* - #ss_list is cleared.
* - #average_ss is initialised to 0.
*/
void Measurement::clear()
{
ss_list.clear() ;
average_ss = 0 ;
ap = NULL ;
}
/* *** Operations *** */
void Measurement::update_average_ss()
{
if (ss_list.empty())
{
average_ss = 0 ;
return ;
}
double total_ss_mwatts = 0 ;
for (vector<ss_t>::const_iterator i = ss_list.begin() ;
i != ss_list.end() ; ++i)
// Add the current value in mW to the total
total_ss_mwatts += pow(10, static_cast<double>(*i) / 10.0) ;
// Compute the average in mW and convert it to dBm
average_ss = 10 * log10(total_ss_mwatts / ss_list.size()) ;
}
/* *** Operators *** */
Measurement& Measurement::operator=(const Measurement &m)
{
if (this == &m)
return *this ;
ap = m.ap ;
ss_list = m.ss_list ;
average_ss = m.average_ss ;
return *this ;
}
bool Measurement::operator==(const Measurement &m) const
{
if (this == &m)
return true ;
return
ap == m.ap &&
ss_list == m.ss_list &&
average_ss == m.average_ss ;
}
ostream &operator<<(ostream &os, const Measurement &m)
{
// MAC address
os
<< "AP: " << (m.ap != NULL ? m.ap->get_mac_addr() : "Unknown_AP")
<< ": " ;
// List of SS
if (m.ss_list.empty())
os << "No values" ;
else
for (vector<ss_t>::const_iterator i = m.ss_list.begin() ;
i != m.ss_list.end() ; ++i)
{
os << *i ;
if (i != m.ss_list.end() - 1)
os << ";" ;
}
os << " [AVG=" << m.average_ss << "]" ;
return os ;
}