TPLink/SmartHome/Direct.pm

filter: Statementstmt 26.3% 15 / 57 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 42.8% 3 / 7 Podpod 100% 3 / 3 totaltotal 26.2% 21 / 80 CC 8 SCAR 30.0
CC
8
Cov
22%
passthrough
42.0
encrypt
6.0
decrypt
6.0
CRAP 20.1
↑ Summary TPLink/Kasa.pm »
1 package Device::TPLink::SmartHome::Direct;
2  
3 1 ▶use 5.008003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 extends 'Device::TPLink::SmartHome';
8  
9 1 has addr => (
10 is => 'rw',
11 isa => 'Str',
12 );
13  
14 1 has port =>(
15 is => 'rw',
16 isa => 'Int',
17 );
18  
19 =head1 NAME
20  
21 Device::TPLink::SmartHome::Direct - Control TP-Link Smart Home devices over a direct TCP connection
22  
23 =head1 VERSION
24  
25 Version 0.02
26  
27 =cut
28  
29 1 our $VERSION = '0.02';
30  
31  
32 =head1 SYNOPSIS
33  
34 Quick summary of what the module does.
35  
36 Perhaps a little code snippet.
37  
38 use Device::TPLink::SmartHome::Direct;
39  
40 my $foo = Device::TPLink::SmartHome::Direct->new();
41 ...
42  
43 =head1 SUBROUTINES/METHODS
44  
45 =head2 passthrough
46  
47 Recieves an object, formats it as a JSON command and passes it to the device. Returns a response object.
48  
49 =cut
50  
51 0 ▶sub passthrough {
Subroutine passthrough: not called CC=6 SCAR=37.4
Pod documented
52 0 my $self = shift;
53 0 my ($requestData) = @_;
54  
55 0 my $remote_host = $self->addr;
56 0 my $remote_port = $self->port;
57  
58 0 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("PeerAddr", $remote_host, "PeerPort", $remote_port, "Proto", "tcp", "Type", 1) or die "Couldn't connect to ${remote_host}:$remote_port : $@\n" 0 0 0
59 PeerAddr => $remote_host,
60 PeerPort => $remote_port,
61 Proto => "tcp",
62 Type => SOCK_STREAM
63 ) or die "Couldn't connect to $remote_host:$remote_port : $@\n";
64  
65 0 my $json = JSON->new->allow_nonref;
66 # Fun fact: For the HS105, your JSON cannot have any newlines in it, so no pretty JSON for us...
67 # $json = $json->pretty(1);
68 0 my $plaintext = $json->encode($requestData);
69 0 my $ciphertext = encrypt($plaintext);
70 0 ▶ print $socket $ciphertext or die "Socket not connected? ";
Branch: 1 branch
BranchTrueFalseTotal
print $socket $ciphertext or die "Socket not connected? " 0 0 0
71 0 my $responseData;
72 0 my $responseLength = 1;
73 0 ▶ while ($responseLength) {
Branch: 1 branch
BranchTrueFalseTotal
if ($responseLength) 0 0 0
74 0 my $buffer;
75 0 my $retValue = $socket->recv($buffer, 4096);
76 0 $responseData .= $buffer;
77 0 $responseLength = unpack("N", substr($responseData,0,4));
78 0 ▶ if (length($responseData) > 0 && length($responseData) >= ($responseLength + 4)) { $responseLength = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $responseData > 0 and length $responseData >= $responseLength + 4) 0 0 0
Condition0%0/3
length $responseData > 0 and length $responseData >= $responseLength + 4
!ll&&!rl&&r
000
MC/DC0%0/2
length $responseData > 0 and length $responseData >= $responseLength + 4
length $responseData > 0 length $responseData >= $responseLength + 4
Truth table0%0/3
A and B
A= length $responseData > 0B= length $responseData >= $responseLength + 4
ABresult
0X0
100
111
79 }
80 0 $responseData = decrypt($responseData);
81 0 my $responseDataScalar = $json->decode($responseData);
82 0 return $responseDataScalar;
83 }
84  
85 =head2 function2
86  
87 =cut
88  
89 # Pass the command to the device
90 0 ▶around [qw(on off getSystemInfo reboot)] => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 my $next = shift;
92 0 my $self = shift;
93 0 my $requestData = $next->($self);
94 0 return $self->passthrough($requestData);
95 1 };
96  
97 =head2 encrypt
98  
99 Encrypts plain text in the manner expected by TP-Link devices.
100  
101 =cut
102  
103 0 ▶sub encrypt {
Subroutine encrypt: not called CC=2 SCAR=17.9
Pod documented
104 0 my $plaintext = shift;
105 0 my $key = 171;
106 0 my $ciphertext = pack("N", length($plaintext));
107 0 foreach my $i (split //, $plaintext) {
108 0 my $a = $key ^ ord($i);
109 0 $key = $a;
110 0 $ciphertext = $ciphertext . chr($a);
111 }
112 0 return $ciphertext;
113 }
114  
115 =head2 decrypt
116  
117 Decrypts text returned from TP-Link device.
118  
119 =cut
120  
121 0 ▶sub decrypt {
Subroutine decrypt: not called CC=2 SCAR=17.9
Pod documented
122 0 my $ciphertext = shift;
123 0 $ciphertext = substr($ciphertext,4);
124 0 my $key = 171;
125 0 my $plaintext = "";
126 0 foreach my $i (split //, $ciphertext) {
127 0 my $a = $key ^ ord($i);
128 0 $key = ord($i);
129 0 $plaintext = $plaintext . chr($a);
130 }
131 0 return $plaintext;
132 }
133  
134  
135 =head1 AUTHOR
136  
137 Verlin Henderson, C<< <verlin at gmail.com> >>
138  
139 =head1 BUGS / SUPPORT
140  
141 To report any bugs or feature requests, please use the github issue tracker: L<https://github.com/verlin/Device-TPLink/issues>
142  
143 =head1 ACKNOWLEDGEMENTS
144  
145  
146 =head1 LICENSE AND COPYRIGHT
147  
148 Copyright 2018 Verlin Henderson.
149  
150 This program is free software; you can redistribute it and/or modify it
151 under the terms of the the Artistic License (2.0). You may obtain a
152 copy of the full license at:
153  
154 L<http://www.perlfoundation.org/artistic_license_2_0>
155  
156 Any use, modification, and distribution of the Standard or Modified
157 Versions is governed by this Artistic License. By using, modifying or
158 distributing the Package, you accept this license. Do not use, modify,
159 or distribute the Package, if you do not accept this license.
160  
161 If your Modified Version has been derived from a Modified Version made
162 by someone other than you, you are nevertheless required to ensure that
163 your Modified Version complies with the requirements of this license.
164  
165 This license does not grant you the right to use any trademark, service
166 mark, tradename, or logo of the Copyright Holder.
167  
168 This license includes the non-exclusive, worldwide, free-of-charge
169 patent license to make, have made, use, offer to sell, sell, import and
170 otherwise transfer the Package with respect to any patent claims
171 licensable by the Copyright Holder that are necessarily infringed by the
172 Package. If you institute patent litigation (including a cross-claim or
173 counterclaim) against any party alleging that the Package constitutes
174 direct or contributory patent infringement, then this Artistic License
175 to you shall terminate on the date that such litigation is filed.
176  
177 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
178 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
179 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
180 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
181 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
182 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
183 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
184 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
185  
186  
187 =cut
188  
189 1 __PACKAGE__->meta->make_immutable;
190  
191 1 1; # End of Device::TPLink::SmartHome::Direct
↑ Summary TPLink/Kasa.pm »