-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathLibGpiodV1DriverTests.cs
135 lines (116 loc) · 4.6 KB
/
LibGpiodV1DriverTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Device.Gpio.Drivers;
using System.Diagnostics;
using System.Threading;
using Xunit;
using Xunit.Abstractions;
namespace System.Device.Gpio.Tests;
#pragma warning disable SDGPIO0001
[Trait("feature", "gpio")]
[Trait("feature", "gpio-libgpiod")]
[Trait("SkipOnTestRun", "Windows_NT")]
public class LibGpiodV1DriverTests : GpioControllerTestBase
{
public LibGpiodV1DriverTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}
protected override GpioDriver GetTestDriver() => new LibGpiodDriver(0);
[Fact]
public void SetPinModeSetsDefaultValue()
{
using (GpioController controller = new GpioController(GetTestDriver()))
{
int testPin = OutputPin;
// Set value to low prior to test, so that we have a defined start situation
controller.OpenPin(testPin, PinMode.Output);
controller.Write(testPin, PinValue.Low);
controller.ClosePin(testPin);
// For this test, we use the input pin as an external pull-up
controller.OpenPin(InputPin, PinMode.Output);
controller.Write(InputPin, PinValue.High);
Thread.Sleep(2);
controller.OpenPin(testPin, PinMode.Input);
Thread.Sleep(50);
// It's not possible to change the direction while listening to events (causes an error). Therefore the real behavior of the driver
// can only be tested with a scope (or if we had a third pin connected in the lab hardware)
// We do another test here and make sure the
// pin is really high now
controller.Write(testPin, PinValue.High);
controller.SetPinMode(testPin, PinMode.Output);
controller.SetPinMode(InputPin, PinMode.Input);
Assert.True(controller.Read(InputPin) == PinValue.High);
controller.ClosePin(OutputPin);
controller.ClosePin(InputPin);
}
}
[Fact]
public void UnregisterPinValueChangedShallNotThrow()
{
using var gc = new GpioController(GetTestDriver());
gc.OpenPin(InputPin, PinMode.Input);
static void PinChanged(object sender, PinValueChangedEventArgs args)
{
}
for (var i = 0; i < 1000; i++)
{
gc.RegisterCallbackForPinValueChangedEvent(InputPin, PinEventTypes.Rising | PinEventTypes.Falling, PinChanged);
gc.UnregisterCallbackForPinValueChangedEvent(InputPin, PinChanged);
}
}
/// <summary>
/// Ensure leaking instances of the driver doesn't cause a segfault
/// Regression test for https://github.com/dotnet/iot/issues/1849
/// </summary>
[Fact]
public void LeakingDriverDoesNotCrash()
{
GpioController controller1 = new GpioController(new LibGpiodDriver());
controller1.OpenPin(10, PinMode.Output);
GpioController controller2 = new GpioController(new LibGpiodDriver());
controller2.OpenPin(11, PinMode.Output);
GpioController controller3 = new GpioController(new LibGpiodDriver());
controller3.OpenPin(12, PinMode.Output);
GpioController controller4 = new GpioController(new LibGpiodDriver());
controller4.OpenPin(13, PinMode.Output);
GpioController controller5 = new GpioController(new LibGpiodDriver());
controller5.OpenPin(14, PinMode.Output);
for (int i = 0; i < 10; i++)
{
GC.Collect();
GpioController controller6 = new GpioController(new LibGpiodDriver());
controller6.OpenPin(15, PinMode.Output);
controller6.ClosePin(15);
controller6.Dispose();
GC.Collect();
Thread.Sleep(20);
}
GC.WaitForPendingFinalizers();
}
[Fact]
public void CheckAllChipsCanBeConstructed()
{
var chips = LibGpiodDriver.GetAvailableChips();
foreach (var c in chips)
{
Logger.WriteLine(c.ToString());
}
Assert.NotEmpty(chips);
if (IsRaspi4())
{
// 2 entries. Here they're always named 0 and 1
Assert.Equal(2, chips.Count);
}
foreach (var chip in chips)
{
var driver = new LibGpiodDriver(chip.Id);
var ctrl = new GpioController(driver);
Assert.NotNull(ctrl);
var driverInfo = driver.GetChipInfo();
Assert.NotNull(driverInfo);
Assert.Equal(chip, driverInfo);
ctrl.Dispose();
}
}
}