handle init/exit functions calling

automatically inside the library
This commit is contained in:
Dimitri Stolnikov 2012-04-01 13:06:22 +02:00
parent 46acfaebd2
commit 9d15dc565a
3 changed files with 36 additions and 28 deletions

View File

@ -27,12 +27,6 @@ extern "C" {
typedef int rtlsdr_dev_t; typedef int rtlsdr_dev_t;
/* must be called once before using any other library functions */
int rtlsdr_init(void);
/* must be called once at application shutdown */
void rtlsdr_exit(void);
uint32_t rtlsdr_get_device_count(void); uint32_t rtlsdr_get_device_count(void);
const char *rtlsdr_get_device_name(uint32_t index); const char *rtlsdr_get_device_name(uint32_t index);
@ -66,10 +60,6 @@ int rtlsdr_reset_buffer(rtlsdr_dev_t *dev);
int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, uint32_t len, uint32_t *n_read); int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, uint32_t len, uint32_t *n_read);
typedef void(*rtlsdr_async_read_cb_t)(const char *buf, uint32_t len, void *ctx);
int rtlsdr_async_loop(rtlsdr_dev_t *dev, rtlsdr_async_read_cb_t cb, void *ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -64,13 +64,13 @@ int main(int argc, char **argv)
dev_index = atoi(optarg); dev_index = atoi(optarg);
break; break;
case 'f': case 'f':
frequency = atoi(optarg); frequency = (int)atof(optarg);
break; break;
case 'g': case 'g':
gain = atoi(optarg); gain = atoi(optarg);
break; break;
case 's': case 's':
samp_rate = atoi(optarg); samp_rate = (int)atof(optarg);
break; break;
default: default:
usage(); usage();
@ -84,8 +84,6 @@ int main(int argc, char **argv)
filename = argv[optind]; filename = argv[optind];
} }
rtlsdr_init();
int device_count = rtlsdr_get_device_count(); int device_count = rtlsdr_get_device_count();
if (!device_count) { if (!device_count) {
fprintf(stderr, "No supported devices found.\n"); fprintf(stderr, "No supported devices found.\n");
@ -153,11 +151,12 @@ int main(int argc, char **argv)
} }
} }
if (do_exit)
printf("\nUser cancel, exiting...\n");
fclose(file); fclose(file);
rtlsdr_close(dev); rtlsdr_close(dev);
rtlsdr_exit();
out: out:
return r >= 0 ? r : -r; return r >= 0 ? r : -r;
} }

View File

@ -79,8 +79,6 @@ enum rtlsdr_tuners {
RTLSDR_TUNER_FC0013, RTLSDR_TUNER_FC0013,
}; };
static rtlsdr_tuner_t tuners[] = { static rtlsdr_tuner_t tuners[] = {
{ e4k_init, e4k_exit, e4k_tune, e4k_set_bw, e4k_set_gain, 0, 0, 0 }, { e4k_init, e4k_exit, e4k_tune, e4k_set_bw, e4k_set_gain, 0, 0, 0 },
{ fc0012_init, fc0012_exit, fc0012_tune, fc0012_set_bw, fc0012_set_gain, 0, 0, 0 }, { fc0012_init, fc0012_exit, fc0012_tune, fc0012_set_bw, fc0012_set_gain, 0, 0, 0 },
@ -108,6 +106,9 @@ typedef struct {
int rate; /* Hz */ int rate; /* Hz */
} rtlsdr_dev_t; } rtlsdr_dev_t;
static int opened_devices = 0;
static int libusb_inited = 0;
#define CRYSTAL_FREQ 28800000 #define CRYSTAL_FREQ 28800000
#define MAX_SAMP_RATE 3200000 #define MAX_SAMP_RATE 3200000
@ -489,16 +490,6 @@ int rtlsdr_get_sample_rate(rtlsdr_dev_t *dev)
return dev->rate; return dev->rate;
} }
int rtlsdr_init(void)
{
return libusb_init(NULL);
}
void rtlsdr_exit(void)
{
libusb_exit(NULL);
}
rtlsdr_device_t *find_known_device(uint16_t vid, uint16_t pid) rtlsdr_device_t *find_known_device(uint16_t vid, uint16_t pid)
{ {
int i; int i;
@ -521,6 +512,9 @@ uint32_t rtlsdr_get_device_count(void)
uint32_t device_count = 0; uint32_t device_count = 0;
struct libusb_device_descriptor dd; struct libusb_device_descriptor dd;
if (!libusb_inited)
libusb_init(NULL);
ssize_t cnt = libusb_get_device_list(NULL, &list); ssize_t cnt = libusb_get_device_list(NULL, &list);
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
@ -532,6 +526,9 @@ uint32_t rtlsdr_get_device_count(void)
libusb_free_device_list(list, 0); libusb_free_device_list(list, 0);
if (!libusb_inited)
libusb_exit(NULL);
return device_count; return device_count;
} }
@ -543,6 +540,9 @@ const char *rtlsdr_get_device_name(uint32_t index)
rtlsdr_device_t *device = NULL; rtlsdr_device_t *device = NULL;
uint32_t device_count = 0; uint32_t device_count = 0;
if (!libusb_inited)
libusb_init(NULL);
ssize_t cnt = libusb_get_device_list(NULL, &list); ssize_t cnt = libusb_get_device_list(NULL, &list);
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
@ -560,6 +560,9 @@ const char *rtlsdr_get_device_name(uint32_t index)
libusb_free_device_list(list, 0); libusb_free_device_list(list, 0);
if (!libusb_inited)
libusb_exit(NULL);
if (device) if (device)
return device->name; return device->name;
else else
@ -586,6 +589,13 @@ rtlsdr_dev_t *rtlsdr_open(int index)
dev = malloc(sizeof(rtlsdr_dev_t)); dev = malloc(sizeof(rtlsdr_dev_t));
memset(dev, 0, sizeof(rtlsdr_dev_t)); memset(dev, 0, sizeof(rtlsdr_dev_t));
if (1 == ++opened_devices) {
if (!libusb_inited) {
libusb_init(NULL);
libusb_inited = 1;
}
}
ssize_t cnt = libusb_get_device_list(NULL, &list); ssize_t cnt = libusb_get_device_list(NULL, &list);
for (i = 0; i < cnt; i++) { for (i = 0; i < cnt; i++) {
@ -681,6 +691,13 @@ int rtlsdr_close(rtlsdr_dev_t *dev)
libusb_close(dev->devh); libusb_close(dev->devh);
free(dev); free(dev);
if (0 == --opened_devices) {
if (libusb_inited) {
libusb_exit(NULL);
libusb_inited = 0;
}
}
return 0; return 0;
} }
@ -703,6 +720,8 @@ int rtlsdr_read_sync(rtlsdr_dev_t *dev, void *buf, int len, int *n_read)
return libusb_bulk_transfer(dev->devh, 0x81, buf, len, n_read, 3000); return libusb_bulk_transfer(dev->devh, 0x81, buf, len, n_read, 3000);
} }
#if 0 #if 0
typedef void(*rtlsdr_async_read_cb_t)(const char *buf, uint32_t len, void *ctx);
int rtlsdr_async_loop(rtlsdr_dev_t *dev, rtlsdr_async_read_cb_t cb, void *ctx) int rtlsdr_async_loop(rtlsdr_dev_t *dev, rtlsdr_async_read_cb_t cb, void *ctx)
{ {
return 0; return 0;