Skip to content

Commit 5349b61

Browse files
authored
Merge pull request #21 from jerryneedell/jerryn_fifo
do not clear FIFO on receive timeout
2 parents 36ee456 + 23de49e commit 5349b61

File tree

1 file changed

+67
-59
lines changed

1 file changed

+67
-59
lines changed

adafruit_rfm/rfm_common.py

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ async def asyncio_receive( # noqa: PLR0912
417417
# Make sure we are listening for packets.
418418
self.listen()
419419
timed_out = await asyncio_check_timeout(self.payload_ready, timeout, self.timeout_poll)
420+
if timed_out and not self.payload_ready():
421+
# Return if timed out without clearing FIFO
422+
if not keep_listening:
423+
self.idle()
424+
return None
420425
# Payload ready is set, a packet is in the FIFO.
421426
packet = None
422427
# save last RSSI reading
@@ -425,24 +430,23 @@ async def asyncio_receive( # noqa: PLR0912
425430

426431
# Enter idle mode to stop receiving other packets.
427432
self.idle()
428-
if not timed_out:
429-
if self.enable_crc and self.crc_error:
430-
self.crc_error_count += 1
431-
else:
432-
packet = self.read_fifo()
433-
if (packet is not None) and self.radiohead:
434-
if len(packet) < 5:
435-
# reject the packet if it is too small to contain the RAdioHead Header
433+
if self.enable_crc and self.crc_error:
434+
self.crc_error_count += 1
435+
else:
436+
packet = self.read_fifo()
437+
if (packet is not None) and self.radiohead:
438+
if len(packet) < 5:
439+
# reject the packet if it is too small to contain the RAdioHead Header
440+
packet = None
441+
if packet is not None:
442+
if (
443+
self.node != _RH_BROADCAST_ADDRESS # noqa: PLR1714
444+
and packet[0] != _RH_BROADCAST_ADDRESS
445+
and packet[0] != self.node
446+
):
436447
packet = None
437-
if packet is not None:
438-
if (
439-
self.node != _RH_BROADCAST_ADDRESS # noqa: PLR1714
440-
and packet[0] != _RH_BROADCAST_ADDRESS
441-
and packet[0] != self.node
442-
):
443-
packet = None
444-
if not with_header and packet is not None: # skip the header if not wanted
445-
packet = packet[4:]
448+
if not with_header and packet is not None: # skip the header if not wanted
449+
packet = packet[4:]
446450
# Listen again if necessary and return the result packet.
447451
if keep_listening:
448452
self.listen()
@@ -490,6 +494,11 @@ async def asyncio_receive_with_ack( # noqa: PLR0912
490494
# Make sure we are listening for packets.
491495
self.listen()
492496
timed_out = await asyncio_check_timeout(self.payload_ready, timeout, self.timeout_poll)
497+
if timed_out and not self.payload_ready():
498+
# Return if timed out without clearing FIFO
499+
if not keep_listening:
500+
self.idle()
501+
return None
493502
# Payload ready is set, a packet is in the FIFO.
494503
packet = None
495504
# save last RSSI reading
@@ -498,51 +507,50 @@ async def asyncio_receive_with_ack( # noqa: PLR0912
498507

499508
# Enter idle mode to stop receiving other packets.
500509
self.idle()
501-
if not timed_out:
502-
if self.enable_crc and self.crc_error:
503-
self.crc_error_count += 1
504-
else:
505-
packet = self.read_fifo()
506-
if (packet is not None) and self.radiohead:
507-
if len(packet) < 5:
508-
# reject the packet if it is too small to contain the RAdioHead Header
510+
if self.enable_crc and self.crc_error:
511+
self.crc_error_count += 1
512+
else:
513+
packet = self.read_fifo()
514+
if (packet is not None) and self.radiohead:
515+
if len(packet) < 5:
516+
# reject the packet if it is too small to contain the RAdioHead Header
517+
packet = None
518+
if packet is not None:
519+
if (
520+
self.node != _RH_BROADCAST_ADDRESS # noqa: PLR1714
521+
and packet[0] != _RH_BROADCAST_ADDRESS
522+
and packet[0] != self.node
523+
):
509524
packet = None
510-
if packet is not None:
511-
if (
512-
self.node != _RH_BROADCAST_ADDRESS # noqa: PLR1714
513-
and packet[0] != _RH_BROADCAST_ADDRESS
514-
and packet[0] != self.node
515-
):
516-
packet = None
517-
# send ACK unless this was an ACK or a broadcast
518-
elif ((packet[3] & _RH_FLAGS_ACK) == 0) and (
519-
packet[0] != _RH_BROADCAST_ADDRESS
525+
# send ACK unless this was an ACK or a broadcast
526+
elif ((packet[3] & _RH_FLAGS_ACK) == 0) and (
527+
packet[0] != _RH_BROADCAST_ADDRESS
528+
):
529+
# delay before sending Ack to give receiver a chance to get ready
530+
if self.ack_delay is not None:
531+
await asyncio.sleep(self.ack_delay)
532+
# send ACK packet to sender (data is b'!')
533+
await self.asyncio_send(
534+
b"!",
535+
destination=packet[1],
536+
node=packet[0],
537+
identifier=packet[2],
538+
flags=(packet[3] | _RH_FLAGS_ACK),
539+
keep_listening=keep_listening,
540+
)
541+
# reject Retries if we have seen this idetifier from this source before
542+
if (self.seen_ids[packet[1]] == packet[2]) and (
543+
packet[3] & _RH_FLAGS_RETRY
520544
):
521-
# delay before sending Ack to give receiver a chance to get ready
522-
if self.ack_delay is not None:
523-
await asyncio.sleep(self.ack_delay)
524-
# send ACK packet to sender (data is b'!')
525-
await self.asyncio_send(
526-
b"!",
527-
destination=packet[1],
528-
node=packet[0],
529-
identifier=packet[2],
530-
flags=(packet[3] | _RH_FLAGS_ACK),
531-
keep_listening=keep_listening,
532-
)
533-
# reject Retries if we have seen this idetifier from this source before
534-
if (self.seen_ids[packet[1]] == packet[2]) and (
535-
packet[3] & _RH_FLAGS_RETRY
536-
):
537-
packet = None
538-
else: # save the packet identifier for this source
539-
self.seen_ids[packet[1]] = packet[2]
540-
if (
541-
packet is not None and (packet[3] & _RH_FLAGS_ACK) != 0
542-
): # Ignore it if it was an ACK packet
543545
packet = None
544-
if not with_header and packet is not None: # skip the header if not wanted
545-
packet = packet[4:]
546+
else: # save the packet identifier for this source
547+
self.seen_ids[packet[1]] = packet[2]
548+
if (
549+
packet is not None and (packet[3] & _RH_FLAGS_ACK) != 0
550+
): # Ignore it if it was an ACK packet
551+
packet = None
552+
if not with_header and packet is not None: # skip the header if not wanted
553+
packet = packet[4:]
546554
# Listen again if necessary and return the result packet.
547555
if keep_listening:
548556
self.listen()

0 commit comments

Comments
 (0)