blob: e04a896b38e5775dfcb1da15f7ebae1e0985520f [file] [log] [blame]
Paul Stewart23cfe942010-09-29 16:32:31 -07001#!/usr/bin/python
2
3"""Tell system to suspend, and resume some number of seconds later.
4
5Use the RTC to generate a wakeup some number of seconds into the
6future, then go to sleep. Note that this module is not aware of
7the actual time when the system will suspend. Depending on other
8system activities, there may be several seconds between when this
9script runs until when the system actually goes to sleep. In fact
10that time may be after the wakeup has been scheduled, and the
11system may never wake up! It is up to the caller to make prudent
12decisions as to upper bound of delay before going to sleep, and to
13choose a wakeup time greater than this interval.
14"""
15
16import os, re, subprocess, sys
17import rtc, sys_power
18
19time_to_sleep = 30
20if len(sys.argv) > 1:
21 time_to_sleep = int(sys.argv[1])
22
23if len(sys.argv) > 2:
24 after_command = ' '.join(sys.argv[2:])
25else:
26 after_command = None
27
28rtc.set_wake_alarm(rtc.get_seconds() + time_to_sleep)
Paul Stewart42a77a02010-10-01 15:45:54 -070029
30# We want output from suspend_to_ram to go to stderr so that
31# tests that depend on the output of after_command won't have
32# their output polluted
33saveout = os.dup(sys.stdout.fileno())
34os.dup2(sys.stderr.fileno(), sys.stdout.fileno())
Paul Stewart23cfe942010-09-29 16:32:31 -070035sys_power.suspend_to_ram()
Paul Stewart42a77a02010-10-01 15:45:54 -070036os.dup2(saveout, sys.stdout.fileno())
Paul Stewart23cfe942010-09-29 16:32:31 -070037
38if after_command:
39 os.system(after_command)