Skip to content

Commit cb648ad

Browse files
authored
Properly set the Chip ID (#2431)
* Properly get the Chip ID * These tests would block forever in an error case * Disposing the controller would always dispose the driver ... thus this would never run successfully in case of a retry * Fix a markdown warning
1 parent fd76721 commit cb648ad

File tree

3 files changed

+238
-227
lines changed

3 files changed

+238
-227
lines changed

src/System.Device.Gpio.Tests/GpioControllerTestBase.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,10 @@ void Callback(object sender, PinValueChangedEventArgs e)
320320
[Fact]
321321
public void AddCallbackRemoveAllCallbackTest()
322322
{
323-
using GpioDriver testDriver = GetTestDriver();
324-
// Skipping the test for now when using the SysFsDriver or the RaspberryPi3Driver given that this test is flaky for those drivers.
325-
// Issue tracking this problem is https://github.com/dotnet/iot/issues/629
326-
if (testDriver is SysFsDriver || testDriver is RaspberryPi3Driver)
327-
{
328-
return;
329-
}
330-
331323
RetryHelper.Execute(() =>
332324
{
333325
int risingEventOccurredCount = 0, fallingEventOccurredCount = 0;
334-
using (GpioController controller = new GpioController(testDriver))
326+
using (GpioController controller = new GpioController(GetTestDriver()))
335327
{
336328
controller.OpenPin(InputPin, PinMode.Input);
337329
controller.OpenPin(OutputPin, PinMode.Output);
@@ -454,6 +446,7 @@ public void WaitForEventRisingEdgeTest()
454446
using (GpioController controller = new GpioController(GetTestDriver()))
455447
{
456448
CancellationTokenSource tokenSource = new CancellationTokenSource();
449+
tokenSource.CancelAfter(TimeSpan.FromSeconds(20));
457450
controller.OpenPin(InputPin, PinMode.Input);
458451
controller.OpenPin(OutputPin, PinMode.Output);
459452
controller.Write(OutputPin, PinValue.Low);
@@ -479,6 +472,7 @@ public void WaitForEventFallingEdgeTest()
479472
using (GpioController controller = new GpioController(GetTestDriver()))
480473
{
481474
CancellationTokenSource tokenSource = new CancellationTokenSource();
475+
tokenSource.CancelAfter(TimeSpan.FromSeconds(30));
482476
controller.OpenPin(InputPin, PinMode.Input);
483477
controller.OpenPin(OutputPin, PinMode.Output);
484478
controller.Write(OutputPin, PinValue.Low);

src/System.Device.Gpio/System/Device/Gpio/Drivers/LibGpiodDriver.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Diagnostics;
1010
using System.Linq;
1111
using System.Diagnostics.CodeAnalysis;
12-
12+
using System.Globalization;
1313
using LibgpiodV1 = Interop.LibgpiodV1;
1414

1515
namespace System.Device.Gpio.Drivers;
@@ -114,7 +114,6 @@ public static IList<GpioChipInfo> GetAvailableChips()
114114
{
115115
List<GpioChipInfo> result = new List<GpioChipInfo>();
116116
var iterator = new SafeChipIteratorHandle(LibgpiodV1.gpiod_chip_iter_new());
117-
int index = 0;
118117
while (true)
119118
{
120119
SafeChipHandle chip = new SafeChipHandle(LibgpiodV1.gpiod_chip_iter_next_noclose(iterator));
@@ -129,8 +128,26 @@ public static IList<GpioChipInfo> GetAvailableChips()
129128
if (!result.Any(x => x.Label == label && x.NumLines == numLines))
130129
{
131130
// The iterator may find duplicates, but we skip them here
132-
result.Add(new GpioChipInfo(index, name, label, numLines));
133-
index++;
131+
// Need to find the number at the end of the name (e.g. 15 in gpiochip15)
132+
int id = 0;
133+
int numberOfDigitsAtEnd = 0;
134+
for (var i = name.Length - 1; i >= 0; i--)
135+
{
136+
if (!char.IsDigit(name[i]))
137+
{
138+
break;
139+
}
140+
141+
numberOfDigitsAtEnd++;
142+
}
143+
144+
string theNumber = name[^numberOfDigitsAtEnd..];
145+
if (!Int32.TryParse(theNumber, CultureInfo.InvariantCulture, out id))
146+
{
147+
id = 0;
148+
}
149+
150+
result.Add(new GpioChipInfo(id, name, label, numLines));
134151
}
135152

136153
chip.Dispose();

0 commit comments

Comments
 (0)