blob: 9d7cc89a47dddefa7945f1affdb721dc0ccd1045 [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 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800204 {IMPL_STUB,
205 [](auto &out) {
206 out << "configureInstrumentation();\n";
207 }
208 },
209 {IMPL_PASSTHROUGH,
210 [](auto &out) {
211 out << "configureInstrumentation();\n";
212 out << "return ::android::hardware::Void();\n";
213 }
214 },
215 }, /*cppImpl */
216 { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */
217 // Not support for Java Impl for now.
218 } } } /*javaImpl */
219 );
220}
221
Yifan Hong10fe0b52016-10-19 14:20:17 -0700222Method *Interface::createDescriptorChainMethod() const {
223 VectorType *vecType = new VectorType();
224 vecType->setElementType(new StringType());
225 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
226 results->push_back(new TypedVar("indicator", vecType));
227
228 return new Method("interfaceChain",
229 new std::vector<TypedVar *>() /* args */,
230 results,
231 false /* oneway */,
232 new std::vector<Annotation *>(),
233 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100234 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700235 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800236 out << "_hidl_cb(";
237 out.block([&] {
238 for (const Interface *iface : chain) {
239 out << iface->fullName() << "::descriptor,\n";
240 }
241 });
242 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700243 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100244 } } }, /* cppImpl */
245 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700246 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800247 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700248 out.indent(); out.indent();
249 for (size_t i = 0; i < chain.size(); ++i) {
250 if (i != 0)
251 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800252 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700253 }
254 out << "));";
255 out.unindent(); out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100256 } } } /* javaImpl */
257 );
Yifan Hong10fe0b52016-10-19 14:20:17 -0700258}
259
260
Steven Moreland14ee6742016-10-18 12:58:28 -0700261bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800262 if (isIBase()) {
263 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
264 return true;
265 }
266
Yifan Hong10fe0b52016-10-19 14:20:17 -0700267 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700268 if (lookupMethod(method->name()) != nullptr) {
269 LOG(ERROR) << "Redefinition of method " << method->name();
270 return false;
271 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700272 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700273
Yifan Hong10fe0b52016-10-19 14:20:17 -0700274 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700275
Yifan Hong10fe0b52016-10-19 14:20:17 -0700276 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700277 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700278 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700279 ancestor = ancestor->superType();
280 }
281
Yifan Hong10fe0b52016-10-19 14:20:17 -0700282 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
283 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700284 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700285 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700286
287 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700288}
289
Yifan Hong10fe0b52016-10-19 14:20:17 -0700290
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700291const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700292 return mSuperType;
293}
294
Yifan Hong10fe0b52016-10-19 14:20:17 -0700295std::vector<const Interface *> Interface::typeChain() const {
296 std::vector<const Interface *> v;
297 const Interface *iface = this;
298 while (iface != nullptr) {
299 v.push_back(iface);
300 iface = iface->mSuperType;
301 }
302 return v;
303}
304
Yifan Hongfe95aa22016-10-19 17:26:45 -0700305std::vector<const Interface *> Interface::superTypeChain() const {
306 return superType()->typeChain(); // should work even if superType is nullptr
307}
308
Martijn Coenenb40ef022017-01-02 15:21:46 +0100309bool Interface::isElidableType() const {
310 return true;
311}
312
Andreas Hubera2723d22016-07-29 15:36:07 -0700313bool Interface::isInterface() const {
314 return true;
315}
316
Andreas Huber295ad302016-08-16 11:35:00 -0700317bool Interface::isBinder() const {
318 return true;
319}
320
Yifan Hong10fe0b52016-10-19 14:20:17 -0700321const std::vector<Method *> &Interface::userDefinedMethods() const {
322 return mUserMethods;
323}
324
325const std::vector<Method *> &Interface::hidlReservedMethods() const {
326 return mReservedMethods;
327}
328
329std::vector<Method *> Interface::methods() const {
330 std::vector<Method *> v(mUserMethods);
331 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
332 return v;
333}
334
335std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
336 std::vector<InterfaceAndMethod> v;
337 std::vector<const Interface *> chain = typeChain();
338 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
339 const Interface *iface = *it;
340 for (Method *userMethod : iface->userDefinedMethods()) {
341 v.push_back(InterfaceAndMethod(iface, userMethod));
342 }
343 }
344 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800345 v.push_back(InterfaceAndMethod(
346 *chain.rbegin(), // IBase
347 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700348 }
349 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700350}
351
Steven Moreland14ee6742016-10-18 12:58:28 -0700352Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700353 for (const auto &tuple : allMethodsFromRoot()) {
354 Method *method = tuple.method();
355 if (method->name() == name) {
356 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700357 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700358 }
359
360 return nullptr;
361}
362
Steven Moreland40786312016-08-16 10:29:40 -0700363std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700364 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700365}
366
Yifan Hongeefe4f22017-01-04 15:32:42 -0800367std::string Interface::getProxyName() const {
368 return fqName().getInterfaceProxyName();
369}
370
371std::string Interface::getStubName() const {
372 return fqName().getInterfaceStubName();
373}
374
375std::string Interface::getHwName() const {
376 return fqName().getInterfaceHwName();
377}
378
379std::string Interface::getPassthroughName() const {
380 return fqName().getInterfacePassthroughName();
381}
382
Yifan Hong51a65092017-01-04 15:41:44 -0800383FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800384 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800385}
386
Yifan Hong51a65092017-01-04 15:41:44 -0800387FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800388 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800389}
390
Yifan Hong51a65092017-01-04 15:41:44 -0800391FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800392 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800393}
394
Steven Moreland979e0992016-09-07 09:18:08 -0700395std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700396 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700397 const std::string base =
398 std::string(specifyNamespaces ? "::android::" : "")
399 + "sp<"
400 + (specifyNamespaces ? fullName() : partialCppName())
401 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700402
403 switch (mode) {
404 case StorageMode_Stack:
405 case StorageMode_Result:
406 return base;
407
408 case StorageMode_Argument:
409 return "const " + base + "&";
410 }
411}
412
Yifan Hong4ed13472016-11-02 10:44:11 -0700413std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700414 return fullJavaName();
415}
416
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800417std::string Interface::getVtsType() const {
418 if (StringHelper::EndsWith(localName(), "Callback")) {
419 return "TYPE_HIDL_CALLBACK";
420 } else {
421 return "TYPE_HIDL_INTERFACE";
422 }
423}
424
Andreas Huber881227d2016-08-02 14:20:21 -0700425void Interface::emitReaderWriter(
426 Formatter &out,
427 const std::string &name,
428 const std::string &parcelObj,
429 bool parcelObjIsPointer,
430 bool isReader,
431 ErrorMode mode) const {
432 const std::string parcelObjDeref =
433 parcelObj + (parcelObjIsPointer ? "->" : ".");
434
435 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700436 out << "{\n";
437 out.indent();
438
Iliyan Malchev549e2592016-08-10 08:59:12 -0700439 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700440
Andreas Huber8a82ff72016-08-04 10:29:39 -0700441 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700442 << binderName << ";\n";
443
Iliyan Malchev549e2592016-08-10 08:59:12 -0700444 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700445 out << parcelObjDeref
446 << "readNullableStrongBinder(&"
447 << binderName
448 << ");\n";
449
450 handleError(out, mode);
451
452 out << name
453 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100454 << "::android::hardware::fromBinder<"
455 << fqName().cppName()
456 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800457 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100458 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800459 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100460 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700461 << binderName
462 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700463
464 out.unindent();
465 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700466 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200467 out << "if (" << name << " == nullptr) {\n";
468 out.indent();
469 out << "_hidl_err = ";
470 out << parcelObjDeref
471 << "writeStrongBinder(nullptr);\n";
472 out.unindent();
473 out << "} else {\n";
474 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800475 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
476 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800477 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100478 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800479 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800480 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800481 << ">("
482 << name
483 << ");\n";
484 });
485 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800486 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800487 out << "_hidl_err = "
488 << parcelObjDeref
489 << "writeStrongBinder(_hidl_binder);\n";
490 });
491 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800492 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800493 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
494 });
495 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200496 out.unindent();
497 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700498
Andreas Huber881227d2016-08-02 14:20:21 -0700499 handleError(out, mode);
500 }
501}
502
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800503status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
504 status_t status = Scope::emitGlobalTypeDeclarations(out);
505 if (status != OK) {
506 return status;
507 }
508 out << "std::string toString("
509 << getCppArgumentType()
510 << ");\n";
511 return OK;
512}
513
514
515status_t Interface::emitTypeDefinitions(
516 Formatter &out, const std::string prefix) const {
517 std::string space = prefix.empty() ? "" : (prefix + "::");
518 status_t err = Scope::emitTypeDefinitions(out, space + localName());
519 if (err != OK) {
520 return err;
521 }
522
523 out << "std::string toString("
524 << getCppArgumentType()
525 << " o) ";
526
527 out.block([&] {
528 out << "std::string os;\nbool ok = false;\n";
529 // TODO b/34136228 use interfaceDescriptor instead
530 out << "auto ret = o->interfaceChain([&os, &ok] (const auto &chain) ";
531 out.block([&] {
532 out.sIf("chain.size() >= 1", [&] {
533 out << "os += chain[0].c_str();\n"
534 << "ok = true;\n";
535 }).endl();
536 });
537 out << ");\n";
538 out.sIf("!ret.isOk() || !ok", [&] {
539 out << "os += \"[class or subclass of \";\n"
540 << "os += " << fullName() << "::descriptor;\n"
541 << "os += \"]\";\n";
542 }).endl();
543 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
544 << "return os;\n";
545 }).endl().endl();
546
547 return OK;
548}
549
Andreas Huber2831d512016-08-15 09:33:47 -0700550void Interface::emitJavaReaderWriter(
551 Formatter &out,
552 const std::string &parcelObj,
553 const std::string &argName,
554 bool isReader) const {
555 if (isReader) {
556 out << fullJavaName()
557 << ".asInterface("
558 << parcelObj
559 << ".readStrongBinder());\n";
560 } else {
561 out << parcelObj
562 << ".writeStrongBinder("
563 << argName
564 << " == null ? null : "
565 << argName
566 << ".asBinder());\n";
567 }
568}
569
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700570status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
571 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700572 // Skip for TypeDef as it is just an alias of a defined type.
573 if (type->isTypeDef()) {
574 continue;
575 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700576 out << "attribute: {\n";
577 out.indent();
578 status_t status = type->emitVtsTypeDeclarations(out);
579 if (status != OK) {
580 return status;
581 }
582 out.unindent();
583 out << "}\n\n";
584 }
585 return OK;
586}
587
588status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700589 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800590 if (method->isHidlReserved()) {
591 continue;
592 }
593
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700594 out << "api: {\n";
595 out.indent();
596 out << "name: \"" << method->name() << "\"\n";
597 // Generate declaration for each return value.
598 for (const auto &result : method->results()) {
599 out << "return_type_hidl: {\n";
600 out.indent();
601 status_t status = result->type().emitVtsAttributeType(out);
602 if (status != OK) {
603 return status;
604 }
605 out.unindent();
606 out << "}\n";
607 }
608 // Generate declaration for each input argument
609 for (const auto &arg : method->args()) {
610 out << "arg: {\n";
611 out.indent();
612 status_t status = arg->type().emitVtsAttributeType(out);
613 if (status != OK) {
614 return status;
615 }
616 out.unindent();
617 out << "}\n";
618 }
619 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700620 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700621 out << "callflow: {\n";
622 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700623 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700624 if (name == "entry") {
625 out << "entry: true\n";
626 } else if (name == "exit") {
627 out << "exit: true\n";
628 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700629 const AnnotationParam *param =
630 annotation->getParam("next");
631 if (param != nullptr) {
632 for (auto value : *param->getValues()) {
633 out << "next: " << value << "\n";
634 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700635 }
636 } else {
637 std::cerr << "Invalid annotation '"
638 << name << "' for method: " << method->name()
639 << ". Should be one of: entry, exit, callflow. \n";
640 return UNKNOWN_ERROR;
641 }
642 out.unindent();
643 out << "}\n";
644 }
645 out.unindent();
646 out << "}\n\n";
647 }
648 return OK;
649}
650
651status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800652 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700653 << "predefined_type: \""
654 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700655 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800656 << "is_callback: "
657 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
658 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700659 return OK;
660}
661
Steven Moreland69e7c702016-09-09 11:16:32 -0700662bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700663 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700664 if (method->isOneway()) {
665 return true;
666 }
667 }
668
669 const Interface* superClass = superType();
670
671 if (superClass != nullptr) {
672 return superClass->hasOnewayMethods();
673 }
674
675 return false;
676}
677
Andreas Huber70a59e12016-08-16 12:57:01 -0700678bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700679 if (mIsJavaCompatibleInProgress) {
680 // We're currently trying to determine if this Interface is
681 // java-compatible and something is referencing this interface through
682 // one of its methods. Assume we'll ultimately succeed, if we were wrong
683 // the original invocation of Interface::isJavaCompatible() will then
684 // return the correct "false" result.
685 return true;
686 }
687
Andreas Huber0fa9e392016-08-31 09:05:44 -0700688 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
689 mIsJavaCompatibleInProgress = false;
690 return false;
691 }
692
Andreas Huberea081b32016-08-17 15:57:47 -0700693 mIsJavaCompatibleInProgress = true;
694
Andreas Huber70a59e12016-08-16 12:57:01 -0700695 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700696 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700697 return false;
698 }
699
Yifan Hong10fe0b52016-10-19 14:20:17 -0700700 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700701 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700702 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700703 return false;
704 }
705 }
706
Andreas Huberea081b32016-08-17 15:57:47 -0700707 mIsJavaCompatibleInProgress = false;
708
Andreas Huber70a59e12016-08-16 12:57:01 -0700709 return true;
710}
711
Andreas Huberc9410c72016-07-28 12:18:40 -0700712} // namespace android
713