mirror of
git://projects.qi-hardware.com/ben-wpan.git
synced 2024-11-22 14:10:37 +02:00
tools/usbwait/usbwait.c: new option -r to require removal before appearance
- usbwait.c (need_removal, wait_for_usb): if need_removal is set, wait for the device to be removed before trying to find it - usbwait.c (usage, main): new option -r to set need_removal
This commit is contained in:
parent
7037858a3e
commit
62a03bbf32
@ -24,19 +24,27 @@
|
|||||||
|
|
||||||
static useconds_t interval_us = DEFAULT_POLL_S*1000000;
|
static useconds_t interval_us = DEFAULT_POLL_S*1000000;
|
||||||
static unsigned long timeout = 0;
|
static unsigned long timeout = 0;
|
||||||
|
static int need_removal = 0;
|
||||||
|
|
||||||
|
|
||||||
static void wait_for_usb(void)
|
static void wait_for_usb(void)
|
||||||
{
|
{
|
||||||
struct timeval to, now;
|
struct timeval to, now;
|
||||||
|
usb_dev_handle *dev;
|
||||||
|
|
||||||
gettimeofday(&to, NULL);
|
gettimeofday(&to, NULL);
|
||||||
to.tv_sec += timeout;
|
to.tv_sec += timeout;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
usb_rescan();
|
usb_rescan();
|
||||||
if (open_usb(0, 0))
|
dev = open_usb(0, 0);
|
||||||
return;
|
if (dev) {
|
||||||
|
if (!need_removal)
|
||||||
|
return;
|
||||||
|
usb_close(dev);
|
||||||
|
} else {
|
||||||
|
need_removal = 0;
|
||||||
|
}
|
||||||
if (timeout) {
|
if (timeout) {
|
||||||
gettimeofday(&now, NULL);
|
gettimeofday(&now, NULL);
|
||||||
if (now.tv_sec > to.tv_sec)
|
if (now.tv_sec > to.tv_sec)
|
||||||
@ -59,9 +67,10 @@ static void wait_for_usb(void)
|
|||||||
static void usage(const char *name)
|
static void usage(const char *name)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"usage: %s [-i poll_s] [-p path] [-t timeout_s] [vendor]:[product]\n\n"
|
"usage: %s [-i poll_s] [-p path] [-r] [-t timeout_s] [vendor]:[product]\n\n"
|
||||||
" -i poll_s poll interval in seconds (default: %g s)\n"
|
" -i poll_s poll interval in seconds (default: %g s)\n"
|
||||||
" -p path USB device path\n"
|
" -p path USB device path\n"
|
||||||
|
" -r wait for device removal first\n"
|
||||||
" -t timeout_s timeout in seconds (default: infinite)\n"
|
" -t timeout_s timeout in seconds (default: infinite)\n"
|
||||||
, name, DEFAULT_POLL_S);
|
, name, DEFAULT_POLL_S);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -73,7 +82,7 @@ int main(int argc, char **argv)
|
|||||||
char *end;
|
char *end;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "i:p:t:")) != EOF)
|
while ((c = getopt(argc, argv, "i:p:rt:")) != EOF)
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'i':
|
case 'i':
|
||||||
interval_us = strtod(optarg, &end)*1000000;
|
interval_us = strtod(optarg, &end)*1000000;
|
||||||
@ -83,6 +92,9 @@ int main(int argc, char **argv)
|
|||||||
case 'p':
|
case 'p':
|
||||||
restrict_usb_path(optarg);
|
restrict_usb_path(optarg);
|
||||||
break;
|
break;
|
||||||
|
case 'r':
|
||||||
|
need_removal = 1;
|
||||||
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
timeout = strtoul(optarg, &end, 0);
|
timeout = strtoul(optarg, &end, 0);
|
||||||
if (*end || !timeout)
|
if (*end || !timeout)
|
||||||
|
Loading…
Reference in New Issue
Block a user