DbcBuilder.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018 New Eagle
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of New Eagle nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
38 
39 namespace NewEagle
40 {
42  {
43  MessageToken = std::string("BO_");
44  SignalToken = std::string("SG_");
45  CommentToken = std::string("CM_");
46  EnumValueToken = std::string("VAL_");
47  AttributeToken = std::string("BA_");
48  SignalValueTypeToken = std::string("SIG_VALTYPE_");
49  }
50 
52  {
53  }
54 
55  NewEagle::Dbc DbcBuilder::NewDbc(const std::string &dbcFile)
56  {
57 
58  NewEagle::Dbc dbc;
59 
60  std::istringstream f(dbcFile);
61  std::string line;
62  uint32_t lineNumber = 0;
63 
64  NewEagle::DbcMessage currentMessage;
65 
66  int32_t cnt = 0;
67 
68  while (std::getline(f, line, '\n'))
69  {
70  lineNumber++;
71 
72  NewEagle::LineParser parser(line);
73 
74 
75  std::string identifier;
76  try
77  {
78  identifier = parser.ReadCIdentifier();
79  }
80  catch(std::exception& ex)
81  {
82  identifier = std::string();
83  }
84 
85  if (!MessageToken.compare(identifier))
86  {
87  try
88  {
89  currentMessage = ReadMessage(parser);
90  currentMessage.SetRawText(line);
91  dbc.AddMessage(currentMessage.GetName(), currentMessage);
92  }
93  catch(LineParserExceptionBase& exlp)
94  {
95  ROS_WARN("LineParser Exception: [%s]", exlp.what());
96  }
97  catch(std::exception& ex)
98  {
99  ROS_ERROR("DBC Message Parser Exception: [%s]", ex.what());
100  }
101  }
102  else if (!SignalToken.compare(identifier))
103  {
104  try
105  {
106  NewEagle::DbcSignal signal = ReadSignal(parser);
107 
108  NewEagle::DbcMessage* msg = dbc.GetMessage(currentMessage.GetName());
109  msg->AddSignal(signal.GetName(), signal);
110  }
111  catch(LineParserExceptionBase& exlp)
112  {
113  ROS_WARN("LineParser Exception: [%s]", exlp.what());
114  }
115  catch(std::exception& ex)
116  {
117  ROS_ERROR("DBC Signal Parser Exception: [%s]", ex.what());
118  }
119  }
120  else if (!CommentToken.compare(identifier))
121  {
122  try
123  {
124  std::string token = parser.ReadCIdentifier();
125 
126  if (!MessageToken.compare(token))
127  {
128  NewEagle::DbcMessageComment dbcMessageComment = ReadMessageComment(parser);
129 
130  std::map<std::string, NewEagle::DbcMessage>::iterator it;
131  int32_t ttt = 0;
132 
133  for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
134  {
135  ttt++;
136  if (it->second.GetRawId() == dbcMessageComment.Id)
137  {
138  it->second.SetComment(dbcMessageComment);
139  break;
140  }
141  }
142  }
143  else if (!SignalToken.compare(token))
144  {
145  NewEagle::DbcSignalComment dbcSignalComment = ReadSignalComment(parser);
146 
147  std::map<std::string, NewEagle::DbcMessage>::iterator it;
148  for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
149  {
150  if (it->second.GetRawId() == dbcSignalComment.Id)
151  {
152  DbcMessage msg = it->second;
153  std::map<std::string, NewEagle::DbcSignal>::iterator its;
154 
155  for (its = msg.GetSignals()->begin(); its != msg.GetSignals()->end(); ++its)
156  {
157  if (its->second.GetName() == dbcSignalComment.SignalName)
158  {
159  its->second.SetComment(dbcSignalComment);
160  break;
161  }
162  }
163  }
164  }
165  }
166  }
167  catch(LineParserExceptionBase& exlp)
168  {
169  ROS_WARN("LineParser Exception: [%s]", exlp.what());
170  }
171  catch(std::exception& ex)
172  {
173  ROS_ERROR("DBC Comment Parser Exception: [%s]", ex.what());
174  }
175  }
176  else if (!AttributeToken.compare(identifier))
177  {
178  try
179  {
180  NewEagle::DbcAttribute dbcAttribute = ReadAttribute(parser);
181 
182  if (dbc.GetMessageCount() > 0)
183  {
184  std::map<std::string, NewEagle::DbcMessage>::iterator it;
185  for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
186  {
187  if (it->second.GetRawId() == dbcAttribute.Id)
188  {
189  std::map<std::string, NewEagle::DbcSignal>::iterator its;
190  DbcMessage msg = it->second;
191  for (its = msg.GetSignals()->begin(); its != msg.GetSignals()->end(); ++its)
192  {
193  if (its->second.GetName() == dbcAttribute.SignalName)
194  {
195  NewEagle::DbcSignal sig = its->second;
196 
197  double gain = sig.GetGain();
198  double offset = sig.GetOffset();
199 
200  double f = 0.0;
201 
202  std::stringstream ss;
203  ss << dbcAttribute.Value;
204  ss >> f;
205 
206  double val = gain * f + offset;
207  sig.SetInitialValue(val);
208  break;
209  }
210  }
211  }
212  }
213  }
214  }
215  catch(LineParserExceptionBase& exlp)
216  {
217  ROS_WARN("LineParser Exception: [%s]", exlp.what());
218  }
219  catch(std::exception& ex)
220  {
221  ROS_ERROR("DBC Signal Value Type Parser Exception: [%s]", ex.what());
222  }
223  }
224  else if (!EnumValueToken.compare(identifier))
225  {
226  // Empty for now.
227  }
228  else if (!SignalValueTypeToken.compare(identifier))
229  {
230  try
231  {
232  NewEagle::DbcSignalValueType dbcSignalValueType = ReadSignalValueType(parser);
233 
234  if (dbc.GetMessageCount() > 0)
235  {
236  std::map<std::string, NewEagle::DbcMessage>::iterator it;
237  for (it = dbc.GetMessages()->begin(); it != dbc.GetMessages()->end(); ++it)
238  {
239  if (it->second.GetRawId() == dbcSignalValueType.Id)
240  {
241  std::map<std::string, NewEagle::DbcSignal>::iterator its;
242  DbcMessage msg = it->second;
243  for (its = msg.GetSignals()->begin(); its != msg.GetSignals()->end(); ++its)
244  {
245  if (its->second.GetName() == dbcSignalValueType.SignalName)
246  {
247  NewEagle::DbcSignal sig = its->second;
248  sig.SetDataType(dbcSignalValueType.Type);
249  break;
250  }
251  }
252  }
253  }
254  }
255  }
256  catch(LineParserExceptionBase& exlp)
257  {
258  ROS_WARN("LineParser Exception: [%s]", exlp.what());
259  }
260  catch(std::exception& ex)
261  {
262  ROS_ERROR("DBC Signal Value Type Parser Exception: [%s]", ex.what());
263  }
264 
265  }
266  }
267 
268  ROS_INFO("dbc.size() = %d", dbc.GetMessageCount());
269 
270  return dbc;
271  }
272 }
std::string EnumValueToken
Definition: DbcBuilder.h:76
static NewEagle::DbcMessageComment ReadMessageComment(NewEagle::LineParser parser)
Definition: DbcBuilder.h:123
static NewEagle::DbcMessage ReadMessage(NewEagle::LineParser parser)
Definition: DbcBuilder.h:142
std::string GetName()
Definition: DbcMessage.cpp:83
Definition: Dbc.h:45
f
double GetOffset() const
Definition: DbcSignal.cpp:104
void SetRawText(std::string rawText)
Definition: DbcMessage.cpp:197
NewEagle::Dbc NewDbc(const std::string &dbcFile)
Definition: DbcBuilder.cpp:55
static NewEagle::DbcSignal ReadSignal(NewEagle::LineParser parser)
Definition: DbcBuilder.h:156
void AddMessage(std::string messageName, NewEagle::DbcMessage message)
Definition: Dbc.cpp:54
static NewEagle::DbcAttribute ReadAttribute(NewEagle::LineParser parser)
Definition: DbcBuilder.h:94
NewEagle::DataType Type
Definition: DbcBuilder.h:52
#define ROS_WARN(...)
void SetDataType(DataType type)
Definition: DbcSignal.cpp:153
std::string ReadCIdentifier()
Definition: LineParser.cpp:54
std::string SignalToken
Definition: DbcBuilder.h:74
NewEagle::DbcMessage * GetMessage(std::string messageName)
Definition: Dbc.cpp:59
std::string CommentToken
Definition: DbcBuilder.h:75
#define ROS_INFO(...)
static NewEagle::DbcSignalComment ReadSignalComment(NewEagle::LineParser parser)
Definition: DbcBuilder.h:132
std::map< std::string, NewEagle::DbcSignal > * GetSignals()
Definition: DbcMessage.cpp:212
void AddSignal(std::string signalName, NewEagle::DbcSignal signal)
Definition: DbcMessage.cpp:176
double GetGain() const
Definition: DbcSignal.cpp:99
std::string SignalValueTypeToken
Definition: DbcBuilder.h:78
std::map< std::string, NewEagle::DbcMessage > * GetMessages()
Definition: Dbc.cpp:48
std::string SignalName
Definition: DbcBuilder.h:60
std::string MessageToken
Definition: DbcBuilder.h:73
static NewEagle::DbcSignalValueType ReadSignalValueType(NewEagle::LineParser parser)
Definition: DbcBuilder.h:82
std::string AttributeToken
Definition: DbcBuilder.h:77
uint16_t GetMessageCount()
Definition: Dbc.cpp:91
void SetInitialValue(double value)
Definition: DbcSignal.cpp:144
#define ROS_ERROR(...)
void SetComment(NewEagle::DbcMessageComment comment)
Definition: DbcMessage.cpp:207
std::string GetName() const
Definition: DbcSignal.cpp:129


can_dbc_parser
Author(s): Ryan Borchert
autogenerated on Sat Jan 9 2021 03:56:18