Esempio n. 1
0
        private void OpenUSBDevice(string devicePath)
        {
            SafeFileHandle deviceHandle = CreateFile(devicePath,
                                                     GENERIC_WRITE | GENERIC_READ,
                                                     FILE_SHARE_WRITE | FILE_SHARE_READ,
                                                     IntPtr.Zero,
                                                     OPEN_EXISTING,
                                                     FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
                                                     IntPtr.Zero);

            if (deviceHandle.IsInvalid)
            {
                WirekiteException.ThrowWin32Exception("Cannot open Wirekite device for communication");
            }

            IntPtr interfaceHandle = IntPtr.Zero;
            bool   success         = WinUsb_Initialize(deviceHandle, ref interfaceHandle);

            if (!success)
            {
                WirekiteException.ThrowWin32Exception("Cannot open USB interface of Wirekite device");
            }

            WirekiteDevice device = new WirekiteDevice(this, devicePath, deviceHandle, interfaceHandle);

            _devices.Add(device);
            if (_deviceNotification != null)
            {
                _deviceNotification.OnDeviceConnected(device);
            }
        }
Esempio n. 2
0
        private unsafe void SubmitReadRequest()
        {
            _rxBuffer = new byte[64];
            Overlapped        overlapped       = new Overlapped();
            NativeOverlapped *nativeOverlapped = overlapped.Pack(ReadComplete, _rxBuffer);
            bool   success;
            UInt32 numBytes;
            int    errorCode;

            fixed(byte *buf = _rxBuffer)
            {
                success   = WinUsb_ReadPipe(_interfaceHandle, RxEndpointAddress, buf, (UInt32)_rxBuffer.Length, out numBytes, nativeOverlapped);
                errorCode = Marshal.GetLastWin32Error();
            }

            if (success)
            {
                // call was executed synchronously
                ReadComplete(0, numBytes, nativeOverlapped);
            }
            else
            {
                if (errorCode != ERROR_IO_PENDING)
                {
                    Overlapped.Unpack(nativeOverlapped);
                    Overlapped.Free(nativeOverlapped);
                    WirekiteException.ThrowWin32Exception("Failed to submit read request for Wirekite device");
                }
            }
        }
Esempio n. 3
0
        private void FindDevices()
        {
            IntPtr deviceInfoSet = SetupDiGetClassDevs(ref _interfaceGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

            if (deviceInfoSet == INVALID_HANDLE_VALUE)
            {
                WirekiteException.ThrowWin32Exception("Failed to enumerate Wirekite devices");
            }

            try
            {
                for (UInt32 index = 0; true; index++)
                {
                    SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
                    deviceInterfaceData.Size = (UInt32)Marshal.SizeOf(deviceInterfaceData);
                    bool success = SetupDiEnumDeviceInterfaces(deviceInfoSet, IntPtr.Zero, ref _interfaceGuid, index, ref deviceInterfaceData);
                    if (!success)
                    {
                        int errorCode = Marshal.GetLastWin32Error();
                        if (errorCode == ERROR_NO_MORE_ITEMS)
                        {
                            break;
                        }
                        WirekiteException.ThrowWin32Exception("Failed to enumerate Wirekite devices");
                    }

                    IntPtr interfaceDetailData = Marshal.AllocHGlobal(1024);
                    try
                    {
                        Marshal.WriteInt32(interfaceDetailData, IntPtr.Size == 4 ? 4 + Marshal.SystemDefaultCharSize : 4 + 4);

                        SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA();
                        deviceInfoData.Size = (UInt32)Marshal.SizeOf(deviceInfoData);
                        UInt32 requiredSize = 0;
                        success = SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, interfaceDetailData, 1024, ref requiredSize, ref deviceInfoData);
                        if (!success)
                        {
                            WirekiteException.ThrowWin32Exception("Failed to get device information");
                        }

                        IntPtr devicePathPtr = IntPtr.Add(interfaceDetailData, 4);
                        string devicePath    = Marshal.PtrToStringUni(devicePathPtr);
                        OpenUSBDevice(devicePath);
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(interfaceDetailData);
                    }
                }
            }
            finally
            {
                SetupDiDestroyDeviceInfoList(deviceInfoSet);
            }
        }
Esempio n. 4
0
        private void WriteMessage(Message message)
        {
            //message.Dump();

            int messageSize = message.MessageSize;

            byte[] buffer = new byte[messageSize];
            message.Write(buffer, 0);

            unsafe
            {
                Overlapped        overlapped       = new Overlapped();
                NativeOverlapped *nativeOverlapped = overlapped.Pack(WriteComplete, buffer);
                bool   success;
                UInt32 numBytes;
                int    errorCode;
                fixed(byte *buf = buffer)
                {
                    success   = WinUsb_WritePipe(_interfaceHandle, TxEndpointAddress, buf, (UInt32)messageSize, out numBytes, nativeOverlapped);
                    errorCode = Marshal.GetLastWin32Error();
                }

                if (success)
                {
                    // call was executed synchronously
                    WriteComplete(0, numBytes, nativeOverlapped);
                }
                else
                {
                    if (errorCode != ERROR_IO_PENDING)
                    {
                        Overlapped.Unpack(nativeOverlapped);
                        Overlapped.Free(nativeOverlapped);
                        if (errorCode == 22)
                        {
                            return; // device was probably removed; broadcast will arrive later
                        }
                        WirekiteException.ThrowWin32Exception("Failed to submit write request to Wirekite device");
                    }
                }
            }
        }
Esempio n. 5
0
        private void RegisterUSBDeviceNotification(IntPtr windowHandle)
        {
            DEV_BROADCAST_DEVICEINTERFACE dbi = new DEV_BROADCAST_DEVICEINTERFACE
            {
                DeviceType = DBT_DEVTYP_DEVICEINTERFACE,
                Reserved   = 0,
                ClassGuid  = _interfaceGuid,
                Name       = 0
            };

            dbi.Size = (UInt32)Marshal.SizeOf(dbi);
            _notificationFilterBuffer = Marshal.AllocHGlobal((int)dbi.Size);
            Marshal.StructureToPtr(dbi, _notificationFilterBuffer, false);

            _notificationHandle = RegisterDeviceNotification(windowHandle, _notificationFilterBuffer, 0);
            if (_notificationHandle == INVALID_HANDLE_VALUE)
            {
                WirekiteException.ThrowWin32Exception("Registration of device notification failed");
            }
        }