|
| 1 | +#region License |
| 2 | +/* Copyright 2017 James F. Bellinger <http://www.zer7.com/software/hidsharp> |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, |
| 11 | + software distributed under the License is distributed on an |
| 12 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | + KIND, either express or implied. See the License for the |
| 14 | + specific language governing permissions and limitations |
| 15 | + under the License. */ |
| 16 | +#endregion |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Diagnostics; |
| 20 | +using HidSharp.Utility; |
| 21 | + |
| 22 | +namespace HidSharp |
| 23 | +{ |
| 24 | + public abstract class Device |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// Makes a connection to the device, or throws an exception if the connection cannot be made. |
| 28 | + /// </summary> |
| 29 | + /// <returns>The stream to use to communicate with the device.</returns> |
| 30 | + public DeviceStream Open() |
| 31 | + { |
| 32 | + return Open(null); |
| 33 | + } |
| 34 | + |
| 35 | + public DeviceStream Open(OpenConfiguration openConfig) |
| 36 | + { |
| 37 | + return OpenDeviceAndRestrictAccess(openConfig ?? new OpenConfiguration()); |
| 38 | + } |
| 39 | + |
| 40 | + protected virtual DeviceStream OpenDeviceAndRestrictAccess(OpenConfiguration openConfig) |
| 41 | + { |
| 42 | + bool exclusive = (bool)openConfig.GetOption(OpenOption.Exclusive); |
| 43 | + |
| 44 | + DeviceOpenUtility openUtility = null; |
| 45 | + if (exclusive) |
| 46 | + { |
| 47 | + string streamPath = GetStreamPath(openConfig); |
| 48 | + openUtility = new DeviceOpenUtility(this, streamPath, openConfig); |
| 49 | + openUtility.Open(); |
| 50 | + } |
| 51 | + |
| 52 | + DeviceStream stream; |
| 53 | + try |
| 54 | + { |
| 55 | + stream = OpenDeviceDirectly(openConfig); |
| 56 | + if (exclusive) |
| 57 | + { |
| 58 | + stream.Closed += (sender, e) => openUtility.Close(); |
| 59 | + openUtility.InterruptRequested += (sender, e) => |
| 60 | + { |
| 61 | + stream.OnInterruptRequested(); |
| 62 | + HidSharpDiagnostics.Trace("Delivered an interrupt request."); |
| 63 | + }; |
| 64 | + } |
| 65 | + } |
| 66 | + catch |
| 67 | + { |
| 68 | + if (exclusive) { openUtility.Close(); } |
| 69 | + throw; |
| 70 | + } |
| 71 | + |
| 72 | + return stream; |
| 73 | + } |
| 74 | + |
| 75 | + protected abstract DeviceStream OpenDeviceDirectly(OpenConfiguration openConfig); |
| 76 | + |
| 77 | + // Used for exclusion... and also may be used inside OpenDeviceDirectly if desired. |
| 78 | + protected virtual string GetStreamPath(OpenConfiguration openConfig) |
| 79 | + { |
| 80 | + return DevicePath; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Tries to make a connection to the device. |
| 85 | + /// </summary> |
| 86 | + /// <param name="stream">The stream to use to communicate with the device.</param> |
| 87 | + /// <returns><c>true</c> if the connection was successful.</returns> |
| 88 | + public bool TryOpen(out DeviceStream stream) |
| 89 | + { |
| 90 | + return TryOpen(null, out stream); |
| 91 | + } |
| 92 | + |
| 93 | + public bool TryOpen(OpenConfiguration openConfig, out DeviceStream stream) |
| 94 | + { |
| 95 | + Exception exception; |
| 96 | + return TryOpen(openConfig, out stream, out exception); |
| 97 | + } |
| 98 | + |
| 99 | + public bool TryOpen(OpenConfiguration openConfig, out DeviceStream stream, out Exception exception) |
| 100 | + { |
| 101 | + try |
| 102 | + { |
| 103 | + stream = Open(openConfig); exception = null; return true; |
| 104 | + } |
| 105 | + catch (Exception e) |
| 106 | + { |
| 107 | + Debug.WriteLine(e); |
| 108 | + stream = null; exception = e; return false; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Returns the file system path of the device. |
| 114 | + /// This can be used to check permissions on Linux hidraw, for instance. |
| 115 | + /// </summary> |
| 116 | + /// <returns>The file system path.</returns> |
| 117 | + public abstract string GetFileSystemName(); |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Returns a name appropriate for display. |
| 121 | + /// </summary> |
| 122 | + /// <returns>The friendly name.</returns> |
| 123 | + public abstract string GetFriendlyName(); |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Checks if a particular implementation detail, such as the use of the Linux hidraw API, applies to this device. |
| 127 | + /// See <see cref="ImplementationDetail"/> for a list of possible details. |
| 128 | + /// </summary> |
| 129 | + /// <param name="detail">The detail to check.</param> |
| 130 | + /// <returns><c>true</c> if the implementation detail applies.</returns> |
| 131 | + public virtual bool HasImplementationDetail(Guid detail) |
| 132 | + { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// The operating system's name for the device. |
| 138 | + /// |
| 139 | + /// If you have multiple devices with the same Vendor ID, Product ID, Serial Number, etc., |
| 140 | + /// this may be useful for differentiating them. |
| 141 | + /// </summary> |
| 142 | + public abstract string DevicePath |
| 143 | + { |
| 144 | + get; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments