blib/lib/Tvh/Htsp/Client.pm

filter: Statementstmt 54.7% 145 / 265 Branchbran 38.1% 74 / 194 Conditioncond 16.2% 6 / 37 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 73.6% 14 / 19 Podpod 70.0% 7 / 10 totaltotal 45.0% 246 / 546 CC 114 SCAR 51.5
CC
114
Cov
45%
htsp_recv
600.0
htsmsg_field_deserialise
147.2
getChanNamId
132.0
CRAP 171.7
↑ Summary
1 package Tvh::Htsp::Client;
2 # Tvheadend HTSP client library written in perl
3 # https://docs.tvheadend.org/documentation/development/htsp
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Socket qw(AF_INET SOCK_STREAM);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LooksLike;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our $VERSION = '0.06'; # set the version for version checking
12 1 ▶sub new {
Subroutine new: called CC=9 SCAR=26.5
Pod documented
13 1 my ($class, $args) = @_;
14 my $self = {
15 host => $args->{host} // 'localhost',
16 port => $args->{port} // 9982,
17 debug_info => $args->{debug_info} // 0,
18 no_client => $args->{no_client} // 0,
19 1 ▶ epgdb_v3 => $args->{epgdb_v3} // 0,
Condition50%5/10
$args->{"host"} // "localhost"
l!l
01
$args->{"port"} // 9982
l!l
01
$args->{"debug_info"} // 0
l!l
10
$args->{"no_client"} // 0
l!l
10
$args->{"epgdb_v3"} // 0
l!l
01
MC/DC0%0/1
$args->{"host"} // "localhost"
$args->{"host"}
MC/DC0%0/1
$args->{"port"} // 9982
$args->{"port"}
MC/DC0%0/1
$args->{"debug_info"} // 0
$args->{"debug_info"}
MC/DC0%0/1
$args->{"no_client"} // 0
$args->{"no_client"}
MC/DC0%0/1
$args->{"epgdb_v3"} // 0
$args->{"epgdb_v3"}
Truth table50%1/2
A
A= $args->{"host"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"port"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"debug_info"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"no_client"}
Aresult
11
00
Truth table50%1/2
A
A= $args->{"epgdb_v3"}
Aresult
11
00
20 db => [],
21 };
22 1 ▶ $self->{template} = $self->{epgdb_v3} ? "w" : "N"; # unpack template for length of htsp messages to be deserialised
Branch: 1 branch
BranchTrueFalseTotal
$self->{'epgdb_v3'} ? : 0 1 1
23 1 ▶ unless ($self->{no_client}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_client"}) 0 1 1
24 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 $self->{client} = IO::Socket->new(
26 Domain => AF_INET,
27 Type => SOCK_STREAM,
28 proto => 'tcp',
29 PeerHost => "$self->{host}",
30 PeerPort => $self->{port},
31 0 ▶ ) || die "Error ".(caller(0))[3].": can't open socket: $IO::Socket::errstr";
Condition0%0/2
'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerHost', "$self->{'host'}", 'PeerPort', $self->{'port'}) || die('Error ' . (caller 0)[3] . ": can't open socket: $IO::Socket::errstr")
l!l
00
MC/DC0%0/1
'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerHost', "$self->{'host'}", 'PeerPort', $self->{'port'}) || die('Error ' . (caller 0)[3] . ": can't open socket: $IO::Socket::errstr")
'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerHost', "$self->{'host'}", 'PeerPort', $self->{'port'})
Truth table0%0/2
A
A= 'IO::Socket'->new('Domain', 2, 'Type', 1, 'proto', 'tcp', 'PeerHost', "$self->{'host'}", 'PeerPort', $self->{'port'})
Aresult
11
00
32 # my $peer_addr = $self->{client}->connected();
33 # if ($peer_addr) { say "Connected to $peer_addr"; }
34 }
35 1 return bless $self, $class;
36 }
37 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
38 # close IO::Socket
39 1 my $self = shift;
40 1 ▶ $self->{client}->close() if $self->{client};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"client"} 0 1 1
41 }
42 0 ▶sub getChanUuidId {
Subroutine getChanUuidId: not called CC=4 SCAR=30.0
Pod documented
43 # Get all channel uuid and ID
44 0 my $self = shift;
45 # Enable async to find channel uuid and ID
46 0 $self->htsp_send({'method' => 'enableAsyncMetadata'});
47 # Process messages
48 0 my $chan_uuid_id = {};
49 0 while (1) {
50 0 my $reply = $self->htsp_recv();
51 0 ▶ if ($reply->{'method'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->{"method"}) 0 0 0
52 0 ▶ if ($reply->{'method'} eq 'channelAdd') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reply->{'method'} eq 'channelAdd') { } 0 0 0
elsif ($reply->{'method'} eq 'initialSyncCompleted') { } 0 0 0
53 0 $chan_uuid_id->{$reply->{'channelIdStr'}} = $reply->{'channelId'};
54 }
55 elsif ($reply->{'method'} eq 'initialSyncCompleted') {
56 0 last;
57 }
58 }
59 }
60 0 return $chan_uuid_id;
61 }
62 0 ▶sub getChanNamId {
Subroutine getChanNamId: not called CC=11 SCAR=48.8
Pod documented
63 # Get 'channelName' and channelId'
64 0 my $self = shift;
65 0 my $channel = shift;
66 0 my ($chan_name, $chan_id);
67 0 ▶ if (LooksLike::integer($channel) and $channel > 100_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if (LooksLike::integer($channel) and $channel > 100000000) { } 0 0 0
Condition0%0/3
LooksLike::integer($channel) and $channel > 100000000
!ll&&!rl&&r
000
MC/DC0%0/2
LooksLike::integer($channel) and $channel > 100000000
LooksLike::integer($channel) $channel > 100000000
Truth table0%0/3
A and B
A= LooksLike::integer($channel)B= $channel > 100000000
ABresult
0X0
100
111
68 # $channel > 100_000_000 must be a 'channelId'
69 0 $chan_id = $channel;
70 # get channel name
71 0 my $reply = $self->htsp_send_recv({'method' => 'getChannel', 'channelId' => $channel});
72 0 ▶ if ($reply->{'channelName'} and $reply->{'channelId'} eq "$channel") {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->{'channelName'} and $reply->{'channelId'} eq "$channel") { } 0 0 0
Condition0%0/3
$reply->{"channelName"} and $reply->{"channelId"} eq "$channel"
!ll&&!rl&&r
000
MC/DC0%0/2
$reply->{"channelName"} and $reply->{"channelId"} eq "$channel"
$reply->{"channelName"} $reply->{"channelId"} eq "$channel"
Truth table0%0/3
A and B
A= $reply->{"channelName"}B= $reply->{"channelId"} eq "$channel"
ABresult
0X0
100
111
73 0 $chan_name = $reply->{'channelName'};
74 }
75 0 else {die "Error ".(caller(0))[3].": channelId '$channel' not found -- ".join(', ', map {"'$_ => $reply->{$_}'"} (sort keys $reply->%*))}
76 }
77 else {
78 # Enable async to find 'channelId' from 'channelName' or 'channelNumber'
79 0 $self->htsp_send({'method' => 'enableAsyncMetadata'});
80 # Process messages
81 0 my $chan_nam_id = {};
82 0 my $chan_num_id = {};
83 0 my $chan_num_nam = {};
84 0 while (1) {
85 0 my $reply = $self->htsp_recv();
86 0 ▶ if ($reply->{'method'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->{"method"}) 0 0 0
87 0 ▶ if ($reply->{'method'} eq 'channelAdd') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reply->{'method'} eq 'channelAdd') { } 0 0 0
elsif ($reply->{'method'} eq 'initialSyncCompleted') { } 0 0 0
88 0 $chan_nam_id->{$reply->{'channelName'}} = $reply->{'channelId'};
89 0 $chan_num_id->{$reply->{'channelNumber'}} = $reply->{'channelId'};
90 0 $chan_num_nam->{$reply->{'channelNumber'}} = $reply->{'channelName'};
91 }
92 elsif ($reply->{'method'} eq 'initialSyncCompleted') {
93 0 last;
94 }
95 }
96 }
97 0 ▶ if ($chan_nam_id->{$channel}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($chan_nam_id->{$channel}) { } 0 0 0
elsif ($chan_num_id->{$channel}) { } 0 0 0
elsif (LooksLike::integer($channel)) { } 0 0 0
98 # found 'channelName'
99 0 $chan_name = $channel;
100 0 $chan_id = $chan_nam_id->{$channel};
101 }
102 elsif ($chan_num_id->{$channel}) {
103 # found 'channelNumber'
104 0 $chan_name = $chan_num_nam->{$channel};
105 0 $chan_id = $chan_num_id->{$channel};
106 }
107 elsif (LooksLike::integer($channel)) {
108 # 'channelNumber' not found
109 0 my $chan_num_sorted = join(', ', map {"'$_ $chan_num_nam->{$_}'"} (sort {$a <=> $b} keys $chan_num_nam->%*));
110 0 die ("Error ".(caller(0))[3].": channelNumber '$channel' not found in [ $chan_num_sorted ]");
111 }
112 else {
113 # 'channelName' not found
114 0 my $chan_nam_sorted = join(', ', map {"'$_ $chan_num_nam->{$_}'"} (sort {$a <=> $b} keys $chan_num_nam->%*));
115 0 die ("Error ".(caller(0))[3].": channelName '$channel' not found in [ $chan_nam_sorted ]");
116 }
117 }
118 0 return ($chan_name, $chan_id);
119 }
120 0 ▶sub htsp_send_recv {
Subroutine htsp_send_recv: not called CC=1 SCAR=6.9
Pod documented
121 # send and receive HTSP message
122 0 my $self = shift;
123 0 $self->htsp_send(shift); # serialise and send
124 0 return $self->htsp_recv(); # receive and deserialise
125 }
126 0 ▶sub htsp_send {
Subroutine htsp_send: not called CC=5 SCAR=34.0
Pod documented
127 # send HTSP message
128 0 my $self = shift;
129 0 my $msg = shift;
130 0 state $i = 0;
131 0 $i++;
132 0 $self->{msg} = $msg;
133 0 my $htspmsg = $self->htsmsg_serialise($msg); # serialise
134 0 ▶ say STDERR join(' ',(unpack("H*",$htspmsg) =~ /../g)) if $self->{debug_info}; # convert serialised message to H = hex string (high nybble first)
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
135 0 ▶ say "$i sending '$msg->{'method'}' -- ".(caller(0))[3] if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
136 0 my $size = $self->{client}->send($htspmsg);
137 0 ▶ say " sent data of length $size" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
138 0 ▶ $self->htsmsg_deserialise(\$htspmsg) if $self->{debug_info}; # deserialise
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
139 0 return $size;
140 }
141 0 ▶sub htsp_recv {
Subroutine htsp_recv: not called CC=24 SCAR=64.0
Pod documented
142 # receive HTSP message
143 0 my $self = shift;
144 0 my ($buffer, $length) = ('', 0);
145 0 state $rest = '';
146 0 state $i = 0;
147 0 $i++;
148 0 $buffer = $rest; # take over the 'rest' = remaining buffer from the previous call
149 0 ▶ $length = unpack("N",substr($buffer,0,4,'')) if ($buffer); # Total length of message = 4 byte integer "network" big-endian byte-order
Branch: 1 branch
BranchTrueFalseTotal
if $buffer 0 0 0
150 0 ▶ while (not $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer) 0 0 0
151 # Determine beginning and length of the message
152 0 ▶ $self->{client}->recv($buffer, 4096) // die "Error ".(caller(0))[3].": from socket: $IO::Socket::errstr"; # get bytes from HTSP server
Condition0%0/2
$self->{"client"}->recv($buffer, 4096) // die('Error ' . (caller 0)[3] . ": from socket: $IO::Socket::errstr")
l!l
00
MC/DC0%0/1
$self->{"client"}->recv($buffer, 4096) // die('Error ' . (caller 0)[3] . ": from socket: $IO::Socket::errstr")
$self->{"client"}->recv($buffer, 4096)
Truth table0%0/2
A
A= $self->{"client"}->recv($buffer, 4096)
Aresult
11
00
153 0 ▶ die "Error ".(caller(0))[3].": received '$buffer' of length ".length($buffer) unless $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer 0 0 0
154 0 ▶ say "$i received data of length ".length($buffer)." -- ".(caller(0))[3] if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
155 0 ▶ say STDERR join(' ',(unpack("H*",$buffer) =~ /../g)) if $self->{debug_info}; # convert serialised message to H = hex string (high nybble first)
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
156 0 ▶ while ($length == 0 and $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 0 and $buffer) 0 0 0
Condition0%0/3
$length == 0 and $buffer
!ll&&!rl&&r
000
MC/DC0%0/2
$length == 0 and $buffer
$length == 0 $buffer
Truth table0%0/3
A and B
A= $length == 0B= $buffer
ABresult
0X0
100
111
157 # enableAsyncMetadata first transmits a Null-Byte, we need to get rid of this
158 0 $length = unpack("N",substr($buffer,0,4,'')); # Total length of message = 4 byte integer "network" big-endian byte-order
159 0 ▶ say " message length $length" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
160 }
161 0 ▶ return({ response => 0 }) if ($length == 0 and not $buffer and $self->{msg}->{method} ne "enableAsyncMetadata");
Branch: 1 branch
BranchTrueFalseTotal
if $length == 0 and not $buffer and $self->{"msg"}{"method"} ne "enableAsyncMetadata" 0 0 0
Condition0%0/6
$length == 0 and not $buffer and $self->{"msg"}{"method"} ne "enableAsyncMetadata"
!ll&&!rl&&r
000
$length == 0 and not $buffer
!ll&&!rl&&r
000
MC/DC0%0/3
$length == 0 and not $buffer and $self->{"msg"}{"method"} ne "enableAsyncMetadata"
$length == 0 not $buffer $self->{"msg"}{"method"} ne "enableAsyncMetadata"
Truth table0%0/4
A and not(B) and C
A= $length == 0B= not $bufferC= $self->{"msg"}{"method"} ne "enableAsyncMetadata"
ABCresult
0XX0
10X0
1100
1111
162 }
163 0 ▶ if ($length > 10_000_000 and $self->{debug_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 10000000 and $self->{"debug_info"}) 0 0 0
Condition0%0/3
$length > 10000000 and $self->{"debug_info"}
!ll&&!rl&&r
000
MC/DC0%0/2
$length > 10000000 and $self->{"debug_info"}
$length > 10000000 $self->{"debug_info"}
Truth table0%0/3
A and B
A= $length > 10000000B= $self->{"debug_info"}
ABresult
0X0
100
111
164 # a likely incorrect excessive length, because the bytes are not being interpreted correctly
165 0 say STDERR join(' ',(unpack("H*",$buffer) =~ /../g)); # convert serialised message to H = hex string (high nybble first)
166 0 die ("Error ".(caller(0))[3].": message length '$length' > 10_000_000, not realistic");
167 }
168 0 ▶ while ($length > length($buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > length $buffer) 0 0 0
169 # the message length requires more bytes
170 0 ▶ say " message length $length > ".length($buffer)." length(\$buffer)" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
171 0 ▶ $self->{client}->recv(my $buffer0, min($length-length($buffer),4096)) // die "Error ".(caller(0))[3].": from socket: $IO::Socket::errstr"; # get bytes from HTSP server
Condition0%0/2
$self->{"client"}->recv(my $buffer0, &min($length - length($buffer), 4096)) // die('Error ' . (caller 0)[3] . ": from socket: $IO::Socket::errstr")
l!l
00
MC/DC0%0/1
$self->{"client"}->recv(my $buffer0, &min($length - length($buffer), 4096)) // die('Error ' . (caller 0)[3] . ": from socket: $IO::Socket::errstr")
$self->{"client"}->recv(my $buffer0, &min($length - length($buffer), 4096))
Truth table0%0/2
A
A= $self->{"client"}->recv(my $buffer0, &min($length - length($buffer), 4096))
Aresult
11
00
172 0 ▶ die "Error ".(caller(0))[3].": received '$buffer0' of length ".length($buffer0) unless $buffer0;
Branch: 1 branch
BranchTrueFalseTotal
unless $buffer0 0 0 0
173 0 ▶ say " received data of length ".length($buffer0) if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
174 0 ▶ say STDERR join(' ',(unpack("H*",$buffer0) =~ /../g)) if $self->{debug_info}; # convert serialised message to H = hex string (high nybble first)
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
175 0 $buffer .= $buffer0; # append the received bytes to the response '$buffer'
176 }
177 0 ▶ if ($length <= length($buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= length $buffer) 0 0 0
178 # the response contains additional bytes of the subsequent message
179 0 ▶ say " message length $length <= ".length($buffer)." length(\$buffer)" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
180 0 $rest = $buffer; # transfer response to '$rest'
181 0 $buffer = substr($rest,0,$length,''); # shorten response '$buffer' to the required message '$length', keep the remainder in '$rest' for the subsequent call to 'htsp_recv'
182 }
183 0 ▶ say "$i buffer length -- ".length($buffer)." -- ".(caller(0))[3] if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
184 0 my $htspmsg = pack("N",$length).$buffer; # prepend the previously removed 4 bytes with the message length
185 0 return $self->htsmsg_deserialise(\$htspmsg); # deserialise the message an return it
186 }
187 7 ▶sub htsmsg_serialise {
Subroutine htsmsg_serialise: called CC=24 SCAR=33.4
Pod undocumented
188 # serialise a HTSP message
189 7 my $self = shift;
190 7 my $msg = shift;
191 7 state $sub_message = 0;
192 7 state $i=0;
193 7 ▶ say ++$i." -- ".(caller(0))[3] if $self->{debug_info} and not $sub_message;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} and not $sub_message 0 7 7
Condition33%1/3
$self->{"debug_info"} and not $sub_message
!ll&&!rl&&r
700
MC/DC0%0/2
$self->{"debug_info"} and not $sub_message
$self->{"debug_info"} not $sub_message
Truth table33%1/3
A and not(B)
A= $self->{"debug_info"}B= not $sub_message
ABresult
0X0
100
111
194 7 my $htspmsg = '';
195 7 my ($ishash, @keys, @vals);
196 7 ▶ if (ref $msg eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $msg eq 'HASH') { } 5 2 7
elsif (ref $msg eq 'ARRAY') { } 2 0 2
197 5 $ishash = 1;
198 5 @keys = keys $msg->%*;
199 5 @vals = values $msg->%*;
200 }
201 elsif (ref $msg eq "ARRAY") {
202 2 ▶ die ("Error ".(caller(0))[3].": root message must be of type 'Map' = a 'HASH' reference, not '".ref($msg)."'") unless $sub_message;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_message 0 2 2
203 2 $ishash = 0;
204 2 @keys = keys $msg->@*;
205 2 @vals = values $msg->@*;
206 }
207 else {
208 0 die ("Error ".(caller(0))[3].": message must be a 'HASH' or 'ARRAY' reference, not '".ref($msg)."'");
209 }
210 7 for my $key (@keys) {
211 25 my $val = shift @vals;
212 25 ▶ say ' 'x($sub_message+1)."$key => $val" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 25 25
213 25 ▶ if (ref $val eq "HASH") {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $val eq 'HASH') { } 3 22 25
elsif (ref $val eq 'ARRAY') { } 2 20 22
elsif (ref $val ne '') { } 0 20 20
elsif (LooksLike::integer($val)) { } 7 13 20
elsif (LooksLike::numeric($val)) { } 0 13 13
214 # Map = 1 = Sub message of type map
215 3 my $type = pack("C",1); # 1 byte integer
216 3 ▶ my $namelength = $ishash ? pack("C",length($key)) : chr(0); # 1 byte integer
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 1 2 3
217 3 ▶ my $name = $ishash ? $key : ''; # string
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 1 2 3
218 3 $sub_message++;
219 3 my $data = $self->htsmsg_serialise($val);
220 3 $sub_message--;
221 3 my $datalength = pack("N",length($data)); # 4 byte integer "network" big-endian byte-order
222 3 $htspmsg .= $type.$namelength.$datalength.$name.$data;
223 }
224 elsif (ref $val eq "ARRAY") {
225 # List = 5 = Sub message of type list
226 2 my $type = pack("C",5); # 1 byte integer
227 2 ▶ my $namelength = $ishash ? pack("C",length($key)) : chr(0); # 1 byte integer
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 1 1 2
228 2 ▶ my $name = $ishash ? $key : ''; # string
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 1 1 2
229 2 $sub_message++;
230 2 my $data = $self->htsmsg_serialise($val);
231 2 $sub_message--;
232 2 my $datalength = pack("N",length($data)); # 4 byte integer "network" big-endian byte-order
233 2 $htspmsg .= $type.$namelength.$datalength.$name.$data;
234 }
235 elsif (ref $val ne "") {
236 0 die ("Error ".(caller(0))[3].": field value cannot be a reference of type '".ref($val)."'");
237 }
238 elsif (LooksLike::integer($val)) {
239 # S64 = 2 = Signed 64bit integer
240 7 my $type = pack("C",2); # 1 byte integer
241 7 ▶ my $namelength = $ishash ? pack("C",length($key)) : chr(0); # 1 byte integer
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 4 3 7
242 7 ▶ my $name = $ishash ? $key : ''; # string
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 4 3 7
243 7 my $data = pack("q<",$val); # 64 bit = 8 byte signed integer little-endian byte-order -> q = signed quad (64-bit) value
244 # Integers are encoded using a very simple variable length encoding. All leading bytes that are 0 is discarded.
245 7 $data =~ s/\0{1,7}$//; # remove Null-Byte from the end, keep a first one
246 7 my $datalength = pack("N",length($data)); # 4 byte integer "network" big-endian byte-order
247 7 $htspmsg .= $type.$namelength.$datalength.$name.$data;
248 }
249 elsif (LooksLike::numeric($val)) {
250 # Dbl = 6 = Double precision floating point
251 0 my $type = pack("C",6); # 1 byte integer
252 0 ▶ my $namelength = $ishash ? pack("C",length($key)) : chr(0); # 1 byte integer
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 0 0 0
253 0 ▶ my $name = $ishash ? $key : ''; # string
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 0 0 0
254 0 my $data = pack("d",$val); # d = A double-precision float in native format
255 0 my $datalength = pack("N",length($data)); # 4 byte integer "network" big-endian byte-order
256 0 $htspmsg .= $type.$namelength.$datalength.$name.$data;
257 }
258 else {
259 # Str = 3 = UTF-8 encoded string
260 13 my $type = pack("C",3); # 1 byte integer
261 13 ▶ my $namelength = $ishash ? pack("C",length($key)) : chr(0); # 1 byte integer
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 12 1 13
262 13 ▶ my $name = $ishash ? $key : ''; # string
Branch: 1 branch
BranchTrueFalseTotal
$ishash ? : 12 1 13
263 13 my $data = $val; # UTF-8 encoded string
264 13 my $datalength = pack("N",length($data)); # 4 byte integer "network" big-endian byte-order
265 13 $htspmsg .= $type.$namelength.$datalength.$name.$data;
266 }
267 }
268 # say ' 'x($sub_message+1)."sub_message = ".$sub_message if $self->{debug_info};
269 # in case of root message prepend message length
270 7 ▶ return $sub_message ? $htspmsg : pack("N",length($htspmsg)).$htspmsg;
Branch: 1 branch
BranchTrueFalseTotal
$sub_message ? : 5 2 7
271 }
272 4 ▶sub htsmsg_deserialise {
Subroutine htsmsg_deserialise: called CC=4 SCAR=13.9
Pod documented
273 # deserialise a HTSP message
274 # https://docs.tvheadend.org/documentation/development/htsp/htsmsg-binary-format
275 4 my $self = shift;
276 4 my $htsmsg = shift; # reference to the message to be deserialised
277 4 state $i=0;
278 4 my $bd={}; # root message must be of type 'Map' = a 'HASH' reference
279 # Root body
280 # Length of Root body = 4 byte integer = Total length of message (not including this length field itself)
281 4 my $length = $self->htsmsg_message_length($htsmsg); # Total length of message
282 4 ▶ say ++$i." -- $length -- ".(caller(0))[3] if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 4 4
283 4 my $body = substr($$htsmsg,0,$length,''); # keep additional length in '$htsmsg' for any subsequent messages
284 # Body = HTSMSG-Field * N = Fields in the root body
285 4 ▶ while (length $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body) 18 4 22
286 18 $self->htsmsg_field_deserialise (\$body, $bd);
287 }
288 4 ▶ push($self->{db}->@*, $bd) if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 4 4
289 4 return $bd;
290 }
291 43 ▶sub htsmsg_field_deserialise {
Subroutine htsmsg_field_deserialise: called CC=39 SCAR=49.9
Pod undocumented
292 # deserialise a HTSP field
293 43 my $self = shift;
294 43 my $htsmsg = shift;
295 43 my $bd = shift;
296 43 state $indent=1;
297 # process HTSMSG-Field
298 # Type = 1 byte integer = Type of field by ID
299 43 my $type = unpack("C",substr($$htsmsg,0,1,''));
300 # Namelength = 1 byte integer = Length of name of field. If a field is part of a list message this must be 0
301 43 my $namelength = unpack("C",substr($$htsmsg,0,1,''));
302 # Datalength = 4 byte integer = Length of field data
303 43 my $datalength = $self->htsmsg_message_length($htsmsg); # determine data length
304 # Name = N bytes = Field name, length as specified by Namelength
305 43 my $name = substr($$htsmsg,0,$namelength,'');
306 # Data = N bytes = Field payload
307 43 my $data = substr($$htsmsg,0,$datalength,'');
308 43 ▶ if ($type == 1) {
Branch: 8 branches
BranchTrueFalseTotal
if ($type == 1) { } 3 40 43
elsif ($type == 2) { } 10 30 40
elsif ($type == 3) { } 26 4 30
elsif ($type == 4) { } 1 3 4
elsif ($type == 5) { } 3 0 3
elsif ($type == 7) { } 0 0 0
elsif ($type == 8) { } 0 0 0
elsif ($type == 6) { } 0 0 0
309 # Map = 1 = Sub message of type map
310 3 ▶ say ' 'x$indent."$type $namelength $datalength '$name'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 3 3
311 3 my $ref;
312 3 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 1 2 3
elsif (ref $bd eq 'ARRAY') { } 2 0 2
313 1 ▶ $bd->{"$name"} = {} unless $bd->{"$name"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$bd{"$name"} 1 0 1
314 1 $ref = $bd->{"$name"};
315 }
316 elsif (ref $bd eq "ARRAY") {
317 2 push($bd->@*, {});
318 2 $ref = $bd->@[-1];
319 }
320 3 ▶ while (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 11 3 14
321 11 $indent++;
322 11 $self->htsmsg_field_deserialise (\$data, $ref) ;
323 11 $indent--;
324 }
325 }
326 elsif ($type == 2) {
327 # S64 = 2 = Signed 64bit integer
328 # Integers are encoded using a very simple variable length encoding. All leading bytes that are 0 is discarded.
329 10 ▶ while (length($data) < 8) {$data .= chr(0);} # fill up with Null-Bytes
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 8) 68 10 78
330 10 $data = unpack("q<",$data); # 64 bit = 8 byte signed integer little-endian byte-order -> q = signed quad (64-bit) value
331 10 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 7 3 10
elsif (ref $bd eq 'ARRAY') { } 3 0 3
332 7 $bd->{"$name"} = $data;
333 }
334 elsif (ref $bd eq "ARRAY") {
335 3 push($bd->@*, $data);
336 }
337 10 ▶ say ' 'x$indent."$type $namelength $datalength '$name' '$data'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 10 10
338 }
339 elsif ($type == 3) {
340 # Str = 3 = UTF-8 encoded string
341 # utf8::decode($data);
342 26 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 18 8 26
elsif (ref $bd eq 'ARRAY') { } 8 0 8
343 18 $bd->{"$name"} = $data;
344 }
345 elsif (ref $bd eq "ARRAY") {
346 8 push($bd->@*, $data);
347 }
348 26 ▶ say ' 'x$indent."$type $namelength $datalength '$name' '$data'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 26 26
349 }
350 elsif ($type == 4) {
351 # Bin = 4 = Binary blob
352 1 $data = unpack("H*",$data); # convert binary blob to H = hex string (high nybble first)
353 1 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 1 0 1
elsif (ref $bd eq 'ARRAY') { } 0 0 0
354 1 $bd->{"$name"} = $data;
355 }
356 elsif (ref $bd eq "ARRAY") {
357 0 push($bd->@*, $data);
358 }
359 1 ▶ say ' 'x$indent."$type $namelength $datalength '$name' '$data'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 1 1
360 }
361 elsif ($type == 5) {
362 # List = 5 = Sub message of type list
363 3 ▶ say ' 'x$indent."$type $namelength $datalength '$name'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 3 3
364 3 my $ref;
365 3 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 2 1 3
elsif (ref $bd eq 'ARRAY') { } 1 0 1
366 2 ▶ $bd->{"$name"} = [] unless $bd->{"$name"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$bd{"$name"} 2 0 2
367 2 $ref = $bd->{"$name"};
368 }
369 elsif (ref $bd eq "ARRAY") {
370 1 push($bd->@*, []);
371 1 $ref = $bd->@[-1];
372 }
373 3 ▶ while (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 14 3 17
374 14 $indent++;
375 14 $self->htsmsg_field_deserialise(\$data, $ref);
376 14 $indent--;
377 }
378 }
379 elsif ($type == 7) {
380 # Bool = 7 = Boolean
381 0 ▶ $data = $datalength ? unpack("C",$data) : 0; # C = 1 byte unsigned char
Branch: 1 branch
BranchTrueFalseTotal
$datalength ? : 0 0 0
382 0 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 0 0 0
elsif (ref $bd eq 'ARRAY') { } 0 0 0
383 0 $bd->{"$name"} = $data;
384 }
385 elsif (ref $bd eq "ARRAY") {
386 0 push($bd->@*, $data);
387 }
388 0 ▶ say ' 'x$indent."$type $namelength $datalength '$name' '$data'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
389 }
390 elsif ($type == 8) {
391 # UUID = 8 = 64 bit UUID in binary format
392 0 $data = unpack("H*",$data); # 16 byte = 128 bit binary number -> H = hex string (high nybble first)
393 0 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 0 0 0
elsif (ref $bd eq 'ARRAY') { } 0 0 0
394 0 $bd->{"$name"} = $data;
395 }
396 elsif (ref $bd eq "ARRAY") {
397 0 push($bd->@*, $data);
398 }
399 0 ▶ say ' 'x$indent."$type $namelength $datalength '$name' '$data'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
400 }
401 elsif ($type == 6) {
402 # Dbl = 6 = Double precision floating point
403 0 $data = unpack("d",$data); # d = A double-precision float in native format
404 0 ▶ if (ref $bd eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $bd eq 'HASH') { } 0 0 0
elsif (ref $bd eq 'ARRAY') { } 0 0 0
405 0 $bd->{"$name"} = $data;
406 }
407 elsif (ref $bd eq "ARRAY") {
408 0 push($bd->@*, $data);
409 }
410 0 ▶ say ' 'x$indent."$type $namelength $datalength '$name' '$data'" if $self->{debug_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug_info"} 0 0 0
411 }
412 else {
413 0 die ("Error ".(caller(0))[3].": encountered unknown field type ID '$type', must be one of 1..8");
414 }
415 }
416 47 ▶sub htsmsg_message_length {
Subroutine htsmsg_message_length: called CC=1 SCAR=0.0
Pod undocumented
417 # determine message or data length of a htsp message to be deserialised
418 47 my $self = shift;
419 47 my $htsmsg = shift;
420 47 my $template = $self->{template};
421 # for database version 3 -> a variable-length integer discarding leading zero bytes
422 # -> template = "w" a BER compressed integer, Bit eight (the high bit) is set on each byte except the last
423 # -- or --
424 # for HTSP protocol and database version 2 -> 4 byte integer "network" big-endian byte-order
425 # -> template = "N"
426 47 my $length = unpack("$template", $$htsmsg);
427 47 substr($$htsmsg,0,length(pack("$template", $length)),''); # remove number of bytes consumed
428 47 return $length;
429 # instead of template "w" we can do like this
430 # my ($bits, $seven_bit_chunks) = (1,''); # initalise
431 # while ($bits) {
432 # the continuation bit = most significant bit, MSB is set to 1, indicating that more bytes follow
433 # $bits = unpack("B8", substr($$htsmsg,0,1,'')); # get next byte as a string of 8 bits
434 # $seven_bit_chunks .= substr($bits,1,7,''); # add next 7-bit chunk
435 # }
436 # return oct('0b'.$seven_bit_chunks); # convert bits to value
437 }
438 1 1;
439 # __ end of package htsp_tvh_client __
440 __END__
↑ Summary