00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef WIFI_TRACES_SIMILARITY_HXX_
00023 # define WIFI_TRACES_SIMILARITY_HXX_
00024
00025 # include "similarity.hh"
00026
00027 # include <boost/tuple/tuple_comparison.hpp>
00028
00029 # include <wipal/filter/non_noisy_prism.hh>
00030 # include <wipal/filter/uniquely_identifiable.hh>
00031
00032 namespace wpl
00033 {
00034
00035 namespace wifi
00036 {
00037
00039 namespace similarity_helpers
00040 {
00041
00042 template <class I>
00043 void
00044 info(unsigned i, const I& t)
00045 {
00046
00047 (void) i;
00048 (void) t;
00049
00050 # ifdef WP_ENABLE_INFO
00051 std::cerr << "INFO: Trace " << i << " is " << *t << std::endl;
00052 # endif // WP_ENABLE_INFO
00053 }
00054
00055 template <class Result, class Intersector, class I1, class I2>
00056 unsigned
00057 score(Result& r, Intersector& i, const I1& lhs, const I2& rhs)
00058 {
00059 unsigned count = 0;
00060
00061 typename Intersector::const_iterator it = i.begin();
00062 while (it != i.end() and
00063 it.rhs_uframe_count() < similarity_score_limit)
00064 {
00065 ++it;
00066 ++count;
00067 }
00068 if (it != i.end() and count > 0)
00069 --count;
00070
00071 r.insert(boost::make_tuple(count, lhs, rhs));
00072 # ifdef WP_ENABLE_INFO
00073 std::cerr << "INFO: Score for " << *lhs
00074 << " and " << *rhs
00075 << " is " << count << '.' << std::endl;
00076 # endif // WP_ENABLE_INFO
00077
00078 return count;
00079 }
00080
00081 template <class U, class HT, template <class, class, class> class I,
00082 class C>
00083 std::set< boost::tuple<unsigned, typename C::iterator,
00084 typename C::iterator> >
00085 ignore_prism(C& traces, opt::list& options)
00086 {
00087 using filter::uniquely_identifiable;
00088 using opt::get;
00089
00090 typedef tool::end::endianness endianness;
00091 typedef typename C::iterator trace_iterator;
00092 typedef typename C::value_type::const_iterator frame_iterator;
00093 typedef uniquely_identifiable<U, frame_iterator, HT> ui;
00094 typedef typename ui::const_iterator ui_iterator;
00095 typedef I<ui_iterator, ui_iterator, tool::bottom> intersector;
00096 typedef typename intersector::const_iterator i_iterator;
00097
00098 typedef std::set< boost::tuple<unsigned, typename C::iterator,
00099 typename C::iterator> >
00100 result;
00101
00102 result r;
00103
00104 if (traces.empty())
00105 return r;
00106
00107 const unsigned& prec = get<unsigned>(options["precision"]);
00108 const endianness& end = get<endianness>(options["endianness"]);
00109 addr_mapping& map = get<addr_mapping>(options["mapping"]);
00110 const bool pcapts = not get<bool>(options["phy_tstamps"]);
00111
00112 trace_iterator t = traces.begin();
00113 trace_iterator lhs, rhs;
00114
00115 while (lhs = t++, rhs = t, rhs != traces.end())
00116 {
00117 info(1, lhs);
00118
00119 ui lhs_u (lhs->begin(), lhs->end(),
00120 map, end, pcapts);
00121 intersector i (lhs_u.begin(), lhs_u.end(),
00122 lhs_u.begin(), lhs_u.end(), prec);
00123 do
00124 {
00125 info(2, rhs);
00126
00127 ui rhs_u (rhs->begin(), rhs->end(), map, end, pcapts);
00128
00129 i.reset_rhs(rhs_u.begin(), rhs_u.end());
00130 score(r, i, lhs, rhs);
00131 }
00132 while (++rhs != traces.end());
00133 }
00134 return r;
00135 }
00136
00137 template <class U, class HT, template <class, class, class> class I,
00138 class C>
00139 std::set< boost::tuple<unsigned, typename C::iterator,
00140 typename C::iterator> >
00141 filter_prism(C& traces, opt::list& options)
00142 {
00143 using filter::uniquely_identifiable;
00144 using opt::get;
00145
00146 typedef tool::end::endianness endianness;
00147 typedef typename C::iterator trace_iterator;
00148 typedef typename C::value_type::const_iterator frame_iterator;
00149 typedef filter::non_noisy_prism<frame_iterator> nnp;
00150 typedef typename nnp::const_iterator nnp_iterator;
00151 typedef uniquely_identifiable<U, nnp_iterator, HT> ui;
00152 typedef typename ui::const_iterator ui_iterator;
00153 typedef I<ui_iterator, ui_iterator, tool::bottom> intersector;
00154 typedef typename intersector::const_iterator i_iterator;
00155
00156 typedef std::set< boost::tuple<unsigned, typename C::iterator,
00157 typename C::iterator> >
00158 result;
00159
00160 result r;
00161
00162 if (traces.empty())
00163 return r;
00164
00165 const unsigned& prec = get<unsigned>(options["precision"]);
00166 const endianness& end = get<endianness>(options["endianness"]);
00167 addr_mapping& map = get<addr_mapping>(options["mapping"]);
00168 const bool pcapts = not get<bool>(options["phy_tstamps"]);
00169
00170 trace_iterator t = traces.begin();
00171 trace_iterator lhs, rhs;
00172
00173 while (lhs = t++, rhs = t, rhs != traces.end())
00174 {
00175 info(1, lhs);
00176
00177 nnp lhs_n (lhs->begin(), lhs->end());
00178 ui lhs_u (lhs_n.begin(), lhs_n.end(),
00179 map, end, pcapts);
00180 intersector i (lhs_u.begin(), lhs_u.end(),
00181 lhs_u.begin(), lhs_u.end(), prec);
00182 do
00183 {
00184 info(2, rhs);
00185
00186 nnp rhs_n (rhs->begin(), rhs->end());
00187 ui rhs_u (rhs_n.begin(), rhs_n.end(), map, end, pcapts);
00188
00189 i.reset_rhs(rhs_u.begin(), rhs_u.end());
00190 score(r, i, lhs, rhs);
00191 }
00192 while (++rhs != traces.end());
00193 }
00194 return r;
00195 }
00196
00197 }
00198
00199
00200 template <class U, class P, template <class, class, class> class I,
00201 class C>
00202 std::set< boost::tuple<unsigned, typename C::iterator,
00203 typename C::iterator> >
00204 similarity(C& traces, opt::list& options)
00205 {
00206 return options["ignore_noisy_prism"].get<bool>()?
00207 similarity_helpers::filter_prism<U, P, I>(traces, options):
00208 similarity_helpers::ignore_prism<U, P, I>(traces, options);
00209 }
00210
00211 }
00212
00213 }
00214
00215 #endif // ! WIFI_TRACES_SIMILARITY_HXX_