Modbus/Exception.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 10 / 10 Conditioncond 100% 17 / 17 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 96.3% 78 / 81 CC 12 SCAR 19.3
CC
12
Cov
100%
new
11.0
stringify
2.0
BEGIN
1.0
CRAP 6.9
« Modbus/Server.pm ↑ Summary Modbus/Unit/Route.pm »
1 package Device::Modbus::Exception;
2  
3 13 ▶use parent 'Device::Modbus';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 my %exc_descr_for = (
11 1 => '1 - Function code not supported',
12 2 => '2 - Incorrect address or address not supported',
13 3 => '3 - Invalid data (either quantity or values)',
14 4 => '4 - Execution failed',
15 );
16  
17 66 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod undocumented
18 66 my ($class, %args) = @_;
19  
20 # Must receive either a function name or a function code
21 66 ▶ unless (exists $args{function} or exists $args{code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"function"} or exists $args{"code"}) 1 65 66
Condition100%3/3
exists $args{"function"} or exists $args{"code"}
l!l&&r!l&&!r
13521
MC/DC100%2/2
exists $args{"function"} or exists $args{"code"}
exists $args{"function"} exists $args{"code"}
Truth table100%3/3
A or B
A= exists $args{"function"}B= exists $args{"code"}
ABresult
1X1
011
000
22 1 croak 'Please specify either a function name or code to instantiate an exception';
23 }
24  
25 65 ▶ if (exists $args{function} && exists $Device::Modbus::code_for{$args{function}}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args{'function'} and exists $Device::Modbus::code_for{$args{'function'}}) { } 12 53 65
elsif (exists $args{'code'} and exists $Device::Modbus::function_for{$args{'code'} - 128}) { } 50 3 53
elsif (exists $args{'code'}) { } 2 1 3
Condition100%3/3
exists $args{"function"} and exists $Device::Modbus::code_for{$args{"function"}}
!ll&&!rl&&r
52112
MC/DC100%2/2
exists $args{"function"} and exists $Device::Modbus::code_for{$args{"function"}}
exists $args{"function"} exists $Device::Modbus::code_for{$args{"function"}}
Truth table100%3/3
A and B
A= exists $args{"function"}B= exists $Device::Modbus::code_for{$args{"function"}}
ABresult
0X0
100
111
26 12 $args{code} = $Device::Modbus::code_for{$args{function}} + 0x80;
27 }
28 ▶ elsif (exists $args{code} && exists $Device::Modbus::function_for{$args{code} - 0x80}) {
Condition100%3/3
exists $args{"code"} and exists $Device::Modbus::function_for{$args{"code"} - 128}
!ll&&!rl&&r
1250
MC/DC100%2/2
exists $args{"code"} and exists $Device::Modbus::function_for{$args{"code"} - 128}
exists $args{"code"} exists $Device::Modbus::function_for{$args{"code"} - 128}
Truth table100%3/3
A and B
A= exists $args{"code"}B= exists $Device::Modbus::function_for{$args{"code"} - 128}
ABresult
0X0
100
111
29 50 $args{function} = $Device::Modbus::function_for{$args{code} - 0x80};
30 }
31 elsif (exists $args{code}) {
32 2 $args{function} = 'Non-supported function';
33 }
34 else {
35 1 croak "Function $args{function} is not supported";
36 }
37  
38 # Must receive an exception code
39 croak 'A valid exception code (between 1 and 4) is required'
40 64 ▶ unless exists $args{exception_code} && $args{exception_code} > 0 && $args{exception_code} <= 4;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exception_code"} and $args{"exception_code"} > 0 and $args{"exception_code"} <= 4 3 61 64
Condition100%6/6
exists $args{"exception_code"} and $args{"exception_code"} > 0 and $args{"exception_code"} <= 4
!ll&&!rl&&r
2161
exists $args{"exception_code"} and $args{"exception_code"} > 0
!ll&&!rl&&r
1162
MC/DC100%3/3
exists $args{"exception_code"} and $args{"exception_code"} > 0 and $args{"exception_code"} <= 4
exists $args{"exception_code"} $args{"exception_code"} > 0 $args{"exception_code"} <= 4
Truth table100%4/4
A and B and C
A= exists $args{"exception_code"}B= $args{"exception_code"} > 0C= $args{"exception_code"} <= 4
ABCresult
0XX0
10X0
1100
1111
41  
42 61 return bless \%args, $class;
43 }
44  
45 1 ▶sub pdu {
Subroutine pdu: called CC=1 SCAR=0.0
Pod undocumented
46 1 my $self = shift;
47 1 return pack('CC', $self->{code}, $self->{exception_code});
48 }
49  
50 26 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=6.9
Pod undocumented
51 26 my $self = shift;
52  
53 26 ▶ my $str = $Device::Modbus::function_for{$self->{code}-0x80} // 'Unknown';
Condition100%2/2
$Device::Modbus::function_for{$self->{"code"} - 128} // "Unknown"
l!l
251
MC/DC100%1/1
$Device::Modbus::function_for{$self->{"code"} - 128} // "Unknown"
$Device::Modbus::function_for{$self->{"code"} - 128}
Truth table100%2/2
A
A= $Device::Modbus::function_for{$self->{"code"} - 128}
Aresult
11
00
54 26 my $descr = $exc_descr_for{$self->{exception_code}};
55  
56 26 return "Exception for function <$str>: $descr";
57 }
58  
59 13 1;
« Modbus/Server.pm ↑ Summary Modbus/Unit/Route.pm »