blob: f97f216df29d63350f7a17ad4b93ff35cbddeb80 [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 Moreland5cc16912019-04-18 12:54:56 -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 Moreland5cc16912019-04-18 12:54:56 -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 Moreland5cc16912019-04-18 12:54:56 -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 Moreland5cc16912019-04-18 12:54:56 -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
Steven Moreland5cc16912019-04-18 12:54:56 -0700141hidl_string::hidl_string() {
142 memset(this, 0, sizeof(*this));
143 // mSize is zero
144 // mOwnsBuffer is false
145 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200146}
147
148hidl_string::~hidl_string() {
149 clear();
150}
151
Steven Morelande03c0872016-10-24 10:43:50 -0700152hidl_string::hidl_string(const char *s) : hidl_string() {
Steven Morelanda21d84f2017-02-16 09:23:45 -0800153 if (s == nullptr) {
154 return;
155 }
156
Yifan Hong602b85a2016-10-24 13:40:01 -0700157 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -0700158}
159
Steven Moreland53120f72017-01-12 09:39:26 -0800160hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
161 copyFrom(s, length);
162}
163
Yifan Hong602b85a2016-10-24 13:40:01 -0700164hidl_string::hidl_string(const hidl_string &other): hidl_string() {
165 copyFrom(other.c_str(), other.size());
166}
167
168hidl_string::hidl_string(const std::string &s) : hidl_string() {
169 copyFrom(s.c_str(), s.size());
170}
171
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700172hidl_string::hidl_string(hidl_string&& other) noexcept : hidl_string() {
Yifan Hong602b85a2016-10-24 13:40:01 -0700173 moveFrom(std::forward<hidl_string>(other));
174}
175
Chih-Hung Hsieh3833f202018-09-25 12:03:06 -0700176hidl_string& hidl_string::operator=(hidl_string&& other) noexcept {
Yifan Hong602b85a2016-10-24 13:40:01 -0700177 if (this != &other) {
178 clear();
179 moveFrom(std::forward<hidl_string>(other));
180 }
181 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200182}
183
184hidl_string &hidl_string::operator=(const hidl_string &other) {
185 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700186 clear();
187 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +0200188 }
189
190 return *this;
191}
192
193hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700194 clear();
Steven Moreland153f87a2017-02-28 09:42:26 -0800195
196 if (s == nullptr) {
197 return *this;
198 }
199
Yifan Hong602b85a2016-10-24 13:40:01 -0700200 copyFrom(s, strlen(s));
201 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200202}
203
Yifan Hong602b85a2016-10-24 13:40:01 -0700204hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +0200205 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -0700206 copyFrom(s.c_str(), s.size());
207 return *this;
208}
Martijn Coenen72110162016-08-19 14:28:25 +0200209
Yifan Hong602b85a2016-10-24 13:40:01 -0700210hidl_string::operator std::string() const {
211 return std::string(mBuffer, mSize);
212}
213
Scott Randolph0c84ab42017-04-03 14:07:14 -0700214std::ostream& operator<<(std::ostream& os, const hidl_string& str) {
215 os << str.c_str();
216 return os;
Yifan Hong602b85a2016-10-24 13:40:01 -0700217}
218
219void hidl_string::copyFrom(const char *data, size_t size) {
220 // assume my resources are freed.
221
Steven Moreland98893802017-10-24 18:12:34 -0700222 if (size >= UINT32_MAX) {
Elliott Hughes0e55b452017-05-01 21:38:48 -0700223 LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100224 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700225 char *buf = (char *)malloc(size + 1);
226 memcpy(buf, data, size);
227 buf[size] = '\0';
228 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200229
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100230 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200231 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700232}
Martijn Coenen72110162016-08-19 14:28:25 +0200233
Yifan Hong602b85a2016-10-24 13:40:01 -0700234void hidl_string::moveFrom(hidl_string &&other) {
235 // assume my resources are freed.
236
Hridya Valsaraju01268892017-02-27 08:48:38 -0800237 mBuffer = std::move(other.mBuffer);
Yifan Hong602b85a2016-10-24 13:40:01 -0700238 mSize = other.mSize;
239 mOwnsBuffer = other.mOwnsBuffer;
240
241 other.mOwnsBuffer = false;
Hridya Valsaraju01268892017-02-27 08:48:38 -0800242 other.clear();
Martijn Coenen72110162016-08-19 14:28:25 +0200243}
244
245void hidl_string::clear() {
246 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100247 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200248 }
249
Yifan Hong602b85a2016-10-24 13:40:01 -0700250 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200251 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700252 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200253}
254
255void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100256 if (size > UINT32_MAX) {
Elliott Hughes0e55b452017-05-01 21:38:48 -0700257 LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100258 }
Steven Moreland57c6fcb2018-03-06 11:31:33 -0800259
260 // When the binder driver copies this data into its buffer, it must
261 // have a zero byte there because the remote process will have a pointer
262 // directly into the read-only binder buffer. If we manually copy the
263 // data now to add a zero, then we lose the efficiency of this method.
264 // Checking here (it's also checked in the parceling code later).
265 CHECK(data[size] == '\0');
266
Martijn Coenen72110162016-08-19 14:28:25 +0200267 clear();
268
Yifan Hong602b85a2016-10-24 13:40:01 -0700269 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100270 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200271 mOwnsBuffer = false;
272}
273
274const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700275 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200276}
277
278size_t hidl_string::size() const {
279 return mSize;
280}
281
282bool hidl_string::empty() const {
283 return mSize == 0;
284}
285
Howard Chen8e42f5a2017-11-09 13:58:28 +0800286sp<HidlMemory> HidlMemory::getInstance(const hidl_memory& mem) {
287 sp<HidlMemory> instance = new HidlMemory();
288 instance->hidl_memory::operator=(mem);
289 return instance;
290}
291
Howard Chen9bc35c42017-10-13 14:21:46 +0800292sp<HidlMemory> HidlMemory::getInstance(hidl_memory&& mem) {
293 sp<HidlMemory> instance = new HidlMemory();
Howard Chen8e42f5a2017-11-09 13:58:28 +0800294 instance->hidl_memory::operator=(std::move(mem));
Howard Chen9bc35c42017-10-13 14:21:46 +0800295 return instance;
296}
297
298sp<HidlMemory> HidlMemory::getInstance(const hidl_string& name, int fd, uint64_t size) {
299 native_handle_t* handle = native_handle_create(1, 0);
300 if (!handle) {
301 close(fd);
302 LOG(ERROR) << "native_handle_create fails";
303 return new HidlMemory();
304 }
305 handle->data[0] = fd;
306
307 hidl_handle hidlHandle;
308 hidlHandle.setTo(handle, true /* shouldOwn */);
309
310 sp<HidlMemory> instance = new HidlMemory(name, std::move(hidlHandle), size);
311 return instance;
312}
313
Howard Chen8e42f5a2017-11-09 13:58:28 +0800314HidlMemory::HidlMemory() : hidl_memory() {}
315
316HidlMemory::HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size)
317 : hidl_memory(name, std::move(handle), size) {}
318
Howard Chen9bc35c42017-10-13 14:21:46 +0800319// it's required to have at least one out-of-line method to avoid weak vtable
Alec Edgingtonaf789482018-06-14 21:03:14 +0100320HidlMemory::~HidlMemory() {}
Howard Chen9bc35c42017-10-13 14:21:46 +0800321
Martijn Coenen72110162016-08-19 14:28:25 +0200322} // namespace hardware
323} // namespace android
324
325