Home | History | Annotate | Download | only in local_time
      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 // 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 
     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 
     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 
     65 static int ltdev_close(hw_device_t *device)
     66 {
     67     free(device);
     68     return 0;
     69 }
     70 
     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     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 
    100 static struct hw_module_methods_t hal_module_methods = {
    101     .open = ltdev_open,
    102 };
    103 
    104 struct 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 };
    115