コード例 #1
0
        private void OnHotPlug(KHOT_HANDLE hotHandle,
                               KLST_DEVINFO_HANDLE deviceInfo,
                               KLST_SYNC_FLAG plugType)
        {
            string plugText;

            int totalPluggedDeviceCount = (int)hotHandle.GetContext().ToInt64();

            if (totalPluggedDeviceCount == int.MaxValue)
            {
                Console.WriteLine("OnHotPlug is being called for the first time on handle:{0}", hotHandle.Pointer);
                totalPluggedDeviceCount = 0;
            }

            switch (plugType)
            {
            case KLST_SYNC_FLAG.ADDED:
                plugText = "Arrival";
                totalPluggedDeviceCount++;
                if (deviceInfo.DeviceID.Contains("VID_2DC4&PID_0200"))
                {
                    bool deviceFound = false;
                    setUI(HUD_STATE.Application);
                    DarwinDevice = new StmTestParameters(SIX15_VID, SIX15_PID, 0, 0x02, 512, null, -1, 4, 0);

                    if (DarwinDevice != null)
                    {
                        // Find and configure the device.
                        if (!DarwinDevice.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor))
                        {
                            DarwinDevice = new StmTestParameters(SIX15_VID, SIX15_PID, 0, 0x06, 128, null, -1, 4, 0);

                            if (!DarwinDevice.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor))
                            {
                                Console.WriteLine("Device not connected");
                                setUI(HUD_STATE.NotDetected);
                            }
                            else
                            {
                                Console.WriteLine("IS NEXT GEN FIRMWARE");
                                isNextGen   = true;
                                deviceFound = true;
                            }
                        }
                        else
                        {
                            deviceFound = true;
                        }

                        if (deviceFound)
                        {
                            if (DarwinDevice.TransferBufferSize == -1)
                            {
                                DarwinDevice.TransferBufferSize = pipeInfo.MaximumPacketSize * 512;
                            }

                            Console.WriteLine("Darwin Device Connected");

                            int[] pipeTimeoutMS = new[] { 0 };
                            usb.SetPipePolicy((byte)DarwinDevice.PipeId,
                                              (int)PipePolicyType.PIPE_TRANSFER_TIMEOUT,
                                              Marshal.SizeOf(typeof(int)),
                                              pipeTimeoutMS);
                        }
                    }
                }
                else if (deviceInfo.DeviceID.Contains("VID_0483&PID_5740"))
                {
                    setUI(HUD_STATE.Bootloader);
                    DarwinDevice = new StmTestParameters(STM_VID, STM_PID, 0, 0x01, 512, null, -1, 4, 0);

                    if (DarwinDevice != null)
                    {
                        // Find and configure the device.
                        if (!DarwinDevice.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor))
                        {
                            Console.WriteLine("Device not connected");
                            setUI(HUD_STATE.NotDetected);
                        }
                        else
                        {
                            if (DarwinDevice.TransferBufferSize == -1)
                            {
                                DarwinDevice.TransferBufferSize = pipeInfo.MaximumPacketSize * 512;
                            }

                            Console.WriteLine("Darwin Device Connected");

                            int[] pipeTimeoutMS = new[] { 0 };
                            usb.SetPipePolicy((byte)DarwinDevice.PipeId,
                                              (int)PipePolicyType.PIPE_TRANSFER_TIMEOUT,
                                              Marshal.SizeOf(typeof(int)),
                                              pipeTimeoutMS);
                        }
                    }
                }
                break;

            case KLST_SYNC_FLAG.REMOVED:
                plugText = "Removal";
                totalPluggedDeviceCount--;
                if (deviceInfo.DeviceID.Contains("VID_2DC4&PID_0200"))
                {
                    if (DarwinDevice != null)
                    {
                        if (isNextGen)
                        {
                            usb.FlushPipe(EP_DISP);
                        }
                        else
                        {
                            usb.FlushPipe(EP_CDC);
                        }

                        DarwinDevice.Free();
                        usb.Free();
                        DarwinDevice = null;
                        setUI(HUD_STATE.NotDetected);
                    }
                    else if (deviceInfo.DeviceID.Contains("VID_0483&PID_5740"))
                    {
                        usb.FlushPipe(EP_CDC_BL);
                        DarwinDevice.Free();
                        usb.Free();
                        DarwinDevice = null;
                        setUI(HUD_STATE.NotDetected);
                    }
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("plugType");
            }

            hotHandle.SetContext(new IntPtr(totalPluggedDeviceCount));

            Console.WriteLine("\n[OnHotPlug] Device {0}:{1} \n",
                              plugText,
                              deviceInfo);
            Console.WriteLine("Total Plugged Device Count: {0}",
                              totalPluggedDeviceCount);
        }