main: Move code around to let the SIMtrace keep running...

Re-open the USB device, reset the state... handle reconnects of
the device.
This commit is contained in:
Holger Hans Peter Freyther 2011-10-10 00:06:19 +02:00 committed by Harald Welte
parent 54683c7b8f
commit 5f9f38378b
1 changed files with 28 additions and 17 deletions

View File

@ -123,6 +123,7 @@ static void print_help(void)
printf( "\t-i\t--gsmtap-ip\tA.B.C.D\n" printf( "\t-i\t--gsmtap-ip\tA.B.C.D\n"
"\t-a\t--skip-atr\n" "\t-a\t--skip-atr\n"
"\t-h\t--help\n" "\t-h\t--help\n"
"\t-k\t--keep-running\n"
"\n" "\n"
); );
} }
@ -131,6 +132,7 @@ static const struct option opts[] = {
{ "gsmtap-ip", 1, 0, 'i' }, { "gsmtap-ip", 1, 0, 'i' },
{ "skip-atr", 0, 0, 'a' }, { "skip-atr", 0, 0, 'a' },
{ "help", 0, 0, 'h' }, { "help", 0, 0, 'h' },
{ "keep-running", 0, 0, 'k' },
{ NULL, 0, 0, 0 } { NULL, 0, 0, 0 }
}; };
@ -165,6 +167,7 @@ int main(int argc, char **argv)
int rc; int rc;
int c, ret = 1; int c, ret = 1;
int skip_atr = 0; int skip_atr = 0;
int keep_running = 0;
struct libusb_device_handle *devh; struct libusb_device_handle *devh;
print_welcome(); print_welcome();
@ -172,7 +175,7 @@ int main(int argc, char **argv)
while (1) { while (1) {
int option_index = 0; int option_index = 0;
c = getopt_long(argc, argv, "i:ah", opts, &option_index); c = getopt_long(argc, argv, "i:ahk", opts, &option_index);
if (c == -1) if (c == -1)
break; break;
switch (c) { switch (c) {
@ -186,6 +189,9 @@ int main(int argc, char **argv)
case 'a': case 'a':
skip_atr = 1; skip_atr = 1;
break; break;
case 'k':
keep_running = 1;
break;
} }
} }
@ -206,26 +212,31 @@ int main(int argc, char **argv)
if (!as) if (!as)
goto release_exit; goto release_exit;
devh = libusb_open_device_with_vid_pid(NULL, SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT); do {
if (!devh) { devh = libusb_open_device_with_vid_pid(NULL, SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT);
fprintf(stderr, "can't open USB device\n"); if (!devh) {
goto close_exit; fprintf(stderr, "can't open USB device\n");
} goto close_exit;
}
rc = libusb_claim_interface(devh, 0); rc = libusb_claim_interface(devh, 0);
if (rc < 0) { if (rc < 0) {
fprintf(stderr, "can't claim interface; rc=%d\n", rc); fprintf(stderr, "can't claim interface; rc=%d\n", rc);
goto close_exit; goto close_exit;
} }
run_mainloop(devh); run_mainloop(devh);
ret = 0; ret = 0;
libusb_release_interface(devh, 0);
close_exit:
if (devh)
libusb_close(devh);
if (keep_running)
sleep(1);
} while (keep_running);
release_exit: release_exit:
libusb_release_interface(devh, 0);
close_exit:
if (devh)
libusb_close(devh);
libusb_exit(NULL); libusb_exit(NULL);
return ret; return ret;
} }