Skip to content

Commit 0160bd7

Browse files
committed
manage uploadFile2 outstanding packets; perltidy
1 parent cf776ca commit 0160bd7

File tree

1 file changed

+66
-24
lines changed

1 file changed

+66
-24
lines changed

qdload.pl

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ sub writeChunk {
9696
}
9797

9898
sub writeChunk2 {
99-
my $fd = shift;
100-
my $address = shift;
101-
my $chunk = shift;
99+
my $fd = shift;
100+
my $address = shift;
101+
my $chunk = shift;
102+
my $sentPacketsRef = shift;
102103

103104
my $length = length($chunk);
104105
my $response;
@@ -116,17 +117,25 @@ sub writeChunk2 {
116117
print "Failed to send chunk.\n";
117118
return undef;
118119
}
119-
if ( !( $response = readPacket( $fd, 0.001 ) ) ) {
120-
return 1;
121-
}
120+
$sentPacketsRef->{ sprintf( "%0.8x", $address ) } = 1;
122121

123-
my @responseBytes = unpack( 'C*', $response );
124-
if ( $responseBytes[0] != 8 ) {
125-
print "Invalid response code\n";
126-
exit 1;
127-
}
128-
else {
129-
print "Got response: ", serialize($response), "\n";
122+
while ( $response = readPacket( $fd, 0.0001 ) ) {
123+
my @responseBytes = unpack( 'C*', $response );
124+
if ( scalar @responseBytes != 5 || $responseBytes[0] != 8 ) {
125+
print "Invalid Response: ", serialize($response), "\n";
126+
exit 1;
127+
}
128+
129+
my $ackpacket;
130+
$ackpacket = sprintf(
131+
'%.2x%.2x%.2x%.2x',
132+
$responseBytes[4], $responseBytes[3],
133+
$responseBytes[2], $responseBytes[1]
134+
);
135+
136+
delete $sentPacketsRef->{$ackpacket};
137+
print "Response: ACK 0x", $ackpacket, " (outstanding: ",
138+
scalar keys %{$sentPacketsRef}, ")\n";
130139
}
131140
return 1;
132141
}
@@ -153,9 +162,9 @@ sub uploadFile {
153162
" bytes left.\n";
154163

155164
if ( !writeChunk( $fd, $address, $chunk ) ) {
156-
print "uploadFile failed.\n";
157-
exit 1;
158-
}
165+
print "uploadFile failed.\n";
166+
exit 1;
167+
}
159168

160169
$address += length($chunk);
161170
$data = $restOfData;
@@ -169,6 +178,8 @@ sub uploadFile2 {
169178
my $filename = shift;
170179
my $response;
171180

181+
my %sentPackets = ();
182+
172183
local $/ = undef;
173184

174185
return undef if ( !open( FILE, $filename ) );
@@ -185,16 +196,44 @@ sub uploadFile2 {
185196
sprintf( '%.8x', $address ), "; ", length($restOfData),
186197
" bytes left.\n";
187198

188-
return undef if ( !writeChunk2( $fd, $address, $chunk ) );
199+
return undef
200+
if ( !writeChunk2( $fd, $address, $chunk, \%sentPackets ) );
189201

190202
$address += length($chunk);
191203
$data = $restOfData;
192204

193205
#select(undef, undef, undef, 0.1);
194206
}
195-
while ( $response = readPacket( $fd, 2 ) ) {
196-
print "Got response: ", serialize($response), "\n";
207+
208+
my $numout = scalar keys %sentPackets;
209+
210+
while ( $numout > 0 ) {
211+
if ( !( $response = readPacket( $fd, 5.0 ) ) ) {
212+
print "Failed to receive ACK for ", $numout, " packets\n";
213+
foreach my $packet ( keys %sentPackets ) {
214+
print "Outstanding: 0x", $packet, "\n";
215+
}
216+
exit 1;
217+
}
218+
219+
my @responseBytes = unpack( 'C*', $response );
220+
if ( scalar @responseBytes != 5 || $responseBytes[0] != 8 ) {
221+
print "Invalid Response: ", serialize($response), "\n";
222+
exit 1;
223+
}
224+
225+
my $ackpacket;
226+
$ackpacket = sprintf(
227+
'%.2x%.2x%.2x%.2x',
228+
$responseBytes[4], $responseBytes[3],
229+
$responseBytes[2], $responseBytes[1]
230+
);
231+
232+
delete $sentPackets{$ackpacket};
233+
$numout = scalar keys %sentPackets;
234+
print "Response: ACK 0x", $ackpacket, " (outstanding: ", $numout, ")\n";
197235
}
236+
198237
return 1;
199238
}
200239

@@ -586,7 +625,7 @@ sub doCloseFlush {
586625
exit 1;
587626
}
588627

589-
if ( !( $response = readPacket($fd, 2.0) ) ) {
628+
if ( !( $response = readPacket( $fd, 2.0 ) ) ) {
590629
print "Failed to read response.\n";
591630
exit 1;
592631
}
@@ -607,7 +646,7 @@ sub doSecureMode {
607646
exit 1;
608647
}
609648

610-
if ( !( $response = readPacket($fd, 2.0) ) ) {
649+
if ( !( $response = readPacket( $fd, 2.0 ) ) ) {
611650
print "Failed to read response.\n";
612651
exit 1;
613652
}
@@ -628,13 +667,16 @@ sub doOpenMulti {
628667
exit 1;
629668
}
630669

631-
if ( !( $response = readPacket($fd, 2.0) ) ) {
670+
if ( !( $response = readPacket( $fd, 2.0 ) ) ) {
632671
print "Failed to read response.\n";
633672
exit 1;
634673
}
635674

636675
my @responseBytes = unpack( 'C*', $response );
637-
if ( scalar @responseBytes != 2 || $responseBytes[0] != 0x1c || $responseBytes[1] != 0 ) {
676+
if ( scalar @responseBytes != 2
677+
|| $responseBytes[0] != 0x1c
678+
|| $responseBytes[1] != 0 )
679+
{
638680
print "Invalid Response: ", serialize($response), "\n";
639681
exit 1;
640682
}
@@ -649,7 +691,7 @@ sub doReset2 {
649691
exit 1;
650692
}
651693

652-
if ( !( $response = readPacket($fd, 2.0) ) ) {
694+
if ( !( $response = readPacket( $fd, 2.0 ) ) ) {
653695
print "Failed to read response.\n";
654696
exit 1;
655697
}

0 commit comments

Comments
 (0)