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

Non-root android support #1150

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions host/hackrf-tools/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ if(MSVC)
../getopt/getopt.c
)
LIST(APPEND TOOLS_LINK_LIBS ${FFTW_LIBRARIES})
elseif(ANDROID)
LIST(APPEND TOOLS_LINK_LIBS m ${FFTW_LIBRARIES})
else()
LIST(APPEND TOOLS_LINK_LIBS m fftw3f)
endif()
Expand Down
5 changes: 5 additions & 0 deletions host/libhackrf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ include(${PROJECT_SOURCE_DIR}/../cmake/set_release.cmake)
add_definitions(-DLIBRARY_RELEASE="${RELEASE}")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../cmake/modules)

option(DISABLE_USB_DEVICE_DISCOVERY "Prevent libusb from trying to enumerate devices. Useful on non-root android" OFF)
if (DISABLE_USB_DEVICE_DISCOVERY)
add_definitions(-DDISABLE_USB_DEVICE_DISCOVERY)
endif()

if(MSVC)
set(THREADS_USE_PTHREADS_WIN32 true)
else()
Expand Down
35 changes: 35 additions & 0 deletions host/libhackrf/src/hackrf.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ int ADDCALL hackrf_init(void)
return HACKRF_SUCCESS;
}

#ifdef DISABLE_USB_DEVICE_DISCOVERY
// LibUSB does not support device discovery on android
libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
#endif

libusb_error = libusb_init(&g_libusb_context);
if (libusb_error != 0) {
last_libusb_error = libusb_error;
Expand Down Expand Up @@ -754,6 +759,36 @@ int ADDCALL hackrf_open_by_serial(
return hackrf_open_setup(usb_device, device);
}

int ADDCALL hackrf_open_by_fd(
int fd,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should take intptr_t sys_dev here instead and pass it straight through to libusb. Looking back at at the PR for the libusb function (libusb/libusb#242 (comment)), they made that choice to make it more general across different platforms so I think we should do the same and leave it up to the user to do any platform-specific conversion/checking.

hackrf_device** device)
{
#ifndef _WIN32
libusb_device_handle* usb_device;

if (fd < 0) {
return HACKRF_ERROR_INVALID_PARAM;
}

if (device == NULL) {
return HACKRF_ERROR_INVALID_PARAM;
}

int err = libusb_wrap_sys_device(g_libusb_context, (intptr_t)fd, &usb_device);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was added fairly recently (in libusb 1.0.23) so I think we should add an ifdef on LIBUSB_API_VERSION and return unsupported otherwise, to allow for building with older versions.

if (err) {
return HACKRF_ERROR_NOT_FOUND;
}

if (usb_device == NULL) {
return HACKRF_ERROR_NOT_FOUND;
}

return hackrf_open_setup(usb_device, device);
#else
return HACKRF_ERROR_UNSUPPORTED;
#endif
}

int ADDCALL hackrf_device_list_open(
hackrf_device_list_t* list,
int idx,
Expand Down
5 changes: 5 additions & 0 deletions host/libhackrf/src/hackrf.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum hackrf_error {
HACKRF_ERROR_NOT_FOUND = -5,
HACKRF_ERROR_BUSY = -6,
HACKRF_ERROR_NO_MEM = -11,
HACKRF_ERROR_UNSUPPORTED = -12,
HACKRF_ERROR_LIBUSB = -1000,
HACKRF_ERROR_THREAD = -1001,
HACKRF_ERROR_STREAMING_THREAD_ERR = -1002,
Expand Down Expand Up @@ -223,6 +224,10 @@ extern ADDAPI int ADDCALL hackrf_open_by_serial(
const char* const desired_serial_number,
hackrf_device** device);

extern ADDAPI int ADDCALL hackrf_open_by_fd(
int fd,
hackrf_device** device);

extern ADDAPI int ADDCALL hackrf_close(hackrf_device* device);

extern ADDAPI int ADDCALL hackrf_start_rx(
Expand Down