/*
 * This file is part of the Owl Positioning System (OwlPS) project.
 * It is subject to the copyright notice and license terms in the
 * COPYRIGHT.t2t file found in the top-level directory of this
 * distribution and at
 *   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
 * No part of the OwlPS Project, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms
 * contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
 * distributed along with this file, either separately or by replacing
 * this notice by the COPYRIGHT.t2t file's contents.
 */


#include "frbhmbasic.hh"



Result FRBHMBasic::compute(const Request &_request)
{
  request = &_request ;

  // Select the closest point in SS
  closest_in_ss = &select_point(_request) ;

  compute_cp_distance_circles() ;
  Point3D position(trilaterate_2d(closest_in_ss->get_z())) ;

  return Result(request, name, position) ;
}


/* TODO:
 * It is ugly to duplicate this function from FBCM. We should find a
 * trick to do it prettier. The thing is that FBCM::estimate_distance()
 * will not call FRBHMBasic::friis_index() but FBCM::friis_index(),
 * which is wrong in our case.
 */
float FRBHMBasic::estimate_distance(const Measurement &measurement)
{
  double constant_term = make_constant_term(measurement) ;
  float average_dbm = measurement.get_average_dbm() ;
  const CapturePoint *cp = measurement.get_cp() ;
  return pow(10, (constant_term - average_dbm) /
             (10 * friis_index(cp))) ;
}


inline float FRBHMBasic::friis_index(const CapturePoint *const cp) const
{
  return closest_in_ss->friis_index_for_cp(cp->get_mac_addr()) ;
}
