blob: 141242e3b438b9dd56bf2871a2a740f0356de974 [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() {
39 mHandle = nullptr;
40 mOwnsHandle = false;
41}
42
43hidl_handle::~hidl_handle() {
44 freeHandle();
45}
46
47hidl_handle::hidl_handle(const native_handle_t *handle) {
48 mHandle = handle;
49 mOwnsHandle = false;
50}
51
52// copy constructor.
53hidl_handle::hidl_handle(const hidl_handle &other) {
54 mOwnsHandle = false;
55 *this = other;
56}
57
58// move constructor.
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -070059hidl_handle::hidl_handle(hidl_handle&& other) noexcept {
Martijn Coenen04b91c02017-01-19 14:14:21 +010060 mOwnsHandle = false;
61 *this = std::move(other);
62}
63
64// assignment operators
65hidl_handle &hidl_handle::operator=(const hidl_handle &other) {
66 if (this == &other) {
67 return *this;
68 }
69 freeHandle();
70 if (other.mHandle != nullptr) {
71 mHandle = native_handle_clone(other.mHandle);
72 if (mHandle == nullptr) {
Elliott Hughes0e55b452017-05-01 21:38:48 -070073 PLOG(FATAL) << "Failed to clone native_handle in hidl_handle";
Martijn Coenen04b91c02017-01-19 14:14:21 +010074 }
75 mOwnsHandle = true;
76 } else {
77 mHandle = nullptr;
78 mOwnsHandle = false;
79 }
80 return *this;
81}
82
83hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) {
84 freeHandle();
85 mHandle = native_handle;
86 mOwnsHandle = false;
87 return *this;
88}
89
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -070090hidl_handle& hidl_handle::operator=(hidl_handle&& other) noexcept {
Martijn Coenen04b91c02017-01-19 14:14:21 +010091 if (this != &other) {
92 freeHandle();
93 mHandle = other.mHandle;
94 mOwnsHandle = other.mOwnsHandle;
95 other.mHandle = nullptr;
96 other.mOwnsHandle = false;
97 }
98 return *this;
99}
100
101void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) {
Scott Randolphca37c0e2017-02-15 16:38:46 -0800102 freeHandle();
Martijn Coenen04b91c02017-01-19 14:14:21 +0100103 mHandle = handle;
104 mOwnsHandle = shouldOwn;
105}
106
107const native_handle_t* hidl_handle::operator->() const {
108 return mHandle;
109}
110
111// implicit conversion to const native_handle_t*
112hidl_handle::operator const native_handle_t *() const {
113 return mHandle;
114}
115
116// explicit conversion
117const native_handle_t *hidl_handle::getNativeHandle() const {
118 return mHandle;
119}
120
121void hidl_handle::freeHandle() {
122 if (mOwnsHandle && mHandle != nullptr) {
123 // This can only be true if:
124 // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
125 // wasn't const to begin with.
126 // 2. Copy/assignment from another hidl_handle, in which case we have
127 // cloned the handle.
128 // 3. Move constructor from another hidl_handle, in which case the original
129 // hidl_handle must have been non-const as well.
130 native_handle_t *handle = const_cast<native_handle_t*>(
131 static_cast<const native_handle_t*>(mHandle));
132 native_handle_close(handle);
133 native_handle_delete(handle);
134 mHandle = nullptr;
135 }
136}
137
Martijn Coenen72110162016-08-19 14:28:25 +0200138static const char *const kEmptyString = "";
139
Steven Moreland7daef882019-04-18 12:54:56 -0700140hidl_string::hidl_string() {
141 memset(this, 0, sizeof(*this));
142 // mSize is zero
143 // mOwnsBuffer is false
144 mBuffer = kEmptyString;
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