blob: 174e54462cc5c5e2e065924945fcb1593fc7b561 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 */
16
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "Interface.h"
18
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070019#include "Annotation.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010020#include "DeathRecipientType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070021#include "Method.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010022#include "ScalarType.h"
Yifan Hong10fe0b52016-10-19 14:20:17 -070023#include "StringType.h"
24#include "VectorType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070025
Steven Moreland14ee6742016-10-18 12:58:28 -070026#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070028#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070029#include <iostream>
Yifan Hong10fe0b52016-10-19 14:20:17 -070030#include <sstream>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070031
Andreas Huberc9410c72016-07-28 12:18:40 -070032namespace android {
33
Yifan Hong10fe0b52016-10-19 14:20:17 -070034/* It is very important that these values NEVER change. These values
35 * must remain unchanged over the lifetime of android. This is
36 * because the framework on a device will be updated independently of
37 * the hals on a device. If the hals are compiled with one set of
38 * transaction values, and the framework with another, then the
39 * interface between them will be destroyed, and the device will not
40 * work.
41 */
42enum {
43 // These values are defined in hardware::IBinder.
44 /////////////////// User defined transactions
45 FIRST_CALL_TRANSACTION = 0x00000001,
46 LAST_CALL_TRANSACTION = 0x00efffff,
47 /////////////////// HIDL reserved
48 FIRST_HIDL_TRANSACTION = 0x00f00000,
49 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = FIRST_HIDL_TRANSACTION,
Yifan Hongc75fd472017-01-11 12:37:31 -080050 HIDL_GET_DESCRIPTOR_TRANSACTION,
Martijn Coenenaf712c02016-11-16 15:26:27 +010051 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +010052 HIDL_LINK_TO_DEATH_TRANSACTION,
53 HIDL_UNLINK_TO_DEATH_TRANSACTION,
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080054 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
Yifan Hongcd2ae452017-01-31 14:33:40 -080055 HIDL_GET_REF_INFO_TRANSACTION,
Andreas Huber37065d62017-02-07 14:36:54 -080056 HIDL_DEBUG_TRANSACTION,
Yifan Hong10fe0b52016-10-19 14:20:17 -070057 LAST_HIDL_TRANSACTION = 0x00ffffff,
58};
59
Yifan Honga4b53d02016-10-31 17:29:10 -070060Interface::Interface(const char *localName, const Location &location, Interface *super)
61 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070062 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070063 mIsJavaCompatibleInProgress(false) {
Martijn Coenenaf712c02016-11-16 15:26:27 +010064}
65
Steven Moreland30bb6a82016-11-30 09:18:34 -080066std::string Interface::typeName() const {
67 return "interface " + localName();
68}
69
Yifan Hongffa91392017-01-31 13:41:23 -080070bool Interface::fillLinkToDeathMethod(Method *method) const {
71 if (method->name() != "linkToDeath") {
72 return false;
73 }
Martijn Coenen115d4282016-12-19 05:14:04 +010074
Yifan Hongffa91392017-01-31 13:41:23 -080075 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +010076 HIDL_LINK_TO_DEATH_TRANSACTION,
77 {
78 {IMPL_HEADER,
79 [](auto &out) {
80 out << "(void)cookie;\n"
81 << "return (recipient != nullptr);\n";
82 }
83 },
84 {IMPL_PROXY,
85 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010086 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +010087 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
88 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
89 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
90 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
91 << "return (remote()->linkToDeath(binder_recipient)"
92 << " == ::android::OK);\n";
93 }
94 },
Martijn Coenen8d12b502016-12-27 14:30:27 +010095 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +010096 }, /*cppImpl*/
97 {
98 {IMPL_HEADER,
99 [this](auto &out) {
100 out << "return true;";
101 }
102 },
103 {IMPL_PROXY,
104 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100105 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100106 }
107 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100108 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100109 } /*javaImpl*/
110 );
Yifan Hongffa91392017-01-31 13:41:23 -0800111 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100112}
113
Yifan Hongffa91392017-01-31 13:41:23 -0800114bool Interface::fillUnlinkToDeathMethod(Method *method) const {
115 if (method->name() != "unlinkToDeath") {
116 return false;
117 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100118
Yifan Hongffa91392017-01-31 13:41:23 -0800119 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100120 HIDL_UNLINK_TO_DEATH_TRANSACTION,
121 {
122 {IMPL_HEADER,
123 [](auto &out) {
124 out << "return (recipient != nullptr);\n";
125 }
126 },
127 {IMPL_PROXY,
128 [](auto &out) {
129 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
130 << "for (auto it = _hidl_mDeathRecipients.begin();"
131 << "it != _hidl_mDeathRecipients.end();"
132 << "++it) {\n";
133 out.indent([&] {
134 out.sIf("(*it)->getRecipient() == recipient", [&] {
135 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
136 << "_hidl_mDeathRecipients.erase(it);\n"
137 << "return status == ::android::OK;\n";
138 });
139 });
140 out << "}\n";
141 out << "return false;\n";
142 }
143 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100144 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100145 }, /*cppImpl*/
146 {
147 {IMPL_HEADER,
148 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100149 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100150 }
151 },
152 {IMPL_PROXY,
153 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100154 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100155 }
156 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100157 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100158 } /*javaImpl*/
159 );
Yifan Hongffa91392017-01-31 13:41:23 -0800160 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100161}
Yifan Hongffa91392017-01-31 13:41:23 -0800162bool Interface::fillSyspropsChangedMethod(Method *method) const {
163 if (method->name() != "notifySyspropsChanged") {
164 return false;
165 }
166
167 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100168 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100169 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100170 out << "::android::report_sysprop_change();\n";
171 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100172 } } }, /*cppImpl */
173 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100174 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100175 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100176 );
Yifan Hongffa91392017-01-31 13:41:23 -0800177 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700178}
179
Yifan Hongffa91392017-01-31 13:41:23 -0800180bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
181 if (method->name() != "setHALInstrumentation") {
182 return false;
183 }
184
185 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800186 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
187 {
188 {IMPL_HEADER,
189 [this](auto &out) {
190 // do nothing for base class.
191 out << "return ::android::hardware::Void();\n";
192 }
193 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800194 {IMPL_STUB,
195 [](auto &out) {
196 out << "configureInstrumentation();\n";
197 }
198 },
199 {IMPL_PASSTHROUGH,
200 [](auto &out) {
201 out << "configureInstrumentation();\n";
202 out << "return ::android::hardware::Void();\n";
203 }
204 },
205 }, /*cppImpl */
206 { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */
207 // Not support for Java Impl for now.
208 } } } /*javaImpl */
209 );
Yifan Hongffa91392017-01-31 13:41:23 -0800210 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800211}
212
Yifan Hongffa91392017-01-31 13:41:23 -0800213bool Interface::fillDescriptorChainMethod(Method *method) const {
214 if (method->name() != "interfaceChain") {
215 return false;
216 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700217
Yifan Hongffa91392017-01-31 13:41:23 -0800218 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700219 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100220 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700221 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800222 out << "_hidl_cb(";
223 out.block([&] {
224 for (const Interface *iface : chain) {
225 out << iface->fullName() << "::descriptor,\n";
226 }
227 });
228 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700229 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100230 } } }, /* cppImpl */
231 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700232 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800233 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700234 out.indent(); out.indent();
235 for (size_t i = 0; i < chain.size(); ++i) {
236 if (i != 0)
237 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800238 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700239 }
240 out << "));";
241 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800242 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100243 );
Yifan Hongffa91392017-01-31 13:41:23 -0800244 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700245}
246
Yifan Hongffa91392017-01-31 13:41:23 -0800247bool Interface::fillGetDescriptorMethod(Method *method) const {
248 if (method->name() != "interfaceDescriptor") {
249 return false;
250 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800251
Yifan Hongffa91392017-01-31 13:41:23 -0800252 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800253 HIDL_GET_DESCRIPTOR_TRANSACTION,
254 { { IMPL_HEADER, [this](auto &out) {
255 out << "_hidl_cb("
256 << fullName()
257 << "::descriptor);\n"
258 << "return ::android::hardware::Void();";
259 } } }, /* cppImpl */
260 { { IMPL_HEADER, [this](auto &out) {
261 out << "return "
262 << fullJavaName()
263 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800264 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800265 );
Yifan Hongffa91392017-01-31 13:41:23 -0800266 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800267}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700268
Yifan Hongbcffce22017-02-01 15:52:06 -0800269bool Interface::fillGetDebugInfoMethod(Method *method) const {
270 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800271 return false;
272 }
273
274 method->fillImplementation(
275 HIDL_GET_REF_INFO_TRANSACTION,
276 {
277 {IMPL_HEADER,
278 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800279 // getDebugInfo returns N/A for local objects.
280 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */ });\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800281 << "return ::android::hardware::Void();";
282 }
283 },
284 {IMPL_STUB_IMPL,
285 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800286 out << "_hidl_cb({ getpid(), reinterpret_cast<uint64_t>(this) });\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800287 << "return ::android::hardware::Void();";
288 }
289 }
290 }, /* cppImpl */
291 { { IMPL_HEADER, [this, method](auto &out) {
292 const Type &refInfo = method->results().front()->type();
293 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
294 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800295 // TODO(b/34777099): PID for java.
296 << "info.pid = -1;\n"
297 << "info.ptr = 0;\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800298 << "return info;";
299 } } } /* javaImpl */
300 );
301
302 return true;
303}
304
Andreas Huber37065d62017-02-07 14:36:54 -0800305bool Interface::fillDebugMethod(Method *method) const {
306 if (method->name() != "debug") {
307 return false;
308 }
309
310 method->fillImplementation(
311 HIDL_DEBUG_TRANSACTION,
312 {
313 {IMPL_HEADER,
314 [this](auto &out) {
315 out << "(void)fd;\n"
316 << "(void)options;\n"
317 << "return ::android::hardware::Void();";
318 }
319 },
320 }, /* cppImpl */
321 {
322 /* unused, as the debug method is hidden from Java */
323 } /* javaImpl */
324 );
325
326 return true;
327}
328
Yifan Hongffa91392017-01-31 13:41:23 -0800329static std::map<std::string, Method *> gAllReservedMethods;
330
Steven Moreland14ee6742016-10-18 12:58:28 -0700331bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800332 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800333 if (!gAllReservedMethods.emplace(method->name(), method).second) {
334 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
335 << method->name();
336 return false;
337 }
338 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800339 return true;
340 }
341
Yifan Hong10fe0b52016-10-19 14:20:17 -0700342 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700343 if (lookupMethod(method->name()) != nullptr) {
344 LOG(ERROR) << "Redefinition of method " << method->name();
345 return false;
346 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700347 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700348
Yifan Hong10fe0b52016-10-19 14:20:17 -0700349 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700350
Yifan Hong10fe0b52016-10-19 14:20:17 -0700351 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700352 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700353 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700354 ancestor = ancestor->superType();
355 }
356
Yifan Hong10fe0b52016-10-19 14:20:17 -0700357 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
358 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700359 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700360 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700361
362 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700363}
364
Yifan Hongffa91392017-01-31 13:41:23 -0800365bool Interface::addAllReservedMethods() {
366 // use a sorted map to insert them in serial ID order.
367 std::map<int32_t, Method *> reservedMethodsById;
368 for (const auto &pair : gAllReservedMethods) {
369 Method *method = pair.second->copySignature();
370 bool fillSuccess = fillDescriptorChainMethod(method)
371 || fillGetDescriptorMethod(method)
372 || fillSyspropsChangedMethod(method)
373 || fillLinkToDeathMethod(method)
374 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800375 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800376 || fillGetDebugInfoMethod(method)
377 || fillDebugMethod(method);
378
Yifan Hongffa91392017-01-31 13:41:23 -0800379 if (!fillSuccess) {
380 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
381 << method->name();
382 return false;
383 }
384 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
385 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
386 << method->name() << " and "
387 << reservedMethodsById[method->getSerialId()]->name()
388 << ", serialId = " << method->getSerialId();
389 return false;
390 }
391 }
392 for (const auto &pair : reservedMethodsById) {
393 this->mReservedMethods.push_back(pair.second);
394 }
395 return true;
396}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700397
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700398const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700399 return mSuperType;
400}
401
Yifan Hong10fe0b52016-10-19 14:20:17 -0700402std::vector<const Interface *> Interface::typeChain() const {
403 std::vector<const Interface *> v;
404 const Interface *iface = this;
405 while (iface != nullptr) {
406 v.push_back(iface);
407 iface = iface->mSuperType;
408 }
409 return v;
410}
411
Yifan Hongfe95aa22016-10-19 17:26:45 -0700412std::vector<const Interface *> Interface::superTypeChain() const {
413 return superType()->typeChain(); // should work even if superType is nullptr
414}
415
Martijn Coenenb40ef022017-01-02 15:21:46 +0100416bool Interface::isElidableType() const {
417 return true;
418}
419
Andreas Hubera2723d22016-07-29 15:36:07 -0700420bool Interface::isInterface() const {
421 return true;
422}
423
Andreas Huber295ad302016-08-16 11:35:00 -0700424bool Interface::isBinder() const {
425 return true;
426}
427
Yifan Hong10fe0b52016-10-19 14:20:17 -0700428const std::vector<Method *> &Interface::userDefinedMethods() const {
429 return mUserMethods;
430}
431
432const std::vector<Method *> &Interface::hidlReservedMethods() const {
433 return mReservedMethods;
434}
435
436std::vector<Method *> Interface::methods() const {
437 std::vector<Method *> v(mUserMethods);
438 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
439 return v;
440}
441
442std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
443 std::vector<InterfaceAndMethod> v;
444 std::vector<const Interface *> chain = typeChain();
445 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
446 const Interface *iface = *it;
447 for (Method *userMethod : iface->userDefinedMethods()) {
448 v.push_back(InterfaceAndMethod(iface, userMethod));
449 }
450 }
451 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800452 v.push_back(InterfaceAndMethod(
453 *chain.rbegin(), // IBase
454 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700455 }
456 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700457}
458
Steven Moreland14ee6742016-10-18 12:58:28 -0700459Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700460 for (const auto &tuple : allMethodsFromRoot()) {
461 Method *method = tuple.method();
462 if (method->name() == name) {
463 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700464 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700465 }
466
467 return nullptr;
468}
469
Steven Moreland40786312016-08-16 10:29:40 -0700470std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700471 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700472}
473
Yifan Hongeefe4f22017-01-04 15:32:42 -0800474std::string Interface::getProxyName() const {
475 return fqName().getInterfaceProxyName();
476}
477
478std::string Interface::getStubName() const {
479 return fqName().getInterfaceStubName();
480}
481
482std::string Interface::getHwName() const {
483 return fqName().getInterfaceHwName();
484}
485
486std::string Interface::getPassthroughName() const {
487 return fqName().getInterfacePassthroughName();
488}
489
Yifan Hong51a65092017-01-04 15:41:44 -0800490FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800491 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800492}
493
Yifan Hong51a65092017-01-04 15:41:44 -0800494FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800495 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800496}
497
Yifan Hong51a65092017-01-04 15:41:44 -0800498FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800499 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800500}
501
Steven Moreland979e0992016-09-07 09:18:08 -0700502std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700503 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700504 const std::string base =
505 std::string(specifyNamespaces ? "::android::" : "")
506 + "sp<"
507 + (specifyNamespaces ? fullName() : partialCppName())
508 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700509
510 switch (mode) {
511 case StorageMode_Stack:
512 case StorageMode_Result:
513 return base;
514
515 case StorageMode_Argument:
516 return "const " + base + "&";
517 }
518}
519
Yifan Hong4ed13472016-11-02 10:44:11 -0700520std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700521 return fullJavaName();
522}
523
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800524std::string Interface::getVtsType() const {
525 if (StringHelper::EndsWith(localName(), "Callback")) {
526 return "TYPE_HIDL_CALLBACK";
527 } else {
528 return "TYPE_HIDL_INTERFACE";
529 }
530}
531
Andreas Huber881227d2016-08-02 14:20:21 -0700532void Interface::emitReaderWriter(
533 Formatter &out,
534 const std::string &name,
535 const std::string &parcelObj,
536 bool parcelObjIsPointer,
537 bool isReader,
538 ErrorMode mode) const {
539 const std::string parcelObjDeref =
540 parcelObj + (parcelObjIsPointer ? "->" : ".");
541
542 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700543 out << "{\n";
544 out.indent();
545
Iliyan Malchev549e2592016-08-10 08:59:12 -0700546 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700547
Andreas Huber8a82ff72016-08-04 10:29:39 -0700548 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700549 << binderName << ";\n";
550
Iliyan Malchev549e2592016-08-10 08:59:12 -0700551 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700552 out << parcelObjDeref
553 << "readNullableStrongBinder(&"
554 << binderName
555 << ");\n";
556
557 handleError(out, mode);
558
559 out << name
560 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100561 << "::android::hardware::fromBinder<"
562 << fqName().cppName()
563 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800564 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100565 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800566 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100567 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700568 << binderName
569 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700570
571 out.unindent();
572 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700573 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200574 out << "if (" << name << " == nullptr) {\n";
575 out.indent();
576 out << "_hidl_err = ";
577 out << parcelObjDeref
578 << "writeStrongBinder(nullptr);\n";
579 out.unindent();
580 out << "} else {\n";
581 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800582 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
583 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800584 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100585 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800586 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800587 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800588 << ">("
589 << name
590 << ");\n";
591 });
592 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800593 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800594 out << "_hidl_err = "
595 << parcelObjDeref
596 << "writeStrongBinder(_hidl_binder);\n";
597 });
598 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800599 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800600 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
601 });
602 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200603 out.unindent();
604 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700605
Andreas Huber881227d2016-08-02 14:20:21 -0700606 handleError(out, mode);
607 }
608}
609
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800610status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
611 status_t status = Scope::emitGlobalTypeDeclarations(out);
612 if (status != OK) {
613 return status;
614 }
615 out << "std::string toString("
616 << getCppArgumentType()
617 << ");\n";
618 return OK;
619}
620
621
622status_t Interface::emitTypeDefinitions(
623 Formatter &out, const std::string prefix) const {
624 std::string space = prefix.empty() ? "" : (prefix + "::");
625 status_t err = Scope::emitTypeDefinitions(out, space + localName());
626 if (err != OK) {
627 return err;
628 }
629
630 out << "std::string toString("
631 << getCppArgumentType()
632 << " o) ";
633
634 out.block([&] {
635 out << "std::string os;\nbool ok = false;\n";
636 // TODO b/34136228 use interfaceDescriptor instead
637 out << "auto ret = o->interfaceChain([&os, &ok] (const auto &chain) ";
638 out.block([&] {
639 out.sIf("chain.size() >= 1", [&] {
640 out << "os += chain[0].c_str();\n"
641 << "ok = true;\n";
642 }).endl();
643 });
644 out << ");\n";
645 out.sIf("!ret.isOk() || !ok", [&] {
646 out << "os += \"[class or subclass of \";\n"
647 << "os += " << fullName() << "::descriptor;\n"
648 << "os += \"]\";\n";
649 }).endl();
650 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
651 << "return os;\n";
652 }).endl().endl();
653
654 return OK;
655}
656
Andreas Huber2831d512016-08-15 09:33:47 -0700657void Interface::emitJavaReaderWriter(
658 Formatter &out,
659 const std::string &parcelObj,
660 const std::string &argName,
661 bool isReader) const {
662 if (isReader) {
663 out << fullJavaName()
664 << ".asInterface("
665 << parcelObj
666 << ".readStrongBinder());\n";
667 } else {
668 out << parcelObj
669 << ".writeStrongBinder("
670 << argName
671 << " == null ? null : "
672 << argName
673 << ".asBinder());\n";
674 }
675}
676
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700677status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
678 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700679 // Skip for TypeDef as it is just an alias of a defined type.
680 if (type->isTypeDef()) {
681 continue;
682 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700683 out << "attribute: {\n";
684 out.indent();
685 status_t status = type->emitVtsTypeDeclarations(out);
686 if (status != OK) {
687 return status;
688 }
689 out.unindent();
690 out << "}\n\n";
691 }
692 return OK;
693}
694
695status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700696 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800697 if (method->isHidlReserved()) {
698 continue;
699 }
700
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700701 out << "api: {\n";
702 out.indent();
703 out << "name: \"" << method->name() << "\"\n";
704 // Generate declaration for each return value.
705 for (const auto &result : method->results()) {
706 out << "return_type_hidl: {\n";
707 out.indent();
708 status_t status = result->type().emitVtsAttributeType(out);
709 if (status != OK) {
710 return status;
711 }
712 out.unindent();
713 out << "}\n";
714 }
715 // Generate declaration for each input argument
716 for (const auto &arg : method->args()) {
717 out << "arg: {\n";
718 out.indent();
719 status_t status = arg->type().emitVtsAttributeType(out);
720 if (status != OK) {
721 return status;
722 }
723 out.unindent();
724 out << "}\n";
725 }
726 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700727 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700728 out << "callflow: {\n";
729 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700730 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700731 if (name == "entry") {
732 out << "entry: true\n";
733 } else if (name == "exit") {
734 out << "exit: true\n";
735 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700736 const AnnotationParam *param =
737 annotation->getParam("next");
738 if (param != nullptr) {
739 for (auto value : *param->getValues()) {
740 out << "next: " << value << "\n";
741 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700742 }
743 } else {
744 std::cerr << "Invalid annotation '"
745 << name << "' for method: " << method->name()
746 << ". Should be one of: entry, exit, callflow. \n";
747 return UNKNOWN_ERROR;
748 }
749 out.unindent();
750 out << "}\n";
751 }
752 out.unindent();
753 out << "}\n\n";
754 }
755 return OK;
756}
757
758status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800759 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700760 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800761 << fullName()
762 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700763 return OK;
764}
765
Steven Moreland69e7c702016-09-09 11:16:32 -0700766bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700767 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700768 if (method->isOneway()) {
769 return true;
770 }
771 }
772
773 const Interface* superClass = superType();
774
775 if (superClass != nullptr) {
776 return superClass->hasOnewayMethods();
777 }
778
779 return false;
780}
781
Andreas Huber70a59e12016-08-16 12:57:01 -0700782bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700783 if (mIsJavaCompatibleInProgress) {
784 // We're currently trying to determine if this Interface is
785 // java-compatible and something is referencing this interface through
786 // one of its methods. Assume we'll ultimately succeed, if we were wrong
787 // the original invocation of Interface::isJavaCompatible() will then
788 // return the correct "false" result.
789 return true;
790 }
791
Andreas Huber0fa9e392016-08-31 09:05:44 -0700792 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
793 mIsJavaCompatibleInProgress = false;
794 return false;
795 }
796
Andreas Huberea081b32016-08-17 15:57:47 -0700797 mIsJavaCompatibleInProgress = true;
798
Andreas Huber70a59e12016-08-16 12:57:01 -0700799 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700800 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700801 return false;
802 }
803
Yifan Hong10fe0b52016-10-19 14:20:17 -0700804 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700805 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700806 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700807 return false;
808 }
809 }
810
Andreas Huberea081b32016-08-17 15:57:47 -0700811 mIsJavaCompatibleInProgress = false;
812
Andreas Huber70a59e12016-08-16 12:57:01 -0700813 return true;
814}
815
Andreas Huberc9410c72016-07-28 12:18:40 -0700816} // namespace android
817