1525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project/* 2525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * Copyright (C) 2012 The Android Open Source Project 3525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * 4525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 5525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * you may not use this file except in compliance with the License. 6525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * You may obtain a copy of the License at 7525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * 8525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 9525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * 10525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 11525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 12525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * See the License for the specific language governing permissions and 14525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * limitations under the License. 15525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project */ 16525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 17525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project/* 18525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * Asynchronous interval timer. 19525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project */ 20525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 21525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project#include "IntervalTimer.h" 22525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project#include "OverrideLog.h" 23525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 24525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 25525c260303268a83da4c3413b953d13c9084e834The Android Open Source ProjectIntervalTimer::IntervalTimer() 26525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project{ 272534d9a546f24435d9dab8168f9ea5bf8ca1eb9bElliott Hughes mTimerId = 0; 28525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project mCb = NULL; 29525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project} 30525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 31525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 32525c260303268a83da4c3413b953d13c9084e834The Android Open Source Projectbool IntervalTimer::set(int ms, TIMER_FUNC cb) 33525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project{ 342534d9a546f24435d9dab8168f9ea5bf8ca1eb9bElliott Hughes if (mTimerId == 0) 35525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project { 36525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project if (cb == NULL) 37525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project return false; 38525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 39525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project if (!create(cb)) 40525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project return false; 41525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project } 42525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project if (cb != mCb) 43525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project { 44525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project kill(); 45525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project if (!create(cb)) 46525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project return false; 47525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project } 48525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 49525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project int stat = 0; 50525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project struct itimerspec ts; 51525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project ts.it_value.tv_sec = ms / 1000; 52525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project ts.it_value.tv_nsec = (ms % 1000) * 1000000; 53525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 54525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project ts.it_interval.tv_sec = 0; 55525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project ts.it_interval.tv_nsec = 0; 56525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 57525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project stat = timer_settime(mTimerId, 0, &ts, 0); 58525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project if (stat == -1) 59525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project ALOGE("IntervalTimer::set: fail set timer"); 60525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project return stat == 0; 61525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project} 62525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 63525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 64525c260303268a83da4c3413b953d13c9084e834The Android Open Source ProjectIntervalTimer::~IntervalTimer() 65525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project{ 66525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project kill(); 67525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project} 68525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 69525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 70525c260303268a83da4c3413b953d13c9084e834The Android Open Source Projectvoid IntervalTimer::kill() 71525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project{ 722534d9a546f24435d9dab8168f9ea5bf8ca1eb9bElliott Hughes if (mTimerId == 0) 73525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project return; 74525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 75525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project timer_delete(mTimerId); 762534d9a546f24435d9dab8168f9ea5bf8ca1eb9bElliott Hughes mTimerId = 0; 77525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project mCb = NULL; 78525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project} 79525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 80525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 81525c260303268a83da4c3413b953d13c9084e834The Android Open Source Projectbool IntervalTimer::create(TIMER_FUNC cb) 82525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project{ 83525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project struct sigevent se; 84525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project int stat = 0; 85525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project 86525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project /* 87525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * Set the sigevent structure to cause the signal to be 88525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project * delivered by creating a new thread. 89525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project */ 90525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project se.sigev_notify = SIGEV_THREAD; 91525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project se.sigev_value.sival_ptr = &mTimerId; 92525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project se.sigev_notify_function = cb; 93525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project se.sigev_notify_attributes = NULL; 94525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project mCb = cb; 95525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project stat = timer_create(CLOCK_MONOTONIC, &se, &mTimerId); 96525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project if (stat == -1) 97525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project ALOGE("IntervalTimer::create: fail create timer"); 98525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project return stat == 0; 99525c260303268a83da4c3413b953d13c9084e834The Android Open Source Project} 100