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