initial support for serial port on FreeBSD (dotnet/corefx#42519)
authorTomas Weinfurt <tweinfurt@yahoo.com>
Thu, 14 Nov 2019 00:52:58 +0000 (16:52 -0800)
committerGitHub <noreply@github.com>
Thu, 14 Nov 2019 00:52:58 +0000 (16:52 -0800)
* initial support for serial port on FreeBSD

* adjust port name selection

* update packaging

* feedback from review

* fix PortHelper

* update include with AnyOS

Commit migrated from https://github.com/dotnet/corefx/commit/7a4822f323bcaa18e184cf2403eb68cf1f8c9d28

src/libraries/System.IO.Ports/src/Configurations.props
src/libraries/System.IO.Ports/src/System.IO.Ports.csproj
src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.FreeBSD.cs [new file with mode: 0644]
src/libraries/System.IO.Ports/tests/Configurations.props
src/libraries/System.IO.Ports/tests/Support/PortHelper.cs
src/libraries/pkg/runtime.native.System.IO.Ports/netcoreapp.rids.props
src/libraries/pkg/runtime.native.System.IO.Ports/runtime.native.System.IO.Ports.pkgproj

index 05f6eed..7db193e 100644 (file)
@@ -4,6 +4,7 @@
       netstandard2.0-Windows_NT;
       netstandard2.0-Linux;
       netstandard2.0-OSX;
+      netstandard2.0-FreeBSD;
       netstandard2.0;
       net461-Windows_NT;
     </PackageConfigurations>
index 05c5d7b..bb26fad 100644 (file)
@@ -2,12 +2,12 @@
   <PropertyGroup>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
-    <GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetsNetStandard)' == 'true' and '$(TargetsWindows)' != 'true' and '$(TargetsLinux)' != 'true' and '$(TargetsOSX)' != 'true'">SR.PlatformNotSupported_IOPorts</GeneratePlatformNotSupportedAssemblyMessage>
+    <GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetsNetStandard)' == 'true' and '$(OSGroup)' == 'AnyOS'">SR.PlatformNotSupported_IOPorts</GeneratePlatformNotSupportedAssemblyMessage>
     <DefineConstants>$(DefineConstants);NOSPAN</DefineConstants>
     <IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
     <Configurations>net461-Windows_NT-Debug;net461-Windows_NT-Release;netfx-Windows_NT-Debug;netfx-Windows_NT-Release;netstandard2.0-Debug;netstandard2.0-Linux-Debug;netstandard2.0-Linux-Release;netstandard2.0-OSX-Debug;netstandard2.0-OSX-Release;netstandard2.0-Release;netstandard2.0-Windows_NT-Debug;netstandard2.0-Windows_NT-Release</Configurations>
   </PropertyGroup>
-  <ItemGroup Condition="'$(TargetsNetFx)' != 'true' and ('$(TargetsWindows)' == 'true' or '$(TargetsLinux)' == 'true' or '$(TargetsOSX)' == 'true')">
+  <ItemGroup Condition="'$(TargetsNetStandard)' == 'true' and '$(OSGroup)' != 'AnyOS'">
     <Compile Include="System\IO\Ports\Handshake.cs" />
     <Compile Include="System\IO\Ports\InternalResources.cs" />
     <Compile Include="System\IO\Ports\Parity.cs" />
   <ItemGroup Condition=" '$(TargetsOSX)' == 'true' ">
     <Compile Include="System\IO\Ports\SerialPort.OSX.cs" />
   </ItemGroup>
-  <ItemGroup Condition=" '$(TargetsLinux)' == 'true'  or '$(TargetsOSX)' == 'true'">
+  <ItemGroup Condition=" '$(TargetsFreeBSD)' == 'true' ">
+    <Compile Include="System\IO\Ports\SerialPort.FreeBSD.cs" />
+  </ItemGroup>
+  <ItemGroup Condition=" '$(TargetsLinux)' == 'true' or '$(TargetsOSX)' == 'true' or '$(TargetsFreeBSD)' == 'true'">
     <Compile Include="System\IO\Ports\SafeSerialDeviceHandle.Unix.cs" />
     <Compile Include="System\IO\Ports\SerialStream.Unix.cs" />
     <Compile Include="Interop\Unix\Interop.Termios.cs" />
     <Reference Include="System.Threading" />
     <Reference Include="System.Threading.Thread" />
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.FreeBSD.cs b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.FreeBSD.cs
new file mode 100644 (file)
index 0000000..c56f59c
--- /dev/null
@@ -0,0 +1,37 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.ComponentModel;
+using System.Collections.Generic;
+using System.IO;
+
+namespace System.IO.Ports
+{
+    public partial class SerialPort : Component
+    {
+        public static string[] GetPortNames()
+        {
+            List<string> ports = new List<string>();
+
+            foreach (string name in Directory.GetFiles("/dev", "ttyd*"))
+            {
+                if (!name.EndsWith(".init") && !name.EndsWith(".lock"))
+                {
+                    ports.Add(name);
+                }
+            }
+
+            foreach (string name in Directory.GetFiles("/dev", "cuau*"))
+            {
+                if (!name.EndsWith(".init") && !name.EndsWith(".lock"))
+                {
+                    ports.Add(name);
+                }
+            }
+
+            return ports.ToArray();
+        }
+    }
+}
index b7e6c2c..6a40cf6 100644 (file)
@@ -4,6 +4,7 @@
       netcoreapp-Windows_NT;
       netcoreapp-Linux;
       netcoreapp-OSX;
+      netcoreapp-FreeBSD;
       netfx;
     </BuildConfigurations>
   </PropertyGroup>
index f3ba015..9bc9880 100644 (file)
@@ -22,7 +22,7 @@ namespace Legacy.Support
 
         public static string[] GetPorts()
         {
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+            if (!PlatformDetection.IsWindows)
             {
                 return SerialPort.GetPortNames();
             }
index 5aae9f1..a490435 100644 (file)
@@ -8,5 +8,6 @@
     </BuildRID>
     <BuildRID Include="linux-x64" />
     <BuildRID Include="osx-x64" />
+    <BuildRID Include="freebsd-x64" />
   </ItemGroup>
 </Project>
index ccf8d4b..d7a35de 100644 (file)
@@ -4,7 +4,7 @@
     <SkipPackageFileCheck>true</SkipPackageFileCheck>
     <SkipValidatePackage>true</SkipValidatePackage>
   </PropertyGroup>
-  <ItemGroup Condition="'$(PackageTargetRuntime)' == 'linux-arm' or '$(PackageTargetRuntime)' == 'linux-arm64' or '$(PackageTargetRuntime)' == 'linux-x64' or '$(PackageTargetRuntime)' == 'osx-x64'">
+  <ItemGroup Condition="'$(PackageTargetRuntime)' == 'linux-arm' or '$(PackageTargetRuntime)' == 'linux-arm64' or '$(PackageTargetRuntime)' == 'linux-x64' or '$(PackageTargetRuntime)' == 'osx-x64' or '$(PackageTargetRuntime)' == 'freebsd-x64'">
     <File Include="$(NativeBinDir)\System.IO.Ports.Native$(LibraryFileExtension)" >
       <TargetPath>runtimes/$(PackageRID)/native</TargetPath>
     </File>