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

Possible error in _handle_command_or_response() #118

Open
gbrooker opened this issue Mar 5, 2024 · 0 comments
Open

Possible error in _handle_command_or_response() #118

gbrooker opened this issue Mar 5, 2024 · 0 comments

Comments

@gbrooker
Copy link

gbrooker commented Mar 5, 2024

Hi, I have been writing my own code (in swift) to interact with a fingerbot from an iPhone. Thanks very much to the developers here for this code, it has been a very helpful reference for my development.

While working on my handler for responses, I noticed what may be a couple of errors in _handle_command_or_response() in tuya_ble.py. The following code, starting at line 1130 handles DP responses which also have a time and or flag combined with the DPs.

Firstly, when a response has a flag field, a 16bit sequence number is extracted first. An acknowledgment packet is then created, with both the extracted sequence number and flags, however the code below writes the sequence number into the acknowledgment as 8 bits, not 16bits, which does not seem correct. This anomaly is present for both the FUN_RECEIVE_SIGN_DP and FUN_RECEIVE_SIGN_TIME_DP cases.

Secondly, once the sequence number and flag fields have been extracted, the DP extraction should begin from the next byte. However in the FUN_RECEIVE_SIGN_DP case, the DP extraction begins at byte 2 (which ought to be the flag byte), but logically it should begin at byte 3.

I have not seen any of these responses from my device, so I cannot check the correct behavior, but thought I should raise it here

            case TuyaBLECode.FUN_RECEIVE_SIGN_DP:
                dp_seq_num = int.from_bytes(data[:2], "big")            # NB: two bytes long
                flags = data[2]
                self._parse_datapoints_v3(time.time(), flags, data, 2)  # <--- Should that be 3 ?
                data = pack(">HBB", dp_seq_num, flags, 0)               # <--- dp_seq_num 16bit int, not 8bit
                asyncio.create_task(self._send_response(code, data, seq_num))

            case TuyaBLECode.FUN_RECEIVE_TIME_DP:
                timestamp: float
                pos: int
                timestamp, pos = self._parse_timestamp(data, 0)
                self._parse_datapoints_v3(timestamp, 0, data, pos)
                asyncio.create_task(
                    self._send_response(code, bytes(0), seq_num))

            case TuyaBLECode.FUN_RECEIVE_SIGN_TIME_DP:
                timestamp: float
                pos: int
                dp_seq_num = int.from_bytes(data[:2], "big")            # NB: two bytes long
                flags = data[2]
                timestamp, pos = self._parse_timestamp(data, 3)
                self._parse_datapoints_v3(time.time(), flags, data, pos)
                data = pack(">HBB", dp_seq_num, flags, 0)               # <--- dp_seq_num 16bit int, not 8bit
                asyncio.create_task(self._send_response(code, data, seq_num))

Finally, one question: What type of devices give these responses and under what circumstances ? What is the purpose of the flags and timestamps ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant