From 430f4839ff5d4a168150cf095472ecf4da733dce Mon Sep 17 00:00:00 2001 From: Wojciech Bober Date: Thu, 15 Dec 2016 18:04:55 +0100 Subject: [PATCH] [spinel-py] Allow for waiting for a given property change (#1081) * [spinel-py] Allow to wait for a particular property * [spinel-py] Added reset command to codec.py --- tools/spinel-cli/spinel/codec.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/tools/spinel-cli/spinel/codec.py b/tools/spinel-cli/spinel/codec.py index 4a72182a3..0716bb126 100644 --- a/tools/spinel-cli/spinel/codec.py +++ b/tools/spinel-cli/spinel/codec.py @@ -848,13 +848,28 @@ class WpanApi(SpinelCodec): with self.__queue_prop[tid].mutex: self.__queue_prop[tid].queue.clear() - def queue_wait_for_prop(self, _prop, tid=SPINEL.HEADER_DEFAULT, timeout=TIMEOUT_PROP): + def queue_get(self, tid, timeout = None): try: - item = self.__queue_prop[tid].get(True, timeout) - # self.__queue_prop[tid].task_done() + if (timeout): + item = self.__queue_prop[tid].get(True, timeout) + else: + item = self.__queue_prop[tid].get_nowait() except Queue.Empty: item = None + return item + def queue_wait_for_prop(self, _prop, tid=SPINEL.HEADER_DEFAULT, timeout=TIMEOUT_PROP): + start = time.time() + item = self.queue_get(tid, timeout) + if (_prop is not None): + while (item and (item.prop != _prop)): + self.__queue_prop[tid].put_nowait(item) + reminder = timeout - (time.time() - start) + if (reminder <= 0.0): + item = None + break + item = self.queue_get(tid, reminder) + # self.__queue_prop[tid].task_done() return item def ip_send(self, pkt): @@ -868,6 +883,12 @@ class WpanApi(SpinelCodec): self.transact(SPINEL.CMD_PROP_VALUE_SET, pay) + def cmd_reset(self): + self.queue_wait_prepare(None, SPINEL.HEADER_ASYNC) + self.transact(SPINEL.CMD_RESET, "", SPINEL.HEADER_DEFAULT) + result = self.queue_wait_for_prop(SPINEL.PROP_LAST_STATUS, SPINEL.HEADER_ASYNC, 5) + return (result is not None and result.value == 114) + def cmd_send(self, command_id, payload="", tid=SPINEL.HEADER_DEFAULT): self.queue_wait_prepare(None, tid) self.transact(command_id, payload, tid)