DbcBuilder.h
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 
35 #ifndef _NEW_EAGLE_DBCBUILDER_H
36 #define _NEW_EAGLE_DBCBUILDER_H
37 
38 #include <ros/ros.h>
39 
40 #include <string>
41 
42 #include <can_dbc_parser/Dbc.h>
43 
44 #include "LineParser.h"
45 
46 namespace NewEagle
47 {
48  struct DbcSignalValueType
49  {
50  uint32_t Id;
51  std::string SignalName;
53  };
54 
55  struct DbcAttribute
56  {
57  std::string AttributeName;
58  uint32_t Id;
59  std::string ObjectType;
60  std::string SignalName;
61  std::string Value;
62  };
63 
64  class DbcBuilder
65  {
66  public:
67  DbcBuilder();
68  ~DbcBuilder();
69 
70  NewEagle::Dbc NewDbc(const std::string &dbcFile);
71 
72  private:
73  std::string MessageToken;
74  std::string SignalToken;
75  std::string CommentToken;
76  std::string EnumValueToken;
77  std::string AttributeToken;
78  std::string SignalValueTypeToken;
79  };
80 
81 
83  {
84  NewEagle::DbcSignalValueType signalValueType;
85 
86  signalValueType.Id = parser.ReadUInt("id");
87  signalValueType.SignalName = parser.ReadCIdentifier();
88  parser.SeekSeparator(':');
89  signalValueType.Type = parser.ReadUInt("DataType") == 1 ? NewEagle::FLOAT : NewEagle::DOUBLE;
90 
91  return signalValueType;
92  }
93 
95  {
97 
98  try
99  {
100  attribute.AttributeName = parser.ReadQuotedString();
101  if (attribute.AttributeName == "GenSigStartValue")
102  {
103  attribute.ObjectType = parser.ReadCIdentifier();
104  attribute.Id = parser.ReadUInt("id");
105  if (attribute.ObjectType == "SG_")
106  {
107  attribute.SignalName = parser.ReadCIdentifier();
108 
109  std::ostringstream sstream;
110  sstream << parser.ReadDouble();
111  attribute.Value = sstream.str();
112  }
113  }
114  }
115  catch(std::exception& ex)
116  {
117  throw;
118  }
119 
120  return attribute;
121  }
122 
124  {
126  comment.Id = parser.ReadUInt("id");
127  comment.Comment = parser.ReadQuotedString();
128 
129  return comment;
130  }
131 
133  {
135  comment.Id = parser.ReadUInt("id");
136  comment.SignalName = parser.ReadCIdentifier();
137  comment.Comment = parser.ReadQuotedString();
138 
139  return comment;
140  }
141 
143  {
144  uint32_t canId = parser.ReadUInt("message id");
145  IdType idType = ((canId & 0x80000000u) > 0) ? NewEagle::EXT : NewEagle::STD;
146  std::string name(parser.ReadCIdentifier("message name"));
147  parser.SeekSeparator(':');
148  uint8_t dlc = parser.ReadUInt("size");
149  std::string sendingNode(parser.ReadCIdentifier("transmitter"));
150  uint32_t id = (uint32_t)(canId & 0x3FFFFFFFu);
151 
152  NewEagle::DbcMessage msg(dlc, id, idType, name, canId);
153  return msg;
154  }
155 
157  {
158  std::string name = parser.ReadCIdentifier();
159  char mux = parser.ReadNextChar("mux");
161  int32_t muxSwitch = 0;
162 
163  switch (mux) {
164  case ':':
165  multiplexMode = NewEagle::NONE;
166  break;
167  case 'M':
168  multiplexMode = NewEagle::MUX_SWITCH;
169  parser.SeekSeparator(':');
170  break;
171  case 'm':
172  multiplexMode = NewEagle::MUX_SIGNAL;
173  muxSwitch = parser.ReadInt();
174  parser.SeekSeparator(':');
175  break;
176  default:
177  throw std::runtime_error("Synxax Error: Expected \':\' " + parser.GetPosition());
178  }
179 
180  int32_t startBit = parser.ReadUInt("start bit");
181 
182  parser.SeekSeparator('|');
183 
184  uint8_t length = (uint8_t)parser.ReadUInt("size");
185  parser.SeekSeparator('@');
186 
187  char byteOrder = parser.ReadNextChar("byte order");
188 
189  NewEagle::ByteOrder endianness;
190 
191  switch (byteOrder) {
192  case '1':
193  endianness = NewEagle::LITTLE_END;
194  break;
195  case '0':
196  endianness = NewEagle::BIG_END;
197  break;
198  default:
199  throw std::runtime_error("Synxax Error: Byte Order - Expected 0 or 1, got " + byteOrder);
200  }
201 
202  char valType = parser.ReadNextChar("value type");
203  NewEagle::SignType sign;
204 
205  switch (valType) {
206  case '+':
207  sign = NewEagle::UNSIGNED;
208  break;
209  case '-':
210  sign = NewEagle::SIGNED;
211  break;
212  default:
213  throw std::runtime_error("Synxax Error: Value Type - Expected + or -, got " + valType);
214  }
215 
216  // Set the default data type: INT.
217  // Might be changed what parsing SIG_VALTYPE_
219 
220 
221  parser.SeekSeparator('(');
222  double gain = parser.ReadDouble("factor");
223  parser.SeekSeparator(',');
224  double offset = parser.ReadDouble("offset");
225  parser.SeekSeparator(')');
226 
227  parser.SeekSeparator('[');
228  double minimum = parser.ReadDouble("minimum");
229  parser.SeekSeparator('|');
230  double maximum = parser.ReadDouble("maximum");
231  parser.SeekSeparator(']');
232 
233  // Need to include Min, Max, DataType, MuxSwitch, Unit, Receiver
234  // Find a way to include the DLC...
235  NewEagle::DbcSignal* signal;
236 
237  if (NewEagle::MUX_SIGNAL == multiplexMode) {
238  //NewEagle::DbcSignal signal(8, gain, offset, startBit, endianness, length, sign, name, multiplexMode, muxSwitch);
239  signal = new NewEagle::DbcSignal(8, gain, offset, startBit, endianness, length, sign, name, multiplexMode, muxSwitch);
240  }
241  else {
242  //NewEagle::DbcSignal signal(8, gain, offset, startBit, endianness, length, sign, name, multiplexMode);
243  signal = new NewEagle::DbcSignal(8, gain, offset, startBit, endianness, length, sign, name, multiplexMode);
244  }
245 
246  signal->SetDataType(type);
247  return NewEagle::DbcSignal(*signal);
248  }
249 }
250 
251 #endif // _NEW_EAGLE_DBCBUILDER_H
NewEagle::DbcSignalValueType::SignalName
std::string SignalName
Definition: DbcBuilder.h:115
NewEagle::ReadMessage
static NewEagle::DbcMessage ReadMessage(NewEagle::LineParser parser)
Definition: DbcBuilder.h:174
NewEagle::DbcAttribute::Id
uint32_t Id
Definition: DbcBuilder.h:90
NewEagle::DbcBuilder::AttributeToken
std::string AttributeToken
Definition: DbcBuilder.h:109
NewEagle::DbcBuilder::~DbcBuilder
~DbcBuilder()
Definition: DbcBuilder.cpp:83
NewEagle::DbcSignalComment::Id
uint32_t Id
Definition: DbcSignal.h:110
NewEagle::ReadSignalValueType
static NewEagle::DbcSignalValueType ReadSignalValueType(NewEagle::LineParser parser)
Definition: DbcBuilder.h:114
NewEagle::DataType
DataType
Definition: DbcSignal.h:95
ros.h
NewEagle::Dbc
Definition: Dbc.h:79
NewEagle::ReadMessageComment
static NewEagle::DbcMessageComment ReadMessageComment(NewEagle::LineParser parser)
Definition: DbcBuilder.h:155
NewEagle::EXT
@ EXT
Definition: DbcMessage.h:86
NewEagle::DbcBuilder::SignalValueTypeToken
std::string SignalValueTypeToken
Definition: DbcBuilder.h:110
NewEagle::DbcSignalComment
Definition: DbcSignal.h:76
NewEagle::FLOAT
@ FLOAT
Definition: DbcSignal.h:98
NewEagle::LineParser::ReadQuotedString
std::string ReadQuotedString()
Definition: LineParser.cpp:427
NewEagle::ReadSignal
static NewEagle::DbcSignal ReadSignal(NewEagle::LineParser parser)
Definition: DbcBuilder.h:188
NewEagle
Definition: Dbc.h:45
NewEagle::INT
@ INT
Definition: DbcSignal.h:97
NewEagle::DbcBuilder::EnumValueToken
std::string EnumValueToken
Definition: DbcBuilder.h:108
NewEagle::IdType
IdType
Definition: DbcMessage.h:83
NewEagle::DbcBuilder::MessageToken
std::string MessageToken
Definition: DbcBuilder.h:105
NewEagle::DbcSignalComment::Comment
std::string Comment
Definition: DbcSignal.h:112
NewEagle::DbcMessageComment::Comment
std::string Comment
Definition: DbcMessage.h:112
NewEagle::NONE
@ NONE
Definition: DbcSignal.h:104
NewEagle::UNSIGNED
@ UNSIGNED
Definition: DbcSignal.h:85
NewEagle::LineParser::ReadDouble
double ReadDouble()
Definition: LineParser.cpp:305
NewEagle::MultiplexerMode
MultiplexerMode
Definition: DbcSignal.h:102
NewEagle::ByteOrder
ByteOrder
Definition: DbcSignal.h:89
NewEagle::LineParser::ReadInt
int32_t ReadInt()
Definition: LineParser.cpp:261
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::DbcAttribute::AttributeName
std::string AttributeName
Definition: DbcBuilder.h:89
NewEagle::DbcMessageComment
Definition: DbcMessage.h:77
NewEagle::SignType
SignType
Definition: DbcSignal.h:83
NewEagle::DbcSignal
Definition: DbcSignal.h:109
NewEagle::DbcMessage
Definition: DbcMessage.h:100
NewEagle::DbcAttribute::ObjectType
std::string ObjectType
Definition: DbcBuilder.h: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::BIG_END
@ BIG_END
Definition: DbcSignal.h:92
NewEagle::LineParser::SeekSeparator
void SeekSeparator(char separator)
Definition: LineParser.cpp:417
NewEagle::MUX_SIGNAL
@ MUX_SIGNAL
Definition: DbcSignal.h:106
Dbc.h
NewEagle::ReadSignalComment
static NewEagle::DbcSignalComment ReadSignalComment(NewEagle::LineParser parser)
Definition: DbcBuilder.h:164
NewEagle::LineParser::ReadNextChar
char ReadNextChar(std::string fieldName)
Definition: LineParser.cpp:156
NewEagle::DbcBuilder::CommentToken
std::string CommentToken
Definition: DbcBuilder.h:107
NewEagle::LineParser::ReadUInt
uint32_t ReadUInt()
Definition: LineParser.cpp:210
NewEagle::SIGNED
@ SIGNED
Definition: DbcSignal.h:86
NewEagle::DOUBLE
@ DOUBLE
Definition: DbcSignal.h:99
NewEagle::LineParser
Definition: LineParser.h:115
NewEagle::DbcSignalComment::SignalName
std::string SignalName
Definition: DbcSignal.h:111
NewEagle::DbcSignalValueType::Type
NewEagle::DataType Type
Definition: DbcBuilder.h:116
NewEagle::ReadAttribute
static NewEagle::DbcAttribute ReadAttribute(NewEagle::LineParser parser)
Definition: DbcBuilder.h:126
NewEagle::STD
@ STD
Definition: DbcMessage.h:85
NewEagle::LITTLE_END
@ LITTLE_END
Definition: DbcSignal.h:91
NewEagle::DbcAttribute
Definition: DbcBuilder.h:87
NewEagle::LineParser::ReadCIdentifier
std::string ReadCIdentifier()
Definition: LineParser.cpp:86
LineParser.h
NewEagle::MUX_SWITCH
@ MUX_SWITCH
Definition: DbcSignal.h:105
NewEagle::DbcBuilder::SignalToken
std::string SignalToken
Definition: DbcBuilder.h:106
NewEagle::DbcMessageComment::Id
uint32_t Id
Definition: DbcMessage.h:111
NewEagle::DbcBuilder::NewDbc
NewEagle::Dbc NewDbc(const std::string &dbcFile)
Definition: DbcBuilder.cpp:87
NewEagle::LineParser::GetPosition
int32_t GetPosition()
Definition: LineParser.cpp:81


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