1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "common_time"
18#include <utils/Log.h>
19
20#include <assert.h>
21#include <stdint.h>
22
23#include <common_time/local_clock.h>
24#include <hardware/hardware.h>
25#include <hardware/local_time_hal.h>
26#include <utils/Errors.h>
27#include <utils/threads.h>
28
29namespace android {
30
31Mutex LocalClock::dev_lock_;
32local_time_hw_device_t* LocalClock::dev_ = NULL;
33
34LocalClock::LocalClock() {
35    int res;
36    const hw_module_t* mod;
37
38    AutoMutex lock(&dev_lock_);
39
40    if (dev_ != NULL)
41        return;
42
43    res = hw_get_module_by_class(LOCAL_TIME_HARDWARE_MODULE_ID, NULL, &mod);
44    if (res) {
45        ALOGE("Failed to open local time HAL module (res = %d)", res);
46    } else {
47        res = local_time_hw_device_open(mod, &dev_);
48        if (res) {
49            ALOGE("Failed to open local time HAL device (res = %d)", res);
50            dev_ = NULL;
51        }
52    }
53}
54
55bool LocalClock::initCheck() {
56    return (NULL != dev_);
57}
58
59int64_t LocalClock::getLocalTime() {
60    assert(NULL != dev_);
61    assert(NULL != dev_->get_local_time);
62
63    return dev_->get_local_time(dev_);
64}
65
66uint64_t LocalClock::getLocalFreq() {
67    assert(NULL != dev_);
68    assert(NULL != dev_->get_local_freq);
69
70    return dev_->get_local_freq(dev_);
71}
72
73status_t LocalClock::setLocalSlew(int16_t rate) {
74    assert(NULL != dev_);
75
76    if (!dev_->set_local_slew)
77        return INVALID_OPERATION;
78
79    return static_cast<status_t>(dev_->set_local_slew(dev_, rate));
80}
81
82int32_t LocalClock::getDebugLog(struct local_time_debug_event* records,
83                                int max_records) {
84    assert(NULL != dev_);
85
86    if (!dev_->get_debug_log)
87        return INVALID_OPERATION;
88
89    return dev_->get_debug_log(dev_, records, max_records);
90}
91
92}  // namespace android
93