private static string GetErrorMessage(Error errorCode) { if (MonoUsbApi.ErrorCodeFromLibUsbError((int)errorCode, out string errorMessage) == Main.ErrorCode.Success) { return(errorMessage); } else { return($"An unknown error with code {(int)errorCode} has occurred."); } }
/// <summary> /// Wait for the transfer to complete, timeout, or get cancelled. /// </summary> /// <param name="transferredCount">The number of bytes transferred on <see cref="ErrorCode.Success"/>.</param> /// <param name="cancel">Not used for libusb-1.0. Transfers are always cancelled on timeout or error.</param> /// <returns><see cref="ErrorCode.Success"/> if the transfer completes successfully, otherwise one of the other <see cref="ErrorCode"/> codes.</returns> public override ErrorCode Wait(out int transferredCount, bool cancel) { transferredCount = 0; int ret = 0; MonoUsbError monoError; ErrorCode ec; int iWait = WaitHandle.WaitAny(new WaitHandle[] { mTransferCompleteEvent, mTransferCancelEvent }, Timeout.Infinite, UsbConstants.EXIT_CONTEXT); switch (iWait) { case 0: // TransferCompleteEvent if (mTransfer.Status == MonoUsbTansferStatus.TransferCompleted) { transferredCount = mTransfer.ActualLength; return(ErrorCode.Success); } string s; monoError = MonoUsbApi.MonoLibUsbErrorFromTransferStatus(mTransfer.Status); ec = MonoUsbApi.ErrorCodeFromLibUsbError((int)monoError, out s); UsbError.Error(ErrorCode.MonoApiError, (int)monoError, "Wait:" + s, EndpointBase); return(ec); case 1: // TransferCancelEvent ret = (int)mTransfer.Cancel(); bool bTransferComplete = mTransferCompleteEvent.WaitOne(100, UsbConstants.EXIT_CONTEXT); mTransferCompleteEvent.Set(); if (ret != 0 || !bTransferComplete) { ec = ret == 0 ? ErrorCode.CancelIoFailed : ErrorCode.MonoApiError; UsbError.Error(ec, ret, String.Format("Wait:Unable to cancel transfer or the transfer did not return after it was cancelled. Cancelled:{0} TransferCompleted:{1}", (MonoUsbError)ret, bTransferComplete), EndpointBase); return(ec); } return(ErrorCode.IoCancelled); default: // Critical failure timeout mTransfer.Cancel(); ec = ((EndpointBase.mEpNum & (byte)UsbCtrlFlags.Direction_In) > 0) ? ErrorCode.ReadFailed : ErrorCode.WriteFailed; mTransferCompleteEvent.Set(); UsbError.Error(ec, ret, String.Format("Wait:Critical timeout failure! The transfer callback function was not called within the allotted time."), EndpointBase); return(ec); } }