blib/lib/Convert/EDS/XDD.pm

filter: Statementstmt 72.8% 86 / 118 Branchbran 30.5% 11 / 36 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 88.2% 15 / 17 Podpod 100% 2 / 2 totaltotal 58.0% 115 / 198 CC 26 SCAR 35.7
CC
26
Cov
58%
eds2xdd_string
78.7
eds2xdd
17.1
_hex_or_dec
6.0
CRAP 35.5
↑ Summary
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use v5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Convert::EDS::XDD;
5 4 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # ABSTRACT: Converts CANopen EDS format to Ethernet POWERLINK XDD
8 4 our $VERSION = '0.009'; # VERSION
9  
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 our @EXPORT_OK = qw(eds2xdd eds2xdd_string); # symbols to export on request
18  
19 =pod
20  
21 =encoding utf8
22  
23 =head1 NAME
24  
25 Convert::EDS::XDD - Convert CANopen EDS to POWERLINK XDD
26  
27 =head1 SYNOPSIS
28  
29 $ cpan Convert::EDS::XDD # install from CPAN
30 $ eds2xdd profile.eds > profile.xdd # Convert with the eds2xdd script
31  
32 =head1 DESCRIPTION
33  
34 EDS is an L<ini|Config::Tiny> based format specified by the CiA e.V. in order to describe CANopen devices. The Ethernet POWERLINK Standardization Group specifies an EDS-based L<XML|XML::Writer> format for EPL devices.
35  
36 This module takes in an EDS file or a string with its content and returns a XDD string. An L<eds2xdd> wrapper script is also installed into the C<PATH>.
37  
38 C<eds2xdd> is also available as a self-contained (fatpacked) script L<at the Github releases page|https://github.com/epl-viz/Convert-EDS-XDD/releases/latest>.
39  
40 =head1 LIMITATIONS
41  
42 May not handle all details of the EDS. Pull requests and reports (L<issues on Github|https://github.com/epl-viz/Convert-EDS-XDD/issues>) are welcome.
43  
44 =cut
45  
46 0 ▶sub _hex_or_dec {
Subroutine _hex_or_dec: not called CC=2 SCAR=17.9
47 0 my $num = shift;
48 0 ▶ return $num =~ /^0x/ ? hex($num) : $num;
Branch: 1 branch
BranchTrueFalseTotal
$num =~ /^0x/ ? : 0 0 0
49 }
50 9 ▶sub _hashref_filterout {
Subroutine _hashref_filterout: called CC=1 SCAR=0.0
51 9 my $hash = shift;
52 9 delete @$hash{ grep { not defined $hash->{$_} } keys %$hash };
53  
54 }
55 9 ▶sub _array_filterout {
Subroutine _array_filterout: called CC=3 SCAR=11.0
56 9 my $it = natatime 2, @_;
57 9 my @out;
58 9 ▶ while (my @vals = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@vals) = &$it()) 167 9 176
59 167 ▶ push @out, @vals if defined $vals[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $vals[1] 51 116 167
60 }
61 9 return @out;
62 }
63  
64  
65 4 my @PDOmapping_str_of = qw( no yes );
66 0 ▶sub _extract {
Subroutine _extract: not called CC=1 SCAR=6.9
67 0 my $obj = shift;
68 _array_filterout (
69 name => $obj->{ParameterName},
70 objectType => _hex_or_dec($obj->{ObjectType}),
71 dataType => sprintf("%04X", _hex_or_dec($obj->{DataType})),
72 accessType => $obj->{AccessType},
73 PDOmapping => $PDOmapping_str_of[$obj->{PDOMapping}],
74 lowLimit => _hex_or_dec($obj->{LowLimit}),
75 highLimit => _hex_or_dec($obj->{HighLimit}),
76 defaultValue => $obj->{DefaultValue},
77 actualValue => $obj->{ActualValue},
78 0 );
79 }
80  
81  
82 =head1 METHODS AND ARGUMENTS
83  
84 =over 4
85  
86 =item eds2xdd($filename, [$encoding])
87  
88 Here, the C<[]> indicate an optional parameter.
89  
90 Returns the EDS' content as XML string on success or undef on error in file contents.
91  
92 Function croaks if opening file fails.
93  
94 C<$encoding> may be used to indicate the encoding of the file, e.g. C<'utf8'> or
95 C<'encoding(iso-8859-1)'>.
96  
97 Do not add a prefix to C<$encoding>, such as C<< '<' >> or C<< '<:' >>.
98  
99 =cut
100  
101 1 ▶sub eds2xdd {
Subroutine eds2xdd: called CC=7 SCAR=28.4
Pod documented
102 1 my($file, $encoding) = @_;
103 1 ▶ croak 'No file name provided' if !defined $file || $file eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $file or $file eq "" 0 1 1
Condition33%1/3
not defined $file or $file eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table33%1/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
104  
105 # Slurp in the file.
106 1 ▶ $encoding = $encoding ? "<:$encoding" : '<';
Branch: 1 branch
BranchTrueFalseTotal
$encoding ? : 0 1 1
107 1 local $/;
108  
109 1 ▶ open(my $eds, $encoding, $file) or croak "Failed to open file '$file' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $eds, $encoding, $file or croak("Failed to open file '${file}' for reading: $!") 0 1 1
110 0 my $contents = <$eds>;
111 0 close($eds);
112  
113 0 ▶ croak "Reading from '$file' returned undef" unless defined $contents;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $contents 0 0 0
114 0 ▶ eds2xdd_string($contents) or return;
Branch: 1 branch
BranchTrueFalseTotal
eds2xdd_string($contents) or return 0 0 0
115 };
116  
117 4 my $template = do {
118 4 local $/;
119 <DATA>
120 4 };
121  
122  
123 =item eds2xdd_string($string)
124  
125 Returns the EDS string as XML string
126  
127 =cut
128  
129 9 ▶sub eds2xdd_string {
Subroutine eds2xdd_string: called CC=17 SCAR=43.7
Pod documented
130 9 my $str = shift;
131 9 $str =~ s/#.*//gm;
132 9 my $eds = Config::Tiny->read_string($str);
133  
134 9 my ($basename, $extension) = $eds->{FileInfo}->{FileName} =~ /^(.*)(\.[^.]*)/;
135 9 $basename = undef;
136  
137 9 my $comments = do {
138 9 ▶ if($eds->{Comments}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eds->{"Comments"}) 0 9 9
139 0 my $comments = "<!--\n" . ('*' x76) . "\n";
140 0 for my $i (1..$eds->{Comments}->{Lines}) {
141 0 $comments .= $eds->{Comments}->{"Line$i"} . "\n";
142 }
143 0 $comments .= ('*' x 76) . "\n-->";
144 }
145 9 delete $eds->{Comments};
146 };
147  
148 my %placeholder = _mktemplate(
149 fileCreator => $eds->{FileInfo}->{CreatedBy},
150 fileModifiedBy => $eds->{FileInfo}->{ModifiedBy},
151 ProfileName => $eds->{FileInfo}->{Description},
152 fileCreationTime => $eds->{FileInfo}->{CreationTime},
153 fileCreationDate => $eds->{FileInfo}->{CreationDate},
154 fileModificationTime => $eds->{FileInfo}->{ModificationTime},
155 fileModificationDate => $eds->{FileInfo}->{ModificationDate},
156 basename => $basename,
157 extension => $extension,
158 version => sprintf('%02u.%02u', $eds->{FileInfo}->{FileVersion},
159 $eds->{FileInfo}->{FileRevision}),
160  
161 vendorID => $eds->{DeviceInfo}->{VendorNumber},
162 vendorName => $eds->{DeviceInfo}->{VendorName},
163 productName => "$eds->{DeviceInfo}->{ProductName} ".
164 $eds->{DeviceInfo}->{ProductNumber},
165 product_version => $eds->{DeviceInfo}->{RevisionNumber},
166 9 comments => $comments,
167  
168 @_
169 );
170 9 delete $eds->{FileInfo};
171 9 delete $eds->{DeviceInfo};
172  
173 9 my $writer = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1, DATA_INDENT => 2);
174 9 $writer->startTag("ObjectList");
175  
176 9 my ($in_sublist, $in_6000, $in_2000, $in_1000) = (0) x 4;
177 9 my @sections = (sort keys %{$eds});
178 9 foreach my $section_index (0 .. @sections - 1) {
179 2 my $section = $sections[$section_index];
180 2 ▶ unless ($section =~ /([[:xdigit:]]{4})(?:sub([[:xdigit:]]))|([[:xdigit:]]{4})/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($section =~ /([[:xdigit:]]{4})(?:sub([[:xdigit:]]))|([[:xdigit:]]{4})/) 2 0 2
181 2 carp "Ignoring unknown section $section\n";
182 2 next;
183 }
184 0 ▶ my ($index, $subindex) = ($1 // $3, $2);
Condition0%0/3
$1 // $3
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 // $3
$1 $3
Truth table0%0/3
A // B
A= $1B= $3
ABresult
1X1
011
000
185  
186 0 my $obj = $eds->{$section};
187  
188 0 my @object = _extract($obj);
189 0 ▶ if (not defined $subindex) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $subindex) { } 0 0 0
190 0 ▶ $writer->endTag("Object") if $in_sublist;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sublist 0 0 0
191 0 ▶ if (!$in_6000 && hex($index) >= 0x6000) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $in_6000 and hex $index >= 24576) { } 0 0 0
elsif (not $in_2000 and hex $index >= 8192) { } 0 0 0
elsif (not $in_1000 and hex $index >= 4096) { } 0 0 0
Condition0%0/3
not $in_6000 and hex $index >= 24576
!ll&&!rl&&r
000
MC/DC0%0/2
not $in_6000 and hex $index >= 24576
not $in_6000 hex $index >= 24576
Truth table0%0/3
not(A) and B
A= not $in_6000B= hex $index >= 24576
ABresult
0X0
100
111
192 0 $writer->comment('Standardised Device Profile Area (0x6000 - 0x9FFF): may be used according to a CiA device profile.'
193 .'The profile to be used is given by NMT_DeviceType_U32');
194 0 $in_6000 = 1;
195 ▶ } elsif (!$in_2000 && hex($index) >= 0x2000) {
Condition0%0/3
not $in_2000 and hex $index >= 8192
!ll&&!rl&&r
000
MC/DC0%0/2
not $in_2000 and hex $index >= 8192
not $in_2000 hex $index >= 8192
Truth table0%0/3
not(A) and B
A= not $in_2000B= hex $index >= 8192
ABresult
0X0
100
111
196 0 $writer->comment('Manufacturer Specific Profile Area (0x2000 - 0x5FFF): may freely be used by the device manufacturer');
197 0 $in_2000 = 1;
198 ▶ } elsif (!$in_1000 && hex($index) >= 0x1000) {
Condition0%0/3
not $in_1000 and hex $index >= 4096
!ll&&!rl&&r
000
MC/DC0%0/2
not $in_1000 and hex $index >= 4096
not $in_1000 hex $index >= 4096
Truth table0%0/3
not(A) and B
A= not $in_1000B= hex $index >= 4096
ABresult
0X0
100
111
199 0 $writer->comment('Communication Profile Area (0x1000 - 0x1FFF): defined by EPSG 301');
200 0 $in_1000 = 1;
201 }
202  
203 0 $in_sublist = 0;
204  
205 0 unshift @object, index => sprintf('%04X', hex($index));
206 0 ▶ if ($sections[$section_index+1] =~ /^${index}sub/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sections[$section_index + 1] =~ /^${index}sub/) { } 0 0 0
207 0 $writer->startTag("Object", @object);
208 0 $in_sublist = 1;
209 } else {
210 0 $writer->emptyTag("Object", @object);
211 }
212 } else {
213 0 unshift @object, subIndex => sprintf('%02X', hex($subindex));
214 0 $writer->emptyTag("SubObject", @object);
215 }
216 }
217  
218 9 ▶ $writer->endTag("Object") if $in_sublist;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sublist 0 9 9
219 9 $writer->endTag("ObjectList");
220  
221 9 my $ObjectList = $writer->end();
222 9 my $xdd = $template;
223 9 ▶ if ($xdd =~ s/^([ \t]+?)\$ObjectList/\$ObjectList/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xdd =~ s/^([ \t]+?)\$ObjectList/\$ObjectList/m) 9 0 9
224 9 my $ObjectList_indent = $1;
225 9 $ObjectList =~ s/^/$ObjectList_indent/mg;
226 }
227 9 $xdd =~ s/(\$\w+(?:\{\w+\})?)/$1/gee;
228 9 return $xdd;
229 }
230  
231 9 ▶sub _mktemplate {
Subroutine _mktemplate: called CC=1 SCAR=0.0
232 9 my $dt = DateTime->now();
233 9 @_ = _array_filterout(@_);
234 9 my %placeholder = (
235 basename => 'unknown',
236 extension => '',
237 date => $dt->ymd,
238 time => $dt->hms,
239 version => '01.00',
240 product_version => '1.00',
241  
242 #@_
243 );
244 %placeholder = (
245 fileName => "$placeholder{basename}.xdd",
246 comment => "Generated from $placeholder{basename}$placeholder{extension} by " . __PACKAGE__,
247 ProfileName => "POWERLINK $placeholder{basename}",
248  
249 fileCreator => __PACKAGE__,
250 fileCreationDate => $placeholder{date},
251 fileCreationTime => $placeholder{time},
252  
253 fileModifiedBy => __PACKAGE__,
254 fileModificationDate => $placeholder{date},
255 fileModificationTime => $placeholder{time},
256  
257 vendorName => 'Unknown vendor',
258 vendorID => '0x00000000',
259 productName => $placeholder{basename},
260 versionHW => $placeholder{product_version},
261 versionFW => $placeholder{product_version},
262 versionSW => $placeholder{product_version},
263  
264 9 transferRate => '100 MBit/s',
265  
266 @_
267 );
268 9 _hashref_filterout(\%placeholder);
269 9 %placeholder
270 }
271  
272  
273 4 1;
274  
275 =back
276  
277 =head1 GIT REPOSITORY
278  
279 L<http://github.com/epl-viz/Convert-EDS-XDD>
280  
281 =head1 SEE ALSO
282  
283 L<EPL-Viz - Visualization for Ethernet POWERLINK|http://github.com/epl-viz>
284  
285 =head1 AUTHOR
286  
287 Ahmad Fatoum C<< <athreef@cpan.org> >>, L<http://a3f.at>
288  
289 =head1 COPYRIGHT AND LICENSE
290  
291 Copyright (C) 2017-2018 Ahmad Fatoum
292  
293 This library is free software; you can redistribute it and/or modify
294 it under the same terms as Perl itself.
295  
296 =cut
297  
298 __DATA__
↑ Summary