blob: 29afee1cd6462647825f108f6eb3e0559f7ce663 [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) {
92 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
93 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
94 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
95 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
96 << "return (remote()->linkToDeath(binder_recipient)"
97 << " == ::android::OK);\n";
98 }
99 },
100 {IMPL_STUB,
101 [](auto &/*out*/) {
102 // Do nothing
103 }
104 }
105 }, /*cppImpl*/
106 {
107 {IMPL_HEADER,
108 [this](auto &out) {
109 out << "return true;";
110 }
111 },
112 {IMPL_PROXY,
113 [this](auto &out) {
114 // TODO (b/31632518)
115 out << "return false;";
116 }
117 },
118 {IMPL_STUB,
119 [this](auto &/*out*/) {
120 // Do nothing
121 }
122 }
123 } /*javaImpl*/
124 );
125}
126
127Method *Interface::createUnlinkToDeathMethod() const {
128 auto *results = new std::vector<TypedVar *> {
129 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
130 auto *args = new std::vector<TypedVar *> {
131 new TypedVar("recipient", new DeathRecipientType()) };
132
133 return new Method("unlinkToDeath",
134 args,
135 results,
136 false /*oneway */,
137 new std::vector<Annotation *>(),
138 HIDL_UNLINK_TO_DEATH_TRANSACTION,
139 {
140 {IMPL_HEADER,
141 [](auto &out) {
142 out << "return (recipient != nullptr);\n";
143 }
144 },
145 {IMPL_PROXY,
146 [](auto &out) {
147 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
148 << "for (auto it = _hidl_mDeathRecipients.begin();"
149 << "it != _hidl_mDeathRecipients.end();"
150 << "++it) {\n";
151 out.indent([&] {
152 out.sIf("(*it)->getRecipient() == recipient", [&] {
153 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
154 << "_hidl_mDeathRecipients.erase(it);\n"
155 << "return status == ::android::OK;\n";
156 });
157 });
158 out << "}\n";
159 out << "return false;\n";
160 }
161 },
162 {IMPL_STUB,
163 [](auto &/*out*/) {
164 // Do nothing
165 }
166 }
167 }, /*cppImpl*/
168 {
169 {IMPL_HEADER,
170 [this](auto &out) {
171 out << "return true;";
172 }
173 },
174 {IMPL_PROXY,
175 [this](auto &out) {
176 // TODO (b/31632518)
177 out << "return false;";
178 }
179 },
180 {IMPL_STUB,
181 [this](auto &/*out*/) {
182 // Do nothing
183 }
184 }
185 } /*javaImpl*/
186 );
187}
Martijn Coenenaf712c02016-11-16 15:26:27 +0100188Method *Interface::createSyspropsChangedMethod() const {
189 return new Method("notifySyspropsChanged",
190 new std::vector<TypedVar *>() /*args */,
191 new std::vector<TypedVar *>() /*results */,
192 true /*oneway */,
193 new std::vector<Annotation *>(),
194 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100195 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100196 out << "::android::report_sysprop_change();\n";
197 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100198 } } }, /*cppImpl */
199 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100200 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100201 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100202 );
Andreas Huberc9410c72016-07-28 12:18:40 -0700203}
204
Yifan Hong10fe0b52016-10-19 14:20:17 -0700205Method *Interface::createDescriptorChainMethod() const {
206 VectorType *vecType = new VectorType();
207 vecType->setElementType(new StringType());
208 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
209 results->push_back(new TypedVar("indicator", vecType));
210
211 return new Method("interfaceChain",
212 new std::vector<TypedVar *>() /* args */,
213 results,
214 false /* oneway */,
215 new std::vector<Annotation *>(),
216 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100217 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700218 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800219 out << "_hidl_cb(";
220 out.block([&] {
221 for (const Interface *iface : chain) {
222 out << iface->fullName() << "::descriptor,\n";
223 }
224 });
225 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700226 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100227 } } }, /* cppImpl */
228 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700229 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800230 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700231 out.indent(); out.indent();
232 for (size_t i = 0; i < chain.size(); ++i) {
233 if (i != 0)
234 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800235 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700236 }
237 out << "));";
238 out.unindent(); out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100239 } } } /* javaImpl */
240 );
Yifan Hong10fe0b52016-10-19 14:20:17 -0700241}
242
243
Steven Moreland14ee6742016-10-18 12:58:28 -0700244bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800245 if (isIBase()) {
246 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
247 return true;
248 }
249
Yifan Hong10fe0b52016-10-19 14:20:17 -0700250 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700251 if (lookupMethod(method->name()) != nullptr) {
252 LOG(ERROR) << "Redefinition of method " << method->name();
253 return false;
254 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700255 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700256
Yifan Hong10fe0b52016-10-19 14:20:17 -0700257 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700258
Yifan Hong10fe0b52016-10-19 14:20:17 -0700259 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700260 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700261 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700262 ancestor = ancestor->superType();
263 }
264
Yifan Hong10fe0b52016-10-19 14:20:17 -0700265 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
266 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700267 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700268 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700269
270 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700271}
272
Yifan Hong10fe0b52016-10-19 14:20:17 -0700273
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700274const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700275 return mSuperType;
276}
277
Yifan Hong10fe0b52016-10-19 14:20:17 -0700278std::vector<const Interface *> Interface::typeChain() const {
279 std::vector<const Interface *> v;
280 const Interface *iface = this;
281 while (iface != nullptr) {
282 v.push_back(iface);
283 iface = iface->mSuperType;
284 }
285 return v;
286}
287
Yifan Hongfe95aa22016-10-19 17:26:45 -0700288std::vector<const Interface *> Interface::superTypeChain() const {
289 return superType()->typeChain(); // should work even if superType is nullptr
290}
291
Andreas Hubera2723d22016-07-29 15:36:07 -0700292bool Interface::isInterface() const {
293 return true;
294}
295
Andreas Huber295ad302016-08-16 11:35:00 -0700296bool Interface::isBinder() const {
297 return true;
298}
299
Yifan Hong10fe0b52016-10-19 14:20:17 -0700300const std::vector<Method *> &Interface::userDefinedMethods() const {
301 return mUserMethods;
302}
303
304const std::vector<Method *> &Interface::hidlReservedMethods() const {
305 return mReservedMethods;
306}
307
308std::vector<Method *> Interface::methods() const {
309 std::vector<Method *> v(mUserMethods);
310 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
311 return v;
312}
313
314std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
315 std::vector<InterfaceAndMethod> v;
316 std::vector<const Interface *> chain = typeChain();
317 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
318 const Interface *iface = *it;
319 for (Method *userMethod : iface->userDefinedMethods()) {
320 v.push_back(InterfaceAndMethod(iface, userMethod));
321 }
322 }
323 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800324 v.push_back(InterfaceAndMethod(
325 *chain.rbegin(), // IBase
326 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700327 }
328 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700329}
330
Steven Moreland14ee6742016-10-18 12:58:28 -0700331Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700332 for (const auto &tuple : allMethodsFromRoot()) {
333 Method *method = tuple.method();
334 if (method->name() == name) {
335 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700336 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700337 }
338
339 return nullptr;
340}
341
Steven Moreland40786312016-08-16 10:29:40 -0700342std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700343 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700344}
345
Yifan Hong60e52bd2016-11-09 14:47:45 -0800346FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800347 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
348}
349
Yifan Hong60e52bd2016-11-09 14:47:45 -0800350FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800351 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
352}
353
Yifan Hong60e52bd2016-11-09 14:47:45 -0800354FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800355 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
356}
357
358
Steven Moreland979e0992016-09-07 09:18:08 -0700359std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700360 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700361 const std::string base =
362 std::string(specifyNamespaces ? "::android::" : "")
363 + "sp<"
364 + (specifyNamespaces ? fullName() : partialCppName())
365 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700366
367 switch (mode) {
368 case StorageMode_Stack:
369 case StorageMode_Result:
370 return base;
371
372 case StorageMode_Argument:
373 return "const " + base + "&";
374 }
375}
376
Yifan Hong4ed13472016-11-02 10:44:11 -0700377std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700378 return fullJavaName();
379}
380
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800381std::string Interface::getVtsType() const {
382 if (StringHelper::EndsWith(localName(), "Callback")) {
383 return "TYPE_HIDL_CALLBACK";
384 } else {
385 return "TYPE_HIDL_INTERFACE";
386 }
387}
388
Andreas Huber881227d2016-08-02 14:20:21 -0700389void Interface::emitReaderWriter(
390 Formatter &out,
391 const std::string &name,
392 const std::string &parcelObj,
393 bool parcelObjIsPointer,
394 bool isReader,
395 ErrorMode mode) const {
396 const std::string parcelObjDeref =
397 parcelObj + (parcelObjIsPointer ? "->" : ".");
398
399 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700400 out << "{\n";
401 out.indent();
402
Iliyan Malchev549e2592016-08-10 08:59:12 -0700403 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700404
Andreas Huber8a82ff72016-08-04 10:29:39 -0700405 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700406 << binderName << ";\n";
407
Iliyan Malchev549e2592016-08-10 08:59:12 -0700408 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700409 out << parcelObjDeref
410 << "readNullableStrongBinder(&"
411 << binderName
412 << ");\n";
413
414 handleError(out, mode);
415
416 out << name
417 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100418 << "::android::hardware::fromBinder<"
419 << fqName().cppName()
420 << ","
421 << getProxyName().cppName()
422 << ","
423 << getStubName().cppName()
424 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700425 << binderName
426 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700427
428 out.unindent();
429 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700430 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200431 out << "if (" << name << " == nullptr) {\n";
432 out.indent();
433 out << "_hidl_err = ";
434 out << parcelObjDeref
435 << "writeStrongBinder(nullptr);\n";
436 out.unindent();
437 out << "} else {\n";
438 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800439 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
440 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800441 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100442 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800443 << ", "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100444 << getProxyName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800445 << ">("
446 << name
447 << ");\n";
448 });
449 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800450 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800451 out << "_hidl_err = "
452 << parcelObjDeref
453 << "writeStrongBinder(_hidl_binder);\n";
454 });
455 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800456 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800457 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
458 });
459 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200460 out.unindent();
461 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700462
Andreas Huber881227d2016-08-02 14:20:21 -0700463 handleError(out, mode);
464 }
465}
466
Andreas Huber2831d512016-08-15 09:33:47 -0700467void Interface::emitJavaReaderWriter(
468 Formatter &out,
469 const std::string &parcelObj,
470 const std::string &argName,
471 bool isReader) const {
472 if (isReader) {
473 out << fullJavaName()
474 << ".asInterface("
475 << parcelObj
476 << ".readStrongBinder());\n";
477 } else {
478 out << parcelObj
479 << ".writeStrongBinder("
480 << argName
481 << " == null ? null : "
482 << argName
483 << ".asBinder());\n";
484 }
485}
486
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700487status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
488 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700489 // Skip for TypeDef as it is just an alias of a defined type.
490 if (type->isTypeDef()) {
491 continue;
492 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700493 out << "attribute: {\n";
494 out.indent();
495 status_t status = type->emitVtsTypeDeclarations(out);
496 if (status != OK) {
497 return status;
498 }
499 out.unindent();
500 out << "}\n\n";
501 }
502 return OK;
503}
504
505status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700506 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800507 if (method->isHidlReserved()) {
508 continue;
509 }
510
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700511 out << "api: {\n";
512 out.indent();
513 out << "name: \"" << method->name() << "\"\n";
514 // Generate declaration for each return value.
515 for (const auto &result : method->results()) {
516 out << "return_type_hidl: {\n";
517 out.indent();
518 status_t status = result->type().emitVtsAttributeType(out);
519 if (status != OK) {
520 return status;
521 }
522 out.unindent();
523 out << "}\n";
524 }
525 // Generate declaration for each input argument
526 for (const auto &arg : method->args()) {
527 out << "arg: {\n";
528 out.indent();
529 status_t status = arg->type().emitVtsAttributeType(out);
530 if (status != OK) {
531 return status;
532 }
533 out.unindent();
534 out << "}\n";
535 }
536 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700537 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700538 out << "callflow: {\n";
539 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700540 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700541 if (name == "entry") {
542 out << "entry: true\n";
543 } else if (name == "exit") {
544 out << "exit: true\n";
545 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700546 const AnnotationParam *param =
547 annotation->getParam("next");
548 if (param != nullptr) {
549 for (auto value : *param->getValues()) {
550 out << "next: " << value << "\n";
551 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700552 }
553 } else {
554 std::cerr << "Invalid annotation '"
555 << name << "' for method: " << method->name()
556 << ". Should be one of: entry, exit, callflow. \n";
557 return UNKNOWN_ERROR;
558 }
559 out.unindent();
560 out << "}\n";
561 }
562 out.unindent();
563 out << "}\n\n";
564 }
565 return OK;
566}
567
568status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800569 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700570 << "predefined_type: \""
571 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700572 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800573 << "is_callback: "
574 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
575 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700576 return OK;
577}
578
Steven Moreland69e7c702016-09-09 11:16:32 -0700579bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700580 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700581 if (method->isOneway()) {
582 return true;
583 }
584 }
585
586 const Interface* superClass = superType();
587
588 if (superClass != nullptr) {
589 return superClass->hasOnewayMethods();
590 }
591
592 return false;
593}
594
Andreas Huber70a59e12016-08-16 12:57:01 -0700595bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700596 if (mIsJavaCompatibleInProgress) {
597 // We're currently trying to determine if this Interface is
598 // java-compatible and something is referencing this interface through
599 // one of its methods. Assume we'll ultimately succeed, if we were wrong
600 // the original invocation of Interface::isJavaCompatible() will then
601 // return the correct "false" result.
602 return true;
603 }
604
Andreas Huber0fa9e392016-08-31 09:05:44 -0700605 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
606 mIsJavaCompatibleInProgress = false;
607 return false;
608 }
609
Andreas Huberea081b32016-08-17 15:57:47 -0700610 mIsJavaCompatibleInProgress = true;
611
Andreas Huber70a59e12016-08-16 12:57:01 -0700612 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700613 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700614 return false;
615 }
616
Yifan Hong10fe0b52016-10-19 14:20:17 -0700617 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700618 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700619 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700620 return false;
621 }
622 }
623
Andreas Huberea081b32016-08-17 15:57:47 -0700624 mIsJavaCompatibleInProgress = false;
625
Andreas Huber70a59e12016-08-16 12:57:01 -0700626 return true;
627}
628
Andreas Huberc9410c72016-07-28 12:18:40 -0700629} // namespace android
630