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
21 namespace chre {
22
SystemTimer()23 SystemTimer::SystemTimer() {}
24
~SystemTimer()25 SystemTimer::~SystemTimer() {
26 if (mInitialized) {
27 slpiTimerUndef(&mTimerHandle);
28 }
29 }
30
init()31 bool SystemTimer::init() {
32 if (mInitialized) {
33 LOGW("Tried re-initializing timer");
34 } else {
35 #ifdef CHRE_SLPI_UIMG_ENABLED
36 SlpiTimerErrorType status = utimer_def_osal(
37 &mTimerHandle, UTIMER_FUNC1_CB_TYPE,
38 reinterpret_cast<utimer_osal_notify_obj_ptr>(systemTimerNotifyCallback),
39 reinterpret_cast<utimer_osal_notify_data>(this));
40 #else
41 SlpiTimerErrorType status = timer_def_osal(
42 &mTimerHandle, &timer_non_defer_group, TIMER_FUNC1_CB_TYPE,
43 reinterpret_cast<time_osal_notify_obj_ptr>(systemTimerNotifyCallback),
44 reinterpret_cast<time_osal_notify_data>(this));
45 #endif // CHRE_SLPI_UIMG_ENABLED
46
47 if (status != SLPI_TIMER_SUCCESS) {
48 LOGE("Error initializing timer %d", status);
49 } else {
50 mInitialized = true;
51 }
52 }
53
54 return mInitialized;
55 }
56
set(SystemTimerCallback * callback,void * data,Nanoseconds delay)57 bool SystemTimer::set(SystemTimerCallback *callback, void *data,
58 Nanoseconds delay) {
59 bool wasSet = false;
60 if (mInitialized) {
61 mCallback = callback;
62 mData = data;
63 SlpiTimerErrorType status = slpiTimerSet64(&mTimerHandle,
64 Microseconds(delay).getMicroseconds(), 0, SlpiTimerMicroUnit);
65 if (status != SLPI_TIMER_SUCCESS) {
66 LOGE("Error setting timer %d", status);
67 } else {
68 wasSet = true;
69 }
70 }
71
72 return wasSet;
73 }
74
cancel()75 bool SystemTimer::cancel() {
76 bool wasCancelled = false;
77 if (mInitialized) {
78 SlpiTimerTickType ticksRemaining = slpiTimerClr64(&mTimerHandle,
79 SlpiTimerTickUnit);
80 wasCancelled = (ticksRemaining > 0);
81 }
82
83 return wasCancelled;
84 }
85
isActive()86 bool SystemTimer::isActive() {
87 SlpiTimerTickType ticksRemaining = slpiTimerGet64(&mTimerHandle,
88 SlpiTimerTickUnit);
89 return (mInitialized && ticksRemaining > 0);
90 }
91
systemTimerNotifyCallback(SlpiTimerCallbackDataType data)92 void SystemTimerBase::systemTimerNotifyCallback(SlpiTimerCallbackDataType data) {
93 SystemTimer *systemTimer = reinterpret_cast<SystemTimer *>(data);
94 systemTimer->mCallback(systemTimer->mData);
95 }
96
97 } // namespace chre
98