Skip to content
Snippets Groups Projects
Commit e09fe2e4 authored by simimeie's avatar simimeie
Browse files

- new restartonerror parameter

- new reset command (if the device supports it too)
parent b00d2319
No related branches found
No related tags found
No related merge requests found
/* $Id: hostsoftware.c,v 1.7 2010/01/30 16:19:45 simimeie Exp $
/* $Id: hostsoftware.c,v 1.8 2010/03/23 07:52:25 simimeie Exp $
* This is the control software for the ds1820-to-usb device that runs
* on the USB host to which the device is connected.
* You will need libusb and its development headers to compile this.
......@@ -31,6 +31,7 @@ int verblev = 1;
}
int devicenr = 1;
int runinforeground = 0;
int restartonerror = 0;
#define USBDEV_SHARED_VENDOR 0x16C0 /* VOTI */
#define USBDEV_SHARED_PRODUCT 0x05DC /* Obdev's free shared PID */
......@@ -43,6 +44,7 @@ int runinforeground = 0;
#define CMD_STATUS_SHORT 1 /* query device for status */
#define CMD_RESCAN 2 /* tell the device to rescan the probe bus */
#define CMD_STATUS_LONG 3 /* query device for list of probes and their status */
#define CMD_HARDRESET 4 /* reset device and probe bus */
struct daemondata {
unsigned char serial[6];
......@@ -66,6 +68,7 @@ static void usage(char *name)
printf(" test do an echo test with the device\n");
printf(" status query and show device status\n");
printf(" rescan tell the device to rescan its probe bus\n");
printf(" reset tell the device to reset the probe bus and itself\n");
printf(" showint shows 'interrupts' received from the device. These are\n");
printf(" not real interrupts, they are USB 'interrupts', and actually\n");
printf(" work by polling in regular intervals...\n");
......@@ -413,7 +416,20 @@ static void printtooutbuf(char * outbuf, int oblen, struct daemondata * dd) {
*outbuf = 0;
}
static void dodaemon(usb_dev_handle * handle, struct daemondata * dd) {
static void dotryrestart(struct daemondata * dd, char ** argv) {
if (restartonerror) {
struct daemondata * curdd = dd;
while (curdd != NULL) {
close(curdd->fd);
curdd = curdd->next;
}
fprintf(stderr, "Will try to restart in %d second(s)...\n", restartonerror);
sleep(restartonerror);
execv(argv[0], argv);
}
}
static void dodaemon(usb_dev_handle * handle, struct daemondata * dd, char ** argv) {
fd_set mylsocks;
struct daemondata * curdd;
struct timeval to;
......@@ -436,6 +452,7 @@ static void dodaemon(usb_dev_handle * handle, struct daemondata * dd) {
if ((readysocks = select((maxfd + 1), &mylsocks, NULL, NULL, &to)) < 0) { /* Error?! */
if (errno != EINTR) {
perror("ERROR: error on select()");
dotryrestart(dd, argv);
exit(1);
}
} else {
......@@ -472,6 +489,7 @@ static void dodaemon(usb_dev_handle * handle, struct daemondata * dd) {
* usb_errno variable one can poll, but no... */
if (nBytes != -ETIMEDOUT) {
fprintf(stderr, "ERROR: USB error: %s\n", usb_strerror());
dotryrestart(dd, argv);
exit(1);
}
} else if (nBytes != 8) { /* No error but wrong number of bytes */
......@@ -492,7 +510,7 @@ static void dodaemon(usb_dev_handle * handle, struct daemondata * dd) {
/* never reached */
}
int main(int argc, char **argv)
int main(int argc, char ** argv)
{
usb_dev_handle * handle = NULL;
unsigned char buffer[8];
......@@ -510,6 +528,8 @@ int main(int argc, char **argv)
usage(argv[0]); exit(0);
} else if (strcmp(argv[curarg], "--help") == 0) {
usage(argv[0]); exit(0);
} else if (strcmp(argv[curarg], "--restartonerror") == 0) {
restartonerror++;
} else if (strcmp(argv[curarg], "-d") == 0) {
curarg++;
if (curarg >= argc) {
......@@ -565,6 +585,10 @@ int main(int argc, char **argv)
fprintf(stderr, "ERROR: Invalid reply to rescan command from device (%d bytes instead of 1)\n", nBytes);
}
}
} else if (strcmp(argv[curarg], "reset") == 0) {
nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CMD_HARDRESET, 0, 0, (char *)buffer, sizeof(buffer), 5000);
VERBPRINT(1, "%d bytes received as reply.\n", nBytes);
VERBPRINT(0, "%s\n", "Reset command sent. If this worked, you should see an USB disconnect and reconnect now\n");
} else if (strcmp(argv[curarg], "showint") == 0) { /* More of a debug mode: Show interrupt data */
doshowint(handle);
} else if (strcmp(argv[curarg], "daemon") == 0) { /* Daemon mode */
......@@ -642,7 +666,7 @@ int main(int argc, char **argv)
sia.sa_flags = 0; /* to die from 'broken pipe'! */
sigaction(SIGPIPE, &sia, NULL);
}
dodaemon(handle, mydaemondata);
dodaemon(handle, mydaemondata, argv);
} else {
fprintf(stderr, "ERROR: Command '%s' is unknown.\n", argv[curarg]);
usage(argv[0]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment