Skip to content

Commit ad6d667

Browse files
authored
Initial version of UsbClient library (#1)
1 parent 62e9f4a commit ad6d667

13 files changed

+514
-3
lines changed

System.Device.UsbClient.sln

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32901.215
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "System.Device.UsbStream", "System.Device.UsbStream\System.Device.UsbStream.nfproj", "{F6EF056C-F577-4ED2-837C-9813EFDF1443}"
7+
EndProject
8+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "UsbClient", "UsbClient\UsbClient.shproj", "{76ACBAB1-8046-4792-8FF7-0C7F007F1DF3}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{345F1C0E-07DF-44E0-AA02-BC00158649E9}"
11+
ProjectSection(SolutionItems) = preProject
12+
README.md = README.md
13+
version.json = version.json
14+
EndProjectSection
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Any CPU = Debug|Any CPU
19+
Release|Any CPU = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
22+
{F6EF056C-F577-4ED2-837C-9813EFDF1443}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{F6EF056C-F577-4ED2-837C-9813EFDF1443}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{F6EF056C-F577-4ED2-837C-9813EFDF1443}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
25+
{F6EF056C-F577-4ED2-837C-9813EFDF1443}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{F6EF056C-F577-4ED2-837C-9813EFDF1443}.Release|Any CPU.Build.0 = Release|Any CPU
27+
{F6EF056C-F577-4ED2-837C-9813EFDF1443}.Release|Any CPU.Deploy.0 = Release|Any CPU
28+
EndGlobalSection
29+
GlobalSection(SolutionProperties) = preSolution
30+
HideSolutionNode = FALSE
31+
EndGlobalSection
32+
GlobalSection(ExtensibilityGlobals) = postSolution
33+
SolutionGuid = {6765A1D6-ACD1-4555-881A-4613D5BBCEEA}
34+
EndGlobalSection
35+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
36+
UsbClient\UsbClient.projitems*{76acbab1-8046-4792-8ff7-0c7f007f1df3}*SharedItemsImports = 13
37+
EndGlobalSection
38+
EndGlobal
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
using System.Reflection;
5+
using System.Runtime.InteropServices;
6+
7+
// General Information about an assembly is controlled through the following
8+
// set of attributes. Change these attribute values to modify the information
9+
// associated with an assembly.
10+
[assembly: AssemblyTitle("System.Device.UsbStream")]
11+
[assembly: AssemblyCompany("nanoFramework Contributors")]
12+
[assembly: AssemblyProduct("System.Device.UsbStream")]
13+
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]
14+
15+
// Setting ComVisible to false makes the types in this assembly not visible
16+
// to COM components. If you need to access a type in this assembly from
17+
// COM, set the ComVisible attribute to true on that type.
18+
[assembly: ComVisible(false)]
19+
20+
/////////////////////////////////////////////////////////////////
21+
// This attribute is mandatory when building Interop libraries //
22+
// update this whenever the native assembly signature changes //
23+
[assembly: AssemblyNativeVersion("1.0.0.0")]
24+
/////////////////////////////////////////////////////////////////
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<StyleCopSettings Version="105">
2+
<Analyzers>
3+
<Analyzer AnalyzerId="StyleCop.CSharp.DocumentationRules">
4+
<Rules>
5+
<Rule Name="FileHeaderMustContainFileName">
6+
<RuleSettings>
7+
<BooleanProperty Name="Enabled">False</BooleanProperty>
8+
</RuleSettings>
9+
</Rule>
10+
<Rule Name="FileHeaderMustHaveValidCompanyText">
11+
<RuleSettings>
12+
<BooleanProperty Name="Enabled">False</BooleanProperty>
13+
</RuleSettings>
14+
</Rule>
15+
<Rule Name="FileHeaderFileNameDocumentationMustMatchTypeName">
16+
<RuleSettings>
17+
<BooleanProperty Name="Enabled">False</BooleanProperty>
18+
</RuleSettings>
19+
</Rule>
20+
<Rule Name="PropertyDocumentationMustHaveValueText">
21+
<RuleSettings>
22+
<BooleanProperty Name="Enabled">True</BooleanProperty>
23+
</RuleSettings>
24+
</Rule>
25+
<Rule Name="DocumentationTextMustBeginWithACapitalLetter">
26+
<RuleSettings>
27+
<BooleanProperty Name="Enabled">True</BooleanProperty>
28+
</RuleSettings>
29+
</Rule>
30+
<Rule Name="DocumentationTextMustEndWithAPeriod">
31+
<RuleSettings>
32+
<BooleanProperty Name="Enabled">True</BooleanProperty>
33+
</RuleSettings>
34+
</Rule>
35+
<Rule Name="FileHeaderMustShowCopyright">
36+
<RuleSettings>
37+
<BooleanProperty Name="Enabled">False</BooleanProperty>
38+
</RuleSettings>
39+
</Rule>
40+
<Rule Name="FileHeaderMustHaveCopyrightText">
41+
<RuleSettings>
42+
<BooleanProperty Name="Enabled">False</BooleanProperty>
43+
</RuleSettings>
44+
</Rule>
45+
<Rule Name="ElementDocumentationMustBeSpelledCorrectly">
46+
<RuleSettings>
47+
<BooleanProperty Name="Enabled">False</BooleanProperty>
48+
</RuleSettings>
49+
</Rule>
50+
<Rule Name="DocumentationTextMustContainWhitespace">
51+
<RuleSettings>
52+
<BooleanProperty Name="Enabled">False</BooleanProperty>
53+
</RuleSettings>
54+
</Rule>
55+
</Rules>
56+
<AnalyzerSettings>
57+
<BooleanProperty Name="IgnorePrivates">True</BooleanProperty>
58+
<BooleanProperty Name="IgnoreInternals">True</BooleanProperty>
59+
</AnalyzerSettings>
60+
</Analyzer>
61+
<Analyzer AnalyzerId="StyleCop.CSharp.ReadabilityRules">
62+
<Rules>
63+
<Rule Name="PrefixLocalCallsWithThis">
64+
<RuleSettings>
65+
<BooleanProperty Name="Enabled">False</BooleanProperty>
66+
</RuleSettings>
67+
</Rule>
68+
<Rule Name="PrefixCallsCorrectly">
69+
<RuleSettings>
70+
<BooleanProperty Name="Enabled">False</BooleanProperty>
71+
</RuleSettings>
72+
</Rule>
73+
</Rules>
74+
<AnalyzerSettings />
75+
</Analyzer>
76+
<Analyzer AnalyzerId="StyleCop.CSharp.OrderingRules">
77+
<Rules>
78+
<Rule Name="UsingDirectivesMustBePlacedWithinNamespace">
79+
<RuleSettings>
80+
<BooleanProperty Name="Enabled">False</BooleanProperty>
81+
</RuleSettings>
82+
</Rule>
83+
<Rule Name="ElementsMustAppearInTheCorrectOrder">
84+
<RuleSettings>
85+
<BooleanProperty Name="Enabled">False</BooleanProperty>
86+
</RuleSettings>
87+
</Rule>
88+
<Rule Name="ElementsMustBeOrderedByAccess">
89+
<RuleSettings>
90+
<BooleanProperty Name="Enabled">False</BooleanProperty>
91+
</RuleSettings>
92+
</Rule>
93+
</Rules>
94+
<AnalyzerSettings />
95+
</Analyzer>
96+
<Analyzer AnalyzerId="StyleCop.CSharp.NamingRules">
97+
<Rules>
98+
<Rule Name="FieldNamesMustNotBeginWithUnderscore">
99+
<RuleSettings>
100+
<BooleanProperty Name="Enabled">False</BooleanProperty>
101+
</RuleSettings>
102+
</Rule>
103+
<Rule Name="FieldNamesMustNotUseHungarianNotation">
104+
<RuleSettings>
105+
<BooleanProperty Name="Enabled">False</BooleanProperty>
106+
</RuleSettings>
107+
</Rule>
108+
</Rules>
109+
<AnalyzerSettings />
110+
</Analyzer>
111+
</Analyzers>
112+
</StyleCopSettings>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.props" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.props')" />
4+
<PropertyGroup Label="Globals">
5+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
6+
</PropertyGroup>
7+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
8+
<PropertyGroup>
9+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
10+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
11+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
12+
<ProjectGuid>f6ef056c-f577-4ed2-837c-9813efdf1443</ProjectGuid>
13+
<OutputType>Library</OutputType>
14+
<AppDesignerFolder>Properties</AppDesignerFolder>
15+
<FileAlignment>512</FileAlignment>
16+
<RootNamespace>System.Device.UsbStream</RootNamespace>
17+
<AssemblyName>System.Device.UsbStream</AssemblyName>
18+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
19+
<NF_IsCoreLibrary>True</NF_IsCoreLibrary>
20+
<DocumentationFile>bin\$(Configuration)\System.Device.UsbStream.xml</DocumentationFile>
21+
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
22+
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
23+
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
24+
</PropertyGroup>
25+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
26+
<PropertyGroup>
27+
<SignAssembly>true</SignAssembly>
28+
</PropertyGroup>
29+
<PropertyGroup>
30+
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
31+
</PropertyGroup>
32+
<PropertyGroup>
33+
<DelaySign>false</DelaySign>
34+
</PropertyGroup>
35+
<PropertyGroup>
36+
<!-- override default options for MetaDataProcessor -->
37+
<NF_GenerateStubsDirectory>bin\$(Configuration)\Stubs</NF_GenerateStubsDirectory>
38+
<NF_GenerateSkeletonProjectName>sys_dev_usbstream_native</NF_GenerateSkeletonProjectName>
39+
<Name>System.Device.UsbStream</Name>
40+
</PropertyGroup>
41+
<ItemGroup>
42+
<NFMDP_PE_ExcludeClassByName Include="ThisAssembly">
43+
<InProject>false</InProject>
44+
</NFMDP_PE_ExcludeClassByName>
45+
</ItemGroup>
46+
<ItemGroup>
47+
<Compile Include="UsbStream.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Reference Include="mscorlib">
52+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
53+
</Reference>
54+
<Reference Include="nanoFramework.System.Text">
55+
<HintPath>..\packages\nanoFramework.System.Text.1.2.7\lib\nanoFramework.System.Text.dll</HintPath>
56+
</Reference>
57+
<Reference Include="System.IO.Streams">
58+
<HintPath>..\packages\nanoFramework.System.IO.Streams.1.1.15\lib\System.IO.Streams.dll</HintPath>
59+
</Reference>
60+
</ItemGroup>
61+
<ItemGroup>
62+
<None Include="packages.config" />
63+
</ItemGroup>
64+
<ItemGroup>
65+
<Content Include="packages.lock.json" />
66+
</ItemGroup>
67+
<Import Project="..\UsbClient\UsbClient.projitems" Label="Shared" />
68+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
69+
<ProjectExtensions>
70+
<ProjectCapabilities>
71+
<ProjectConfigurationsDeclaredAsItems />
72+
</ProjectCapabilities>
73+
</ProjectExtensions>
74+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
75+
<PropertyGroup>
76+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
77+
</PropertyGroup>
78+
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.props'))" />
79+
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets'))" />
80+
</Target>
81+
<Import Project="..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets')" />
82+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
83+
<PropertyGroup>
84+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
85+
</PropertyGroup>
86+
<Error Condition="!Exists('..\packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets'))" />
87+
</Target>
88+
<Import Project="..\packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets" Condition="Exists('..\packages\StyleCop.MSBuild.6.2.0\build\StyleCop.MSBuild.targets')" />
89+
</Project>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections;
6+
using System.IO;
7+
using System.Runtime.CompilerServices;
8+
9+
namespace System.Device.UsbClient
10+
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="UsbStream"/> class.
13+
/// </summary>
14+
public sealed class UsbStream : System.IO.Stream
15+
{
16+
private readonly int _streamIndex;
17+
private bool _disposed;
18+
19+
/// <inheritdoc/>
20+
public override bool CanRead => true;
21+
22+
/// <inheritdoc/>
23+
public override bool CanSeek => false;
24+
25+
/// <inheritdoc/>
26+
public override bool CanWrite => true;
27+
28+
/// <inheritdoc/>
29+
/// <exception cref="PlatformNotSupportedException">This is not support in .NET nanoFramework.</exception>
30+
public override long Length => throw new PlatformNotSupportedException();
31+
32+
/// <inheritdoc/>
33+
/// <exception cref="PlatformNotSupportedException">This is not support in .NET nanoFramework.</exception>
34+
public override long Position { get => throw new PlatformNotSupportedException(); set => throw new PlatformNotSupportedException(); }
35+
36+
internal UsbStream(string name)
37+
{
38+
_streamIndex = NativeOpen(name);
39+
40+
_disposed = false;
41+
}
42+
43+
/// <inheritdoc/>
44+
~UsbStream()
45+
{
46+
Dispose(false);
47+
}
48+
49+
/// <inheritdoc/>
50+
protected override void Dispose(bool disposing)
51+
{
52+
if (!_disposed)
53+
{
54+
NativeClose();
55+
56+
_disposed = true;
57+
}
58+
}
59+
60+
/// <inheritdoc/>
61+
[MethodImpl(MethodImplOptions.InternalCall)]
62+
public extern override void Flush();
63+
64+
/// <inheritdoc/>
65+
/// <exception cref="ObjectDisposedException">This <see cref="UsbStream"/> has been disposed.</exception>
66+
public override int Read(byte[] buffer, int offset, int count)
67+
{
68+
if (_disposed)
69+
{
70+
throw new ObjectDisposedException();
71+
}
72+
73+
return NativeRead(buffer, offset, count);
74+
}
75+
76+
/// <inheritdoc/>
77+
/// <exception cref="NotImplementedException"></exception>
78+
public override int Read(SpanByte buffer)
79+
{
80+
return Read(buffer.ToArray(), 0, buffer.Length);
81+
}
82+
83+
/// <inheritdoc/>
84+
/// <exception cref="PlatformNotSupportedException">This is not support in .NET nanoFramework.</exception>
85+
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
86+
87+
/// <inheritdoc/>
88+
/// <exception cref="PlatformNotSupportedException">This is not support in .NET nanoFramework.</exception>
89+
public override void SetLength(long value) => throw new PlatformNotSupportedException();
90+
91+
/// <inheritdoc/>
92+
/// <exception cref="ObjectDisposedException">This <see cref="UsbStream"/> has been disposed.</exception>
93+
public override void Write(byte[] buffer, int offset, int count)
94+
{
95+
if (_disposed)
96+
{
97+
throw new ObjectDisposedException();
98+
}
99+
100+
NativeWrite(buffer, offset, count);
101+
}
102+
103+
#region Native Methods
104+
105+
[MethodImpl(MethodImplOptions.InternalCall)]
106+
private extern void NativeClose();
107+
108+
[MethodImpl(MethodImplOptions.InternalCall)]
109+
private extern int NativeOpen(string name);
110+
111+
[MethodImpl(MethodImplOptions.InternalCall)]
112+
private extern void NativeWrite(byte[] buffer, int offset, int count);
113+
114+
[MethodImpl(MethodImplOptions.InternalCall)]
115+
private extern int NativeRead(byte[] buffer, int offset, int count);
116+
117+
#endregion
118+
}
119+
}

System.Device.UsbStream/key.snk

596 Bytes
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.System.IO.Streams" version="1.1.15" targetFramework="netnano1.0" />
5+
<package id="nanoFramework.System.Text" version="1.2.7" targetFramework="netnano1.0" />
6+
<package id="Nerdbank.GitVersioning" version="3.5.113" targetFramework="netnano1.0" developmentDependency="true" />
7+
<package id="StyleCop.MSBuild" version="6.2.0" targetFramework="netnano1.0" developmentDependency="true" />
8+
</packages>

0 commit comments

Comments
 (0)