blob: 91d56e7aca46547792601ce5b589489087b74a6e [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,
33 const std::string &manifestName,
Yifan Hong64a41d52017-02-27 18:52:44 -080034 const vintf::HalManifest *vm) {
Yifan Hong44c0e572017-01-20 15:41:52 -080035 if (vm == nullptr) {
Yifan Hong64a41d52017-02-27 18:52:44 -080036 LOG(WARNING) << "getTransportFromManifest: No " << manifestName << " manifest defined, "
37 << "using default transport for " << fqName.string();
Yifan Hong20273f92017-01-30 14:13:19 -080038 return vintf::Transport::EMPTY;
39 }
Yifan Hong64a41d52017-02-27 18:52:44 -080040 vintf::Transport tr = vm->getTransport(fqName.package(),
Yifan Hong37b36202017-02-28 16:04:22 -080041 vintf::Version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()},
42 fqName.name(), instanceName);
Yifan Hong44c0e572017-01-20 15:41:52 -080043 if (tr == vintf::Transport::EMPTY) {
Yifan Hong64a41d52017-02-27 18:52:44 -080044 LOG(WARNING) << "getTransportFromManifest: Cannot find entry "
45 << fqName.string()
46 << " in " << manifestName << " manifest, using default transport.";
Yifan Hong44c0e572017-01-20 15:41:52 -080047 } else {
Yifan Hong64a41d52017-02-27 18:52:44 -080048 LOG(DEBUG) << "getTransportFromManifest: " << fqName.string()
49 << " declares transport method " << to_string(tr)
50 << " in " << manifestName << " manifest";
Yifan Hong20273f92017-01-30 14:13:19 -080051 }
Yifan Hong44c0e572017-01-20 15:41:52 -080052 return tr;
53}
54
Yifan Hong37b36202017-02-28 16:04:22 -080055vintf::Transport getTransport(const std::string &interfaceName, const std::string &instanceName) {
56 FQName fqName(interfaceName);
Yifan Hong20273f92017-01-30 14:13:19 -080057 if (!fqName.isValid()) {
Yifan Hong37b36202017-02-28 16:04:22 -080058 LOG(ERROR) << "getTransport: " << interfaceName << " is not a valid fully-qualified name.";
Steven Moreland6d8960d2017-02-23 13:19:32 -080059 return vintf::Transport::EMPTY;
60 }
61 if (!fqName.hasVersion()) {
62 LOG(ERROR) << "getTransport: " << fqName.string()
63 << " does not specify a version. Using default transport.";
Yifan Hong20273f92017-01-30 14:13:19 -080064 return vintf::Transport::EMPTY;
65 }
Yifan Hong37b36202017-02-28 16:04:22 -080066 if (fqName.name().empty()) {
67 LOG(ERROR) << "getTransport: " << fqName.string()
68 << " does not specify an interface name. Using default transport.";
69 return vintf::Transport::EMPTY;
70 }
Yifan Hong64a41d52017-02-27 18:52:44 -080071 // TODO(b/34772739): modify the list if other packages are added to system/manifest.xml
Yifan Hong20273f92017-01-30 14:13:19 -080072 if (fqName.inPackage("android.hidl")) {
Yifan Hong37b36202017-02-28 16:04:22 -080073 return getTransportFromManifest(fqName, instanceName, "framework",
Yifan Hong64a41d52017-02-27 18:52:44 -080074 vintf::VintfObject::GetFrameworkHalManifest());
Yifan Hong20273f92017-01-30 14:13:19 -080075 }
Yifan Hong37b36202017-02-28 16:04:22 -080076 return getTransportFromManifest(fqName, instanceName, "device",
Yifan Hong64a41d52017-02-27 18:52:44 -080077 vintf::VintfObject::GetDeviceHalManifest());
Yifan Hong20273f92017-01-30 14:13:19 -080078}
79
Martijn Coenen04b91c02017-01-19 14:14:21 +010080hidl_handle::hidl_handle() {
81 mHandle = nullptr;
82 mOwnsHandle = false;
83}
84
85hidl_handle::~hidl_handle() {
86 freeHandle();
87}
88
89hidl_handle::hidl_handle(const native_handle_t *handle) {
90 mHandle = handle;
91 mOwnsHandle = false;
92}
93
94// copy constructor.
95hidl_handle::hidl_handle(const hidl_handle &other) {
96 mOwnsHandle = false;
97 *this = other;
98}
99
100// move constructor.
101hidl_handle::hidl_handle(hidl_handle &&other) {
102 mOwnsHandle = false;
103 *this = std::move(other);
104}
105
106// assignment operators
107hidl_handle &hidl_handle::operator=(const hidl_handle &other) {
108 if (this == &other) {
109 return *this;
110 }
111 freeHandle();
112 if (other.mHandle != nullptr) {
113 mHandle = native_handle_clone(other.mHandle);
114 if (mHandle == nullptr) {
115 LOG(FATAL) << "Failed to clone native_handle in hidl_handle.";
116 }
117 mOwnsHandle = true;
118 } else {
119 mHandle = nullptr;
120 mOwnsHandle = false;
121 }
122 return *this;
123}
124
125hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) {
126 freeHandle();
127 mHandle = native_handle;
128 mOwnsHandle = false;
129 return *this;
130}
131
132hidl_handle &hidl_handle::operator=(hidl_handle &&other) {
133 if (this != &other) {
134 freeHandle();
135 mHandle = other.mHandle;
136 mOwnsHandle = other.mOwnsHandle;
137 other.mHandle = nullptr;
138 other.mOwnsHandle = false;
139 }
140 return *this;
141}
142
143void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) {
144 mHandle = handle;
145 mOwnsHandle = shouldOwn;
146}
147
148const native_handle_t* hidl_handle::operator->() const {
149 return mHandle;
150}
151
152// implicit conversion to const native_handle_t*
153hidl_handle::operator const native_handle_t *() const {
154 return mHandle;
155}
156
157// explicit conversion
158const native_handle_t *hidl_handle::getNativeHandle() const {
159 return mHandle;
160}
161
162void hidl_handle::freeHandle() {
163 if (mOwnsHandle && mHandle != nullptr) {
164 // This can only be true if:
165 // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
166 // wasn't const to begin with.
167 // 2. Copy/assignment from another hidl_handle, in which case we have
168 // cloned the handle.
169 // 3. Move constructor from another hidl_handle, in which case the original
170 // hidl_handle must have been non-const as well.
171 native_handle_t *handle = const_cast<native_handle_t*>(
172 static_cast<const native_handle_t*>(mHandle));
173 native_handle_close(handle);
174 native_handle_delete(handle);
175 mHandle = nullptr;
176 }
177}
178
Martijn Coenen72110162016-08-19 14:28:25 +0200179static const char *const kEmptyString = "";
180
181hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -0700182 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +0200183 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -0700184 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200185}
186
187hidl_string::~hidl_string() {
188 clear();
189}
190
Steven Morelande03c0872016-10-24 10:43:50 -0700191hidl_string::hidl_string(const char *s) : hidl_string() {
Steven Morelanda21d84f2017-02-16 09:23:45 -0800192 if (s == nullptr) {
193 return;
194 }
195
Yifan Hong602b85a2016-10-24 13:40:01 -0700196 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -0700197}
198
Steven Moreland53120f72017-01-12 09:39:26 -0800199hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
200 copyFrom(s, length);
201}
202
Yifan Hong602b85a2016-10-24 13:40:01 -0700203hidl_string::hidl_string(const hidl_string &other): hidl_string() {
204 copyFrom(other.c_str(), other.size());
205}
206
207hidl_string::hidl_string(const std::string &s) : hidl_string() {
208 copyFrom(s.c_str(), s.size());
209}
210
211hidl_string::hidl_string(hidl_string &&other): hidl_string() {
212 moveFrom(std::forward<hidl_string>(other));
213}
214
215hidl_string &hidl_string::operator=(hidl_string &&other) {
216 if (this != &other) {
217 clear();
218 moveFrom(std::forward<hidl_string>(other));
219 }
220 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200221}
222
223hidl_string &hidl_string::operator=(const hidl_string &other) {
224 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700225 clear();
226 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +0200227 }
228
229 return *this;
230}
231
232hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700233 clear();
Steven Moreland153f87a2017-02-28 09:42:26 -0800234
235 if (s == nullptr) {
236 return *this;
237 }
238
Yifan Hong602b85a2016-10-24 13:40:01 -0700239 copyFrom(s, strlen(s));
240 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200241}
242
Yifan Hong602b85a2016-10-24 13:40:01 -0700243hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +0200244 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -0700245 copyFrom(s.c_str(), s.size());
246 return *this;
247}
Martijn Coenen72110162016-08-19 14:28:25 +0200248
Yifan Hong602b85a2016-10-24 13:40:01 -0700249hidl_string::operator std::string() const {
250 return std::string(mBuffer, mSize);
251}
252
253hidl_string::operator const char *() const {
254 return mBuffer;
255}
256
257void hidl_string::copyFrom(const char *data, size_t size) {
258 // assume my resources are freed.
259
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100260 if (size > UINT32_MAX) {
261 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
262 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700263 char *buf = (char *)malloc(size + 1);
264 memcpy(buf, data, size);
265 buf[size] = '\0';
266 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200267
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100268 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200269 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700270}
Martijn Coenen72110162016-08-19 14:28:25 +0200271
Yifan Hong602b85a2016-10-24 13:40:01 -0700272void hidl_string::moveFrom(hidl_string &&other) {
273 // assume my resources are freed.
274
Hridya Valsaraju01268892017-02-27 08:48:38 -0800275 mBuffer = std::move(other.mBuffer);
Yifan Hong602b85a2016-10-24 13:40:01 -0700276 mSize = other.mSize;
277 mOwnsBuffer = other.mOwnsBuffer;
278
279 other.mOwnsBuffer = false;
Hridya Valsaraju01268892017-02-27 08:48:38 -0800280 other.clear();
Martijn Coenen72110162016-08-19 14:28:25 +0200281}
282
283void hidl_string::clear() {
284 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100285 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200286 }
287
Yifan Hong602b85a2016-10-24 13:40:01 -0700288 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200289 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700290 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200291}
292
293void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100294 if (size > UINT32_MAX) {
295 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
296 }
Martijn Coenen72110162016-08-19 14:28:25 +0200297 clear();
298
Yifan Hong602b85a2016-10-24 13:40:01 -0700299 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100300 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200301 mOwnsBuffer = false;
302}
303
304const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700305 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200306}
307
308size_t hidl_string::size() const {
309 return mSize;
310}
311
312bool hidl_string::empty() const {
313 return mSize == 0;
314}
315
Martijn Coenen72110162016-08-19 14:28:25 +0200316} // namespace hardware
317} // namespace android
318
319