1 #ifndef ANDROID_PDX_UDS_CHANNEL_EVENT_SET_H_
2 #define ANDROID_PDX_UDS_CHANNEL_EVENT_SET_H_
3 
4 #include <errno.h>
5 #include <poll.h>
6 #include <sys/epoll.h>
7 #include <sys/eventfd.h>
8 
9 #include <pdx/file_handle.h>
10 #include <pdx/status.h>
11 
12 namespace android {
13 namespace pdx {
14 namespace uds {
15 
16 class ChannelEventSet {
17  public:
18   ChannelEventSet();
19   ChannelEventSet(ChannelEventSet&&) = default;
20   ChannelEventSet& operator=(ChannelEventSet&&) = default;
21 
event_fd()22   BorrowedHandle event_fd() const { return epoll_fd_.Borrow(); }
23 
24   explicit operator bool() const { return !!epoll_fd_ && !!event_fd_; }
25 
26   Status<void> AddDataFd(const LocalHandle& data_fd);
27   int ModifyEvents(int clear_mask, int set_mask);
28 
29  private:
30   LocalHandle epoll_fd_;
31   LocalHandle event_fd_;
32   uint32_t event_bits_ = 0;
33 
34   static Status<void> SetupHandle(int fd, LocalHandle* handle,
35                                   const char* error_name);
36 
37   ChannelEventSet(const ChannelEventSet&) = delete;
38   void operator=(const ChannelEventSet&) = delete;
39 };
40 
41 class ChannelEventReceiver {
42  public:
43   ChannelEventReceiver() = default;
ChannelEventReceiver(LocalHandle epoll_fd)44   ChannelEventReceiver(LocalHandle epoll_fd) : epoll_fd_{std::move(epoll_fd)} {}
45   ChannelEventReceiver(ChannelEventReceiver&&) = default;
46   ChannelEventReceiver& operator=(ChannelEventReceiver&&) = default;
47 
event_fd()48   BorrowedHandle event_fd() const { return epoll_fd_.Borrow(); }
49   Status<int> GetPendingEvents() const;
50 
51  private:
52   LocalHandle epoll_fd_;
53 
54   ChannelEventReceiver(const ChannelEventReceiver&) = delete;
55   void operator=(const ChannelEventReceiver&) = delete;
56 };
57 
58 }  // namespace uds
59 }  // namespace pdx
60 }  // namespace android
61 
62 #endif  // ANDROID_PDX_UDS_CHANNEL_EVENT_SET_H_
63