nut: Add USB hotplug support
authorDaniel F. Dickinson <cshored@thecshore.com>
Mon, 27 Aug 2018 20:06:58 +0000 (16:06 -0400)
committerDaniel F. Dickinson <cshored@thecshore.com>
Wed, 29 Aug 2018 04:12:53 +0000 (00:12 -0400)
usbhid-ups has the necessary information in the source, so
let's support USB hotplugging out of the box.  Takes advantage of
the procd support now in nut-server initscript.

Signed-off-by: Daniel F. Dickinson <cshored@thecshore.com>
net/nut/Makefile
net/nut/files/30-libhid-ups.head [new file with mode: 0755]
net/nut/files/30-libhid-ups.tail [new file with mode: 0644]

index 972056959c07f7718e032abc1b798b98586ebdde..d5a573ede7727c46cfdbbd7dc78773a06386faf9 100644 (file)
@@ -80,6 +80,16 @@ define Package/nut-server/install
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/upsdrvctl $(1)/usr/sbin
        $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/share/nut/driver.list $(1)/usr/share/nut/
        ln -sf /var/etc/nut/ups.conf $(1)/etc/nut/ups.conf
+       # Mangle libhid.usermap into a format (hotplug shell script) useful for OpenWrt
+       $(INSTALL_DIR) $(1)/etc/hotplug.d/usb
+       $(INSTALL_BIN) ./files/30-libhid-ups.head $(1)/etc/hotplug.d/usb/30-libhid-ups
+       $(CP) $(PKG_INSTALL_DIR)/etc/hotplug/usb/libhid.usermap $(PKG_BUILD_DIR)/30-libhid-ups.middle
+       $(SED) '/^$$$$/d' \
+               -e '/^#/d' \
+               -E -e 's:^[^ ][^ ]*  *0x0003  *0x0{0,3}([^ ][^ ]*)  *0x{0,3}*([^ ][^ ]*).*:\1/\2/* | \\:' \
+               $(PKG_BUILD_DIR)/30-libhid-ups.middle
+       tail -n+2 $(PKG_BUILD_DIR)/30-libhid-ups.middle >>$(1)/etc/hotplug.d/usb/30-libhid-ups
+       cat ./files/30-libhid-ups.tail >>$(1)/etc/hotplug.d/usb/30-libhid-ups
 endef
 
 define Package/nut-common
@@ -515,6 +525,7 @@ CONFIGURE_ARGS += \
        --without-neon \
        --without-powerman \
        --without-wrap \
+       --with-hotplug-dir=/etc/hotplug \
        --with-cgi \
        --without-ipmi \
        --without-freeipmi \
diff --git a/net/nut/files/30-libhid-ups.head b/net/nut/files/30-libhid-ups.head
new file mode 100755 (executable)
index 0000000..efcbcf6
--- /dev/null
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+. /lib/functions.sh
+
+nut_driver_config() {
+       local cfg="$1"
+       local nomatch="$2"
+
+       config_get runas "$cfg" runas "nut"
+       config_get vendorid "$cfg" vendorid
+       config_get productid "$cfg" productid
+
+       [ "$ACTION" = "add" ] &&[ -n "$DEVNAME" ] && {
+               chmod 0660 /dev/"$DEVNAME"
+               chown ${runas:-root}:$(id -gn "${runas:-root}") /dev/"$DEVNAME"
+       }
+
+       if [ "$(printf "%04x" 0x"$pvendid")" = "$vendorid" ] && \
+               [ "$(printf "%04x" 0x"$pprodid")" = "$productid" ]; then
+                       [ "$ACTION" = "add" ] && {
+                               /etc/init.d/nut-server start "$cfg"
+                       }
+                       [ "$ACTION" = "remove" ] && {
+                               /etc/init.d/nut-server stop "$cfg"
+                       }
+                       found=1
+       elif [ "$nomatch" = "1" ]; then
+               [ "$ACTION" = "add" ] && {
+                       /etc/init.d/nut-server start "$cfg"
+               }
+               [ "$ACTION" = "remove" ] && {
+                       /etc/init.d/nut-server stop "$cfg"
+               }
+       fi
+}
+
+perform_libhid_action() {
+       local vendorid productid runas
+       local pvendid pprodid found
+
+       pvendid=${PRODUCT%/*}
+       pvendid=${pvendid%/*}
+       pprodid=${PRODUCT%/*}
+       pprodid=${pprodid##*/}
+
+       config_load nut_server
+       config_foreach nut_driver_config driver 0
+       [ "$found" != "1" ] && config_foreach nut_driver_config driver 1
+}
+
+[ -n "$PRODUCT" ] && case "$PRODUCT" in
diff --git a/net/nut/files/30-libhid-ups.tail b/net/nut/files/30-libhid-ups.tail
new file mode 100644 (file)
index 0000000..24c2a96
--- /dev/null
@@ -0,0 +1,5 @@
+"")
+       [ -d /var/run/nut ] && [ ! -f /var/run/nut/disable-hotplug ] && \
+               /etc/init.d/nut-server enabled &&  perform_libhid_action
+       ;;
+esac