Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Send sdb command and get string result of the sdb command.
        /// </summary>
        /// <param name="serialNumber">Serial number to specify target device. Refer to the 1st column of "$sdb devices".</param>
        /// <param name="protocol">Command type of sdb. ex) appcmd, etc</param>
        /// <param name="cmd">Command to be executed. ex)install, appinfo etc.</param>
        /// <param name="args">Command to be executed. ex)App ID, package ID etc.</param>
        /// <returns>Instant which stores returned output and exit value of the sdb command.</returns>
        public static List <string> RequestToTargetSync(string serialNumber, string protocol, string cmd, params string[] args)
        {
            SDBConnection sdbConnection = EstablishConnection(serialNumber);

            if (sdbConnection == null || !SendMsg(sdbConnection, protocol, cmd, args))
            {
                return(null);
            }

            List <string> responsedMsg = new List <string>();

            while (true)
            {
                string responsedLine = string.Empty;
                string ch            = string.Empty;

                while ((ch) != SDBProtocol.terminator)
                {
                    ch             = sdbConnection.ReadData(new byte[1]);
                    responsedLine += ch;
                }

                if (responsedLine == SDBProtocol.terminator)
                {
                    break;
                }

                responsedMsg.Add(responsedLine);
            }

            sdbConnection.Close();
            return(responsedMsg);
        }
Beispiel #3
0
        public bool ConnectBridge()
        {
            this.sdbConnection = SDBConnection.Create();
            if (this.sdbConnection == null)
            {
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Send sdb command and get string result of the sdb command.
        /// </summary>
        /// <param name="serialNumber">Serial number to specify target device. Refer to the 1st column of "$sdb devices".</param>
        /// <param name="msgCharHandler">Method to be called when each character is returned by the sdb command. String parameter and void return type is required</param>
        /// <param name="lastWorker">Method to be called when the sdb command response is over. Empty parameter and void return type is required. You can leave it as null for permanent works such as a dlogutil.</param>
        /// <param name="protocol">Command type of sdb. ex) appcmd, etc</param>
        /// <param name="cmd">Command to be executed. ex)install, appinfo etc.</param>
        /// <param name="args">Command to be executed. ex)App ID, package ID etc.</param>
        /// <returns>Thread instance running in background</returns>
        public static Thread RequestToTargetAsync(string serialNumber, MessageHandler msgCharHandler, LastWorkExecutor lastWorker,
                                                  string protocol, string cmd, string[] args)
        {
            SDBConnection sdbConnection = EstablishConnection(serialNumber);

            if (msgCharHandler == null || sdbConnection == null || !SendMsg(sdbConnection, protocol, cmd, args))
            {
                return(null);
            }

            Thread mgsConsumerThread = new Thread(() => ConsumeMsgInBG(sdbConnection, msgCharHandler, lastWorker));

            mgsConsumerThread.Start();

            return(mgsConsumerThread);
        }
Beispiel #5
0
        private static void ConsumeMsgInBG(SDBConnection sdbConnection, MessageHandler msgCharHandler, LastWorkExecutor lastWorker)
        {
            string responsedMsg;

            try
            {
                while ((responsedMsg = sdbConnection.ReadData(new byte[1])) != string.Empty && responsedMsg != "\0")
                {
                    msgCharHandler(responsedMsg);
                }

                lastWorker?.Invoke();

                sdbConnection.Close();
            }
            catch (ThreadAbortException abortException)
            {
                Debug.WriteLine($"Message consumer aborted. {abortException.Message}");
                sdbConnection.Close();
            }
        }
Beispiel #6
0
        private static bool SendMsg(SDBConnection sdbConnection, string protocol, string cmd, string[] args)
        {
            string msg = protocol + SDBProtocol.delemeter + cmd;

            foreach (string arg in args)
            {
                msg += (SDBProtocol.delemeter + arg);
            }

            SDBRequest  request  = SDBConnection.MakeRequest(msg);
            SDBResponse response = sdbConnection.Send(request);

            if (!response.IOSuccess || !response.Okay)
            {
                Debug.WriteLine($"Failed to get SDB response. {response.Message}");
                sdbConnection.Close();
                return(false);
            }

            return(true);
        }
Beispiel #7
0
        public static SDBConnection Create()
        {
            SDBConnection sdbconnection = new SDBConnection();

            sdbconnection.Initialize();

            if (!sdbconnection.ConnectSDB())
            {
                if (!sdbconnection.RunDaemon())
                {
                    return(null);
                }

                if (!sdbconnection.ConnectSDB())
                {
                    return(null);
                }
            }

            return(sdbconnection);
        }
        public void StopService()
        {
            if (this.taskCancelSource != null)
            {
                this.taskCancelSource.Cancel();
            }

            if (this.taskMonitor != null)
            {
                try
                {
                    if (this.sdbconnection != null)
                    {
                        this.sdbconnection.Shutdown();
                    }

                    this.taskMonitor.Wait();
                }
                catch (AggregateException e)
                {
                    foreach (var v in e.InnerExceptions)
                    {
                        Console.WriteLine(e.Message + " " + v.Message);
                    }
                }
            }

            if (this.taskCancelSource != null)
            {
                this.taskCancelSource.Dispose();
                this.taskCancelSource = null;
            }

            if (this.sdbconnection != null)
            {
                this.sdbconnection.Close();
                this.sdbconnection = null;
            }
        }
        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);
        }