HardwareCanSinkFactory.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // This file is part of FZIs ic_workspace.
3 //
4 // This program is free software licensed under the LGPL
5 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
6 // You can find a copy of this license in LICENSE folder in the top
7 // directory of the source code.
8 //
9 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
10 //
11 // -- END LICENSE BLOCK ------------------------------------------------
12 
13 // ----------------------------------------------------------
14 /*
15  * tHardwareCanSinkFactory.cpp
16  * <section>
17  *
18  * Created by Hugo Ritzkowski on 21.12.2010.
19  * Copyright 2010
20  * Company Forschungszentrum Informatik (FZI), Abteilung IDS.
21  * All rights reserved.
22  *
23  */
24 // ----------------------------------------------------------
33 // ----------------------------------------------------------
34 
35 #include "tHardwareCanSinkFactory.h"
36 #include "tHardwareCanSinkCanfile.h"
37 #include "tHardwareCanSinkConsole.h"
38 #include "tHardwareCanSinkInterpretMessage.h"
39 #ifdef _SYSTEM_POSIX_
40 #include "tHardwareCanSinkPeak.h"
41 #endif
42 
44 #include "icl_core/SchemeParser.h"
45 #include <icl_core_config/Config.h>
46 
47 namespace icl_hardware {
48 namespace can {
49 
51 {
52 
53 }
54 
56 {
57 
58 }
59 
60 tHardwareCanSink * tHardwareCanSinkFactory::Construct(std::string specifier)
61 {
63 
64  if (specifier == "")
65  {
66  LOGGING_DEBUG(icl_hardware::can::CAN, "No specifier was set, read settings from configfile" << "\n");
67  specifier = interpretConfigFile();
68  }
69 
70  if (parser.parseScheme(specifier))
71  {
72  // convert to lowercase
73  std::string lspecifier(parser.getSchemeResult().specifier);
74  for (unsigned int i = 0; i < lspecifier.size(); ++i)
75  {
76  lspecifier[i] = tolower(lspecifier[i]);
77  }
78 
79  LOGGING_DEBUG(icl_hardware::can::CAN, "tHardwareCanSinkFactory parser result: \n");
80  LOGGING_DEBUG(icl_hardware::can::CAN, " scheme_name: "<< parser.getSchemeResult().scheme_name << "\n");
81  LOGGING_DEBUG(icl_hardware::can::CAN, " specifier: "<< parser.getSchemeResult().specifier << "\n");
82  LOGGING_DEBUG(icl_hardware::can::CAN, " anchor: "<< parser.getSchemeResult().anchor << "\n");
83  LOGGING_DEBUG(icl_hardware::can::CAN, " #queries: "<< parser.getSchemeResult().queries.size() << icl_core::logging::endl);
84 
86  {
87  tHardwareCanSinkCanfile * hardware_data_sink = new tHardwareCanSinkCanfile();
88  icl_core::QueryList query_list = parser.getSchemeResult().queries;
89  unsigned int baud_rate = 0;
90  for (unsigned int i=0; i<query_list.size(); ++i)
91  {
92  if (query_list[i].name == "baud")
93  {
94  baud_rate = atoi(query_list[i].value.c_str());
95  }
96  }
97  if (baud_rate != 0)
98  {
99  hardware_data_sink->Open(parser.getSchemeResult().specifier, baud_rate);
100  }
101  else
102  {
103  hardware_data_sink->Open(parser.getSchemeResult().specifier);
104  }
105  return hardware_data_sink;
106  }
108  {
109  if (parser.getSchemeResult().scheme_name == "console://")
110  {
111  if (icl_core::config::getDefault<bool>("/icl_hardware_can/use_interpret", false))
112  {
113  icl_core::String can_matrix = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/can_mask", "");
114  if (can_matrix != "")
115  {
116  tHardwareCanSinkInterpretMessage * hardware_data_sink = new tHardwareCanSinkInterpretMessage();
117  hardware_data_sink->Open(parser.getSchemeResult().specifier, can_matrix);
118  return hardware_data_sink;
119  }
120  else
121  {
122  LOGGING_ERROR(icl_hardware::can::CAN, "Interpretation sink must be used together with CAN mask!" << icl_core::logging::endl);
123  }
124  }
125  else
126  {
127  tHardwareCanSinkConsole * hardware_data_sink = new tHardwareCanSinkConsole();
128  hardware_data_sink->Open(parser.getSchemeResult().specifier);
129  return hardware_data_sink;
130  }
131  }
132  if (parser.getSchemeResult().scheme_name == "can://")
133  {
134 #ifdef _SYSTEM_POSIX_
135  tHardwareCanSinkPeak * hardware_data_sink = new tHardwareCanSinkPeak();
136  icl_core::QueryList query_list = parser.getSchemeResult().queries;
137  unsigned int baud_rate = 0;
138  for (unsigned int i=0; i<query_list.size(); ++i)
139  {
140  if (query_list[i].name == "baud")
141  {
142  baud_rate = atoi(query_list[i].value.c_str());
143  }
144  }
145  if (baud_rate != 0)
146  {
147  hardware_data_sink->Open(parser.getSchemeResult().specifier, baud_rate);
148  }
149  else
150  {
151  hardware_data_sink->Open(parser.getSchemeResult().specifier);
152  }
153 
154  return hardware_data_sink;
155 #else
156  LOGGING_WARNING(icl_hardware::can::CAN, "can:// devices only available on posix platforms.");
157  return NULL;
158 #endif
159  }
160  }
161 
162  LOGGING_ERROR(icl_hardware::can::CAN, "Could not distinguish HardwareSink from scheme \"" << specifier << "\"!" << icl_core::logging::endl);
163  }
164  else
165  {
166  LOGGING_ERROR(icl_hardware::can::CAN, "Failed to parse scheme \"" << specifier << "\"!" << icl_core::logging::endl);
167  }
168 
169  return NULL;
170 }
171 
173 {
174  std::string specifier;
175 
176  icl_core::String sink_specifier = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/sink_factory/sink", "");
177  icl_core::String device_name = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/sink_factory/device", "");
178  icl_core::String baud_rate = icl_core::config::getDefault<icl_core::String>("/icl_hardware_can/sink_factory/baudrate", "");
179 
180  specifier = sink_specifier + device_name;
181 
182  if (sink_specifier == "can://")
183  {
184  if (baud_rate != "")
185  {
186  specifier.append("?baud=" + baud_rate);
187  }
188  }
189  else if (sink_specifier == "file://")
190  {
191  if (baud_rate != "")
192  {
193  specifier.append("baud=" + baud_rate);
194  }
195  }
196 
197  return specifier;
198 }
199 
200 }
201 }
#define LOGGING_DEBUG(streamname, arg)
bool parseScheme(const String &str)
#define LOGGING_ERROR(streamname, arg)
ThreadStream & endl(ThreadStream &stream)
std::string String
#define LOGGING_WARNING(streamname, arg)
SchemeType scheme_type
tHardwareCanSink * Construct(std::string specifier)
QueryList queries
const icl_core::Scheme & getSchemeResult() const
std::vector< Query > QueryList


fzi_icl_can
Author(s):
autogenerated on Mon Jun 10 2019 13:17:02