blob: 9e53e1fca58438aabbca5b123fe333faebd20f28 [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 },
Steven Moreland424a9482017-02-13 19:20:40 -0800108 } /*javaImpl*/
109 );
110
111 return true;
112}
113
Yifan Hongffa91392017-01-31 13:41:23 -0800114bool Interface::fillLinkToDeathMethod(Method *method) const {
115 if (method->name() != "linkToDeath") {
116 return false;
117 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100118
Yifan Hongffa91392017-01-31 13:41:23 -0800119 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100120 HIDL_LINK_TO_DEATH_TRANSACTION,
121 {
Steven Moreland937408a2017-03-20 09:54:18 -0700122 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100123 [](auto &out) {
124 out << "(void)cookie;\n"
125 << "return (recipient != nullptr);\n";
126 }
127 },
128 {IMPL_PROXY,
129 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100130 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100131 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
132 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
133 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
134 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
135 << "return (remote()->linkToDeath(binder_recipient)"
136 << " == ::android::OK);\n";
137 }
138 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100139 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100140 }, /*cppImpl*/
141 {
Steven Moreland937408a2017-03-20 09:54:18 -0700142 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700143 [](auto &out) {
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700144 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100145 }
146 },
147 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700148 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100149 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100150 }
151 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100152 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100153 } /*javaImpl*/
154 );
Yifan Hongffa91392017-01-31 13:41:23 -0800155 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100156}
157
Yifan Hongffa91392017-01-31 13:41:23 -0800158bool Interface::fillUnlinkToDeathMethod(Method *method) const {
159 if (method->name() != "unlinkToDeath") {
160 return false;
161 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100162
Yifan Hongffa91392017-01-31 13:41:23 -0800163 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100164 HIDL_UNLINK_TO_DEATH_TRANSACTION,
165 {
Steven Moreland937408a2017-03-20 09:54:18 -0700166 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100167 [](auto &out) {
168 out << "return (recipient != nullptr);\n";
169 }
170 },
171 {IMPL_PROXY,
172 [](auto &out) {
173 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
174 << "for (auto it = _hidl_mDeathRecipients.begin();"
175 << "it != _hidl_mDeathRecipients.end();"
176 << "++it) {\n";
177 out.indent([&] {
178 out.sIf("(*it)->getRecipient() == recipient", [&] {
179 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
180 << "_hidl_mDeathRecipients.erase(it);\n"
181 << "return status == ::android::OK;\n";
182 });
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700183 }).endl();
Martijn Coenen115d4282016-12-19 05:14:04 +0100184 out << "}\n";
185 out << "return false;\n";
186 }
187 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100188 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100189 }, /*cppImpl*/
190 {
Steven Moreland937408a2017-03-20 09:54:18 -0700191 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700192 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100193 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100194 }
195 },
196 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700197 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100198 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100199 }
200 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100201 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100202 } /*javaImpl*/
203 );
Yifan Hongffa91392017-01-31 13:41:23 -0800204 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100205}
Yifan Hongffa91392017-01-31 13:41:23 -0800206bool Interface::fillSyspropsChangedMethod(Method *method) const {
207 if (method->name() != "notifySyspropsChanged") {
208 return false;
209 }
210
211 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100212 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yi Kongc8ff4672017-04-30 23:46:56 -0700213 { { IMPL_INTERFACE, [](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100214 out << "::android::report_sysprop_change();\n";
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700215 out << "return ::android::hardware::Void();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100216 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700217 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700218 out << "android.os.HwBinder.enableInstrumentation();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100219 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100220 );
Yifan Hongffa91392017-01-31 13:41:23 -0800221 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700222}
223
Yifan Hongffa91392017-01-31 13:41:23 -0800224bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
225 if (method->name() != "setHALInstrumentation") {
226 return false;
227 }
228
229 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800230 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
231 {
Steven Moreland937408a2017-03-20 09:54:18 -0700232 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700233 [](auto &out) {
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800234 // do nothing for base class.
235 out << "return ::android::hardware::Void();\n";
236 }
237 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800238 {IMPL_STUB,
239 [](auto &out) {
240 out << "configureInstrumentation();\n";
241 }
242 },
243 {IMPL_PASSTHROUGH,
244 [](auto &out) {
245 out << "configureInstrumentation();\n";
246 out << "return ::android::hardware::Void();\n";
247 }
248 },
249 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700250 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800251 // Not support for Java Impl for now.
252 } } } /*javaImpl */
253 );
Yifan Hongffa91392017-01-31 13:41:23 -0800254 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800255}
256
Yifan Hongffa91392017-01-31 13:41:23 -0800257bool Interface::fillDescriptorChainMethod(Method *method) const {
258 if (method->name() != "interfaceChain") {
259 return false;
260 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700261
Yifan Hongffa91392017-01-31 13:41:23 -0800262 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700263 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700264 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700265 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800266 out << "_hidl_cb(";
267 out.block([&] {
268 for (const Interface *iface : chain) {
269 out << iface->fullName() << "::descriptor,\n";
270 }
271 });
272 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700273 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100274 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700275 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700276 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800277 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700278 out.indent(); out.indent();
279 for (size_t i = 0; i < chain.size(); ++i) {
280 if (i != 0)
281 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800282 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700283 }
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700284 out << "));\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700285 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800286 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100287 );
Yifan Hongffa91392017-01-31 13:41:23 -0800288 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700289}
290
Steven Moreland04dea8d2018-02-06 13:11:24 -0800291void Interface::emitDigestChain(
Timur Iskhakov7296af12017-08-09 21:52:48 +0000292 Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
Steven Moreland04dea8d2018-02-06 13:11:24 -0800293 std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const {
294 out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700295 out << prefix;
296 out << "{";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800297 out.join(
298 iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",",
299 [&](const auto& e) {
300 // Use ConstantExpression::cppValue / javaValue
301 // because Java used signed byte for uint8_t.
302 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
303 });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700304 out << "} /* ";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800305 out << iface->getFileHash()->hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700306 out << " */";
307 });
308}
309
310bool Interface::fillHashChainMethod(Method *method) const {
311 if (method->name() != "getHashChain") {
312 return false;
313 }
314 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
315 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
316
317 method->fillImplementation(
318 HIDL_HASH_CHAIN_TRANSACTION,
319 { { IMPL_INTERFACE, [this, digestType](auto &out) {
320 std::vector<const Interface *> chain = typeChain();
321 out << "_hidl_cb(";
322 out.block([&] {
Timur Iskhakov7296af12017-08-09 21:52:48 +0000323 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain,
324 [](const auto& e) { return e->cppValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700325 });
326 out << ");\n";
327 out << "return ::android::hardware::Void();\n";
328 } } }, /* cppImpl */
329 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
330 std::vector<const Interface *> chain = typeChain();
331 out << "return new "
332 << chainType->getJavaType(false /* forInitializer */)
333 << "(java.util.Arrays.asList(\n";
334 out.indent(2, [&] {
335 // No need for dimensions when elements are explicitly provided.
336 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
Timur Iskhakov7296af12017-08-09 21:52:48 +0000337 chain, [](const auto& e) { return e->javaValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700338 });
339 out << "));\n";
340 } } } /* javaImpl */
341 );
342 return true;
343}
344
Yifan Hongffa91392017-01-31 13:41:23 -0800345bool Interface::fillGetDescriptorMethod(Method *method) const {
346 if (method->name() != "interfaceDescriptor") {
347 return false;
348 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800349
Yifan Hongffa91392017-01-31 13:41:23 -0800350 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800351 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700352 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800353 out << "_hidl_cb("
354 << fullName()
355 << "::descriptor);\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700356 << "return ::android::hardware::Void();\n";
Yifan Hongc75fd472017-01-11 12:37:31 -0800357 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700358 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800359 out << "return "
360 << fullJavaName()
361 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800362 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800363 );
Yifan Hongffa91392017-01-31 13:41:23 -0800364 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800365}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700366
Yifan Hongbcffce22017-02-01 15:52:06 -0800367bool Interface::fillGetDebugInfoMethod(Method *method) const {
368 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800369 return false;
370 }
371
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800372 static const std::string sArch =
373 "#if defined(__LP64__)\n"
374 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
375 "#else\n"
376 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
377 "#endif\n";
378
Yifan Hongcd2ae452017-01-31 14:33:40 -0800379 method->fillImplementation(
380 HIDL_GET_REF_INFO_TRANSACTION,
381 {
Steven Moreland937408a2017-03-20 09:54:18 -0700382 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700383 [](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800384 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800385 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
386 << sArch
387 << "});\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700388 << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800389 }
390 },
391 {IMPL_STUB_IMPL,
Yi Kongc8ff4672017-04-30 23:46:56 -0700392 [](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800393 out << "_hidl_cb(";
394 out.block([&] {
Yifan Hong96bb0632017-11-14 16:08:37 -0800395 out << "::android::hardware::details::getPidIfSharable(),\n"
Yifan Hong2e036c92017-03-07 13:18:12 -0800396 << "::android::hardware::details::debuggable()"
397 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
398 << sArch << "\n";
399 });
400 out << ");\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700401 << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800402 }
403 }
404 }, /* cppImpl */
Yi Kongc8ff4672017-04-30 23:46:56 -0700405 { { IMPL_INTERFACE, [method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800406 const Type &refInfo = method->results().front()->type();
407 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
408 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800409 << "info.pid = android.os.HidlSupport.getPidIfSharable();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800410 << "info.ptr = 0;\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800411 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700412 << "return info;\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800413 } } } /* javaImpl */
414 );
415
416 return true;
417}
418
Andreas Huber37065d62017-02-07 14:36:54 -0800419bool Interface::fillDebugMethod(Method *method) const {
420 if (method->name() != "debug") {
421 return false;
422 }
423
424 method->fillImplementation(
425 HIDL_DEBUG_TRANSACTION,
426 {
Steven Moreland937408a2017-03-20 09:54:18 -0700427 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700428 [](auto &out) {
Andreas Huber37065d62017-02-07 14:36:54 -0800429 out << "(void)fd;\n"
430 << "(void)options;\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700431 << "return ::android::hardware::Void();\n";
Andreas Huber37065d62017-02-07 14:36:54 -0800432 }
433 },
434 }, /* cppImpl */
435 {
436 /* unused, as the debug method is hidden from Java */
437 } /* javaImpl */
438 );
439
440 return true;
441}
442
Yifan Hongffa91392017-01-31 13:41:23 -0800443static std::map<std::string, Method *> gAllReservedMethods;
444
Steven Moreland14ee6742016-10-18 12:58:28 -0700445bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800446 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800447 if (!gAllReservedMethods.emplace(method->name(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700448 std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name()
449 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800450 return false;
451 }
452 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800453 return true;
454 }
455
Yifan Hong10fe0b52016-10-19 14:20:17 -0700456 CHECK(!method->isHidlReserved());
Yifan Hong10fe0b52016-10-19 14:20:17 -0700457 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700458
459 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700460}
461
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700462std::vector<const Reference<Type>*> Interface::getReferences() const {
463 std::vector<const Reference<Type>*> ret;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700464
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700465 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700466 ret.push_back(&mSuperType);
Timur Iskhakov33431e62017-08-21 17:31:23 -0700467 }
468
469 for (const auto* method : methods()) {
470 const auto& references = method->getReferences();
471 ret.insert(ret.end(), references.begin(), references.end());
472 }
473
474 return ret;
475}
476
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700477std::vector<const ConstantExpression*> Interface::getConstantExpressions() const {
478 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700479 for (const auto* method : methods()) {
480 const auto& retMethod = method->getConstantExpressions();
481 ret.insert(ret.end(), retMethod.begin(), retMethod.end());
482 }
483 return ret;
484}
485
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700486std::vector<const Reference<Type>*> Interface::getStrongReferences() const {
Timur Iskhakov40731af2017-08-24 14:18:35 -0700487 // Interface is a special case as a reference:
488 // its definiton must be completed for extension but
489 // not necessary for other references.
Timur Iskhakov40731af2017-08-24 14:18:35 -0700490
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700491 std::vector<const Reference<Type>*> ret;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700492 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700493 ret.push_back(&mSuperType);
Timur Iskhakov40731af2017-08-24 14:18:35 -0700494 }
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700495
496 for (const auto* method : methods()) {
497 const auto& references = method->getStrongReferences();
498 ret.insert(ret.end(), references.begin(), references.end());
499 }
500
Timur Iskhakov40731af2017-08-24 14:18:35 -0700501 return ret;
502}
503
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700504status_t Interface::resolveInheritance() {
505 size_t serial = FIRST_CALL_TRANSACTION;
506 for (const auto* ancestor : superTypeChain()) {
507 serial += ancestor->mUserMethods.size();
508 }
509
510 for (Method* method : mUserMethods) {
511 if (serial > LAST_CALL_TRANSACTION) {
512 std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION
513 << " methods (including super and reserved) are not allowed at " << location()
Steven Morelandcbff5612017-10-11 17:01:54 -0700514 << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700515 return UNKNOWN_ERROR;
516 }
517
518 method->setSerialId(serial);
519 serial++;
520 }
521
522 return Scope::resolveInheritance();
523}
524
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700525status_t Interface::validate() const {
526 CHECK(isIBase() == mSuperType.isEmptyReference());
527
Timur Iskhakov0344e612017-08-25 12:57:09 -0700528 if (!isIBase() && !mSuperType->isInterface()) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700529 std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location()
530 << std::endl;
Timur Iskhakov0344e612017-08-25 12:57:09 -0700531 return UNKNOWN_ERROR;
532 }
533
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800534 status_t err;
535
536 err = validateUniqueNames();
537 if (err != OK) return err;
538
539 err = validateAnnotations();
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700540 if (err != OK) return err;
541
542 return Scope::validate();
543}
544
Howard Chenecfb4512017-11-21 18:28:53 +0800545void Interface::getAlignmentAndSize(size_t* align, size_t* size) const {
546 *align = 8;
547 *size = 8;
548}
549
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700550status_t Interface::validateUniqueNames() const {
551 std::unordered_map<std::string, const Interface*> registeredMethodNames;
552 for (auto const& tuple : allSuperMethodsFromRoot()) {
553 // No need to check super method uniqueness
554 registeredMethodNames[tuple.method()->name()] = tuple.interface();
555 }
556
557 for (const Method* method : mUserMethods) {
558 auto registered = registeredMethodNames.find(method->name());
559
560 if (registered != registeredMethodNames.end()) {
561 const Interface* definedInType = registered->second;
562
563 if (definedInType == this) {
564 // Defined in this interface
565 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
566 } else if (definedInType->isIBase()) {
567 // Defined in IBase
568 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
569 } else {
570 // Defined in super not IBase
571 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700572 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700573 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700574 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700575 return UNKNOWN_ERROR;
576 }
577
578 registeredMethodNames[method->name()] = this;
579 }
580
581 return OK;
582}
583
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800584status_t Interface::validateAnnotations() const {
585 for (const Method* method : methods()) {
586 for (const Annotation* annotation : method->annotations()) {
587 const std::string name = annotation->name();
588
589 if (name == "entry" || name == "exit" || name == "callflow") {
590 continue;
591 }
592
593 std::cerr << "ERROR: Unrecognized annotation '" << name
594 << "' for method: " << method->name() << ". An annotation should be one of: "
595 << "entry, exit, callflow." << std::endl;
596 return UNKNOWN_ERROR;
597 }
598 }
599 return OK;
600}
601
Yifan Hongffa91392017-01-31 13:41:23 -0800602bool Interface::addAllReservedMethods() {
603 // use a sorted map to insert them in serial ID order.
604 std::map<int32_t, Method *> reservedMethodsById;
605 for (const auto &pair : gAllReservedMethods) {
606 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800607 bool fillSuccess = fillPingMethod(method)
608 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800609 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700610 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800611 || fillSyspropsChangedMethod(method)
612 || fillLinkToDeathMethod(method)
613 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800614 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800615 || fillGetDebugInfoMethod(method)
616 || fillDebugMethod(method);
617
Yifan Hongffa91392017-01-31 13:41:23 -0800618 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700619 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
620 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800621 return false;
622 }
623 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700624 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
625 << " and " << reservedMethodsById[method->getSerialId()]->name()
626 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800627 return false;
628 }
629 }
630 for (const auto &pair : reservedMethodsById) {
631 this->mReservedMethods.push_back(pair.second);
632 }
633 return true;
634}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700635
Timur Iskhakov505316c2017-08-05 03:38:59 +0000636const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700637 if (isIBase()) return nullptr;
638 if (!mSuperType->isInterface()) {
639 // This is actually an error
640 // that would be caught in validate
641 return nullptr;
642 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700643 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700644}
645
Yifan Hong10fe0b52016-10-19 14:20:17 -0700646std::vector<const Interface *> Interface::typeChain() const {
647 std::vector<const Interface *> v;
648 const Interface *iface = this;
649 while (iface != nullptr) {
650 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000651 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700652 }
653 return v;
654}
655
Yifan Hongfe95aa22016-10-19 17:26:45 -0700656std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000657 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700658}
659
Martijn Coenenb40ef022017-01-02 15:21:46 +0100660bool Interface::isElidableType() const {
661 return true;
662}
663
Andreas Hubera2723d22016-07-29 15:36:07 -0700664bool Interface::isInterface() const {
665 return true;
666}
667
Andreas Huber295ad302016-08-16 11:35:00 -0700668bool Interface::isBinder() const {
669 return true;
670}
671
Yifan Hong10fe0b52016-10-19 14:20:17 -0700672const std::vector<Method *> &Interface::userDefinedMethods() const {
673 return mUserMethods;
674}
675
676const std::vector<Method *> &Interface::hidlReservedMethods() const {
677 return mReservedMethods;
678}
679
680std::vector<Method *> Interface::methods() const {
681 std::vector<Method *> v(mUserMethods);
682 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
683 return v;
684}
685
686std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
687 std::vector<InterfaceAndMethod> v;
688 std::vector<const Interface *> chain = typeChain();
689 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
690 const Interface *iface = *it;
691 for (Method *userMethod : iface->userDefinedMethods()) {
692 v.push_back(InterfaceAndMethod(iface, userMethod));
693 }
694 }
695 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800696 v.push_back(InterfaceAndMethod(
697 *chain.rbegin(), // IBase
698 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700699 }
700 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700701}
702
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700703std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
704 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
705}
706
Steven Moreland40786312016-08-16 10:29:40 -0700707std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700708 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700709}
710
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700711std::string Interface::getAdapterName() const {
712 return fqName().getInterfaceAdapterName();
713}
714
Yifan Hongeefe4f22017-01-04 15:32:42 -0800715std::string Interface::getProxyName() const {
716 return fqName().getInterfaceProxyName();
717}
718
719std::string Interface::getStubName() const {
720 return fqName().getInterfaceStubName();
721}
722
723std::string Interface::getHwName() const {
724 return fqName().getInterfaceHwName();
725}
726
727std::string Interface::getPassthroughName() const {
728 return fqName().getInterfacePassthroughName();
729}
730
Yifan Hong51a65092017-01-04 15:41:44 -0800731FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800732 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800733}
734
Yifan Hong51a65092017-01-04 15:41:44 -0800735FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800736 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800737}
738
Yifan Hong51a65092017-01-04 15:41:44 -0800739FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800740 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800741}
742
Steven Moreland979e0992016-09-07 09:18:08 -0700743std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700744 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700745 const std::string base =
746 std::string(specifyNamespaces ? "::android::" : "")
747 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700748 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700749 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700750
751 switch (mode) {
752 case StorageMode_Stack:
753 case StorageMode_Result:
754 return base;
755
756 case StorageMode_Argument:
757 return "const " + base + "&";
758 }
759}
760
Yifan Hong4ed13472016-11-02 10:44:11 -0700761std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700762 return fullJavaName();
763}
764
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800765std::string Interface::getVtsType() const {
766 if (StringHelper::EndsWith(localName(), "Callback")) {
767 return "TYPE_HIDL_CALLBACK";
768 } else {
769 return "TYPE_HIDL_INTERFACE";
770 }
771}
772
Andreas Huber881227d2016-08-02 14:20:21 -0700773void Interface::emitReaderWriter(
774 Formatter &out,
775 const std::string &name,
776 const std::string &parcelObj,
777 bool parcelObjIsPointer,
778 bool isReader,
779 ErrorMode mode) const {
780 const std::string parcelObjDeref =
781 parcelObj + (parcelObjIsPointer ? "->" : ".");
782
783 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700784 out << "{\n";
785 out.indent();
786
Howard Chenecfb4512017-11-21 18:28:53 +0800787 const std::string binderName = "_hidl_binder";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700788 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700789 << binderName << ";\n";
790
Iliyan Malchev549e2592016-08-10 08:59:12 -0700791 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700792 out << parcelObjDeref
793 << "readNullableStrongBinder(&"
794 << binderName
795 << ");\n";
796
797 handleError(out, mode);
798
799 out << name
800 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100801 << "::android::hardware::fromBinder<"
802 << fqName().cppName()
803 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800804 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100805 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800806 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100807 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700808 << binderName
809 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700810
811 out.unindent();
812 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700813 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200814 out << "if (" << name << " == nullptr) {\n";
815 out.indent();
816 out << "_hidl_err = ";
817 out << parcelObjDeref
818 << "writeStrongBinder(nullptr);\n";
819 out.unindent();
820 out << "} else {\n";
821 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800822 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
823 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800824 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100825 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800826 << ">("
827 << name
828 << ");\n";
829 });
830 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800831 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800832 out << "_hidl_err = "
833 << parcelObjDeref
834 << "writeStrongBinder(_hidl_binder);\n";
835 });
836 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800837 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800838 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
839 });
840 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200841 out.unindent();
842 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700843
Andreas Huber881227d2016-08-02 14:20:21 -0700844 handleError(out, mode);
845 }
846}
847
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800848void Interface::emitPackageTypeDeclarations(Formatter& out) const {
849 Scope::emitPackageTypeDeclarations(out);
Steven Morelandbf714212017-10-27 18:29:01 -0700850
851 // TODO(b/65200821): remove these ifndefs
852 out << "#ifdef REALLY_IS_HIDL_INTERNAL_LIB" << gCurrentCompileName << "\n";
Steven Moreland3d98bc42017-06-23 21:36:41 +0000853 out << "std::string toString("
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800854 << getCppArgumentType()
Steven Moreland3d98bc42017-06-23 21:36:41 +0000855 << ");\n";
Steven Morelandbf714212017-10-27 18:29:01 -0700856 out << "#else\n";
857 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
858
859 out.block([&] {
860 out << "std::string os = \"[class or subclass of \";\n"
861 << "os += " << fullName() << "::descriptor;\n"
862 << "os += \"]\";\n"
863 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
864 << "return os;\n";
865 }).endl().endl();
866 out << "#endif // REALLY_IS_HIDL_INTERNAL_LIB\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800867}
868
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800869void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800870 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800871 Scope::emitTypeDefinitions(out, space + localName());
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800872
Steven Morelandbf714212017-10-27 18:29:01 -0700873 // TODO(b/65200821): remove toString from .cpp once all prebuilts are rebuilt
Steven Moreland3d98bc42017-06-23 21:36:41 +0000874 out << "std::string toString("
875 << getCppArgumentType()
876 << " o) ";
877
878 out.block([&] {
879 out << "std::string os = \"[class or subclass of \";\n"
880 << "os += " << fullName() << "::descriptor;\n"
881 << "os += \"]\";\n"
882 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
883 << "return os;\n";
884 }).endl().endl();
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800885}
886
Andreas Huber2831d512016-08-15 09:33:47 -0700887void Interface::emitJavaReaderWriter(
888 Formatter &out,
889 const std::string &parcelObj,
890 const std::string &argName,
891 bool isReader) const {
892 if (isReader) {
893 out << fullJavaName()
894 << ".asInterface("
895 << parcelObj
896 << ".readStrongBinder());\n";
897 } else {
898 out << parcelObj
899 << ".writeStrongBinder("
900 << argName
901 << " == null ? null : "
902 << argName
903 << ".asBinder());\n";
904 }
905}
906
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800907void Interface::emitVtsAttributeDeclaration(Formatter& out) const {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700908 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700909 // Skip for TypeDef as it is just an alias of a defined type.
910 if (type->isTypeDef()) {
911 continue;
912 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700913 out << "attribute: {\n";
914 out.indent();
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800915 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700916 out.unindent();
917 out << "}\n\n";
918 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700919}
920
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800921void Interface::emitVtsMethodDeclaration(Formatter& out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700922 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800923 if (method->isHidlReserved()) {
924 continue;
925 }
926
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700927 out << "api: {\n";
928 out.indent();
929 out << "name: \"" << method->name() << "\"\n";
930 // Generate declaration for each return value.
931 for (const auto &result : method->results()) {
932 out << "return_type_hidl: {\n";
933 out.indent();
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800934 result->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700935 out.unindent();
936 out << "}\n";
937 }
938 // Generate declaration for each input argument
939 for (const auto &arg : method->args()) {
940 out << "arg: {\n";
941 out.indent();
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800942 arg->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700943 out.unindent();
944 out << "}\n";
945 }
946 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700947 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700948 out << "callflow: {\n";
949 out.indent();
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800950 const std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700951 if (name == "entry") {
952 out << "entry: true\n";
953 } else if (name == "exit") {
954 out << "exit: true\n";
955 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700956 const AnnotationParam *param =
957 annotation->getParam("next");
958 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700959 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700960 out << "next: " << value << "\n";
961 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700962 }
963 } else {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800964 CHECK(false);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700965 }
966 out.unindent();
967 out << "}\n";
968 }
969 out.unindent();
970 out << "}\n\n";
971 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700972}
973
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800974void Interface::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800975 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700976 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800977 << fullName()
978 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700979}
980
Steven Moreland69e7c702016-09-09 11:16:32 -0700981bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700982 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700983 if (method->isOneway()) {
984 return true;
985 }
986 }
987
988 const Interface* superClass = superType();
989
990 if (superClass != nullptr) {
991 return superClass->hasOnewayMethods();
992 }
993
994 return false;
995}
996
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700997bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
998 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700999 return false;
1000 }
1001
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001002 for (const auto* method : methods()) {
1003 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -07001004 return false;
1005 }
1006 }
1007
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -07001008 return Scope::isJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -07001009}
1010
Timur Iskhakovff5e64a2017-09-11 14:56:18 -07001011bool Interface::isNeverStrongReference() const {
1012 return true;
1013}
1014
Andreas Huberc9410c72016-07-28 12:18:40 -07001015} // namespace android
1016