blob: e97797d2bde87e1dde4877cacac813bc87ebffbd [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>
24#include <hidl-util/FQName.h>
Yifan Hong64a41d52017-02-27 18:52:44 -080025#include <vintf/VintfObject.h>
Yifan Hong44c0e572017-01-20 15:41:52 -080026#include <vintf/parse_string.h>
Martijn Coenen30791002016-12-01 15:40:46 +010027
Martijn Coenen72110162016-08-19 14:28:25 +020028namespace android {
29namespace hardware {
30
Yifan Hong64a41d52017-02-27 18:52:44 -080031vintf::Transport getTransportFromManifest(
Yifan Hong37b36202017-02-28 16:04:22 -080032 const FQName &fqName, const std::string &instanceName,
Yifan Hong64a41d52017-02-27 18:52:44 -080033 const vintf::HalManifest *vm) {
Yifan Hong44c0e572017-01-20 15:41:52 -080034 if (vm == nullptr) {
Yifan Hong20273f92017-01-30 14:13:19 -080035 return vintf::Transport::EMPTY;
36 }
Yifan Hong1152a2a2017-03-13 15:26:01 -070037 return vm->getTransport(fqName.package(),
Yifan Hong37b36202017-02-28 16:04:22 -080038 vintf::Version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()},
39 fqName.name(), instanceName);
Yifan Hong44c0e572017-01-20 15:41:52 -080040}
41
Yifan Hong37b36202017-02-28 16:04:22 -080042vintf::Transport getTransport(const std::string &interfaceName, const std::string &instanceName) {
43 FQName fqName(interfaceName);
Yifan Hong20273f92017-01-30 14:13:19 -080044 if (!fqName.isValid()) {
Yifan Hong37b36202017-02-28 16:04:22 -080045 LOG(ERROR) << "getTransport: " << interfaceName << " is not a valid fully-qualified name.";
Steven Moreland6d8960d2017-02-23 13:19:32 -080046 return vintf::Transport::EMPTY;
47 }
48 if (!fqName.hasVersion()) {
49 LOG(ERROR) << "getTransport: " << fqName.string()
50 << " does not specify a version. Using default transport.";
Yifan Hong20273f92017-01-30 14:13:19 -080051 return vintf::Transport::EMPTY;
52 }
Yifan Hong37b36202017-02-28 16:04:22 -080053 if (fqName.name().empty()) {
54 LOG(ERROR) << "getTransport: " << fqName.string()
55 << " does not specify an interface name. Using default transport.";
56 return vintf::Transport::EMPTY;
57 }
Yifan Hong1152a2a2017-03-13 15:26:01 -070058
59 vintf::Transport tr = getTransportFromManifest(fqName, instanceName,
60 vintf::VintfObject::GetFrameworkHalManifest());
61 if (tr != vintf::Transport::EMPTY) {
62 return tr;
Yifan Hong20273f92017-01-30 14:13:19 -080063 }
Yifan Hong1152a2a2017-03-13 15:26:01 -070064 tr = getTransportFromManifest(fqName, instanceName,
Yifan Hong64a41d52017-02-27 18:52:44 -080065 vintf::VintfObject::GetDeviceHalManifest());
Yifan Hong1152a2a2017-03-13 15:26:01 -070066 if (tr != vintf::Transport::EMPTY) {
67 return tr;
68 }
69
70 LOG(WARNING) << "getTransportFromManifest: Cannot find entry "
71 << fqName.string()
72 << " in either framework or device manifest, using default transport.";
73 return vintf::Transport::EMPTY;
Yifan Hong20273f92017-01-30 14:13:19 -080074}
75
Yifan Hong24332ef2017-03-07 16:22:19 -080076namespace details {
77bool debuggable() {
78#ifdef LIBHIDL_TARGET_DEBUGGABLE
79 return true;
80#else
81 return false;
82#endif
83}
84} // namespace details
85
Martijn Coenen04b91c02017-01-19 14:14:21 +010086hidl_handle::hidl_handle() {
87 mHandle = nullptr;
88 mOwnsHandle = false;
89}
90
91hidl_handle::~hidl_handle() {
92 freeHandle();
93}
94
95hidl_handle::hidl_handle(const native_handle_t *handle) {
96 mHandle = handle;
97 mOwnsHandle = false;
98}
99
100// copy constructor.
101hidl_handle::hidl_handle(const hidl_handle &other) {
102 mOwnsHandle = false;
103 *this = other;
104}
105
106// move constructor.
107hidl_handle::hidl_handle(hidl_handle &&other) {
108 mOwnsHandle = false;
109 *this = std::move(other);
110}
111
112// assignment operators
113hidl_handle &hidl_handle::operator=(const hidl_handle &other) {
114 if (this == &other) {
115 return *this;
116 }
117 freeHandle();
118 if (other.mHandle != nullptr) {
119 mHandle = native_handle_clone(other.mHandle);
120 if (mHandle == nullptr) {
121 LOG(FATAL) << "Failed to clone native_handle in hidl_handle.";
122 }
123 mOwnsHandle = true;
124 } else {
125 mHandle = nullptr;
126 mOwnsHandle = false;
127 }
128 return *this;
129}
130
131hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) {
132 freeHandle();
133 mHandle = native_handle;
134 mOwnsHandle = false;
135 return *this;
136}
137
138hidl_handle &hidl_handle::operator=(hidl_handle &&other) {
139 if (this != &other) {
140 freeHandle();
141 mHandle = other.mHandle;
142 mOwnsHandle = other.mOwnsHandle;
143 other.mHandle = nullptr;
144 other.mOwnsHandle = false;
145 }
146 return *this;
147}
148
149void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) {
150 mHandle = handle;
151 mOwnsHandle = shouldOwn;
152}
153
154const native_handle_t* hidl_handle::operator->() const {
155 return mHandle;
156}
157
158// implicit conversion to const native_handle_t*
159hidl_handle::operator const native_handle_t *() const {
160 return mHandle;
161}
162
163// explicit conversion
164const native_handle_t *hidl_handle::getNativeHandle() const {
165 return mHandle;
166}
167
168void hidl_handle::freeHandle() {
169 if (mOwnsHandle && mHandle != nullptr) {
170 // This can only be true if:
171 // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
172 // wasn't const to begin with.
173 // 2. Copy/assignment from another hidl_handle, in which case we have
174 // cloned the handle.
175 // 3. Move constructor from another hidl_handle, in which case the original
176 // hidl_handle must have been non-const as well.
177 native_handle_t *handle = const_cast<native_handle_t*>(
178 static_cast<const native_handle_t*>(mHandle));
179 native_handle_close(handle);
180 native_handle_delete(handle);
181 mHandle = nullptr;
182 }
183}
184
Martijn Coenen72110162016-08-19 14:28:25 +0200185static const char *const kEmptyString = "";
186
187hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -0700188 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +0200189 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -0700190 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200191}
192
193hidl_string::~hidl_string() {
194 clear();
195}
196
Steven Morelande03c0872016-10-24 10:43:50 -0700197hidl_string::hidl_string(const char *s) : hidl_string() {
Steven Morelanda21d84f2017-02-16 09:23:45 -0800198 if (s == nullptr) {
199 return;
200 }
201
Yifan Hong602b85a2016-10-24 13:40:01 -0700202 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -0700203}
204
Steven Moreland53120f72017-01-12 09:39:26 -0800205hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
206 copyFrom(s, length);
207}
208
Yifan Hong602b85a2016-10-24 13:40:01 -0700209hidl_string::hidl_string(const hidl_string &other): hidl_string() {
210 copyFrom(other.c_str(), other.size());
211}
212
213hidl_string::hidl_string(const std::string &s) : hidl_string() {
214 copyFrom(s.c_str(), s.size());
215}
216
217hidl_string::hidl_string(hidl_string &&other): hidl_string() {
218 moveFrom(std::forward<hidl_string>(other));
219}
220
221hidl_string &hidl_string::operator=(hidl_string &&other) {
222 if (this != &other) {
223 clear();
224 moveFrom(std::forward<hidl_string>(other));
225 }
226 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200227}
228
229hidl_string &hidl_string::operator=(const hidl_string &other) {
230 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700231 clear();
232 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +0200233 }
234
235 return *this;
236}
237
238hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700239 clear();
Steven Moreland153f87a2017-02-28 09:42:26 -0800240
241 if (s == nullptr) {
242 return *this;
243 }
244
Yifan Hong602b85a2016-10-24 13:40:01 -0700245 copyFrom(s, strlen(s));
246 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200247}
248
Yifan Hong602b85a2016-10-24 13:40:01 -0700249hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +0200250 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -0700251 copyFrom(s.c_str(), s.size());
252 return *this;
253}
Martijn Coenen72110162016-08-19 14:28:25 +0200254
Yifan Hong602b85a2016-10-24 13:40:01 -0700255hidl_string::operator std::string() const {
256 return std::string(mBuffer, mSize);
257}
258
259hidl_string::operator const char *() const {
260 return mBuffer;
261}
262
263void hidl_string::copyFrom(const char *data, size_t size) {
264 // assume my resources are freed.
265
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100266 if (size > UINT32_MAX) {
267 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
268 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700269 char *buf = (char *)malloc(size + 1);
270 memcpy(buf, data, size);
271 buf[size] = '\0';
272 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200273
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100274 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200275 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700276}
Martijn Coenen72110162016-08-19 14:28:25 +0200277
Yifan Hong602b85a2016-10-24 13:40:01 -0700278void hidl_string::moveFrom(hidl_string &&other) {
279 // assume my resources are freed.
280
Hridya Valsaraju01268892017-02-27 08:48:38 -0800281 mBuffer = std::move(other.mBuffer);
Yifan Hong602b85a2016-10-24 13:40:01 -0700282 mSize = other.mSize;
283 mOwnsBuffer = other.mOwnsBuffer;
284
285 other.mOwnsBuffer = false;
Hridya Valsaraju01268892017-02-27 08:48:38 -0800286 other.clear();
Martijn Coenen72110162016-08-19 14:28:25 +0200287}
288
289void hidl_string::clear() {
290 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100291 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200292 }
293
Yifan Hong602b85a2016-10-24 13:40:01 -0700294 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200295 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700296 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200297}
298
299void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100300 if (size > UINT32_MAX) {
301 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
302 }
Martijn Coenen72110162016-08-19 14:28:25 +0200303 clear();
304
Yifan Hong602b85a2016-10-24 13:40:01 -0700305 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100306 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200307 mOwnsBuffer = false;
308}
309
310const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700311 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200312}
313
314size_t hidl_string::size() const {
315 return mSize;
316}
317
318bool hidl_string::empty() const {
319 return mSize == 0;
320}
321
Martijn Coenen72110162016-08-19 14:28:25 +0200322} // namespace hardware
323} // namespace android
324
325