Skip to content

Added DEVPKEY_DsHidMini_RO_IpcSlotIndex for IPC - #433

Merged
nefarius merged 2 commits into
masterfrom
ipc-tuning
Apr 8, 2026
Merged

nefarius merged 2 commits into
masterfrom
ipc-tuning

Conversation

@nefarius

@nefarius nefarius commented Apr 8, 2026 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • Devices now use driver-reported slot indices for stable identification across shared memory and IPC; drivers persistently expose a read-only slot index.
    • Added fallback assignment for older drivers that don't expose a slot index.
  • Bug Fixes

    • Improved handling to avoid duplicate slot assignments and ensure consistent device mapping after removals.
  • Documentation

    • Clarified device index semantics and warned that slot numbers may be non-contiguous after disconnects.

@coderabbitai

coderabbitai Bot commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Added a persistent, exported device property IpcSlotIndexProperty/DEVPKEY_DsHidMini_RO_IpcSlotIndex and updated SDK enumeration to use driver-proported one-based IPC slot indices when available; driver now publishes the slot index during device initialization; README updated to document the semantics and fallback behavior.

Changes

Cohort / File(s) Summary
Device Property Definition
DSHMC/Drivers/DsHidMiniDriver.cs, SDK/Nefarius.DsHidMini.IPC/Models/Drivers/DsHidMiniDriver.cs, include/DsHidMini/dshmguid.h
Added new exported device property key IpcSlotIndexProperty / DEVPKEY_DsHidMini_RO_IpcSlotIndex (property index 6, uint32). BOM/encoding cleanup in managed files.
IPC Device Enumeration Logic
SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs
Refactored RefreshDevices() to prioritize driver-reported one-based IPC slot indices, added TryGetIpcSlotIndex() helper, track used slot keys to avoid collisions, assign remaining devices to lowest available fallback slots, simplified removal handling to call RefreshDevices().
Driver-side Property Publishing
driver/Device.c
After successful slot claim in DsDevice_InitContext(), persistently assign DEVPKEY_DsHidMini_RO_IpcSlotIndex (uint32) with the claimed slot; log and optionally abort initialization if property assignment fails when IPC enabled.
Documentation
SDK/Nefarius.DsHidMini.IPC/README.md
Clarified "device index" as the one-based driver IPC slot, documented reading IpcSlotIndexProperty from each PnPDevice, noted non-contiguous slot assignments after disconnects and fallback behavior for older drivers.

Sequence Diagram

sequenceDiagram
    participant Driver as Kernel Driver
    participant PnP as PnP System
    participant SDK as SDK (DsHidMiniInterop)
    participant DevEnum as Device Enumeration

    Driver->>Driver: DsDevice_InitContext: Claim slot
    Driver->>PnP: Publish IpcSlotIndexProperty with assigned SlotIndex

    SDK->>DevEnum: RefreshDevices(): Enumerate PnPDevices
    DevEnum-->>SDK: Return list of devices

    loop For each discovered device
        SDK->>PnP: Read IpcSlotIndexProperty
        alt Property exists and valid
            PnP-->>SDK: Return one-based slot index
            SDK->>SDK: Store device using slot index (keyed by slot)
        else Property missing/invalid
            SDK->>SDK: Assign to lowest available fallback slot (starting at 1)
        end
    end

    SDK->>SDK: _connectedDevices populated by actual slot indices
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Suggested labels

enhancement, documentation

Poem

🐰 I found a shiny slot, all numbered and bright,
The driver claimed a home by soft morning light,
The SDK hops round, reads the number with glee,
No more guessing where each device should be,
Hooray — every rabbit has a seat by me!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a new device property key for IPC slot indexing across the driver, SDK, and IPC layers.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@driver/Device.c`:
- Around line 483-497: The call to WdfDeviceAssignProperty currently ignores its
return value; change Device.c to capture the NTSTATUS from
WdfDeviceAssignProperty (when assigning DEVPKEY_DsHidMini_RO_IpcSlotIndex with
pDevCtx->SlotIndex), check for failure, and handle it: log the error (include
the NTSTATUS and context) via the driver's logging mechanism and, if IPC is
enabled, propagate the failure out of the initialization path (return a failure
NTSTATUS or abort device init) instead of discarding it so the new property
write does not silently degrade to the old heuristic.

In `@SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs`:
- Around line 266-269: The Single(...) call that looks up the device in
_connectedDevices is a no-op and can throw if the device isn't present or Equals
doesn't match; remove that lookup or replace it with a non-throwing check (e.g.,
use _connectedDevices.Any(kvp => kvp.Value.Equals(device)) or
_connectedDevices.TryGetValue/ContainsKey pattern) so the removal path does not
throw and allows RefreshDevices() to run; update the code around the device
variable and _connectedDevices to perform a safe diagnostic check or omit it
entirely.
- Around line 195-225: The second pass incorrectly assigns fallback slots to
devices that *did* return an explicit IpcSlotIndexProperty but collided, causing
wrong remapping; change the second foreach to only assign a fallback when
TryGetIpcSlotIndex(device) indicates no valid explicit slot (null/invalid), and
skip any device that returned a non-null int (even if that int was already
used). Use TryGetIpcSlotIndex to re-check each device and only run the fallback
allocation logic for devices where the property is missing/invalid; leave
devices that returned an explicit slot untouched so _connectedDevices and
usedKeys remain consistent.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bdbcfb5f-f859-42bf-a53e-8535e33bacef

📥 Commits

Reviewing files that changed from the base of the PR and between 36bfdd8 and 0e065c1.

📒 Files selected for processing (6)
  • DSHMC/Drivers/DsHidMiniDriver.cs
  • SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs
  • SDK/Nefarius.DsHidMini.IPC/Models/Drivers/DsHidMiniDriver.cs
  • SDK/Nefarius.DsHidMini.IPC/README.md
  • driver/Device.c
  • include/DsHidMini/dshmguid.h

Comment thread driver/Device.c Resolved

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs (1)

244-267: Implementation is correct; consider narrowing the exception filter for better diagnostics.

The helper correctly handles missing/invalid properties and validates the one-based slot range. The broad catch (Exception) is intentional for driver compatibility, but you may want to catch more specific exceptions (e.g., Win32Exception, InvalidCastException) and log unexpected ones to aid debugging when issues arise.

💡 Optional: narrower exception handling
         try
         {
             slot = device.GetProperty<uint>(DsHidMiniDriver.IpcSlotIndexProperty);
         }
-        catch (Exception)
+        catch (Exception ex) when (ex is System.ComponentModel.Win32Exception or InvalidCastException)
         {
             // Older driver without the property, or property not yet available
             return null;
         }
+        catch (Exception ex)
+        {
+            // Unexpected error - log and treat as missing
+            System.Diagnostics.Debug.WriteLine($"Unexpected error reading IpcSlotIndexProperty: {ex}");
+            return null;
+        }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs` around lines 244 - 267, The
TryGetIpcSlotIndex method currently uses a broad catch (Exception) around
device.GetProperty<uint>(DsHidMiniDriver.IpcSlotIndexProperty); narrow this by
catching expected exceptions such as InvalidCastException and Win32Exception (or
whatever specific exceptions PnPDevice.GetProperty can throw) and return null
for those, and add a separate catch for unexpected Exception where you log the
exception via your logger before rethrowing or returning null as appropriate;
locate TryGetIpcSlotIndex and the call to device.GetProperty<uint> to implement
the two-tiered catches and logging.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs`:
- Around line 244-267: The TryGetIpcSlotIndex method currently uses a broad
catch (Exception) around
device.GetProperty<uint>(DsHidMiniDriver.IpcSlotIndexProperty); narrow this by
catching expected exceptions such as InvalidCastException and Win32Exception (or
whatever specific exceptions PnPDevice.GetProperty can throw) and return null
for those, and add a separate catch for unexpected Exception where you log the
exception via your logger before rethrowing or returning null as appropriate;
locate TryGetIpcSlotIndex and the call to device.GetProperty<uint> to implement
the two-tiered catches and logging.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ff2ff73f-ca3f-4dcd-8c46-6d1e68b6b5b4

📥 Commits

Reviewing files that changed from the base of the PR and between 0e065c1 and 7122677.

📒 Files selected for processing (2)
  • SDK/Nefarius.DsHidMini.IPC/DsHidMiniInterop.cs
  • driver/Device.c

@nefarius
nefarius merged commit 573d52d into master Apr 8, 2026
@nefarius
nefarius deleted the ipc-tuning branch April 8, 2026 23:23
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.

1 participant