Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle empty file, Remove second purge, Fix dead loop #50

Merged
merged 3 commits into from
May 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions xmodem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0):
error_count = 0
char = 0
cancel = 0
empty = 0
while True:
# first try CRC mode, if this fails,
# fall back to checksum mode
Expand Down Expand Up @@ -459,6 +460,19 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0):
else:
self.log.debug('cancellation at start sequence.')
cancel = 1
elif char == EOT:
if empty:
self.log.info('transmission canceled: file empty ')
self.putc(CAN)
self.putc(CAN)
while True:
if self.getc(1, timeout=1) is None:
break
time.sleep(.001) # better cpu usage
return 0
else:
self.log.info('first eot received ')
empty = 1
else:
error_count += 1

Expand Down Expand Up @@ -508,6 +522,10 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0):
retry)
self.abort()
return None
time.sleep(timeout)

# Reads next char from stream
char = self.getc(1, timeout=1)

# read sequence
error_count = 0
Expand All @@ -530,8 +548,7 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0):
# consume data anyway ... even though we will discard it,
# it is not the sequence we expected!
self.log.error('expected sequence %d, '
'got (seq1=%r, seq2=%r), '
'receiving next block, will NAK.',
'got (seq1=%r, seq2=%r), ',
sequence, seq1, seq2)
self.getc(packet_size + 1 + crc_mode)
else:
Expand All @@ -546,27 +563,18 @@ def recv(self, stream, crc_mode=1, retry=16, timeout=60, delay=1, quiet=0):
stream.write(data)
self.putc(ACK)
sequence = (sequence + 1) % 0x100
# get next start-of-header byte
# get possible start-of-header byte
# may be echoed ACK, NAK signal if so purge
char = self.getc(1, timeout)
if char in { ACK, NAK }:
char = self.getc(1, timeout)
continue

# something went wrong, request retransmission
self.log.warning('recv error: purge, requesting retransmission (NAK)')
while True:
# When the receiver wishes to <nak>, it should call a "PURGE"
# subroutine, to wait for the line to clear. Recall the sender
# tosses any characters in its UART buffer immediately upon
# completing sending a block, to ensure no glitches were mis-
# interpreted. The most common technique is for "PURGE" to
# call the character receive subroutine, specifying a 1-second
# timeout, and looping back to PURGE until a timeout occurs.
# The <nak> is then sent, ensuring the other end will see it.
data = self.getc(1, timeout=1)
if data is None:
break
self.putc(NAK)
# get next start-of-header byte
# get possible start-of-header byte
# may be echoed ACK, NAK signal if so purge
char = self.getc(1, timeout)
if char in { ACK, NAK }:
char = self.getc(1, timeout)
continue

def _verify_recv_checksum(self, crc_mode, data):
Expand Down