• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   timer_undef(&mTimerHandle);
27 }
28 
init()29 bool SystemTimer::init() {
30   if (mInitialized) {
31     LOGW("Tried re-initializing timer");
32   } else {
33     timer_error_type status = timer_def_osal(
34         &mTimerHandle, &timer_non_defer_group, TIMER_FUNC1_CB_TYPE,
35         reinterpret_cast<time_osal_notify_obj_ptr>(systemTimerNotifyCallback),
36         reinterpret_cast<time_osal_notify_data>(this));
37     if (status != TE_SUCCESS) {
38       LOGE("Error initializing timer %d", status);
39     } else {
40       mInitialized = true;
41     }
42   }
43 
44   return mInitialized;
45 }
46 
set(SystemTimerCallback * callback,void * data,Nanoseconds delay)47 bool SystemTimer::set(SystemTimerCallback *callback, void *data,
48     Nanoseconds delay) {
49   bool wasSet = false;
50   if (mInitialized) {
51     mCallback = callback;
52     mData = data;
53     timer_error_type status = timer_set_64(&mTimerHandle,
54         Microseconds(delay).getMicroseconds(), 0, T_USEC);
55     if (status != TE_SUCCESS) {
56       LOGE("Error setting timer %d", status);
57     } else {
58       wasSet = true;
59     }
60   }
61 
62   return wasSet;
63 }
64 
cancel()65 bool SystemTimer::cancel() {
66   bool wasCancelled = false;
67   if (mInitialized) {
68     time_timetick_type ticksRemaining = timer_clr_64(&mTimerHandle, T_TICK);
69     wasCancelled = (ticksRemaining > 0);
70   }
71 
72   return wasCancelled;
73 }
74 
isActive()75 bool SystemTimer::isActive() {
76   return (mInitialized && timer_get_64(&mTimerHandle, T_TICK) > 0);
77 }
78 
systemTimerNotifyCallback(timer_cb_data_type data)79 void SystemTimerBase::systemTimerNotifyCallback(timer_cb_data_type data) {
80   SystemTimer *systemTimer = reinterpret_cast<SystemTimer *>(data);
81   systemTimer->mCallback(systemTimer->mData);
82 }
83 
84 }  // namespace chre
85