blob: 65863b4c9e9ba0e55770384a369d899e3261dbe7 [file] [log] [blame]
Martijn Coenen72110162016-08-19 14:28:25 +02001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Martijn Coenen30791002016-12-01 15:40:46 +010016#define LOG_TAG "HidlSupport"
Martijn Coenen72110162016-08-19 14:28:25 +020017
18#include <hidl/HidlSupport.h>
19
Yifan Hong20273f92017-01-30 14:13:19 -080020#include <unordered_map>
21
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070022#include <android-base/logging.h>
Yifan Hong20273f92017-01-30 14:13:19 -080023#include <android-base/parseint.h>
Martijn Coenen30791002016-12-01 15:40:46 +010024
Martijn Coenen72110162016-08-19 14:28:25 +020025namespace android {
26namespace hardware {
27
Yifan Hong24332ef2017-03-07 16:22:19 -080028namespace details {
29bool debuggable() {
30#ifdef LIBHIDL_TARGET_DEBUGGABLE
31 return true;
32#else
33 return false;
34#endif
35}
36} // namespace details
37
Martijn Coenen04b91c02017-01-19 14:14:21 +010038hidl_handle::hidl_handle() {
Steven Moreland180d1612019-04-24 17:58:28 -070039 memset(this, 0, sizeof(*this));
40 // mHandle = nullptr;
41 // mOwnsHandle = false;
Martijn Coenen04b91c02017-01-19 14:14:21 +010042}
43
44hidl_handle::~hidl_handle() {
45 freeHandle();
46}
47
Steven Moreland180d1612019-04-24 17:58:28 -070048hidl_handle::hidl_handle(const native_handle_t* handle) : hidl_handle() {
Martijn Coenen04b91c02017-01-19 14:14:21 +010049 mHandle = handle;
50 mOwnsHandle = false;
51}
52
53// copy constructor.
Steven Moreland180d1612019-04-24 17:58:28 -070054hidl_handle::hidl_handle(const hidl_handle& other) : hidl_handle() {
Martijn Coenen04b91c02017-01-19 14:14:21 +010055 mOwnsHandle = false;
56 *this = other;
57}
58
59// move constructor.
Steven Moreland180d1612019-04-24 17:58:28 -070060hidl_handle::hidl_handle(hidl_handle&& other) noexcept : hidl_handle() {
Martijn Coenen04b91c02017-01-19 14:14:21 +010061 mOwnsHandle = false;
62 *this = std::move(other);
63}
64
65// assignment operators
66hidl_handle &hidl_handle::operator=(const hidl_handle &other) {
67 if (this == &other) {
68 return *this;
69 }
70 freeHandle();
71 if (other.mHandle != nullptr) {
72 mHandle = native_handle_clone(other.mHandle);
73 if (mHandle == nullptr) {
Elliott Hughes0e55b452017-05-01 21:38:48 -070074 PLOG(FATAL) << "Failed to clone native_handle in hidl_handle";
Martijn Coenen04b91c02017-01-19 14:14:21 +010075 }
76 mOwnsHandle = true;
77 } else {
78 mHandle = nullptr;
79 mOwnsHandle = false;
80 }
81 return *this;
82}
83
84hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) {
85 freeHandle();
86 mHandle = native_handle;
87 mOwnsHandle = false;
88 return *this;
89}
90
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -070091hidl_handle& hidl_handle::operator=(hidl_handle&& other) noexcept {
Martijn Coenen04b91c02017-01-19 14:14:21 +010092 if (this != &other) {
93 freeHandle();
94 mHandle = other.mHandle;
95 mOwnsHandle = other.mOwnsHandle;
96 other.mHandle = nullptr;
97 other.mOwnsHandle = false;
98 }
99 return *this;
100}
101
102void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) {
Scott Randolphca37c0e2017-02-15 16:38:46 -0800103 freeHandle();
Martijn Coenen04b91c02017-01-19 14:14:21 +0100104 mHandle = handle;
105 mOwnsHandle = shouldOwn;
106}
107
108const native_handle_t* hidl_handle::operator->() const {
109 return mHandle;
110}
111
112// implicit conversion to const native_handle_t*
113hidl_handle::operator const native_handle_t *() const {
114 return mHandle;
115}
116
117// explicit conversion
118const native_handle_t *hidl_handle::getNativeHandle() const {
119 return mHandle;
120}
121
122void hidl_handle::freeHandle() {
123 if (mOwnsHandle && mHandle != nullptr) {
124 // This can only be true if:
125 // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
126 // wasn't const to begin with.
127 // 2. Copy/assignment from another hidl_handle, in which case we have
128 // cloned the handle.
129 // 3. Move constructor from another hidl_handle, in which case the original
130 // hidl_handle must have been non-const as well.
131 native_handle_t *handle = const_cast<native_handle_t*>(
132 static_cast<const native_handle_t*>(mHandle));
133 native_handle_close(handle);
134 native_handle_delete(handle);
135 mHandle = nullptr;
136 }
137}
138
Martijn Coenen72110162016-08-19 14:28:25 +0200139static const char *const kEmptyString = "";
140
141hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -0700142 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +0200143 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -0700144 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200145}
146
147hidl_string::~hidl_string() {
148 clear();
149}
150
Steven Morelande03c0872016-10-24 10:43:50 -0700151hidl_string::hidl_string(const char *s) : hidl_string() {
Steven Morelanda21d84f2017-02-16 09:23:45 -0800152 if (s == nullptr) {
153 return;
154 }
155
Yifan Hong602b85a2016-10-24 13:40:01 -0700156 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -0700157}
158
Steven Moreland53120f72017-01-12 09:39:26 -0800159hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
160 copyFrom(s, length);
161}
162
Yifan Hong602b85a2016-10-24 13:40:01 -0700163hidl_string::hidl_string(const hidl_string &other): hidl_string() {
164 copyFrom(other.c_str(), other.size());
165}
166
167hidl_string::hidl_string(const std::string &s) : hidl_string() {
168 copyFrom(s.c_str(), s.size());
169}
170
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700171hidl_string::hidl_string(hidl_string&& other) noexcept : hidl_string() {
Yifan Hong602b85a2016-10-24 13:40:01 -0700172 moveFrom(std::forward<hidl_string>(other));
173}
174
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700175hidl_string& hidl_string::operator=(hidl_string&& other) noexcept {
Yifan Hong602b85a2016-10-24 13:40:01 -0700176 if (this != &other) {
177 clear();
178 moveFrom(std::forward<hidl_string>(other));
179 }
180 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200181}
182
183hidl_string &hidl_string::operator=(const hidl_string &other) {
184 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700185 clear();
186 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +0200187 }
188
189 return *this;
190}
191
192hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700193 clear();
Steven Moreland153f87a2017-02-28 09:42:26 -0800194
195 if (s == nullptr) {
196 return *this;
197 }
198
Yifan Hong602b85a2016-10-24 13:40:01 -0700199 copyFrom(s, strlen(s));
200 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200201}
202
Yifan Hong602b85a2016-10-24 13:40:01 -0700203hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +0200204 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -0700205 copyFrom(s.c_str(), s.size());
206 return *this;
207}
Martijn Coenen72110162016-08-19 14:28:25 +0200208
Yifan Hong602b85a2016-10-24 13:40:01 -0700209hidl_string::operator std::string() const {
210 return std::string(mBuffer, mSize);
211}
212
Scott Randolph0c84ab42017-04-03 14:07:14 -0700213std::ostream& operator<<(std::ostream& os, const hidl_string& str) {
214 os << str.c_str();
215 return os;
Yifan Hong602b85a2016-10-24 13:40:01 -0700216}
217
218void hidl_string::copyFrom(const char *data, size_t size) {
219 // assume my resources are freed.
220
Steven Moreland98893802017-10-24 18:12:34 -0700221 if (size >= UINT32_MAX) {
Elliott Hughes0e55b452017-05-01 21:38:48 -0700222 LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100223 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700224 char *buf = (char *)malloc(size + 1);
225 memcpy(buf, data, size);
226 buf[size] = '\0';
227 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200228
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100229 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200230 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700231}
Martijn Coenen72110162016-08-19 14:28:25 +0200232
Yifan Hong602b85a2016-10-24 13:40:01 -0700233void hidl_string::moveFrom(hidl_string &&other) {
234 // assume my resources are freed.
235
Hridya Valsaraju01268892017-02-27 08:48:38 -0800236 mBuffer = std::move(other.mBuffer);
Yifan Hong602b85a2016-10-24 13:40:01 -0700237 mSize = other.mSize;
238 mOwnsBuffer = other.mOwnsBuffer;
239
240 other.mOwnsBuffer = false;
Hridya Valsaraju01268892017-02-27 08:48:38 -0800241 other.clear();
Martijn Coenen72110162016-08-19 14:28:25 +0200242}
243
244void hidl_string::clear() {
245 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100246 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200247 }
248
Yifan Hong602b85a2016-10-24 13:40:01 -0700249 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200250 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700251 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200252}
253
254void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100255 if (size > UINT32_MAX) {
Elliott Hughes0e55b452017-05-01 21:38:48 -0700256 LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100257 }
Steven Moreland57c6fcb2018-03-06 11:31:33 -0800258
259 // When the binder driver copies this data into its buffer, it must
260 // have a zero byte there because the remote process will have a pointer
261 // directly into the read-only binder buffer. If we manually copy the
262 // data now to add a zero, then we lose the efficiency of this method.
263 // Checking here (it's also checked in the parceling code later).
264 CHECK(data[size] == '\0');
265
Martijn Coenen72110162016-08-19 14:28:25 +0200266 clear();
267
Yifan Hong602b85a2016-10-24 13:40:01 -0700268 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100269 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200270 mOwnsBuffer = false;
271}
272
273const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700274 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200275}
276
277size_t hidl_string::size() const {
278 return mSize;
279}
280
281bool hidl_string::empty() const {
282 return mSize == 0;
283}
284
Howard Chen8e42f5a2017-11-09 13:58:28 +0800285sp<HidlMemory> HidlMemory::getInstance(const hidl_memory& mem) {
286 sp<HidlMemory> instance = new HidlMemory();
287 instance->hidl_memory::operator=(mem);
288 return instance;
289}
290
Howard Chen9bc35c42017-10-13 14:21:46 +0800291sp<HidlMemory> HidlMemory::getInstance(hidl_memory&& mem) {
292 sp<HidlMemory> instance = new HidlMemory();
Howard Chen8e42f5a2017-11-09 13:58:28 +0800293 instance->hidl_memory::operator=(std::move(mem));
Howard Chen9bc35c42017-10-13 14:21:46 +0800294 return instance;
295}
296
297sp<HidlMemory> HidlMemory::getInstance(const hidl_string& name, int fd, uint64_t size) {
298 native_handle_t* handle = native_handle_create(1, 0);
299 if (!handle) {
300 close(fd);
301 LOG(ERROR) << "native_handle_create fails";
302 return new HidlMemory();
303 }
304 handle->data[0] = fd;
305
306 hidl_handle hidlHandle;
307 hidlHandle.setTo(handle, true /* shouldOwn */);
308
309 sp<HidlMemory> instance = new HidlMemory(name, std::move(hidlHandle), size);
310 return instance;
311}
312
Howard Chen8e42f5a2017-11-09 13:58:28 +0800313HidlMemory::HidlMemory() : hidl_memory() {}
314
315HidlMemory::HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size)
316 : hidl_memory(name, std::move(handle), size) {}
317
Howard Chen9bc35c42017-10-13 14:21:46 +0800318// it's required to have at least one out-of-line method to avoid weak vtable
Alec Edgingtonaf789482018-06-14 21:03:14 +0100319HidlMemory::~HidlMemory() {}
Howard Chen9bc35c42017-10-13 14:21:46 +0800320
Martijn Coenen72110162016-08-19 14:28:25 +0200321} // namespace hardware
322} // namespace android
323
324