blob: 154f91e46065bc8f72c8f168d622ce562bea998b [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
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800310 static const std::string sArch =
311 "#if defined(__LP64__)\n"
312 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
313 "#else\n"
314 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
315 "#endif\n";
316
Yifan Hongcd2ae452017-01-31 14:33:40 -0800317 method->fillImplementation(
318 HIDL_GET_REF_INFO_TRANSACTION,
319 {
320 {IMPL_HEADER,
321 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800322 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800323 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
324 << sArch
325 << "});\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800326 << "return ::android::hardware::Void();";
327 }
328 },
329 {IMPL_STUB_IMPL,
330 [this](auto &out) {
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800331 out << "_hidl_cb({ getpid(), reinterpret_cast<uint64_t>(this), \n"
332 << sArch
333 << "});\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800334 << "return ::android::hardware::Void();";
335 }
336 }
337 }, /* cppImpl */
338 { { IMPL_HEADER, [this, method](auto &out) {
339 const Type &refInfo = method->results().front()->type();
340 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
341 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800342 // TODO(b/34777099): PID for java.
343 << "info.pid = -1;\n"
344 << "info.ptr = 0;\n"
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800345 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800346 << "return info;";
347 } } } /* javaImpl */
348 );
349
350 return true;
351}
352
Andreas Huber37065d62017-02-07 14:36:54 -0800353bool Interface::fillDebugMethod(Method *method) const {
354 if (method->name() != "debug") {
355 return false;
356 }
357
358 method->fillImplementation(
359 HIDL_DEBUG_TRANSACTION,
360 {
361 {IMPL_HEADER,
362 [this](auto &out) {
363 out << "(void)fd;\n"
364 << "(void)options;\n"
365 << "return ::android::hardware::Void();";
366 }
367 },
368 }, /* cppImpl */
369 {
370 /* unused, as the debug method is hidden from Java */
371 } /* javaImpl */
372 );
373
374 return true;
375}
376
Yifan Hongffa91392017-01-31 13:41:23 -0800377static std::map<std::string, Method *> gAllReservedMethods;
378
Steven Moreland14ee6742016-10-18 12:58:28 -0700379bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800380 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800381 if (!gAllReservedMethods.emplace(method->name(), method).second) {
382 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
383 << method->name();
384 return false;
385 }
386 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800387 return true;
388 }
389
Yifan Hong10fe0b52016-10-19 14:20:17 -0700390 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700391 if (lookupMethod(method->name()) != nullptr) {
392 LOG(ERROR) << "Redefinition of method " << method->name();
393 return false;
394 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700395 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700396
Yifan Hong10fe0b52016-10-19 14:20:17 -0700397 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700398
Yifan Hong10fe0b52016-10-19 14:20:17 -0700399 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700400 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700401 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700402 ancestor = ancestor->superType();
403 }
404
Yifan Hong10fe0b52016-10-19 14:20:17 -0700405 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
406 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700407 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700408 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700409
410 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700411}
412
Yifan Hongffa91392017-01-31 13:41:23 -0800413bool Interface::addAllReservedMethods() {
414 // use a sorted map to insert them in serial ID order.
415 std::map<int32_t, Method *> reservedMethodsById;
416 for (const auto &pair : gAllReservedMethods) {
417 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800418 bool fillSuccess = fillPingMethod(method)
419 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800420 || fillGetDescriptorMethod(method)
421 || fillSyspropsChangedMethod(method)
422 || fillLinkToDeathMethod(method)
423 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800424 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800425 || fillGetDebugInfoMethod(method)
426 || fillDebugMethod(method);
427
Yifan Hongffa91392017-01-31 13:41:23 -0800428 if (!fillSuccess) {
429 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
430 << method->name();
431 return false;
432 }
433 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
434 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
435 << method->name() << " and "
436 << reservedMethodsById[method->getSerialId()]->name()
437 << ", serialId = " << method->getSerialId();
438 return false;
439 }
440 }
441 for (const auto &pair : reservedMethodsById) {
442 this->mReservedMethods.push_back(pair.second);
443 }
444 return true;
445}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700446
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700447const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700448 return mSuperType;
449}
450
Yifan Hong10fe0b52016-10-19 14:20:17 -0700451std::vector<const Interface *> Interface::typeChain() const {
452 std::vector<const Interface *> v;
453 const Interface *iface = this;
454 while (iface != nullptr) {
455 v.push_back(iface);
456 iface = iface->mSuperType;
457 }
458 return v;
459}
460
Yifan Hongfe95aa22016-10-19 17:26:45 -0700461std::vector<const Interface *> Interface::superTypeChain() const {
462 return superType()->typeChain(); // should work even if superType is nullptr
463}
464
Martijn Coenenb40ef022017-01-02 15:21:46 +0100465bool Interface::isElidableType() const {
466 return true;
467}
468
Andreas Hubera2723d22016-07-29 15:36:07 -0700469bool Interface::isInterface() const {
470 return true;
471}
472
Andreas Huber295ad302016-08-16 11:35:00 -0700473bool Interface::isBinder() const {
474 return true;
475}
476
Yifan Hong10fe0b52016-10-19 14:20:17 -0700477const std::vector<Method *> &Interface::userDefinedMethods() const {
478 return mUserMethods;
479}
480
481const std::vector<Method *> &Interface::hidlReservedMethods() const {
482 return mReservedMethods;
483}
484
485std::vector<Method *> Interface::methods() const {
486 std::vector<Method *> v(mUserMethods);
487 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
488 return v;
489}
490
491std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
492 std::vector<InterfaceAndMethod> v;
493 std::vector<const Interface *> chain = typeChain();
494 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
495 const Interface *iface = *it;
496 for (Method *userMethod : iface->userDefinedMethods()) {
497 v.push_back(InterfaceAndMethod(iface, userMethod));
498 }
499 }
500 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800501 v.push_back(InterfaceAndMethod(
502 *chain.rbegin(), // IBase
503 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700504 }
505 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700506}
507
Steven Moreland14ee6742016-10-18 12:58:28 -0700508Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700509 for (const auto &tuple : allMethodsFromRoot()) {
510 Method *method = tuple.method();
511 if (method->name() == name) {
512 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700513 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700514 }
515
516 return nullptr;
517}
518
Steven Moreland40786312016-08-16 10:29:40 -0700519std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700520 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700521}
522
Yifan Hongeefe4f22017-01-04 15:32:42 -0800523std::string Interface::getProxyName() const {
524 return fqName().getInterfaceProxyName();
525}
526
527std::string Interface::getStubName() const {
528 return fqName().getInterfaceStubName();
529}
530
531std::string Interface::getHwName() const {
532 return fqName().getInterfaceHwName();
533}
534
535std::string Interface::getPassthroughName() const {
536 return fqName().getInterfacePassthroughName();
537}
538
Yifan Hong51a65092017-01-04 15:41:44 -0800539FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800540 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800541}
542
Yifan Hong51a65092017-01-04 15:41:44 -0800543FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800544 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800545}
546
Yifan Hong51a65092017-01-04 15:41:44 -0800547FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800548 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800549}
550
Steven Moreland979e0992016-09-07 09:18:08 -0700551std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700552 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700553 const std::string base =
554 std::string(specifyNamespaces ? "::android::" : "")
555 + "sp<"
556 + (specifyNamespaces ? fullName() : partialCppName())
557 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700558
559 switch (mode) {
560 case StorageMode_Stack:
561 case StorageMode_Result:
562 return base;
563
564 case StorageMode_Argument:
565 return "const " + base + "&";
566 }
567}
568
Yifan Hong4ed13472016-11-02 10:44:11 -0700569std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700570 return fullJavaName();
571}
572
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800573std::string Interface::getVtsType() const {
574 if (StringHelper::EndsWith(localName(), "Callback")) {
575 return "TYPE_HIDL_CALLBACK";
576 } else {
577 return "TYPE_HIDL_INTERFACE";
578 }
579}
580
Andreas Huber881227d2016-08-02 14:20:21 -0700581void Interface::emitReaderWriter(
582 Formatter &out,
583 const std::string &name,
584 const std::string &parcelObj,
585 bool parcelObjIsPointer,
586 bool isReader,
587 ErrorMode mode) const {
588 const std::string parcelObjDeref =
589 parcelObj + (parcelObjIsPointer ? "->" : ".");
590
591 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700592 out << "{\n";
593 out.indent();
594
Iliyan Malchev549e2592016-08-10 08:59:12 -0700595 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700596
Andreas Huber8a82ff72016-08-04 10:29:39 -0700597 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700598 << binderName << ";\n";
599
Iliyan Malchev549e2592016-08-10 08:59:12 -0700600 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700601 out << parcelObjDeref
602 << "readNullableStrongBinder(&"
603 << binderName
604 << ");\n";
605
606 handleError(out, mode);
607
608 out << name
609 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100610 << "::android::hardware::fromBinder<"
611 << fqName().cppName()
612 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800613 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100614 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800615 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100616 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700617 << binderName
618 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700619
620 out.unindent();
621 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700622 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200623 out << "if (" << name << " == nullptr) {\n";
624 out.indent();
625 out << "_hidl_err = ";
626 out << parcelObjDeref
627 << "writeStrongBinder(nullptr);\n";
628 out.unindent();
629 out << "} else {\n";
630 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800631 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
632 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800633 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100634 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800635 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800636 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800637 << ">("
638 << name
639 << ");\n";
640 });
641 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800642 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800643 out << "_hidl_err = "
644 << parcelObjDeref
645 << "writeStrongBinder(_hidl_binder);\n";
646 });
647 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800648 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800649 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
650 });
651 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200652 out.unindent();
653 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700654
Andreas Huber881227d2016-08-02 14:20:21 -0700655 handleError(out, mode);
656 }
657}
658
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800659status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
660 status_t status = Scope::emitGlobalTypeDeclarations(out);
661 if (status != OK) {
662 return status;
663 }
664 out << "std::string toString("
665 << getCppArgumentType()
666 << ");\n";
667 return OK;
668}
669
670
671status_t Interface::emitTypeDefinitions(
672 Formatter &out, const std::string prefix) const {
673 std::string space = prefix.empty() ? "" : (prefix + "::");
674 status_t err = Scope::emitTypeDefinitions(out, space + localName());
675 if (err != OK) {
676 return err;
677 }
678
679 out << "std::string toString("
680 << getCppArgumentType()
681 << " o) ";
682
683 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800684 out << "std::string os;\n";
685 out << "auto ret = o->interfaceDescriptor([&os] (const auto &name) ";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800686 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800687 out << "os += name.c_str();\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800688 });
689 out << ");\n";
Yifan Hongfbcdc802017-02-22 18:12:48 -0800690 out.sIf("!ret.isOk()", [&] {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800691 out << "os += \"[class or subclass of \";\n"
692 << "os += " << fullName() << "::descriptor;\n"
693 << "os += \"]\";\n";
694 }).endl();
695 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
696 << "return os;\n";
697 }).endl().endl();
698
699 return OK;
700}
701
Andreas Huber2831d512016-08-15 09:33:47 -0700702void Interface::emitJavaReaderWriter(
703 Formatter &out,
704 const std::string &parcelObj,
705 const std::string &argName,
706 bool isReader) const {
707 if (isReader) {
708 out << fullJavaName()
709 << ".asInterface("
710 << parcelObj
711 << ".readStrongBinder());\n";
712 } else {
713 out << parcelObj
714 << ".writeStrongBinder("
715 << argName
716 << " == null ? null : "
717 << argName
718 << ".asBinder());\n";
719 }
720}
721
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700722status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
723 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700724 // Skip for TypeDef as it is just an alias of a defined type.
725 if (type->isTypeDef()) {
726 continue;
727 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700728 out << "attribute: {\n";
729 out.indent();
730 status_t status = type->emitVtsTypeDeclarations(out);
731 if (status != OK) {
732 return status;
733 }
734 out.unindent();
735 out << "}\n\n";
736 }
737 return OK;
738}
739
740status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700741 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800742 if (method->isHidlReserved()) {
743 continue;
744 }
745
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700746 out << "api: {\n";
747 out.indent();
748 out << "name: \"" << method->name() << "\"\n";
749 // Generate declaration for each return value.
750 for (const auto &result : method->results()) {
751 out << "return_type_hidl: {\n";
752 out.indent();
753 status_t status = result->type().emitVtsAttributeType(out);
754 if (status != OK) {
755 return status;
756 }
757 out.unindent();
758 out << "}\n";
759 }
760 // Generate declaration for each input argument
761 for (const auto &arg : method->args()) {
762 out << "arg: {\n";
763 out.indent();
764 status_t status = arg->type().emitVtsAttributeType(out);
765 if (status != OK) {
766 return status;
767 }
768 out.unindent();
769 out << "}\n";
770 }
771 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700772 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700773 out << "callflow: {\n";
774 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700775 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700776 if (name == "entry") {
777 out << "entry: true\n";
778 } else if (name == "exit") {
779 out << "exit: true\n";
780 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700781 const AnnotationParam *param =
782 annotation->getParam("next");
783 if (param != nullptr) {
784 for (auto value : *param->getValues()) {
785 out << "next: " << value << "\n";
786 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700787 }
788 } else {
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800789 std::cerr << "Unrecognized annotation '"
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700790 << name << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800791 << ". A VTS annotation should be one of: "
792 << "entry, exit, callflow. \n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700793 }
794 out.unindent();
795 out << "}\n";
796 }
797 out.unindent();
798 out << "}\n\n";
799 }
800 return OK;
801}
802
803status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800804 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700805 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800806 << fullName()
807 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700808 return OK;
809}
810
Steven Moreland69e7c702016-09-09 11:16:32 -0700811bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700812 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700813 if (method->isOneway()) {
814 return true;
815 }
816 }
817
818 const Interface* superClass = superType();
819
820 if (superClass != nullptr) {
821 return superClass->hasOnewayMethods();
822 }
823
824 return false;
825}
826
Andreas Huber70a59e12016-08-16 12:57:01 -0700827bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700828 if (mIsJavaCompatibleInProgress) {
829 // We're currently trying to determine if this Interface is
830 // java-compatible and something is referencing this interface through
831 // one of its methods. Assume we'll ultimately succeed, if we were wrong
832 // the original invocation of Interface::isJavaCompatible() will then
833 // return the correct "false" result.
834 return true;
835 }
836
Andreas Huber0fa9e392016-08-31 09:05:44 -0700837 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
838 mIsJavaCompatibleInProgress = false;
839 return false;
840 }
841
Andreas Huberea081b32016-08-17 15:57:47 -0700842 mIsJavaCompatibleInProgress = true;
843
Andreas Huber70a59e12016-08-16 12:57:01 -0700844 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700845 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700846 return false;
847 }
848
Yifan Hong10fe0b52016-10-19 14:20:17 -0700849 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700850 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700851 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700852 return false;
853 }
854 }
855
Andreas Huberea081b32016-08-17 15:57:47 -0700856 mIsJavaCompatibleInProgress = false;
857
Andreas Huber70a59e12016-08-16 12:57:01 -0700858 return true;
859}
860
Andreas Huberc9410c72016-07-28 12:18:40 -0700861} // namespace android
862