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