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 "local_time_hw_default"
18 //#define LOG_NDEBUG 0
19
20 #include <errno.h>
21 #include <malloc.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <time.h>
25
26 #include <log/log.h>
27
28 #include <hardware/hardware.h>
29 #include <hardware/local_time_hal.h>
30
31 // We only support gcc and clang, both of which support this attribute.
32 #define UNUSED_ARGUMENT __attribute((unused))
33
34 struct stub_local_time_device {
35 struct local_time_hw_device device;
36 };
37
ltdev_get_local_time(struct local_time_hw_device * dev)38 static 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) {
46 ALOGW("%s failed to fetch CLOCK_MONOTONIC value! (res = %d)",
47 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
ltdev_get_local_freq(struct local_time_hw_device * dev UNUSED_ARGUMENT)57 static uint64_t ltdev_get_local_freq(
58 struct local_time_hw_device* dev UNUSED_ARGUMENT)
59 {
60 // For better or worse, linux clock_gettime routines normalize all clock
61 // frequencies to 1GHz
62 return 1000000000ull;
63 }
64
ltdev_close(hw_device_t * device)65 static int ltdev_close(hw_device_t *device)
66 {
67 free(device);
68 return 0;
69 }
70
ltdev_open(const hw_module_t * module,const char * name,hw_device_t ** device)71 static 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
76 if (strcmp(name, LOCAL_TIME_HARDWARE_INTERFACE) != 0)
77 return -EINVAL;
78
79 ltdev = calloc(1, sizeof(struct stub_local_time_device));
80 if (!ltdev)
81 return -ENOMEM;
82
83 ltdev->device.common.tag = HARDWARE_DEVICE_TAG;
84 ltdev->device.common.version = 0;
85 ltdev->device.common.module = (struct hw_module_t *) module;
86 ltdev->device.common.close = ltdev_close;
87
88 ltdev->device.get_local_time = ltdev_get_local_time;
89 ltdev->device.get_local_freq = ltdev_get_local_freq;
90 ltdev->device.set_local_slew = NULL;
91 ltdev->device.get_debug_log = NULL;
92
93 *device = <dev->device.common;
94
95 return 0;
96 }
97
98 static struct hw_module_methods_t hal_module_methods = {
99 .open = ltdev_open,
100 };
101
102 struct local_time_module HAL_MODULE_INFO_SYM = {
103 .common = {
104 .tag = HARDWARE_MODULE_TAG,
105 .version_major = 1,
106 .version_minor = 0,
107 .id = LOCAL_TIME_HARDWARE_MODULE_ID,
108 .name = "Default local_time HW HAL",
109 .author = "The Android Open Source Project",
110 .methods = &hal_module_methods,
111 },
112 };
113