public static byte[] CreateConnectCommand() { var hostData = "host::features=stat_v2,shell_v2,cmd"; var packet = new AdbPacket(CNXN, 0x01000000, 4096, hostData); return(packet.ToBuffer()); }
public void HandlePacket(AdbPacket newPacket) { if (RemoteId == 0) { RemoteId = newPacket.arg1; } var okay = Command.CreateOkCommand(LocalId, RemoteId); _sock.Send(okay, okay.Length); _outputBlock.Post(newPacket.Data); }
private void OnPacket(AdbPacket packet) { _lastPacket = packet == null ? _lastPacket : packet; if (PrintMessages) { Console.WriteLine($"Received command {_lastPacket}"); } else { #if DEBUG Debug.WriteLine($"Received command {_lastPacket}"); #endif } if (_lastPacket.Command == Command.CNXN) { HostVersion = _lastPacket.arg1; HostMaxData = _lastPacket.arg2; State = ConnectionState.Connected; Connected?.Invoke(this, EventArgs.Empty); } else if (_lastPacket.Command == Command.OKAY) { var streamId = _lastPacket.arg2; if (OpenStreams.ContainsKey(streamId)) { var stream = OpenStreams[streamId]; stream.RemoteId = _lastPacket.arg1; //stream.HandlePacket(_lastPacket); } } else if (_lastPacket.Command == Command.WRTE) { var streamId = _lastPacket.arg2; if (OpenStreams.ContainsKey(streamId)) { var stream = OpenStreams[streamId]; stream.HandlePacket(_lastPacket); //Suspended = true; } } else if (_lastPacket.Command == Command.CLSE) { var streamId = _lastPacket.arg2; if (OpenStreams.ContainsKey(streamId)) { OpenStreams[streamId].Close(); OpenStreams.Remove(streamId); } } }
/// <summary> /// /// </summary> /// <param name="singleRun">Quit reading the packet after data has been received</param> /// <returns></returns> private async Task <AdbPacket> ReceivePacket(bool singleRun = false) { AdbPacket currentPacket = null; while (true) { while (Suspended) { Thread.Sleep(1); } if (_waitingForHeader) { byte[] header = new byte[24]; var readBytes = await _socket.ReceiveAsync(header, offset : 0, size : 24, socketFlags : SocketFlags.None, cancellationToken : _readCancellation); if (readBytes == 0) { break; } currentPacket = AdbPacket.FromBuffer(header); if (currentPacket.DataLength == 0) { OnPacket(currentPacket); } else { _waitingForHeader = false; } } else { Debug.Assert(currentPacket != null, nameof(currentPacket) + " != null"); currentPacket.Data = new byte[currentPacket.DataLength]; _socket.Read(currentPacket.Data); OnPacket(currentPacket); //Toggle back for header handling _waitingForHeader = true; if (singleRun) { break; } } } return(currentPacket); }
public static AdbPacket FromBuffer(byte[] buff) { uint command = Conversion.EndianBitConverter.Little.ToUInt32(buff, 0); uint arg1 = Conversion.EndianBitConverter.Little.ToUInt32(buff, 4); uint arg2 = Conversion.EndianBitConverter.Little.ToUInt32(buff, 8); uint dataLength = Conversion.EndianBitConverter.Little.ToUInt32(buff, 12); uint dataCRC = Conversion.EndianBitConverter.Little.ToUInt32(buff, 16); uint magic = Conversion.EndianBitConverter.Little.ToUInt32(buff, 20); var packet = new AdbPacket(command, arg1, arg2); packet.DataLength = dataLength; packet.DataCrc = dataCRC; var c = new byte[4] { buff[0], buff[1], buff[2], buff[3] }; packet.CommandName = Encoding.ASCII.GetString(c); return(packet); }
public static byte[] CreateOkCommand(uint localId, uint remoteId) { var packet = new AdbPacket(OKAY, localId, remoteId); return(packet.ToBuffer()); }
public static byte[] CreateWriteCommand(uint streamId, uint remoteStreamId, byte[] data) { var packet = new AdbPacket(WRTE, streamId, remoteStreamId, data); return(packet.ToBuffer()); }
public static byte[] CreateOpenCommand(string path, uint id) { var packet = new AdbPacket(OPEN, id, 0, path); return(packet.ToBuffer()); }