blob: a2a4e446b960deef39a64adcce4959a23a5d9b9e [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 Huber881227d2016-08-02 14:20:21 -070017#include "AST.h"
18
19#include "Coordinator.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070020#include "EnumType.h"
Andreas Huber6755e9d2017-04-06 11:09:07 -070021#include "HidlTypeAssertion.h"
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070022#include "Interface.h"
Neel Mehta291d02e2019-06-06 17:51:07 -070023#include "Location.h"
Andreas Huber881227d2016-08-02 14:20:21 -070024#include "Method.h"
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070025#include "Reference.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070026#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070027#include "Scope.h"
28
Andreas Huberdca261f2016-08-04 13:47:51 -070029#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070030#include <hidl-util/Formatter.h>
Steven Moreland5708edf2016-11-04 15:33:31 +000031#include <hidl-util/StringHelper.h>
Andreas Huber881227d2016-08-02 14:20:21 -070032#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070033#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070034#include <vector>
35
36namespace android {
37
Andreas Huber737080b2016-08-02 15:38:04 -070038void AST::getPackageComponents(
39 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070040 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070041}
42
43void AST::getPackageAndVersionComponents(
44 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070045 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070046}
47
Steven Moreland5708edf2016-11-04 15:33:31 +000048std::string AST::makeHeaderGuard(const std::string &baseName,
49 bool indicateGenerated) const {
50 std::string guard;
Andreas Huber881227d2016-08-02 14:20:21 -070051
Steven Moreland5708edf2016-11-04 15:33:31 +000052 if (indicateGenerated) {
53 guard += "HIDL_GENERATED_";
54 }
55
56 guard += StringHelper::Uppercase(mPackage.tokenName());
Andreas Huber881227d2016-08-02 14:20:21 -070057 guard += "_";
Steven Moreland5708edf2016-11-04 15:33:31 +000058 guard += StringHelper::Uppercase(baseName);
59 guard += "_H";
Andreas Huber881227d2016-08-02 14:20:21 -070060
61 return guard;
62}
63
Steven Morelandee88eed2016-10-31 17:49:00 -070064void AST::generateCppPackageInclude(
65 Formatter &out,
66 const FQName &package,
67 const std::string &klass) {
68
69 out << "#include <";
70
71 std::vector<std::string> components;
72 package.getPackageAndVersionComponents(&components, false /* cpp_compatible */);
73
74 for (const auto &component : components) {
75 out << component << "/";
76 }
77
78 out << klass
79 << ".h>\n";
80}
81
Andreas Huber881227d2016-08-02 14:20:21 -070082void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
83 std::vector<std::string> packageComponents;
84 getPackageAndVersionComponents(
85 &packageComponents, true /* cpp_compatible */);
86
87 if (enter) {
88 for (const auto &component : packageComponents) {
89 out << "namespace " << component << " {\n";
90 }
91 } else {
92 for (auto it = packageComponents.rbegin();
93 it != packageComponents.rend();
94 ++it) {
95 out << "} // namespace " << *it << "\n";
96 }
97 }
98}
99
Steven Moreland038903b2017-03-30 12:11:24 -0700100static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
101 const std::string functionName = isTry ? "tryGetService" : "getService";
102
Steven Moreland7645fbd2019-03-12 18:49:28 -0700103 if (isTry) {
104 DocComment(
105 "This gets the service of this type with the specified instance name. If the\n"
106 "service is currently not available or not in the VINTF manifest on a Trebilized\n"
107 "device, this will return nullptr. This is useful when you don't want to block\n"
108 "during device boot. If getStub is true, this will try to return an unwrapped\n"
109 "passthrough implementation in the same process. This is useful when getting an\n"
110 "implementation from the same partition/compilation group.\n\n"
Neel Mehta291d02e2019-06-06 17:51:07 -0700111 "In general, prefer getService(std::string,bool)",
112 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700113 .emit(out);
114 } else {
115 DocComment(
116 "This gets the service of this type with the specified instance name. If the\n"
117 "service is not in the VINTF manifest on a Trebilized device, this will return\n"
118 "nullptr. If the service is not available, this will wait for the service to\n"
119 "become available. If the service is a lazy service, this will start the service\n"
120 "and return when it becomes available. If getStub is true, this will try to\n"
121 "return an unwrapped passthrough implementation in the same process. This is\n"
Neel Mehta291d02e2019-06-06 17:51:07 -0700122 "useful when getting an implementation from the same partition/compilation group.",
123 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700124 .emit(out);
125 }
Steven Moreland038903b2017-03-30 12:11:24 -0700126 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800127 << "const std::string &serviceName=\"default\", bool getStub=false);\n";
Neel Mehta291d02e2019-06-06 17:51:07 -0700128 DocComment("Deprecated. See " + functionName + "(std::string, bool)", HIDL_LOCATION_HERE)
129 .emit(out);
Steven Moreland038903b2017-03-30 12:11:24 -0700130 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800131 << "const char serviceName[], bool getStub=false)"
132 << " { std::string str(serviceName ? serviceName : \"\");"
Steven Moreland038903b2017-03-30 12:11:24 -0700133 << " return " << functionName << "(str, getStub); }\n";
Neel Mehta291d02e2019-06-06 17:51:07 -0700134 DocComment("Deprecated. See " + functionName + "(std::string, bool)", HIDL_LOCATION_HERE)
135 .emit(out);
Steven Moreland038903b2017-03-30 12:11:24 -0700136 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800137 << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
138 // without c_str the std::string constructor is ambiguous
139 << " { std::string str(serviceName.c_str());"
Steven Moreland038903b2017-03-30 12:11:24 -0700140 << " return " << functionName << "(str, getStub); }\n";
Steven Moreland7645fbd2019-03-12 18:49:28 -0700141 DocComment("Calls " + functionName +
Neel Mehta291d02e2019-06-06 17:51:07 -0700142 "(\"default\", bool). This is the recommended instance name for singleton "
143 "services.",
144 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700145 .emit(out);
Steven Moreland038903b2017-03-30 12:11:24 -0700146 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
147 << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
148}
149
150static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
151 declareGetService(out, interfaceName, true /* isTry */);
152 declareGetService(out, interfaceName, false /* isTry */);
153
Steven Moreland7645fbd2019-03-12 18:49:28 -0700154 DocComment(
155 "Registers a service with the service manager. For Trebilized devices, the service\n"
Neel Mehta291d02e2019-06-06 17:51:07 -0700156 "must also be in the VINTF manifest.",
157 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700158 .emit(out);
Steven Moreland90831502017-03-27 12:08:40 -0700159 out << "__attribute__ ((warn_unused_result))"
160 << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Neel Mehta291d02e2019-06-06 17:51:07 -0700161 DocComment("Registers for notifications for when a service is registered.", HIDL_LOCATION_HERE)
162 .emit(out);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800163 out << "static bool registerForNotifications(\n";
164 out.indent(2, [&] {
165 out << "const std::string &serviceName,\n"
166 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
167 << "&notification);\n";
168 });
169
170}
171
Steven Moreland038903b2017-03-30 12:11:24 -0700172static void implementGetService(Formatter &out,
173 const FQName &fqName,
174 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800175
176 const std::string interfaceName = fqName.getInterfaceName();
Steven Moreland038903b2017-03-30 12:11:24 -0700177 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800178
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700179 out << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000180 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800181 out.block([&] {
Steven Moreland78f95f92017-10-06 17:07:40 -0700182 out << "return ::android::hardware::details::getServiceInternal<"
183 << fqName.getInterfaceProxyName()
184 << ">(serviceName, "
185 << (!isTry ? "true" : "false") // retry
186 << ", getStub);\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800187 }).endl().endl();
Steven Moreland038903b2017-03-30 12:11:24 -0700188}
189
190static void implementServiceManagerInteractions(Formatter &out,
191 const FQName &fqName, const std::string &package) {
192
193 const std::string interfaceName = fqName.getInterfaceName();
194
195 implementGetService(out, fqName, true /* isTry */);
196 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800197
Yifan Hongeefe4f22017-01-04 15:32:42 -0800198 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800199 << "const std::string &serviceName) ";
200 out.block([&] {
Steven Moreland5f84b382018-10-11 12:10:35 -0700201 out << "return ::android::hardware::details::registerAsServiceInternal(this, serviceName);\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800202 }).endl().endl();
203
Yifan Hongeefe4f22017-01-04 15:32:42 -0800204 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800205 out.indent(2, [&] {
206 out << "const std::string &serviceName,\n"
207 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
208 << "&notification) ";
209 });
210 out.block([&] {
211 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
212 out.indent(2, [&] {
213 out << "= ::android::hardware::defaultServiceManager();\n";
214 });
215 out.sIf("sm == nullptr", [&] {
216 out << "return false;\n";
217 }).endl();
218 out << "::android::hardware::Return<bool> success =\n";
219 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800220 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800221 out.indent(2, [&] {
222 out << "serviceName, notification);\n";
223 });
224 });
225 out << "return success.isOk() && success;\n";
226 }).endl().endl();
227}
228
Steven Moreland368e4602018-02-16 14:21:49 -0800229void AST::generateInterfaceHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700230 const Interface *iface = getInterface();
231 std::string ifaceName = iface ? iface->localName() : "types";
Andreas Huber881227d2016-08-02 14:20:21 -0700232 const std::string guard = makeHeaderGuard(ifaceName);
233
234 out << "#ifndef " << guard << "\n";
235 out << "#define " << guard << "\n\n";
236
Andreas Huber737080b2016-08-02 15:38:04 -0700237 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700238 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700239 }
240
241 if (!mImportedNames.empty()) {
242 out << "\n";
243 }
244
Steven Moreland19f11b52017-05-12 18:22:21 -0700245 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800246 if (isIBase()) {
247 out << "// skipped #include IServiceNotification.h\n\n";
248 } else {
249 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
250 }
Steven Moreland0693f312016-11-09 15:06:14 -0800251 }
252
Yifan Hongc8934042016-11-17 17:10:52 -0800253 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700254 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700255
Steven Moreland19f11b52017-05-12 18:22:21 -0700256 if (iface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200257 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700258 }
259
Martijn Coenenaf712c02016-11-16 15:26:27 +0100260 out << "#include <utils/NativeHandle.h>\n";
261 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700262
263 enterLeaveNamespace(out, true /* enter */);
264 out << "\n";
265
Steven Moreland19f11b52017-05-12 18:22:21 -0700266 if (iface) {
Steven Moreland70cb55e2019-03-12 17:20:54 -0700267 iface->emitDocComment(out);
268
Andreas Huber881227d2016-08-02 14:20:21 -0700269 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700270 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700271
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700272 const Interface *superType = iface->superType();
273
Yi Kong56758da2018-07-24 16:21:37 -0700274 if (superType == nullptr) {
Yifan Hongc8934042016-11-17 17:10:52 -0800275 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700276 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000277 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700278 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700279 }
280
281 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700282
283 out.indent();
284
Neel Mehta291d02e2019-06-06 17:51:07 -0700285 DocComment("Type tag for use in template logic that indicates this is a 'pure' class.",
286 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700287 .emit(out);
Steven Moreland1a52e822017-07-27 13:56:29 -0700288 generateCppTag(out, "android::hardware::details::i_tag");
Andreas Huber881227d2016-08-02 14:20:21 -0700289
Neel Mehta291d02e2019-06-06 17:51:07 -0700290 DocComment("Fully qualified interface name: \"" + iface->fqName().string() + "\"",
291 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700292 .emit(out);
293 out << "static const char* descriptor;\n\n";
294
Steven Moreland70cb55e2019-03-12 17:20:54 -0700295 iface->emitTypeDeclarations(out);
296 } else {
297 mRootScope.emitTypeDeclarations(out);
298 }
Andreas Huber881227d2016-08-02 14:20:21 -0700299
Steven Moreland19f11b52017-05-12 18:22:21 -0700300 if (iface) {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700301 DocComment(
Neel Mehta291d02e2019-06-06 17:51:07 -0700302 "Returns whether this object's implementation is outside of the current process.",
303 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700304 .emit(out);
Yifan Hongc8934042016-11-17 17:10:52 -0800305 out << "virtual bool isRemote() const ";
306 if (!isIBase()) {
307 out << "override ";
308 }
Steven Moreland7645fbd2019-03-12 18:49:28 -0700309 out << "{ return false; }\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800310
Steven Morelandf7f2a9a2017-07-21 18:05:38 -0700311 for (const auto& tuple : iface->allMethodsFromRoot()) {
312 const Method* method = tuple.method();
313
Andreas Huber881227d2016-08-02 14:20:21 -0700314 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700315
Andreas Huber881227d2016-08-02 14:20:21 -0700316 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700317 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Morelandd732ea12016-11-08 17:12:06 -0800318
319 if (elidedReturn == nullptr && returnsValue) {
Neel Mehta291d02e2019-06-06 17:51:07 -0700320 DocComment("Return callback for " + method->name(), HIDL_LOCATION_HERE).emit(out);
Steven Morelandd732ea12016-11-08 17:12:06 -0800321 out << "using "
322 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700323 << "_cb = std::function<void(";
324 method->emitCppResultSignature(out, true /* specify namespaces */);
325 out << ")>;\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800326 }
Andreas Huber881227d2016-08-02 14:20:21 -0700327
Steven Moreland49bad8d2018-05-17 15:45:26 -0700328 method->emitDocComment(out);
329
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700330 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700331 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700332 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700333 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700334 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700335 }
336
337 out << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700338 << "(";
339 method->emitCppArgSignature(out, true /* specify namespaces */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700340 out << ")";
341 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800342 if (!isIBase()) {
343 out << " override";
344 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700345 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700346 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700347 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700348 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700349 }
Steven Moreland40786312016-08-16 10:29:40 -0700350
Steven Moreland7645fbd2019-03-12 18:49:28 -0700351 out << "\n// cast static functions\n";
Yifan Hong3d746092016-12-07 14:26:33 -0800352 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700353
Yifan Hong3d746092016-12-07 14:26:33 -0800354 for (const Interface *superType : iface->typeChain()) {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700355 DocComment(
356 "This performs a checked cast based on what the underlying implementation "
Neel Mehta291d02e2019-06-06 17:51:07 -0700357 "actually is.",
358 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700359 .emit(out);
Yifan Hong200209c2017-03-29 03:39:09 -0700360 out << "static ::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -0800361 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -0700362 << "> castFrom("
Yifan Hong3d746092016-12-07 14:26:33 -0800363 << superType->getCppArgumentType()
364 << " parent"
Yifan Hong200209c2017-03-29 03:39:09 -0700365 << ", bool emitError = false);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700366 }
367
Yifan Hongc8934042016-11-17 17:10:52 -0800368 if (isIBase()) {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700369 out << "\n// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800370 } else {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700371 out << "\n// helper methods for interactions with the hwservicemanager\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800372 declareServiceManagerInteractions(out, iface->localName());
Yifan Hongc8934042016-11-17 17:10:52 -0800373 }
Andreas Huber881227d2016-08-02 14:20:21 -0700374 }
375
Steven Moreland19f11b52017-05-12 18:22:21 -0700376 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700377 out.unindent();
378
Andreas Hubere3f769a2016-10-10 10:54:44 -0700379 out << "};\n\n";
380 }
381
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700382 out << "//\n";
383 out << "// type declarations for package\n";
384 out << "//\n\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800385 mRootScope.emitPackageTypeDeclarations(out);
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700386 out << "//\n";
387 out << "// type header definitions for package\n";
388 out << "//\n\n";
389 mRootScope.emitPackageTypeHeaderDefinitions(out);
Andreas Huber881227d2016-08-02 14:20:21 -0700390
391 out << "\n";
392 enterLeaveNamespace(out, false /* enter */);
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700393 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700394
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700395 out << "//\n";
396 out << "// global type declarations for package\n";
397 out << "//\n\n";
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800398 mRootScope.emitGlobalTypeDeclarations(out);
399
Andreas Huber881227d2016-08-02 14:20:21 -0700400 out << "\n#endif // " << guard << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700401}
402
Steven Moreland368e4602018-02-16 14:21:49 -0800403void AST::generateHwBinderHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700404 const Interface *iface = getInterface();
405 std::string klassName = iface ? iface->getHwName() : "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700406
Steven Moreland40786312016-08-16 10:29:40 -0700407 const std::string guard = makeHeaderGuard(klassName);
408
409 out << "#ifndef " << guard << "\n";
410 out << "#define " << guard << "\n\n";
411
Steven Moreland19f11b52017-05-12 18:22:21 -0700412 generateCppPackageInclude(out, mPackage, iface ? iface->localName() : "types");
Steven Moreland40786312016-08-16 10:29:40 -0700413
Steven Morelandee88eed2016-10-31 17:49:00 -0700414 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700415
416 for (const auto &item : mImportedNames) {
417 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800418 generateCppPackageInclude(out, item, "hwtypes");
419 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800420 generateCppPackageInclude(out, item, item.getInterfaceStubName());
421 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700422 }
Steven Moreland40786312016-08-16 10:29:40 -0700423 }
424
425 out << "\n";
426
Martijn Coenen93915102016-09-01 01:35:52 +0200427 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700428 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100429 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700430
431 out << "\n";
432
433 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700434
Steven Moreland368e4602018-02-16 14:21:49 -0800435 mRootScope.emitPackageHwDeclarations(out);
Steven Moreland40786312016-08-16 10:29:40 -0700436
437 enterLeaveNamespace(out, false /* enter */);
438
439 out << "\n#endif // " << guard << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700440}
441
Steven Moreland58a20c72018-10-09 12:30:51 -0700442static std::string wrapPassthroughArg(Formatter& out, const NamedReference<Type>* arg,
443 std::string name, std::function<void(void)> handleError) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800444 if (!arg->type().isInterface()) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700445 return name;
Yifan Hong7a118f52016-12-07 11:21:15 -0800446 }
Steven Moreland58a20c72018-10-09 12:30:51 -0700447 std::string wrappedName = "_hidl_wrapped_" + name;
Yifan Hong7a118f52016-12-07 11:21:15 -0800448 const Interface &iface = static_cast<const Interface &>(arg->type());
449 out << iface.getCppStackType() << " " << wrappedName << ";\n";
450 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
451 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
452 out << wrappedName
453 << " = "
Steven Morelandbff4bd22017-10-02 14:46:06 -0700454 << "::android::hardware::details::wrapPassthrough("
455 << name
456 << ");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800457 out.sIf(wrappedName + " == nullptr", [&] {
458 // Fatal error. Happens when the BsFoo class is not found in the binary
459 // or any dynamic libraries.
460 handleError();
461 }).endl();
462 }).sElse([&] {
463 out << wrappedName << " = " << name << ";\n";
464 }).endl().endl();
Steven Moreland58a20c72018-10-09 12:30:51 -0700465
466 return wrappedName;
Yifan Hong7a118f52016-12-07 11:21:15 -0800467}
468
Steven Moreland616cf4d2018-10-02 13:52:18 -0700469void AST::generatePassthroughMethod(Formatter& out, const Method* method, const Interface* superInterface) const {
Yifan Hong068c5522016-10-31 14:07:25 -0700470 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700471
Steven Moreland58a20c72018-10-09 12:30:51 -0700472 out << " override {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700473 out.indent();
474
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800475 if (method->isHidlReserved()
476 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
477 method->cppImpl(IMPL_PASSTHROUGH, out);
478 out.unindent();
479 out << "}\n\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800480 return;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800481 }
482
Steven Moreland69e7c702016-09-09 11:16:32 -0700483 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700484 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland69e7c702016-09-09 11:16:32 -0700485
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700486 generateCppInstrumentationCall(
487 out,
488 InstrumentationEvent::PASSTHROUGH_ENTRY,
Steven Moreland616cf4d2018-10-02 13:52:18 -0700489 method,
490 superInterface);
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700491
Steven Moreland58a20c72018-10-09 12:30:51 -0700492 std::vector<std::string> wrappedArgNames;
Yifan Hong7a118f52016-12-07 11:21:15 -0800493 for (const auto &arg : method->args()) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700494 std::string name = wrapPassthroughArg(out, arg, arg->name(), [&] {
Yifan Hong7a118f52016-12-07 11:21:15 -0800495 out << "return ::android::hardware::Status::fromExceptionCode(\n";
496 out.indent(2, [&] {
497 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800498 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800499 });
500 });
Steven Moreland58a20c72018-10-09 12:30:51 -0700501
502 wrappedArgNames.push_back(name);
Yifan Hong7a118f52016-12-07 11:21:15 -0800503 }
504
Steven Moreland58a20c72018-10-09 12:30:51 -0700505 out << "::android::hardware::Status _hidl_error = ::android::hardware::Status::ok();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700506 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700507
508 if (method->isOneway()) {
Steven Moreland836cb312017-06-05 17:25:55 -0700509 out << "addOnewayTask([mImpl = this->mImpl\n"
510 << "#ifdef __ANDROID_DEBUGGABLE__\n"
511 ", mEnableInstrumentation = this->mEnableInstrumentation, "
512 "mInstrumentationCallbacks = this->mInstrumentationCallbacks\n"
513 << "#endif // __ANDROID_DEBUGGABLE__\n";
Steven Moreland58a20c72018-10-09 12:30:51 -0700514 for (const std::string& arg : wrappedArgNames) {
515 out << ", " << arg;
Steven Moreland69e7c702016-09-09 11:16:32 -0700516 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700517 out << "] {\n";
518 out.indent();
Steven Moreland69e7c702016-09-09 11:16:32 -0700519 }
520
521 out << "mImpl->"
522 << method->name()
523 << "(";
524
Yifan Hong932464e2017-03-30 15:40:22 -0700525 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800526 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700527 });
Steven Moreland58a20c72018-10-09 12:30:51 -0700528
529 std::function<void(void)> kHandlePassthroughError = [&] {
530 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
531 out.indent(2, [&] {
532 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
533 << "\"Cannot wrap passthrough interface.\");\n";
534 });
535 };
536
Steven Moreland69e7c702016-09-09 11:16:32 -0700537 if (returnsValue && elidedReturn == nullptr) {
Steven Moreland340c8822017-05-02 14:41:49 -0700538 // never true if oneway since oneway methods don't return values
539
Steven Moreland69e7c702016-09-09 11:16:32 -0700540 if (!method->args().empty()) {
541 out << ", ";
542 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800543 out << "[&](";
Yifan Hong932464e2017-03-30 15:40:22 -0700544 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800545 out << "const auto &_hidl_out_"
546 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700547 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800548
549 out << ") {\n";
550 out.indent();
Steven Moreland92a08a72017-07-31 14:57:37 -0700551 generateCppInstrumentationCall(
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800552 out,
553 InstrumentationEvent::PASSTHROUGH_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -0700554 method,
555 superInterface);
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800556
Steven Moreland58a20c72018-10-09 12:30:51 -0700557 std::vector<std::string> wrappedOutNames;
Yifan Hong7a118f52016-12-07 11:21:15 -0800558 for (const auto &arg : method->results()) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700559 wrappedOutNames.push_back(
560 wrapPassthroughArg(out, arg, "_hidl_out_" + arg->name(), kHandlePassthroughError));
Yifan Hong7a118f52016-12-07 11:21:15 -0800561 }
562
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800563 out << "_hidl_cb(";
Steven Moreland58a20c72018-10-09 12:30:51 -0700564 out.join(wrappedOutNames.begin(), wrappedOutNames.end(), ", ",
565 [&](const std::string& arg) { out << arg; });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800566 out << ");\n";
567 out.unindent();
568 out << "});\n\n";
569 } else {
570 out << ");\n\n";
Steven Moreland30b76e92017-06-02 18:52:24 -0700571
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800572 if (elidedReturn != nullptr) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700573 const std::string outName = "_hidl_out_" + elidedReturn->name();
574
575 out << elidedReturn->type().getCppResultType() << " " << outName
576 << " = _hidl_return;\n";
577 out << "(void) " << outName << ";\n";
578
579 const std::string wrappedName =
580 wrapPassthroughArg(out, elidedReturn, outName, kHandlePassthroughError);
581
582 if (outName != wrappedName) {
583 // update the original value since it is used by generateCppInstrumentationCall
584 out << outName << " = " << wrappedName << ";\n\n";
585
586 // update the value to be returned
587 out << "_hidl_return = " << outName << "\n;";
588 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800589 }
Steven Moreland92a08a72017-07-31 14:57:37 -0700590 generateCppInstrumentationCall(
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800591 out,
592 InstrumentationEvent::PASSTHROUGH_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -0700593 method,
594 superInterface);
Steven Moreland69e7c702016-09-09 11:16:32 -0700595 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700596
597 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700598 out.unindent();
599 out << "});\n";
Steven Moreland58a20c72018-10-09 12:30:51 -0700600 } else {
601 out << "if (!_hidl_error.isOk()) return _hidl_error;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700602 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700603
604 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700605
606 out.unindent();
607 out << "}\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700608}
609
Steven Moreland368e4602018-02-16 14:21:49 -0800610void AST::generateMethods(Formatter& out, const MethodGenerator& gen, bool includeParent) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700611 const Interface* iface = mRootScope.getInterface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700612
Yifan Hong10fe0b52016-10-19 14:20:17 -0700613 const Interface *prevIterface = nullptr;
614 for (const auto &tuple : iface->allMethodsFromRoot()) {
615 const Method *method = tuple.method();
616 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700617
Steven Morelandf16c5c02017-07-31 16:50:06 -0700618 if (!includeParent && superInterface != iface) {
619 continue;
620 }
621
Yifan Hong10fe0b52016-10-19 14:20:17 -0700622 if(prevIterface != superInterface) {
623 if (prevIterface != nullptr) {
624 out << "\n";
625 }
626 out << "// Methods from "
627 << superInterface->fullName()
628 << " follow.\n";
629 prevIterface = superInterface;
630 }
Steven Moreland368e4602018-02-16 14:21:49 -0800631 gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700632 }
633
Yifan Hong10fe0b52016-10-19 14:20:17 -0700634 out << "\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700635}
636
Steven Moreland0b843772017-06-23 16:33:38 -0700637void AST::generateTemplatizationLink(Formatter& out) const {
Neel Mehta291d02e2019-06-06 17:51:07 -0700638 DocComment("The pure class is what this class wraps.", HIDL_LOCATION_HERE).emit(out);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700639 out << "typedef " << mRootScope.getInterface()->localName() << " Pure;\n\n";
Steven Moreland0b843772017-06-23 16:33:38 -0700640}
641
Steven Moreland1a52e822017-07-27 13:56:29 -0700642void AST::generateCppTag(Formatter& out, const std::string& tag) const {
643 out << "typedef " << tag << " _hidl_tag;\n\n";
644}
645
Steven Moreland368e4602018-02-16 14:21:49 -0800646void AST::generateStubHeader(Formatter& out) const {
Steven Moreland5abcf012018-02-08 18:50:18 -0800647 CHECK(AST::isInterface());
Andreas Huber881227d2016-08-02 14:20:21 -0700648
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700649 const Interface* iface = mRootScope.getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800650 const std::string klassName = iface->getStubName();
Steven Moreland40786312016-08-16 10:29:40 -0700651 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700652
653 out << "#ifndef " << guard << "\n";
654 out << "#define " << guard << "\n\n";
655
Yifan Hongeefe4f22017-01-04 15:32:42 -0800656 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Moreland1a52e822017-07-27 13:56:29 -0700657
Steven Morelandee88eed2016-10-31 17:49:00 -0700658 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700659
660 enterLeaveNamespace(out, true /* enter */);
661 out << "\n";
662
663 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800664 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100665 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800666 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000667 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100668 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800669 out << " : public "
670 << gIBaseFqName.getInterfaceStubFqName().cppName()
671 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100672 }
Andreas Huber881227d2016-08-02 14:20:21 -0700673
674 out.indent();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800675 out << "explicit "
676 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700677 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100678 << "\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800679 out << "explicit "
680 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700681 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800682 << " const std::string& HidlInstrumentor_package,"
683 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700684 << "\n\n";
Steven Moreland57a89362017-07-21 19:29:54 +0000685 out << "virtual ~" << klassName << "();\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700686 out << "::android::status_t onTransact(\n";
687 out.indent();
688 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700689 out << "uint32_t _hidl_code,\n";
690 out << "const ::android::hardware::Parcel &_hidl_data,\n";
691 out << "::android::hardware::Parcel *_hidl_reply,\n";
692 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700693 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700694 out.unindent();
695 out.unindent();
696
Steven Moreland0b843772017-06-23 16:33:38 -0700697 out.endl();
698 generateTemplatizationLink(out);
Neel Mehta291d02e2019-06-06 17:51:07 -0700699 DocComment("Type tag for use in template logic that indicates this is a 'native' class.",
700 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700701 .emit(out);
Steven Moreland1a52e822017-07-27 13:56:29 -0700702 generateCppTag(out, "android::hardware::details::bnhw_tag");
Steven Moreland0b843772017-06-23 16:33:38 -0700703
Steven Morelandcbcf9f72017-11-20 10:04:15 -0800704 out << "::android::sp<" << iface->localName() << "> getImpl() { return _hidl_mImpl; }\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -0700705
Steven Moreland368e4602018-02-16 14:21:49 -0800706 generateMethods(out,
707 [&](const Method* method, const Interface*) {
708 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
709 return;
710 }
Steven Morelandf16c5c02017-07-31 16:50:06 -0700711
Steven Moreland368e4602018-02-16 14:21:49 -0800712 out << "static ::android::status_t _hidl_" << method->name() << "(\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -0700713
Steven Moreland368e4602018-02-16 14:21:49 -0800714 out.indent(2,
715 [&] {
716 out << "::android::hidl::base::V1_0::BnHwBase* _hidl_this,\n"
717 << "const ::android::hardware::Parcel &_hidl_data,\n"
718 << "::android::hardware::Parcel *_hidl_reply,\n"
719 << "TransactCallback _hidl_cb);\n";
720 })
721 .endl()
722 .endl();
723 },
724 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -0700725
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100726 out.unindent();
727 out << "private:\n";
728 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800729
Steven Moreland368e4602018-02-16 14:21:49 -0800730 generateMethods(out, [&](const Method* method, const Interface* iface) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800731 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
Steven Moreland368e4602018-02-16 14:21:49 -0800732 return;
Yifan Hongcd2ae452017-01-31 14:33:40 -0800733 }
734 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700735 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800736
737 if (elidedReturn == nullptr && returnsValue) {
738 out << "using " << method->name() << "_cb = "
739 << iface->fqName().cppName()
740 << "::" << method->name() << "_cb;\n";
741 }
742 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800743 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800744 });
Yifan Hongcd2ae452017-01-31 14:33:40 -0800745
Steven Moreland19f11b52017-05-12 18:22:21 -0700746 out << "::android::sp<" << iface->localName() << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700747 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700748 out << "};\n\n";
749
750 enterLeaveNamespace(out, false /* enter */);
751
752 out << "\n#endif // " << guard << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700753}
754
Steven Moreland368e4602018-02-16 14:21:49 -0800755void AST::generateProxyHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700756 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700757 // types.hal does not get a proxy header.
Steven Moreland368e4602018-02-16 14:21:49 -0800758 return;
Andreas Huber881227d2016-08-02 14:20:21 -0700759 }
760
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700761 const Interface* iface = mRootScope.getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800762 const std::string proxyName = iface->getProxyName();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800763 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700764
765 out << "#ifndef " << guard << "\n";
766 out << "#define " << guard << "\n\n";
767
Martijn Coenen115d4282016-12-19 05:14:04 +0100768 out << "#include <hidl/HidlTransportSupport.h>\n\n";
769
Andreas Huber881227d2016-08-02 14:20:21 -0700770 std::vector<std::string> packageComponents;
771 getPackageAndVersionComponents(
772 &packageComponents, false /* cpp_compatible */);
773
Yifan Hongeefe4f22017-01-04 15:32:42 -0800774 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700775 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700776
777 enterLeaveNamespace(out, true /* enter */);
778 out << "\n";
779
780 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800781 << proxyName
782 << " : public ::android::hardware::BpInterface<"
783 << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000784 << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700785
786 out.indent();
787
Yifan Hongeefe4f22017-01-04 15:32:42 -0800788 out << "explicit "
789 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700790 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700791 << "\n\n";
792
Steven Moreland0b843772017-06-23 16:33:38 -0700793 generateTemplatizationLink(out);
Neel Mehta291d02e2019-06-06 17:51:07 -0700794 DocComment("Type tag for use in template logic that indicates this is a 'proxy' class.",
795 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700796 .emit(out);
Steven Moreland1a52e822017-07-27 13:56:29 -0700797 generateCppTag(out, "android::hardware::details::bphw_tag");
Steven Moreland0b843772017-06-23 16:33:38 -0700798
Yifan Hong10fe0b52016-10-19 14:20:17 -0700799 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700800
Steven Moreland596d20e2019-06-07 11:52:21 -0700801 out << "void onLastStrongRef(const void* id) override;\n\n";
802
Steven Moreland368e4602018-02-16 14:21:49 -0800803 generateMethods(
804 out,
805 [&](const Method* method, const Interface*) {
806 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
807 return;
808 }
Steven Morelandf16c5c02017-07-31 16:50:06 -0700809
Steven Moreland368e4602018-02-16 14:21:49 -0800810 out << "static ";
811 method->generateCppReturnType(out);
812 out << " _hidl_" << method->name() << "("
813 << "::android::hardware::IInterface* _hidl_this, "
814 << "::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor";
Steven Morelandf16c5c02017-07-31 16:50:06 -0700815
Steven Moreland368e4602018-02-16 14:21:49 -0800816 if (!method->hasEmptyCppArgSignature()) {
817 out << ", ";
818 }
819 method->emitCppArgSignature(out);
820 out << ");\n";
821 },
822 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -0700823
Steven Moreland368e4602018-02-16 14:21:49 -0800824 generateMethods(out, [&](const Method* method, const Interface*) {
Yifan Hong068c5522016-10-31 14:07:25 -0700825 method->generateCppSignature(out);
826 out << " override;\n";
Yifan Hong068c5522016-10-31 14:07:25 -0700827 });
Steven Moreland9c387612016-09-07 09:54:26 -0700828
Andreas Huber881227d2016-08-02 14:20:21 -0700829 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100830 out << "private:\n";
831 out.indent();
832 out << "std::mutex _hidl_mMutex;\n"
833 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
834 << " _hidl_mDeathRecipients;\n";
835 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700836 out << "};\n\n";
837
838 enterLeaveNamespace(out, false /* enter */);
839
840 out << "\n#endif // " << guard << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700841}
842
Steven Moreland368e4602018-02-16 14:21:49 -0800843void AST::generateCppSource(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700844 std::string baseName = getBaseName();
845 const Interface *iface = getInterface();
Andreas Huber881227d2016-08-02 14:20:21 -0700846
Steven Morelanda885d252017-09-25 18:44:43 -0700847 const std::string klassName = baseName + (baseName == "types" ? "" : "All");
Andreas Huber881227d2016-08-02 14:20:21 -0700848
Steven Moreland623c0042017-01-13 14:42:29 -0800849 out << "#define LOG_TAG \""
850 << mPackage.string() << "::" << baseName
851 << "\"\n\n";
852
Steven Moreland5add34d2018-11-08 16:31:30 -0800853 out << "#include <log/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100854 out << "#include <cutils/trace.h>\n";
855 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Steven Moreland26896a92018-07-31 15:31:01 -0700856 out << "#include <hidl/Static.h>\n";
857 out << "#include <hwbinder/ProcessState.h>\n";
Steven Moreland4607ef52018-05-09 10:52:47 -0700858 out << "#include <utils/Trace.h>\n";
Steven Moreland19f11b52017-05-12 18:22:21 -0700859 if (iface) {
Steven Moreland19d5c172016-10-20 19:20:25 -0700860 // This is a no-op for IServiceManager itself.
861 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
862
Yifan Hongeefe4f22017-01-04 15:32:42 -0800863 generateCppPackageInclude(out, mPackage, iface->getProxyName());
864 generateCppPackageInclude(out, mPackage, iface->getStubName());
865 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -0700866
867 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700868 generateCppPackageInclude(out,
869 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -0800870 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -0700871 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -0800872
873 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700874 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -0700875 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -0800876 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -0700877 }
878
879 out << "\n";
880
881 enterLeaveNamespace(out, true /* enter */);
882 out << "\n";
883
Steven Moreland368e4602018-02-16 14:21:49 -0800884 generateTypeSource(out, iface ? iface->localName() : "");
Andreas Huber881227d2016-08-02 14:20:21 -0700885
Steven Moreland368e4602018-02-16 14:21:49 -0800886 if (iface) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700887 const Interface* iface = mRootScope.getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700888
889 // need to be put here, generateStubSource is using this.
Yifan Hongeefe4f22017-01-04 15:32:42 -0800890 out << "const char* "
891 << iface->localName()
Yifan Hong10fe0b52016-10-19 14:20:17 -0700892 << "::descriptor(\""
893 << iface->fqName().string()
894 << "\");\n\n";
Yifan Hong91977fd2017-11-09 16:07:37 -0800895 out << "__attribute__((constructor)) ";
Martijn Coenen8adcb652017-02-03 17:37:36 +0100896 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800897 out.indent([&] {
Yifan Hong91977fd2017-11-09 16:07:37 -0800898 out << "::android::hardware::details::getBnConstructorMap().set("
Yifan Hongeefe4f22017-01-04 15:32:42 -0800899 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -0800900 << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800901 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -0800902 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800903 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800904 out << "return new "
905 << iface->getStubName()
Yifan Hong341112d2017-04-20 18:12:05 -0700906 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -0800907 << iface->localName()
Yifan Hong158655a2016-11-08 12:34:07 -0800908 << " *>(iIntf));\n";
909 });
Yifan Hongb04de382017-02-06 15:31:52 -0800910 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800911 });
Yifan Hong91977fd2017-11-09 16:07:37 -0800912 out << "::android::hardware::details::getBsConstructorMap().set("
Yifan Hongeefe4f22017-01-04 15:32:42 -0800913 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -0800914 << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800915 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -0800916 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -0800917 << gIBaseFqName.cppName()
918 << "> {\n";
919 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800920 out << "return new "
921 << iface->getPassthroughName()
Yifan Hong341112d2017-04-20 18:12:05 -0700922 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -0800923 << iface->localName()
Yifan Hong7a118f52016-12-07 11:21:15 -0800924 << " *>(iIntf));\n";
925 });
Yifan Hongb04de382017-02-06 15:31:52 -0800926 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800927 });
Yifan Hong158655a2016-11-08 12:34:07 -0800928 });
Martijn Coenen8adcb652017-02-03 17:37:36 +0100929 out << "};\n\n";
930 out << "__attribute__((destructor))";
931 out << "static void static_destructor() {\n";
932 out.indent([&] {
Yifan Hong91977fd2017-11-09 16:07:37 -0800933 out << "::android::hardware::details::getBnConstructorMap().erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +0100934 << iface->localName()
935 << "::descriptor);\n";
Yifan Hong91977fd2017-11-09 16:07:37 -0800936 out << "::android::hardware::details::getBsConstructorMap().erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +0100937 << iface->localName()
938 << "::descriptor);\n";
939 });
940 out << "};\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800941
Steven Moreland368e4602018-02-16 14:21:49 -0800942 generateInterfaceSource(out);
943 generateProxySource(out, iface->fqName());
944 generateStubSource(out, iface);
945 generatePassthroughSource(out);
Steven Moreland9c387612016-09-07 09:54:26 -0700946
Yifan Hongc8934042016-11-17 17:10:52 -0800947 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800948 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800949 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800950 std::string package = iface->fqName().package()
951 + iface->fqName().atVersion();
952
Yifan Hongeefe4f22017-01-04 15:32:42 -0800953 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -0800954 }
Steven Moreland40786312016-08-16 10:29:40 -0700955 }
956
Andreas Huber6755e9d2017-04-06 11:09:07 -0700957 HidlTypeAssertion::EmitAll(out);
958 out << "\n";
959
Andreas Huber881227d2016-08-02 14:20:21 -0700960 enterLeaveNamespace(out, false /* enter */);
Andreas Huber881227d2016-08-02 14:20:21 -0700961}
962
Steven Moreland368e4602018-02-16 14:21:49 -0800963void AST::generateTypeSource(Formatter& out, const std::string& ifaceName) const {
964 mRootScope.emitTypeDefinitions(out, ifaceName);
Andreas Huber881227d2016-08-02 14:20:21 -0700965}
966
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700967void AST::declareCppReaderLocals(Formatter& out, const std::vector<NamedReference<Type>*>& args,
968 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700969 if (args.empty()) {
970 return;
971 }
972
973 for (const auto &arg : args) {
974 const Type &type = arg->type();
975
Yifan Hong3b320f82016-11-01 15:15:54 -0700976 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -0700977 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -0700978 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -0700979 << ";\n";
980 }
981
982 out << "\n";
983}
984
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700985void AST::emitCppReaderWriter(Formatter& out, const std::string& parcelObj, bool parcelObjIsPointer,
986 const NamedReference<Type>* arg, bool isReader, Type::ErrorMode mode,
987 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700988 const Type &type = arg->type();
989
Andreas Huber881227d2016-08-02 14:20:21 -0700990 type.emitReaderWriter(
991 out,
Andreas Huber5e44a292016-09-27 14:52:39 -0700992 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -0700993 parcelObj,
994 parcelObjIsPointer,
995 isReader,
996 mode);
997}
998
Steven Moreland368e4602018-02-16 14:21:49 -0800999void AST::generateProxyMethodSource(Formatter& out, const std::string& klassName,
1000 const Method* method, const Interface* superInterface) const {
Yifan Hong068c5522016-10-31 14:07:25 -07001001 method->generateCppSignature(out,
1002 klassName,
1003 true /* specify namespaces */);
1004
Martijn Coenen115d4282016-12-19 05:14:04 +01001005 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
Steven Morelandf16c5c02017-07-31 16:50:06 -07001006 out.block([&] {
1007 method->cppImpl(IMPL_PROXY, out);
1008 }).endl().endl();
Steven Moreland368e4602018-02-16 14:21:49 -08001009 return;
Martijn Coenen115d4282016-12-19 05:14:04 +01001010 }
1011
Steven Morelandf16c5c02017-07-31 16:50:06 -07001012 out.block([&] {
1013 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -07001014 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Morelandf16c5c02017-07-31 16:50:06 -07001015
1016 method->generateCppReturnType(out);
1017
1018 out << " _hidl_out = "
1019 << superInterface->fqName().cppNamespace()
1020 << "::"
1021 << superInterface->getProxyName()
1022 << "::_hidl_"
1023 << method->name()
1024 << "(this, this";
1025
1026 if (!method->hasEmptyCppArgSignature()) {
1027 out << ", ";
1028 }
1029
1030 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
1031 out << arg->name();
1032 });
1033
1034 if (returnsValue && elidedReturn == nullptr) {
1035 if (!method->args().empty()) {
1036 out << ", ";
1037 }
1038 out << "_hidl_cb";
1039 }
1040
1041 out << ");\n\n";
1042
1043 out << "return _hidl_out;\n";
1044 }).endl().endl();
Steven Morelandf16c5c02017-07-31 16:50:06 -07001045}
1046
Steven Moreland368e4602018-02-16 14:21:49 -08001047void AST::generateStaticProxyMethodSource(Formatter& out, const std::string& klassName,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001048 const Method* method, const Interface* superInterface) const {
Steven Morelandf16c5c02017-07-31 16:50:06 -07001049 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
Steven Moreland368e4602018-02-16 14:21:49 -08001050 return;
Steven Morelandf16c5c02017-07-31 16:50:06 -07001051 }
1052
1053 method->generateCppReturnType(out);
1054
1055 out << klassName
1056 << "::_hidl_"
1057 << method->name()
1058 << "("
1059 << "::android::hardware::IInterface *_hidl_this, "
1060 << "::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor";
1061
1062 if (!method->hasEmptyCppArgSignature()) {
1063 out << ", ";
1064 }
1065
1066 method->emitCppArgSignature(out);
1067 out << ") {\n";
1068
1069 out.indent();
1070
1071 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
1072 out << "bool mEnableInstrumentation = _hidl_this_instrumentor->isInstrumentationEnabled();\n";
1073 out << "const auto &mInstrumentationCallbacks = _hidl_this_instrumentor->getInstrumentationCallbacks();\n";
1074 out << "#else\n";
1075 out << "(void) _hidl_this_instrumentor;\n";
1076 out << "#endif // __ANDROID_DEBUGGABLE__\n";
1077
1078 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -07001079 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland48cc6042019-04-30 11:28:56 -07001080 const bool hasCallback = returnsValue && elidedReturn == nullptr;
1081
Steven Moreland92a08a72017-07-31 14:57:37 -07001082 generateCppInstrumentationCall(
Yifan Hong068c5522016-10-31 14:07:25 -07001083 out,
1084 InstrumentationEvent::CLIENT_API_ENTRY,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001085 method,
1086 superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001087
1088 out << "::android::hardware::Parcel _hidl_data;\n";
1089 out << "::android::hardware::Parcel _hidl_reply;\n";
1090 out << "::android::status_t _hidl_err;\n";
Steven Moreland48cc6042019-04-30 11:28:56 -07001091 out << "::android::status_t _hidl_transact_err;\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001092 out << "::android::hardware::Status _hidl_status;\n\n";
1093
Steven Moreland48cc6042019-04-30 11:28:56 -07001094 if (!hasCallback) {
1095 declareCppReaderLocals(
1096 out, method->results(), true /* forResults */);
1097 }
Yifan Hong068c5522016-10-31 14:07:25 -07001098
1099 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001100 out << klassName;
Yifan Hong068c5522016-10-31 14:07:25 -07001101 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001102 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1103
Martijn Coenenfff73352017-01-04 16:36:31 +01001104 bool hasInterfaceArgument = false;
Steven Moreland8249f0a2019-05-28 17:25:27 -07001105
Yifan Hong068c5522016-10-31 14:07:25 -07001106 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001107 if (arg->type().isInterface()) {
1108 hasInterfaceArgument = true;
1109 }
Yifan Hong068c5522016-10-31 14:07:25 -07001110 emitCppReaderWriter(
1111 out,
1112 "_hidl_data",
1113 false /* parcelObjIsPointer */,
1114 arg,
1115 false /* reader */,
1116 Type::ErrorMode_Goto,
1117 false /* addPrefixToName */);
1118 }
1119
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001120 if (hasInterfaceArgument) {
1121 // Start binder threadpool to handle incoming transactions
1122 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1123 }
Steven Moreland48cc6042019-04-30 11:28:56 -07001124 out << "_hidl_transact_err = ::android::hardware::IInterface::asBinder(_hidl_this)->transact("
Yifan Hong068c5522016-10-31 14:07:25 -07001125 << method->getSerialId()
1126 << " /* "
1127 << method->name()
1128 << " */, _hidl_data, &_hidl_reply";
1129
1130 if (method->isOneway()) {
Steven Moreland77943692018-08-09 12:53:42 -07001131 out << ", " << Interface::FLAG_ONE_WAY->cppValue();
Steven Moreland48cc6042019-04-30 11:28:56 -07001132 } else {
1133 out << ", 0";
Yifan Hong068c5522016-10-31 14:07:25 -07001134 }
Yifan Hong068c5522016-10-31 14:07:25 -07001135
Steven Moreland48cc6042019-04-30 11:28:56 -07001136 if (hasCallback) {
1137 out << ", [&] (::android::hardware::Parcel& _hidl_reply) {\n";
1138 out.indent();
1139 declareCppReaderLocals(
1140 out, method->results(), true /* forResults */);
1141 out.endl();
1142 } else {
1143 out << ");\n";
1144 out << "if (_hidl_transact_err != ::android::OK) \n";
1145 out.block([&] {
1146 out << "_hidl_err = _hidl_transact_err;\n";
1147 out << "goto _hidl_error;\n";
1148 }).endl().endl();
1149 }
Yifan Hong068c5522016-10-31 14:07:25 -07001150
1151 if (!method->isOneway()) {
Steven Moreland48cc6042019-04-30 11:28:56 -07001152 Type::ErrorMode errorMode = hasCallback ? Type::ErrorMode_ReturnNothing : Type::ErrorMode_Goto;
Yifan Hong068c5522016-10-31 14:07:25 -07001153
Steven Moreland48cc6042019-04-30 11:28:56 -07001154 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
1155 Type::handleError(out, errorMode);
1156
1157 if (hasCallback) {
1158 out << "if (!_hidl_status.isOk()) { return; }\n\n";
1159 } else {
1160 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1161 }
Yifan Hong068c5522016-10-31 14:07:25 -07001162
Yifan Hong068c5522016-10-31 14:07:25 -07001163 for (const auto &arg : method->results()) {
1164 emitCppReaderWriter(
1165 out,
1166 "_hidl_reply",
1167 false /* parcelObjIsPointer */,
1168 arg,
1169 true /* reader */,
Steven Moreland48cc6042019-04-30 11:28:56 -07001170 errorMode,
Yifan Hong068c5522016-10-31 14:07:25 -07001171 true /* addPrefixToName */);
1172 }
1173
Yifan Hong068c5522016-10-31 14:07:25 -07001174 if (returnsValue && elidedReturn == nullptr) {
1175 out << "_hidl_cb(";
1176
Yifan Hong932464e2017-03-30 15:40:22 -07001177 out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) {
Yifan Hong068c5522016-10-31 14:07:25 -07001178 if (arg->type().resultNeedsDeref()) {
1179 out << "*";
1180 }
1181 out << "_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001182 });
Yifan Hong068c5522016-10-31 14:07:25 -07001183
1184 out << ");\n\n";
1185 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001186 }
Steven Morelandf16c5c02017-07-31 16:50:06 -07001187
Steven Moreland92a08a72017-07-31 14:57:37 -07001188 generateCppInstrumentationCall(
Martijn Coenen7b295242016-11-04 16:52:56 +01001189 out,
1190 InstrumentationEvent::CLIENT_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001191 method,
1192 superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001193
Steven Moreland48cc6042019-04-30 11:28:56 -07001194 if (hasCallback) {
1195 out.unindent();
1196 out << "});\n";
1197 out << "if (_hidl_transact_err != ::android::OK) ";
1198 out.block([&] {
1199 out << "_hidl_err = _hidl_transact_err;\n";
1200 out << "goto _hidl_error;\n";
1201 }).endl().endl();
1202 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n";
1203 }
1204
Yifan Hong068c5522016-10-31 14:07:25 -07001205 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001206 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001207 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001208 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1209 } else {
Yifan Hong068c5522016-10-31 14:07:25 -07001210 out << "return ::android::hardware::Return<void>();\n\n";
1211 }
1212
1213 out.unindent();
1214 out << "_hidl_error:\n";
1215 out.indent();
1216 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1217 out << "return ::android::hardware::Return<";
1218 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001219 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001220 } else {
1221 out << "void";
1222 }
1223 out << ">(_hidl_status);\n";
1224
1225 out.unindent();
1226 out << "}\n\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001227}
1228
Steven Moreland368e4602018-02-16 14:21:49 -08001229void AST::generateProxySource(Formatter& out, const FQName& fqName) const {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001230 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001231
1232 out << klassName
1233 << "::"
1234 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001235 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001236
1237 out.indent();
1238 out.indent();
1239
1240 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001241 << "<"
1242 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001243 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001244 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001245 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001246 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001247 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001248 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001249
Andreas Huber881227d2016-08-02 14:20:21 -07001250 out.unindent();
1251 out.unindent();
1252 out << "}\n\n";
1253
Steven Moreland596d20e2019-06-07 11:52:21 -07001254 out << "void " << klassName << "::onLastStrongRef(const void* id) ";
1255 out.block([&] {
1256 out.block([&] {
1257 // if unlinkToDeath is not used, remove strong cycle between
1258 // this and hidl_binder_death_recipient
1259 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n";
1260 out << "_hidl_mDeathRecipients.clear();\n";
1261 }).endl().endl();
1262
1263 out << "BpInterface<" << fqName.getInterfaceName() << ">::onLastStrongRef(id);\n";
1264 }).endl();
1265
Steven Moreland368e4602018-02-16 14:21:49 -08001266 generateMethods(out,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001267 [&](const Method* method, const Interface* superInterface) {
1268 generateStaticProxyMethodSource(out, klassName, method, superInterface);
Steven Moreland368e4602018-02-16 14:21:49 -08001269 },
1270 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -07001271
Steven Moreland368e4602018-02-16 14:21:49 -08001272 generateMethods(out, [&](const Method* method, const Interface* superInterface) {
1273 generateProxyMethodSource(out, klassName, method, superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001274 });
Andreas Huber881227d2016-08-02 14:20:21 -07001275}
1276
Steven Moreland368e4602018-02-16 14:21:49 -08001277void AST::generateStubSource(Formatter& out, const Interface* iface) const {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001278 const std::string interfaceName = iface->localName();
1279 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001280
Steven Moreland40786312016-08-16 10:29:40 -07001281 out << klassName
1282 << "::"
1283 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001284 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001285
1286 out.indent();
1287 out.indent();
1288
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001289 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001290 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001291 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001292 out << ": "
1293 << gIBaseFqName.getInterfaceStubFqName().cppName()
1294 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001295 }
1296
1297 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001298 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001299 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001300 << "\") { \n";
1301 out.indent();
1302 out << "_hidl_mImpl = _hidl_impl;\n";
Steven Morelandbd984412019-04-22 10:25:46 -07001303 out << "auto prio = ::android::hardware::details::gServicePrioMap->get("
Martijn Coenenb4d77952017-05-03 13:44:29 -07001304 << "_hidl_impl, {SCHED_NORMAL, 0});\n";
1305 out << "mSchedPolicy = prio.sched_policy;\n";
1306 out << "mSchedPriority = prio.prio;\n";
Steven Morelandbd984412019-04-22 10:25:46 -07001307 out << "setRequestingSid(::android::hardware::details::gServiceSidMap->get(_hidl_impl, "
1308 "false));\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001309 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001310
1311 out.unindent();
1312 out.unindent();
1313 out << "}\n\n";
1314
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001315 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001316 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001317 // class properly.
1318 out << klassName
1319 << "::"
1320 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001321 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1322 << " const std::string &HidlInstrumentor_package,"
1323 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001324
1325 out.indent();
1326 out.indent();
1327
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001328 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001329 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001330 out.indent();
1331 out << "_hidl_mImpl = _hidl_impl;\n";
1332 out.unindent();
1333
1334 out.unindent();
1335 out.unindent();
1336 out << "}\n\n";
1337 }
1338
Steven Moreland57a89362017-07-21 19:29:54 +00001339 out << klassName << "::~" << klassName << "() ";
1340 out.block([&]() {
Steven Morelandbd984412019-04-22 10:25:46 -07001341 out << "::android::hardware::details::gBnMap->eraseIfEqual(_hidl_mImpl.get(), this);\n";
1342 })
1343 .endl()
1344 .endl();
Steven Moreland57a89362017-07-21 19:29:54 +00001345
Steven Moreland368e4602018-02-16 14:21:49 -08001346 generateMethods(out,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001347 [&](const Method* method, const Interface* superInterface) {
1348 return generateStaticStubMethodSource(out, iface->fqName(), method, superInterface);
Steven Moreland368e4602018-02-16 14:21:49 -08001349 },
1350 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -07001351
Steven Moreland368e4602018-02-16 14:21:49 -08001352 generateMethods(out, [&](const Method* method, const Interface*) {
Yifan Hongbcffce22017-02-01 15:52:06 -08001353 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
Steven Moreland368e4602018-02-16 14:21:49 -08001354 return;
Yifan Hongbcffce22017-02-01 15:52:06 -08001355 }
1356 method->generateCppSignature(out, iface->getStubName());
1357 out << " ";
1358 out.block([&] {
1359 method->cppImpl(IMPL_STUB_IMPL, out);
1360 }).endl();
Yifan Hongbcffce22017-02-01 15:52:06 -08001361 });
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001362
Andreas Huber881227d2016-08-02 14:20:21 -07001363 out << "::android::status_t " << klassName << "::onTransact(\n";
1364
1365 out.indent();
1366 out.indent();
1367
Iliyan Malchev549e2592016-08-10 08:59:12 -07001368 out << "uint32_t _hidl_code,\n"
1369 << "const ::android::hardware::Parcel &_hidl_data,\n"
1370 << "::android::hardware::Parcel *_hidl_reply,\n"
1371 << "uint32_t _hidl_flags,\n"
1372 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001373
1374 out.unindent();
1375
Iliyan Malchev549e2592016-08-10 08:59:12 -07001376 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001377 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001378 out.indent();
1379
Yifan Hong10fe0b52016-10-19 14:20:17 -07001380 for (const auto &tuple : iface->allMethodsFromRoot()) {
1381 const Method *method = tuple.method();
1382 const Interface *superInterface = tuple.interface();
Steven Morelandf16c5c02017-07-31 16:50:06 -07001383
Howard Chen71f289f2017-08-29 17:35:01 +08001384 if (!isIBase() && method->isHidlReserved()) {
1385 continue;
1386 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001387 out << "case "
1388 << method->getSerialId()
1389 << " /* "
1390 << method->name()
1391 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001392
Yifan Hong10fe0b52016-10-19 14:20:17 -07001393 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001394
Steven Moreland368e4602018-02-16 14:21:49 -08001395 generateStubSourceForMethod(out, method, superInterface);
Yifan Hong10fe0b52016-10-19 14:20:17 -07001396
1397 out.unindent();
1398 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001399 }
1400
1401 out << "default:\n{\n";
1402 out.indent();
1403
Martijn Coenen225bc922017-06-27 14:39:46 -07001404 if (iface->isIBase()) {
1405 out << "(void)_hidl_flags;\n";
1406 out << "return ::android::UNKNOWN_TRANSACTION;\n";
1407 } else {
1408 out << "return ";
1409 out << gIBaseFqName.getInterfaceStubFqName().cppName();
1410 out << "::onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001411
Martijn Coenen225bc922017-06-27 14:39:46 -07001412 out.indent();
1413 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001414
Martijn Coenen225bc922017-06-27 14:39:46 -07001415 out << "_hidl_code, _hidl_data, _hidl_reply, "
1416 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001417
Martijn Coenen225bc922017-06-27 14:39:46 -07001418 out.unindent();
1419 out.unindent();
1420 }
Andreas Huber881227d2016-08-02 14:20:21 -07001421
1422 out.unindent();
1423 out << "}\n";
1424
1425 out.unindent();
1426 out << "}\n\n";
1427
Yifan Honga018ed52016-12-13 16:35:08 -08001428 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1429 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1430 out.indent(2, [&] {
1431 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1432 out << "_hidl_reply);\n";
1433 });
1434 });
Andreas Huber881227d2016-08-02 14:20:21 -07001435
Iliyan Malchev549e2592016-08-10 08:59:12 -07001436 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001437
1438 out.unindent();
1439 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001440}
1441
Steven Moreland368e4602018-02-16 14:21:49 -08001442void AST::generateStubSourceForMethod(Formatter& out, const Method* method,
1443 const Interface* superInterface) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001444 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1445 method->cppImpl(IMPL_STUB, out);
1446 out << "break;\n";
Steven Moreland368e4602018-02-16 14:21:49 -08001447 return;
Martijn Coenen115d4282016-12-19 05:14:04 +01001448 }
1449
Steven Morelandf16c5c02017-07-31 16:50:06 -07001450 out << "_hidl_err = "
1451 << superInterface->fqName().cppNamespace()
1452 << "::"
1453 << superInterface->getStubName()
1454 << "::_hidl_"
1455 << method->name()
1456 << "(this, _hidl_data, _hidl_reply, _hidl_cb);\n";
1457 out << "break;\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001458}
1459
Steven Moreland368e4602018-02-16 14:21:49 -08001460void AST::generateStaticStubMethodSource(Formatter& out, const FQName& fqName,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001461 const Method* method, const Interface* superInterface) const {
Steven Morelandf16c5c02017-07-31 16:50:06 -07001462 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
Steven Moreland368e4602018-02-16 14:21:49 -08001463 return;
Steven Morelandf16c5c02017-07-31 16:50:06 -07001464 }
1465
Steven Morelandf8197902018-01-30 15:38:37 -08001466 const std::string& klassName = fqName.getInterfaceStubName();
1467
Steven Morelandf16c5c02017-07-31 16:50:06 -07001468 out << "::android::status_t " << klassName << "::_hidl_" << method->name() << "(\n";
1469
1470 out.indent();
1471 out.indent();
1472
1473 out << "::android::hidl::base::V1_0::BnHwBase* _hidl_this,\n"
1474 << "const ::android::hardware::Parcel &_hidl_data,\n"
1475 << "::android::hardware::Parcel *_hidl_reply,\n"
1476 << "TransactCallback _hidl_cb) {\n";
1477
1478 out.unindent();
1479
1480 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
1481 out << "bool mEnableInstrumentation = _hidl_this->isInstrumentationEnabled();\n";
1482 out << "const auto &mInstrumentationCallbacks = _hidl_this->getInstrumentationCallbacks();\n";
1483 out << "#endif // __ANDROID_DEBUGGABLE__\n\n";
1484
1485 out << "::android::status_t _hidl_err = ::android::OK;\n";
1486
Yifan Hongeefe4f22017-01-04 15:32:42 -08001487 out << "if (!_hidl_data.enforceInterface("
Steven Morelandf16c5c02017-07-31 16:50:06 -07001488 << klassName
1489 << "::Pure::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001490
Andreas Huber881227d2016-08-02 14:20:21 -07001491 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001492 out << "_hidl_err = ::android::BAD_TYPE;\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001493 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001494 out.unindent();
1495 out << "}\n\n";
1496
Andreas Huber5e44a292016-09-27 14:52:39 -07001497 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001498
Andreas Huber881227d2016-08-02 14:20:21 -07001499 for (const auto &arg : method->args()) {
1500 emitCppReaderWriter(
1501 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001502 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001503 false /* parcelObjIsPointer */,
1504 arg,
1505 true /* reader */,
Steven Morelandf16c5c02017-07-31 16:50:06 -07001506 Type::ErrorMode_Return,
Andreas Huber5e44a292016-09-27 14:52:39 -07001507 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001508 }
1509
Steven Moreland92a08a72017-07-31 14:57:37 -07001510 generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001511 out,
1512 InstrumentationEvent::SERVER_API_ENTRY,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001513 method,
1514 superInterface);
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001515
Andreas Huber881227d2016-08-02 14:20:21 -07001516 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -07001517 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland3e787002017-08-16 14:59:54 -07001518
1519 std::string callee;
1520
1521 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL)) {
1522 callee = "_hidl_this";
1523 } else {
Steven Morelandf8197902018-01-30 15:38:37 -08001524 callee = "static_cast<" + fqName.getInterfaceName() + "*>(_hidl_this->getImpl().get())";
Steven Moreland3e787002017-08-16 14:59:54 -07001525 }
Andreas Huber881227d2016-08-02 14:20:21 -07001526
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001527 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001528 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001529 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001530 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001531 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001532 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001533 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001534
Yifan Hong932464e2017-03-30 15:40:22 -07001535 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001536 if (arg->type().resultNeedsDeref()) {
1537 out << "*";
1538 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001539 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001540 });
Andreas Huber881227d2016-08-02 14:20:21 -07001541
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001542 out << ");\n\n";
Steven Moreland30232dc2019-03-05 19:39:10 -08001543
Yifan Hong859e53f2016-11-14 19:08:24 -08001544 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1545 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001546
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001547 elidedReturn->type().emitReaderWriter(
1548 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001549 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001550 "_hidl_reply",
1551 true, /* parcelObjIsPointer */
1552 false, /* isReader */
1553 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001554
Steven Moreland92a08a72017-07-31 14:57:37 -07001555 generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001556 out,
1557 InstrumentationEvent::SERVER_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001558 method,
1559 superInterface);
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001560
Iliyan Malchev549e2592016-08-10 08:59:12 -07001561 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001562 } else {
1563 if (returnsValue) {
1564 out << "bool _hidl_callbackCalled = false;\n\n";
1565 }
Andreas Huber881227d2016-08-02 14:20:21 -07001566
Steven Moreland30232dc2019-03-05 19:39:10 -08001567 out << "::android::hardware::Return<void> _hidl_ret = " << callee << "->" << method->name()
1568 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001569
Yifan Hong932464e2017-03-30 15:40:22 -07001570 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001571 if (arg->type().resultNeedsDeref()) {
1572 out << "*";
1573 }
1574
1575 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001576 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001577
1578 if (returnsValue) {
Yifan Hong932464e2017-03-30 15:40:22 -07001579 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001580 out << ", ";
1581 }
1582
1583 out << "[&](";
1584
Yifan Hong932464e2017-03-30 15:40:22 -07001585 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001586 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001587 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001588
1589 out << ") {\n";
1590 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001591 out << "if (_hidl_callbackCalled) {\n";
1592 out.indent();
1593 out << "LOG_ALWAYS_FATAL(\""
1594 << method->name()
1595 << ": _hidl_cb called a second time, but must be called once.\");\n";
1596 out.unindent();
1597 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001598 out << "_hidl_callbackCalled = true;\n\n";
1599
Yifan Hong859e53f2016-11-14 19:08:24 -08001600 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1601 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001602
1603 for (const auto &arg : method->results()) {
1604 emitCppReaderWriter(
1605 out,
1606 "_hidl_reply",
1607 true /* parcelObjIsPointer */,
1608 arg,
1609 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001610 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001611 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001612 }
1613
Steven Moreland92a08a72017-07-31 14:57:37 -07001614 generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001615 out,
1616 InstrumentationEvent::SERVER_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001617 method,
1618 superInterface);
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001619
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001620 out << "_hidl_cb(*_hidl_reply);\n";
1621
1622 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001623 out << "});\n\n";
1624 } else {
1625 out << ");\n\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001626 out << "(void) _hidl_cb;\n\n";
Steven Moreland92a08a72017-07-31 14:57:37 -07001627 generateCppInstrumentationCall(
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001628 out,
1629 InstrumentationEvent::SERVER_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001630 method,
1631 superInterface);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001632 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001633
Steven Moreland30232dc2019-03-05 19:39:10 -08001634 out << "_hidl_ret.assertOk();\n";
1635
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001636 if (returnsValue) {
1637 out << "if (!_hidl_callbackCalled) {\n";
1638 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001639 out << "LOG_ALWAYS_FATAL(\""
1640 << method->name()
1641 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001642 out.unindent();
1643 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001644 } else {
1645 out << "::android::hardware::writeToParcel("
1646 << "::android::hardware::Status::ok(), "
1647 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001648 }
Andreas Huber881227d2016-08-02 14:20:21 -07001649 }
1650
Steven Morelandf16c5c02017-07-31 16:50:06 -07001651 out << "return _hidl_err;\n";
1652 out.unindent();
1653 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001654}
1655
Steven Moreland368e4602018-02-16 14:21:49 -08001656void AST::generatePassthroughHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001657 if (!AST::isInterface()) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001658 // types.hal does not get a stub header.
Steven Moreland368e4602018-02-16 14:21:49 -08001659 return;
Steven Moreland69e7c702016-09-09 11:16:32 -07001660 }
1661
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001662 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -07001663 CHECK(iface != nullptr);
Steven Moreland69e7c702016-09-09 11:16:32 -07001664
Yifan Hongeefe4f22017-01-04 15:32:42 -08001665 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001666
Steven Moreland69e7c702016-09-09 11:16:32 -07001667 const std::string guard = makeHeaderGuard(klassName);
1668
1669 out << "#ifndef " << guard << "\n";
1670 out << "#define " << guard << "\n\n";
1671
1672 std::vector<std::string> packageComponents;
1673 getPackageAndVersionComponents(
1674 &packageComponents, false /* cpp_compatible */);
1675
Steven Moreland61d3f4b2017-04-28 17:30:38 -07001676 out << "#include <android-base/macros.h>\n";
Yifan Hongb0949432016-12-15 15:32:24 -08001677 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001678 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001679
Steven Moreland19f11b52017-05-12 18:22:21 -07001680 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Morelandee88eed2016-10-31 17:49:00 -07001681 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001682
Yifan Hong7a118f52016-12-07 11:21:15 -08001683 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Neel Mehta19f79792019-05-21 13:39:32 -07001684 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001685
1686 enterLeaveNamespace(out, true /* enter */);
1687 out << "\n";
1688
1689 out << "struct "
1690 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -07001691 << " : " << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001692 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001693
1694 out.indent();
1695 out << "explicit "
1696 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001697 << "(const ::android::sp<"
Steven Moreland19f11b52017-05-12 18:22:21 -07001698 << iface->localName()
Steven Moreland69e7c702016-09-09 11:16:32 -07001699 << "> impl);\n";
1700
Steven Moreland0b843772017-06-23 16:33:38 -07001701 out.endl();
1702 generateTemplatizationLink(out);
Steven Moreland1a52e822017-07-27 13:56:29 -07001703 generateCppTag(out, "android::hardware::details::bs_tag");
Steven Moreland0b843772017-06-23 16:33:38 -07001704
Steven Moreland616cf4d2018-10-02 13:52:18 -07001705 generateMethods(out, [&](const Method* method, const Interface* superInterface) {
1706 generatePassthroughMethod(out, method, superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001707 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001708
Steven Moreland69e7c702016-09-09 11:16:32 -07001709 out.unindent();
1710 out << "private:\n";
1711 out.indent();
Steven Moreland19f11b52017-05-12 18:22:21 -07001712 out << "const ::android::sp<" << iface->localName() << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001713
Neel Mehta19f79792019-05-21 13:39:32 -07001714 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001715
Neel Mehta19f79792019-05-21 13:39:32 -07001716 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001717
Neel Mehta19f79792019-05-21 13:39:32 -07001718 out << "::android::hardware::Return<void> addOnewayTask("
1719 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001720
1721 out.unindent();
1722
1723 out << "};\n\n";
1724
1725 enterLeaveNamespace(out, false /* enter */);
1726
1727 out << "\n#endif // " << guard << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001728}
1729
Steven Moreland368e4602018-02-16 14:21:49 -08001730void AST::generateInterfaceSource(Formatter& out) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001731 const Interface* iface = mRootScope.getInterface();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001732
Yifan Hong2d7126b2016-10-20 15:12:57 -07001733 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001734 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001735
Steven Moreland368e4602018-02-16 14:21:49 -08001736 generateMethods(out, [&](const Method* method, const Interface*) {
Steven Morelandd4b068a2017-03-20 06:30:51 -07001737 bool reserved = method->isHidlReserved();
1738
1739 if (!reserved) {
1740 out << "// no default implementation for: ";
1741 }
1742 method->generateCppSignature(out, iface->localName());
1743 if (reserved) {
1744 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001745 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001746 }).endl();
1747 }
1748
1749 out << "\n";
1750
Steven Moreland368e4602018-02-16 14:21:49 -08001751 return;
Steven Morelandd4b068a2017-03-20 06:30:51 -07001752 });
Steven Morelandd4b068a2017-03-20 06:30:51 -07001753
Yifan Hong3d746092016-12-07 14:26:33 -08001754 for (const Interface *superType : iface->typeChain()) {
Steven Moreland23cc5fa2018-05-09 10:48:48 -07001755 out << "::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -08001756 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -07001757 << "> "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001758 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001759 << "::castFrom("
1760 << superType->getCppArgumentType()
Yifan Hong200209c2017-03-29 03:39:09 -07001761 << " parent, bool "
1762 << (iface == superType ? "/* emitError */" : "emitError")
1763 << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001764 out.indent();
1765 if (iface == superType) {
1766 out << "return parent;\n";
1767 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001768 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001769 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001770 << superType->fqName().cppName() << ", "
Steven Moreland57a89362017-07-21 19:29:54 +00001771 << iface->getProxyName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001772 << ">(\n";
1773 out.indent();
1774 out.indent();
1775 out << "parent, \""
1776 << iface->fqName().string()
Yifan Hong200209c2017-03-29 03:39:09 -07001777 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001778 out.unindent();
1779 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001780 }
Yifan Hong3d746092016-12-07 14:26:33 -08001781 out.unindent();
1782 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001783 }
Yifan Hongfe95aa22016-10-19 17:26:45 -07001784}
1785
Steven Moreland368e4602018-02-16 14:21:49 -08001786void AST::generatePassthroughSource(Formatter& out) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001787 const Interface* iface = mRootScope.getInterface();
Steven Moreland69e7c702016-09-09 11:16:32 -07001788
Yifan Hongeefe4f22017-01-04 15:32:42 -08001789 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001790
Neel Mehta19f79792019-05-21 13:39:32 -07001791 out << klassName << "::" << klassName << "(const ::android::sp<" << iface->fullName()
1792 << "> impl) : ::android::hardware::details::HidlInstrumentor(\"" << mPackage.string()
1793 << "\", \"" << iface->localName() << "\"), mImpl(impl) {\n";
1794
1795 out.indent([&] { out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n"; });
1796
Yifan Hong2cbc1472016-10-25 19:02:40 -07001797 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001798
Neel Mehta19f79792019-05-21 13:39:32 -07001799 out << "::android::hardware::Return<void> " << klassName
1800 << "::addOnewayTask(std::function<void(void)> fun) {\n";
1801 out.indent();
1802 out << "if (!mOnewayQueue.push(fun)) {\n";
1803 out.indent();
1804 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1805 out.indent();
1806 out.indent();
1807 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
1808 << "\"Passthrough oneway function queue exceeds maximum size.\");\n";
1809 out.unindent();
1810 out.unindent();
1811 out.unindent();
1812 out << "}\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001813
Neel Mehta19f79792019-05-21 13:39:32 -07001814 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001815
Neel Mehta19f79792019-05-21 13:39:32 -07001816 out.unindent();
1817 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001818}
1819
Steven Moreland92a08a72017-07-31 14:57:37 -07001820void AST::generateCppAtraceCall(Formatter &out,
Martijn Coenen7b295242016-11-04 16:52:56 +01001821 InstrumentationEvent event,
1822 const Method *method) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001823 const Interface* iface = mRootScope.getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08001824 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01001825 switch (event) {
1826 case SERVER_API_ENTRY:
1827 {
1828 out << "atrace_begin(ATRACE_TAG_HAL, \""
1829 << baseString + "::server\");\n";
1830 break;
1831 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001832 case PASSTHROUGH_ENTRY:
1833 {
1834 out << "atrace_begin(ATRACE_TAG_HAL, \""
1835 << baseString + "::passthrough\");\n";
1836 break;
1837 }
1838 case SERVER_API_EXIT:
Martijn Coenen7b295242016-11-04 16:52:56 +01001839 case PASSTHROUGH_EXIT:
1840 {
1841 out << "atrace_end(ATRACE_TAG_HAL);\n";
1842 break;
1843 }
Steven Moreland4607ef52018-05-09 10:52:47 -07001844 // client uses scope because of gotos
1845 // this isn't done for server because the profiled code isn't alone in its scope
1846 // this isn't done for passthrough becuase the profiled boundary isn't even in the same code
1847 case CLIENT_API_ENTRY: {
Michael Butler0a3d99a2018-07-26 13:47:10 -07001848 out << "::android::ScopedTrace PASTE(___tracer, __LINE__) (ATRACE_TAG_HAL, \""
1849 << baseString + "::client\");\n";
Steven Moreland4607ef52018-05-09 10:52:47 -07001850 break;
1851 }
1852 case CLIENT_API_EXIT:
1853 break;
Martijn Coenen7b295242016-11-04 16:52:56 +01001854 default:
1855 {
Steven Morelandcbff5612017-10-11 17:01:54 -07001856 CHECK(false) << "Unsupported instrumentation event: " << event;
Martijn Coenen7b295242016-11-04 16:52:56 +01001857 }
1858 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001859}
1860
Steven Moreland92a08a72017-07-31 14:57:37 -07001861void AST::generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001862 Formatter &out,
1863 InstrumentationEvent event,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001864 const Method *method,
1865 const Interface* superInterface) const {
Steven Moreland92a08a72017-07-31 14:57:37 -07001866 generateCppAtraceCall(out, event, method);
Martijn Coenen7b295242016-11-04 16:52:56 +01001867
Steven Moreland30b76e92017-06-02 18:52:24 -07001868 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001869 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
1870 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001871 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001872 std::string event_str = "";
1873 switch (event) {
1874 case SERVER_API_ENTRY:
1875 {
1876 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
1877 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001878 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001879 << (arg->type().resultNeedsDeref() ? "" : "&")
1880 << arg->name()
1881 << ");\n";
1882 }
1883 break;
1884 }
1885 case SERVER_API_EXIT:
1886 {
1887 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07001888 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08001889 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07001890 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001891 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001892 }
1893 break;
1894 }
1895 case CLIENT_API_ENTRY:
1896 {
1897 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
1898 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001899 out << "_hidl_args.push_back((void *)&"
1900 << arg->name()
1901 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001902 }
1903 break;
1904 }
1905 case CLIENT_API_EXIT:
1906 {
1907 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
1908 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001909 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001910 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001911 << "_hidl_out_"
1912 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001913 << ");\n";
1914 }
1915 break;
1916 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001917 case PASSTHROUGH_ENTRY:
1918 {
1919 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
1920 for (const auto &arg : method->args()) {
1921 out << "_hidl_args.push_back((void *)&"
1922 << arg->name()
1923 << ");\n";
1924 }
1925 break;
1926 }
1927 case PASSTHROUGH_EXIT:
1928 {
1929 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08001930 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08001931 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08001932 << arg->name()
1933 << ");\n";
1934 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001935 break;
1936 }
Steven Moreland031ccf12016-10-31 15:54:38 -07001937 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001938 {
Steven Morelandcbff5612017-10-11 17:01:54 -07001939 CHECK(false) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001940 }
1941 }
1942
Steven Moreland1ab31442016-11-03 18:37:51 -07001943 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001944 out.indent();
1945 out << "callback("
1946 << event_str
1947 << ", \""
Steven Moreland616cf4d2018-10-02 13:52:18 -07001948 << superInterface->fqName().package()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001949 << "\", \""
Steven Moreland616cf4d2018-10-02 13:52:18 -07001950 << superInterface->fqName().version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001951 << "\", \""
Steven Moreland616cf4d2018-10-02 13:52:18 -07001952 << superInterface->localName()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001953 << "\", \""
1954 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001955 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001956 out.unindent();
1957 out << "}\n";
1958 out.unindent();
Steven Moreland30b76e92017-06-02 18:52:24 -07001959 out << "}\n";
1960 out << "#endif // __ANDROID_DEBUGGABLE__\n\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001961}
1962
Andreas Huber881227d2016-08-02 14:20:21 -07001963} // namespace android