Bus/DEBUG.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 243 CC 13 SCAR 27.7
CC
13
Cov
0%
connection_write
42.0
connection_read
30.0
new
6.0
CRAP 15.9
1 package Lab::Bus::DEBUG;
2 #ABSTRACT: Interactive debug bus
3 $Lab::Bus::DEBUG::VERSION = '3.899';
4 use v5.20;
5  
6 use warnings;
7 use strict;
8  
9 use Scalar::Util qw(weaken);
10 use Time::HiRes qw (usleep sleep);
11 use Lab::Bus;
12 use Data::Dumper;
13 use Carp;
14  
15 use Lab::Exception;
16  
17 use parent 'Lab::Bus';
18  
19 our %fields = (
20 brutal => 0, # brutal as default?
21 type => 'DEBUG',
22 wait_status => 10e-6, # sec;
23 wait_query => 10e-6, # sec;
24 query_length => 300, # bytes
25 query_long_length => 10240, #bytes
26 read_length => 1000, # bytesx
27 instrument_index => 0,
28 );
29  
30 sub new {
31 my $proto = shift;
32 my $class = ref($proto) || $proto;
33 my $twin = undef;
34 my $self = $class->SUPER::new(@_)
35 ; # getting fields and _permitted from parent class
36 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
37  
38 # no twin search - just register
39 if ( $class eq __PACKAGE__ )
40 { # careful - do only if this is not a parent class constructor
41 my $i = 0;
42 while ( defined $Lab::Bus::BusList{ $self->type() }->{$i} ) { $i++; }
43 $Lab::Bus::BusList{ $self->type() }->{$i} = $self;
44 weaken( $Lab::Bus::BusList{ $self->type() }->{$i} );
45 }
46  
47 return $self;
48 }
49  
50 sub connection_new { # @_ = ({ resource_name => $resource_name })
51 my $self = shift;
52 my $args = undef;
53 my $status = undef;
54 my $connection_handle = undef;
55 if ( ref $_[0] eq 'HASH' ) {
56 $args = shift;
57 } # try to be flexible about options as hash/hashref
58 else { $args = {@_} }
59  
60 $connection_handle = { debug_instr_index => $self->instrument_index() };
61  
62 $self->instrument_index( $self->instrument_index() + 1 );
63  
64 return $connection_handle;
65 }
66  
67 sub connection_read
68 { # @_ = ( $connection_handle, $args = { read_length, brutal }
69 my $self = shift;
70 my $connection_handle = shift;
71 my $args = undef;
72 if ( ref $_[0] eq 'HASH' ) {
73 $args = shift;
74 } # try to be flexible about options as hash/hashref
75 else { $args = {@_} }
76  
77 my $command = $args->{'command'} || undef;
78 my $brutal = $args->{'brutal'} || $self->brutal();
79 my $read_length = $args->{'read_length'} || $self->read_length();
80  
81 my $result = undef;
82 my $user_status = undef;
83 my $message = "";
84  
85 my $brutal_txt = 'false';
86 $brutal_txt = 'true' if $brutal;
87  
88 ( $message = <<ENDMSG ) =~ s/^\t+//gm;
89  
90  
91 DEBUG bus
92 connection_read called on Instrument No. $connection_handle->{'debug_instr_index'}
93 Brutal: $brutal_txt
94 Read length: $read_length
95  
96 Enter device response (one line). Timeout prefix: 'T!', Error: 'E!'
97 ENDMSG
98  
99 print $message;
100  
101 $result = <STDIN>;
102 chomp($result);
103  
104 if ( $result =~ /^(T!).*/ ) {
105 $result = substr( $result, 2 );
106 Lab::Exception::Timeout->throw(
107 error => "Timeout in " . __PACKAGE__ . "::connection_read().\n",
108 data => $result,
109 );
110 }
111 elsif ( $result =~ /^(E!).*/ ) {
112 $result = substr( $result, 2 );
113 Lab::Exception::Error->throw(
114 error => "Error in " . __PACKAGE__ . "::connection_read().\n", );
115 }
116  
117 print "\n";
118 return $result;
119 }
120  
121 sub connection_write
122 { # @_ = ( $connection_handle, $args = { command, wait_status }
123 my $self = shift;
124 my $connection_handle = shift;
125 my $args = undef;
126 if ( ref $_[0] eq 'HASH' ) {
127 $args = shift;
128 } # try to be flexible about options as hash/hashref
129 else { $args = {@_} }
130  
131 my $command = $args->{'command'} || undef;
132 if ( !defined $command ) {
133 Lab::Exception::CorruptParameter->throw(
134 error => "No command given to "
135 . __PACKAGE__
136 . "::connection_write\n" );
137 }
138 my $brutal = $args->{'brutal'} || $self->brutal();
139 my $read_length = $args->{'read_length'} || $self->read_length();
140 my $wait_status = $args->{'wait_status'} || $self->wait_status();
141  
142 my $message = "";
143 my $user_return = "";
144  
145 my $brutal_txt = 'false';
146 $brutal_txt = 'true' if $brutal;
147  
148 ( $message = <<ENDMSG ) =~ s/^\t+//gm;
149  
150  
151 DEBUG bus
152 connection_write called on Instrument No. $connection_handle->{'debug_instr_index'}
153 Command: $command
154 Brutal: $brutal_txt
155 Read length: $read_length
156 Wait status: $wait_status
157  
158 Enter return state: (E)rror, just Return for success
159 ENDMSG
160 print $message;
161  
162 $user_return = <STDIN>;
163 chomp($user_return);
164  
165 if ( !defined $command ) {
166 Lab::Exception::CorruptParameter->throw(
167 error => "No command given to "
168 . __PACKAGE__
169 . "::connection_write().\n", );
170 }
171 else {
172  
173 if ( $user_return eq 'E' ) {
174 Lab::Exception::Error->throw( error => "Error in "
175 . __PACKAGE__
176 . "::connection_write() while executing $command.", );
177 }
178  
179 print "\n";
180 return 1;
181 }
182 }
183  
184 sub timeout {
185 my $self = shift;
186 my $connection_handle = shift;
187 my $timo = shift;
188  
189 say "DEBUG Bus: setting timeout to '$timo'";
190 }
191  
192 sub connection_query
193 { # @_ = ( $connection_handle, $args = { command, read_length, wait_status, wait_query, brutal }
194 my $self = shift;
195 my $connection_handle = shift;
196 my $args = undef;
197 if ( ref $_[0] eq 'HASH' ) {
198 $args = shift;
199 } # try to be flexible about options as hash/hashref
200 else { $args = {@_} }
201  
202 my $command = $args->{'command'} || undef;
203 my $brutal = $args->{'brutal'} || $self->brutal();
204 my $read_length = $args->{'read_length'} || $self->read_length();
205 my $wait_status = $args->{'wait_status'} || $self->wait_status();
206 my $wait_query = $args->{'wait_query'} || $self->wait_query();
207  
208 my $result = undef;
209 my $status = undef;
210 my $write_cnt = 0;
211 my $read_cnt = undef;
212  
213 $write_cnt = $self->connection_write($args);
214  
215 print "\nwait_query: $wait_query usec\n";
216  
217 $result = $self->connection_read($args);
218 return $result;
219 }
220  
221 sub _search_twin {
222 my $self = shift;
223  
224 return undef;
225 }
226  
227 1;
228  
229 __END__