blob: 7580693f9503bc763f5afe578e2fb7cd3aef6e5f [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.
59hidl_handle::hidl_handle(hidl_handle &&other) {
60 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
90hidl_handle &hidl_handle::operator=(hidl_handle &&other) {
91 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) {
102 mHandle = handle;
103 mOwnsHandle = shouldOwn;
104}
105
106const native_handle_t* hidl_handle::operator->() const {
107 return mHandle;
108}
109
110// implicit conversion to const native_handle_t*
111hidl_handle::operator const native_handle_t *() const {
112 return mHandle;
113}
114
115// explicit conversion
116const native_handle_t *hidl_handle::getNativeHandle() const {
117 return mHandle;
118}
119
120void hidl_handle::freeHandle() {
121 if (mOwnsHandle && mHandle != nullptr) {
122 // This can only be true if:
123 // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
124 // wasn't const to begin with.
125 // 2. Copy/assignment from another hidl_handle, in which case we have
126 // cloned the handle.
127 // 3. Move constructor from another hidl_handle, in which case the original
128 // hidl_handle must have been non-const as well.
129 native_handle_t *handle = const_cast<native_handle_t*>(
130 static_cast<const native_handle_t*>(mHandle));
131 native_handle_close(handle);
132 native_handle_delete(handle);
133 mHandle = nullptr;
134 }
135}
136
Martijn Coenen72110162016-08-19 14:28:25 +0200137static const char *const kEmptyString = "";
138
139hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -0700140 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +0200141 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -0700142 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200143}
144
145hidl_string::~hidl_string() {
146 clear();
147}
148
Steven Morelande03c0872016-10-24 10:43:50 -0700149hidl_string::hidl_string(const char *s) : hidl_string() {
Steven Morelanda21d84f2017-02-16 09:23:45 -0800150 if (s == nullptr) {
151 return;
152 }
153
Yifan Hong602b85a2016-10-24 13:40:01 -0700154 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -0700155}
156
Steven Moreland53120f72017-01-12 09:39:26 -0800157hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
158 copyFrom(s, length);
159}
160
Yifan Hong602b85a2016-10-24 13:40:01 -0700161hidl_string::hidl_string(const hidl_string &other): hidl_string() {
162 copyFrom(other.c_str(), other.size());
163}
164
165hidl_string::hidl_string(const std::string &s) : hidl_string() {
166 copyFrom(s.c_str(), s.size());
167}
168
169hidl_string::hidl_string(hidl_string &&other): hidl_string() {
170 moveFrom(std::forward<hidl_string>(other));
171}
172
173hidl_string &hidl_string::operator=(hidl_string &&other) {
174 if (this != &other) {
175 clear();
176 moveFrom(std::forward<hidl_string>(other));
177 }
178 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200179}
180
181hidl_string &hidl_string::operator=(const hidl_string &other) {
182 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700183 clear();
184 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +0200185 }
186
187 return *this;
188}
189
190hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700191 clear();
Steven Moreland153f87a2017-02-28 09:42:26 -0800192
193 if (s == nullptr) {
194 return *this;
195 }
196
Yifan Hong602b85a2016-10-24 13:40:01 -0700197 copyFrom(s, strlen(s));
198 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200199}
200
Yifan Hong602b85a2016-10-24 13:40:01 -0700201hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +0200202 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -0700203 copyFrom(s.c_str(), s.size());
204 return *this;
205}
Martijn Coenen72110162016-08-19 14:28:25 +0200206
Yifan Hong602b85a2016-10-24 13:40:01 -0700207hidl_string::operator std::string() const {
208 return std::string(mBuffer, mSize);
209}
210
Scott Randolpheb0c3372017-04-03 14:07:14 -0700211std::ostream& operator<<(std::ostream& os, const hidl_string& str) {
212 os << str.c_str();
213 return os;
Yifan Hong602b85a2016-10-24 13:40:01 -0700214}
215
216void hidl_string::copyFrom(const char *data, size_t size) {
217 // assume my resources are freed.
218
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100219 if (size > UINT32_MAX) {
Elliott Hughes0e55b452017-05-01 21:38:48 -0700220 LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100221 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700222 char *buf = (char *)malloc(size + 1);
223 memcpy(buf, data, size);
224 buf[size] = '\0';
225 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200226
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100227 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200228 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700229}
Martijn Coenen72110162016-08-19 14:28:25 +0200230
Yifan Hong602b85a2016-10-24 13:40:01 -0700231void hidl_string::moveFrom(hidl_string &&other) {
232 // assume my resources are freed.
233
Hridya Valsaraju01268892017-02-27 08:48:38 -0800234 mBuffer = std::move(other.mBuffer);
Yifan Hong602b85a2016-10-24 13:40:01 -0700235 mSize = other.mSize;
236 mOwnsBuffer = other.mOwnsBuffer;
237
238 other.mOwnsBuffer = false;
Hridya Valsaraju01268892017-02-27 08:48:38 -0800239 other.clear();
Martijn Coenen72110162016-08-19 14:28:25 +0200240}
241
242void hidl_string::clear() {
243 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100244 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200245 }
246
Yifan Hong602b85a2016-10-24 13:40:01 -0700247 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200248 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700249 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200250}
251
252void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100253 if (size > UINT32_MAX) {
Elliott Hughes0e55b452017-05-01 21:38:48 -0700254 LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100255 }
Martijn Coenen72110162016-08-19 14:28:25 +0200256 clear();
257
Yifan Hong602b85a2016-10-24 13:40:01 -0700258 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100259 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200260 mOwnsBuffer = false;
261}
262
263const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700264 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200265}
266
267size_t hidl_string::size() const {
268 return mSize;
269}
270
271bool hidl_string::empty() const {
272 return mSize == 0;
273}
274
Martijn Coenen72110162016-08-19 14:28:25 +0200275} // namespace hardware
276} // namespace android
277
278