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 <sys/time.h>
25
26 #include <cutils/log.h>
27
28 #include <hardware/hardware.h>
29 #include <hardware/local_time_hal.h>
30
31 struct stub_local_time_device {
32 struct local_time_hw_device device;
33 };
34
ltdev_get_local_time(struct local_time_hw_device * dev)35 static int64_t ltdev_get_local_time(struct local_time_hw_device* dev)
36 {
37 struct timespec ts;
38 uint64_t now;
39 int ret;
40
41 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
42 if (ret < 0) {
43 ALOGW("%s failed to fetch CLOCK_MONOTONIC value! (res = %d)",
44 dev->common.module->name, ret);
45 return 0;
46 }
47
48 now = (((uint64_t)ts.tv_sec) * 1000000000ull) +
49 ((uint64_t)ts.tv_nsec);
50
51 return (int64_t)now;
52 }
53
ltdev_get_local_freq(struct local_time_hw_device * dev)54 static uint64_t ltdev_get_local_freq(struct local_time_hw_device* dev)
55 {
56 // For better or worse, linux clock_gettime routines normalize all clock
57 // frequencies to 1GHz
58 return 1000000000ull;
59 }
60
ltdev_close(hw_device_t * device)61 static int ltdev_close(hw_device_t *device)
62 {
63 free(device);
64 return 0;
65 }
66
ltdev_open(const hw_module_t * module,const char * name,hw_device_t ** device)67 static int ltdev_open(const hw_module_t* module, const char* name,
68 hw_device_t** device)
69 {
70 struct stub_local_time_device *ltdev;
71 struct timespec ts;
72 int ret;
73
74 if (strcmp(name, LOCAL_TIME_HARDWARE_INTERFACE) != 0)
75 return -EINVAL;
76
77 ltdev = calloc(1, sizeof(struct stub_local_time_device));
78 if (!ltdev)
79 return -ENOMEM;
80
81 ltdev->device.common.tag = HARDWARE_DEVICE_TAG;
82 ltdev->device.common.version = 0;
83 ltdev->device.common.module = (struct hw_module_t *) module;
84 ltdev->device.common.close = ltdev_close;
85
86 ltdev->device.get_local_time = ltdev_get_local_time;
87 ltdev->device.get_local_freq = ltdev_get_local_freq;
88 ltdev->device.set_local_slew = NULL;
89 ltdev->device.get_debug_log = NULL;
90
91 *device = <dev->device.common;
92
93 return 0;
94 }
95
96 static struct hw_module_methods_t hal_module_methods = {
97 .open = ltdev_open,
98 };
99
100 struct local_time_module HAL_MODULE_INFO_SYM = {
101 .common = {
102 .tag = HARDWARE_MODULE_TAG,
103 .version_major = 1,
104 .version_minor = 0,
105 .id = LOCAL_TIME_HARDWARE_MODULE_ID,
106 .name = "Default local_time HW HAL",
107 .author = "The Android Open Source Project",
108 .methods = &hal_module_methods,
109 },
110 };
111