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