1#ifndef ANDROID_PDX_UDS_CHANNEL_EVENT_SET_H_
2#define ANDROID_PDX_UDS_CHANNEL_EVENT_SET_H_
3
4#include <vector>
5
6#include <pdx/client_channel.h>
7#include <pdx/file_handle.h>
8#include <pdx/status.h>
9
10namespace android {
11namespace pdx {
12namespace uds {
13
14class ChannelEventSet {
15 public:
16  ChannelEventSet();
17  ChannelEventSet(ChannelEventSet&&) = default;
18  ChannelEventSet& operator=(ChannelEventSet&&) = default;
19
20  BorrowedHandle pollin_event_fd() const { return pollin_event_fd_.Borrow(); }
21  BorrowedHandle pollhup_event_fd() const { return pollhup_event_fd_.Borrow(); }
22
23  explicit operator bool() const {
24    return !!pollin_event_fd_ && !!pollhup_event_fd_;
25  }
26
27  int ModifyEvents(int clear_mask, int set_mask);
28
29 private:
30  LocalHandle pollin_event_fd_;
31  LocalHandle pollhup_event_fd_;
32  uint32_t event_bits_ = 0;
33
34  ChannelEventSet(const ChannelEventSet&) = delete;
35  void operator=(const ChannelEventSet&) = delete;
36};
37
38class ChannelEventReceiver {
39 public:
40  ChannelEventReceiver() = default;
41  ChannelEventReceiver(LocalHandle data_fd, LocalHandle pollin_event_fd,
42                       LocalHandle pollhup_event_fd);
43  ChannelEventReceiver(ChannelEventReceiver&&) = default;
44  ChannelEventReceiver& operator=(ChannelEventReceiver&&) = default;
45
46  explicit operator bool() const {
47    return !!pollin_event_fd_ && !!pollhup_event_fd_ && !!data_fd_ &&
48           !!epoll_fd_;
49  }
50
51  BorrowedHandle event_fd() const { return epoll_fd_.Borrow(); }
52
53  BorrowedHandle pollin_event_fd() const { return pollin_event_fd_.Borrow(); }
54  BorrowedHandle pollhup_event_fd() const { return pollhup_event_fd_.Borrow(); }
55  BorrowedHandle data_fd() const { return data_fd_.Borrow(); }
56
57  // Moves file descriptors out of ChannelEventReceiver. Note these operations
58  // immediately invalidates the receiver.
59  std::tuple<LocalHandle, LocalHandle, LocalHandle> TakeFds() {
60    epoll_fd_.Close();
61    return {std::move(data_fd_), std::move(pollin_event_fd_),
62            std::move(pollhup_event_fd_)};
63  }
64
65  Status<int> GetPendingEvents() const;
66  Status<int> PollPendingEvents(int timeout_ms) const;
67
68  std::vector<ClientChannel::EventSource> GetEventSources() const;
69
70 private:
71  LocalHandle data_fd_;
72  LocalHandle pollin_event_fd_;
73  LocalHandle pollhup_event_fd_;
74  LocalHandle epoll_fd_;
75
76  ChannelEventReceiver(const ChannelEventReceiver&) = delete;
77  void operator=(const ChannelEventReceiver&) = delete;
78};
79
80}  // namespace uds
81}  // namespace pdx
82}  // namespace android
83
84#endif  // ANDROID_PDX_UDS_CHANNEL_EVENT_SET_H_
85