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 }
NewEagle::DbcSignalValueType::SignalName
std::string SignalName
Definition: DbcBuilder.h:115
NewEagle::ReadMessage
static NewEagle::DbcMessage ReadMessage(NewEagle::LineParser parser)
Definition: DbcBuilder.h:174
NewEagle::DbcSignal::SetInitialValue
void SetInitialValue(double value)
Definition: DbcSignal.cpp:176
NewEagle::DbcAttribute::Id
uint32_t Id
Definition: DbcBuilder.h:90
NewEagle::Dbc::AddMessage
void AddMessage(std::string messageName, NewEagle::DbcMessage message)
Definition: Dbc.cpp:86
NewEagle::DbcBuilder::AttributeToken
std::string AttributeToken
Definition: DbcBuilder.h:109
NewEagle::DbcBuilder::~DbcBuilder
~DbcBuilder()
Definition: DbcBuilder.cpp:83
NewEagle::DbcMessage::AddSignal
void AddSignal(std::string signalName, NewEagle::DbcSignal signal)
Definition: DbcMessage.cpp:208
NewEagle::DbcSignalComment::Id
uint32_t Id
Definition: DbcSignal.h:110
DbcBuilder.h
NewEagle::ReadSignalValueType
static NewEagle::DbcSignalValueType ReadSignalValueType(NewEagle::LineParser parser)
Definition: DbcBuilder.h:114
NewEagle::Dbc
Definition: Dbc.h:79
NewEagle::ReadMessageComment
static NewEagle::DbcMessageComment ReadMessageComment(NewEagle::LineParser parser)
Definition: DbcBuilder.h:155
NewEagle::DbcBuilder::SignalValueTypeToken
std::string SignalValueTypeToken
Definition: DbcBuilder.h:110
NewEagle::LineParserExceptionBase
Definition: LineParser.h:78
NewEagle::DbcSignalComment
Definition: DbcSignal.h:76
NewEagle::ReadSignal
static NewEagle::DbcSignal ReadSignal(NewEagle::LineParser parser)
Definition: DbcBuilder.h:188
NewEagle
Definition: Dbc.h:45
NewEagle::DbcBuilder::EnumValueToken
std::string EnumValueToken
Definition: DbcBuilder.h:108
f
f
NewEagle::DbcBuilder::MessageToken
std::string MessageToken
Definition: DbcBuilder.h:105
NewEagle::DbcMessage::SetComment
void SetComment(NewEagle::DbcMessageComment comment)
Definition: DbcMessage.cpp:239
DbcSignal.h
NewEagle::DbcSignalValueType
Definition: DbcBuilder.h:80
NewEagle::DbcBuilder::DbcBuilder
DbcBuilder()
Definition: DbcBuilder.cpp:73
NewEagle::DbcAttribute::SignalName
std::string SignalName
Definition: DbcBuilder.h:92
NewEagle::DbcSignal::GetName
std::string GetName() const
Definition: DbcSignal.cpp:161
NewEagle::DbcMessageComment
Definition: DbcMessage.h:77
NewEagle::DbcSignal
Definition: DbcSignal.h:109
NewEagle::DbcMessage
Definition: DbcMessage.h:100
ROS_WARN
#define ROS_WARN(...)
NewEagle::Dbc::GetMessage
NewEagle::DbcMessage * GetMessage(std::string messageName)
Definition: Dbc.cpp:91
NewEagle::DbcSignalValueType::Id
uint32_t Id
Definition: DbcBuilder.h:114
NewEagle::DbcAttribute::Value
std::string Value
Definition: DbcBuilder.h:93
NewEagle::DbcSignal::SetDataType
void SetDataType(DataType type)
Definition: DbcSignal.cpp:185
NewEagle::DbcSignal::GetOffset
double GetOffset() const
Definition: DbcSignal.cpp:136
NewEagle::DbcSignal::GetGain
double GetGain() const
Definition: DbcSignal.cpp:131
NewEagle::ReadSignalComment
static NewEagle::DbcSignalComment ReadSignalComment(NewEagle::LineParser parser)
Definition: DbcBuilder.h:164
NewEagle::DbcBuilder::CommentToken
std::string CommentToken
Definition: DbcBuilder.h:107
NewEagle::Dbc::GetMessageCount
uint16_t GetMessageCount()
Definition: Dbc.cpp:123
NewEagle::DbcMessage::SetRawText
void SetRawText(std::string rawText)
Definition: DbcMessage.cpp:229
NewEagle::LineParser
Definition: LineParser.h:115
NewEagle::DbcMessage::GetName
std::string GetName()
Definition: DbcMessage.cpp:115
NewEagle::DbcSignalComment::SignalName
std::string SignalName
Definition: DbcSignal.h:111
ROS_ERROR
#define ROS_ERROR(...)
NewEagle::DbcSignalValueType::Type
NewEagle::DataType Type
Definition: DbcBuilder.h:116
NewEagle::ReadAttribute
static NewEagle::DbcAttribute ReadAttribute(NewEagle::LineParser parser)
Definition: DbcBuilder.h:126
NewEagle::Dbc::GetMessages
std::map< std::string, NewEagle::DbcMessage > * GetMessages()
Definition: Dbc.cpp:80
NewEagle::DbcAttribute
Definition: DbcBuilder.h:87
ROS_INFO
#define ROS_INFO(...)
NewEagle::DbcBuilder::SignalToken
std::string SignalToken
Definition: DbcBuilder.h:106
NewEagle::DbcMessageComment::Id
uint32_t Id
Definition: DbcMessage.h:111
DbcMessage.h
NewEagle::DbcBuilder::NewDbc
NewEagle::Dbc NewDbc(const std::string &dbcFile)
Definition: DbcBuilder.cpp:87


can_dbc_parser
Author(s): Ryan Borchert
autogenerated on Sat Apr 9 2022 02:34:32