blob: ad88e3121f27c478bf1bb64304639e60f0c85680 [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) {
Martijn Coenenaf712c02016-11-16 15:26:27 +010062}
63
Steven Moreland30bb6a82016-11-30 09:18:34 -080064std::string Interface::typeName() const {
65 return "interface " + localName();
66}
67
Yifan Hongffa91392017-01-31 13:41:23 -080068bool Interface::fillLinkToDeathMethod(Method *method) const {
69 if (method->name() != "linkToDeath") {
70 return false;
71 }
Martijn Coenen115d4282016-12-19 05:14:04 +010072
Yifan Hongffa91392017-01-31 13:41:23 -080073 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +010074 HIDL_LINK_TO_DEATH_TRANSACTION,
75 {
76 {IMPL_HEADER,
77 [](auto &out) {
78 out << "(void)cookie;\n"
79 << "return (recipient != nullptr);\n";
80 }
81 },
82 {IMPL_PROXY,
83 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +010084 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +010085 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
86 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
87 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
88 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
89 << "return (remote()->linkToDeath(binder_recipient)"
90 << " == ::android::OK);\n";
91 }
92 },
Martijn Coenen8d12b502016-12-27 14:30:27 +010093 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +010094 }, /*cppImpl*/
95 {
96 {IMPL_HEADER,
97 [this](auto &out) {
98 out << "return true;";
99 }
100 },
101 {IMPL_PROXY,
102 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100103 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100104 }
105 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100106 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100107 } /*javaImpl*/
108 );
Yifan Hongffa91392017-01-31 13:41:23 -0800109 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100110}
111
Yifan Hongffa91392017-01-31 13:41:23 -0800112bool Interface::fillUnlinkToDeathMethod(Method *method) const {
113 if (method->name() != "unlinkToDeath") {
114 return false;
115 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100116
Yifan Hongffa91392017-01-31 13:41:23 -0800117 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100118 HIDL_UNLINK_TO_DEATH_TRANSACTION,
119 {
120 {IMPL_HEADER,
121 [](auto &out) {
122 out << "return (recipient != nullptr);\n";
123 }
124 },
125 {IMPL_PROXY,
126 [](auto &out) {
127 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
128 << "for (auto it = _hidl_mDeathRecipients.begin();"
129 << "it != _hidl_mDeathRecipients.end();"
130 << "++it) {\n";
131 out.indent([&] {
132 out.sIf("(*it)->getRecipient() == recipient", [&] {
133 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
134 << "_hidl_mDeathRecipients.erase(it);\n"
135 << "return status == ::android::OK;\n";
136 });
137 });
138 out << "}\n";
139 out << "return false;\n";
140 }
141 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100142 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100143 }, /*cppImpl*/
144 {
145 {IMPL_HEADER,
146 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100147 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100148 }
149 },
150 {IMPL_PROXY,
151 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100152 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100153 }
154 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100155 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100156 } /*javaImpl*/
157 );
Yifan Hongffa91392017-01-31 13:41:23 -0800158 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100159}
Yifan Hongffa91392017-01-31 13:41:23 -0800160bool Interface::fillSyspropsChangedMethod(Method *method) const {
161 if (method->name() != "notifySyspropsChanged") {
162 return false;
163 }
164
165 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100166 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100167 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100168 out << "::android::report_sysprop_change();\n";
169 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100170 } } }, /*cppImpl */
171 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100172 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100173 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100174 );
Yifan Hongffa91392017-01-31 13:41:23 -0800175 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700176}
177
Yifan Hongffa91392017-01-31 13:41:23 -0800178bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
179 if (method->name() != "setHALInstrumentation") {
180 return false;
181 }
182
183 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800184 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
185 {
186 {IMPL_HEADER,
187 [this](auto &out) {
188 // do nothing for base class.
189 out << "return ::android::hardware::Void();\n";
190 }
191 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800192 {IMPL_STUB,
193 [](auto &out) {
194 out << "configureInstrumentation();\n";
195 }
196 },
197 {IMPL_PASSTHROUGH,
198 [](auto &out) {
199 out << "configureInstrumentation();\n";
200 out << "return ::android::hardware::Void();\n";
201 }
202 },
203 }, /*cppImpl */
204 { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */
205 // Not support for Java Impl for now.
206 } } } /*javaImpl */
207 );
Yifan Hongffa91392017-01-31 13:41:23 -0800208 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800209}
210
Yifan Hongffa91392017-01-31 13:41:23 -0800211bool Interface::fillDescriptorChainMethod(Method *method) const {
212 if (method->name() != "interfaceChain") {
213 return false;
214 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700215
Yifan Hongffa91392017-01-31 13:41:23 -0800216 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700217 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100218 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700219 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800220 out << "_hidl_cb(";
221 out.block([&] {
222 for (const Interface *iface : chain) {
223 out << iface->fullName() << "::descriptor,\n";
224 }
225 });
226 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700227 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100228 } } }, /* cppImpl */
229 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700230 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800231 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700232 out.indent(); out.indent();
233 for (size_t i = 0; i < chain.size(); ++i) {
234 if (i != 0)
235 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800236 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700237 }
238 out << "));";
239 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800240 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100241 );
Yifan Hongffa91392017-01-31 13:41:23 -0800242 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700243}
244
Yifan Hongffa91392017-01-31 13:41:23 -0800245bool Interface::fillGetDescriptorMethod(Method *method) const {
246 if (method->name() != "interfaceDescriptor") {
247 return false;
248 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800249
Yifan Hongffa91392017-01-31 13:41:23 -0800250 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800251 HIDL_GET_DESCRIPTOR_TRANSACTION,
252 { { IMPL_HEADER, [this](auto &out) {
253 out << "_hidl_cb("
254 << fullName()
255 << "::descriptor);\n"
256 << "return ::android::hardware::Void();";
257 } } }, /* cppImpl */
258 { { IMPL_HEADER, [this](auto &out) {
259 out << "return "
260 << fullJavaName()
261 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800262 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800263 );
Yifan Hongffa91392017-01-31 13:41:23 -0800264 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800265}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700266
Yifan Hongffa91392017-01-31 13:41:23 -0800267static std::map<std::string, Method *> gAllReservedMethods;
268
Steven Moreland14ee6742016-10-18 12:58:28 -0700269bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800270 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800271 if (!gAllReservedMethods.emplace(method->name(), method).second) {
272 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
273 << method->name();
274 return false;
275 }
276 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800277 return true;
278 }
279
Yifan Hong10fe0b52016-10-19 14:20:17 -0700280 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700281 if (lookupMethod(method->name()) != nullptr) {
282 LOG(ERROR) << "Redefinition of method " << method->name();
283 return false;
284 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700285 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700286
Yifan Hong10fe0b52016-10-19 14:20:17 -0700287 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700288
Yifan Hong10fe0b52016-10-19 14:20:17 -0700289 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700290 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700291 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700292 ancestor = ancestor->superType();
293 }
294
Yifan Hong10fe0b52016-10-19 14:20:17 -0700295 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
296 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700297 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700298 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700299
300 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700301}
302
Yifan Hongffa91392017-01-31 13:41:23 -0800303bool Interface::addAllReservedMethods() {
304 // use a sorted map to insert them in serial ID order.
305 std::map<int32_t, Method *> reservedMethodsById;
306 for (const auto &pair : gAllReservedMethods) {
307 Method *method = pair.second->copySignature();
308 bool fillSuccess = fillDescriptorChainMethod(method)
309 || fillGetDescriptorMethod(method)
310 || fillSyspropsChangedMethod(method)
311 || fillLinkToDeathMethod(method)
312 || fillUnlinkToDeathMethod(method)
313 || fillSetHALInstrumentationMethod(method);
314 if (!fillSuccess) {
315 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
316 << method->name();
317 return false;
318 }
319 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
320 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
321 << method->name() << " and "
322 << reservedMethodsById[method->getSerialId()]->name()
323 << ", serialId = " << method->getSerialId();
324 return false;
325 }
326 }
327 for (const auto &pair : reservedMethodsById) {
328 this->mReservedMethods.push_back(pair.second);
329 }
330 return true;
331}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700332
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700333const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700334 return mSuperType;
335}
336
Yifan Hong10fe0b52016-10-19 14:20:17 -0700337std::vector<const Interface *> Interface::typeChain() const {
338 std::vector<const Interface *> v;
339 const Interface *iface = this;
340 while (iface != nullptr) {
341 v.push_back(iface);
342 iface = iface->mSuperType;
343 }
344 return v;
345}
346
Yifan Hongfe95aa22016-10-19 17:26:45 -0700347std::vector<const Interface *> Interface::superTypeChain() const {
348 return superType()->typeChain(); // should work even if superType is nullptr
349}
350
Martijn Coenenb40ef022017-01-02 15:21:46 +0100351bool Interface::isElidableType() const {
352 return true;
353}
354
Andreas Hubera2723d22016-07-29 15:36:07 -0700355bool Interface::isInterface() const {
356 return true;
357}
358
Andreas Huber295ad302016-08-16 11:35:00 -0700359bool Interface::isBinder() const {
360 return true;
361}
362
Yifan Hong10fe0b52016-10-19 14:20:17 -0700363const std::vector<Method *> &Interface::userDefinedMethods() const {
364 return mUserMethods;
365}
366
367const std::vector<Method *> &Interface::hidlReservedMethods() const {
368 return mReservedMethods;
369}
370
371std::vector<Method *> Interface::methods() const {
372 std::vector<Method *> v(mUserMethods);
373 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
374 return v;
375}
376
377std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
378 std::vector<InterfaceAndMethod> v;
379 std::vector<const Interface *> chain = typeChain();
380 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
381 const Interface *iface = *it;
382 for (Method *userMethod : iface->userDefinedMethods()) {
383 v.push_back(InterfaceAndMethod(iface, userMethod));
384 }
385 }
386 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800387 v.push_back(InterfaceAndMethod(
388 *chain.rbegin(), // IBase
389 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700390 }
391 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700392}
393
Steven Moreland14ee6742016-10-18 12:58:28 -0700394Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700395 for (const auto &tuple : allMethodsFromRoot()) {
396 Method *method = tuple.method();
397 if (method->name() == name) {
398 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700399 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700400 }
401
402 return nullptr;
403}
404
Steven Moreland40786312016-08-16 10:29:40 -0700405std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700406 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700407}
408
Yifan Hongeefe4f22017-01-04 15:32:42 -0800409std::string Interface::getProxyName() const {
410 return fqName().getInterfaceProxyName();
411}
412
413std::string Interface::getStubName() const {
414 return fqName().getInterfaceStubName();
415}
416
417std::string Interface::getHwName() const {
418 return fqName().getInterfaceHwName();
419}
420
421std::string Interface::getPassthroughName() const {
422 return fqName().getInterfacePassthroughName();
423}
424
Yifan Hong51a65092017-01-04 15:41:44 -0800425FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800426 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800427}
428
Yifan Hong51a65092017-01-04 15:41:44 -0800429FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800430 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800431}
432
Yifan Hong51a65092017-01-04 15:41:44 -0800433FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800434 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800435}
436
Steven Moreland979e0992016-09-07 09:18:08 -0700437std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700438 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700439 const std::string base =
440 std::string(specifyNamespaces ? "::android::" : "")
441 + "sp<"
442 + (specifyNamespaces ? fullName() : partialCppName())
443 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700444
445 switch (mode) {
446 case StorageMode_Stack:
447 case StorageMode_Result:
448 return base;
449
450 case StorageMode_Argument:
451 return "const " + base + "&";
452 }
453}
454
Yifan Hong4ed13472016-11-02 10:44:11 -0700455std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700456 return fullJavaName();
457}
458
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800459std::string Interface::getVtsType() const {
460 if (StringHelper::EndsWith(localName(), "Callback")) {
461 return "TYPE_HIDL_CALLBACK";
462 } else {
463 return "TYPE_HIDL_INTERFACE";
464 }
465}
466
Andreas Huber881227d2016-08-02 14:20:21 -0700467void Interface::emitReaderWriter(
468 Formatter &out,
469 const std::string &name,
470 const std::string &parcelObj,
471 bool parcelObjIsPointer,
472 bool isReader,
473 ErrorMode mode) const {
474 const std::string parcelObjDeref =
475 parcelObj + (parcelObjIsPointer ? "->" : ".");
476
477 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700478 out << "{\n";
479 out.indent();
480
Iliyan Malchev549e2592016-08-10 08:59:12 -0700481 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700482
Andreas Huber8a82ff72016-08-04 10:29:39 -0700483 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700484 << binderName << ";\n";
485
Iliyan Malchev549e2592016-08-10 08:59:12 -0700486 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700487 out << parcelObjDeref
488 << "readNullableStrongBinder(&"
489 << binderName
490 << ");\n";
491
492 handleError(out, mode);
493
494 out << name
495 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100496 << "::android::hardware::fromBinder<"
497 << fqName().cppName()
498 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800499 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100500 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800501 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100502 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700503 << binderName
504 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700505
506 out.unindent();
507 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700508 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200509 out << "if (" << name << " == nullptr) {\n";
510 out.indent();
511 out << "_hidl_err = ";
512 out << parcelObjDeref
513 << "writeStrongBinder(nullptr);\n";
514 out.unindent();
515 out << "} else {\n";
516 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800517 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
518 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800519 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100520 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800521 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800522 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800523 << ">("
524 << name
525 << ");\n";
526 });
527 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800528 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800529 out << "_hidl_err = "
530 << parcelObjDeref
531 << "writeStrongBinder(_hidl_binder);\n";
532 });
533 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800534 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800535 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
536 });
537 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200538 out.unindent();
539 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700540
Andreas Huber881227d2016-08-02 14:20:21 -0700541 handleError(out, mode);
542 }
543}
544
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800545status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
546 status_t status = Scope::emitGlobalTypeDeclarations(out);
547 if (status != OK) {
548 return status;
549 }
550 out << "std::string toString("
551 << getCppArgumentType()
552 << ");\n";
553 return OK;
554}
555
556
557status_t Interface::emitTypeDefinitions(
558 Formatter &out, const std::string prefix) const {
559 std::string space = prefix.empty() ? "" : (prefix + "::");
560 status_t err = Scope::emitTypeDefinitions(out, space + localName());
561 if (err != OK) {
562 return err;
563 }
564
565 out << "std::string toString("
566 << getCppArgumentType()
567 << " o) ";
568
569 out.block([&] {
570 out << "std::string os;\nbool ok = false;\n";
571 // TODO b/34136228 use interfaceDescriptor instead
572 out << "auto ret = o->interfaceChain([&os, &ok] (const auto &chain) ";
573 out.block([&] {
574 out.sIf("chain.size() >= 1", [&] {
575 out << "os += chain[0].c_str();\n"
576 << "ok = true;\n";
577 }).endl();
578 });
579 out << ");\n";
580 out.sIf("!ret.isOk() || !ok", [&] {
581 out << "os += \"[class or subclass of \";\n"
582 << "os += " << fullName() << "::descriptor;\n"
583 << "os += \"]\";\n";
584 }).endl();
585 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
586 << "return os;\n";
587 }).endl().endl();
588
589 return OK;
590}
591
Andreas Huber2831d512016-08-15 09:33:47 -0700592void Interface::emitJavaReaderWriter(
593 Formatter &out,
594 const std::string &parcelObj,
595 const std::string &argName,
596 bool isReader) const {
597 if (isReader) {
598 out << fullJavaName()
599 << ".asInterface("
600 << parcelObj
601 << ".readStrongBinder());\n";
602 } else {
603 out << parcelObj
604 << ".writeStrongBinder("
605 << argName
606 << " == null ? null : "
607 << argName
608 << ".asBinder());\n";
609 }
610}
611
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700612status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
613 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700614 // Skip for TypeDef as it is just an alias of a defined type.
615 if (type->isTypeDef()) {
616 continue;
617 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700618 out << "attribute: {\n";
619 out.indent();
620 status_t status = type->emitVtsTypeDeclarations(out);
621 if (status != OK) {
622 return status;
623 }
624 out.unindent();
625 out << "}\n\n";
626 }
627 return OK;
628}
629
630status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700631 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800632 if (method->isHidlReserved()) {
633 continue;
634 }
635
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700636 out << "api: {\n";
637 out.indent();
638 out << "name: \"" << method->name() << "\"\n";
639 // Generate declaration for each return value.
640 for (const auto &result : method->results()) {
641 out << "return_type_hidl: {\n";
642 out.indent();
643 status_t status = result->type().emitVtsAttributeType(out);
644 if (status != OK) {
645 return status;
646 }
647 out.unindent();
648 out << "}\n";
649 }
650 // Generate declaration for each input argument
651 for (const auto &arg : method->args()) {
652 out << "arg: {\n";
653 out.indent();
654 status_t status = arg->type().emitVtsAttributeType(out);
655 if (status != OK) {
656 return status;
657 }
658 out.unindent();
659 out << "}\n";
660 }
661 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700662 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700663 out << "callflow: {\n";
664 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700665 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700666 if (name == "entry") {
667 out << "entry: true\n";
668 } else if (name == "exit") {
669 out << "exit: true\n";
670 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700671 const AnnotationParam *param =
672 annotation->getParam("next");
673 if (param != nullptr) {
674 for (auto value : *param->getValues()) {
675 out << "next: " << value << "\n";
676 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700677 }
678 } else {
679 std::cerr << "Invalid annotation '"
680 << name << "' for method: " << method->name()
681 << ". Should be one of: entry, exit, callflow. \n";
682 return UNKNOWN_ERROR;
683 }
684 out.unindent();
685 out << "}\n";
686 }
687 out.unindent();
688 out << "}\n\n";
689 }
690 return OK;
691}
692
693status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800694 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700695 << "predefined_type: \""
696 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700697 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800698 << "is_callback: "
699 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
700 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700701 return OK;
702}
703
Steven Moreland69e7c702016-09-09 11:16:32 -0700704bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700705 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700706 if (method->isOneway()) {
707 return true;
708 }
709 }
710
711 const Interface* superClass = superType();
712
713 if (superClass != nullptr) {
714 return superClass->hasOnewayMethods();
715 }
716
717 return false;
718}
719
Andreas Huber70a59e12016-08-16 12:57:01 -0700720bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700721 if (mIsJavaCompatibleInProgress) {
722 // We're currently trying to determine if this Interface is
723 // java-compatible and something is referencing this interface through
724 // one of its methods. Assume we'll ultimately succeed, if we were wrong
725 // the original invocation of Interface::isJavaCompatible() will then
726 // return the correct "false" result.
727 return true;
728 }
729
Andreas Huber0fa9e392016-08-31 09:05:44 -0700730 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
731 mIsJavaCompatibleInProgress = false;
732 return false;
733 }
734
Andreas Huberea081b32016-08-17 15:57:47 -0700735 mIsJavaCompatibleInProgress = true;
736
Andreas Huber70a59e12016-08-16 12:57:01 -0700737 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700738 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700739 return false;
740 }
741
Yifan Hong10fe0b52016-10-19 14:20:17 -0700742 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700743 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700744 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700745 return false;
746 }
747 }
748
Andreas Huberea081b32016-08-17 15:57:47 -0700749 mIsJavaCompatibleInProgress = false;
750
Andreas Huber70a59e12016-08-16 12:57:01 -0700751 return true;
752}
753
Andreas Huberc9410c72016-07-28 12:18:40 -0700754} // namespace android
755