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