Ejemplo n.º 1
0
        private static UsbCbiCommandResult ExecuteUfiCommand(IUsbCbiTransport cbi, int deviceAddress, byte[] command, string name, int dataLength)
        {
            var dataBuffer = new byte[dataLength];

            var result = cbi.ExecuteCommand(command, dataBuffer, 0, dataLength, UsbDataDirection.IN);

            /*if(!result.IsError && result.SenseData == null)
             * {
             *  Console.WriteLine("WTF??");
             *  return null;
             * }
             * else*/if (result.IsError || result.SenseData == null || result.SenseData[0] != 0)
            {
                Console.WriteLine($"*** {name}: {result.TransactionResult}");
                if (result.SenseData != null)
                {
                    Console.WriteLine($"Int endpoint: ASC = {result.SenseData[0]:X}h, ASCQ = {result.SenseData[1]:X}h");
                }

                return(null);
            }
            else
            {
                var data = dataBuffer.Take(result.TransferredDataCount).ToArray();
                if (data.Length == 0)
                {
                    Console.WriteLine($"> {name}: No data");
                }
                else
                {
                    PrintHexDump(data);
                }
                return(result);
            }
        }
        public static UsbCbiCommandResult ExecuteCommandWithRetry(this IUsbCbiTransport cbi, byte[] command, byte[] dataBuffer, int dataIndex, int dataLength, UsbDataDirection dataDirection, out bool mediaChanged, bool retryOnMediaChanged = true)
        {
            UsbCbiCommandResult result;

            mediaChanged = false;

            while (true)
            {
                result = cbi.ExecuteCommand(command, dataBuffer, dataIndex, dataLength, dataDirection);
                if (!result.IsError || result.SenseData == null)
                {
                    return(result);
                }

                if (result.SenseData == null)
                {
                    Debug.WriteLine("!!! No sense data on int endpoint!");
                }
                else
                {
                    Debug.WriteLine($"!!! ASC: {result.SenseData[0]:X2}h, ASCQ: {result.SenseData[1]:X2}h");
                }

                cbi.ExecuteCommand(requestSenseCommand, requestSenseBuffer, 0, 1, UsbDataDirection.IN);

                var asc  = result.SenseData[0];
                var ascq = result.SenseData[1];

                //4,1 = LOGICAL DRIVE NOT READY - BECOMING READY
                //4,FF = LOGICAL DRIVE NOT READY - DEVICE IS BUSY
                //28 = NOT READY TO READY TRANSITION - MEDIA CHANGED
                //28..2F = UNIT ATTENTION
                if ((asc < 0x28 || asc > 0x2F) && !(asc == 4 && (ascq == 1 || ascq == 0xFF)))
                {
                    return(result);
                }

                if (asc == 0x28)
                {
                    mediaChanged = true;
                    if (!retryOnMediaChanged)
                    {
                        return(result);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static UsbCbiCommandResult ExecuteUfiCommand(IUsbCbiTransport cbi, int deviceAddress, byte[] command, string name, byte[] dataToSend)
        {
            var result = cbi.ExecuteCommand(command, dataToSend, 0, dataToSend.Length, UsbDataDirection.OUT);

            if (result.IsError || result.SenseData[0] != 0)
            {
                Console.WriteLine($"*** {name}: {result.TransactionResult}");
                if (result.SenseData != null)
                {
                    Console.WriteLine($"Int endpoint: ASC = {result.SenseData[0]:X}h, ASCQ = {result.SenseData[1]:X}h");
                }

                return(null);
            }
            else
            {
                Console.WriteLine($">>> {name}: Ok, sent {dataToSend.Length} bytes");
                return(result);
            }
        }