blob: 01cc06397c53a0edd731b23beb480df0d9107699 [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,
Yifan Hongc75fd472017-01-11 12:37:31 -080050 HIDL_GET_DESCRIPTOR_TRANSACTION,
Martijn Coenenaf712c02016-11-16 15:26:27 +010051 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +010052 HIDL_LINK_TO_DEATH_TRANSACTION,
53 HIDL_UNLINK_TO_DEATH_TRANSACTION,
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080054 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
Yifan Hong10fe0b52016-10-19 14:20:17 -070055 LAST_HIDL_TRANSACTION = 0x00ffffff,
56};
57
Yifan Honga4b53d02016-10-31 17:29:10 -070058Interface::Interface(const char *localName, const Location &location, Interface *super)
59 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070060 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070061 mIsJavaCompatibleInProgress(false) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070062 mReservedMethods.push_back(createDescriptorChainMethod());
Yifan Hongc75fd472017-01-11 12:37:31 -080063 mReservedMethods.push_back(createGetDescriptorMethod());
Martijn Coenenaf712c02016-11-16 15:26:27 +010064 mReservedMethods.push_back(createSyspropsChangedMethod());
Martijn Coenen115d4282016-12-19 05:14:04 +010065 mReservedMethods.push_back(createLinkToDeathMethod());
66 mReservedMethods.push_back(createUnlinkToDeathMethod());
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080067 mReservedMethods.push_back(createSetHALInstrumentationMethod());
Martijn Coenenaf712c02016-11-16 15:26:27 +010068}
69
Steven Moreland30bb6a82016-11-30 09:18:34 -080070std::string Interface::typeName() const {
71 return "interface " + localName();
72}
73
Martijn Coenen115d4282016-12-19 05:14:04 +010074Method *Interface::createLinkToDeathMethod() const {
75 auto *results = new std::vector<TypedVar *> {
76 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
77 auto *args = new std::vector<TypedVar *> {
78 new TypedVar("recipient", new DeathRecipientType()),
79 new TypedVar("cookie", new ScalarType(ScalarType::KIND_UINT64)) };
80
81 return new Method("linkToDeath",
82 args,
83 results,
84 false /*oneway */,
85 new std::vector<Annotation *>(),
86 HIDL_LINK_TO_DEATH_TRANSACTION,
87 {
88 {IMPL_HEADER,
89 [](auto &out) {
90 out << "(void)cookie;\n"
91 << "return (recipient != nullptr);\n";
92 }
93 },
94 {IMPL_PROXY,
95 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010096 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +010097 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
98 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
99 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
100 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
101 << "return (remote()->linkToDeath(binder_recipient)"
102 << " == ::android::OK);\n";
103 }
104 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100105 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100106 }, /*cppImpl*/
107 {
108 {IMPL_HEADER,
109 [this](auto &out) {
110 out << "return true;";
111 }
112 },
113 {IMPL_PROXY,
114 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100115 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100116 }
117 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100118 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100119 } /*javaImpl*/
120 );
121}
122
123Method *Interface::createUnlinkToDeathMethod() const {
124 auto *results = new std::vector<TypedVar *> {
125 new TypedVar("success", new ScalarType(ScalarType::KIND_BOOL)) };
126 auto *args = new std::vector<TypedVar *> {
127 new TypedVar("recipient", new DeathRecipientType()) };
128
129 return new Method("unlinkToDeath",
130 args,
131 results,
132 false /*oneway */,
133 new std::vector<Annotation *>(),
134 HIDL_UNLINK_TO_DEATH_TRANSACTION,
135 {
136 {IMPL_HEADER,
137 [](auto &out) {
138 out << "return (recipient != nullptr);\n";
139 }
140 },
141 {IMPL_PROXY,
142 [](auto &out) {
143 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
144 << "for (auto it = _hidl_mDeathRecipients.begin();"
145 << "it != _hidl_mDeathRecipients.end();"
146 << "++it) {\n";
147 out.indent([&] {
148 out.sIf("(*it)->getRecipient() == recipient", [&] {
149 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
150 << "_hidl_mDeathRecipients.erase(it);\n"
151 << "return status == ::android::OK;\n";
152 });
153 });
154 out << "}\n";
155 out << "return false;\n";
156 }
157 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100158 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100159 }, /*cppImpl*/
160 {
161 {IMPL_HEADER,
162 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100163 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100164 }
165 },
166 {IMPL_PROXY,
167 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100168 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100169 }
170 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100171 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100172 } /*javaImpl*/
173 );
174}
Martijn Coenenaf712c02016-11-16 15:26:27 +0100175Method *Interface::createSyspropsChangedMethod() const {
176 return new Method("notifySyspropsChanged",
177 new std::vector<TypedVar *>() /*args */,
178 new std::vector<TypedVar *>() /*results */,
179 true /*oneway */,
180 new std::vector<Annotation *>(),
181 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100182 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100183 out << "::android::report_sysprop_change();\n";
184 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100185 } } }, /*cppImpl */
186 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100187 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100188 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100189 );
Andreas Huberc9410c72016-07-28 12:18:40 -0700190}
191
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800192Method *Interface::createSetHALInstrumentationMethod() const {
193 return new Method("setHALInstrumentation",
194 new std::vector<TypedVar *>() /*args */,
195 new std::vector<TypedVar *>() /*results */,
196 true /*oneway */,
197 new std::vector<Annotation *>(),
198 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
199 {
200 {IMPL_HEADER,
201 [this](auto &out) {
202 // do nothing for base class.
203 out << "return ::android::hardware::Void();\n";
204 }
205 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800206 {IMPL_STUB,
207 [](auto &out) {
208 out << "configureInstrumentation();\n";
209 }
210 },
211 {IMPL_PASSTHROUGH,
212 [](auto &out) {
213 out << "configureInstrumentation();\n";
214 out << "return ::android::hardware::Void();\n";
215 }
216 },
217 }, /*cppImpl */
218 { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */
219 // Not support for Java Impl for now.
220 } } } /*javaImpl */
221 );
222}
223
Yifan Hong10fe0b52016-10-19 14:20:17 -0700224Method *Interface::createDescriptorChainMethod() const {
225 VectorType *vecType = new VectorType();
226 vecType->setElementType(new StringType());
227 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
Yifan Hongc75fd472017-01-11 12:37:31 -0800228 results->push_back(new TypedVar("descriptors", vecType));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700229
230 return new Method("interfaceChain",
231 new std::vector<TypedVar *>() /* args */,
232 results,
233 false /* oneway */,
234 new std::vector<Annotation *>(),
235 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100236 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700237 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800238 out << "_hidl_cb(";
239 out.block([&] {
240 for (const Interface *iface : chain) {
241 out << iface->fullName() << "::descriptor,\n";
242 }
243 });
244 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700245 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100246 } } }, /* cppImpl */
247 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700248 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800249 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700250 out.indent(); out.indent();
251 for (size_t i = 0; i < chain.size(); ++i) {
252 if (i != 0)
253 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800254 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700255 }
256 out << "));";
257 out.unindent(); out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100258 } } } /* javaImpl */
259 );
Yifan Hong10fe0b52016-10-19 14:20:17 -0700260}
261
Yifan Hongc75fd472017-01-11 12:37:31 -0800262Method *Interface::createGetDescriptorMethod() const {
263 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
264 results->push_back(new TypedVar("descriptor", new StringType()));
265
266 return new Method("interfaceDescriptor",
267 new std::vector<TypedVar *>() /* args */,
268 results,
269 false /* oneway */,
270 new std::vector<Annotation *>(),
271 HIDL_GET_DESCRIPTOR_TRANSACTION,
272 { { IMPL_HEADER, [this](auto &out) {
273 out << "_hidl_cb("
274 << fullName()
275 << "::descriptor);\n"
276 << "return ::android::hardware::Void();";
277 } } }, /* cppImpl */
278 { { IMPL_HEADER, [this](auto &out) {
279 out << "return "
280 << fullJavaName()
281 << ".kInterfaceName;\n";
282 } } } /* javaImpl */
283 );
284}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700285
Steven Moreland14ee6742016-10-18 12:58:28 -0700286bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800287 if (isIBase()) {
288 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
289 return true;
290 }
291
Yifan Hong10fe0b52016-10-19 14:20:17 -0700292 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700293 if (lookupMethod(method->name()) != nullptr) {
294 LOG(ERROR) << "Redefinition of method " << method->name();
295 return false;
296 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700297 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700298
Yifan Hong10fe0b52016-10-19 14:20:17 -0700299 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700300
Yifan Hong10fe0b52016-10-19 14:20:17 -0700301 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700302 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700303 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700304 ancestor = ancestor->superType();
305 }
306
Yifan Hong10fe0b52016-10-19 14:20:17 -0700307 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
308 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700309 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700310 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700311
312 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700313}
314
Yifan Hong10fe0b52016-10-19 14:20:17 -0700315
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700316const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700317 return mSuperType;
318}
319
Yifan Hong10fe0b52016-10-19 14:20:17 -0700320std::vector<const Interface *> Interface::typeChain() const {
321 std::vector<const Interface *> v;
322 const Interface *iface = this;
323 while (iface != nullptr) {
324 v.push_back(iface);
325 iface = iface->mSuperType;
326 }
327 return v;
328}
329
Yifan Hongfe95aa22016-10-19 17:26:45 -0700330std::vector<const Interface *> Interface::superTypeChain() const {
331 return superType()->typeChain(); // should work even if superType is nullptr
332}
333
Martijn Coenenb40ef022017-01-02 15:21:46 +0100334bool Interface::isElidableType() const {
335 return true;
336}
337
Andreas Hubera2723d22016-07-29 15:36:07 -0700338bool Interface::isInterface() const {
339 return true;
340}
341
Andreas Huber295ad302016-08-16 11:35:00 -0700342bool Interface::isBinder() const {
343 return true;
344}
345
Yifan Hong10fe0b52016-10-19 14:20:17 -0700346const std::vector<Method *> &Interface::userDefinedMethods() const {
347 return mUserMethods;
348}
349
350const std::vector<Method *> &Interface::hidlReservedMethods() const {
351 return mReservedMethods;
352}
353
354std::vector<Method *> Interface::methods() const {
355 std::vector<Method *> v(mUserMethods);
356 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
357 return v;
358}
359
360std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
361 std::vector<InterfaceAndMethod> v;
362 std::vector<const Interface *> chain = typeChain();
363 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
364 const Interface *iface = *it;
365 for (Method *userMethod : iface->userDefinedMethods()) {
366 v.push_back(InterfaceAndMethod(iface, userMethod));
367 }
368 }
369 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800370 v.push_back(InterfaceAndMethod(
371 *chain.rbegin(), // IBase
372 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700373 }
374 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700375}
376
Steven Moreland14ee6742016-10-18 12:58:28 -0700377Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700378 for (const auto &tuple : allMethodsFromRoot()) {
379 Method *method = tuple.method();
380 if (method->name() == name) {
381 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700382 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700383 }
384
385 return nullptr;
386}
387
Steven Moreland40786312016-08-16 10:29:40 -0700388std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700389 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700390}
391
Yifan Hongeefe4f22017-01-04 15:32:42 -0800392std::string Interface::getProxyName() const {
393 return fqName().getInterfaceProxyName();
394}
395
396std::string Interface::getStubName() const {
397 return fqName().getInterfaceStubName();
398}
399
400std::string Interface::getHwName() const {
401 return fqName().getInterfaceHwName();
402}
403
404std::string Interface::getPassthroughName() const {
405 return fqName().getInterfacePassthroughName();
406}
407
Yifan Hong51a65092017-01-04 15:41:44 -0800408FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800409 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800410}
411
Yifan Hong51a65092017-01-04 15:41:44 -0800412FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800413 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800414}
415
Yifan Hong51a65092017-01-04 15:41:44 -0800416FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800417 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800418}
419
Steven Moreland979e0992016-09-07 09:18:08 -0700420std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700421 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700422 const std::string base =
423 std::string(specifyNamespaces ? "::android::" : "")
424 + "sp<"
425 + (specifyNamespaces ? fullName() : partialCppName())
426 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700427
428 switch (mode) {
429 case StorageMode_Stack:
430 case StorageMode_Result:
431 return base;
432
433 case StorageMode_Argument:
434 return "const " + base + "&";
435 }
436}
437
Yifan Hong4ed13472016-11-02 10:44:11 -0700438std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700439 return fullJavaName();
440}
441
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800442std::string Interface::getVtsType() const {
443 if (StringHelper::EndsWith(localName(), "Callback")) {
444 return "TYPE_HIDL_CALLBACK";
445 } else {
446 return "TYPE_HIDL_INTERFACE";
447 }
448}
449
Andreas Huber881227d2016-08-02 14:20:21 -0700450void Interface::emitReaderWriter(
451 Formatter &out,
452 const std::string &name,
453 const std::string &parcelObj,
454 bool parcelObjIsPointer,
455 bool isReader,
456 ErrorMode mode) const {
457 const std::string parcelObjDeref =
458 parcelObj + (parcelObjIsPointer ? "->" : ".");
459
460 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700461 out << "{\n";
462 out.indent();
463
Iliyan Malchev549e2592016-08-10 08:59:12 -0700464 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700465
Andreas Huber8a82ff72016-08-04 10:29:39 -0700466 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700467 << binderName << ";\n";
468
Iliyan Malchev549e2592016-08-10 08:59:12 -0700469 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700470 out << parcelObjDeref
471 << "readNullableStrongBinder(&"
472 << binderName
473 << ");\n";
474
475 handleError(out, mode);
476
477 out << name
478 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100479 << "::android::hardware::fromBinder<"
480 << fqName().cppName()
481 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800482 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100483 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800484 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100485 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700486 << binderName
487 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700488
489 out.unindent();
490 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700491 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200492 out << "if (" << name << " == nullptr) {\n";
493 out.indent();
494 out << "_hidl_err = ";
495 out << parcelObjDeref
496 << "writeStrongBinder(nullptr);\n";
497 out.unindent();
498 out << "} else {\n";
499 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800500 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
501 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800502 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100503 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800504 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800505 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800506 << ">("
507 << name
508 << ");\n";
509 });
510 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800511 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800512 out << "_hidl_err = "
513 << parcelObjDeref
514 << "writeStrongBinder(_hidl_binder);\n";
515 });
516 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800517 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800518 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
519 });
520 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200521 out.unindent();
522 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700523
Andreas Huber881227d2016-08-02 14:20:21 -0700524 handleError(out, mode);
525 }
526}
527
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800528status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
529 status_t status = Scope::emitGlobalTypeDeclarations(out);
530 if (status != OK) {
531 return status;
532 }
533 out << "std::string toString("
534 << getCppArgumentType()
535 << ");\n";
536 return OK;
537}
538
539
540status_t Interface::emitTypeDefinitions(
541 Formatter &out, const std::string prefix) const {
542 std::string space = prefix.empty() ? "" : (prefix + "::");
543 status_t err = Scope::emitTypeDefinitions(out, space + localName());
544 if (err != OK) {
545 return err;
546 }
547
548 out << "std::string toString("
549 << getCppArgumentType()
550 << " o) ";
551
552 out.block([&] {
553 out << "std::string os;\nbool ok = false;\n";
554 // TODO b/34136228 use interfaceDescriptor instead
555 out << "auto ret = o->interfaceChain([&os, &ok] (const auto &chain) ";
556 out.block([&] {
557 out.sIf("chain.size() >= 1", [&] {
558 out << "os += chain[0].c_str();\n"
559 << "ok = true;\n";
560 }).endl();
561 });
562 out << ");\n";
563 out.sIf("!ret.isOk() || !ok", [&] {
564 out << "os += \"[class or subclass of \";\n"
565 << "os += " << fullName() << "::descriptor;\n"
566 << "os += \"]\";\n";
567 }).endl();
568 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
569 << "return os;\n";
570 }).endl().endl();
571
572 return OK;
573}
574
Andreas Huber2831d512016-08-15 09:33:47 -0700575void Interface::emitJavaReaderWriter(
576 Formatter &out,
577 const std::string &parcelObj,
578 const std::string &argName,
579 bool isReader) const {
580 if (isReader) {
581 out << fullJavaName()
582 << ".asInterface("
583 << parcelObj
584 << ".readStrongBinder());\n";
585 } else {
586 out << parcelObj
587 << ".writeStrongBinder("
588 << argName
589 << " == null ? null : "
590 << argName
591 << ".asBinder());\n";
592 }
593}
594
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700595status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
596 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700597 // Skip for TypeDef as it is just an alias of a defined type.
598 if (type->isTypeDef()) {
599 continue;
600 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700601 out << "attribute: {\n";
602 out.indent();
603 status_t status = type->emitVtsTypeDeclarations(out);
604 if (status != OK) {
605 return status;
606 }
607 out.unindent();
608 out << "}\n\n";
609 }
610 return OK;
611}
612
613status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700614 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800615 if (method->isHidlReserved()) {
616 continue;
617 }
618
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700619 out << "api: {\n";
620 out.indent();
621 out << "name: \"" << method->name() << "\"\n";
622 // Generate declaration for each return value.
623 for (const auto &result : method->results()) {
624 out << "return_type_hidl: {\n";
625 out.indent();
626 status_t status = result->type().emitVtsAttributeType(out);
627 if (status != OK) {
628 return status;
629 }
630 out.unindent();
631 out << "}\n";
632 }
633 // Generate declaration for each input argument
634 for (const auto &arg : method->args()) {
635 out << "arg: {\n";
636 out.indent();
637 status_t status = arg->type().emitVtsAttributeType(out);
638 if (status != OK) {
639 return status;
640 }
641 out.unindent();
642 out << "}\n";
643 }
644 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700645 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700646 out << "callflow: {\n";
647 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700648 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700649 if (name == "entry") {
650 out << "entry: true\n";
651 } else if (name == "exit") {
652 out << "exit: true\n";
653 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700654 const AnnotationParam *param =
655 annotation->getParam("next");
656 if (param != nullptr) {
657 for (auto value : *param->getValues()) {
658 out << "next: " << value << "\n";
659 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700660 }
661 } else {
662 std::cerr << "Invalid annotation '"
663 << name << "' for method: " << method->name()
664 << ". Should be one of: entry, exit, callflow. \n";
665 return UNKNOWN_ERROR;
666 }
667 out.unindent();
668 out << "}\n";
669 }
670 out.unindent();
671 out << "}\n\n";
672 }
673 return OK;
674}
675
676status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800677 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700678 << "predefined_type: \""
679 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700680 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800681 << "is_callback: "
682 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
683 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700684 return OK;
685}
686
Steven Moreland69e7c702016-09-09 11:16:32 -0700687bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700688 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700689 if (method->isOneway()) {
690 return true;
691 }
692 }
693
694 const Interface* superClass = superType();
695
696 if (superClass != nullptr) {
697 return superClass->hasOnewayMethods();
698 }
699
700 return false;
701}
702
Andreas Huber70a59e12016-08-16 12:57:01 -0700703bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700704 if (mIsJavaCompatibleInProgress) {
705 // We're currently trying to determine if this Interface is
706 // java-compatible and something is referencing this interface through
707 // one of its methods. Assume we'll ultimately succeed, if we were wrong
708 // the original invocation of Interface::isJavaCompatible() will then
709 // return the correct "false" result.
710 return true;
711 }
712
Andreas Huber0fa9e392016-08-31 09:05:44 -0700713 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
714 mIsJavaCompatibleInProgress = false;
715 return false;
716 }
717
Andreas Huberea081b32016-08-17 15:57:47 -0700718 mIsJavaCompatibleInProgress = true;
719
Andreas Huber70a59e12016-08-16 12:57:01 -0700720 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700721 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700722 return false;
723 }
724
Yifan Hong10fe0b52016-10-19 14:20:17 -0700725 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700726 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700727 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700728 return false;
729 }
730 }
731
Andreas Huberea081b32016-08-17 15:57:47 -0700732 mIsJavaCompatibleInProgress = false;
733
Andreas Huber70a59e12016-08-16 12:57:01 -0700734 return true;
735}
736
Andreas Huberc9410c72016-07-28 12:18:40 -0700737} // namespace android
738