SLuRM.pm

filter: Statementstmt 92.5% 273 / 295 Branchbran 71.4% 50 / 70 Conditioncond 61.7% 29 / 47 MC/DCmcdc 16.6% 5 / 30 Subroutinesub 88.1% 37 / 42 Podpod 100% 7 / 7 totaltotal 81.6% 401 / 491 CC 53 SCAR 27.6
CC
53
Cov
85%
_run
41.1
__ANON__
4.1
_request
4.0
CRAP 15.8
↑ Summary SLuRM/Protocol.pm »
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, 2022-2026 -- leonerd@leonerd.org.uk
5  
6 8 ▶use v5.28; # delete %hash{@slice}
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Object::Pad 0.807 ':experimental(adjust_params inherit_field)';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Device::Serial::SLuRM 0.11;
11 class Device::Serial::SLuRM;
12  
13 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Future::Selector 0.02; # ->run_until_ready
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 8 ▶use constant DEBUG => $ENV{SLURM_DEBUG} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"SLURM_DEBUG"} // 0
l!l
08
MC/DC0%0/1
$ENV{"SLURM_DEBUG"} // 0
$ENV{"SLURM_DEBUG"}
Truth table50%1/2
A
A= $ENV{"SLURM_DEBUG"}
Aresult
11
00
24  
25 8 require Device::Serial::SLuRM::Protocol;
26 8 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 our $METRICS = $Device::Serial::SLuRM::Protocol::METRICS;
28  
29 # builtin:: only turned up at 5.36, grrrr
30 8 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 true => !!1,
32 false => !!0,
33 8 };
34  
35 8 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 SLURM_PKTCTRL_META => 0x00,
37 SLURM_PKTCTRL_META_RESET => 0x01,
38 SLURM_PKTCTRL_META_RESETACK => 0x02,
39  
40 SLURM_PKTCTRL_NOTIFY => 0x10,
41  
42 SLURM_PKTCTRL_REQUEST => 0x30,
43  
44 SLURM_PKTCTRL_RESPONSE => 0xB0,
45 SLURM_PKTCTRL_ACK => 0xC0,
46 SLURM_PKTCTRL_ERR => 0xE0,
47 8 };
48  
49 =encoding UTF-8
50  
51 =head1 NAME
52  
53 C<Device::Serial::SLuRM> - communicate the SLµRM protocol over a serial port
54  
55 =head1 SYNOPSIS
56  
57 =for highlighter language=perl
58  
59 use v5.36;
60 use Device::Serial::SLuRM;
61  
62 my $slurm = Device::Serial::SLuRM->new(
63 dev => "/dev/ttyUSB0",
64 baud => 19200,
65 );
66  
67 $slurm->run(
68 on_notify => sub ($payload) {
69 printf "NOTIFY: %v02X\n", $payload;
70 }
71 )->await;
72  
73 =head1 DESCRIPTION
74  
75 This module provides a L<Future::IO>-based interface for communicating with
76 a peer device on a serial port (or similar device handle) which talks the
77 SLµRM messaging protocol. It supports sending and receiving of NOTIFY
78 packets, and sending of REQUEST packets that receive a RESPONSE.
79  
80 It currently does not support receiving REQUESTs, though this could be added
81 relatively easily.
82  
83 Optionally, this module supports being the controller for a multi-drop
84 ("MSLµRM") bus. See the L<Device::Serial::MSLuRM> subclass.
85  
86 =head2 SLµRM
87  
88 SLµRM ("Serial Link Microcontroller Reliable Messaging") is a simple
89 bidirectional communication protocol for adding reliable message framing and
90 request/response semantics to byte-based data links (such as asynchronous
91 serial ports), which may themselves be somewhat unreliable. SLµRM can tolerate
92 bytes arriving corrupted or going missing altogether, or additional noise
93 bytes being received, while still maintaining a reliable bidirectional flow of
94 messages. There are two main kinds of message flows - NOTIFYs and REQUESTs. In
95 all cases, packet payloads can be of a variable length (including zero bytes),
96 and the protocol itself does not put semantic meaning on those bytes - they
97 are free for the application to use as required.
98  
99 A NOTIFY message is a simple notification from one peer to the other, that
100 does not yield a response.
101  
102 A REQUEST message carries typically some sort of command instruction, to which
103 the peer should respond with a RESPONSE or ERR packet. Replies to a REQUEST
104 message do not have to be sent sequentially.
105  
106 The F<doc/> directory of this distribution contains more detailed protocol
107 documentation which may be useful for writing other implementations.
108  
109 The F<contrib/> directory of this distribution contains a reference
110 implementation in C for 8-bit microcontrollers, such as AVR ATtiny and ATmega
111 chips.
112  
113 =cut
114  
115 =head2 Metrics
116  
117 If L<Metrics::Any> is available, this module additionally provides metrics
118 under the namespace prefix of C<slurm>. The following metrics are provided:
119  
120 =over 4
121  
122 =item discards
123  
124 An unlabelled counter tracking the number of times a received packet is
125 discarded due to failing CRC check.
126  
127 =item packets
128  
129 A counter, labelled by direction and packet type, tracking the number of
130 packets sent and received of each type.
131  
132 =item request_success_attempts
133  
134 A distribution that tracks how many attempts it took to get a response to each
135 request.
136  
137 =item request_duration
138  
139 A timer that tracks how long it took to get a response to each request.
140  
141 =item retransmits
142  
143 An unlabelled counter tracking the number of times a (REQUEST) packet had to
144 be retransmitted after the initial one timed out.
145  
146 =item serial_bytes
147  
148 A counter, labelled by direction, tracking the number of bytes sent and
149 received directly over the serial port. The rate of this can be used to
150 calculate overall serial link utilisation.
151  
152 =item timeouts
153  
154 An unlabelled counter tracking the number of times a request transaction was
155 abandoned entirely due to a timeout. This does I<not> count transactions that
156 eventually succeeded after intermediate timeouts and retransmissions.
157  
158 =back
159  
160 =cut
161  
162 =head1 PARAMETERS
163  
164 =head2 dev
165  
166 dev => PATH
167  
168 Path to the F</dev/...> device node representing the serial port used for this
169 communication. This will be opened via L<IO::Termios> and configured into the
170 appropriate mode and baud rate.
171  
172 =head2 baud
173  
174 baud => NUM
175  
176 Optional baud rate to set for communication when opening a device node.
177  
178 SLµRM does not specify a particular rate, but a default value of 115.2k will
179 apply if left unspecified.
180  
181 =head2 host
182  
183 =head2 port
184  
185 host => STR,
186 port => NUM or STR
187  
188 I<Since version 0.11.>
189  
190 An alternative to the C<dev> argument, giving instead a destination IP address
191 to send UDP packets in UDP-wrapped MSLµRM format.
192  
193 =head2 fh
194  
195 fh => IO
196  
197 An IO handle directly to the the serial port device to be used for reading and
198 writing. It will be assumed to be set up correctly; no further setup will be
199 performed.
200  
201 Either C<dev>, C<host> or C<fh> are required.
202  
203 =head2 retransmit_delay
204  
205 retransmit_delay => NUM
206  
207 Optional delay in seconds to wait after a non-response of a REQUEST packet
208 before sending it again.
209  
210 A default value will be calculated if not specified. This is based on the
211 serial link baud rate. At the default 115.2k baud it will be 50msec (0.05);
212 the delay will be scaled appropriately for other baud rates, to maintain a
213 timeout of the time it would take to send 576 bytes.
214  
215 Applications that transfer large amounts of data over slow links, or for which
216 responding to a command may take a long time, should increase this value.
217  
218 =head2 retransmit_count
219  
220 retransmit_count => NUM
221  
222 Optional number of additional attempts to try sending REQUEST packets before
223 giving up entirely. A default of 2 will apply if not specified (thus each
224 C<request> method will make up to 3 attempts).
225  
226 =cut
227  
228 8 ▶use constant is_multidrop => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 8 field $_protocol :inheritable; # TODO
231 ADJUST :params ( %params )
232 {
233 $_protocol = Device::Serial::SLuRM::Protocol->new(
234 multidrop => __CLASS__->is_multidrop,
235 delete %params{qw( fh dev baud host port )},
236 );
237 }
238  
239 8 field $_retransmit_delay :param = undef;
240 8 field $_retransmit_count :param //= 2;
241  
242 8 ADJUST
243 {
244 if( !defined $_retransmit_delay ) {
245 # At 115200baud (being 11520 bytes/sec presuming 1 start, no parity,
246 # 1 stop) we should get 0.05 sec delay; this is the time taken to
247 # transmit 576 bytes.
248 $_retransmit_delay = 576 / $_protocol->bps;
249 }
250 }
251  
252 8 field $_on_notify;
253 8 field $_on_request;
254  
255 class Device::Serial::SLuRM::_NodeState {
256 32 ▶ field $did_reset :mutator;
Subroutine did_reset: called CC=1 SCAR=0.0
257  
258 46 field $seqno_tx :mutator = 0;
259 46 ▶ field $seqno_rx :mutator = 0;
Subroutine seqno_tx: called CC=1 SCAR=0.0
260  
261 52 ▶ field @_pending_slots; # [$seqno] = { payload, response_f }
Subroutine seqno_rx: called CC=1 SCAR=0.0
262  
263 44 ▶ method pending_slot ( $seqno ) { return $_pending_slots[ $seqno ] }
Subroutine pending_slot: called CC=1 SCAR=0.0
264 13 ▶ method set_pending_slot ( $seqno, $data ) { $_pending_slots[ $seqno ] = $data; }
Subroutine set_pending_slot: called CC=1 SCAR=0.0
265 12 ▶ method clear_pending_slot ( $seqno ) { undef $_pending_slots[ $seqno ]; }
Subroutine clear_pending_slot: called CC=1 SCAR=0.0
266 }
267  
268 8 field @_nodestate; # keyed per peer node ID
269  
270 8 field $_rx_nodestate; # a second set of pending slots for received REQUESTs
271  
272 =head1 METHODS
273  
274 =cut
275  
276 =head2 recv_packet
277  
278 ( $pktctrl, $payload ) = await $slurm->recv_packet;
279  
280 Waits for and returns the next packet to be received from the serial port.
281  
282 =cut
283  
284 8 field $_next_resetack_f;
285  
286 0 ▶async method recv_packet ()
Subroutine recv_packet: not called CC=1 SCAR=6.9
Pod documented
287 0 {
288 0 my ( $pktctrl, undef, $payload ) = await $_protocol->recv;
289 0 return ( $pktctrl, $payload );
290 8 }
291  
292 8 field $_run_f;
293  
294 async method _run
295 23 ▶{
Subroutine _run: called CC=33 SCAR=37.2
296 23 while(1) {
297 59 ▶ my ( $pktctrl, $addr, $payload ) = await $_protocol->recv
Branch: 1 branch
BranchTrueFalseTotal
() = <custom op await> or return 36 0 36
298 or return; # EOF
299  
300 36 ▶ redo if $addr & 0x80; # controller reflection
Branch: 1 branch
BranchTrueFalseTotal
if $addr & 128 0 36 36
301  
302 36 my $node_id = $addr;
303  
304 36 my $seqno = $pktctrl & 0x0F;
305 36 $pktctrl &= 0xF0;
306  
307 36 ▶ my $nodestate = $_nodestate[ $node_id ] //= Device::Serial::SLuRM::_NodeState->new;
Condition66%2/3
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
l!l&&r!l&&!r
3420
MC/DC0%0/2
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
$_nodestate[$node_id] "Device::Serial::SLuRM::_NodeState"->new
Truth table66%2/3
A //= B
A= $_nodestate[$node_id]B= "Device::Serial::SLuRM::_NodeState"->new
ABresult
1X1
011
000
308  
309 36 ▶ if( $pktctrl == SLURM_PKTCTRL_META ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pktctrl == 0) 9 27 36
310 match( $seqno : == ) {
311 case( SLURM_PKTCTRL_META_RESET ),
312 case( SLURM_PKTCTRL_META_RESETACK ) {
313 9 ( $nodestate->seqno_rx ) = unpack "C", $payload;
314  
315 9 ▶ if( $seqno == SLURM_PKTCTRL_META_RESET ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seqno == 1) { } 2 7 9
316 2 await $_protocol->send( SLURM_PKTCTRL_META_RESETACK, $node_id, pack "C", $nodestate->seqno_tx );
317 }
318 else {
319 7 ▶ $_next_resetack_f->done if $_next_resetack_f;
Branch: 1 branch
BranchTrueFalseTotal
if $_next_resetack_f 7 0 7
320 }
321 }
322 9 ▶ default {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 1 || $(Syntax::Keyword::Match/topic) == 2 ? : 9 0 9
Condition66%2/3
$(Syntax::Keyword::Match/topic) == 1 or $(Syntax::Keyword::Match/topic) == 2
l!l&&r!l&&!r
270
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) == 1 or $(Syntax::Keyword::Match/topic) == 2
$(Syntax::Keyword::Match/topic) == 1 $(Syntax::Keyword::Match/topic) == 2
Truth table66%2/3
A or B
A= $(Syntax::Keyword::Match/topic) == 1B= $(Syntax::Keyword::Match/topic) == 2
ABresult
1X1
011
000
323 0 warn sprintf "No idea what to do with pktctrl(meta) = %02X\n", $seqno;
324 }
325 }
326  
327 9 next;
328 }
329  
330 27 my $is_dup;
331 27 ▶ if( !( $pktctrl & 0x80 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pktctrl & 128) 14 13 27
332 14 ▶ if( defined $nodestate->seqno_rx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodestate->seqno_rx) 14 0 14
333 14 my $seqdiff = $seqno - $nodestate->seqno_rx;
334 14 ▶ $seqdiff += 16 if $seqdiff < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $seqdiff < 0 2 12 14
335 14 ▶ $is_dup = !$seqdiff || $seqdiff > 8; # suppress duplicates / backsteps
Condition100%3/3
!$seqdiff || $seqdiff > 8
l!l&&r!l&&!r
2111
MC/DC100%2/2
!$seqdiff || $seqdiff > 8
!$seqdiff $seqdiff > 8
Truth table100%3/3
not(A) || B
A= !$seqdiffB= $seqdiff > 8
ABresult
1X1
011
000
336 }
337  
338 14 $nodestate->seqno_rx = $seqno;
339 }
340  
341 match( $pktctrl : == ) {
342 case( SLURM_PKTCTRL_NOTIFY ) {
343 11 ▶ next if $is_dup;
Branch: 1 branch
BranchTrueFalseTotal
if $is_dup 2 9 11
344  
345 9 printf STDERR "SLuRM rx-NOTIFY(%d): %v02X\n", $seqno, $payload
346 if DEBUG;
347  
348 9 ▶ $_on_notify ? $_on_notify->( ( __CLASS__->is_multidrop ? ( $node_id ) : () ), $payload )
Branch: 3 branches
BranchTrueFalseTotal
$_on_notify ? : 9 0 9
<custom op>->is_multidrop ? : 1 8 9
$(Syntax::Keyword::Match/topic) == 48 ? : 3 13 16
349 : warn "Received NOTIFY packet with no handler\n";
350 }
351  
352 case( SLURM_PKTCTRL_REQUEST ) {
353 3 printf STDERR "SLuRM rx-REQUEST(%d): %v02X\n", $seqno, $payload
354 if DEBUG;
355  
356 3 ▶ $_rx_nodestate //= Device::Serial::SLuRM::_NodeState->new;
Condition66%2/3
$_rx_nodestate //= "Device::Serial::SLuRM::_NodeState"->new
l!l&&r!l&&!r
210
MC/DC0%0/2
$_rx_nodestate //= "Device::Serial::SLuRM::_NodeState"->new
$_rx_nodestate "Device::Serial::SLuRM::_NodeState"->new
Truth table66%2/3
A //= B
A= $_rx_nodestateB= "Device::Serial::SLuRM::_NodeState"->new
ABresult
1X1
011
000
357 3 ▶ if( my $slot = $_rx_nodestate->pending_slot( $seqno ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $slot = $_rx_nodestate->pending_slot($seqno)) { } 1 2 3
358 await $self->_send_response( $node_id, $seqno, $slot->{payload} )
359 1 ▶ if defined $slot->{payload};
Branch: 1 branch
BranchTrueFalseTotal
if defined $slot->{"payload"} 1 0 1
360 }
361 else {
362 2 ▶ next if $is_dup;
Branch: 1 branch
BranchTrueFalseTotal
if $is_dup 0 2 2
363  
364 # TODO: If multidrop we need to ignore requests except for us
365  
366 2 $_rx_nodestate->set_pending_slot( $seqno,
367 {
368 # no payload yet
369 start_time => [ gettimeofday ],
370 }
371 );
372  
373 2 ▶ if( $_on_request ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_on_request) { } 2 0 2
374 2 $_on_request->( $seqno, $payload );
375 }
376 else {
377 0 ▶ warn "Received REQUEST packet with no handler\n";
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 176 || $(Syntax::Keyword::Match/topic) == 224 ? : 9 4 13
Condition100%3/3
$(Syntax::Keyword::Match/topic) == 176 or $(Syntax::Keyword::Match/topic) == 224
l!l&&r!l&&!r
814
MC/DC100%2/2
$(Syntax::Keyword::Match/topic) == 176 or $(Syntax::Keyword::Match/topic) == 224
$(Syntax::Keyword::Match/topic) == 176 $(Syntax::Keyword::Match/topic) == 224
Truth table100%3/3
A or B
A= $(Syntax::Keyword::Match/topic) == 176B= $(Syntax::Keyword::Match/topic) == 224
ABresult
1X1
011
000
378 }
379 }
380 }
381  
382 case( SLURM_PKTCTRL_RESPONSE ),
383 case( SLURM_PKTCTRL_ERR ) {
384 9 my $slot = $nodestate->pending_slot( $seqno );
385 9 ▶ unless( $slot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($slot) 0 9 9
386 0 warn "Received reply to unsent request seqno=$seqno\n";
387 0 next;
388 }
389  
390 $METRICS and
391 9 ▶ $METRICS->report_timer( request_duration => tv_interval $slot->{start_time} );
Condition33%1/3
$Device::Serial::SLuRM::METRICS and $Device::Serial::SLuRM::METRICS->report_timer("request_duration", tv_interval($slot->{"start_time"}))
!ll&&!rl&&r
900
MC/DC0%0/2
$Device::Serial::SLuRM::METRICS and $Device::Serial::SLuRM::METRICS->report_timer("request_duration", tv_interval($slot->{"start_time"}))
$Device::Serial::SLuRM::METRICS $Device::Serial::SLuRM::METRICS->report_timer("request_duration", tv_interval($slot->{"start_time"}))
Truth table33%1/3
A and B
A= $Device::Serial::SLuRM::METRICSB= $Device::Serial::SLuRM::METRICS->report_timer("request_duration", tv_interval($slot->{"start_time"}))
ABresult
0X0
100
111
392  
393 # Send the first ACK before completing the future
394 9 printf STDERR "SLuRM tx-ACK(%d)\n", $seqno
395 if DEBUG;
396  
397 9 await $_protocol->send( SLURM_PKTCTRL_ACK | $seqno, $node_id | 0x80, "" );
398  
399 9 ▶ if( $pktctrl == SLURM_PKTCTRL_RESPONSE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pktctrl == 176) { } 8 1 9
400 8 printf STDERR "SLuRM rx-RESPONSE(%d): %v02X\n", $seqno, $payload
401 if DEBUG;
402  
403 8 $slot->{response_f}->done( $payload );
404 }
405 else {
406 1 printf STDERR "SLuRM rx-ERR(%d): %v02X\n", $seqno, $payload
407 if DEBUG;
408  
409 1 ▶ my $message = sprintf "Received ERR packet <%v02X%s>",
Branch: 1 branch
BranchTrueFalseTotal
length $payload > 3 ? : 0 1 1
410 substr( $payload, 0, 3 ),
411 length $payload > 3 ? "..." : "";
412 1 $slot->{response_f}->fail( $message, slurm => $payload );
413 }
414 9 $slot->{retransmit_f}->cancel;
415  
416 $METRICS and
417 9 ▶ $METRICS->report_distribution( request_success_attempts => 1 + $_retransmit_count - $slot->{retransmit_count} );
Condition33%1/3
$Device::Serial::SLuRM::METRICS and $Device::Serial::SLuRM::METRICS->report_distribution("request_success_attempts", 1 + $_retransmit_count - $slot->{"retransmit_count"})
!ll&&!rl&&r
900
MC/DC0%0/2
$Device::Serial::SLuRM::METRICS and $Device::Serial::SLuRM::METRICS->report_distribution("request_success_attempts", 1 + $_retransmit_count - $slot->{"retransmit_count"})
$Device::Serial::SLuRM::METRICS $Device::Serial::SLuRM::METRICS->report_distribution("request_success_attempts", 1 + $_retransmit_count - $slot->{"retransmit_count"})
Truth table33%1/3
A and B
A= $Device::Serial::SLuRM::METRICSB= $Device::Serial::SLuRM::METRICS->report_distribution("request_success_attempts", 1 + $_retransmit_count - $slot->{"retransmit_count"})
ABresult
0X0
100
111
418  
419 # Second ACK
420 9 await $_protocol->send( SLURM_PKTCTRL_ACK | $seqno, $node_id | 0x80, "",
421 linestuff => true,
422 );
423  
424 9 ▶ $nodestate->clear_pending_slot( $seqno );
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 192 ? : 4 0 4
425 }
426  
427 case( SLURM_PKTCTRL_ACK ) {
428 4 ▶ $_rx_nodestate or next;
Condition50%1/2
$_rx_nodestate or next
l!l
40
MC/DC0%0/1
$_rx_nodestate or next
$_rx_nodestate
Truth table50%1/2
A
A= $_rx_nodestate
Aresult
11
00
429 4 ▶ my $slot = $_rx_nodestate->pending_slot( $seqno ) or next;
Condition100%2/2
my $slot = $_rx_nodestate->pending_slot($seqno) or next
l!l
22
MC/DC100%1/1
my $slot = $_rx_nodestate->pending_slot($seqno) or next
my $slot = $_rx_nodestate->pending_slot($seqno)
Truth table100%2/2
A
A= my $slot = $_rx_nodestate->pending_slot($seqno)
Aresult
11
00
430  
431 2 $_rx_nodestate->clear_pending_slot( $seqno );
432 }
433  
434 default {
435 0 warn sprintf "Received unrecognised packet type=%02X\n", $pktctrl;
436 }
437 }
438 27 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) == 16 ? : 11 16 27
439 8 }
440  
441 8 field $_selector;
442 method _selector
443 30 ▶{
Subroutine _selector: called CC=2 SCAR=7.0
444 30 ▶ return $_selector if $_selector;
Branch: 1 branch
BranchTrueFalseTotal
if $_selector 7 23 30
445  
446 23 $_selector = Future::Selector->new;
447 $_selector->add(
448 data => "runloop",
449 f => $_run_f = $self->_run
450 ->set_label( "Device::Serial::SLuRM runloop" )
451 0 ▶ ->on_fail( sub { die "Device::Serial::SLuRM runloop failed: $_[0]" } ),
Subroutine __ANON__: not called CC=1 SCAR=1.2
452 );
453  
454 23 return $_selector;
455 8 }
456  
457 =head2 run
458  
459 $run_f = $slurm->run( %args );
460  
461 Starts the receiver run-loop, which can be used to wait for incoming NOTIFY
462 packets. This method returns a future, but the returned future will not
463 complete in normal circumstances. It will remain pending while the run-loop is
464 running. If an unrecoverable error happens (such as an IO error on the
465 underlying serial port device) then this future will fail.
466  
467 Takes the following named arguments:
468  
469 =over 4
470  
471 =item on_notify => CODE
472  
473 $on_notify->( $payload )
474  
475 Optional. Invoked on receipt of a NOTIFY packet.
476  
477 =back
478  
479 Will automatically L</reset> first if required.
480  
481 =cut
482  
483 async method _autoreset
484 10 ▶{
Subroutine _autoreset: called CC=3 SCAR=11.3
485 10 ▶ my $nodestate = $_nodestate[0] //= Device::Serial::SLuRM::_NodeState->new;
Condition66%2/3
$_nodestate[0] //= "Device::Serial::SLuRM::_NodeState"->new
l!l&&r!l&&!r
820
MC/DC0%0/2
$_nodestate[0] //= "Device::Serial::SLuRM::_NodeState"->new
$_nodestate[0] "Device::Serial::SLuRM::_NodeState"->new
Truth table66%2/3
A //= B
A= $_nodestate[0]B= "Device::Serial::SLuRM::_NodeState"->new
ABresult
1X1
011
000
486  
487 10 ▶ $nodestate->did_reset or
Condition66%2/3
$nodestate->did_reset || <custom op await>
l!l&&r!l&&!r
820
MC/DC0%0/2
$nodestate->did_reset || <custom op await>
$nodestate->did_reset <custom op await>
Truth table66%2/3
A || B
A= $nodestate->did_resetB= <custom op await>
ABresult
1X1
011
000
488 await $self->_reset( 0 );
489 8 }
490  
491 12 ▶async method run ( %args )
Subroutine run: called CC=2 SCAR=6.9
Pod documented
492 12 {
493 12 $_on_notify = $args{on_notify}; # TODO: save old, restore on exit?
494  
495 12 my $s = $self->_selector;
496 12 my $f = $_run_f;
497  
498 # Ugh this is a terrible way to do this
499 12 ▶ if( my $handle_request = $args{handle_request} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle_request = $args{"handle_request"}) 2 10 12
500 # Currently undocumented pending an idea of how to do a receiver subclass
501 2 ▶ $_on_request = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
502 2 my ( $seqno, $payload ) = @_;
503  
504 my $ret_f = $handle_request->( $payload )
505 ->then(
506 2 ▶ sub { # on_done
Subroutine __ANON__: called CC=1 SCAR=0.0
507 2 my ( $response ) = @_;
508 # TODO: insert my own node ID
509 # TODO: Consider reporting a metric?
510 2 return $self->_send_response( 0, $seqno, $response );
511 },
512 0 ▶ sub { # on_fail
Subroutine __ANON__: not called CC=1 SCAR=6.9
513 # TODO: Consider reporting a metric?
514 0 warn "TODO: handle_request failed, we should send ERR somehow";
515 },
516 2 );
517  
518 2 $s->add( f => $ret_f, data => undef );
519 2 };
520 }
521  
522 12 await $self->_autoreset;
523  
524 10 await $s->run_until_ready( $f );
525 8 }
526  
527 =head2 stop
528  
529 $slurm->stop;
530  
531 Stops the receiver run-loop, if running, causing its future to be cancelled.
532  
533 It is not an error to call this method if the run loop is not running.
534  
535 =cut
536  
537 method stop
538 21 ▶{
Subroutine stop: called CC=3 SCAR=11.3
Pod documented
539 21 ▶ return unless $_run_f;
Branch: 1 branch
BranchTrueFalseTotal
unless $_run_f 0 21 21
540  
541 21 ▶ eval { $_run_f->cancel } or warn "Failed to ->cancel the runloop future - $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_run_f->cancel } } or warn "Failed to ->cancel the runloop future - $@" 21 0 21
542 21 undef $_run_f;
543 21 undef $_selector;
544 8 }
545  
546 =head2 send_packet
547  
548 await $slurm->send_packet( $pktctrl, $payload );
549  
550 Sends a packet to the serial port.
551  
552 =cut
553  
554 0 ▶async method send_packet ( $pktctrl, $payload ) { await $_protocol->send( $pktctrl, undef, $payload ); }
Subroutine send_packet: not called CC=1 SCAR=5.1
Pod documented
555  
556 =head2 reset
557  
558 $slurm->reset;
559  
560 Resets the transmitter sequence number and sends a META-RESET packet.
561  
562 It is not normally required to explicitly call this, as the first call to
563 L</run>, L</send_notify> or L</request> will do it if required.
564  
565 =cut
566  
567 2 ▶method reset () { $self->_reset( 0 ); }
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
568  
569 7 ▶async method _reset ( $node_id )
Subroutine _reset: called CC=3 SCAR=11.0
570 7 {
571 7 my $s = $self->_selector;
572  
573 7 ▶ my $nodestate = $_nodestate[ $node_id ] //= Device::Serial::SLuRM::_NodeState->new;
Condition66%2/3
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
l!l&&r!l&&!r
430
MC/DC0%0/2
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
$_nodestate[$node_id] "Device::Serial::SLuRM::_NodeState"->new
Truth table66%2/3
A //= B
A= $_nodestate[$node_id]B= "Device::Serial::SLuRM::_NodeState"->new
ABresult
1X1
011
000
574  
575 7 $nodestate->seqno_tx = 0;
576  
577 # Need to create this before sending because of unit testing
578 7 $_next_resetack_f = $_run_f->new;
579  
580 7 await $_protocol->send_twice( SLURM_PKTCTRL_META_RESET, $node_id | 0x80, pack "C", $nodestate->seqno_tx );
581 7 $nodestate->did_reset = 1;
582  
583 # TODO: These might collide, do we need a Queue?
584 7 await $s->run_until_ready( Future->wait_any(
585 $_next_resetack_f,
586 Future::IO->sleep( $_retransmit_delay * 3 ),
587 ) );
588 5 ▶ ( undef $nodestate->did_reset ), die "Timed out waiting for reset\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $_next_resetack_f->is_done 0 5 5
589 unless $_next_resetack_f->is_done;
590  
591 5 undef $_next_resetack_f;
592 8 }
593  
594 =head2 send_notify
595  
596 await $slurm->send_notify( $payload );
597  
598 Sends a NOTIFY packet.
599  
600 Will automatically L</reset> first if required.
601  
602 =cut
603  
604 2 ▶method send_notify ( $payload ) { $self->_send_notify( 0, $payload ); }
Subroutine send_notify: called CC=1 SCAR=0.0
Pod documented
605  
606 4 ▶async method _send_notify ( $node_id, $payload )
Subroutine _send_notify: called CC=3 SCAR=11.1
607 4 {
608 4 ▶ my $nodestate = $_nodestate[ $node_id ] //= Device::Serial::SLuRM::_NodeState->new;
Condition33%1/3
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
l!l&&r!l&&!r
400
MC/DC0%0/2
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
$_nodestate[$node_id] "Device::Serial::SLuRM::_NodeState"->new
Truth table33%1/3
A //= B
A= $_nodestate[$node_id]B= "Device::Serial::SLuRM::_NodeState"->new
ABresult
1X1
011
000
609  
610 4 ▶ $nodestate->did_reset or
Branch: 1 branch
BranchTrueFalseTotal
$nodestate->did_reset or <custom op await> 4 0 4
611 await $self->_reset( $node_id );
612  
613 4 ( $nodestate->seqno_tx += 1 ) &= 0x0F;
614 4 my $seqno = $nodestate->seqno_tx;
615  
616 4 printf STDERR "SLuRM tx-NOTIFY(%d): %v02X\n", $seqno, $payload
617 if DEBUG;
618  
619 4 my $pktctrl = SLURM_PKTCTRL_NOTIFY | $seqno;
620  
621 4 await $_protocol->send_twice( $pktctrl, $node_id | 0x80, $payload );
622 8 }
623  
624 =head2 request
625  
626 $data_in = await $slurm->request( $data_out );
627  
628 Sends a REQUEST packet, and waits for a response to it.
629  
630 If the peer responds with an ERR packet, the returned future will fail with
631 an error message, the category of C<slurm>, and the payload body of the ERR
632 packet in the message details:
633  
634 $f->fail( $message, slurm => $payload );
635  
636 If the peer does not respond at all and all retransmit attempts end in a
637 timeout, the returned future will fail the same way but with C<undef> as the
638 message details:
639  
640 $f->fail( $message, slurm => undef );
641  
642 Will automatically L</reset> first if required.
643  
644 =cut
645  
646 8 ▶method request ( $payload ) { $self->_request( 0, $payload ); }
Subroutine request: called CC=1 SCAR=0.0
Pod documented
647  
648 11 ▶async method _request ( $node_id, $payload )
Subroutine _request: called CC=4 SCAR=13.9
649 11 {
650 11 my $s = $self->_selector;
651  
652 11 ▶ my $nodestate = $_nodestate[ $node_id ] //= Device::Serial::SLuRM::_NodeState->new;
Condition66%2/3
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
l!l&&r!l&&!r
920
MC/DC0%0/2
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
$_nodestate[$node_id] "Device::Serial::SLuRM::_NodeState"->new
Truth table66%2/3
A //= B
A= $_nodestate[$node_id]B= "Device::Serial::SLuRM::_NodeState"->new
ABresult
1X1
011
000
653  
654 11 ▶ $nodestate->did_reset or
Branch: 1 branch
BranchTrueFalseTotal
$nodestate->did_reset or <custom op await> 9 2 11
655 await $self->_reset( $node_id );
656  
657 11 ( $nodestate->seqno_tx += 1 ) &= 0x0F;
658 11 my $seqno = $nodestate->seqno_tx;
659  
660 11 printf STDERR "SLuRM tx-REQUEST(%d): %v02X\n", $seqno, $payload
661 if DEBUG;
662  
663 11 ▶ $nodestate->pending_slot( $seqno ) and croak "TODO: Request seqno collision - pick a new one?";
Branch: 1 branch
BranchTrueFalseTotal
$nodestate->pending_slot($seqno) and croak("TODO: Request seqno collision - pick a new one?") 0 11 11
664  
665 11 my $pktctrl = SLURM_PKTCTRL_REQUEST | $seqno;
666  
667 11 await $_protocol->send( $pktctrl, $node_id | 0x80, $payload );
668  
669 11 $nodestate->set_pending_slot( $seqno,
670 {
671 payload => $payload,
672 response_f => my $f = $_run_f->new,
673 retransmit_count => $_retransmit_count,
674 start_time => [ gettimeofday ],
675 }
676 );
677  
678 11 $self->_set_retransmit( $node_id, $seqno );
679  
680 11 return await $f;
681 8 }
682  
683 3 ▶async method _send_response ( $node_id, $seqno, $payload )
Subroutine _send_response: called CC=2 SCAR=6.9
684 3 {
685 3 printf STDERR "SLuRM tx-RESPONSE(%d): %v02X\n", $seqno, $payload
686 if DEBUG;
687  
688 3 my $pktctrl = SLURM_PKTCTRL_RESPONSE | $seqno;
689  
690 3 ▶ my $slot = ( $_rx_nodestate // die "ARGH cannot _send_response without a valid _rx_nodestate" )
Condition50%1/2
$_rx_nodestate // die("ARGH cannot _send_response without a valid _rx_nodestate")
l!l
30
MC/DC0%0/1
$_rx_nodestate // die("ARGH cannot _send_response without a valid _rx_nodestate")
$_rx_nodestate
Truth table50%1/2
A
A= $_rx_nodestate
Aresult
11
00
691 ->pending_slot( $seqno );
692  
693 3 $slot->{payload} = $payload;
694  
695 3 await $_protocol->send( $pktctrl, $node_id, $payload );
696 8 }
697  
698 14 ▶method _set_retransmit ( $node_id, $seqno )
Subroutine _set_retransmit: called CC=3 SCAR=11.3
699 {
700 14 ▶ my $nodestate = $_nodestate[ $node_id ] //= Device::Serial::SLuRM::_NodeState->new;
Condition33%1/3
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
l!l&&r!l&&!r
1400
MC/DC0%0/2
$_nodestate[$node_id] //= "Device::Serial::SLuRM::_NodeState"->new
$_nodestate[$node_id] "Device::Serial::SLuRM::_NodeState"->new
Truth table33%1/3
A //= B
A= $_nodestate[$node_id]B= "Device::Serial::SLuRM::_NodeState"->new
ABresult
1X1
011
000
701  
702 14 ▶ my $slot = $nodestate->pending_slot( $seqno ) or die "ARG expected $seqno request";
Branch: 1 branch
BranchTrueFalseTotal
my $slot = $nodestate->pending_slot($seqno) or die "ARG expected $seqno request" 14 0 14
703  
704 $slot->{retransmit_f} = Future::IO->sleep( $_retransmit_delay )
705 4 ▶ ->on_done( sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
706 4 ▶ if( $slot->{retransmit_count}-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot->{'retransmit_count'}--) { } 3 1 4
707 3 printf STDERR "SLuRM retransmit REQUEST(%d)\n", $seqno
708 if DEBUG;
709  
710 3 my $pktctrl = SLURM_PKTCTRL_REQUEST | $seqno;
711 $slot->{retransmit_f} = $_protocol->send( $pktctrl, $node_id | 0x80, $slot->{payload} )
712 0 ▶ ->on_fail( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
713 0 warn "Retransmit failed: @_";
714 0 $slot->{response_f}->fail( @_ );
715 } )
716 3 ▶ ->on_done( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
717 3 $self->_set_retransmit( $node_id, $seqno );
718 3 } );
719  
720 3 ▶ $METRICS and
Branch: 1 branch
BranchTrueFalseTotal
$Device::Serial::SLuRM::METRICS and $Device::Serial::SLuRM::METRICS->inc_counter("retransmits") 0 3 3
721 $METRICS->inc_counter( retransmits => );
722 }
723 else {
724 1 printf STDERR "SLuRM timeout REQUEST(%d)\n", $seqno
725 if DEBUG;
726  
727 1 my $message = sprintf "Request timed out after %d attempts\n", 1 + $_retransmit_count;
728 1 $slot->{response_f}->fail( $message, slurm => undef );
729  
730 1 ▶ $METRICS and
Branch: 1 branch
BranchTrueFalseTotal
$Device::Serial::SLuRM::METRICS and $Device::Serial::SLuRM::METRICS->inc_counter("timeouts") 0 1 1
731 $METRICS->inc_counter( timeouts => );
732  
733 1 $nodestate->clear_pending_slot( $seqno );
734 }
735 14 });
736 8 }
737  
738 =head1 AUTHOR
739  
740 Paul Evans <leonerd@leonerd.org.uk>
741  
742 =cut
743  
744 8 0x55AA;
↑ Summary SLuRM/Protocol.pm »