Skip to content

type-c-service: add power swap functionality and testing#917

Merged
tullom merged 2 commits into
OpenDevicePartnership:mainfrom
tullom:type-c-service/add-power-swap-functionality-and-testing
Jul 8, 2026
Merged

type-c-service: add power swap functionality and testing#917
tullom merged 2 commits into
OpenDevicePartnership:mainfrom
tullom:type-c-service/add-power-swap-functionality-and-testing

Conversation

@tullom

@tullom tullom commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Handle USB-PD power role swaps in the Type-C port and add test coverage.

Previously the port never tore down the old role on a swap, so the power policy rejected the new-role connection. Now process_power_role_swap runs on a power_swap_completed event:

  • disable the sink path if consuming
  • reset psu_state to Idle, and
  • notify the power policy of the disconnect.

The next contract event connects the new role.

Tests cover both swap directions (plug + contract -> power_swap_completed -> new contract), asserting the psu_state transitions and the disconnect -> connect power-policy broadcasts.

Assisted-by: GitHub Copilot:claude-opus-4.8

Resolves #831

@tullom tullom self-assigned this Jul 8, 2026
Copilot AI review requested due to automatic review settings July 8, 2026 21:38
@tullom tullom requested a review from a team as a code owner July 8, 2026 21:38
@tullom tullom added the enhancement New feature or request label Jul 8, 2026
@tullom tullom requested review from asasine and gjpmsft July 8, 2026 21:38
@tullom tullom requested review from RobertZ2011 and kurtjd July 8, 2026 21:38
@tullom tullom force-pushed the type-c-service/add-power-swap-functionality-and-testing branch from 6428009 to 8ecf60a Compare July 8, 2026 21:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Summary of changes

This PR adds explicit handling for USB-PD power role swaps in the Type-C controller so that the previous power role is torn down before the new-role contract is established. On a power_swap_completed status event, the port now transitions its local PSU tracking back to Idle, disables the sink path when swapping away from a consumer role, and notifies the power policy of a disconnect so it can accept the subsequent connect in the new role. The PR also adds integration-style tests to validate both swap directions and to ensure the power policy observes a disconnect -> connect sequence across the swap. This directly addresses the reported issue where the power policy rejected the new-role connection because the old role was never cleared.

Changes:

  • Add process_power_role_swap() to tear down the prior consumer/provider contract on power_swap_completed before the next contract connects.
  • Wire role-swap handling into the port status-changed processing pipeline.
  • Add integration tests for consumer→provider and provider→consumer swaps, asserting expected PSU state transitions and power-policy broadcasts.

Step-by-step review guide

  1. Role swap teardown in the controller

    • process_power_role_swap() is invoked on power_swap_completed and only acts if the port remains connected and the local PSU state indicates an active connected role.
    • Key behavior: when swapping away from consumer, it calls enable_sink_path(..., false) to safely stop consumption; then it resets the local psu_state back to Idle (clearing caps) and emits power_policy_interface::psu::event::EventData::Disconnected(...) so the power policy stops tracking the old role.
  2. Event ordering in process_port_status_changed

    • The swap teardown is intentionally processed before any “new contract” handling (sink_ready consumer contract path, and provider contract change detection). This ordering is what allows the subsequent contract event to be accepted as a fresh connect in the new role.
  3. Test coverage for both swap directions

    • The new tests model: initial plug + contract → power_swap_completed → new contract, and assert:
      • the intermediate psu_state becomes Idle after swap completion, and
      • the power policy broadcasts *Disconnected followed by *Connected for the new role.
    • Non-obvious detail: the tests are integration-style (they run the Type-C service task + power policy task concurrently), so correct behavior depends on both the controller emitting the right PSU events and the power-policy service interpreting them as intended.

Potential issues

# Severity File Description Code
1 Medium type-c-service/tests/power.rs:611-613 The consumer→provider role-swap test doesn’t assert that the sink path was actually disabled during the swap. Because the harness doesn’t verify unused mock expectations, this test can still pass even if process_power_role_swap stops calling enable_sink_path(..., false) (a key behavior described in the PR). assert_eq!(port0.port.lock().await.state().psu_state, PsuState::Idle);

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
type-c-service/src/controller/power.rs Adds process_power_role_swap() to tear down the old role and notify power policy on swap completion.
type-c-service/src/controller/mod.rs Invokes role-swap teardown when power_swap_completed is present in the status event bitfield.
type-c-service/tests/power.rs Adds integration tests for consumer→provider and provider→consumer swap flows (disconnect → connect, PSU state transitions).

Comment thread type-c-service/tests/power.rs
@tullom tullom force-pushed the type-c-service/add-power-swap-functionality-and-testing branch from 8ecf60a to 35c40e2 Compare July 8, 2026 21:46
@tullom tullom requested a review from Copilot July 8, 2026 21:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@tullom tullom merged commit 26dd033 into OpenDevicePartnership:main Jul 8, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type-C: Create type-c-service::controller::Port unit test for power role swap

4 participants