aboutsummaryrefslogtreecommitdiffstats
path: root/nci/jni/IntervalTimer.cpp
blob: 5f034f42ee6659b09bd3be28eb25a684c1d7292b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/******************************************************************************
 *
 *  The original Work has been changed by NXP Semiconductors.
 *
 *  Copyright (C) 2015 NXP Semiconductors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/
/*
 *  Asynchronous interval timer.
 */
#define LOG_TAG "IntervalTimer"

#include "IntervalTimer.h"
#include "_OverrideLog.h"
#include <string.h>


IntervalTimer::IntervalTimer()
{
    mTimerId = 0;
    mCb = NULL;
}


bool IntervalTimer::set(int ms, TIMER_FUNC cb)
{
    if (mTimerId == 0)
    {
        if (cb == NULL)
            return false;

        if (!create(cb))
            return false;
    }
    if (cb != mCb)
    {
        kill();
        if (!create(cb))
            return false;
    }

    int stat = 0;
    struct itimerspec ts;
    ts.it_value.tv_sec = ms / 1000;
    ts.it_value.tv_nsec = (ms % 1000) * 1000000;

    ts.it_interval.tv_sec = 0;
    ts.it_interval.tv_nsec = 0;

    stat = timer_settime(mTimerId, 0, &ts, 0);
    if (stat == -1)
        ALOGE("fail set timer");
    return stat == 0;
}


IntervalTimer::~IntervalTimer()
{
    kill();
}


void IntervalTimer::kill()
{
    if (mTimerId == 0)
        return;

    timer_delete(mTimerId);
    mTimerId = 0;
    mCb = NULL;
}


bool IntervalTimer::create(TIMER_FUNC cb)
{
    struct sigevent se;
    memset(&se,0,sizeof(struct sigevent));
    int stat = 0;

    /*
     * Set the sigevent structure to cause the signal to be
     * delivered by creating a new thread.
     */
    se.sigev_notify = SIGEV_THREAD;
    se.sigev_value.sival_ptr = &mTimerId;
    se.sigev_notify_function = cb;
    se.sigev_notify_attributes = NULL;
    mCb = cb;
    stat = timer_create(CLOCK_MONOTONIC, &se, &mTimerId);
    if (stat == -1)
        ALOGE("fail create timer");
    return stat == 0;
}

bool IntervalTimer::isRunning(void)
{
   if (mTimerId == 0)
       return false;

    int stat = 0;
    struct itimerspec ts;

    stat = timer_gettime(mTimerId, &ts);
    if (stat != 0)
        return false;
    return ((ts.it_value.tv_sec > 0 || ts.it_value.tv_nsec > 0) ? true : false);
}