public static byte[] ReadBuffer(this UsbK USB, ulong Size) { var data = new byte[Size]; USB.ReadPipe(ReadPipeId, data, (int)Size, out int _, IntPtr.Zero); return(data); }
public static void SendPayload(FileInfo info) { Writes = 0; byte[] payload = File.ReadAllBytes(info.FullName); var buf = new byte[0x10]; Device.ReadPipe(0x81, buf, 0x10, out _, IntPtr.Zero); WritePayload(Device, SwizzlePayload(payload)); if (Writes % 2 != 1) { Console.WriteLine("Switching buffers..."); Device.WritePipe(1, new byte[0x1000], 0x1000, out _, IntPtr.Zero); } var setup = new WINUSB_SETUP_PACKET { RequestType = 0x81, Request = 0, Value = 0, Index = 0, Length = 0x7000 }; Task.Run(() => Device.ControlTransfer(setup, new byte[0x7000], 0x7000, out var b, IntPtr.Zero)); }
protected override byte[] DriverRead() { int bytesRead, buffOffset; byte[] p = null; try { if (DriverAPI != null) { while (true) { bytesRead = 0; if (DriverAPI != null) { if (DriverAPI.ReadPipe(ReadPipeId, TempReadBuffer, TempReadBuffer.Length, out bytesRead, IntPtr.Zero)) { if (bytesRead > 0) { if (IsFTDI) { if (bytesRead <= 2) // It's just a status packet. { continue; } bytesRead -= 2; buffOffset = 2; } else { buffOffset = 0; } p = new byte[bytesRead]; Buffer.BlockCopy(TempReadBuffer, buffOffset, p, 0, bytesRead); if (Settings.DebugLevel > 7) { Log.d(TAG, "DriverRead Bytes:" + bytesRead + "\n"); } break; } } else { if (Settings.DebugLevel > 8) { Log.d(TAG, "DriverRead " + Error.Message() + "\n"); } } } if (IsInterrupted) { break; } } } } catch (Exception) { } return(p); }
public byte[] Read() { byte[] buffer = new byte[0x1000]; if (Device.ReadPipe(0x81, buffer, buffer.Length, out int length, IntPtr.Zero)) { byte[] data = new byte[length]; Array.Copy(buffer, data, length); return(data); } return(null); }
public bool RefreshCapture() { try { USB.ReadPipe(0x81, CaptureBlocks[0], CaptureBlocks[0].Length, out _, IntPtr.Zero); Array.Copy(CaptureBlocks[4], CaptureBlocks[5], CaptureBlocks[4].Length); Array.Copy(CaptureBlocks[3], CaptureBlocks[4], CaptureBlocks[3].Length); Array.Copy(CaptureBlocks[2], CaptureBlocks[3], CaptureBlocks[2].Length); Array.Copy(CaptureBlocks[1], CaptureBlocks[2], CaptureBlocks[1].Length); Array.Copy(CaptureBlocks[0], CaptureBlocks[1], CaptureBlocks[0].Length); ApplyRGBAInternal(CaptureBlocks[0]); } catch { return(false); } return(true); }
private bool RefreshCapture() { try { int aaaaa; USB.ReadPipe(0x81, CaptureBlocks[0], CaptureBlocks[0].Length, out aaaaa, IntPtr.Zero); ApplyRGBAInternal(CaptureBlocks[0]); } catch (Exception e) { return(false); } if (disconnected) { return(false); } else { return(true); } }
public bool RefreshCapture() { if (USB == null) { MessageBox.Show("Unable to connect to uLaunch via USB-C cable.", "Unable to connect"); Environment.Exit(Environment.ExitCode); } try { USB.ReadPipe(0x81, CaptureBlocks[0], CaptureBlocks[0].Length, out _, IntPtr.Zero); Array.Copy(CaptureBlocks[4], CaptureBlocks[5], CaptureBlocks[4].Length); Array.Copy(CaptureBlocks[3], CaptureBlocks[4], CaptureBlocks[3].Length); Array.Copy(CaptureBlocks[2], CaptureBlocks[3], CaptureBlocks[2].Length); Array.Copy(CaptureBlocks[1], CaptureBlocks[2], CaptureBlocks[1].Length); Array.Copy(CaptureBlocks[0], CaptureBlocks[1], CaptureBlocks[0].Length); ApplyRGBAInternal(CaptureBlocks[0]); } catch { return(false); } return(true); }
private int StmSubmitRead(KSTM_INFO info, KSTM_XFER_CONTEXT xferContext, int xferContextIndex, IntPtr nativeOverlapped) { int notUsedForAsync; int bufferSize = xferContext.BufferSize; bool success = mUsb.ReadPipe(mPipeId, xferContext.Buffer, bufferSize, out notUsedForAsync, nativeOverlapped); int errorCode = Marshal.GetLastWin32Error(); if (!success && errorCode == ErrorCodes.IoPending) { return(ErrorCodes.Success); } Debug.WriteLine( String.Format("Failed submitting transfer. PipeID:{0:X2}h ErrorCode:{1:X8}h", mPipeId, errorCode), "ERROR"); return(errorCode); }
public bool RefreshCapture() { try { USB.ReadPipe(0x81, CaptureBlocks[0], (int)USBPacketSize, out _, IntPtr.Zero); if (Mode == USBMode.Invalid) { var mode_raw = BitConverter.ToUInt32(CaptureBlocks[0], 0); Mode = (USBMode)mode_raw; switch (Mode) { case USBMode.RawRGBA: ApplyModeDelegate = ApplyRGBA; break; case USBMode.JPEG: ApplyModeDelegate = ApplyJPEG; break; default: break; } } Buffer.BlockCopy(CaptureBlocks[4], 0, CaptureBlocks[5], 0, (int)USBPacketSize); Buffer.BlockCopy(CaptureBlocks[3], 0, CaptureBlocks[4], 0, (int)USBPacketSize); Buffer.BlockCopy(CaptureBlocks[2], 0, CaptureBlocks[3], 0, (int)USBPacketSize); Buffer.BlockCopy(CaptureBlocks[1], 0, CaptureBlocks[2], 0, (int)USBPacketSize); Buffer.BlockCopy(CaptureBlocks[0], 0, CaptureBlocks[1], 0, (int)USBPacketSize); ApplyDataImpl(CaptureBlocks[0]); } catch { return(false); } return(true); }
public static byte[] ReadBytes(this UsbK USB, int Length) { byte[] b = new byte[Length]; USB.ReadPipe(0x81, b, Length, out _, IntPtr.Zero); return(b); }
public static void CommandHandleLoop(this UsbK USB) { Array.Clear(CommandBlockBuf, 0, CommandBlockLength); USB.ReadPipe(ReadPipeId, CommandBlockBuf, CommandBlockLength, out int readbuf, IntPtr.Zero); if (readbuf == 0) { return; } if (readbuf != CommandBlockLength) { Program.Warn.LogL($"Command block read size mismatch (expected: 0x{CommandBlockLength:X}, got 0x{readbuf:X}!)"); return; } using (var strm = new MemoryStream(CommandBlockBuf)) { using (var inblock = new BinaryReader(strm)) { var inmagic = inblock.ReadUInt32(); if (inmagic != Command.InputMagic) { Program.Warn.LogL($"Input magic mismatch (expected: 0x{Command.InputMagic:X}, got 0x{inmagic:X}!)"); return; } var command = inblock.ReadUInt32(); if (!Enum.IsDefined(typeof(CommandId), command)) { Program.Warn.LogL($"Invalid command Id (got {command}!)"); return; } CommandId cmd = (CommandId)command; Program.Log.LogL("Received command: " + cmd.ToString()); var outblockbuf = new byte[CommandBlockLength]; var bufs = new List <byte[]>(); using (var wstrm = new MemoryStream(outblockbuf)) { using (var outblock = new BinaryWriter(wstrm)) { bool out_written = false; void WriteOutBlockBase(uint Result) { if (out_written) { return; } out_written = true; outblock.Write(Command.OutputMagic); outblock.Write(Result); } switch (cmd) { case CommandId.GetDriveCount: { var drives = DriveInfo.GetDrives().Where(drive => drive.IsReady).ToList(); LastDrives = drives; WriteOutBlockBase(Command.ResultSuccess); outblock.Write((uint)drives.Count); break; } case CommandId.GetDriveInfo: { var driveidx = inblock.ReadUInt32(); var drives = LastDrives; if (LastDrives == null) { drives = DriveInfo.GetDrives().Where(drive => drive.IsReady).ToList(); LastDrives = drives; } if (driveidx >= drives.Count) { WriteOutBlockBase(Command.ResultInvalidInput); } else { var drive = drives[(int)driveidx]; WriteOutBlockBase(Command.ResultSuccess); string label = drive.VolumeLabel; string prefix = drive.Name.Substring(0, 1); if (string.IsNullOrEmpty(label) || label.IsUnicode()) { label = prefix; } outblock.WriteStringEx(label); outblock.WriteStringEx(prefix); outblock.Write((uint)drive.TotalFreeSpace); outblock.Write((uint)drive.TotalSize); } break; } case CommandId.StatPath: { var path = inblock.ReadStringEx().NormalizeAsPath(); uint type = 0; ulong filesize = 0; if (File.Exists(path)) { type = 1; filesize = (ulong)new FileInfo(path).Length; } else if (Directory.Exists(path)) { type = 2; } else { WriteOutBlockBase(Command.ResultInvalidInput); break; } WriteOutBlockBase(Command.ResultSuccess); outblock.Write(type); outblock.Write(filesize); break; } case CommandId.GetFileCount: { var path = inblock.ReadStringEx().NormalizeAsPath(); if (Directory.Exists(path)) { if ((LastFilesPath != path) || (LastFiles == null)) { LastFilesPath = path; LastFiles = Directory.GetFiles(path); } uint count = (uint)LastFiles.Length; WriteOutBlockBase(Command.ResultSuccess); outblock.Write(count); } else { WriteOutBlockBase(Command.ResultInvalidInput); } break; } case CommandId.GetFile: { var path = inblock.ReadStringEx().NormalizeAsPath(); var fileidx = inblock.ReadUInt32(); if (Directory.Exists(path)) { if ((LastFilesPath != path) || (LastFiles == null)) { LastFilesPath = path; LastFiles = Directory.GetFiles(path); } if (fileidx >= LastFiles.Length) { WriteOutBlockBase(Command.ResultInvalidInput); } else { var filename = Path.GetFileName(LastFiles[fileidx]); WriteOutBlockBase(Command.ResultSuccess); outblock.WriteStringEx(filename); } } else { WriteOutBlockBase(Command.ResultInvalidInput); } break; } case CommandId.GetDirectoryCount: { var path = inblock.ReadStringEx().NormalizeAsPath(); if (Directory.Exists(path)) { if ((LastDirectoriesPath != path) || (LastDirectories == null)) { LastDirectoriesPath = path; LastDirectories = Directory.GetDirectories(path); } uint count = (uint)LastDirectories.Length; WriteOutBlockBase(Command.ResultSuccess); outblock.Write(count); } else { WriteOutBlockBase(Command.ResultInvalidInput); } break; } case CommandId.GetDirectory: { var path = inblock.ReadStringEx().NormalizeAsPath(); var diridx = inblock.ReadUInt32(); if (Directory.Exists(path)) { if ((LastDirectoriesPath != path) || (LastDirectories == null)) { LastDirectoriesPath = path; LastDirectories = Directory.GetDirectories(path); } if (diridx >= LastDirectories.Length) { WriteOutBlockBase(Command.ResultInvalidInput); } else { var dirname = Path.GetFileName(LastDirectories[diridx]); WriteOutBlockBase(Command.ResultSuccess); outblock.WriteStringEx(dirname); } } else { WriteOutBlockBase(Command.ResultInvalidInput); } break; } case CommandId.ReadFile: { var path = inblock.ReadStringEx().NormalizeAsPath(); var offset = inblock.ReadUInt64(); var size = inblock.ReadUInt64(); try { if (string.IsNullOrEmpty(LastRFile) || (LastRFile != path)) { if (LastReadFile != null) { LastReadFile.Close(); LastReadFile = null; } LastRFile = path; LastReadFile = new BinaryReader(new FileStream(LastRFile, FileMode.Open, FileAccess.Read)); } LastReadFile.BaseStream.Seek((long)offset, SeekOrigin.Begin); byte[] data = new byte[size]; var rbytes = LastReadFile.Read(data, 0, (int)size); if (rbytes > 0) { WriteOutBlockBase(Command.ResultSuccess); outblock.Write((ulong)rbytes); bufs.Add(data); } } catch { WriteOutBlockBase(Command.ResultInvalidInput); break; } break; } case CommandId.WriteFile: { var path = inblock.ReadStringEx().NormalizeAsPath(); var size = inblock.ReadUInt64(); var data = USB.ReadBuffer(size); try { if (string.IsNullOrEmpty(LastWFile) || (LastWFile != path)) { if (LastWriteFile != null) { LastWriteFile.Close(); LastWriteFile = null; } LastWFile = path; LastWriteFile = new FileStream(path, FileMode.Append, FileAccess.Write); } LastWriteFile.Write(data, 0, (int)size); WriteOutBlockBase(Command.ResultSuccess); } catch { WriteOutBlockBase(Command.ResultInvalidInput); break; } break; } case CommandId.Create: { var type = inblock.ReadUInt32(); var path = inblock.ReadStringEx().NormalizeAsPath(); switch (type) { case 1: { try { File.Create(path); WriteOutBlockBase(Command.ResultSuccess); } catch { WriteOutBlockBase(Command.ResultInvalidInput); } break; } case 2: { try { Directory.CreateDirectory(path); WriteOutBlockBase(Command.ResultSuccess); } catch { WriteOutBlockBase(Command.ResultInvalidInput); } break; } default: { WriteOutBlockBase(Command.ResultInvalidInput); break; } } break; } case CommandId.Delete: { var type = inblock.ReadUInt32(); var path = inblock.ReadStringEx().NormalizeAsPath(); switch (type) { case 1: { try { File.Delete(path); WriteOutBlockBase(Command.ResultSuccess); } catch { WriteOutBlockBase(Command.ResultInvalidInput); } break; } case 2: { try { Directory.Delete(path, true); WriteOutBlockBase(Command.ResultSuccess); } catch { WriteOutBlockBase(Command.ResultInvalidInput); } break; } default: { WriteOutBlockBase(Command.ResultInvalidInput); break; } } break; } case CommandId.Rename: { var type = inblock.ReadUInt32(); var path = inblock.ReadStringEx().NormalizeAsPath(); var pathnew = inblock.ReadStringEx().NormalizeAsPath(); switch (type) { case 1: { try { Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(path, pathnew); WriteOutBlockBase(Command.ResultSuccess); } catch { WriteOutBlockBase(Command.ResultInvalidInput); } break; } case 2: { try { Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(path, pathnew); WriteOutBlockBase(Command.ResultSuccess); } catch { WriteOutBlockBase(Command.ResultInvalidInput); } break; } default: { WriteOutBlockBase(Command.ResultInvalidInput); break; } } break; } case CommandId.GetSpecialPathCount: { WriteOutBlockBase(Command.ResultSuccess); outblock.Write((uint)SpecialPathList.Count); break; } case CommandId.GetSpecialPath: { var spathidx = inblock.ReadUInt32(); if (spathidx >= SpecialPathList.Count) { WriteOutBlockBase(Command.ResultInvalidInput); } else { var spath = SpecialPathList.ElementAt((int)spathidx); var path = Environment.GetFolderPath(spath.Value).NormalizeAsGoldleafPath(); WriteOutBlockBase(Command.ResultSuccess); outblock.WriteStringEx(spath.Key); outblock.WriteStringEx(path); } break; } case CommandId.SelectFile: { try { var openfd = new OpenFileDialog { Filter = "Any file (*.*)|*.*", Title = "Select file for Goldleaf", Multiselect = false }; if (openfd.ShowDialog() == DialogResult.OK) { WriteOutBlockBase(Command.ResultSuccess); var path = openfd.FileName.NormalizeAsGoldleafPath(); outblock.WriteStringEx(path); } else { WriteOutBlockBase(Command.ResultInvalidInput); } } catch { WriteOutBlockBase(Command.ResultInvalidInput); } break; } } } } USB.WritePipe(WritePipeId, outblockbuf, CommandBlockLength, out int writtenbuf, IntPtr.Zero); if (bufs.Any()) { foreach (var buf in bufs) { USB.WriteBuffer(buf); } } if (writtenbuf != CommandBlockLength) { Program.Command.LogL($"Command block write size mismatch (expected: 0x{CommandBlockLength:X}, got 0x{writtenbuf:X}!)"); } } } }