Recommended

Multicore community

 

Articles

Intel.com

Microsoft.co.il

 

Community

Microsoft Forums

Intel's Forum

Intel's Multicore Community

 

Resources

http://msdn.com/concurrency

Intel Multicore

NVidia Multicore GPU

 

Downloads

.Net Parallel Extensions

Intel's TBB

WinModules   

 

Tools

AsyncOp Logger

Intel thread analysis

Intel VTune

 

Contact

Asaf Shelly

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ichilov

-->

 

 

 

 

 

 
2 / 1
 
 
 
 
 
 
 
    // /  This HTML file was produced by the ProjectPublisher WebService, By Asaf Shelly                   / //
   // /    WebService URL is: http://Services.AsyncOp.com/ProjectPublisher/ProjectPublisherWSvc.asmx      / //
  // /    Learn more and find the source code here: http://AsyncOp.com                                   / //


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using WinUSBDeviceAPI;
using System.Drawing;

namespace WinUSBDevice
{
   [ToolboxBitmap(typeof(WinUSBDevice), "WinUSBDeviceIcon.ico")]
   public partial class WinUSBDevice : Component, WinUSBNotifyDevice
   {

      public WinUSBDevice()
      {
         InitializeComponent();
      }

      public WinUSBDevice(IContainer container)
      {
         container.Add(this);
         InitializeComponent();
      }

      ~WinUSBDevice()
      {
         if (NotificationWindow != null) WinUSBNotificationWindow.NotifierDestroy(ref NotificationWindow);
      }

      private WinUSBNotificationWindow NotificationWindow = null;
      private int pipeReadIndex = 0;
      private int pipeWriteIndex = 0;
      private Guid deviceGuid = Guid.Empty;
      private WinUSBDevice sharedDevice = null;

      public int PipeReadIndex
      {
         get { return pipeReadIndex; }
         set
         {
            if (value == pipeReadIndex) return;
            if (NotificationWindow != null) NotificationWindow.UnregisterReadCompletion((byte)pipeReadIndex, OnReadCompletion);
            pipeReadIndex = value;
            if (NotificationWindow != null) NotificationWindow.RegisterReadCompletion((byte)pipeReadIndex, OnReadCompletion);
         }
      }
      public int PipeWriteIndex
      {
         get { return pipeWriteIndex; }
         set 
         {
            if (value == pipeWriteIndex) return;
            if (NotificationWindow!=null) NotificationWindow.UnregisterWriteCompletion((byte)pipeWriteIndex, OnWriteCompletion);
            pipeWriteIndex = value;
            if (NotificationWindow != null) NotificationWindow.RegisterWriteCompletion((byte)pipeWriteIndex, OnWriteCompletion);
         }
      }

      public Guid DeviceGuid
      {
         get { return (deviceGuid); }
         set
         {
            if (this.DesignMode)
            {
               deviceGuid = value;
               sharedDevice = null;
            }
            else
            {
               if ((value != Guid.Empty) && (deviceGuid != value))
               {
                  sharedDevice = null;
                  if (NotificationWindow != null)
                  {
                     NotificationWindow.UnregisterReadCompletion((byte)pipeReadIndex, OnReadCompletion);
                     NotificationWindow.UnregisterWriteCompletion((byte)pipeWriteIndex, OnWriteCompletion);
                  }
                  if (deviceGuid != Guid.Empty) WinUSBNotificationWindow.NotifierDestroy(ref NotificationWindow);
                  deviceGuid = value;
                  NotificationWindow = WinUSBNotificationWindow.NotifierCreate(deviceGuid, this);
                  NotificationWindow.RegisterReadCompletion((byte)pipeReadIndex, OnReadCompletion);
                  NotificationWindow.RegisterWriteCompletion((byte)pipeWriteIndex, OnWriteCompletion);
               }
            }
         }
      }

      public WinUSBDevice SharedDevice
      {
         get { return sharedDevice; }
         set
         {
            if ((sharedDevice == value) || (this == value)) return;
            if (this.DesignMode)
            {
               sharedDevice = value;
               DeviceGuid = Guid.Empty;
            }
            else
            {
               if ((sharedDevice != null) && (null != value.NotificationWindow))
               {
                  if (NotificationWindow != null)
                  {
                     NotificationWindow.UnregisterReadCompletion((byte)pipeReadIndex, OnReadCompletion);
                     NotificationWindow.UnregisterWriteCompletion((byte)pipeWriteIndex, OnWriteCompletion);
                  }
                  if (deviceGuid != Guid.Empty)
                  {
                     WinUSBNotificationWindow.NotifierDestroy(ref NotificationWindow);
                  }
                  DeviceGuid = Guid.Empty;
                  sharedDevice = value;
                  NotificationWindow = sharedDevice.NotificationWindow;
                  NotificationWindow.RegisterReadCompletion((byte)pipeReadIndex, OnReadCompletion);
                  NotificationWindow.RegisterWriteCompletion((byte)pipeWriteIndex, OnWriteCompletion);
               }
            }
         }
      }


      public delegate void OnDeviceNotificationDelegate();
      public event OnDeviceNotificationDelegate DeviceAttach = null;
      public event OnDeviceNotificationDelegate DeviceDetach = null;

      private bool deviceAttached = false;

      public bool Attached
      {
         get { return deviceAttached; }
      }

      #region WinUSBNotifyDevice Members

      public void OnDeviceAttach()
      {
         deviceAttached = true;
         if (DesignMode) return;
         if ((NotificationWindow != null) && (NotificationWindow.WinUsbDevice != null))
         {
            NotificationWindow.WinUsbDevice.SetPipeTimeout((byte)pipeReadIndex, readTimeout);
            NotificationWindow.WinUsbDevice.SetPipeTimeout((byte)pipeWriteIndex, writeTimeout);
         }
         if (DeviceAttach != null) DeviceAttach.Invoke();
      }

      public void OnDeviceDetach()
      {
         deviceAttached = false;
         if (DesignMode) return;
         if (DeviceDetach != null) DeviceDetach.Invoke(); ;
      }

      #endregion

      public class ReadCompletionEventArgs : EventArgs { public byte[] buffer; public int lastErrorCode; public object userContext; }
      public class WriteCompletionEventArgs : EventArgs { public int byteCount; public int lastErrorCode; public object userContext; }

      public event ReadCompletionDelegate ReadCompletion = null;
      public event WriteCompletionDelegate WriteCompletion = null;

      void OnReadCompletion(byte[] buffer, int lastErrorCode, object userContext)
      {
         if (ReadCompletion!=null) ReadCompletion.Invoke(buffer, lastErrorCode, userContext);
      }
      void OnWriteCompletion(int byteCount, int lastErrorCode, object userContext)
      {
         if (WriteCompletion!=null) WriteCompletion.Invoke(byteCount, lastErrorCode, userContext);
      }

      public byte[] ReadBlocking(int byteCount)
      {
         if (null == NotificationWindow) return (new byte[0]);
         return (NotificationWindow.WinUsbDevice.ReadBlocking((byte)pipeReadIndex, byteCount));
      }

      public void Read(int byteCount)
      {
         if (null == NotificationWindow) { OnReadCompletion(new byte[0], 6/*ERROR_INVALID_HANDLE*/, null); return; }
         NotificationWindow.WinUsbDevice.Read((byte)pipeReadIndex, byteCount);
      }

      public int WriteBlocking(byte[] buffer)
      {
         if (null == NotificationWindow) return (0);
         return (NotificationWindow.WinUsbDevice.WriteBlocking((byte)pipeWriteIndex, buffer));
      }

      public void Write(byte[] buffer)
      {
         if (null == NotificationWindow) { OnWriteCompletion(0,6/*ERROR_INVALID_HANDLE*/,null); return; }
         NotificationWindow.WinUsbDevice.Write((byte)pipeWriteIndex, buffer);
      }

      uint readTimeout = 1000;
      uint writeTimeout = 0;

      public uint ReadTimeout
      {
         get { return readTimeout; }
         set
         {
            if (value == readTimeout) return;
            readTimeout = value;
            if ((NotificationWindow != null) && (NotificationWindow.WinUsbDevice != null))
            {
               NotificationWindow.WinUsbDevice.SetPipeTimeout((byte)pipeReadIndex, readTimeout);
            }
         }
      }

      public uint WriteTimeout
      {
         get { return writeTimeout; }
         set
         {
            if (value == writeTimeout) return;
            writeTimeout = value;
            if ((NotificationWindow != null) && (NotificationWindow.WinUsbDevice != null))
            {
               NotificationWindow.WinUsbDevice.SetPipeTimeout((byte)pipeWriteIndex, writeTimeout);
            }
         }
      }


   }
}