An Alarm Clock to Wakeup MythTV for Recording
"Hi, is this the front desk?", she said into the phone. "Yes, How can I help you?" inquired the desk clerk. "Well, this is Myth Dora in room 192.168.1.104. I am going to take a nap, and would like a wake up call for 9:00 PM." The clerk replied "Yes ma'am. A quick nap before a big night on the town?". "Oh no, I just want to recharge my batteries before my favorite show comes on TV. ", she explained.
MythTV is great at recording scheduled programs. But the scheduler can do more than just start and stop recording. It can also power off the computer and then wake it up again just in time to start a recording.
While MythTV may be able to record programs right out-of-the-box, the ability to automatically power up and down the computer requires some user configuration. This is because of operating system and hardware dependencies that have numerous variations. Too many in fact for MythTV to figure out on its own.
To simplify matters, we will describe how to enable the power control feature for the MythDora distribution. We chose MythDora because it is very popular and works quite well. Version 5.0 of MythDora is current as of this writing and includes MythTV version 0.21.
MythTV, or MythDora in this case, can determine when it wants to wake up, but like the story above, it needs a "desk clerk" to actually make the wake up call. In this case, the desk clerk is a function of the motherboard, called the ACPI. It uses the clock (RTC), which runs even when the power is off, to wake up the processor when the alarm time is reached.
Apply Glue Between MythTV and the ACPI
What is not provided by MythTV or MythDora is the ability to write this wakeup alarm time to the motherboard ACPI. MythTV does provide a hook that allows for user provided code that sets the alarm time. In the case of MythDora this user code can be as little as:
#!/bin/sh echo $* > /proc/acpi/alarm
This little bit of code copies the time provided by MythTV into the Linux ACPI system. Actually, it's a little too simplistic to do the job entirely. You can read Setting MythTV Wakeup Time for a discussion of a full version this program with the source code listing. That version accepts any formatted time string, and corrects for some schedule conflicts. It is also compatible with Linux distributions other than MythDora (Fedora).
We'll assume that you saved this program, in a file called/usr/bin/setwakeup.sh. MythTV then needs to be told
where to find the program. Sadly there is not one but two places this must
be done. This is because the mythbackend server and the
mythwelcome program get their settings separately. See the
guides Setting
MythTV Backend Shutdown/Wakeup Options and
Setting
MythShutdown/MythWelcome Options.
The /usr/bin/setwakeup.sh program needs root privileges to
operate properly. Therefore it needs to be added to the sudoers
list. Edit the file /etc/sudoers and add
/usr/bin/setwakeup.sh to the list of files that are assigned to
Cmnd_Alias MYTHDORA. It should look similar to the
following:
Cmnd_Alias MYTHDORA= /usr/bin/setwakeup.sh, ...
May the Clock and the ACPI Play Nicely Together
The ACPI wakeup time gets corrupted during the shutdown sequence, and we
need to patch existing code to prevent this problem. As part of the computer
shutdown sequence, the script /etc/init.d/halt gets executed.
Part of the job of this script is to synchronize the system time back to the
hardware clock (RTC). The system time is the more accurate of the two clocks
because it is corrected by the
NTP. The
hardware clock is only as accurate as its crystal oscillator and tends to
drift slightly from correct time.
During this synchronization process the ACPI alarm is cleared. This occurs
in the script /etc/init.d/halt at the line that looks similar to
this:
[ -x /sbin/hwclock ] &&
action $"Syncing hardware clock to system time" /sbin/hw clock $CLOCKFLAGS
The workaround for this problem is to save the ACPI alarm setting into
the variable ACPITIME, just prior to the RTC synchronization.
Afterwards, the value in ACPITIME is copied back to the ACPI
alarm. The updated code is as follows:
ACPITIME='cat /proc/acpi/alarm'
[ -x /sbin/hwclock ] &&
action $"Syncing hardware clock to system time" /sbin/hw clock $CLOCKFLAGS
CHKTIME='date -d "$ACPITIME" "+%F %H:%M:%S"'
if [ "$ACPITIME" == "$CHKTIME" ] &&
[[ ! "$ACPITIME" =~ ".......... 00:00:00" ]]; then
echo $ACPITIME > /proc/acpi/alarm
fi
Notice that we are not restoring the ACPI alarm unless it meets a couple
of tests. The reason is that we don't know if the time that we saved in
ACPITIME is a real wakeup up time, or is just some vestigial
garbage leftover in the RTC. Failure to perform these tests WILL result in
your MythTV box waking up at unscheduled and undesired times.
We start by checking if ACPITIME represents a valid time and
date.
CHKTIME='date -d "$ACPITIME" "+%F %H:%M:%S"'
The date command is used to reprint the ACPITIME
in the identical format. The date command will generate the
exact same string if the input data is valid. So all we have to do is compare
the two strings:
if [ "$ACPITIME" == "$CHKTIME" ] &&
The second test checks if the time portion is all zeros:
[[ ! "$ACPITIME" =~ ".......... 00:00:00" ]]; then
This value is valid for midnight, but this is usually an indicator that a wakeup has not been scheduled. We can safely ignore all midnight settings because any TV program that starts at midnight would initiate a wakeup time several minutes before twelve.
Success on both of the previous tests means that a wake up has indeed
been scheduled. Thus all we need do now is restore the original
ACPITIME value back into the Linux ACPI system.
echo $ACPITIME > /proc/acpi/alarm

