From 4dc45acff7592bf1459a2be28d2052a5904d53de Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Fri, 30 Dec 2011 18:27:30 +0100 Subject: [PATCH] Get rid of the SS - 0x100 problems In the listener & aggregator, the signal strength is stored as an unsigned byte. To display the actual (negative) value, one must substract 256 (0x100) to the unsigned value. This commit changes several things: - Use the decimal (256) instead of the hexadecimal value (0x100). - Don't substract 256 when copying the value to another unsigned byte (aggregator). - Be careful with the type length. In the positioning server, a SS could be copied to a signed byte, while (theoretically) the negative value can exceed the capacity of the signed byte. measurement.hh now defines a type ss_t to store a signal strength value with the good size (it is currently an int_fast16_t). --- owlps-aggregator/owlps-aggregatord.c | 9 ++++----- owlps-listener/owlps-listenerd.c | 4 ++-- owlps-positioning/src/inputlogcsv.cc | 4 ++-- owlps-positioning/src/inputudpsocket.cc | 4 ++-- owlps-positioning/src/measurement.cc | 16 ++++++++-------- owlps-positioning/src/measurement.hh | 16 +++++++++------- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/owlps-aggregator/owlps-aggregatord.c b/owlps-aggregator/owlps-aggregatord.c index c348447..7f53229 100644 --- a/owlps-aggregator/owlps-aggregatord.c +++ b/owlps-aggregator/owlps-aggregatord.c @@ -514,7 +514,7 @@ int read_loop(int sockfd) mobile_ip_str, request_time_str, capture_time_str, - request.ss_dbm - 0x100, + request.ss_dbm - 256, request.x_position, request.y_position, request.z_position, @@ -664,8 +664,7 @@ void* monitor_requests(void *NULL_value) memcpy(info.ap_mac_addr_bytes, request_info_ptr->ap_mac_addr_bytes, ETHER_ADDR_LEN) ; - info.ss_dbm = - request_info_ptr->ss_dbm - 0x100 ; + info.ss_dbm = request_info_ptr->ss_dbm ; sendto(sockfd, &info, sizeof(info), 0, (struct sockaddr *)&serv, serv_len) ; @@ -675,7 +674,7 @@ void* monitor_requests(void *NULL_value) mac_str) ; fprintf(fd, ";%s;%d", mac_str, - request_info_ptr->ss_dbm - 0x100) ; + request_info_ptr->ss_dbm - 256) ; // Delete request request_info_ptr = request_info_ptr->next ; @@ -1297,7 +1296,7 @@ void print_request_info(request_info_list *info) "\tAP MAC: %s\n" "\tSignal strength: %d dBm\n", ap_mac_str, - info->ss_dbm - 0x100 + info->ss_dbm - 256 ) ; } #endif // NDEBUG diff --git a/owlps-listener/owlps-listenerd.c b/owlps-listener/owlps-listenerd.c index 7b2c7d4..bbdeb42 100644 --- a/owlps-listener/owlps-listenerd.c +++ b/owlps-listener/owlps-listenerd.c @@ -1056,7 +1056,7 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header, request_time_str, capture_time_str, rtap_fields[RTAP_ANTENNASIGNALDBM] ? - request.ss_dbm - 0x100 : 0, + request.ss_dbm - 256 : 0, owl_ntohf(request.x_position), owl_ntohf(request.y_position), owl_ntohf(request.z_position), @@ -1152,7 +1152,7 @@ void extract_radiotap_data(const u_char *packet, rtap_fields[RTAP_ANTENNASIGNALDBM] = owl_true; if (VERBOSE_INFO) printf("Antenna signal: %d dBm\n", - request->ss_dbm - 0x100); + request->ss_dbm - 256); rtap_position += RTAP_L_ANTENNASIGNALDBM ; break ; case RTAP_ANTENNANOISEDBM: diff --git a/owlps-positioning/src/inputlogcsv.cc b/owlps-positioning/src/inputlogcsv.cc index 1661a7b..686fb15 100644 --- a/owlps-positioning/src/inputlogcsv.cc +++ b/owlps-positioning/src/inputlogcsv.cc @@ -42,11 +42,11 @@ const string InputLogCSV::request_to_csv(const Request &request) const for (unordered_map::const_iterator i = measurements.begin() ; i != measurements.end() ; ++i) { - const vector &ss_list = i->second.get_ss_list() ; + const vector &ss_list = i->second.get_ss_list() ; const string &ap_mac = i->second.get_ap() != NULL ? i->second.get_ap()->get_mac_addr() : "" ; - for (vector::const_iterator i = ss_list.begin() ; + for (vector::const_iterator i = ss_list.begin() ; i != ss_list.end() ; ++i) { csv_line << ';' << ap_mac ; diff --git a/owlps-positioning/src/inputudpsocket.cc b/owlps-positioning/src/inputudpsocket.cc index 0406820..51297bf 100644 --- a/owlps-positioning/src/inputudpsocket.cc +++ b/owlps-positioning/src/inputudpsocket.cc @@ -167,14 +167,14 @@ bool InputUDPSocket::fill_current_request() // Substracting 256 is not mandatory here since it is done // automatically when casting from unsigned to signed, but // I write it anyway for the sake of clarity. - int_fast8_t ss = request_info.ss_dbm - 0x100 ; + ss_t ss = request_info.ss_dbm - 256 ; if (Configuration::is_configured("verbose")) cout << "\t* Packet received from the aggregator:" << "\n\t\tAP MAC: " << mac_ap << "\n\t\tSignal: " - << static_cast(ss) << " dBm" + << ss << " dBm" << '\n' ; if (! Configuration::bool_value("positioning.accept-new-aps") && diff --git a/owlps-positioning/src/measurement.cc b/owlps-positioning/src/measurement.cc index 73a4354..3c734d6 100644 --- a/owlps-positioning/src/measurement.cc +++ b/owlps-positioning/src/measurement.cc @@ -16,7 +16,7 @@ Measurement::Measurement(const AccessPoint *_ap): Measurement::Measurement(const AccessPoint *_ap, - const vector &_ss_list): + const vector &_ss_list): ap(const_cast(_ap)), ss_list(_ss_list) { ss_list.reserve(10) ; @@ -41,7 +41,7 @@ Measurement::~Measurement() /** * @param ss_dbm The signal strength to add to #ss_list (in dBm). */ -void Measurement::add_ss(const int &ss_dbm) +void Measurement::add_ss(const ss_t &ss_dbm) { double total_ss_mwatts = pow(10, static_cast(ss_dbm) / 10.0) + // New SS in mW @@ -54,14 +54,14 @@ void Measurement::add_ss(const int &ss_dbm) } -void Measurement::set_ss_list(const std::vector &_ss_list) +void Measurement::set_ss_list(const std::vector &_ss_list) { ss_list = _ss_list ; update_average_ss() ; } -void Measurement::add_ss_list(const std::vector &_ss_list) +void Measurement::add_ss_list(const std::vector &_ss_list) { ss_list.insert(ss_list.end(), _ss_list.begin(), _ss_list.end()) ; update_average_ss() ; @@ -69,8 +69,8 @@ void Measurement::add_ss_list(const std::vector &_ss_list) /** - * 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. + * 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) @@ -110,7 +110,7 @@ void Measurement::update_average_ss() double total_ss_mwatts = 0 ; - for (vector::const_iterator i = ss_list.begin() ; + for (vector::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(*i) / 10.0) ; @@ -161,7 +161,7 @@ ostream &operator<<(ostream &os, const Measurement &m) if (m.ss_list.empty()) os << "No values" ; else - for (vector::const_iterator i = m.ss_list.begin() ; + for (vector::const_iterator i = m.ss_list.begin() ; i != m.ss_list.end() ; ++i) { os << *i ; diff --git a/owlps-positioning/src/measurement.hh b/owlps-positioning/src/measurement.hh index 9f2b82d..1cf09a0 100644 --- a/owlps-positioning/src/measurement.hh +++ b/owlps-positioning/src/measurement.hh @@ -7,6 +7,8 @@ #include #include +typedef int_fast16_t ss_t ; + /// Represents a list of signal strengths captured by one AccessPoint class Measurement { @@ -14,7 +16,7 @@ protected: /// The AccessPoint that performed the measurement AccessPoint *ap ; /// List of signal strengths captured (in dBm) - std::vector ss_list ; + std::vector ss_list ; /// Average of all signal strength captured (dBm) double average_ss ; @@ -29,7 +31,7 @@ public: Measurement(const AccessPoint *_ap = NULL) ; Measurement(const AccessPoint *_ap, - const std::vector &_ss_list) ; + const std::vector &_ss_list) ; Measurement(const Measurement &source): ap(source.ap), ss_list(source.ss_list), @@ -40,7 +42,7 @@ public: /** @name Read accessors */ //@{ AccessPoint* get_ap() const ; - const std::vector& get_ss_list() const ; + const std::vector& get_ss_list() const ; double get_average_ss() const ; int get_ss_list_size() const ; //@} @@ -48,11 +50,11 @@ public: /** @name Write accessors */ //@{ void set_ap(const AccessPoint *_ap) ; - void set_ss_list(const std::vector &_ss_list) ; + void set_ss_list(const std::vector &_ss_list) ; /// Adds a signal strength to #ss_list - void add_ss(const int &ss_dbm) ; + void add_ss(const ss_t &ss_dbm) ; /// Adds several signal strengths to #ss_list - void add_ss_list(const std::vector &_ss_list) ; + void add_ss_list(const std::vector &_ss_list) ; /// Merges a given Measurement into the current Measurement void merge(const Measurement &source) ; void clear(void) ; @@ -89,7 +91,7 @@ inline AccessPoint* Measurement::get_ap() const } -inline const std::vector& Measurement::get_ss_list() const +inline const std::vector& Measurement::get_ss_list() const { return ss_list ; }