Recent

Author Topic: Linux USB communication with iOS Device  (Read 19322 times)

aidv

  • Full Member
  • ***
  • Posts: 173
Linux USB communication with iOS Device
« on: October 09, 2015, 02:01:27 am »
Hey guys.

I am doing some research on how to establish communication between Linux and an iOS device via USB.

I came across a cool project called PeerTalk (https://github.com/rsms/peertalk) which is an XCode project that allows communicaton between an iOS device an an OSX app.

I searched some more regarding if there is any PeerTalk project for Linux, and I came across this blog post: http://www.dailyack.com/2012/08/peertalk-and-beaglebone.html

and this blog post:
http://www.dailyack.com/2012/08/blinking-beaglebones-heartbeat-led-from.html

As it seems, they are using libusb and usbmuxd to talk with their iOS device from Linux using Python.

I then found this project: http://www.libimobiledevice.org/

I am trying to figure out how to do all of this with Lazarus.

Has anyone done anything similar before?

The reason I need to do this is because my product that is under heavy development needs to be set-up
by the customer.

There are several options for me to choose between, and I want to use the most straight forward way to set-up my
product just to make it super easy for my customers.

My options are Ethernet, Bluetooth, USB and Ultrasound/audible sound (yes, ultrasound. It's a quite interesting approach actually...)

I do not choose ethernet because the user would need to be near a computer, which is unnecessary since the product is
completely independent.

I do not choose bluetooth because that component would be useless for anything else other than setup,
and would obviously just add unnecessary cost.

I do not choose Ultrasound/audible sound because then a microphone would be an unnecessary component and add cost.

So what I choose is USB because the product will have a USB connector anyways and everyone has a USB cable for their iPhone, quite obvious.

If anyone find this particular question interesting feel free to help me solve this problem,
or if you guys want, a few of us could get together and solve it together, and I'll pay you a small amount of money
or I could donate some money to any organization on the behalf of the Lazarus/FPC community.

My project is coming to an end and I just need to finish some of the final coding. Yaay.

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Linux USB communication with iOS Device
« Reply #1 on: October 09, 2015, 05:17:42 am »
Ok I've found some more info.

In this post (http://stackoverflow.com/questions/7278177/usb-communication-between-ipad-and-mac-or-pc/12949721#12949721) the answer shows the following code:

Code: C  [Select][+][-]
  1. truct sockaddr_un endpoint;
  2. size_t size;
  3.  
  4. _usbMuxSocket = socket(PF_LOCAL, SOCK_STREAM, 0);
  5.  
  6. endpoint.sun_family = AF_LOCAL;
  7. strncpy(endpoint.sun_path, "/var/run/usbmuxd", 17);
  8. size = (offsetof (struct sockaddr_un, sun_path)
  9.         + strlen (endpoint.sun_path) + 1);
  10.  
  11. connect(_usbMuxSocket, (struct sockaddr *) &endpoint, size);

I tried to convert it to pascal as shown below:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Sockets;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Memo1: TMemo;
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     procedure Log(str: string);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   addr: sockaddr_un;
  27.   S : Longint;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. procedure TForm1.Log(str: string);
  34. begin
  35.   memo1.lines.add(str);
  36. end;
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   S := fpSocket(PF_LOCAL, SOCK_STREAM, 0);
  43.   log('fpSocket = ' + inttostr(S));
  44.  
  45.   addr.sun_family := AF_LOCAL;
  46.   addr.sun_path := '/var/run/usbmuxd';
  47.  
  48.   log('fpConnect = ' + inttostr(fpConnect(S, @addr, StrLen(addr.sun_path) )));
  49. end;
  50.  
  51. end.
  52.  

The log tells me that "fSocket" returns 12, and it does so regardless if the iPhone is connected or not.
And "fpConnect" always returns -1, which means it has not connected.

I think that I have missed something.

When I go to "/var/run/" there's always a file present with the name "usbmuxd", however, when I connect my iPhone a new file pops up out of nowhere which is called "usbmuxd.pid"

I have tried to change "addr.sun_path" to "/usr/run/usbmuxd.pid/" as well, but fpConnect still gives me -1.

Now notice that the variable "addr" is of type "sockaddr_un" while fpConnect only accepts the type "psockaddr",
so i put an "@" symbol in front of addr, but I have also tried "psockaddr(@addr)" and many other combinations.

The reason I am using sockaddr_un is because it is the only type that is similar to that of the code in the SO post
and has the "path" entry.

Can anybody give me some insight?

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: Linux USB communication with iOS Device
« Reply #2 on: October 09, 2015, 09:05:10 am »
Do you have enough permissions ?
 
udev-rule:
Code: Text  [Select][+][-]
  1. SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device",SYSFS{idVendor}=="05ac" , SYSFS{idProduct}=="1297", MODE="0666"

Perform a Google search on usbmuxd permissions, and you will find a lot of hints (also because you have an automatic usbmuxd daemon staring up somehow I think).
« Last Edit: October 09, 2015, 09:13:58 am by DonAlfredo »

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Linux USB communication with iOS Device
« Reply #3 on: October 09, 2015, 01:06:48 pm »
According to this post (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=572867) the permission issue was a bug in usbmuxd 1.0.0-2,

I have usbmuxd 1.0.9.

However I have tried my code again running from the terminal from SU, but I still get "-1" on fpConnect but fpSocket now returns 8 instead of 12 when running from SU.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: Linux USB communication with iOS Device
« Reply #4 on: October 09, 2015, 01:32:33 pm »
Another try then:
Please look into /dev to see if an usb file handle is available for your device.
Check its permissions.
Do a lsusb to find it and to get more info

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Linux USB communication with iOS Device
« Reply #5 on: October 09, 2015, 01:45:30 pm »
I have looked, there are usb file handles, 3 of them actually, and one of them are the for the iPhone.

I looked inside "/dev/bus/usb/001/", inside are the files 001, 002, and 003.
003 which is the iPhone.

lsusb returns all three devices, one is Apple iPhone.

How do I check its permission?
Nevermind, I checked, they are all read/write.

Edit:

I am trying to get pas-LibUSB up and running to try to access the device that way.
When trying to compile my project with "LibUSB" and "USB" in the uses clause I get the following errors:

Code: Pascal  [Select][+][-]
  1. /usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
  2. /usr/bin/ld.bfd: cannot find -lusb
  3. project1.lpr(20,1) Error: Error while linking

What do I do?
« Last Edit: October 09, 2015, 01:48:45 pm by aidv »

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: Linux USB communication with iOS Device
« Reply #6 on: October 09, 2015, 02:37:47 pm »
Did you install libusb ?

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Linux USB communication with iOS Device
« Reply #7 on: October 09, 2015, 06:43:56 pm »
Isn't it already installed?

I try both apt-get install libusb and aptitude install libusb but they both seem to be up to date.

Or have I missed something?

Edit: I installed LibUSB 1.0 by writing
Code: Pascal  [Select][+][-]
  1. apt-get install libusb-1.0

I still get the same error in Lazarus.

What about the GNU linker, isn't it what ld.bfd belongs to?

Maybe I should install that instead?
« Last Edit: October 09, 2015, 07:16:50 pm by aidv »

benohb

  • Full Member
  • ***
  • Posts: 213
Re: Linux USB communication with iOS Device
« Reply #8 on: October 10, 2015, 02:52:37 am »
Is there a file /dev/ttyUSB0 when  brought the device ??

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Linux USB communication with iOS Device
« Reply #9 on: October 10, 2015, 04:06:40 am »
No however, upon reading more about UBSMUXD, what seems to happen is that USBMUXD channels USB device traffic to a the network, so that the Host (the iPhone) will run the PeerTalk app and host on 127.0.0.1 at port 2345 (for example), and the client app (on linux, windows, osx...) should be able to connect to connect to the phone via TCP.

It really seems to be very high level and easy to access, BUT, I according to some instructions, I need to install
LibUSB 1.0.3+, UBSMUXD 1.1.0 and iFuse 1.1.3.

I am on Debian 8 Jessie, and when I apt-get or aptitude, I only seem to get old packages of those components.

Could anybody tell me how to install those packages?

I downloaded them directly from: http://www.libimobiledevice.org/

which has all that is needed in order to get full access to everything on an iOS device.

When I ran the python script mentioned before, I ran it in "foreground" mode which showed me exactly what was happening, and what happened was that USBMUXD kept finding my iPhone, and then instantly removed it from its own list of devices, which causes the python script to return an "item out of range" error.
Basically it was telling me that there are no devices available, which is False, because there are.

Upon researching some more, I found this post: https://bbs.archlinux.org/viewtopic.php?id=169611
and at the bottom the OP says that by installing the latest version of iFuse the problem was fixed.

So yeah, in summary:
By installing all the dependencies found at http://www.libimobiledevice.org/ the problem should be solved,
and I (and you) should be able to directly communicate with an iOS device via USB using the PeerTalk library.

So anybody knows how to install downloaded packages?
They are not actual package files, but folders with a lot of stuff in them. I'm still kind of new to Linux.

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1739
Re: Linux USB communication with iOS Device
« Reply #10 on: October 10, 2015, 06:44:12 pm »
Well, I just check Arch Linux (running on my RPi2).
I has all the recent packages you need.

So, if you are able to run Arch on your machine, then you would be able to test all you want with all the recent packages.

Besides, I am not very into Debian, but perhaps somebody else can point you in the direction of getting the most recent software.

Arch Bang is a very nice Arch distro, that gives you a GUI.

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Linux USB communication with iOS Device
« Reply #11 on: October 10, 2015, 08:16:16 pm »
Thanks for the info. I managed to install the latest LibUSB 1.0.9 and the latest USBMUXD 1.1.0.

I also installed the latest version of "fuse", "iFuse", "util-linux", "libplist", python and libpython-dev.

I also changed my Virtual Box VM setting so that it supports USB3.0.

Now when I run USBMUXD with -f -v -v options the device seems to initiated properly and it stays connected and now I get the following output:

Code: Text  [Select][+][-]
  1. [19:44:30.567][3] usbmuxd v1.1.0 starting up
  2. [19:44:30.568][4] Creating socket
  3. [19:44:30.568][5] initialized config_dir to /var/lib/lockdown
  4. [19:44:30.568][5] client_init
  5. [19:44:30.568][5] device_init
  6. [19:44:30.568][4] Initializing USB
  7. [19:44:30.568][5] usb_init for linux / libusb 1.0
  8. [19:44:30.569][4] Found new device with v/p 05ac:12a8 at 1-5
  9. [19:44:30.569][4] Found interface 1 with endpoints 04/85 for device 1-5
  10. [19:44:30.571][4] Using wMaxPacketSize=512 for device 1-5
  11. [19:44:30.571][3] Connecting to new device on location 0x10005 as ID 1
  12. [19:44:30.577][5] All 3 RX loops started successfully
  13. [19:44:30.577][4] 1 device detected
  14. [19:44:30.577][3] Initialization complete
  15.  

Notice that at line 8 that it says "05ac:12a8" and when I type "lsusb" I can confirm that it is my iPhone:

Code: Text  [Select][+][-]
  1. Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
  2. Bus 001 Device 005: ID 05ac:12a8 Apple, Inc.
  3. Bus 001 Device 002: ID 80ee:0021 VirtualBox USB Tablet
  4. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
  5.  

But, when I run the PeerTalk Python script in a new terminal window it still outputs me the following error:

Code: Text  [Select][+][-]
  1. peertalk starting
  2. Waiting for devices...
  3. No device found
  4. Traceback (most recent call last):
  5.   File "/home/aidvllasaliu/Desktop/iphone-dataprotection-3e6e6f047d73 2/usbmuxd-python-client/peertalk.py", line 66, in <module>
  6.     dev = mux.devices[0]
  7. IndexError: list index out of range
  8.  

There's also a different Py script which is called "usbmux.py" and when I run it in a new , two things happen.

First it outputs the following:
Code: Text  [Select][+][-]
  1. Waiting for devices...
  2. Devices:
  3.  

and the "USBMUXD -f -v -v" window adds some more lines (see line 15 and onwards):
Code: Text  [Select][+][-]
  1. [19:44:30.567][3] usbmuxd v1.1.0 starting up
  2. [19:44:30.568][4] Creating socket
  3. [19:44:30.568][5] initialized config_dir to /var/lib/lockdown
  4. [19:44:30.568][5] client_init
  5. [19:44:30.568][5] device_init
  6. [19:44:30.568][4] Initializing USB
  7. [19:44:30.568][5] usb_init for linux / libusb 1.0
  8. [19:44:30.569][4] Found new device with v/p 05ac:12a8 at 1-5
  9. [19:44:30.569][4] Found interface 1 with endpoints 04/85 for device 1-5
  10. [19:44:30.571][4] Using wMaxPacketSize=512 for device 1-5
  11. [19:44:30.571][3] Connecting to new device on location 0x10005 as ID 1
  12. [19:44:30.577][5] All 3 RX loops started successfully
  13. [19:44:30.577][4] 1 device detected
  14. [19:44:30.577][3] Initialization complete
  15. [19:50:55.802][4] New client on fd 15
  16. [19:50:55.805][5] Client command in fd 15 len 16 ver 0 msg 3 tag 1
  17. [19:50:55.805][5] send_pkt fd 15 tag 1 msg 1 payload_length 4
  18. [19:50:55.805][5] Client 15 now LISTENING

Here's a GitHub project where the person of the project has successfully managed to get it to work on Raspberry Pi,
and he says that it should be working on any Linux distro: https://github.com/davidahouse/PiTalk

I follow these exact steps but the got damn python script wont work.

benohb

  • Full Member
  • ***
  • Posts: 213
Re: Linux USB communication with iOS Device
« Reply #12 on: October 12, 2015, 02:36:31 am »
fromm DOC

When usbmuxd is running (normally started, or stopped as a result of "udev" auto-insertion messages or by systemd) it provides a socket interface in "/var/run/usbmuxd" that is designed to be compatible with the socket interface that is provided on Mac OS X. You should also create a "usbmux" user that has access to USB devices on your system. Alternatively, you can pass a different username using the -U argume

benohb

  • Full Member
  • ***
  • Posts: 213
Re: Linux USB communication with iOS Device
« Reply #13 on: October 12, 2015, 04:12:16 am »


In debian  install libimobiledevice
Code: Pascal  [Select][+][-]
  1. sudo apt-get install libimobiledevice-dev


Plug device into computer


Test this code


Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3.  
  4. {$mode objfpc}{$H+}
  5.   {$LinkLib libimobiledevice} // <-------------------------------------  
  6. interface
  7.  
  8.  
  9. uses
  10.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  11.  
  12.  
  13. type
  14.  
  15.  
  16.   { TForm1 }
  17.  
  18.  
  19.   TForm1 = class(TForm)
  20.     Button1: TButton;
  21.     procedure Button1Click(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.     { public declarations }
  26.  
  27.  
  28.   end;
  29.  
  30.  
  31. var
  32.   Form1: TForm1;
  33.   function idevice_get_device_list(devices:PPPchar; count:Plongint):integer; cdecl;external;
  34.  
  35.  
  36. implementation
  37.  
  38.  
  39. {$R *.lfm}
  40.  
  41.  
  42. { TForm1 }
  43.  
  44.  
  45. procedure TForm1.Button1Click(Sender: TObject);
  46. var
  47.   devices :  array of char;
  48.   count:LongInt;
  49. begin
  50.  
  51.  
  52.        ShowMessage(inttostr(idevice_get_device_list(@devices,@count))+
  53.       #13#10+
  54.       'devices find :'+inttostr(count));;
  55.  
  56.  
  57. end;
  58.  
  59.  
  60. end.
  61.  



Unplug device from computer
Retest.... and give me results
sorry for my bad english
« Last Edit: October 12, 2015, 04:51:42 am by benohb »

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Linux USB communication with iOS Device
« Reply #14 on: October 12, 2015, 04:38:52 am »
Thanks.

No problems, bad english is better than no english :)

I tried the code and USBMUXD gives me the following information when the app tries to connect:

Code: Text  [Select][+][-]
  1. [04:33:24.355][5] Client command in fd 15 len 504 ver 1 msg 8 tag 1
  2. [04:33:24.355][5] send_pkt fd 15 tag 1 msg 8 payload_length 230
  3. [04:33:24.355][4] Client 15 connection closed
  4. [04:33:24.355][4] Disconnecting client fd 15

and much more similar information.

And sometimes the lazarus project crashes and sometimes it doesnt.

The result tells me that 0 devices are available:

Code: Pascal  [Select][+][-]
  1. 0. devices find :0

 

TinyPortal © 2005-2018