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_timer.h"
18
19 #include "chre/platform/log.h"
20 #include "chre/util/time.h"
21
22 #include <errno.h>
23 #include <signal.h>
24 #include <string.h>
25 #include <cinttypes>
26
27 namespace chre {
28
29 namespace {
30
31 constexpr uint64_t kOneSecondInNanoseconds = 1000000000;
32
NanosecondsToTimespec(uint64_t ns,struct timespec * ts)33 void NanosecondsToTimespec(uint64_t ns, struct timespec *ts) {
34 ts->tv_sec = ns / kOneSecondInNanoseconds;
35 ts->tv_nsec = ns % kOneSecondInNanoseconds;
36 }
37
38 } // anonymous namespace
39
systemTimerNotifyCallback(union sigval cookie)40 void SystemTimerBase::systemTimerNotifyCallback(union sigval cookie) {
41 SystemTimer *sysTimer = static_cast<SystemTimer *>(cookie.sival_ptr);
42 sysTimer->mCallback(sysTimer->mData);
43 }
44
SystemTimer()45 SystemTimer::SystemTimer() {}
46
~SystemTimer()47 SystemTimer::~SystemTimer() {
48 if (mInitialized) {
49 int ret = timer_delete(mTimerId);
50 if (ret != 0) {
51 LOGE("Couldn't delete timer: %s", strerror(errno));
52 }
53 mInitialized = false;
54 }
55 }
56
init()57 bool SystemTimer::init() {
58 if (mInitialized) {
59 LOGW("Tried re-initializing timer");
60 } else {
61 struct sigevent sigevt = {};
62 sigevt.sigev_notify = SIGEV_THREAD;
63 sigevt.sigev_value.sival_ptr = this;
64 sigevt.sigev_notify_function = systemTimerNotifyCallback;
65 sigevt.sigev_notify_attributes = nullptr;
66
67 int ret = timer_create(CLOCK_MONOTONIC, &sigevt, &mTimerId);
68 if (ret != 0) {
69 LOGE("Couldn't create timer: %s", strerror(errno));
70 } else {
71 mInitialized = true;
72 }
73 }
74
75 return mInitialized;
76 }
77
set(SystemTimerCallback * callback,void * data,Nanoseconds delay)78 bool SystemTimer::set(SystemTimerCallback *callback, void *data,
79 Nanoseconds delay) {
80 // 0 has a special meaning in POSIX, i.e. cancel the timer. In our API, a
81 // value of 0 just means fire right away.
82 if (delay.toRawNanoseconds() == 0) {
83 delay = Nanoseconds(1);
84 }
85
86 if (mInitialized) {
87 mCallback = callback;
88 mData = data;
89 return setInternal(delay.toRawNanoseconds());
90 } else {
91 return false;
92 }
93 }
94
cancel()95 bool SystemTimer::cancel() {
96 if (mInitialized) {
97 // Setting delay to 0 disarms the timer.
98 return setInternal(0);
99 } else {
100 return false;
101 }
102 }
103
isActive()104 bool SystemTimer::isActive() {
105 bool isActive = false;
106 if (mInitialized) {
107 struct itimerspec spec = {};
108 int ret = timer_gettime(mTimerId, &spec);
109 if (ret != 0) {
110 LOGE("Couldn't obtain current timer configuration: %s", strerror(errno));
111 }
112
113 isActive = (spec.it_value.tv_sec > 0 || spec.it_value.tv_nsec > 0);
114 }
115
116 return isActive;
117 }
118
setInternal(uint64_t delayNs)119 bool SystemTimerBase::setInternal(uint64_t delayNs) {
120 constexpr int kFlags = 0;
121 struct itimerspec spec = {};
122 bool success = false;
123
124 NanosecondsToTimespec(delayNs, &spec.it_value);
125 NanosecondsToTimespec(0, &spec.it_interval);
126
127 int ret = timer_settime(mTimerId, kFlags, &spec, nullptr);
128 if (ret != 0) {
129 LOGE("Couldn't set timer: %s", strerror(errno));
130 } else {
131 success = true;
132 }
133
134 return success;
135 }
136
137 } // namespace chre
138