blob: 1e40bd205a9b09b472d2d102e1914640af3712a6 [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 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100100 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100101 }, /*cppImpl*/
102 {
103 {IMPL_HEADER,
104 [this](auto &out) {
105 out << "return true;";
106 }
107 },
108 {IMPL_PROXY,
109 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100110 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100111 }
112 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100113 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100114 } /*javaImpl*/
115 );
116}
117
118Method *Interface::createUnlinkToDeathMethod() const {
119 auto *results = new std::vector<TypedVar *> {
120 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
121 auto *args = new std::vector<TypedVar *> {
122 new TypedVar("recipient", new DeathRecipientType()) };
123
124 return new Method("unlinkToDeath",
125 args,
126 results,
127 false /*oneway */,
128 new std::vector<Annotation *>(),
129 HIDL_UNLINK_TO_DEATH_TRANSACTION,
130 {
131 {IMPL_HEADER,
132 [](auto &out) {
133 out << "return (recipient != nullptr);\n";
134 }
135 },
136 {IMPL_PROXY,
137 [](auto &out) {
138 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
139 << "for (auto it = _hidl_mDeathRecipients.begin();"
140 << "it != _hidl_mDeathRecipients.end();"
141 << "++it) {\n";
142 out.indent([&] {
143 out.sIf("(*it)->getRecipient() == recipient", [&] {
144 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
145 << "_hidl_mDeathRecipients.erase(it);\n"
146 << "return status == ::android::OK;\n";
147 });
148 });
149 out << "}\n";
150 out << "return false;\n";
151 }
152 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100153 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100154 }, /*cppImpl*/
155 {
156 {IMPL_HEADER,
157 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100158 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100159 }
160 },
161 {IMPL_PROXY,
162 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100163 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100164 }
165 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100166 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100167 } /*javaImpl*/
168 );
169}
Martijn Coenenaf712c02016-11-16 15:26:27 +0100170Method *Interface::createSyspropsChangedMethod() const {
171 return new Method("notifySyspropsChanged",
172 new std::vector<TypedVar *>() /*args */,
173 new std::vector<TypedVar *>() /*results */,
174 true /*oneway */,
175 new std::vector<Annotation *>(),
176 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100177 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100178 out << "::android::report_sysprop_change();\n";
179 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100180 } } }, /*cppImpl */
181 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100182 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100183 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100184 );
Andreas Huberc9410c72016-07-28 12:18:40 -0700185}
186
Yifan Hong10fe0b52016-10-19 14:20:17 -0700187Method *Interface::createDescriptorChainMethod() const {
188 VectorType *vecType = new VectorType();
189 vecType->setElementType(new StringType());
190 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
191 results->push_back(new TypedVar("indicator", vecType));
192
193 return new Method("interfaceChain",
194 new std::vector<TypedVar *>() /* args */,
195 results,
196 false /* oneway */,
197 new std::vector<Annotation *>(),
198 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100199 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700200 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800201 out << "_hidl_cb(";
202 out.block([&] {
203 for (const Interface *iface : chain) {
204 out << iface->fullName() << "::descriptor,\n";
205 }
206 });
207 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700208 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100209 } } }, /* cppImpl */
210 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700211 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800212 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700213 out.indent(); out.indent();
214 for (size_t i = 0; i < chain.size(); ++i) {
215 if (i != 0)
216 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800217 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700218 }
219 out << "));";
220 out.unindent(); out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100221 } } } /* javaImpl */
222 );
Yifan Hong10fe0b52016-10-19 14:20:17 -0700223}
224
225
Steven Moreland14ee6742016-10-18 12:58:28 -0700226bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800227 if (isIBase()) {
228 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
229 return true;
230 }
231
Yifan Hong10fe0b52016-10-19 14:20:17 -0700232 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700233 if (lookupMethod(method->name()) != nullptr) {
234 LOG(ERROR) << "Redefinition of method " << method->name();
235 return false;
236 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700237 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700238
Yifan Hong10fe0b52016-10-19 14:20:17 -0700239 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700240
Yifan Hong10fe0b52016-10-19 14:20:17 -0700241 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700242 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700243 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700244 ancestor = ancestor->superType();
245 }
246
Yifan Hong10fe0b52016-10-19 14:20:17 -0700247 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
248 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700249 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700250 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700251
252 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700253}
254
Yifan Hong10fe0b52016-10-19 14:20:17 -0700255
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700256const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700257 return mSuperType;
258}
259
Yifan Hong10fe0b52016-10-19 14:20:17 -0700260std::vector<const Interface *> Interface::typeChain() const {
261 std::vector<const Interface *> v;
262 const Interface *iface = this;
263 while (iface != nullptr) {
264 v.push_back(iface);
265 iface = iface->mSuperType;
266 }
267 return v;
268}
269
Yifan Hongfe95aa22016-10-19 17:26:45 -0700270std::vector<const Interface *> Interface::superTypeChain() const {
271 return superType()->typeChain(); // should work even if superType is nullptr
272}
273
Andreas Hubera2723d22016-07-29 15:36:07 -0700274bool Interface::isInterface() const {
275 return true;
276}
277
Andreas Huber295ad302016-08-16 11:35:00 -0700278bool Interface::isBinder() const {
279 return true;
280}
281
Yifan Hong10fe0b52016-10-19 14:20:17 -0700282const std::vector<Method *> &Interface::userDefinedMethods() const {
283 return mUserMethods;
284}
285
286const std::vector<Method *> &Interface::hidlReservedMethods() const {
287 return mReservedMethods;
288}
289
290std::vector<Method *> Interface::methods() const {
291 std::vector<Method *> v(mUserMethods);
292 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
293 return v;
294}
295
296std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
297 std::vector<InterfaceAndMethod> v;
298 std::vector<const Interface *> chain = typeChain();
299 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
300 const Interface *iface = *it;
301 for (Method *userMethod : iface->userDefinedMethods()) {
302 v.push_back(InterfaceAndMethod(iface, userMethod));
303 }
304 }
305 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800306 v.push_back(InterfaceAndMethod(
307 *chain.rbegin(), // IBase
308 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700309 }
310 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700311}
312
Steven Moreland14ee6742016-10-18 12:58:28 -0700313Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700314 for (const auto &tuple : allMethodsFromRoot()) {
315 Method *method = tuple.method();
316 if (method->name() == name) {
317 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700318 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700319 }
320
321 return nullptr;
322}
323
Steven Moreland40786312016-08-16 10:29:40 -0700324std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700325 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700326}
327
Yifan Hong60e52bd2016-11-09 14:47:45 -0800328FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800329 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
330}
331
Yifan Hong60e52bd2016-11-09 14:47:45 -0800332FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800333 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
334}
335
Yifan Hong60e52bd2016-11-09 14:47:45 -0800336FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800337 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
338}
339
340
Steven Moreland979e0992016-09-07 09:18:08 -0700341std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700342 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700343 const std::string base =
344 std::string(specifyNamespaces ? "::android::" : "")
345 + "sp<"
346 + (specifyNamespaces ? fullName() : partialCppName())
347 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700348
349 switch (mode) {
350 case StorageMode_Stack:
351 case StorageMode_Result:
352 return base;
353
354 case StorageMode_Argument:
355 return "const " + base + "&";
356 }
357}
358
Yifan Hong4ed13472016-11-02 10:44:11 -0700359std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700360 return fullJavaName();
361}
362
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800363std::string Interface::getVtsType() const {
364 if (StringHelper::EndsWith(localName(), "Callback")) {
365 return "TYPE_HIDL_CALLBACK";
366 } else {
367 return "TYPE_HIDL_INTERFACE";
368 }
369}
370
Andreas Huber881227d2016-08-02 14:20:21 -0700371void Interface::emitReaderWriter(
372 Formatter &out,
373 const std::string &name,
374 const std::string &parcelObj,
375 bool parcelObjIsPointer,
376 bool isReader,
377 ErrorMode mode) const {
378 const std::string parcelObjDeref =
379 parcelObj + (parcelObjIsPointer ? "->" : ".");
380
381 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700382 out << "{\n";
383 out.indent();
384
Iliyan Malchev549e2592016-08-10 08:59:12 -0700385 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700386
Andreas Huber8a82ff72016-08-04 10:29:39 -0700387 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700388 << binderName << ";\n";
389
Iliyan Malchev549e2592016-08-10 08:59:12 -0700390 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700391 out << parcelObjDeref
392 << "readNullableStrongBinder(&"
393 << binderName
394 << ");\n";
395
396 handleError(out, mode);
397
398 out << name
399 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100400 << "::android::hardware::fromBinder<"
401 << fqName().cppName()
402 << ","
403 << getProxyName().cppName()
404 << ","
405 << getStubName().cppName()
406 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700407 << binderName
408 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700409
410 out.unindent();
411 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700412 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200413 out << "if (" << name << " == nullptr) {\n";
414 out.indent();
415 out << "_hidl_err = ";
416 out << parcelObjDeref
417 << "writeStrongBinder(nullptr);\n";
418 out.unindent();
419 out << "} else {\n";
420 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800421 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
422 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800423 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100424 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800425 << ", "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100426 << getProxyName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800427 << ">("
428 << name
429 << ");\n";
430 });
431 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800432 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800433 out << "_hidl_err = "
434 << parcelObjDeref
435 << "writeStrongBinder(_hidl_binder);\n";
436 });
437 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800438 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800439 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
440 });
441 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200442 out.unindent();
443 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700444
Andreas Huber881227d2016-08-02 14:20:21 -0700445 handleError(out, mode);
446 }
447}
448
Andreas Huber2831d512016-08-15 09:33:47 -0700449void Interface::emitJavaReaderWriter(
450 Formatter &out,
451 const std::string &parcelObj,
452 const std::string &argName,
453 bool isReader) const {
454 if (isReader) {
455 out << fullJavaName()
456 << ".asInterface("
457 << parcelObj
458 << ".readStrongBinder());\n";
459 } else {
460 out << parcelObj
461 << ".writeStrongBinder("
462 << argName
463 << " == null ? null : "
464 << argName
465 << ".asBinder());\n";
466 }
467}
468
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700469status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
470 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700471 // Skip for TypeDef as it is just an alias of a defined type.
472 if (type->isTypeDef()) {
473 continue;
474 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700475 out << "attribute: {\n";
476 out.indent();
477 status_t status = type->emitVtsTypeDeclarations(out);
478 if (status != OK) {
479 return status;
480 }
481 out.unindent();
482 out << "}\n\n";
483 }
484 return OK;
485}
486
487status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700488 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800489 if (method->isHidlReserved()) {
490 continue;
491 }
492
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700493 out << "api: {\n";
494 out.indent();
495 out << "name: \"" << method->name() << "\"\n";
496 // Generate declaration for each return value.
497 for (const auto &result : method->results()) {
498 out << "return_type_hidl: {\n";
499 out.indent();
500 status_t status = result->type().emitVtsAttributeType(out);
501 if (status != OK) {
502 return status;
503 }
504 out.unindent();
505 out << "}\n";
506 }
507 // Generate declaration for each input argument
508 for (const auto &arg : method->args()) {
509 out << "arg: {\n";
510 out.indent();
511 status_t status = arg->type().emitVtsAttributeType(out);
512 if (status != OK) {
513 return status;
514 }
515 out.unindent();
516 out << "}\n";
517 }
518 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700519 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700520 out << "callflow: {\n";
521 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700522 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700523 if (name == "entry") {
524 out << "entry: true\n";
525 } else if (name == "exit") {
526 out << "exit: true\n";
527 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700528 const AnnotationParam *param =
529 annotation->getParam("next");
530 if (param != nullptr) {
531 for (auto value : *param->getValues()) {
532 out << "next: " << value << "\n";
533 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700534 }
535 } else {
536 std::cerr << "Invalid annotation '"
537 << name << "' for method: " << method->name()
538 << ". Should be one of: entry, exit, callflow. \n";
539 return UNKNOWN_ERROR;
540 }
541 out.unindent();
542 out << "}\n";
543 }
544 out.unindent();
545 out << "}\n\n";
546 }
547 return OK;
548}
549
550status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800551 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700552 << "predefined_type: \""
553 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700554 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800555 << "is_callback: "
556 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
557 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700558 return OK;
559}
560
Steven Moreland69e7c702016-09-09 11:16:32 -0700561bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700562 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700563 if (method->isOneway()) {
564 return true;
565 }
566 }
567
568 const Interface* superClass = superType();
569
570 if (superClass != nullptr) {
571 return superClass->hasOnewayMethods();
572 }
573
574 return false;
575}
576
Andreas Huber70a59e12016-08-16 12:57:01 -0700577bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700578 if (mIsJavaCompatibleInProgress) {
579 // We're currently trying to determine if this Interface is
580 // java-compatible and something is referencing this interface through
581 // one of its methods. Assume we'll ultimately succeed, if we were wrong
582 // the original invocation of Interface::isJavaCompatible() will then
583 // return the correct "false" result.
584 return true;
585 }
586
Andreas Huber0fa9e392016-08-31 09:05:44 -0700587 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
588 mIsJavaCompatibleInProgress = false;
589 return false;
590 }
591
Andreas Huberea081b32016-08-17 15:57:47 -0700592 mIsJavaCompatibleInProgress = true;
593
Andreas Huber70a59e12016-08-16 12:57:01 -0700594 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700595 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700596 return false;
597 }
598
Yifan Hong10fe0b52016-10-19 14:20:17 -0700599 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700600 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700601 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700602 return false;
603 }
604 }
605
Andreas Huberea081b32016-08-17 15:57:47 -0700606 mIsJavaCompatibleInProgress = false;
607
Andreas Huber70a59e12016-08-16 12:57:01 -0700608 return true;
609}
610
Andreas Huberc9410c72016-07-28 12:18:40 -0700611} // namespace android
612