From: Antonio Ospite Date: Tue, 28 Feb 2012 20:57:45 +0000 (+0100) Subject: am7xxx: pass the context to add_new_device() and find_device() X-Git-Tag: v0.1.0~1^2~31 X-Git-Url: https://git.ao2.it/libam7xxx.git/commitdiff_plain/8116c40f687d89cbcd521e2268a3a084b9280dfe am7xxx: pass the context to add_new_device() and find_device() This is preparation for when we will track the context in am7xxx_device as well, and as a side effect those functions now look more consistent with the other ones. --- diff --git a/src/am7xxx.c b/src/am7xxx.c index 11c259d..1ece881 100644 --- a/src/am7xxx.c +++ b/src/am7xxx.c @@ -321,10 +321,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"); @@ -343,12 +351,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 +434,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 +447,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;