Skip to content

Commit 79224fb

Browse files
authored
Update API (#3)
1 parent 803e65f commit 79224fb

11 files changed

+172
-13
lines changed

System.Device.UsbClient.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "UsbClient", "UsbClient\UsbC
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{345F1C0E-07DF-44E0-AA02-BC00158649E9}"
1111
ProjectSection(SolutionItems) = preProject
12+
nanoFramework.System.Device.UsbStream.nuspec = nanoFramework.System.Device.UsbStream.nuspec
1213
README.md = README.md
1314
version.json = version.json
1415
EndProjectSection

System.Device.UsbStream/System.Device.UsbStream.nfproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<Reference Include="mscorlib">
5252
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
5353
</Reference>
54+
<Reference Include="nanoFramework.Runtime.Events">
55+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.11.1\lib\nanoFramework.Runtime.Events.dll</HintPath>
56+
</Reference>
5457
<Reference Include="nanoFramework.System.Text">
5558
<HintPath>..\packages\nanoFramework.System.Text.1.2.7\lib\nanoFramework.System.Text.dll</HintPath>
5659
</Reference>

System.Device.UsbStream/UsbStream.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation and Contributors
22
// See LICENSE file in the project root for full license information.
33

4-
using System;
5-
using System.Collections;
64
using System.IO;
75
using System.Runtime.CompilerServices;
86

@@ -68,41 +66,58 @@ protected override void Dispose(bool disposing)
6866

6967
/// <inheritdoc/>
7068
/// <exception cref="ObjectDisposedException">This <see cref="UsbStream"/> has been disposed.</exception>
71-
public override int Read(byte[] buffer, int offset, int count)
69+
/// <exception cref="InvalidOperationException">If the USB device is not connected.</exception>
70+
/// <remarks>Device connectivity can be checked with </remarks>
71+
public override int Read(
72+
byte[] buffer,
73+
int offset,
74+
int count)
7275
{
7376
if (_disposed)
7477
{
7578
throw new ObjectDisposedException();
7679
}
7780

78-
return NativeRead(buffer, offset, count);
81+
return NativeRead(
82+
buffer,
83+
offset,
84+
count);
7985
}
8086

8187
/// <inheritdoc/>
8288
/// <exception cref="NotImplementedException"></exception>
89+
/// <exception cref="InvalidOperationException">If the USB device is not connected.</exception>
8390
public override int Read(SpanByte buffer)
8491
{
85-
return Read(buffer.ToArray(), 0, buffer.Length);
92+
return Read(
93+
buffer.ToArray(),
94+
0,
95+
buffer.Length);
8696
}
8797

8898
/// <inheritdoc/>
8999
/// <exception cref="PlatformNotSupportedException">This is not support in .NET nanoFramework.</exception>
90-
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
100+
public override long Seek(
101+
long offset,
102+
SeekOrigin origin) => throw new NotSupportedException();
91103

92104
/// <inheritdoc/>
93105
/// <exception cref="PlatformNotSupportedException">This is not support in .NET nanoFramework.</exception>
94106
public override void SetLength(long value) => throw new PlatformNotSupportedException();
95107

96108
/// <inheritdoc/>
97109
/// <exception cref="ObjectDisposedException">This <see cref="UsbStream"/> has been disposed.</exception>
98-
public override void Write(byte[] buffer, int offset, int count)
110+
/// <exception cref="InvalidOperationException">If the USB device is not connected.</exception>
111+
public override void Write(
112+
byte[] buffer,
113+
int offset,
114+
int count)
99115
{
100-
if (_disposed)
101-
{
102-
throw new ObjectDisposedException();
103-
}
104-
105-
NativeWrite(buffer, offset, count);
116+
// developer note: check for "disposed" it's carried out at native code
117+
NativeWrite(
118+
buffer,
119+
offset,
120+
count);
106121
}
107122

108123
#region Native Methods

System.Device.UsbStream/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.Runtime.Events" version="1.11.1" targetFramework="netnano1.0" />
45
<package id="nanoFramework.System.IO.Streams" version="1.1.15" targetFramework="netnano1.0" />
56
<package id="nanoFramework.System.Text" version="1.2.7" targetFramework="netnano1.0" />
67
<package id="Nerdbank.GitVersioning" version="3.5.113" targetFramework="netnano1.0" developmentDependency="true" />

System.Device.UsbStream/packages.lock.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"resolved": "1.12.0",
99
"contentHash": "qQrFNXmJiStMC4VXk5cVMOJp23/qlT9FW5i9i+igwQVwraQTtvpkam8yK1hj992jqrbjoCIFZP4Hw9E8H0pB7w=="
1010
},
11+
"nanoFramework.Runtime.Events": {
12+
"type": "Direct",
13+
"requested": "[1.11.1, 1.11.1]",
14+
"resolved": "1.11.1",
15+
"contentHash": "hHRPhNagq1T0oN9QJfPOqreuWUV42DJamT0f7GrPQvrOYcr9ne4YTZq8MIpudvKtSALs50iYqxJ+raOF2CiNsA=="
16+
},
1117
"nanoFramework.System.IO.Streams": {
1218
"type": "Direct",
1319
"requested": "[1.1.15, 1.1.15]",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
namespace System.Device.UsbClient
5+
{
6+
/// <summary>
7+
/// Contains argument values for USB device connection events.
8+
/// </summary>
9+
public class DeviceConnectionEventArgs : EventArgs
10+
{
11+
private readonly bool _isConnected;
12+
13+
/// <summary>
14+
/// Gets a value indicating whether the USB device is connected or not.
15+
/// </summary>
16+
public bool IsConnected => _isConnected;
17+
18+
internal DeviceConnectionEventArgs(bool isConnected) => _isConnected = isConnected;
19+
}
20+
}

UsbClient/UsbClient.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
// Copyright (c) .NET Foundation and Contributors
22
// See LICENSE file in the project root for full license information.
33

4+
using System.Runtime.CompilerServices;
5+
using nanoFramework.Runtime.Events;
6+
47
namespace System.Device.UsbClient
58
{
9+
/// <summary>
10+
/// Indicates a change in the connection state of the USB device.
11+
/// </summary>
12+
/// <param name="sender">Specifies the object that sent the USB device connection state changed event. </param>
13+
/// <param name="e">Contains the connection changed event arguments. </param>
14+
public delegate void UsbDeviceConnectionChangedEventHandler(object sender, DeviceConnectionEventArgs e);
15+
616
/// <summary>
717
/// Provides static methods for the creation of USB client instances.
818
/// </summary>
919
public class UsbClient
1020
{
21+
/// <summary>
22+
/// Gets a value indicating whether the USB device is connected or not.
23+
/// </summary>
24+
public extern bool IsConnected
25+
{
26+
[MethodImpl(MethodImplOptions.InternalCall)]
27+
get;
28+
}
29+
30+
static UsbClient()
31+
{
32+
UsbDeviceEventListener useDeviceEventListener = new ();
33+
34+
EventSink.AddEventProcessor(EventCategory.Usb, useDeviceEventListener);
35+
EventSink.AddEventListener(EventCategory.Usb, useDeviceEventListener);
36+
}
37+
38+
/// <summary>
39+
/// Event occurs when the connection state of the USB device changes.
40+
/// </summary>
41+
public static event UsbDeviceConnectionChangedEventHandler UsbDeviceConnectionChanged;
42+
1143
/// <summary>
1244
/// Creates an USB Stream from a WinUSB device that will use the specified name as the device description.
1345
/// </summary>
@@ -22,5 +54,50 @@ public static UsbStream CreateUsbStream(
2254
classId,
2355
name);
2456
}
57+
58+
internal class UsbDeviceEventListener : IEventProcessor, IEventListener
59+
{
60+
public BaseEvent ProcessEvent(uint data1, uint data2, DateTime time)
61+
{
62+
return new UsbDeviceEvent()
63+
{
64+
// Data1 is packed by PostManagedEvent, so we need to unpack the high word.
65+
EventType = (UsbEventType)(data1 & 0xFF),
66+
67+
// Data2 - Low 8 bits are the interface index
68+
InterfaceIndex = (ushort)(data2 & 0xff)
69+
};
70+
}
71+
72+
public void InitializeForEventSource()
73+
{
74+
// This method has to exist
75+
}
76+
77+
public bool OnEvent(BaseEvent ev)
78+
{
79+
if (ev is UsbDeviceEvent myEvent)
80+
{
81+
switch (myEvent.EventType)
82+
{
83+
case UsbEventType.DeviceDisconnected:
84+
85+
// fire event, if subscribed
86+
UsbDeviceConnectionChanged?.Invoke(this, new DeviceConnectionEventArgs(false));
87+
88+
break;
89+
90+
case UsbEventType.DeviceConnected:
91+
92+
// fire event, if subscribed
93+
UsbDeviceConnectionChanged?.Invoke(this, new DeviceConnectionEventArgs(true));
94+
95+
break;
96+
}
97+
}
98+
99+
return true;
100+
}
101+
}
25102
}
26103
}

UsbClient/UsbClient.projitems

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<Import_RootNamespace>System.Device.UsbClient</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)UsbDeviceEvent.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)DeviceConnectionEventArgs.cs" />
1214
<Compile Include="$(MSBuildThisFileDirectory)UsbClient.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)UsbEventType.cs" />
1316
</ItemGroup>
1417
</Project>

UsbClient/UsbDeviceEvent.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
using nanoFramework.Runtime.Events;
5+
6+
namespace System.Device.UsbClient
7+
{
8+
internal class UsbDeviceEvent : BaseEvent
9+
{
10+
private UsbEventType _eventType;
11+
private ushort _eventData;
12+
private ushort _interfaceIndex;
13+
14+
public UsbEventType EventType { get => _eventType; set => _eventType = value; }
15+
16+
public ushort EventData { get => _eventData; set => _eventData = value; }
17+
18+
public ushort InterfaceIndex { get => _interfaceIndex; set => _interfaceIndex = value; }
19+
}
20+
}

UsbClient/UsbEventType.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
namespace System.Device.UsbClient
5+
{
6+
internal enum UsbEventType : byte
7+
{
8+
Invalid = 0,
9+
DeviceConnected = 1,
10+
DeviceDisconnected = 2
11+
}
12+
}

0 commit comments

Comments
 (0)