Exemple #1
0
        /// <inheritdoc/>
        protected void Root(string request, DeviceData device)
        {
            this.EnsureDevice(device);

            using (IAdbSocket socket = this.adbSocketFactory(this.EndPoint))
            {
                this.SetDevice(socket, device);
                socket.SendAdbRequest(request);
                var response = socket.ReadAdbResponse();

                // ADB will send some additional data
                byte[] buffer = new byte[1024];
                int    read   = socket.Read(buffer);

                var responseMessage = Encoding.UTF8.GetString(buffer, 0, read);

                // See https://android.googlesource.com/platform/system/core/+/master/adb/commandline.cpp#1026 (adb_root)
                // for more information on how upstream does this.
                if (!string.Equals(responseMessage, "restarting", StringComparison.OrdinalIgnoreCase))
                {
                    throw new AdbException(responseMessage);
                }
                else
                {
                    // Give adbd some time to kill itself and come back up.
                    // We can't use wait-for-device because devices (e.g. adb over network) might not come back.
                    Task.Delay(3000).GetAwaiter().GetResult();
                }
            }
        }
Exemple #2
0
        /// <include file='IAdbClient.xml' path='/IAdbClient/GetFrameBuffer/*'/>
        public async Task <Image> GetFrameBuffer(DeviceData device, CancellationToken cancellationToken)
        {
            using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint))
            {
                // Select the target device
                this.SetDevice(socket, device);

                // Send the framebuffer command
                socket.SendAdbRequest("framebuffer:");
                socket.ReadAdbResponse();

                // The result first is a FramebufferHeader object,
                var size       = Marshal.SizeOf(typeof(FramebufferHeader));
                var headerData = new byte[size];
                await socket.ReadAsync(headerData, cancellationToken).ConfigureAwait(false);

                var header = FramebufferHeader.Read(headerData);

                // followed by the actual framebuffer content
                var imageData = new byte[header.Size];
                socket.Read(imageData);

                // Convert the framebuffer to an image, and return that.
                return(header.ToImage(imageData));
            }
        }
Exemple #3
0
        /// <inheritdoc/>
        public void Install(DeviceData device, Stream apk, params string[] arguments)
        {
            this.EnsureDevice(device);

            if (apk == null)
            {
                throw new ArgumentNullException(nameof(apk));
            }

            if (!apk.CanRead || !apk.CanSeek)
            {
                throw new ArgumentOutOfRangeException(nameof(apk), "The apk stream must be a readable and seekable stream");
            }

            StringBuilder requestBuilder = new StringBuilder();

            requestBuilder.Append("exec:cmd package 'install' ");

            if (arguments != null)
            {
                foreach (var argument in arguments)
                {
                    requestBuilder.Append(" ");
                    requestBuilder.Append(argument);
                }
            }

            // add size parameter [required for streaming installs]
            // do last to override any user specified value
            requestBuilder.Append($" -S {apk.Length}");

            using (IAdbSocket socket = this.adbSocketFactory(this.EndPoint))
            {
                this.SetDevice(socket, device);

                socket.SendAdbRequest(requestBuilder.ToString());
                var response = socket.ReadAdbResponse();

                byte[] buffer = new byte[32 * 1024];
                int    read   = 0;

                while ((read = apk.Read(buffer, 0, buffer.Length)) > 0)
                {
                    socket.Send(buffer, read);
                }

                read = socket.Read(buffer);
                var value = Encoding.UTF8.GetString(buffer, 0, read);

                if (!string.Equals(value, "Success\n"))
                {
                    throw new AdbException(value);
                }
            }
        }
Exemple #4
0
        public override void ReadFrom(IAdbSocket socket)
        {
            this.Command = socket.ReadSyncResponse();

            var reply = new byte[4];

            socket.Read(reply);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(reply);
            }

            // The first 4 bytes contain the length of the data packet
            this.Size = BitConverter.ToInt32(reply, 0);
        }
Exemple #5
0
        public override void ReadFrom(IAdbSocket socket)
        {
            this.Command = socket.ReadSyncResponse();

            byte[] statResult = new byte[12];
            socket.Read(statResult);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(statResult, 0, 4);
                Array.Reverse(statResult, 4, 4);
                Array.Reverse(statResult, 8, 4);
            }

            this.FileMode = (UnixFileMode)BitConverter.ToInt32(statResult, 0);
            this.Size     = BitConverter.ToInt32(statResult, 4);
            this.Time     = DateTimeHelper.Epoch.AddSeconds(BitConverter.ToInt32(statResult, 8)).ToLocalTime();
        }