hackrf icon indicating copy to clipboard operation
hackrf copied to clipboard

Non-root android support

Open AlexandreRouma opened this issue 3 years ago • 15 comments

This PR contains the following modifications:

  1. Fixed the cmake to allow compilation with the Android NDK
  2. Disabled libusb enumeration when compiling on android since it is not supported (As explained in the libusb documentation line 92)
  3. Add the hackrf_open_by_fd function that allows to open a hackrf device using a file descriptor. This is the only possible way to open a usb device on a non-rooted android device.

This code has been tested and confirmed working on my serveral android device but I still recommend testing it further. It should not impact any other operating system. image

AlexandreRouma avatar Sep 02 '22 23:09 AlexandreRouma

Hi @AlexandreRouma, thanks for the PR!

The changes look straightforward, but as you may have seen there's a build failure on the CI - this is on Appveyor, which runs a Windows build. Since Windows doesn't provide an fd for USB devices I suggest we just wrap hackrf_open_by_fd with #ifndef _WIN32.

My other thought is about the use of LIBUSB_OPTION_NO_DEVICE_DISCOVERY due to discovery not currently working on Android. There's a libusb branch aiming to fix that problem which has been getting a lot of traction: https://github.com/libusb/libusb/pull/874, later updated at https://github.com/libusb/libusb/pull/1164. It would be good to avoid making changes in libhackrf that would prevent discovery from working if the libusb issue is fixed. Can we make this conditional somehow?

Finally, what is the Android app in your screenshot? Could you point us to some instructions for building this with libhackrf so that we can test it?

martinling avatar Sep 03 '22 11:09 martinling

Hi,

I'll add the windows ifdef in a sec, didn't realize that libusb completely removes the function instead of simply returning LIBUSB_ERROR_NOSUPPORTED like basically all other functions. I should bring this up to the libusb team for more consistency.

The branches that aim to fix the enumeration support do exists but I have very high doubts that they will ever work. Still, it can indeed be made conditional.

The app in the screenshot is an android build of my SDR++ project. It is currently very hard to build for android and still a bit buggy (not the fault of libhackrf). So I don't think it's an ideal way to test libhackrf on android.

AlexandreRouma avatar Sep 03 '22 11:09 AlexandreRouma

not exactly sure why the CI's complaining about code style when it perfectly matches the rest of the file

AlexandreRouma avatar Sep 03 '22 16:09 AlexandreRouma

Did you start your work on this PR from the latest version in the repository?

straithe avatar Sep 03 '22 17:09 straithe

yes, it was pulled yesterday a few hours before the PR

AlexandreRouma avatar Sep 03 '22 17:09 AlexandreRouma

Thank you for the update. I'll check this again next week when I'm back in the office (if no one else gets to you before then).

straithe avatar Sep 03 '22 20:09 straithe

We have a clang-format config in the repository which is checked on the CI. If you have clang-format installed, you can run ./tools/reformat-source.sh to make the code match.

martinling avatar Sep 03 '22 20:09 martinling

Do you have any recommended process for building and testing libhackrf on android? I've done a little work with the NDK and Android dev tools in the past but it was some years ago.

martinling avatar Sep 13 '22 10:09 martinling

Building for the NDK is quite involved because the dependencies must be compiles as well, so, so I'd suggest following the scripts I wrote for my android-sdr-kit project that builds all of these libraries for android (or just run the container).

Then, to test, you need to write an NDK app and link as usual. All you have to do is, in the java part, enumerate devices and locate the hackrf using its vid/pid combo, then request permission, and when you've got permission, send the file descriptor android gives you. After using the new hackrf_open_by_fd function, you can use everything as normal.

AlexandreRouma avatar Sep 13 '22 12:09 AlexandreRouma

Hi, Is there any update on this?

AlexandreRouma avatar Sep 26 '22 15:09 AlexandreRouma

Hi, Is there any update on this?

We've been very busy preparing for a release for Opera Cake support which will be out very soon. We will give this a proper review after the release and hope to incorporate it into the next release. Thank you for your patience!

mossmann avatar Sep 28 '22 10:09 mossmann

@AlexandreRouma I'm checking in to see if you've seen the requested changes. Please let me know!

straithe avatar Dec 06 '22 23:12 straithe

Hi,

Sorry, had some stuff come up IRL, didn't have as much time for coding and completely forgot about the PR. Will try to implement this weekend if I get the time.

AlexandreRouma avatar Dec 07 '22 00:12 AlexandreRouma

Hope you are doing ok! I'll watch out for an update.

straithe avatar Dec 07 '22 00:12 straithe

@AlexandreRouma Any plans to complete this?

CRD716 avatar Feb 29 '24 16:02 CRD716