HardwareCanSinkFactory.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  * tHardwareCanSinkFactory.cpp
00016  * <section>
00017  *
00018  * Created by Hugo Ritzkowski on 21.12.2010.
00019  * Copyright 2010
00020  * Company Forschungszentrum Informatik (FZI), Abteilung IDS.
00021  * All rights reserved.
00022  *
00023  */
00024 // ----------------------------------------------------------
00033 // ----------------------------------------------------------
00034 
00035 #include "tHardwareCanSinkFactory.h"
00036 #include "tHardwareCanSinkCanfile.h"
00037 #include "tHardwareCanSinkConsole.h"
00038 #include "tHardwareCanSinkInterpretMessage.h"
00039 #ifdef _SYSTEM_POSIX_
00040 #include "tHardwareCanSinkPeak.h"
00041 #endif
00042 
00043 #include "icl_hardware_can/Logging.h"
00044 #include "icl_core/SchemeParser.h"
00045 #include <icl_core_config/Config.h>
00046 
00047 namespace icl_hardware {
00048 namespace can {
00049 
00050 tHardwareCanSinkFactory::tHardwareCanSinkFactory()
00051 {
00052 
00053 }
00054 
00055 tHardwareCanSinkFactory::~tHardwareCanSinkFactory()
00056 {
00057 
00058 }
00059 
00060 tHardwareCanSink * tHardwareCanSinkFactory::Construct(std::string specifier)
00061 {
00062     icl_core::SchemeParser parser;
00063 
00064     if (specifier == "")
00065     {
00066       LOGGING_DEBUG(icl_hardware::can::CAN, "No specifier was set, read settings from configfile" << "\n");
00067       specifier = interpretConfigFile();
00068     }
00069 
00070     if (parser.parseScheme(specifier))
00071     {
00072       // convert to lowercase
00073       std::string lspecifier(parser.getSchemeResult().specifier);
00074       for (unsigned int i = 0; i < lspecifier.size(); ++i)
00075       {
00076         lspecifier[i] = tolower(lspecifier[i]);
00077       }
00078 
00079       LOGGING_DEBUG(icl_hardware::can::CAN, "tHardwareCanSinkFactory parser result: \n");
00080       LOGGING_DEBUG(icl_hardware::can::CAN, " scheme_name: "<< parser.getSchemeResult().scheme_name << "\n");
00081       LOGGING_DEBUG(icl_hardware::can::CAN, " specifier: "<< parser.getSchemeResult().specifier << "\n");
00082       LOGGING_DEBUG(icl_hardware::can::CAN, " anchor: "<< parser.getSchemeResult().anchor << "\n");
00083       LOGGING_DEBUG(icl_hardware::can::CAN, " #queries: "<< parser.getSchemeResult().queries.size() << icl_core::logging::endl);
00084 
00085       if (parser.getSchemeResult().scheme_type == icl_core::FileScheme)
00086       {
00087         tHardwareCanSinkCanfile * hardware_data_sink = new tHardwareCanSinkCanfile();
00088         icl_core::QueryList query_list = parser.getSchemeResult().queries;
00089         unsigned int baud_rate = 0;
00090         for (unsigned int i=0; i<query_list.size(); ++i)
00091         {
00092           if (query_list[i].name == "baud")
00093           {
00094             baud_rate = atoi(query_list[i].value.c_str());
00095           }
00096         }
00097         if (baud_rate != 0)
00098         {
00099           hardware_data_sink->Open(parser.getSchemeResult().specifier, baud_rate);
00100         }
00101         else
00102         {
00103           hardware_data_sink->Open(parser.getSchemeResult().specifier);
00104         }
00105         return hardware_data_sink;
00106       }
00107       else if (parser.getSchemeResult().scheme_type == icl_core::OtherScheme)
00108       {
00109         if (parser.getSchemeResult().scheme_name == "console://")
00110         {
00111           if (icl_core::config::getDefault<bool>("/icl_hardware_can/use_interpret", false))
00112           {
00113             icl_core::String can_matrix = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/can_mask", "");
00114             if (can_matrix != "")
00115             {
00116               tHardwareCanSinkInterpretMessage * hardware_data_sink = new tHardwareCanSinkInterpretMessage();
00117               hardware_data_sink->Open(parser.getSchemeResult().specifier, can_matrix);
00118               return hardware_data_sink;
00119             }
00120             else
00121             {
00122               LOGGING_ERROR(icl_hardware::can::CAN, "Interpretation sink must be used together with CAN mask!" << icl_core::logging::endl);
00123             }
00124           }
00125           else
00126           {
00127             tHardwareCanSinkConsole * hardware_data_sink = new tHardwareCanSinkConsole();
00128             hardware_data_sink->Open(parser.getSchemeResult().specifier);
00129             return hardware_data_sink;
00130           }
00131         }
00132         if (parser.getSchemeResult().scheme_name == "can://")
00133         {
00134 #ifdef _SYSTEM_POSIX_
00135           tHardwareCanSinkPeak * hardware_data_sink = new tHardwareCanSinkPeak();
00136           icl_core::QueryList query_list = parser.getSchemeResult().queries;
00137           unsigned int baud_rate = 0;
00138           for (unsigned int i=0; i<query_list.size(); ++i)
00139           {
00140             if (query_list[i].name == "baud")
00141             {
00142               baud_rate = atoi(query_list[i].value.c_str());
00143             }
00144           }
00145           if (baud_rate != 0)
00146           {
00147             hardware_data_sink->Open(parser.getSchemeResult().specifier, baud_rate);
00148           }
00149           else
00150           {
00151             hardware_data_sink->Open(parser.getSchemeResult().specifier);
00152           }
00153 
00154           return hardware_data_sink;
00155 #else
00156           LOGGING_WARNING(icl_hardware::can::CAN, "can:// devices only available on posix platforms.");
00157           return NULL;
00158 #endif
00159         }
00160       }
00161 
00162       LOGGING_ERROR(icl_hardware::can::CAN, "Could not distinguish HardwareSink from scheme \"" << specifier << "\"!" << icl_core::logging::endl);
00163     }
00164     else
00165     {
00166       LOGGING_ERROR(icl_hardware::can::CAN, "Failed to parse scheme \"" << specifier << "\"!" << icl_core::logging::endl);
00167     }
00168 
00169     return NULL;
00170 }
00171 
00172 std::string tHardwareCanSinkFactory::interpretConfigFile()
00173 {
00174   std::string specifier;
00175 
00176   icl_core::String sink_specifier = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/sink_factory/sink", "");
00177   icl_core::String device_name = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/sink_factory/device", "");
00178   icl_core::String baud_rate = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/sink_factory/baudrate", "");
00179 
00180   specifier = sink_specifier + device_name;
00181 
00182   if (sink_specifier == "can://")
00183   {
00184     if (baud_rate != "")
00185     {
00186       specifier.append("?baud=" + baud_rate);
00187     }
00188   }
00189   else if (sink_specifier == "file://")
00190   {
00191     if (baud_rate != "")
00192     {
00193       specifier.append("baud=" + baud_rate);
00194     }
00195   }
00196 
00197   return specifier;
00198 }
00199 
00200 }
00201 }


fzi_icl_can
Author(s):
autogenerated on Tue Aug 8 2017 03:07:51