1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import errno, logging, os, shutil, time
6from autotest_lib.client.bin import test, utils
7from autotest_lib.client.cros import rtc, sys_power
8from autotest_lib.client.common_lib import error
9
10
11def read_rtc_wakeup(rtc_device):
12    """
13    Read the wakeup setting for for the RTC device.
14    """
15    sysfs_path = '/sys/class/rtc/%s/device/power/wakeup' % rtc_device
16    return file(sysfs_path).read().strip()
17
18
19def read_rtc_wakeup_count(rtc_device):
20    """
21    Read the current wakeup count for the RTC device.
22    """
23    sysfs_path = '/sys/class/rtc/%s/device/power/wakeup_count' % rtc_device
24    return int(file(sysfs_path).read())
25
26
27def fire_wakealarm(rtc_device):
28    """
29    Schedule a wakealarm and wait for it to fire.
30    """
31    rtc.set_wake_alarm('+1', rtc_device)
32    time.sleep(2)
33
34
35class power_WakeupRTC(test.test):
36    """Test RTC wake events."""
37
38    version = 1
39
40    def run_once(self):
41        for rtc_device in rtc.get_rtc_devices():
42            self.run_once_rtc(rtc_device)
43
44    def run_once_rtc(self, rtc_device):
45        logging.info('testing rtc device %s', rtc_device)
46
47        # Test that RTC wakeup is enabled
48        rtc_wakeup = read_rtc_wakeup(rtc_device)
49        if rtc_wakeup != 'enabled':
50            raise error.TestError('RTC wakeup is not enabled: %s' % rtc_device)
51
52        # Test that RTC can generate wake events
53        old_sys_wakeup_count = sys_power.read_wakeup_count()
54        old_rtc_wakeup_count = read_rtc_wakeup_count(rtc_device)
55        fire_wakealarm(rtc_device)
56        new_sys_wakeup_count = sys_power.read_wakeup_count()
57        new_rtc_wakeup_count = read_rtc_wakeup_count(rtc_device)
58        if new_rtc_wakeup_count == old_rtc_wakeup_count:
59            raise error.TestFail(
60                    'RTC alarm should increase RTC wakeup_count: %s'
61                    % rtc_device)
62        if new_sys_wakeup_count == old_sys_wakeup_count:
63            raise error.TestFail(
64                    'RTC alarm should increase system wakeup_count: %s'
65                    % rtc_device)
66