Ejemplo n.º 1
0
 //==================================================================================================================
 /// <summary>
 /// Virtual method for getting a list of DeviceInfos
 /// </summary>
 /// <param name="deviceInfoList">The list of devices</param>
 /// <param name="deviceInfoList">A flag indicating if the device list should be refreshed</param>
 //==================================================================================================================
 internal override ErrorCodes GetDevices(Dictionary<int, DeviceInfo> deviceInfoList, DeviceListUsage deviceListUsage)
 {
     return GetUsbDevices(deviceInfoList, deviceListUsage);
 }
Ejemplo n.º 2
0
 //==================================================================================================================
 /// <summary>
 /// Virtual method for getting a list of DeviceInfos
 /// </summary>
 /// <param name="deviceInfoList">The list of devices</param>
 /// <param name="deviceInfoList">A flag indicating if the device list should be refreshed</param>
 //==================================================================================================================
 internal abstract ErrorCodes GetUsbDevices(Dictionary<int, DeviceInfo> deviceInfoList, DeviceListUsage deviceListUsage);
Ejemplo n.º 3
0
 //======================================================================================================================
 /// <summary>
 /// Overrides abstract method in base class
 /// </summary>
 /// <param name="deviceInfoList">The list of devices</param>
 /// <param name="deviceInfoList">A flag indicating if the device list should be refreshed</param>
 //======================================================================================================================
 internal override ErrorCodes GetUsbDevices(Dictionary<int, DeviceInfo> deviceInfoList, DeviceListUsage deviceListUsage)
 {
     System.Diagnostics.Debug.Assert(false, "GetUsbDevices not implemented in SynchronousUsbInterop");
     return ErrorCodes.MethodRequiresImplementation;
 }
Ejemplo n.º 4
0
 //=========================================================================================================================
 /// <summary>
 /// Virtual method for getting a list of DeviceInfos
 /// </summary>
 /// <param name="deviceInfoList">The list of devices</param>
 /// <param name="deviceInfoList">A flag indicating if the device list should be refreshed</param>
 //=========================================================================================================================
 internal override ErrorCodes GetUsbDevices(Dictionary<int, DeviceInfo> deviceInfoList, DeviceListUsage deviceListUsage)
 {
     System.Diagnostics.Debug.Assert(false, "GetUsbDevices must be implemented in a derived class");
     return ErrorCodes.MethodRequiresImplementation;
 }
Ejemplo n.º 5
0
        //===================================================================================================================
        /// <summary>
        /// Fills a list with usb device information
        /// </summary>
        /// <param name="deviceInfoList">The list of devices</param>
        /// <param name="deviceInfoList">A flag indicating if the device list should be refreshed</param>
        //====================================================================================================================
        internal override ErrorCodes GetUsbDevices(Dictionary<int, DeviceInfo> deviceInfoList, DeviceListUsage deviceListUsage)
        {
            if (deviceListUsage == DeviceListUsage.UpdateList)
                return UpdateDeviceInfoList(deviceInfoList);

            bool lastDevice = true;
            string[] pathParts = null;
            int deviceNumber = 0;
            string devicePath = string.Empty;
            int sizeOfDeviceInterfaceData;
            ErrorCodes errorCode = ErrorCodes.NoErrors;

            m_setupDeviceInfo = IntPtr.Zero;

            Guid deviceGuid = new Guid(DEVICE_INTERFACE_GUID);

            if (deviceListUsage == DeviceListUsage.RefreshList)
                deviceInfoList.Clear();

            if (deviceInfoList.Count == 0)
            {
                // get a handle to the device information set using the device GUID
                m_setupDeviceInfo = SetupDiGetClassDevs(ref deviceGuid, null, 0, (int)(UsbDeviceConfigInfoFlags.Present | UsbDeviceConfigInfoFlags.DeviceInterface));

                if (m_setupDeviceInfo != IntPtr.Zero)
                {
                    // initialize the device interface data
                    if (sizeof(IntPtr) == 4)
                        sizeOfDeviceInterfaceData = 28;
                    else
                        sizeOfDeviceInterfaceData = 32;

                    IntPtr deviceInterfaceData = Marshal.AllocHGlobal(sizeOfDeviceInterfaceData);
                    Marshal.WriteInt32(deviceInterfaceData, sizeOfDeviceInterfaceData);

                    int memberIndex = 0;

                    do
                    {
                        DeviceInfo di = new DeviceInfo();

                        // get a handle to the device interface data structure
                        bool result = SetupDiEnumDeviceInterfaces(m_setupDeviceInfo, 0, ref deviceGuid, memberIndex, deviceInterfaceData);

                        if (result == true)
                        {
                            lastDevice = false;

                            int requiredLength = 0;

                            // first call to GetDeviceInterfaceDetail will return the required length for the detail buffer
                            result = SetupDiGetDeviceInterfaceDetail(m_setupDeviceInfo, deviceInterfaceData, IntPtr.Zero, 0, ref requiredLength, IntPtr.Zero);

                            if (requiredLength > 0)
                            {
                                // allocate the detail buffer using the required length
                                IntPtr detailBuffer = Marshal.AllocHGlobal(requiredLength);

                                if (detailBuffer != IntPtr.Zero)
                                {
                                    // set the size
                                    if (sizeof(IntPtr) == 4)
                                        Marshal.WriteInt32(detailBuffer, 4 + Marshal.SystemDefaultCharSize);
                                    else
                                        Marshal.WriteInt32(detailBuffer, 8);

                                    result = SetupDiGetDeviceInterfaceDetail(m_setupDeviceInfo, deviceInterfaceData, detailBuffer, requiredLength, ref requiredLength, IntPtr.Zero);

                                    if (result == true)
                                    {
                                        // set the device path name ptr (detailBuffer[4])
                                        char* pPathName = (char*)((byte*)detailBuffer.ToPointer() + 4);

                                        // copy unmanaged string contents to managed string
                                        devicePath = new String(pPathName);
                                        pathParts = devicePath.Split(new char[] { '#' });

                                        // set the device info properties
                                        di.DevicePath = devicePath;
                                        di.DeviceNumber = deviceNumber;
                                        di.Vid = GetVid(pathParts[1]);
                                        di.Pid = GetPid(pathParts[1]);
                                        di.DisplayName = DaqDeviceManager.GetDeviceName(di.Pid);
                                        di.SerialNumber = pathParts[2];

                                        if (Enum.IsDefined(typeof(DeviceIDs), di.Pid))
                                        {
                                            deviceInfoList.Add(deviceNumber, di);
                                            deviceNumber++;
                                        }

                                        memberIndex++;
                                    }
                                }
                                else
                                {
                                    lastDevice = true;
                                    errorCode = ErrorCodes.DetailBufferIsNull;
                                }

                                // free the unmanaged memory pointed to by detailBuffer
                                Marshal.FreeHGlobal(detailBuffer);
                            }
                            else
                            {
                                lastDevice = true;
                                errorCode = ErrorCodes.RequiredLengthIsZero;
                            }
                        }
                        else
                        {
                            lastDevice = true;
                        }

                    } while (!lastDevice);

                    // free the unmanaged memory pointed to by detailBuffer
                    Marshal.FreeHGlobal(deviceInterfaceData);

                    SetupDiDestroyDeviceInfoList(m_setupDeviceInfo);
                }
            }

            return errorCode;
        }
Ejemplo n.º 6
0
        public static string[] GetDeviceNames(DeviceNameFormat format, DeviceListUsage deviceListUsage)
        {
            m_nameFormat = format;

            if (deviceListUsage == DeviceListUsage.UpdateList)
                return GetUnusedDeviceNames(format);

            if (m_daqDeviceList.Count > 0)
                throw new DaqException(ErrorMessages.DaqDeviceListNotEmpty, ErrorCodes.DaqDeviceListNotEmpty);

            PlatformInterop platformInterop = PlatformInterop.GetUsbPlatformInterop();

            if (platformInterop != null)
            {
                if (m_deviceInfoList.Count == 0 || 
                        m_deviceNames.Count == 0 || 
                            deviceListUsage == DeviceListUsage.RefreshList ||
                                deviceListUsage == DeviceListUsage.UpdateList)
                {
                    /* get a list of detected devices */
                    ErrorCodes er = platformInterop.GetDevices(m_deviceInfoList, deviceListUsage);
    				
                    /* create a DeviceInfo object for the virtual device */
                    //AddVirtualDeviceInfo();

				    if (er == ErrorCodes.LibusbCouldNotBeInitialized)
                        throw new DaqException(ErrorMessages.LibusbCouldNotBeInitialized, er);
    				
				    if (er == ErrorCodes.LibusbCouldNotBeLoaded)
                        throw new DaqException(ErrorMessages.LibusbCouldNotBeLoaded, er);

				    if (er == ErrorCodes.LibUsbGetDeviceDescriptorFailed)
                        throw new DaqException(ErrorMessages.LibUsbGetDeviceDescriptorFailed, er);
                }

                m_deviceNames.Clear();

				foreach (KeyValuePair<int, DeviceInfo> kvp in m_deviceInfoList)
                {
                    DeviceInfo di = kvp.Value;

                    di.DeviceID = platformInterop.GetDeviceID(kvp.Value);

                    if (Environment.OSVersion.Platform != PlatformID.Win32NT)
                        di.SerialNumber = platformInterop.GetSerno(kvp.Value);

                    if (m_nameFormat == DeviceNameFormat.NameOnly)
                    {
                        m_deviceNames.Add(di.DisplayName);
                    }
                    else if (m_nameFormat == DeviceNameFormat.NameAndSerno)
                    {
                        m_deviceNames.Add(String.Format("{0}{1}{2}", di.DisplayName, Constants.DEVICE_NAME_SEPARATOR, di.SerialNumber));
                    }
                    else if (m_nameFormat == DeviceNameFormat.NameAndID)
                    {
                        if (di.DeviceID != String.Empty)
                        {
                            m_deviceNames.Add(String.Format("{0}{1}{2}", di.DisplayName, Constants.DEVICE_NAME_SEPARATOR, di.DeviceID));
                        }
                    }
                    else if (m_nameFormat == DeviceNameFormat.NameSernoAndID)
                    {
                        if (di.DeviceID != String.Empty)
                        {
                            m_deviceNames.Add(String.Format("{0}{1}{2}{3}{4}", di.DisplayName, Constants.DEVICE_NAME_SEPARATOR, di.SerialNumber, Constants.DEVICE_NAME_SEPARATOR, di.DeviceID));
                        }
                    }
                }
            }
            else
            {
                throw new DaqException(ErrorMessages.PlatformNotSupported, ErrorCodes.PlatformNotSupported);
            }


            return m_deviceNames.ToArray();
        }
Ejemplo n.º 7
0
        //=======================================================================================================================
        /// <summary>
        /// Fills a list with usb device information
        /// </summary>
        /// <param name="deviceInfoList">The list of devices</param>
        /// <param name="deviceInfoList">A flag indicating if the device list should be refreshed</param>
        //=======================================================================================================================
        internal override ErrorCodes GetUsbDevices(Dictionary<int, DeviceInfo> deviceInfoList, DeviceListUsage deviceListUsage)
        {
            Monitor.Enter(m_deviceChangeLock);

            // initialize lib_usb
            int result;

            try
            {
                result = LibUsbInterop.LibUsbInit(IntPtr.Zero);

                if (result != 0)
                    m_errorCode = ErrorCodes.LibusbCouldNotBeInitialized;
            }
            catch (Exception ex)
            {
                if (ex is DllNotFoundException)
                {
                    m_errorCode = ErrorCodes.LibusbCouldNotBeLoaded;
                }
                else
                {
                    m_errorCode = ErrorCodes.LibusbCouldNotBeInitialized;
                }

                return m_errorCode;
            }

            int** pDevices = null;
            int devCount = LibUsbInterop.LibUsbGetDeviceList(IntPtr.Zero, &pDevices);

            deviceNumber = 0;

            if (devCount > 0)
            {
                for (int i = 0; i < devCount; i++)
                {
                    IntPtr pDevice = new IntPtr(pDevices[i]);

                    IntPtr pDesc = Marshal.AllocHGlobal(18);
                    result = LibUsbInterop.LibUsbGetDeviceDescriptor(pDevice, pDesc);

                    if (result != 0)
                    {
                        Monitor.Exit(m_deviceChangeLock);
                        return ErrorCodes.LibUsbGetDeviceDescriptorFailed;
                    }

                    UsbDeviceDescriptor udd = new UsbDeviceDescriptor(pDesc);

                    if (udd.vendorID == VID)
                    {
                        DeviceInfo di = new DeviceInfo();

                        di.UsbDevicePtr = pDevice;
                        di.Vid = udd.vendorID;
                        di.Pid = udd.deviceID;
                        di.SerialNumber = udd.serialNumber.ToString();
                        di.MaxPacketSize = udd.maxPacketSize;
                        di.DisplayName = DaqDeviceManager.GetDeviceName(di.Pid);

                        //if (Enum.IsDefined(typeof(SupportedDevices), di.Pid))
                        if (DaqDeviceManager.IsSupportedDevice(di.Pid))
                        {
                            deviceInfoList.Add(deviceNumber, di);
                            deviceNumber++;
                        }
                    }

                    Marshal.FreeHGlobal(pDesc);
                }
            }

            Monitor.Exit(m_deviceChangeLock);

            return ErrorCodes.NoErrors;
        }