コード例 #1
0
        //TODO: Make async properly
        public async Task <byte[]> ReadAsync()
        {
            try
            {
                var byteBuffer = ByteBuffer.Allocate(ReadBufferLength);
                var request    = new UsbRequest();
                request.Initialize(_UsbDeviceConnection, _ReadEndpoint);
                request.Queue(byteBuffer, ReadBufferLength);
                await _UsbDeviceConnection.RequestWaitAsync();

                var buffers = new byte[ReadBufferLength];

                byteBuffer.Rewind();
                for (var i = 0; i < ReadBufferLength; i++)
                {
                    buffers[i] = (byte)byteBuffer.Get();
                }

                //Marshal.Copy(byteBuffer.GetDirectBufferAddress(), buffers, 0, ReadBufferLength);

                Tracer?.Trace(false, buffers);

                return(buffers);
            }
            catch (Exception ex)
            {
                Logger.Log(Helpers.ReadErrorMessage, ex, LogSection);
                throw new IOException(Helpers.ReadErrorMessage, ex);
            }
        }
コード例 #2
0
        public Task <TransferResult> ReadAsync(uint bufferLength, CancellationToken cancellationToken = default)
        {
            return(Task.Run(async() =>
            {
                try
                {
                    //TODO: validate here
                    var endpoint = ((AndroidUsbEndpoint)ReadEndpoint).UsbEndpoint;

                    using var logScope = Logger.BeginScope(
                              "UsbInterface: {usbInterface} Call: {call} Endpoint Id: {endpointId}",
                              UsbInterface.Id,
                              nameof(ReadAsync),
                              endpoint.EndpointNumber);
                    var byteBuffer = ByteBuffer.Allocate((int)bufferLength);
                    var request = _androidFactory.CreateUsbRequest();
                    _ = request.Initialize(_UsbDeviceConnection, endpoint);
#pragma warning disable CS0618
                    _ = request.Queue(byteBuffer, (int)bufferLength);
#pragma warning restore CS0618

                    _ = _timeout.HasValue
                        //Note: two versions here in case they have different functionality. When both code paths are tested it's probably possible to remove one
                        ? await _UsbDeviceConnection.RequestWaitAsync(_timeout.Value).ConfigureAwait(false)
                        : await _UsbDeviceConnection.RequestWaitAsync().ConfigureAwait(false);

                    //TODO: Get the actual length of the data read instead of just returning the length of the array

                    var buffers = new TransferResult(new byte[bufferLength], bufferLength);

                    _ = byteBuffer.Rewind();

                    //Ouch. Super nasty
                    for (var i = 0; i < bufferLength; i++)
                    {
                        buffers.Data[i] = (byte)byteBuffer.Get();
                    }

                    //Marshal.Copy(byteBuffer.GetDirectBufferAddress(), buffers, 0, ReadBufferLength);

                    Logger.LogDataTransfer(new Trace(false, buffers));

                    return buffers;
                }
                catch (Exception ex)
                {
                    Logger.LogError(Messages.ErrorMessageRead);
                    throw new IOException(Messages.ErrorMessageRead, ex);
                }
            }, cancellationToken));
        }
コード例 #3
0
        public async Task <ReadResult> ReadAsync(uint bufferLength)
        {
            return(await Task.Run(async() =>
            {
                try
                {
                    var byteBuffer = ByteBuffer.Allocate((int)bufferLength);
                    var request = new UsbRequest();
                    var endpoint = ((AndroidUsbEndpoint)ReadEndpoint).UsbEndpoint;
                    request.Initialize(_UsbDeviceConnection, endpoint);
#pragma warning disable CS0618
                    request.Queue(byteBuffer, (int)bufferLength);
#pragma warning restore CS0618
                    await _UsbDeviceConnection.RequestWaitAsync();

                    //TODO: Get the actual length of the data read instead of just returning the length of the array

                    var buffers = new ReadResult(new byte[bufferLength], bufferLength);

                    byteBuffer.Rewind();

                    //Ouch. Super nasty
                    for (var i = 0; i < bufferLength; i++)
                    {
                        buffers.Data[i] = (byte)byteBuffer.Get();
                    }

                    //Marshal.Copy(byteBuffer.GetDirectBufferAddress(), buffers, 0, ReadBufferLength);

                    Tracer?.Trace(false, buffers);

                    return buffers;
                }
                catch (Exception ex)
                {
                    Logger?.Log(Messages.ErrorMessageRead, nameof(AndroidUsbInterfaceManager), ex, LogLevel.Error);
                    throw new IOException(Messages.ErrorMessageRead, ex);
                }
            }));
        }