added tools
[profile/ivi/automotive-message-broker.git] / tools / AmbSignalMapper / lib / Intel / IviPoc / DbcGrammar
1 #Copyright (C) 2014  Intel Corporation
2 #
3 #This library is free software; you can redistribute it and/or
4 #modify it under the terms of the GNU Lesser General Public
5 #License as published by the Free Software Foundation; either
6 #version 2.1 of the License, or (at your option) any later version.
7 #
8 #This library is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 #Lesser General Public License for more details.
12 #
13 #You should have received a copy of the GNU Lesser General Public
14 #License along with this library; if not, write to the Free Software
15 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16
17 {
18     my @networks = ();
19     my @ecuList = ();
20     my %ecuMessages = ();
21     my %ecuValues = ();
22 }
23
24 DbcOutput: Line(s?) eofile
25     {
26         my @electronicControlUnits = ();
27         for my $ecui (0..scalar(@ecuList)-1) {
28             my %ecuField = ();
29             $ecuField{'canId'} = $ecuList[$ecui];
30
31             if ( exists( $ecuMessages{$ecuList[$ecui]} ) ) {
32                 my $messagesRef = $ecuMessages{$ecuList[$ecui]};
33                 my @messages = @{$messagesRef};
34
35                 $ecuField{'messages'} = \@messages;
36                 for my $msgi (0..scalar(@messages)-1) {
37                         my @signals = @{$messages[$msgi]{'signals'}};
38
39                         for my $si (0..scalar(@signals)-1) {
40                             my $signal = \%{$signals[$si]};
41                             my $msgid = $messages[$msgi]{'canId'};
42
43                             if ( exists( ${ecuValues{$msgid}} ) && defined ${$ecuValues{$msgid}{$signal->{'canId'}}} ) {
44                                 my $valueListRef=${$ecuValues{$msgid}->{$signal->{'canId'}}};
45                                 my @valueList = @$valueListRef;
46                                 $signal->{'values'} = \@valueList;
47
48                                 # Add AMBPropertyName prefix if valueName starts with number
49                                 foreach my $vali (@{$signal->{'values'}}) {
50                                     if ( $vali->{'name'} =~ m/^[0-9]/ ) {
51                                         $vali->{'name'} = $signal->{'AMBPropertyName'} . '_' . $vali->{'name'};
52                                     }
53                                 }
54                             }
55                             use Intel::IviPoc::AmbCommon;
56                             $signal->{'AMBPropertyType'} = &evaluateType($signal);
57                         }
58                     }
59                 }
60                 push (@electronicControlUnits, \%ecuField);
61             }
62
63         my %jsonroot = ();
64         $jsonroot{'pluginName'} = "pluginName";
65         $jsonroot{'electronicControlUnits'} = \@electronicControlUnits;
66
67         # Now return the whole internal json structure
68         return \%jsonroot;
69     }
70
71
72 eofile: /^\Z/
73
74 Line: CommentPrefix /.*\n/
75         # Not needed to output anything yet
76     | 'VERSION' DoubleQuotedId
77         # Not needed to output anything yet
78     | 'NS_' ':' NSList(s?) ...'BS_'
79         {
80             # We just read the list of NSs
81             @networks = @{$item[3]};
82         }
83     | 'BS_' /.*\n/
84         # Not needed to output anything yet
85     | 'BU_' ':' EcuList(s?) ...'BO_'
86         {
87             @ecuList = @{$item[3]};
88         }
89     | 'BO_' Number Identifier ':' Identifier Identifier Signal(s?)
90         {
91             my %msg = ();
92             $msg{'canId'} = $item[2];
93             $msg{'canName'} = $item[3];
94             $msg{'canDlc'} = $item[5];
95             $msg{'canMessageId'} = $item[6];
96             $msg{'signals'} = $item[7];
97
98             push ( @{$ecuMessages{$item[6]}}, \%msg );
99         }
100     | 'VAL_' Number Identifier ValuePair(s?) ';'
101         {
102             ${$ecuValues{$item[2]}->{$item[3]}} = $item[4];
103         }
104     | <error: Invalid dbc format at $thisline!>
105
106 Signal: 'SG_' Identifier ':' DataPlacement FactorOffset Range DoubleQuotedId Receiver(s /,/)
107         {
108             my %signal;
109             $signal{'canId'} = $item[2];
110
111             my %placement = %{$item[4]};
112             $signal{'startbit'} = $placement{'startbit'};
113             $signal{'length'} = $placement{'length'};
114             $signal{'byteOrdering'} = $placement{'byteOrdering'};
115             $signal{'signedness'} = $placement{'signedness'};
116
117             my %fo=%{$item[5]};
118             $signal{'factor'} = $fo{'factor'};
119             $signal{'offset'} = $fo{'offset'};
120
121             my %range=%{$item[6]};
122             $signal{'minValue'} = $range{'minValue'};
123             $signal{'maxValue'} = $range{'maxValue'};
124
125             $signal{'unit'} = $item[7];
126
127             $signal{'receivers'} = $item[8];
128
129             my $ambPropertyName = $item[2];
130             $ambPropertyName =~ s/\_[HM]S$//;
131             $signal{'AMBPropertyName'} = $ambPropertyName;
132
133             $return=\%signal;
134         }
135
136 EcuList: ...!'BO_' Identifier
137         {
138             $return = $item[2];
139         }
140
141 NSList: ...!'BS_' Identifier
142         {
143             $return = $item[2];
144         }
145
146 Receiver: Identifier
147         {
148             my %rx;
149             $rx{'name'} = $item[1];
150             $return = \%rx;
151         }
152
153 ValuePair: Number DoubleQuotedId
154         {
155             my %valuepair;
156             $valuepair{'value'} = $item[1];
157             $valuepair{'description'} = $item[2];
158
159             my $s = $item[2];
160             $s =~ s!(^|\s)(\w)!$1\U$2!g;
161             $s =~ s/ |-/_/g;
162             $s =~ s/[^_a-zA-Z0-9]//g;
163             $s =~ s/_(?=[_]*$)//;
164             $s =~ s/(_)\1+/$1/g;
165             $s =~ s/^\_//;
166             $s =~ s/\_$//;
167             $valuepair{'name'} = $s;
168
169             $return=\%valuepair;
170         }
171
172 DataPlacement: Number '|' Number '@' Number Sign
173         {
174             my %placement;
175             $placement{'startbit'} = $item[1];
176             $placement{'length'} = $item[3];
177             $placement{'byteOrdering'} = $item[5];
178             $placement{'signedness'} = $item[6];
179             $return=\%placement;
180         }
181
182 FactorOffset: '(' RealNumber ',' RealNumber ')'
183         {
184             my %fo;
185             $fo{'factor'} = $item[2];
186             $fo{'offset'} = $item[4];
187             $return=\%fo;
188         }
189
190 Range: '[' RealNumber '|' RealNumber ']'
191         {
192             my %range;
193             $range{'minValue'} = $item[2];
194             $range{'maxValue'} = $item[4];
195             $return=\%range;
196         }
197
198 DoubleQuotedId: DoubleQuotation /[A-Za-z0-9°\!@#\$%\^&\*()\-_=\+\[\]\{\};:\'|,\.\/<>\?`~\ ]+/ DoubleQuotation
199         {
200             $return = $item[2];
201         }
202     | DoubleQuotation DoubleQuotation
203         {
204             $return = "";
205         }
206
207 Identifier: /[A-Za-z0-9_\-]+/
208         {
209             $return = $item[1];
210         }
211
212 Number: /[-+]?[0-9]*/
213 RealNumber: /[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/
214 Sign: /^[-+]?/
215
216 CommentPrefix: /^\/\//
217
218 DoubleQuotation: "\""