summaryrefslogtreecommitdiffstats
path: root/timer.cpp
blob: d371c980707f2f5d02674164fb4a428a39c6c37a (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * Copyright © 2017 IBM Corporation
 *
 * 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.
 */
#include <chrono>
#include <phosphor-logging/log.hpp>
#include <type_traits>
#include "timer.hpp"

namespace phosphor
{
namespace fan
{
namespace util
{

using namespace phosphor::logging;

Timer::Timer(phosphor::fan::event::EventPtr& events,
             std::function<void()> callbackFunc) :
    timeEvent(events),
    callback(callbackFunc),
    timeout(0)
{
    sd_event_source* source = nullptr;

    // Start with an infinite expiration time
    auto r = sd_event_add_time(timeEvent.get(),
                               &source,
                               CLOCK_MONOTONIC, // Time base
                               UINT64_MAX,      // Expire time - way long time
                               0,               // Use default event accuracy
                               timeoutHandler,  // Callback handler on timeout
                               this);           // User data
    if (r < 0)
    {
        log<level::ERR>("Timer::Timer failed call to sd_event_add_time",
                        entry("ERROR=%s", strerror(-r)));
        //TODO openbmc/openbmc#1555 throw an elog
        throw std::runtime_error("Timer initialization failed");
    }

    eventSource.reset(source);

    //Ensure timer isn't running
    setTimer(SD_EVENT_OFF);
}


Timer::~Timer()
{
    setTimer(SD_EVENT_OFF);
}


int Timer::timeoutHandler(sd_event_source* eventSource,
                          uint64_t usec, void* userData)
{
    auto timer = static_cast<Timer*>(userData);

    if (timer->type == TimerType::repeating)
    {
        //Set the next expiration time
        timer->setTimeout();
    }

    timer->callback();

    return 0;
}


std::chrono::microseconds Timer::getTime()
{
    using namespace std::chrono;
    auto now = steady_clock::now().time_since_epoch();
    return duration_cast<microseconds>(now);
}


void Timer::setTimer(int action)
{
    auto r = sd_event_source_set_enabled(eventSource.get(), action);
    if (r < 0)
    {
        log<level::ERR>("Failed call to sd_event_source_set_enabled",
                        entry("ERROR=%s", strerror(-r)),
                        entry("ACTION=%d", action));
        //TODO openbmc/openbmc#1555 throw an elog
        throw std::runtime_error("Failed call to sd_event_source_set_enabled");
    }
}


void Timer::stop()
{
    setTimer(SD_EVENT_OFF);
}


bool Timer::running()
{
    int status = 0;

    //returns SD_EVENT_OFF, SD_EVENT_ON, or SD_EVENT_ONESHOT
    auto r = sd_event_source_get_enabled(eventSource.get(), &status);
    if (r < 0)
    {
        log<level::ERR>("Failed call to sd_event_source_get_enabled",
                        entry("ERROR=%s", strerror(-r)));
        //TODO openbmc/openbmc#1555 throw an elog
        throw std::runtime_error("Failed call to sd_event_source_get_enabled");
    }

    return (status != SD_EVENT_OFF);
}


void Timer::setTimeout()
{
    //Get the current time and add the delta
    static_assert(std::is_same<decltype(getTime()),
            std::chrono::microseconds>::value,
            "Timer::getTime() is the wrong type");
    static_assert(std::is_same<decltype(timeout),
            std::chrono::microseconds>::value,
            "Timer::timeout is the wrong type");

    auto expireTime = getTime() + timeout;

    //Set the time
    auto r = sd_event_source_set_time(eventSource.get(), expireTime.count());
    if (r < 0)
    {
        log<level::ERR>("Failed call to sd_event_source_set_time",
                        entry("ERROR=%s", strerror(-r)));
        //TODO openbmc/openbmc#1555 throw an elog
        throw std::runtime_error("Failed call to sd_event_source_set_time");
    }
}


void Timer::start(std::chrono::microseconds timeValue,
                  TimerType timerType)
{
    type = timerType;

    // Disable the timer
    setTimer(SD_EVENT_OFF);

    //Rearm the timer
    timeout = timeValue;
    setTimeout();

    setTimer((type == TimerType::oneshot) ? SD_EVENT_ONESHOT : SD_EVENT_ON);
}


}
}
}
OpenPOWER on IntegriCloud