summaryrefslogtreecommitdiff
path: root/tools/AmbSignalMapper/bin/dbc2json
blob: e96f8679f1273918d91cd09f2b4483f1b34f2dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/perl -w

#	Copyright (C) 2014  Intel Corporation
#
#	This library is free software; you can redistribute it and/or
#	modify it under the terms of the GNU Lesser General Public
#	License as published by the Free Software Foundation; either
#	version 2.1 of the License, or (at your option) any later version.
#
#	This library is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#	Lesser General Public License for more details.
#
#	You should have received a copy of the GNU Lesser General Public
#	License along with this library; if not, write to the Free Software
#	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

use strict;
use warnings;

use Intel::IviPoc::DbcGrammar;
use Intel::IviPoc::AmbCommon;
use File::Basename;
use File::Spec;
use Cwd;
use JSON;


=head1 NAME

dbc2json - Vector CANdb++ to JSON file format converter.

=head1 SYNOPSIS

B<dbc2json> I<infile> [ I<outfile> ]

=head1 DESCRIPTION

Part of Automotive Message Broker Signal Mapper tool.

B<dbc2json> converts file in Vector CANdb++ format to intermediate JSON file.

Intermediate file is used to format and annotate raw data from CANDb++ file into format suitable for AMB plugin creation.

=head1 OPTIONS

=over 4

=item I<infile>

Input I<infile> file in Vector CANdb++ (*.dbc) format.

=item [ I<outfile> ]

Target output I<outfile> file converted to JSON, otherwise created in current directory.

=back

=head1 FILES

=over 4

=back

=head1 REQUIRES

Perl 5.006, Intel::IviPoc::DbcGrammar, File::Basename, File::Spec, Cwd, JSON

=head1 SEE ALSO

perl(1), json2amb(1)

=cut

local $/;

# get total arg passed to this script
my $total = $#ARGV + 1;

if ( $total < 1 or $total > 2) {
    # get script name and print usage
    my $scriptname = $0;
    &printUsage($scriptname);
    exit;
    }

# First parameter is input file
my $inputfilename = $ARGV[0];
my $text = &readFileContent($inputfilename);

# Second optional parameter is output file
my $outputfilename = '';
if ( $total == 1 ) {
    my ($infilename, $indirectories, $insuffix) = fileparse($inputfilename, qr/\.[^.]*/);
    my $targetDir = getcwd;
    $outputfilename = File::Spec->catfile( ($targetDir), $infilename . '.json' );
} else {
    $outputfilename = $ARGV[1];
}

# We add one space if there is something like  ""ReceiverId to get "" ReceiverId
$text =~ s/"(.*)"(\w*)/"$1" $2/g;

#Create parser
my $parser = new Intel::IviPoc::DbcGrammar;

# Print wait info for user
&printWait($inputfilename, $outputfilename);

# Parse input file and generate json
my $result;
$result = $parser->DbcOutput($text);
my %jsonroot = %{$result};

# Get the filename without path and extension and set it as pluginName
my ($outfilename, $outdirectories, $outsuffix) = fileparse($outputfilename, qr/\.[^.]*/);
$jsonroot{'pluginName'} = $outfilename;

# Now just spit the JSON out
my $json = JSON->new;
$json = $json->utf8;
my $json_text = $json->pretty->encode( \%jsonroot  );

open (my $fho, '>', $outputfilename)
  or die "Can't create filehandle: $!";

binmode($fho, ":utf8");

print { $fho } $json_text;

if ( $total == 1 or $total == 2 ) {
    # close current output file
    close($fho);
}

# Finnish
exit;

=head2 printWait

Prints out wait information

=cut

sub printWait {
    print STDERR ("\n");
    print STDERR ("Processing input file:\n");
    print STDERR ("    $_[0]\n");
    print STDERR ("Generating output file:\n");
    print STDERR ("    $_[1]\n");
    print STDERR ("\n");
    print STDERR ("This operation may take some while. Please wait...\n");
    print STDERR ("\n");
    print STDERR ("\n");
}

=head2 printUsage

Prints out basic usage help

=cut

sub printUsage {
    my $scriptname=$_[0];
    print STDERR ("$scriptname\n");
    print STDERR ("Usage: dbc2json infile [outfile]\n");
    print STDERR ("  infile              Input dbc file\n");
    print STDERR ("  outfile             File (optional) converted to json,\n");
    print STDERR ("                      otherwise created in current directory.\n");
    print STDERR ("\n");
}

=head1 AUTHOR

IntelIVIPoc, C<< <ivipoc at intel.com> >>

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc dbc2json

=head1 ACKNOWLEDGEMENTS

=head1 LICENSE AND COPYRIGHT

Copyright (C) 2014  Intel Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

=cut