blob: f9a23e86380b10a8bf4e89bf11e528f8400bef2b [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 Moreland0cea8572019-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 Moreland0cea8572019-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 Moreland0cea8572019-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 Moreland0cea8572019-04-18 12:54:56 -070060hidl_handle::hidl_handle(hidl_handle&& other) : 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) {
74 LOG(FATAL) << "Failed to clone native_handle in hidl_handle.";
75 }
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
91hidl_handle &hidl_handle::operator=(hidl_handle &&other) {
92 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 Moreland0cea8572019-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
172hidl_string::hidl_string(hidl_string &&other): hidl_string() {
173 moveFrom(std::forward<hidl_string>(other));
174}
175
176hidl_string &hidl_string::operator=(hidl_string &&other) {
177 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
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100222 if (size > UINT32_MAX) {
223 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
224 }
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) {
257 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
258 }
Martijn Coenen72110162016-08-19 14:28:25 +0200259 clear();
260
Yifan Hong602b85a2016-10-24 13:40:01 -0700261 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100262 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200263 mOwnsBuffer = false;
264}
265
266const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700267 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200268}
269
270size_t hidl_string::size() const {
271 return mSize;
272}
273
274bool hidl_string::empty() const {
275 return mSize == 0;
276}
277
Martijn Coenen72110162016-08-19 14:28:25 +0200278} // namespace hardware
279} // namespace android
280
281