Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add multiple device support with command line option #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions imx_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static struct mach_id *parse_imx_conf(char const *filename)
return head;
}

static struct mach_id * imx_device(unsigned short vid, unsigned short pid, struct mach_id *p)
static struct mach_id *imx_device(unsigned short vid, unsigned short pid, struct mach_id *p)
{
// printf("%s: vid=%x pid=%x\n", __func__, vid, pid);
while (p) {
Expand All @@ -165,7 +165,7 @@ static struct mach_id * imx_device(unsigned short vid, unsigned short pid, struc
}


static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id, struct mach_id *list)
static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id, struct mach_id *list, int device_count)
{
int i = 0;
struct mach_id *p;
Expand All @@ -179,10 +179,20 @@ static libusb_device *find_imx_dev(libusb_device **devs, struct mach_id **pp_id,
fprintf(stderr, "failed to get device descriptor");
return NULL;
}
uint8_t bnum = libusb_get_bus_number(dev);
uint8_t dnum = libusb_get_device_address(dev);
fprintf(stderr, " * bus_number: %d\n", bnum);
fprintf(stderr, " * device_number: %d\n", dnum);
p = imx_device(desc.idVendor, desc.idProduct, list);
printf("device_count: %d\n", i);
//if (p && (device_count == 0 || device_count == i) ) {
if (p) {
*pp_id = p;
return dev;
if (device_count == 0) {
*pp_id = p;
return dev;
} else {
device_count--;
}
}
}
fprintf(stderr, "no matching USB device found\n");
Expand Down Expand Up @@ -333,7 +343,7 @@ void print_usage(void)
}

int parse_opts(int argc, char * const *argv, char const **configdir,
int *verify, struct sdp_work **cmd_head)
int *verify, int *device_count, struct sdp_work **cmd_head)
{
int c;

Expand All @@ -345,7 +355,7 @@ int parse_opts(int argc, char * const *argv, char const **configdir,
{0, 0, 0, 0 },
};

while ((c = getopt_long(argc, argv, "+hdvc:", long_options, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "+hdvcn:", long_options, NULL)) != -1) {
switch (c)
{
case 'h':
Expand All @@ -361,6 +371,9 @@ int parse_opts(int argc, char * const *argv, char const **configdir,
case 'c':
*configdir = optarg;
break;
case 'n':
*device_count = atoi(optarg);
break;
}
}

Expand Down Expand Up @@ -454,7 +467,8 @@ int do_work(struct sdp_dev *p_id, struct sdp_work **work, int verify)
}

int do_autodetect_dev(char const *base_path, char const *conf_path,
struct mach_id *list, int verify, struct sdp_work *cmd_head)
struct mach_id *list, int verify, struct sdp_work *cmd_head,
int device_count)
{
struct sdp_dev *p_id;
struct mach_id *mach;
Expand All @@ -479,7 +493,7 @@ int do_autodetect_dev(char const *base_path, char const *conf_path,

if (debugmode)
print_devs(devs);
dev = find_imx_dev(devs, &mach, list);
dev = find_imx_dev(devs, &mach, list, device_count);
if (!dev) {
libusb_free_device_list(devs, 1);
err = LIBUSB_ERROR_NO_DEVICE;
Expand Down Expand Up @@ -561,8 +575,9 @@ int main(int argc, char * const argv[])
char const *conf;
char const *base_path = get_base_path(argv[0]);
char const *conf_path = get_global_conf_path();
int device_count = 0;

err = parse_opts(argc, argv, &conf_path, &verify, &cmd_head);
err = parse_opts(argc, argv, &conf_path, &verify, &device_count, &cmd_head);
if (err < 0)
return EXIT_FAILURE;
else if (err > 0)
Expand All @@ -577,7 +592,7 @@ int main(int argc, char * const argv[])
if (!list)
return EXIT_FAILURE;

err = do_autodetect_dev(base_path, conf_path, list, verify, cmd_head);
err = do_autodetect_dev(base_path, conf_path, list, verify, cmd_head, device_count);
if (err < 0)
return EXIT_FAILURE;

Expand Down