Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 1 | #include "timer.hpp" |
| 2 | |
| 3 | #include <string.h> |
| 4 | |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 5 | #include <chrono> |
| 6 | #include <system_error> |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace hwmon |
| 11 | { |
| 12 | |
| 13 | static std::chrono::microseconds getTime() |
| 14 | { |
| 15 | using namespace std::chrono; |
| 16 | auto usec = steady_clock::now().time_since_epoch(); |
| 17 | return duration_cast<microseconds>(usec); |
| 18 | } |
| 19 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 20 | Timer::Timer(sd_event* event, std::function<void()> callback, |
| 21 | std::chrono::microseconds usec, timer::Action action) : |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 22 | event(event), |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 23 | callback(callback), duration(usec), action(action) |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 24 | { |
| 25 | auto r = sd_event_add_time(event, &eventSource, |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 26 | CLOCK_MONOTONIC, // Time base |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 27 | (getTime() + usec).count(), // When to fire |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 28 | 0, // Use default event accuracy |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 29 | timeoutHandler, // Callback handler on timeout |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 30 | this); // User data |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 31 | if (r < 0) |
| 32 | { |
| 33 | throw std::system_error(r, std::generic_category(), strerror(-r)); |
| 34 | } |
| 35 | } |
| 36 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 37 | int Timer::timeoutHandler(sd_event_source* eventSource, uint64_t usec, |
| 38 | void* userData) |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 39 | { |
| 40 | auto timer = static_cast<Timer*>(userData); |
| 41 | |
| 42 | if (timer->getAction() == timer::ON) |
| 43 | { |
| 44 | auto r = sd_event_source_set_time( |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 45 | eventSource, (getTime() + timer->getDuration()).count()); |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 46 | if (r < 0) |
| 47 | { |
| 48 | throw std::system_error(r, std::generic_category(), strerror(-r)); |
| 49 | } |
| 50 | r = sd_event_source_set_enabled(eventSource, timer::ON); |
| 51 | if (r < 0) |
| 52 | { |
| 53 | throw std::system_error(r, std::generic_category(), strerror(-r)); |
| 54 | } |
| 55 | } |
| 56 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 57 | if (timer->callback) |
Deepak Kodihalli | 6aa24d4 | 2018-03-07 01:10:03 -0600 | [diff] [blame] | 58 | { |
| 59 | timer->callback(); |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | } // namespace hwmon |
| 66 | } // namespace phosphor |