How to use IOCTL() to access H/W, via Sahred Library + JNI or else, in Android applications?

1,536 views
Skip to first unread message

Mars Alucard

unread,
Mar 11, 2009, 12:10:42 PM3/11/09
to android-platform
Hi,

In my Android application, I need to use IOCTL() to do I2C R/W
operations.

My native C application could do it successfully, no matter by
standalone or dynamically linking .so, but...
ioctl() returns -1 when Android application loads shared library (.so)
and calls the function including ioctl() via JNI.

Could Android application directly use ioctl() in shared library (.so)
thruogh JNI with any tips?

Or, if Android application needs to use ioctl() in native C code,
could some methods help to do this?
(Like Service Manager, Binder, Android source code, or something
else?)

I've tried to find articles for several days to solve this issue, but
still get no ideas now.

(Followings are the environment and testing steps for my trials.)

1. Testing Steps for Native C Application on Android (Successful)
(1) Chiefly I want to execute the two statements below on Android:

unsigned int hI2C = open("/dev/i2c-2", O_RDWR);
int retI2C = ioctl(hI2C, I2C_SLAVE, 0x0C);

(2) A native C application is compiled on Ubuntu, and then put to
Beagle/G1.
(3) The native C application could work well to open I2C and bind/read/
write I2C slave address 0x0C.
(4)
Even more, the native C application could dynamically link the shared
library (.so) in 2. (1) below,
and then call JNI function to execute the statements in 1. (1) above
successfully.

2. Testing Steps for Android Application with JNI + Shared Library .so
(Unsuccessful)
(1) I make a shared library (.so) with JNI, including the statements
in 1. (1) above, by the commands below:

~/arm-2007q3/bin/arm-none-linux-gnueabi-gcc -I/usr/lib/jvm/java-6-sun/
include -I/usr/lib/jvm/java-6-sun/include/linux -fpic -c XXX.c
~/arm-2007q3/bin/arm-none-linux-gnueabi-ld -rpath /system/lib -rpath ~/
OMAP_ROOTFS/system/lib -L ~/OMAP_ROOTFS/system/lib -lc -lm -T ~/
arm-2007q3/arm-none-linux-gnueabi/lib/ldscripts/armelf_linux_eabi.xsc -
shared -o libXXX.so XXX.o

(2) An Android application is compiled by Eclipse on WIN32, with a few
system-level permission. (enabled by "uses-permission")
(3) Then put the Android application (.apk) and shared library (.so)
to Beagle/G1, with chmod 777.
(4) Run the Android application, to load the shared library and call
JNI function to execute the statements in 1. (1) above.
(5) The Android application could load the shared library, call JNI
functions, and execute almost all statements well, BUT...
(6) When the Android application call JNI function to execute ioctl(),
ioctl() return -1, which means FAILED.

3. Environment
(1) Platform: Android @ T1 OMAP Beagle Board & Google Phone
(2) WIN32: JDK v1.60_12 & Eclipse v3.42 (with a few system-level
permissions enabled by "uses-permission")
(3) Linux: Ubuntu & JDK v1.60_10 & arm-2007q3 ( with arm-non-linux-
gnueabi-gcc/ld)



Many thanks,

Mars Alucard

Jean-Baptiste Queru

unread,
Mar 11, 2009, 12:15:47 PM3/11/09
to android-...@googlegroups.com
i2c isn't reasonable to do from user-space, you should do it from
within the kernel. android-kernel is probably the right list for this.

JBQ

PS: you'll probably get a better response if you're more explicit
about what you intend to contribute to Android.

--
Jean-Baptiste M. "JBQ" Queru
Android Engineer, Google.

Questions sent directly to me that have no reason for being private
will likely get ignored or forwarded to a public forum with no further
warning.

David Turner

unread,
Mar 11, 2009, 4:17:13 PM3/11/09
to android-...@googlegroups.com
you are not using the Android toolchain to build your shared library.
Don't expect anything to work correctly.

Dima Zavin

unread,
Mar 12, 2009, 3:02:56 AM3/12/09
to android-...@googlegroups.com
Also, you are probably running it from command line as root, but when running it inside an app it runs as an unprivileged user, and can't access the i2c device nodes. But as others have said, whatever you think you want to do with i2c from userspace, don't.

--Dima

vishal bhoj

unread,
Mar 12, 2009, 5:14:11 AM3/12/09
to android-...@googlegroups.com
Dima and others,

THIS MIGHT BE DRIFTING AWAY FROM THE TOPIC ABOVE but,
Even I am facing a similar problem. on command line when i run I was able to create a file through a native library. but when i load the library through jni ,it is unable to create the file .Is there any way in which this could be achieved. 
--
with regards vishal

詹東恩

unread,
Mar 12, 2009, 5:43:26 AM3/12/09
to android-...@googlegroups.com
Hi all,

I also faced the similar problem. There is a point I could figure out.
In android, there is an service called rild which takes care about the radio part. (See also /hardware/ril/rild and /hardware/ril/reference-ril)
There is a function call inside the rild's main function named "switchuser".
Before this function call, I could open the device node. (/dev/snd/hwC0D0)
But I could not open this device node any more after this function call (switchuser).
The error message is "Permission denied".
I've tried to change the permission of this device node, but it didn't work.
So did you check the return code of opening device node?
Maybe you could try to test inside the rild.c to check it.
If you have any result, please kindly share with me.

Many thanks
Tec
2009/3/12 vishal bhoj <visha...@gmail.com>

Ludwig

unread,
Mar 12, 2009, 3:42:50 PM3/12/09
to android-...@googlegroups.com
I think it would be most worthwhile to check the errno set after your
failed call. -1 only tells you that it failed, the errno will tell
you why.
As others have already suggested this is almost certainly a permission
problem as Android apps do not run as root.
I have successfully called ioctl in a shared library, but only after
sorting out my permissions problem, which in my case was accessing a
USB device. The trick (or hack) in my case was to make the virtual
device file system writeable to all through some command line option
to mount, which was good enough for me.
HTH

Ludwig

2009/3/11 Mars Alucard <KL...@cis.nctu.edu.tw>:

Reply all
Reply to author
Forward
0 new messages