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

USB data transmission timeout handling && fingerprint/camera error prompts #229

Merged
merged 4 commits into from
Jan 15, 2025

Conversation

lihuanhuan
Copy link
Contributor

@lihuanhuan lihuanhuan commented Jan 15, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced error handling for camera and fingerprint module operations.
    • Improved WebUSB write functionality with better timeout management.
    • Added status checks before performing critical operations.
  • Refactor

    • Updated function return types to use boolean values for clarity.
    • Simplified error handling in various system components.
  • Chores

    • Reduced test duration from 3 hours to 2 hours.
    • Added more robust status tracking for hardware modules.

@lihuanhuan lihuanhuan requested a review from a team as a code owner January 15, 2025 07:06
Copy link

coderabbitai bot commented Jan 15, 2025

Walkthrough

The pull request introduces modifications across multiple files in the core embedded system, focusing on improving error handling, status tracking, and operational checks for various hardware components like USB, camera, and fingerprint modules. The changes enhance robustness by adding more precise status checks, modifying function return types, and implementing more comprehensive error detection mechanisms.

Changes

File Change Summary
core/embed/extmod/modtrezorio/... Updated mod_trezorio_poll function with improved write operation handling
core/embed/fp_sensor_wrapper/fingerprint.{c,h} Added status tracking for fingerprint module, new status retrieval function
core/embed/fp_sensor_wrapper/fpsensor_common.c Enhanced hardware ID retrieval with improved SPI transaction error handling
core/embed/trezorhal/camera.{c,h} Modified camera-related functions to use boolean returns, improved error handling
core/embed/trezorhal/camera_qrcode.c Added camera online status check before QR code decoding
core/embed/trezorhal/device.c Reduced test duration, added component status checks for testing
core/embed/trezorhal/usb_webusb-{defs,impl}.h Changed WebUSB write function return type, added write operation timeout handling
core/src/trezor/wire/codec_v1.py Added write operation error handling with timeout detection

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Nitpick comments (3)
core/embed/trezorhal/camera.c (3)

182-184: Log I2C errors.

Silent failure in I2C operations.


229-231: Log camera offline errors.

Silent failure when camera is offline.


233-233: Simplify return statement.

Unnecessary temporary variable.

-    ret = (read == GC2145_ADDR) ? true : false;
-
-    return ret;
+    return read == GC2145_ADDR;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 59472af and fa82382.

📒 Files selected for processing (11)
  • core/embed/extmod/modtrezorio/modtrezorio-poll.h (1 hunks)
  • core/embed/fp_sensor_wrapper/fingerprint.c (3 hunks)
  • core/embed/fp_sensor_wrapper/fingerprint.h (1 hunks)
  • core/embed/fp_sensor_wrapper/fpsensor_common.c (1 hunks)
  • core/embed/trezorhal/camera.c (4 hunks)
  • core/embed/trezorhal/camera.h (2 hunks)
  • core/embed/trezorhal/camera_qrcode.c (1 hunks)
  • core/embed/trezorhal/device.c (7 hunks)
  • core/embed/trezorhal/usb_webusb-defs.h (1 hunks)
  • core/embed/trezorhal/usb_webusb-impl.h (2 hunks)
  • core/src/trezor/wire/codec_v1.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Gen check
  • GitHub Check: Defs check
  • GitHub Check: Style check
🔇 Additional comments (11)
core/embed/trezorhal/usb_webusb-defs.h (1)

65-65: LGTM!

The return type change from secbool to int enables better error handling.

core/embed/extmod/modtrezorio/modtrezorio-poll.h (1)

251-262: LGTM!

The changes properly handle USB write timeouts and error conditions.

core/src/trezor/wire/codec_v1.py (1)

106-108: LGTM!

The added timeout error handling improves robustness.

core/embed/trezorhal/usb_webusb-impl.h (1)

293-294: LGTM!

The EP buffer flush improves reliability of subsequent transfers.

core/embed/fp_sensor_wrapper/fingerprint.h (1)

15-15: LGTM!

The new function declaration follows standard C conventions.

core/embed/trezorhal/camera.h (1)

5-5: LGTM! Function signatures improved for type safety.

The changes from numeric types to bool improve clarity and type safety.

Also applies to: 34-35

core/embed/trezorhal/device.c (4)

471-471: Test duration reduced from 3 to 2 hours.


553-555: LGTM! Added camera status check.


639-641: LGTM! Added fingerprint module status check.


862-869: LGTM! Added component validation before burn-in test.

core/embed/fp_sensor_wrapper/fpsensor_common.c (1)

31-40: LGTM! Improved error handling in HWID retrieval.

The changes properly validate SPI transaction status before using the data.

core/embed/trezorhal/usb_webusb-impl.h Show resolved Hide resolved
core/embed/trezorhal/usb_webusb-impl.h Show resolved Hide resolved
core/embed/trezorhal/camera_qrcode.c Outdated Show resolved Hide resolved
core/embed/fp_sensor_wrapper/fingerprint.c Show resolved Hide resolved
core/embed/fp_sensor_wrapper/fingerprint.c Show resolved Hide resolved
core/embed/trezorhal/device.c Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

♻️ Duplicate comments (2)
core/embed/trezorhal/device.c (1)

1009-1039: ⚠️ Potential issue

Add timeout to NFC polling loop.

The while loop could run indefinitely if no card is present.

 if (loop_counter % 2) {
   if (flashled_value) {
     ble_set_flashled(0);
   }
   display_printf("Poll card...\n");
   nfc_pwr_ctl(true);
   HAL_TIM_Base_Stop(&TimHandle);
+  uint32_t start_time = HAL_GetTick();
   while (1) {
+    if (HAL_GetTick() - start_time > 5000) {
+      display_printf("Card test timeout\n");
+      break;
+    }
     if (nfc_poll_card()) {
core/embed/trezorhal/usb_webusb-impl.h (1)

113-113: ⚠️ Potential issue

Static timer causes race conditions.

Move timer to interface struct.

🧹 Nitpick comments (2)
core/embed/trezorhal/camera.c (1)

310-316: Use a defined constant for error code.

Replace magic number -1 with a defined constant.

+#define CAMERA_ERROR_OFFLINE -1
+
 int camera_init(void)
 {
     i2c_handle_camera = &i2c_handles[i2c_find_channel_by_device(I2C_CAMERA)];
     i2c_init_by_device(I2C_CAMERA);
     if (!camera_is_online())
     {
-        return -1;
+        return CAMERA_ERROR_OFFLINE;
     }
core/embed/trezorhal/device.c (1)

1105-1114: Define constants for timing values.

Replace magic numbers with named constants.

+#define FLASHLED_ON_DURATION_MS 500
+#define FLASHLED_OFF_DURATION_MS 20000
+
-      if (flashled_now - flashled_pre > 500) {
+      if (flashled_now - flashled_pre > FLASHLED_ON_DURATION_MS) {
         flashled_pre = flashled_now;
         flashled_value = 0;
         ble_set_flashled(flashled_value);
       }
     } else {
-      if (flashled_now - flashled_pre > 20000) {
+      if (flashled_now - flashled_pre > FLASHLED_OFF_DURATION_MS) {
         flashled_pre = flashled_now;
         flashled_value = 1;
         ble_set_flashled(flashled_value);
       }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fa82382 and 23fb346.

📒 Files selected for processing (11)
  • core/embed/extmod/modtrezorio/modtrezorio-poll.h (1 hunks)
  • core/embed/fp_sensor_wrapper/fingerprint.c (3 hunks)
  • core/embed/fp_sensor_wrapper/fingerprint.h (1 hunks)
  • core/embed/fp_sensor_wrapper/fpsensor_common.c (1 hunks)
  • core/embed/trezorhal/camera.c (4 hunks)
  • core/embed/trezorhal/camera.h (2 hunks)
  • core/embed/trezorhal/camera_qrcode.c (1 hunks)
  • core/embed/trezorhal/device.c (7 hunks)
  • core/embed/trezorhal/usb_webusb-defs.h (1 hunks)
  • core/embed/trezorhal/usb_webusb-impl.h (2 hunks)
  • core/src/trezor/wire/codec_v1.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (7)
  • core/embed/fp_sensor_wrapper/fingerprint.h
  • core/src/trezor/wire/codec_v1.py
  • core/embed/trezorhal/camera_qrcode.c
  • core/embed/fp_sensor_wrapper/fpsensor_common.c
  • core/embed/trezorhal/usb_webusb-defs.h
  • core/embed/fp_sensor_wrapper/fingerprint.c
  • core/embed/trezorhal/camera.h
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Gen check
  • GitHub Check: Defs check
  • GitHub Check: Style check
🔇 Additional comments (6)
core/embed/trezorhal/camera.c (3)

182-184: LGTM!


Line range hint 201-220: LGTM!


223-235: LGTM!

core/embed/trezorhal/device.c (2)

471-471: LGTM!


639-641: LGTM!

core/embed/trezorhal/usb_webusb-impl.h (1)

131-131: Magic number needs constant.

Define 500 as USB_WRITE_TIMEOUT_MS.

core/embed/trezorhal/usb_webusb-impl.h Show resolved Hide resolved
@lihuanhuan lihuanhuan merged commit 142a6cc into OneKeyHQ:main Jan 15, 2025
7 checks passed
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

Successfully merging this pull request may close these issues.

3 participants