blob: af6315fd709fcd63e2f37088ff2f385bc7be649c [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>
Neel Mehta69920a62019-07-22 16:22:13 -070033#include <string>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070034#include <unordered_map>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070035
Steven Moreland14ee6742016-10-18 12:58:28 -070036#include <android-base/logging.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
Steven Moreland77943692018-08-09 12:53:42 -070072const std::unique_ptr<ConstantExpression> Interface::FLAG_ONE_WAY =
73 std::make_unique<LiteralConstantExpression>(ScalarType::KIND_UINT32, 0x01, "oneway");
74
Neel Mehta69920a62019-07-22 16:22:13 -070075Interface::Interface(const std::string& localName, const FQName& fullName, const Location& location,
Steven Moreland04dea8d2018-02-06 13:11:24 -080076 Scope* parent, const Reference<Type>& superType, const Hash* fileHash)
77 : Scope(localName, fullName, location, parent), mSuperType(superType), mFileHash(fileHash) {}
Martijn Coenenaf712c02016-11-16 15:26:27 +010078
Steven Moreland30bb6a82016-11-30 09:18:34 -080079std::string Interface::typeName() const {
Neel Mehta257cce02019-07-19 13:24:57 -070080 return "interface " + definedName();
Steven Moreland30bb6a82016-11-30 09:18:34 -080081}
82
Steven Moreland04dea8d2018-02-06 13:11:24 -080083const Hash* Interface::getFileHash() const {
84 return mFileHash;
85}
86
Steven Moreland424a9482017-02-13 19:20:40 -080087bool Interface::fillPingMethod(Method *method) const {
88 if (method->name() != "ping") {
89 return false;
90 }
91
92 method->fillImplementation(
93 HIDL_PING_TRANSACTION,
94 {
Steven Moreland937408a2017-03-20 09:54:18 -070095 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080096 [](auto &out) {
97 out << "return ::android::hardware::Void();\n";
98 }
99 },
100 {IMPL_STUB_IMPL,
101 [](auto &out) {
102 out << "return ::android::hardware::Void();\n";
103 }
104 }
105 }, /*cppImpl*/
106 {
Steven Moreland937408a2017-03-20 09:54:18 -0700107 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700108 [](auto &out) {
Steven Moreland424a9482017-02-13 19:20:40 -0800109 out << "return;\n";
110 }
111 },
Steven Moreland424a9482017-02-13 19:20:40 -0800112 } /*javaImpl*/
113 );
114
115 return true;
116}
117
Yifan Hongffa91392017-01-31 13:41:23 -0800118bool Interface::fillLinkToDeathMethod(Method *method) const {
119 if (method->name() != "linkToDeath") {
120 return false;
121 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100122
Yifan Hongffa91392017-01-31 13:41:23 -0800123 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100124 HIDL_LINK_TO_DEATH_TRANSACTION,
125 {
Steven Moreland937408a2017-03-20 09:54:18 -0700126 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100127 [](auto &out) {
128 out << "(void)cookie;\n"
129 << "return (recipient != nullptr);\n";
130 }
131 },
132 {IMPL_PROXY,
133 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100134 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100135 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
136 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
137 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
138 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
139 << "return (remote()->linkToDeath(binder_recipient)"
140 << " == ::android::OK);\n";
141 }
142 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100143 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100144 }, /*cppImpl*/
145 {
Steven Moreland937408a2017-03-20 09:54:18 -0700146 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700147 [](auto &out) {
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700148 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100149 }
150 },
151 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700152 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100153 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100154 }
155 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100156 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100157 } /*javaImpl*/
158 );
Yifan Hongffa91392017-01-31 13:41:23 -0800159 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100160}
161
Yifan Hongffa91392017-01-31 13:41:23 -0800162bool Interface::fillUnlinkToDeathMethod(Method *method) const {
163 if (method->name() != "unlinkToDeath") {
164 return false;
165 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100166
Yifan Hongffa91392017-01-31 13:41:23 -0800167 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100168 HIDL_UNLINK_TO_DEATH_TRANSACTION,
169 {
Steven Moreland937408a2017-03-20 09:54:18 -0700170 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100171 [](auto &out) {
172 out << "return (recipient != nullptr);\n";
173 }
174 },
175 {IMPL_PROXY,
176 [](auto &out) {
177 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
Steven Morelandba140f42018-07-24 13:13:15 -0700178 << "for (auto it = _hidl_mDeathRecipients.rbegin();"
179 << "it != _hidl_mDeathRecipients.rend();"
Martijn Coenen115d4282016-12-19 05:14:04 +0100180 << "++it) {\n";
181 out.indent([&] {
182 out.sIf("(*it)->getRecipient() == recipient", [&] {
183 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
Steven Morelandba140f42018-07-24 13:13:15 -0700184 << "_hidl_mDeathRecipients.erase(it.base()-1);\n"
Martijn Coenen115d4282016-12-19 05:14:04 +0100185 << "return status == ::android::OK;\n";
186 });
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700187 }).endl();
Martijn Coenen115d4282016-12-19 05:14:04 +0100188 out << "}\n";
189 out << "return false;\n";
190 }
191 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100192 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100193 }, /*cppImpl*/
194 {
Steven Moreland937408a2017-03-20 09:54:18 -0700195 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700196 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100197 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100198 }
199 },
200 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700201 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100202 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100203 }
204 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100205 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100206 } /*javaImpl*/
207 );
Yifan Hongffa91392017-01-31 13:41:23 -0800208 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100209}
Yifan Hongffa91392017-01-31 13:41:23 -0800210bool Interface::fillSyspropsChangedMethod(Method *method) const {
211 if (method->name() != "notifySyspropsChanged") {
212 return false;
213 }
214
215 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100216 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yi Kongc8ff4672017-04-30 23:46:56 -0700217 { { IMPL_INTERFACE, [](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100218 out << "::android::report_sysprop_change();\n";
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700219 out << "return ::android::hardware::Void();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100220 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700221 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700222 out << "android.os.HwBinder.enableInstrumentation();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100223 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100224 );
Yifan Hongffa91392017-01-31 13:41:23 -0800225 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700226}
227
Yifan Hongffa91392017-01-31 13:41:23 -0800228bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
229 if (method->name() != "setHALInstrumentation") {
230 return false;
231 }
232
233 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800234 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
235 {
Steven Moreland937408a2017-03-20 09:54:18 -0700236 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700237 [](auto &out) {
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800238 // do nothing for base class.
239 out << "return ::android::hardware::Void();\n";
240 }
241 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800242 {IMPL_STUB,
243 [](auto &out) {
244 out << "configureInstrumentation();\n";
245 }
246 },
247 {IMPL_PASSTHROUGH,
248 [](auto &out) {
249 out << "configureInstrumentation();\n";
250 out << "return ::android::hardware::Void();\n";
251 }
252 },
253 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700254 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800255 // Not support for Java Impl for now.
256 } } } /*javaImpl */
257 );
Yifan Hongffa91392017-01-31 13:41:23 -0800258 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800259}
260
Yifan Hongffa91392017-01-31 13:41:23 -0800261bool Interface::fillDescriptorChainMethod(Method *method) const {
262 if (method->name() != "interfaceChain") {
263 return false;
264 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700265
Yifan Hongffa91392017-01-31 13:41:23 -0800266 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700267 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700268 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700269 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800270 out << "_hidl_cb(";
271 out.block([&] {
272 for (const Interface *iface : chain) {
273 out << iface->fullName() << "::descriptor,\n";
274 }
275 });
276 out << ");\n";
Steven Moreland3768f422019-05-09 12:29:12 -0700277 out << "return ::android::hardware::Void();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100278 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700279 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700280 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800281 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700282 out.indent(); out.indent();
283 for (size_t i = 0; i < chain.size(); ++i) {
284 if (i != 0)
285 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800286 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700287 }
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700288 out << "));\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700289 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800290 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100291 );
Yifan Hongffa91392017-01-31 13:41:23 -0800292 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700293}
294
Steven Moreland04dea8d2018-02-06 13:11:24 -0800295void Interface::emitDigestChain(
Timur Iskhakov7296af12017-08-09 21:52:48 +0000296 Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
Steven Moreland04dea8d2018-02-06 13:11:24 -0800297 std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const {
298 out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700299 out << prefix;
300 out << "{";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800301 out.join(
302 iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",",
303 [&](const auto& e) {
304 // Use ConstantExpression::cppValue / javaValue
305 // because Java used signed byte for uint8_t.
306 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
307 });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700308 out << "} /* ";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800309 out << iface->getFileHash()->hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700310 out << " */";
311 });
312}
313
314bool Interface::fillHashChainMethod(Method *method) const {
315 if (method->name() != "getHashChain") {
316 return false;
317 }
318 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
319 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
320
321 method->fillImplementation(
322 HIDL_HASH_CHAIN_TRANSACTION,
323 { { IMPL_INTERFACE, [this, digestType](auto &out) {
324 std::vector<const Interface *> chain = typeChain();
325 out << "_hidl_cb(";
326 out.block([&] {
Timur Iskhakov7296af12017-08-09 21:52:48 +0000327 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain,
328 [](const auto& e) { return e->cppValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700329 });
330 out << ");\n";
331 out << "return ::android::hardware::Void();\n";
332 } } }, /* cppImpl */
333 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
334 std::vector<const Interface *> chain = typeChain();
335 out << "return new "
336 << chainType->getJavaType(false /* forInitializer */)
337 << "(java.util.Arrays.asList(\n";
338 out.indent(2, [&] {
339 // No need for dimensions when elements are explicitly provided.
340 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
Timur Iskhakov7296af12017-08-09 21:52:48 +0000341 chain, [](const auto& e) { return e->javaValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700342 });
343 out << "));\n";
344 } } } /* javaImpl */
345 );
346 return true;
347}
348
Yifan Hongffa91392017-01-31 13:41:23 -0800349bool Interface::fillGetDescriptorMethod(Method *method) const {
350 if (method->name() != "interfaceDescriptor") {
351 return false;
352 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800353
Yifan Hongffa91392017-01-31 13:41:23 -0800354 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800355 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700356 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800357 out << "_hidl_cb("
358 << fullName()
359 << "::descriptor);\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700360 << "return ::android::hardware::Void();\n";
Yifan Hongc75fd472017-01-11 12:37:31 -0800361 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700362 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800363 out << "return "
364 << fullJavaName()
365 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800366 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800367 );
Yifan Hongffa91392017-01-31 13:41:23 -0800368 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800369}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700370
Yifan Hongbcffce22017-02-01 15:52:06 -0800371bool Interface::fillGetDebugInfoMethod(Method *method) const {
372 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800373 return false;
374 }
375
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800376 static const std::string sArch =
377 "#if defined(__LP64__)\n"
378 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
379 "#else\n"
380 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
381 "#endif\n";
382
Yifan Hongcd2ae452017-01-31 14:33:40 -0800383 method->fillImplementation(
384 HIDL_GET_REF_INFO_TRANSACTION,
385 {
Steven Moreland937408a2017-03-20 09:54:18 -0700386 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700387 [](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800388 // getDebugInfo returns N/A for local objects.
Steven Moreland523b12c2019-04-23 16:03:18 -0700389 out << "::android::hidl::base::V1_0::DebugInfo info = {};\n";
390 out << "info.pid = -1;\n";
391 out << "info.ptr = 0;\n";
392 out << "info.arch = \n" << sArch << ";\n";
393 out << "_hidl_cb(info);\n";
394 out << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800395 }
396 },
397 {IMPL_STUB_IMPL,
Yi Kongc8ff4672017-04-30 23:46:56 -0700398 [](auto &out) {
Steven Moreland523b12c2019-04-23 16:03:18 -0700399 out << "::android::hidl::base::V1_0::DebugInfo info = {};\n";
400 out << "info.pid = ::android::hardware::details::getPidIfSharable();\n";
401 out << "info.ptr = ::android::hardware::details::debuggable()"
402 << "? reinterpret_cast<uint64_t>(this) : 0;\n";
403 out << "info.arch = \n" << sArch << ";\n";
404 out << "_hidl_cb(info);\n";
405 out << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800406 }
407 }
408 }, /* cppImpl */
Yi Kongc8ff4672017-04-30 23:46:56 -0700409 { { IMPL_INTERFACE, [method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800410 const Type &refInfo = method->results().front()->type();
411 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
412 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800413 << "info.pid = android.os.HidlSupport.getPidIfSharable();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800414 << "info.ptr = 0;\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800415 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700416 << "return info;\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800417 } } } /* javaImpl */
418 );
419
420 return true;
421}
422
Andreas Huber37065d62017-02-07 14:36:54 -0800423bool Interface::fillDebugMethod(Method *method) const {
424 if (method->name() != "debug") {
425 return false;
426 }
427
Steven Moreland327fd8b2018-08-10 15:40:41 -0700428 method->fillImplementation(HIDL_DEBUG_TRANSACTION,
429 {
430 {IMPL_INTERFACE,
431 [](auto& out) {
432 out << "(void)fd;\n"
433 << "(void)options;\n"
434 << "return ::android::hardware::Void();\n";
435 }},
436 }, /* cppImpl */
437 {
438 {IMPL_INTERFACE, [](auto& out) { out << "return;\n"; }},
439 } /* javaImpl */
Andreas Huber37065d62017-02-07 14:36:54 -0800440 );
441
442 return true;
443}
444
Neel Mehta0ee353f2019-05-30 17:40:29 -0700445void Interface::addUserDefinedMethod(Method* method) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700446 CHECK(!method->isHidlReserved());
Yifan Hong10fe0b52016-10-19 14:20:17 -0700447 mUserMethods.push_back(method);
Andreas Huberc9410c72016-07-28 12:18:40 -0700448}
449
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700450std::vector<const Reference<Type>*> Interface::getReferences() const {
451 std::vector<const Reference<Type>*> ret;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700452
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700453 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700454 ret.push_back(&mSuperType);
Timur Iskhakov33431e62017-08-21 17:31:23 -0700455 }
456
457 for (const auto* method : methods()) {
458 const auto& references = method->getReferences();
459 ret.insert(ret.end(), references.begin(), references.end());
460 }
461
462 return ret;
463}
464
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700465std::vector<const Reference<Type>*> Interface::getStrongReferences() const {
Timur Iskhakov40731af2017-08-24 14:18:35 -0700466 // Interface is a special case as a reference:
467 // its definiton must be completed for extension but
468 // not necessary for other references.
Timur Iskhakov40731af2017-08-24 14:18:35 -0700469
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700470 std::vector<const Reference<Type>*> ret;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700471 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700472 ret.push_back(&mSuperType);
Timur Iskhakov40731af2017-08-24 14:18:35 -0700473 }
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700474
475 for (const auto* method : methods()) {
476 const auto& references = method->getStrongReferences();
477 ret.insert(ret.end(), references.begin(), references.end());
478 }
479
Timur Iskhakov40731af2017-08-24 14:18:35 -0700480 return ret;
481}
482
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700483status_t Interface::resolveInheritance() {
484 size_t serial = FIRST_CALL_TRANSACTION;
485 for (const auto* ancestor : superTypeChain()) {
486 serial += ancestor->mUserMethods.size();
487 }
488
489 for (Method* method : mUserMethods) {
490 if (serial > LAST_CALL_TRANSACTION) {
491 std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION
492 << " methods (including super and reserved) are not allowed at " << location()
Steven Morelandcbff5612017-10-11 17:01:54 -0700493 << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700494 return UNKNOWN_ERROR;
495 }
496
497 method->setSerialId(serial);
498 serial++;
499 }
500
501 return Scope::resolveInheritance();
502}
503
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700504status_t Interface::validate() const {
505 CHECK(isIBase() == mSuperType.isEmptyReference());
506
Timur Iskhakov0344e612017-08-25 12:57:09 -0700507 if (!isIBase() && !mSuperType->isInterface()) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700508 std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location()
509 << std::endl;
Timur Iskhakov0344e612017-08-25 12:57:09 -0700510 return UNKNOWN_ERROR;
511 }
512
Steven Moreland368e4602018-02-16 14:21:49 -0800513 status_t err;
514
515 err = validateUniqueNames();
516 if (err != OK) return err;
517
518 err = validateAnnotations();
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700519 if (err != OK) return err;
520
521 return Scope::validate();
522}
523
Howard Chenecfb4512017-11-21 18:28:53 +0800524void Interface::getAlignmentAndSize(size_t* align, size_t* size) const {
525 *align = 8;
526 *size = 8;
527}
528
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700529status_t Interface::validateUniqueNames() const {
530 std::unordered_map<std::string, const Interface*> registeredMethodNames;
531 for (auto const& tuple : allSuperMethodsFromRoot()) {
532 // No need to check super method uniqueness
533 registeredMethodNames[tuple.method()->name()] = tuple.interface();
534 }
535
536 for (const Method* method : mUserMethods) {
537 auto registered = registeredMethodNames.find(method->name());
538
539 if (registered != registeredMethodNames.end()) {
540 const Interface* definedInType = registered->second;
541
542 if (definedInType == this) {
543 // Defined in this interface
544 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
545 } else if (definedInType->isIBase()) {
546 // Defined in IBase
547 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
548 } else {
549 // Defined in super not IBase
550 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700551 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700552 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700553 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700554 return UNKNOWN_ERROR;
555 }
556
557 registeredMethodNames[method->name()] = this;
558 }
559
560 return OK;
561}
562
Steven Moreland368e4602018-02-16 14:21:49 -0800563status_t Interface::validateAnnotations() const {
564 for (const Method* method : methods()) {
565 for (const Annotation* annotation : method->annotations()) {
566 const std::string name = annotation->name();
567
568 if (name == "entry" || name == "exit" || name == "callflow") {
569 continue;
570 }
571
572 std::cerr << "ERROR: Unrecognized annotation '" << name
573 << "' for method: " << method->name() << ". An annotation should be one of: "
574 << "entry, exit, callflow." << std::endl;
575 return UNKNOWN_ERROR;
576 }
577 }
578 return OK;
579}
580
Neel Mehta0ee353f2019-05-30 17:40:29 -0700581bool Interface::addAllReservedMethods(const std::map<std::string, Method*>& allReservedMethods) {
Yifan Hongffa91392017-01-31 13:41:23 -0800582 // use a sorted map to insert them in serial ID order.
583 std::map<int32_t, Method *> reservedMethodsById;
Neel Mehta0ee353f2019-05-30 17:40:29 -0700584 for (const auto& pair : allReservedMethods) {
Yifan Hongffa91392017-01-31 13:41:23 -0800585 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800586 bool fillSuccess = fillPingMethod(method)
587 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800588 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700589 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800590 || fillSyspropsChangedMethod(method)
591 || fillLinkToDeathMethod(method)
592 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800593 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800594 || fillGetDebugInfoMethod(method)
595 || fillDebugMethod(method);
596
Yifan Hongffa91392017-01-31 13:41:23 -0800597 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700598 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
599 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800600 return false;
601 }
602 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700603 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
604 << " and " << reservedMethodsById[method->getSerialId()]->name()
605 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800606 return false;
607 }
608 }
609 for (const auto &pair : reservedMethodsById) {
610 this->mReservedMethods.push_back(pair.second);
611 }
612 return true;
613}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700614
Timur Iskhakov505316c2017-08-05 03:38:59 +0000615const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700616 if (isIBase()) return nullptr;
617 if (!mSuperType->isInterface()) {
618 // This is actually an error
619 // that would be caught in validate
620 return nullptr;
621 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700622 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700623}
624
Yifan Hong10fe0b52016-10-19 14:20:17 -0700625std::vector<const Interface *> Interface::typeChain() const {
626 std::vector<const Interface *> v;
627 const Interface *iface = this;
628 while (iface != nullptr) {
629 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000630 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700631 }
632 return v;
633}
634
Yifan Hongfe95aa22016-10-19 17:26:45 -0700635std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000636 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700637}
638
Martijn Coenenb40ef022017-01-02 15:21:46 +0100639bool Interface::isElidableType() const {
640 return true;
641}
642
Andreas Hubera2723d22016-07-29 15:36:07 -0700643bool Interface::isInterface() const {
644 return true;
645}
646
Yifan Hong10fe0b52016-10-19 14:20:17 -0700647const std::vector<Method *> &Interface::userDefinedMethods() const {
648 return mUserMethods;
649}
650
651const std::vector<Method *> &Interface::hidlReservedMethods() const {
652 return mReservedMethods;
653}
654
655std::vector<Method *> Interface::methods() const {
656 std::vector<Method *> v(mUserMethods);
657 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
658 return v;
659}
660
661std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
662 std::vector<InterfaceAndMethod> v;
663 std::vector<const Interface *> chain = typeChain();
664 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
665 const Interface *iface = *it;
666 for (Method *userMethod : iface->userDefinedMethods()) {
667 v.push_back(InterfaceAndMethod(iface, userMethod));
668 }
669 }
670 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800671 v.push_back(InterfaceAndMethod(
672 *chain.rbegin(), // IBase
673 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700674 }
675 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700676}
677
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700678std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
679 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
680}
681
Steven Moreland40786312016-08-16 10:29:40 -0700682std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700683 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700684}
685
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700686std::string Interface::getAdapterName() const {
687 return fqName().getInterfaceAdapterName();
688}
689
Yifan Hongeefe4f22017-01-04 15:32:42 -0800690std::string Interface::getProxyName() const {
691 return fqName().getInterfaceProxyName();
692}
693
694std::string Interface::getStubName() const {
695 return fqName().getInterfaceStubName();
696}
697
698std::string Interface::getHwName() const {
699 return fqName().getInterfaceHwName();
700}
701
702std::string Interface::getPassthroughName() const {
703 return fqName().getInterfacePassthroughName();
704}
705
Yifan Hong51a65092017-01-04 15:41:44 -0800706FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800707 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800708}
709
Yifan Hong51a65092017-01-04 15:41:44 -0800710FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800711 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800712}
713
Yifan Hong51a65092017-01-04 15:41:44 -0800714FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800715 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800716}
717
Steven Moreland979e0992016-09-07 09:18:08 -0700718std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700719 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700720 const std::string base =
721 std::string(specifyNamespaces ? "::android::" : "")
722 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700723 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700724 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700725
726 switch (mode) {
727 case StorageMode_Stack:
728 case StorageMode_Result:
729 return base;
730
731 case StorageMode_Argument:
732 return "const " + base + "&";
733 }
734}
735
Yifan Hong4ed13472016-11-02 10:44:11 -0700736std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700737 return fullJavaName();
738}
739
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800740std::string Interface::getVtsType() const {
Neel Mehta257cce02019-07-19 13:24:57 -0700741 if (StringHelper::EndsWith(definedName(), "Callback")) {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800742 return "TYPE_HIDL_CALLBACK";
743 } else {
744 return "TYPE_HIDL_INTERFACE";
745 }
746}
747
Andreas Huber881227d2016-08-02 14:20:21 -0700748void Interface::emitReaderWriter(
749 Formatter &out,
750 const std::string &name,
751 const std::string &parcelObj,
752 bool parcelObjIsPointer,
753 bool isReader,
754 ErrorMode mode) const {
755 const std::string parcelObjDeref =
756 parcelObj + (parcelObjIsPointer ? "->" : ".");
757
758 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700759 out << "{\n";
760 out.indent();
761
Howard Chenecfb4512017-11-21 18:28:53 +0800762 const std::string binderName = "_hidl_binder";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700763 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700764 << binderName << ";\n";
765
Iliyan Malchev549e2592016-08-10 08:59:12 -0700766 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700767 out << parcelObjDeref
768 << "readNullableStrongBinder(&"
769 << binderName
770 << ");\n";
771
772 handleError(out, mode);
773
774 out << name
775 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100776 << "::android::hardware::fromBinder<"
777 << fqName().cppName()
778 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800779 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100780 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800781 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100782 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700783 << binderName
784 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700785
786 out.unindent();
787 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700788 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200789 out << "if (" << name << " == nullptr) {\n";
790 out.indent();
791 out << "_hidl_err = ";
792 out << parcelObjDeref
793 << "writeStrongBinder(nullptr);\n";
794 out.unindent();
795 out << "} else {\n";
796 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800797 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
Steven Moreland731dbb12018-12-03 18:09:46 -0800798 << "::android::hardware::getOrCreateCachedBinder(" << name << ".get());\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800799 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800800 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800801 out << "_hidl_err = "
802 << parcelObjDeref
803 << "writeStrongBinder(_hidl_binder);\n";
804 });
805 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800806 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800807 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
808 });
809 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200810 out.unindent();
811 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700812
Andreas Huber881227d2016-08-02 14:20:21 -0700813 handleError(out, mode);
814 }
815}
816
Neel Mehta3b414a82019-07-02 15:47:48 -0700817void Interface::emitHidlDefinition(Formatter& out) const {
818 if (getDocComment() != nullptr) getDocComment()->emit(out);
819 out << typeName() << " ";
820
821 const Interface* super = superType();
822 if (super != nullptr && !super->isIBase()) {
823 out << "extends " << super->fqName().getRelativeFQName(fqName()) << " ";
824 }
825
Neel Mehtac554d422019-08-09 16:57:02 -0700826 out << "{";
Neel Mehta3b414a82019-07-02 15:47:48 -0700827
828 out.indent([&] {
829 const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
Neel Mehtac554d422019-08-09 16:57:02 -0700830 if (definedTypes.size() > 0 || userDefinedMethods().size() > 0) out << "\n";
831
Neel Mehta3b414a82019-07-02 15:47:48 -0700832 out.join(definedTypes.begin(), definedTypes.end(), "\n",
833 [&](auto t) { t->emitHidlDefinition(out); });
834
835 if (definedTypes.size() > 0 && userDefinedMethods().size() > 0) out << "\n";
836
837 out.join(userDefinedMethods().begin(), userDefinedMethods().end(), "\n",
838 [&](auto method) { method->emitHidlDefinition(out); });
839 });
840
841 out << "};\n";
842}
843
Steven Moreland368e4602018-02-16 14:21:49 -0800844void Interface::emitPackageTypeDeclarations(Formatter& out) const {
845 Scope::emitPackageTypeDeclarations(out);
Steven Morelandbf714212017-10-27 18:29:01 -0700846
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700847 out << "static inline std::string toString(" << getCppArgumentType() << " o);\n\n";
848}
849
850void Interface::emitPackageTypeHeaderDefinitions(Formatter& out) const {
851 Scope::emitPackageTypeHeaderDefinitions(out);
852
Steven Morelandbf714212017-10-27 18:29:01 -0700853 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
854
855 out.block([&] {
856 out << "std::string os = \"[class or subclass of \";\n"
857 << "os += " << fullName() << "::descriptor;\n"
858 << "os += \"]\";\n"
859 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
860 << "return os;\n";
861 }).endl().endl();
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800862}
863
Steven Moreland368e4602018-02-16 14:21:49 -0800864void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800865 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Morelandd8c7a292017-10-27 18:32:06 -0700866
Neel Mehta257cce02019-07-19 13:24:57 -0700867 Scope::emitTypeDefinitions(out, space + definedName());
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800868}
869
Andreas Huber2831d512016-08-15 09:33:47 -0700870void Interface::emitJavaReaderWriter(
871 Formatter &out,
872 const std::string &parcelObj,
873 const std::string &argName,
874 bool isReader) const {
875 if (isReader) {
876 out << fullJavaName()
877 << ".asInterface("
878 << parcelObj
879 << ".readStrongBinder());\n";
880 } else {
881 out << parcelObj
882 << ".writeStrongBinder("
883 << argName
884 << " == null ? null : "
885 << argName
886 << ".asBinder());\n";
887 }
888}
889
Steven Moreland368e4602018-02-16 14:21:49 -0800890void Interface::emitVtsAttributeDeclaration(Formatter& out) const {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700891 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700892 // Skip for TypeDef as it is just an alias of a defined type.
893 if (type->isTypeDef()) {
894 continue;
895 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700896 out << "attribute: {\n";
897 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800898 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700899 out.unindent();
900 out << "}\n\n";
901 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700902}
903
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700904void Interface::emitVtsMethodDeclaration(Formatter& out, bool isInherited) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700905 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800906 if (method->isHidlReserved()) {
907 continue;
908 }
909
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700910 out << "api: {\n";
911 out.indent();
912 out << "name: \"" << method->name() << "\"\n";
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700913 out << "is_inherited: " << (isInherited ? "true" : "false") << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700914 // Generate declaration for each return value.
915 for (const auto &result : method->results()) {
916 out << "return_type_hidl: {\n";
917 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700918 out << "name: \"" << result->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800919 result->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700920 out.unindent();
921 out << "}\n";
922 }
923 // Generate declaration for each input argument
924 for (const auto &arg : method->args()) {
925 out << "arg: {\n";
926 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700927 out << "name: \"" << arg->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800928 arg->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700929 out.unindent();
930 out << "}\n";
931 }
932 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700933 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700934 out << "callflow: {\n";
935 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800936 const std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700937 if (name == "entry") {
938 out << "entry: true\n";
939 } else if (name == "exit") {
940 out << "exit: true\n";
941 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700942 const AnnotationParam *param =
943 annotation->getParam("next");
944 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700945 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700946 out << "next: " << value << "\n";
947 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700948 }
949 } else {
Steven Moreland368e4602018-02-16 14:21:49 -0800950 CHECK(false);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700951 }
952 out.unindent();
953 out << "}\n";
954 }
955 out.unindent();
956 out << "}\n\n";
957 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700958}
959
Steven Moreland368e4602018-02-16 14:21:49 -0800960void Interface::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800961 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700962 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800963 << fullName()
964 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700965}
966
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700967bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
968 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700969 return false;
970 }
971
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700972 for (const auto* method : methods()) {
973 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700974 return false;
975 }
976 }
977
Steven Morelandb0da6fc2018-10-16 17:15:11 -0700978 return Scope::deepIsJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -0700979}
980
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700981bool Interface::isNeverStrongReference() const {
982 return true;
983}
984
Steven Morelandad81efd2020-02-26 13:15:10 -0800985const FQName gIBaseFqName = FQName("android.hidl.base", "1.0", "IBase");
986const FQName gIManagerFqName = FQName("android.hidl.manager", "1.0", "IServiceManager");
987
Andreas Huberc9410c72016-07-28 12:18:40 -0700988} // namespace android
989