Skip to content

Commit

Permalink
introduced controller frames stats (#15)
Browse files Browse the repository at this point in the history
bug fixes
style fixes
improved watchdog, removed typo
bump version
  • Loading branch information
Yoda-x authored Jan 7, 2019
1 parent bbd5ba5 commit 2817e31
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
3 changes: 3 additions & 0 deletions bellows/ezsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def __init__(self):

def status(self):
return self._gw.status()

def stats(self):
return self._gw.stats()

async def connect(self, device, baudrate):
assert self._gw is None
Expand Down
28 changes: 24 additions & 4 deletions bellows/uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,26 @@ def __init__(self, application, connected_future=None):
self._Run_Event = asyncio.Event()
self._Run_Event.set()
self._task_send_task = None
self._stats_frames_rx = 0
self._stats_frames_rx_dup = 0
self._stats_frames_tx = 0
self._stats_frames_error = 0
self._stats_frames_reset = 0

def status(self):
return [bool(self._failed_mode),
self._task_send_task.done(),
not self._Run_Event.is_set(),
]

def stats(self):
return {
"stats_frames_rx": self._stats_frames_rx,
"stats_frames_tx": self._stats_frames_tx,
"stats_frames_rx_dup": self._stats_frames_rx_dup,
"stats_frames_error": self._stats_frames_error,
"stats_frames_reset": self._stats_frames_reset,
}

def connection_made(self, transport):
"""Callback when the uart is connected."""
Expand Down Expand Up @@ -87,6 +101,7 @@ def _extract_frame(self, data):
return None, data

def frame_received(self, data):
self._stats_frames_rx += 1
LOGGER.debug("Status _send_task.done: %s", self._task_send_task.done())
"""Frame receive handler."""
if (data[0] & 0b10000000) == 0:
Expand All @@ -108,10 +123,11 @@ def data_frame_received(self, data):
"""Data frame receive handler."""
seq = (data[0] & 0b01110000) >> 4

if data[0] & self.reTx:
retrans = 1
else:
retrans = 0
retrans = 1 if data[0] & self.reTx else 0
# if data[0] & self.reTx:
# retrans = 1
# else:
# retrans = 0
LOGGER.debug("Data frame SEQ(%s)/ReTx(%s): %s", seq, retrans, binascii.hexlify(data))
if self._rec_seq != seq and not retrans:
if not self._reject_mode:
Expand All @@ -130,6 +146,7 @@ def data_frame_received(self, data):
frame_data = self._randomize(data[1:-3])
if retrans and (self._rx_buffer[seq] == frame_data):
LOGGER.debug("DUP Data frame SEQ(%s)/ReTx(%s): %s", seq, retrans, binascii.hexlify(data))
self._stats_frames_rx_dup += 1
return

self._rx_buffer[seq] = frame_data
Expand Down Expand Up @@ -176,6 +193,7 @@ def rstack_frame_received(self, data):

def error_frame_received(self, data):
"""Error frame receive handler."""
self._stats_frames_error += 1
try:
code = t.NcpResetCode(data[2])
except ValueError:
Expand All @@ -202,6 +220,7 @@ def close(self):

async def reset(self):
"""Sends a reset frame."""
self._stats_frames_reset += 1
LOGGER.debug("Sending: RESET")
# TODO: It'd be nice to delete self._reset_future.
if self._reset_future is not None:
Expand Down Expand Up @@ -266,6 +285,7 @@ def _handle_nak(self, control):

def data(self, data):
"""Send a data frame."""
self._stats_frames_tx += 1
self._sendq.put_nowait((data))
LOGGER.debug("add command to send queue: %s-%s", self._task_send_task.done(), self._Run_Event.is_set())

Expand Down
7 changes: 5 additions & 2 deletions bellows/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ def status(self):
self._watchdog_task.done(),
self._pull_frames_task.done()]
]

def stats(self):
return self._ezsp.stats()

async def _watchdog(self, wakemeup=60):
"""run in background, checks if uart restart hangs and restart as needed."""
while True:
try:
await asyncio.sleep(wakemeup)
LOGGER.debug("watchdog: running")
if self._startup or bool(sum(self._ezsp.status())):
if self._startup or (sum(self._ezsp.status())):
LOGGER.error("watchdog: startup running or failed uart restart ")
if self._startup_task and not self._startup_task.done():
self._startup_task.cancel()
Expand All @@ -55,7 +58,7 @@ async def _watchdog(self, wakemeup=60):
except asyncio.CancelledError:
LOGGER.info("watchdog: startup terminated, catched Cancelled")
self._startup = False
self._startup_task = asyncio.ensure_future(self._startup())
self._startup_task = asyncio.ensure_future(self.startup())
except Exception as e:
LOGGER.info("watchdog: catched %s", e)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="bellows",
version="0.7.4.1",
version="100.7.4.5",
description="Library implementing EZSP",
# url="http://github.com/Yoda-x/bellows",
author="orig by Russell Cloran",
Expand Down

0 comments on commit 2817e31

Please sign in to comment.