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

Make LibGpiodDriver V2 driver public #2386

Merged
merged 44 commits into from
Apr 3, 2025
Merged

Conversation

pgrawehr
Copy link
Contributor

@pgrawehr pgrawehr commented Mar 1, 2025

Fixes: #2382
Fixes: #2366
Fixes: #2384

@krwq krwq changed the title Provide Chip info for most of our drivers Make LibGpiodDriver V2 driver public Mar 6, 2025
@krwq krwq added the Priority:0 Work that we can't release without label Mar 6, 2025
@krwq krwq added this to the v4.0.0 milestone Mar 6, 2025
LibgpiodV1.gpiod_chip_iter_free(handle);
// We can't close the chip here, as this would possibly result in it being freed twice, which causes a crash
LibgpiodV1.gpiod_chip_iter_free_noclose(handle);
handle = IntPtr.Zero;
Copy link
Member

Choose a reason for hiding this comment

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

public LibGpiodDriver(int gpioChip, LibGpiodDriverVersion driverVersion)
private static bool s_isLibgpiodVersion1_5orHigher = IsLibgpiodVersion1_5orHigher();

private enum RequestFlag : ulong
Copy link
Member

Choose a reason for hiding this comment

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

Should this be in the interop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's only used here. And it's private, so no change necessary.

/// <inheritdoc/>
protected internal override int PinCount => GetDriver().PinCount;
int numLines = LibgpiodV1.gpiod_chip_num_lines(chip);
string name = Marshal.PtrToStringAnsi(LibgpiodV1.gpiod_chip_name(chip)) ?? string.Empty;
Copy link
Member

Choose a reason for hiding this comment

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

Try using string marshalling attribute for that

Copy link
Member

Choose a reason for hiding this comment

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

(and for one below)

/// Returns the list of available chips.
/// </summary>
/// <returns>A list of available chips. Can be used to determine the chipNumber when calling the constructor</returns>
public static IList<GpioChipInfo> GetAvailableChips()
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need all of this API public? All of this sounds like implementation details that I'm n ot sure we have a valid use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you need these. There might be a reason to select a non-default chip for various reasons. Also, we now know that our automatic selection of the default chip can fail when the hardware/operating system changes, so this allows the user to make it work correctly for his board, regardless of our possibly flaky detection algorithm.

/// This can be used to determine the correct gpio chip for constructor calls to <see cref="LibGpiodDriver"/>
/// </summary>
/// <returns>A list of chips detected</returns>
public static IList<GpioChipInfo> GetAvailableChips()
Copy link
Member

Choose a reason for hiding this comment

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

Can you mark this as Experimental? Do we want 2 entries even though there is 1 chip?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As seen in the issue #2366 gpiodetect does the same and also (sometimes) reports the same chip twice. So I mimiked the behavior of the LibGpiod driver here.

@@ -398,14 +447,20 @@ public override ComponentInformation QueryComponentInformation()
return self;
}

/// <inheritdoc />
public override GpioChipInfo GetChipInfo()
Copy link
Member

Choose a reason for hiding this comment

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

You called this MakeSnapshot in one place but GetChipInfo here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MakeSnapShot is part of the internal implementation of the V2 driver and has a different purpose. It's never exposed.

@@ -32,18 +34,39 @@ public static UnixDriver Create()
}

UnixDriver? driver = null;

if (TryCreate(() => new LibGpiodDriver(0), out driver))
Copy link
Member

Choose a reason for hiding this comment

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

do we need to pass in 0 here? shouldn't this be () and below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should consider moving this logic to some other place. We shouldn't have detection logic all over the place that works differently each time.

I would consider obsoleting this, but we can discuss.

@@ -628,7 +627,7 @@ private void InitializeInterruptDriver()
{
try
{
_interruptDriver = new LibGpiodV1Driver(0);
_interruptDriver = new LibGpiodDriver(0);
Copy link
Member

Choose a reason for hiding this comment

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

should this be ()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's more clear to keep it explicit here. This is for RPI3 and 4 only.

@pgrawehr
Copy link
Contributor Author

/azp run dotnet.iot

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@joperezr
Copy link
Member

joperezr commented Apr 1, 2025

/azp run dotnet.iot

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@joperezr joperezr enabled auto-merge (squash) April 3, 2025 15:55
@joperezr joperezr merged commit 1190b6f into dotnet:main Apr 3, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority:0 Work that we can't release without
Projects
None yet
4 participants