#!/bin/sh
+# shellcheck disable=SC3043 # aka 'local VAR'
# c 2024 systemcrash (GitHub)
#hotplug.d triggers this script on the plug {in|out} of USB printers
DAEMON_HOTPLUG="$DAEMON hotplug"
DAEMON_ERR="daemon.err"
DAEMON_INFO="daemon.info"
-DRIVER_HOME_DEFAULT="/opt/"$DAEMON"_drivers"
+DRIVER_HOME_DEFAULT=/opt/"$DAEMON"_drivers
SYSUPGRADE_CONF="/etc/sysupgrade.conf"
# Assumptions:
ACTUAL_DEVPATH=${DEVPATH%/usbmisc/lp*}
# Prepend /sys/ to get actual device path,
ACTUAL_DEVPATH="/sys$ACTUAL_DEVPATH"
-[ $DEBUG ] && echo ACTUAL_DEVPATH is $ACTUAL_DEVPATH
+[ "$DEBUG" ] && echo ACTUAL_DEVPATH is "$ACTUAL_DEVPATH"
PARENT_DEVPATH=$( dirname "${ACTUAL_DEVPATH}" )
# We might need to do this if symlinks are problematic. Might not:
# devpath="$(readlink -f $ACTUAL_DEVPATH)"
# https://www.usb.org/sites/default/files/usbprint11a021811.pdf
# Check whether connected device is a "Printer"
-[ "$(cat "$ACTUAL_DEVPATH/bInterfaceClass")" == "07" ] && [ "$(cat "$ACTUAL_DEVPATH/bInterfaceSubClass")" == "01" ] && iAmAPrinter=true
+[ "$(cat "$ACTUAL_DEVPATH/bInterfaceClass")" = "07" ] && [ "$(cat "$ACTUAL_DEVPATH/bInterfaceSubClass")" = "01" ] && iAmAPrinter=true
# Not a printer? Bail.
-[ ! $iAmAPrinter ] && exit 0
+[ ! "$iAmAPrinter" ] && exit 0
# Port directionality
BIP=$( cat "$ACTUAL_DEVPATH/bInterfaceProtocol" )
-[ $DEBUG ] && echo BIP: $BIP
+[ "$DEBUG" ] && echo BIP: "$BIP"
case $BIP in
01 )
BIDIR=0
# printer driver blobs account for different hw versions anyway, so ignore it.
# THIS_USB_VIDPID="3f0/4117"
-idVendor=$( cat ""$PARENT_DEVPATH"/idVendor" )
-idProduct=$( cat ""$PARENT_DEVPATH"/idProduct" )
-[ $DEBUG ] && echo idVendor+idProduct: $idVendor + $idProduct
+idVendor=$( cat "$PARENT_DEVPATH/idVendor" )
+idProduct=$( cat "$PARENT_DEVPATH/idProduct" )
+[ "$DEBUG" ] && echo idVendor+idProduct: "$idVendor" + "$idProduct"
THIS_USB_VIDPID="$idVendor/$idProduct"
# Driver blob e.g.: Hewlett-Packard_HP_LaserJet_1018_03f0_4117.bin
# Not always available:
iSerialNumber=$( cat "$PARENT_DEVPATH/iSerialNumber" 2>/dev/null ) || iSerialNumber=$( cat "$PARENT_DEVPATH/serial" 2>/dev/null )
-[ $DEBUG ] && echo iSerialNumber is $iSerialNumber
+[ "$DEBUG" ] && echo iSerialNumber is "$iSerialNumber"
# Get the special IEEE1284 Device ID string (apparently limited to 127 chars)
-ieee1284info=$(cat ""$ACTUAL_DEVPATH"/ieee1284_id" )
-[ $DEBUG ] && echo ieee1284info is $ieee1284info
+ieee1284info=$(cat "$ACTUAL_DEVPATH/ieee1284_id" )
+[ "$DEBUG" ] && echo ieee1284info is "$ieee1284info"
# Absent the uci daemon hotplug config group, set it to a default
-[ -z $(uci -q get $DAEMON.@hotplug[0]) ] && uci -q add $DAEMON hotplug
+[ -z "$(uci -q get $DAEMON.@hotplug[0])" ] && uci -q add $DAEMON hotplug
# # Absent the driver_home path config, set it to a default
-[ -z $(uci -q get $DAEMON.@hotplug[0].driver_home) ] && uci -q set $DAEMON.@hotplug[-1].driver_home=$DRIVER_HOME_DEFAULT && uci -q commit $DAEMON
+[ -z "$(uci -q get $DAEMON.@hotplug[0].driver_home)" ] && uci -q set $DAEMON.@hotplug[-1].driver_home=$DRIVER_HOME_DEFAULT && uci -q commit $DAEMON
# Make the driver folder hierarchy
-if ! $(mkdir -p $DRIVER_HOME_DEFAULT); then
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_ERR "Error running 'mkdir -p "$DRIVER_HOME_DEFAULT"'."
+if ! mkdir -p "$DRIVER_HOME_DEFAULT"; then
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_ERR" Error running 'mkdir -p' "$DRIVER_HOME_DEFAULT".
fi
# Help the folder survive a sysupgrade:
-if ! $( grep -q "^$DRIVER_HOME_DEFAULT$" "$SYSUPGRADE_CONF" ); then
+if ! grep -q "^$DRIVER_HOME_DEFAULT$" "$SYSUPGRADE_CONF" ; then
# TODO: remove old non-existent p910nd paths from $SYSUPGRADE_CONF?
# Absent the path, try to add it to $SYSUPGRADE_CONF
if ! echo $DRIVER_HOME_DEFAULT >> $SYSUPGRADE_CONF; then
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_ERR "Problem adding "$DRIVER_HOME_DEFAULT" path to $SYSUPGRADE_CONF."
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_ERR" Problem adding "$DRIVER_HOME_DEFAULT" path to "$SYSUPGRADE_CONF."
else
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_INFO "Added "$DRIVER_HOME_DEFAULT" path to $SYSUPGRADE_CONF."
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_INFO" Added "$DRIVER_HOME_DEFAULT" path to "$SYSUPGRADE_CONF".
fi
fi
DRIVER_HOME=$(uci -q get $DAEMON.@hotplug[-1].driver_home)
-[ $DEBUG ] && echo DRIVER_HOME is $DRIVER_HOME
+[ "$DEBUG" ] && echo DRIVER_HOME is "$DRIVER_HOME"
# Trim trailing forward slash if it crept in somehow.
DRIVER_HOME=${DRIVER_HOME%/}
-DRIVER_BLOBNAME_TAIL=""$idVendor"_"$idProduct".bin"
-[ $DEBUG ] && echo DRIVER_BLOBNAME_TAIL is $DRIVER_BLOBNAME_TAIL
+DRIVER_BLOBNAME_TAIL="$idVendor"_"$idProduct".bin
+[ "$DEBUG" ] && echo DRIVER_BLOBNAME_TAIL is "$DRIVER_BLOBNAME_TAIL"
# Global device config number variable
# find which daemon configs have the matching lpX interface
match_current_device() {
# Build array of /dev/usb/lpX character devices already configured
- set -- $(IFS="\n" && uci -q batch <<- EOI
- get $DAEMON.@$DAEMON[0].device
- get $DAEMON.@$DAEMON[1].device
- get $DAEMON.@$DAEMON[2].device
- get $DAEMON.@$DAEMON[3].device
- get $DAEMON.@$DAEMON[4].device
- get $DAEMON.@$DAEMON[5].device
- get $DAEMON.@$DAEMON[6].device
- get $DAEMON.@$DAEMON[7].device
- get $DAEMON.@$DAEMON[8].device
- get $DAEMON.@$DAEMON[9].device
+
+ set -- "$(IFS=$(printf '\n') && uci -q batch <<- EOI
+ get "$DAEMON".@"$DAEMON"[0].device
+ get "$DAEMON".@"$DAEMON"[1].device
+ get "$DAEMON".@"$DAEMON"[2].device
+ get "$DAEMON".@"$DAEMON"[3].device
+ get "$DAEMON".@"$DAEMON"[4].device
+ get "$DAEMON".@"$DAEMON"[5].device
+ get "$DAEMON".@"$DAEMON"[6].device
+ get "$DAEMON".@"$DAEMON"[7].device
+ get "$DAEMON".@"$DAEMON"[8].device
+ get "$DAEMON".@"$DAEMON"[9].device
EOI
- )
+ )"
# $1-$10 are now set
x=0
+ # shellcheck disable=SC2068
for i in $@; do
# $DEVNAME is passed by hotplug
- [ $DEBUG ] && echo UCI_DEV_CFG_NUMBER is $UCI_DEV_CFG_NUMBER and CHAR_DEV is $CHAR_DEV
- [ $DEVNAME == ${i#/dev/} ] && UCI_DEV_CFG_NUMBER=$x && CHAR_DEV=$i
+ [ "$DEBUG" ] && echo UCI_DEV_CFG_NUMBER is $UCI_DEV_CFG_NUMBER and CHAR_DEV is "$CHAR_DEV"
+ [ "$DEVNAME" = "${i#/dev/}" ] && UCI_DEV_CFG_NUMBER=$x && CHAR_DEV=$i
# TODO: multiple configured devices could have same CHAR_DEV if not connected concurrently
- x=$(( $x+1 ))
+ x=$(( x+1 ))
done
}
# Build array of /dev/usb/lpX character devices already configured
match_current_device
- uqgddu_cmd="uci -q get $DAEMON.@$DAEMON[$UCI_DEV_CFG_NUMBER]"
- uqsddu_cmd="uci -q set $DAEMON.@$DAEMON[$UCI_DEV_CFG_NUMBER]"
+ uqgddu_cmd='uci -q get '"$DAEMON".@"$DAEMON"[$UCI_DEV_CFG_NUMBER]
+ uqsddu_cmd='uci -q set '"$DAEMON".@"$DAEMON"[$UCI_DEV_CFG_NUMBER]
# set Internal Field Separator to semicolon found in ieee1284_id files
IFS=";"
# Got 1284 Device ID string
- set -- $ieee1284info
- [ $DEBUG ] && echo ieee1284info: $ieee1284info
+ set -- "$ieee1284info"
+ [ "$DEBUG" ] && echo ieee1284info: "$ieee1284info"
- for i in "$@"; do
- [ $DEBUG ] && echo i:"$i"
+ # shellcheck disable=SC2068
+ for i in $@; do
+ [ "$DEBUG" ] && echo i:"$i"
case $i in
MFG:* | MANUFACTURER:* | MFR:* )
VER=${i##*:};;
esac
- [ -n "$SN" ] || SN=$iSerialNumber
- [ $DEBUG ] && echo ${MFG:+MFG=$MFG} ${MDL:+MDL=$MDL} ${CMD:+CMD=$CMD} ${CLS:+CLS=$CLS} ${DES:+DES=$DES} ${SN:+SN=$SN}
+ [ -n "$SN" ] || SN="$iSerialNumber"
+ [ "$DEBUG" ] && echo ${MFG:+MFG=$MFG} ${MDL:+MDL=$MDL} ${CMD:+CMD=$CMD} ${CLS:+CLS=$CLS} ${DES:+DES=$DES} ${SN:+SN=$SN}
- [ $DEBUG ] && echo 'uci set' for UCI_DEV_CFG_NUMBER: $UCI_DEV_CFG_NUMBER
+ [ "$DEBUG" ] && echo 'uci set' for UCI_DEV_CFG_NUMBER: $UCI_DEV_CFG_NUMBER
# Take the USB info as fact: set bidir regardless. It seems to be a source of confusion.
eval "$uqsddu_cmd.bidirectional='$BIDIR'"
[ -z "$(eval "$uqgddu_cmd".port)" ] && eval "$uqsddu_cmd.port='0'"
# No previously configured device? Configure this instance. Set CHAR_DEV so we can send driver.
[ $UCI_DEV_CFG_NUMBER -eq -1 ] && eval "$uqsddu_cmd.device=/dev/'$DEVNAME'" && CHAR_DEV=/dev/"$DEVNAME"
- if [ -n "$MFG" ] && [ -n "$MDL" ] && [ -n "$DRIVER_BLOBNAME_TAIL" ] && [ -z "$($uqgddu_cmd.driver_file)" ]; then
+ if [ -n "$MFG" ] && [ -n "$MDL" ] && [ -n "$DRIVER_BLOBNAME_TAIL" ] && [ -z "$(eval "$uqgddu_cmd".driver_file)" ]; then
DRIVER_FILE="$MFG"_"$MDL"_"$DRIVER_BLOBNAME_TAIL"
# Make blob filename more friendly: change space to underscore
- DRIVER_FILE="$DRIVER_HOME"/"${DRIVER_FILE// /_}"
- [ $DEBUG ] && echo DRIVER_FILE: $DRIVER_FILE
- uci set $DAEMON.@$DAEMON[$UCI_DEV_CFG_NUMBER].driver_file="$DRIVER_FILE"
+ DRIVER_FILE="$DRIVER_HOME"/"$(echo "$DRIVER_FILE" | sed 's/ /_/g')"
+ [ "$DEBUG" ] && echo DRIVER_FILE: "$DRIVER_FILE"
+ uci set "$DAEMON".@"$DAEMON"[$UCI_DEV_CFG_NUMBER].driver_file="$DRIVER_FILE"
fi
done
}
daemon_restart() {
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_INFO "(Re)starting $DAEMON"
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_INFO" "(Re)starting $DAEMON"
/etc/init.d/$DAEMON restart
}
daemon_stop() {
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_INFO "Stopping $DAEMON"
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_INFO" "Stopping $DAEMON"
/etc/init.d/$DAEMON stop
}
send_driver() {
- DRIVER_FILE=$( uci -q get $DAEMON.@$DAEMON[$UCI_DEV_CFG_NUMBER].driver_file )
+ DRIVER_FILE=$( uci -q get "$DAEMON".@"$DAEMON"[$UCI_DEV_CFG_NUMBER].driver_file )
- if [ -e $DRIVER_FILE ]; then
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_INFO "Sending driver to $DAEMON printer $THIS_USB_VIDPID"
+ if [ -e "$DRIVER_FILE" ]; then
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_INFO" "Sending driver to $DAEMON printer $THIS_USB_VIDPID"
- if ! cat $DRIVER_FILE > $CHAR_DEV; then
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_ERR "Sending driver to $CHAR_DEV [ $THIS_USB_VIDPID ] failed for some reason."
+ if ! cat "$DRIVER_FILE" > "$CHAR_DEV"; then
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_ERR" "Sending driver to $CHAR_DEV [ $THIS_USB_VIDPID ] failed for some reason."
else
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_INFO "Sent $DRIVER_FILE to $CHAR_DEV [ $THIS_USB_VIDPID ]."
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_INFO" "Sent $DRIVER_FILE to $CHAR_DEV [ $THIS_USB_VIDPID ]."
daemon_restart
fi
else
- logger -t "$DAEMON_HOTPLUG" -p $DAEMON_ERR "Missing driver file: $DRIVER_FILE for $CHAR_DEV [ $THIS_USB_VIDPID ] (please upload it)."
+ logger -t "$DAEMON_HOTPLUG" -p "$DAEMON_ERR" "Missing driver file: $DRIVER_FILE for $CHAR_DEV [ $THIS_USB_VIDPID ] (please upload it)."
fi
}
get_and_store_printer_info
- [ $DEBUG ] && echo THIS_USB_VIDPID: $THIS_USB_VIDPID
- [ $DEBUG ] && echo CHAR_DEV: $CHAR_DEV
+ [ "$DEBUG" ] && echo THIS_USB_VIDPID: "$THIS_USB_VIDPID"
+ [ "$DEBUG" ] && echo CHAR_DEV: "$CHAR_DEV"
# usb subsys only:
- # [ $DEBUG ] && echo DEVTYPE: $DEVTYPE
- # [ $DEBUG ] && echo DEV_TYPE_FILTER: $DEV_TYPE_FILTER
- # [ $DEBUG ] && echo PRODUCT: $PRODUCT
+ # [ "$DEBUG" ] && echo DEVTYPE: $DEVTYPE
+ # [ "$DEBUG" ] && echo DEV_TYPE_FILTER: $DEV_TYPE_FILTER
+ # [ "$DEBUG" ] && echo PRODUCT: $PRODUCT
# Extra checks available when run as hotplug usb script:
# [ "$DEVTYPE" == "${DEV_TYPE_FILTER}" ]
# [ -z "${PRODUCT##*$THIS_USB_VIDPID*}" ]
# Ensure dev is character device
- if [ -n "$THIS_USB_VIDPID" ] && [ -c $CHAR_DEV ]; then
+ if [ -n "$THIS_USB_VIDPID" ] && [ -c "$CHAR_DEV" ]; then
# if zero string, i.e. usb_ID is a match for $PRODUCT supplied by hotplug
- if [ $(uci -q get $DAEMON.@$DAEMON[$UCI_DEV_CFG_NUMBER].usbvidpid) == "$THIS_USB_VIDPID" ]; then
- [ $DEBUG ] && echo "THIS_USB_VIDPID match for $DAEMON device $THIS_USB_VIDPID."
+ if [ "$(uci -q get "$DAEMON".@"$DAEMON"[$UCI_DEV_CFG_NUMBER].usbvidpid)" = "$THIS_USB_VIDPID" ]; then
+ [ "$DEBUG" ] && echo "THIS_USB_VIDPID match for $DAEMON device $THIS_USB_VIDPID."
send_driver
else
- [ $DEBUG ] && echo "No THIS_USB_VIDPID match."
+ [ "$DEBUG" ] && echo "No THIS_USB_VIDPID match."
fi
fi
esac
# Commit any changes
-[ -n $( uci -q changes $DAEMON ) ] && uci commit $DAEMON
+[ -n "$( uci -q changes $DAEMON )" ] && uci commit $DAEMON