Skip to content

Instantly share code, notes, and snippets.

@ao-kenji
Last active December 26, 2015 01:19
Show Gist options
  • Save ao-kenji/7070817 to your computer and use it in GitHub Desktop.
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.
--- 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;
}
@ao-kenji
Copy link
Author

Quick guide to use OMRON UPS on NetBSD/OpenBSD with NUT(Network UPS Tools)

ABSOLUTELY NO WARRANTY

  1. The UPS must be recognized as a ugen(4) device. You may edit the kernel source and make the customized kernel like http://openbsd.7691.n7.nabble.com/feature-request-OMRON-BX50F-UPS-not-recognized-as-ugen-tc190332.html#none
  2. Build and install nut-2.6.5 package in pkgsrc/ports tree, with above patch.
  3. In 'ups.conf' configuration file, set blazer_usb as 'driver' and ippon as 'subdriver'. Also set vendorid and productid of the UPS. Here is my example:

[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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment