blob: d7ee043a88ad18a03a33364af34d188e7199caff [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
Steven Moreland5708edf2016-11-04 15:33:31 +000038std::string AST::makeHeaderGuard(const std::string &baseName,
39 bool indicateGenerated) const {
40 std::string guard;
Andreas Huber881227d2016-08-02 14:20:21 -070041
Steven Moreland5708edf2016-11-04 15:33:31 +000042 if (indicateGenerated) {
43 guard += "HIDL_GENERATED_";
44 }
45
46 guard += StringHelper::Uppercase(mPackage.tokenName());
Andreas Huber881227d2016-08-02 14:20:21 -070047 guard += "_";
Steven Moreland5708edf2016-11-04 15:33:31 +000048 guard += StringHelper::Uppercase(baseName);
49 guard += "_H";
Andreas Huber881227d2016-08-02 14:20:21 -070050
51 return guard;
52}
53
Steven Morelandee88eed2016-10-31 17:49:00 -070054void AST::generateCppPackageInclude(
55 Formatter &out,
56 const FQName &package,
57 const std::string &klass) {
58
59 out << "#include <";
60
Steven Moreland4a385ed2019-12-09 14:47:18 -080061 std::vector<std::string> components =
62 package.getPackageAndVersionComponents(false /* sanitized */);
Steven Morelandee88eed2016-10-31 17:49:00 -070063
64 for (const auto &component : components) {
65 out << component << "/";
66 }
67
68 out << klass
69 << ".h>\n";
70}
71
Andreas Huber881227d2016-08-02 14:20:21 -070072void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
Steven Moreland4a385ed2019-12-09 14:47:18 -080073 std::vector<std::string> packageComponents =
74 mPackage.getPackageAndVersionComponents(true /* sanitized */);
Andreas Huber881227d2016-08-02 14:20:21 -070075
76 if (enter) {
77 for (const auto &component : packageComponents) {
78 out << "namespace " << component << " {\n";
79 }
80 } else {
81 for (auto it = packageComponents.rbegin();
82 it != packageComponents.rend();
83 ++it) {
84 out << "} // namespace " << *it << "\n";
85 }
86 }
87}
88
Steven Moreland038903b2017-03-30 12:11:24 -070089static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
90 const std::string functionName = isTry ? "tryGetService" : "getService";
91
Steven Moreland7645fbd2019-03-12 18:49:28 -070092 if (isTry) {
93 DocComment(
94 "This gets the service of this type with the specified instance name. If the\n"
95 "service is currently not available or not in the VINTF manifest on a Trebilized\n"
96 "device, this will return nullptr. This is useful when you don't want to block\n"
97 "during device boot. If getStub is true, this will try to return an unwrapped\n"
98 "passthrough implementation in the same process. This is useful when getting an\n"
99 "implementation from the same partition/compilation group.\n\n"
Neel Mehta291d02e2019-06-06 17:51:07 -0700100 "In general, prefer getService(std::string,bool)",
101 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700102 .emit(out);
103 } else {
104 DocComment(
105 "This gets the service of this type with the specified instance name. If the\n"
106 "service is not in the VINTF manifest on a Trebilized device, this will return\n"
107 "nullptr. If the service is not available, this will wait for the service to\n"
108 "become available. If the service is a lazy service, this will start the service\n"
109 "and return when it becomes available. If getStub is true, this will try to\n"
110 "return an unwrapped passthrough implementation in the same process. This is\n"
Neel Mehta291d02e2019-06-06 17:51:07 -0700111 "useful when getting an implementation from the same partition/compilation group.",
112 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700113 .emit(out);
114 }
Steven Moreland038903b2017-03-30 12:11:24 -0700115 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800116 << "const std::string &serviceName=\"default\", bool getStub=false);\n";
Neel Mehta291d02e2019-06-06 17:51:07 -0700117 DocComment("Deprecated. See " + functionName + "(std::string, bool)", HIDL_LOCATION_HERE)
118 .emit(out);
Steven Moreland038903b2017-03-30 12:11:24 -0700119 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800120 << "const char serviceName[], bool getStub=false)"
121 << " { std::string str(serviceName ? serviceName : \"\");"
Steven Moreland038903b2017-03-30 12:11:24 -0700122 << " return " << functionName << "(str, getStub); }\n";
Neel Mehta291d02e2019-06-06 17:51:07 -0700123 DocComment("Deprecated. See " + functionName + "(std::string, bool)", HIDL_LOCATION_HERE)
124 .emit(out);
Steven Moreland038903b2017-03-30 12:11:24 -0700125 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800126 << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
127 // without c_str the std::string constructor is ambiguous
128 << " { std::string str(serviceName.c_str());"
Steven Moreland038903b2017-03-30 12:11:24 -0700129 << " return " << functionName << "(str, getStub); }\n";
Steven Moreland7645fbd2019-03-12 18:49:28 -0700130 DocComment("Calls " + functionName +
Neel Mehta291d02e2019-06-06 17:51:07 -0700131 "(\"default\", bool). This is the recommended instance name for singleton "
132 "services.",
133 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700134 .emit(out);
Steven Moreland038903b2017-03-30 12:11:24 -0700135 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
136 << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
137}
138
139static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
140 declareGetService(out, interfaceName, true /* isTry */);
141 declareGetService(out, interfaceName, false /* isTry */);
142
Steven Moreland7645fbd2019-03-12 18:49:28 -0700143 DocComment(
144 "Registers a service with the service manager. For Trebilized devices, the service\n"
Neel Mehta291d02e2019-06-06 17:51:07 -0700145 "must also be in the VINTF manifest.",
146 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700147 .emit(out);
Steven Moreland90831502017-03-27 12:08:40 -0700148 out << "__attribute__ ((warn_unused_result))"
149 << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Neel Mehta291d02e2019-06-06 17:51:07 -0700150 DocComment("Registers for notifications for when a service is registered.", HIDL_LOCATION_HERE)
151 .emit(out);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800152 out << "static bool registerForNotifications(\n";
153 out.indent(2, [&] {
154 out << "const std::string &serviceName,\n"
155 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
156 << "&notification);\n";
157 });
158
159}
160
Steven Moreland038903b2017-03-30 12:11:24 -0700161static void implementGetService(Formatter &out,
162 const FQName &fqName,
163 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800164
165 const std::string interfaceName = fqName.getInterfaceName();
Steven Moreland038903b2017-03-30 12:11:24 -0700166 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800167
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700168 out << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000169 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800170 out.block([&] {
Steven Moreland78f95f92017-10-06 17:07:40 -0700171 out << "return ::android::hardware::details::getServiceInternal<"
172 << fqName.getInterfaceProxyName()
173 << ">(serviceName, "
174 << (!isTry ? "true" : "false") // retry
175 << ", getStub);\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800176 }).endl().endl();
Steven Moreland038903b2017-03-30 12:11:24 -0700177}
178
179static void implementServiceManagerInteractions(Formatter &out,
180 const FQName &fqName, const std::string &package) {
181
182 const std::string interfaceName = fqName.getInterfaceName();
183
184 implementGetService(out, fqName, true /* isTry */);
185 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800186
Yifan Hongeefe4f22017-01-04 15:32:42 -0800187 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800188 << "const std::string &serviceName) ";
189 out.block([&] {
Steven Moreland5f84b382018-10-11 12:10:35 -0700190 out << "return ::android::hardware::details::registerAsServiceInternal(this, serviceName);\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800191 }).endl().endl();
192
Yifan Hongeefe4f22017-01-04 15:32:42 -0800193 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800194 out.indent(2, [&] {
195 out << "const std::string &serviceName,\n"
196 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
197 << "&notification) ";
198 });
199 out.block([&] {
200 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
201 out.indent(2, [&] {
202 out << "= ::android::hardware::defaultServiceManager();\n";
203 });
204 out.sIf("sm == nullptr", [&] {
205 out << "return false;\n";
206 }).endl();
207 out << "::android::hardware::Return<bool> success =\n";
208 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800209 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800210 out.indent(2, [&] {
211 out << "serviceName, notification);\n";
212 });
213 });
214 out << "return success.isOk() && success;\n";
215 }).endl().endl();
216}
217
Steven Moreland368e4602018-02-16 14:21:49 -0800218void AST::generateInterfaceHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700219 const Interface *iface = getInterface();
Neel Mehta257cce02019-07-19 13:24:57 -0700220 std::string ifaceName = iface ? iface->definedName() : "types";
Andreas Huber881227d2016-08-02 14:20:21 -0700221 const std::string guard = makeHeaderGuard(ifaceName);
222
223 out << "#ifndef " << guard << "\n";
224 out << "#define " << guard << "\n\n";
225
Andreas Huber737080b2016-08-02 15:38:04 -0700226 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700227 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700228 }
229
230 if (!mImportedNames.empty()) {
231 out << "\n";
232 }
233
Steven Moreland19f11b52017-05-12 18:22:21 -0700234 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800235 if (isIBase()) {
236 out << "// skipped #include IServiceNotification.h\n\n";
237 } else {
238 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
239 }
Steven Moreland0693f312016-11-09 15:06:14 -0800240 }
241
Yifan Hongc8934042016-11-17 17:10:52 -0800242 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700243 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700244
Steven Moreland19f11b52017-05-12 18:22:21 -0700245 if (iface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200246 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700247 }
248
Martijn Coenenaf712c02016-11-16 15:26:27 +0100249 out << "#include <utils/NativeHandle.h>\n";
250 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700251
252 enterLeaveNamespace(out, true /* enter */);
253 out << "\n";
254
Steven Moreland19f11b52017-05-12 18:22:21 -0700255 if (iface) {
Steven Moreland70cb55e2019-03-12 17:20:54 -0700256 iface->emitDocComment(out);
257
Andreas Huber881227d2016-08-02 14:20:21 -0700258 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700259 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700260
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700261 const Interface *superType = iface->superType();
262
Yi Kong56758da2018-07-24 16:21:37 -0700263 if (superType == nullptr) {
Yifan Hongc8934042016-11-17 17:10:52 -0800264 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700265 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000266 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700267 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700268 }
269
270 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700271
272 out.indent();
273
Neel Mehta291d02e2019-06-06 17:51:07 -0700274 DocComment("Type tag for use in template logic that indicates this is a 'pure' class.",
275 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700276 .emit(out);
Diogo Ferreira604fe012019-10-17 12:32:00 +0100277 generateCppTag(out, "::android::hardware::details::i_tag");
Andreas Huber881227d2016-08-02 14:20:21 -0700278
Neel Mehta291d02e2019-06-06 17:51:07 -0700279 DocComment("Fully qualified interface name: \"" + iface->fqName().string() + "\"",
280 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700281 .emit(out);
282 out << "static const char* descriptor;\n\n";
283
Steven Moreland70cb55e2019-03-12 17:20:54 -0700284 iface->emitTypeDeclarations(out);
285 } else {
286 mRootScope.emitTypeDeclarations(out);
287 }
Andreas Huber881227d2016-08-02 14:20:21 -0700288
Steven Moreland19f11b52017-05-12 18:22:21 -0700289 if (iface) {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700290 DocComment(
Neel Mehta291d02e2019-06-06 17:51:07 -0700291 "Returns whether this object's implementation is outside of the current process.",
292 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700293 .emit(out);
Yifan Hongc8934042016-11-17 17:10:52 -0800294 out << "virtual bool isRemote() const ";
295 if (!isIBase()) {
296 out << "override ";
297 }
Steven Moreland7645fbd2019-03-12 18:49:28 -0700298 out << "{ return false; }\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800299
Steven Morelandf7f2a9a2017-07-21 18:05:38 -0700300 for (const auto& tuple : iface->allMethodsFromRoot()) {
301 const Method* method = tuple.method();
302
Andreas Huber881227d2016-08-02 14:20:21 -0700303 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700304
Andreas Huber881227d2016-08-02 14:20:21 -0700305 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700306 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Morelandd732ea12016-11-08 17:12:06 -0800307
308 if (elidedReturn == nullptr && returnsValue) {
Neel Mehta291d02e2019-06-06 17:51:07 -0700309 DocComment("Return callback for " + method->name(), HIDL_LOCATION_HERE).emit(out);
Steven Morelandd732ea12016-11-08 17:12:06 -0800310 out << "using "
311 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700312 << "_cb = std::function<void(";
313 method->emitCppResultSignature(out, true /* specify namespaces */);
314 out << ")>;\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800315 }
Andreas Huber881227d2016-08-02 14:20:21 -0700316
Steven Moreland49bad8d2018-05-17 15:45:26 -0700317 method->emitDocComment(out);
318
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700319 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700320 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700321 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700322 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700323 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700324 }
325
326 out << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700327 << "(";
328 method->emitCppArgSignature(out, true /* specify namespaces */);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700329 out << ")";
330 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800331 if (!isIBase()) {
332 out << " override";
333 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700334 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700335 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700336 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700337 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700338 }
Steven Moreland40786312016-08-16 10:29:40 -0700339
Steven Moreland7645fbd2019-03-12 18:49:28 -0700340 out << "\n// cast static functions\n";
Yifan Hong3d746092016-12-07 14:26:33 -0800341 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700342
Yifan Hong3d746092016-12-07 14:26:33 -0800343 for (const Interface *superType : iface->typeChain()) {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700344 DocComment(
345 "This performs a checked cast based on what the underlying implementation "
Neel Mehta291d02e2019-06-06 17:51:07 -0700346 "actually is.",
347 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700348 .emit(out);
Yifan Hong200209c2017-03-29 03:39:09 -0700349 out << "static ::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -0800350 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -0700351 << "> castFrom("
Yifan Hong3d746092016-12-07 14:26:33 -0800352 << superType->getCppArgumentType()
353 << " parent"
Yifan Hong200209c2017-03-29 03:39:09 -0700354 << ", bool emitError = false);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700355 }
356
Yifan Hongc8934042016-11-17 17:10:52 -0800357 if (isIBase()) {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700358 out << "\n// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800359 } else {
Steven Moreland7645fbd2019-03-12 18:49:28 -0700360 out << "\n// helper methods for interactions with the hwservicemanager\n";
Neel Mehta257cce02019-07-19 13:24:57 -0700361 declareServiceManagerInteractions(out, iface->definedName());
Yifan Hongc8934042016-11-17 17:10:52 -0800362 }
Andreas Huber881227d2016-08-02 14:20:21 -0700363 }
364
Steven Moreland19f11b52017-05-12 18:22:21 -0700365 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700366 out.unindent();
367
Andreas Hubere3f769a2016-10-10 10:54:44 -0700368 out << "};\n\n";
369 }
370
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700371 out << "//\n";
372 out << "// type declarations for package\n";
373 out << "//\n\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800374 mRootScope.emitPackageTypeDeclarations(out);
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700375 out << "//\n";
376 out << "// type header definitions for package\n";
377 out << "//\n\n";
378 mRootScope.emitPackageTypeHeaderDefinitions(out);
Andreas Huber881227d2016-08-02 14:20:21 -0700379
380 out << "\n";
381 enterLeaveNamespace(out, false /* enter */);
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700382 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700383
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700384 out << "//\n";
385 out << "// global type declarations for package\n";
386 out << "//\n\n";
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800387 mRootScope.emitGlobalTypeDeclarations(out);
388
Andreas Huber881227d2016-08-02 14:20:21 -0700389 out << "\n#endif // " << guard << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700390}
391
Steven Moreland368e4602018-02-16 14:21:49 -0800392void AST::generateHwBinderHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700393 const Interface *iface = getInterface();
394 std::string klassName = iface ? iface->getHwName() : "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700395
Steven Moreland40786312016-08-16 10:29:40 -0700396 const std::string guard = makeHeaderGuard(klassName);
397
398 out << "#ifndef " << guard << "\n";
399 out << "#define " << guard << "\n\n";
400
Neel Mehta257cce02019-07-19 13:24:57 -0700401 generateCppPackageInclude(out, mPackage, iface ? iface->definedName() : "types");
Steven Moreland40786312016-08-16 10:29:40 -0700402
Steven Morelandee88eed2016-10-31 17:49:00 -0700403 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700404
405 for (const auto &item : mImportedNames) {
406 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800407 generateCppPackageInclude(out, item, "hwtypes");
408 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800409 generateCppPackageInclude(out, item, item.getInterfaceStubName());
410 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700411 }
Steven Moreland40786312016-08-16 10:29:40 -0700412 }
413
414 out << "\n";
415
Martijn Coenen93915102016-09-01 01:35:52 +0200416 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700417 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100418 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700419
420 out << "\n";
421
422 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700423
Steven Moreland368e4602018-02-16 14:21:49 -0800424 mRootScope.emitPackageHwDeclarations(out);
Steven Moreland40786312016-08-16 10:29:40 -0700425
426 enterLeaveNamespace(out, false /* enter */);
427
428 out << "\n#endif // " << guard << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700429}
430
Steven Moreland58a20c72018-10-09 12:30:51 -0700431static std::string wrapPassthroughArg(Formatter& out, const NamedReference<Type>* arg,
432 std::string name, std::function<void(void)> handleError) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800433 if (!arg->type().isInterface()) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700434 return name;
Yifan Hong7a118f52016-12-07 11:21:15 -0800435 }
Steven Moreland58a20c72018-10-09 12:30:51 -0700436 std::string wrappedName = "_hidl_wrapped_" + name;
Yifan Hong7a118f52016-12-07 11:21:15 -0800437 const Interface &iface = static_cast<const Interface &>(arg->type());
438 out << iface.getCppStackType() << " " << wrappedName << ";\n";
439 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
440 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
441 out << wrappedName
442 << " = "
Steven Morelandbff4bd22017-10-02 14:46:06 -0700443 << "::android::hardware::details::wrapPassthrough("
444 << name
445 << ");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800446 out.sIf(wrappedName + " == nullptr", [&] {
447 // Fatal error. Happens when the BsFoo class is not found in the binary
448 // or any dynamic libraries.
449 handleError();
450 }).endl();
451 }).sElse([&] {
452 out << wrappedName << " = " << name << ";\n";
453 }).endl().endl();
Steven Moreland58a20c72018-10-09 12:30:51 -0700454
455 return wrappedName;
Yifan Hong7a118f52016-12-07 11:21:15 -0800456}
457
Steven Moreland616cf4d2018-10-02 13:52:18 -0700458void AST::generatePassthroughMethod(Formatter& out, const Method* method, const Interface* superInterface) const {
Yifan Hong068c5522016-10-31 14:07:25 -0700459 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700460
Steven Moreland58a20c72018-10-09 12:30:51 -0700461 out << " override {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700462 out.indent();
463
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800464 if (method->isHidlReserved()
465 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
466 method->cppImpl(IMPL_PASSTHROUGH, out);
467 out.unindent();
468 out << "}\n\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800469 return;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800470 }
471
Steven Moreland69e7c702016-09-09 11:16:32 -0700472 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700473 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland69e7c702016-09-09 11:16:32 -0700474
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700475 generateCppInstrumentationCall(
476 out,
477 InstrumentationEvent::PASSTHROUGH_ENTRY,
Steven Moreland616cf4d2018-10-02 13:52:18 -0700478 method,
479 superInterface);
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700480
Steven Moreland58a20c72018-10-09 12:30:51 -0700481 std::vector<std::string> wrappedArgNames;
Yifan Hong7a118f52016-12-07 11:21:15 -0800482 for (const auto &arg : method->args()) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700483 std::string name = wrapPassthroughArg(out, arg, arg->name(), [&] {
Yifan Hong7a118f52016-12-07 11:21:15 -0800484 out << "return ::android::hardware::Status::fromExceptionCode(\n";
485 out.indent(2, [&] {
486 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800487 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800488 });
489 });
Steven Moreland58a20c72018-10-09 12:30:51 -0700490
491 wrappedArgNames.push_back(name);
Yifan Hong7a118f52016-12-07 11:21:15 -0800492 }
493
Steven Moreland58a20c72018-10-09 12:30:51 -0700494 out << "::android::hardware::Status _hidl_error = ::android::hardware::Status::ok();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700495 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700496
497 if (method->isOneway()) {
Steven Moreland836cb312017-06-05 17:25:55 -0700498 out << "addOnewayTask([mImpl = this->mImpl\n"
499 << "#ifdef __ANDROID_DEBUGGABLE__\n"
500 ", mEnableInstrumentation = this->mEnableInstrumentation, "
501 "mInstrumentationCallbacks = this->mInstrumentationCallbacks\n"
502 << "#endif // __ANDROID_DEBUGGABLE__\n";
Steven Moreland58a20c72018-10-09 12:30:51 -0700503 for (const std::string& arg : wrappedArgNames) {
504 out << ", " << arg;
Steven Moreland69e7c702016-09-09 11:16:32 -0700505 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700506 out << "] {\n";
507 out.indent();
Steven Moreland69e7c702016-09-09 11:16:32 -0700508 }
509
510 out << "mImpl->"
511 << method->name()
512 << "(";
513
Yifan Hong932464e2017-03-30 15:40:22 -0700514 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800515 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700516 });
Steven Moreland58a20c72018-10-09 12:30:51 -0700517
518 std::function<void(void)> kHandlePassthroughError = [&] {
519 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
520 out.indent(2, [&] {
521 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
522 << "\"Cannot wrap passthrough interface.\");\n";
523 });
524 };
525
Steven Moreland69e7c702016-09-09 11:16:32 -0700526 if (returnsValue && elidedReturn == nullptr) {
Steven Moreland340c8822017-05-02 14:41:49 -0700527 // never true if oneway since oneway methods don't return values
528
Steven Moreland69e7c702016-09-09 11:16:32 -0700529 if (!method->args().empty()) {
530 out << ", ";
531 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800532 out << "[&](";
Yifan Hong932464e2017-03-30 15:40:22 -0700533 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800534 out << "const auto &_hidl_out_"
535 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700536 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800537
538 out << ") {\n";
539 out.indent();
Steven Moreland92a08a72017-07-31 14:57:37 -0700540 generateCppInstrumentationCall(
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800541 out,
542 InstrumentationEvent::PASSTHROUGH_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -0700543 method,
544 superInterface);
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800545
Steven Moreland58a20c72018-10-09 12:30:51 -0700546 std::vector<std::string> wrappedOutNames;
Yifan Hong7a118f52016-12-07 11:21:15 -0800547 for (const auto &arg : method->results()) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700548 wrappedOutNames.push_back(
549 wrapPassthroughArg(out, arg, "_hidl_out_" + arg->name(), kHandlePassthroughError));
Yifan Hong7a118f52016-12-07 11:21:15 -0800550 }
551
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800552 out << "_hidl_cb(";
Steven Moreland58a20c72018-10-09 12:30:51 -0700553 out.join(wrappedOutNames.begin(), wrappedOutNames.end(), ", ",
554 [&](const std::string& arg) { out << arg; });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800555 out << ");\n";
556 out.unindent();
557 out << "});\n\n";
558 } else {
559 out << ");\n\n";
Steven Moreland30b76e92017-06-02 18:52:24 -0700560
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800561 if (elidedReturn != nullptr) {
Steven Moreland58a20c72018-10-09 12:30:51 -0700562 const std::string outName = "_hidl_out_" + elidedReturn->name();
563
564 out << elidedReturn->type().getCppResultType() << " " << outName
565 << " = _hidl_return;\n";
566 out << "(void) " << outName << ";\n";
567
568 const std::string wrappedName =
569 wrapPassthroughArg(out, elidedReturn, outName, kHandlePassthroughError);
570
571 if (outName != wrappedName) {
572 // update the original value since it is used by generateCppInstrumentationCall
573 out << outName << " = " << wrappedName << ";\n\n";
574
575 // update the value to be returned
576 out << "_hidl_return = " << outName << "\n;";
577 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800578 }
Steven Moreland92a08a72017-07-31 14:57:37 -0700579 generateCppInstrumentationCall(
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800580 out,
581 InstrumentationEvent::PASSTHROUGH_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -0700582 method,
583 superInterface);
Steven Moreland69e7c702016-09-09 11:16:32 -0700584 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700585
586 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700587 out.unindent();
588 out << "});\n";
Steven Moreland58a20c72018-10-09 12:30:51 -0700589 } else {
590 out << "if (!_hidl_error.isOk()) return _hidl_error;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700591 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700592
593 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700594
595 out.unindent();
596 out << "}\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700597}
598
Steven Moreland368e4602018-02-16 14:21:49 -0800599void AST::generateMethods(Formatter& out, const MethodGenerator& gen, bool includeParent) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700600 const Interface* iface = mRootScope.getInterface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700601
Yifan Hong10fe0b52016-10-19 14:20:17 -0700602 const Interface *prevIterface = nullptr;
603 for (const auto &tuple : iface->allMethodsFromRoot()) {
604 const Method *method = tuple.method();
605 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700606
Steven Morelandf16c5c02017-07-31 16:50:06 -0700607 if (!includeParent && superInterface != iface) {
608 continue;
609 }
610
Yifan Hong10fe0b52016-10-19 14:20:17 -0700611 if(prevIterface != superInterface) {
612 if (prevIterface != nullptr) {
613 out << "\n";
614 }
615 out << "// Methods from "
616 << superInterface->fullName()
617 << " follow.\n";
618 prevIterface = superInterface;
619 }
Steven Moreland368e4602018-02-16 14:21:49 -0800620 gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700621 }
622
Yifan Hong10fe0b52016-10-19 14:20:17 -0700623 out << "\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700624}
625
Steven Moreland0b843772017-06-23 16:33:38 -0700626void AST::generateTemplatizationLink(Formatter& out) const {
Neel Mehta291d02e2019-06-06 17:51:07 -0700627 DocComment("The pure class is what this class wraps.", HIDL_LOCATION_HERE).emit(out);
Neel Mehta257cce02019-07-19 13:24:57 -0700628 out << "typedef " << mRootScope.getInterface()->definedName() << " Pure;\n\n";
Steven Moreland0b843772017-06-23 16:33:38 -0700629}
630
Steven Moreland1a52e822017-07-27 13:56:29 -0700631void AST::generateCppTag(Formatter& out, const std::string& tag) const {
632 out << "typedef " << tag << " _hidl_tag;\n\n";
633}
634
Steven Moreland368e4602018-02-16 14:21:49 -0800635void AST::generateStubHeader(Formatter& out) const {
Steven Moreland5abcf012018-02-08 18:50:18 -0800636 CHECK(AST::isInterface());
Andreas Huber881227d2016-08-02 14:20:21 -0700637
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700638 const Interface* iface = mRootScope.getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800639 const std::string klassName = iface->getStubName();
Steven Moreland40786312016-08-16 10:29:40 -0700640 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700641
642 out << "#ifndef " << guard << "\n";
643 out << "#define " << guard << "\n\n";
644
Yifan Hongeefe4f22017-01-04 15:32:42 -0800645 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Moreland1a52e822017-07-27 13:56:29 -0700646
Steven Morelandee88eed2016-10-31 17:49:00 -0700647 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700648
649 enterLeaveNamespace(out, true /* enter */);
650 out << "\n";
651
652 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800653 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100654 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800655 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000656 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100657 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800658 out << " : public "
659 << gIBaseFqName.getInterfaceStubFqName().cppName()
660 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100661 }
Andreas Huber881227d2016-08-02 14:20:21 -0700662
663 out.indent();
Neel Mehta257cce02019-07-19 13:24:57 -0700664 out << "explicit " << klassName << "(const ::android::sp<" << iface->definedName()
665 << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100666 << "\n";
Neel Mehta257cce02019-07-19 13:24:57 -0700667 out << "explicit " << klassName << "(const ::android::sp<" << iface->definedName()
668 << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800669 << " const std::string& HidlInstrumentor_package,"
670 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700671 << "\n\n";
Steven Moreland57a89362017-07-21 19:29:54 +0000672 out << "virtual ~" << klassName << "();\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700673 out << "::android::status_t onTransact(\n";
674 out.indent();
675 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700676 out << "uint32_t _hidl_code,\n";
677 out << "const ::android::hardware::Parcel &_hidl_data,\n";
678 out << "::android::hardware::Parcel *_hidl_reply,\n";
679 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700680 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700681 out.unindent();
682 out.unindent();
683
Steven Moreland0b843772017-06-23 16:33:38 -0700684 out.endl();
685 generateTemplatizationLink(out);
Neel Mehta291d02e2019-06-06 17:51:07 -0700686 DocComment("Type tag for use in template logic that indicates this is a 'native' class.",
687 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700688 .emit(out);
Diogo Ferreira604fe012019-10-17 12:32:00 +0100689 generateCppTag(out, "::android::hardware::details::bnhw_tag");
Steven Moreland0b843772017-06-23 16:33:38 -0700690
Neel Mehta257cce02019-07-19 13:24:57 -0700691 out << "::android::sp<" << iface->definedName() << "> getImpl() { return _hidl_mImpl; }\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -0700692
Steven Moreland173c4c92019-05-20 10:46:27 -0700693 // Because the Bn class hierarchy always inherits from BnHwBase (and no other parent classes)
694 // and also no HIDL-specific things exist in the base binder classes, whenever we want to do
695 // C++ HIDL things with a binder, we only have the choice to convert it into a BnHwBase.
696 // Other hwbinder C++ class hierarchies (namely the one used for Java binder) will still
697 // be libhwbinder binders, but they are not instances of BnHwBase.
698 if (isIBase()) {
699 out << "bool checkSubclass(const void* subclassID) const;\n";
700 }
701
Steven Moreland368e4602018-02-16 14:21:49 -0800702 generateMethods(out,
703 [&](const Method* method, const Interface*) {
704 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
705 return;
706 }
Steven Morelandf16c5c02017-07-31 16:50:06 -0700707
Steven Moreland368e4602018-02-16 14:21:49 -0800708 out << "static ::android::status_t _hidl_" << method->name() << "(\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -0700709
Steven Moreland368e4602018-02-16 14:21:49 -0800710 out.indent(2,
711 [&] {
712 out << "::android::hidl::base::V1_0::BnHwBase* _hidl_this,\n"
713 << "const ::android::hardware::Parcel &_hidl_data,\n"
714 << "::android::hardware::Parcel *_hidl_reply,\n"
715 << "TransactCallback _hidl_cb);\n";
716 })
717 .endl()
718 .endl();
719 },
720 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -0700721
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100722 out.unindent();
723 out << "private:\n";
724 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800725
Steven Moreland368e4602018-02-16 14:21:49 -0800726 generateMethods(out, [&](const Method* method, const Interface* iface) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800727 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
Steven Moreland368e4602018-02-16 14:21:49 -0800728 return;
Yifan Hongcd2ae452017-01-31 14:33:40 -0800729 }
730 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700731 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800732
733 if (elidedReturn == nullptr && returnsValue) {
734 out << "using " << method->name() << "_cb = "
735 << iface->fqName().cppName()
736 << "::" << method->name() << "_cb;\n";
737 }
738 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800739 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800740 });
Yifan Hongcd2ae452017-01-31 14:33:40 -0800741
Neel Mehta257cce02019-07-19 13:24:57 -0700742 out << "::android::sp<" << iface->definedName() << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700743 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700744 out << "};\n\n";
745
746 enterLeaveNamespace(out, false /* enter */);
747
748 out << "\n#endif // " << guard << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700749}
750
Steven Moreland368e4602018-02-16 14:21:49 -0800751void AST::generateProxyHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700752 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700753 // types.hal does not get a proxy header.
Steven Moreland368e4602018-02-16 14:21:49 -0800754 return;
Andreas Huber881227d2016-08-02 14:20:21 -0700755 }
756
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700757 const Interface* iface = mRootScope.getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800758 const std::string proxyName = iface->getProxyName();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800759 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700760
761 out << "#ifndef " << guard << "\n";
762 out << "#define " << guard << "\n\n";
763
Martijn Coenen115d4282016-12-19 05:14:04 +0100764 out << "#include <hidl/HidlTransportSupport.h>\n\n";
765
Yifan Hongeefe4f22017-01-04 15:32:42 -0800766 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700767 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700768
769 enterLeaveNamespace(out, true /* enter */);
770 out << "\n";
771
Neel Mehta257cce02019-07-19 13:24:57 -0700772 out << "struct " << proxyName << " : public ::android::hardware::BpInterface<"
773 << iface->definedName() << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700774
775 out.indent();
776
Yifan Hongeefe4f22017-01-04 15:32:42 -0800777 out << "explicit "
778 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700779 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700780 << "\n\n";
781
Steven Moreland0b843772017-06-23 16:33:38 -0700782 generateTemplatizationLink(out);
Neel Mehta291d02e2019-06-06 17:51:07 -0700783 DocComment("Type tag for use in template logic that indicates this is a 'proxy' class.",
784 HIDL_LOCATION_HERE)
Steven Moreland7645fbd2019-03-12 18:49:28 -0700785 .emit(out);
Diogo Ferreira604fe012019-10-17 12:32:00 +0100786 generateCppTag(out, "::android::hardware::details::bphw_tag");
Steven Moreland0b843772017-06-23 16:33:38 -0700787
Yifan Hong10fe0b52016-10-19 14:20:17 -0700788 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700789
Steven Moreland596d20e2019-06-07 11:52:21 -0700790 out << "void onLastStrongRef(const void* id) override;\n\n";
791
Steven Moreland368e4602018-02-16 14:21:49 -0800792 generateMethods(
793 out,
794 [&](const Method* method, const Interface*) {
795 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
796 return;
797 }
Steven Morelandf16c5c02017-07-31 16:50:06 -0700798
Steven Moreland368e4602018-02-16 14:21:49 -0800799 out << "static ";
800 method->generateCppReturnType(out);
801 out << " _hidl_" << method->name() << "("
802 << "::android::hardware::IInterface* _hidl_this, "
803 << "::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor";
Steven Morelandf16c5c02017-07-31 16:50:06 -0700804
Steven Moreland368e4602018-02-16 14:21:49 -0800805 if (!method->hasEmptyCppArgSignature()) {
806 out << ", ";
807 }
808 method->emitCppArgSignature(out);
809 out << ");\n";
810 },
811 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -0700812
Steven Moreland368e4602018-02-16 14:21:49 -0800813 generateMethods(out, [&](const Method* method, const Interface*) {
Yifan Hong068c5522016-10-31 14:07:25 -0700814 method->generateCppSignature(out);
815 out << " override;\n";
Yifan Hong068c5522016-10-31 14:07:25 -0700816 });
Steven Moreland9c387612016-09-07 09:54:26 -0700817
Andreas Huber881227d2016-08-02 14:20:21 -0700818 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100819 out << "private:\n";
820 out.indent();
821 out << "std::mutex _hidl_mMutex;\n"
822 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
823 << " _hidl_mDeathRecipients;\n";
824 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700825 out << "};\n\n";
826
827 enterLeaveNamespace(out, false /* enter */);
828
829 out << "\n#endif // " << guard << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700830}
831
Steven Moreland368e4602018-02-16 14:21:49 -0800832void AST::generateCppSource(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700833 std::string baseName = getBaseName();
834 const Interface *iface = getInterface();
Andreas Huber881227d2016-08-02 14:20:21 -0700835
Steven Morelanda885d252017-09-25 18:44:43 -0700836 const std::string klassName = baseName + (baseName == "types" ? "" : "All");
Andreas Huber881227d2016-08-02 14:20:21 -0700837
Steven Moreland623c0042017-01-13 14:42:29 -0800838 out << "#define LOG_TAG \""
839 << mPackage.string() << "::" << baseName
840 << "\"\n\n";
841
Steven Moreland5add34d2018-11-08 16:31:30 -0800842 out << "#include <log/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100843 out << "#include <cutils/trace.h>\n";
844 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Steven Moreland26896a92018-07-31 15:31:01 -0700845 out << "#include <hidl/Static.h>\n";
846 out << "#include <hwbinder/ProcessState.h>\n";
Steven Moreland4607ef52018-05-09 10:52:47 -0700847 out << "#include <utils/Trace.h>\n";
Steven Moreland19f11b52017-05-12 18:22:21 -0700848 if (iface) {
Steven Moreland19d5c172016-10-20 19:20:25 -0700849 // This is a no-op for IServiceManager itself.
850 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
851
Yifan Hongeefe4f22017-01-04 15:32:42 -0800852 generateCppPackageInclude(out, mPackage, iface->getProxyName());
853 generateCppPackageInclude(out, mPackage, iface->getStubName());
854 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -0700855
856 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700857 generateCppPackageInclude(out,
858 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -0800859 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -0700860 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -0800861
862 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700863 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -0700864 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -0800865 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -0700866 }
867
868 out << "\n";
869
870 enterLeaveNamespace(out, true /* enter */);
871 out << "\n";
872
Neel Mehta257cce02019-07-19 13:24:57 -0700873 generateTypeSource(out, iface ? iface->definedName() : "");
Andreas Huber881227d2016-08-02 14:20:21 -0700874
Steven Moreland368e4602018-02-16 14:21:49 -0800875 if (iface) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700876 const Interface* iface = mRootScope.getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700877
878 // need to be put here, generateStubSource is using this.
Neel Mehta257cce02019-07-19 13:24:57 -0700879 out << "const char* " << iface->definedName() << "::descriptor(\""
880 << iface->fqName().string() << "\");\n\n";
Yifan Hong91977fd2017-11-09 16:07:37 -0800881 out << "__attribute__((constructor)) ";
Martijn Coenen8adcb652017-02-03 17:37:36 +0100882 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800883 out.indent([&] {
Yifan Hong91977fd2017-11-09 16:07:37 -0800884 out << "::android::hardware::details::getBnConstructorMap().set("
Neel Mehta257cce02019-07-19 13:24:57 -0700885 << iface->definedName() << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800886 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -0800887 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800888 out.indent([&] {
Neel Mehta257cce02019-07-19 13:24:57 -0700889 out << "return new " << iface->getStubName() << "(static_cast<"
890 << iface->definedName() << " *>(iIntf));\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800891 });
Yifan Hongb04de382017-02-06 15:31:52 -0800892 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800893 });
Yifan Hong91977fd2017-11-09 16:07:37 -0800894 out << "::android::hardware::details::getBsConstructorMap().set("
Neel Mehta257cce02019-07-19 13:24:57 -0700895 << iface->definedName() << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800896 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -0800897 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -0800898 << gIBaseFqName.cppName()
899 << "> {\n";
900 out.indent([&] {
Neel Mehta257cce02019-07-19 13:24:57 -0700901 out << "return new " << iface->getPassthroughName() << "(static_cast<"
902 << iface->definedName() << " *>(iIntf));\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800903 });
Yifan Hongb04de382017-02-06 15:31:52 -0800904 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800905 });
Yifan Hong158655a2016-11-08 12:34:07 -0800906 });
Bernhard Rosenkränzera8be3492018-04-15 22:32:41 +0200907 out << "}\n\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +0100908 out << "__attribute__((destructor))";
909 out << "static void static_destructor() {\n";
910 out.indent([&] {
Yifan Hong91977fd2017-11-09 16:07:37 -0800911 out << "::android::hardware::details::getBnConstructorMap().erase("
Neel Mehta257cce02019-07-19 13:24:57 -0700912 << iface->definedName() << "::descriptor);\n";
Yifan Hong91977fd2017-11-09 16:07:37 -0800913 out << "::android::hardware::details::getBsConstructorMap().erase("
Neel Mehta257cce02019-07-19 13:24:57 -0700914 << iface->definedName() << "::descriptor);\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +0100915 });
Bernhard Rosenkränzera8be3492018-04-15 22:32:41 +0200916 out << "}\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800917
Steven Moreland368e4602018-02-16 14:21:49 -0800918 generateInterfaceSource(out);
919 generateProxySource(out, iface->fqName());
920 generateStubSource(out, iface);
921 generatePassthroughSource(out);
Steven Moreland9c387612016-09-07 09:54:26 -0700922
Yifan Hongc8934042016-11-17 17:10:52 -0800923 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800924 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800925 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800926 std::string package = iface->fqName().package()
927 + iface->fqName().atVersion();
928
Yifan Hongeefe4f22017-01-04 15:32:42 -0800929 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -0800930 }
Steven Moreland40786312016-08-16 10:29:40 -0700931 }
932
Andreas Huber6755e9d2017-04-06 11:09:07 -0700933 HidlTypeAssertion::EmitAll(out);
934 out << "\n";
935
Andreas Huber881227d2016-08-02 14:20:21 -0700936 enterLeaveNamespace(out, false /* enter */);
Andreas Huber881227d2016-08-02 14:20:21 -0700937}
938
Steven Moreland368e4602018-02-16 14:21:49 -0800939void AST::generateTypeSource(Formatter& out, const std::string& ifaceName) const {
940 mRootScope.emitTypeDefinitions(out, ifaceName);
Andreas Huber881227d2016-08-02 14:20:21 -0700941}
942
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700943void AST::declareCppReaderLocals(Formatter& out, const std::vector<NamedReference<Type>*>& args,
944 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700945 if (args.empty()) {
946 return;
947 }
948
949 for (const auto &arg : args) {
950 const Type &type = arg->type();
951
Yifan Hong3b320f82016-11-01 15:15:54 -0700952 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -0700953 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -0700954 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -0700955 << ";\n";
956 }
957
958 out << "\n";
959}
960
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700961void AST::emitCppReaderWriter(Formatter& out, const std::string& parcelObj, bool parcelObjIsPointer,
962 const NamedReference<Type>* arg, bool isReader, Type::ErrorMode mode,
963 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700964 const Type &type = arg->type();
965
Andreas Huber881227d2016-08-02 14:20:21 -0700966 type.emitReaderWriter(
967 out,
Andreas Huber5e44a292016-09-27 14:52:39 -0700968 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -0700969 parcelObj,
970 parcelObjIsPointer,
971 isReader,
972 mode);
973}
974
Steven Moreland368e4602018-02-16 14:21:49 -0800975void AST::generateProxyMethodSource(Formatter& out, const std::string& klassName,
976 const Method* method, const Interface* superInterface) const {
Yifan Hong068c5522016-10-31 14:07:25 -0700977 method->generateCppSignature(out,
978 klassName,
979 true /* specify namespaces */);
980
Martijn Coenen115d4282016-12-19 05:14:04 +0100981 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
Steven Morelandf16c5c02017-07-31 16:50:06 -0700982 out.block([&] {
983 method->cppImpl(IMPL_PROXY, out);
984 }).endl().endl();
Steven Moreland368e4602018-02-16 14:21:49 -0800985 return;
Martijn Coenen115d4282016-12-19 05:14:04 +0100986 }
987
Steven Morelandf16c5c02017-07-31 16:50:06 -0700988 out.block([&] {
989 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700990 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Morelandf16c5c02017-07-31 16:50:06 -0700991
992 method->generateCppReturnType(out);
993
994 out << " _hidl_out = "
995 << superInterface->fqName().cppNamespace()
996 << "::"
997 << superInterface->getProxyName()
998 << "::_hidl_"
999 << method->name()
1000 << "(this, this";
1001
1002 if (!method->hasEmptyCppArgSignature()) {
1003 out << ", ";
1004 }
1005
1006 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
1007 out << arg->name();
1008 });
1009
1010 if (returnsValue && elidedReturn == nullptr) {
1011 if (!method->args().empty()) {
1012 out << ", ";
1013 }
1014 out << "_hidl_cb";
1015 }
1016
1017 out << ");\n\n";
1018
1019 out << "return _hidl_out;\n";
1020 }).endl().endl();
Steven Morelandf16c5c02017-07-31 16:50:06 -07001021}
1022
Steven Moreland368e4602018-02-16 14:21:49 -08001023void AST::generateStaticProxyMethodSource(Formatter& out, const std::string& klassName,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001024 const Method* method, const Interface* superInterface) const {
Steven Morelandf16c5c02017-07-31 16:50:06 -07001025 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
Steven Moreland368e4602018-02-16 14:21:49 -08001026 return;
Steven Morelandf16c5c02017-07-31 16:50:06 -07001027 }
1028
1029 method->generateCppReturnType(out);
1030
1031 out << klassName
1032 << "::_hidl_"
1033 << method->name()
1034 << "("
1035 << "::android::hardware::IInterface *_hidl_this, "
1036 << "::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor";
1037
1038 if (!method->hasEmptyCppArgSignature()) {
1039 out << ", ";
1040 }
1041
1042 method->emitCppArgSignature(out);
1043 out << ") {\n";
1044
1045 out.indent();
1046
1047 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
1048 out << "bool mEnableInstrumentation = _hidl_this_instrumentor->isInstrumentationEnabled();\n";
1049 out << "const auto &mInstrumentationCallbacks = _hidl_this_instrumentor->getInstrumentationCallbacks();\n";
1050 out << "#else\n";
1051 out << "(void) _hidl_this_instrumentor;\n";
1052 out << "#endif // __ANDROID_DEBUGGABLE__\n";
1053
1054 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -07001055 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland48cc6042019-04-30 11:28:56 -07001056 const bool hasCallback = returnsValue && elidedReturn == nullptr;
1057
Steven Moreland92a08a72017-07-31 14:57:37 -07001058 generateCppInstrumentationCall(
Yifan Hong068c5522016-10-31 14:07:25 -07001059 out,
1060 InstrumentationEvent::CLIENT_API_ENTRY,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001061 method,
1062 superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001063
1064 out << "::android::hardware::Parcel _hidl_data;\n";
1065 out << "::android::hardware::Parcel _hidl_reply;\n";
1066 out << "::android::status_t _hidl_err;\n";
Steven Moreland48cc6042019-04-30 11:28:56 -07001067 out << "::android::status_t _hidl_transact_err;\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001068 out << "::android::hardware::Status _hidl_status;\n\n";
1069
Steven Moreland48cc6042019-04-30 11:28:56 -07001070 if (!hasCallback) {
1071 declareCppReaderLocals(
1072 out, method->results(), true /* forResults */);
1073 }
Yifan Hong068c5522016-10-31 14:07:25 -07001074
1075 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001076 out << klassName;
Yifan Hong068c5522016-10-31 14:07:25 -07001077 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001078 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1079
Martijn Coenenfff73352017-01-04 16:36:31 +01001080 bool hasInterfaceArgument = false;
Steven Moreland8249f0a2019-05-28 17:25:27 -07001081
Yifan Hong068c5522016-10-31 14:07:25 -07001082 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001083 if (arg->type().isInterface()) {
1084 hasInterfaceArgument = true;
1085 }
Yifan Hong068c5522016-10-31 14:07:25 -07001086 emitCppReaderWriter(
1087 out,
1088 "_hidl_data",
1089 false /* parcelObjIsPointer */,
1090 arg,
1091 false /* reader */,
1092 Type::ErrorMode_Goto,
1093 false /* addPrefixToName */);
1094 }
1095
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001096 if (hasInterfaceArgument) {
1097 // Start binder threadpool to handle incoming transactions
1098 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1099 }
Steven Moreland48cc6042019-04-30 11:28:56 -07001100 out << "_hidl_transact_err = ::android::hardware::IInterface::asBinder(_hidl_this)->transact("
Yifan Hong068c5522016-10-31 14:07:25 -07001101 << method->getSerialId()
1102 << " /* "
1103 << method->name()
1104 << " */, _hidl_data, &_hidl_reply";
1105
1106 if (method->isOneway()) {
Steven Moreland77943692018-08-09 12:53:42 -07001107 out << ", " << Interface::FLAG_ONE_WAY->cppValue();
Steven Moreland48cc6042019-04-30 11:28:56 -07001108 } else {
1109 out << ", 0";
Yifan Hong068c5522016-10-31 14:07:25 -07001110 }
Yifan Hong068c5522016-10-31 14:07:25 -07001111
Steven Moreland48cc6042019-04-30 11:28:56 -07001112 if (hasCallback) {
1113 out << ", [&] (::android::hardware::Parcel& _hidl_reply) {\n";
1114 out.indent();
1115 declareCppReaderLocals(
1116 out, method->results(), true /* forResults */);
1117 out.endl();
1118 } else {
1119 out << ");\n";
1120 out << "if (_hidl_transact_err != ::android::OK) \n";
1121 out.block([&] {
1122 out << "_hidl_err = _hidl_transact_err;\n";
1123 out << "goto _hidl_error;\n";
1124 }).endl().endl();
1125 }
Yifan Hong068c5522016-10-31 14:07:25 -07001126
1127 if (!method->isOneway()) {
Steven Moreland48cc6042019-04-30 11:28:56 -07001128 Type::ErrorMode errorMode = hasCallback ? Type::ErrorMode_ReturnNothing : Type::ErrorMode_Goto;
Yifan Hong068c5522016-10-31 14:07:25 -07001129
Steven Moreland48cc6042019-04-30 11:28:56 -07001130 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
1131 Type::handleError(out, errorMode);
1132
1133 if (hasCallback) {
1134 out << "if (!_hidl_status.isOk()) { return; }\n\n";
1135 } else {
1136 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1137 }
Yifan Hong068c5522016-10-31 14:07:25 -07001138
Yifan Hong068c5522016-10-31 14:07:25 -07001139 for (const auto &arg : method->results()) {
1140 emitCppReaderWriter(
1141 out,
1142 "_hidl_reply",
1143 false /* parcelObjIsPointer */,
1144 arg,
1145 true /* reader */,
Steven Moreland48cc6042019-04-30 11:28:56 -07001146 errorMode,
Yifan Hong068c5522016-10-31 14:07:25 -07001147 true /* addPrefixToName */);
1148 }
1149
Yifan Hong068c5522016-10-31 14:07:25 -07001150 if (returnsValue && elidedReturn == nullptr) {
1151 out << "_hidl_cb(";
1152
Yifan Hong932464e2017-03-30 15:40:22 -07001153 out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) {
Yifan Hong068c5522016-10-31 14:07:25 -07001154 if (arg->type().resultNeedsDeref()) {
1155 out << "*";
1156 }
1157 out << "_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001158 });
Yifan Hong068c5522016-10-31 14:07:25 -07001159
1160 out << ");\n\n";
1161 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001162 }
Steven Morelandf16c5c02017-07-31 16:50:06 -07001163
Steven Moreland92a08a72017-07-31 14:57:37 -07001164 generateCppInstrumentationCall(
Martijn Coenen7b295242016-11-04 16:52:56 +01001165 out,
1166 InstrumentationEvent::CLIENT_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001167 method,
1168 superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001169
Steven Moreland48cc6042019-04-30 11:28:56 -07001170 if (hasCallback) {
1171 out.unindent();
1172 out << "});\n";
1173 out << "if (_hidl_transact_err != ::android::OK) ";
1174 out.block([&] {
1175 out << "_hidl_err = _hidl_transact_err;\n";
1176 out << "goto _hidl_error;\n";
1177 }).endl().endl();
1178 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n";
1179 }
1180
Yifan Hong068c5522016-10-31 14:07:25 -07001181 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001182 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001183 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001184 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1185 } else {
Yifan Hong068c5522016-10-31 14:07:25 -07001186 out << "return ::android::hardware::Return<void>();\n\n";
1187 }
1188
1189 out.unindent();
1190 out << "_hidl_error:\n";
1191 out.indent();
1192 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1193 out << "return ::android::hardware::Return<";
1194 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001195 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001196 } else {
1197 out << "void";
1198 }
1199 out << ">(_hidl_status);\n";
1200
1201 out.unindent();
1202 out << "}\n\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001203}
1204
Steven Moreland368e4602018-02-16 14:21:49 -08001205void AST::generateProxySource(Formatter& out, const FQName& fqName) const {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001206 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001207
1208 out << klassName
1209 << "::"
1210 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001211 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001212
1213 out.indent();
1214 out.indent();
1215
1216 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001217 << "<"
1218 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001219 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001220 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001221 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001222 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001223 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001224 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001225
Andreas Huber881227d2016-08-02 14:20:21 -07001226 out.unindent();
1227 out.unindent();
1228 out << "}\n\n";
1229
Steven Moreland596d20e2019-06-07 11:52:21 -07001230 out << "void " << klassName << "::onLastStrongRef(const void* id) ";
1231 out.block([&] {
1232 out.block([&] {
1233 // if unlinkToDeath is not used, remove strong cycle between
1234 // this and hidl_binder_death_recipient
1235 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n";
1236 out << "_hidl_mDeathRecipients.clear();\n";
1237 }).endl().endl();
1238
1239 out << "BpInterface<" << fqName.getInterfaceName() << ">::onLastStrongRef(id);\n";
1240 }).endl();
1241
Steven Moreland368e4602018-02-16 14:21:49 -08001242 generateMethods(out,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001243 [&](const Method* method, const Interface* superInterface) {
1244 generateStaticProxyMethodSource(out, klassName, method, superInterface);
Steven Moreland368e4602018-02-16 14:21:49 -08001245 },
1246 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -07001247
Steven Moreland368e4602018-02-16 14:21:49 -08001248 generateMethods(out, [&](const Method* method, const Interface* superInterface) {
1249 generateProxyMethodSource(out, klassName, method, superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001250 });
Andreas Huber881227d2016-08-02 14:20:21 -07001251}
1252
Steven Moreland368e4602018-02-16 14:21:49 -08001253void AST::generateStubSource(Formatter& out, const Interface* iface) const {
Neel Mehta257cce02019-07-19 13:24:57 -07001254 const std::string interfaceName = iface->definedName();
Yifan Hongeefe4f22017-01-04 15:32:42 -08001255 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001256
Steven Moreland40786312016-08-16 10:29:40 -07001257 out << klassName
1258 << "::"
1259 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001260 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001261
1262 out.indent();
1263 out.indent();
1264
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001265 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001266 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001267 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001268 out << ": "
1269 << gIBaseFqName.getInterfaceStubFqName().cppName()
1270 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001271 }
1272
1273 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001274 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001275 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001276 << "\") { \n";
1277 out.indent();
1278 out << "_hidl_mImpl = _hidl_impl;\n";
Steven Moreland0e0935c2019-09-18 12:05:36 -07001279 out << "auto prio = ::android::hardware::getMinSchedulerPolicy(_hidl_impl);\n";
Martijn Coenenb4d77952017-05-03 13:44:29 -07001280 out << "mSchedPolicy = prio.sched_policy;\n";
1281 out << "mSchedPriority = prio.prio;\n";
Steven Moreland0e0935c2019-09-18 12:05:36 -07001282 out << "setRequestingSid(::android::hardware::getRequestingSid(_hidl_impl));\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001283 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001284
1285 out.unindent();
1286 out.unindent();
1287 out << "}\n\n";
1288
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001289 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001290 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001291 // class properly.
1292 out << klassName
1293 << "::"
1294 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001295 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1296 << " const std::string &HidlInstrumentor_package,"
1297 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001298
1299 out.indent();
1300 out.indent();
1301
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001302 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001303 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001304 out.indent();
1305 out << "_hidl_mImpl = _hidl_impl;\n";
1306 out.unindent();
1307
1308 out.unindent();
1309 out.unindent();
1310 out << "}\n\n";
1311 }
1312
Steven Moreland57a89362017-07-21 19:29:54 +00001313 out << klassName << "::~" << klassName << "() ";
1314 out.block([&]() {
Steven Morelandbd984412019-04-22 10:25:46 -07001315 out << "::android::hardware::details::gBnMap->eraseIfEqual(_hidl_mImpl.get(), this);\n";
1316 })
1317 .endl()
1318 .endl();
Steven Moreland57a89362017-07-21 19:29:54 +00001319
Steven Moreland173c4c92019-05-20 10:46:27 -07001320 if (isIBase()) {
1321 out << "bool " << klassName << "::checkSubclass(const void* subclassID) const ";
1322 out.block([&] { out << "return subclassID == " << interfaceName << "::descriptor;\n"; });
1323 }
1324
Steven Moreland368e4602018-02-16 14:21:49 -08001325 generateMethods(out,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001326 [&](const Method* method, const Interface* superInterface) {
1327 return generateStaticStubMethodSource(out, iface->fqName(), method, superInterface);
Steven Moreland368e4602018-02-16 14:21:49 -08001328 },
1329 false /* include parents */);
Steven Morelandf16c5c02017-07-31 16:50:06 -07001330
Steven Moreland368e4602018-02-16 14:21:49 -08001331 generateMethods(out, [&](const Method* method, const Interface*) {
Yifan Hongbcffce22017-02-01 15:52:06 -08001332 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
Steven Moreland368e4602018-02-16 14:21:49 -08001333 return;
Yifan Hongbcffce22017-02-01 15:52:06 -08001334 }
1335 method->generateCppSignature(out, iface->getStubName());
1336 out << " ";
1337 out.block([&] {
1338 method->cppImpl(IMPL_STUB_IMPL, out);
1339 }).endl();
Yifan Hongbcffce22017-02-01 15:52:06 -08001340 });
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001341
Andreas Huber881227d2016-08-02 14:20:21 -07001342 out << "::android::status_t " << klassName << "::onTransact(\n";
1343
1344 out.indent();
1345 out.indent();
1346
Iliyan Malchev549e2592016-08-10 08:59:12 -07001347 out << "uint32_t _hidl_code,\n"
1348 << "const ::android::hardware::Parcel &_hidl_data,\n"
1349 << "::android::hardware::Parcel *_hidl_reply,\n"
1350 << "uint32_t _hidl_flags,\n"
1351 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001352
1353 out.unindent();
1354
Iliyan Malchev549e2592016-08-10 08:59:12 -07001355 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001356 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001357 out.indent();
1358
Yifan Hong10fe0b52016-10-19 14:20:17 -07001359 for (const auto &tuple : iface->allMethodsFromRoot()) {
1360 const Method *method = tuple.method();
1361 const Interface *superInterface = tuple.interface();
Steven Morelandf16c5c02017-07-31 16:50:06 -07001362
Howard Chen71f289f2017-08-29 17:35:01 +08001363 if (!isIBase() && method->isHidlReserved()) {
1364 continue;
1365 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001366 out << "case "
1367 << method->getSerialId()
1368 << " /* "
1369 << method->name()
1370 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001371
Yifan Hong10fe0b52016-10-19 14:20:17 -07001372 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001373
Steven Moreland368e4602018-02-16 14:21:49 -08001374 generateStubSourceForMethod(out, method, superInterface);
Yifan Hong10fe0b52016-10-19 14:20:17 -07001375
1376 out.unindent();
1377 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001378 }
1379
1380 out << "default:\n{\n";
1381 out.indent();
1382
Martijn Coenen225bc922017-06-27 14:39:46 -07001383 if (iface->isIBase()) {
1384 out << "(void)_hidl_flags;\n";
1385 out << "return ::android::UNKNOWN_TRANSACTION;\n";
1386 } else {
1387 out << "return ";
1388 out << gIBaseFqName.getInterfaceStubFqName().cppName();
1389 out << "::onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001390
Martijn Coenen225bc922017-06-27 14:39:46 -07001391 out.indent();
1392 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001393
Martijn Coenen225bc922017-06-27 14:39:46 -07001394 out << "_hidl_code, _hidl_data, _hidl_reply, "
1395 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001396
Martijn Coenen225bc922017-06-27 14:39:46 -07001397 out.unindent();
1398 out.unindent();
1399 }
Andreas Huber881227d2016-08-02 14:20:21 -07001400
1401 out.unindent();
1402 out << "}\n";
1403
1404 out.unindent();
1405 out << "}\n\n";
1406
Yifan Honga018ed52016-12-13 16:35:08 -08001407 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1408 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1409 out.indent(2, [&] {
1410 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1411 out << "_hidl_reply);\n";
1412 });
1413 });
Andreas Huber881227d2016-08-02 14:20:21 -07001414
Iliyan Malchev549e2592016-08-10 08:59:12 -07001415 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001416
1417 out.unindent();
1418 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001419}
1420
Steven Moreland368e4602018-02-16 14:21:49 -08001421void AST::generateStubSourceForMethod(Formatter& out, const Method* method,
1422 const Interface* superInterface) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001423 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1424 method->cppImpl(IMPL_STUB, out);
1425 out << "break;\n";
Steven Moreland368e4602018-02-16 14:21:49 -08001426 return;
Martijn Coenen115d4282016-12-19 05:14:04 +01001427 }
1428
Steven Morelandf16c5c02017-07-31 16:50:06 -07001429 out << "_hidl_err = "
1430 << superInterface->fqName().cppNamespace()
1431 << "::"
1432 << superInterface->getStubName()
1433 << "::_hidl_"
1434 << method->name()
1435 << "(this, _hidl_data, _hidl_reply, _hidl_cb);\n";
1436 out << "break;\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001437}
1438
Steven Moreland368e4602018-02-16 14:21:49 -08001439void AST::generateStaticStubMethodSource(Formatter& out, const FQName& fqName,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001440 const Method* method, const Interface* superInterface) const {
Steven Morelandf16c5c02017-07-31 16:50:06 -07001441 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
Steven Moreland368e4602018-02-16 14:21:49 -08001442 return;
Steven Morelandf16c5c02017-07-31 16:50:06 -07001443 }
1444
Steven Morelandf8197902018-01-30 15:38:37 -08001445 const std::string& klassName = fqName.getInterfaceStubName();
1446
Steven Morelandf16c5c02017-07-31 16:50:06 -07001447 out << "::android::status_t " << klassName << "::_hidl_" << method->name() << "(\n";
1448
1449 out.indent();
1450 out.indent();
1451
1452 out << "::android::hidl::base::V1_0::BnHwBase* _hidl_this,\n"
1453 << "const ::android::hardware::Parcel &_hidl_data,\n"
1454 << "::android::hardware::Parcel *_hidl_reply,\n"
1455 << "TransactCallback _hidl_cb) {\n";
1456
1457 out.unindent();
1458
1459 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
1460 out << "bool mEnableInstrumentation = _hidl_this->isInstrumentationEnabled();\n";
1461 out << "const auto &mInstrumentationCallbacks = _hidl_this->getInstrumentationCallbacks();\n";
1462 out << "#endif // __ANDROID_DEBUGGABLE__\n\n";
1463
1464 out << "::android::status_t _hidl_err = ::android::OK;\n";
1465
Yifan Hongeefe4f22017-01-04 15:32:42 -08001466 out << "if (!_hidl_data.enforceInterface("
Steven Morelandf16c5c02017-07-31 16:50:06 -07001467 << klassName
1468 << "::Pure::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001469
Andreas Huber881227d2016-08-02 14:20:21 -07001470 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001471 out << "_hidl_err = ::android::BAD_TYPE;\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001472 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001473 out.unindent();
1474 out << "}\n\n";
1475
Andreas Huber5e44a292016-09-27 14:52:39 -07001476 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001477
Andreas Huber881227d2016-08-02 14:20:21 -07001478 for (const auto &arg : method->args()) {
1479 emitCppReaderWriter(
1480 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001481 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001482 false /* parcelObjIsPointer */,
1483 arg,
1484 true /* reader */,
Steven Morelandf16c5c02017-07-31 16:50:06 -07001485 Type::ErrorMode_Return,
Andreas Huber5e44a292016-09-27 14:52:39 -07001486 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001487 }
1488
Steven Moreland92a08a72017-07-31 14:57:37 -07001489 generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001490 out,
1491 InstrumentationEvent::SERVER_API_ENTRY,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001492 method,
1493 superInterface);
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001494
Andreas Huber881227d2016-08-02 14:20:21 -07001495 const bool returnsValue = !method->results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -07001496 const NamedReference<Type>* elidedReturn = method->canElideCallback();
Steven Moreland3e787002017-08-16 14:59:54 -07001497
1498 std::string callee;
1499
1500 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL)) {
1501 callee = "_hidl_this";
1502 } else {
Steven Morelandf8197902018-01-30 15:38:37 -08001503 callee = "static_cast<" + fqName.getInterfaceName() + "*>(_hidl_this->getImpl().get())";
Steven Moreland3e787002017-08-16 14:59:54 -07001504 }
Andreas Huber881227d2016-08-02 14:20:21 -07001505
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001506 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001507 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001508 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001509 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001510 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001511 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001512 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001513
Yifan Hong932464e2017-03-30 15:40:22 -07001514 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001515 if (arg->type().resultNeedsDeref()) {
1516 out << "*";
1517 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001518 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001519 });
Andreas Huber881227d2016-08-02 14:20:21 -07001520
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001521 out << ");\n\n";
Steven Moreland30232dc2019-03-05 19:39:10 -08001522
Yifan Hong859e53f2016-11-14 19:08:24 -08001523 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1524 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001525
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001526 elidedReturn->type().emitReaderWriter(
1527 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001528 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001529 "_hidl_reply",
1530 true, /* parcelObjIsPointer */
1531 false, /* isReader */
Steven Moreland70f66252020-01-09 17:26:57 -08001532 Type::ErrorMode_Goto);
1533
1534 out.unindent();
1535 out << "_hidl_error:\n";
1536 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001537
Steven Moreland92a08a72017-07-31 14:57:37 -07001538 generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001539 out,
1540 InstrumentationEvent::SERVER_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001541 method,
1542 superInterface);
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001543
Steven Moreland70f66252020-01-09 17:26:57 -08001544 out << "if (_hidl_err != ::android::OK) { return _hidl_err; }\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001545 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001546 } else {
1547 if (returnsValue) {
1548 out << "bool _hidl_callbackCalled = false;\n\n";
1549 }
Andreas Huber881227d2016-08-02 14:20:21 -07001550
Steven Moreland30232dc2019-03-05 19:39:10 -08001551 out << "::android::hardware::Return<void> _hidl_ret = " << callee << "->" << method->name()
1552 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001553
Yifan Hong932464e2017-03-30 15:40:22 -07001554 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001555 if (arg->type().resultNeedsDeref()) {
1556 out << "*";
1557 }
1558
1559 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001560 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001561
1562 if (returnsValue) {
Yifan Hong932464e2017-03-30 15:40:22 -07001563 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001564 out << ", ";
1565 }
1566
1567 out << "[&](";
1568
Yifan Hong932464e2017-03-30 15:40:22 -07001569 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001570 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001571 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001572
1573 out << ") {\n";
1574 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001575 out << "if (_hidl_callbackCalled) {\n";
1576 out.indent();
1577 out << "LOG_ALWAYS_FATAL(\""
1578 << method->name()
1579 << ": _hidl_cb called a second time, but must be called once.\");\n";
1580 out.unindent();
1581 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001582 out << "_hidl_callbackCalled = true;\n\n";
1583
Yifan Hong859e53f2016-11-14 19:08:24 -08001584 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1585 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001586
1587 for (const auto &arg : method->results()) {
1588 emitCppReaderWriter(
1589 out,
1590 "_hidl_reply",
1591 true /* parcelObjIsPointer */,
1592 arg,
1593 false /* reader */,
Steven Moreland70f66252020-01-09 17:26:57 -08001594 Type::ErrorMode_Goto,
Yifan Honga47eef32016-12-12 10:38:54 -08001595 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001596 }
1597
Steven Moreland70f66252020-01-09 17:26:57 -08001598 if (!method->results().empty()) {
1599 out.unindent();
1600 out << "_hidl_error:\n";
1601 out.indent();
1602 }
1603
Steven Moreland92a08a72017-07-31 14:57:37 -07001604 generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001605 out,
1606 InstrumentationEvent::SERVER_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001607 method,
1608 superInterface);
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001609
Steven Moreland70f66252020-01-09 17:26:57 -08001610 out << "if (_hidl_err != ::android::OK) { return; }\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001611 out << "_hidl_cb(*_hidl_reply);\n";
1612
1613 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001614 out << "});\n\n";
1615 } else {
1616 out << ");\n\n";
Steven Morelandf16c5c02017-07-31 16:50:06 -07001617 out << "(void) _hidl_cb;\n\n";
Steven Moreland92a08a72017-07-31 14:57:37 -07001618 generateCppInstrumentationCall(
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001619 out,
1620 InstrumentationEvent::SERVER_API_EXIT,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001621 method,
1622 superInterface);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001623 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001624
Steven Moreland30232dc2019-03-05 19:39:10 -08001625 out << "_hidl_ret.assertOk();\n";
1626
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001627 if (returnsValue) {
1628 out << "if (!_hidl_callbackCalled) {\n";
1629 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001630 out << "LOG_ALWAYS_FATAL(\""
1631 << method->name()
1632 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001633 out.unindent();
1634 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001635 } else {
1636 out << "::android::hardware::writeToParcel("
1637 << "::android::hardware::Status::ok(), "
1638 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001639 }
Andreas Huber881227d2016-08-02 14:20:21 -07001640 }
1641
Steven Morelandf16c5c02017-07-31 16:50:06 -07001642 out << "return _hidl_err;\n";
1643 out.unindent();
1644 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001645}
1646
Steven Moreland368e4602018-02-16 14:21:49 -08001647void AST::generatePassthroughHeader(Formatter& out) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001648 if (!AST::isInterface()) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001649 // types.hal does not get a stub header.
Steven Moreland368e4602018-02-16 14:21:49 -08001650 return;
Steven Moreland69e7c702016-09-09 11:16:32 -07001651 }
1652
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001653 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -07001654 CHECK(iface != nullptr);
Steven Moreland69e7c702016-09-09 11:16:32 -07001655
Yifan Hongeefe4f22017-01-04 15:32:42 -08001656 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001657
Steven Moreland69e7c702016-09-09 11:16:32 -07001658 const std::string guard = makeHeaderGuard(klassName);
1659
1660 out << "#ifndef " << guard << "\n";
1661 out << "#define " << guard << "\n\n";
1662
Steven Moreland61d3f4b2017-04-28 17:30:38 -07001663 out << "#include <android-base/macros.h>\n";
Yifan Hongb0949432016-12-15 15:32:24 -08001664 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001665 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001666
Neel Mehta257cce02019-07-19 13:24:57 -07001667 generateCppPackageInclude(out, mPackage, iface->definedName());
Steven Morelandee88eed2016-10-31 17:49:00 -07001668 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001669
Yifan Hong7a118f52016-12-07 11:21:15 -08001670 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Neel Mehta19f79792019-05-21 13:39:32 -07001671 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001672
1673 enterLeaveNamespace(out, true /* enter */);
1674 out << "\n";
1675
Neel Mehta257cce02019-07-19 13:24:57 -07001676 out << "struct " << klassName << " : " << iface->definedName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001677 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001678
1679 out.indent();
Neel Mehta257cce02019-07-19 13:24:57 -07001680 out << "explicit " << klassName << "(const ::android::sp<" << iface->definedName()
Steven Moreland69e7c702016-09-09 11:16:32 -07001681 << "> impl);\n";
1682
Steven Moreland0b843772017-06-23 16:33:38 -07001683 out.endl();
1684 generateTemplatizationLink(out);
Diogo Ferreira604fe012019-10-17 12:32:00 +01001685 generateCppTag(out, "::android::hardware::details::bs_tag");
Steven Moreland0b843772017-06-23 16:33:38 -07001686
Steven Moreland616cf4d2018-10-02 13:52:18 -07001687 generateMethods(out, [&](const Method* method, const Interface* superInterface) {
1688 generatePassthroughMethod(out, method, superInterface);
Yifan Hong068c5522016-10-31 14:07:25 -07001689 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001690
Steven Moreland69e7c702016-09-09 11:16:32 -07001691 out.unindent();
1692 out << "private:\n";
1693 out.indent();
Neel Mehta257cce02019-07-19 13:24:57 -07001694 out << "const ::android::sp<" << iface->definedName() << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001695
Neel Mehta19f79792019-05-21 13:39:32 -07001696 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001697
Neel Mehta19f79792019-05-21 13:39:32 -07001698 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001699
Neel Mehta19f79792019-05-21 13:39:32 -07001700 out << "::android::hardware::Return<void> addOnewayTask("
1701 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001702
1703 out.unindent();
1704
1705 out << "};\n\n";
1706
1707 enterLeaveNamespace(out, false /* enter */);
1708
1709 out << "\n#endif // " << guard << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001710}
1711
Steven Moreland368e4602018-02-16 14:21:49 -08001712void AST::generateInterfaceSource(Formatter& out) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001713 const Interface* iface = mRootScope.getInterface();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001714
Yifan Hong2d7126b2016-10-20 15:12:57 -07001715 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001716 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001717
Steven Moreland368e4602018-02-16 14:21:49 -08001718 generateMethods(out, [&](const Method* method, const Interface*) {
Steven Morelandd4b068a2017-03-20 06:30:51 -07001719 bool reserved = method->isHidlReserved();
1720
1721 if (!reserved) {
1722 out << "// no default implementation for: ";
1723 }
Neel Mehta257cce02019-07-19 13:24:57 -07001724 method->generateCppSignature(out, iface->definedName());
Steven Morelandd4b068a2017-03-20 06:30:51 -07001725 if (reserved) {
1726 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001727 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001728 }).endl();
1729 }
1730
1731 out << "\n";
1732
Steven Moreland368e4602018-02-16 14:21:49 -08001733 return;
Steven Morelandd4b068a2017-03-20 06:30:51 -07001734 });
Steven Morelandd4b068a2017-03-20 06:30:51 -07001735
Yifan Hong3d746092016-12-07 14:26:33 -08001736 for (const Interface *superType : iface->typeChain()) {
Neel Mehta257cce02019-07-19 13:24:57 -07001737 out << "::android::hardware::Return<" << childTypeResult << "> " << iface->definedName()
1738 << "::castFrom(" << superType->getCppArgumentType() << " parent, bool "
1739 << (iface == superType ? "/* emitError */" : "emitError") << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001740 out.indent();
1741 if (iface == superType) {
1742 out << "return parent;\n";
1743 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001744 out << "return ::android::hardware::details::castInterface<";
Neel Mehta257cce02019-07-19 13:24:57 -07001745 out << iface->definedName() << ", " << superType->fqName().cppName() << ", "
1746 << iface->getProxyName() << ">(\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001747 out.indent();
1748 out.indent();
1749 out << "parent, \""
1750 << iface->fqName().string()
Yifan Hong200209c2017-03-29 03:39:09 -07001751 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001752 out.unindent();
1753 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001754 }
Yifan Hong3d746092016-12-07 14:26:33 -08001755 out.unindent();
1756 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001757 }
Yifan Hongfe95aa22016-10-19 17:26:45 -07001758}
1759
Steven Moreland368e4602018-02-16 14:21:49 -08001760void AST::generatePassthroughSource(Formatter& out) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001761 const Interface* iface = mRootScope.getInterface();
Steven Moreland69e7c702016-09-09 11:16:32 -07001762
Yifan Hongeefe4f22017-01-04 15:32:42 -08001763 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001764
Neel Mehta19f79792019-05-21 13:39:32 -07001765 out << klassName << "::" << klassName << "(const ::android::sp<" << iface->fullName()
1766 << "> impl) : ::android::hardware::details::HidlInstrumentor(\"" << mPackage.string()
Neel Mehta257cce02019-07-19 13:24:57 -07001767 << "\", \"" << iface->definedName() << "\"), mImpl(impl) {\n";
Neel Mehta19f79792019-05-21 13:39:32 -07001768
1769 out.indent([&] { out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n"; });
1770
Yifan Hong2cbc1472016-10-25 19:02:40 -07001771 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001772
Neel Mehta19f79792019-05-21 13:39:32 -07001773 out << "::android::hardware::Return<void> " << klassName
1774 << "::addOnewayTask(std::function<void(void)> fun) {\n";
1775 out.indent();
1776 out << "if (!mOnewayQueue.push(fun)) {\n";
1777 out.indent();
1778 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1779 out.indent();
1780 out.indent();
1781 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
1782 << "\"Passthrough oneway function queue exceeds maximum size.\");\n";
1783 out.unindent();
1784 out.unindent();
1785 out.unindent();
1786 out << "}\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001787
Neel Mehta19f79792019-05-21 13:39:32 -07001788 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001789
Neel Mehta19f79792019-05-21 13:39:32 -07001790 out.unindent();
1791 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001792}
1793
Steven Moreland92a08a72017-07-31 14:57:37 -07001794void AST::generateCppAtraceCall(Formatter &out,
Martijn Coenen7b295242016-11-04 16:52:56 +01001795 InstrumentationEvent event,
1796 const Method *method) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -07001797 const Interface* iface = mRootScope.getInterface();
Neel Mehta257cce02019-07-19 13:24:57 -07001798 std::string baseString = "HIDL::" + iface->definedName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01001799 switch (event) {
1800 case SERVER_API_ENTRY:
1801 {
1802 out << "atrace_begin(ATRACE_TAG_HAL, \""
1803 << baseString + "::server\");\n";
1804 break;
1805 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001806 case PASSTHROUGH_ENTRY:
1807 {
1808 out << "atrace_begin(ATRACE_TAG_HAL, \""
1809 << baseString + "::passthrough\");\n";
1810 break;
1811 }
1812 case SERVER_API_EXIT:
Martijn Coenen7b295242016-11-04 16:52:56 +01001813 case PASSTHROUGH_EXIT:
1814 {
1815 out << "atrace_end(ATRACE_TAG_HAL);\n";
1816 break;
1817 }
Steven Moreland4607ef52018-05-09 10:52:47 -07001818 // client uses scope because of gotos
1819 // this isn't done for server because the profiled code isn't alone in its scope
1820 // this isn't done for passthrough becuase the profiled boundary isn't even in the same code
1821 case CLIENT_API_ENTRY: {
Michael Butler0a3d99a2018-07-26 13:47:10 -07001822 out << "::android::ScopedTrace PASTE(___tracer, __LINE__) (ATRACE_TAG_HAL, \""
1823 << baseString + "::client\");\n";
Steven Moreland4607ef52018-05-09 10:52:47 -07001824 break;
1825 }
1826 case CLIENT_API_EXIT:
1827 break;
Martijn Coenen7b295242016-11-04 16:52:56 +01001828 default:
1829 {
Steven Morelandcbff5612017-10-11 17:01:54 -07001830 CHECK(false) << "Unsupported instrumentation event: " << event;
Martijn Coenen7b295242016-11-04 16:52:56 +01001831 }
1832 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001833}
1834
Steven Moreland92a08a72017-07-31 14:57:37 -07001835void AST::generateCppInstrumentationCall(
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001836 Formatter &out,
1837 InstrumentationEvent event,
Steven Moreland616cf4d2018-10-02 13:52:18 -07001838 const Method *method,
1839 const Interface* superInterface) const {
Steven Moreland92a08a72017-07-31 14:57:37 -07001840 generateCppAtraceCall(out, event, method);
Martijn Coenen7b295242016-11-04 16:52:56 +01001841
Steven Moreland30b76e92017-06-02 18:52:24 -07001842 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001843 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
1844 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001845 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001846 std::string event_str = "";
1847 switch (event) {
1848 case SERVER_API_ENTRY:
1849 {
1850 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
1851 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001852 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001853 << (arg->type().resultNeedsDeref() ? "" : "&")
1854 << arg->name()
1855 << ");\n";
1856 }
1857 break;
1858 }
1859 case SERVER_API_EXIT:
1860 {
1861 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07001862 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08001863 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07001864 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001865 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001866 }
1867 break;
1868 }
1869 case CLIENT_API_ENTRY:
1870 {
1871 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
1872 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001873 out << "_hidl_args.push_back((void *)&"
1874 << arg->name()
1875 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001876 }
1877 break;
1878 }
1879 case CLIENT_API_EXIT:
1880 {
1881 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
1882 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001883 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001884 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001885 << "_hidl_out_"
1886 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001887 << ");\n";
1888 }
1889 break;
1890 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001891 case PASSTHROUGH_ENTRY:
1892 {
1893 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
1894 for (const auto &arg : method->args()) {
1895 out << "_hidl_args.push_back((void *)&"
1896 << arg->name()
1897 << ");\n";
1898 }
1899 break;
1900 }
1901 case PASSTHROUGH_EXIT:
1902 {
1903 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08001904 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08001905 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08001906 << arg->name()
1907 << ");\n";
1908 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001909 break;
1910 }
Steven Moreland031ccf12016-10-31 15:54:38 -07001911 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001912 {
Steven Morelandcbff5612017-10-11 17:01:54 -07001913 CHECK(false) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001914 }
1915 }
1916
Steven Moreland1ab31442016-11-03 18:37:51 -07001917 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001918 out.indent();
Neel Mehta257cce02019-07-19 13:24:57 -07001919 out << "callback(" << event_str << ", \"" << superInterface->fqName().package() << "\", \""
1920 << superInterface->fqName().version() << "\", \"" << superInterface->definedName()
1921 << "\", \"" << method->name() << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001922 out.unindent();
1923 out << "}\n";
1924 out.unindent();
Steven Moreland30b76e92017-06-02 18:52:24 -07001925 out << "}\n";
1926 out << "#endif // __ANDROID_DEBUGGABLE__\n\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001927}
1928
Andreas Huber881227d2016-08-02 14:20:21 -07001929} // namespace android