Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old August 12th, 2011, 10:47 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Aug 2011
Posts: 6
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Compiling LibUSB for Android

Hey Guys,
I'm writing an application that requires a custom USB driver, and i need LibUSB to be able to do it. I downloaded the following:

android.git.kernel.org Git - platform/external/libusb.git/summary

My problem is that when i try to cross compile using ./configure this lib, i get the error: unsupported operating system. I've been trying to configure like this:

Code:
./configure --host=arm-eabi CC=arm-linux-androideabi-gcc CPPFLAGS="-I$NDK_ROOT/platforms/android-3/arch-arm/usr/include/" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/" LIBS="-lc"
The main aim is to be able to use the library with the Android NDK. If anybody has managed this - or could help me with this, it would be most appreciated! (Also - i am using Ubuntu)

Cheers, Tony.

Advertisements
t.elimpus is offline  
Reply With Quote
sponsored links
Old August 24th, 2011, 11:13 AM   #2 (permalink)
New Member
 
Join Date: Aug 2011
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by t.elimpus View Post
Hey Guys,
I'm writing an application that requires a custom USB driver, and i need LibUSB to be able to do it. I downloaded the following:

android.git.kernel.org Git - platform/external/libusb.git/summary

My problem is that when i try to cross compile using ./configure this lib, i get the error: unsupported operating system. I've been trying to configure like this:

Code:
./configure --host=arm-eabi CC=arm-linux-androideabi-gcc CPPFLAGS="-I$NDK_ROOT/platforms/android-3/arch-arm/usr/include/" CFLAGS="-nostdlib" LDFLAGS="-Wl,-rpath-link=$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/ -L$NDK_ROOT/platforms/android-3/arch-arm/usr/lib/" LIBS="-lc"
The main aim is to be able to use the library with the Android NDK. If anybody has managed this - or could help me with this, it would be most appreciated! (Also - i am using Ubuntu)

Cheers, Tony.

Hi Tony,

I have to do a similar stuff with android NDK using Libusb. Please can you help via sharing how did you get on with the libusb and did you achieved the communication?

Highly appreciated and thanks in advance.

-Arsalan
arsalan.haqs is offline  
Reply With Quote
Old September 15th, 2011, 09:53 AM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Aug 2011
Posts: 6
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey Arsalan,
Sorry about the late reply. I managed to build the library in the same makefile that you build your C native library in. I took out all the C/header files from the uncompiled libusb folder which is the link i provided in my first post, and put them in the jni folder of the App's root folder. In the makefile, i included all the .c files in the jni folder as LOCAL_C_INCLUDES. This is the way that has worked for me, hope it helps you too.

Tony.
t.elimpus is offline  
Reply With Quote
Old September 15th, 2011, 11:01 PM   #4 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

When compiling libraries using android, you need to do the following:

1) Make sure the files are all in the jni folder
2) Create an Android.mk file for each library to be compiled
3) OPTIONAL: Create an Application.mk file that sets various compilation flags
4) compile using the ndk-build shell script. Configuration/Make files will not work correctly because an alternate toolchain is used for android.
jonbonazza is offline  
Reply With Quote
Old November 10th, 2011, 09:35 AM   #5 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 3
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by t.elimpus View Post
This is the way that has worked for me, hope it helps you too.
Tony.
Tony -- I got lsusb working, but when I attempt to write anything to the USB peripheral, the application restarts. Were you able to send commands to the USB peripheral without having to first get root access?
-Matt
ndnfan is offline  
Reply With Quote
Old February 8th, 2012, 11:04 AM   #6 (permalink)
New Member
 
Join Date: Nov 2011
Posts: 3
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is how I got libusb working in Android without rooting the phone.
1) In Java, enumerate the USB and find the device you want. Reference this URL: USB Host | Android Developers
2) Once you find the device, get the file descriptor to it. Reference this method:UsbDeviceConnection | Android Developers
3) Pass the file descriptor to the ported libusb code. I had to change the libusb code slightly to accept a file descriptor instead of trying to open its own descriptor.

Once that's complete, I was able to make use of libusb w/o issue. As far as I know, this is the only way to get isochronous USB support on Android.
ndnfan is offline  
Reply With Quote
Old February 8th, 2012, 05:11 PM   #7 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Please note that USB Host capabilities were not added until Android 4.0, so if your application requires those capabilities, you will need to either (a) purchase an android 4.0 device for testing, or (b) root your current device and find a functional Android 4.0 ROM to install on it.
jonbonazza is offline  
Reply With Quote
Old April 2nd, 2012, 02:25 PM   #8 (permalink)
New Member
 
Join Date: Apr 2012
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by ndnfan View Post
Here is how I got libusb working in Android without rooting the phone.
1) In Java, enumerate the USB and find the device you want. Reference this URL:
2) Once you find the device, get the file descriptor to it. Reference this method:
3) Pass the file descriptor to the ported libusb code. I had to change the libusb code slightly to accept a file descriptor instead of trying to open its own descriptor.

Once that's complete, I was able to make use of libusb w/o issue. As far as I know, this is the only way to get isochronous USB support on Android.
Can you explain a bit better what changes were made to libusb to make this work? Or better yet, source? Thanks!
jazzatar is offline  
Reply With Quote
Old July 27th, 2012, 03:47 AM   #9 (permalink)
New Member
 
Join Date: Jul 2012
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Smile

Quote:
Originally Posted by ndnfan View Post
Here is how I got libusb working in Android without rooting the phone.
1) In Java, enumerate the USB and find the device you want. Reference this URL: USB Host | Android Developers
2) Once you find the device, get the file descriptor to it. Reference this method:UsbDeviceConnection | Android Developers
3) Pass the file descriptor to the ported libusb code. I had to change the libusb code slightly to accept a file descriptor instead of trying to open its own descriptor.

Once that's complete, I was able to make use of libusb w/o issue. As far as I know, this is the only way to get isochronous USB support on Android.
This is exactly what I need to do for my school project. How do I do step 3? I already can get the file descriptor. How do I pass it to libusb?

Really need help to do it.

Thanks!
MagicAsh is offline  
Reply With Quote
Old January 3rd, 2013, 05:34 PM   #10 (permalink)
New Member
 
Join Date: Jan 2013
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 1 Time in 1 Post
Default

I modified libusb open function as follows:

[HIGH]static int op_open2(struct libusb_device_handle *handle, int fd) {
struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);

hpriv->fd = fd;

return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
}[/HIGH]

where fd is the file descriptor obtained in Java via

[HIGH]final UsbDeviceConnection connection = manager.openDevice(device);
return connection.getFileDescriptor();[/HIGH]

Unfortunatelly I keep getting an error when I call

[HIGH]static int op_claim_interface(struct libusb_device_handle *handle, int iface)
{
int fd = _device_handle_priv(handle)->fd;

int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface);

if (r) {
if (errno == ENOENT)
return LIBUSB_ERROR_NOT_FOUND;
else if (errno == EBUSY)
return LIBUSB_ERROR_BUSY;
else if (errno == ENODEV)
return LIBUSB_ERROR_NO_DEVICE;

usbi_err(HANDLE_CTX(handle),
"claim interface failed, error %d errno %d", r, errno);
return LIBUSB_ERROR_OTHER;
}
return 0;
}[/HIGH]

claim interface failed, error -1 errno 9

which is translated to "Bad file number". The file descriptor I get from Java is a positive integer!

The only other small detail is that my native code runs as a separate binary that is spawned with a Java ProcessBuilder. But they share the same uid, so I presume the USB permissions I have from Java should still apply for libusb.

Any thoughts would be much appreciated!
martinmarinov is offline  
Last edited by martinmarinov; January 3rd, 2013 at 06:03 PM.
Reply With Quote
sponsored links
Old January 4th, 2013, 08:59 AM   #11 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,935
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 482 Times in 294 Posts
Default

Can you post your native function that is called from java? There might be an error in it.
jonbonazza is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 01:55 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.