Constants.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 100% 18 / 18 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.4% 17 / 18 Podpod 100% 12 / 12 totaltotal 98.4% 128 / 130 CC 14 SCAR 8.6
CC
14
Cov
99%
dump_string
5.0
import
3.0
encode_remaining_length
3.0
CRAP 2.4
« Message/Connect.pm ↑ Summary Message/Publish.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Net::MQTT::Constants;
4 3 $Net::MQTT::Constants::VERSION = '1.143250';
5 # ABSTRACT: Module to export constants for MQTT protocol
6  
7  
8 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 my %constants =
11 (
12 MQTT_CONNECT => 0x1,
13 MQTT_CONNACK => 0x2,
14 MQTT_PUBLISH => 0x3,
15 MQTT_PUBACK => 0x4,
16 MQTT_PUBREC => 0x5,
17 MQTT_PUBREL => 0x6,
18 MQTT_PUBCOMP => 0x7,
19 MQTT_SUBSCRIBE => 0x8,
20 MQTT_SUBACK => 0x9,
21 MQTT_UNSUBSCRIBE => 0xa,
22 MQTT_UNSUBACK => 0xb,
23 MQTT_PINGREQ => 0xc,
24 MQTT_PINGRESP => 0xd,
25 MQTT_DISCONNECT => 0xe,
26  
27 MQTT_QOS_AT_MOST_ONCE => 0x0,
28 MQTT_QOS_AT_LEAST_ONCE => 0x1,
29 MQTT_QOS_EXACTLY_ONCE => 0x2,
30  
31 MQTT_CONNECT_ACCEPTED => 0,
32 MQTT_CONNECT_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION => 1,
33 MQTT_CONNECT_REFUSED_IDENTIFIER_REJECTED => 2,
34 MQTT_CONNECT_REFUSED_SERVER_UNAVAILABLE => 3,
35 MQTT_CONNECT_REFUSED_BAD_USER_NAME_OR_PASSWORD => 4,
36 MQTT_CONNECT_REFUSED_NOT_AUTHORIZED => 5,
37 );
38  
39 sub import {
40 3 ▶ no strict qw/refs/; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
41 42 ▶ my $pkg = caller(0);
Subroutine import: called CC=3 SCAR=11.0
42 42 foreach (keys %constants) {
43 966 my $v = $constants{$_};
44 966 ▶ *{$pkg.'::'.$_} = sub () { $v };
Subroutine __ANON__: not called CC=1 SCAR=0.4
45 }
46 42 foreach (qw/decode_byte encode_byte
47 decode_short encode_short
48 decode_string encode_string
49 decode_remaining_length encode_remaining_length
50 qos_string
51 message_type_string
52 dump_string
53 connect_return_code_string
54 /) {
55 504 *{$pkg.'::'.$_} = \&{$_};
56 }
57 }
58  
59  
60 sub decode_remaining_length {
61 25 ▶ my ($data, $offset) = @_;
Subroutine decode_remaining_length: called CC=2 SCAR=6.9
Pod documented
62 25 my $multiplier = 1;
63 25 my $v = 0;
64 25 my $d;
65 25 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$d & 128 and do { do { $d = decode_byte($data, $offset); $v += ($d & 127) * $multiplier; $multiplier *= 128 } } 5 23 28
66 30 $d = decode_byte($data, $offset);
67 28 $v += ($d&0x7f) * $multiplier;
68 28 $multiplier *= 128;
69 } while ($d&0x80);
70 23 $v
71 }
72  
73  
74 sub encode_remaining_length {
75 42 ▶ my $v = shift;
Subroutine encode_remaining_length: called CC=3 SCAR=11.0
Pod documented
76 42 my $o;
77 my $d;
78 42 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$d & 128 and do { do { $d = $v % 128; $v = int $v / 128; if ($v) { $d |= 128; } ; $o .= encode_byte($d) } } 2 42 44
79 44 $d = $v % 128;
80 44 $v = int($v/128);
81 44 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 2 42 44
82 2 $d |= 0x80;
83 }
84 44 $o .= encode_byte($d);
85 } while ($d&0x80);
86 42 $o;
87 }
88  
89  
90 sub decode_byte {
91 68 ▶ my ($data, $offset) = @_;
Subroutine decode_byte: called CC=2 SCAR=6.9
Pod documented
92 68 ▶ croak 'decode_byte: insufficient data' unless (length $data >= $$offset+1);
Branch: 1 branch
BranchTrueFalseTotal
unless length $data >= $$offset + 1 3 65 68
93 65 my $res = unpack 'C', substr $data, $$offset, 1;
94 65 $$offset++;
95 65 $res
96 }
97  
98  
99 sub encode_byte {
100 110 ▶ pack 'C', $_[0];
Subroutine encode_byte: called CC=1 SCAR=0.0
Pod documented
101 }
102  
103  
104 sub decode_short {
105 29 ▶ my ($data, $offset) = @_;
Subroutine decode_short: called CC=2 SCAR=6.9
Pod documented
106 29 ▶ croak 'decode_short: insufficient data' unless (length $data >= $$offset+2);
Branch: 1 branch
BranchTrueFalseTotal
unless length $data >= $$offset + 2 2 27 29
107 27 my $res = unpack 'n', substr $data, $$offset, 2;
108 27 $$offset += 2;
109 27 $res;
110 }
111  
112  
113 sub encode_short {
114 24 ▶ pack 'n', $_[0];
Subroutine encode_short: called CC=1 SCAR=0.0
Pod documented
115 }
116  
117  
118 sub decode_string {
119 16 ▶ my ($data, $offset) = @_;
Subroutine decode_string: called CC=2 SCAR=6.9
Pod documented
120 16 my $len = decode_short($data, $offset);
121 15 ▶ croak 'decode_string: insufficient data'
Branch: 1 branch
BranchTrueFalseTotal
unless length $data >= $$offset + $len 1 14 15
122 unless (length $data >= $$offset+$len);
123 14 my $res = substr $data, $$offset, $len;
124 14 $$offset += $len;
125 14 $res;
126 }
127  
128  
129 sub encode_string {
130 28 ▶ pack "n/a*", $_[0];
Subroutine encode_string: called CC=1 SCAR=0.0
Pod documented
131 }
132  
133  
134 sub qos_string {
135 48 ▶ [qw/at-most-once at-least-once exactly-once reserved/]->[$_[0]]
Subroutine qos_string: called CC=1 SCAR=0.0
Pod documented
136 }
137  
138  
139 sub message_type_string {
140 42 ▶ [qw/Reserved0 Connect ConnAck Publish PubAck PubRec PubRel PubComp
Subroutine message_type_string: called CC=1 SCAR=0.0
Pod documented
141 Subscribe SubAck Unsubscribe UnsubAck PingReq PingResp Disconnect
142 Reserved15/]->[$_[0]];
143 }
144  
145  
146 sub dump_string {
147 42 ▶ my $data = shift || '';
Subroutine dump_string: called CC=5 SCAR=16.1
Pod documented
Condition100%2/2
shift() || ''
l!l
1032
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
148 42 ▶ my $prefix = shift || '';
Condition100%2/2
shift() || ''
l!l
141
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
149 42 $prefix .= ' ';
150 42 my @lines;
151 42 ▶ while (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 26 42 68
152 26 my $d = substr $data, 0, 16, '';
153 26 my $line = unpack 'H*', $d;
154 26 $line =~ s/([A-F0-9]{2})/$1 /ig;
155 26 $d =~ s/[^ -~]/./g;
156 26 $line = sprintf "%-48s %s", $line, $d;
157 26 push @lines, $line
158 }
159 42 ▶ scalar @lines ? "\n".$prefix.(join "\n".$prefix, @lines) : ''
Branch: 1 branch
BranchTrueFalseTotal
scalar @lines ? : 10 32 42
160 }
161  
162  
163  
164 sub connect_return_code_string {
165 [
166 4 ▶ 'Connection Accepted',
Subroutine connect_return_code_string: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
["Connection Accepted", "Connection Refused: unacceptable protocol version", "Connection Refused: identifier rejected", "Connection Refused: server unavailable", "Connection Refused: bad user name or password", "Connection Refused: not authorized"]->[$_[0]] or "Reserved" 2 2 4
167 'Connection Refused: unacceptable protocol version',
168 'Connection Refused: identifier rejected',
169 'Connection Refused: server unavailable',
170 'Connection Refused: bad user name or password',
171 'Connection Refused: not authorized',
172 ]->[$_[0]] || 'Reserved'
173 }
174  
175 __END__
« Message/Connect.pm ↑ Summary Message/Publish.pm »