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