private static SDBConnection EstablishConnection(string serialNumber) { SDBConnection sdbConnection = SDBConnection.Create(); if (sdbConnection == null) { Debug.WriteLine("Failed to establish SDB connection."); return(null); } string msgForConnection = "host:transport:" + serialNumber; SDBRequest request = SDBConnection.MakeRequest(msgForConnection); SDBResponse response = sdbConnection.Send(request); if (response.IOSuccess && response.Okay) { return(sdbConnection); } else { Debug.WriteLine($"Failed to get SDB response. {response.Message}"); sdbConnection.Close(); return(null); } }
public bool ConnectBridge() { this.sdbConnection = SDBConnection.Create(); if (this.sdbConnection == null) { return(false); } return(true); }
private DeviceMonitorResult DeviceChangeDetectTask() { DeviceMonitorResult ret = DeviceMonitorResult.CanceledByUser; this.sdbconnection = SDBConnection.Create(); if (this.sdbconnection == null) { OutputDeviceInfoMsg("Failed to connect to SDB Server. "); this.taskCancelSource.Cancel(); retryConntectServerCount--; if (retryConntectServerCount > 0) { NotifyConnectServerFailed(); } else { OutputDeviceInfoMsg("Too many retries.\n" + " Please check Tools option and sdb.exe is installed correctly.\n" + " After the problem is fixed, reopen the Solution to start the Device Monitor"); } return(DeviceMonitorResult.SdbEstablishmentError); } Thread.Sleep(100); SDBRequest request = SDBConnection.MakeRequest("host:track-devices"); SDBResponse response = this.sdbconnection.Send(request); if (!response.IOSuccess || !response.Okay) { this.sdbconnection.Close(); this.sdbconnection = null; OutputDeviceInfoMsg("Failed to start device monitor."); this.taskCancelSource.Cancel(); NotifyConnectServerFailed(); return(DeviceMonitorResult.SdbResponseError); } OutputDeviceInfoMsg("Device monitor started."); CancellationToken cancelToken = this.taskCancelSource.Token; bool connectionError = false; while (true) { if (this.sdbconnection.ConnectionError()) { connectionError = true; OutputDeviceInfoMsg("SDB Server disconnected."); ret = DeviceMonitorResult.SdbServerKilled; break; } if (this.sdbconnection.DataAvailable()) { int length = this.sdbconnection.ReadLength(); if (length > 0) { OutputDeviceChangeMsg(); byte[] buffer = new byte[length]; string result = this.sdbconnection.ReadData(buffer); ProcessDeviceData(result); NotifySubscribers(); } else if (length == 0) { if (this.deviceInfoList.Count > 0) { // show message only if there was any device connected // we don't have to show when 0 changed to 0 OutputDeviceChangeMsg(); } ProcessDeviceData(String.Empty); NotifySubscribers(); } else { if (cancelToken.IsCancellationRequested) { ret = DeviceMonitorResult.CanceledByUser; break; } connectionError = true; OutputDeviceInfoMsg("SDB Server disconnected."); ret = DeviceMonitorResult.SdbServerKilled; break; } } else { #if DEBUG ////OutputDeviceInfoMsg("sdb host:track-devices timeout...", false); #endif Thread.Sleep(1); } if (cancelToken.IsCancellationRequested) { ret = DeviceMonitorResult.CanceledByUser; break; } } this.sdbconnection.Close(); this.sdbconnection = null; if (connectionError) { NeedsRestart = true; } return(ret); }