Last active
December 26, 2015 01:19
-
-
Save ao-kenji/7070817 to your computer and use it in GitHub Desktop.
A quick hack to Network UPS Tools(NUT) "blazer_usb" driver to use OMRON UPS on NetBSD/OpenBSD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- nut-2.6.5/drivers/blazer_usb.c.orig Wed Aug 1 02:38:59 2012 | |
+++ nut-2.6.5/drivers/blazer_usb.c Mon Oct 21 00:04:22 2013 | |
@@ -173,8 +173,8 @@ | |
static int ippon_command(const char *cmd, char *buf, size_t buflen) | |
{ | |
- char tmp[64]; | |
- int ret; | |
+ char tmp[64], tmp2[64]; | |
+ int ret, len1; | |
size_t i; | |
snprintf(tmp, sizeof(tmp), "%s", cmd); | |
@@ -183,7 +183,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"); | |
@@ -206,9 +206,36 @@ | |
} | |
snprintf(buf, buflen, "%.*s", ret, tmp); | |
+ len1 = ret; | |
- upsdebugx(3, "read: %.*s", (int)strcspn(buf, "\r"), buf); | |
- return 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 rest, | |
+ * and concatenate them. | |
+ */ | |
+ sleep(1); | |
+ | |
+ /* Read all 64 bytes of the reply in one large chunk, second time */ | |
+ ret = usb_interrupt_read(udev, 0x81, tmp2, sizeof(tmp2), 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) { | |
+ fprintf(stderr, "read2: %s", (ret != -ETIMEDOUT) ? usb_strerror() : "Connection timed out"); | |
+ return ret; | |
+ } | |
+ | |
+ snprintf(&buf[len1], buflen - len1, "%.*s", ret, tmp2); | |
+ | |
+ upsdebugx(5, "read2: %.*s", (int)strcspn(&buf[len1], "\r"), &buf[len1]); | |
+ upsdebugx(3, "read:%.*s", (int)strcspn(buf, "\r"), buf); | |
+ | |
+ return len1 + ret; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick guide to use OMRON UPS on NetBSD/OpenBSD with NUT(Network UPS Tools)
ABSOLUTELY NO WARRANTY
[by35s]
driver = blazer_usb
port = auto
desc = "OMRON BY35S"
vendorid = 0590
productid = 0080
subdriver = ippon
default.battery.voltage.high = 13.6
default.battery.voltage.low = 11.5