• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 os
7import time
8from autotest_lib.client.bin import test
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros import rtc
11from autotest_lib.client.cros.power import sys_power
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    if os.path.isfile(sysfs_path):
19        return file(sysfs_path).read().strip()
20
21
22def read_rtc_wakeup_active_count(rtc_device):
23    """
24    Read the current wakeup active count for the RTC device.
25    """
26    path = '/sys/class/rtc/%s/device/power/wakeup_active_count' % rtc_device
27    return int(file(path).read())
28
29
30def fire_wakealarm(rtc_device):
31    """
32    Schedule a wakealarm and wait for it to fire.
33    """
34    rtc.set_wake_alarm('+1', rtc_device)
35    time.sleep(2)
36
37
38class power_WakeupRTC(test.test):
39    """Test RTC wake events."""
40
41    version = 1
42
43    def run_once(self):
44        """
45        Tests that RTC devices generate wakeup events.
46        We require /dev/rtc0 to work since there are many things which rely
47        on the rtc0 wakeup alarm. For all the other RTCs, only test those
48        that have wakeup alarm capabilities.
49        """
50        default_rtc = "/dev/rtc0"
51        if not os.path.exists(default_rtc):
52            raise error.TestFail('RTC device %s does not exist' % default_rtc)
53        default_rtc_device = os.path.basename(default_rtc)
54        if read_rtc_wakeup(default_rtc_device) != 'enabled':
55            raise error.TestFail('RTC wakeup is not enabled: %s' % default_rtc_device)
56        for rtc_device in rtc.get_rtc_devices():
57            if read_rtc_wakeup(rtc_device) != 'enabled':
58                logging.info('RTC wakeup is not enabled: %s' % rtc_device)
59            else:
60                logging.info('RTC wakeup is enabled for: %s' % rtc_device)
61                self.run_once_rtc(rtc_device)
62
63    def run_once_rtc(self, rtc_device):
64        """Tests that a RTC device generate wakeup events.
65
66        @param rtc_device: RTC device to be tested.
67        """
68        logging.info('testing rtc device %s', rtc_device)
69
70        # Test that RTC can generate wake events
71        old_sys_wakeup_count = sys_power.read_wakeup_count()
72        old_rtc_wakeup_active_count = read_rtc_wakeup_active_count(rtc_device)
73        fire_wakealarm(rtc_device)
74        new_sys_wakeup_count = sys_power.read_wakeup_count()
75        new_rtc_wakeup_active_count = read_rtc_wakeup_active_count(rtc_device)
76        if new_rtc_wakeup_active_count == old_rtc_wakeup_active_count:
77            raise error.TestFail(
78                    'RTC alarm should increase RTC wakeup_active_count: %s'
79                    % rtc_device)
80        if new_sys_wakeup_count == old_sys_wakeup_count:
81            raise error.TestFail(
82                    'RTC alarm should increase system wakeup_count: %s'
83                    % rtc_device)
84