blob: f7390d77c4152d7a3087d199bc310909c36cde3a [file] [log] [blame]
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07001#include <chrono>
2#include <cstdio>
3#include <sdeventplus/clock.hpp>
4#include <sdeventplus/event.hpp>
William A. Kennington III2584f9d2019-03-23 21:05:48 -07005#include <sdeventplus/source/signal.hpp>
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07006#include <sdeventplus/source/time.hpp>
William A. Kennington III2584f9d2019-03-23 21:05:48 -07007#include <stdplus/signal.hpp>
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07008#include <string>
9#include <utility>
10
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070011using sdeventplus::Event;
William A. Kennington III406b86e2018-09-25 15:23:00 -070012using sdeventplus::source::Enabled;
William A. Kennington III2584f9d2019-03-23 21:05:48 -070013using sdeventplus::source::Signal;
William A. Kennington III406b86e2018-09-25 15:23:00 -070014
15constexpr auto clockId = sdeventplus::ClockId::RealTime;
16using Clock = sdeventplus::Clock<clockId>;
17using Time = sdeventplus::source::Time<clockId>;
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070018
William A. Kennington III2584f9d2019-03-23 21:05:48 -070019void intCb(Signal& signal, const struct signalfd_siginfo*)
20{
21 printf("Exiting\n");
22 signal.get_event().exit(0);
23}
24
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070025int main(int argc, char* argv[])
26{
27 if (argc != 2)
28 {
29 fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
30 return 1;
31 }
32
33 unsigned interval = std::stoul(argv[1]);
34 fprintf(stderr, "Beating every %u seconds\n", interval);
35
36 auto event = Event::get_default();
William A. Kennington III406b86e2018-09-25 15:23:00 -070037 auto hbFunc = [interval](Time& source, Time::TimePoint time) {
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070038 printf("Beat\n");
William A. Kennington III406b86e2018-09-25 15:23:00 -070039
40 // Time sources are oneshot and are based on an absolute time
41 // we need to reconfigure the time source to go off again after the
42 // configured interval and re-enable it.
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070043 source.set_time(time + std::chrono::seconds{interval});
William A. Kennington III406b86e2018-09-25 15:23:00 -070044 source.set_enabled(Enabled::OneShot);
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070045 };
William A. Kennington III406b86e2018-09-25 15:23:00 -070046 Time time(event, Clock(event).now(), std::chrono::seconds{1},
47 std::move(hbFunc));
William A. Kennington III2584f9d2019-03-23 21:05:48 -070048 stdplus::signal::block(SIGINT);
49 Signal signal(event, SIGINT, intCb);
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070050 return event.loop();
51}