src/probe-stats/wifi_stats.hh

00001 /*
00002  * WiPal - A library and a set of tools to manipulate wireless traces.
00003  * Copyright (C) 2007  Universite Pierre et Marie Curie - Paris 6
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  * MA  02110-1301  USA
00019  *
00020  * Author: Thomas Claveirole <thomas.claveirole@lip6.fr>
00021  */
00022 #ifndef WIFI_STATS_HH_
00023 # define WIFI_STATS_HH_
00024 
00025 # include <ostream>
00026 # include <vector>
00027 # include <map>
00028 
00029 # include <boost/date_time/posix_time/posix_time.hpp>
00030 
00031 # include <wipal/phy/prism_header.hh>
00032 # include <wipal/wifi/frame/frame.hh>
00033 # include <wipal/wifi/addr.hh>
00034 # include <wipal/pcap/descriptor.hh>
00035 
00036 namespace wifi
00037 {
00038 
00039   namespace stats
00040   {
00041 
00042     /*-------------.
00043     | distribution |
00044     `-------------*/
00045 
00046     struct distribution
00047     {
00048       distribution(float min, float max, float granularity);
00049 
00050       void              insert(float);
00051       std::ostream&     dump(std::ostream&) const;
00052 
00053     private:
00054       float                     min_;
00055       float                     max_;
00056       float                     gran_;
00057       std::vector<unsigned>     dist_;
00058       unsigned                  total_;
00059     };
00060 
00061     std::ostream&
00062     operator << (std::ostream&, const distribution&);
00063 
00064     /*-------------.
00065     | timed_events |
00066     `-------------*/
00067 
00068     struct timed_events
00069     {
00070       typedef boost::posix_time::ptime          ptime;
00071       typedef boost::posix_time::time_duration  time_duration;
00072 
00073       time_duration                             insert(const ptime&);
00074       const std::vector<ptime>&                 evs() const;
00075 
00076     private:
00077       std::vector<ptime>                        evs_;
00078     };
00079 
00080     template <class C>
00081     std::vector<boost::posix_time::time_duration>
00082     its(const C&);
00083 
00084     template <class C>
00085     boost::posix_time::time_duration
00086     mean(const C&);
00087 
00088     template <class C>
00089     boost::posix_time::time_duration
00090     stddev(const C&                                     its,
00091            const boost::posix_time::time_duration*      mean = 0);
00092 
00093     template <class C>
00094     boost::posix_time::time_duration
00095     median(const std::vector<boost::posix_time::time_duration>&);
00096 
00097     template <class C>
00098     distribution
00099     make_distribution(float     min,
00100                       float     max,
00101                       float     granularity,
00102                       const C&  container);
00103 
00104     /*-----------.
00105     | addr_state |
00106     `-----------*/
00107 
00108     struct addr_state
00109     {
00110       typedef boost::posix_time::ptime  ptime;
00111 
00112       struct presp_attr
00113       {
00114         presp_attr();
00115 
00116         ptime           arrival;
00117         ptime           ack_expected_arrival;
00118         ptime           ack_arrival;
00119         unsigned        retries;
00120         unsigned        acks;
00121       };
00122 
00123       typedef std::map<addr, presp_attr>        preq_answer_set;
00124       typedef std::vector<preq_answer_set>      pr_answers_t;
00125 
00126       addr_state();
00127 
00128       timed_events      pr_stats[3]; // [da, sa, bssid]
00129       pr_answers_t      pr_answers;
00130       presp_attr*       last_presp;
00131       unsigned          heard_as_tmiter;
00132       unsigned          mentioned_as_ap;
00133     };
00134 
00135     typedef std::map<addr, addr_state>  addr_state_map;
00136 
00137     /*---------------.
00138     | probe_requests |
00139     `---------------*/
00140 
00141     struct probe_requests
00142     {
00143       typedef boost::posix_time::ptime  ptime;
00144 
00145       probe_requests(addr_state_map&);
00146 
00147       void              update(const frame::mgt::header *frame,
00148                                const ptime& toa);
00149 
00150       std::ostream&     dump(std::ostream&,
00151                              unsigned           successful_frames,
00152                              unsigned           distinct_transmitters,
00153                              const ptime&       first) const;
00154 
00155     protected:
00156       std::ostream&     dump_header(std::ostream&,
00157                                     unsigned            successful_frames)
00158                                                                         const;
00159       std::ostream&     dump_footer(std::ostream&) const;
00160       std::ostream&     dump_addr_stats(std::ostream&,
00161                                         unsigned        index,
00162                                         unsigned        distinct_transmitters)
00163                                                                         const;
00164       std::ostream&     dump_pr_events(std::ostream&,
00165                                        unsigned         index,
00166                                        const ptime&     ref) const;
00167       static
00168       std::ostream&     dump_it_dist(std::ostream&, const distribution&);
00169 
00170     private:
00171       unsigned          count;          // Number of received Probe Requests.
00172       addr_state_map*   addrs;          // Per source address state.
00173       distribution      it_dist;        // Interarrival times distribution.
00174 
00175       // Parameters related to IT distributions.
00176       static const float        unit;
00177       static const float        min;
00178       static const float        max;
00179       static const float        gran;
00180       static const unsigned     tic;
00181     };
00182 
00183     /*-------.
00184     | frames |
00185     `-------*/
00186 
00187     struct frames
00188     {
00189       frames();
00190 
00191       std::ostream&     dump(std::ostream&) const;
00192 
00193       void              update(const pcapxx::pkthdr*    pcap_header,
00194                                const void*              ieee80211_frame);
00195 
00196       void              update(const pcapxx::pkthdr*    pcap_header,
00197                                const prism::header*     prism_header);
00198 
00199     private:
00200       typedef boost::posix_time::ptime  ptime;
00201 
00202       std::ostream&     dump_pr_answers(std::ostream&,
00203                                         const addr_state_map::const_iterator&)
00204                                                                         const;
00205       void              dump_all_pr_answers(const std::string& prefix,
00206                                             const std::string& suffix) const;
00207 
00208       ptime             update_counters(const pcapxx::pkthdr*);
00209 
00210 
00211       unsigned          frame_count;    // Number of received frames.
00212       unsigned          total_len;      // Cumulative length of received
00213                                         // frames in bytes, including
00214                                         // payloads.
00215       unsigned          total_caplen;   // Total number of captured bytes.
00216       unsigned          phy_error_count; // Number of incorrectly
00217                                          // transmitted frames
00218                                          // according to the prism
00219                                          // header noise field.
00220       probe_requests    pr;             // Probe Request stats.
00221       addr_state_map    addrs;          // Addresses states.
00222       ptime             first;          // First frame arrival time.
00223       ptime             last;           // Last frame arrival time.
00224     };
00225 
00226     std::ostream&
00227     operator << (std::ostream&, const frames&);
00228 
00229   } // End of namespace wifi::stats.
00230 
00231 } // End of namespace wifi.
00232 
00233 # include "wifi_stats.hxx"
00234 
00235 #endif // ! WIFI_STATS_HH_

Generated on Tue Jan 15 19:32:31 2008 for wipal by  doxygen 1.5.4