1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
From cd41ea7c9affcb22ba6f4e1cbae7fff27efc533e Mon Sep 17 00:00:00 2001
From: AlexandreRouma <alexandre.rouma@gmail.com>
Date: Thu, 1 Sep 2022 21:31:43 +0200
Subject: [PATCH 09/29] Added android support
---
libairspy/src/airspy.c | 97 +++++++++++++++++++++++++++++++++++-------
libairspy/src/airspy.h | 1 +
2 files changed, 83 insertions(+), 15 deletions(-)
diff --git a/libairspy/src/airspy.c b/libairspy/src/airspy.c
index d86d7aa..fa04e71 100644
--- a/libairspy/src/airspy.c
+++ b/libairspy/src/airspy.c
@@ -62,6 +62,7 @@ typedef int bool;
#define SAMPLE_SCALE (1.0f / (1 << (15 - SAMPLE_SHIFT)))
#define SERIAL_NUMBER_UNUSED (0ULL)
+#define FILE_DESCRIPTOR_UNUSED (-1)
#define USB_PRODUCT_ID (2)
#define STR_DESCRIPTOR_SIZE (250)
@@ -113,11 +114,11 @@ static const uint16_t airspy_usb_pid = 0x60a1;
#define GAIN_COUNT (22)
-uint8_t airspy_linearity_vga_gains[GAIN_COUNT] = { 13, 12, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4 };
-uint8_t airspy_linearity_mixer_gains[GAIN_COUNT] = { 12, 12, 11, 9, 8, 7, 6, 6, 5, 0, 0, 1, 0, 0, 2, 2, 1, 1, 1, 1, 0, 0 };
-uint8_t airspy_linearity_lna_gains[GAIN_COUNT] = { 14, 14, 14, 13, 12, 10, 9, 9, 8, 9, 8, 6, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0 };
-uint8_t airspy_sensitivity_vga_gains[GAIN_COUNT] = { 13, 12, 11, 10, 9, 8, 7, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
-uint8_t airspy_sensitivity_mixer_gains[GAIN_COUNT] = { 12, 12, 12, 12, 11, 10, 10, 9, 9, 8, 7, 4, 4, 4, 3, 2, 2, 1, 0, 0, 0, 0 };
+uint8_t airspy_linearity_vga_gains[GAIN_COUNT] = { 13, 12, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4 };
+uint8_t airspy_linearity_mixer_gains[GAIN_COUNT] = { 12, 12, 11, 9, 8, 7, 6, 6, 5, 0, 0, 1, 0, 0, 2, 2, 1, 1, 1, 1, 0, 0 };
+uint8_t airspy_linearity_lna_gains[GAIN_COUNT] = { 14, 14, 14, 13, 12, 10, 9, 9, 8, 9, 8, 6, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0 };
+uint8_t airspy_sensitivity_vga_gains[GAIN_COUNT] = { 13, 12, 11, 10, 9, 8, 7, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
+uint8_t airspy_sensitivity_mixer_gains[GAIN_COUNT] = { 12, 12, 12, 12, 11, 10, 10, 9, 9, 8, 7, 4, 4, 4, 3, 2, 2, 1, 0, 0, 0, 0 };
uint8_t airspy_sensitivity_lna_gains[GAIN_COUNT] = { 14, 14, 14, 14, 14, 14, 14, 14, 14, 13, 12, 12, 9, 9, 8, 7, 6, 5, 3, 2, 1, 0 };
static int cancel_transfers(airspy_device_t* device)
@@ -433,7 +434,7 @@ static void* consumer_threadproc(void *arg)
}
pthread_mutex_unlock(&device->consumer_mp);
-
+
pthread_exit(NULL);
return NULL;
@@ -506,7 +507,7 @@ static void* transfer_threadproc(void* arg)
device->stop_requested = true;
}
}
-
+
pthread_exit(NULL);
return NULL;
@@ -746,6 +747,51 @@ static void airspy_open_device(airspy_device_t* device,
return;
}
+static void airspy_open_device_fd(airspy_device_t* device,
+ int* ret,
+ int fd)
+{
+ int result;
+
+ result = libusb_wrap_sys_device(device->usb_context, (intptr_t)fd, &device->usb_device);
+ if (result != 0 || device->usb_device == NULL)
+ {
+ *ret = AIRSPY_ERROR_LIBUSB;
+ return;
+ }
+
+#ifdef __linux__
+ /* Check whether a kernel driver is attached to interface #0. If so, we'll
+ * need to detach it.
+ */
+ if (libusb_kernel_driver_active(device->usb_device, 0))
+ {
+ libusb_detach_kernel_driver(device->usb_device, 0);
+ }
+#endif
+
+ result = libusb_set_configuration(device->usb_device, 1);
+ if (result != 0)
+ {
+ libusb_close(device->usb_device);
+ device->usb_device = NULL;
+ *ret = AIRSPY_ERROR_LIBUSB;
+ return;
+ }
+
+ result = libusb_claim_interface(device->usb_device, 0);
+ if (result != 0)
+ {
+ libusb_close(device->usb_device);
+ device->usb_device = NULL;
+ *ret = AIRSPY_ERROR_LIBUSB;
+ return;
+ }
+
+ *ret = AIRSPY_SUCCESS;
+ return;
+}
+
static int airspy_read_samplerates_from_fw(struct airspy_device* device, uint32_t* buffer, const uint32_t len)
{
int result;
@@ -768,7 +814,7 @@ static int airspy_read_samplerates_from_fw(struct airspy_device* device, uint32_
return AIRSPY_SUCCESS;
}
-static int airspy_open_init(airspy_device_t** device, uint64_t serial_number)
+static int airspy_open_init(airspy_device_t** device, uint64_t serial_number, int fd)
{
airspy_device_t* lib_device;
int libusb_error;
@@ -782,6 +828,11 @@ static int airspy_open_init(airspy_device_t** device, uint64_t serial_number)
return AIRSPY_ERROR_NO_MEM;
}
+#ifdef __ANDROID__
+ // LibUSB does not support device discovery on android
+ libusb_set_option(NULL, LIBUSB_OPTION_NO_DEVICE_DISCOVERY, NULL);
+#endif
+
libusb_error = libusb_init(&lib_device->usb_context);
if (libusb_error != 0)
{
@@ -789,11 +840,19 @@ static int airspy_open_init(airspy_device_t** device, uint64_t serial_number)
return AIRSPY_ERROR_LIBUSB;
}
- airspy_open_device(lib_device,
- &result,
- airspy_usb_vid,
- airspy_usb_pid,
- serial_number);
+ if (fd == FILE_DESCRIPTOR_UNUSED) {
+ airspy_open_device(lib_device,
+ &result,
+ airspy_usb_vid,
+ airspy_usb_pid,
+ serial_number);
+ }
+ else {
+ airspy_open_device_fd(lib_device,
+ &result,
+ fd);
+ }
+
if (lib_device->usb_device == NULL)
{
libusb_exit(lib_device->usb_context);
@@ -962,7 +1021,15 @@ int airspy_list_devices(uint64_t *serials, int count)
{
int result;
- result = airspy_open_init(device, serial_number);
+ result = airspy_open_init(device, serial_number, FILE_DESCRIPTOR_UNUSED);
+ return result;
+ }
+
+ int ADDCALL airspy_open_fd(airspy_device_t** device, int fd)
+ {
+ int result;
+
+ result = airspy_open_init(device, SERIAL_NUMBER_UNUSED, fd);
return result;
}
@@ -970,7 +1037,7 @@ int airspy_list_devices(uint64_t *serials, int count)
{
int result;
- result = airspy_open_init(device, SERIAL_NUMBER_UNUSED);
+ result = airspy_open_init(device, SERIAL_NUMBER_UNUSED, FILE_DESCRIPTOR_UNUSED);
return result;
}
diff --git a/libairspy/src/airspy.h b/libairspy/src/airspy.h
index ff83033..952eee3 100644
--- a/libairspy/src/airspy.h
+++ b/libairspy/src/airspy.h
@@ -126,6 +126,7 @@ extern ADDAPI int ADDCALL airspy_exit(void);
extern ADDAPI int ADDCALL airspy_list_devices(uint64_t *serials, int count);
extern ADDAPI int ADDCALL airspy_open_sn(struct airspy_device** device, uint64_t serial_number);
+extern ADDAPI int ADDCALL airspy_open_fd(struct airspy_device** device, int fd);
extern ADDAPI int ADDCALL airspy_open(struct airspy_device** device);
extern ADDAPI int ADDCALL airspy_close(struct airspy_device* device);
--
2.47.3
|