Root/tools/usbwait/usbwait.c

Source at commit 624caeb00652f6118dc64698b7dad6fa3e5bdcc2 created 2 years 6 months ago.
By Werner Almesberger, atusb/: use VR, POWERED, and LED from kicad-libs
1/*
2 * usbwait/usbwait.c - Wait for a USB device to appear
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <sys/time.h>
18
19#include "usbopen.h"
20
21
22#define DEFAULT_POLL_S 0.1
23
24
25static useconds_t interval_us = DEFAULT_POLL_S*1000000;
26static unsigned long timeout = 0;
27static int need_removal = 0;
28
29
30static void wait_for_usb(void)
31{
32    struct timeval to, now;
33    usb_dev_handle *dev;
34
35    gettimeofday(&to, NULL);
36    to.tv_sec += timeout;
37
38    while (1) {
39        usb_rescan();
40        dev = open_usb(0, 0);
41        if (dev) {
42            if (!need_removal)
43                return;
44            usb_close(dev);
45        } else {
46            need_removal = 0;
47        }
48        if (timeout) {
49            gettimeofday(&now, NULL);
50            if (now.tv_sec > to.tv_sec)
51                break;
52            if (now.tv_sec == to.tv_sec &&
53                now.tv_usec > to.tv_usec)
54                break;
55        }
56        if (usleep(interval_us) < 0) {
57            perror("usleep");
58            exit(1);
59        }
60    }
61
62    fprintf(stderr, "timeout\n");
63    exit(1);
64}
65
66
67static void usage(const char *name)
68{
69    fprintf(stderr,
70"usage: %s [-i poll_s] [-p path] [-r] [-t timeout_s] [vendor]:[product]\n\n"
71" -i poll_s poll interval in seconds (default: %g s)\n"
72" -p path USB device path\n"
73" -r wait for device removal first\n"
74" -t timeout_s timeout in seconds (default: infinite)\n"
75  , name, DEFAULT_POLL_S);
76    exit(1);
77}
78
79
80int main(int argc, char **argv)
81{
82    char *end;
83    int c;
84
85    while ((c = getopt(argc, argv, "i:p:rt:")) != EOF)
86        switch (c) {
87        case 'i':
88            interval_us = strtod(optarg, &end)*1000000;
89            if (*end || interval_us < 0)
90                usage(*argv);
91            break;
92        case 'p':
93            restrict_usb_path(optarg);
94            break;
95        case 'r':
96            need_removal = 1;
97            break;
98        case 't':
99            timeout = strtoul(optarg, &end, 0);
100            if (*end || !timeout)
101                usage(*argv);
102            break;
103        default:
104            usage(*argv);
105        }
106
107    if (argc != optind+1)
108        usage(*argv);
109
110    parse_usb_id(argv[optind]);
111    wait_for_usb();
112
113    return 0;
114}
115

Archive Download this file



interactive