Here is an updated and probably final version.
It uses lipc-wait-event to not have to busy-wait, so there is virtually no additional overhead now. Precision is plus/minus a second at best, though the RTC clock may wake up several seconds earlier/later. But this should be okay.
N.B.: My wifi issue is only at work, so this must be a network problem (probably need a higher timeout). At home it is working flawlessly.
Code:
RTC=0
currentTime () {
date +%s
}
wait_for () {
# calculate the time we should return
ENDWAIT=$(( $(currentTime) + $1 ))
# disable/reset current alarm
echo 0 > /sys/class/rtc/rtc$RTC/wakealarm
# set new alarm
echo $ENDWAIT > /sys/class/rtc/rtc$RTC/wakealarm
# check whether we could set the alarm successfully
if [ $ENDWAIT -eq `cat /sys/class/rtc/rtc$RTC/wakealarm` ]; then
# wait for timeout to expire
while [ $(currentTime) -lt $ENDWAIT ]; do
REMAININGWAITTIME=$(( $ENDWAIT - $(currentTime) ))
if [ 0 -lt $REMAININGWAITTIME ]; then
# wait for device to resume from sleep, or for time out
lipc-wait-event -s $REMAININGWAITTIME com.lab126.powerd resuming || true
fi
done
else
echo "Failure setting alarm on rtc$RTC, wanted $ENDWAIT, got `cat /sys/class/rtc/rtc$RTC/wakealarm`"
fi
# not sure whether this is required
lipc-set-prop com.lab126.powerd -i deferSuspend 1
}
# wait 2 hours and then wake up
wait_for 120
echo "Good morning"