public static IDeviceFactory CreateAndroidUsbDeviceFactory(
     this FilterDeviceDefinition filterDeviceDefinition,
     UsbManager usbManager,
     Context context,
     ILoggerFactory loggerFactory = null,
     GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
     GetUsbInterfaceManager getUsbInterfaceManager = null,
     ushort?readBufferSize          = null,
     ushort?writeBufferSize         = null,
     IAndroidFactory androidFactory = null,
     Func <AndroidUsbDevice, IUsbPermissionBroadcastReceiver> getUsbPermissionBroadcastReceiver = null
     ) =>
 CreateAndroidUsbDeviceFactory(
     new ReadOnlyCollection <FilterDeviceDefinition>(new List <FilterDeviceDefinition> {
     filterDeviceDefinition
 }),
     usbManager,
     context,
     loggerFactory,
     getConnectedDeviceDefinitionsAsync,
     getUsbInterfaceManager,
     readBufferSize,
     writeBufferSize,
     androidFactory,
     getUsbPermissionBroadcastReceiver);
Example #2
0
 public static IDeviceFactory CreateWindowsUsbDeviceFactory(
     ILoggerFactory loggerFactory = null,
     GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
     GetUsbInterfaceManager getUsbInterfaceManager = null,
     Guid?classGuid         = null,
     ushort?readBufferSize  = null,
     ushort?writeBufferSize = null
     ) => CreateWindowsUsbDeviceFactory(
     new List <FilterDeviceDefinition>(),
     loggerFactory,
     getConnectedDeviceDefinitionsAsync,
     getUsbInterfaceManager,
     classGuid,
     readBufferSize,
     writeBufferSize);
Example #3
0
 public static IDeviceFactory CreateUwpUsbDeviceFactory(
     this FilterDeviceDefinition filterDeviceDefinitions,
     ILoggerFactory loggerFactory = null,
     GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
     GetUsbInterfaceManager getUsbInterfaceManager = null,
     ushort?readBufferSize  = null,
     ushort?writeBufferSize = null
     ) =>
 CreateUwpUsbDeviceFactory(
     new List <FilterDeviceDefinition> {
     filterDeviceDefinitions
 },
     loggerFactory,
     getConnectedDeviceDefinitionsAsync,
     getUsbInterfaceManager,
     readBufferSize,
     writeBufferSize);
 public static IDeviceFactory CreateUsbDeviceFactory(
     GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync,
     GetUsbInterfaceManager getUsbInterfaceManager,
     ILoggerFactory loggerFactory = null,
     Guid?classGuid = null)
 =>
 new DeviceFactory(
     loggerFactory,
     getConnectedDeviceDefinitionsAsync,
     async(d, cancellationToken) =>
 {
     var usbInterfaceManager = await getUsbInterfaceManager(d.DeviceId, cancellationToken).ConfigureAwait(false);
     return(new UsbDevice(d.DeviceId, usbInterfaceManager, loggerFactory));
 },
     //Support the device if the factory doesn't filter on class guid, or the filter matches the device
     (c, cancellationToken) => Task.FromResult(c.DeviceType == DeviceType.Usb && (classGuid == null || classGuid.Value == c.ClassGuid))
     );
Example #5
0
        public static IDeviceFactory CreateUwpUsbDeviceFactory(
            this IEnumerable <FilterDeviceDefinition> filterDeviceDefinitions,
            ILoggerFactory loggerFactory,
            GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
            GetUsbInterfaceManager getUsbInterfaceManager = null,
            ushort?readBufferSize  = null,
            ushort?writeBufferSize = null,
            Func <wde.DeviceInformation, bool> deviceInformationFilter = null,
            Func <windowsUsbDevice, SetupPacket, byte[], CancellationToken, Task <TransferResult> > performControlTransferAsync = null,
            IDataReceiver dataReceiver = null)
        {
            if (getConnectedDeviceDefinitionsAsync == null)
            {
                //Filter to by device Id.
                //TODO: There is surely a better way to do this
                deviceInformationFilter ??= d =>
                d.Id.Contains(@"\\?\usb", StringComparison.OrdinalIgnoreCase) &&
                d.Id.Contains(@"vid", StringComparison.OrdinalIgnoreCase) &&
                d.Id.Contains(@"pid", StringComparison.OrdinalIgnoreCase);

                var uwpHidDeviceEnumerator = new UwpDeviceEnumerator(
                    AqsHelpers.GetAqs(filterDeviceDefinitions, DeviceType.Usb),
                    DeviceType.Usb,
                    (d, cancellationToken) => Task.FromResult(new ConnectionInfo {
                    CanConnect = true
                }),
                    loggerFactory,
                    deviceInformationFilter);

                getConnectedDeviceDefinitionsAsync = uwpHidDeviceEnumerator.GetConnectedDeviceDefinitionsAsync;
            }

            getUsbInterfaceManager ??= (deviceId, cancellationToken) =>
            Task.FromResult <IUsbInterfaceManager>(
                new UwpUsbInterfaceManager(
                    //TODO: no idea if this is OK...
                    new ConnectedDeviceDefinition(deviceId, DeviceType.Usb),
                    dataReceiver,
                    performControlTransferAsync,
                    loggerFactory,
                    readBufferSize,
                    writeBufferSize));

            return(UsbDeviceFactoryExtensions.CreateUsbDeviceFactory(getConnectedDeviceDefinitionsAsync, getUsbInterfaceManager, loggerFactory));
        }
Example #6
0
        public static IDeviceFactory CreateWindowsUsbDeviceFactory(
            this IEnumerable <FilterDeviceDefinition> filterDeviceDefinitions,
            ILoggerFactory loggerFactory = null,
            GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
            GetUsbInterfaceManager getUsbInterfaceManager = null,
            Guid?classGuid         = null,
            ushort?readBufferSize  = null,
            ushort?writeBufferSize = null
            )
        {
            if (filterDeviceDefinitions == null)
            {
                throw new ArgumentNullException(nameof(filterDeviceDefinitions));
            }

            loggerFactory ??= NullLoggerFactory.Instance;

            if (getConnectedDeviceDefinitionsAsync == null)
            {
                var logger = loggerFactory.CreateLogger <WindowsDeviceEnumerator>();

                var uwpHidDeviceEnumerator = new WindowsDeviceEnumerator(
                    logger,
                    classGuid ?? WindowsDeviceConstants.WinUSBGuid,
                    (d, guid) => DeviceBase.GetDeviceDefinitionFromWindowsDeviceId(d, DeviceType.Usb, logger, guid),
                    c =>
                    Task.FromResult(!filterDeviceDefinitions.Any() || filterDeviceDefinitions.FirstOrDefault(f => f.IsDefinitionMatch(c, DeviceType.Usb)) != null));

                getConnectedDeviceDefinitionsAsync = uwpHidDeviceEnumerator.GetConnectedDeviceDefinitionsAsync;
            }

            getUsbInterfaceManager ??=
            (d, cancellationToken) =>
            Task.FromResult <IUsbInterfaceManager>(new WindowsUsbInterfaceManager
                                                   (
                                                       //TODO: no idea if this is OK...
                                                       d,
                                                       loggerFactory,
                                                       readBufferSize,
                                                       writeBufferSize
                                                   )
                                                   );

            return(UsbDeviceFactoryExtensions.CreateUsbDeviceFactory(getConnectedDeviceDefinitionsAsync, getUsbInterfaceManager, loggerFactory));
        }
        public static IDeviceFactory CreateAndroidUsbDeviceFactory(
            this IEnumerable <FilterDeviceDefinition> filterDeviceDefinitions,
            UsbManager usbManager,
            Context context,
            ILoggerFactory loggerFactory = null,
            GetConnectedDeviceDefinitionsAsync getConnectedDeviceDefinitionsAsync = null,
            GetUsbInterfaceManager getUsbInterfaceManager = null,
            ushort?readBufferSize          = null,
            ushort?writeBufferSize         = null,
            IAndroidFactory androidFactory = null,
            Func <AndroidUsbDevice, IUsbPermissionBroadcastReceiver> getUsbPermissionBroadcastReceiver = null
            )
        {
            if (usbManager == null)
            {
                throw new ArgumentNullException(nameof(usbManager));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            loggerFactory ??= NullLoggerFactory.Instance;

#if __ANDROID__
            androidFactory ??= new AndroidFactory();

            getUsbPermissionBroadcastReceiver ??= new Func <AndroidUsbDevice, IUsbPermissionBroadcastReceiver>((ud) =>
                                                                                                               new UsbPermissionBroadcastReceiver(
                                                                                                                   usbManager,
                                                                                                                   ud,
                                                                                                                   context,
                                                                                                                   androidFactory,
                                                                                                                   loggerFactory.CreateLogger <UsbPermissionBroadcastReceiver>()));
#else
            if (androidFactory == null)
            {
                throw new ArgumentNullException(nameof(androidFactory));
            }

            if (getUsbPermissionBroadcastReceiver == null)
            {
                throw new ArgumentNullException(nameof(getUsbPermissionBroadcastReceiver));
            }
#endif

            getConnectedDeviceDefinitionsAsync ??= (cancellationToken) =>
            {
                return(Task.FromResult <IEnumerable <ConnectedDeviceDefinition> >
                       (
                           new ReadOnlyCollection <ConnectedDeviceDefinition>
                           (
                               usbManager
                               .DeviceList
                               .Select(kvp => kvp.Value)
                               .Where
                               (
                                   d => filterDeviceDefinitions
                                   .FirstOrDefault
                                   (
                                       f =>
                                       (!f.VendorId.HasValue || f.VendorId.Value == d.VendorId) &&
                                       (!f.ProductId.HasValue || f.ProductId.Value == d.ProductId)
                                   ) != null
                               )
                               .Select(AndroidUsbInterfaceManager.GetAndroidDeviceDefinition)
                               .ToList()
                           )
                       ));
            };

            getUsbInterfaceManager ??= (a, cancellationToken) => Task.FromResult <IUsbInterfaceManager>(
                new AndroidUsbInterfaceManager(
                    usbManager,
                    //TODO: throw a validation message
                    int.Parse(a, IntParsingCulture),
                    androidFactory,
                    getUsbPermissionBroadcastReceiver,
                    loggerFactory,
                    readBufferSize,
                    writeBufferSize
                    ));

            return(UsbDeviceFactoryExtensions.CreateUsbDeviceFactory(getConnectedDeviceDefinitionsAsync, getUsbInterfaceManager, loggerFactory));
        }