void UploadFile(SharpAdbClient.DeviceData device, string fileLocal, string fileRemote) { //var device = AdbClient.Instance.GetDevices().First(); try { AdbClient adbClient = new AdbClient(); try { adbClient.Connect(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)); adbClient.ExecuteRemoteCommand("mkdir " + Constants.remotePath, device, new myReceiver()); }catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } using (SyncService service = new SyncService(new AdbSocket(adbClient.EndPoint), device)) using (Stream stream = File.OpenRead(fileLocal)) { service.Push(stream, fileRemote, 444, DateTime.Now, null, CancellationToken.None); } textBox1.Text = "File send successfully"; } catch (Exception ex) { textBox1.Text = ex.Message + " Is MTP enabled?"; } }
public void GetVersionTest() { VersionInfoReceiver receiver = new VersionInfoReceiver(); Assert.AreEqual<int>(10210, (int)receiver.GetVersionCode("versionCode=10210 targetSdk=18")); Assert.AreEqual<object>(null, receiver.GetVersionCode(null)); Assert.AreEqual<object>(null, receiver.GetVersionCode(string.Empty)); Assert.AreEqual<object>(null, receiver.GetVersionCode("versionCode=10210targetSdk=18")); Assert.AreEqual<string>("4.7.1", (string)receiver.GetVersionName(" versionName=4.7.1")); Assert.AreEqual<string>(null, (string)receiver.GetVersionName(null)); Assert.AreEqual<string>(null, (string)receiver.GetVersionName(" test")); Assert.AreEqual<string>(null, (string)receiver.GetVersionName(" versionName")); Assert.AreEqual<string>(string.Empty, (string)receiver.GetVersionName(" versionName=")); DeviceData device = new DeviceData(); var dumpsys = string.Join(Environment.NewLine, File.ReadAllLines(@"dumpsys_package.txt")); StringReader reader = new StringReader(dumpsys); while (reader.Peek() >= 0) { receiver.AddOutput(reader.ReadLine()); } receiver.Flush(); Assert.AreEqual(10210, receiver.VersionInfo.VersionCode); Assert.AreEqual("4.7.1", receiver.VersionInfo.VersionName); }
void DownloadFile(SharpAdbClient.DeviceData device, string fileLocal, string fileRemote) { //var device = AdbClient.Instance.GetDevices().First(); try { using (SyncService service = new SyncService(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)), device)) using (Stream stream = File.OpenWrite(fileLocal)) { service.Pull(fileRemote, stream, null, CancellationToken.None); } textBox1.Text = "File loaded OK"; }catch (Exception ex) { textBox1.Text = ex.Message; } }
/// <summary> /// Executes a shell command on the remote device /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="command">The command to execute</param> /// <param name="device">The device to execute on</param> /// <param name="rcvr">The shell output receiver</param> public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver rcvr) { try { client.ExecuteRemoteCommand(command, device, rcvr, CancellationToken.None, int.MaxValue); } catch (AggregateException ex) { if (ex.InnerExceptions.Count == 1) { throw ex.InnerException; } else { throw; } } }
public SyncService(DeviceData device) { throw new NotImplementedException(); }
/// <inheritdoc/> public void Root(DeviceData device) { this.Root("root:", device); }
/// <summary> /// Initializes a new instance of the <see cref="DeviceDataEventArgs"/> class. /// </summary> /// <param name="device">The device.</param> public DeviceDataEventArgs(DeviceData device) { this.Device = device; }
/// <summary> /// Executes a shell command on the remote device /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="command">The command to execute</param> /// <param name="device">The device to execute on</param> /// <param name="receiver">The shell output receiver</param> /// <param name="encoding">The encoding to use.</param> public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver receiver, Encoding encoding) { try { client.ExecuteRemoteCommandAsync(command, device, receiver, CancellationToken.None, int.MaxValue, encoding).Wait(); } catch (AggregateException ex) { if (ex.InnerExceptions.Count == 1) { throw ex.InnerException; } else { throw; } } }
/// <inheritdoc/> public Task ExecuteRemoteCommandAsync(string command, DeviceData device, IShellOutputReceiver receiver, CancellationToken cancellationToken, int maxTimeToOutputResponse) { return(this.ExecuteRemoteCommandAsync(command, device, receiver, cancellationToken, maxTimeToOutputResponse, Encoding)); }
/// <summary> /// Initializes a new instance of the <see cref="SyncService"/> class. /// </summary> /// <param name="device"> /// The device on which to interact with the files. /// </param> public SyncService(DeviceData device) : this(Factories.AdbSocketFactory(AdbServer.Instance.EndPoint), device) { }
/// <summary> /// Forwards a remote Unix socket to a local TCP socket. /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="device"> /// The device to which to forward the connections. /// </param> /// <param name="localPort"> /// The local port to forward. /// </param> /// <param name="remoteSocket"> /// The remote Unix socket. /// </param> /// <exception cref="AdbException"> /// The client failed to submit the forward command. /// </exception> /// <exception cref="AdbException"> /// The device rejected command. The error message will include the error message provided by the device. /// </exception> public static int CreateForward(this IAdbClient client, DeviceData device, int localPort, string remoteSocket) { return(client.CreateForward(device, $"tcp:{localPort}", $"local:{remoteSocket}", true)); }
/// <inheritdoc/> public void RemoveAllForwards(DeviceData device) { this.EnsureDevice(device); using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint)) { socket.SendAdbRequest($"host-serial:{device.Serial}:killforward-all"); var response = socket.ReadAdbResponse(); } }
/// <inheritdoc/> public IEnumerable<ForwardData> ListForward(DeviceData device) { this.EnsureDevice(device); using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint)) { socket.SendAdbRequest($"host-serial:{device.Serial}:list-forward"); var response = socket.ReadAdbResponse(); var data = socket.ReadString(); var parts = data.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); return parts.Select(p => ForwardData.FromString(p)); } }
/// <inheritdoc/> public void CreateForward(DeviceData device, ForwardSpec local, ForwardSpec remote, bool allowRebind) { this.CreateForward(device, local?.ToString(), remote?.ToString(), allowRebind); }
/// <inheritdoc/> public void RemoveForward(DeviceData device, int localPort) { this.EnsureDevice(device); using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint)) { socket.SendAdbRequest($"host-serial:{device.Serial}:killforward:tcp:{localPort}"); var response = socket.ReadAdbResponse(); } }
/// <inheritdoc/> public void CreateForward(DeviceData device, string local, string remote, bool allowRebind) { this.EnsureDevice(device); using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint)) { string rebind = allowRebind ? string.Empty : "norebind:"; socket.SendAdbRequest($"host-serial:{device.Serial}:forward:{rebind}{local};{remote}"); var response = socket.ReadAdbResponse(); } }
/// <inheritdoc/> public void SetDevice(IAdbSocket socket, DeviceData device) { // if the device is not null, then we first tell adb we're looking to talk // to a specific device if (device != null) { socket.SendAdbRequest($"host:transport:{device.Serial}"); try { var response = socket.ReadAdbResponse(); } catch (AdbException e) { if (string.Equals("device not found", e.AdbError, StringComparison.OrdinalIgnoreCase)) { throw new DeviceNotFoundException(device.Serial); } else { throw; } } } }
/// <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); } }
/// <inheritdoc/> public void ExecuteRemoteCommand(string command, DeviceData device, IShellOutputReceiver receiver, CancellationToken cancellationToken, int maxTimeToOutputResponse) { this.EnsureDevice(device); using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint)) { cancellationToken.Register(() => socket.Close()); this.SetDevice(socket, device); socket.SendAdbRequest($"shell:{command}"); var response = socket.ReadAdbResponse(); try { using (StreamReader reader = new StreamReader(socket.GetShellStream(), Encoding)) { // Previously, we would loop while reader.Peek() >= 0. Turns out that this would // break too soon in certain cases (about every 10 loops, so it appears to be a timing // issue). Checking for reader.ReadLine() to return null appears to be much more robust // -- one of the integration test fetches output 1000 times and found no truncations. while (!cancellationToken.IsCancellationRequested) { var line = reader.ReadLine(); if (line == null) { break; } if (receiver != null) { receiver.AddOutput(line); } } } } catch (Exception e) { // If a cancellation was requested, this main loop is interrupted with an exception // because the socket is closed. In that case, we don't need to throw a ShellCommandUnresponsiveException. // In all other cases, something went wrong, and we want to report it to the user. if (!cancellationToken.IsCancellationRequested) { throw new ShellCommandUnresponsiveException(e); } } finally { if (receiver != null) { receiver.Flush(); } } } }
/// <summary> /// Executes a shell command on the remote device /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="command">The command to execute</param> /// <param name="device">The device to execute on</param> /// <param name="rcvr">The shell output receiver</param> public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver rcvr, CancellationToken ct) { ExecuteRemoteCommand(client, command, device, rcvr, AdbClient.Encoding, ct); }
/// <inheritdoc/> public IEnumerable<LogEntry> RunLogService(DeviceData device, params LogId[] logNames) { this.EnsureDevice(device); // The 'log' service has been deprecated, see // https://android.googlesource.com/platform/system/core/+/7aa39a7b199bb9803d3fd47246ee9530b4a96177 using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint)) { this.SetDevice(socket, device); StringBuilder request = new StringBuilder(); request.Append("shell:logcat -B"); foreach (var logName in logNames) { request.Append($" -b {logName.ToString().ToLower()}"); } socket.SendAdbRequest(request.ToString()); var response = socket.ReadAdbResponse(); using (Stream stream = socket.GetShellStream()) using (LogReader reader = new LogReader(stream)) { while (true) { LogEntry entry = null; try { entry = reader.ReadEntry(); } catch (EndOfStreamException) { // This indicates the end of the stream; the entry will remain null. } if (entry != null) { yield return entry; } else { break; } } } } }
/// <summary> /// Reboots the specified adb socket address. /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="device">The device.</param> public static void Reboot(this IAdbClient client, DeviceData device) { client.Reboot(string.Empty, device); }
/// <inheritdoc/> public void Reboot(string into, DeviceData device) { this.EnsureDevice(device); var request = $"reboot:{into}"; using (IAdbSocket socket = Factories.AdbSocketFactory(this.EndPoint)) { this.SetDevice(socket, device); socket.SendAdbRequest(request); var response = socket.ReadAdbResponse(); } }
/// <summary> /// Executes a shell command on the remote device /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="command">The command to execute</param> /// <param name="device">The device to execute on</param> /// <param name="receiver">The shell output receiver</param> public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver receiver) { ExecuteRemoteCommand(client, command, device, receiver, AdbClient.Encoding); }
/// <summary> /// Throws an <see cref="ArgumentNullException"/> if the <paramref name="device"/> /// parameter is <see langword="null"/>, and a <see cref="ArgumentOutOfRangeException"/> /// if <paramref name="device"/> does not have a valid serial number. /// </summary> /// <param name="device"> /// A <see cref="DeviceData"/> object to validate. /// </param> protected void EnsureDevice(DeviceData device) { if (device == null) { throw new ArgumentNullException(nameof(device)); } if (string.IsNullOrEmpty(device.Serial)) { throw new ArgumentOutOfRangeException(nameof(device), "You must specific a serial number for the device"); } }
/// <inheritdoc/> public int CreateForward(DeviceData device, ForwardSpec local, ForwardSpec remote, bool allowRebind) { return(this.CreateForward(device, local?.ToString(), remote?.ToString(), allowRebind)); }
/// <inheritdoc/> public Task ExecuteRemoteCommandAsync(string command, DeviceData device, IShellOutputReceiver receiver, CancellationToken cancellationToken) { return(this.ExecuteRemoteCommandAsync(command, device, receiver, Encoding, cancellationToken)); }
/// <inheritdoc/> public Framebuffer CreateRefreshableFramebuffer(DeviceData device) { this.EnsureDevice(device); return(new Framebuffer(device, this)); }
/// <inheritdoc/> public void Unroot(DeviceData device) { this.Root("unroot:", device); }
/// <summary> /// Executes a shell command on the remote device /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="command">The command to execute</param> /// <param name="device">The device to execute on</param> /// <param name="rcvr">The shell output receiver</param> public static void ExecuteRemoteCommand(this IAdbClient client, string command, DeviceData device, IShellOutputReceiver rcvr) { client.ExecuteRemoteCommand(command, device, rcvr, CancellationToken.None, int.MaxValue).Wait(); }
public Device(DeviceData data) : this(data.Serial, data.State, data.Model, data.Product, data.Name) { }
/// <summary> /// Creates a port forwarding between a local and a remote port. /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="device"> /// The device to which to forward the connections. /// </param> /// <param name="localPort"> /// The local port to forward. /// </param> /// <param name="remotePort"> /// The remote port to forward to /// </param> /// <exception cref="AdbException"> /// failed to submit the forward command. /// or /// Device rejected command: + resp.Message /// </exception> public static void CreateForward(this IAdbClient client, DeviceData device, int localPort, int remotePort) { client.CreateForward(device, $"tcp:{localPort}", $"tcp:{remotePort}", true); }
private void btnSend_Click(object sender, EventArgs e) { SharpAdbClient.DeviceData device = (SharpAdbClient.DeviceData)cboListDevices.SelectedItem; UploadFile(device, _filename, Constants.remoteFile); }
/// <summary> /// Initializes a new instance of the <see cref="SyncService"/> class. /// </summary> /// <param name="socket"> /// A <see cref="IAdbSocket"/> that enables to connection with the /// adb server. /// </param> /// <param name="device"> /// The device on which to interact with the files. /// </param> public SyncService(IAdbSocket socket, DeviceData device) { this.Socket = socket; this.Device = device; this.Open(); }
/// <summary> /// Creates a reversed port forwarding between a local and a remote port. /// </summary> /// <param name="client"> /// An instance of a class that implements the <see cref="IAdbClient"/> interface. /// </param> /// <param name="device"> /// The device to which to forward the connections. /// </param> /// <param name="localPort"> /// The local device port to forward. /// </param> /// <param name="remotePort"> /// The remote host port to forward to /// </param> /// <exception cref="AdbException"> /// failed to submit the reverse command. /// or /// Device rejected command: + resp.Message /// </exception> public static bool CreateReverse(this IAdbClient client, DeviceData device, int localPort, int remotePort) { return(client.CreateReverse(device, $"tcp:{localPort}", $"tcp:{remotePort}", true)); }