config 'etherwake' 'setup'
# interface to use, defaults to 'eth0'
#option 'interface' 'eth0'
+ # wait if the interface is not already up, defaults to 60 secs
+ #option 'interface_timeout' '60'
# send wake-up packet to the broadcast address, defaults to off
#option 'broadcast' 'off'
# Copyright (C) 2009 OpenWrt.org
NAME='etherwake'
-START=60
+START=99
PROGRAM='/usr/bin/etherwake'
start()
{
local value=''
+ local timeout=''
config_load "${NAME}"
config_get value 'setup' 'interface' 'eth0'
[ -n "${value}" ] && append PROGRAM "-i ${value}"
+ # interface timeout
+ config_get timeout 'setup' 'interface_timeout'
+ case "$timeout" in
+ ''|*[!0-9]*)
+ timeout=60
+ ;;
+ esac
+
+ # wait for interface to come up
+ if ! etherwake_wait_for_iface "${value}" "${timeout}"; then
+ echo "Interface ${value} did not come up within ${timeout} seconds"
+ exit 1
+ fi
+
# broadcast
config_get_bool value 'setup' 'broadcast' '0'
[ "${value}" -ne 0 ] && append PROGRAM '-b'
config_foreach etherwake_start target $*
}
+etherwake_wait_for_iface()
+{
+ local iface="$1"
+ local timeout="${2:-1}"
+ local interval=1
+ local elapsed=0
+
+ [ -n "${iface}" ] || return 1
+
+ while [ "${elapsed}" -lt "${timeout}" ]; do
+ if [ "$(ubus call network.interface."${iface}" status 2> /dev/null |
+ jsonfilter -e '@.up' 2> /dev/null)" = "true" ]; then
+ return 0
+ fi
+
+ sleep "${interval}"
+ elapsed=$((elapsed + interval))
+ done
+
+ return 1
+}
+
etherwake_start()
{
local section="$1"
# mac address
config_get value "${section}" 'mac'
- [ -z "${value}" ] && { echo "${initscript}: Target ${section_name} has no MAC address"; return 1; }
+ [ -z "${value}" ] && { echo "Target ${section_name} has no MAC address"; return 1; }
append args "${value}"
# execute command
- echo "${initscript}: Waking up ${section_name} via ${PROGRAM}${args:+ ${args}}"
+ echo "Waking up ${section_name} via ${PROGRAM}${args:+ ${args}}"
${PROGRAM} ${args}
return $?
}