system_time.cc revision da0fd50de0eb302a5e488466312f89bfa3d8c477
1/*
2 * Copyright (C) 2016 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#include "chre/platform/system_time.h"
18#include "chre/platform/slpi/system_time.h"
19
20#include "chre/platform/fatal_error.h"
21#include "chre/platform/host_link.h"
22#include "chre/platform/log.h"
23#include "chre/platform/system_timer.h"
24
25extern "C" {
26
27#include "uTimetick.h"
28
29}  // extern "C"
30
31namespace chre {
32
33namespace {
34int64_t gEstimatedHostTimeOffset = 0;
35
36// A timer for scheduling a time sync request.
37SystemTimer gTimeSyncRequestTimer;
38bool gTimeSyncRequestTimerInitialized = false;
39
40Nanoseconds gLastTimeSyncRequestNanos(0);
41
42void setTimeSyncRequestTimer(Nanoseconds delay) {
43  // Check for timer init since this method might be called before CHRE
44  // init is called.
45  if (!gTimeSyncRequestTimerInitialized) {
46    if (!gTimeSyncRequestTimer.init()) {
47      FATAL_ERROR("Failed to initialize time sync request timer.");
48    } else {
49      gTimeSyncRequestTimerInitialized = true;
50    }
51  }
52  if (gTimeSyncRequestTimer.isActive()) {
53    gTimeSyncRequestTimer.cancel();
54  }
55  auto callback = [](void* /* data */) {
56    sendTimeSyncRequest();
57  };
58  if (!gTimeSyncRequestTimer.set(callback, nullptr, delay)) {
59    LOGE("Failed to set time sync request timer.");
60  }
61}
62} // anonymous namespace
63
64uint64_t getNanosecondsFromQTimerTicks(uint64_t ticks) {
65  constexpr uint64_t kClockFreq = 19200000;  // 19.2MHz QTimer clock
66
67  uint64_t nsec = 0;
68  if (ticks >= kClockFreq) {
69    uint64_t seconds = (ticks / kClockFreq);
70    ticks %= kClockFreq;
71
72    nsec = (seconds * kOneSecondInNanoseconds);
73  }
74  nsec += (ticks * kOneSecondInNanoseconds) / kClockFreq;
75  return nsec;
76}
77
78Nanoseconds SystemTime::getMonotonicTime() {
79  return Nanoseconds(getNanosecondsFromQTimerTicks(uTimetick_Get()));
80}
81
82int64_t SystemTime::getEstimatedHostTimeOffset() {
83  return gEstimatedHostTimeOffset;
84}
85
86void setEstimatedHostTimeOffset(int64_t offset) {
87  gEstimatedHostTimeOffset = offset;
88
89  // Schedule a time sync request since offset may drift
90  constexpr Seconds kTimeSyncLongInterval = Seconds(60 * 60 * 6); // 6 hours
91  setTimeSyncRequestTimer(kTimeSyncLongInterval);
92}
93
94void requestTimeSyncIfStale() {
95  constexpr Seconds kTimeSyncShortInterval = Seconds(60 * 60 * 1); // 1 hour
96  if (SystemTime::getMonotonicTime() >
97      gLastTimeSyncRequestNanos + kTimeSyncShortInterval) {
98    sendTimeSyncRequest();
99  }
100}
101
102void updateLastTimeSyncRequest() {
103  gLastTimeSyncRequestNanos = SystemTime::getMonotonicTime();
104}
105
106}  // namespace chre
107