diff --git a/src/NmeaParser/BluetoothDevice.WinStore.cs b/src/NmeaParser/BluetoothDevice.UWP.cs similarity index 91% rename from src/NmeaParser/BluetoothDevice.WinStore.cs rename to src/NmeaParser/BluetoothDevice.UWP.cs index 0bf62aa..2fbc41e 100644 --- a/src/NmeaParser/BluetoothDevice.WinStore.cs +++ b/src/NmeaParser/BluetoothDevice.UWP.cs @@ -1,18 +1,18 @@ -// -// Copyright (c) 2014 Morten Nielsen -// -// Licensed under the Microsoft Public License (Ms-PL) (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://opensource.org/licenses/Ms-PL.html -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +// +// Copyright (c) 2014 Morten Nielsen +// +// Licensed under the Microsoft Public License (Ms-PL) (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://opensource.org/licenses/Ms-PL.html +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// #if NETFX_CORE using System; using System.Collections.Generic; @@ -23,11 +23,9 @@ using System.Threading.Tasks; using Windows.Networking.Sockets; using Windows.Devices.Bluetooth.Rfcomm; using System.Runtime.InteropServices.WindowsRuntime; -using System.Threading; -using Windows.Devices.Enumeration; -#if WINDOWS_UWP +using System.Threading; +using Windows.Devices.Enumeration; using Windows.Networking.Proximity; -#endif namespace NmeaParser { @@ -37,39 +35,36 @@ namespace NmeaParser public class BluetoothDevice : NmeaDevice { private Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService m_deviceService; -#if WINDOWS_UWP private Windows.Networking.Proximity.PeerInformation m_devicePeer; -#endif - private StreamSocket m_socket; - private bool m_disposeService; - - /// - /// Gets a list of bluetooth devices that supports serial communication - /// + private StreamSocket m_socket; + private bool m_disposeService; + private SemaphoreSlim m_semaphoreSlim = new SemaphoreSlim(1, 1); + + /// + /// Gets a list of bluetooth devices that supports serial communication + /// /// public static async Task> GetBluetoothSerialDevicesAsync() - { - string serialDeviceType = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort); - var devices = await DeviceInformation.FindAllAsync(serialDeviceType); - List services = new List(); - foreach(var d in devices) - services.Add(await RfcommDeviceService.FromIdAsync(d.Id)); - return services; - } - + { + string serialDeviceType = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort); + var devices = await DeviceInformation.FindAllAsync(serialDeviceType); + List services = new List(); + foreach(var d in devices) + services.Add(await RfcommDeviceService.FromIdAsync(d.Id)); + return services; + } + /// /// Initializes a new instance of the class. /// - /// The RF Comm Device service. + /// The RF Comm Device service. /// Whether this devicee should also dispose the RfcommDeviceService provided when this device disposes. public BluetoothDevice(RfcommDeviceService service, bool disposeService = false) { m_deviceService = service; - m_disposeService = disposeService; + m_disposeService = disposeService; } -#if WINDOWS_UWP - /// /// Initializes a new instance of the class. /// @@ -78,16 +73,15 @@ namespace NmeaParser { m_devicePeer = peer; } -#endif /// - protected override void Dispose(bool disposing) - { - if (m_disposeService && m_deviceService != null) - m_deviceService.Dispose(); - m_deviceService = null; - m_devicePeer = null; - base.Dispose(disposing); + protected override void Dispose(bool disposing) + { + if (m_disposeService && m_deviceService != null) + m_deviceService.Dispose(); + m_deviceService = null; + m_devicePeer = null; + base.Dispose(disposing); } /// @@ -98,18 +92,16 @@ namespace NmeaParser { var socket = new Windows.Networking.Sockets.StreamSocket(); socket.Control.KeepAlive = true; -#if WINDOWS_UWP if (m_devicePeer != null) { await socket.ConnectAsync(m_devicePeer.HostName, "1"); } else -#endif { await socket.ConnectAsync(m_deviceService.ConnectionHostName, m_deviceService.ConnectionServiceName); } m_socket = socket; - return null; //We're going to use WinRT buffers instead and will handle read/write, so no reason to return a stream + return null; //We're going to use WinRT buffers instead and will handle read/write, so no reason to return a stream. This is mainly done to avoid locking issues reading and writing at the same time } /// @@ -119,51 +111,50 @@ namespace NmeaParser /// protected override Task CloseStreamAsync(System.IO.Stream stream) { - if(m_socket == null) - throw new InvalidOperationException("No connection to close"); + if(m_socket == null) + throw new InvalidOperationException("No connection to close"); m_socket.Dispose(); m_socket = null; return Task.FromResult(true); } - private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1); /// - protected override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - { - // Reading and writing to the Bluetooth serial connection at the same time seems very unstable in UWP, - // so we use a semaphore to ensure we don't read and write at the same time - await semaphoreSlim.WaitAsync().ConfigureAwait(false); - try - { - var r = await m_socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)count, Windows.Storage.Streams.InputStreamOptions.None); - return (int)r.Length; - } - finally - { - semaphoreSlim.Release(); - } + protected override async Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + // Reading and writing to the Bluetooth serial connection at the same time seems very unstable in UWP, + // so we use a semaphore to ensure we don't read and write at the same time + await m_semaphoreSlim.WaitAsync().ConfigureAwait(false); + try + { + var r = await m_socket.InputStream.ReadAsync(buffer.AsBuffer(), (uint)count, Windows.Storage.Streams.InputStreamOptions.None); + return (int)r.Length; + } + finally + { + m_semaphoreSlim.Release(); + } } /// public override bool CanWrite => true; - + /// - public override async Task WriteAsync(byte[] buffer, int offset, int length) - { - if (m_socket == null) - throw new InvalidOperationException("Device not open"); - // Reading and writing to the Bluetooth serial connection at the same time seems very unstable in UWP, - // so we use a semaphore to ensure we don't read and write at the same time - await semaphoreSlim.WaitAsync().ConfigureAwait(false); - try - { - await m_socket.OutputStream.WriteAsync(buffer.AsBuffer(offset, length)).AsTask().ConfigureAwait(false); - } - finally - { - semaphoreSlim.Release(); - } + public override async Task WriteAsync(byte[] buffer, int offset, int length) + { + if (m_socket == null) + throw new InvalidOperationException("Device not open"); + // Reading and writing to the Bluetooth serial connection at the same time seems very unstable in UWP, + // so we use a semaphore to ensure we don't read and write at the same time + await m_semaphoreSlim.WaitAsync().ConfigureAwait(false); + try + { + await m_socket.OutputStream.WriteAsync(buffer.AsBuffer(offset, length)).AsTask().ConfigureAwait(false); + } + finally + { + m_semaphoreSlim.Release(); + } } } } diff --git a/src/NmeaParser/SerialPortDevice.Desktop.cs b/src/NmeaParser/SerialPortDevice.Desktop.cs index 1fc845f..c86d004 100644 --- a/src/NmeaParser/SerialPortDevice.Desktop.cs +++ b/src/NmeaParser/SerialPortDevice.Desktop.cs @@ -27,40 +27,33 @@ namespace NmeaParser /// A Serial Port NMEA device /// public class SerialPortDevice : NmeaDevice - { - private System.IO.Ports.SerialPort m_port; - - /// - /// Initializes a new instance of the class. - /// - /// The serial port. - /// port - public SerialPortDevice(System.IO.Ports.SerialPort port) + { + /// + /// Initializes a new instance of the class. + /// + /// The serial port. + /// port + public SerialPortDevice(System.IO.Ports.SerialPort port) { if (port == null) throw new ArgumentNullException("port"); - m_port = port; - } - - /// - /// Gets the active serial port. - /// - public System.IO.Ports.SerialPort Port + Port = port; + } + + /// + /// Gets the active serial port. + /// + public System.IO.Ports.SerialPort Port { get; } + + /// + /// Creates the stream the NmeaDevice is working on top off. + /// + /// + protected override Task OpenStreamAsync() { - get - { - return m_port; - } - } - - /// - /// Creates the stream the NmeaDevice is working on top off. - /// - /// - protected override Task OpenStreamAsync() - { - m_port.Open(); - return Task.FromResult(m_port.BaseStream); + if (!Port.IsOpen) + Port.Open(); + return Task.FromResult(Port.BaseStream); } /// @@ -69,9 +62,10 @@ namespace NmeaParser /// The stream. /// protected override Task CloseStreamAsync(System.IO.Stream stream) - { - m_port.Close(); - return Task.FromResult(true); + { + if (Port.IsOpen) + Port.Close(); + return Task.FromResult(null); } /// @@ -84,7 +78,7 @@ namespace NmeaParser [Obsolete("Use WriteAsync")] public void Write(byte[] buffer, int offset, int count) { - m_port.Write(buffer, offset, count); + Port.Write(buffer, offset, count); } /// @@ -93,10 +87,10 @@ namespace NmeaParser /// public override Task WriteAsync(byte[] buffer, int offset, int length) { - if (!m_port.IsOpen) + if (!Port.IsOpen) throw new InvalidOperationException("Device not open"); - m_port.Write(buffer, offset, length); + Port.Write(buffer, offset, length); return Task.FromResult(null); } } diff --git a/src/NmeaParser/SerialPortDevice.UWP.cs b/src/NmeaParser/SerialPortDevice.UWP.cs index d97c005..75c3c63 100644 --- a/src/NmeaParser/SerialPortDevice.UWP.cs +++ b/src/NmeaParser/SerialPortDevice.UWP.cs @@ -71,7 +71,8 @@ namespace NmeaParser /// protected override Task CloseStreamAsync(System.IO.Stream stream) { - return Task.FromResult(true); + stream.Dispose(); + return Task.CompletedTask; } /// diff --git a/src/NmeaParser/StreamDevice.cs b/src/NmeaParser/StreamDevice.cs index 0823b83..96c34cc 100644 --- a/src/NmeaParser/StreamDevice.cs +++ b/src/NmeaParser/StreamDevice.cs @@ -68,7 +68,6 @@ namespace NmeaParser m_stream = null; } - /// public override bool CanWrite => m_stream?.CanWrite == true;