• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can
3# be # found in the LICENSE file.
4
5"""Provides utility methods for the Real Time Clock device.
6"""
7
8import errno, glob, os
9
10
11def get_rtc_devices():
12    """
13    Return a list of all RTC device names on the system.
14
15    The RTC device node will be found at /dev/$NAME.
16    """
17    return [os.path.basename(rtc) for rtc in glob.glob('/sys/class/rtc/*')]
18
19
20def get_seconds(utc=True, rtc_device='rtc0'):
21    """
22    Read the current time out of the RTC
23    """
24    return int(file('/sys/class/rtc/%s/since_epoch' % rtc_device).readline())
25
26
27def write_wake_alarm(alarm_time, rtc_device='rtc0'):
28    """
29    Write a value to the wake alarm
30    """
31    f = file('/sys/class/rtc/%s/wakealarm' % rtc_device, 'w')
32    f.write('%s\n' % str(alarm_time))
33    f.close()
34
35
36def set_wake_alarm(alarm_time, rtc_device='rtc0'):
37    """
38    Set the hardware RTC-based wake alarm to 'alarm_time'.
39    """
40    try:
41        write_wake_alarm(alarm_time, rtc_device)
42    except IOError as (errnum, strerror):
43        if errnum != errno.EBUSY:
44            raise
45        write_wake_alarm('clear', rtc_device)
46        write_wake_alarm(alarm_time, rtc_device)
47