include/wipal/wifi/time_converter.hxx

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_TIME_CONVERTER_HXX_
00023 # define WIFI_TIME_CONVERTER_HXX_
00024 
00025 # include "time_converter.hh"
00026 
00027 namespace wifi
00028 {
00029 
00030   template <class Bottom>
00031   time_converter<Bottom>::
00032   time_converter(const pcaptime_type&           local_pcaptime,
00033                  const hosttime_type&           local_hosttime,
00034                  const mactime_type&            local_mactime,
00035                  const tool::microseconds&      global_time):
00036     super_type (),
00037     local_reference_ (local_pcaptime, local_hosttime, local_mactime),
00038     global_reference_ (global_time)
00039   {
00040   }
00041 
00042   template <class Bottom>
00043   time_converter<Bottom>::time_values::time_values()
00044   {
00045   }
00046 
00047   template <class Bottom>
00048   time_converter<Bottom>::
00049   time_values::time_values(const pcaptime_type& pcaptime,
00050                            const hosttime_type& hosttime,
00051                            const mactime_type&  mactime):
00052     pcaptime (pcaptime),
00053     hosttime (hosttime),
00054     mactime (mactime)
00055   {
00056   }
00057 
00058   template <class Bottom>
00059   typename time_converter<Bottom>::time_values
00060   time_converter<Bottom>::
00061   operator () (const tool::microseconds& global_time) const
00062   {
00063     enum { s = 1000000 }; // One second in microseconds.
00064 
00065     const mpz_class     dt      = global_time - global_reference_;
00066 
00067     mpz_class           usec    = local_reference_.pcaptime.tv_usec + dt;
00068     const mpz_class     sec     = local_reference_.pcaptime.tv_sec + usec / s;
00069     usec %= s;
00070 
00071     mpz_class           host    = local_reference_.hosttime + dt / 10;
00072     if (dt % 10 >= 5)
00073       ++host;
00074 
00075     const mpz_class     mac     = local_reference_.mactime + dt;
00076 
00077     time_values         r;
00078 
00079     r.pcaptime.tv_usec  = usec.get_ui();
00080     r.pcaptime.tv_sec   = sec.get_si();
00081     r.hosttime          = host.get_ui();
00082     r.mactime           = mac.get_ui();
00083 
00084     return r;
00085   }
00086 
00087 } // End of namespace wifi.
00088 
00089 #endif // ! WIFI_TIME_CONVERTER_HXX_

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