AmbSignalMaper: Implement dbc2amb
[profile/ivi/automotive-message-broker.git] / tools / AmbSignalMapper / lib / Intel / IviPoc / DbcGrammar
1 #Copyright (C) 2014  Intel Corporation
2 #Copyright (c) 2015  Cogent Embedded Inc.
3 #
4 #This library is free software; you can redistribute it and/or
5 #modify it under the terms of the GNU Lesser General Public
6 #License as published by the Free Software Foundation; either
7 #version 2.1 of the License, or (at your option) any later version.
8 #
9 #This library is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #Lesser General Public License for more details.
13 #
14 #You should have received a copy of the GNU Lesser General Public
15 #License along with this library; if not, write to the Free Software
16 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17
18 {
19     my @networks = ();
20     my @ecuList = ();
21     my %ecuMessages = ();
22     my %ecuValues = ();
23     my %enumValues = ();
24     my %messageSenders = ();
25     my $fileComment = '';
26     my %ecuComments = ();
27     my %messageComments = ();
28     my %signalComments = ();
29 }
30
31 DbcOutput: Line(s?) eofile
32     {
33         my @electronicControlUnits = ();
34         for my $ecui (0..scalar(@ecuList)-1) {
35             my %ecuField = ();
36             $ecuField{'canId'} = $ecuList[$ecui];
37
38             if ( exists( $ecuMessages{$ecuList[$ecui]} ) ) {
39                 my $messagesRef = $ecuMessages{$ecuList[$ecui]};
40                 my @messages = @{$messagesRef};
41
42                 $ecuField{'messages'} = \@messages;
43                 for my $msgi (0..scalar(@messages)-1) {
44                         my @signals = @{$messages[$msgi]{'signals'}};
45
46                         for my $si (0..scalar(@signals)-1) {
47                             my $signal = \%{$signals[$si]};
48                             my $msgid = $messages[$msgi]{'canId'};
49
50                             if ( exists( ${ecuValues{$msgid}} ) && defined ${$ecuValues{$msgid}{$signal->{'canId'}}} ) {
51                                 my $valueListRef=${$ecuValues{$msgid}->{$signal->{'canId'}}};
52                                 my @valueList = @$valueListRef;
53                                 $signal->{'values'} = \@valueList;
54
55                                 # Add AMBPropertyName prefix if valueName starts with number
56                                 foreach my $vali (@{$signal->{'values'}}) {
57                                     if ( $vali->{'name'} =~ m/^[0-9]/ ) {
58                                         $vali->{'name'} = $signal->{'AMBPropertyName'} . '_' . $vali->{'name'};
59                                     }
60                                 }
61                             }
62                             use Intel::IviPoc::AmbCommon;
63                             $signal->{'AMBPropertyType'} = &evaluateType($signal);
64                         }
65                     }
66                 }
67                 push (@electronicControlUnits, \%ecuField);
68             }
69
70         my %jsonroot = ();
71         $jsonroot{'pluginName'} = "pluginName";
72         $jsonroot{'electronicControlUnits'} = \@electronicControlUnits;
73
74         # Now return the whole internal json structure
75         return \%jsonroot;
76     }
77
78
79 eofile: /^\Z/
80
81 Line: CommentPrefix /.*\n/
82         # Not needed to output anything yet
83     | 'VERSION' DoubleQuotedId
84         # Not needed to output anything yet
85         # Version can be empty like this ""
86     | 'NS_' ':' NSList(s?) ...'BS_'
87         # this tag holds a list of "new symbols" for the file
88         # Not needed to output anything
89     | 'BS_' ':'
90         # This tag holds definition of bit timings, obsolete
91         # Not need to output anything
92     | 'BU_' ':' <skip:'[ \t]*'> Identifier(s?)
93         {
94             # This tag holds the list of ECUs in one line
95             @ecuList = @{$item[4]};
96         }
97     | 'BO_' Integer Identifier ':' Identifier Identifier Signal(s?)
98         {
99             my %msg = ();
100             $msg{'canId'} = $item[2];
101             $msg{'canName'} = $item[3];
102             $msg{'canDlc'} = $item[5];
103             $msg{'canEcuId'} = $item[6];
104             $msg{'signals'} = $item[7];
105
106             push ( @{$ecuMessages{$item[6]}}, \%msg );
107         }
108     | 'VAL_' Number Identifier ValuePair(s?) ';'
109         {
110             # This tag holds enums defined for specific messages
111             ${$ecuValues{$item[2]}->{$item[3]}} = $item[4];
112         }
113     | 'VAL_TABLE_' Identifier ValuePair(s?) ';'
114         {
115             # This tag holds predefined enumerations
116             ${$enumValues{$item[2]}} = $item[3];
117         }
118     | 'BO_TX_BU_' Number ':' Receiver(s /,/) ';'
119         {
120             # This tag defines message senders
121             @{$messageSenders{$item[2]}} = $item[4];
122         }
123     | 'CM_' DoubleQuotedId ';'
124         {
125             # This tag is a comment for the whole file
126             $fileComment = $item[2];
127         }
128     | 'CM_' 'BU_' Identifier DoubleQuotedId ';'
129         {
130             # This tag is a comment for an ECU
131             ${$ecuComments{$item[3]}} = $item[4];
132         }
133     | 'CM_' 'BO_' Identifier DoubleQuotedId ';'
134         {
135             # This tag is a comment for a message
136             ${$messageComments{$item[3]}} = $item[4];
137         }
138     | 'CM_' 'SG_' Number Identifier DoubleQuotedId ';'
139         {
140             # This tag is a comment for a signal
141             ${$signalComments{$item[3]}->{$item[4]}} = $item[5];
142         }
143     | 'BA_' /.*;/
144             # This tag is an attribute definition
145             # Not needed to output anything yet
146     | 'BA_DEF_' /.*;/
147             # This tag is an attribute definition
148             # Not needed to output anything yet
149     | 'BA_DEF_DEF_' /.*;/
150             # This tag is an attribute definition
151             # Not needed to output anything yet
152     | 'BA_REL_' /.*;/
153             # This tag is an attribute definition
154             # Not needed to output anything yet
155     | 'BA_DEF_REL_' /.*;/
156             # This tag is an attribute definition
157             # Not needed to output anything yet
158     | 'BA_DEF_DEF_REL_' /.*;/
159             # This tag is an attribute definition
160             # Not needed to output anything yet
161     | 'BA_SGTYPE_' /.*;/
162             # This tag is an attribute definition
163             # Not needed to output anything yet
164     | 'BA_DEF_SGTYPE_' /.*;/
165             # This tag is an attribute definition
166             # Not needed to output anything yet
167     | 'SIG_GROUP_' Number Identifier Number ':' Identifier(s) ';'
168             # This tag is a signal group definition
169     | <error: Invalid dbc format at $thisline!>
170
171 Signal: 'SG_' Identifier ':' DataPlacement FactorOffset Range DoubleQuotedId Receiver(s /,/)
172         {
173             my %signal;
174             $signal{'canId'} = $item[2];
175
176             my %placement = %{$item[4]};
177             $signal{'startBit'} = $placement{'startbit'};
178             $signal{'length'} = $placement{'length'};
179             $signal{'byteOrdering'} = $placement{'byteOrdering'};
180             $signal{'signedness'} = $placement{'signedness'};
181
182             my %fo=%{$item[5]};
183             $signal{'factor'} = $fo{'factor'};
184             $signal{'offset'} = $fo{'offset'};
185
186             my %range=%{$item[6]};
187             $signal{'minValue'} = $range{'minValue'};
188             $signal{'maxValue'} = $range{'maxValue'};
189
190             $signal{'unit'} = $item[7];
191
192             $signal{'receivers'} = $item[8];
193
194             my $ambPropertyName = $item[2];
195             $ambPropertyName =~ s/\_[HM]S$//;
196             $signal{'AMBPropertyName'} = $ambPropertyName;
197
198             $return=\%signal;
199         }
200
201 NSList: ...!'BS_' Identifier
202         {
203             $return = $item[2];
204         }
205
206 Receiver: Identifier
207         {
208             my %rx;
209             $rx{'name'} = $item[1];
210             $return = \%rx;
211         }
212
213 ValuePair: Number DoubleQuotedId
214         {
215             my %valuepair;
216             $valuepair{'value'} = $item[1];
217             $valuepair{'description'} = $item[2];
218
219             my $s = $item[2];
220             $s =~ s!(^|\s)(\w)!$1\U$2!g;
221             $s =~ s/ |-/_/g;
222             $s =~ s/[^_a-zA-Z0-9]//g;
223             $s =~ s/_(?=[_]*$)//;
224             $s =~ s/(_)\1+/$1/g;
225             $s =~ s/^\_//;
226             $s =~ s/\_$//;
227             $valuepair{'name'} = $s;
228
229             $return=\%valuepair;
230         }
231
232 DataPlacement: Number '|' Number '@' Number Sign
233         {
234             my %placement;
235             $placement{'startbit'} = $item[1];
236             $placement{'length'} = $item[3];
237             $placement{'byteOrdering'} = $item[5];
238             $placement{'signedness'} = $item[6];
239             $return=\%placement;
240         }
241
242 FactorOffset: '(' RealNumber ',' RealNumber ')'
243         {
244             my %fo;
245             $fo{'factor'} = $item[2];
246             $fo{'offset'} = $item[4];
247             $return=\%fo;
248         }
249
250 Range: '[' RealNumber '|' RealNumber ']'
251         {
252             my %range;
253             $range{'minValue'} = $item[2];
254             $range{'maxValue'} = $item[4];
255             $return=\%range;
256         }
257
258 DoubleQuotedId: DoubleQuotation /[A-Za-z0-9°\!@#\$%\^&\*()\-_=\+\[\]\{\};:\'|,\.\/<>\?`~\ ]+/ DoubleQuotation
259         {
260             $return = $item[2];
261         }
262     | DoubleQuotation DoubleQuotation
263         {
264             $return = "";
265         }
266
267 Identifier: /[A-Za-z0-9_\-]+/
268         {
269             $return = $item[1];
270         }
271
272 Number: Integer | RealNumber | Sign
273
274 Integer: /[-+]?[0-9]*/
275 RealNumber: /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/
276 Sign: /^[-+]?/
277
278 CommentPrefix: /^\/\//
279
280 DoubleQuotation: "\""
281