Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give /dev/bus/usb permissions for libusb android applications?

i am developing an Application that uses libusb via jni. this application is currently targeted only to rooted, usb-host Android 3+ machines.

the scenario is as follows:

<java Activity>
       loads <jni_wrapper.so>
               which wraps <my_main_lib.so>
                       that uses <libusb.so>
                               that needs rw access to: /dev/bus/usb/<device>

all the .so native libraries are part of an infrastructure that i install (as root) on /system/lib, which then can be used by java Activities, run by the simple users.

that means that the whole usb communication must be done from the native side.

i got everything to work fine, except for one issue - the usb permissions: the default permissions of /dev/bus/usb/ entries are (0660, uid= root, gid= usb).

obviously, a standard java process running a native code does not meet any of the above requirements, forcing me to "chmod 666" the corresponding /dev entry, to get this working.

this solution is not so good, though, because it does not survive reboot, or unplugging/replugging of the device (permissions go back to 0660).

the solution i'm looking for needs to:

  • survive reboot/replugging
  • be installed ~once~ by the root, and not involve bothering the user every time to gain permissions
  • be generic and run on all (/most) android 3+ machines
  • [not mandatory] gives the minimal credentials necessary

directions i thought of:

  • changing permissions in /init.rc or /ueventd.rc --> both are overridden on every reboot
  • mounting usbfs /proc/bus/usb with devmode=0666 --> survives replugging, but not reboot
  • making my process join the "usb" group? --> i tried android.permission.ACCESS_USB, but it doesnt work/supported... :/

any ideas? :-)

thanks!

like image 453
Nulldef Avatar asked Sep 14 '11 11:09

Nulldef


2 Answers

I've same problem and I solve it doing this in the ndk, changing /proc/bus/usb for where usb is mounted in your case. Hope it helps.

r = system("su -c \"chmod -R 777 /proc/bus/usb/\"");
if(r!=0) {
    LOGD("Could not grant permissions to USB\n");
    eturn CANT_GRAN_PERMISSION_USB;
}
like image 110
Pedro Fraca Avatar answered Sep 18 '22 20:09

Pedro Fraca


There are some method from libusb developer team. for my case, I select the first one method.

from:https://github.com/libusb/libusb/blob/master/android/README

Runtime Permissions:

The default system configuration on most Android device will not allow access to USB devices. There are several options for changing this.

If you have control of the system image then you can modify the ueventd.rc used in the image to change the permissions on /dev/bus/usb//. If using this approach then it is advisable to create a new Android permission to protect access to these files. It is not advisable to give all applications read and write permissions to these files.

For rooted devices the code using libusb could be executed as root using the "su" command. An alternative would be to use the "su" command to change the permissions on the appropriate /dev/bus/usb/ files.

Users have reported success in using android.hardware.usb.UsbManager to request permission to use the UsbDevice and then opening the device. The difficulties in this method is that there is no guarantee that it will continue to work in the future Android versions, it requires invoking Java APIs and running code to match each android.hardware.usb.UsbDevice to a libusb_device.

like image 20
kangear Avatar answered Sep 18 '22 20:09

kangear