blob: a325f9172f43cd98578164f6355ff8cbe835c766 [file] [log] [blame]
William A. Kennington IIIce80c3f2018-07-22 14:59:13 -07001#include <sdeventplus/source/io.hpp>
2#include <type_traits>
3#include <utility>
4
5namespace sdeventplus
6{
7namespace source
8{
9
10IO::IO(const Event& event, int fd, uint32_t events, Callback&& callback) :
11 Base(event, create_source(event, fd, events), std::false_type()),
12 callback(std::move(callback))
13{
14}
15
16int IO::get_fd() const
17{
18 int r = event.getSdEvent()->sd_event_source_get_io_fd(source.get());
19 if (r < 0)
20 {
21 throw SdEventError(-r, "sd_event_source_get_io_fd");
22 }
23 return r;
24}
25
26void IO::set_fd(int fd) const
27{
28 int r = event.getSdEvent()->sd_event_source_set_io_fd(source.get(), fd);
29 if (r < 0)
30 {
31 throw SdEventError(-r, "sd_event_source_set_io_fd");
32 }
33}
34
William A. Kennington IIIce80c3f2018-07-22 14:59:13 -070035uint32_t IO::get_events() const
36{
37 uint32_t events;
38 int r = event.getSdEvent()->sd_event_source_get_io_events(source.get(),
39 &events);
40 if (r < 0)
41 {
42 throw SdEventError(-r, "sd_event_source_get_io_events");
43 }
44 return events;
45}
46
47void IO::set_events(uint32_t events) const
48{
49 int r =
50 event.getSdEvent()->sd_event_source_set_io_events(source.get(), events);
51 if (r < 0)
52 {
53 throw SdEventError(-r, "sd_event_source_set_io_events");
54 }
55}
56
57uint32_t IO::get_revents() const
58{
59 uint32_t revents;
60 int r = event.getSdEvent()->sd_event_source_get_io_revents(source.get(),
61 &revents);
62 if (r < 0)
63 {
64 throw SdEventError(-r, "sd_event_source_get_io_revents");
65 }
66 return revents;
67}
68
69const IO::Callback& IO::get_callback() const
70{
71 return callback;
72}
73
74sd_event_source* IO::create_source(const Event& event, int fd, uint32_t events)
75{
76 sd_event_source* source;
77 int r = event.getSdEvent()->sd_event_add_io(event.get(), &source, fd,
78 events, ioCallback, nullptr);
79 if (r < 0)
80 {
81 throw SdEventError(-r, "sd_event_add_io");
82 }
83 return source;
84}
85
86int IO::ioCallback(sd_event_source* source, int fd, uint32_t revents,
87 void* userdata)
88{
89 return sourceCallback<Callback, IO, &IO::get_callback>(
90 "ioCallback", source, userdata, fd, revents);
91}
92
93} // namespace source
94} // namespace sdeventplus