File: channel_parcelable.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (125 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "uds/channel_parcelable.h"

#include <binder/Parcel.h>
#include <uds/channel_manager.h>

namespace android {
namespace pdx {
namespace uds {

namespace {

static constexpr uint32_t kUdsMagicParcelHeader = 0x7564736d;  // 'udsm'.

}  // namespace

ChannelParcelable::ChannelParcelable(LocalHandle data_fd,
                                     LocalHandle pollin_event_fd,
                                     LocalHandle pollhup_event_fd)
    : data_fd_{std::move(data_fd)},
      pollin_event_fd_{std::move(pollin_event_fd)},
      pollhup_event_fd_{std::move(pollhup_event_fd)} {}

bool ChannelParcelable::IsValid() const {
  return !!data_fd_ && !!pollin_event_fd_ && !!pollhup_event_fd_;
}

LocalChannelHandle ChannelParcelable::TakeChannelHandle() {
  if (!IsValid()) {
    ALOGE("ChannelParcelable::TakeChannelHandle: Invalid channel parcel.");
    return {};  // Returns an empty channel handle.
  }

  return ChannelManager::Get().CreateHandle(std::move(data_fd_),
                                            std::move(pollin_event_fd_),
                                            std::move(pollhup_event_fd_));
}

status_t ChannelParcelable::writeToParcel(Parcel* parcel) const {
  status_t res = OK;

  if (!IsValid()) {
    ALOGE("ChannelParcelable::writeToParcel: Invalid channel parcel.");
    return BAD_VALUE;
  }

  res = parcel->writeUint32(kUdsMagicParcelHeader);
  if (res != OK) {
    ALOGE("ChannelParcelable::writeToParcel: Cannot write magic: res=%d.", res);
    return res;
  }

  res = parcel->writeFileDescriptor(data_fd_.Get());
  if (res != OK) {
    ALOGE("ChannelParcelable::writeToParcel: Cannot write data fd: res=%d.",
          res);
    return res;
  }

  res = parcel->writeFileDescriptor(pollin_event_fd_.Get());
  if (res != OK) {
    ALOGE(
        "ChannelParcelable::writeToParcel: Cannot write pollin event fd: "
        "res=%d.",
        res);
    return res;
  }

  res = parcel->writeFileDescriptor(pollhup_event_fd_.Get());
  if (res != OK) {
    ALOGE(
        "ChannelParcelable::writeToParcel: Cannot write pollhup event fd: "
        "res=%d.",
        res);
    return res;
  }

  return res;
}

status_t ChannelParcelable::readFromParcel(const Parcel* parcel) {
  uint32_t magic = 0;
  status_t res = OK;

  if (IsValid()) {
    ALOGE(
        "ChannelParcelable::readFromParcel: This channel parcel is already "
        "initailzied.");
    return ALREADY_EXISTS;
  }

  res = parcel->readUint32(&magic);
  if (res != OK) {
    ALOGE("ChannelParcelable::readFromParcel: Failed to read magic: res=%d.",
          res);
    return res;
  }

  if (magic != kUdsMagicParcelHeader) {
    ALOGE(
        "ChannelParcelable::readFromParcel: Unknown magic: 0x%x, epxected: "
        "0x%x",
        magic, kUdsMagicParcelHeader);
    return BAD_VALUE;
  }

  // TODO(b/69010509): We have to dup() the FD from android::Parcel as it
  // doesn't support taking out the FD's ownership. We can remove the dup() here
  // once android::Parcel support such operation.
  data_fd_.Reset(dup(parcel->readFileDescriptor()));
  pollin_event_fd_.Reset(dup(parcel->readFileDescriptor()));
  pollhup_event_fd_.Reset(dup(parcel->readFileDescriptor()));
  if (!IsValid()) {
    ALOGE(
        "ChannelParcelable::readFromParcel: Cannot read fd from parcel: "
        "data_fd=%d, pollin_event_fd=%d, pollhup_event_fd=%d.",
        data_fd_.Get(), pollin_event_fd_.Get(), pollhup_event_fd_.Get());
    return DEAD_OBJECT;
  }

  return res;
}

}  // namespace uds
}  // namespace pdx
}  // namespace android