This patch adds routines for reading the hardware real-time clock, setting
the
 RTC-based wake alarm and managing the system power state (standby,
 suspend-to-ram, suspend-to-disk).
From cb43d909c0347d6a30a11809bb741cd31659eda4 Mon Sep 17 00:00:00 2001
From: Darin Petkov <[email protected]>
Date: Mon, 1 Feb 2010 23:05:22 -0800
Subject: [PATCH] Base utilities for reading the hwclock and system 
suspend/resume.
 This patch adds routines for reading the hardware real-time clock, setting the
 RTC-based wake alarm and managing the system power state (standby,
 suspend-to-ram, suspend-to-disk).

---
 client/bin/base_utils.py |   59 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/client/bin/base_utils.py b/client/bin/base_utils.py
index b4f5646..43148d5 100644
--- a/client/bin/base_utils.py
+++ b/client/bin/base_utils.py
@@ -665,3 +665,62 @@ def process_is_alive(name_pattern):
     """
     return utils.system("pgrep -f '^([^ /]*/)*(%s)([ ]|$)'" % name_pattern,
                         ignore_status=True) == 0
+
+
+def get_hwclock_seconds(utc=True):
+    """
+    Return the hardware clock in seconds as a floating point value.
+    Use Coordinated Universal Time if utc is True, local time otherwise.
+    Raise a ValueError if unable to read the hardware clock.
+    """
+    cmd = '/sbin/hwclock --debug'
+    if utc:
+        cmd += ' --utc'
+    hwclock_output = utils.system_output(cmd, ignore_status=True)
+    match = re.search(r'= ([0-9]+) seconds since .+ (-?[0-9.]+) seconds$',
+                      hwclock_output, re.DOTALL)
+    if match:
+        seconds = int(match.group(1)) + float(match.group(2))
+        logging.debug('hwclock seconds = %f' % seconds)
+        return seconds
+
+    raise ValueError('Unable to read the hardware clock -- ' +
+                     hwclock_output)
+
+
+def set_wake_alarm(alarm_time):
+    """
+    Set the hardware RTC-based wake alarm to 'alarm_time'.
+    """
+    set_wake_cmd = 'echo %d > /sys/class/rtc/rtc0/wakealarm' % alarm_time
+    if utils.system(set_wake_cmd) != 0:
+        raise error.TestError('Unable to set the wake alarm')
+
+
+def set_power_state(state):
+    """
+    Set the system power state to 'state'.
+    """
+    if utils.system('echo %s > /sys/power/state' % state) != 0:
+        raise error.TestError('Unable to set power state to ' + state)
+
+
+def standby():
+    """
+    Power-on suspend (S1)
+    """
+    set_power_state('standby')
+
+
+def suspend_to_ram():
+    """
+    Suspend the system to RAM (S3)
+    """
+    set_power_state('mem')
+
+
+def suspend_to_disk():
+    """
+    Suspend the system to disk (S4)
+    """
+    set_power_state('disk')
-- 
1.6.6

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to