blob: ac13f9fafbc11ea33b44f3c49083f2888d4bd7e7 [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>
Steven Moreland5bdfa702017-04-18 23:20:39 -070036#include <hidl-hash/Hash.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070037#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070038#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070039
Andreas Huberc9410c72016-07-28 12:18:40 -070040namespace android {
41
Steven Morelandb8e15a52017-02-13 19:35:40 -080042#define B_PACK_CHARS(c1, c2, c3, c4) \
43 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
44
Yifan Hong10fe0b52016-10-19 14:20:17 -070045/* It is very important that these values NEVER change. These values
46 * must remain unchanged over the lifetime of android. This is
47 * because the framework on a device will be updated independently of
48 * the hals on a device. If the hals are compiled with one set of
49 * transaction values, and the framework with another, then the
50 * interface between them will be destroyed, and the device will not
51 * work.
52 */
53enum {
Yifan Hong10fe0b52016-10-19 14:20:17 -070054 /////////////////// User defined transactions
55 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080056 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070057 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080058 FIRST_HIDL_TRANSACTION = 0x0f000000,
59 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
60 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
61 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
62 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
63 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
64 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
65 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
66 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
67 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
Yifan Hong30b5d1f2017-04-03 12:19:25 -070068 HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'),
Steven Morelandb8e15a52017-02-13 19:35:40 -080069 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070070};
71
Timur Iskhakov565b0132017-09-06 18:07:11 -070072Interface::Interface(const char* localName, const FQName& fullName, const Location& location,
73 Scope* parent, const Reference<Type>& superType)
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -070074 : Scope(localName, fullName, location, parent), mSuperType(superType) {}
Martijn Coenenaf712c02016-11-16 15:26:27 +010075
Steven Moreland30bb6a82016-11-30 09:18:34 -080076std::string Interface::typeName() const {
77 return "interface " + localName();
78}
79
Steven Moreland424a9482017-02-13 19:20:40 -080080bool Interface::fillPingMethod(Method *method) const {
81 if (method->name() != "ping") {
82 return false;
83 }
84
85 method->fillImplementation(
86 HIDL_PING_TRANSACTION,
87 {
Steven Moreland937408a2017-03-20 09:54:18 -070088 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080089 [](auto &out) {
90 out << "return ::android::hardware::Void();\n";
91 }
92 },
93 {IMPL_STUB_IMPL,
94 [](auto &out) {
95 out << "return ::android::hardware::Void();\n";
96 }
97 }
98 }, /*cppImpl*/
99 {
Steven Moreland937408a2017-03-20 09:54:18 -0700100 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700101 [](auto &out) {
Steven Moreland424a9482017-02-13 19:20:40 -0800102 out << "return;\n";
103 }
104 },
105 {IMPL_STUB, nullptr /* don't generate code */}
106 } /*javaImpl*/
107 );
108
109 return true;
110}
111
Yifan Hongffa91392017-01-31 13:41:23 -0800112bool Interface::fillLinkToDeathMethod(Method *method) const {
113 if (method->name() != "linkToDeath") {
114 return false;
115 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100116
Yifan Hongffa91392017-01-31 13:41:23 -0800117 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100118 HIDL_LINK_TO_DEATH_TRANSACTION,
119 {
Steven Moreland937408a2017-03-20 09:54:18 -0700120 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100121 [](auto &out) {
122 out << "(void)cookie;\n"
123 << "return (recipient != nullptr);\n";
124 }
125 },
126 {IMPL_PROXY,
127 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100128 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100129 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
130 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
131 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
132 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
133 << "return (remote()->linkToDeath(binder_recipient)"
134 << " == ::android::OK);\n";
135 }
136 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100137 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100138 }, /*cppImpl*/
139 {
Steven Moreland937408a2017-03-20 09:54:18 -0700140 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700141 [](auto &out) {
Martijn Coenen115d4282016-12-19 05:14:04 +0100142 out << "return true;";
143 }
144 },
145 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700146 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100147 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100148 }
149 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100150 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100151 } /*javaImpl*/
152 );
Yifan Hongffa91392017-01-31 13:41:23 -0800153 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100154}
155
Yifan Hongffa91392017-01-31 13:41:23 -0800156bool Interface::fillUnlinkToDeathMethod(Method *method) const {
157 if (method->name() != "unlinkToDeath") {
158 return false;
159 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100160
Yifan Hongffa91392017-01-31 13:41:23 -0800161 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100162 HIDL_UNLINK_TO_DEATH_TRANSACTION,
163 {
Steven Moreland937408a2017-03-20 09:54:18 -0700164 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100165 [](auto &out) {
166 out << "return (recipient != nullptr);\n";
167 }
168 },
169 {IMPL_PROXY,
170 [](auto &out) {
171 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
172 << "for (auto it = _hidl_mDeathRecipients.begin();"
173 << "it != _hidl_mDeathRecipients.end();"
174 << "++it) {\n";
175 out.indent([&] {
176 out.sIf("(*it)->getRecipient() == recipient", [&] {
177 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
178 << "_hidl_mDeathRecipients.erase(it);\n"
179 << "return status == ::android::OK;\n";
180 });
181 });
182 out << "}\n";
183 out << "return false;\n";
184 }
185 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100186 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100187 }, /*cppImpl*/
188 {
Steven Moreland937408a2017-03-20 09:54:18 -0700189 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700190 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100191 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100192 }
193 },
194 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700195 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100196 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100197 }
198 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100199 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100200 } /*javaImpl*/
201 );
Yifan Hongffa91392017-01-31 13:41:23 -0800202 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100203}
Yifan Hongffa91392017-01-31 13:41:23 -0800204bool Interface::fillSyspropsChangedMethod(Method *method) const {
205 if (method->name() != "notifySyspropsChanged") {
206 return false;
207 }
208
209 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100210 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yi Kongc8ff4672017-04-30 23:46:56 -0700211 { { IMPL_INTERFACE, [](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100212 out << "::android::report_sysprop_change();\n";
213 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100214 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700215 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Sundong Ahnc1432c92017-07-13 23:58:27 +0900216 out << "android.os.HwBinder.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100217 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100218 );
Yifan Hongffa91392017-01-31 13:41:23 -0800219 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700220}
221
Yifan Hongffa91392017-01-31 13:41:23 -0800222bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
223 if (method->name() != "setHALInstrumentation") {
224 return false;
225 }
226
227 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800228 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
229 {
Steven Moreland937408a2017-03-20 09:54:18 -0700230 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700231 [](auto &out) {
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800232 // do nothing for base class.
233 out << "return ::android::hardware::Void();\n";
234 }
235 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800236 {IMPL_STUB,
237 [](auto &out) {
238 out << "configureInstrumentation();\n";
239 }
240 },
241 {IMPL_PASSTHROUGH,
242 [](auto &out) {
243 out << "configureInstrumentation();\n";
244 out << "return ::android::hardware::Void();\n";
245 }
246 },
247 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700248 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800249 // Not support for Java Impl for now.
250 } } } /*javaImpl */
251 );
Yifan Hongffa91392017-01-31 13:41:23 -0800252 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800253}
254
Yifan Hongffa91392017-01-31 13:41:23 -0800255bool Interface::fillDescriptorChainMethod(Method *method) const {
256 if (method->name() != "interfaceChain") {
257 return false;
258 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700259
Yifan Hongffa91392017-01-31 13:41:23 -0800260 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700261 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700262 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700263 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800264 out << "_hidl_cb(";
265 out.block([&] {
266 for (const Interface *iface : chain) {
267 out << iface->fullName() << "::descriptor,\n";
268 }
269 });
270 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700271 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100272 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700273 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700274 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800275 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700276 out.indent(); out.indent();
277 for (size_t i = 0; i < chain.size(); ++i) {
278 if (i != 0)
279 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800280 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700281 }
282 out << "));";
283 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800284 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100285 );
Yifan Hongffa91392017-01-31 13:41:23 -0800286 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700287}
288
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700289static void emitDigestChain(
Timur Iskhakov7296af12017-08-09 21:52:48 +0000290 Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
291 std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700292 out.join(chain.begin(), chain.end(), ",\n", [&] (const auto &iface) {
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700293 const Hash &hash = Hash::getHash(iface->location().begin().filename());
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700294 out << prefix;
295 out << "{";
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700296 out.join(hash.raw().begin(), hash.raw().end(), ",", [&](const auto &e) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700297 // Use ConstantExpression::cppValue / javaValue
298 // because Java used signed byte for uint8_t.
299 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
300 });
301 out << "} /* ";
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700302 out << hash.hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700303 out << " */";
304 });
305}
306
307bool Interface::fillHashChainMethod(Method *method) const {
308 if (method->name() != "getHashChain") {
309 return false;
310 }
311 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
312 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
313
314 method->fillImplementation(
315 HIDL_HASH_CHAIN_TRANSACTION,
316 { { IMPL_INTERFACE, [this, digestType](auto &out) {
317 std::vector<const Interface *> chain = typeChain();
318 out << "_hidl_cb(";
319 out.block([&] {
Timur Iskhakov7296af12017-08-09 21:52:48 +0000320 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain,
321 [](const auto& e) { return e->cppValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700322 });
323 out << ");\n";
324 out << "return ::android::hardware::Void();\n";
325 } } }, /* cppImpl */
326 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
327 std::vector<const Interface *> chain = typeChain();
328 out << "return new "
329 << chainType->getJavaType(false /* forInitializer */)
330 << "(java.util.Arrays.asList(\n";
331 out.indent(2, [&] {
332 // No need for dimensions when elements are explicitly provided.
333 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
Timur Iskhakov7296af12017-08-09 21:52:48 +0000334 chain, [](const auto& e) { return e->javaValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700335 });
336 out << "));\n";
337 } } } /* javaImpl */
338 );
339 return true;
340}
341
Yifan Hongffa91392017-01-31 13:41:23 -0800342bool Interface::fillGetDescriptorMethod(Method *method) const {
343 if (method->name() != "interfaceDescriptor") {
344 return false;
345 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800346
Yifan Hongffa91392017-01-31 13:41:23 -0800347 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800348 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700349 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800350 out << "_hidl_cb("
351 << fullName()
352 << "::descriptor);\n"
353 << "return ::android::hardware::Void();";
354 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700355 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800356 out << "return "
357 << fullJavaName()
358 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800359 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800360 );
Yifan Hongffa91392017-01-31 13:41:23 -0800361 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800362}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700363
Yifan Hongbcffce22017-02-01 15:52:06 -0800364bool Interface::fillGetDebugInfoMethod(Method *method) const {
365 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800366 return false;
367 }
368
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800369 static const std::string sArch =
370 "#if defined(__LP64__)\n"
371 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
372 "#else\n"
373 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
374 "#endif\n";
375
Yifan Hongcd2ae452017-01-31 14:33:40 -0800376 method->fillImplementation(
377 HIDL_GET_REF_INFO_TRANSACTION,
378 {
Steven Moreland937408a2017-03-20 09:54:18 -0700379 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700380 [](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800381 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800382 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
383 << sArch
384 << "});\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800385 << "return ::android::hardware::Void();";
386 }
387 },
388 {IMPL_STUB_IMPL,
Yi Kongc8ff4672017-04-30 23:46:56 -0700389 [](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800390 out << "_hidl_cb(";
391 out.block([&] {
Yifan Hong96bb0632017-11-14 16:08:37 -0800392 out << "::android::hardware::details::getPidIfSharable(),\n"
Yifan Hong2e036c92017-03-07 13:18:12 -0800393 << "::android::hardware::details::debuggable()"
394 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
395 << sArch << "\n";
396 });
397 out << ");\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800398 << "return ::android::hardware::Void();";
399 }
400 }
401 }, /* cppImpl */
Yi Kongc8ff4672017-04-30 23:46:56 -0700402 { { IMPL_INTERFACE, [method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800403 const Type &refInfo = method->results().front()->type();
404 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
405 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800406 << "info.pid = android.os.HidlSupport.getPidIfSharable();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800407 << "info.ptr = 0;\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800408 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800409 << "return info;";
410 } } } /* javaImpl */
411 );
412
413 return true;
414}
415
Andreas Huber37065d62017-02-07 14:36:54 -0800416bool Interface::fillDebugMethod(Method *method) const {
417 if (method->name() != "debug") {
418 return false;
419 }
420
421 method->fillImplementation(
422 HIDL_DEBUG_TRANSACTION,
423 {
Steven Moreland937408a2017-03-20 09:54:18 -0700424 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700425 [](auto &out) {
Andreas Huber37065d62017-02-07 14:36:54 -0800426 out << "(void)fd;\n"
427 << "(void)options;\n"
428 << "return ::android::hardware::Void();";
429 }
430 },
431 }, /* cppImpl */
432 {
433 /* unused, as the debug method is hidden from Java */
434 } /* javaImpl */
435 );
436
437 return true;
438}
439
Yifan Hongffa91392017-01-31 13:41:23 -0800440static std::map<std::string, Method *> gAllReservedMethods;
441
Steven Moreland14ee6742016-10-18 12:58:28 -0700442bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800443 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800444 if (!gAllReservedMethods.emplace(method->name(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700445 std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name()
446 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800447 return false;
448 }
449 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800450 return true;
451 }
452
Yifan Hong10fe0b52016-10-19 14:20:17 -0700453 CHECK(!method->isHidlReserved());
Yifan Hong10fe0b52016-10-19 14:20:17 -0700454 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700455
456 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700457}
458
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700459std::vector<const Reference<Type>*> Interface::getReferences() const {
460 std::vector<const Reference<Type>*> ret;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700461
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700462 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700463 ret.push_back(&mSuperType);
Timur Iskhakov33431e62017-08-21 17:31:23 -0700464 }
465
466 for (const auto* method : methods()) {
467 const auto& references = method->getReferences();
468 ret.insert(ret.end(), references.begin(), references.end());
469 }
470
471 return ret;
472}
473
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700474std::vector<const ConstantExpression*> Interface::getConstantExpressions() const {
475 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700476 for (const auto* method : methods()) {
477 const auto& retMethod = method->getConstantExpressions();
478 ret.insert(ret.end(), retMethod.begin(), retMethod.end());
479 }
480 return ret;
481}
482
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700483std::vector<const Reference<Type>*> Interface::getStrongReferences() const {
Timur Iskhakov40731af2017-08-24 14:18:35 -0700484 // Interface is a special case as a reference:
485 // its definiton must be completed for extension but
486 // not necessary for other references.
Timur Iskhakov40731af2017-08-24 14:18:35 -0700487
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700488 std::vector<const Reference<Type>*> ret;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700489 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700490 ret.push_back(&mSuperType);
Timur Iskhakov40731af2017-08-24 14:18:35 -0700491 }
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700492
493 for (const auto* method : methods()) {
494 const auto& references = method->getStrongReferences();
495 ret.insert(ret.end(), references.begin(), references.end());
496 }
497
Timur Iskhakov40731af2017-08-24 14:18:35 -0700498 return ret;
499}
500
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700501status_t Interface::resolveInheritance() {
502 size_t serial = FIRST_CALL_TRANSACTION;
503 for (const auto* ancestor : superTypeChain()) {
504 serial += ancestor->mUserMethods.size();
505 }
506
507 for (Method* method : mUserMethods) {
508 if (serial > LAST_CALL_TRANSACTION) {
509 std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION
510 << " methods (including super and reserved) are not allowed at " << location()
Steven Morelandcbff5612017-10-11 17:01:54 -0700511 << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700512 return UNKNOWN_ERROR;
513 }
514
515 method->setSerialId(serial);
516 serial++;
517 }
518
519 return Scope::resolveInheritance();
520}
521
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700522status_t Interface::validate() const {
523 CHECK(isIBase() == mSuperType.isEmptyReference());
524
Timur Iskhakov0344e612017-08-25 12:57:09 -0700525 if (!isIBase() && !mSuperType->isInterface()) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700526 std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location()
527 << std::endl;
Timur Iskhakov0344e612017-08-25 12:57:09 -0700528 return UNKNOWN_ERROR;
529 }
530
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700531 status_t err = validateUniqueNames();
532 if (err != OK) return err;
533
534 return Scope::validate();
535}
536
537status_t Interface::validateUniqueNames() const {
538 std::unordered_map<std::string, const Interface*> registeredMethodNames;
539 for (auto const& tuple : allSuperMethodsFromRoot()) {
540 // No need to check super method uniqueness
541 registeredMethodNames[tuple.method()->name()] = tuple.interface();
542 }
543
544 for (const Method* method : mUserMethods) {
545 auto registered = registeredMethodNames.find(method->name());
546
547 if (registered != registeredMethodNames.end()) {
548 const Interface* definedInType = registered->second;
549
550 if (definedInType == this) {
551 // Defined in this interface
552 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
553 } else if (definedInType->isIBase()) {
554 // Defined in IBase
555 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
556 } else {
557 // Defined in super not IBase
558 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700559 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700560 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700561 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700562 return UNKNOWN_ERROR;
563 }
564
565 registeredMethodNames[method->name()] = this;
566 }
567
568 return OK;
569}
570
Yifan Hongffa91392017-01-31 13:41:23 -0800571bool Interface::addAllReservedMethods() {
572 // use a sorted map to insert them in serial ID order.
573 std::map<int32_t, Method *> reservedMethodsById;
574 for (const auto &pair : gAllReservedMethods) {
575 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800576 bool fillSuccess = fillPingMethod(method)
577 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800578 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700579 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800580 || fillSyspropsChangedMethod(method)
581 || fillLinkToDeathMethod(method)
582 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800583 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800584 || fillGetDebugInfoMethod(method)
585 || fillDebugMethod(method);
586
Yifan Hongffa91392017-01-31 13:41:23 -0800587 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700588 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
589 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800590 return false;
591 }
592 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700593 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
594 << " and " << reservedMethodsById[method->getSerialId()]->name()
595 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800596 return false;
597 }
598 }
599 for (const auto &pair : reservedMethodsById) {
600 this->mReservedMethods.push_back(pair.second);
601 }
602 return true;
603}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700604
Timur Iskhakov505316c2017-08-05 03:38:59 +0000605const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700606 if (isIBase()) return nullptr;
607 if (!mSuperType->isInterface()) {
608 // This is actually an error
609 // that would be caught in validate
610 return nullptr;
611 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700612 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700613}
614
Yifan Hong10fe0b52016-10-19 14:20:17 -0700615std::vector<const Interface *> Interface::typeChain() const {
616 std::vector<const Interface *> v;
617 const Interface *iface = this;
618 while (iface != nullptr) {
619 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000620 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700621 }
622 return v;
623}
624
Yifan Hongfe95aa22016-10-19 17:26:45 -0700625std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000626 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700627}
628
Martijn Coenenb40ef022017-01-02 15:21:46 +0100629bool Interface::isElidableType() const {
630 return true;
631}
632
Andreas Hubera2723d22016-07-29 15:36:07 -0700633bool Interface::isInterface() const {
634 return true;
635}
636
Andreas Huber295ad302016-08-16 11:35:00 -0700637bool Interface::isBinder() const {
638 return true;
639}
640
Yifan Hong10fe0b52016-10-19 14:20:17 -0700641const std::vector<Method *> &Interface::userDefinedMethods() const {
642 return mUserMethods;
643}
644
645const std::vector<Method *> &Interface::hidlReservedMethods() const {
646 return mReservedMethods;
647}
648
649std::vector<Method *> Interface::methods() const {
650 std::vector<Method *> v(mUserMethods);
651 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
652 return v;
653}
654
655std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
656 std::vector<InterfaceAndMethod> v;
657 std::vector<const Interface *> chain = typeChain();
658 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
659 const Interface *iface = *it;
660 for (Method *userMethod : iface->userDefinedMethods()) {
661 v.push_back(InterfaceAndMethod(iface, userMethod));
662 }
663 }
664 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800665 v.push_back(InterfaceAndMethod(
666 *chain.rbegin(), // IBase
667 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700668 }
669 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700670}
671
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700672std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
673 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
674}
675
Steven Moreland40786312016-08-16 10:29:40 -0700676std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700677 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700678}
679
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700680std::string Interface::getAdapterName() const {
681 return fqName().getInterfaceAdapterName();
682}
683
Yifan Hongeefe4f22017-01-04 15:32:42 -0800684std::string Interface::getProxyName() const {
685 return fqName().getInterfaceProxyName();
686}
687
688std::string Interface::getStubName() const {
689 return fqName().getInterfaceStubName();
690}
691
692std::string Interface::getHwName() const {
693 return fqName().getInterfaceHwName();
694}
695
696std::string Interface::getPassthroughName() const {
697 return fqName().getInterfacePassthroughName();
698}
699
Yifan Hong51a65092017-01-04 15:41:44 -0800700FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800701 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800702}
703
Yifan Hong51a65092017-01-04 15:41:44 -0800704FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800705 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800706}
707
Yifan Hong51a65092017-01-04 15:41:44 -0800708FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800709 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800710}
711
Steven Moreland979e0992016-09-07 09:18:08 -0700712std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700713 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700714 const std::string base =
715 std::string(specifyNamespaces ? "::android::" : "")
716 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700717 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700718 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700719
720 switch (mode) {
721 case StorageMode_Stack:
722 case StorageMode_Result:
723 return base;
724
725 case StorageMode_Argument:
726 return "const " + base + "&";
727 }
728}
729
Yifan Hong4ed13472016-11-02 10:44:11 -0700730std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700731 return fullJavaName();
732}
733
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800734std::string Interface::getVtsType() const {
735 if (StringHelper::EndsWith(localName(), "Callback")) {
736 return "TYPE_HIDL_CALLBACK";
737 } else {
738 return "TYPE_HIDL_INTERFACE";
739 }
740}
741
Andreas Huber881227d2016-08-02 14:20:21 -0700742void Interface::emitReaderWriter(
743 Formatter &out,
744 const std::string &name,
745 const std::string &parcelObj,
746 bool parcelObjIsPointer,
747 bool isReader,
748 ErrorMode mode) const {
749 const std::string parcelObjDeref =
750 parcelObj + (parcelObjIsPointer ? "->" : ".");
751
752 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700753 out << "{\n";
754 out.indent();
755
Iliyan Malchev549e2592016-08-10 08:59:12 -0700756 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700757
Andreas Huber8a82ff72016-08-04 10:29:39 -0700758 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700759 << binderName << ";\n";
760
Iliyan Malchev549e2592016-08-10 08:59:12 -0700761 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700762 out << parcelObjDeref
763 << "readNullableStrongBinder(&"
764 << binderName
765 << ");\n";
766
767 handleError(out, mode);
768
769 out << name
770 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100771 << "::android::hardware::fromBinder<"
772 << fqName().cppName()
773 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800774 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100775 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800776 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100777 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700778 << binderName
779 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700780
781 out.unindent();
782 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700783 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200784 out << "if (" << name << " == nullptr) {\n";
785 out.indent();
786 out << "_hidl_err = ";
787 out << parcelObjDeref
788 << "writeStrongBinder(nullptr);\n";
789 out.unindent();
790 out << "} else {\n";
791 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800792 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
793 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800794 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100795 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800796 << ">("
797 << name
798 << ");\n";
799 });
800 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800801 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800802 out << "_hidl_err = "
803 << parcelObjDeref
804 << "writeStrongBinder(_hidl_binder);\n";
805 });
806 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800807 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800808 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
809 });
810 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200811 out.unindent();
812 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700813
Andreas Huber881227d2016-08-02 14:20:21 -0700814 handleError(out, mode);
815 }
816}
817
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800818status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
819 status_t status = Scope::emitGlobalTypeDeclarations(out);
820 if (status != OK) {
821 return status;
822 }
Steven Morelandbf714212017-10-27 18:29:01 -0700823
824 // TODO(b/65200821): remove these ifndefs
825 out << "#ifdef REALLY_IS_HIDL_INTERNAL_LIB" << gCurrentCompileName << "\n";
Steven Moreland3d98bc42017-06-23 21:36:41 +0000826 out << "std::string toString("
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800827 << getCppArgumentType()
Steven Moreland3d98bc42017-06-23 21:36:41 +0000828 << ");\n";
Steven Morelandbf714212017-10-27 18:29:01 -0700829 out << "#else\n";
830 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
831
832 out.block([&] {
833 out << "std::string os = \"[class or subclass of \";\n"
834 << "os += " << fullName() << "::descriptor;\n"
835 << "os += \"]\";\n"
836 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
837 << "return os;\n";
838 }).endl().endl();
839 out << "#endif // REALLY_IS_HIDL_INTERNAL_LIB\n";
840
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800841 return OK;
842}
843
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700844status_t Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800845 std::string space = prefix.empty() ? "" : (prefix + "::");
846 status_t err = Scope::emitTypeDefinitions(out, space + localName());
847 if (err != OK) {
848 return err;
849 }
850
Steven Morelandbf714212017-10-27 18:29:01 -0700851 // TODO(b/65200821): remove toString from .cpp once all prebuilts are rebuilt
Steven Moreland3d98bc42017-06-23 21:36:41 +0000852 out << "std::string toString("
853 << getCppArgumentType()
854 << " o) ";
855
856 out.block([&] {
857 out << "std::string os = \"[class or subclass of \";\n"
858 << "os += " << fullName() << "::descriptor;\n"
859 << "os += \"]\";\n"
860 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
861 << "return os;\n";
862 }).endl().endl();
863
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800864 return OK;
865}
866
Andreas Huber2831d512016-08-15 09:33:47 -0700867void Interface::emitJavaReaderWriter(
868 Formatter &out,
869 const std::string &parcelObj,
870 const std::string &argName,
871 bool isReader) const {
872 if (isReader) {
873 out << fullJavaName()
874 << ".asInterface("
875 << parcelObj
876 << ".readStrongBinder());\n";
877 } else {
878 out << parcelObj
879 << ".writeStrongBinder("
880 << argName
881 << " == null ? null : "
882 << argName
883 << ".asBinder());\n";
884 }
885}
886
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700887status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
888 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700889 // Skip for TypeDef as it is just an alias of a defined type.
890 if (type->isTypeDef()) {
891 continue;
892 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700893 out << "attribute: {\n";
894 out.indent();
895 status_t status = type->emitVtsTypeDeclarations(out);
896 if (status != OK) {
897 return status;
898 }
899 out.unindent();
900 out << "}\n\n";
901 }
902 return OK;
903}
904
905status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700906 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800907 if (method->isHidlReserved()) {
908 continue;
909 }
910
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700911 out << "api: {\n";
912 out.indent();
913 out << "name: \"" << method->name() << "\"\n";
914 // Generate declaration for each return value.
915 for (const auto &result : method->results()) {
916 out << "return_type_hidl: {\n";
917 out.indent();
918 status_t status = result->type().emitVtsAttributeType(out);
919 if (status != OK) {
920 return status;
921 }
922 out.unindent();
923 out << "}\n";
924 }
925 // Generate declaration for each input argument
926 for (const auto &arg : method->args()) {
927 out << "arg: {\n";
928 out.indent();
929 status_t status = arg->type().emitVtsAttributeType(out);
930 if (status != OK) {
931 return status;
932 }
933 out.unindent();
934 out << "}\n";
935 }
936 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700937 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700938 out << "callflow: {\n";
939 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700940 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700941 if (name == "entry") {
942 out << "entry: true\n";
943 } else if (name == "exit") {
944 out << "exit: true\n";
945 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700946 const AnnotationParam *param =
947 annotation->getParam("next");
948 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700949 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700950 out << "next: " << value << "\n";
951 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700952 }
953 } else {
Steven Morelandcbff5612017-10-11 17:01:54 -0700954 std::cerr << "ERROR: Unrecognized annotation '" << name
955 << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800956 << ". A VTS annotation should be one of: "
Steven Morelandcbff5612017-10-11 17:01:54 -0700957 << "entry, exit, callflow." << std::endl;
958 return UNKNOWN_ERROR;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700959 }
960 out.unindent();
961 out << "}\n";
962 }
963 out.unindent();
964 out << "}\n\n";
965 }
966 return OK;
967}
968
969status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800970 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700971 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800972 << fullName()
973 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700974 return OK;
975}
976
Steven Moreland69e7c702016-09-09 11:16:32 -0700977bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700978 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700979 if (method->isOneway()) {
980 return true;
981 }
982 }
983
984 const Interface* superClass = superType();
985
986 if (superClass != nullptr) {
987 return superClass->hasOnewayMethods();
988 }
989
990 return false;
991}
992
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700993bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
994 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700995 return false;
996 }
997
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700998 for (const auto* method : methods()) {
999 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -07001000 return false;
1001 }
1002 }
1003
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001004 return Scope::isJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -07001005}
1006
Timur Iskhakovff5e64a2017-09-11 14:56:18 -07001007bool Interface::isNeverStrongReference() const {
1008 return true;
1009}
1010
Andreas Huberc9410c72016-07-28 12:18:40 -07001011} // namespace android
1012