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 =
64         slpiTimerSet64(&mTimerHandle, Microseconds(delay).getMicroseconds(), 0,
65                        SlpiTimerMicroUnit);
66     if (status != SLPI_TIMER_SUCCESS) {
67       LOGE("Error setting timer %d", status);
68     } else {
69       wasSet = true;
70     }
71   }
72 
73   return wasSet;
74 }
75 
cancel()76 bool SystemTimer::cancel() {
77   bool wasCancelled = false;
78   if (mInitialized) {
79     SlpiTimerTickType ticksRemaining =
80         slpiTimerClr64(&mTimerHandle, SlpiTimerTickUnit);
81     wasCancelled = (ticksRemaining > 0);
82   }
83 
84   return wasCancelled;
85 }
86 
isActive()87 bool SystemTimer::isActive() {
88   SlpiTimerTickType ticksRemaining =
89       slpiTimerGet64(&mTimerHandle, SlpiTimerTickUnit);
90   return (mInitialized && ticksRemaining > 0);
91 }
92 
systemTimerNotifyCallback(SlpiTimerCallbackDataType data)93 void SystemTimerBase::systemTimerNotifyCallback(
94     SlpiTimerCallbackDataType data) {
95   SystemTimer *systemTimer = reinterpret_cast<SystemTimer *>(data);
96   systemTimer->mCallback(systemTimer->mData);
97 }
98 
99 }  // namespace chre
100