Home | History | Annotate | Download | only in src
      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 
     18 /**
     19  ************************************************************************
     20  * @file         M4OSA_Clock.c
     21  * @brief        Clock related functions
     22  * @note         This file implements functions to manipulate clock
     23  ************************************************************************
     24 */
     25 
     26 #include <sys/time.h>
     27 #include <time.h>
     28 
     29 #include "M4OSA_Debug.h"
     30 #include "M4OSA_Clock.h"
     31 #include "M4OSA_Memory.h"
     32 #include "M4OSA_Types.h"
     33 
     34 
     35 
     36 
     37 /**
     38  ************************************************************************
     39  * @brief      This function gets an absolute time to an unknown reference with
     40  *             a high precision.
     41  * @note       It means it can only be used to get a relative time by computing
     42  *             differences between to times.
     43  *             It is to the caller to allocate time. Time is expressed in
     44  *             timescale unit.
     45  *             M4OSA_ROLLOVER_CLOCK in M4OSA_Types.h must be configured with the rollover
     46  *             offset of this function.
     47  * @param      time: (IN/OUT) time
     48  * @param      timescale: (IN) The timescale (time unit per second)
     49  * @return     M4NO_ERROR: there is no error
     50  * @return     M4ERR_PARAMETER: at least one parameter is NULL
     51  * @return     M4WAR_TIMESCALE_TOO_BIG: the precision of the system clock is
     52  *             not
     53  *             compliant with the input timescale
     54  ************************************************************************
     55 */
     56 M4OSA_ERR M4OSA_clockGetTime(M4OSA_Time* pTime, M4OSA_UInt32 timescale)
     57 {
     58     struct timeval tv;
     59     struct timezone tz;
     60     M4OSA_UInt32 u32_time = 0;
     61     M4OSA_UInt32 u32_time_hi;
     62     M4OSA_UInt32 u32_time_lo;
     63     M4OSA_UInt32 u32_time_lh;
     64     M4OSA_UInt32 factor;
     65 
     66     M4OSA_TRACE1_2("M4OSA_clockGetTime\t\tM4OSA_Time* 0x%x\tM4OSA_UInt32 %d",
     67                                                               pTime, timescale);
     68 
     69     M4OSA_DEBUG_IF2(M4OSA_NULL == pTime, M4ERR_PARAMETER,
     70                                      "M4OSA_clockGetTime: pTime is M4OSA_NULL");
     71     M4OSA_DEBUG_IF2(0 == timescale, M4ERR_PARAMETER,
     72                                           "M4OSA_clockGetTime: timescale is 0");
     73 
     74     factor = 1000000 / timescale;
     75 
     76     if(gettimeofday(&tv, &tz) == 0)
     77     {
     78         u32_time_lo = (tv.tv_sec & 0xFFFF) * timescale;
     79         u32_time_hi = (((tv.tv_sec >> 16) & 0xFFFF) * timescale) + ((u32_time_lo >> 16) & 0xFFFF);
     80         u32_time_lo &= 0xFFFF;
     81         u32_time_lo += tv.tv_usec / factor;
     82         u32_time_hi += ((u32_time_lo >> 16) & 0xFFFF);
     83         u32_time_lo &= 0xFFFF;
     84         u32_time = ((u32_time_hi & 0x7FFF) << 16) | u32_time_lo;
     85     }
     86 
     87     /* M4OSA_Time is signed, so we need to check the max value*/
     88     if (u32_time > M4OSA_INT32_MAX)
     89     {
     90         u32_time = u32_time - M4OSA_INT32_MAX;
     91     }
     92 
     93     *pTime = (M4OSA_Time)u32_time;
     94 
     95     if( timescale > 10000 )
     96     {
     97         return M4WAR_TIMESCALE_TOO_BIG;
     98     }
     99 
    100     return M4NO_ERROR;
    101 }
    102