blib/lib/Device/Chip/Adapter/UART.pm

filter: Statementstmt 55.8% 62 / 111 Branchbran 7.1% 1 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 58.3% 14 / 24 Podpod 0.0% 0 / 13 totaltotal 46.7% 78 / 167 CC 12 SCAR 19.2
CC
12
Cov
50%
configure
20.0
write_gpios
20.0
read_gpios
12.0
CRAP 6.8
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2018-2023 -- leonerd@leonerd.org.uk
5  
6 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Object::Pad 0.70;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Device::Chip::Adapter::UART 0.03;
11 class Device::Chip::Adapter::UART;
12  
13 # Can't isa Device::Chip::Adapter because it doesn't have a 'new'
14 3 ▶use Device::Chip::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 *make_protocol = \&Device::Chip::Adapter::make_protocol;
16  
17 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Future::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Future::IO 0.04; # ->syswrite_exactly
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use IO::Termios;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 NAME
25  
26 C<Device::Chip::Adapter::UART> - a C<Device::Chip::Adapter> implementation for
27 serial devices
28  
29 =head1 DESCRIPTION
30  
31 This class implements the L<Device::Chip::Adapter> interface around a regular
32 serial port, such as a USB UART adapter, allowing an instance of a
33 L<Device::Chip> driver to communicate with actual chip hardware using this
34 adapter.
35  
36 This adapter provides both the C<GPIO> and C<UART> protocols. The C<GPIO>
37 protocol wraps the modem control and handshaking lines. The C<UART> protocol
38 adds access to the transmit and receive lines by adding the L</write> and
39 L</read> methods.
40  
41 As the C<Device::Chip> interface is intended for hardware IO interfaces, it
42 does not support the concept that a serial stream might spontaneously become
43 disconnected. As such, an end-of-file condition on the stream filehandle will
44 be reported as a future failure.
45  
46 =cut
47  
48 =head1 CONSTRUCTOR
49  
50 =cut
51  
52 =head2 new
53  
54 $adapter = Device::Chip::Adapter::UART->new( %args )
55  
56 Returns a new instance of a C<Device::Chip::Adapter::UART>.
57  
58 Takes the following named arguments:
59  
60 =over 4
61  
62 =item dev => STRING
63  
64 Path to the device node representing the UART; usually something like
65 F</dev/ttyUSB0> or F</dev/ttyACM0>.
66  
67 =back
68  
69 =cut
70  
71 3 field $_fh;
72 field %_config = (
73 bits => 8,
74 parity => "n",
75 stop => 1,
76 );
77  
78 3 ADJUST :params (
79 :$fh = undef,
80 :$dev = undef,
81 ) {
82 if( defined $fh ) {
83 $_fh = $fh;
84 # OK
85 }
86 else {
87 $_fh = IO::Termios->open( $dev ) or
88 die "Cannot open $dev - $!";
89  
90 $_fh->blocking( 0 );
91  
92 for( $_fh->getattr ) {
93 $_->cfmakeraw;
94 $_->setflag_clocal( 1 );
95  
96 $_fh->setattr( $_ );
97 }
98 }
99 }
100  
101 3 sub new_from_description
102 0 ▶{
Subroutine new_from_description: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my $class = shift;
104 0 my %args = @_;
105 0 return $class->new( map { $_ => $args{$_} } qw( dev ) );
106 }
107  
108 =head1 PROTOCOLS
109  
110 The following C<Device::Chip::Adapter> protocol types are supported
111  
112 =over 2
113  
114 =item *
115  
116 GPIO
117  
118 =back
119  
120 =cut
121  
122 0 ▶sub make_protocol_GPIO { return Future->done( $_[0] ) }
Subroutine make_protocol_GPIO: not called CC=1 SCAR=6.9
Pod undocumented
123 0 ▶sub make_protocol_UART { return Future->done( $_[0] ) }
Subroutine make_protocol_UART: not called CC=1 SCAR=6.9
Pod undocumented
124  
125 # Protocol implementation
126  
127 3 my %GPIOS_READ = (
128 DSR => 1,
129 CTS => 1,
130 CD => 1,
131 RI => 1,
132 );
133  
134 0 ▶method configure ( %args )
Subroutine configure: not called CC=4 SCAR=30.0
Pod undocumented
135 {
136 exists $args{$_} and $_config{$_} = delete $args{$_}
137 0 ▶ for qw( baudrate bits parity stop );
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} and $_config{$_} = delete $args{$_} 0 0 0
138  
139 0 ▶ keys %args and
Branch: 1 branch
BranchTrueFalseTotal
keys %args and croak('Unrecognised configure options: ' . join(", ", keys %args)) 0 0 0
140 croak "Unrecognised configure options: " . join( ", ", keys %args );
141  
142 $_fh->set_mode( join ",",
143 0 @_config{qw( baudrate bits parity stop )}
144 );
145  
146 0 return Future->done;
147 3 }
148  
149 0 ▶method power ( $ ) { return Future->done } # ignore
Subroutine power: not called CC=1 SCAR=4.1
Pod undocumented
150  
151 0 ▶method list_gpios ()
Subroutine list_gpios: not called CC=1 SCAR=6.9
Pod undocumented
152 {
153 0 return qw( DTR DSR RTS CTS CD RI );
154 3 }
155  
156 0 ▶method meta_gpios ()
Subroutine meta_gpios: not called CC=2 SCAR=17.9
Pod undocumented
157 {
158 return map {
159 0 ▶ $GPIOS_READ{$_} ?
Branch: 1 branch
BranchTrueFalseTotal
$GPIOS_READ{$_} ? : 0 0 0
160 Device::Chip::Adapter::GPIODefinition( $_, "r", 1 ) :
161 Device::Chip::Adapter::GPIODefinition( $_, "w", 1 );
162 } shift->list_gpios;
163 3 }
164  
165 0 ▶method read_gpios ( $gpios )
Subroutine read_gpios: not called CC=3 SCAR=24.8
Pod undocumented
166 {
167 0 my $values = $_fh->get_modem();
168  
169 0 my %ret;
170  
171 0 foreach my $gpio ( @$gpios ) {
172 0 ▶ $ret{$gpio} = $values->{lc $gpio} if $GPIOS_READ{$gpio};
Branch: 1 branch
BranchTrueFalseTotal
if $GPIOS_READ{$gpio} 0 0 0
173 }
174  
175 0 return Future->done( \%ret );
176 3 }
177  
178 0 ▶method write_gpios ( $gpios )
Subroutine write_gpios: not called CC=4 SCAR=30.0
Pod undocumented
179 {
180 0 my %set;
181 defined $gpios->{$_} and $set{lc $_} = $gpios->{$_}
182 0 ▶ for qw( DTR RTS );
Branch: 1 branch
BranchTrueFalseTotal
defined $gpios->{$_} and $set{lc $_} = $gpios->{$_} 0 0 0
183  
184 0 ▶ if( %set ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%set) 0 0 0
185 0 $_fh->set_modem( \%set );
186 }
187  
188 0 return Future->done;
189 3 }
190  
191 0 ▶method tris_gpios ( $ )
Subroutine tris_gpios: not called CC=1 SCAR=6.9
Pod undocumented
192 {
193 # ignore
194 0 Future->done;
195 3 }
196  
197 1 ▶method write ( $bytes )
Subroutine write: called CC=1 SCAR=0.0
Pod undocumented
198 {
199 1 return Future::IO->syswrite_exactly( $_fh, $bytes );
200 3 }
201  
202 3 field $_readbuf;
203 1 ▶method readbuffer ()
Subroutine readbuffer: called CC=2 SCAR=7.1
Pod undocumented
204 {
205 1 ▶ return $_readbuf //= do {
Condition33%1/3
$_readbuf //= do { my $fh = $_fh; "Future::Buffer"->new("fill", sub { "Future::IO"->sysread($fh, 256); } ) }
l!l&&r!l&&!r
010
MC/DC0%0/2
$_readbuf //= do { my $fh = $_fh; "Future::Buffer"->new("fill", sub { "Future::IO"->sysread($fh, 256); } ) }
$_readbuf do { my $fh = $_fh; "Future::Buffer"->new("fill", sub { "Future::IO"->sysread($fh, 256); } ) }
Truth table33%1/3
A //= B
A= $_readbufB= do { my $fh = $_fh; "Future::Buffer"->new("fill", sub { "Future::IO"->sysread($fh, 256); } ) }
ABresult
1X1
011
000
206 1 my $fh = $_fh;
207  
208 Future::Buffer->new(
209 1 ▶ fill => sub { Future::IO->sysread( $fh, 256 ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
210 1 );
211 };
212 3 }
213  
214 1 ▶method read ( $len )
Subroutine read: called CC=1 SCAR=0.0
Pod undocumented
215 {
216 # This is a 'read_exactly'
217 return $self->readbuffer->read_exactly( $len )
218 1 ▶ ->then( sub { return @_ ? Future->done( @_ ) : Future->fail( "EOF" ) } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
219 3 }
220  
221 =head1 AUTHOR
222  
223 Paul Evans <leonerd@leonerd.org.uk>
224  
225 =cut
226  
227 3 0x55AA;
↑ Summary