Moose/Connection/VXI11.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 150 SCAR 45.0
CC
9
Cov
0%
Write
12.0
Read
12.0
DEMOLISH
12.0
CRAP 90.0
1 package Lab::Moose::Connection::VXI11;
2 $Lab::Moose::Connection::VXI11::VERSION = '3.881';
3 #ABSTRACT: Connection backend to VXI-11 (Lan/TCP)
4  
5 use v5.20;
6  
7  
8 use Moose;
9 use MooseX::Params::Validate;
10 use Moose::Util::TypeConstraints 'enum';
11 use Carp;
12  
13 use Lab::Moose::Instrument qw/timeout_param read_length_param/;
14 use Lab::VXI11;
15 use namespace::autoclean;
16  
17 use constant {
18 WAITLOCK_OPERATION_FLAG => 1 << 0,
19 END_OPERATION_FLAG => 1 << 3,
20 TERMCHRSET_OPERATION_FLAG => 1 << 7,
21  
22 END_REASON => 1 << 2,
23 CHR_REASON => 1 << 1,
24 REQCNT_REASON => 1 << 0,
25 };
26  
27 has client => (
28 is => 'ro',
29 isa => 'Lab::VXI11',
30 writer => '_client',
31 init_arg => undef,
32 );
33  
34 has lid => (
35 is => 'ro',
36 isa => 'Int',
37 writer => '_lid',
38 init_arg => undef
39 );
40  
41 has host => (
42 is => 'ro',
43 isa => 'Str',
44 required => 1,
45 );
46  
47 has proto => (
48 is => 'ro',
49 isa => enum( [qw/tcp udp/] ),
50 default => 'tcp'
51 );
52  
53 has device => (
54 is => 'ro',
55 isa => 'Str',
56 default => "inst0",
57 );
58  
59 sub _timeout_arg {
60 my $self = shift;
61 my %arg = @_;
62 my $timeout = $arg{timeout} // $self->timeout();
63 return sprintf( "%.0f", $timeout * 1000 );
64 }
65  
66 sub BUILD {
67 my $self = shift;
68 my $host = $self->host();
69 my $proto = $self->proto();
70 my $device = $self->device();
71 my $client
72 = Lab::VXI11->new( $host, DEVICE_CORE, DEVICE_CORE_VERSION, $proto )
73 or croak "cannot open VXI-11 connection with $host: $!";
74 $self->_client($client);
75  
76 my $clientId = 0;
77 my $lockDevice = 0;
78 my $lock_timeout = 0;
79 my ( $error, $lid, $abortPort, $maxRecvSize ) = $client->create_link(
80 $client, $lockDevice, $lock_timeout,
81 $device
82 );
83 if ($error) {
84 croak "Cannot create VXI-11 link. error: $error";
85 }
86 $self->_lid($lid);
87 }
88  
89 sub Write {
90 my ( $self, %args ) = validated_hash(
91 \@_,
92 timeout_param,
93 command => { isa => 'Str' },
94 );
95  
96 my $command = $args{command};
97 my $timeout = $self->_timeout_arg(%args);
98  
99 my $client = $self->client();
100 my $lid = $self->lid();
101  
102 my $lock_timeout = 0;
103 my $flags = END_OPERATION_FLAG;
104  
105 my ( $error, $size ) = $client->device_write(
106 $lid, $timeout, $lock_timeout, $flags,
107 $command
108 );
109 if ($error) {
110 croak "VXI-11 device_write failed with error $error.";
111 }
112 my $command_length = length($command);
113 if ( $size != $command_length ) {
114 croak
115 "VXI-11 device_write incomplete: write size: $size, expected: $command_length.";
116 }
117 }
118  
119 sub Read {
120 my ( $self, %args ) = validated_hash(
121 \@_,
122 timeout_param(),
123 read_length_param(),
124 );
125 my $timeout = $self->_timeout_arg(%args);
126 my $read_length = $self->_read_length_arg(%args);
127  
128 my $client = $self->client();
129 my $lid = $self->lid();
130  
131 my $lock_timeout = 0;
132 my $flags = 0;
133 my $termChar = 0; # not used
134  
135 my $result = '';
136 while ($read_length) {
137 my ( $error, $reason, $data ) = $client->device_read(
138 $lid, $read_length, $timeout,
139 $lock_timeout, $flags, $termChar
140 );
141 if ($error) {
142 croak "VXI-11 device_read failed with error $error.";
143 }
144 $result .= $data;
145 $read_length -= length($data);
146  
147 if ( $reason & END_REASON or $reason & REQCNT_REASON ) {
148 last;
149 }
150 }
151 return $result;
152 }
153  
154 sub Clear {
155 my ( $self, %args ) = validated_hash(
156 \@_,
157 timeout_param(),
158 );
159 my $timeout = $self->_timeout_arg(%args);
160  
161 my $client = $self->client();
162 my $lid = $self->lid();
163  
164 my $flags = 0;
165 my $lock_timeout = 0;
166  
167 my ($error)
168 = $client->device_clear( $lid, $flags, $lock_timeout, $timeout );
169 if ($error) {
170 croak "VXI-11 device_clear failed with error $error.";
171 }
172 }
173  
174 sub DEMOLISH {
175 my $self = shift;
176 my $client = $self->client();
177 my $lid = $self->lid();
178  
179 if ( $client && $lid ) {
180 my ($error) = $client->destroy_link($lid);
181  
182 if ($error) {
183 croak "VXI-11 destroy_link failed with error $error.";
184 }
185 }
186 }
187  
188 with qw/
189 Lab::Moose::Connection
190 /;
191  
192 __PACKAGE__->meta->make_immutable();
193  
194 1;
195  
196 __END__
197  
198 =pod
199  
200 =encoding UTF-8
201  
202 =head1 NAME
203  
204 Lab::Moose::Connection::VXI11 - Connection backend to VXI-11 (Lan/TCP)
205  
206 =head1 VERSION
207  
208 version 3.881
209  
210 =head1 SYNOPSIS
211  
212 use Lab::Moose;
213  
214 my $instrument = instrument(
215 type => 'random_instrument',
216 connection_type => 'VXI11',
217 connection_options => {host => '132.199.11.2'}
218 );
219  
220 =head1 DESCRIPTION
221  
222 Connection backend based on L<Lab::VXI11>.
223  
224 =head1 COPYRIGHT AND LICENSE
225  
226 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
227  
228 Copyright 2017 Simon Reinhardt
229 2020 Andreas K. Huettel
230  
231  
232 This is free software; you can redistribute it and/or modify it under
233 the same terms as the Perl 5 programming language system itself.
234  
235 =cut