blob: d326703af9d63cf64b44b37cb741e9d0e8d16ba1 [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
30#include <fstream>
31#include <iostream>
32#include <sstream>
33
Steven Moreland14ee6742016-10-18 12:58:28 -070034#include <android-base/logging.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>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070037#include <openssl/sha.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070038
Andreas Huberc9410c72016-07-28 12:18:40 -070039namespace android {
40
Steven Morelandb8e15a52017-02-13 19:35:40 -080041#define B_PACK_CHARS(c1, c2, c3, c4) \
42 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
43
Yifan Hong10fe0b52016-10-19 14:20:17 -070044/* It is very important that these values NEVER change. These values
45 * must remain unchanged over the lifetime of android. This is
46 * because the framework on a device will be updated independently of
47 * the hals on a device. If the hals are compiled with one set of
48 * transaction values, and the framework with another, then the
49 * interface between them will be destroyed, and the device will not
50 * work.
51 */
52enum {
53 // These values are defined in hardware::IBinder.
54 /////////////////// User defined transactions
55 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080056 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070057 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080058 FIRST_HIDL_TRANSACTION = 0x0f000000,
59 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
60 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
61 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
62 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
63 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
64 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
65 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
66 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
67 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
Yifan Hong30b5d1f2017-04-03 12:19:25 -070068 HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'),
Steven Morelandb8e15a52017-02-13 19:35:40 -080069 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070070};
71
Yifan Honga4b53d02016-10-31 17:29:10 -070072Interface::Interface(const char *localName, const Location &location, Interface *super)
73 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070074 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070075 mIsJavaCompatibleInProgress(false) {
Martijn Coenenaf712c02016-11-16 15:26:27 +010076}
77
Steven Moreland30bb6a82016-11-30 09:18:34 -080078std::string Interface::typeName() const {
79 return "interface " + localName();
80}
81
Steven Moreland424a9482017-02-13 19:20:40 -080082bool Interface::fillPingMethod(Method *method) const {
83 if (method->name() != "ping") {
84 return false;
85 }
86
87 method->fillImplementation(
88 HIDL_PING_TRANSACTION,
89 {
Steven Moreland937408a2017-03-20 09:54:18 -070090 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080091 [](auto &out) {
92 out << "return ::android::hardware::Void();\n";
93 }
94 },
95 {IMPL_STUB_IMPL,
96 [](auto &out) {
97 out << "return ::android::hardware::Void();\n";
98 }
99 }
100 }, /*cppImpl*/
101 {
Steven Moreland937408a2017-03-20 09:54:18 -0700102 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -0800103 [this](auto &out) {
104 out << "return;\n";
105 }
106 },
107 {IMPL_STUB, nullptr /* don't generate code */}
108 } /*javaImpl*/
109 );
110
111 return true;
112}
113
Yifan Hongffa91392017-01-31 13:41:23 -0800114bool Interface::fillLinkToDeathMethod(Method *method) const {
115 if (method->name() != "linkToDeath") {
116 return false;
117 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100118
Yifan Hongffa91392017-01-31 13:41:23 -0800119 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100120 HIDL_LINK_TO_DEATH_TRANSACTION,
121 {
Steven Moreland937408a2017-03-20 09:54:18 -0700122 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100123 [](auto &out) {
124 out << "(void)cookie;\n"
125 << "return (recipient != nullptr);\n";
126 }
127 },
128 {IMPL_PROXY,
129 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100130 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100131 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
132 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
133 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
134 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
135 << "return (remote()->linkToDeath(binder_recipient)"
136 << " == ::android::OK);\n";
137 }
138 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100139 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100140 }, /*cppImpl*/
141 {
Steven Moreland937408a2017-03-20 09:54:18 -0700142 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100143 [this](auto &out) {
144 out << "return true;";
145 }
146 },
147 {IMPL_PROXY,
148 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100149 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100150 }
151 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100152 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100153 } /*javaImpl*/
154 );
Yifan Hongffa91392017-01-31 13:41:23 -0800155 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100156}
157
Yifan Hongffa91392017-01-31 13:41:23 -0800158bool Interface::fillUnlinkToDeathMethod(Method *method) const {
159 if (method->name() != "unlinkToDeath") {
160 return false;
161 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100162
Yifan Hongffa91392017-01-31 13:41:23 -0800163 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100164 HIDL_UNLINK_TO_DEATH_TRANSACTION,
165 {
Steven Moreland937408a2017-03-20 09:54:18 -0700166 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100167 [](auto &out) {
168 out << "return (recipient != nullptr);\n";
169 }
170 },
171 {IMPL_PROXY,
172 [](auto &out) {
173 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
174 << "for (auto it = _hidl_mDeathRecipients.begin();"
175 << "it != _hidl_mDeathRecipients.end();"
176 << "++it) {\n";
177 out.indent([&] {
178 out.sIf("(*it)->getRecipient() == recipient", [&] {
179 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
180 << "_hidl_mDeathRecipients.erase(it);\n"
181 << "return status == ::android::OK;\n";
182 });
183 });
184 out << "}\n";
185 out << "return false;\n";
186 }
187 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100188 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100189 }, /*cppImpl*/
190 {
Steven Moreland937408a2017-03-20 09:54:18 -0700191 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100192 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100193 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100194 }
195 },
196 {IMPL_PROXY,
197 [this](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100198 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100199 }
200 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100201 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100202 } /*javaImpl*/
203 );
Yifan Hongffa91392017-01-31 13:41:23 -0800204 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100205}
Yifan Hongffa91392017-01-31 13:41:23 -0800206bool Interface::fillSyspropsChangedMethod(Method *method) const {
207 if (method->name() != "notifySyspropsChanged") {
208 return false;
209 }
210
211 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100212 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700213 { { IMPL_INTERFACE, [this](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100214 out << "::android::report_sysprop_change();\n";
215 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100216 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700217 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100218 out << "android.os.SystemProperties.reportSyspropChanged();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100219 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100220 );
Yifan Hongffa91392017-01-31 13:41:23 -0800221 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700222}
223
Yifan Hongffa91392017-01-31 13:41:23 -0800224bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
225 if (method->name() != "setHALInstrumentation") {
226 return false;
227 }
228
229 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800230 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
231 {
Steven Moreland937408a2017-03-20 09:54:18 -0700232 {IMPL_INTERFACE,
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800233 [this](auto &out) {
234 // do nothing for base class.
235 out << "return ::android::hardware::Void();\n";
236 }
237 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800238 {IMPL_STUB,
239 [](auto &out) {
240 out << "configureInstrumentation();\n";
241 }
242 },
243 {IMPL_PASSTHROUGH,
244 [](auto &out) {
245 out << "configureInstrumentation();\n";
246 out << "return ::android::hardware::Void();\n";
247 }
248 },
249 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700250 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800251 // Not support for Java Impl for now.
252 } } } /*javaImpl */
253 );
Yifan Hongffa91392017-01-31 13:41:23 -0800254 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800255}
256
Yifan Hongffa91392017-01-31 13:41:23 -0800257bool Interface::fillDescriptorChainMethod(Method *method) const {
258 if (method->name() != "interfaceChain") {
259 return false;
260 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700261
Yifan Hongffa91392017-01-31 13:41:23 -0800262 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700263 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700264 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700265 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800266 out << "_hidl_cb(";
267 out.block([&] {
268 for (const Interface *iface : chain) {
269 out << iface->fullName() << "::descriptor,\n";
270 }
271 });
272 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700273 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100274 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700275 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700276 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800277 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700278 out.indent(); out.indent();
279 for (size_t i = 0; i < chain.size(); ++i) {
280 if (i != 0)
281 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800282 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700283 }
284 out << "));";
285 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800286 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100287 );
Yifan Hongffa91392017-01-31 13:41:23 -0800288 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700289}
290
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700291static void sha256File(const std::string &path, uint8_t *outDigest) {
292 std::ifstream stream(path);
293 std::stringstream fileStream;
294 fileStream << stream.rdbuf();
295 std::string fileContent = fileStream.str();
296 SHA256(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
297 fileContent.size(), outDigest);
298}
299
300static void emitDigestChain(
301 Formatter &out,
302 const std::string &prefix,
303 const std::vector<const Interface *> &chain,
304 std::function<std::string(const ConstantExpression &)> byteToString) {
305 out.join(chain.begin(), chain.end(), ",\n", [&] (const auto &iface) {
306 const std::string &filename = iface->location().begin().filename();
307 uint8_t digest[SHA256_DIGEST_LENGTH];
308 sha256File(filename, digest);
309 out << prefix;
310 out << "{";
311 out.join(digest, digest + SHA256_DIGEST_LENGTH, ",", [&](const auto &e) {
312 // Use ConstantExpression::cppValue / javaValue
313 // because Java used signed byte for uint8_t.
314 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
315 });
316 out << "} /* ";
317 out.join(digest, digest + SHA256_DIGEST_LENGTH, "", [&](const auto &e) {
318 static const char hexes[] = "0123456789abcdef";
319 out << hexes[e >> 4] << hexes[e & 0xF];
320 });
321 out << " */";
322 });
323}
324
325bool Interface::fillHashChainMethod(Method *method) const {
326 if (method->name() != "getHashChain") {
327 return false;
328 }
329 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
330 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
331
332 method->fillImplementation(
333 HIDL_HASH_CHAIN_TRANSACTION,
334 { { IMPL_INTERFACE, [this, digestType](auto &out) {
335 std::vector<const Interface *> chain = typeChain();
336 out << "_hidl_cb(";
337 out.block([&] {
338 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")",
339 chain, [](const auto &e){return e.cppValue();});
340 });
341 out << ");\n";
342 out << "return ::android::hardware::Void();\n";
343 } } }, /* cppImpl */
344 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
345 std::vector<const Interface *> chain = typeChain();
346 out << "return new "
347 << chainType->getJavaType(false /* forInitializer */)
348 << "(java.util.Arrays.asList(\n";
349 out.indent(2, [&] {
350 // No need for dimensions when elements are explicitly provided.
351 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
352 chain, [](const auto &e){return e.javaValue();});
353 });
354 out << "));\n";
355 } } } /* javaImpl */
356 );
357 return true;
358}
359
Yifan Hongffa91392017-01-31 13:41:23 -0800360bool Interface::fillGetDescriptorMethod(Method *method) const {
361 if (method->name() != "interfaceDescriptor") {
362 return false;
363 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800364
Yifan Hongffa91392017-01-31 13:41:23 -0800365 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800366 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700367 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800368 out << "_hidl_cb("
369 << fullName()
370 << "::descriptor);\n"
371 << "return ::android::hardware::Void();";
372 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700373 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800374 out << "return "
375 << fullJavaName()
376 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800377 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800378 );
Yifan Hongffa91392017-01-31 13:41:23 -0800379 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800380}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700381
Yifan Hongbcffce22017-02-01 15:52:06 -0800382bool Interface::fillGetDebugInfoMethod(Method *method) const {
383 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800384 return false;
385 }
386
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800387 static const std::string sArch =
388 "#if defined(__LP64__)\n"
389 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
390 "#else\n"
391 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
392 "#endif\n";
393
Yifan Hongcd2ae452017-01-31 14:33:40 -0800394 method->fillImplementation(
395 HIDL_GET_REF_INFO_TRANSACTION,
396 {
Steven Moreland937408a2017-03-20 09:54:18 -0700397 {IMPL_INTERFACE,
Yifan Hongcd2ae452017-01-31 14:33:40 -0800398 [this](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800399 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800400 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
401 << sArch
402 << "});\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800403 << "return ::android::hardware::Void();";
404 }
405 },
406 {IMPL_STUB_IMPL,
407 [this](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800408 out << "_hidl_cb(";
409 out.block([&] {
410 out << "::android::hardware::details::debuggable()"
411 << "? getpid() : -1 /* pid */,\n"
412 << "::android::hardware::details::debuggable()"
413 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
414 << sArch << "\n";
415 });
416 out << ");\n"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800417 << "return ::android::hardware::Void();";
418 }
419 }
420 }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700421 { { IMPL_INTERFACE, [this, method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800422 const Type &refInfo = method->results().front()->type();
423 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
424 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800425 // TODO(b/34777099): PID for java.
426 << "info.pid = -1;\n"
427 << "info.ptr = 0;\n"
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800428 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;"
Yifan Hongcd2ae452017-01-31 14:33:40 -0800429 << "return info;";
430 } } } /* javaImpl */
431 );
432
433 return true;
434}
435
Andreas Huber37065d62017-02-07 14:36:54 -0800436bool Interface::fillDebugMethod(Method *method) const {
437 if (method->name() != "debug") {
438 return false;
439 }
440
441 method->fillImplementation(
442 HIDL_DEBUG_TRANSACTION,
443 {
Steven Moreland937408a2017-03-20 09:54:18 -0700444 {IMPL_INTERFACE,
Andreas Huber37065d62017-02-07 14:36:54 -0800445 [this](auto &out) {
446 out << "(void)fd;\n"
447 << "(void)options;\n"
448 << "return ::android::hardware::Void();";
449 }
450 },
451 }, /* cppImpl */
452 {
453 /* unused, as the debug method is hidden from Java */
454 } /* javaImpl */
455 );
456
457 return true;
458}
459
Yifan Hongffa91392017-01-31 13:41:23 -0800460static std::map<std::string, Method *> gAllReservedMethods;
461
Steven Moreland14ee6742016-10-18 12:58:28 -0700462bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800463 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800464 if (!gAllReservedMethods.emplace(method->name(), method).second) {
465 LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method "
466 << method->name();
467 return false;
468 }
469 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800470 return true;
471 }
472
Yifan Hong10fe0b52016-10-19 14:20:17 -0700473 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700474 if (lookupMethod(method->name()) != nullptr) {
475 LOG(ERROR) << "Redefinition of method " << method->name();
476 return false;
477 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700478 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700479
Yifan Hong10fe0b52016-10-19 14:20:17 -0700480 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700481
Yifan Hong10fe0b52016-10-19 14:20:17 -0700482 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700483 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700484 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700485 ancestor = ancestor->superType();
486 }
487
Yifan Hong10fe0b52016-10-19 14:20:17 -0700488 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
489 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700490 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700491 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700492
493 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700494}
495
Yifan Hongffa91392017-01-31 13:41:23 -0800496bool Interface::addAllReservedMethods() {
497 // use a sorted map to insert them in serial ID order.
498 std::map<int32_t, Method *> reservedMethodsById;
499 for (const auto &pair : gAllReservedMethods) {
500 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800501 bool fillSuccess = fillPingMethod(method)
502 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800503 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700504 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800505 || fillSyspropsChangedMethod(method)
506 || fillLinkToDeathMethod(method)
507 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800508 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800509 || fillGetDebugInfoMethod(method)
510 || fillDebugMethod(method);
511
Yifan Hongffa91392017-01-31 13:41:23 -0800512 if (!fillSuccess) {
513 LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method "
514 << method->name();
515 return false;
516 }
517 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
518 LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for "
519 << method->name() << " and "
520 << reservedMethodsById[method->getSerialId()]->name()
521 << ", serialId = " << method->getSerialId();
522 return false;
523 }
524 }
525 for (const auto &pair : reservedMethodsById) {
526 this->mReservedMethods.push_back(pair.second);
527 }
528 return true;
529}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700530
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700531const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700532 return mSuperType;
533}
534
Yifan Hong10fe0b52016-10-19 14:20:17 -0700535std::vector<const Interface *> Interface::typeChain() const {
536 std::vector<const Interface *> v;
537 const Interface *iface = this;
538 while (iface != nullptr) {
539 v.push_back(iface);
540 iface = iface->mSuperType;
541 }
542 return v;
543}
544
Yifan Hongfe95aa22016-10-19 17:26:45 -0700545std::vector<const Interface *> Interface::superTypeChain() const {
546 return superType()->typeChain(); // should work even if superType is nullptr
547}
548
Martijn Coenenb40ef022017-01-02 15:21:46 +0100549bool Interface::isElidableType() const {
550 return true;
551}
552
Andreas Hubera2723d22016-07-29 15:36:07 -0700553bool Interface::isInterface() const {
554 return true;
555}
556
Andreas Huber295ad302016-08-16 11:35:00 -0700557bool Interface::isBinder() const {
558 return true;
559}
560
Yifan Hong10fe0b52016-10-19 14:20:17 -0700561const std::vector<Method *> &Interface::userDefinedMethods() const {
562 return mUserMethods;
563}
564
565const std::vector<Method *> &Interface::hidlReservedMethods() const {
566 return mReservedMethods;
567}
568
569std::vector<Method *> Interface::methods() const {
570 std::vector<Method *> v(mUserMethods);
571 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
572 return v;
573}
574
575std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
576 std::vector<InterfaceAndMethod> v;
577 std::vector<const Interface *> chain = typeChain();
578 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
579 const Interface *iface = *it;
580 for (Method *userMethod : iface->userDefinedMethods()) {
581 v.push_back(InterfaceAndMethod(iface, userMethod));
582 }
583 }
584 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800585 v.push_back(InterfaceAndMethod(
586 *chain.rbegin(), // IBase
587 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700588 }
589 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700590}
591
Steven Moreland14ee6742016-10-18 12:58:28 -0700592Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700593 for (const auto &tuple : allMethodsFromRoot()) {
594 Method *method = tuple.method();
595 if (method->name() == name) {
596 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700597 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700598 }
599
600 return nullptr;
601}
602
Steven Moreland40786312016-08-16 10:29:40 -0700603std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700604 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700605}
606
Yifan Hongeefe4f22017-01-04 15:32:42 -0800607std::string Interface::getProxyName() const {
608 return fqName().getInterfaceProxyName();
609}
610
611std::string Interface::getStubName() const {
612 return fqName().getInterfaceStubName();
613}
614
615std::string Interface::getHwName() const {
616 return fqName().getInterfaceHwName();
617}
618
619std::string Interface::getPassthroughName() const {
620 return fqName().getInterfacePassthroughName();
621}
622
Yifan Hong51a65092017-01-04 15:41:44 -0800623FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800624 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800625}
626
Yifan Hong51a65092017-01-04 15:41:44 -0800627FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800628 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800629}
630
Yifan Hong51a65092017-01-04 15:41:44 -0800631FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800632 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800633}
634
Steven Moreland979e0992016-09-07 09:18:08 -0700635std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700636 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700637 const std::string base =
638 std::string(specifyNamespaces ? "::android::" : "")
639 + "sp<"
640 + (specifyNamespaces ? fullName() : partialCppName())
641 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700642
643 switch (mode) {
644 case StorageMode_Stack:
645 case StorageMode_Result:
646 return base;
647
648 case StorageMode_Argument:
649 return "const " + base + "&";
650 }
651}
652
Yifan Hong4ed13472016-11-02 10:44:11 -0700653std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700654 return fullJavaName();
655}
656
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800657std::string Interface::getVtsType() const {
658 if (StringHelper::EndsWith(localName(), "Callback")) {
659 return "TYPE_HIDL_CALLBACK";
660 } else {
661 return "TYPE_HIDL_INTERFACE";
662 }
663}
664
Andreas Huber881227d2016-08-02 14:20:21 -0700665void Interface::emitReaderWriter(
666 Formatter &out,
667 const std::string &name,
668 const std::string &parcelObj,
669 bool parcelObjIsPointer,
670 bool isReader,
671 ErrorMode mode) const {
672 const std::string parcelObjDeref =
673 parcelObj + (parcelObjIsPointer ? "->" : ".");
674
675 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700676 out << "{\n";
677 out.indent();
678
Iliyan Malchev549e2592016-08-10 08:59:12 -0700679 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700680
Andreas Huber8a82ff72016-08-04 10:29:39 -0700681 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700682 << binderName << ";\n";
683
Iliyan Malchev549e2592016-08-10 08:59:12 -0700684 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700685 out << parcelObjDeref
686 << "readNullableStrongBinder(&"
687 << binderName
688 << ");\n";
689
690 handleError(out, mode);
691
692 out << name
693 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100694 << "::android::hardware::fromBinder<"
695 << fqName().cppName()
696 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800697 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100698 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800699 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100700 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700701 << binderName
702 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700703
704 out.unindent();
705 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700706 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200707 out << "if (" << name << " == nullptr) {\n";
708 out.indent();
709 out << "_hidl_err = ";
710 out << parcelObjDeref
711 << "writeStrongBinder(nullptr);\n";
712 out.unindent();
713 out << "} else {\n";
714 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800715 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
716 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800717 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100718 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800719 << ", "
Yifan Hong51a65092017-01-04 15:41:44 -0800720 << getProxyFqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800721 << ">("
722 << name
723 << ");\n";
724 });
725 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800726 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800727 out << "_hidl_err = "
728 << parcelObjDeref
729 << "writeStrongBinder(_hidl_binder);\n";
730 });
731 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800732 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800733 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
734 });
735 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200736 out.unindent();
737 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700738
Andreas Huber881227d2016-08-02 14:20:21 -0700739 handleError(out, mode);
740 }
741}
742
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800743status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const {
744 status_t status = Scope::emitGlobalTypeDeclarations(out);
745 if (status != OK) {
746 return status;
747 }
748 out << "std::string toString("
749 << getCppArgumentType()
750 << ");\n";
751 return OK;
752}
753
754
755status_t Interface::emitTypeDefinitions(
756 Formatter &out, const std::string prefix) const {
757 std::string space = prefix.empty() ? "" : (prefix + "::");
758 status_t err = Scope::emitTypeDefinitions(out, space + localName());
759 if (err != OK) {
760 return err;
761 }
762
763 out << "std::string toString("
764 << getCppArgumentType()
765 << " o) ";
766
767 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800768 out << "std::string os;\n";
769 out << "auto ret = o->interfaceDescriptor([&os] (const auto &name) ";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800770 out.block([&] {
Yifan Hongfbcdc802017-02-22 18:12:48 -0800771 out << "os += name.c_str();\n";
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800772 });
773 out << ");\n";
Yifan Hongfbcdc802017-02-22 18:12:48 -0800774 out.sIf("!ret.isOk()", [&] {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800775 out << "os += \"[class or subclass of \";\n"
776 << "os += " << fullName() << "::descriptor;\n"
777 << "os += \"]\";\n";
778 }).endl();
779 out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
780 << "return os;\n";
781 }).endl().endl();
782
783 return OK;
784}
785
Andreas Huber2831d512016-08-15 09:33:47 -0700786void Interface::emitJavaReaderWriter(
787 Formatter &out,
788 const std::string &parcelObj,
789 const std::string &argName,
790 bool isReader) const {
791 if (isReader) {
792 out << fullJavaName()
793 << ".asInterface("
794 << parcelObj
795 << ".readStrongBinder());\n";
796 } else {
797 out << parcelObj
798 << ".writeStrongBinder("
799 << argName
800 << " == null ? null : "
801 << argName
802 << ".asBinder());\n";
803 }
804}
805
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700806status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
807 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700808 // Skip for TypeDef as it is just an alias of a defined type.
809 if (type->isTypeDef()) {
810 continue;
811 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700812 out << "attribute: {\n";
813 out.indent();
814 status_t status = type->emitVtsTypeDeclarations(out);
815 if (status != OK) {
816 return status;
817 }
818 out.unindent();
819 out << "}\n\n";
820 }
821 return OK;
822}
823
824status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700825 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800826 if (method->isHidlReserved()) {
827 continue;
828 }
829
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700830 out << "api: {\n";
831 out.indent();
832 out << "name: \"" << method->name() << "\"\n";
833 // Generate declaration for each return value.
834 for (const auto &result : method->results()) {
835 out << "return_type_hidl: {\n";
836 out.indent();
837 status_t status = result->type().emitVtsAttributeType(out);
838 if (status != OK) {
839 return status;
840 }
841 out.unindent();
842 out << "}\n";
843 }
844 // Generate declaration for each input argument
845 for (const auto &arg : method->args()) {
846 out << "arg: {\n";
847 out.indent();
848 status_t status = arg->type().emitVtsAttributeType(out);
849 if (status != OK) {
850 return status;
851 }
852 out.unindent();
853 out << "}\n";
854 }
855 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700856 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700857 out << "callflow: {\n";
858 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700859 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700860 if (name == "entry") {
861 out << "entry: true\n";
862 } else if (name == "exit") {
863 out << "exit: true\n";
864 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700865 const AnnotationParam *param =
866 annotation->getParam("next");
867 if (param != nullptr) {
868 for (auto value : *param->getValues()) {
869 out << "next: " << value << "\n";
870 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700871 }
872 } else {
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800873 std::cerr << "Unrecognized annotation '"
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700874 << name << "' for method: " << method->name()
Keun Soo Yima5a57e92017-02-04 11:32:06 -0800875 << ". A VTS annotation should be one of: "
876 << "entry, exit, callflow. \n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700877 }
878 out.unindent();
879 out << "}\n";
880 }
881 out.unindent();
882 out << "}\n\n";
883 }
884 return OK;
885}
886
887status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800888 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700889 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800890 << fullName()
891 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700892 return OK;
893}
894
Steven Moreland69e7c702016-09-09 11:16:32 -0700895bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700896 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700897 if (method->isOneway()) {
898 return true;
899 }
900 }
901
902 const Interface* superClass = superType();
903
904 if (superClass != nullptr) {
905 return superClass->hasOnewayMethods();
906 }
907
908 return false;
909}
910
Andreas Huber70a59e12016-08-16 12:57:01 -0700911bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700912 if (mIsJavaCompatibleInProgress) {
913 // We're currently trying to determine if this Interface is
914 // java-compatible and something is referencing this interface through
915 // one of its methods. Assume we'll ultimately succeed, if we were wrong
916 // the original invocation of Interface::isJavaCompatible() will then
917 // return the correct "false" result.
918 return true;
919 }
920
Andreas Huber0fa9e392016-08-31 09:05:44 -0700921 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
922 mIsJavaCompatibleInProgress = false;
923 return false;
924 }
925
Andreas Huberea081b32016-08-17 15:57:47 -0700926 mIsJavaCompatibleInProgress = true;
927
Andreas Huber70a59e12016-08-16 12:57:01 -0700928 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700929 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700930 return false;
931 }
932
Yifan Hong10fe0b52016-10-19 14:20:17 -0700933 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700934 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700935 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700936 return false;
937 }
938 }
939
Andreas Huberea081b32016-08-17 15:57:47 -0700940 mIsJavaCompatibleInProgress = false;
941
Andreas Huber70a59e12016-08-16 12:57:01 -0700942 return true;
943}
944
Andreas Huberc9410c72016-07-28 12:18:40 -0700945} // namespace android
946