Tigge / openant

ANT and ANT-FS Python Library
MIT License
174 stars 80 forks source link

antfs-cli fails with timeout #95

Closed robho closed 7 months ago

robho commented 7 months ago

I've been trying to download logs from my Garmin Forerunner watch and have found that it fails with recent openant versions.

I applied #93 and #94 and then it worked up until 6eb8e19233a9960d316b168d5c0cbf16c8be54e6. After this commit it says:

Request basic information...
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/antfs_cli-0.4-py3.9.egg/antfs_cli/program.py", line 382, in main
    g = AntFSCLI(config_dir, args)
  File "/usr/local/lib/python3.9/dist-packages/antfs_cli-0.4-py3.9.egg/antfs_cli/program.py", line 140, in __init__
    Application.__init__(self)
  File "/usr/local/lib/python3.9/dist-packages/ant/fs/manager.py", line 141, in __init__
    raise e
  File "/usr/local/lib/python3.9/dist-packages/ant/fs/manager.py", line 115, in __init__
    m = self._node.request_message(Message.ID.RESPONSE_CAPABILITIES)
  File "/usr/local/lib/python3.9/dist-packages/ant/easy/node.py", line 116, in request_message
    return self.wait_for_special(messageId)
  File "/usr/local/lib/python3.9/dist-packages/ant/easy/node.py", line 144, in wait_for_special
    return wait_for_special(event_id, self._responses, self._responses_cond)
  File "/usr/local/lib/python3.9/dist-packages/ant/easy/filter.py", line 111, in wait_for_special
    return wait_for_message(match, process, queue, condition)
  File "/usr/local/lib/python3.9/dist-packages/ant/easy/filter.py", line 62, in wait_for_message
    raise AntException("Timed out while waiting for message")
ant.easy.exception.AntException: Timed out while waiting for message
Interrupted: Timed out while waiting for message

I'll take a closer look at this breaking commit, but if you have any pointers to what to try, that would be good.

This is also reported in Tigge/antfs-cli#194.

robho commented 7 months ago

As I wrote above the problem started with 6eb8e19233a9960d316b168d5c0cbf16c8be54e6.

I can restore functionality with either of these changes:

--- a/openant/easy/node.py
+++ b/openant/easy/node.py
@@ -124,11 +124,11 @@ class Node:
         elif event == Message.ID.RESPONSE_ANT_VERSION:
             self.ant_version = bytes(data).decode("ascii")
             _logger.debug(f"ant_version {self.ant_version}")
-        else:
-            self._responses_cond.acquire()
-            self._responses.append((channel, event, data))
-            self._responses_cond.notify()
-            self._responses_cond.release()
+
+        self._responses_cond.acquire()
+        self._responses.append((channel, event, data))
+        self._responses_cond.notify()
+        self._responses_cond.release()

     def _worker_event(self, channel, event, data):
         _logger.debug(f"_worker_event {channel}, {event}, {data}")

or:

--- a/openant/fs/manager.py
+++ b/openant/fs/manager.py
@@ -112,8 +112,8 @@ class Application:

             print("Request basic information...")

-            m = self._node.request_message(Message.ID.RESPONSE_CAPABILITIES)
-            print("  Capabilities: ", m[2])
+            # m = self._node.request_message(Message.ID.RESPONSE_CAPABILITIES)
+            # print("  Capabilities: ", m[2])

             # m = self._node.request_message(Message.ID.RESPONSE_ANT_VERSION)
             # print "  ANT version:  ", struct.unpack("<10sx", m[2])[0]

The problem seems to be that the capabilities response requested by fs/manager gets ignored/discarded. Can you create a proper fix from this information? I don't know what the expected behaviour is.

tuna-f1sh commented 7 months ago

What USB ANT+ stick are you using? A try/catch can be put around the request for capabilities but it's odd that it isn't being responded to.

robho commented 7 months ago

It's not a hardware problem.

The scenario is:

  1. fs/manager requests CAPABILITIES and waits for a response (calls Node.request_message())
  2. A response arrives to Node._worker_response() but the response is not added to Node._responses (this is the problem and it's new behaviour starting with 6eb8e192. See https://github.com/Tigge/openant/commit/6eb8e19233a9960d316b168d5c0cbf16c8be54e6#diff-10a81464831c734c4ccadcc3c0ea9aeae3816c32e676baf8c305688ae3b4f60dR113)
  3. The call to Node.request_message() times out because it's never notified of the received response

The same problem seems to exist for SERIAL_NUMBER and ANT_VERSION requests. Ie it's not possible to send these messages and receive a response.

tuna-f1sh commented 7 months ago

Ah I see the issue when you explain it like that; I've not used the antfs stuff myself and didn't have time to look into it when you opened this. Thanks for the PR to just fix it since this stuff isn't used and if is needed, can be grabbed from https://github.com/Tigge/openant/blob/master/openant/easy/node.py#L151 etc.