blob: 77681d41118314068cc85ea60e33783534f7a0a1 [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,
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080053 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
Yifan Hong10fe0b52016-10-19 14:20:17 -070054 LAST_HIDL_TRANSACTION = 0x00ffffff,
55};
56
Yifan Honga4b53d02016-10-31 17:29:10 -070057Interface::Interface(const char *localName, const Location &location, Interface *super)
58 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070059 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070060 mIsJavaCompatibleInProgress(false) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070061 mReservedMethods.push_back(createDescriptorChainMethod());
Martijn Coenenaf712c02016-11-16 15:26:27 +010062 mReservedMethods.push_back(createSyspropsChangedMethod());
Martijn Coenen115d4282016-12-19 05:14:04 +010063 mReservedMethods.push_back(createLinkToDeathMethod());
64 mReservedMethods.push_back(createUnlinkToDeathMethod());
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080065 mReservedMethods.push_back(createSetHALInstrumentationMethod());
Martijn Coenenaf712c02016-11-16 15:26:27 +010066}
67
Steven Moreland30bb6a82016-11-30 09:18:34 -080068std::string Interface::typeName() const {
69 return "interface " + localName();
70}
71
Martijn Coenen115d4282016-12-19 05:14:04 +010072Method *Interface::createLinkToDeathMethod() const {
73 auto *results = new std::vector<TypedVar *> {
74 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
75 auto *args = new std::vector<TypedVar *> {
76 new TypedVar("recipient", new DeathRecipientType()),
77 new TypedVar("cookie", new ScalarType(ScalarType::KIND_UINT64)) };
78
79 return new Method("linkToDeath",
80 args,
81 results,
82 false /*oneway */,
83 new std::vector<Annotation *>(),
84 HIDL_LINK_TO_DEATH_TRANSACTION,
85 {
86 {IMPL_HEADER,
87 [](auto &out) {
88 out << "(void)cookie;\n"
89 << "return (recipient != nullptr);\n";
90 }
91 },
92 {IMPL_PROXY,
93 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010094 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +010095 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
96 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
97 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
98 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
99 << "return (remote()->linkToDeath(binder_recipient)"
100 << " == ::android::OK);\n";
101 }
102 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100103 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100104 }, /*cppImpl*/
105 {
106 {IMPL_HEADER,
107 [this](auto &out) {
108 out << "return true;";
109 }
110 },
111 {IMPL_PROXY,
112 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100113 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100114 }
115 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100116 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100117 } /*javaImpl*/
118 );
119}
120
121Method *Interface::createUnlinkToDeathMethod() const {
122 auto *results = new std::vector<TypedVar *> {
123 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
124 auto *args = new std::vector<TypedVar *> {
125 new TypedVar("recipient", new DeathRecipientType()) };
126
127 return new Method("unlinkToDeath",
128 args,
129 results,
130 false /*oneway */,
131 new std::vector<Annotation *>(),
132 HIDL_UNLINK_TO_DEATH_TRANSACTION,
133 {
134 {IMPL_HEADER,
135 [](auto &out) {
136 out << "return (recipient != nullptr);\n";
137 }
138 },
139 {IMPL_PROXY,
140 [](auto &out) {
141 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
142 << "for (auto it = _hidl_mDeathRecipients.begin();"
143 << "it != _hidl_mDeathRecipients.end();"
144 << "++it) {\n";
145 out.indent([&] {
146 out.sIf("(*it)->getRecipient() == recipient", [&] {
147 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
148 << "_hidl_mDeathRecipients.erase(it);\n"
149 << "return status == ::android::OK;\n";
150 });
151 });
152 out << "}\n";
153 out << "return false;\n";
154 }
155 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100156 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100157 }, /*cppImpl*/
158 {
159 {IMPL_HEADER,
160 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100161 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100162 }
163 },
164 {IMPL_PROXY,
165 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100166 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100167 }
168 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100169 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100170 } /*javaImpl*/
171 );
172}
Martijn Coenenaf712c02016-11-16 15:26:27 +0100173Method *Interface::createSyspropsChangedMethod() const {
174 return new Method("notifySyspropsChanged",
175 new std::vector<TypedVar *>() /*args */,
176 new std::vector<TypedVar *>() /*results */,
177 true /*oneway */,
178 new std::vector<Annotation *>(),
179 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100180 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100181 out << "::android::report_sysprop_change();\n";
182 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100183 } } }, /*cppImpl */
184 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100185 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100186 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100187 );
Andreas Huberc9410c72016-07-28 12:18:40 -0700188}
189
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800190Method *Interface::createSetHALInstrumentationMethod() const {
191 return new Method("setHALInstrumentation",
192 new std::vector<TypedVar *>() /*args */,
193 new std::vector<TypedVar *>() /*results */,
194 true /*oneway */,
195 new std::vector<Annotation *>(),
196 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
197 {
198 {IMPL_HEADER,
199 [this](auto &out) {
200 // do nothing for base class.
201 out << "return ::android::hardware::Void();\n";
202 }
203 },
204 {IMPL_PROXY,
205 [](auto &out) {
206 out << "configureInstrumentation();\n";
207 out << "return ::android::hardware::Void();\n";
208 }
209 },
210 {IMPL_STUB,
211 [](auto &out) {
212 out << "configureInstrumentation();\n";
213 }
214 },
215 {IMPL_PASSTHROUGH,
216 [](auto &out) {
217 out << "configureInstrumentation();\n";
218 out << "return ::android::hardware::Void();\n";
219 }
220 },
221 }, /*cppImpl */
222 { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */
223 // Not support for Java Impl for now.
224 } } } /*javaImpl */
225 );
226}
227
Yifan Hong10fe0b52016-10-19 14:20:17 -0700228Method *Interface::createDescriptorChainMethod() const {
229 VectorType *vecType = new VectorType();
230 vecType->setElementType(new StringType());
231 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
232 results->push_back(new TypedVar("indicator", vecType));
233
234 return new Method("interfaceChain",
235 new std::vector<TypedVar *>() /* args */,
236 results,
237 false /* oneway */,
238 new std::vector<Annotation *>(),
239 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100240 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700241 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800242 out << "_hidl_cb(";
243 out.block([&] {
244 for (const Interface *iface : chain) {
245 out << iface->fullName() << "::descriptor,\n";
246 }
247 });
248 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700249 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100250 } } }, /* cppImpl */
251 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700252 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800253 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700254 out.indent(); out.indent();
255 for (size_t i = 0; i < chain.size(); ++i) {
256 if (i != 0)
257 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800258 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700259 }
260 out << "));";
261 out.unindent(); out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100262 } } } /* javaImpl */
263 );
Yifan Hong10fe0b52016-10-19 14:20:17 -0700264}
265
266
Steven Moreland14ee6742016-10-18 12:58:28 -0700267bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800268 if (isIBase()) {
269 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
270 return true;
271 }
272
Yifan Hong10fe0b52016-10-19 14:20:17 -0700273 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700274 if (lookupMethod(method->name()) != nullptr) {
275 LOG(ERROR) << "Redefinition of method " << method->name();
276 return false;
277 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700278 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700279
Yifan Hong10fe0b52016-10-19 14:20:17 -0700280 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700281
Yifan Hong10fe0b52016-10-19 14:20:17 -0700282 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700283 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700284 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700285 ancestor = ancestor->superType();
286 }
287
Yifan Hong10fe0b52016-10-19 14:20:17 -0700288 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
289 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700290 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700291 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700292
293 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700294}
295
Yifan Hong10fe0b52016-10-19 14:20:17 -0700296
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700297const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700298 return mSuperType;
299}
300
Yifan Hong10fe0b52016-10-19 14:20:17 -0700301std::vector<const Interface *> Interface::typeChain() const {
302 std::vector<const Interface *> v;
303 const Interface *iface = this;
304 while (iface != nullptr) {
305 v.push_back(iface);
306 iface = iface->mSuperType;
307 }
308 return v;
309}
310
Yifan Hongfe95aa22016-10-19 17:26:45 -0700311std::vector<const Interface *> Interface::superTypeChain() const {
312 return superType()->typeChain(); // should work even if superType is nullptr
313}
314
Martijn Coenenb40ef022017-01-02 15:21:46 +0100315bool Interface::isElidableType() const {
316 return true;
317}
318
Andreas Hubera2723d22016-07-29 15:36:07 -0700319bool Interface::isInterface() const {
320 return true;
321}
322
Andreas Huber295ad302016-08-16 11:35:00 -0700323bool Interface::isBinder() const {
324 return true;
325}
326
Yifan Hong10fe0b52016-10-19 14:20:17 -0700327const std::vector<Method *> &Interface::userDefinedMethods() const {
328 return mUserMethods;
329}
330
331const std::vector<Method *> &Interface::hidlReservedMethods() const {
332 return mReservedMethods;
333}
334
335std::vector<Method *> Interface::methods() const {
336 std::vector<Method *> v(mUserMethods);
337 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
338 return v;
339}
340
341std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
342 std::vector<InterfaceAndMethod> v;
343 std::vector<const Interface *> chain = typeChain();
344 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
345 const Interface *iface = *it;
346 for (Method *userMethod : iface->userDefinedMethods()) {
347 v.push_back(InterfaceAndMethod(iface, userMethod));
348 }
349 }
350 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800351 v.push_back(InterfaceAndMethod(
352 *chain.rbegin(), // IBase
353 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700354 }
355 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700356}
357
Steven Moreland14ee6742016-10-18 12:58:28 -0700358Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700359 for (const auto &tuple : allMethodsFromRoot()) {
360 Method *method = tuple.method();
361 if (method->name() == name) {
362 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700363 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700364 }
365
366 return nullptr;
367}
368
Steven Moreland40786312016-08-16 10:29:40 -0700369std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700370 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700371}
372
Yifan Hongeefe4f22017-01-04 15:32:42 -0800373std::string Interface::getProxyName() const {
374 return fqName().getInterfaceProxyName();
375}
376
377std::string Interface::getStubName() const {
378 return fqName().getInterfaceStubName();
379}
380
381std::string Interface::getHwName() const {
382 return fqName().getInterfaceHwName();
383}
384
385std::string Interface::getPassthroughName() const {
386 return fqName().getInterfacePassthroughName();
387}
388
Yifan Hong51a65092017-01-04 15:41:44 -0800389FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800390 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800391}
392
Yifan Hong51a65092017-01-04 15:41:44 -0800393FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800394 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800395}
396
Yifan Hong51a65092017-01-04 15:41:44 -0800397FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800398 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800399}
400
Steven Moreland979e0992016-09-07 09:18:08 -0700401std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700402 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700403 const std::string base =
404 std::string(specifyNamespaces ? "::android::" : "")
405 + "sp<"
406 + (specifyNamespaces ? fullName() : partialCppName())
407 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700408
409 switch (mode) {
410 case StorageMode_Stack:
411 case StorageMode_Result:
412 return base;
413
414 case StorageMode_Argument:
415 return "const " + base + "&";
416 }
417}
418
Yifan Hong4ed13472016-11-02 10:44:11 -0700419std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700420 return fullJavaName();
421}
422
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800423std::string Interface::getVtsType() const {
424 if (StringHelper::EndsWith(localName(), "Callback")) {
425 return "TYPE_HIDL_CALLBACK";
426 } else {
427 return "TYPE_HIDL_INTERFACE";
428 }
429}
430
Andreas Huber881227d2016-08-02 14:20:21 -0700431void Interface::emitReaderWriter(
432 Formatter &out,
433 const std::string &name,
434 const std::string &parcelObj,
435 bool parcelObjIsPointer,
436 bool isReader,
437 ErrorMode mode) const {
438 const std::string parcelObjDeref =
439 parcelObj + (parcelObjIsPointer ? "->" : ".");
440
441 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700442 out << "{\n";
443 out.indent();
444
Iliyan Malchev549e2592016-08-10 08:59:12 -0700445 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700446
Andreas Huber8a82ff72016-08-04 10:29:39 -0700447 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700448 << binderName << ";\n";
449
Iliyan Malchev549e2592016-08-10 08:59:12 -0700450 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700451 out << parcelObjDeref
452 << "readNullableStrongBinder(&"
453 << binderName
454 << ");\n";
455
456 handleError(out, mode);
457
458 out << name
459 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100460 << "::android::hardware::fromBinder<"
461 << fqName().cppName()
462 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800463 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100464 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800465 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100466 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700467 << binderName
468 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700469
470 out.unindent();
471 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700472 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200473 out << "if (" << name << " == nullptr) {\n";
474 out.indent();
475 out << "_hidl_err = ";
476 out << parcelObjDeref
477 << "writeStrongBinder(nullptr);\n";
478 out.unindent();
479 out << "} else {\n";
480 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800481 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
482 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800483 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100484 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800485 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800486 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800487 << ">("
488 << name
489 << ");\n";
490 });
491 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800492 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800493 out << "_hidl_err = "
494 << parcelObjDeref
495 << "writeStrongBinder(_hidl_binder);\n";
496 });
497 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800498 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800499 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
500 });
501 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200502 out.unindent();
503 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700504
Andreas Huber881227d2016-08-02 14:20:21 -0700505 handleError(out, mode);
506 }
507}
508
Andreas Huber2831d512016-08-15 09:33:47 -0700509void Interface::emitJavaReaderWriter(
510 Formatter &out,
511 const std::string &parcelObj,
512 const std::string &argName,
513 bool isReader) const {
514 if (isReader) {
515 out << fullJavaName()
516 << ".asInterface("
517 << parcelObj
518 << ".readStrongBinder());\n";
519 } else {
520 out << parcelObj
521 << ".writeStrongBinder("
522 << argName
523 << " == null ? null : "
524 << argName
525 << ".asBinder());\n";
526 }
527}
528
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700529status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
530 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700531 // Skip for TypeDef as it is just an alias of a defined type.
532 if (type->isTypeDef()) {
533 continue;
534 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700535 out << "attribute: {\n";
536 out.indent();
537 status_t status = type->emitVtsTypeDeclarations(out);
538 if (status != OK) {
539 return status;
540 }
541 out.unindent();
542 out << "}\n\n";
543 }
544 return OK;
545}
546
547status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700548 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800549 if (method->isHidlReserved()) {
550 continue;
551 }
552
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700553 out << "api: {\n";
554 out.indent();
555 out << "name: \"" << method->name() << "\"\n";
556 // Generate declaration for each return value.
557 for (const auto &result : method->results()) {
558 out << "return_type_hidl: {\n";
559 out.indent();
560 status_t status = result->type().emitVtsAttributeType(out);
561 if (status != OK) {
562 return status;
563 }
564 out.unindent();
565 out << "}\n";
566 }
567 // Generate declaration for each input argument
568 for (const auto &arg : method->args()) {
569 out << "arg: {\n";
570 out.indent();
571 status_t status = arg->type().emitVtsAttributeType(out);
572 if (status != OK) {
573 return status;
574 }
575 out.unindent();
576 out << "}\n";
577 }
578 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700579 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700580 out << "callflow: {\n";
581 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700582 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700583 if (name == "entry") {
584 out << "entry: true\n";
585 } else if (name == "exit") {
586 out << "exit: true\n";
587 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700588 const AnnotationParam *param =
589 annotation->getParam("next");
590 if (param != nullptr) {
591 for (auto value : *param->getValues()) {
592 out << "next: " << value << "\n";
593 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700594 }
595 } else {
596 std::cerr << "Invalid annotation '"
597 << name << "' for method: " << method->name()
598 << ". Should be one of: entry, exit, callflow. \n";
599 return UNKNOWN_ERROR;
600 }
601 out.unindent();
602 out << "}\n";
603 }
604 out.unindent();
605 out << "}\n\n";
606 }
607 return OK;
608}
609
610status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800611 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700612 << "predefined_type: \""
613 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700614 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800615 << "is_callback: "
616 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
617 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700618 return OK;
619}
620
Steven Moreland69e7c702016-09-09 11:16:32 -0700621bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700622 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700623 if (method->isOneway()) {
624 return true;
625 }
626 }
627
628 const Interface* superClass = superType();
629
630 if (superClass != nullptr) {
631 return superClass->hasOnewayMethods();
632 }
633
634 return false;
635}
636
Andreas Huber70a59e12016-08-16 12:57:01 -0700637bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700638 if (mIsJavaCompatibleInProgress) {
639 // We're currently trying to determine if this Interface is
640 // java-compatible and something is referencing this interface through
641 // one of its methods. Assume we'll ultimately succeed, if we were wrong
642 // the original invocation of Interface::isJavaCompatible() will then
643 // return the correct "false" result.
644 return true;
645 }
646
Andreas Huber0fa9e392016-08-31 09:05:44 -0700647 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
648 mIsJavaCompatibleInProgress = false;
649 return false;
650 }
651
Andreas Huberea081b32016-08-17 15:57:47 -0700652 mIsJavaCompatibleInProgress = true;
653
Andreas Huber70a59e12016-08-16 12:57:01 -0700654 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700655 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700656 return false;
657 }
658
Yifan Hong10fe0b52016-10-19 14:20:17 -0700659 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700660 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700661 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700662 return false;
663 }
664 }
665
Andreas Huberea081b32016-08-17 15:57:47 -0700666 mIsJavaCompatibleInProgress = false;
667
Andreas Huber70a59e12016-08-16 12:57:01 -0700668 return true;
669}
670
Andreas Huberc9410c72016-07-28 12:18:40 -0700671} // namespace android
672