blob: 595a92896032be8384d2ad08648a0287a1a749b3 [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 Hong44c0e572017-01-20 15:41:52 -080025#include <vintf/VendorManifest.h>
26#include <vintf/parse_string.h>
Martijn Coenen30791002016-12-01 15:40:46 +010027
Martijn Coenen72110162016-08-19 14:28:25 +020028namespace android {
29namespace hardware {
Yifan Hong20273f92017-01-30 14:13:19 -080030vintf::Transport getTransportForFrameworkPackages(const std::string &name) {
Steven Moreland23bb9992017-02-16 19:23:24 -080031 // TODO(b/34772739): move to framework vintf
Yifan Hong20273f92017-01-30 14:13:19 -080032 const static std::unordered_map<std::string, vintf::Transport> sTransports {
33 {"android.hidl.manager@1.0::IServiceManager", vintf::Transport::HWBINDER},
Steven Moreland23bb9992017-02-16 19:23:24 -080034 {"android.hidl.allocator@1.0::IAllocator" , vintf::Transport::HWBINDER},
Yifan Hong20273f92017-01-30 14:13:19 -080035 {"android.hidl.memory@1.0::IMapper" , vintf::Transport::PASSTHROUGH},
36 {"android.hidl.memory@1.0::IMemory" , vintf::Transport::PASSTHROUGH},
37 };
38 auto it = sTransports.find(name);
39 if (it == sTransports.end()) {
40 LOG(WARNING) << "getTransportForFrameworkPackages: Cannot find entry "
41 << name << " in the static map.";
42 return vintf::Transport::EMPTY;
43 } else {
44 LOG(INFO) << "getTransportForFrameworkPackages: " << name
45 << " declares transport method " << to_string(it->second);
46 }
47 return it->second;
48}
Martijn Coenen72110162016-08-19 14:28:25 +020049
Yifan Hong20273f92017-01-30 14:13:19 -080050vintf::Transport getTransportForHals(const FQName &fqName) {
51 const std::string package = fqName.package();
Yifan Hong44c0e572017-01-20 15:41:52 -080052 const vintf::VendorManifest *vm = vintf::VendorManifest::Get();
53 if (vm == nullptr) {
Steven Moreland01732732017-02-02 18:42:49 -080054 LOG(WARNING) << "getTransportFromManifest: Cannot find vendor interface manifest.";
Yifan Hong44c0e572017-01-20 15:41:52 -080055 return vintf::Transport::EMPTY;
56 }
Yifan Honge682a5a2017-02-13 18:14:39 +000057 if (!fqName.hasVersion()) {
Yifan Hong20273f92017-01-30 14:13:19 -080058 LOG(ERROR) << "getTransportFromManifest: " << fqName.string()
59 << " does not specify a version.";
60 return vintf::Transport::EMPTY;
61 }
Yifan Honge682a5a2017-02-13 18:14:39 +000062 vintf::Transport tr = vm->getTransport(package,
63 vintf::Version{fqName.getPackageMajorVersion(), fqName.getPackageMinorVersion()});
Yifan Hong44c0e572017-01-20 15:41:52 -080064 if (tr == vintf::Transport::EMPTY) {
65 LOG(WARNING) << "getTransportFromManifest: Cannot find entry "
Yifan Hong20273f92017-01-30 14:13:19 -080066 << package << fqName.atVersion() << " in vendor interface manifest.";
Yifan Hong44c0e572017-01-20 15:41:52 -080067 } else {
Yifan Hong20273f92017-01-30 14:13:19 -080068 LOG(INFO) << "getTransportFromManifest: " << package << fqName.atVersion()
Yifan Hong44c0e572017-01-20 15:41:52 -080069 << " declares transport method " << to_string(tr);
Yifan Hong20273f92017-01-30 14:13:19 -080070 }
Yifan Hong44c0e572017-01-20 15:41:52 -080071 return tr;
72}
73
Yifan Hong20273f92017-01-30 14:13:19 -080074vintf::Transport getTransport(const std::string &name) {
75 FQName fqName(name);
76 if (!fqName.isValid()) {
77 LOG(WARNING) << name << " is not a valid fully-qualified name.";
78 return vintf::Transport::EMPTY;
79 }
80 if (fqName.inPackage("android.hidl")) {
81 return getTransportForFrameworkPackages(name);
82 }
83 return getTransportForHals(fqName);
84}
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) {
Scott Randolphca37c0e2017-02-15 16:38:46 -0800150 freeHandle();
Martijn Coenen04b91c02017-01-19 14:14:21 +0100151 mHandle = handle;
152 mOwnsHandle = shouldOwn;
153}
154
155const native_handle_t* hidl_handle::operator->() const {
156 return mHandle;
157}
158
159// implicit conversion to const native_handle_t*
160hidl_handle::operator const native_handle_t *() const {
161 return mHandle;
162}
163
164// explicit conversion
165const native_handle_t *hidl_handle::getNativeHandle() const {
166 return mHandle;
167}
168
169void hidl_handle::freeHandle() {
170 if (mOwnsHandle && mHandle != nullptr) {
171 // This can only be true if:
172 // 1. Somebody called setTo() with shouldOwn=true, so we know the handle
173 // wasn't const to begin with.
174 // 2. Copy/assignment from another hidl_handle, in which case we have
175 // cloned the handle.
176 // 3. Move constructor from another hidl_handle, in which case the original
177 // hidl_handle must have been non-const as well.
178 native_handle_t *handle = const_cast<native_handle_t*>(
179 static_cast<const native_handle_t*>(mHandle));
180 native_handle_close(handle);
181 native_handle_delete(handle);
182 mHandle = nullptr;
183 }
184}
185
Martijn Coenen72110162016-08-19 14:28:25 +0200186static const char *const kEmptyString = "";
187
188hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -0700189 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +0200190 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -0700191 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +0200192}
193
194hidl_string::~hidl_string() {
195 clear();
196}
197
Steven Morelande03c0872016-10-24 10:43:50 -0700198hidl_string::hidl_string(const char *s) : hidl_string() {
Steven Morelanda21d84f2017-02-16 09:23:45 -0800199 if (s == nullptr) {
200 return;
201 }
202
Yifan Hong602b85a2016-10-24 13:40:01 -0700203 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -0700204}
205
Steven Moreland53120f72017-01-12 09:39:26 -0800206hidl_string::hidl_string(const char *s, size_t length) : hidl_string() {
207 copyFrom(s, length);
208}
209
Yifan Hong602b85a2016-10-24 13:40:01 -0700210hidl_string::hidl_string(const hidl_string &other): hidl_string() {
211 copyFrom(other.c_str(), other.size());
212}
213
214hidl_string::hidl_string(const std::string &s) : hidl_string() {
215 copyFrom(s.c_str(), s.size());
216}
217
218hidl_string::hidl_string(hidl_string &&other): hidl_string() {
219 moveFrom(std::forward<hidl_string>(other));
220}
221
222hidl_string &hidl_string::operator=(hidl_string &&other) {
223 if (this != &other) {
224 clear();
225 moveFrom(std::forward<hidl_string>(other));
226 }
227 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200228}
229
230hidl_string &hidl_string::operator=(const hidl_string &other) {
231 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700232 clear();
233 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +0200234 }
235
236 return *this;
237}
238
239hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700240 clear();
241 copyFrom(s, strlen(s));
242 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +0200243}
244
Yifan Hong602b85a2016-10-24 13:40:01 -0700245hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +0200246 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -0700247 copyFrom(s.c_str(), s.size());
248 return *this;
249}
Martijn Coenen72110162016-08-19 14:28:25 +0200250
Yifan Hong602b85a2016-10-24 13:40:01 -0700251hidl_string::operator std::string() const {
252 return std::string(mBuffer, mSize);
253}
254
255hidl_string::operator const char *() const {
256 return mBuffer;
257}
258
259void hidl_string::copyFrom(const char *data, size_t size) {
260 // assume my resources are freed.
261
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100262 if (size > UINT32_MAX) {
263 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
264 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700265 char *buf = (char *)malloc(size + 1);
266 memcpy(buf, data, size);
267 buf[size] = '\0';
268 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200269
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100270 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200271 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700272}
Martijn Coenen72110162016-08-19 14:28:25 +0200273
Yifan Hong602b85a2016-10-24 13:40:01 -0700274void hidl_string::moveFrom(hidl_string &&other) {
275 // assume my resources are freed.
276
277 mBuffer = other.mBuffer;
278 mSize = other.mSize;
279 mOwnsBuffer = other.mOwnsBuffer;
280
281 other.mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200282}
283
284void hidl_string::clear() {
285 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100286 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200287 }
288
Yifan Hong602b85a2016-10-24 13:40:01 -0700289 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200290 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700291 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200292}
293
294void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100295 if (size > UINT32_MAX) {
296 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
297 }
Martijn Coenen72110162016-08-19 14:28:25 +0200298 clear();
299
Yifan Hong602b85a2016-10-24 13:40:01 -0700300 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100301 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200302 mOwnsBuffer = false;
303}
304
305const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700306 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200307}
308
309size_t hidl_string::size() const {
310 return mSize;
311}
312
313bool hidl_string::empty() const {
314 return mSize == 0;
315}
316
Martijn Coenen72110162016-08-19 14:28:25 +0200317} // namespace hardware
318} // namespace android
319
320