Skip to content

Instantly share code, notes, and snippets.

@ao-kenji
Created May 23, 2020 23:40
Show Gist options
  • Save ao-kenji/11542fd9659339a0b5f491c4bfb7d118 to your computer and use it in GitHub Desktop.
Save ao-kenji/11542fd9659339a0b5f491c4bfb7d118 to your computer and use it in GitHub Desktop.
--- drivers/blazer_usb.c.orig Fri Feb 28 06:17:32 2014
+++ drivers/blazer_usb.c Sun Nov 2 20:09:29 2014
@@ -185,7 +185,7 @@
/* Write data in 8-byte chunks */
ret = usb_control_msg(udev, USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
- 0x09, 0x2, 0, &tmp[i], 8, 1000);
+ 0x09, 0x200, 0, &tmp[i], 8, 1000);
if (ret <= 0) {
upsdebugx(3, "send: %s", (ret != -ETIMEDOUT) ? usb_strerror() : "Connection timed out");
@@ -203,11 +203,43 @@
* will happen after successfully writing a command to the UPS)
*/
if (ret <= 0) {
- upsdebugx(3, "read: %s", (ret != -ETIMEDOUT) ? usb_strerror() : "Connection timed out");
+ upsdebugx(3, "read1: %s", (ret != -ETIMEDOUT) ? usb_strerror() : "Connection timed out");
return ret;
}
+ snprintf(buf, buflen, "%.*s", ret, tmp);
+ len = ret;
+
+ upsdebugx(5, "read1: %.*s", (int)strcspn(buf, "\r"), buf);
+
/*
+ * XXX:
+ * On {Net,Open}BSD + libusb + OMRON UPS, it seems that we can not get
+ * whole data at once. So we wait a while (1 sec here), read the res,
+ * and concatenate them.
+ */
+ sleep(1);
+
+ /* Read all 64 bytes of the reply in one large chunk, second time */
+ ret = usb_interrupt_read(udev, 0x81, tmp, sizeof(tmp), 1000);
+
+ /*
+ * Any errors here mean that we are unable to read a reply (which
+ * will happen after successfully writing a command to the UPS)
+ */
+ if (ret <= 0) {
+ upsdebugx(3, "read2: %s", (ret != -ETIMEDOUT) ? usb_strerror() : "Connection timed out");
+ return ret;
+ }
+
+ snprintf(&buf[len], buflen - len, "%.*s", ret, tmp);
+
+ upsdebugx(5, "read2: %.*s", (int)strcspn(&buf[len], "\r"), &buf[len]);
+ upsdebugx(3, "read:%.*s", (int)strcspn(buf, "\r"), buf);
+
+ len += ret;
+#if 0
+ /*
* As Ippon will always return 64 bytes in response, we have to
* calculate and return length of actual response data here.
* Empty response will look like 0x00 0x0D, otherwise it will be
@@ -219,6 +251,7 @@
len ++;
}
snprintf(buf, buflen, "%.*s", len, tmp);
+#endif
return len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment