blob: 7d2c83c858892b20f9535b7670aeb3dce7b871a8 [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 */
Sundong Ahnc1432c92017-07-13 23:58:27 +0900219 out << "android.os.HwBinder.reportSyspropChanged();";
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
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700535 status_t err = validateUniqueNames();
536 if (err != OK) return err;
537
538 return Scope::validate();
539}
540
Howard Chenecfb4512017-11-21 18:28:53 +0800541void Interface::getAlignmentAndSize(size_t* align, size_t* size) const {
542 *align = 8;
543 *size = 8;
544}
545
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700546status_t Interface::validateUniqueNames() const {
547 std::unordered_map<std::string, const Interface*> registeredMethodNames;
548 for (auto const& tuple : allSuperMethodsFromRoot()) {
549 // No need to check super method uniqueness
550 registeredMethodNames[tuple.method()->name()] = tuple.interface();
551 }
552
553 for (const Method* method : mUserMethods) {
554 auto registered = registeredMethodNames.find(method->name());
555
556 if (registered != registeredMethodNames.end()) {
557 const Interface* definedInType = registered->second;
558
559 if (definedInType == this) {
560 // Defined in this interface
561 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
562 } else if (definedInType->isIBase()) {
563 // Defined in IBase
564 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
565 } else {
566 // Defined in super not IBase
567 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700568 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700569 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700570 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700571 return UNKNOWN_ERROR;
572 }
573
574 registeredMethodNames[method->name()] = this;
575 }
576
577 return OK;
578}
579
Yifan Hongffa91392017-01-31 13:41:23 -0800580bool Interface::addAllReservedMethods() {
581 // use a sorted map to insert them in serial ID order.
582 std::map<int32_t, Method *> reservedMethodsById;
583 for (const auto &pair : gAllReservedMethods) {
584 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800585 bool fillSuccess = fillPingMethod(method)
586 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800587 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700588 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800589 || fillSyspropsChangedMethod(method)
590 || fillLinkToDeathMethod(method)
591 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800592 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800593 || fillGetDebugInfoMethod(method)
594 || fillDebugMethod(method);
595
Yifan Hongffa91392017-01-31 13:41:23 -0800596 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700597 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
598 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800599 return false;
600 }
601 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700602 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
603 << " and " << reservedMethodsById[method->getSerialId()]->name()
604 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800605 return false;
606 }
607 }
608 for (const auto &pair : reservedMethodsById) {
609 this->mReservedMethods.push_back(pair.second);
610 }
611 return true;
612}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700613
Timur Iskhakov505316c2017-08-05 03:38:59 +0000614const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700615 if (isIBase()) return nullptr;
616 if (!mSuperType->isInterface()) {
617 // This is actually an error
618 // that would be caught in validate
619 return nullptr;
620 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700621 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700622}
623
Yifan Hong10fe0b52016-10-19 14:20:17 -0700624std::vector<const Interface *> Interface::typeChain() const {
625 std::vector<const Interface *> v;
626 const Interface *iface = this;
627 while (iface != nullptr) {
628 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000629 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700630 }
631 return v;
632}
633
Yifan Hongfe95aa22016-10-19 17:26:45 -0700634std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000635 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700636}
637
Martijn Coenenb40ef022017-01-02 15:21:46 +0100638bool Interface::isElidableType() const {
639 return true;
640}
641
Andreas Hubera2723d22016-07-29 15:36:07 -0700642bool Interface::isInterface() const {
643 return true;
644}
645
Andreas Huber295ad302016-08-16 11:35:00 -0700646bool Interface::isBinder() const {
647 return true;
648}
649
Yifan Hong10fe0b52016-10-19 14:20:17 -0700650const std::vector<Method *> &Interface::userDefinedMethods() const {
651 return mUserMethods;
652}
653
654const std::vector<Method *> &Interface::hidlReservedMethods() const {
655 return mReservedMethods;
656}
657
658std::vector<Method *> Interface::methods() const {
659 std::vector<Method *> v(mUserMethods);
660 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
661 return v;
662}
663
664std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
665 std::vector<InterfaceAndMethod> v;
666 std::vector<const Interface *> chain = typeChain();
667 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
668 const Interface *iface = *it;
669 for (Method *userMethod : iface->userDefinedMethods()) {
670 v.push_back(InterfaceAndMethod(iface, userMethod));
671 }
672 }
673 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800674 v.push_back(InterfaceAndMethod(
675 *chain.rbegin(), // IBase
676 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700677 }
678 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700679}
680
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700681std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
682 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
683}
684
Steven Moreland40786312016-08-16 10:29:40 -0700685std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700686 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700687}
688
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700689std::string Interface::getAdapterName() const {
690 return fqName().getInterfaceAdapterName();
691}
692
Yifan Hongeefe4f22017-01-04 15:32:42 -0800693std::string Interface::getProxyName() const {
694 return fqName().getInterfaceProxyName();
695}
696
697std::string Interface::getStubName() const {
698 return fqName().getInterfaceStubName();
699}
700
701std::string Interface::getHwName() const {
702 return fqName().getInterfaceHwName();
703}
704
705std::string Interface::getPassthroughName() const {
706 return fqName().getInterfacePassthroughName();
707}
708
Yifan Hong51a65092017-01-04 15:41:44 -0800709FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800710 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800711}
712
Yifan Hong51a65092017-01-04 15:41:44 -0800713FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800714 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800715}
716
Yifan Hong51a65092017-01-04 15:41:44 -0800717FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800718 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800719}
720
Steven Moreland979e0992016-09-07 09:18:08 -0700721std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700722 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700723 const std::string base =
724 std::string(specifyNamespaces ? "::android::" : "")
725 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700726 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700727 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700728
729 switch (mode) {
730 case StorageMode_Stack:
731 case StorageMode_Result:
732 return base;
733
734 case StorageMode_Argument:
735 return "const " + base + "&";
736 }
737}
738
Yifan Hong4ed13472016-11-02 10:44:11 -0700739std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700740 return fullJavaName();
741}
742
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800743std::string Interface::getVtsType() const {
744 if (StringHelper::EndsWith(localName(), "Callback")) {
745 return "TYPE_HIDL_CALLBACK";
746 } else {
747 return "TYPE_HIDL_INTERFACE";
748 }
749}
750
Andreas Huber881227d2016-08-02 14:20:21 -0700751void Interface::emitReaderWriter(
752 Formatter &out,
753 const std::string &name,
754 const std::string &parcelObj,
755 bool parcelObjIsPointer,
756 bool isReader,
757 ErrorMode mode) const {
758 const std::string parcelObjDeref =
759 parcelObj + (parcelObjIsPointer ? "->" : ".");
760
761 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700762 out << "{\n";
763 out.indent();
764
Howard Chenecfb4512017-11-21 18:28:53 +0800765 const std::string binderName = "_hidl_binder";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700766 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700767 << binderName << ";\n";
768
Iliyan Malchev549e2592016-08-10 08:59:12 -0700769 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700770 out << parcelObjDeref
771 << "readNullableStrongBinder(&"
772 << binderName
773 << ");\n";
774
775 handleError(out, mode);
776
777 out << name
778 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100779 << "::android::hardware::fromBinder<"
780 << fqName().cppName()
781 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800782 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100783 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800784 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100785 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700786 << binderName
787 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700788
789 out.unindent();
790 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700791 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200792 out << "if (" << name << " == nullptr) {\n";
793 out.indent();
794 out << "_hidl_err = ";
795 out << parcelObjDeref
796 << "writeStrongBinder(nullptr);\n";
797 out.unindent();
798 out << "} else {\n";
799 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800800 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
801 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800802 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100803 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800804 << ">("
805 << name
806 << ");\n";
807 });
808 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800809 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800810 out << "_hidl_err = "
811 << parcelObjDeref
812 << "writeStrongBinder(_hidl_binder);\n";
813 });
814 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800815 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800816 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
817 });
818 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200819 out.unindent();
820 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700821
Andreas Huber881227d2016-08-02 14:20:21 -0700822 handleError(out, mode);
823 }
824}
825
Steven Moreland4b8f7a12017-11-17 15:39:54 -0800826status_t Interface::emitPackageTypeDeclarations(Formatter& out) const {
827 status_t status = Scope::emitPackageTypeDeclarations(out);
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800828 if (status != OK) {
829 return status;
830 }
Steven Morelandbf714212017-10-27 18:29:01 -0700831
832 // TODO(b/65200821): remove these ifndefs
833 out << "#ifdef REALLY_IS_HIDL_INTERNAL_LIB" << gCurrentCompileName << "\n";
Steven Moreland3d98bc42017-06-23 21:36:41 +0000834 out << "std::string toString("
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800835 << getCppArgumentType()
Steven Moreland3d98bc42017-06-23 21:36:41 +0000836 << ");\n";
Steven Morelandbf714212017-10-27 18:29:01 -0700837 out << "#else\n";
838 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
839
840 out.block([&] {
841 out << "std::string os = \"[class or subclass of \";\n"
842 << "os += " << fullName() << "::descriptor;\n"
843 << "os += \"]\";\n"
844 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
845 << "return os;\n";
846 }).endl().endl();
847 out << "#endif // REALLY_IS_HIDL_INTERNAL_LIB\n";
848
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800849 return OK;
850}
851
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700852status_t Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800853 std::string space = prefix.empty() ? "" : (prefix + "::");
854 status_t err = Scope::emitTypeDefinitions(out, space + localName());
855 if (err != OK) {
856 return err;
857 }
858
Steven Morelandbf714212017-10-27 18:29:01 -0700859 // TODO(b/65200821): remove toString from .cpp once all prebuilts are rebuilt
Steven Moreland3d98bc42017-06-23 21:36:41 +0000860 out << "std::string toString("
861 << getCppArgumentType()
862 << " o) ";
863
864 out.block([&] {
865 out << "std::string os = \"[class or subclass of \";\n"
866 << "os += " << fullName() << "::descriptor;\n"
867 << "os += \"]\";\n"
868 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
869 << "return os;\n";
870 }).endl().endl();
871
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800872 return OK;
873}
874
Andreas Huber2831d512016-08-15 09:33:47 -0700875void Interface::emitJavaReaderWriter(
876 Formatter &out,
877 const std::string &parcelObj,
878 const std::string &argName,
879 bool isReader) const {
880 if (isReader) {
881 out << fullJavaName()
882 << ".asInterface("
883 << parcelObj
884 << ".readStrongBinder());\n";
885 } else {
886 out << parcelObj
887 << ".writeStrongBinder("
888 << argName
889 << " == null ? null : "
890 << argName
891 << ".asBinder());\n";
892 }
893}
894
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700895status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
896 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700897 // Skip for TypeDef as it is just an alias of a defined type.
898 if (type->isTypeDef()) {
899 continue;
900 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700901 out << "attribute: {\n";
902 out.indent();
903 status_t status = type->emitVtsTypeDeclarations(out);
904 if (status != OK) {
905 return status;
906 }
907 out.unindent();
908 out << "}\n\n";
909 }
910 return OK;
911}
912
913status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700914 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800915 if (method->isHidlReserved()) {
916 continue;
917 }
918
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700919 out << "api: {\n";
920 out.indent();
921 out << "name: \"" << method->name() << "\"\n";
922 // Generate declaration for each return value.
923 for (const auto &result : method->results()) {
924 out << "return_type_hidl: {\n";
925 out.indent();
926 status_t status = result->type().emitVtsAttributeType(out);
927 if (status != OK) {
928 return status;
929 }
930 out.unindent();
931 out << "}\n";
932 }
933 // Generate declaration for each input argument
934 for (const auto &arg : method->args()) {
935 out << "arg: {\n";
936 out.indent();
937 status_t status = arg->type().emitVtsAttributeType(out);
938 if (status != OK) {
939 return status;
940 }
941 out.unindent();
942 out << "}\n";
943 }
944 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700945 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700946 out << "callflow: {\n";
947 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700948 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700949 if (name == "entry") {
950 out << "entry: true\n";
951 } else if (name == "exit") {
952 out << "exit: true\n";
953 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700954 const AnnotationParam *param =
955 annotation->getParam("next");
956 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700957 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700958 out << "next: " << value << "\n";
959 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700960 }
961 } else {
Steven Morelandcbff5612017-10-11 17:01:54 -0700962 std::cerr << "ERROR: Unrecognized annotation '" << name
963 << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800964 << ". A VTS annotation should be one of: "
Steven Morelandcbff5612017-10-11 17:01:54 -0700965 << "entry, exit, callflow." << std::endl;
966 return UNKNOWN_ERROR;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700967 }
968 out.unindent();
969 out << "}\n";
970 }
971 out.unindent();
972 out << "}\n\n";
973 }
974 return OK;
975}
976
977status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800978 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700979 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800980 << fullName()
981 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700982 return OK;
983}
984
Steven Moreland69e7c702016-09-09 11:16:32 -0700985bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700986 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700987 if (method->isOneway()) {
988 return true;
989 }
990 }
991
992 const Interface* superClass = superType();
993
994 if (superClass != nullptr) {
995 return superClass->hasOnewayMethods();
996 }
997
998 return false;
999}
1000
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001001bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
1002 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -07001003 return false;
1004 }
1005
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001006 for (const auto* method : methods()) {
1007 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -07001008 return false;
1009 }
1010 }
1011
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001012 return Scope::isJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -07001013}
1014
Timur Iskhakovff5e64a2017-09-11 14:56:18 -07001015bool Interface::isNeverStrongReference() const {
1016 return true;
1017}
1018
Andreas Huberc9410c72016-07-28 12:18:40 -07001019} // namespace android
1020