@@ -90,13 +90,13 @@ public class DbcReader {
90
90
private ObjectFactory factory = null ;
91
91
private NetworkDefinition network = null ;
92
92
private Document document = null ;
93
- private Bus bus = null ;
93
+ private static Bus bus = null ;
94
94
private Map <Long , Set <Signal >> muxed = new TreeMap <Long , Set <Signal >>();
95
95
private Set <LabelDescription > labels = new HashSet <LabelDescription >();
96
96
private Set <SignalComment > signalComments = new HashSet <SignalComment >();
97
97
private String version = "" ;
98
- private PrintWriter logWriter ;
99
-
98
+ private static PrintWriter logWriter ;
99
+
100
100
private static class LabelDescription {
101
101
102
102
private long id ;
@@ -177,33 +177,67 @@ public void setSignalName(String signalName) {
177
177
}
178
178
};
179
179
180
- private static Signal findSignal (List <Message > messages , long id , boolean e , String name ) {
181
- for (Message message : messages ) {
180
+ /**
181
+ * Find a single CAN message from the list of messages
182
+ * @param messages List of message objects
183
+ * @param id CAN identifier of the message to find
184
+ * @param e True, if the message to find is of extended frame format
185
+ * @return Message object found, null otherwise
186
+ */
187
+ private static Message findMessage (List <Message > messages , long id , boolean e ) {
188
+ for (Message message : messages ){
182
189
boolean extended = "extended" .equals (message .getFormat ());
183
190
if (Long .parseLong (message .getId ().substring (2 ), 16 ) == id
184
191
&& extended == e ) {
185
- List <Signal > signals = message .getSignal ();
186
- /* Find signal name */
187
- for (Signal signal : signals ) {
192
+ return message ;
193
+ }
194
+ }
195
+ return null ;
196
+ }
197
+
198
+ /**
199
+ * Find a single CAN signal from list of CAN messages.
200
+ * @param messages List of messages object
201
+ * @param id Identifier of CAN message to find
202
+ * @param e True, if CAN message to find is extended frame format
203
+ * @param name Name of signal to find
204
+ * @return Signal object found, null otherwise
205
+ */
206
+ private static Signal findSignal (List <Message > messages , long id , boolean e , String name ) {
207
+ Message message ;
208
+ message = findMessage (messages , id , e );
209
+ List <Signal > signals ;
210
+
211
+ if (message != null ){
212
+ signals = message .getSignal ();
213
+ } else if (id == 0 ) {
214
+ /* orphaned signal found */
215
+ return null ;
216
+ } else {
217
+ /* valid signal found but message not defined */
218
+ return null ;
219
+ }
220
+
221
+
222
+ /* Find signal name */
223
+ for (Signal signal : signals ) {
224
+ if (signal .getName ().equals (name )) {
225
+ return signal ;
226
+ }
227
+ }
228
+
229
+ for (Multiplex multiplex : message .getMultiplex ()) {
230
+ for (MuxGroup group : multiplex .getMuxGroup ()) {
231
+ for (Signal signal : group .getSignal ()) {
188
232
if (signal .getName ().equals (name )) {
189
233
return signal ;
190
234
}
191
235
}
192
-
193
- for (Multiplex multiplex : message .getMultiplex ()) {
194
- for (MuxGroup group : multiplex .getMuxGroup ()) {
195
- for (Signal signal : group .getSignal ()) {
196
- if (signal .getName ().equals (name )) {
197
- return signal ;
198
- }
199
- }
200
- }
201
- }
202
236
}
203
- }
237
+ }
204
238
return null ;
205
239
}
206
-
240
+
207
241
208
242
/**
209
243
* Read in given CAN database file (*.dbc)
@@ -280,7 +314,9 @@ public boolean parseFile(File file, OutputStream logStream) {
280
314
List <BasicLabelType > labellist = set .getLabelOrLabelGroup ();
281
315
labellist .addAll (description .getLabels ());
282
316
283
- Signal signal = findSignal (messages , description .getId (), description .isExtended (), description .getSignalName ());
317
+ Signal signal = null ;
318
+ signal = findSignal (messages , description .getId (), description .isExtended (), description .getSignalName ());
319
+
284
320
if (signal != null ) {
285
321
signal .setLabelSet (set );
286
322
}
@@ -299,7 +335,7 @@ public boolean parseFile(File file, OutputStream logStream) {
299
335
signal .setNotes (comment .getComment ());
300
336
}
301
337
}
302
-
338
+
303
339
return true ;
304
340
}
305
341
@@ -483,17 +519,20 @@ private static void parseComment(StringBuffer line) {
483
519
*/
484
520
private static void parseAttribute (StringBuffer line ) {
485
521
486
- // if (Pattern.matches("BA_\\s+\"GenMsgCycleTime.*", line)) {
487
- // String[] splitted = splitString(line.toString());
488
- // if (splitted != null) {
489
- // //System.out.println("Attribute: " + line.toString());
490
- //
491
- // System.out.println("id: "+splitted[3]+"cycle: "+splitted[4].substring(0, splitted[4].length()-1));
492
- // }
493
- // }
522
+ List <Message > messages = bus .getMessage ();
494
523
495
- // TODO getMessageByCanId needed to attach cycle time to a message node
496
- // messag context : List<Message> messages = bus.getMessage();
524
+ /* Find message with given id in GenMsgCycleTime and attach to message node */
525
+ if (Pattern .matches ("BA_\\ s+\" GenMsgCycleTime.*" , line )) {
526
+ String [] splitted = splitString (line .toString ());
527
+ if (splitted != null ) {
528
+ Message message = findMessage (messages , Long .valueOf (splitted [3 ]), isExtendedFrameFormat (splitted [3 ]) );
529
+ Integer ival = Integer .valueOf (splitted [4 ].substring (0 , splitted [4 ].length ()-1 ));
530
+ // Omit default interval = 0
531
+ if (ival != 0 ) {
532
+ message .setInterval (ival );
533
+ }
534
+ }
535
+ }
497
536
}
498
537
499
538
/**
@@ -508,7 +547,6 @@ private void parseMessageDefinition(StringBuffer line) {
508
547
muxed = new TreeMap <Long , Set <Signal >>();
509
548
510
549
// BO_ 1984 Messagename: 8 Producername
511
- // System.out.println("Message Definition: " + line.toString());
512
550
513
551
// remove BO_
514
552
line .replace (0 , 4 , "" );
@@ -591,7 +629,7 @@ private int getCanIdFromString(String canIdStr) {
591
629
return canId ;
592
630
}
593
631
594
- private boolean isExtendedFrameFormat (String canIdStr ) {
632
+ private static boolean isExtendedFrameFormat (String canIdStr ) {
595
633
596
634
long canIdLong = Long .valueOf (canIdStr ).longValue ();
597
635
return ((canIdLong >>> 31 & 1 ) == 1 ) ? true : false ;
0 commit comments