1c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov/****************************************************************************** 2c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * 3c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * Copyright (C) 2015 Google, Inc. 4c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * 5c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * Licensed under the Apache License, Version 2.0 (the "License"); 6c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * you may not use this file except in compliance with the License. 7c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * You may obtain a copy of the License at: 8c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * 9c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * http://www.apache.org/licenses/LICENSE-2.0 10c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * 11c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * Unless required by applicable law or agreed to in writing, software 12c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * distributed under the License is distributed on an "AS IS" BASIS, 13c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * See the License for the specific language governing permissions and 15c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * limitations under the License. 16c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov * 17c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov ******************************************************************************/ 18c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov 19c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov#define LOG_TAG "bt_osi_time" 20c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov 21071b507ad4278ae1ad24a188144d18cddf1b7137Jack He#include <sys/time.h> 22c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov#include <time.h> 23c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov 24c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov#include "osi/include/time.h" 25c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov 26c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavovuint32_t time_get_os_boottime_ms(void) { 27b55040cc6448a8847490da807d2b6362aa8cb8d9Myles Watson return (uint32_t)(time_get_os_boottime_us() / 1000); 2855c37f980b50694225faf9cf30e8da7c5afa6d5aPavlin Radoslavov} 2955c37f980b50694225faf9cf30e8da7c5afa6d5aPavlin Radoslavov 3055c37f980b50694225faf9cf30e8da7c5afa6d5aPavlin Radoslavovuint64_t time_get_os_boottime_us(void) { 3155c37f980b50694225faf9cf30e8da7c5afa6d5aPavlin Radoslavov struct timespec ts_now; 3255c37f980b50694225faf9cf30e8da7c5afa6d5aPavlin Radoslavov clock_gettime(CLOCK_BOOTTIME, &ts_now); 3355c37f980b50694225faf9cf30e8da7c5afa6d5aPavlin Radoslavov 3455c37f980b50694225faf9cf30e8da7c5afa6d5aPavlin Radoslavov return ((uint64_t)ts_now.tv_sec * 1000000L) + 35b55040cc6448a8847490da807d2b6362aa8cb8d9Myles Watson ((uint64_t)ts_now.tv_nsec / 1000); 36c196f214c5ae349ec2022f8d3cbaf56910b3b9f8Pavlin Radoslavov} 37071b507ad4278ae1ad24a188144d18cddf1b7137Jack He 38071b507ad4278ae1ad24a188144d18cddf1b7137Jack Heuint64_t time_gettimeofday_us(void) { 39071b507ad4278ae1ad24a188144d18cddf1b7137Jack He struct timeval tv; 40071b507ad4278ae1ad24a188144d18cddf1b7137Jack He gettimeofday(&tv, NULL); 41071b507ad4278ae1ad24a188144d18cddf1b7137Jack He return static_cast<uint64_t>(tv.tv_sec) * 1000000ULL + 42071b507ad4278ae1ad24a188144d18cddf1b7137Jack He static_cast<uint64_t>(tv.tv_usec); 43071b507ad4278ae1ad24a188144d18cddf1b7137Jack He} 44