Skip to content

Commit

Permalink
usb.c: Scanning doesn't require context
Browse files Browse the repository at this point in the history
Remove functions usb_context_scan_init() and usb_context_scan_free(),
as the context structure is not really required; the libusb context can
be created then destroyed in usb_context_scan() directly.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed May 24, 2021
1 parent d93de75 commit f20ffd9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 47 deletions.
7 changes: 1 addition & 6 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ static inline __check_ret void * ERR_TO_PTR(intptr_t err)
struct iio_context_pdata;
struct iio_device_pdata;
struct iio_channel_pdata;
struct iio_scan_backend_context;

struct iio_channel_attr {
char *name;
Expand Down Expand Up @@ -246,11 +245,7 @@ struct iio_context * serial_create_context_from_uri(const char *uri);

int local_context_scan(struct iio_scan_result *scan_result);

struct iio_scan_backend_context * usb_context_scan_init(void);
void usb_context_scan_free(struct iio_scan_backend_context *ctx);

int usb_context_scan(struct iio_scan_backend_context *ctx,
struct iio_scan_result *scan_result);
int usb_context_scan(struct iio_scan_result *scan_result);

int dnssd_context_scan(struct iio_scan_result *scan_result);

Expand Down
12 changes: 5 additions & 7 deletions scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string.h>

struct iio_scan_context {
struct iio_scan_backend_context *usb_ctx;
bool scan_usb;
bool scan_network;
bool scan_local;
};
Expand Down Expand Up @@ -45,8 +45,8 @@ ssize_t iio_scan_context_get_info_list(struct iio_scan_context *ctx,
}
}

if (WITH_USB_BACKEND && ctx->usb_ctx) {
int ret = usb_context_scan(ctx->usb_ctx, &scan_result);
if (WITH_USB_BACKEND && ctx->scan_usb) {
int ret = usb_context_scan(&scan_result);
if (ret < 0) {
if (scan_result.info)
iio_context_info_list_free(scan_result.info);
Expand Down Expand Up @@ -135,8 +135,8 @@ struct iio_scan_context * iio_create_scan_context(
if (!backend || strstr(backend, "local"))
ctx->scan_local = true;

if (WITH_USB_BACKEND && (!backend || strstr(backend, "usb")))
ctx->usb_ctx = usb_context_scan_init();
if (!backend || strstr(backend, "usb"))
ctx->scan_usb = true;

if (!backend || strstr(backend, "ip"))
ctx->scan_network = true;
Expand All @@ -146,8 +146,6 @@ struct iio_scan_context * iio_create_scan_context(

void iio_scan_context_destroy(struct iio_scan_context *ctx)
{
if (WITH_USB_BACKEND && ctx->usb_ctx)
usb_context_scan_free(ctx->usb_ctx);
free(ctx);
}

Expand Down
45 changes: 11 additions & 34 deletions usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1200,49 +1200,24 @@ static int usb_fill_context_info(struct iio_context_info *info,
return 0;
}

struct iio_scan_backend_context {
libusb_context *ctx;
};

struct iio_scan_backend_context * usb_context_scan_init(void)
{
struct iio_scan_backend_context *ctx;
int ret;

ctx = malloc(sizeof(*ctx));
if (!ctx) {
errno = ENOMEM;
return NULL;
}

ret = libusb_init(&ctx->ctx);
if (ret) {
free(ctx);
errno = (int) libusb_to_errno(ret);
return NULL;
}

return ctx;
}

void usb_context_scan_free(struct iio_scan_backend_context *ctx)
{
libusb_exit(ctx->ctx);
free(ctx);
}

int usb_context_scan(struct iio_scan_backend_context *ctx,
struct iio_scan_result *scan_result)
int usb_context_scan(struct iio_scan_result *scan_result)
{
struct iio_context_info **info;
libusb_device **device_list;
libusb_context *ctx;
unsigned int i;
int ret;

ret = (int) libusb_get_device_list(ctx->ctx, &device_list);
ret = libusb_init(&ctx);
if (ret < 0)
return -(int) libusb_to_errno(ret);

ret = (int) libusb_get_device_list(ctx, &device_list);
if (ret < 0) {
ret = -(int) libusb_to_errno(ret);
goto cleanup_libusb_exit;
}

for (i = 0; device_list[i]; i++) {
struct libusb_device_handle *hdl;
struct libusb_device *dev = device_list[i];
Expand Down Expand Up @@ -1270,5 +1245,7 @@ int usb_context_scan(struct iio_scan_backend_context *ctx,

cleanup_free_device_list:
libusb_free_device_list(device_list, true);
cleanup_libusb_exit:
libusb_exit(ctx);
return ret;
}

0 comments on commit f20ffd9

Please sign in to comment.