Skip to content

refactor(rog-platform, asusd): simplify sysfs attribute parsing and error handling - #266

Open
scardracs wants to merge 7 commits into
OpenGamingCollective:mainfrom
scardracs:refactor/sysfs-armoury-cleanups
Open

refactor(rog-platform, asusd): simplify sysfs attribute parsing and error handling#266
scardracs wants to merge 7 commits into
OpenGamingCollective:mainfrom
scardracs:refactor/sysfs-armoury-cleanups

Conversation

@scardracs

Copy link
Copy Markdown
Contributor

Description

This PR refactors sysfs reading, attribute parsing, and error handling across rog-platform, asusd, and rog-aura to eliminate redundant boilerplate, unnecessary filesystem stat calls, and nested if let Ok(...) statements.

Rationale & Key Improvements:

  1. Generic Sysfs Parser (read_sysfs_parsed<T>):
    Introduced a shared helper in rog-platform to read, trim, and parse sysfs attributes in a single line. Replaced 4-level deep if let Ok(...) nests in cpu.rs, gpu_pci.rs, and platform.rs with clean functional expressions.

  2. AttrValue Conversion Helpers:
    Added .as_i32() and .as_str() helper methods on AttrValue in rog-platform/src/asus_armoury.rs. Refactored asusd daemon methods (reset_to_default, current_value property getter, and reload) to use type-safe helper methods instead of manual if let AttrValue::Integer(i) pattern matching.

  3. current_value & read_i32 Cleanup:
    Simplified current_value(&self) by using ? for early error propagation and concise match parsing on val.parse::<i32>(). Refactored read_i32 to reuse read_string.

  4. Elimination of Redundant Sysfs exists() Syscalls:
    Removed if !path.exists() checks prior to std::fs::read_to_string in power.rs. read_to_string already returns io::ErrorKind::NotFound if the node is absent, eliminating duplicate stat syscalls.

  5. Improved Error Logging:
    Updated user-defined LED support config parsing in rog-aura/src/aura_detection.rs to match default config handling and log explicit deserialization errors if RON parsing fails.


Tested Hardware & Environment

  • ASUS Laptop Model: G614PR
  • Linux Distribution: Arch Linux
  • Kernel Version: 7.1.5

Verification and testing:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My code follows the style guidelines of this project (cargo fmt --all -- --check)
  • My changes generate no new warnings (cargo clippy --all -- -D warnings/cargo check --all-targets)
  • New and existing unit tests pass locally with my changes (cargo test --all)
  • Cranky with 0 warning (cargo cranky)

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of numeric and text-based hardware attributes.
    • Invalid user LED configurations are now skipped without misleading success messages.
    • Improved CPU, GPU, fan, and battery readings.
    • Added safer fallback behavior when hardware data is unavailable or cannot be parsed.
  • Reliability
    • Standardized hardware data reading for more consistent results and error handling.
    • Preserved mixed hardware configuration values instead of discarding supported entries.
    • Improved handling of missing or unreadable hardware data.

Walkthrough

The PR centralizes sysfs parsing, adds typed AttrValue conversions, and updates Armoury value handling. It also clarifies Aura configuration errors, simplifies battery reads, and changes CPU and GPU missing-value fallbacks.

Changes

Platform parsing and Armoury handling

Layer / File(s) Summary
Armoury value conversion and reads
rog-platform/src/asus_armoury.rs, asusd/src/asus_armoury.rs
AttrValue converts numeric and string inputs. Armoury defaults, cached values, firmware values, current values, and possible-value tokens use these conversions. Tests cover integer, string, mixed, and single-token parsing.
Shared sysfs parsing consumers
rog-platform/src/lib.rs, rog-platform/src/cpu.rs, rog-platform/src/gpu_pci.rs, rog-platform/src/platform.rs
read_sysfs_parsed handles trimmed reads and parsing for CPU, GPU, and fan metrics. CPU and GPU metric functions return -1.0 when no value is available.
Configuration and direct read outcomes
asusd/src/asus_armoury.rs, rog-aura/src/aura_detection.rs, rog-platform/src/power.rs
Attribute reads distinguish successful, absent, and failed values. Invalid Aura configuration entries are skipped and logged as errors. Battery methods use direct file reads.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested labels: asusd, rog-aura, rog-platform

Suggested reviewers: neroreflex, ghoul4500

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description summarizes the refactor, lists the tested environment, and marks all required verification checks as complete.
Title check ✅ Passed The title clearly and concisely describes the main sysfs parsing and error-handling refactor across the affected crates.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot added asusd System Daemon / D-Bus rog-aura Keyboard / Aura RGB rog-platform GPU Switching / Armoury / WMI labels Aug 4, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@rog-platform/src/asus_armoury.rs`:
- Around line 174-181: Update the value parsing in the possible_values handling
to split delimited input into tokens and create AttrValue::EnumInt only when
every token parses successfully as i32; otherwise preserve all original tokens
in AttrValue::EnumStr, including mixed or wholly non-integer values, and avoid
filter_map silently dropping entries.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 423eba2c-852c-4f83-8269-b3ef683cd088

📥 Commits

Reviewing files that changed from the base of the PR and between 4c66527 and 43b4018.

📒 Files selected for processing (8)
  • asusd/src/asus_armoury.rs
  • rog-aura/src/aura_detection.rs
  • rog-platform/src/asus_armoury.rs
  • rog-platform/src/cpu.rs
  • rog-platform/src/gpu_pci.rs
  • rog-platform/src/lib.rs
  • rog-platform/src/platform.rs
  • rog-platform/src/power.rs
💤 Files with no reviewable changes (1)
  • rog-platform/src/power.rs
📜 Review details
🔇 Additional comments (8)
rog-platform/src/asus_armoury.rs (1)

16-18: LGTM!

Also applies to: 38-69, 93-96

asusd/src/asus_armoury.rs (1)

65-65: LGTM!

Also applies to: 283-290, 320-321, 382-383, 401-401, 658-659

rog-platform/src/lib.rs (1)

31-34: LGTM!

rog-platform/src/cpu.rs (1)

9-9: LGTM!

Also applies to: 320-329, 340-342

rog-platform/src/platform.rs (1)

9-9: LGTM!

Also applies to: 336-338

rog-platform/src/gpu_pci.rs (2)

19-19: LGTM!

Also applies to: 462-470, 616-618, 633-641, 668-669


689-694: 🎯 Functional Correctness

No change is required. The only caller passes -1.0 to SystemPageData, whose UI binding displays N/A for values below 0.0. No serialization or policy consumer exists.

			> Likely an incorrect or invalid review comment.
rog-aura/src/aura_detection.rs (1)

136-147: LGTM!

Comment thread rog-platform/src/asus_armoury.rs
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@scardracs
scardracs force-pushed the refactor/sysfs-armoury-cleanups branch from d125693 to 7a5780a Compare August 6, 2026 09:11
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@rog-platform/src/asus_armoury.rs`:
- Around line 594-641: Update test_possible_values_parsing to create a unique
per-invocation temporary directory instead of the fixed
test_possible_values_parsing path, and assert that directory creation, each
possible_values write, and cleanup succeed. Preserve the existing parsing cases
and assertions while preventing stale data from concurrent or interrupted runs.

In `@rog-platform/src/gpu_pci.rs`:
- Around line 636-641: Remove the busy_path.exists() guard in the GPU busy
percentage lookup and call read_sysfs_parsed::<f32>(busy_path) directly,
retaining the existing vendor check and None-handling return behavior.
- Around line 462-470: Update find_slot_power to retain the first error from
reading each slot’s address instead of ignoring it; continue scanning for a
readable matching slot, return the matching power path immediately, and if none
matches return the retained PlatformError::Io before falling back to
PlatformError::NotSupported when no address read failed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 19b0b839-e0e3-4d9d-ad1c-df7b7d9854fa

📥 Commits

Reviewing files that changed from the base of the PR and between 2aa8ab2 and 7a5780a.

📒 Files selected for processing (8)
  • asusd/src/asus_armoury.rs
  • rog-aura/src/aura_detection.rs
  • rog-platform/src/asus_armoury.rs
  • rog-platform/src/cpu.rs
  • rog-platform/src/gpu_pci.rs
  • rog-platform/src/lib.rs
  • rog-platform/src/platform.rs
  • rog-platform/src/power.rs
💤 Files with no reviewable changes (1)
  • rog-platform/src/power.rs
📜 Review details
🔇 Additional comments (15)
rog-platform/src/asus_armoury.rs (1)

16-18: LGTM!

Also applies to: 38-68, 93-96, 170-189

asusd/src/asus_armoury.rs (1)

65-65: LGTM!

Also applies to: 283-290, 320-321, 382-383, 401-401, 658-659

rog-aura/src/aura_detection.rs (1)

136-146: LGTM!

rog-platform/src/lib.rs (1)

31-33: LGTM!

rog-platform/src/cpu.rs (3)

9-9: LGTM!


320-329: LGTM!


340-342: LGTM!

rog-platform/src/platform.rs (2)

9-9: LGTM!


336-338: LGTM!

rog-platform/src/gpu_pci.rs (6)

19-19: LGTM!


616-618: LGTM!


633-635: LGTM!


668-669: LGTM!


689-693: LGTM!


694-694: 🎯 Functional Correctness

No change needed. Both usage values display N/A when they are negative.

Comment thread rog-platform/src/asus_armoury.rs Outdated
Comment thread rog-platform/src/gpu_pci.rs Outdated
Comment thread rog-platform/src/gpu_pci.rs Outdated
@scardracs
scardracs force-pushed the refactor/sysfs-armoury-cleanups branch from 7a5780a to cc0bc4d Compare August 6, 2026 09:46

@Ghoul4500 Ghoul4500 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Commit history needs fixing. Final HEAD is good except some unused code

Comment thread rog-platform/src/gpu_pci.rs
Comment thread rog-platform/src/asus_armoury.rs
Comment thread rog-platform/src/asus_armoury.rs Outdated
Comment thread rog-platform/src/asus_armoury.rs Outdated
Comment thread rog-platform/src/asus_armoury.rs
}

/// Find the PCI hotplug slot power control file for a device.
pub fn find_slot_power(address: &str) -> Result<PathBuf> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is an unused function. drop any commits that bring any change to this. this together with a lot of other code related to GPU will be dropped in main very soon by me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't make any change to it though?

Comment thread rog-platform/src/cpu.rs Outdated
@scardracs
scardracs force-pushed the refactor/sysfs-armoury-cleanups branch from cc0bc4d to 898e3c7 Compare August 6, 2026 19:11
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@asusd/src/asus_armoury.rs`:
- Around line 287-290: Update the available-attribute condition in the relevant
attribute-reading flow to add “current_value” only when the value can be
converted with value.as_i32().is_some(). Preserve the existing error-state reset
and avoid advertising values that the current_value D-Bus property cannot
return.

In `@rog-platform/src/cpu.rs`:
- Line 329: Handle the unavailable-value sentinel returned by get_cpu_temp and
get_cpu_frequency_mhz so the UI does not display -1 °C or -1 MHz, following the
existing GPU metric handling. Apply the corresponding change in
rog-platform/src/cpu.rs at lines 329-329 and 364-364; use
rog-platform/src/gpu_pci.rs lines 684-684 and 703-703 as the reference
implementation, with no direct changes required there.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: db51d111-feee-497b-bd09-492d581cabf8

📥 Commits

Reviewing files that changed from the base of the PR and between d23d0cc and 898e3c7.

📒 Files selected for processing (8)
  • asusd/src/asus_armoury.rs
  • rog-aura/src/aura_detection.rs
  • rog-platform/src/asus_armoury.rs
  • rog-platform/src/cpu.rs
  • rog-platform/src/gpu_pci.rs
  • rog-platform/src/lib.rs
  • rog-platform/src/platform.rs
  • rog-platform/src/power.rs
💤 Files with no reviewable changes (1)
  • rog-platform/src/power.rs
📜 Review details
🔇 Additional comments (7)
rog-platform/src/asus_armoury.rs (1)

15-18: LGTM!

Also applies to: 38-53, 77-77, 150-169, 574-645

asusd/src/asus_armoury.rs (1)

68-68: LGTM!

Also applies to: 327-328, 389-390, 410-410, 668-669

rog-aura/src/aura_detection.rs (1)

136-147: LGTM!

rog-platform/src/lib.rs (1)

31-33: LGTM!

rog-platform/src/cpu.rs (1)

9-9: LGTM!

Also applies to: 320-322, 340-342

rog-platform/src/gpu_pci.rs (1)

19-19: LGTM!

Also applies to: 627-629, 644-651, 677-680, 698-700

rog-platform/src/platform.rs (1)

9-9: LGTM!

Also applies to: 336-338

Comment thread asusd/src/asus_armoury.rs
Comment on lines +287 to 290
Ok(value) if !matches!(value, AttrValue::None) => {
self.logged_read_error.store(false, Ordering::Relaxed);
if matches!(value, AttrValue::Integer(_)) {
attrs.push("current_value".to_string());
}
attrs.push("current_value".to_string());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Do not advertise a property that this D-Bus interface cannot read.

AttrValue::String passes this condition and adds current_value to available_attrs. The current_value property returns i32 only, so it then returns fdo::Error::Failed for the same value.

Only add current_value when value.as_i32().is_some(), or extend the D-Bus API to expose string values.

Proposed fix
-            Ok(value) if !matches!(value, AttrValue::None) => {
+            Ok(value) => {
                 self.logged_read_error.store(false, Ordering::Relaxed);
-                attrs.push("current_value".to_string());
+                if value.as_i32().is_some() {
+                    attrs.push("current_value".to_string());
+                }
             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Ok(value) if !matches!(value, AttrValue::None) => {
self.logged_read_error.store(false, Ordering::Relaxed);
if matches!(value, AttrValue::Integer(_)) {
attrs.push("current_value".to_string());
}
attrs.push("current_value".to_string());
}
Ok(value) => {
self.logged_read_error.store(false, Ordering::Relaxed);
if value.as_i32().is_some() {
attrs.push("current_value".to_string());
}
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@asusd/src/asus_armoury.rs` around lines 287 - 290, Update the
available-attribute condition in the relevant attribute-reading flow to add
“current_value” only when the value can be converted with
value.as_i32().is_some(). Preserve the existing error-state reset and avoid
advertising values that the current_value D-Bus property cannot return.

Comment thread rog-platform/src/cpu.rs
@scardracs
scardracs force-pushed the refactor/sysfs-armoury-cleanups branch from 898e3c7 to 4971164 Compare August 6, 2026 19:16
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
asusd/src/asus_armoury.rs (1)

287-290: ⚠️ Potential issue | 🟠 Major

Do not advertise an unreadable current_value.

AttrValue::String is non-None, so this branch advertises "current_value". The D-Bus property returns i32, while AttrValue::as_i32() returns Some only for AttrValue::Integer in rog-platform/src/asus_armoury.rs at Lines 38-45. A string current value is therefore advertised but the property call returns fdo::Error::Failed. Gate this entry on value.as_i32().is_some() or add a string-valued property. This is the same unresolved finding from the previous review.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@asusd/src/asus_armoury.rs` around lines 287 - 290, Update the current_value
advertisement branch to add "current_value" only when the returned AttrValue can
be converted with value.as_i32().is_some(), rather than merely being non-None.
Preserve the logged_read_error reset, and keep non-integer values from
advertising a property whose D-Bus type is i32.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Duplicate comments:
In `@asusd/src/asus_armoury.rs`:
- Around line 287-290: Update the current_value advertisement branch to add
"current_value" only when the returned AttrValue can be converted with
value.as_i32().is_some(), rather than merely being non-None. Preserve the
logged_read_error reset, and keep non-integer values from advertising a property
whose D-Bus type is i32.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c5ecbebb-8b0e-42a5-b040-44155aa10362

📥 Commits

Reviewing files that changed from the base of the PR and between d23d0cc and 4971164.

📒 Files selected for processing (8)
  • asusd/src/asus_armoury.rs
  • rog-aura/src/aura_detection.rs
  • rog-platform/src/asus_armoury.rs
  • rog-platform/src/cpu.rs
  • rog-platform/src/gpu_pci.rs
  • rog-platform/src/lib.rs
  • rog-platform/src/platform.rs
  • rog-platform/src/power.rs
💤 Files with no reviewable changes (1)
  • rog-platform/src/power.rs
📜 Review details
🔇 Additional comments (8)
rog-platform/src/asus_armoury.rs (1)

15-18: LGTM!

Also applies to: 38-53, 77-77, 150-169, 573-645

asusd/src/asus_armoury.rs (1)

68-68: LGTM!

Also applies to: 291-297, 327-328, 389-390, 410-410, 668-669

rog-aura/src/aura_detection.rs (1)

136-147: LGTM!

rog-platform/src/cpu.rs (2)

327-329: Re-check the CPU unavailable sentinel in downstream consumers.

get_cpu_temp() and get_cpu_frequency_mhz() now return -1.0 when no value is available. The previous review already found that the UI rendered these values as -1 °C and -1 MHz. Verify that current consumers map -1.0 to an unavailable state before merging.

#!/usr/bin/env bash
set -euo pipefail

rg -n -C 6 \
  '\b(get_cpu_temp|get_cpu_frequency_mhz)\s*\(|cpu_temp_val|cpu_freq_mhz' \
  . --glob '*.rs' --glob '*.slint'

Also applies to: 361-364


9-9: LGTM!

Also applies to: 320-321, 340-342

rog-platform/src/lib.rs (1)

31-33: LGTM!

rog-platform/src/gpu_pci.rs (1)

19-19: LGTM!

Also applies to: 627-629, 644-651, 677-684, 698-703

rog-platform/src/platform.rs (1)

9-9: LGTM!

Also applies to: 336-338

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

asusd System Daemon / D-Bus rog-aura Keyboard / Aura RGB rog-platform GPU Switching / Armoury / WMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants