blob: 899c38ac05db19ce39aa3ae6466c56b6725bb4cb [file] [log] [blame]
Mike J. Chenad255ee2011-08-15 11:59:47 -07001/*
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 "local_time_hw_default"
18//#define LOG_NDEBUG 0
19
20#include <errno.h>
Elliott Hughes07c08552015-01-29 21:19:10 -080021#include <malloc.h>
Mike J. Chenad255ee2011-08-15 11:59:47 -070022#include <stdint.h>
Elliott Hughes07c08552015-01-29 21:19:10 -080023#include <string.h>
Mike J. Chenad255ee2011-08-15 11:59:47 -070024#include <sys/time.h>
Mike J. Chenad255ee2011-08-15 11:59:47 -070025
26#include <cutils/log.h>
27
28#include <hardware/hardware.h>
29#include <hardware/local_time_hal.h>
30
Greg Kaisere8343252016-08-08 10:02:03 -070031// We only support gcc and clang, both of which support this attribute.
32#define UNUSED_ARGUMENT __attribute((unused))
33
Mike J. Chenad255ee2011-08-15 11:59:47 -070034struct stub_local_time_device {
35 struct local_time_hw_device device;
36};
37
38static int64_t ltdev_get_local_time(struct local_time_hw_device* dev)
39{
40 struct timespec ts;
41 uint64_t now;
42 int ret;
43
44 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
45 if (ret < 0) {
Mike J. Chen5ad38a92011-08-15 12:05:00 -070046 ALOGW("%s failed to fetch CLOCK_MONOTONIC value! (res = %d)",
Mike J. Chenad255ee2011-08-15 11:59:47 -070047 dev->common.module->name, ret);
48 return 0;
49 }
50
51 now = (((uint64_t)ts.tv_sec) * 1000000000ull) +
52 ((uint64_t)ts.tv_nsec);
53
54 return (int64_t)now;
55}
56
Greg Kaisere8343252016-08-08 10:02:03 -070057static uint64_t ltdev_get_local_freq(
58 struct local_time_hw_device* dev UNUSED_ARGUMENT)
Mike J. Chenad255ee2011-08-15 11:59:47 -070059{
60 // For better or worse, linux clock_gettime routines normalize all clock
61 // frequencies to 1GHz
62 return 1000000000ull;
63}
64
65static int ltdev_close(hw_device_t *device)
66{
67 free(device);
68 return 0;
69}
70
71static int ltdev_open(const hw_module_t* module, const char* name,
72 hw_device_t** device)
73{
74 struct stub_local_time_device *ltdev;
75 struct timespec ts;
76 int ret;
77
78 if (strcmp(name, LOCAL_TIME_HARDWARE_INTERFACE) != 0)
79 return -EINVAL;
80
81 ltdev = calloc(1, sizeof(struct stub_local_time_device));
82 if (!ltdev)
83 return -ENOMEM;
84
85 ltdev->device.common.tag = HARDWARE_DEVICE_TAG;
86 ltdev->device.common.version = 0;
87 ltdev->device.common.module = (struct hw_module_t *) module;
88 ltdev->device.common.close = ltdev_close;
89
90 ltdev->device.get_local_time = ltdev_get_local_time;
91 ltdev->device.get_local_freq = ltdev_get_local_freq;
92 ltdev->device.set_local_slew = NULL;
93 ltdev->device.get_debug_log = NULL;
94
95 *device = &ltdev->device.common;
96
97 return 0;
98}
99
100static struct hw_module_methods_t hal_module_methods = {
101 .open = ltdev_open,
102};
103
104struct local_time_module HAL_MODULE_INFO_SYM = {
105 .common = {
106 .tag = HARDWARE_MODULE_TAG,
107 .version_major = 1,
108 .version_minor = 0,
109 .id = LOCAL_TIME_HARDWARE_MODULE_ID,
110 .name = "Default local_time HW HAL",
111 .author = "The Android Open Source Project",
112 .methods = &hal_module_methods,
113 },
114};