blob: 5974288ca7310b4cf7e3ad0e4d135b1e8efc9d5a [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"
Yifan Hong30b5d1f2017-04-03 12:19:25 -070020#include "ArrayType.h"
21#include "ConstantExpression.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010022#include "DeathRecipientType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023#include "Method.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010024#include "ScalarType.h"
Yifan Hong10fe0b52016-10-19 14:20:17 -070025#include "StringType.h"
26#include "VectorType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Yifan Hong30b5d1f2017-04-03 12:19:25 -070028#include <unistd.h>
29
Yifan Hong30b5d1f2017-04-03 12:19:25 -070030#include <iostream>
31#include <sstream>
32
Steven Moreland14ee6742016-10-18 12:58:28 -070033#include <android-base/logging.h>
Steven Moreland5bdfa702017-04-18 23:20:39 -070034#include <hidl-hash/Hash.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070035#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070036#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070037
Andreas Huberc9410c72016-07-28 12:18:40 -070038namespace android {
39
Steven Morelandb8e15a52017-02-13 19:35:40 -080040#define B_PACK_CHARS(c1, c2, c3, c4) \
41 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
42
Yifan Hong10fe0b52016-10-19 14:20:17 -070043/* It is very important that these values NEVER change. These values
44 * must remain unchanged over the lifetime of android. This is
45 * because the framework on a device will be updated independently of
46 * the hals on a device. If the hals are compiled with one set of
47 * transaction values, and the framework with another, then the
48 * interface between them will be destroyed, and the device will not
49 * work.
50 */
51enum {
Yifan Hong10fe0b52016-10-19 14:20:17 -070052 /////////////////// User defined transactions
53 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080054 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070055 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080056 FIRST_HIDL_TRANSACTION = 0x0f000000,
57 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
58 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
59 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
60 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
61 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
62 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
63 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
64 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
65 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
Yifan Hong30b5d1f2017-04-03 12:19:25 -070066 HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'),
Steven Morelandb8e15a52017-02-13 19:35:40 -080067 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070068};
69
Yifan Honga4b53d02016-10-31 17:29:10 -070070Interface::Interface(const char *localName, const Location &location, Interface *super)
71 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070072 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070073 mIsJavaCompatibleInProgress(false) {
Martijn Coenenaf712c02016-11-16 15:26:27 +010074}
75
Steven Moreland30bb6a82016-11-30 09:18:34 -080076std::string Interface::typeName() const {
77 return "interface " + localName();
78}
79
Steven Moreland424a9482017-02-13 19:20:40 -080080bool Interface::fillPingMethod(Method *method) const {
81 if (method->name() != "ping") {
82 return false;
83 }
84
85 method->fillImplementation(
86 HIDL_PING_TRANSACTION,
87 {
Steven Moreland937408a2017-03-20 09:54:18 -070088 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080089 [](auto &out) {
90 out << "return ::android::hardware::Void();\n";
91 }
92 },
93 {IMPL_STUB_IMPL,
94 [](auto &out) {
95 out << "return ::android::hardware::Void();\n";
96 }
97 }
98 }, /*cppImpl*/
99 {
Steven Moreland937408a2017-03-20 09:54:18 -0700100 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700101 [](auto &out) {
Steven Moreland424a9482017-02-13 19:20:40 -0800102 out << "return;\n";
103 }
104 },
105 {IMPL_STUB, nullptr /* don't generate code */}
106 } /*javaImpl*/
107 );
108
109 return true;
110}
111
Yifan Hongffa91392017-01-31 13:41:23 -0800112bool Interface::fillLinkToDeathMethod(Method *method) const {
113 if (method->name() != "linkToDeath") {
114 return false;
115 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100116
Yifan Hongffa91392017-01-31 13:41:23 -0800117 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100118 HIDL_LINK_TO_DEATH_TRANSACTION,
119 {
Steven Moreland937408a2017-03-20 09:54:18 -0700120 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100121 [](auto &out) {
122 out << "(void)cookie;\n"
123 << "return (recipient != nullptr);\n";
124 }
125 },
126 {IMPL_PROXY,
127 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100128 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100129 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
130 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
131 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
132 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
133 << "return (remote()->linkToDeath(binder_recipient)"
134 << " == ::android::OK);\n";
135 }
136 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100137 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100138 }, /*cppImpl*/
139 {
Steven Moreland937408a2017-03-20 09:54:18 -0700140 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700141 [](auto &out) {
Martijn Coenen115d4282016-12-19 05:14:04 +0100142 out << "return true;";
143 }
144 },
145 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700146 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100147 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100148 }
149 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100150 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100151 } /*javaImpl*/
152 );
Yifan Hongffa91392017-01-31 13:41:23 -0800153 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100154}
155
Yifan Hongffa91392017-01-31 13:41:23 -0800156bool Interface::fillUnlinkToDeathMethod(Method *method) const {
157 if (method->name() != "unlinkToDeath") {
158 return false;
159 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100160
Yifan Hongffa91392017-01-31 13:41:23 -0800161 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100162 HIDL_UNLINK_TO_DEATH_TRANSACTION,
163 {
Steven Moreland937408a2017-03-20 09:54:18 -0700164 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100165 [](auto &out) {
166 out << "return (recipient != nullptr);\n";
167 }
168 },
169 {IMPL_PROXY,
170 [](auto &out) {
171 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
172 << "for (auto it = _hidl_mDeathRecipients.begin();"
173 << "it != _hidl_mDeathRecipients.end();"
174 << "++it) {\n";
175 out.indent([&] {
176 out.sIf("(*it)->getRecipient() == recipient", [&] {
177 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
178 << "_hidl_mDeathRecipients.erase(it);\n"
179 << "return status == ::android::OK;\n";
180 });
181 });
182 out << "}\n";
183 out << "return false;\n";
184 }
185 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100186 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100187 }, /*cppImpl*/
188 {
Steven Moreland937408a2017-03-20 09:54:18 -0700189 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700190 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100191 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100192 }
193 },
194 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700195 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100196 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100197 }
198 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100199 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100200 } /*javaImpl*/
201 );
Yifan Hongffa91392017-01-31 13:41:23 -0800202 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100203}
Yifan Hongffa91392017-01-31 13:41:23 -0800204bool Interface::fillSyspropsChangedMethod(Method *method) const {
205 if (method->name() != "notifySyspropsChanged") {
206 return false;
207 }
208
209 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100210 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yi Kongc8ff4672017-04-30 23:46:56 -0700211 { { IMPL_INTERFACE, [](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100212 out << "::android::report_sysprop_change();\n";
213 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100214 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700215 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100216 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100217 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100218 );
Yifan Hongffa91392017-01-31 13:41:23 -0800219 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700220}
221
Yifan Hongffa91392017-01-31 13:41:23 -0800222bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
223 if (method->name() != "setHALInstrumentation") {
224 return false;
225 }
226
227 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800228 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
229 {
Steven Moreland937408a2017-03-20 09:54:18 -0700230 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700231 [](auto &out) {
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800232 // do nothing for base class.
233 out << "return ::android::hardware::Void();\n";
234 }
235 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800236 {IMPL_STUB,
237 [](auto &out) {
238 out << "configureInstrumentation();\n";
239 }
240 },
241 {IMPL_PASSTHROUGH,
242 [](auto &out) {
243 out << "configureInstrumentation();\n";
244 out << "return ::android::hardware::Void();\n";
245 }
246 },
247 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700248 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800249 // Not support for Java Impl for now.
250 } } } /*javaImpl */
251 );
Yifan Hongffa91392017-01-31 13:41:23 -0800252 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800253}
254
Yifan Hongffa91392017-01-31 13:41:23 -0800255bool Interface::fillDescriptorChainMethod(Method *method) const {
256 if (method->name() != "interfaceChain") {
257 return false;
258 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700259
Yifan Hongffa91392017-01-31 13:41:23 -0800260 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700261 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700262 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700263 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800264 out << "_hidl_cb(";
265 out.block([&] {
266 for (const Interface *iface : chain) {
267 out << iface->fullName() << "::descriptor,\n";
268 }
269 });
270 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700271 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100272 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700273 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700274 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800275 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700276 out.indent(); out.indent();
277 for (size_t i = 0; i < chain.size(); ++i) {
278 if (i != 0)
279 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800280 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700281 }
282 out << "));";
283 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800284 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100285 );
Yifan Hongffa91392017-01-31 13:41:23 -0800286 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700287}
288
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700289static void emitDigestChain(
290 Formatter &out,
291 const std::string &prefix,
292 const std::vector<const Interface *> &chain,
293 std::function<std::string(const ConstantExpression &)> byteToString) {
294 out.join(chain.begin(), chain.end(), ",\n", [&] (const auto &iface) {
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700295 const Hash &hash = Hash::getHash(iface->location().begin().filename());
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700296 out << prefix;
297 out << "{";
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700298 out.join(hash.raw().begin(), hash.raw().end(), ",", [&](const auto &e) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700299 // Use ConstantExpression::cppValue / javaValue
300 // because Java used signed byte for uint8_t.
301 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
302 });
303 out << "} /* ";
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700304 out << hash.hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700305 out << " */";
306 });
307}
308
309bool Interface::fillHashChainMethod(Method *method) const {
310 if (method->name() != "getHashChain") {
311 return false;
312 }
313 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
314 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
315
316 method->fillImplementation(
317 HIDL_HASH_CHAIN_TRANSACTION,
318 { { IMPL_INTERFACE, [this, digestType](auto &out) {
319 std::vector<const Interface *> chain = typeChain();
320 out << "_hidl_cb(";
321 out.block([&] {
322 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")",
323 chain, [](const auto &e){return e.cppValue();});
324 });
325 out << ");\n";
326 out << "return ::android::hardware::Void();\n";
327 } } }, /* cppImpl */
328 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
329 std::vector<const Interface *> chain = typeChain();
330 out << "return new "
331 << chainType->getJavaType(false /* forInitializer */)
332 << "(java.util.Arrays.asList(\n";
333 out.indent(2, [&] {
334 // No need for dimensions when elements are explicitly provided.
335 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
336 chain, [](const auto &e){return e.javaValue();});
337 });
338 out << "));\n";
339 } } } /* javaImpl */
340 );
341 return true;
342}
343
Yifan Hongffa91392017-01-31 13:41:23 -0800344bool Interface::fillGetDescriptorMethod(Method *method) const {
345 if (method->name() != "interfaceDescriptor") {
346 return false;
347 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800348
Yifan Hongffa91392017-01-31 13:41:23 -0800349 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800350 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700351 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800352 out << "_hidl_cb("
353 << fullName()
354 << "::descriptor);\n"
355 << "return ::android::hardware::Void();";
356 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700357 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800358 out << "return "
359 << fullJavaName()
360 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800361 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800362 );
Yifan Hongffa91392017-01-31 13:41:23 -0800363 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800364}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700365
Yifan Hongbcffce22017-02-01 15:52:06 -0800366bool Interface::fillGetDebugInfoMethod(Method *method) const {
367 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800368 return false;
369 }
370
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800371 static const std::string sArch =
372 "#if defined(__LP64__)\n"
373 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
374 "#else\n"
375 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
376 "#endif\n";
377
Yifan Hongcd2ae452017-01-31 14:33:40 -0800378 method->fillImplementation(
379 HIDL_GET_REF_INFO_TRANSACTION,
380 {
Steven Moreland937408a2017-03-20 09:54:18 -0700381 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700382 [](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800383 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800384 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
385 << sArch
386 << "});\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800387 << "return ::android::hardware::Void();";
388 }
389 },
390 {IMPL_STUB_IMPL,
Yi Kongc8ff4672017-04-30 23:46:56 -0700391 [](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800392 out << "_hidl_cb(";
393 out.block([&] {
394 out << "::android::hardware::details::debuggable()"
395 << "? getpid() : -1 /* pid */,\n"
396 << "::android::hardware::details::debuggable()"
397 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
398 << sArch << "\n";
399 });
400 out << ");\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800401 << "return ::android::hardware::Void();";
402 }
403 }
404 }, /* cppImpl */
Yi Kongc8ff4672017-04-30 23:46:56 -0700405 { { IMPL_INTERFACE, [method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800406 const Type &refInfo = method->results().front()->type();
407 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
408 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800409 // TODO(b/34777099): PID for java.
410 << "info.pid = -1;\n"
411 << "info.ptr = 0;\n"
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800412 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800413 << "return info;";
414 } } } /* javaImpl */
415 );
416
417 return true;
418}
419
Andreas Huber37065d62017-02-07 14:36:54 -0800420bool Interface::fillDebugMethod(Method *method) const {
421 if (method->name() != "debug") {
422 return false;
423 }
424
425 method->fillImplementation(
426 HIDL_DEBUG_TRANSACTION,
427 {
Steven Moreland937408a2017-03-20 09:54:18 -0700428 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700429 [](auto &out) {
Andreas Huber37065d62017-02-07 14:36:54 -0800430 out << "(void)fd;\n"
431 << "(void)options;\n"
432 << "return ::android::hardware::Void();";
433 }
434 },
435 }, /* cppImpl */
436 {
437 /* unused, as the debug method is hidden from Java */
438 } /* javaImpl */
439 );
440
441 return true;
442}
443
Yifan Hongffa91392017-01-31 13:41:23 -0800444static std::map<std::string, Method *> gAllReservedMethods;
445
Steven Moreland14ee6742016-10-18 12:58:28 -0700446bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800447 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800448 if (!gAllReservedMethods.emplace(method->name(), method).second) {
449 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
450 << method->name();
451 return false;
452 }
453 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800454 return true;
455 }
456
Yifan Hong10fe0b52016-10-19 14:20:17 -0700457 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700458 if (lookupMethod(method->name()) != nullptr) {
459 LOG(ERROR) << "Redefinition of method " << method->name();
460 return false;
461 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700462 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700463
Yifan Hong10fe0b52016-10-19 14:20:17 -0700464 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700465
Yifan Hong10fe0b52016-10-19 14:20:17 -0700466 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700467 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700468 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700469 ancestor = ancestor->superType();
470 }
471
Yifan Hong10fe0b52016-10-19 14:20:17 -0700472 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
473 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700474 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700475 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700476
477 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700478}
479
Yifan Hongffa91392017-01-31 13:41:23 -0800480bool Interface::addAllReservedMethods() {
481 // use a sorted map to insert them in serial ID order.
482 std::map<int32_t, Method *> reservedMethodsById;
483 for (const auto &pair : gAllReservedMethods) {
484 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800485 bool fillSuccess = fillPingMethod(method)
486 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800487 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700488 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800489 || fillSyspropsChangedMethod(method)
490 || fillLinkToDeathMethod(method)
491 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800492 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800493 || fillGetDebugInfoMethod(method)
494 || fillDebugMethod(method);
495
Yifan Hongffa91392017-01-31 13:41:23 -0800496 if (!fillSuccess) {
497 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
498 << method->name();
499 return false;
500 }
501 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
502 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
503 << method->name() << " and "
504 << reservedMethodsById[method->getSerialId()]->name()
505 << ", serialId = " << method->getSerialId();
506 return false;
507 }
508 }
509 for (const auto &pair : reservedMethodsById) {
510 this->mReservedMethods.push_back(pair.second);
511 }
512 return true;
513}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700514
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700515const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700516 return mSuperType;
517}
518
Yifan Hong10fe0b52016-10-19 14:20:17 -0700519std::vector<const Interface *> Interface::typeChain() const {
520 std::vector<const Interface *> v;
521 const Interface *iface = this;
522 while (iface != nullptr) {
523 v.push_back(iface);
524 iface = iface->mSuperType;
525 }
526 return v;
527}
528
Yifan Hongfe95aa22016-10-19 17:26:45 -0700529std::vector<const Interface *> Interface::superTypeChain() const {
530 return superType()->typeChain(); // should work even if superType is nullptr
531}
532
Martijn Coenenb40ef022017-01-02 15:21:46 +0100533bool Interface::isElidableType() const {
534 return true;
535}
536
Andreas Hubera2723d22016-07-29 15:36:07 -0700537bool Interface::isInterface() const {
538 return true;
539}
540
Andreas Huber295ad302016-08-16 11:35:00 -0700541bool Interface::isBinder() const {
542 return true;
543}
544
Yifan Hong10fe0b52016-10-19 14:20:17 -0700545const std::vector<Method *> &Interface::userDefinedMethods() const {
546 return mUserMethods;
547}
548
549const std::vector<Method *> &Interface::hidlReservedMethods() const {
550 return mReservedMethods;
551}
552
553std::vector<Method *> Interface::methods() const {
554 std::vector<Method *> v(mUserMethods);
555 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
556 return v;
557}
558
559std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
560 std::vector<InterfaceAndMethod> v;
561 std::vector<const Interface *> chain = typeChain();
562 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
563 const Interface *iface = *it;
564 for (Method *userMethod : iface->userDefinedMethods()) {
565 v.push_back(InterfaceAndMethod(iface, userMethod));
566 }
567 }
568 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800569 v.push_back(InterfaceAndMethod(
570 *chain.rbegin(), // IBase
571 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700572 }
573 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700574}
575
Steven Moreland14ee6742016-10-18 12:58:28 -0700576Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700577 for (const auto &tuple : allMethodsFromRoot()) {
578 Method *method = tuple.method();
579 if (method->name() == name) {
580 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700581 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700582 }
583
584 return nullptr;
585}
586
Steven Moreland40786312016-08-16 10:29:40 -0700587std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700588 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700589}
590
Yifan Hongeefe4f22017-01-04 15:32:42 -0800591std::string Interface::getProxyName() const {
592 return fqName().getInterfaceProxyName();
593}
594
595std::string Interface::getStubName() const {
596 return fqName().getInterfaceStubName();
597}
598
599std::string Interface::getHwName() const {
600 return fqName().getInterfaceHwName();
601}
602
603std::string Interface::getPassthroughName() const {
604 return fqName().getInterfacePassthroughName();
605}
606
Yifan Hong51a65092017-01-04 15:41:44 -0800607FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800608 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800609}
610
Yifan Hong51a65092017-01-04 15:41:44 -0800611FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800612 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800613}
614
Yifan Hong51a65092017-01-04 15:41:44 -0800615FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800616 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800617}
618
Steven Moreland979e0992016-09-07 09:18:08 -0700619std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700620 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700621 const std::string base =
622 std::string(specifyNamespaces ? "::android::" : "")
623 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700624 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700625 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700626
627 switch (mode) {
628 case StorageMode_Stack:
629 case StorageMode_Result:
630 return base;
631
632 case StorageMode_Argument:
633 return "const " + base + "&";
634 }
635}
636
Yifan Hong4ed13472016-11-02 10:44:11 -0700637std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700638 return fullJavaName();
639}
640
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800641std::string Interface::getVtsType() const {
642 if (StringHelper::EndsWith(localName(), "Callback")) {
643 return "TYPE_HIDL_CALLBACK";
644 } else {
645 return "TYPE_HIDL_INTERFACE";
646 }
647}
648
Andreas Huber881227d2016-08-02 14:20:21 -0700649void Interface::emitReaderWriter(
650 Formatter &out,
651 const std::string &name,
652 const std::string &parcelObj,
653 bool parcelObjIsPointer,
654 bool isReader,
655 ErrorMode mode) const {
656 const std::string parcelObjDeref =
657 parcelObj + (parcelObjIsPointer ? "->" : ".");
658
659 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700660 out << "{\n";
661 out.indent();
662
Iliyan Malchev549e2592016-08-10 08:59:12 -0700663 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700664
Andreas Huber8a82ff72016-08-04 10:29:39 -0700665 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700666 << binderName << ";\n";
667
Iliyan Malchev549e2592016-08-10 08:59:12 -0700668 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700669 out << parcelObjDeref
670 << "readNullableStrongBinder(&"
671 << binderName
672 << ");\n";
673
674 handleError(out, mode);
675
676 out << name
677 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100678 << "::android::hardware::fromBinder<"
679 << fqName().cppName()
680 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800681 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100682 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800683 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100684 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700685 << binderName
686 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700687
688 out.unindent();
689 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700690 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200691 out << "if (" << name << " == nullptr) {\n";
692 out.indent();
693 out << "_hidl_err = ";
694 out << parcelObjDeref
695 << "writeStrongBinder(nullptr);\n";
696 out.unindent();
697 out << "} else {\n";
698 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800699 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
700 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800701 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100702 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800703 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800704 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800705 << ">("
706 << name
707 << ");\n";
708 });
709 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800710 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800711 out << "_hidl_err = "
712 << parcelObjDeref
713 << "writeStrongBinder(_hidl_binder);\n";
714 });
715 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800716 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800717 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
718 });
719 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200720 out.unindent();
721 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700722
Andreas Huber881227d2016-08-02 14:20:21 -0700723 handleError(out, mode);
724 }
725}
726
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800727status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
728 status_t status = Scope::emitGlobalTypeDeclarations(out);
729 if (status != OK) {
730 return status;
731 }
Steven Moreland3d98bc42017-06-23 21:36:41 +0000732 out << "std::string toString("
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800733 << getCppArgumentType()
Steven Moreland3d98bc42017-06-23 21:36:41 +0000734 << ");\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800735 return OK;
736}
737
738
739status_t Interface::emitTypeDefinitions(
740 Formatter &out, const std::string prefix) const {
741 std::string space = prefix.empty() ? "" : (prefix + "::");
742 status_t err = Scope::emitTypeDefinitions(out, space + localName());
743 if (err != OK) {
744 return err;
745 }
746
Steven Moreland3d98bc42017-06-23 21:36:41 +0000747 out << "std::string toString("
748 << getCppArgumentType()
749 << " o) ";
750
751 out.block([&] {
752 out << "std::string os = \"[class or subclass of \";\n"
753 << "os += " << fullName() << "::descriptor;\n"
754 << "os += \"]\";\n"
755 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
756 << "return os;\n";
757 }).endl().endl();
758
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800759 return OK;
760}
761
Andreas Huber2831d512016-08-15 09:33:47 -0700762void Interface::emitJavaReaderWriter(
763 Formatter &out,
764 const std::string &parcelObj,
765 const std::string &argName,
766 bool isReader) const {
767 if (isReader) {
768 out << fullJavaName()
769 << ".asInterface("
770 << parcelObj
771 << ".readStrongBinder());\n";
772 } else {
773 out << parcelObj
774 << ".writeStrongBinder("
775 << argName
776 << " == null ? null : "
777 << argName
778 << ".asBinder());\n";
779 }
780}
781
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700782status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
783 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700784 // Skip for TypeDef as it is just an alias of a defined type.
785 if (type->isTypeDef()) {
786 continue;
787 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700788 out << "attribute: {\n";
789 out.indent();
790 status_t status = type->emitVtsTypeDeclarations(out);
791 if (status != OK) {
792 return status;
793 }
794 out.unindent();
795 out << "}\n\n";
796 }
797 return OK;
798}
799
800status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700801 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800802 if (method->isHidlReserved()) {
803 continue;
804 }
805
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700806 out << "api: {\n";
807 out.indent();
808 out << "name: \"" << method->name() << "\"\n";
809 // Generate declaration for each return value.
810 for (const auto &result : method->results()) {
811 out << "return_type_hidl: {\n";
812 out.indent();
813 status_t status = result->type().emitVtsAttributeType(out);
814 if (status != OK) {
815 return status;
816 }
817 out.unindent();
818 out << "}\n";
819 }
820 // Generate declaration for each input argument
821 for (const auto &arg : method->args()) {
822 out << "arg: {\n";
823 out.indent();
824 status_t status = arg->type().emitVtsAttributeType(out);
825 if (status != OK) {
826 return status;
827 }
828 out.unindent();
829 out << "}\n";
830 }
831 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700832 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700833 out << "callflow: {\n";
834 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700835 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700836 if (name == "entry") {
837 out << "entry: true\n";
838 } else if (name == "exit") {
839 out << "exit: true\n";
840 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700841 const AnnotationParam *param =
842 annotation->getParam("next");
843 if (param != nullptr) {
844 for (auto value : *param->getValues()) {
845 out << "next: " << value << "\n";
846 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700847 }
848 } else {
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800849 std::cerr << "Unrecognized annotation '"
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700850 << name << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800851 << ". A VTS annotation should be one of: "
852 << "entry, exit, callflow. \n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700853 }
854 out.unindent();
855 out << "}\n";
856 }
857 out.unindent();
858 out << "}\n\n";
859 }
860 return OK;
861}
862
863status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800864 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700865 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800866 << fullName()
867 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700868 return OK;
869}
870
Steven Moreland69e7c702016-09-09 11:16:32 -0700871bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700872 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700873 if (method->isOneway()) {
874 return true;
875 }
876 }
877
878 const Interface* superClass = superType();
879
880 if (superClass != nullptr) {
881 return superClass->hasOnewayMethods();
882 }
883
884 return false;
885}
886
Andreas Huber70a59e12016-08-16 12:57:01 -0700887bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700888 if (mIsJavaCompatibleInProgress) {
889 // We're currently trying to determine if this Interface is
890 // java-compatible and something is referencing this interface through
891 // one of its methods. Assume we'll ultimately succeed, if we were wrong
892 // the original invocation of Interface::isJavaCompatible() will then
893 // return the correct "false" result.
894 return true;
895 }
896
Andreas Huber0fa9e392016-08-31 09:05:44 -0700897 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
898 mIsJavaCompatibleInProgress = false;
899 return false;
900 }
901
Andreas Huberea081b32016-08-17 15:57:47 -0700902 mIsJavaCompatibleInProgress = true;
903
Andreas Huber70a59e12016-08-16 12:57:01 -0700904 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700905 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700906 return false;
907 }
908
Yifan Hong10fe0b52016-10-19 14:20:17 -0700909 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700910 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700911 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700912 return false;
913 }
914 }
915
Andreas Huberea081b32016-08-17 15:57:47 -0700916 mIsJavaCompatibleInProgress = false;
917
Andreas Huber70a59e12016-08-16 12:57:01 -0700918 return true;
919}
920
Andreas Huberc9410c72016-07-28 12:18:40 -0700921} // namespace android
922