blob: 7183ba840f62a3ee93defad6f33ffdd1a1593e0d [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"
Martijn Coenen115d4282016-12-19 05:14:04 +010020#include "DeathRecipientType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070021#include "Method.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010022#include "ScalarType.h"
Yifan Hong10fe0b52016-10-19 14:20:17 -070023#include "StringType.h"
24#include "VectorType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070025
Steven Moreland14ee6742016-10-18 12:58:28 -070026#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070028#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070029#include <iostream>
Yifan Hong10fe0b52016-10-19 14:20:17 -070030#include <sstream>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070031
Andreas Huberc9410c72016-07-28 12:18:40 -070032namespace android {
33
Yifan Hong10fe0b52016-10-19 14:20:17 -070034/* It is very important that these values NEVER change. These values
35 * must remain unchanged over the lifetime of android. This is
36 * because the framework on a device will be updated independently of
37 * the hals on a device. If the hals are compiled with one set of
38 * transaction values, and the framework with another, then the
39 * interface between them will be destroyed, and the device will not
40 * work.
41 */
42enum {
43 // These values are defined in hardware::IBinder.
44 /////////////////// User defined transactions
45 FIRST_CALL_TRANSACTION = 0x00000001,
46 LAST_CALL_TRANSACTION = 0x00efffff,
47 /////////////////// HIDL reserved
48 FIRST_HIDL_TRANSACTION = 0x00f00000,
49 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = FIRST_HIDL_TRANSACTION,
Martijn Coenenaf712c02016-11-16 15:26:27 +010050 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +010051 HIDL_LINK_TO_DEATH_TRANSACTION,
52 HIDL_UNLINK_TO_DEATH_TRANSACTION,
Yifan Hong10fe0b52016-10-19 14:20:17 -070053 LAST_HIDL_TRANSACTION = 0x00ffffff,
54};
55
Yifan Honga4b53d02016-10-31 17:29:10 -070056Interface::Interface(const char *localName, const Location &location, Interface *super)
57 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070058 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070059 mIsJavaCompatibleInProgress(false) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070060 mReservedMethods.push_back(createDescriptorChainMethod());
Martijn Coenenaf712c02016-11-16 15:26:27 +010061 mReservedMethods.push_back(createSyspropsChangedMethod());
Martijn Coenen115d4282016-12-19 05:14:04 +010062 mReservedMethods.push_back(createLinkToDeathMethod());
63 mReservedMethods.push_back(createUnlinkToDeathMethod());
Martijn Coenenaf712c02016-11-16 15:26:27 +010064}
65
Steven Moreland30bb6a82016-11-30 09:18:34 -080066std::string Interface::typeName() const {
67 return "interface " + localName();
68}
69
Martijn Coenen115d4282016-12-19 05:14:04 +010070Method *Interface::createLinkToDeathMethod() const {
71 auto *results = new std::vector<TypedVar *> {
72 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
73 auto *args = new std::vector<TypedVar *> {
74 new TypedVar("recipient", new DeathRecipientType()),
75 new TypedVar("cookie", new ScalarType(ScalarType::KIND_UINT64)) };
76
77 return new Method("linkToDeath",
78 args,
79 results,
80 false /*oneway */,
81 new std::vector<Annotation *>(),
82 HIDL_LINK_TO_DEATH_TRANSACTION,
83 {
84 {IMPL_HEADER,
85 [](auto &out) {
86 out << "(void)cookie;\n"
87 << "return (recipient != nullptr);\n";
88 }
89 },
90 {IMPL_PROXY,
91 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010092 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +010093 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
94 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
95 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
96 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
97 << "return (remote()->linkToDeath(binder_recipient)"
98 << " == ::android::OK);\n";
99 }
100 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100101 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100102 }, /*cppImpl*/
103 {
104 {IMPL_HEADER,
105 [this](auto &out) {
106 out << "return true;";
107 }
108 },
109 {IMPL_PROXY,
110 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100111 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100112 }
113 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100114 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100115 } /*javaImpl*/
116 );
117}
118
119Method *Interface::createUnlinkToDeathMethod() const {
120 auto *results = new std::vector<TypedVar *> {
121 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
122 auto *args = new std::vector<TypedVar *> {
123 new TypedVar("recipient", new DeathRecipientType()) };
124
125 return new Method("unlinkToDeath",
126 args,
127 results,
128 false /*oneway */,
129 new std::vector<Annotation *>(),
130 HIDL_UNLINK_TO_DEATH_TRANSACTION,
131 {
132 {IMPL_HEADER,
133 [](auto &out) {
134 out << "return (recipient != nullptr);\n";
135 }
136 },
137 {IMPL_PROXY,
138 [](auto &out) {
139 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
140 << "for (auto it = _hidl_mDeathRecipients.begin();"
141 << "it != _hidl_mDeathRecipients.end();"
142 << "++it) {\n";
143 out.indent([&] {
144 out.sIf("(*it)->getRecipient() == recipient", [&] {
145 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
146 << "_hidl_mDeathRecipients.erase(it);\n"
147 << "return status == ::android::OK;\n";
148 });
149 });
150 out << "}\n";
151 out << "return false;\n";
152 }
153 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100154 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100155 }, /*cppImpl*/
156 {
157 {IMPL_HEADER,
158 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100159 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100160 }
161 },
162 {IMPL_PROXY,
163 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100164 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100165 }
166 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100167 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100168 } /*javaImpl*/
169 );
170}
Martijn Coenenaf712c02016-11-16 15:26:27 +0100171Method *Interface::createSyspropsChangedMethod() const {
172 return new Method("notifySyspropsChanged",
173 new std::vector<TypedVar *>() /*args */,
174 new std::vector<TypedVar *>() /*results */,
175 true /*oneway */,
176 new std::vector<Annotation *>(),
177 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100178 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100179 out << "::android::report_sysprop_change();\n";
180 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100181 } } }, /*cppImpl */
182 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100183 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100184 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100185 );
Andreas Huberc9410c72016-07-28 12:18:40 -0700186}
187
Yifan Hong10fe0b52016-10-19 14:20:17 -0700188Method *Interface::createDescriptorChainMethod() const {
189 VectorType *vecType = new VectorType();
190 vecType->setElementType(new StringType());
191 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
192 results->push_back(new TypedVar("indicator", vecType));
193
194 return new Method("interfaceChain",
195 new std::vector<TypedVar *>() /* args */,
196 results,
197 false /* oneway */,
198 new std::vector<Annotation *>(),
199 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100200 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700201 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800202 out << "_hidl_cb(";
203 out.block([&] {
204 for (const Interface *iface : chain) {
205 out << iface->fullName() << "::descriptor,\n";
206 }
207 });
208 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700209 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100210 } } }, /* cppImpl */
211 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700212 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800213 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700214 out.indent(); out.indent();
215 for (size_t i = 0; i < chain.size(); ++i) {
216 if (i != 0)
217 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800218 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700219 }
220 out << "));";
221 out.unindent(); out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100222 } } } /* javaImpl */
223 );
Yifan Hong10fe0b52016-10-19 14:20:17 -0700224}
225
226
Steven Moreland14ee6742016-10-18 12:58:28 -0700227bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800228 if (isIBase()) {
229 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
230 return true;
231 }
232
Yifan Hong10fe0b52016-10-19 14:20:17 -0700233 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700234 if (lookupMethod(method->name()) != nullptr) {
235 LOG(ERROR) << "Redefinition of method " << method->name();
236 return false;
237 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700238 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700239
Yifan Hong10fe0b52016-10-19 14:20:17 -0700240 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700241
Yifan Hong10fe0b52016-10-19 14:20:17 -0700242 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700243 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700244 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700245 ancestor = ancestor->superType();
246 }
247
Yifan Hong10fe0b52016-10-19 14:20:17 -0700248 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
249 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700250 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700251 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700252
253 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700254}
255
Yifan Hong10fe0b52016-10-19 14:20:17 -0700256
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700257const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700258 return mSuperType;
259}
260
Yifan Hong10fe0b52016-10-19 14:20:17 -0700261std::vector<const Interface *> Interface::typeChain() const {
262 std::vector<const Interface *> v;
263 const Interface *iface = this;
264 while (iface != nullptr) {
265 v.push_back(iface);
266 iface = iface->mSuperType;
267 }
268 return v;
269}
270
Yifan Hongfe95aa22016-10-19 17:26:45 -0700271std::vector<const Interface *> Interface::superTypeChain() const {
272 return superType()->typeChain(); // should work even if superType is nullptr
273}
274
Andreas Hubera2723d22016-07-29 15:36:07 -0700275bool Interface::isInterface() const {
276 return true;
277}
278
Andreas Huber295ad302016-08-16 11:35:00 -0700279bool Interface::isBinder() const {
280 return true;
281}
282
Yifan Hong10fe0b52016-10-19 14:20:17 -0700283const std::vector<Method *> &Interface::userDefinedMethods() const {
284 return mUserMethods;
285}
286
287const std::vector<Method *> &Interface::hidlReservedMethods() const {
288 return mReservedMethods;
289}
290
291std::vector<Method *> Interface::methods() const {
292 std::vector<Method *> v(mUserMethods);
293 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
294 return v;
295}
296
297std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
298 std::vector<InterfaceAndMethod> v;
299 std::vector<const Interface *> chain = typeChain();
300 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
301 const Interface *iface = *it;
302 for (Method *userMethod : iface->userDefinedMethods()) {
303 v.push_back(InterfaceAndMethod(iface, userMethod));
304 }
305 }
306 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800307 v.push_back(InterfaceAndMethod(
308 *chain.rbegin(), // IBase
309 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700310 }
311 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700312}
313
Steven Moreland14ee6742016-10-18 12:58:28 -0700314Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700315 for (const auto &tuple : allMethodsFromRoot()) {
316 Method *method = tuple.method();
317 if (method->name() == name) {
318 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700319 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700320 }
321
322 return nullptr;
323}
324
Steven Moreland40786312016-08-16 10:29:40 -0700325std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700326 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700327}
328
Yifan Hong60e52bd2016-11-09 14:47:45 -0800329FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800330 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
331}
332
Yifan Hong60e52bd2016-11-09 14:47:45 -0800333FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800334 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
335}
336
Yifan Hong60e52bd2016-11-09 14:47:45 -0800337FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800338 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
339}
340
341
Steven Moreland979e0992016-09-07 09:18:08 -0700342std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700343 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700344 const std::string base =
345 std::string(specifyNamespaces ? "::android::" : "")
346 + "sp<"
347 + (specifyNamespaces ? fullName() : partialCppName())
348 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700349
350 switch (mode) {
351 case StorageMode_Stack:
352 case StorageMode_Result:
353 return base;
354
355 case StorageMode_Argument:
356 return "const " + base + "&";
357 }
358}
359
Yifan Hong4ed13472016-11-02 10:44:11 -0700360std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700361 return fullJavaName();
362}
363
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800364std::string Interface::getVtsType() const {
365 if (StringHelper::EndsWith(localName(), "Callback")) {
366 return "TYPE_HIDL_CALLBACK";
367 } else {
368 return "TYPE_HIDL_INTERFACE";
369 }
370}
371
Andreas Huber881227d2016-08-02 14:20:21 -0700372void Interface::emitReaderWriter(
373 Formatter &out,
374 const std::string &name,
375 const std::string &parcelObj,
376 bool parcelObjIsPointer,
377 bool isReader,
378 ErrorMode mode) const {
379 const std::string parcelObjDeref =
380 parcelObj + (parcelObjIsPointer ? "->" : ".");
381
382 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700383 out << "{\n";
384 out.indent();
385
Iliyan Malchev549e2592016-08-10 08:59:12 -0700386 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700387
Andreas Huber8a82ff72016-08-04 10:29:39 -0700388 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700389 << binderName << ";\n";
390
Iliyan Malchev549e2592016-08-10 08:59:12 -0700391 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700392 out << parcelObjDeref
393 << "readNullableStrongBinder(&"
394 << binderName
395 << ");\n";
396
397 handleError(out, mode);
398
399 out << name
400 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100401 << "::android::hardware::fromBinder<"
402 << fqName().cppName()
403 << ","
404 << getProxyName().cppName()
405 << ","
406 << getStubName().cppName()
407 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700408 << binderName
409 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700410
411 out.unindent();
412 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700413 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200414 out << "if (" << name << " == nullptr) {\n";
415 out.indent();
416 out << "_hidl_err = ";
417 out << parcelObjDeref
418 << "writeStrongBinder(nullptr);\n";
419 out.unindent();
420 out << "} else {\n";
421 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800422 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
423 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800424 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100425 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800426 << ", "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100427 << getProxyName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800428 << ">("
429 << name
430 << ");\n";
431 });
432 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800433 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800434 out << "_hidl_err = "
435 << parcelObjDeref
436 << "writeStrongBinder(_hidl_binder);\n";
437 });
438 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800439 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800440 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
441 });
442 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200443 out.unindent();
444 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700445
Andreas Huber881227d2016-08-02 14:20:21 -0700446 handleError(out, mode);
447 }
448}
449
Andreas Huber2831d512016-08-15 09:33:47 -0700450void Interface::emitJavaReaderWriter(
451 Formatter &out,
452 const std::string &parcelObj,
453 const std::string &argName,
454 bool isReader) const {
455 if (isReader) {
456 out << fullJavaName()
457 << ".asInterface("
458 << parcelObj
459 << ".readStrongBinder());\n";
460 } else {
461 out << parcelObj
462 << ".writeStrongBinder("
463 << argName
464 << " == null ? null : "
465 << argName
466 << ".asBinder());\n";
467 }
468}
469
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700470status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
471 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700472 // Skip for TypeDef as it is just an alias of a defined type.
473 if (type->isTypeDef()) {
474 continue;
475 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700476 out << "attribute: {\n";
477 out.indent();
478 status_t status = type->emitVtsTypeDeclarations(out);
479 if (status != OK) {
480 return status;
481 }
482 out.unindent();
483 out << "}\n\n";
484 }
485 return OK;
486}
487
488status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700489 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800490 if (method->isHidlReserved()) {
491 continue;
492 }
493
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700494 out << "api: {\n";
495 out.indent();
496 out << "name: \"" << method->name() << "\"\n";
497 // Generate declaration for each return value.
498 for (const auto &result : method->results()) {
499 out << "return_type_hidl: {\n";
500 out.indent();
501 status_t status = result->type().emitVtsAttributeType(out);
502 if (status != OK) {
503 return status;
504 }
505 out.unindent();
506 out << "}\n";
507 }
508 // Generate declaration for each input argument
509 for (const auto &arg : method->args()) {
510 out << "arg: {\n";
511 out.indent();
512 status_t status = arg->type().emitVtsAttributeType(out);
513 if (status != OK) {
514 return status;
515 }
516 out.unindent();
517 out << "}\n";
518 }
519 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700520 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700521 out << "callflow: {\n";
522 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700523 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700524 if (name == "entry") {
525 out << "entry: true\n";
526 } else if (name == "exit") {
527 out << "exit: true\n";
528 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700529 const AnnotationParam *param =
530 annotation->getParam("next");
531 if (param != nullptr) {
532 for (auto value : *param->getValues()) {
533 out << "next: " << value << "\n";
534 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700535 }
536 } else {
537 std::cerr << "Invalid annotation '"
538 << name << "' for method: " << method->name()
539 << ". Should be one of: entry, exit, callflow. \n";
540 return UNKNOWN_ERROR;
541 }
542 out.unindent();
543 out << "}\n";
544 }
545 out.unindent();
546 out << "}\n\n";
547 }
548 return OK;
549}
550
551status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800552 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700553 << "predefined_type: \""
554 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700555 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800556 << "is_callback: "
557 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
558 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700559 return OK;
560}
561
Steven Moreland69e7c702016-09-09 11:16:32 -0700562bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700563 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700564 if (method->isOneway()) {
565 return true;
566 }
567 }
568
569 const Interface* superClass = superType();
570
571 if (superClass != nullptr) {
572 return superClass->hasOnewayMethods();
573 }
574
575 return false;
576}
577
Andreas Huber70a59e12016-08-16 12:57:01 -0700578bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700579 if (mIsJavaCompatibleInProgress) {
580 // We're currently trying to determine if this Interface is
581 // java-compatible and something is referencing this interface through
582 // one of its methods. Assume we'll ultimately succeed, if we were wrong
583 // the original invocation of Interface::isJavaCompatible() will then
584 // return the correct "false" result.
585 return true;
586 }
587
Andreas Huber0fa9e392016-08-31 09:05:44 -0700588 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
589 mIsJavaCompatibleInProgress = false;
590 return false;
591 }
592
Andreas Huberea081b32016-08-17 15:57:47 -0700593 mIsJavaCompatibleInProgress = true;
594
Andreas Huber70a59e12016-08-16 12:57:01 -0700595 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700596 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700597 return false;
598 }
599
Yifan Hong10fe0b52016-10-19 14:20:17 -0700600 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700601 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700602 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700603 return false;
604 }
605 }
606
Andreas Huberea081b32016-08-17 15:57:47 -0700607 mIsJavaCompatibleInProgress = false;
608
Andreas Huber70a59e12016-08-16 12:57:01 -0700609 return true;
610}
611
Andreas Huberc9410c72016-07-28 12:18:40 -0700612} // namespace android
613