X-Git-Url: https://git.ao2.it/libam7xxx.git/blobdiff_plain/14b6a47d284966d034259b433028e25c5adcd9bd..3dcf58dc4693136812e4b3ccba3b0f726c1bf3d2:/src/am7xxx.c diff --git a/src/am7xxx.c b/src/am7xxx.c index 114027e..20b0ddf 100644 --- a/src/am7xxx.c +++ b/src/am7xxx.c @@ -55,6 +55,7 @@ static struct am7xxx_usb_device_descriptor supported_devices[] = { struct _am7xxx_device { libusb_device_handle *usb_device; uint8_t buffer[AM7XXX_HEADER_WIRE_SIZE]; + am7xxx_context *ctx; am7xxx_device *next; }; @@ -321,10 +322,18 @@ static int send_header(am7xxx_device *dev, struct am7xxx_header *h) return ret; } -static am7xxx_device *add_new_device(am7xxx_device **devices_list) +static am7xxx_device *add_new_device(am7xxx_context *ctx) { + am7xxx_device **devices_list; am7xxx_device *new_device; + if (ctx == NULL) { + fprintf(stderr, "%s: context must not be NULL!\n", __func__); + return NULL; + } + + devices_list = &(ctx->devices_list); + new_device = malloc(sizeof(*new_device)); if (new_device == NULL) { perror("malloc"); @@ -332,6 +341,8 @@ static am7xxx_device *add_new_device(am7xxx_device **devices_list) } memset(new_device, 0, sizeof(*new_device)); + new_device->ctx = ctx; + if (*devices_list == NULL) { *devices_list = new_device; } else { @@ -343,12 +354,18 @@ static am7xxx_device *add_new_device(am7xxx_device **devices_list) return new_device; } -static am7xxx_device *find_device(am7xxx_device *devices_list, +static am7xxx_device *find_device(am7xxx_context *ctx, unsigned int device_index) { unsigned int i = 0; - am7xxx_device *current = devices_list; + am7xxx_device *current; + if (ctx == NULL) { + fprintf(stderr, "%s: context must not be NULL!\n", __func__); + return NULL; + } + + current = ctx->devices_list; while (current && i++ < device_index) current = current->next; @@ -420,7 +437,7 @@ static int scan_devices(am7xxx_context *ctx, scan_op op, printf("am7xxx device found, index: %d, name: %s\n", current_index, supported_devices[j].name); - new_device = add_new_device(&(ctx->devices_list)); + new_device = add_new_device(ctx); if (new_device == NULL) { /* XXX, the caller may want * to call am7xxx_shutdown() if @@ -433,7 +450,7 @@ static int scan_devices(am7xxx_context *ctx, scan_op op, } else if (op == SCAN_OP_OPEN_DEVICE && current_index == open_device_index) { - *dev = find_device(ctx->devices_list, open_device_index); + *dev = find_device(ctx, open_device_index); if (*dev == NULL) { ret = -ENODEV; goto out; @@ -464,6 +481,8 @@ static int scan_devices(am7xxx_context *ctx, scan_op op, goto out; } + /* everything went fine when building the device list */ + ret = 0; out: libusb_free_device_list(list, 1); return ret;