title: Developing iTunes like application in c#
body: <p>I need to develop an application in c# that could automatically detect an iPhone when it is connected to the system and read a particular file for the iPhone file system. I basically want this file to be downloaded automatically from device to the PC. I used USBpcap tool that suggests that iTunes connects to phone using some XML format. Any help or insight greatly appreciated. Is there any documentation of Third party APIs that can get me started? There are some applications that can replicate iTunes functionality e.g <a href="http://www.copytrans.net/copytransmanager.php" rel="nofollow">Copytrans</a></p>
  
  <p>Is there any protocol or APIs provided by Apple?</p>
  
  <p>I have been digging the internet and found this link <a href="http://wikee.iphwn.org/usb%3ausbmux" rel="nofollow">Layered communication for iPhone</a>.
  Also I am using the LibUsbDotNet libraries for communicating to the usb device(<a href="http://libusbdotnet.sourceforge.net/V2/html/50a71afe-5cfa-4ec1-b416-092c79117272.htm" rel="nofollow">Example</a>). Can any one suggest which EndPoints should be used.</p>
  
  <p>It seems to me that I have to implement usbmuxd in windows application. It is a multilayer protocol. There must be some libraries that implement usbmuxd(I dont think I have to implement the protocol all by my self)</p>
  
  <p>I dont have much idea about iTunes communication as well as USB communication. I am adding as much information as I can(of course with the things I come up with in my R&amp;D). Any help is highly appreciated.</p>
  
  <pre><code>public static DateTime LastDataEventDate = DateTime.Now;
      public static UsbDevice MyUsbDevice;
  
      #region SET YOUR USB Vendor and Product ID!
  
      public static UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(1452, 4768);
  
      #endregion
  
      private void LibUSB()
      {
          ErrorCode ec = ErrorCode.None;
  
          try
          {
              // Find and open the usb device.
              MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
  
              // If the device is open and ready
              if (MyUsbDevice == null)
                  throw new Exception("Device Not Found.");
  
              // If this is a "whole" usb device (libusb-win32, linux libusb)
              // it will have an IUsbDevice interface. If not (WinUSB) the 
              // variable will be null indicating this is an interface of a 
              // device.
              IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
              if (!ReferenceEquals(wholeUsbDevice, null))
              {
                  // This is a "whole" USB device. Before it can be used, 
                  // the desired configuration and interface must be selected.
  
                  // Select config #1
                  wholeUsbDevice.SetConfiguration(1);
  
                  // Claim interface #0.
                  wholeUsbDevice.ClaimInterface(0);
              }
  
              // open read endpoint 1.
              UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep03);
  
              // open write endpoint 1.
              UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
  
  
                  int bytesWritten;
                  ec = writer.Write(usbmux_header.GetBytes(), 2000, out bytesWritten);
                  if (ec != ErrorCode.None)
                      throw new Exception(UsbDevice.LastErrorString);
  
                  byte[] readBuffer = new byte[1024];
                  while (ec == ErrorCode.None)
                  {
                      int bytesRead;
  
                      // If the device hasn't sent data in the last 100 milliseconds,
                      // a timeout error (ec = IoTimedOut) will occur. 
                      ec = reader.Read(readBuffer, 10000, out bytesRead);
  
                      if (ec == ErrorCode.Win32Error)
                          throw new Exception("port not open");
                      if (bytesRead == 0)
                          throw new Exception("No more bytes!");
  
                      // Write that output to the console.
                      Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));
                  }
  
          }
          catch (Exception ex)
          {
              Console.WriteLine();
              Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
          }
          finally
          {
              if (MyUsbDevice != null)
              {
                  if (MyUsbDevice.IsOpen)
                  {
                      // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                      // it exposes an IUsbDevice interface. If not (WinUSB) the 
                      // 'wholeUsbDevice' variable will be null indicating this is 
                      // an interface of a device; it does not require or support 
                      // configuration and interface selection.
                      IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                      if (!ReferenceEquals(wholeUsbDevice, null))
                      {
                          // Release interface #0.
                          wholeUsbDevice.ReleaseInterface(0);
                      }
  
                      MyUsbDevice.Close();
                  }
                  MyUsbDevice = null;
  
                  // Free usb resources
                  UsbDevice.Exit();
  
              }
          }
      }
  
  class usbmux_header
  {
      public static UInt32 length = 10;   // length of message, including header
      public static UInt32 reserved = 0;  // always zero
      public static UInt32 type = 3;       // message type
      public static UInt32 tag = 2;   // responses to this query will echo back this tag
  
      public static byte[] GetBytes()
      {
          byte[] lgth = BitConverter.GetBytes(length);
          byte[] res = BitConverter.GetBytes(reserved);
          byte[] tpe = BitConverter.GetBytes(type);
          byte[] tg = BitConverter.GetBytes(tag);
  
          byte[] retArray = new byte[16];
          lgth.CopyTo(retArray, 0);
          res.CopyTo(retArray, 4);
          tpe.CopyTo(retArray, 8);
          tg.CopyTo(retArray, 12);
  
          return retArray;
      }
  };
  </code></pre>
  
  <p>I have been trying to send hello packet bytes to iPhone but I am not able to read any response from phone.</p>