blob: ed87b5daae3faf837daf38a70f06651073791f03 [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"
Yifan Hong30b5d1f2017-04-03 12:19:25 -070020#include "ArrayType.h"
21#include "ConstantExpression.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010022#include "DeathRecipientType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023#include "Method.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010024#include "ScalarType.h"
Yifan Hong10fe0b52016-10-19 14:20:17 -070025#include "StringType.h"
26#include "VectorType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Yifan Hong30b5d1f2017-04-03 12:19:25 -070028#include <unistd.h>
29
Yifan Hong30b5d1f2017-04-03 12:19:25 -070030#include <iostream>
Timur Iskhakov7296af12017-08-09 21:52:48 +000031#include <memory>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070032#include <sstream>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070033#include <unordered_map>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070034
Steven Moreland14ee6742016-10-18 12:58:28 -070035#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070036#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070037#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070038
Andreas Huberc9410c72016-07-28 12:18:40 -070039namespace android {
40
Steven Morelandb8e15a52017-02-13 19:35:40 -080041#define B_PACK_CHARS(c1, c2, c3, c4) \
42 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
43
Yifan Hong10fe0b52016-10-19 14:20:17 -070044/* It is very important that these values NEVER change. These values
45 * must remain unchanged over the lifetime of android. This is
46 * because the framework on a device will be updated independently of
47 * the hals on a device. If the hals are compiled with one set of
48 * transaction values, and the framework with another, then the
49 * interface between them will be destroyed, and the device will not
50 * work.
51 */
52enum {
Yifan Hong10fe0b52016-10-19 14:20:17 -070053 /////////////////// User defined transactions
54 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080055 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070056 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080057 FIRST_HIDL_TRANSACTION = 0x0f000000,
58 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
59 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
60 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
61 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
62 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
63 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
64 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
65 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
66 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
Yifan Hong30b5d1f2017-04-03 12:19:25 -070067 HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'),
Steven Morelandb8e15a52017-02-13 19:35:40 -080068 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070069};
70
Timur Iskhakov565b0132017-09-06 18:07:11 -070071Interface::Interface(const char* localName, const FQName& fullName, const Location& location,
Steven Moreland04dea8d2018-02-06 13:11:24 -080072 Scope* parent, const Reference<Type>& superType, const Hash* fileHash)
73 : Scope(localName, fullName, location, parent), mSuperType(superType), mFileHash(fileHash) {}
Martijn Coenenaf712c02016-11-16 15:26:27 +010074
Steven Moreland30bb6a82016-11-30 09:18:34 -080075std::string Interface::typeName() const {
76 return "interface " + localName();
77}
78
Steven Moreland04dea8d2018-02-06 13:11:24 -080079const Hash* Interface::getFileHash() const {
80 return mFileHash;
81}
82
Steven Moreland424a9482017-02-13 19:20:40 -080083bool Interface::fillPingMethod(Method *method) const {
84 if (method->name() != "ping") {
85 return false;
86 }
87
88 method->fillImplementation(
89 HIDL_PING_TRANSACTION,
90 {
Steven Moreland937408a2017-03-20 09:54:18 -070091 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080092 [](auto &out) {
93 out << "return ::android::hardware::Void();\n";
94 }
95 },
96 {IMPL_STUB_IMPL,
97 [](auto &out) {
98 out << "return ::android::hardware::Void();\n";
99 }
100 }
101 }, /*cppImpl*/
102 {
Steven Moreland937408a2017-03-20 09:54:18 -0700103 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700104 [](auto &out) {
Steven Moreland424a9482017-02-13 19:20:40 -0800105 out << "return;\n";
106 }
107 },
108 {IMPL_STUB, nullptr /* don't generate code */}
109 } /*javaImpl*/
110 );
111
112 return true;
113}
114
Yifan Hongffa91392017-01-31 13:41:23 -0800115bool Interface::fillLinkToDeathMethod(Method *method) const {
116 if (method->name() != "linkToDeath") {
117 return false;
118 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100119
Yifan Hongffa91392017-01-31 13:41:23 -0800120 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100121 HIDL_LINK_TO_DEATH_TRANSACTION,
122 {
Steven Moreland937408a2017-03-20 09:54:18 -0700123 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100124 [](auto &out) {
125 out << "(void)cookie;\n"
126 << "return (recipient != nullptr);\n";
127 }
128 },
129 {IMPL_PROXY,
130 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100131 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100132 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
133 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
134 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
135 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
136 << "return (remote()->linkToDeath(binder_recipient)"
137 << " == ::android::OK);\n";
138 }
139 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100140 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100141 }, /*cppImpl*/
142 {
Steven Moreland937408a2017-03-20 09:54:18 -0700143 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700144 [](auto &out) {
Martijn Coenen115d4282016-12-19 05:14:04 +0100145 out << "return true;";
146 }
147 },
148 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700149 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100150 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100151 }
152 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100153 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100154 } /*javaImpl*/
155 );
Yifan Hongffa91392017-01-31 13:41:23 -0800156 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100157}
158
Yifan Hongffa91392017-01-31 13:41:23 -0800159bool Interface::fillUnlinkToDeathMethod(Method *method) const {
160 if (method->name() != "unlinkToDeath") {
161 return false;
162 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100163
Yifan Hongffa91392017-01-31 13:41:23 -0800164 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100165 HIDL_UNLINK_TO_DEATH_TRANSACTION,
166 {
Steven Moreland937408a2017-03-20 09:54:18 -0700167 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100168 [](auto &out) {
169 out << "return (recipient != nullptr);\n";
170 }
171 },
172 {IMPL_PROXY,
173 [](auto &out) {
174 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
175 << "for (auto it = _hidl_mDeathRecipients.begin();"
176 << "it != _hidl_mDeathRecipients.end();"
177 << "++it) {\n";
178 out.indent([&] {
179 out.sIf("(*it)->getRecipient() == recipient", [&] {
180 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
181 << "_hidl_mDeathRecipients.erase(it);\n"
182 << "return status == ::android::OK;\n";
183 });
184 });
185 out << "}\n";
186 out << "return false;\n";
187 }
188 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100189 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100190 }, /*cppImpl*/
191 {
Steven Moreland937408a2017-03-20 09:54:18 -0700192 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700193 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100194 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100195 }
196 },
197 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700198 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100199 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100200 }
201 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100202 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100203 } /*javaImpl*/
204 );
Yifan Hongffa91392017-01-31 13:41:23 -0800205 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100206}
Yifan Hongffa91392017-01-31 13:41:23 -0800207bool Interface::fillSyspropsChangedMethod(Method *method) const {
208 if (method->name() != "notifySyspropsChanged") {
209 return false;
210 }
211
212 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100213 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yi Kongc8ff4672017-04-30 23:46:56 -0700214 { { IMPL_INTERFACE, [](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100215 out << "::android::report_sysprop_change();\n";
216 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100217 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700218 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Steven Moreland1bdb18c2018-01-25 15:44:39 -0800219 out << "android.os.HwBinder.enableInstrumentation();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100220 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100221 );
Yifan Hongffa91392017-01-31 13:41:23 -0800222 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700223}
224
Yifan Hongffa91392017-01-31 13:41:23 -0800225bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
226 if (method->name() != "setHALInstrumentation") {
227 return false;
228 }
229
230 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800231 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
232 {
Steven Moreland937408a2017-03-20 09:54:18 -0700233 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700234 [](auto &out) {
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800235 // do nothing for base class.
236 out << "return ::android::hardware::Void();\n";
237 }
238 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800239 {IMPL_STUB,
240 [](auto &out) {
241 out << "configureInstrumentation();\n";
242 }
243 },
244 {IMPL_PASSTHROUGH,
245 [](auto &out) {
246 out << "configureInstrumentation();\n";
247 out << "return ::android::hardware::Void();\n";
248 }
249 },
250 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700251 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800252 // Not support for Java Impl for now.
253 } } } /*javaImpl */
254 );
Yifan Hongffa91392017-01-31 13:41:23 -0800255 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800256}
257
Yifan Hongffa91392017-01-31 13:41:23 -0800258bool Interface::fillDescriptorChainMethod(Method *method) const {
259 if (method->name() != "interfaceChain") {
260 return false;
261 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700262
Yifan Hongffa91392017-01-31 13:41:23 -0800263 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700264 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700265 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700266 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800267 out << "_hidl_cb(";
268 out.block([&] {
269 for (const Interface *iface : chain) {
270 out << iface->fullName() << "::descriptor,\n";
271 }
272 });
273 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700274 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100275 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700276 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700277 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800278 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700279 out.indent(); out.indent();
280 for (size_t i = 0; i < chain.size(); ++i) {
281 if (i != 0)
282 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800283 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700284 }
285 out << "));";
286 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800287 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100288 );
Yifan Hongffa91392017-01-31 13:41:23 -0800289 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700290}
291
Steven Moreland04dea8d2018-02-06 13:11:24 -0800292void Interface::emitDigestChain(
Timur Iskhakov7296af12017-08-09 21:52:48 +0000293 Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
Steven Moreland04dea8d2018-02-06 13:11:24 -0800294 std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const {
295 out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700296 out << prefix;
297 out << "{";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800298 out.join(
299 iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",",
300 [&](const auto& e) {
301 // Use ConstantExpression::cppValue / javaValue
302 // because Java used signed byte for uint8_t.
303 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
304 });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700305 out << "} /* ";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800306 out << iface->getFileHash()->hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700307 out << " */";
308 });
309}
310
311bool Interface::fillHashChainMethod(Method *method) const {
312 if (method->name() != "getHashChain") {
313 return false;
314 }
315 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
316 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
317
318 method->fillImplementation(
319 HIDL_HASH_CHAIN_TRANSACTION,
320 { { IMPL_INTERFACE, [this, digestType](auto &out) {
321 std::vector<const Interface *> chain = typeChain();
322 out << "_hidl_cb(";
323 out.block([&] {
Timur Iskhakov7296af12017-08-09 21:52:48 +0000324 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain,
325 [](const auto& e) { return e->cppValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700326 });
327 out << ");\n";
328 out << "return ::android::hardware::Void();\n";
329 } } }, /* cppImpl */
330 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
331 std::vector<const Interface *> chain = typeChain();
332 out << "return new "
333 << chainType->getJavaType(false /* forInitializer */)
334 << "(java.util.Arrays.asList(\n";
335 out.indent(2, [&] {
336 // No need for dimensions when elements are explicitly provided.
337 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
Timur Iskhakov7296af12017-08-09 21:52:48 +0000338 chain, [](const auto& e) { return e->javaValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700339 });
340 out << "));\n";
341 } } } /* javaImpl */
342 );
343 return true;
344}
345
Yifan Hongffa91392017-01-31 13:41:23 -0800346bool Interface::fillGetDescriptorMethod(Method *method) const {
347 if (method->name() != "interfaceDescriptor") {
348 return false;
349 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800350
Yifan Hongffa91392017-01-31 13:41:23 -0800351 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800352 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700353 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800354 out << "_hidl_cb("
355 << fullName()
356 << "::descriptor);\n"
357 << "return ::android::hardware::Void();";
358 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700359 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800360 out << "return "
361 << fullJavaName()
362 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800363 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800364 );
Yifan Hongffa91392017-01-31 13:41:23 -0800365 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800366}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700367
Yifan Hongbcffce22017-02-01 15:52:06 -0800368bool Interface::fillGetDebugInfoMethod(Method *method) const {
369 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800370 return false;
371 }
372
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800373 static const std::string sArch =
374 "#if defined(__LP64__)\n"
375 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
376 "#else\n"
377 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
378 "#endif\n";
379
Yifan Hongcd2ae452017-01-31 14:33:40 -0800380 method->fillImplementation(
381 HIDL_GET_REF_INFO_TRANSACTION,
382 {
Steven Moreland937408a2017-03-20 09:54:18 -0700383 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700384 [](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800385 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800386 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
387 << sArch
388 << "});\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800389 << "return ::android::hardware::Void();";
390 }
391 },
392 {IMPL_STUB_IMPL,
Yi Kongc8ff4672017-04-30 23:46:56 -0700393 [](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800394 out << "_hidl_cb(";
395 out.block([&] {
Yifan Hong96bb0632017-11-14 16:08:37 -0800396 out << "::android::hardware::details::getPidIfSharable(),\n"
Yifan Hong2e036c92017-03-07 13:18:12 -0800397 << "::android::hardware::details::debuggable()"
398 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
399 << sArch << "\n";
400 });
401 out << ");\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800402 << "return ::android::hardware::Void();";
403 }
404 }
405 }, /* cppImpl */
Yi Kongc8ff4672017-04-30 23:46:56 -0700406 { { IMPL_INTERFACE, [method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800407 const Type &refInfo = method->results().front()->type();
408 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
409 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800410 << "info.pid = android.os.HidlSupport.getPidIfSharable();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800411 << "info.ptr = 0;\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800412 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800413 << "return info;";
414 } } } /* javaImpl */
415 );
416
417 return true;
418}
419
Andreas Huber37065d62017-02-07 14:36:54 -0800420bool Interface::fillDebugMethod(Method *method) const {
421 if (method->name() != "debug") {
422 return false;
423 }
424
425 method->fillImplementation(
426 HIDL_DEBUG_TRANSACTION,
427 {
Steven Moreland937408a2017-03-20 09:54:18 -0700428 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700429 [](auto &out) {
Andreas Huber37065d62017-02-07 14:36:54 -0800430 out << "(void)fd;\n"
431 << "(void)options;\n"
432 << "return ::android::hardware::Void();";
433 }
434 },
435 }, /* cppImpl */
436 {
437 /* unused, as the debug method is hidden from Java */
438 } /* javaImpl */
439 );
440
441 return true;
442}
443
Yifan Hongffa91392017-01-31 13:41:23 -0800444static std::map<std::string, Method *> gAllReservedMethods;
445
Steven Moreland14ee6742016-10-18 12:58:28 -0700446bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800447 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800448 if (!gAllReservedMethods.emplace(method->name(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700449 std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name()
450 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800451 return false;
452 }
453 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800454 return true;
455 }
456
Yifan Hong10fe0b52016-10-19 14:20:17 -0700457 CHECK(!method->isHidlReserved());
Yifan Hong10fe0b52016-10-19 14:20:17 -0700458 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700459
460 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700461}
462
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700463std::vector<const Reference<Type>*> Interface::getReferences() const {
464 std::vector<const Reference<Type>*> ret;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700465
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700466 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700467 ret.push_back(&mSuperType);
Timur Iskhakov33431e62017-08-21 17:31:23 -0700468 }
469
470 for (const auto* method : methods()) {
471 const auto& references = method->getReferences();
472 ret.insert(ret.end(), references.begin(), references.end());
473 }
474
475 return ret;
476}
477
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700478std::vector<const ConstantExpression*> Interface::getConstantExpressions() const {
479 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700480 for (const auto* method : methods()) {
481 const auto& retMethod = method->getConstantExpressions();
482 ret.insert(ret.end(), retMethod.begin(), retMethod.end());
483 }
484 return ret;
485}
486
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700487std::vector<const Reference<Type>*> Interface::getStrongReferences() const {
Timur Iskhakov40731af2017-08-24 14:18:35 -0700488 // Interface is a special case as a reference:
489 // its definiton must be completed for extension but
490 // not necessary for other references.
Timur Iskhakov40731af2017-08-24 14:18:35 -0700491
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700492 std::vector<const Reference<Type>*> ret;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700493 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700494 ret.push_back(&mSuperType);
Timur Iskhakov40731af2017-08-24 14:18:35 -0700495 }
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700496
497 for (const auto* method : methods()) {
498 const auto& references = method->getStrongReferences();
499 ret.insert(ret.end(), references.begin(), references.end());
500 }
501
Timur Iskhakov40731af2017-08-24 14:18:35 -0700502 return ret;
503}
504
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700505status_t Interface::resolveInheritance() {
506 size_t serial = FIRST_CALL_TRANSACTION;
507 for (const auto* ancestor : superTypeChain()) {
508 serial += ancestor->mUserMethods.size();
509 }
510
511 for (Method* method : mUserMethods) {
512 if (serial > LAST_CALL_TRANSACTION) {
513 std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION
514 << " methods (including super and reserved) are not allowed at " << location()
Steven Morelandcbff5612017-10-11 17:01:54 -0700515 << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700516 return UNKNOWN_ERROR;
517 }
518
519 method->setSerialId(serial);
520 serial++;
521 }
522
523 return Scope::resolveInheritance();
524}
525
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700526status_t Interface::validate() const {
527 CHECK(isIBase() == mSuperType.isEmptyReference());
528
Timur Iskhakov0344e612017-08-25 12:57:09 -0700529 if (!isIBase() && !mSuperType->isInterface()) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700530 std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location()
531 << std::endl;
Timur Iskhakov0344e612017-08-25 12:57:09 -0700532 return UNKNOWN_ERROR;
533 }
534
Steven Moreland368e4602018-02-16 14:21:49 -0800535 status_t err;
536
537 err = validateUniqueNames();
538 if (err != OK) return err;
539
540 err = validateAnnotations();
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700541 if (err != OK) return err;
542
543 return Scope::validate();
544}
545
Howard Chenecfb4512017-11-21 18:28:53 +0800546void Interface::getAlignmentAndSize(size_t* align, size_t* size) const {
547 *align = 8;
548 *size = 8;
549}
550
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700551status_t Interface::validateUniqueNames() const {
552 std::unordered_map<std::string, const Interface*> registeredMethodNames;
553 for (auto const& tuple : allSuperMethodsFromRoot()) {
554 // No need to check super method uniqueness
555 registeredMethodNames[tuple.method()->name()] = tuple.interface();
556 }
557
558 for (const Method* method : mUserMethods) {
559 auto registered = registeredMethodNames.find(method->name());
560
561 if (registered != registeredMethodNames.end()) {
562 const Interface* definedInType = registered->second;
563
564 if (definedInType == this) {
565 // Defined in this interface
566 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
567 } else if (definedInType->isIBase()) {
568 // Defined in IBase
569 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
570 } else {
571 // Defined in super not IBase
572 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700573 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700574 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700575 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700576 return UNKNOWN_ERROR;
577 }
578
579 registeredMethodNames[method->name()] = this;
580 }
581
582 return OK;
583}
584
Steven Moreland368e4602018-02-16 14:21:49 -0800585status_t Interface::validateAnnotations() const {
586 for (const Method* method : methods()) {
587 for (const Annotation* annotation : method->annotations()) {
588 const std::string name = annotation->name();
589
590 if (name == "entry" || name == "exit" || name == "callflow") {
591 continue;
592 }
593
594 std::cerr << "ERROR: Unrecognized annotation '" << name
595 << "' for method: " << method->name() << ". An annotation should be one of: "
596 << "entry, exit, callflow." << std::endl;
597 return UNKNOWN_ERROR;
598 }
599 }
600 return OK;
601}
602
Yifan Hongffa91392017-01-31 13:41:23 -0800603bool Interface::addAllReservedMethods() {
604 // use a sorted map to insert them in serial ID order.
605 std::map<int32_t, Method *> reservedMethodsById;
606 for (const auto &pair : gAllReservedMethods) {
607 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800608 bool fillSuccess = fillPingMethod(method)
609 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800610 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700611 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800612 || fillSyspropsChangedMethod(method)
613 || fillLinkToDeathMethod(method)
614 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800615 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800616 || fillGetDebugInfoMethod(method)
617 || fillDebugMethod(method);
618
Yifan Hongffa91392017-01-31 13:41:23 -0800619 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700620 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
621 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800622 return false;
623 }
624 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700625 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
626 << " and " << reservedMethodsById[method->getSerialId()]->name()
627 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800628 return false;
629 }
630 }
631 for (const auto &pair : reservedMethodsById) {
632 this->mReservedMethods.push_back(pair.second);
633 }
634 return true;
635}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700636
Timur Iskhakov505316c2017-08-05 03:38:59 +0000637const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700638 if (isIBase()) return nullptr;
639 if (!mSuperType->isInterface()) {
640 // This is actually an error
641 // that would be caught in validate
642 return nullptr;
643 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700644 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700645}
646
Yifan Hong10fe0b52016-10-19 14:20:17 -0700647std::vector<const Interface *> Interface::typeChain() const {
648 std::vector<const Interface *> v;
649 const Interface *iface = this;
650 while (iface != nullptr) {
651 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000652 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700653 }
654 return v;
655}
656
Yifan Hongfe95aa22016-10-19 17:26:45 -0700657std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000658 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700659}
660
Martijn Coenenb40ef022017-01-02 15:21:46 +0100661bool Interface::isElidableType() const {
662 return true;
663}
664
Andreas Hubera2723d22016-07-29 15:36:07 -0700665bool Interface::isInterface() const {
666 return true;
667}
668
Andreas Huber295ad302016-08-16 11:35:00 -0700669bool Interface::isBinder() const {
670 return true;
671}
672
Yifan Hong10fe0b52016-10-19 14:20:17 -0700673const std::vector<Method *> &Interface::userDefinedMethods() const {
674 return mUserMethods;
675}
676
677const std::vector<Method *> &Interface::hidlReservedMethods() const {
678 return mReservedMethods;
679}
680
681std::vector<Method *> Interface::methods() const {
682 std::vector<Method *> v(mUserMethods);
683 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
684 return v;
685}
686
687std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
688 std::vector<InterfaceAndMethod> v;
689 std::vector<const Interface *> chain = typeChain();
690 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
691 const Interface *iface = *it;
692 for (Method *userMethod : iface->userDefinedMethods()) {
693 v.push_back(InterfaceAndMethod(iface, userMethod));
694 }
695 }
696 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800697 v.push_back(InterfaceAndMethod(
698 *chain.rbegin(), // IBase
699 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700700 }
701 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700702}
703
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700704std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
705 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
706}
707
Steven Moreland40786312016-08-16 10:29:40 -0700708std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700709 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700710}
711
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700712std::string Interface::getAdapterName() const {
713 return fqName().getInterfaceAdapterName();
714}
715
Yifan Hongeefe4f22017-01-04 15:32:42 -0800716std::string Interface::getProxyName() const {
717 return fqName().getInterfaceProxyName();
718}
719
720std::string Interface::getStubName() const {
721 return fqName().getInterfaceStubName();
722}
723
724std::string Interface::getHwName() const {
725 return fqName().getInterfaceHwName();
726}
727
728std::string Interface::getPassthroughName() const {
729 return fqName().getInterfacePassthroughName();
730}
731
Yifan Hong51a65092017-01-04 15:41:44 -0800732FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800733 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800734}
735
Yifan Hong51a65092017-01-04 15:41:44 -0800736FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800737 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800738}
739
Yifan Hong51a65092017-01-04 15:41:44 -0800740FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800741 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800742}
743
Steven Moreland979e0992016-09-07 09:18:08 -0700744std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700745 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700746 const std::string base =
747 std::string(specifyNamespaces ? "::android::" : "")
748 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700749 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700750 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700751
752 switch (mode) {
753 case StorageMode_Stack:
754 case StorageMode_Result:
755 return base;
756
757 case StorageMode_Argument:
758 return "const " + base + "&";
759 }
760}
761
Yifan Hong4ed13472016-11-02 10:44:11 -0700762std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700763 return fullJavaName();
764}
765
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800766std::string Interface::getVtsType() const {
767 if (StringHelper::EndsWith(localName(), "Callback")) {
768 return "TYPE_HIDL_CALLBACK";
769 } else {
770 return "TYPE_HIDL_INTERFACE";
771 }
772}
773
Andreas Huber881227d2016-08-02 14:20:21 -0700774void Interface::emitReaderWriter(
775 Formatter &out,
776 const std::string &name,
777 const std::string &parcelObj,
778 bool parcelObjIsPointer,
779 bool isReader,
780 ErrorMode mode) const {
781 const std::string parcelObjDeref =
782 parcelObj + (parcelObjIsPointer ? "->" : ".");
783
784 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700785 out << "{\n";
786 out.indent();
787
Howard Chenecfb4512017-11-21 18:28:53 +0800788 const std::string binderName = "_hidl_binder";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700789 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700790 << binderName << ";\n";
791
Iliyan Malchev549e2592016-08-10 08:59:12 -0700792 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700793 out << parcelObjDeref
794 << "readNullableStrongBinder(&"
795 << binderName
796 << ");\n";
797
798 handleError(out, mode);
799
800 out << name
801 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100802 << "::android::hardware::fromBinder<"
803 << fqName().cppName()
804 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800805 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100806 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800807 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100808 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700809 << binderName
810 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700811
812 out.unindent();
813 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700814 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200815 out << "if (" << name << " == nullptr) {\n";
816 out.indent();
817 out << "_hidl_err = ";
818 out << parcelObjDeref
819 << "writeStrongBinder(nullptr);\n";
820 out.unindent();
821 out << "} else {\n";
822 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800823 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
824 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800825 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100826 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800827 << ">("
828 << name
829 << ");\n";
830 });
831 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800832 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800833 out << "_hidl_err = "
834 << parcelObjDeref
835 << "writeStrongBinder(_hidl_binder);\n";
836 });
837 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800838 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800839 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
840 });
841 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200842 out.unindent();
843 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700844
Andreas Huber881227d2016-08-02 14:20:21 -0700845 handleError(out, mode);
846 }
847}
848
Steven Moreland368e4602018-02-16 14:21:49 -0800849void Interface::emitPackageTypeDeclarations(Formatter& out) const {
850 Scope::emitPackageTypeDeclarations(out);
Steven Morelandbf714212017-10-27 18:29:01 -0700851
Steven Morelandbf714212017-10-27 18:29:01 -0700852 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
853
854 out.block([&] {
855 out << "std::string os = \"[class or subclass of \";\n"
856 << "os += " << fullName() << "::descriptor;\n"
857 << "os += \"]\";\n"
858 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
859 << "return os;\n";
860 }).endl().endl();
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800861}
862
Steven Moreland368e4602018-02-16 14:21:49 -0800863void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800864 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Morelandd8c7a292017-10-27 18:32:06 -0700865
Steven Moreland368e4602018-02-16 14:21:49 -0800866 Scope::emitTypeDefinitions(out, space + localName());
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800867}
868
Andreas Huber2831d512016-08-15 09:33:47 -0700869void Interface::emitJavaReaderWriter(
870 Formatter &out,
871 const std::string &parcelObj,
872 const std::string &argName,
873 bool isReader) const {
874 if (isReader) {
875 out << fullJavaName()
876 << ".asInterface("
877 << parcelObj
878 << ".readStrongBinder());\n";
879 } else {
880 out << parcelObj
881 << ".writeStrongBinder("
882 << argName
883 << " == null ? null : "
884 << argName
885 << ".asBinder());\n";
886 }
887}
888
Steven Moreland368e4602018-02-16 14:21:49 -0800889void Interface::emitVtsAttributeDeclaration(Formatter& out) const {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700890 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700891 // Skip for TypeDef as it is just an alias of a defined type.
892 if (type->isTypeDef()) {
893 continue;
894 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700895 out << "attribute: {\n";
896 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800897 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700898 out.unindent();
899 out << "}\n\n";
900 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700901}
902
Steven Moreland368e4602018-02-16 14:21:49 -0800903void Interface::emitVtsMethodDeclaration(Formatter& out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700904 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800905 if (method->isHidlReserved()) {
906 continue;
907 }
908
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700909 out << "api: {\n";
910 out.indent();
911 out << "name: \"" << method->name() << "\"\n";
912 // Generate declaration for each return value.
913 for (const auto &result : method->results()) {
914 out << "return_type_hidl: {\n";
915 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800916 result->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700917 out.unindent();
918 out << "}\n";
919 }
920 // Generate declaration for each input argument
921 for (const auto &arg : method->args()) {
922 out << "arg: {\n";
923 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800924 arg->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700925 out.unindent();
926 out << "}\n";
927 }
928 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700929 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700930 out << "callflow: {\n";
931 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800932 const std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700933 if (name == "entry") {
934 out << "entry: true\n";
935 } else if (name == "exit") {
936 out << "exit: true\n";
937 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700938 const AnnotationParam *param =
939 annotation->getParam("next");
940 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700941 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700942 out << "next: " << value << "\n";
943 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700944 }
945 } else {
Steven Moreland368e4602018-02-16 14:21:49 -0800946 CHECK(false);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700947 }
948 out.unindent();
949 out << "}\n";
950 }
951 out.unindent();
952 out << "}\n\n";
953 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700954}
955
Steven Moreland368e4602018-02-16 14:21:49 -0800956void Interface::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800957 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700958 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800959 << fullName()
960 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700961}
962
Steven Moreland69e7c702016-09-09 11:16:32 -0700963bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700964 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700965 if (method->isOneway()) {
966 return true;
967 }
968 }
969
970 const Interface* superClass = superType();
971
972 if (superClass != nullptr) {
973 return superClass->hasOnewayMethods();
974 }
975
976 return false;
977}
978
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700979bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
980 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700981 return false;
982 }
983
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700984 for (const auto* method : methods()) {
985 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700986 return false;
987 }
988 }
989
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700990 return Scope::isJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -0700991}
992
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700993bool Interface::isNeverStrongReference() const {
994 return true;
995}
996
Andreas Huberc9410c72016-07-28 12:18:40 -0700997} // namespace android
998