blob: 7aa25bf90b3acb90dff46099341c575da52c0719 [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 Huber881227d2016-08-02 14:20:21 -070021#include "Interface.h"
Andreas Huber6755e9d2017-04-06 11:09:07 -070022#include "HidlTypeAssertion.h"
Andreas Huber881227d2016-08-02 14:20:21 -070023#include "Method.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070024#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070025#include "Scope.h"
26
Andreas Huberdca261f2016-08-04 13:47:51 -070027#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland5708edf2016-11-04 15:33:31 +000029#include <hidl-util/StringHelper.h>
Andreas Huber881227d2016-08-02 14:20:21 -070030#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070031#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070032#include <vector>
33
34namespace android {
35
Andreas Huberb82318c2016-08-02 14:45:54 -070036status_t AST::generateCpp(const std::string &outputPath) const {
Steven Moreland1cbf0362017-05-09 14:32:53 -070037 status_t err = generateCppHeaders(outputPath);
38
39 if (err == OK) {
40 err = generateCppSources(outputPath);
41 }
42
43 return err;
44}
45
46status_t AST::generateCppHeaders(const std::string &outputPath) const {
Andreas Huberb82318c2016-08-02 14:45:54 -070047 status_t err = generateInterfaceHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070048
49 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070050 err = generateStubHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070051 }
52
53 if (err == OK) {
Steven Moreland40786312016-08-16 10:29:40 -070054 err = generateHwBinderHeader(outputPath);
55 }
56
57 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070058 err = generateProxyHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070059 }
60
61 if (err == OK) {
Yifan Hong7a118f52016-12-07 11:21:15 -080062 err = generatePassthroughHeader(outputPath);
Steven Moreland69e7c702016-09-09 11:16:32 -070063 }
64
Andreas Huber881227d2016-08-02 14:20:21 -070065 return err;
66}
67
Andreas Huber737080b2016-08-02 15:38:04 -070068void AST::getPackageComponents(
69 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070070 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070071}
72
73void AST::getPackageAndVersionComponents(
74 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070075 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070076}
77
Steven Moreland5708edf2016-11-04 15:33:31 +000078std::string AST::makeHeaderGuard(const std::string &baseName,
79 bool indicateGenerated) const {
80 std::string guard;
Andreas Huber881227d2016-08-02 14:20:21 -070081
Steven Moreland5708edf2016-11-04 15:33:31 +000082 if (indicateGenerated) {
83 guard += "HIDL_GENERATED_";
84 }
85
86 guard += StringHelper::Uppercase(mPackage.tokenName());
Andreas Huber881227d2016-08-02 14:20:21 -070087 guard += "_";
Steven Moreland5708edf2016-11-04 15:33:31 +000088 guard += StringHelper::Uppercase(baseName);
89 guard += "_H";
Andreas Huber881227d2016-08-02 14:20:21 -070090
91 return guard;
92}
93
Steven Morelandee88eed2016-10-31 17:49:00 -070094void AST::generateCppPackageInclude(
95 Formatter &out,
96 const FQName &package,
97 const std::string &klass) {
98
99 out << "#include <";
100
101 std::vector<std::string> components;
102 package.getPackageAndVersionComponents(&components, false /* cpp_compatible */);
103
104 for (const auto &component : components) {
105 out << component << "/";
106 }
107
108 out << klass
109 << ".h>\n";
110}
111
Andreas Huber881227d2016-08-02 14:20:21 -0700112void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
113 std::vector<std::string> packageComponents;
114 getPackageAndVersionComponents(
115 &packageComponents, true /* cpp_compatible */);
116
117 if (enter) {
118 for (const auto &component : packageComponents) {
119 out << "namespace " << component << " {\n";
120 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700121
Andreas Huber2831d512016-08-15 09:33:47 -0700122 out.setNamespace(mPackage.cppNamespace() + "::");
Andreas Huber881227d2016-08-02 14:20:21 -0700123 } else {
Andreas Huber0e00de42016-08-03 09:56:02 -0700124 out.setNamespace(std::string());
125
Andreas Huber881227d2016-08-02 14:20:21 -0700126 for (auto it = packageComponents.rbegin();
127 it != packageComponents.rend();
128 ++it) {
129 out << "} // namespace " << *it << "\n";
130 }
131 }
132}
133
Steven Moreland038903b2017-03-30 12:11:24 -0700134static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
135 const std::string functionName = isTry ? "tryGetService" : "getService";
136
137 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800138 << "const std::string &serviceName=\"default\", bool getStub=false);\n";
Steven Moreland038903b2017-03-30 12:11:24 -0700139 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800140 << "const char serviceName[], bool getStub=false)"
141 << " { std::string str(serviceName ? serviceName : \"\");"
Steven Moreland038903b2017-03-30 12:11:24 -0700142 << " return " << functionName << "(str, getStub); }\n";
143 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800144 << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
145 // without c_str the std::string constructor is ambiguous
146 << " { std::string str(serviceName.c_str());"
Steven Moreland038903b2017-03-30 12:11:24 -0700147 << " return " << functionName << "(str, getStub); }\n";
148 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
149 << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
150}
151
152static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
153 declareGetService(out, interfaceName, true /* isTry */);
154 declareGetService(out, interfaceName, false /* isTry */);
155
Steven Moreland90831502017-03-27 12:08:40 -0700156 out << "__attribute__ ((warn_unused_result))"
157 << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800158 out << "static bool registerForNotifications(\n";
159 out.indent(2, [&] {
160 out << "const std::string &serviceName,\n"
161 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
162 << "&notification);\n";
163 });
164
165}
166
Steven Moreland038903b2017-03-30 12:11:24 -0700167static void implementGetService(Formatter &out,
168 const FQName &fqName,
169 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800170
171 const std::string interfaceName = fqName.getInterfaceName();
Steven Moreland038903b2017-03-30 12:11:24 -0700172 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800173
174 out << "// static\n"
Steven Moreland038903b2017-03-30 12:11:24 -0700175 << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000176 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800177 out.block([&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700178 out << "using ::android::hardware::defaultServiceManager;\n";
179 out << "using ::android::hardware::details::waitForHwService;\n";
180 out << "using ::android::hardware::getPassthroughServiceManager;\n";
181 out << "using ::android::hardware::Return;\n";
182 out << "using ::android::sp;\n";
183 out << "using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;\n\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000184
Steven Morelandbcf51802017-04-06 09:17:44 -0700185 out << "sp<" << interfaceName << "> iface = nullptr;\n";
186
187 out.endl();
188
189 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> sm"
190 << " = defaultServiceManager();\n";
191
192 out.sIf("sm == nullptr", [&] {
193 // hwbinder is not available on this device, so future tries
194 // would also be null. I can only return nullptr.
195 out << "ALOGE(\"getService: defaultServiceManager() is null\");\n"
196 << "return nullptr;\n";
197 }).endl().endl();
198
199 out << "Return<Transport> transportRet = sm->getTransport("
200 << interfaceName << "::descriptor, serviceName);\n\n";
201
202 out.sIf("!transportRet.isOk()", [&] {
203 out << "ALOGE(\"getService: defaultServiceManager()->getTransport returns %s\", "
204 << "transportRet.description().c_str());\n";
205 out << "return nullptr;\n";
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700206 }).endl();
Steven Morelandbcf51802017-04-06 09:17:44 -0700207
208 out << "Transport transport = transportRet;\n";
209 out << "const bool vintfHwbinder = (transport == Transport::HWBINDER);\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700210 << "const bool vintfPassthru = (transport == Transport::PASSTHROUGH);\n\n";
211
212 // This means that you must set TREBLE_TESTING_OVERRIDE when running a test such
213 // as hidl_test. Ideally these binaries set this value themselves. This allows
214 // test modules to dynamically add and unset services even though they are not
215 // declared in the device manifest. This prevents a problem where framework
216 // changes are accidentally made in a way that is not backwards compatible. For
217 // instance, consider the following situation for two devices developed in the
218 // same tree:
219 // A: serves @1.1::IFoo, declares @1.0::IFoo (incorrect)
220 // B: serves @1.0::IFoo, declares @1.0::IFoo (correct configuration)
221 // If development is done on device A, then framework code like: "V1_1::IFoo::
222 // getService()->doV1_0Api()" will work. However, this will unintentionally break
223 // the feature for devices like device B for which "V1_1::IFoo::getService()
224 // will return nullptr. In order to prevent problems like this, we only allow
225 // fetching an interface if it is declared in a VINTF manifest.
226 out << "#ifdef __ANDROID_TREBLE__\n\n"
227 << "#ifdef __ANDROID_DEBUGGABLE__\n"
228 << "const char* env = std::getenv(\"TREBLE_TESTING_OVERRIDE\");\n"
Steven Morelandd281ebc2017-06-20 16:27:35 -0700229 << "const bool trebleTestingOverride = env && !strcmp(env, \"true\");\n"
230 << "const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700231 << "#else // __ANDROID_TREBLE__ but not __ANDROID_DEBUGGABLE__\n"
Steven Morelandd281ebc2017-06-20 16:27:35 -0700232 << "const bool trebleTestingOverride = false;\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700233 << "const bool vintfLegacy = false;\n"
234 << "#endif // __ANDROID_DEBUGGABLE__\n\n"
235 << "#else // not __ANDROID_TREBLE__\n"
Steven Moreland82472102017-06-26 09:40:37 -0700236 << "const char* env = std::getenv(\"TREBLE_TESTING_OVERRIDE\");\n"
237 << "const bool trebleTestingOverride = env && !strcmp(env, \"true\");\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700238 << "const bool vintfLegacy = (transport == Transport::EMPTY);\n\n"
239 << "#endif // __ANDROID_TREBLE__\n\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000240
241 // if (getStub) {
242 // getPassthroughServiceManager()->get only once.
243 // } else {
244 // if (vintfHwbinder) {
245 // while (no alive service) {
Steven Moreland369d0532017-06-09 12:42:10 -0700246 // if (have already tried to get service)
247 // waitForHwService
Yifan Hong31f07ff2017-03-21 18:56:35 +0000248 // defaultServiceManager()->get
249 // }
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700250 // } else if (vintfLegacy) {
Yifan Hong31f07ff2017-03-21 18:56:35 +0000251 // defaultServiceManager()->get only once.
252 // getPassthroughServiceManager()->get only once.
253 // } else if (vintfPassthru) {
254 // getPassthroughServiceManager()->get only once.
255 // }
256 // }
257
Steven Moreland369d0532017-06-09 12:42:10 -0700258 out.sFor("int tries = 0; !getStub && (vintfHwbinder || (vintfLegacy && tries == 0)); tries++", [&] {
Steven Moreland038903b2017-03-30 12:11:24 -0700259 if (!isTry) {
Steven Moreland369d0532017-06-09 12:42:10 -0700260 out.sIf("tries > 1", [&] {
261 // sleep only after the first time we've called waitForHwService.
262 out << "ALOGI(\"" << functionName << ": Will do try %d for %s/%s in 1s...\", tries, "
263 << interfaceName << "::descriptor, serviceName.c_str());\n"
264 << "sleep(1);\n";
265 }).endl();
266
267 out.sIf("vintfHwbinder && tries > 0", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700268 out << "waitForHwService("
269 << interfaceName << "::descriptor, serviceName);\n";
Steven Moreland038903b2017-03-30 12:11:24 -0700270 }).endl();
271 }
Yifan Hong31f07ff2017-03-21 18:56:35 +0000272
Steven Morelandbcf51802017-04-06 09:17:44 -0700273 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000274 out.indent(2, [&] {
275 out << "sm->get(" << interfaceName << "::descriptor, serviceName);\n";
276 });
277
278 out.sIf("!ret.isOk()", [&] {
Steven Moreland42394ce2017-03-27 17:03:04 -0700279 // hwservicemanager fails, may be security issue
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700280 out << "ALOGE(\"" << interfaceName << ": defaultServiceManager()->get returns %s\", "
Yifan Hong31f07ff2017-03-21 18:56:35 +0000281 << "ret.description().c_str());\n"
Steven Moreland42394ce2017-03-27 17:03:04 -0700282 << "break;\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000283 }).endl();
284
Steven Morelandbcf51802017-04-06 09:17:44 -0700285 out << "sp<" << gIBaseFqName.cppName() << "> base = ret;\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700286 out.sIf("base == nullptr", [&] {
Steven Moreland369d0532017-06-09 12:42:10 -0700287 // if tries > 0: race condition. hwservicemanager drops the service
Yifan Hong200209c2017-03-29 03:39:09 -0700288 // from waitForHwService to here
Steven Moreland369d0532017-06-09 12:42:10 -0700289 out.sIf("tries > 0", [&] {
290 out << "ALOGW(\"" << interfaceName << ": found null hwbinder interface\");\n";
291 });
292 out << (isTry ? "break" : "continue")
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700293 << ";\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000294 }).endl();
Steven Morelandbcf51802017-04-06 09:17:44 -0700295 out << "Return<sp<" << interfaceName
Yifan Hong200209c2017-03-29 03:39:09 -0700296 << ">> castRet = " << interfaceName << "::castFrom(base, true /* emitError */);\n";
297 out.sIf("!castRet.isOk()", [&] {
298 out.sIf("castRet.isDeadObject()", [&] {
299 // service is dead (castFrom cannot call interfaceChain)
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700300 out << "ALOGW(\"" << interfaceName << ": found dead hwbinder service\");\n"
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700301 << (isTry ? "break" : "continue")
302 << ";\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700303 }).sElse([&] {
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700304 out << "ALOGW(\"" << interfaceName << ": cannot call into hwbinder service: %s"
Yifan Hong200209c2017-03-29 03:39:09 -0700305 << "; No permission? Check for selinux denials.\", "
306 << "castRet.description().c_str());\n"
307 << "break;\n";
308 }).endl();
309 }).endl();
310 out << "iface = castRet;\n";
311 out.sIf("iface == nullptr", [&] {
312 // returned service isn't of correct type; this is a bug
313 // to hwservicemanager or to the service itself (interfaceChain
314 // is not consistent).
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700315 out << "ALOGW(\"" << interfaceName << ": received incompatible service; bug in hwservicemanager?\");\n"
Yifan Hong200209c2017-03-29 03:39:09 -0700316 << "break;\n";
317 }).endl();
Yifan Hong31f07ff2017-03-21 18:56:35 +0000318
319 out << "return iface;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800320 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800321
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700322 out.sIf("getStub || vintfPassthru || vintfLegacy", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700323 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> pm"
324 << " = getPassthroughServiceManager();\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000325
326 out.sIf("pm != nullptr", [&] () {
Steven Morelandbcf51802017-04-06 09:17:44 -0700327 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Steven Morelandf10af872017-01-25 16:01:56 +0000328 out.indent(2, [&] {
329 out << "pm->get(" << interfaceName << "::descriptor" << ", serviceName);\n";
Steven Moreland2c2dea82017-01-18 17:24:17 -0800330 });
Steven Morelandf10af872017-01-25 16:01:56 +0000331 out.sIf("ret.isOk()", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700332 out << "sp<" << gIBaseFqName.cppName()
Steven Morelandf10af872017-01-25 16:01:56 +0000333 << "> baseInterface = ret;\n";
334 out.sIf("baseInterface != nullptr", [&]() {
Steven Morelandd281ebc2017-06-20 16:27:35 -0700335 out << "iface = " << interfaceName << "::castFrom(baseInterface);\n";
336 out.sIf("!getStub || trebleTestingOverride", [&] () {
337 out << "iface = new " << fqName.getInterfacePassthroughName() << "(iface);\n";
338 }).endl();
Yifan Hong31f07ff2017-03-21 18:56:35 +0000339 }).endl();
Steven Morelandf10af872017-01-25 16:01:56 +0000340 }).endl();
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800341 }).endl();
342 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800343
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800344 out << "return iface;\n";
345 }).endl().endl();
Steven Moreland038903b2017-03-30 12:11:24 -0700346}
347
348static void implementServiceManagerInteractions(Formatter &out,
349 const FQName &fqName, const std::string &package) {
350
351 const std::string interfaceName = fqName.getInterfaceName();
352
353 implementGetService(out, fqName, true /* isTry */);
354 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800355
Yifan Hongeefe4f22017-01-04 15:32:42 -0800356 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800357 << "const std::string &serviceName) ";
358 out.block([&] {
Steven Moreland58b478b2017-04-09 10:54:50 -0700359 out << "::android::hardware::details::onRegistration(\""
360 << fqName.getPackageAndVersion().string() << "\", \""
361 << interfaceName
362 << "\", serviceName);\n\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800363 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
364 out.indent(2, [&] {
365 out << "= ::android::hardware::defaultServiceManager();\n";
366 });
367 out.sIf("sm == nullptr", [&] {
368 out << "return ::android::INVALID_OPERATION;\n";
369 }).endl();
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100370 out << "::android::hardware::Return<bool> ret = "
371 << "sm->add(serviceName.c_str(), this);\n"
372 << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800373 }).endl().endl();
374
Yifan Hongeefe4f22017-01-04 15:32:42 -0800375 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800376 out.indent(2, [&] {
377 out << "const std::string &serviceName,\n"
378 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
379 << "&notification) ";
380 });
381 out.block([&] {
382 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
383 out.indent(2, [&] {
384 out << "= ::android::hardware::defaultServiceManager();\n";
385 });
386 out.sIf("sm == nullptr", [&] {
387 out << "return false;\n";
388 }).endl();
389 out << "::android::hardware::Return<bool> success =\n";
390 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800391 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800392 out.indent(2, [&] {
393 out << "serviceName, notification);\n";
394 });
395 });
396 out << "return success.isOk() && success;\n";
397 }).endl().endl();
398}
399
Andreas Huberb82318c2016-08-02 14:45:54 -0700400status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700401 const Interface *iface = getInterface();
402 std::string ifaceName = iface ? iface->localName() : "types";
Andreas Huber881227d2016-08-02 14:20:21 -0700403
Andreas Huberb82318c2016-08-02 14:45:54 -0700404 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700405 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700406 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700407 path.append(ifaceName);
408 path.append(".h");
409
Andreas Huberd2943e12016-08-05 11:59:31 -0700410 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700411 FILE *file = fopen(path.c_str(), "w");
412
413 if (file == NULL) {
414 return -errno;
415 }
416
417 Formatter out(file);
418
419 const std::string guard = makeHeaderGuard(ifaceName);
420
421 out << "#ifndef " << guard << "\n";
422 out << "#define " << guard << "\n\n";
423
Andreas Huber737080b2016-08-02 15:38:04 -0700424 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700425 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700426 }
427
428 if (!mImportedNames.empty()) {
429 out << "\n";
430 }
431
Steven Moreland19f11b52017-05-12 18:22:21 -0700432 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800433 if (isIBase()) {
434 out << "// skipped #include IServiceNotification.h\n\n";
435 } else {
436 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
437 }
Steven Moreland0693f312016-11-09 15:06:14 -0800438 }
439
Yifan Hongc8934042016-11-17 17:10:52 -0800440 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700441 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700442
Steven Moreland19f11b52017-05-12 18:22:21 -0700443 if (iface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200444 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700445 }
446
Martijn Coenenaf712c02016-11-16 15:26:27 +0100447 out << "#include <utils/NativeHandle.h>\n";
448 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700449
450 enterLeaveNamespace(out, true /* enter */);
451 out << "\n";
452
Steven Moreland19f11b52017-05-12 18:22:21 -0700453 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700454 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700455 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700456
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700457 const Interface *superType = iface->superType();
458
Steven Moreland40786312016-08-16 10:29:40 -0700459 if (superType == NULL) {
Yifan Hongc8934042016-11-17 17:10:52 -0800460 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700461 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000462 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700463 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700464 }
465
466 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700467
468 out.indent();
469
Andreas Huber881227d2016-08-02 14:20:21 -0700470 }
471
472 status_t err = emitTypeDeclarations(out);
473
474 if (err != OK) {
475 return err;
476 }
477
Steven Moreland19f11b52017-05-12 18:22:21 -0700478 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800479 out << "virtual bool isRemote() const ";
480 if (!isIBase()) {
481 out << "override ";
482 }
483 out << "{ return false; }\n\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800484
Andreas Huber881227d2016-08-02 14:20:21 -0700485 for (const auto &method : iface->methods()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700486 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700487
Andreas Huber881227d2016-08-02 14:20:21 -0700488 const bool returnsValue = !method->results().empty();
Steven Morelandd732ea12016-11-08 17:12:06 -0800489 const TypedVar *elidedReturn = method->canElideCallback();
490
491 if (elidedReturn == nullptr && returnsValue) {
492 out << "using "
493 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700494 << "_cb = std::function<void(";
495 method->emitCppResultSignature(out, true /* specify namespaces */);
496 out << ")>;\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800497 }
Andreas Huber881227d2016-08-02 14:20:21 -0700498
Andreas Huber3599d922016-08-09 10:42:57 -0700499 method->dumpAnnotations(out);
500
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700501 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700502 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700503 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700504 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700505 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700506 }
507
508 out << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700509 << "(";
510 method->emitCppArgSignature(out, true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700511
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700512 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700513 if (!method->args().empty()) {
514 out << ", ";
515 }
516
Steven Moreland67f67b42016-09-29 08:59:02 -0700517 out << method->name() << "_cb _hidl_cb";
Andreas Huber881227d2016-08-02 14:20:21 -0700518 }
519
Yifan Hong10fe0b52016-10-19 14:20:17 -0700520 out << ")";
521 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800522 if (!isIBase()) {
523 out << " override";
524 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700525 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700526 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700527 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700528 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700529 }
Steven Moreland40786312016-08-16 10:29:40 -0700530
Yifan Hong3d746092016-12-07 14:26:33 -0800531 out << "// cast static functions\n";
532 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700533
Yifan Hong3d746092016-12-07 14:26:33 -0800534 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -0700535 out << "static ::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -0800536 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -0700537 << "> castFrom("
Yifan Hong3d746092016-12-07 14:26:33 -0800538 << superType->getCppArgumentType()
539 << " parent"
Yifan Hong200209c2017-03-29 03:39:09 -0700540 << ", bool emitError = false);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700541 }
542
Steven Morelandd39133b2016-11-11 12:30:08 -0800543 out << "\nstatic const char* descriptor;\n\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700544
Yifan Hongc8934042016-11-17 17:10:52 -0800545 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800546 out << "// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800547 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800548 declareServiceManagerInteractions(out, iface->localName());
Yifan Hongc8934042016-11-17 17:10:52 -0800549 }
Andreas Huber881227d2016-08-02 14:20:21 -0700550 }
551
Steven Moreland19f11b52017-05-12 18:22:21 -0700552 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700553 out.unindent();
554
Andreas Hubere3f769a2016-10-10 10:54:44 -0700555 out << "};\n\n";
556 }
557
558 err = mRootScope->emitGlobalTypeDeclarations(out);
559
560 if (err != OK) {
561 return err;
Andreas Huber881227d2016-08-02 14:20:21 -0700562 }
563
564 out << "\n";
565 enterLeaveNamespace(out, false /* enter */);
566
567 out << "\n#endif // " << guard << "\n";
568
569 return OK;
570}
571
Steven Moreland40786312016-08-16 10:29:40 -0700572status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700573 const Interface *iface = getInterface();
574 std::string klassName = iface ? iface->getHwName() : "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700575
Steven Moreland40786312016-08-16 10:29:40 -0700576 std::string path = outputPath;
577 path.append(mCoordinator->convertPackageRootToPath(mPackage));
578 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
579 path.append(klassName + ".h");
580
Yifan Hong244e82d2016-11-11 11:13:57 -0800581 FILE *file = fopen(path.c_str(), "w");
Steven Moreland40786312016-08-16 10:29:40 -0700582
583 if (file == NULL) {
584 return -errno;
585 }
586
587 Formatter out(file);
588
589 const std::string guard = makeHeaderGuard(klassName);
590
591 out << "#ifndef " << guard << "\n";
592 out << "#define " << guard << "\n\n";
593
Steven Moreland19f11b52017-05-12 18:22:21 -0700594 generateCppPackageInclude(out, mPackage, iface ? iface->localName() : "types");
Steven Moreland40786312016-08-16 10:29:40 -0700595
Steven Morelandee88eed2016-10-31 17:49:00 -0700596 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700597
598 for (const auto &item : mImportedNames) {
599 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800600 generateCppPackageInclude(out, item, "hwtypes");
601 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800602 generateCppPackageInclude(out, item, item.getInterfaceStubName());
603 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700604 }
Steven Moreland40786312016-08-16 10:29:40 -0700605 }
606
607 out << "\n";
608
Martijn Coenen93915102016-09-01 01:35:52 +0200609 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700610 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100611 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700612
613 out << "\n";
614
615 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700616
Yifan Hong244e82d2016-11-11 11:13:57 -0800617 status_t err = mRootScope->emitGlobalHwDeclarations(out);
618 if (err != OK) {
619 return err;
620 }
Steven Moreland40786312016-08-16 10:29:40 -0700621
622 enterLeaveNamespace(out, false /* enter */);
623
624 out << "\n#endif // " << guard << "\n";
625
626 return OK;
627}
628
Andreas Huber881227d2016-08-02 14:20:21 -0700629status_t AST::emitTypeDeclarations(Formatter &out) const {
630 return mRootScope->emitTypeDeclarations(out);
631}
632
Yifan Hong7a118f52016-12-07 11:21:15 -0800633static void wrapPassthroughArg(Formatter &out,
634 const TypedVar *arg, bool addPrefixToName,
635 std::function<void(void)> handleError) {
636 if (!arg->type().isInterface()) {
637 return;
638 }
639 std::string name = (addPrefixToName ? "_hidl_out_" : "") + arg->name();
640 std::string wrappedName = (addPrefixToName ? "_hidl_out_wrapped_" : "_hidl_wrapped_")
641 + arg->name();
642 const Interface &iface = static_cast<const Interface &>(arg->type());
643 out << iface.getCppStackType() << " " << wrappedName << ";\n";
644 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
645 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
646 out << wrappedName
647 << " = "
648 << iface.fqName().cppName()
Yifan Hong341112d2017-04-20 18:12:05 -0700649 << "::castFrom(::android::hardware::details::wrapPassthrough<"
650 << iface.fqName().cppName()
651 << ">("
Yifan Hong7a118f52016-12-07 11:21:15 -0800652 << name << "));\n";
653 out.sIf(wrappedName + " == nullptr", [&] {
654 // Fatal error. Happens when the BsFoo class is not found in the binary
655 // or any dynamic libraries.
656 handleError();
657 }).endl();
658 }).sElse([&] {
659 out << wrappedName << " = " << name << ";\n";
660 }).endl().endl();
661}
662
Steven Moreland69e7c702016-09-09 11:16:32 -0700663status_t AST::generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700664 const Method *method) const {
665 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700666
667 out << " {\n";
668 out.indent();
669
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800670 if (method->isHidlReserved()
671 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
672 method->cppImpl(IMPL_PASSTHROUGH, out);
673 out.unindent();
674 out << "}\n\n";
675 return OK;
676 }
677
Steven Moreland69e7c702016-09-09 11:16:32 -0700678 const bool returnsValue = !method->results().empty();
679 const TypedVar *elidedReturn = method->canElideCallback();
680
Steven Moreland67f67b42016-09-29 08:59:02 -0700681 if (returnsValue && elidedReturn == nullptr) {
682 generateCheckNonNull(out, "_hidl_cb");
683 }
684
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700685 generateCppInstrumentationCall(
686 out,
687 InstrumentationEvent::PASSTHROUGH_ENTRY,
688 method);
689
Yifan Hong7a118f52016-12-07 11:21:15 -0800690
691 for (const auto &arg : method->args()) {
692 wrapPassthroughArg(out, arg, false /* addPrefixToName */, [&] {
693 out << "return ::android::hardware::Status::fromExceptionCode(\n";
694 out.indent(2, [&] {
695 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800696 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800697 });
698 });
699 }
700
701 out << "auto _hidl_error = ::android::hardware::Void();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700702 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700703
704 if (method->isOneway()) {
Steven Moreland836cb312017-06-05 17:25:55 -0700705 out << "addOnewayTask([mImpl = this->mImpl\n"
706 << "#ifdef __ANDROID_DEBUGGABLE__\n"
707 ", mEnableInstrumentation = this->mEnableInstrumentation, "
708 "mInstrumentationCallbacks = this->mInstrumentationCallbacks\n"
709 << "#endif // __ANDROID_DEBUGGABLE__\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700710 for (const auto &arg : method->args()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800711 out << ", "
712 << (arg->type().isInterface() ? "_hidl_wrapped_" : "")
713 << arg->name();
Steven Moreland69e7c702016-09-09 11:16:32 -0700714 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700715 out << "] {\n";
716 out.indent();
Steven Moreland69e7c702016-09-09 11:16:32 -0700717 }
718
719 out << "mImpl->"
720 << method->name()
721 << "(";
722
Yifan Hong932464e2017-03-30 15:40:22 -0700723 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800724 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700725 });
Steven Moreland69e7c702016-09-09 11:16:32 -0700726 if (returnsValue && elidedReturn == nullptr) {
Steven Moreland340c8822017-05-02 14:41:49 -0700727 // never true if oneway since oneway methods don't return values
728
Steven Moreland69e7c702016-09-09 11:16:32 -0700729 if (!method->args().empty()) {
730 out << ", ";
731 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800732 out << "[&](";
Yifan Hong932464e2017-03-30 15:40:22 -0700733 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800734 out << "const auto &_hidl_out_"
735 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700736 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800737
738 out << ") {\n";
739 out.indent();
740 status_t status = generateCppInstrumentationCall(
741 out,
742 InstrumentationEvent::PASSTHROUGH_EXIT,
743 method);
744 if (status != OK) {
745 return status;
746 }
747
Yifan Hong7a118f52016-12-07 11:21:15 -0800748 for (const auto &arg : method->results()) {
749 wrapPassthroughArg(out, arg, true /* addPrefixToName */, [&] {
750 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
751 out.indent(2, [&] {
752 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800753 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800754 });
755 out << "return;\n";
756 });
757 }
758
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800759 out << "_hidl_cb(";
Yifan Hong932464e2017-03-30 15:40:22 -0700760 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800761 out << (arg->type().isInterface() ? "_hidl_out_wrapped_" : "_hidl_out_")
762 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700763 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800764 out << ");\n";
765 out.unindent();
766 out << "});\n\n";
767 } else {
768 out << ");\n\n";
Steven Moreland30b76e92017-06-02 18:52:24 -0700769
770 // used by generateCppInstrumentationCall
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800771 if (elidedReturn != nullptr) {
Steven Moreland30b76e92017-06-02 18:52:24 -0700772 out << "#ifdef __ANDROID_DEBUGGABLE__\n"
773 << elidedReturn->type().getCppResultType() << " _hidl_out_" << elidedReturn->name()
774 << " = _hidl_return;\n"
775 << "#endif // __ANDROID_DEBUGGABLE__\n";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800776 }
777 status_t status = generateCppInstrumentationCall(
778 out,
779 InstrumentationEvent::PASSTHROUGH_EXIT,
780 method);
781 if (status != OK) {
782 return status;
783 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700784 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700785
786 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700787 out.unindent();
788 out << "});\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700789 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700790
791 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700792
793 out.unindent();
794 out << "}\n";
795
796 return OK;
797}
798
Yifan Hong068c5522016-10-31 14:07:25 -0700799status_t AST::generateMethods(Formatter &out, MethodGenerator gen) const {
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700800 const Interface *iface = mRootScope->getInterface();
801
Yifan Hong10fe0b52016-10-19 14:20:17 -0700802 const Interface *prevIterface = nullptr;
803 for (const auto &tuple : iface->allMethodsFromRoot()) {
804 const Method *method = tuple.method();
805 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700806
Yifan Hong10fe0b52016-10-19 14:20:17 -0700807 if(prevIterface != superInterface) {
808 if (prevIterface != nullptr) {
809 out << "\n";
810 }
811 out << "// Methods from "
812 << superInterface->fullName()
813 << " follow.\n";
814 prevIterface = superInterface;
815 }
Yifan Hong068c5522016-10-31 14:07:25 -0700816 status_t err = gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700817
Yifan Hong10fe0b52016-10-19 14:20:17 -0700818 if (err != OK) {
819 return err;
820 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700821 }
822
Yifan Hong10fe0b52016-10-19 14:20:17 -0700823 out << "\n";
824
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700825 return OK;
826}
827
Steven Moreland0b843772017-06-23 16:33:38 -0700828void AST::generateTemplatizationLink(Formatter& out) const {
829 out << "typedef " << mRootScope->getInterface()->localName() << " Pure;\n\n";
830}
831
Andreas Huberb82318c2016-08-02 14:45:54 -0700832status_t AST::generateStubHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700833 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700834 // types.hal does not get a stub header.
835 return OK;
836 }
837
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700838 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800839 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -0700840
Andreas Huberb82318c2016-08-02 14:45:54 -0700841 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700842 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700843 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700844 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700845 path.append(".h");
846
Andreas Huberd2943e12016-08-05 11:59:31 -0700847 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700848 FILE *file = fopen(path.c_str(), "w");
849
850 if (file == NULL) {
851 return -errno;
852 }
853
854 Formatter out(file);
855
Steven Moreland40786312016-08-16 10:29:40 -0700856 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700857
858 out << "#ifndef " << guard << "\n";
859 out << "#define " << guard << "\n\n";
860
Yifan Hongeefe4f22017-01-04 15:32:42 -0800861 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700862 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700863
864 enterLeaveNamespace(out, true /* enter */);
865 out << "\n";
866
867 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800868 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100869 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800870 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000871 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100872 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800873 out << " : public "
874 << gIBaseFqName.getInterfaceStubFqName().cppName()
875 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100876 }
Andreas Huber881227d2016-08-02 14:20:21 -0700877
878 out.indent();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800879 out << "explicit "
880 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700881 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100882 << "\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800883 out << "explicit "
884 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700885 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800886 << " const std::string& HidlInstrumentor_package,"
887 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700888 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700889 out << "::android::status_t onTransact(\n";
890 out.indent();
891 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700892 out << "uint32_t _hidl_code,\n";
893 out << "const ::android::hardware::Parcel &_hidl_data,\n";
894 out << "::android::hardware::Parcel *_hidl_reply,\n";
895 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700896 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700897 out.unindent();
898 out.unindent();
899
Steven Moreland0b843772017-06-23 16:33:38 -0700900 out.endl();
901 generateTemplatizationLink(out);
902
Steven Moreland19f11b52017-05-12 18:22:21 -0700903 out << "::android::sp<" << iface->localName() << "> getImpl() { return _hidl_mImpl; };\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100904 out.unindent();
905 out << "private:\n";
906 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800907
908 status_t err = generateMethods(out, [&](const Method *method, const Interface *iface) {
909 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
910 return OK;
911 }
912 const bool returnsValue = !method->results().empty();
913 const TypedVar *elidedReturn = method->canElideCallback();
914
915 if (elidedReturn == nullptr && returnsValue) {
916 out << "using " << method->name() << "_cb = "
917 << iface->fqName().cppName()
918 << "::" << method->name() << "_cb;\n";
919 }
920 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800921 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800922 return OK;
923 });
924 if (err != OK) {
925 return err;
926 }
927
Steven Moreland19f11b52017-05-12 18:22:21 -0700928 out << "::android::sp<" << iface->localName() << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700929 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700930 out << "};\n\n";
931
932 enterLeaveNamespace(out, false /* enter */);
933
934 out << "\n#endif // " << guard << "\n";
935
936 return OK;
937}
938
Andreas Huberb82318c2016-08-02 14:45:54 -0700939status_t AST::generateProxyHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700940 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700941 // types.hal does not get a proxy header.
942 return OK;
943 }
944
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700945 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800946 const std::string proxyName = iface->getProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -0700947
Andreas Huberb82318c2016-08-02 14:45:54 -0700948 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700949 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700950 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Yifan Hongeefe4f22017-01-04 15:32:42 -0800951 path.append(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700952 path.append(".h");
953
Andreas Huberd2943e12016-08-05 11:59:31 -0700954 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700955 FILE *file = fopen(path.c_str(), "w");
956
957 if (file == NULL) {
958 return -errno;
959 }
960
961 Formatter out(file);
962
Yifan Hongeefe4f22017-01-04 15:32:42 -0800963 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700964
965 out << "#ifndef " << guard << "\n";
966 out << "#define " << guard << "\n\n";
967
Martijn Coenen115d4282016-12-19 05:14:04 +0100968 out << "#include <hidl/HidlTransportSupport.h>\n\n";
969
Andreas Huber881227d2016-08-02 14:20:21 -0700970 std::vector<std::string> packageComponents;
971 getPackageAndVersionComponents(
972 &packageComponents, false /* cpp_compatible */);
973
Yifan Hongeefe4f22017-01-04 15:32:42 -0800974 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700975 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700976
977 enterLeaveNamespace(out, true /* enter */);
978 out << "\n";
979
980 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800981 << proxyName
982 << " : public ::android::hardware::BpInterface<"
983 << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000984 << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700985
986 out.indent();
987
Yifan Hongeefe4f22017-01-04 15:32:42 -0800988 out << "explicit "
989 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700990 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700991 << "\n\n";
992
Steven Moreland0b843772017-06-23 16:33:38 -0700993 generateTemplatizationLink(out);
994
Yifan Hong10fe0b52016-10-19 14:20:17 -0700995 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700996
Yifan Hong068c5522016-10-31 14:07:25 -0700997 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
998 method->generateCppSignature(out);
999 out << " override;\n";
1000 return OK;
1001 });
Steven Moreland9c387612016-09-07 09:54:26 -07001002
1003 if (err != OK) {
1004 return err;
1005 }
Andreas Huber881227d2016-08-02 14:20:21 -07001006
1007 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +01001008 out << "private:\n";
1009 out.indent();
1010 out << "std::mutex _hidl_mMutex;\n"
1011 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
1012 << " _hidl_mDeathRecipients;\n";
1013 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -07001014 out << "};\n\n";
1015
1016 enterLeaveNamespace(out, false /* enter */);
1017
1018 out << "\n#endif // " << guard << "\n";
1019
1020 return OK;
1021}
1022
Steven Moreland1cbf0362017-05-09 14:32:53 -07001023status_t AST::generateCppSources(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001024 std::string baseName = getBaseName();
1025 const Interface *iface = getInterface();
Andreas Huber881227d2016-08-02 14:20:21 -07001026
Andreas Huberb82318c2016-08-02 14:45:54 -07001027 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -07001028 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -07001029 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -07001030 path.append(baseName);
1031
1032 if (baseName != "types") {
1033 path.append("All");
1034 }
1035
1036 path.append(".cpp");
1037
Andreas Huberd2943e12016-08-05 11:59:31 -07001038 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -07001039 FILE *file = fopen(path.c_str(), "w");
1040
1041 if (file == NULL) {
1042 return -errno;
1043 }
1044
1045 Formatter out(file);
1046
Steven Moreland623c0042017-01-13 14:42:29 -08001047 out << "#define LOG_TAG \""
1048 << mPackage.string() << "::" << baseName
1049 << "\"\n\n";
1050
Steven Moreland05cd4232016-11-21 16:01:12 -08001051 out << "#include <android/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001052 out << "#include <cutils/trace.h>\n";
1053 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Steven Moreland19f11b52017-05-12 18:22:21 -07001054 if (iface) {
Steven Moreland19d5c172016-10-20 19:20:25 -07001055 // This is a no-op for IServiceManager itself.
1056 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
1057
Yifan Hongeefe4f22017-01-04 15:32:42 -08001058 generateCppPackageInclude(out, mPackage, iface->getProxyName());
1059 generateCppPackageInclude(out, mPackage, iface->getStubName());
1060 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001061
1062 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -07001063 generateCppPackageInclude(out,
1064 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -08001065 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001066 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -08001067
1068 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001069 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -07001070 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -08001071 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -07001072 }
1073
1074 out << "\n";
1075
1076 enterLeaveNamespace(out, true /* enter */);
1077 out << "\n";
1078
Steven Moreland19f11b52017-05-12 18:22:21 -07001079 status_t err = generateTypeSource(out, iface ? iface->localName() : "");
Andreas Huber881227d2016-08-02 14:20:21 -07001080
Steven Moreland19f11b52017-05-12 18:22:21 -07001081 if (err == OK && iface) {
Yifan Hong10fe0b52016-10-19 14:20:17 -07001082 const Interface *iface = mRootScope->getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -07001083
1084 // need to be put here, generateStubSource is using this.
Yifan Hongeefe4f22017-01-04 15:32:42 -08001085 out << "const char* "
1086 << iface->localName()
Yifan Hong10fe0b52016-10-19 14:20:17 -07001087 << "::descriptor(\""
1088 << iface->fqName().string()
1089 << "\");\n\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +01001090 out << "__attribute__((constructor))";
1091 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001092 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001093 out << "::android::hardware::details::gBnConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001094 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001095 << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001096 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001097 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001098 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001099 out << "return new "
1100 << iface->getStubName()
Yifan Hong341112d2017-04-20 18:12:05 -07001101 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001102 << iface->localName()
Yifan Hong158655a2016-11-08 12:34:07 -08001103 << " *>(iIntf));\n";
1104 });
Yifan Hongb04de382017-02-06 15:31:52 -08001105 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001106 });
Yifan Honga159f3b2017-03-16 14:53:51 -07001107 out << "::android::hardware::details::gBsConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001108 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001109 << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001110 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001111 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -08001112 << gIBaseFqName.cppName()
1113 << "> {\n";
1114 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001115 out << "return new "
1116 << iface->getPassthroughName()
Yifan Hong341112d2017-04-20 18:12:05 -07001117 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001118 << iface->localName()
Yifan Hong7a118f52016-12-07 11:21:15 -08001119 << " *>(iIntf));\n";
1120 });
Yifan Hongb04de382017-02-06 15:31:52 -08001121 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001122 });
Yifan Hong158655a2016-11-08 12:34:07 -08001123 });
Martijn Coenen8adcb652017-02-03 17:37:36 +01001124 out << "};\n\n";
1125 out << "__attribute__((destructor))";
1126 out << "static void static_destructor() {\n";
1127 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001128 out << "::android::hardware::details::gBnConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001129 << iface->localName()
1130 << "::descriptor);\n";
Yifan Honga159f3b2017-03-16 14:53:51 -07001131 out << "::android::hardware::details::gBsConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001132 << iface->localName()
1133 << "::descriptor);\n";
1134 });
1135 out << "};\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001136
Yifan Hongfe95aa22016-10-19 17:26:45 -07001137 err = generateInterfaceSource(out);
1138 }
1139
Steven Moreland19f11b52017-05-12 18:22:21 -07001140 if (err == OK && iface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001141 err = generateProxySource(out, iface->fqName());
Andreas Huber881227d2016-08-02 14:20:21 -07001142 }
1143
Steven Moreland19f11b52017-05-12 18:22:21 -07001144 if (err == OK && iface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001145 err = generateStubSource(out, iface);
Andreas Huber881227d2016-08-02 14:20:21 -07001146 }
1147
Steven Moreland19f11b52017-05-12 18:22:21 -07001148 if (err == OK && iface) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001149 err = generatePassthroughSource(out);
1150 }
1151
Steven Moreland19f11b52017-05-12 18:22:21 -07001152 if (err == OK && iface) {
Steven Moreland9c387612016-09-07 09:54:26 -07001153 const Interface *iface = mRootScope->getInterface();
1154
Yifan Hongc8934042016-11-17 17:10:52 -08001155 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001156 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -08001157 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001158 std::string package = iface->fqName().package()
1159 + iface->fqName().atVersion();
1160
Yifan Hongeefe4f22017-01-04 15:32:42 -08001161 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -08001162 }
Steven Moreland40786312016-08-16 10:29:40 -07001163 }
1164
Andreas Huber6755e9d2017-04-06 11:09:07 -07001165 HidlTypeAssertion::EmitAll(out);
1166 out << "\n";
1167
Andreas Huber881227d2016-08-02 14:20:21 -07001168 enterLeaveNamespace(out, false /* enter */);
1169
1170 return err;
1171}
1172
Steven Moreland67f67b42016-09-29 08:59:02 -07001173void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) {
Yifan Honga018ed52016-12-13 16:35:08 -08001174 out.sIf(nonNull + " == nullptr", [&] {
1175 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1176 out.indent(2, [&] {
Steven Moreland610002f2017-06-16 13:02:49 -07001177 out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT,\n"
1178 << "\"Null synchronous callback passed.\");\n";
Yifan Honga018ed52016-12-13 16:35:08 -08001179 });
1180 }).endl().endl();
Steven Moreland67f67b42016-09-29 08:59:02 -07001181}
1182
Andreas Huber881227d2016-08-02 14:20:21 -07001183status_t AST::generateTypeSource(
1184 Formatter &out, const std::string &ifaceName) const {
1185 return mRootScope->emitTypeDefinitions(out, ifaceName);
1186}
1187
Andreas Hubere7ff2282016-08-16 13:50:03 -07001188void AST::declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -07001189 Formatter &out,
1190 const std::vector<TypedVar *> &args,
1191 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -07001192 if (args.empty()) {
1193 return;
1194 }
1195
1196 for (const auto &arg : args) {
1197 const Type &type = arg->type();
1198
Yifan Hong3b320f82016-11-01 15:15:54 -07001199 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001200 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -07001201 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001202 << ";\n";
1203 }
1204
1205 out << "\n";
1206}
1207
Andreas Huber881227d2016-08-02 14:20:21 -07001208void AST::emitCppReaderWriter(
1209 Formatter &out,
1210 const std::string &parcelObj,
1211 bool parcelObjIsPointer,
1212 const TypedVar *arg,
1213 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -07001214 Type::ErrorMode mode,
1215 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -07001216 const Type &type = arg->type();
1217
Andreas Huber881227d2016-08-02 14:20:21 -07001218 type.emitReaderWriter(
1219 out,
Andreas Huber5e44a292016-09-27 14:52:39 -07001220 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -07001221 parcelObj,
1222 parcelObjIsPointer,
1223 isReader,
1224 mode);
1225}
1226
Yifan Hongbf459bc2016-08-23 16:50:37 -07001227void AST::emitCppResolveReferences(
1228 Formatter &out,
1229 const std::string &parcelObj,
1230 bool parcelObjIsPointer,
1231 const TypedVar *arg,
1232 bool isReader,
1233 Type::ErrorMode mode,
1234 bool addPrefixToName) const {
1235 const Type &type = arg->type();
1236 if(type.needsResolveReferences()) {
1237 type.emitResolveReferences(
1238 out,
1239 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
1240 isReader, // nameIsPointer
1241 parcelObj,
1242 parcelObjIsPointer,
1243 isReader,
1244 mode);
1245 }
1246}
1247
Yifan Hong068c5522016-10-31 14:07:25 -07001248status_t AST::generateProxyMethodSource(Formatter &out,
1249 const std::string &klassName,
1250 const Method *method,
1251 const Interface *superInterface) const {
1252
1253 method->generateCppSignature(out,
1254 klassName,
1255 true /* specify namespaces */);
1256
1257 const bool returnsValue = !method->results().empty();
1258 const TypedVar *elidedReturn = method->canElideCallback();
1259
Steven Moreland41c6d2e2016-11-07 12:26:54 -08001260 out << " {\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001261
1262 out.indent();
1263
Martijn Coenen115d4282016-12-19 05:14:04 +01001264 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
1265 method->cppImpl(IMPL_PROXY, out);
1266 out.unindent();
1267 out << "}\n\n";
1268 return OK;
1269 }
1270
Yifan Hong068c5522016-10-31 14:07:25 -07001271 if (returnsValue && elidedReturn == nullptr) {
1272 generateCheckNonNull(out, "_hidl_cb");
1273 }
1274
1275 status_t status = generateCppInstrumentationCall(
1276 out,
1277 InstrumentationEvent::CLIENT_API_ENTRY,
Yifan Hong068c5522016-10-31 14:07:25 -07001278 method);
1279 if (status != OK) {
1280 return status;
1281 }
1282
1283 out << "::android::hardware::Parcel _hidl_data;\n";
1284 out << "::android::hardware::Parcel _hidl_reply;\n";
1285 out << "::android::status_t _hidl_err;\n";
1286 out << "::android::hardware::Status _hidl_status;\n\n";
1287
1288 declareCppReaderLocals(
1289 out, method->results(), true /* forResults */);
1290
1291 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001292 out << superInterface->fqName().cppName();
Yifan Hong068c5522016-10-31 14:07:25 -07001293 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001294 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1295
Martijn Coenenfff73352017-01-04 16:36:31 +01001296 bool hasInterfaceArgument = false;
Yifan Hong068c5522016-10-31 14:07:25 -07001297 // First DFS: write all buffers and resolve pointers for parent
1298 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001299 if (arg->type().isInterface()) {
1300 hasInterfaceArgument = true;
1301 }
Yifan Hong068c5522016-10-31 14:07:25 -07001302 emitCppReaderWriter(
1303 out,
1304 "_hidl_data",
1305 false /* parcelObjIsPointer */,
1306 arg,
1307 false /* reader */,
1308 Type::ErrorMode_Goto,
1309 false /* addPrefixToName */);
1310 }
1311
1312 // Second DFS: resolve references.
1313 for (const auto &arg : method->args()) {
1314 emitCppResolveReferences(
1315 out,
1316 "_hidl_data",
1317 false /* parcelObjIsPointer */,
1318 arg,
1319 false /* reader */,
1320 Type::ErrorMode_Goto,
1321 false /* addPrefixToName */);
1322 }
1323
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001324 if (hasInterfaceArgument) {
1325 // Start binder threadpool to handle incoming transactions
1326 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1327 }
Yifan Hong068c5522016-10-31 14:07:25 -07001328 out << "_hidl_err = remote()->transact("
1329 << method->getSerialId()
1330 << " /* "
1331 << method->name()
1332 << " */, _hidl_data, &_hidl_reply";
1333
1334 if (method->isOneway()) {
1335 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
1336 }
1337 out << ");\n";
1338
1339 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1340
1341 if (!method->isOneway()) {
Yifan Hong859e53f2016-11-14 19:08:24 -08001342 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001343 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1344 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1345
1346
1347 // First DFS: write all buffers and resolve pointers for parent
1348 for (const auto &arg : method->results()) {
1349 emitCppReaderWriter(
1350 out,
1351 "_hidl_reply",
1352 false /* parcelObjIsPointer */,
1353 arg,
1354 true /* reader */,
1355 Type::ErrorMode_Goto,
1356 true /* addPrefixToName */);
1357 }
1358
1359 // Second DFS: resolve references.
1360 for (const auto &arg : method->results()) {
1361 emitCppResolveReferences(
1362 out,
1363 "_hidl_reply",
1364 false /* parcelObjIsPointer */,
1365 arg,
1366 true /* reader */,
1367 Type::ErrorMode_Goto,
1368 true /* addPrefixToName */);
1369 }
1370
1371 if (returnsValue && elidedReturn == nullptr) {
1372 out << "_hidl_cb(";
1373
Yifan Hong932464e2017-03-30 15:40:22 -07001374 out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) {
Yifan Hong068c5522016-10-31 14:07:25 -07001375 if (arg->type().resultNeedsDeref()) {
1376 out << "*";
1377 }
1378 out << "_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001379 });
Yifan Hong068c5522016-10-31 14:07:25 -07001380
1381 out << ");\n\n";
1382 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001383 }
1384 status = generateCppInstrumentationCall(
1385 out,
1386 InstrumentationEvent::CLIENT_API_EXIT,
1387 method);
1388 if (status != OK) {
1389 return status;
Yifan Hong068c5522016-10-31 14:07:25 -07001390 }
1391
1392 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001393 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1394 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001395 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001396 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1397 } else {
1398 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1399 out << "return ::android::hardware::Return<void>();\n\n";
1400 }
1401
1402 out.unindent();
1403 out << "_hidl_error:\n";
1404 out.indent();
1405 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1406 out << "return ::android::hardware::Return<";
1407 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001408 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001409 } else {
1410 out << "void";
1411 }
1412 out << ">(_hidl_status);\n";
1413
1414 out.unindent();
1415 out << "}\n\n";
1416 return OK;
1417}
1418
Andreas Huber881227d2016-08-02 14:20:21 -07001419status_t AST::generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -08001420 Formatter &out, const FQName &fqName) const {
1421 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001422
1423 out << klassName
1424 << "::"
1425 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001426 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001427
1428 out.indent();
1429 out.indent();
1430
1431 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001432 << "<"
1433 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001434 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001435 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001436 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001437 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001438 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001439 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001440
Andreas Huber881227d2016-08-02 14:20:21 -07001441 out.unindent();
1442 out.unindent();
1443 out << "}\n\n";
1444
Yifan Hong068c5522016-10-31 14:07:25 -07001445 status_t err = generateMethods(out, [&](const Method *method, const Interface *superInterface) {
1446 return generateProxyMethodSource(out, klassName, method, superInterface);
1447 });
Andreas Huber881227d2016-08-02 14:20:21 -07001448
Yifan Hong068c5522016-10-31 14:07:25 -07001449 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001450}
1451
1452status_t AST::generateStubSource(
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001453 Formatter &out,
Yifan Hongeefe4f22017-01-04 15:32:42 -08001454 const Interface *iface) const {
1455 const std::string interfaceName = iface->localName();
1456 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001457
Steven Moreland40786312016-08-16 10:29:40 -07001458 out << klassName
1459 << "::"
1460 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001461 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001462
1463 out.indent();
1464 out.indent();
1465
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001466 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001467 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001468 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001469 out << ": "
1470 << gIBaseFqName.getInterfaceStubFqName().cppName()
1471 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001472 }
1473
1474 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001475 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001476 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001477 << "\") { \n";
1478 out.indent();
1479 out << "_hidl_mImpl = _hidl_impl;\n";
1480 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001481
1482 out.unindent();
1483 out.unindent();
1484 out << "}\n\n";
1485
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001486 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001487 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001488 // class properly.
1489 out << klassName
1490 << "::"
1491 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001492 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1493 << " const std::string &HidlInstrumentor_package,"
1494 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001495
1496 out.indent();
1497 out.indent();
1498
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001499 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001500 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001501 out.indent();
1502 out << "_hidl_mImpl = _hidl_impl;\n";
1503 out.unindent();
1504
1505 out.unindent();
1506 out.unindent();
1507 out << "}\n\n";
1508 }
1509
Yifan Hongbcffce22017-02-01 15:52:06 -08001510 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1511 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
1512 return OK;
1513 }
1514 method->generateCppSignature(out, iface->getStubName());
1515 out << " ";
1516 out.block([&] {
1517 method->cppImpl(IMPL_STUB_IMPL, out);
1518 }).endl();
1519 return OK;
1520 });
Steven Moreland60818632017-02-04 00:33:42 -08001521 if (err != OK) {
1522 return err;
1523 }
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001524
Andreas Huber881227d2016-08-02 14:20:21 -07001525 out << "::android::status_t " << klassName << "::onTransact(\n";
1526
1527 out.indent();
1528 out.indent();
1529
Iliyan Malchev549e2592016-08-10 08:59:12 -07001530 out << "uint32_t _hidl_code,\n"
1531 << "const ::android::hardware::Parcel &_hidl_data,\n"
1532 << "::android::hardware::Parcel *_hidl_reply,\n"
1533 << "uint32_t _hidl_flags,\n"
1534 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001535
1536 out.unindent();
1537
Iliyan Malchev549e2592016-08-10 08:59:12 -07001538 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001539 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001540 out.indent();
1541
Yifan Hong10fe0b52016-10-19 14:20:17 -07001542 for (const auto &tuple : iface->allMethodsFromRoot()) {
1543 const Method *method = tuple.method();
1544 const Interface *superInterface = tuple.interface();
1545 out << "case "
1546 << method->getSerialId()
1547 << " /* "
1548 << method->name()
1549 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001550
Yifan Hong10fe0b52016-10-19 14:20:17 -07001551 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001552
Yifan Hong10fe0b52016-10-19 14:20:17 -07001553 status_t err =
1554 generateStubSourceForMethod(out, superInterface, method);
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001555
Yifan Hong10fe0b52016-10-19 14:20:17 -07001556 if (err != OK) {
1557 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001558 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001559
1560 out.unindent();
1561 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001562 }
1563
1564 out << "default:\n{\n";
1565 out.indent();
1566
Martijn Coenen225bc922017-06-27 14:39:46 -07001567 if (iface->isIBase()) {
1568 out << "(void)_hidl_flags;\n";
1569 out << "return ::android::UNKNOWN_TRANSACTION;\n";
1570 } else {
1571 out << "return ";
1572 out << gIBaseFqName.getInterfaceStubFqName().cppName();
1573 out << "::onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001574
Martijn Coenen225bc922017-06-27 14:39:46 -07001575 out.indent();
1576 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001577
Martijn Coenen225bc922017-06-27 14:39:46 -07001578 out << "_hidl_code, _hidl_data, _hidl_reply, "
1579 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001580
Martijn Coenen225bc922017-06-27 14:39:46 -07001581 out.unindent();
1582 out.unindent();
1583 }
Andreas Huber881227d2016-08-02 14:20:21 -07001584
1585 out.unindent();
1586 out << "}\n";
1587
1588 out.unindent();
1589 out << "}\n\n";
1590
Yifan Honga018ed52016-12-13 16:35:08 -08001591 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1592 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1593 out.indent(2, [&] {
1594 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1595 out << "_hidl_reply);\n";
1596 });
1597 });
Andreas Huber881227d2016-08-02 14:20:21 -07001598
Iliyan Malchev549e2592016-08-10 08:59:12 -07001599 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001600
1601 out.unindent();
1602 out << "}\n\n";
1603
1604 return OK;
1605}
1606
1607status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001608 Formatter &out, const Interface *iface, const Method *method) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001609 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1610 method->cppImpl(IMPL_STUB, out);
1611 out << "break;\n";
1612 return OK;
1613 }
1614
Yifan Hongeefe4f22017-01-04 15:32:42 -08001615 out << "if (!_hidl_data.enforceInterface("
1616 << iface->fullName()
1617 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001618
Andreas Huber881227d2016-08-02 14:20:21 -07001619 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001620 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001621 out << "break;\n";
1622 out.unindent();
1623 out << "}\n\n";
1624
Andreas Huber5e44a292016-09-27 14:52:39 -07001625 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001626
Yifan Hongbf459bc2016-08-23 16:50:37 -07001627 // First DFS: write buffers
Andreas Huber881227d2016-08-02 14:20:21 -07001628 for (const auto &arg : method->args()) {
1629 emitCppReaderWriter(
1630 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001631 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001632 false /* parcelObjIsPointer */,
1633 arg,
1634 true /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001635 Type::ErrorMode_Break,
1636 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001637 }
1638
Yifan Hongbf459bc2016-08-23 16:50:37 -07001639 // Second DFS: resolve references
1640 for (const auto &arg : method->args()) {
1641 emitCppResolveReferences(
1642 out,
1643 "_hidl_data",
1644 false /* parcelObjIsPointer */,
1645 arg,
1646 true /* reader */,
1647 Type::ErrorMode_Break,
1648 false /* addPrefixToName */);
1649 }
1650
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001651 status_t status = generateCppInstrumentationCall(
1652 out,
1653 InstrumentationEvent::SERVER_API_ENTRY,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001654 method);
1655 if (status != OK) {
1656 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001657 }
1658
Andreas Huber881227d2016-08-02 14:20:21 -07001659 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001660 const TypedVar *elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -08001661 const std::string callee =
1662 (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL))
1663 ? "this" : "_hidl_mImpl";
Andreas Huber881227d2016-08-02 14:20:21 -07001664
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001665 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001666 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001667 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001668 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001669 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001670 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001671 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001672
Yifan Hong932464e2017-03-30 15:40:22 -07001673 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001674 if (arg->type().resultNeedsDeref()) {
1675 out << "*";
1676 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001677 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001678 });
Andreas Huber881227d2016-08-02 14:20:21 -07001679
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001680 out << ");\n\n";
Yifan Hong859e53f2016-11-14 19:08:24 -08001681 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1682 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001683
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001684 elidedReturn->type().emitReaderWriter(
1685 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001686 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001687 "_hidl_reply",
1688 true, /* parcelObjIsPointer */
1689 false, /* isReader */
1690 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001691
Yifan Hongbf459bc2016-08-23 16:50:37 -07001692 emitCppResolveReferences(
1693 out,
1694 "_hidl_reply",
1695 true /* parcelObjIsPointer */,
1696 elidedReturn,
1697 false /* reader */,
1698 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001699 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001700
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001701 status_t status = generateCppInstrumentationCall(
1702 out,
1703 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001704 method);
1705 if (status != OK) {
1706 return status;
1707 }
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001708
Iliyan Malchev549e2592016-08-10 08:59:12 -07001709 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001710 } else {
1711 if (returnsValue) {
1712 out << "bool _hidl_callbackCalled = false;\n\n";
1713 }
Andreas Huber881227d2016-08-02 14:20:21 -07001714
Yifan Hongcd2ae452017-01-31 14:33:40 -08001715 out << callee << "->" << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001716
Yifan Hong932464e2017-03-30 15:40:22 -07001717 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001718 if (arg->type().resultNeedsDeref()) {
1719 out << "*";
1720 }
1721
1722 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001723 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001724
1725 if (returnsValue) {
Yifan Hong932464e2017-03-30 15:40:22 -07001726 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001727 out << ", ";
1728 }
1729
1730 out << "[&](";
1731
Yifan Hong932464e2017-03-30 15:40:22 -07001732 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001733 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001734 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001735
1736 out << ") {\n";
1737 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001738 out << "if (_hidl_callbackCalled) {\n";
1739 out.indent();
1740 out << "LOG_ALWAYS_FATAL(\""
1741 << method->name()
1742 << ": _hidl_cb called a second time, but must be called once.\");\n";
1743 out.unindent();
1744 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001745 out << "_hidl_callbackCalled = true;\n\n";
1746
Yifan Hong859e53f2016-11-14 19:08:24 -08001747 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1748 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001749
Yifan Hongbf459bc2016-08-23 16:50:37 -07001750 // First DFS: buffers
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001751 for (const auto &arg : method->results()) {
1752 emitCppReaderWriter(
1753 out,
1754 "_hidl_reply",
1755 true /* parcelObjIsPointer */,
1756 arg,
1757 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001758 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001759 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001760 }
1761
Yifan Hongbf459bc2016-08-23 16:50:37 -07001762 // Second DFS: resolve references
1763 for (const auto &arg : method->results()) {
1764 emitCppResolveReferences(
1765 out,
1766 "_hidl_reply",
1767 true /* parcelObjIsPointer */,
1768 arg,
1769 false /* reader */,
1770 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001771 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001772 }
1773
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001774 status_t status = generateCppInstrumentationCall(
1775 out,
1776 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001777 method);
1778 if (status != OK) {
1779 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001780 }
1781
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001782 out << "_hidl_cb(*_hidl_reply);\n";
1783
1784 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001785 out << "});\n\n";
1786 } else {
1787 out << ");\n\n";
1788 status_t status = generateCppInstrumentationCall(
1789 out,
1790 InstrumentationEvent::SERVER_API_EXIT,
1791 method);
1792 if (status != OK) {
1793 return status;
1794 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001795 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001796
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001797 if (returnsValue) {
1798 out << "if (!_hidl_callbackCalled) {\n";
1799 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001800 out << "LOG_ALWAYS_FATAL(\""
1801 << method->name()
1802 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001803 out.unindent();
1804 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001805 } else {
1806 out << "::android::hardware::writeToParcel("
1807 << "::android::hardware::Status::ok(), "
1808 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001809 }
Andreas Huber881227d2016-08-02 14:20:21 -07001810 }
1811
1812 out << "break;\n";
1813
1814 return OK;
1815}
1816
Steven Moreland69e7c702016-09-09 11:16:32 -07001817status_t AST::generatePassthroughHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001818 if (!AST::isInterface()) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001819 // types.hal does not get a stub header.
1820 return OK;
1821 }
1822
1823 const Interface *iface = mRootScope->getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -07001824 CHECK(iface != nullptr);
Steven Moreland69e7c702016-09-09 11:16:32 -07001825
Yifan Hongeefe4f22017-01-04 15:32:42 -08001826 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001827
1828 bool supportOneway = iface->hasOnewayMethods();
1829
1830 std::string path = outputPath;
1831 path.append(mCoordinator->convertPackageRootToPath(mPackage));
1832 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
1833 path.append(klassName);
1834 path.append(".h");
1835
1836 CHECK(Coordinator::MakeParentHierarchy(path));
1837 FILE *file = fopen(path.c_str(), "w");
1838
1839 if (file == NULL) {
1840 return -errno;
1841 }
1842
1843 Formatter out(file);
1844
1845 const std::string guard = makeHeaderGuard(klassName);
1846
1847 out << "#ifndef " << guard << "\n";
1848 out << "#define " << guard << "\n\n";
1849
1850 std::vector<std::string> packageComponents;
1851 getPackageAndVersionComponents(
1852 &packageComponents, false /* cpp_compatible */);
1853
Steven Moreland61d3f4b2017-04-28 17:30:38 -07001854 out << "#include <android-base/macros.h>\n";
Yifan Hongb0949432016-12-15 15:32:24 -08001855 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001856 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001857
Steven Moreland19f11b52017-05-12 18:22:21 -07001858 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Morelandee88eed2016-10-31 17:49:00 -07001859 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001860
Yifan Hong7a118f52016-12-07 11:21:15 -08001861 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001862 if (supportOneway) {
Yifan Hong2cbc1472016-10-25 19:02:40 -07001863 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001864 }
1865
1866 enterLeaveNamespace(out, true /* enter */);
1867 out << "\n";
1868
1869 out << "struct "
1870 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -07001871 << " : " << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001872 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001873
1874 out.indent();
1875 out << "explicit "
1876 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001877 << "(const ::android::sp<"
Steven Moreland19f11b52017-05-12 18:22:21 -07001878 << iface->localName()
Steven Moreland69e7c702016-09-09 11:16:32 -07001879 << "> impl);\n";
1880
Steven Moreland0b843772017-06-23 16:33:38 -07001881 out.endl();
1882 generateTemplatizationLink(out);
1883
Yifan Hong068c5522016-10-31 14:07:25 -07001884 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1885 return generatePassthroughMethod(out, method);
1886 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001887
1888 if (err != OK) {
1889 return err;
1890 }
1891
1892 out.unindent();
1893 out << "private:\n";
1894 out.indent();
Steven Moreland19f11b52017-05-12 18:22:21 -07001895 out << "const ::android::sp<" << iface->localName() << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001896
1897 if (supportOneway) {
Yifan Hongef91d362017-03-20 17:18:13 -07001898 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001899
1900 out << "\n";
1901
1902 out << "::android::hardware::Return<void> addOnewayTask("
1903 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001904 }
1905
1906 out.unindent();
1907
1908 out << "};\n\n";
1909
1910 enterLeaveNamespace(out, false /* enter */);
1911
1912 out << "\n#endif // " << guard << "\n";
1913
1914 return OK;
1915}
1916
Yifan Hongfe95aa22016-10-19 17:26:45 -07001917status_t AST::generateInterfaceSource(Formatter &out) const {
1918 const Interface *iface = mRootScope->getInterface();
1919
Yifan Hong2d7126b2016-10-20 15:12:57 -07001920 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001921 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001922
Steven Morelandd4b068a2017-03-20 06:30:51 -07001923 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1924 bool reserved = method->isHidlReserved();
1925
1926 if (!reserved) {
1927 out << "// no default implementation for: ";
1928 }
1929 method->generateCppSignature(out, iface->localName());
1930 if (reserved) {
1931 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001932 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001933 }).endl();
1934 }
1935
1936 out << "\n";
1937
1938 return OK;
1939 });
1940 if (err != OK) {
1941 return err;
1942 }
1943
Yifan Hong3d746092016-12-07 14:26:33 -08001944 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -07001945 out << "// static \n::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -08001946 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -07001947 << "> "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001948 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001949 << "::castFrom("
1950 << superType->getCppArgumentType()
Yifan Hong200209c2017-03-29 03:39:09 -07001951 << " parent, bool "
1952 << (iface == superType ? "/* emitError */" : "emitError")
1953 << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001954 out.indent();
1955 if (iface == superType) {
1956 out << "return parent;\n";
1957 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001958 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001959 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001960 << superType->fqName().cppName() << ", "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001961 << iface->getProxyName() << ", "
Yifan Hong51a65092017-01-04 15:41:44 -08001962 << superType->getProxyFqName().cppName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001963 << ">(\n";
1964 out.indent();
1965 out.indent();
1966 out << "parent, \""
1967 << iface->fqName().string()
Yifan Hong200209c2017-03-29 03:39:09 -07001968 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001969 out.unindent();
1970 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001971 }
Yifan Hong3d746092016-12-07 14:26:33 -08001972 out.unindent();
1973 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001974 }
1975
1976 return OK;
1977}
1978
Steven Moreland69e7c702016-09-09 11:16:32 -07001979status_t AST::generatePassthroughSource(Formatter &out) const {
1980 const Interface *iface = mRootScope->getInterface();
1981
Yifan Hongeefe4f22017-01-04 15:32:42 -08001982 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001983
1984 out << klassName
1985 << "::"
1986 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001987 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001988 << iface->fullName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001989 << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001990 << mPackage.string()
1991 << "\", \""
1992 << iface->localName()
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001993 << "\"), mImpl(impl) {";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001994 if (iface->hasOnewayMethods()) {
1995 out << "\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001996 out.indent([&] {
Yifan Hongf01dad42017-03-20 19:03:11 -07001997 out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001998 });
1999 }
2000 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002001
2002 if (iface->hasOnewayMethods()) {
2003 out << "::android::hardware::Return<void> "
2004 << klassName
2005 << "::addOnewayTask(std::function<void(void)> fun) {\n";
2006 out.indent();
Yifan Hong2cbc1472016-10-25 19:02:40 -07002007 out << "if (!mOnewayQueue.push(fun)) {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002008 out.indent();
Steven Moreland67f67b42016-09-29 08:59:02 -07002009 out << "return ::android::hardware::Status::fromExceptionCode(\n";
2010 out.indent();
2011 out.indent();
Steven Moreland610002f2017-06-16 13:02:49 -07002012 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
2013 << "\"Passthrough oneway function queue exceeds maximum size.\");\n";
Steven Moreland67f67b42016-09-29 08:59:02 -07002014 out.unindent();
2015 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002016 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002017 out << "}\n";
2018
Steven Morelandd366c262016-10-11 15:29:10 -07002019 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002020
2021 out.unindent();
2022 out << "}\n\n";
2023
2024
2025 }
2026
2027 return OK;
2028}
2029
Martijn Coenen7b295242016-11-04 16:52:56 +01002030status_t AST::generateCppAtraceCall(Formatter &out,
2031 InstrumentationEvent event,
2032 const Method *method) const {
2033 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08002034 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01002035 switch (event) {
2036 case SERVER_API_ENTRY:
2037 {
2038 out << "atrace_begin(ATRACE_TAG_HAL, \""
2039 << baseString + "::server\");\n";
2040 break;
2041 }
2042 case CLIENT_API_ENTRY:
2043 {
2044 out << "atrace_begin(ATRACE_TAG_HAL, \""
2045 << baseString + "::client\");\n";
2046 break;
2047 }
2048 case PASSTHROUGH_ENTRY:
2049 {
2050 out << "atrace_begin(ATRACE_TAG_HAL, \""
2051 << baseString + "::passthrough\");\n";
2052 break;
2053 }
2054 case SERVER_API_EXIT:
2055 case CLIENT_API_EXIT:
2056 case PASSTHROUGH_EXIT:
2057 {
2058 out << "atrace_end(ATRACE_TAG_HAL);\n";
2059 break;
2060 }
2061 default:
2062 {
2063 LOG(ERROR) << "Unsupported instrumentation event: " << event;
2064 return UNKNOWN_ERROR;
2065 }
2066 }
2067
2068 return OK;
2069}
2070
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002071status_t AST::generateCppInstrumentationCall(
2072 Formatter &out,
2073 InstrumentationEvent event,
Steven Moreland031ccf12016-10-31 15:54:38 -07002074 const Method *method) const {
Martijn Coenen7b295242016-11-04 16:52:56 +01002075 status_t err = generateCppAtraceCall(out, event, method);
2076 if (err != OK) {
2077 return err;
2078 }
2079
Steven Moreland30b76e92017-06-02 18:52:24 -07002080 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002081 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
2082 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002083 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002084 std::string event_str = "";
2085 switch (event) {
2086 case SERVER_API_ENTRY:
2087 {
2088 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
2089 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002090 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002091 << (arg->type().resultNeedsDeref() ? "" : "&")
2092 << arg->name()
2093 << ");\n";
2094 }
2095 break;
2096 }
2097 case SERVER_API_EXIT:
2098 {
2099 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07002100 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002101 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07002102 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002103 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002104 }
2105 break;
2106 }
2107 case CLIENT_API_ENTRY:
2108 {
2109 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
2110 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002111 out << "_hidl_args.push_back((void *)&"
2112 << arg->name()
2113 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002114 }
2115 break;
2116 }
2117 case CLIENT_API_EXIT:
2118 {
2119 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
2120 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002121 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002122 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002123 << "_hidl_out_"
2124 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002125 << ");\n";
2126 }
2127 break;
2128 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002129 case PASSTHROUGH_ENTRY:
2130 {
2131 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
2132 for (const auto &arg : method->args()) {
2133 out << "_hidl_args.push_back((void *)&"
2134 << arg->name()
2135 << ");\n";
2136 }
2137 break;
2138 }
2139 case PASSTHROUGH_EXIT:
2140 {
2141 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002142 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002143 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002144 << arg->name()
2145 << ");\n";
2146 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002147 break;
2148 }
Steven Moreland031ccf12016-10-31 15:54:38 -07002149 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002150 {
Steven Moreland031ccf12016-10-31 15:54:38 -07002151 LOG(ERROR) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002152 return UNKNOWN_ERROR;
2153 }
2154 }
2155
Steven Moreland031ccf12016-10-31 15:54:38 -07002156 const Interface *iface = mRootScope->getInterface();
2157
Steven Moreland1ab31442016-11-03 18:37:51 -07002158 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002159 out.indent();
2160 out << "callback("
2161 << event_str
2162 << ", \""
2163 << mPackage.package()
2164 << "\", \""
Yifan Hong90ea87f2016-11-01 14:25:47 -07002165 << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002166 << "\", \""
2167 << iface->localName()
2168 << "\", \""
2169 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002170 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002171 out.unindent();
2172 out << "}\n";
2173 out.unindent();
Steven Moreland30b76e92017-06-02 18:52:24 -07002174 out << "}\n";
2175 out << "#endif // __ANDROID_DEBUGGABLE__\n\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002176
2177 return OK;
2178}
2179
Andreas Huber881227d2016-08-02 14:20:21 -07002180} // namespace android