Here's an updated patch that uses write_one_line. Let me know if you feel
strongly about moving these utilities in a separate module or modules --
especially, if utils get reorganized soon anyway as suggested by lmr@

Darin

On Tue, Feb 2, 2010 at 10:03 AM, Darin Petkov <[email protected]> wrote:

>
>
> On Tue, Feb 2, 2010 at 9:52 AM, Martin Bligh <[email protected]> wrote:
>
>> On Tue, Feb 2, 2010 at 9:44 AM, Darin Petkov <[email protected]> wrote:
>> >
>> >
>> > On Tue, Feb 2, 2010 at 9:31 AM, Martin Bligh <[email protected]> wrote:
>> >>
>> >> +    if utils.system('echo %s > /sys/power/state' % state) != 0:
>> >> +        raise error.TestError('Unable to set power state to ' + state)
>> >>
>> >> That looks odd. If you're looking for a non-zero exit code, it'll throw
>> >> an exception from utils.system?
>> >
>> > So do a bunch of other utilities in base_utils.py -- e.g.
>> get_cpu_family,
>> > get_cpu_vendor, ping_default_gateway... We can certainly do something
>> > different for these routines, of course.
>>
>> Those look different to me, it's not general error checking.
>> ping_default_gateway
>> is close, but it's trying to turn a success or failure into a return code.
>>
>> As is, the code will never hit the != 0 check because it'll throw an
>> exception
>> in system (well, from run inside system)
>>
>
>  Ah, you're right -- the strange default for utils.system's ignore_status.
>
>
>> I'd think the code in the patch would be either just:
>>
>> utils.write_one_line('/sys/power/state', state)
>>
>
> Yes, will changed to that.
>
> Darin
>
>
>>
>> and you'll get the default exceptions or:
>>
>> try:
>>    utils.write_one_line('/sys/power/state', state)
>> except:
>>     raise error.TestError('Unable to set power state to ' + state)
>>
>
>
From 05b8563a5044d1536f7505f7e5c5d16b88fb71e9 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 |   56 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/client/bin/base_utils.py b/client/bin/base_utils.py
index b4f5646..1600348 100644
--- a/client/bin/base_utils.py
+++ b/client/bin/base_utils.py
@@ -665,3 +665,59 @@ 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'.
+    """
+    utils.write_one_line('/sys/class/rtc/rtc0/wakealarm', str(alarm_time))
+
+
+def set_power_state(state):
+    """
+    Set the system power state to 'state'.
+    """
+    utils.write_one_line('/sys/power/state', 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