blob: 57f3ca4c3e6b55f86b636b9bf03387d1f570e9be [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
Steven Morelandb8e15a52017-02-13 19:35:40 -080034#define B_PACK_CHARS(c1, c2, c3, c4) \
35 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
36
Yifan Hong10fe0b52016-10-19 14:20:17 -070037/* It is very important that these values NEVER change. These values
38 * must remain unchanged over the lifetime of android. This is
39 * because the framework on a device will be updated independently of
40 * the hals on a device. If the hals are compiled with one set of
41 * transaction values, and the framework with another, then the
42 * interface between them will be destroyed, and the device will not
43 * work.
44 */
45enum {
46 // These values are defined in hardware::IBinder.
47 /////////////////// User defined transactions
48 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080049 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070050 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080051 FIRST_HIDL_TRANSACTION = 0x0f000000,
52 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
53 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
54 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
55 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
56 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
57 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
58 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
59 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
60 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
61 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070062};
63
Yifan Honga4b53d02016-10-31 17:29:10 -070064Interface::Interface(const char *localName, const Location &location, Interface *super)
65 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070066 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070067 mIsJavaCompatibleInProgress(false) {
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
Steven Moreland424a9482017-02-13 19:20:40 -080074bool Interface::fillPingMethod(Method *method) const {
75 if (method->name() != "ping") {
76 return false;
77 }
78
79 method->fillImplementation(
80 HIDL_PING_TRANSACTION,
81 {
82 {IMPL_HEADER,
83 [](auto &out) {
84 out << "return ::android::hardware::Void();\n";
85 }
86 },
87 {IMPL_STUB_IMPL,
88 [](auto &out) {
89 out << "return ::android::hardware::Void();\n";
90 }
91 }
92 }, /*cppImpl*/
93 {
94 {IMPL_HEADER,
95 [this](auto &out) {
96 out << "return;\n";
97 }
98 },
99 {IMPL_STUB, nullptr /* don't generate code */}
100 } /*javaImpl*/
101 );
102
103 return true;
104}
105
Yifan Hongffa91392017-01-31 13:41:23 -0800106bool Interface::fillLinkToDeathMethod(Method *method) const {
107 if (method->name() != "linkToDeath") {
108 return false;
109 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100110
Yifan Hongffa91392017-01-31 13:41:23 -0800111 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100112 HIDL_LINK_TO_DEATH_TRANSACTION,
113 {
114 {IMPL_HEADER,
115 [](auto &out) {
116 out << "(void)cookie;\n"
117 << "return (recipient != nullptr);\n";
118 }
119 },
120 {IMPL_PROXY,
121 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100122 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100123 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
124 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
125 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
126 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
127 << "return (remote()->linkToDeath(binder_recipient)"
128 << " == ::android::OK);\n";
129 }
130 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100131 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100132 }, /*cppImpl*/
133 {
134 {IMPL_HEADER,
135 [this](auto &out) {
136 out << "return true;";
137 }
138 },
139 {IMPL_PROXY,
140 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100141 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100142 }
143 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100144 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100145 } /*javaImpl*/
146 );
Yifan Hongffa91392017-01-31 13:41:23 -0800147 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100148}
149
Yifan Hongffa91392017-01-31 13:41:23 -0800150bool Interface::fillUnlinkToDeathMethod(Method *method) const {
151 if (method->name() != "unlinkToDeath") {
152 return false;
153 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100154
Yifan Hongffa91392017-01-31 13:41:23 -0800155 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100156 HIDL_UNLINK_TO_DEATH_TRANSACTION,
157 {
158 {IMPL_HEADER,
159 [](auto &out) {
160 out << "return (recipient != nullptr);\n";
161 }
162 },
163 {IMPL_PROXY,
164 [](auto &out) {
165 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
166 << "for (auto it = _hidl_mDeathRecipients.begin();"
167 << "it != _hidl_mDeathRecipients.end();"
168 << "++it) {\n";
169 out.indent([&] {
170 out.sIf("(*it)->getRecipient() == recipient", [&] {
171 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
172 << "_hidl_mDeathRecipients.erase(it);\n"
173 << "return status == ::android::OK;\n";
174 });
175 });
176 out << "}\n";
177 out << "return false;\n";
178 }
179 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100180 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100181 }, /*cppImpl*/
182 {
183 {IMPL_HEADER,
184 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100185 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100186 }
187 },
188 {IMPL_PROXY,
189 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100190 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100191 }
192 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100193 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100194 } /*javaImpl*/
195 );
Yifan Hongffa91392017-01-31 13:41:23 -0800196 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100197}
Yifan Hongffa91392017-01-31 13:41:23 -0800198bool Interface::fillSyspropsChangedMethod(Method *method) const {
199 if (method->name() != "notifySyspropsChanged") {
200 return false;
201 }
202
203 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100204 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100205 { { IMPL_HEADER, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100206 out << "::android::report_sysprop_change();\n";
207 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100208 } } }, /*cppImpl */
209 { { IMPL_HEADER, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100210 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100211 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100212 );
Yifan Hongffa91392017-01-31 13:41:23 -0800213 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700214}
215
Yifan Hongffa91392017-01-31 13:41:23 -0800216bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
217 if (method->name() != "setHALInstrumentation") {
218 return false;
219 }
220
221 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800222 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
223 {
224 {IMPL_HEADER,
225 [this](auto &out) {
226 // do nothing for base class.
227 out << "return ::android::hardware::Void();\n";
228 }
229 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800230 {IMPL_STUB,
231 [](auto &out) {
232 out << "configureInstrumentation();\n";
233 }
234 },
235 {IMPL_PASSTHROUGH,
236 [](auto &out) {
237 out << "configureInstrumentation();\n";
238 out << "return ::android::hardware::Void();\n";
239 }
240 },
241 }, /*cppImpl */
242 { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */
243 // Not support for Java Impl for now.
244 } } } /*javaImpl */
245 );
Yifan Hongffa91392017-01-31 13:41:23 -0800246 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800247}
248
Yifan Hongffa91392017-01-31 13:41:23 -0800249bool Interface::fillDescriptorChainMethod(Method *method) const {
250 if (method->name() != "interfaceChain") {
251 return false;
252 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700253
Yifan Hongffa91392017-01-31 13:41:23 -0800254 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700255 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Martijn Coenen115d4282016-12-19 05:14:04 +0100256 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700257 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800258 out << "_hidl_cb(";
259 out.block([&] {
260 for (const Interface *iface : chain) {
261 out << iface->fullName() << "::descriptor,\n";
262 }
263 });
264 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700265 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100266 } } }, /* cppImpl */
267 { { IMPL_HEADER, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700268 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800269 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700270 out.indent(); out.indent();
271 for (size_t i = 0; i < chain.size(); ++i) {
272 if (i != 0)
273 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800274 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700275 }
276 out << "));";
277 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800278 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100279 );
Yifan Hongffa91392017-01-31 13:41:23 -0800280 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700281}
282
Yifan Hongffa91392017-01-31 13:41:23 -0800283bool Interface::fillGetDescriptorMethod(Method *method) const {
284 if (method->name() != "interfaceDescriptor") {
285 return false;
286 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800287
Yifan Hongffa91392017-01-31 13:41:23 -0800288 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800289 HIDL_GET_DESCRIPTOR_TRANSACTION,
290 { { IMPL_HEADER, [this](auto &out) {
291 out << "_hidl_cb("
292 << fullName()
293 << "::descriptor);\n"
294 << "return ::android::hardware::Void();";
295 } } }, /* cppImpl */
296 { { IMPL_HEADER, [this](auto &out) {
297 out << "return "
298 << fullJavaName()
299 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800300 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800301 );
Yifan Hongffa91392017-01-31 13:41:23 -0800302 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800303}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700304
Yifan Hongbcffce22017-02-01 15:52:06 -0800305bool Interface::fillGetDebugInfoMethod(Method *method) const {
306 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800307 return false;
308 }
309
310 method->fillImplementation(
311 HIDL_GET_REF_INFO_TRANSACTION,
312 {
313 {IMPL_HEADER,
314 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800315 // getDebugInfo returns N/A for local objects.
316 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */ });\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800317 << "return ::android::hardware::Void();";
318 }
319 },
320 {IMPL_STUB_IMPL,
321 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800322 out << "_hidl_cb({ getpid(), reinterpret_cast<uint64_t>(this) });\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800323 << "return ::android::hardware::Void();";
324 }
325 }
326 }, /* cppImpl */
327 { { IMPL_HEADER, [this, method](auto &out) {
328 const Type &refInfo = method->results().front()->type();
329 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
330 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800331 // TODO(b/34777099): PID for java.
332 << "info.pid = -1;\n"
333 << "info.ptr = 0;\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800334 << "return info;";
335 } } } /* javaImpl */
336 );
337
338 return true;
339}
340
Andreas Huber37065d62017-02-07 14:36:54 -0800341bool Interface::fillDebugMethod(Method *method) const {
342 if (method->name() != "debug") {
343 return false;
344 }
345
346 method->fillImplementation(
347 HIDL_DEBUG_TRANSACTION,
348 {
349 {IMPL_HEADER,
350 [this](auto &out) {
351 out << "(void)fd;\n"
352 << "(void)options;\n"
353 << "return ::android::hardware::Void();";
354 }
355 },
356 }, /* cppImpl */
357 {
358 /* unused, as the debug method is hidden from Java */
359 } /* javaImpl */
360 );
361
362 return true;
363}
364
Yifan Hongffa91392017-01-31 13:41:23 -0800365static std::map<std::string, Method *> gAllReservedMethods;
366
Steven Moreland14ee6742016-10-18 12:58:28 -0700367bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800368 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800369 if (!gAllReservedMethods.emplace(method->name(), method).second) {
370 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
371 << method->name();
372 return false;
373 }
374 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800375 return true;
376 }
377
Yifan Hong10fe0b52016-10-19 14:20:17 -0700378 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700379 if (lookupMethod(method->name()) != nullptr) {
380 LOG(ERROR) << "Redefinition of method " << method->name();
381 return false;
382 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700383 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700384
Yifan Hong10fe0b52016-10-19 14:20:17 -0700385 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700386
Yifan Hong10fe0b52016-10-19 14:20:17 -0700387 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700388 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700389 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700390 ancestor = ancestor->superType();
391 }
392
Yifan Hong10fe0b52016-10-19 14:20:17 -0700393 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
394 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700395 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700396 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700397
398 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700399}
400
Yifan Hongffa91392017-01-31 13:41:23 -0800401bool Interface::addAllReservedMethods() {
402 // use a sorted map to insert them in serial ID order.
403 std::map<int32_t, Method *> reservedMethodsById;
404 for (const auto &pair : gAllReservedMethods) {
405 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800406 bool fillSuccess = fillPingMethod(method)
407 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800408 || fillGetDescriptorMethod(method)
409 || fillSyspropsChangedMethod(method)
410 || fillLinkToDeathMethod(method)
411 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800412 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800413 || fillGetDebugInfoMethod(method)
414 || fillDebugMethod(method);
415
Yifan Hongffa91392017-01-31 13:41:23 -0800416 if (!fillSuccess) {
417 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
418 << method->name();
419 return false;
420 }
421 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
422 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
423 << method->name() << " and "
424 << reservedMethodsById[method->getSerialId()]->name()
425 << ", serialId = " << method->getSerialId();
426 return false;
427 }
428 }
429 for (const auto &pair : reservedMethodsById) {
430 this->mReservedMethods.push_back(pair.second);
431 }
432 return true;
433}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700434
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700435const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700436 return mSuperType;
437}
438
Yifan Hong10fe0b52016-10-19 14:20:17 -0700439std::vector<const Interface *> Interface::typeChain() const {
440 std::vector<const Interface *> v;
441 const Interface *iface = this;
442 while (iface != nullptr) {
443 v.push_back(iface);
444 iface = iface->mSuperType;
445 }
446 return v;
447}
448
Yifan Hongfe95aa22016-10-19 17:26:45 -0700449std::vector<const Interface *> Interface::superTypeChain() const {
450 return superType()->typeChain(); // should work even if superType is nullptr
451}
452
Martijn Coenenb40ef022017-01-02 15:21:46 +0100453bool Interface::isElidableType() const {
454 return true;
455}
456
Andreas Hubera2723d22016-07-29 15:36:07 -0700457bool Interface::isInterface() const {
458 return true;
459}
460
Andreas Huber295ad302016-08-16 11:35:00 -0700461bool Interface::isBinder() const {
462 return true;
463}
464
Yifan Hong10fe0b52016-10-19 14:20:17 -0700465const std::vector<Method *> &Interface::userDefinedMethods() const {
466 return mUserMethods;
467}
468
469const std::vector<Method *> &Interface::hidlReservedMethods() const {
470 return mReservedMethods;
471}
472
473std::vector<Method *> Interface::methods() const {
474 std::vector<Method *> v(mUserMethods);
475 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
476 return v;
477}
478
479std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
480 std::vector<InterfaceAndMethod> v;
481 std::vector<const Interface *> chain = typeChain();
482 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
483 const Interface *iface = *it;
484 for (Method *userMethod : iface->userDefinedMethods()) {
485 v.push_back(InterfaceAndMethod(iface, userMethod));
486 }
487 }
488 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800489 v.push_back(InterfaceAndMethod(
490 *chain.rbegin(), // IBase
491 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700492 }
493 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700494}
495
Steven Moreland14ee6742016-10-18 12:58:28 -0700496Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700497 for (const auto &tuple : allMethodsFromRoot()) {
498 Method *method = tuple.method();
499 if (method->name() == name) {
500 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700501 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700502 }
503
504 return nullptr;
505}
506
Steven Moreland40786312016-08-16 10:29:40 -0700507std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700508 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700509}
510
Yifan Hongeefe4f22017-01-04 15:32:42 -0800511std::string Interface::getProxyName() const {
512 return fqName().getInterfaceProxyName();
513}
514
515std::string Interface::getStubName() const {
516 return fqName().getInterfaceStubName();
517}
518
519std::string Interface::getHwName() const {
520 return fqName().getInterfaceHwName();
521}
522
523std::string Interface::getPassthroughName() const {
524 return fqName().getInterfacePassthroughName();
525}
526
Yifan Hong51a65092017-01-04 15:41:44 -0800527FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800528 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800529}
530
Yifan Hong51a65092017-01-04 15:41:44 -0800531FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800532 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800533}
534
Yifan Hong51a65092017-01-04 15:41:44 -0800535FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800536 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800537}
538
Steven Moreland979e0992016-09-07 09:18:08 -0700539std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700540 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700541 const std::string base =
542 std::string(specifyNamespaces ? "::android::" : "")
543 + "sp<"
544 + (specifyNamespaces ? fullName() : partialCppName())
545 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700546
547 switch (mode) {
548 case StorageMode_Stack:
549 case StorageMode_Result:
550 return base;
551
552 case StorageMode_Argument:
553 return "const " + base + "&";
554 }
555}
556
Yifan Hong4ed13472016-11-02 10:44:11 -0700557std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700558 return fullJavaName();
559}
560
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800561std::string Interface::getVtsType() const {
562 if (StringHelper::EndsWith(localName(), "Callback")) {
563 return "TYPE_HIDL_CALLBACK";
564 } else {
565 return "TYPE_HIDL_INTERFACE";
566 }
567}
568
Andreas Huber881227d2016-08-02 14:20:21 -0700569void Interface::emitReaderWriter(
570 Formatter &out,
571 const std::string &name,
572 const std::string &parcelObj,
573 bool parcelObjIsPointer,
574 bool isReader,
575 ErrorMode mode) const {
576 const std::string parcelObjDeref =
577 parcelObj + (parcelObjIsPointer ? "->" : ".");
578
579 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700580 out << "{\n";
581 out.indent();
582
Iliyan Malchev549e2592016-08-10 08:59:12 -0700583 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700584
Andreas Huber8a82ff72016-08-04 10:29:39 -0700585 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700586 << binderName << ";\n";
587
Iliyan Malchev549e2592016-08-10 08:59:12 -0700588 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700589 out << parcelObjDeref
590 << "readNullableStrongBinder(&"
591 << binderName
592 << ");\n";
593
594 handleError(out, mode);
595
596 out << name
597 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100598 << "::android::hardware::fromBinder<"
599 << fqName().cppName()
600 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800601 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100602 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800603 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100604 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700605 << binderName
606 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700607
608 out.unindent();
609 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700610 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200611 out << "if (" << name << " == nullptr) {\n";
612 out.indent();
613 out << "_hidl_err = ";
614 out << parcelObjDeref
615 << "writeStrongBinder(nullptr);\n";
616 out.unindent();
617 out << "} else {\n";
618 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800619 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
620 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800621 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100622 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800623 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800624 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800625 << ">("
626 << name
627 << ");\n";
628 });
629 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800630 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800631 out << "_hidl_err = "
632 << parcelObjDeref
633 << "writeStrongBinder(_hidl_binder);\n";
634 });
635 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800636 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800637 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
638 });
639 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200640 out.unindent();
641 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700642
Andreas Huber881227d2016-08-02 14:20:21 -0700643 handleError(out, mode);
644 }
645}
646
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800647status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
648 status_t status = Scope::emitGlobalTypeDeclarations(out);
649 if (status != OK) {
650 return status;
651 }
652 out << "std::string toString("
653 << getCppArgumentType()
654 << ");\n";
655 return OK;
656}
657
658
659status_t Interface::emitTypeDefinitions(
660 Formatter &out, const std::string prefix) const {
661 std::string space = prefix.empty() ? "" : (prefix + "::");
662 status_t err = Scope::emitTypeDefinitions(out, space + localName());
663 if (err != OK) {
664 return err;
665 }
666
667 out << "std::string toString("
668 << getCppArgumentType()
669 << " o) ";
670
671 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800672 out << "std::string os;\n";
673 out << "auto ret = o->interfaceDescriptor([&os] (const auto &name) ";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800674 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800675 out << "os += name.c_str();\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800676 });
677 out << ");\n";
Yifan Hongfbcdc802017-02-22 18:12:48 -0800678 out.sIf("!ret.isOk()", [&] {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800679 out << "os += \"[class or subclass of \";\n"
680 << "os += " << fullName() << "::descriptor;\n"
681 << "os += \"]\";\n";
682 }).endl();
683 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
684 << "return os;\n";
685 }).endl().endl();
686
687 return OK;
688}
689
Andreas Huber2831d512016-08-15 09:33:47 -0700690void Interface::emitJavaReaderWriter(
691 Formatter &out,
692 const std::string &parcelObj,
693 const std::string &argName,
694 bool isReader) const {
695 if (isReader) {
696 out << fullJavaName()
697 << ".asInterface("
698 << parcelObj
699 << ".readStrongBinder());\n";
700 } else {
701 out << parcelObj
702 << ".writeStrongBinder("
703 << argName
704 << " == null ? null : "
705 << argName
706 << ".asBinder());\n";
707 }
708}
709
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700710status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
711 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700712 // Skip for TypeDef as it is just an alias of a defined type.
713 if (type->isTypeDef()) {
714 continue;
715 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700716 out << "attribute: {\n";
717 out.indent();
718 status_t status = type->emitVtsTypeDeclarations(out);
719 if (status != OK) {
720 return status;
721 }
722 out.unindent();
723 out << "}\n\n";
724 }
725 return OK;
726}
727
728status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700729 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800730 if (method->isHidlReserved()) {
731 continue;
732 }
733
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700734 out << "api: {\n";
735 out.indent();
736 out << "name: \"" << method->name() << "\"\n";
737 // Generate declaration for each return value.
738 for (const auto &result : method->results()) {
739 out << "return_type_hidl: {\n";
740 out.indent();
741 status_t status = result->type().emitVtsAttributeType(out);
742 if (status != OK) {
743 return status;
744 }
745 out.unindent();
746 out << "}\n";
747 }
748 // Generate declaration for each input argument
749 for (const auto &arg : method->args()) {
750 out << "arg: {\n";
751 out.indent();
752 status_t status = arg->type().emitVtsAttributeType(out);
753 if (status != OK) {
754 return status;
755 }
756 out.unindent();
757 out << "}\n";
758 }
759 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700760 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700761 out << "callflow: {\n";
762 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700763 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700764 if (name == "entry") {
765 out << "entry: true\n";
766 } else if (name == "exit") {
767 out << "exit: true\n";
768 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700769 const AnnotationParam *param =
770 annotation->getParam("next");
771 if (param != nullptr) {
772 for (auto value : *param->getValues()) {
773 out << "next: " << value << "\n";
774 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700775 }
776 } else {
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800777 std::cerr << "Unrecognized annotation '"
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700778 << name << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800779 << ". A VTS annotation should be one of: "
780 << "entry, exit, callflow. \n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700781 }
782 out.unindent();
783 out << "}\n";
784 }
785 out.unindent();
786 out << "}\n\n";
787 }
788 return OK;
789}
790
791status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800792 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700793 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800794 << fullName()
795 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700796 return OK;
797}
798
Steven Moreland69e7c702016-09-09 11:16:32 -0700799bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700800 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700801 if (method->isOneway()) {
802 return true;
803 }
804 }
805
806 const Interface* superClass = superType();
807
808 if (superClass != nullptr) {
809 return superClass->hasOnewayMethods();
810 }
811
812 return false;
813}
814
Andreas Huber70a59e12016-08-16 12:57:01 -0700815bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700816 if (mIsJavaCompatibleInProgress) {
817 // We're currently trying to determine if this Interface is
818 // java-compatible and something is referencing this interface through
819 // one of its methods. Assume we'll ultimately succeed, if we were wrong
820 // the original invocation of Interface::isJavaCompatible() will then
821 // return the correct "false" result.
822 return true;
823 }
824
Andreas Huber0fa9e392016-08-31 09:05:44 -0700825 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
826 mIsJavaCompatibleInProgress = false;
827 return false;
828 }
829
Andreas Huberea081b32016-08-17 15:57:47 -0700830 mIsJavaCompatibleInProgress = true;
831
Andreas Huber70a59e12016-08-16 12:57:01 -0700832 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700833 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700834 return false;
835 }
836
Yifan Hong10fe0b52016-10-19 14:20:17 -0700837 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700838 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700839 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700840 return false;
841 }
842 }
843
Andreas Huberea081b32016-08-17 15:57:47 -0700844 mIsJavaCompatibleInProgress = false;
845
Andreas Huber70a59e12016-08-16 12:57:01 -0700846 return true;
847}
848
Andreas Huberc9410c72016-07-28 12:18:40 -0700849} // namespace android
850