blob: e7ed5455e928fd2e47c5995f3509d84399eb1f8a [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,
Yifan Hong10fe0b52016-10-19 14:20:17 -070056 LAST_HIDL_TRANSACTION = 0x00ffffff,
57};
58
Yifan Honga4b53d02016-10-31 17:29:10 -070059Interface::Interface(const char *localName, const Location &location, Interface *super)
60 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070061 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070062 mIsJavaCompatibleInProgress(false) {
Martijn Coenenaf712c02016-11-16 15:26:27 +010063}
64
Steven Moreland30bb6a82016-11-30 09:18:34 -080065std::string Interface::typeName() const {
66 return "interface " + localName();
67}
68
Yifan Hongffa91392017-01-31 13:41:23 -080069bool Interface::fillLinkToDeathMethod(Method *method) const {
70 if (method->name() != "linkToDeath") {
71 return false;
72 }
Martijn Coenen115d4282016-12-19 05:14:04 +010073
Yifan Hongffa91392017-01-31 13:41:23 -080074 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +010075 HIDL_LINK_TO_DEATH_TRANSACTION,
76 {
77 {IMPL_HEADER,
78 [](auto &out) {
79 out << "(void)cookie;\n"
80 << "return (recipient != nullptr);\n";
81 }
82 },
83 {IMPL_PROXY,
84 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010085 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +010086 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
87 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
88 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
89 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
90 << "return (remote()->linkToDeath(binder_recipient)"
91 << " == ::android::OK);\n";
92 }
93 },
Martijn Coenen8d12b502016-12-27 14:30:27 +010094 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +010095 }, /*cppImpl*/
96 {
97 {IMPL_HEADER,
98 [this](auto &out) {
99 out << "return true;";
100 }
101 },
102 {IMPL_PROXY,
103 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100104 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100105 }
106 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100107 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100108 } /*javaImpl*/
109 );
Yifan Hongffa91392017-01-31 13:41:23 -0800110 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100111}
112
Yifan Hongffa91392017-01-31 13:41:23 -0800113bool Interface::fillUnlinkToDeathMethod(Method *method) const {
114 if (method->name() != "unlinkToDeath") {
115 return false;
116 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100117
Yifan Hongffa91392017-01-31 13:41:23 -0800118 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100119 HIDL_UNLINK_TO_DEATH_TRANSACTION,
120 {
121 {IMPL_HEADER,
122 [](auto &out) {
123 out << "return (recipient != nullptr);\n";
124 }
125 },
126 {IMPL_PROXY,
127 [](auto &out) {
128 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
129 << "for (auto it = _hidl_mDeathRecipients.begin();"
130 << "it != _hidl_mDeathRecipients.end();"
131 << "++it) {\n";
132 out.indent([&] {
133 out.sIf("(*it)->getRecipient() == recipient", [&] {
134 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
135 << "_hidl_mDeathRecipients.erase(it);\n"
136 << "return status == ::android::OK;\n";
137 });
138 });
139 out << "}\n";
140 out << "return false;\n";
141 }
142 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100143 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100144 }, /*cppImpl*/
145 {
146 {IMPL_HEADER,
147 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100148 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100149 }
150 },
151 {IMPL_PROXY,
152 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100153 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100154 }
155 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100156 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100157 } /*javaImpl*/
158 );
Yifan Hongffa91392017-01-31 13:41:23 -0800159 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100160}
Yifan Hongffa91392017-01-31 13:41:23 -0800161bool Interface::fillSyspropsChangedMethod(Method *method) const {
162 if (method->name() != "notifySyspropsChanged") {
163 return false;
164 }
165
166 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100167 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100168 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100169 out << "::android::report_sysprop_change();\n";
170 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100171 } } }, /*cppImpl */
172 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100173 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100174 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100175 );
Yifan Hongffa91392017-01-31 13:41:23 -0800176 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700177}
178
Yifan Hongffa91392017-01-31 13:41:23 -0800179bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
180 if (method->name() != "setHALInstrumentation") {
181 return false;
182 }
183
184 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800185 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
186 {
187 {IMPL_HEADER,
188 [this](auto &out) {
189 // do nothing for base class.
190 out << "return ::android::hardware::Void();\n";
191 }
192 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800193 {IMPL_STUB,
194 [](auto &out) {
195 out << "configureInstrumentation();\n";
196 }
197 },
198 {IMPL_PASSTHROUGH,
199 [](auto &out) {
200 out << "configureInstrumentation();\n";
201 out << "return ::android::hardware::Void();\n";
202 }
203 },
204 }, /*cppImpl */
205 { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */
206 // Not support for Java Impl for now.
207 } } } /*javaImpl */
208 );
Yifan Hongffa91392017-01-31 13:41:23 -0800209 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800210}
211
Yifan Hongffa91392017-01-31 13:41:23 -0800212bool Interface::fillDescriptorChainMethod(Method *method) const {
213 if (method->name() != "interfaceChain") {
214 return false;
215 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700216
Yifan Hongffa91392017-01-31 13:41:23 -0800217 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700218 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100219 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700220 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800221 out << "_hidl_cb(";
222 out.block([&] {
223 for (const Interface *iface : chain) {
224 out << iface->fullName() << "::descriptor,\n";
225 }
226 });
227 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700228 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100229 } } }, /* cppImpl */
230 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700231 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800232 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700233 out.indent(); out.indent();
234 for (size_t i = 0; i < chain.size(); ++i) {
235 if (i != 0)
236 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800237 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700238 }
239 out << "));";
240 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800241 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100242 );
Yifan Hongffa91392017-01-31 13:41:23 -0800243 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700244}
245
Yifan Hongffa91392017-01-31 13:41:23 -0800246bool Interface::fillGetDescriptorMethod(Method *method) const {
247 if (method->name() != "interfaceDescriptor") {
248 return false;
249 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800250
Yifan Hongffa91392017-01-31 13:41:23 -0800251 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800252 HIDL_GET_DESCRIPTOR_TRANSACTION,
253 { { IMPL_HEADER, [this](auto &out) {
254 out << "_hidl_cb("
255 << fullName()
256 << "::descriptor);\n"
257 << "return ::android::hardware::Void();";
258 } } }, /* cppImpl */
259 { { IMPL_HEADER, [this](auto &out) {
260 out << "return "
261 << fullJavaName()
262 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800263 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800264 );
Yifan Hongffa91392017-01-31 13:41:23 -0800265 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800266}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700267
Yifan Hongbcffce22017-02-01 15:52:06 -0800268bool Interface::fillGetDebugInfoMethod(Method *method) const {
269 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800270 return false;
271 }
272
273 method->fillImplementation(
274 HIDL_GET_REF_INFO_TRANSACTION,
275 {
276 {IMPL_HEADER,
277 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800278 // getDebugInfo returns N/A for local objects.
279 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */ });\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800280 << "return ::android::hardware::Void();";
281 }
282 },
283 {IMPL_STUB_IMPL,
284 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800285 out << "_hidl_cb({ getpid(), reinterpret_cast<uint64_t>(this) });\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800286 << "return ::android::hardware::Void();";
287 }
288 }
289 }, /* cppImpl */
290 { { IMPL_HEADER, [this, method](auto &out) {
291 const Type &refInfo = method->results().front()->type();
292 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
293 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800294 // TODO(b/34777099): PID for java.
295 << "info.pid = -1;\n"
296 << "info.ptr = 0;\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800297 << "return info;";
298 } } } /* javaImpl */
299 );
300
301 return true;
302}
303
Yifan Hongffa91392017-01-31 13:41:23 -0800304static std::map<std::string, Method *> gAllReservedMethods;
305
Steven Moreland14ee6742016-10-18 12:58:28 -0700306bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800307 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800308 if (!gAllReservedMethods.emplace(method->name(), method).second) {
309 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
310 << method->name();
311 return false;
312 }
313 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800314 return true;
315 }
316
Yifan Hong10fe0b52016-10-19 14:20:17 -0700317 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700318 if (lookupMethod(method->name()) != nullptr) {
319 LOG(ERROR) << "Redefinition of method " << method->name();
320 return false;
321 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700322 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700323
Yifan Hong10fe0b52016-10-19 14:20:17 -0700324 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700325
Yifan Hong10fe0b52016-10-19 14:20:17 -0700326 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700327 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700328 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700329 ancestor = ancestor->superType();
330 }
331
Yifan Hong10fe0b52016-10-19 14:20:17 -0700332 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
333 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700334 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700335 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700336
337 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700338}
339
Yifan Hongffa91392017-01-31 13:41:23 -0800340bool Interface::addAllReservedMethods() {
341 // use a sorted map to insert them in serial ID order.
342 std::map<int32_t, Method *> reservedMethodsById;
343 for (const auto &pair : gAllReservedMethods) {
344 Method *method = pair.second->copySignature();
345 bool fillSuccess = fillDescriptorChainMethod(method)
346 || fillGetDescriptorMethod(method)
347 || fillSyspropsChangedMethod(method)
348 || fillLinkToDeathMethod(method)
349 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800350 || fillSetHALInstrumentationMethod(method)
Yifan Hongbcffce22017-02-01 15:52:06 -0800351 || fillGetDebugInfoMethod(method);
Yifan Hongffa91392017-01-31 13:41:23 -0800352 if (!fillSuccess) {
353 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
354 << method->name();
355 return false;
356 }
357 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
358 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
359 << method->name() << " and "
360 << reservedMethodsById[method->getSerialId()]->name()
361 << ", serialId = " << method->getSerialId();
362 return false;
363 }
364 }
365 for (const auto &pair : reservedMethodsById) {
366 this->mReservedMethods.push_back(pair.second);
367 }
368 return true;
369}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700370
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700371const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700372 return mSuperType;
373}
374
Yifan Hong10fe0b52016-10-19 14:20:17 -0700375std::vector<const Interface *> Interface::typeChain() const {
376 std::vector<const Interface *> v;
377 const Interface *iface = this;
378 while (iface != nullptr) {
379 v.push_back(iface);
380 iface = iface->mSuperType;
381 }
382 return v;
383}
384
Yifan Hongfe95aa22016-10-19 17:26:45 -0700385std::vector<const Interface *> Interface::superTypeChain() const {
386 return superType()->typeChain(); // should work even if superType is nullptr
387}
388
Martijn Coenenb40ef022017-01-02 15:21:46 +0100389bool Interface::isElidableType() const {
390 return true;
391}
392
Andreas Hubera2723d22016-07-29 15:36:07 -0700393bool Interface::isInterface() const {
394 return true;
395}
396
Andreas Huber295ad302016-08-16 11:35:00 -0700397bool Interface::isBinder() const {
398 return true;
399}
400
Yifan Hong10fe0b52016-10-19 14:20:17 -0700401const std::vector<Method *> &Interface::userDefinedMethods() const {
402 return mUserMethods;
403}
404
405const std::vector<Method *> &Interface::hidlReservedMethods() const {
406 return mReservedMethods;
407}
408
409std::vector<Method *> Interface::methods() const {
410 std::vector<Method *> v(mUserMethods);
411 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
412 return v;
413}
414
415std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
416 std::vector<InterfaceAndMethod> v;
417 std::vector<const Interface *> chain = typeChain();
418 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
419 const Interface *iface = *it;
420 for (Method *userMethod : iface->userDefinedMethods()) {
421 v.push_back(InterfaceAndMethod(iface, userMethod));
422 }
423 }
424 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800425 v.push_back(InterfaceAndMethod(
426 *chain.rbegin(), // IBase
427 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700428 }
429 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700430}
431
Steven Moreland14ee6742016-10-18 12:58:28 -0700432Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700433 for (const auto &tuple : allMethodsFromRoot()) {
434 Method *method = tuple.method();
435 if (method->name() == name) {
436 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700437 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700438 }
439
440 return nullptr;
441}
442
Steven Moreland40786312016-08-16 10:29:40 -0700443std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700444 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700445}
446
Yifan Hongeefe4f22017-01-04 15:32:42 -0800447std::string Interface::getProxyName() const {
448 return fqName().getInterfaceProxyName();
449}
450
451std::string Interface::getStubName() const {
452 return fqName().getInterfaceStubName();
453}
454
455std::string Interface::getHwName() const {
456 return fqName().getInterfaceHwName();
457}
458
459std::string Interface::getPassthroughName() const {
460 return fqName().getInterfacePassthroughName();
461}
462
Yifan Hong51a65092017-01-04 15:41:44 -0800463FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800464 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800465}
466
Yifan Hong51a65092017-01-04 15:41:44 -0800467FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800468 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800469}
470
Yifan Hong51a65092017-01-04 15:41:44 -0800471FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800472 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800473}
474
Steven Moreland979e0992016-09-07 09:18:08 -0700475std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700476 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700477 const std::string base =
478 std::string(specifyNamespaces ? "::android::" : "")
479 + "sp<"
480 + (specifyNamespaces ? fullName() : partialCppName())
481 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700482
483 switch (mode) {
484 case StorageMode_Stack:
485 case StorageMode_Result:
486 return base;
487
488 case StorageMode_Argument:
489 return "const " + base + "&";
490 }
491}
492
Yifan Hong4ed13472016-11-02 10:44:11 -0700493std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700494 return fullJavaName();
495}
496
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800497std::string Interface::getVtsType() const {
498 if (StringHelper::EndsWith(localName(), "Callback")) {
499 return "TYPE_HIDL_CALLBACK";
500 } else {
501 return "TYPE_HIDL_INTERFACE";
502 }
503}
504
Andreas Huber881227d2016-08-02 14:20:21 -0700505void Interface::emitReaderWriter(
506 Formatter &out,
507 const std::string &name,
508 const std::string &parcelObj,
509 bool parcelObjIsPointer,
510 bool isReader,
511 ErrorMode mode) const {
512 const std::string parcelObjDeref =
513 parcelObj + (parcelObjIsPointer ? "->" : ".");
514
515 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700516 out << "{\n";
517 out.indent();
518
Iliyan Malchev549e2592016-08-10 08:59:12 -0700519 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700520
Andreas Huber8a82ff72016-08-04 10:29:39 -0700521 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700522 << binderName << ";\n";
523
Iliyan Malchev549e2592016-08-10 08:59:12 -0700524 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700525 out << parcelObjDeref
526 << "readNullableStrongBinder(&"
527 << binderName
528 << ");\n";
529
530 handleError(out, mode);
531
532 out << name
533 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100534 << "::android::hardware::fromBinder<"
535 << fqName().cppName()
536 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800537 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100538 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800539 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100540 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700541 << binderName
542 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700543
544 out.unindent();
545 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700546 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200547 out << "if (" << name << " == nullptr) {\n";
548 out.indent();
549 out << "_hidl_err = ";
550 out << parcelObjDeref
551 << "writeStrongBinder(nullptr);\n";
552 out.unindent();
553 out << "} else {\n";
554 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800555 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
556 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800557 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100558 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800559 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800560 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800561 << ">("
562 << name
563 << ");\n";
564 });
565 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800566 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800567 out << "_hidl_err = "
568 << parcelObjDeref
569 << "writeStrongBinder(_hidl_binder);\n";
570 });
571 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800572 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800573 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
574 });
575 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200576 out.unindent();
577 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700578
Andreas Huber881227d2016-08-02 14:20:21 -0700579 handleError(out, mode);
580 }
581}
582
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800583status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
584 status_t status = Scope::emitGlobalTypeDeclarations(out);
585 if (status != OK) {
586 return status;
587 }
588 out << "std::string toString("
589 << getCppArgumentType()
590 << ");\n";
591 return OK;
592}
593
594
595status_t Interface::emitTypeDefinitions(
596 Formatter &out, const std::string prefix) const {
597 std::string space = prefix.empty() ? "" : (prefix + "::");
598 status_t err = Scope::emitTypeDefinitions(out, space + localName());
599 if (err != OK) {
600 return err;
601 }
602
603 out << "std::string toString("
604 << getCppArgumentType()
605 << " o) ";
606
607 out.block([&] {
608 out << "std::string os;\nbool ok = false;\n";
609 // TODO b/34136228 use interfaceDescriptor instead
610 out << "auto ret = o->interfaceChain([&os, &ok] (const auto &chain) ";
611 out.block([&] {
612 out.sIf("chain.size() >= 1", [&] {
613 out << "os += chain[0].c_str();\n"
614 << "ok = true;\n";
615 }).endl();
616 });
617 out << ");\n";
618 out.sIf("!ret.isOk() || !ok", [&] {
619 out << "os += \"[class or subclass of \";\n"
620 << "os += " << fullName() << "::descriptor;\n"
621 << "os += \"]\";\n";
622 }).endl();
623 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
624 << "return os;\n";
625 }).endl().endl();
626
627 return OK;
628}
629
Andreas Huber2831d512016-08-15 09:33:47 -0700630void Interface::emitJavaReaderWriter(
631 Formatter &out,
632 const std::string &parcelObj,
633 const std::string &argName,
634 bool isReader) const {
635 if (isReader) {
636 out << fullJavaName()
637 << ".asInterface("
638 << parcelObj
639 << ".readStrongBinder());\n";
640 } else {
641 out << parcelObj
642 << ".writeStrongBinder("
643 << argName
644 << " == null ? null : "
645 << argName
646 << ".asBinder());\n";
647 }
648}
649
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700650status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
651 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700652 // Skip for TypeDef as it is just an alias of a defined type.
653 if (type->isTypeDef()) {
654 continue;
655 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700656 out << "attribute: {\n";
657 out.indent();
658 status_t status = type->emitVtsTypeDeclarations(out);
659 if (status != OK) {
660 return status;
661 }
662 out.unindent();
663 out << "}\n\n";
664 }
665 return OK;
666}
667
668status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700669 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800670 if (method->isHidlReserved()) {
671 continue;
672 }
673
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700674 out << "api: {\n";
675 out.indent();
676 out << "name: \"" << method->name() << "\"\n";
677 // Generate declaration for each return value.
678 for (const auto &result : method->results()) {
679 out << "return_type_hidl: {\n";
680 out.indent();
681 status_t status = result->type().emitVtsAttributeType(out);
682 if (status != OK) {
683 return status;
684 }
685 out.unindent();
686 out << "}\n";
687 }
688 // Generate declaration for each input argument
689 for (const auto &arg : method->args()) {
690 out << "arg: {\n";
691 out.indent();
692 status_t status = arg->type().emitVtsAttributeType(out);
693 if (status != OK) {
694 return status;
695 }
696 out.unindent();
697 out << "}\n";
698 }
699 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700700 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700701 out << "callflow: {\n";
702 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700703 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700704 if (name == "entry") {
705 out << "entry: true\n";
706 } else if (name == "exit") {
707 out << "exit: true\n";
708 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700709 const AnnotationParam *param =
710 annotation->getParam("next");
711 if (param != nullptr) {
712 for (auto value : *param->getValues()) {
713 out << "next: " << value << "\n";
714 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700715 }
716 } else {
717 std::cerr << "Invalid annotation '"
718 << name << "' for method: " << method->name()
719 << ". Should be one of: entry, exit, callflow. \n";
720 return UNKNOWN_ERROR;
721 }
722 out.unindent();
723 out << "}\n";
724 }
725 out.unindent();
726 out << "}\n\n";
727 }
728 return OK;
729}
730
731status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800732 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700733 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800734 << fullName()
735 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700736 return OK;
737}
738
Steven Moreland69e7c702016-09-09 11:16:32 -0700739bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700740 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700741 if (method->isOneway()) {
742 return true;
743 }
744 }
745
746 const Interface* superClass = superType();
747
748 if (superClass != nullptr) {
749 return superClass->hasOnewayMethods();
750 }
751
752 return false;
753}
754
Andreas Huber70a59e12016-08-16 12:57:01 -0700755bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700756 if (mIsJavaCompatibleInProgress) {
757 // We're currently trying to determine if this Interface is
758 // java-compatible and something is referencing this interface through
759 // one of its methods. Assume we'll ultimately succeed, if we were wrong
760 // the original invocation of Interface::isJavaCompatible() will then
761 // return the correct "false" result.
762 return true;
763 }
764
Andreas Huber0fa9e392016-08-31 09:05:44 -0700765 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
766 mIsJavaCompatibleInProgress = false;
767 return false;
768 }
769
Andreas Huberea081b32016-08-17 15:57:47 -0700770 mIsJavaCompatibleInProgress = true;
771
Andreas Huber70a59e12016-08-16 12:57:01 -0700772 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700773 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700774 return false;
775 }
776
Yifan Hong10fe0b52016-10-19 14:20:17 -0700777 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700778 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700779 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700780 return false;
781 }
782 }
783
Andreas Huberea081b32016-08-17 15:57:47 -0700784 mIsJavaCompatibleInProgress = false;
785
Andreas Huber70a59e12016-08-16 12:57:01 -0700786 return true;
787}
788
Andreas Huberc9410c72016-07-28 12:18:40 -0700789} // namespace android
790