protected override object[] Refresh()
        {
            var paths = new List <string> ();

            Guid hidGuid;

            NativeMethods.HidD_GetHidGuid(out hidGuid);
            NativeMethods.HDEVINFO devInfo = NativeMethods.SetupDiGetClassDevs(hidGuid, null, IntPtr.Zero,
                                                                               /*NativeMethods.DIGCF.AllClasses |*/ NativeMethods.DIGCF.DeviceInterface | NativeMethods.DIGCF.Present);

            if (devInfo.IsValid)
            {
                try {
                    NativeMethods.SP_DEVICE_INTERFACE_DATA did = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
                    did.Size = Marshal.SizeOf(did);

                    for (int i = 0; NativeMethods.SetupDiEnumDeviceInterfaces(devInfo, IntPtr.Zero, hidGuid, i, ref did); i++)
                    {
                        NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA didetail = new NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA();
                        didetail.Size = IntPtr.Size == 8 ? 8 : (4 + Marshal.SystemDefaultCharSize);
                        if (NativeMethods.SetupDiGetDeviceInterfaceDetail(devInfo, ref did, ref didetail,
                                                                          Marshal.SizeOf(didetail) - (int)Marshal.OffsetOf(didetail.GetType(), "DevicePath"),
                                                                          IntPtr.Zero, IntPtr.Zero))
                        {
                            paths.Add(didetail.DevicePath);
                        }
                    }
                } finally {
                    NativeMethods.SetupDiDestroyDeviceInfoList(devInfo);
                }
            }

            return(paths.Cast <object> ().ToArray());
        }
示例#2
0
        void GetDevicePaths(uint devInst, Guid guid, List <string> devicePaths)
        {
            string deviceID;

            if (0 != NativeMethods.CM_Get_Device_ID(devInst, out deviceID))
            {
                return;
            }

            NativeMethods.HDEVINFO devInfo = NativeMethods.SetupDiGetClassDevs(
                guid, deviceID, IntPtr.Zero,
                NativeMethods.DIGCF.DeviceInterface | NativeMethods.DIGCF.Present
                );

            if (devInfo.IsValid)
            {
                try
                {
                    NativeMethods.SP_DEVINFO_DATA dvi = new NativeMethods.SP_DEVINFO_DATA();
                    dvi.Size = Marshal.SizeOf(dvi);

                    for (int j = 0; NativeMethods.SetupDiEnumDeviceInfo(devInfo, j, ref dvi); j++)
                    {
                        NativeMethods.SP_DEVICE_INTERFACE_DATA did = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
                        did.Size = Marshal.SizeOf(did);

                        for (int k = 0; NativeMethods.SetupDiEnumDeviceInterfaces(devInfo, ref dvi, guid, k, ref did); k++)
                        {
                            NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA didetail;
                            if (NativeMethods.SetupDiGetDeviceInterfaceDetail(devInfo, ref did, out didetail))
                            {
                                devicePaths.Add(didetail.DevicePath);
                            }
                        }
                    }
                }
                finally
                {
                    NativeMethods.SetupDiDestroyDeviceInfoList(devInfo);
                }
            }
        }
示例#3
0
        static void EnumerateDevices(Guid guid, EnumerateDevicesCallback callback)
        {
            NativeMethods.HDEVINFO devInfo = NativeMethods.SetupDiGetClassDevs(
                guid, null, IntPtr.Zero,
                NativeMethods.DIGCF.DeviceInterface | NativeMethods.DIGCF.Present
                );

            if (devInfo.IsValid)
            {
                try
                {
                    NativeMethods.SP_DEVINFO_DATA dvi = new NativeMethods.SP_DEVINFO_DATA();
                    dvi.Size = Marshal.SizeOf(dvi);

                    for (int j = 0; NativeMethods.SetupDiEnumDeviceInfo(devInfo, j, ref dvi); j++)
                    {
                        string deviceID;
                        if (0 != NativeMethods.CM_Get_Device_ID(dvi.DevInst, out deviceID))
                        {
                            continue;
                        }

                        NativeMethods.SP_DEVICE_INTERFACE_DATA did = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
                        did.Size = Marshal.SizeOf(did);

                        for (int i = 0; NativeMethods.SetupDiEnumDeviceInterfaces(devInfo, ref dvi, guid, i, ref did); i++)
                        {
                            NativeMethods.SP_DEVICE_INTERFACE_DETAIL_DATA didetail;
                            if (NativeMethods.SetupDiGetDeviceInterfaceDetail(devInfo, ref did, out didetail))
                            {
                                callback(devInfo, dvi, did, deviceID, didetail);
                            }
                        }
                    }
                }
                finally
                {
                    NativeMethods.SetupDiDestroyDeviceInfoList(devInfo);
                }
            }
        }
示例#4
0
        protected override string GetStreamPath(OpenConfiguration openConfig)
        {
            var service = GetService(openConfig);

            if (service == null)
            {
                throw DeviceException.CreateIOException(this, "BLE service not specified.");
            }
            if (service.Device != this)
            {
                throw DeviceException.CreateIOException(this, "BLE service is on a different device.");
            }

            uint devInst;

            if (0 == NativeMethods.CM_Locate_DevNode(out devInst, _id))
            {
                if (0 == NativeMethods.CM_Get_Child(out devInst, devInst))
                {
                    do
                    {
                        string serviceDeviceID;
                        if (0 == NativeMethods.CM_Get_Device_ID(devInst, out serviceDeviceID))
                        {
                            NativeMethods.HDEVINFO devInfo = NativeMethods.SetupDiGetClassDevs(
                                service.Uuid, serviceDeviceID, IntPtr.Zero,
                                NativeMethods.DIGCF.DeviceInterface | NativeMethods.DIGCF.Present
                                );

                            if (devInfo.IsValid)
                            {
                                try
                                {
                                    NativeMethods.SP_DEVINFO_DATA dvi = new NativeMethods.SP_DEVINFO_DATA();
                                    dvi.Size = Marshal.SizeOf(dvi);

                                    for (int j = 0; NativeMethods.SetupDiEnumDeviceInfo(devInfo, j, ref dvi); j++)
                                    {
                                        NativeMethods.SP_DEVICE_INTERFACE_DATA did = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
                                        did.Size = Marshal.SizeOf(did);

                                        for (int k = 0; NativeMethods.SetupDiEnumDeviceInterfaces(devInfo, ref dvi, service.Uuid, k, ref did); k++)
                                        {
                                            string devicePath;
                                            if (NativeMethods.SetupDiGetDeviceInterfaceDevicePath(devInfo, ref did, out devicePath))
                                            {
                                                // FIXME: Take the attribute handle into account as well.
                                                //        Right now, if there are multiple services with the same GUID, we do not distinguish between them.
                                                return(devicePath);
                                            }
                                        }
                                    }
                                }
                                finally
                                {
                                    NativeMethods.SetupDiDestroyDeviceInfoList(devInfo);
                                }
                            }
                        }
                    }while (0 == NativeMethods.CM_Get_Sibling(out devInst, devInst));
                }
            }

            throw DeviceException.CreateIOException(this, string.Format("BLE service {0} not found.", service.Uuid));
        }