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

Create an enum for valid device rotations #4125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions common/protob/messages-management.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ enum SafetyCheckLevel {
PromptTemporarily = 2; // like PromptAlways but reverts to Strict after reboot
}

/**
* Allowed display rotation angles (in degrees from North)
*/
enum DisplayRotation {
North = 0;
East = 90;
South = 180;
West = 270;
}

/**
* Format of the homescreen image
Expand Down Expand Up @@ -101,7 +110,7 @@ message Features {
optional bool passphrase_always_on_device = 36; // device enforces passphrase entry on Trezor
optional SafetyCheckLevel safety_checks = 37; // safety check level, set to Prompt to limit path namespace enforcement
optional uint32 auto_lock_delay_ms = 38; // number of milliseconds after which the device locks itself
optional uint32 display_rotation = 39; // in degrees from North
optional DisplayRotation display_rotation = 39; // rotation of display (in degrees from North)
optional bool experimental_features = 40; // are experimental message types enabled?
optional bool busy = 41; // is the device busy, showing "Do not disconnect"?
optional HomescreenFormat homescreen_format = 42; // format of the homescreen, 1 = TOIf, 2 = jpg, 3 = TOIG
Expand Down Expand Up @@ -200,7 +209,7 @@ message ApplySettings {
optional bytes homescreen = 4;
optional uint32 _passphrase_source = 5 [deprecated=true]; // ASK = 0; DEVICE = 1; HOST = 2;
optional uint32 auto_lock_delay_ms = 6;
optional uint32 display_rotation = 7; // in degrees from North
optional DisplayRotation display_rotation = 7; // rotation of display (in degrees from North)
optional bool passphrase_always_on_device = 8; // do not prompt for passphrase, enforce device entry
optional SafetyCheckLevel safety_checks = 9; // Safety check level, set to Prompt to limit path namespace enforcement
optional bool experimental_features = 10; // enable experimental message types
Expand Down
2 changes: 2 additions & 0 deletions core/src/all_modules.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions core/src/apps/management/apply_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import storage.device as storage_device
import trezorui2
from trezor import TR, utils
from trezor.enums import ButtonRequestType
from trezor.enums import ButtonRequestType, DisplayRotation
from trezor.ui.layouts import confirm_action
from trezor.wire import DataError

Expand Down Expand Up @@ -167,17 +167,17 @@ async def _require_confirm_change_passphrase_source(
await confirm_change_passphrase_source(passphrase_always_on_device)


async def _require_confirm_change_display_rotation(rotation: int) -> None:
if rotation == 0:
async def _require_confirm_change_display_rotation(rotation: DisplayRotation) -> None:
if rotation == DisplayRotation.North:
label = TR.rotation__north
elif rotation == 90:
elif rotation == DisplayRotation.East:
label = TR.rotation__east
elif rotation == 180:
elif rotation == DisplayRotation.South:
label = TR.rotation__south
elif rotation == 270:
elif rotation == DisplayRotation.West:
label = TR.rotation__west
else:
raise DataError("Unsupported display rotation")
raise RuntimeError # Unsupported display rotation

await confirm_action(
"set_rotation",
Expand Down
25 changes: 18 additions & 7 deletions core/src/storage/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from trezor import utils

if TYPE_CHECKING:
from trezor.enums import BackupType
from trezor.enums import BackupType, DisplayRotation
from typing_extensions import Literal

# Namespace:
Expand Down Expand Up @@ -95,16 +95,27 @@ def get_device_id() -> str:
return dev_id.decode()


def get_rotation() -> int:
def get_rotation() -> DisplayRotation:
from trezor.enums import DisplayRotation

rotation = common.get(_NAMESPACE, _ROTATION, public=True)
if not rotation:
return 0
return int.from_bytes(rotation, "big")
return DisplayRotation.North # Default to North if no rotation is set

value = int.from_bytes(rotation, "big")
if value == 90:
rotation = DisplayRotation.East
elif value == 180:
rotation = DisplayRotation.South
elif value == 270:
rotation = DisplayRotation.West
else:
rotation = DisplayRotation.North

return rotation


def set_rotation(value: int) -> None:
if value not in (0, 90, 180, 270):
raise ValueError # unsupported display rotation
def set_rotation(value: DisplayRotation) -> None:
common.set(_NAMESPACE, _ROTATION, value.to_bytes(2, "big"), True) # public


Expand Down
8 changes: 8 additions & 0 deletions core/src/trezor/enums/DisplayRotation.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions core/src/trezor/enums/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions core/src/trezor/messages.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions python/src/trezorlib/cli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
except ImportError:
PIL_AVAILABLE = False

ROTATION = {"north": 0, "east": 90, "south": 180, "west": 270}
ROTATION = {
"north": messages.DisplayRotation.North,
"east": messages.DisplayRotation.East,
"south": messages.DisplayRotation.South,
"west": messages.DisplayRotation.West,
}

SAFETY_LEVELS = {
"strict": messages.SafetyCheckLevel.Strict,
"prompt": messages.SafetyCheckLevel.PromptTemporarily,
Expand Down Expand Up @@ -261,7 +267,7 @@ def language(
@cli.command()
@click.argument("rotation", type=ChoiceType(ROTATION))
@with_client
def display_rotation(client: "TrezorClient", rotation: int) -> str:
def display_rotation(client: "TrezorClient", rotation: messages.DisplayRotation) -> str:
"""Set display rotation.

Configure display rotation for Trezor Model T. The options are
Expand Down
2 changes: 1 addition & 1 deletion python/src/trezorlib/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def apply_settings(
homescreen: Optional[bytes] = None,
passphrase_always_on_device: Optional[bool] = None,
auto_lock_delay_ms: Optional[int] = None,
display_rotation: Optional[int] = None,
display_rotation: Optional[messages.DisplayRotation] = None,
safety_checks: Optional[messages.SafetyCheckLevel] = None,
experimental_features: Optional[bool] = None,
hide_passphrase_from_host: Optional[bool] = None,
Expand Down
15 changes: 11 additions & 4 deletions python/src/trezorlib/messages.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/device_tests/test_msg_applysettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def test_apply_settings_rotation(client: Client):

with client:
_set_expected_responses(client)
device.apply_settings(client, display_rotation=270)
device.apply_settings(client, display_rotation=messages.DisplayRotation.West)

assert client.features.display_rotation == 270
assert client.features.display_rotation == messages.DisplayRotation.West


@pytest.mark.setup_client(pin=PIN4, passphrase=False)
Expand Down
Loading