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

Open
wants to merge 42 commits into
base: main
Choose a base branch
from
Open

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
@@ -19,6 +19,7 @@ public SafeChipHandle()
protected override bool ReleaseHandle()
{
LibgpiodV1.gpiod_chip_close(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.

Where is this handle being set to something other than IntPtr.Zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rewritten to make this more clear. Using a handle as return type to a pinvoke call sets the value implicitly.

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 WaitForEventResult WaitForEvent(int pinNumber, PinEventTypes eventTypes, CancellationToken cancellationToken)
[Experimental(DiagnosticIds.SDGPIO0001, UrlFormat = DiagnosticIds.UrlFormat)] // TODO: necessary?
Copy link
Member

Choose a reason for hiding this comment

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

Yes, let's keep the diagnostic for now :)

/// <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

/// 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.

did we lock on the names here? We should be consistent

@@ -3,6 +3,7 @@

using System.Device.Gpio.Drivers;
using System.Diagnostics;
using System.IO;
Copy link
Member

Choose a reason for hiding this comment

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

Ensure GpioChipInfo is part of QueryComponentInformation

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