blob: abf731755f834ea7b362156666d51af644972084 [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 Hong2e036c92017-03-07 13:18:12 -0800331 out << "_hidl_cb(";
332 out.block([&] {
333 out << "::android::hardware::details::debuggable()"
334 << "? getpid() : -1 /* pid */,\n"
335 << "::android::hardware::details::debuggable()"
336 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
337 << sArch << "\n";
338 });
339 out << ");\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800340 << "return ::android::hardware::Void();";
341 }
342 }
343 }, /* cppImpl */
344 { { IMPL_HEADER, [this, method](auto &out) {
345 const Type &refInfo = method->results().front()->type();
346 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
347 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800348 // TODO(b/34777099): PID for java.
349 << "info.pid = -1;\n"
350 << "info.ptr = 0;\n"
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800351 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800352 << "return info;";
353 } } } /* javaImpl */
354 );
355
356 return true;
357}
358
Andreas Huber37065d62017-02-07 14:36:54 -0800359bool Interface::fillDebugMethod(Method *method) const {
360 if (method->name() != "debug") {
361 return false;
362 }
363
364 method->fillImplementation(
365 HIDL_DEBUG_TRANSACTION,
366 {
367 {IMPL_HEADER,
368 [this](auto &out) {
369 out << "(void)fd;\n"
370 << "(void)options;\n"
371 << "return ::android::hardware::Void();";
372 }
373 },
374 }, /* cppImpl */
375 {
376 /* unused, as the debug method is hidden from Java */
377 } /* javaImpl */
378 );
379
380 return true;
381}
382
Yifan Hongffa91392017-01-31 13:41:23 -0800383static std::map<std::string, Method *> gAllReservedMethods;
384
Steven Moreland14ee6742016-10-18 12:58:28 -0700385bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800386 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800387 if (!gAllReservedMethods.emplace(method->name(), method).second) {
388 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
389 << method->name();
390 return false;
391 }
392 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800393 return true;
394 }
395
Yifan Hong10fe0b52016-10-19 14:20:17 -0700396 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700397 if (lookupMethod(method->name()) != nullptr) {
398 LOG(ERROR) << "Redefinition of method " << method->name();
399 return false;
400 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700401 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700402
Yifan Hong10fe0b52016-10-19 14:20:17 -0700403 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700404
Yifan Hong10fe0b52016-10-19 14:20:17 -0700405 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700406 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700407 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700408 ancestor = ancestor->superType();
409 }
410
Yifan Hong10fe0b52016-10-19 14:20:17 -0700411 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
412 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700413 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700414 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700415
416 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700417}
418
Yifan Hongffa91392017-01-31 13:41:23 -0800419bool Interface::addAllReservedMethods() {
420 // use a sorted map to insert them in serial ID order.
421 std::map<int32_t, Method *> reservedMethodsById;
422 for (const auto &pair : gAllReservedMethods) {
423 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800424 bool fillSuccess = fillPingMethod(method)
425 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800426 || fillGetDescriptorMethod(method)
427 || fillSyspropsChangedMethod(method)
428 || fillLinkToDeathMethod(method)
429 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800430 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800431 || fillGetDebugInfoMethod(method)
432 || fillDebugMethod(method);
433
Yifan Hongffa91392017-01-31 13:41:23 -0800434 if (!fillSuccess) {
435 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
436 << method->name();
437 return false;
438 }
439 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
440 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
441 << method->name() << " and "
442 << reservedMethodsById[method->getSerialId()]->name()
443 << ", serialId = " << method->getSerialId();
444 return false;
445 }
446 }
447 for (const auto &pair : reservedMethodsById) {
448 this->mReservedMethods.push_back(pair.second);
449 }
450 return true;
451}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700452
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700453const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700454 return mSuperType;
455}
456
Yifan Hong10fe0b52016-10-19 14:20:17 -0700457std::vector<const Interface *> Interface::typeChain() const {
458 std::vector<const Interface *> v;
459 const Interface *iface = this;
460 while (iface != nullptr) {
461 v.push_back(iface);
462 iface = iface->mSuperType;
463 }
464 return v;
465}
466
Yifan Hongfe95aa22016-10-19 17:26:45 -0700467std::vector<const Interface *> Interface::superTypeChain() const {
468 return superType()->typeChain(); // should work even if superType is nullptr
469}
470
Martijn Coenenb40ef022017-01-02 15:21:46 +0100471bool Interface::isElidableType() const {
472 return true;
473}
474
Andreas Hubera2723d22016-07-29 15:36:07 -0700475bool Interface::isInterface() const {
476 return true;
477}
478
Andreas Huber295ad302016-08-16 11:35:00 -0700479bool Interface::isBinder() const {
480 return true;
481}
482
Yifan Hong10fe0b52016-10-19 14:20:17 -0700483const std::vector<Method *> &Interface::userDefinedMethods() const {
484 return mUserMethods;
485}
486
487const std::vector<Method *> &Interface::hidlReservedMethods() const {
488 return mReservedMethods;
489}
490
491std::vector<Method *> Interface::methods() const {
492 std::vector<Method *> v(mUserMethods);
493 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
494 return v;
495}
496
497std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
498 std::vector<InterfaceAndMethod> v;
499 std::vector<const Interface *> chain = typeChain();
500 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
501 const Interface *iface = *it;
502 for (Method *userMethod : iface->userDefinedMethods()) {
503 v.push_back(InterfaceAndMethod(iface, userMethod));
504 }
505 }
506 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800507 v.push_back(InterfaceAndMethod(
508 *chain.rbegin(), // IBase
509 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700510 }
511 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700512}
513
Steven Moreland14ee6742016-10-18 12:58:28 -0700514Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700515 for (const auto &tuple : allMethodsFromRoot()) {
516 Method *method = tuple.method();
517 if (method->name() == name) {
518 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700519 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700520 }
521
522 return nullptr;
523}
524
Steven Moreland40786312016-08-16 10:29:40 -0700525std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700526 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700527}
528
Yifan Hongeefe4f22017-01-04 15:32:42 -0800529std::string Interface::getProxyName() const {
530 return fqName().getInterfaceProxyName();
531}
532
533std::string Interface::getStubName() const {
534 return fqName().getInterfaceStubName();
535}
536
537std::string Interface::getHwName() const {
538 return fqName().getInterfaceHwName();
539}
540
541std::string Interface::getPassthroughName() const {
542 return fqName().getInterfacePassthroughName();
543}
544
Yifan Hong51a65092017-01-04 15:41:44 -0800545FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800546 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800547}
548
Yifan Hong51a65092017-01-04 15:41:44 -0800549FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800550 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800551}
552
Yifan Hong51a65092017-01-04 15:41:44 -0800553FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800554 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800555}
556
Steven Moreland979e0992016-09-07 09:18:08 -0700557std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700558 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700559 const std::string base =
560 std::string(specifyNamespaces ? "::android::" : "")
561 + "sp<"
562 + (specifyNamespaces ? fullName() : partialCppName())
563 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700564
565 switch (mode) {
566 case StorageMode_Stack:
567 case StorageMode_Result:
568 return base;
569
570 case StorageMode_Argument:
571 return "const " + base + "&";
572 }
573}
574
Yifan Hong4ed13472016-11-02 10:44:11 -0700575std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700576 return fullJavaName();
577}
578
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800579std::string Interface::getVtsType() const {
580 if (StringHelper::EndsWith(localName(), "Callback")) {
581 return "TYPE_HIDL_CALLBACK";
582 } else {
583 return "TYPE_HIDL_INTERFACE";
584 }
585}
586
Andreas Huber881227d2016-08-02 14:20:21 -0700587void Interface::emitReaderWriter(
588 Formatter &out,
589 const std::string &name,
590 const std::string &parcelObj,
591 bool parcelObjIsPointer,
592 bool isReader,
593 ErrorMode mode) const {
594 const std::string parcelObjDeref =
595 parcelObj + (parcelObjIsPointer ? "->" : ".");
596
597 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700598 out << "{\n";
599 out.indent();
600
Iliyan Malchev549e2592016-08-10 08:59:12 -0700601 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700602
Andreas Huber8a82ff72016-08-04 10:29:39 -0700603 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700604 << binderName << ";\n";
605
Iliyan Malchev549e2592016-08-10 08:59:12 -0700606 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700607 out << parcelObjDeref
608 << "readNullableStrongBinder(&"
609 << binderName
610 << ");\n";
611
612 handleError(out, mode);
613
614 out << name
615 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100616 << "::android::hardware::fromBinder<"
617 << fqName().cppName()
618 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800619 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100620 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800621 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100622 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700623 << binderName
624 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700625
626 out.unindent();
627 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700628 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200629 out << "if (" << name << " == nullptr) {\n";
630 out.indent();
631 out << "_hidl_err = ";
632 out << parcelObjDeref
633 << "writeStrongBinder(nullptr);\n";
634 out.unindent();
635 out << "} else {\n";
636 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800637 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
638 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800639 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100640 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800641 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800642 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800643 << ">("
644 << name
645 << ");\n";
646 });
647 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800648 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800649 out << "_hidl_err = "
650 << parcelObjDeref
651 << "writeStrongBinder(_hidl_binder);\n";
652 });
653 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800654 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800655 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
656 });
657 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200658 out.unindent();
659 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700660
Andreas Huber881227d2016-08-02 14:20:21 -0700661 handleError(out, mode);
662 }
663}
664
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800665status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
666 status_t status = Scope::emitGlobalTypeDeclarations(out);
667 if (status != OK) {
668 return status;
669 }
670 out << "std::string toString("
671 << getCppArgumentType()
672 << ");\n";
673 return OK;
674}
675
676
677status_t Interface::emitTypeDefinitions(
678 Formatter &out, const std::string prefix) const {
679 std::string space = prefix.empty() ? "" : (prefix + "::");
680 status_t err = Scope::emitTypeDefinitions(out, space + localName());
681 if (err != OK) {
682 return err;
683 }
684
685 out << "std::string toString("
686 << getCppArgumentType()
687 << " o) ";
688
689 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800690 out << "std::string os;\n";
691 out << "auto ret = o->interfaceDescriptor([&os] (const auto &name) ";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800692 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800693 out << "os += name.c_str();\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800694 });
695 out << ");\n";
Yifan Hongfbcdc802017-02-22 18:12:48 -0800696 out.sIf("!ret.isOk()", [&] {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800697 out << "os += \"[class or subclass of \";\n"
698 << "os += " << fullName() << "::descriptor;\n"
699 << "os += \"]\";\n";
700 }).endl();
701 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
702 << "return os;\n";
703 }).endl().endl();
704
705 return OK;
706}
707
Andreas Huber2831d512016-08-15 09:33:47 -0700708void Interface::emitJavaReaderWriter(
709 Formatter &out,
710 const std::string &parcelObj,
711 const std::string &argName,
712 bool isReader) const {
713 if (isReader) {
714 out << fullJavaName()
715 << ".asInterface("
716 << parcelObj
717 << ".readStrongBinder());\n";
718 } else {
719 out << parcelObj
720 << ".writeStrongBinder("
721 << argName
722 << " == null ? null : "
723 << argName
724 << ".asBinder());\n";
725 }
726}
727
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700728status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
729 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700730 // Skip for TypeDef as it is just an alias of a defined type.
731 if (type->isTypeDef()) {
732 continue;
733 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700734 out << "attribute: {\n";
735 out.indent();
736 status_t status = type->emitVtsTypeDeclarations(out);
737 if (status != OK) {
738 return status;
739 }
740 out.unindent();
741 out << "}\n\n";
742 }
743 return OK;
744}
745
746status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700747 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800748 if (method->isHidlReserved()) {
749 continue;
750 }
751
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700752 out << "api: {\n";
753 out.indent();
754 out << "name: \"" << method->name() << "\"\n";
755 // Generate declaration for each return value.
756 for (const auto &result : method->results()) {
757 out << "return_type_hidl: {\n";
758 out.indent();
759 status_t status = result->type().emitVtsAttributeType(out);
760 if (status != OK) {
761 return status;
762 }
763 out.unindent();
764 out << "}\n";
765 }
766 // Generate declaration for each input argument
767 for (const auto &arg : method->args()) {
768 out << "arg: {\n";
769 out.indent();
770 status_t status = arg->type().emitVtsAttributeType(out);
771 if (status != OK) {
772 return status;
773 }
774 out.unindent();
775 out << "}\n";
776 }
777 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700778 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700779 out << "callflow: {\n";
780 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700781 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700782 if (name == "entry") {
783 out << "entry: true\n";
784 } else if (name == "exit") {
785 out << "exit: true\n";
786 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700787 const AnnotationParam *param =
788 annotation->getParam("next");
789 if (param != nullptr) {
790 for (auto value : *param->getValues()) {
791 out << "next: " << value << "\n";
792 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700793 }
794 } else {
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800795 std::cerr << "Unrecognized annotation '"
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700796 << name << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800797 << ". A VTS annotation should be one of: "
798 << "entry, exit, callflow. \n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700799 }
800 out.unindent();
801 out << "}\n";
802 }
803 out.unindent();
804 out << "}\n\n";
805 }
806 return OK;
807}
808
809status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800810 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700811 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800812 << fullName()
813 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700814 return OK;
815}
816
Steven Moreland69e7c702016-09-09 11:16:32 -0700817bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700818 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700819 if (method->isOneway()) {
820 return true;
821 }
822 }
823
824 const Interface* superClass = superType();
825
826 if (superClass != nullptr) {
827 return superClass->hasOnewayMethods();
828 }
829
830 return false;
831}
832
Andreas Huber70a59e12016-08-16 12:57:01 -0700833bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700834 if (mIsJavaCompatibleInProgress) {
835 // We're currently trying to determine if this Interface is
836 // java-compatible and something is referencing this interface through
837 // one of its methods. Assume we'll ultimately succeed, if we were wrong
838 // the original invocation of Interface::isJavaCompatible() will then
839 // return the correct "false" result.
840 return true;
841 }
842
Andreas Huber0fa9e392016-08-31 09:05:44 -0700843 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
844 mIsJavaCompatibleInProgress = false;
845 return false;
846 }
847
Andreas Huberea081b32016-08-17 15:57:47 -0700848 mIsJavaCompatibleInProgress = true;
849
Andreas Huber70a59e12016-08-16 12:57:01 -0700850 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700851 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700852 return false;
853 }
854
Yifan Hong10fe0b52016-10-19 14:20:17 -0700855 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700856 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700857 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700858 return false;
859 }
860 }
861
Andreas Huberea081b32016-08-17 15:57:47 -0700862 mIsJavaCompatibleInProgress = false;
863
Andreas Huber70a59e12016-08-16 12:57:01 -0700864 return true;
865}
866
Andreas Huberc9410c72016-07-28 12:18:40 -0700867} // namespace android
868