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 <dbc/Dbc.h>
43 
44 #include "LineParser.h"
45 
46 namespace NewEagle
47 {
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  {
96  NewEagle::DbcAttribute attribute;
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
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
char ReadNextChar(std::string fieldName)
Definition: LineParser.cpp:124
Definition: Dbc.h:45
void SeekSeparator(char separator)
Definition: LineParser.cpp:385
static NewEagle::DbcSignal ReadSignal(NewEagle::LineParser parser)
Definition: DbcBuilder.h:156
MultiplexerMode
Definition: DbcSignal.h:70
static NewEagle::DbcAttribute ReadAttribute(NewEagle::LineParser parser)
Definition: DbcBuilder.h:94
NewEagle::DataType Type
Definition: DbcBuilder.h:52
void SetDataType(DataType type)
Definition: DbcSignal.cpp:153
std::string ReadCIdentifier()
Definition: LineParser.cpp:54
std::string SignalToken
Definition: DbcBuilder.h:74
std::string CommentToken
Definition: DbcBuilder.h:75
static NewEagle::DbcSignalComment ReadSignalComment(NewEagle::LineParser parser)
Definition: DbcBuilder.h:132
std::string ObjectType
Definition: DbcBuilder.h:59
std::string AttributeName
Definition: DbcBuilder.h:57
std::string SignalValueTypeToken
Definition: DbcBuilder.h:78
std::string ReadQuotedString()
Definition: LineParser.cpp:395
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


dbc
Author(s): Ryan Borchert
autogenerated on Fri Mar 20 2020 03:31:33