blob: c592bb45a0b72718ff1eae06fe2e52d239d6114e [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 {
52 // These values are defined in hardware::IBinder.
53 /////////////////// User defined transactions
54 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080055 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070056 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080057 FIRST_HIDL_TRANSACTION = 0x0f000000,
58 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
59 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
60 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
61 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
62 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
63 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
64 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
65 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
66 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
Yifan Hong30b5d1f2017-04-03 12:19:25 -070067 HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'),
Steven Morelandb8e15a52017-02-13 19:35:40 -080068 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070069};
70
Yifan Honga4b53d02016-10-31 17:29:10 -070071Interface::Interface(const char *localName, const Location &location, Interface *super)
72 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070073 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070074 mIsJavaCompatibleInProgress(false) {
Martijn Coenenaf712c02016-11-16 15:26:27 +010075}
76
Steven Moreland30bb6a82016-11-30 09:18:34 -080077std::string Interface::typeName() const {
78 return "interface " + localName();
79}
80
Steven Moreland424a9482017-02-13 19:20:40 -080081bool Interface::fillPingMethod(Method *method) const {
82 if (method->name() != "ping") {
83 return false;
84 }
85
86 method->fillImplementation(
87 HIDL_PING_TRANSACTION,
88 {
Steven Moreland937408a2017-03-20 09:54:18 -070089 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080090 [](auto &out) {
91 out << "return ::android::hardware::Void();\n";
92 }
93 },
94 {IMPL_STUB_IMPL,
95 [](auto &out) {
96 out << "return ::android::hardware::Void();\n";
97 }
98 }
99 }, /*cppImpl*/
100 {
Steven Moreland937408a2017-03-20 09:54:18 -0700101 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -0800102 [this](auto &out) {
103 out << "return;\n";
104 }
105 },
106 {IMPL_STUB, nullptr /* don't generate code */}
107 } /*javaImpl*/
108 );
109
110 return true;
111}
112
Yifan Hongffa91392017-01-31 13:41:23 -0800113bool Interface::fillLinkToDeathMethod(Method *method) const {
114 if (method->name() != "linkToDeath") {
115 return false;
116 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100117
Yifan Hongffa91392017-01-31 13:41:23 -0800118 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100119 HIDL_LINK_TO_DEATH_TRANSACTION,
120 {
Steven Moreland937408a2017-03-20 09:54:18 -0700121 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100122 [](auto &out) {
123 out << "(void)cookie;\n"
124 << "return (recipient != nullptr);\n";
125 }
126 },
127 {IMPL_PROXY,
128 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100129 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100130 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
131 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
132 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
133 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
134 << "return (remote()->linkToDeath(binder_recipient)"
135 << " == ::android::OK);\n";
136 }
137 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100138 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100139 }, /*cppImpl*/
140 {
Steven Moreland937408a2017-03-20 09:54:18 -0700141 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100142 [this](auto &out) {
143 out << "return true;";
144 }
145 },
146 {IMPL_PROXY,
147 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100148 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100149 }
150 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100151 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100152 } /*javaImpl*/
153 );
Yifan Hongffa91392017-01-31 13:41:23 -0800154 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100155}
156
Yifan Hongffa91392017-01-31 13:41:23 -0800157bool Interface::fillUnlinkToDeathMethod(Method *method) const {
158 if (method->name() != "unlinkToDeath") {
159 return false;
160 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100161
Yifan Hongffa91392017-01-31 13:41:23 -0800162 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100163 HIDL_UNLINK_TO_DEATH_TRANSACTION,
164 {
Steven Moreland937408a2017-03-20 09:54:18 -0700165 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100166 [](auto &out) {
167 out << "return (recipient != nullptr);\n";
168 }
169 },
170 {IMPL_PROXY,
171 [](auto &out) {
172 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
173 << "for (auto it = _hidl_mDeathRecipients.begin();"
174 << "it != _hidl_mDeathRecipients.end();"
175 << "++it) {\n";
176 out.indent([&] {
177 out.sIf("(*it)->getRecipient() == recipient", [&] {
178 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
179 << "_hidl_mDeathRecipients.erase(it);\n"
180 << "return status == ::android::OK;\n";
181 });
182 });
183 out << "}\n";
184 out << "return false;\n";
185 }
186 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100187 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100188 }, /*cppImpl*/
189 {
Steven Moreland937408a2017-03-20 09:54:18 -0700190 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100191 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100192 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100193 }
194 },
195 {IMPL_PROXY,
196 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100197 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100198 }
199 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100200 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100201 } /*javaImpl*/
202 );
Yifan Hongffa91392017-01-31 13:41:23 -0800203 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100204}
Yifan Hongffa91392017-01-31 13:41:23 -0800205bool Interface::fillSyspropsChangedMethod(Method *method) const {
206 if (method->name() != "notifySyspropsChanged") {
207 return false;
208 }
209
210 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100211 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700212 { { IMPL_INTERFACE, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100213 out << "::android::report_sysprop_change();\n";
214 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100215 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700216 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100217 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100218 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100219 );
Yifan Hongffa91392017-01-31 13:41:23 -0800220 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700221}
222
Yifan Hongffa91392017-01-31 13:41:23 -0800223bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
224 if (method->name() != "setHALInstrumentation") {
225 return false;
226 }
227
228 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800229 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
230 {
Steven Moreland937408a2017-03-20 09:54:18 -0700231 {IMPL_INTERFACE,
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800232 [this](auto &out) {
233 // do nothing for base class.
234 out << "return ::android::hardware::Void();\n";
235 }
236 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800237 {IMPL_STUB,
238 [](auto &out) {
239 out << "configureInstrumentation();\n";
240 }
241 },
242 {IMPL_PASSTHROUGH,
243 [](auto &out) {
244 out << "configureInstrumentation();\n";
245 out << "return ::android::hardware::Void();\n";
246 }
247 },
248 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700249 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800250 // Not support for Java Impl for now.
251 } } } /*javaImpl */
252 );
Yifan Hongffa91392017-01-31 13:41:23 -0800253 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800254}
255
Yifan Hongffa91392017-01-31 13:41:23 -0800256bool Interface::fillDescriptorChainMethod(Method *method) const {
257 if (method->name() != "interfaceChain") {
258 return false;
259 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700260
Yifan Hongffa91392017-01-31 13:41:23 -0800261 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700262 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700263 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700264 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800265 out << "_hidl_cb(";
266 out.block([&] {
267 for (const Interface *iface : chain) {
268 out << iface->fullName() << "::descriptor,\n";
269 }
270 });
271 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700272 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100273 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700274 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700275 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800276 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700277 out.indent(); out.indent();
278 for (size_t i = 0; i < chain.size(); ++i) {
279 if (i != 0)
280 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800281 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700282 }
283 out << "));";
284 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800285 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100286 );
Yifan Hongffa91392017-01-31 13:41:23 -0800287 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700288}
289
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700290static void emitDigestChain(
291 Formatter &out,
292 const std::string &prefix,
293 const std::vector<const Interface *> &chain,
294 std::function<std::string(const ConstantExpression &)> byteToString) {
295 out.join(chain.begin(), chain.end(), ",\n", [&] (const auto &iface) {
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700296 const Hash &hash = Hash::getHash(iface->location().begin().filename());
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700297 out << prefix;
298 out << "{";
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700299 out.join(hash.raw().begin(), hash.raw().end(), ",", [&](const auto &e) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700300 // Use ConstantExpression::cppValue / javaValue
301 // because Java used signed byte for uint8_t.
302 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
303 });
304 out << "} /* ";
Steven Moreland0e4be1e2017-04-18 19:50:29 -0700305 out << hash.hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700306 out << " */";
307 });
308}
309
310bool Interface::fillHashChainMethod(Method *method) const {
311 if (method->name() != "getHashChain") {
312 return false;
313 }
314 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
315 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
316
317 method->fillImplementation(
318 HIDL_HASH_CHAIN_TRANSACTION,
319 { { IMPL_INTERFACE, [this, digestType](auto &out) {
320 std::vector<const Interface *> chain = typeChain();
321 out << "_hidl_cb(";
322 out.block([&] {
323 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")",
324 chain, [](const auto &e){return e.cppValue();});
325 });
326 out << ");\n";
327 out << "return ::android::hardware::Void();\n";
328 } } }, /* cppImpl */
329 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
330 std::vector<const Interface *> chain = typeChain();
331 out << "return new "
332 << chainType->getJavaType(false /* forInitializer */)
333 << "(java.util.Arrays.asList(\n";
334 out.indent(2, [&] {
335 // No need for dimensions when elements are explicitly provided.
336 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
337 chain, [](const auto &e){return e.javaValue();});
338 });
339 out << "));\n";
340 } } } /* javaImpl */
341 );
342 return true;
343}
344
Yifan Hongffa91392017-01-31 13:41:23 -0800345bool Interface::fillGetDescriptorMethod(Method *method) const {
346 if (method->name() != "interfaceDescriptor") {
347 return false;
348 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800349
Yifan Hongffa91392017-01-31 13:41:23 -0800350 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800351 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700352 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800353 out << "_hidl_cb("
354 << fullName()
355 << "::descriptor);\n"
356 << "return ::android::hardware::Void();";
357 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700358 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800359 out << "return "
360 << fullJavaName()
361 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800362 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800363 );
Yifan Hongffa91392017-01-31 13:41:23 -0800364 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800365}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700366
Yifan Hongbcffce22017-02-01 15:52:06 -0800367bool Interface::fillGetDebugInfoMethod(Method *method) const {
368 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800369 return false;
370 }
371
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800372 static const std::string sArch =
373 "#if defined(__LP64__)\n"
374 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
375 "#else\n"
376 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
377 "#endif\n";
378
Yifan Hongcd2ae452017-01-31 14:33:40 -0800379 method->fillImplementation(
380 HIDL_GET_REF_INFO_TRANSACTION,
381 {
Steven Moreland937408a2017-03-20 09:54:18 -0700382 {IMPL_INTERFACE,
Yifan Hongcd2ae452017-01-31 14:33:40 -0800383 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800384 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800385 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
386 << sArch
387 << "});\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800388 << "return ::android::hardware::Void();";
389 }
390 },
391 {IMPL_STUB_IMPL,
392 [this](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800393 out << "_hidl_cb(";
394 out.block([&] {
395 out << "::android::hardware::details::debuggable()"
396 << "? getpid() : -1 /* pid */,\n"
397 << "::android::hardware::details::debuggable()"
398 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
399 << sArch << "\n";
400 });
401 out << ");\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800402 << "return ::android::hardware::Void();";
403 }
404 }
405 }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700406 { { IMPL_INTERFACE, [this, method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800407 const Type &refInfo = method->results().front()->type();
408 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
409 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800410 // TODO(b/34777099): PID for java.
411 << "info.pid = -1;\n"
412 << "info.ptr = 0;\n"
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800413 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800414 << "return info;";
415 } } } /* javaImpl */
416 );
417
418 return true;
419}
420
Andreas Huber37065d62017-02-07 14:36:54 -0800421bool Interface::fillDebugMethod(Method *method) const {
422 if (method->name() != "debug") {
423 return false;
424 }
425
426 method->fillImplementation(
427 HIDL_DEBUG_TRANSACTION,
428 {
Steven Moreland937408a2017-03-20 09:54:18 -0700429 {IMPL_INTERFACE,
Andreas Huber37065d62017-02-07 14:36:54 -0800430 [this](auto &out) {
431 out << "(void)fd;\n"
432 << "(void)options;\n"
433 << "return ::android::hardware::Void();";
434 }
435 },
436 }, /* cppImpl */
437 {
438 /* unused, as the debug method is hidden from Java */
439 } /* javaImpl */
440 );
441
442 return true;
443}
444
Yifan Hongffa91392017-01-31 13:41:23 -0800445static std::map<std::string, Method *> gAllReservedMethods;
446
Steven Moreland14ee6742016-10-18 12:58:28 -0700447bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800448 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800449 if (!gAllReservedMethods.emplace(method->name(), method).second) {
450 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
451 << method->name();
452 return false;
453 }
454 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800455 return true;
456 }
457
Yifan Hong10fe0b52016-10-19 14:20:17 -0700458 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700459 if (lookupMethod(method->name()) != nullptr) {
460 LOG(ERROR) << "Redefinition of method " << method->name();
461 return false;
462 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700463 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700464
Yifan Hong10fe0b52016-10-19 14:20:17 -0700465 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700466
Yifan Hong10fe0b52016-10-19 14:20:17 -0700467 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700468 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700469 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700470 ancestor = ancestor->superType();
471 }
472
Yifan Hong10fe0b52016-10-19 14:20:17 -0700473 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
474 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700475 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700476 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700477
478 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700479}
480
Yifan Hongffa91392017-01-31 13:41:23 -0800481bool Interface::addAllReservedMethods() {
482 // use a sorted map to insert them in serial ID order.
483 std::map<int32_t, Method *> reservedMethodsById;
484 for (const auto &pair : gAllReservedMethods) {
485 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800486 bool fillSuccess = fillPingMethod(method)
487 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800488 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700489 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800490 || fillSyspropsChangedMethod(method)
491 || fillLinkToDeathMethod(method)
492 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800493 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800494 || fillGetDebugInfoMethod(method)
495 || fillDebugMethod(method);
496
Yifan Hongffa91392017-01-31 13:41:23 -0800497 if (!fillSuccess) {
498 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
499 << method->name();
500 return false;
501 }
502 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
503 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
504 << method->name() << " and "
505 << reservedMethodsById[method->getSerialId()]->name()
506 << ", serialId = " << method->getSerialId();
507 return false;
508 }
509 }
510 for (const auto &pair : reservedMethodsById) {
511 this->mReservedMethods.push_back(pair.second);
512 }
513 return true;
514}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700515
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700516const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700517 return mSuperType;
518}
519
Yifan Hong10fe0b52016-10-19 14:20:17 -0700520std::vector<const Interface *> Interface::typeChain() const {
521 std::vector<const Interface *> v;
522 const Interface *iface = this;
523 while (iface != nullptr) {
524 v.push_back(iface);
525 iface = iface->mSuperType;
526 }
527 return v;
528}
529
Yifan Hongfe95aa22016-10-19 17:26:45 -0700530std::vector<const Interface *> Interface::superTypeChain() const {
531 return superType()->typeChain(); // should work even if superType is nullptr
532}
533
Martijn Coenenb40ef022017-01-02 15:21:46 +0100534bool Interface::isElidableType() const {
535 return true;
536}
537
Andreas Hubera2723d22016-07-29 15:36:07 -0700538bool Interface::isInterface() const {
539 return true;
540}
541
Andreas Huber295ad302016-08-16 11:35:00 -0700542bool Interface::isBinder() const {
543 return true;
544}
545
Yifan Hong10fe0b52016-10-19 14:20:17 -0700546const std::vector<Method *> &Interface::userDefinedMethods() const {
547 return mUserMethods;
548}
549
550const std::vector<Method *> &Interface::hidlReservedMethods() const {
551 return mReservedMethods;
552}
553
554std::vector<Method *> Interface::methods() const {
555 std::vector<Method *> v(mUserMethods);
556 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
557 return v;
558}
559
560std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
561 std::vector<InterfaceAndMethod> v;
562 std::vector<const Interface *> chain = typeChain();
563 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
564 const Interface *iface = *it;
565 for (Method *userMethod : iface->userDefinedMethods()) {
566 v.push_back(InterfaceAndMethod(iface, userMethod));
567 }
568 }
569 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800570 v.push_back(InterfaceAndMethod(
571 *chain.rbegin(), // IBase
572 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700573 }
574 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700575}
576
Steven Moreland14ee6742016-10-18 12:58:28 -0700577Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700578 for (const auto &tuple : allMethodsFromRoot()) {
579 Method *method = tuple.method();
580 if (method->name() == name) {
581 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700582 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700583 }
584
585 return nullptr;
586}
587
Steven Moreland40786312016-08-16 10:29:40 -0700588std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700589 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700590}
591
Yifan Hongeefe4f22017-01-04 15:32:42 -0800592std::string Interface::getProxyName() const {
593 return fqName().getInterfaceProxyName();
594}
595
596std::string Interface::getStubName() const {
597 return fqName().getInterfaceStubName();
598}
599
600std::string Interface::getHwName() const {
601 return fqName().getInterfaceHwName();
602}
603
604std::string Interface::getPassthroughName() const {
605 return fqName().getInterfacePassthroughName();
606}
607
Yifan Hong51a65092017-01-04 15:41:44 -0800608FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800609 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800610}
611
Yifan Hong51a65092017-01-04 15:41:44 -0800612FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800613 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800614}
615
Yifan Hong51a65092017-01-04 15:41:44 -0800616FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800617 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800618}
619
Steven Moreland979e0992016-09-07 09:18:08 -0700620std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700621 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700622 const std::string base =
623 std::string(specifyNamespaces ? "::android::" : "")
624 + "sp<"
625 + (specifyNamespaces ? fullName() : partialCppName())
626 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700627
628 switch (mode) {
629 case StorageMode_Stack:
630 case StorageMode_Result:
631 return base;
632
633 case StorageMode_Argument:
634 return "const " + base + "&";
635 }
636}
637
Yifan Hong4ed13472016-11-02 10:44:11 -0700638std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700639 return fullJavaName();
640}
641
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800642std::string Interface::getVtsType() const {
643 if (StringHelper::EndsWith(localName(), "Callback")) {
644 return "TYPE_HIDL_CALLBACK";
645 } else {
646 return "TYPE_HIDL_INTERFACE";
647 }
648}
649
Andreas Huber881227d2016-08-02 14:20:21 -0700650void Interface::emitReaderWriter(
651 Formatter &out,
652 const std::string &name,
653 const std::string &parcelObj,
654 bool parcelObjIsPointer,
655 bool isReader,
656 ErrorMode mode) const {
657 const std::string parcelObjDeref =
658 parcelObj + (parcelObjIsPointer ? "->" : ".");
659
660 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700661 out << "{\n";
662 out.indent();
663
Iliyan Malchev549e2592016-08-10 08:59:12 -0700664 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700665
Andreas Huber8a82ff72016-08-04 10:29:39 -0700666 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700667 << binderName << ";\n";
668
Iliyan Malchev549e2592016-08-10 08:59:12 -0700669 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700670 out << parcelObjDeref
671 << "readNullableStrongBinder(&"
672 << binderName
673 << ");\n";
674
675 handleError(out, mode);
676
677 out << name
678 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100679 << "::android::hardware::fromBinder<"
680 << fqName().cppName()
681 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800682 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100683 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800684 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100685 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700686 << binderName
687 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700688
689 out.unindent();
690 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700691 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200692 out << "if (" << name << " == nullptr) {\n";
693 out.indent();
694 out << "_hidl_err = ";
695 out << parcelObjDeref
696 << "writeStrongBinder(nullptr);\n";
697 out.unindent();
698 out << "} else {\n";
699 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800700 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
701 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800702 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100703 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800704 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800705 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800706 << ">("
707 << name
708 << ");\n";
709 });
710 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800711 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800712 out << "_hidl_err = "
713 << parcelObjDeref
714 << "writeStrongBinder(_hidl_binder);\n";
715 });
716 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800717 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800718 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
719 });
720 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200721 out.unindent();
722 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700723
Andreas Huber881227d2016-08-02 14:20:21 -0700724 handleError(out, mode);
725 }
726}
727
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800728status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
729 status_t status = Scope::emitGlobalTypeDeclarations(out);
730 if (status != OK) {
731 return status;
732 }
733 out << "std::string toString("
734 << getCppArgumentType()
735 << ");\n";
736 return OK;
737}
738
739
740status_t Interface::emitTypeDefinitions(
741 Formatter &out, const std::string prefix) const {
742 std::string space = prefix.empty() ? "" : (prefix + "::");
743 status_t err = Scope::emitTypeDefinitions(out, space + localName());
744 if (err != OK) {
745 return err;
746 }
747
748 out << "std::string toString("
749 << getCppArgumentType()
750 << " o) ";
751
752 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800753 out << "std::string os;\n";
754 out << "auto ret = o->interfaceDescriptor([&os] (const auto &name) ";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800755 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800756 out << "os += name.c_str();\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800757 });
758 out << ");\n";
Yifan Hongfbcdc802017-02-22 18:12:48 -0800759 out.sIf("!ret.isOk()", [&] {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800760 out << "os += \"[class or subclass of \";\n"
761 << "os += " << fullName() << "::descriptor;\n"
762 << "os += \"]\";\n";
763 }).endl();
764 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
765 << "return os;\n";
766 }).endl().endl();
767
768 return OK;
769}
770
Andreas Huber2831d512016-08-15 09:33:47 -0700771void Interface::emitJavaReaderWriter(
772 Formatter &out,
773 const std::string &parcelObj,
774 const std::string &argName,
775 bool isReader) const {
776 if (isReader) {
777 out << fullJavaName()
778 << ".asInterface("
779 << parcelObj
780 << ".readStrongBinder());\n";
781 } else {
782 out << parcelObj
783 << ".writeStrongBinder("
784 << argName
785 << " == null ? null : "
786 << argName
787 << ".asBinder());\n";
788 }
789}
790
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700791status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
792 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700793 // Skip for TypeDef as it is just an alias of a defined type.
794 if (type->isTypeDef()) {
795 continue;
796 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700797 out << "attribute: {\n";
798 out.indent();
799 status_t status = type->emitVtsTypeDeclarations(out);
800 if (status != OK) {
801 return status;
802 }
803 out.unindent();
804 out << "}\n\n";
805 }
806 return OK;
807}
808
809status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700810 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800811 if (method->isHidlReserved()) {
812 continue;
813 }
814
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700815 out << "api: {\n";
816 out.indent();
817 out << "name: \"" << method->name() << "\"\n";
818 // Generate declaration for each return value.
819 for (const auto &result : method->results()) {
820 out << "return_type_hidl: {\n";
821 out.indent();
822 status_t status = result->type().emitVtsAttributeType(out);
823 if (status != OK) {
824 return status;
825 }
826 out.unindent();
827 out << "}\n";
828 }
829 // Generate declaration for each input argument
830 for (const auto &arg : method->args()) {
831 out << "arg: {\n";
832 out.indent();
833 status_t status = arg->type().emitVtsAttributeType(out);
834 if (status != OK) {
835 return status;
836 }
837 out.unindent();
838 out << "}\n";
839 }
840 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700841 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700842 out << "callflow: {\n";
843 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700844 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700845 if (name == "entry") {
846 out << "entry: true\n";
847 } else if (name == "exit") {
848 out << "exit: true\n";
849 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700850 const AnnotationParam *param =
851 annotation->getParam("next");
852 if (param != nullptr) {
853 for (auto value : *param->getValues()) {
854 out << "next: " << value << "\n";
855 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700856 }
857 } else {
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800858 std::cerr << "Unrecognized annotation '"
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700859 << name << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800860 << ". A VTS annotation should be one of: "
861 << "entry, exit, callflow. \n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700862 }
863 out.unindent();
864 out << "}\n";
865 }
866 out.unindent();
867 out << "}\n\n";
868 }
869 return OK;
870}
871
872status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800873 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700874 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800875 << fullName()
876 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700877 return OK;
878}
879
Steven Moreland69e7c702016-09-09 11:16:32 -0700880bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700881 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700882 if (method->isOneway()) {
883 return true;
884 }
885 }
886
887 const Interface* superClass = superType();
888
889 if (superClass != nullptr) {
890 return superClass->hasOnewayMethods();
891 }
892
893 return false;
894}
895
Andreas Huber70a59e12016-08-16 12:57:01 -0700896bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700897 if (mIsJavaCompatibleInProgress) {
898 // We're currently trying to determine if this Interface is
899 // java-compatible and something is referencing this interface through
900 // one of its methods. Assume we'll ultimately succeed, if we were wrong
901 // the original invocation of Interface::isJavaCompatible() will then
902 // return the correct "false" result.
903 return true;
904 }
905
Andreas Huber0fa9e392016-08-31 09:05:44 -0700906 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
907 mIsJavaCompatibleInProgress = false;
908 return false;
909 }
910
Andreas Huberea081b32016-08-17 15:57:47 -0700911 mIsJavaCompatibleInProgress = true;
912
Andreas Huber70a59e12016-08-16 12:57:01 -0700913 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700914 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700915 return false;
916 }
917
Yifan Hong10fe0b52016-10-19 14:20:17 -0700918 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700919 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700920 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700921 return false;
922 }
923 }
924
Andreas Huberea081b32016-08-17 15:57:47 -0700925 mIsJavaCompatibleInProgress = false;
926
Andreas Huber70a59e12016-08-16 12:57:01 -0700927 return true;
928}
929
Andreas Huberc9410c72016-07-28 12:18:40 -0700930} // namespace android
931