|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading; |
| 6 | +using LabNation.DeviceInterface.Hardware; |
| 7 | +using LabNation.Common; |
| 8 | +#if ANDROID |
| 9 | +using Android.Content; |
| 10 | +#endif |
| 11 | + |
| 12 | +namespace LabNation.DeviceInterface.Devices |
| 13 | +{ |
| 14 | + public class HackerSpecialManager |
| 15 | + { |
| 16 | + public event DeviceConnectHandler DeviceConnected; |
| 17 | + private IDevice device = null; |
| 18 | + public IDevice Device { get { return device; } } |
| 19 | + |
| 20 | + Thread pollThread; |
| 21 | + |
| 22 | +#if WINDOWS |
| 23 | + Thread badDriverDetectionThread; |
| 24 | + bool running = true; |
| 25 | + DateTime? lastSmartScopeDetectedThroughWinUsb; |
| 26 | + DateTime? lastSmartScopeDetectedThroughVidPid; |
| 27 | + int WinUsbDetectionWindow = 3000; |
| 28 | + public bool BadDriver { get; private set; } |
| 29 | + int SmartScopeVid = 0x04D8; |
| 30 | + int SmartScopePid = 0xF4B5; |
| 31 | +#endif |
| 32 | + |
| 33 | +#if ANDROID |
| 34 | + Context context; |
| 35 | +#endif |
| 36 | + |
| 37 | + |
| 38 | + public HackerSpecialManager( |
| 39 | +#if ANDROID |
| 40 | + Context context |
| 41 | +#endif |
| 42 | +) |
| 43 | + : this( |
| 44 | +#if ANDROID |
| 45 | + context, |
| 46 | +#endif |
| 47 | + null) { } |
| 48 | + |
| 49 | + public HackerSpecialManager( |
| 50 | +#if ANDROID |
| 51 | + Context context |
| 52 | +#endif |
| 53 | + DeviceConnectHandler deviceConnectHandler |
| 54 | +) |
| 55 | + { |
| 56 | +#if ANDROID |
| 57 | + this.context = context; |
| 58 | +#endif |
| 59 | + this.DeviceConnected = deviceConnectHandler; |
| 60 | + } |
| 61 | + |
| 62 | + public void Start(bool async = true) |
| 63 | + { |
| 64 | + pollThread = new Thread(PollUponStart); |
| 65 | + pollThread.Name = "Devicemanager Startup poll"; |
| 66 | + |
| 67 | +#if ANDROID |
| 68 | + InterfaceManagerXamarin.context = this.context; |
| 69 | + InterfaceManagerXamarin.Instance.onConnect += OnHardwareConnect; |
| 70 | +#elif WINUSB |
| 71 | + InterfaceManagerWinUsb.Instance.onConnect += OnHardwareConnect; |
| 72 | +#elif IOS |
| 73 | + //Nothing for the moment |
| 74 | +#else |
| 75 | + InterfaceManagerLibUsb.Instance.onConnect += OnHardwareConnect; |
| 76 | +#endif |
| 77 | + |
| 78 | + pollThread.Start(); |
| 79 | + |
| 80 | + if (!async) |
| 81 | + pollThread.Join(); |
| 82 | + } |
| 83 | + |
| 84 | + private void PollUponStart() |
| 85 | + { |
| 86 | +#if ANDROID |
| 87 | + InterfaceManagerXamarin.Instance.PollDevice(); |
| 88 | +#elif WINUSB |
| 89 | + InterfaceManagerWinUsb.Instance.PollDevice(); |
| 90 | + badDriverDetectionThread = new Thread(SearchDeviceFromVidPidThread); |
| 91 | + badDriverDetectionThread.Name = "Bad WINUSB driver detection"; |
| 92 | + BadDriver = false; |
| 93 | + badDriverDetectionThread.Start(); |
| 94 | +#elif !IOS |
| 95 | + InterfaceManagerLibUsb.Instance.PollDevice(); |
| 96 | +#endif |
| 97 | + } |
| 98 | + |
| 99 | + public void Stop() |
| 100 | + { |
| 101 | + if(pollThread != null) |
| 102 | + pollThread.Join(100); |
| 103 | + |
| 104 | +#if ANDROID |
| 105 | + //Nothing to do here, just keeping same ifdef structure as above |
| 106 | +#elif WINDOWS |
| 107 | + BadDriver = false; |
| 108 | + running = false; |
| 109 | + if(badDriverDetectionThread != null) |
| 110 | + badDriverDetectionThread.Join(100); |
| 111 | +#elif IOS |
| 112 | + //Nothing for the moment |
| 113 | +#else |
| 114 | + //Linux, MacOS |
| 115 | + InterfaceManagerLibUsb.Instance.Destroy(); |
| 116 | +#endif |
| 117 | + } |
| 118 | + |
| 119 | + private void OnHardwareConnect(IHardwareInterface hardwareInterface, bool connected) |
| 120 | + { |
| 121 | + string serial = hardwareInterface.Serial; |
| 122 | + if (!(hardwareInterface is ISmartScopeInterface)) |
| 123 | + { |
| 124 | + Logger.Info("Ignoring hardwareinterface since not ISmartScopeInterface but " + hardwareInterface.GetType().ToString()); |
| 125 | + return; |
| 126 | + } |
| 127 | + ISmartScopeInterface ssIface = hardwareInterface as ISmartScopeInterface; |
| 128 | + if(connected) { |
| 129 | + this.device = new HackerSpecial(ssIface); |
| 130 | + |
| 131 | + #if WINDOWS |
| 132 | + lastSmartScopeDetectedThroughWinUsb = DateTime.Now; |
| 133 | + Logger.Debug(String.Format("Update winusb detection time to {0}", lastSmartScopeDetectedThroughWinUsb)); |
| 134 | + #endif |
| 135 | + |
| 136 | + if (this.DeviceConnected != null) |
| 137 | + DeviceConnected(this.device, true); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + if (this.device != null && this.device.HardwareInterface == hardwareInterface) |
| 142 | + { |
| 143 | + if (this.DeviceConnected != null) |
| 144 | + DeviceConnected(this.device, false); |
| 145 | + } |
| 146 | + #if WINDOWS |
| 147 | + lastSmartScopeDetectedThroughWinUsb = null; |
| 148 | + #endif |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | +#if WINDOWS |
| 153 | + public void WinUsbPoll() |
| 154 | + { |
| 155 | + InterfaceManagerWinUsb.Instance.PollDevice(); |
| 156 | + } |
| 157 | + |
| 158 | + private void SearchDeviceFromVidPidThread() |
| 159 | + { |
| 160 | + while (running) |
| 161 | + { |
| 162 | + Thread.Sleep(500); |
| 163 | + //Abort this thread once a device is found through WinUSB |
| 164 | + if (lastSmartScopeDetectedThroughWinUsb != null) |
| 165 | + { |
| 166 | + Logger.Debug("Good winusb driver!"); |
| 167 | + BadDriver = false; |
| 168 | + running = false; |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + //Try to find a device through the system management stuff |
| 173 | + string serial = null; |
| 174 | + if (LabNation.Common.Utils.TestUsbDeviceFound(SmartScopeVid, SmartScopePid, out serial)) |
| 175 | + { |
| 176 | + lastSmartScopeDetectedThroughVidPid = DateTime.Now; |
| 177 | + Logger.Debug(String.Format("Update vidpid detection time to {0}", lastSmartScopeDetectedThroughVidPid)); |
| 178 | + } |
| 179 | + |
| 180 | + //A device was found using VID/PID at least <WinUsbDetectionWindow>ms ago |
| 181 | + if (lastSmartScopeDetectedThroughVidPid != null) |
| 182 | + { |
| 183 | + //Wait <WinUsbDetectionWindow>ms for corresponding WinUSB detection |
| 184 | + do |
| 185 | + { |
| 186 | + //If a device came in through WinUSB, stop this detection |
| 187 | + if (lastSmartScopeDetectedThroughWinUsb != null) |
| 188 | + { |
| 189 | + Logger.Debug("Good winusb driver!"); |
| 190 | + BadDriver = false; |
| 191 | + running = false; |
| 192 | + return; |
| 193 | + } |
| 194 | + Thread.Sleep(100); |
| 195 | + } while ((DateTime.Now - lastSmartScopeDetectedThroughVidPid.Value).TotalMilliseconds < WinUsbDetectionWindow); |
| 196 | + |
| 197 | + //If no winusb device came in during the detection window, flag a bad driver |
| 198 | + BadDriver = true; |
| 199 | + Logger.Warn("Bad WINUSB driver"); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | +#endif |
| 204 | + } |
| 205 | +} |
0 commit comments