Repository navigation
fix: reject empty I2C reads before device commands #312
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
Open
YashAnand69
wants to merge
2
commits into
fossasia:main
Choose a base branch
from
YashAnand69:fix/i2c-empty-read
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """Empty I2C reads must not consume sensor bytes or resize output buffers.""" | ||
|
|
||
| import pytest | ||
|
|
||
| import pslab.protocol as CP | ||
| from pslab.bus.busio import I2C | ||
|
|
||
|
|
||
| class I2CConnection: | ||
| """Record primitive commands and provide deterministic sensor bytes.""" | ||
|
|
||
| def __init__(self): | ||
| self.commands = [] | ||
| self.bytes_read = 0 | ||
|
|
||
| def send_byte(self, value): | ||
| """Record command and parameter bytes.""" | ||
| self.commands.append(value) | ||
|
|
||
| def send_int(self, value): | ||
| """Accept the constructor's bus frequency configuration.""" | ||
| assert 0 <= value <= 65535 | ||
|
|
||
| def get_ack(self): | ||
| """Acknowledge commands and the addressed peripheral.""" | ||
| return 1 | ||
|
|
||
| def get_byte(self): | ||
| """Return a distinct byte for each sensor read.""" | ||
| self.bytes_read += 1 | ||
| return 0x40 + self.bytes_read | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("combined", [False, True]) | ||
| @pytest.mark.parametrize("view", [False, True]) | ||
| @pytest.mark.parametrize("initial, offset", [(b"", 0), (b"abc", 1)]) | ||
| def test_empty_read_preserves_buffer_and_does_not_read_sensor( | ||
| combined, view, initial, offset | ||
| ): | ||
| connection = I2CConnection() | ||
| bus = I2C(connection) | ||
| storage = bytearray(initial) | ||
| output = memoryview(storage) if view else storage | ||
|
|
||
| connection.commands.clear() | ||
|
|
||
| with pytest.raises(ValueError, match="at least one byte"): | ||
| if combined: | ||
| bus.writeto_then_readfrom( | ||
| 0x40, b"\x10", output, in_start=offset, in_end=offset | ||
| ) | ||
| else: | ||
| bus.readfrom_into(0x40, output, start=offset, end=offset) | ||
|
|
||
| assert storage == initial | ||
| assert connection.bytes_read == 0 | ||
| assert CP.I2C_READ_MORE not in connection.commands | ||
| assert CP.I2C_READ_END not in connection.commands | ||
| assert connection.commands == [] | ||
| assert bus._running is False | ||
| assert bus._mode is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("combined", [False, True]) | ||
| @pytest.mark.parametrize("count", [1, 3]) | ||
| def test_nonempty_read_keeps_ack_nack_sequence_and_slice(combined, count): | ||
| connection = I2CConnection() | ||
| bus = I2C(connection) | ||
| storage = bytearray(b"z" * (count + 2)) | ||
|
|
||
| if combined: | ||
| bus.writeto_then_readfrom(0x40, b"\x10", storage, in_start=1, in_end=count + 1) | ||
| else: | ||
| bus.readfrom_into(0x40, storage, start=1, end=count + 1) | ||
|
|
||
| assert storage == b"z" + bytes(range(0x41, 0x41 + count)) + b"z" | ||
| assert connection.bytes_read == count | ||
| assert connection.commands.count(CP.I2C_READ_MORE) == count - 1 | ||
| assert connection.commands.count(CP.I2C_READ_END) == 1 | ||
| assert connection.commands[-2:] == [CP.I2C_HEADER, CP.I2C_STOP] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("combined", [False, True]) | ||
| @pytest.mark.parametrize("view", [False, True]) | ||
| @pytest.mark.parametrize("start, end", [(5, 6), (1, 8), (-2, None), (-8, 2)]) | ||
| def test_read_uses_effective_python_slice(combined, view, start, end): | ||
| connection = I2CConnection() | ||
| bus = I2C(connection) | ||
| storage = bytearray(b"abc") | ||
| output = memoryview(storage) if view else storage | ||
| selected = storage[start:end] | ||
| expected = storage.copy() | ||
| connection.commands.clear() | ||
|
|
||
| def read(): | ||
| if combined: | ||
| bus.writeto_then_readfrom(0x40, b"\x10", output, in_start=start, in_end=end) | ||
| else: | ||
| bus.readfrom_into(0x40, output, start=start, end=end) | ||
|
|
||
| if not selected: | ||
| with pytest.raises(ValueError, match="at least one byte"): | ||
| read() | ||
| assert connection.commands == [] | ||
| else: | ||
| read() | ||
| expected[start:end] = bytes(range(0x41, 0x41 + len(selected))) | ||
| assert connection.commands[-2:] == [CP.I2C_HEADER, CP.I2C_STOP] | ||
| assert storage == expected | ||
| assert connection.bytes_read == len(selected) | ||
| assert bus._running is False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.