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 logging
6import time
7from autotest_lib.client.bin import test
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import rtc
10from autotest_lib.client.cros.power import sys_power
11
12
13def read_rtc_wakeup(rtc_device):
14    """
15    Read the wakeup setting for for the RTC device.
16    """
17    sysfs_path = '/sys/class/rtc/%s/device/power/wakeup' % rtc_device
18    return file(sysfs_path).read().strip()
19
20
21def read_rtc_wakeup_active_count(rtc_device):
22    """
23    Read the current wakeup active count for the RTC device.
24    """
25    path = '/sys/class/rtc/%s/device/power/wakeup_active_count' % rtc_device
26    return int(file(path).read())
27
28
29def fire_wakealarm(rtc_device):
30    """
31    Schedule a wakealarm and wait for it to fire.
32    """
33    rtc.set_wake_alarm('+1', rtc_device)
34    time.sleep(2)
35
36
37class power_WakeupRTC(test.test):
38    """Test RTC wake events."""
39
40    version = 1
41
42    def run_once(self):
43        """Tests that RTC devices generate wakeup events."""
44        for rtc_device in rtc.get_rtc_devices():
45            self.run_once_rtc(rtc_device)
46
47    def run_once_rtc(self, rtc_device):
48        """Tests that a RTC device generate wakeup events.
49
50        @param rtc_device: RTC device to be tested.
51        """
52        logging.info('testing rtc device %s', rtc_device)
53
54        # Test that RTC wakeup is enabled
55        rtc_wakeup = read_rtc_wakeup(rtc_device)
56        if rtc_wakeup != 'enabled':
57            raise error.TestError('RTC wakeup is not enabled: %s' % rtc_device)
58
59        # Test that RTC can generate wake events
60        old_sys_wakeup_count = sys_power.read_wakeup_count()
61        old_rtc_wakeup_active_count = read_rtc_wakeup_active_count(rtc_device)
62        fire_wakealarm(rtc_device)
63        new_sys_wakeup_count = sys_power.read_wakeup_count()
64        new_rtc_wakeup_active_count = read_rtc_wakeup_active_count(rtc_device)
65        if new_rtc_wakeup_active_count == old_rtc_wakeup_active_count:
66            raise error.TestFail(
67                    'RTC alarm should increase RTC wakeup_active_count: %s'
68                    % rtc_device)
69        if new_sys_wakeup_count == old_sys_wakeup_count:
70            raise error.TestFail(
71                    'RTC alarm should increase system wakeup_count: %s'
72                    % rtc_device)
73