tCanMessageMetaDumper.cpp
Go to the documentation of this file.
00001 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00002 // This file is part of FZIs ic_workspace.
00003 //
00004 // This program is free software licensed under the LGPL
00005 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00006 // You can find a copy of this license in LICENSE folder in the top
00007 // directory of the source code.
00008 //
00009 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00010 //
00011 // -- END LICENSE BLOCK ------------------------------------------------
00012 
00013 // ----------------------------------------------------------
00014 /*
00015  * tCanMessageMetaDumper.cpp
00016  * <section>
00017  *
00018  * Created by Hugo Ritzkowski on 24.01.2012.
00019  * Copyright 2012
00020  * Company Forschungszentrum Informatik (FZI), Abteilung IDS.
00021  * All rights reserved.
00022  *
00023  */
00024 // ----------------------------------------------------------
00034 // ----------------------------------------------------------
00035 
00036 #include "tCanMessageMetaDumper.h"
00037 
00038 #include "icl_hardware_can/Logging.h"
00039 #include "icl_core_config/Config.h"
00040 #include <stdio.h>
00041 
00042 namespace icl_hardware {
00043 
00044 namespace can {
00045 
00046 tCanMessageMetaDumper::tCanMessageMetaDumper()
00047   : m_baudrate(0),
00048     m_data_file_name(0)
00049 {
00050 
00051 }
00052 
00053 tCanMessageMetaDumper::~tCanMessageMetaDumper()
00054 {
00055 
00056 }
00057 
00058 void tCanMessageMetaDumper::open(const std::string identifier, const int baudrate)
00059 {
00060   if (identifier != "")
00061   {
00062     setDataFileName(boost::filesystem::path(identifier));
00063   }
00064   else
00065   {
00066     LOGGING_ERROR(icl_hardware::can::CAN, "No data file provided!" << icl_core::logging::endl);
00067     return;
00068   }
00069 
00070   if (baudrate != 0)
00071   {
00072     m_baudrate = baudrate;
00073   }
00074   else
00075   {
00076     LOGGING_ERROR(icl_hardware::can::CAN, "No baudrate provided!" << icl_core::logging::endl);
00077     return;
00078   }
00079 
00080   int ret=prepareFile();
00081 
00082   if (ret!=0)
00083   {
00084     LOGGING_ERROR(icl_hardware::can::CAN, "Failed to open file for recording!" << icl_core::logging::endl);
00085     return;
00086   }
00087 }
00088 
00089 void tCanMessageMetaDumper::close()
00090 {
00091   std::cout << "</record>" << std::endl;
00092   if (m_data_file.is_open())
00093   {
00094     m_data_file.close();
00095   }
00096 }
00097 
00104 bool tCanMessageMetaDumper::constructXmlFile()
00105 {
00106   std::string line;
00107   unsigned int line_counter = 0;
00108   unsigned int next_commit = 1; 
00109 
00110   //print header
00111   std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << std::endl;
00112   std::cout << "<record version=\"" << m_major_fileversion << "." << m_minor_fileversion << "\" baudrate=\"" << m_baudrate << "\" buffer=\"" << getDataFileName().string() << "\" >" << std::endl;
00113 
00114   //read through file, save every m_baudrate line the frame number, timestamp and fileposition
00115   double last_timestamp = -1.;
00116   while(m_data_file.good())
00117   {
00118     getline(m_data_file, line);
00119 
00120     if (line.length() == 0 || line.substr(0,1) == "#")
00121     {
00122       //empty line or comment
00123       continue;
00124     }
00125     ++line_counter;
00126 
00127     // assure that time sequence of can file in order
00128     double timestamp = atof((line.substr(0, line.find(' ', 0))).data());
00129     if (last_timestamp > timestamp)
00130     {
00131       if (!icl_core::config::getDefault<bool>("/icl_hardware_can/force_meta_dump", false))
00132       {
00133         LOGGING_ERROR(icl_hardware::can::CAN, "Time sequence of can file out of order, line number: " << line_counter << "" << icl_core::logging::endl);
00134         return false;
00135       }
00136       else
00137       {
00138         std::cout << "<!-- Warning: Time sequence of can file out of order -->" << std::endl;
00139       }
00140     }
00141 
00142     if (next_commit == line_counter)
00143     {
00144       unsigned int time_sec = static_cast<int>(timestamp);
00145 
00146       std::cout << "<frame number=\"" << line_counter;
00147       std::cout << "\" timesec=\"" << time_sec;
00148       std::cout << "\" timeusec=\"" << static_cast<int>((timestamp - time_sec)*10000000);
00149       std::cout << "\" filepos=\"" << m_data_file.tellg() << "\">";
00150       std::cout << " </frame>" << std::endl;
00151       next_commit += m_baudrate;
00152     }
00153     last_timestamp = timestamp;
00154   }
00155 
00156   if (line_counter < 1)
00157   {
00158     return false;
00159   }
00160   else
00161   {
00162     return true;
00163   }
00164 }
00165 
00166 int tCanMessageMetaDumper::prepareFile()
00167 {
00168   m_data_file.open(getDataFileName().string().c_str(), std::ios::in);
00169 
00170   if (!m_data_file.is_open())
00171   {
00172     return -1;
00173   }
00174 
00175   return 0;
00176 }
00177 
00178 void tCanMessageMetaDumper::setDataFileName(const boost::filesystem::path &file_name)
00179 {
00180   if (m_data_file_name)
00181   {
00182     delete m_data_file_name;
00183   }
00184   m_data_file_name = new boost::filesystem::path(file_name);
00185 }
00186 
00187 const boost::filesystem::path & tCanMessageMetaDumper::getDataFileName() const
00188 {
00189   return *m_data_file_name;
00190 }
00191 
00192 }
00193 
00194 }


fzi_icl_can
Author(s):
autogenerated on Thu Jun 6 2019 20:26:01