1#ifndef ANDROID_PDX_UDS_CLIENT_CHANNEL_H_
2#define ANDROID_PDX_UDS_CLIENT_CHANNEL_H_
3
4#include <pdx/client_channel.h>
5
6#include <mutex>
7
8#include <uds/channel_event_set.h>
9#include <uds/channel_manager.h>
10#include <uds/service_endpoint.h>
11
12namespace android {
13namespace pdx {
14namespace uds {
15
16class ClientChannel : public pdx::ClientChannel {
17 public:
18  ~ClientChannel() override;
19
20  static std::unique_ptr<pdx::ClientChannel> Create(
21      LocalChannelHandle channel_handle);
22
23  uint32_t GetIpcTag() const override { return Endpoint::kIpcTag; }
24
25  int event_fd() const override {
26    return channel_data_ ? channel_data_->event_receiver.event_fd().Get() : -1;
27  }
28  Status<int> GetEventMask(int /*events*/) override {
29    if (channel_data_)
30      return channel_data_->event_receiver.GetPendingEvents();
31    else
32      return ErrorStatus(EINVAL);
33  }
34
35  LocalChannelHandle& GetChannelHandle() override { return channel_handle_; }
36  void* AllocateTransactionState() override;
37  void FreeTransactionState(void* state) override;
38
39  Status<void> SendImpulse(int opcode, const void* buffer,
40                           size_t length) override;
41
42  Status<int> SendWithInt(void* transaction_state, int opcode,
43                          const iovec* send_vector, size_t send_count,
44                          const iovec* receive_vector,
45                          size_t receive_count) override;
46  Status<LocalHandle> SendWithFileHandle(void* transaction_state, int opcode,
47                                         const iovec* send_vector,
48                                         size_t send_count,
49                                         const iovec* receive_vector,
50                                         size_t receive_count) override;
51  Status<LocalChannelHandle> SendWithChannelHandle(
52      void* transaction_state, int opcode, const iovec* send_vector,
53      size_t send_count, const iovec* receive_vector,
54      size_t receive_count) override;
55
56  FileReference PushFileHandle(void* transaction_state,
57                               const LocalHandle& handle) override;
58  FileReference PushFileHandle(void* transaction_state,
59                               const BorrowedHandle& handle) override;
60  ChannelReference PushChannelHandle(void* transaction_state,
61                                     const LocalChannelHandle& handle) override;
62  ChannelReference PushChannelHandle(
63      void* transaction_state, const BorrowedChannelHandle& handle) override;
64  bool GetFileHandle(void* transaction_state, FileReference ref,
65                     LocalHandle* handle) const override;
66  bool GetChannelHandle(void* transaction_state, ChannelReference ref,
67                        LocalChannelHandle* handle) const override;
68
69 private:
70  explicit ClientChannel(LocalChannelHandle channel_handle);
71
72  Status<int> SendAndReceive(void* transaction_state, int opcode,
73                             const iovec* send_vector, size_t send_count,
74                             const iovec* receive_vector, size_t receive_count);
75
76  LocalChannelHandle channel_handle_;
77  ChannelManager::ChannelData* channel_data_;
78  std::mutex socket_mutex_;
79};
80
81}  // namespace uds
82}  // namespace pdx
83}  // namespace android
84
85#endif  // ANDROID_PDX_UDS_CLIENT_CHANNEL_H_
86