1 /*
2 * Copyright (C) 2012 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 *
18 * The original Work has been changed by NXP.
19 *
20 * Copyright 2019, 2023 NXP
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 *
34 ******************************************************************************/
35
36 /*
37 * Asynchronous interval timer.
38 */
39 #define LOG_TAG "IntervalTimer"
40
41 #include <android-base/stringprintf.h>
42 #include <android-base/logging.h>
43
44 #include <IntervalTimer.h>
45
46 using android::base::StringPrintf;
47
IntervalTimer()48 IntervalTimer::IntervalTimer() {
49 mTimerId = 0;
50 mCb = NULL;
51 }
52
set(int ms,void * ptr,TIMER_FUNC cb)53 bool IntervalTimer::set(int ms, void* ptr, TIMER_FUNC cb) {
54 if (mTimerId == 0) {
55 if (cb == NULL) return false;
56
57 if (!create(ptr,cb)) return false;
58 }
59 if (cb != mCb) {
60 kill();
61 if (!create(ptr,cb)) return false;
62 }
63
64 int stat = 0;
65 struct itimerspec ts;
66 ts.it_value.tv_sec = ms / 1000;
67 ts.it_value.tv_nsec = (ms % 1000) * 1000000;
68
69 ts.it_interval.tv_sec = 0;
70 ts.it_interval.tv_nsec = 0;
71
72 stat = timer_settime(mTimerId, 0, &ts, 0);
73 if (stat == -1) LOG(ERROR) << StringPrintf("fail set timer");
74 return stat == 0;
75 }
76
~IntervalTimer()77 IntervalTimer::~IntervalTimer() { kill(); }
78
kill()79 void IntervalTimer::kill() {
80 if (mTimerId == 0) return;
81
82 timer_delete(mTimerId);
83 mTimerId = 0;
84 mCb = NULL;
85 }
86
create(void * ptr,TIMER_FUNC cb)87 bool IntervalTimer::create(void* ptr , TIMER_FUNC cb) {
88 struct sigevent se;
89 int stat = 0;
90
91 /*
92 * Set the sigevent structure to cause the signal to be
93 * delivered by creating a new thread.
94 */
95 se.sigev_notify = SIGEV_THREAD;
96 //se.sigev_value.sival_ptr = &mTimerId;
97 se.sigev_value.sival_ptr = ptr;
98 se.sigev_notify_function = cb;
99 se.sigev_notify_attributes = NULL;
100 #ifdef NXP_EXTNS
101 se.sigev_signo = 0;
102 #endif
103 mCb = cb;
104 stat = timer_create(CLOCK_BOOTTIME_ALARM, &se, &mTimerId);
105 if (stat == -1) LOG(ERROR) << StringPrintf("fail create timer");
106 return stat == 0;
107 }
108