Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use hid_open in android #137

Closed
xmutzlq opened this issue Jan 20, 2020 · 7 comments
Closed

Unable to use hid_open in android #137

xmutzlq opened this issue Jan 20, 2020 · 7 comments
Labels
Android Android OS related question Information is requested

Comments

@xmutzlq
Copy link

xmutzlq commented Jan 20, 2020

Hi,
thank you for you solution to https://github.com/libusb/hidapi/issues/24, I have build android .so lib。
first,I have use "hid_init()",and the result is 0;
second,I have got "hid_device_info" from the method "hid_enumerate(0x0, 0x0)" , and the result is (
info = HidDeviceInfo{path='0003:0003:00', vendor_id=D197, product_id=E0A097}
info = HidDeviceInfo{path='0003:000b:00', vendor_id=D1B1, product_id=E78083}
info = HidDeviceInfo{path='0003:000b:01', vendor_id=D1B1, product_id=E78083}
)
third,When i use hid_open(vendor_id, product_id, NULL) from hidapi.h(vendor_id and product_id from hid_device_info),but it is not working, and log result is "unable to open device"。
then,i was tested "hid_open" in hid.c,flag "handle = hid_open_path(path_to_open);",go into this method,i do not know why "hid_open_path" return NULL。
can you help me? thanks

@tuxun
Copy link

tuxun commented Jan 20, 2020

Hi,
Does android ask you the permission to use the device when you start your app?

https://developer.android.com/guide/topics/connectivity/usb/host.html#permission-d

You can dig in your system logs running "logcat" as admin in a terminal, that may help you to understand whats wrong.

@Youw Youw added Android Android OS related question Information is requested labels Jan 20, 2020
@xmutzlq
Copy link
Author

xmutzlq commented Jan 21, 2020

Hi @tuxun
thank you for your answer.

i have added usb permission, but hid_open is still no working
and i hava a question about if vendor_id and product_id those two params are get from hid_enumerate(0x0, 0x0), and put those params to hid_open(vendor_id, product_id, NULL) ?

because the method hid_open in hid.c is use while condition and check "cur_dev->vendor_id == vendor_id && cur_dev->product_id == product_id".

hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
{
	struct hid_device_info *devs, *cur_dev;
	const char *path_to_open = NULL;
	hid_device *handle = NULL;
	devs = hid_enumerate(vendor_id, product_id);
	cur_dev = devs;
	**while (cur_dev) {
		if (cur_dev->vendor_id == vendor_id &&
		    cur_dev->product_id == product_id) {**
			if (serial_number) {
				if (cur_dev->serial_number &&
				    wcscmp(serial_number, cur_dev->serial_number) == 0) {
					path_to_open = cur_dev->path;
					break;
				}
			}
			else {
				path_to_open = cur_dev->path;
				break;
			}
		}
		cur_dev = cur_dev->next;
	}
        if (path_to_open) {
		/* Open the device */
		handle = hid_open_path(path_to_open);
	}
	hid_free_enumeration(devs);
	return handle;
}

i thank the problem is hid_open_path(const char *path) , but i have no idea how to test this method, can you help me ? thanks.

@tuxun
Copy link

tuxun commented Jan 21, 2020

Hi,

I think i understand. your path is wrongly returned by enum_device().
/dev/bus/usb/003/003 is a path, but "0003:0003:00" isnt a path. you need to build a string leading to usb device file descriptor.

You should check the file exist before open it :)

@Youw
Copy link
Member

Youw commented Jan 21, 2020

Hi @tuxun,
Thanks for helping out. Please note, that your last answer isn't correct for this case and makes a confusion:
On Android hidapi is using libusb backend, and it doesn't operate with file-paths, but instead with USB entities, like "bus_number:device_address:interface_number", which makes a path, and it is a correct representation.

@xmutzlq

  1. I assume you're using a build from latest master. Can you confirm?

log result is "unable to open device"

Is it from hidtest here:

printf("unable to open device\n");
?
If so, it doesn't exactly describe what went wrong.

i do not know why "hid_open_path" return NULL

Would you be able to trace with a debugger, to catch where exactly hid_open_path fails?

What would be helpful to know: is it goes as far as here:

res = libusb_open(usb_dev, &dev->device_handle);

or not.

@tuxun
Copy link

tuxun commented Jul 26, 2020

coud we have an hid_open_path function using file path and another one using the other kind of path? IMHO its confusing ATM.
The hid_open_path function both take filepath and USB path?

@Youw
Copy link
Member

Youw commented Jul 26, 2020

hid_open_path takes a platform-specific path to a device (which happens to be a file-path only for linux/hidraw backend)

strictly speaking, it is intended to receive only a path field of hid_device_info

it is not designed to be human-readable, or manually constructible
it it shouldn't be confusing, as you shouldn't normally try to read it

@mcuee
Copy link
Member

mcuee commented Jan 26, 2021

libusb/libusb#830
This example might help.

@mcuee mcuee closed this as completed Jun 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Android OS related question Information is requested
Projects
None yet
Development

No branches or pull requests

4 participants