blob: 4c9e9a58114002177d1620d7e434814584b3679e [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
Martijn Coenenb40ef022017-01-02 15:21:46 +0100275bool Interface::isElidableType() const {
276 return true;
277}
278
Andreas Hubera2723d22016-07-29 15:36:07 -0700279bool Interface::isInterface() const {
280 return true;
281}
282
Andreas Huber295ad302016-08-16 11:35:00 -0700283bool Interface::isBinder() const {
284 return true;
285}
286
Yifan Hong10fe0b52016-10-19 14:20:17 -0700287const std::vector<Method *> &Interface::userDefinedMethods() const {
288 return mUserMethods;
289}
290
291const std::vector<Method *> &Interface::hidlReservedMethods() const {
292 return mReservedMethods;
293}
294
295std::vector<Method *> Interface::methods() const {
296 std::vector<Method *> v(mUserMethods);
297 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
298 return v;
299}
300
301std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
302 std::vector<InterfaceAndMethod> v;
303 std::vector<const Interface *> chain = typeChain();
304 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
305 const Interface *iface = *it;
306 for (Method *userMethod : iface->userDefinedMethods()) {
307 v.push_back(InterfaceAndMethod(iface, userMethod));
308 }
309 }
310 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800311 v.push_back(InterfaceAndMethod(
312 *chain.rbegin(), // IBase
313 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700314 }
315 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700316}
317
Steven Moreland14ee6742016-10-18 12:58:28 -0700318Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700319 for (const auto &tuple : allMethodsFromRoot()) {
320 Method *method = tuple.method();
321 if (method->name() == name) {
322 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700323 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700324 }
325
326 return nullptr;
327}
328
Steven Moreland40786312016-08-16 10:29:40 -0700329std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700330 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700331}
332
Yifan Hongeefe4f22017-01-04 15:32:42 -0800333std::string Interface::getProxyName() const {
334 return fqName().getInterfaceProxyName();
335}
336
337std::string Interface::getStubName() const {
338 return fqName().getInterfaceStubName();
339}
340
341std::string Interface::getHwName() const {
342 return fqName().getInterfaceHwName();
343}
344
345std::string Interface::getPassthroughName() const {
346 return fqName().getInterfacePassthroughName();
347}
348
Yifan Hong51a65092017-01-04 15:41:44 -0800349FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800350 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800351}
352
Yifan Hong51a65092017-01-04 15:41:44 -0800353FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800354 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800355}
356
Yifan Hong51a65092017-01-04 15:41:44 -0800357FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800358 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800359}
360
Steven Moreland979e0992016-09-07 09:18:08 -0700361std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700362 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700363 const std::string base =
364 std::string(specifyNamespaces ? "::android::" : "")
365 + "sp<"
366 + (specifyNamespaces ? fullName() : partialCppName())
367 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700368
369 switch (mode) {
370 case StorageMode_Stack:
371 case StorageMode_Result:
372 return base;
373
374 case StorageMode_Argument:
375 return "const " + base + "&";
376 }
377}
378
Yifan Hong4ed13472016-11-02 10:44:11 -0700379std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700380 return fullJavaName();
381}
382
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800383std::string Interface::getVtsType() const {
384 if (StringHelper::EndsWith(localName(), "Callback")) {
385 return "TYPE_HIDL_CALLBACK";
386 } else {
387 return "TYPE_HIDL_INTERFACE";
388 }
389}
390
Andreas Huber881227d2016-08-02 14:20:21 -0700391void Interface::emitReaderWriter(
392 Formatter &out,
393 const std::string &name,
394 const std::string &parcelObj,
395 bool parcelObjIsPointer,
396 bool isReader,
397 ErrorMode mode) const {
398 const std::string parcelObjDeref =
399 parcelObj + (parcelObjIsPointer ? "->" : ".");
400
401 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700402 out << "{\n";
403 out.indent();
404
Iliyan Malchev549e2592016-08-10 08:59:12 -0700405 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700406
Andreas Huber8a82ff72016-08-04 10:29:39 -0700407 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700408 << binderName << ";\n";
409
Iliyan Malchev549e2592016-08-10 08:59:12 -0700410 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700411 out << parcelObjDeref
412 << "readNullableStrongBinder(&"
413 << binderName
414 << ");\n";
415
416 handleError(out, mode);
417
418 out << name
419 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100420 << "::android::hardware::fromBinder<"
421 << fqName().cppName()
422 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800423 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100424 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800425 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100426 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700427 << binderName
428 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700429
430 out.unindent();
431 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700432 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200433 out << "if (" << name << " == nullptr) {\n";
434 out.indent();
435 out << "_hidl_err = ";
436 out << parcelObjDeref
437 << "writeStrongBinder(nullptr);\n";
438 out.unindent();
439 out << "} else {\n";
440 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800441 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
442 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800443 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100444 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800445 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800446 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800447 << ">("
448 << name
449 << ");\n";
450 });
451 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800452 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800453 out << "_hidl_err = "
454 << parcelObjDeref
455 << "writeStrongBinder(_hidl_binder);\n";
456 });
457 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800458 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800459 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
460 });
461 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200462 out.unindent();
463 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700464
Andreas Huber881227d2016-08-02 14:20:21 -0700465 handleError(out, mode);
466 }
467}
468
Andreas Huber2831d512016-08-15 09:33:47 -0700469void Interface::emitJavaReaderWriter(
470 Formatter &out,
471 const std::string &parcelObj,
472 const std::string &argName,
473 bool isReader) const {
474 if (isReader) {
475 out << fullJavaName()
476 << ".asInterface("
477 << parcelObj
478 << ".readStrongBinder());\n";
479 } else {
480 out << parcelObj
481 << ".writeStrongBinder("
482 << argName
483 << " == null ? null : "
484 << argName
485 << ".asBinder());\n";
486 }
487}
488
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700489status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
490 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700491 // Skip for TypeDef as it is just an alias of a defined type.
492 if (type->isTypeDef()) {
493 continue;
494 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700495 out << "attribute: {\n";
496 out.indent();
497 status_t status = type->emitVtsTypeDeclarations(out);
498 if (status != OK) {
499 return status;
500 }
501 out.unindent();
502 out << "}\n\n";
503 }
504 return OK;
505}
506
507status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700508 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800509 if (method->isHidlReserved()) {
510 continue;
511 }
512
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700513 out << "api: {\n";
514 out.indent();
515 out << "name: \"" << method->name() << "\"\n";
516 // Generate declaration for each return value.
517 for (const auto &result : method->results()) {
518 out << "return_type_hidl: {\n";
519 out.indent();
520 status_t status = result->type().emitVtsAttributeType(out);
521 if (status != OK) {
522 return status;
523 }
524 out.unindent();
525 out << "}\n";
526 }
527 // Generate declaration for each input argument
528 for (const auto &arg : method->args()) {
529 out << "arg: {\n";
530 out.indent();
531 status_t status = arg->type().emitVtsAttributeType(out);
532 if (status != OK) {
533 return status;
534 }
535 out.unindent();
536 out << "}\n";
537 }
538 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700539 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700540 out << "callflow: {\n";
541 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700542 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700543 if (name == "entry") {
544 out << "entry: true\n";
545 } else if (name == "exit") {
546 out << "exit: true\n";
547 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700548 const AnnotationParam *param =
549 annotation->getParam("next");
550 if (param != nullptr) {
551 for (auto value : *param->getValues()) {
552 out << "next: " << value << "\n";
553 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700554 }
555 } else {
556 std::cerr << "Invalid annotation '"
557 << name << "' for method: " << method->name()
558 << ". Should be one of: entry, exit, callflow. \n";
559 return UNKNOWN_ERROR;
560 }
561 out.unindent();
562 out << "}\n";
563 }
564 out.unindent();
565 out << "}\n\n";
566 }
567 return OK;
568}
569
570status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800571 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700572 << "predefined_type: \""
573 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700574 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800575 << "is_callback: "
576 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
577 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700578 return OK;
579}
580
Steven Moreland69e7c702016-09-09 11:16:32 -0700581bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700582 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700583 if (method->isOneway()) {
584 return true;
585 }
586 }
587
588 const Interface* superClass = superType();
589
590 if (superClass != nullptr) {
591 return superClass->hasOnewayMethods();
592 }
593
594 return false;
595}
596
Andreas Huber70a59e12016-08-16 12:57:01 -0700597bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700598 if (mIsJavaCompatibleInProgress) {
599 // We're currently trying to determine if this Interface is
600 // java-compatible and something is referencing this interface through
601 // one of its methods. Assume we'll ultimately succeed, if we were wrong
602 // the original invocation of Interface::isJavaCompatible() will then
603 // return the correct "false" result.
604 return true;
605 }
606
Andreas Huber0fa9e392016-08-31 09:05:44 -0700607 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
608 mIsJavaCompatibleInProgress = false;
609 return false;
610 }
611
Andreas Huberea081b32016-08-17 15:57:47 -0700612 mIsJavaCompatibleInProgress = true;
613
Andreas Huber70a59e12016-08-16 12:57:01 -0700614 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700615 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700616 return false;
617 }
618
Yifan Hong10fe0b52016-10-19 14:20:17 -0700619 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700620 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700621 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700622 return false;
623 }
624 }
625
Andreas Huberea081b32016-08-17 15:57:47 -0700626 mIsJavaCompatibleInProgress = false;
627
Andreas Huber70a59e12016-08-16 12:57:01 -0700628 return true;
629}
630
Andreas Huberc9410c72016-07-28 12:18:40 -0700631} // namespace android
632