blob: 2f3321735c6dbb085bb07192114c2f5e17b16395 [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 Coenen6ec2f0b2016-12-11 01:04:55 +01001567 out << "return onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001568
1569 out.indent();
1570 out.indent();
1571
Iliyan Malchev549e2592016-08-10 08:59:12 -07001572 out << "_hidl_code, _hidl_data, _hidl_reply, "
1573 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001574
1575 out.unindent();
1576 out.unindent();
1577
1578 out.unindent();
1579 out << "}\n";
1580
1581 out.unindent();
1582 out << "}\n\n";
1583
Yifan Honga018ed52016-12-13 16:35:08 -08001584 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1585 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1586 out.indent(2, [&] {
1587 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1588 out << "_hidl_reply);\n";
1589 });
1590 });
Andreas Huber881227d2016-08-02 14:20:21 -07001591
Iliyan Malchev549e2592016-08-10 08:59:12 -07001592 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001593
1594 out.unindent();
1595 out << "}\n\n";
1596
1597 return OK;
1598}
1599
1600status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001601 Formatter &out, const Interface *iface, const Method *method) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001602 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1603 method->cppImpl(IMPL_STUB, out);
1604 out << "break;\n";
1605 return OK;
1606 }
1607
Yifan Hongeefe4f22017-01-04 15:32:42 -08001608 out << "if (!_hidl_data.enforceInterface("
1609 << iface->fullName()
1610 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001611
Andreas Huber881227d2016-08-02 14:20:21 -07001612 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001613 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001614 out << "break;\n";
1615 out.unindent();
1616 out << "}\n\n";
1617
Andreas Huber5e44a292016-09-27 14:52:39 -07001618 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001619
Yifan Hongbf459bc2016-08-23 16:50:37 -07001620 // First DFS: write buffers
Andreas Huber881227d2016-08-02 14:20:21 -07001621 for (const auto &arg : method->args()) {
1622 emitCppReaderWriter(
1623 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001624 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001625 false /* parcelObjIsPointer */,
1626 arg,
1627 true /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001628 Type::ErrorMode_Break,
1629 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001630 }
1631
Yifan Hongbf459bc2016-08-23 16:50:37 -07001632 // Second DFS: resolve references
1633 for (const auto &arg : method->args()) {
1634 emitCppResolveReferences(
1635 out,
1636 "_hidl_data",
1637 false /* parcelObjIsPointer */,
1638 arg,
1639 true /* reader */,
1640 Type::ErrorMode_Break,
1641 false /* addPrefixToName */);
1642 }
1643
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001644 status_t status = generateCppInstrumentationCall(
1645 out,
1646 InstrumentationEvent::SERVER_API_ENTRY,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001647 method);
1648 if (status != OK) {
1649 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001650 }
1651
Andreas Huber881227d2016-08-02 14:20:21 -07001652 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001653 const TypedVar *elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -08001654 const std::string callee =
1655 (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL))
1656 ? "this" : "_hidl_mImpl";
Andreas Huber881227d2016-08-02 14:20:21 -07001657
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001658 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001659 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001660 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001661 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001662 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001663 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001664 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001665
Yifan Hong932464e2017-03-30 15:40:22 -07001666 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001667 if (arg->type().resultNeedsDeref()) {
1668 out << "*";
1669 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001670 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001671 });
Andreas Huber881227d2016-08-02 14:20:21 -07001672
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001673 out << ");\n\n";
Yifan Hong859e53f2016-11-14 19:08:24 -08001674 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1675 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001676
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001677 elidedReturn->type().emitReaderWriter(
1678 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001679 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001680 "_hidl_reply",
1681 true, /* parcelObjIsPointer */
1682 false, /* isReader */
1683 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001684
Yifan Hongbf459bc2016-08-23 16:50:37 -07001685 emitCppResolveReferences(
1686 out,
1687 "_hidl_reply",
1688 true /* parcelObjIsPointer */,
1689 elidedReturn,
1690 false /* reader */,
1691 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001692 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001693
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001694 status_t status = generateCppInstrumentationCall(
1695 out,
1696 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001697 method);
1698 if (status != OK) {
1699 return status;
1700 }
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001701
Iliyan Malchev549e2592016-08-10 08:59:12 -07001702 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001703 } else {
1704 if (returnsValue) {
1705 out << "bool _hidl_callbackCalled = false;\n\n";
1706 }
Andreas Huber881227d2016-08-02 14:20:21 -07001707
Yifan Hongcd2ae452017-01-31 14:33:40 -08001708 out << callee << "->" << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001709
Yifan Hong932464e2017-03-30 15:40:22 -07001710 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001711 if (arg->type().resultNeedsDeref()) {
1712 out << "*";
1713 }
1714
1715 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001716 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001717
1718 if (returnsValue) {
Yifan Hong932464e2017-03-30 15:40:22 -07001719 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001720 out << ", ";
1721 }
1722
1723 out << "[&](";
1724
Yifan Hong932464e2017-03-30 15:40:22 -07001725 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001726 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001727 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001728
1729 out << ") {\n";
1730 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001731 out << "if (_hidl_callbackCalled) {\n";
1732 out.indent();
1733 out << "LOG_ALWAYS_FATAL(\""
1734 << method->name()
1735 << ": _hidl_cb called a second time, but must be called once.\");\n";
1736 out.unindent();
1737 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001738 out << "_hidl_callbackCalled = true;\n\n";
1739
Yifan Hong859e53f2016-11-14 19:08:24 -08001740 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1741 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001742
Yifan Hongbf459bc2016-08-23 16:50:37 -07001743 // First DFS: buffers
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001744 for (const auto &arg : method->results()) {
1745 emitCppReaderWriter(
1746 out,
1747 "_hidl_reply",
1748 true /* parcelObjIsPointer */,
1749 arg,
1750 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001751 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001752 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001753 }
1754
Yifan Hongbf459bc2016-08-23 16:50:37 -07001755 // Second DFS: resolve references
1756 for (const auto &arg : method->results()) {
1757 emitCppResolveReferences(
1758 out,
1759 "_hidl_reply",
1760 true /* parcelObjIsPointer */,
1761 arg,
1762 false /* reader */,
1763 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001764 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001765 }
1766
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001767 status_t status = generateCppInstrumentationCall(
1768 out,
1769 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001770 method);
1771 if (status != OK) {
1772 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001773 }
1774
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001775 out << "_hidl_cb(*_hidl_reply);\n";
1776
1777 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001778 out << "});\n\n";
1779 } else {
1780 out << ");\n\n";
1781 status_t status = generateCppInstrumentationCall(
1782 out,
1783 InstrumentationEvent::SERVER_API_EXIT,
1784 method);
1785 if (status != OK) {
1786 return status;
1787 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001788 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001789
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001790 if (returnsValue) {
1791 out << "if (!_hidl_callbackCalled) {\n";
1792 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001793 out << "LOG_ALWAYS_FATAL(\""
1794 << method->name()
1795 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001796 out.unindent();
1797 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001798 } else {
1799 out << "::android::hardware::writeToParcel("
1800 << "::android::hardware::Status::ok(), "
1801 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001802 }
Andreas Huber881227d2016-08-02 14:20:21 -07001803 }
1804
1805 out << "break;\n";
1806
1807 return OK;
1808}
1809
Steven Moreland69e7c702016-09-09 11:16:32 -07001810status_t AST::generatePassthroughHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001811 if (!AST::isInterface()) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001812 // types.hal does not get a stub header.
1813 return OK;
1814 }
1815
1816 const Interface *iface = mRootScope->getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -07001817 CHECK(iface != nullptr);
Steven Moreland69e7c702016-09-09 11:16:32 -07001818
Yifan Hongeefe4f22017-01-04 15:32:42 -08001819 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001820
1821 bool supportOneway = iface->hasOnewayMethods();
1822
1823 std::string path = outputPath;
1824 path.append(mCoordinator->convertPackageRootToPath(mPackage));
1825 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
1826 path.append(klassName);
1827 path.append(".h");
1828
1829 CHECK(Coordinator::MakeParentHierarchy(path));
1830 FILE *file = fopen(path.c_str(), "w");
1831
1832 if (file == NULL) {
1833 return -errno;
1834 }
1835
1836 Formatter out(file);
1837
1838 const std::string guard = makeHeaderGuard(klassName);
1839
1840 out << "#ifndef " << guard << "\n";
1841 out << "#define " << guard << "\n\n";
1842
1843 std::vector<std::string> packageComponents;
1844 getPackageAndVersionComponents(
1845 &packageComponents, false /* cpp_compatible */);
1846
Steven Moreland61d3f4b2017-04-28 17:30:38 -07001847 out << "#include <android-base/macros.h>\n";
Yifan Hongb0949432016-12-15 15:32:24 -08001848 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001849 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001850
Steven Moreland19f11b52017-05-12 18:22:21 -07001851 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Morelandee88eed2016-10-31 17:49:00 -07001852 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001853
Yifan Hong7a118f52016-12-07 11:21:15 -08001854 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001855 if (supportOneway) {
Yifan Hong2cbc1472016-10-25 19:02:40 -07001856 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001857 }
1858
1859 enterLeaveNamespace(out, true /* enter */);
1860 out << "\n";
1861
1862 out << "struct "
1863 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -07001864 << " : " << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001865 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001866
1867 out.indent();
1868 out << "explicit "
1869 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001870 << "(const ::android::sp<"
Steven Moreland19f11b52017-05-12 18:22:21 -07001871 << iface->localName()
Steven Moreland69e7c702016-09-09 11:16:32 -07001872 << "> impl);\n";
1873
Steven Moreland0b843772017-06-23 16:33:38 -07001874 out.endl();
1875 generateTemplatizationLink(out);
1876
Yifan Hong068c5522016-10-31 14:07:25 -07001877 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1878 return generatePassthroughMethod(out, method);
1879 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001880
1881 if (err != OK) {
1882 return err;
1883 }
1884
1885 out.unindent();
1886 out << "private:\n";
1887 out.indent();
Steven Moreland19f11b52017-05-12 18:22:21 -07001888 out << "const ::android::sp<" << iface->localName() << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001889
1890 if (supportOneway) {
Yifan Hongef91d362017-03-20 17:18:13 -07001891 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001892
1893 out << "\n";
1894
1895 out << "::android::hardware::Return<void> addOnewayTask("
1896 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001897 }
1898
1899 out.unindent();
1900
1901 out << "};\n\n";
1902
1903 enterLeaveNamespace(out, false /* enter */);
1904
1905 out << "\n#endif // " << guard << "\n";
1906
1907 return OK;
1908}
1909
Yifan Hongfe95aa22016-10-19 17:26:45 -07001910status_t AST::generateInterfaceSource(Formatter &out) const {
1911 const Interface *iface = mRootScope->getInterface();
1912
Yifan Hong2d7126b2016-10-20 15:12:57 -07001913 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001914 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001915
Steven Morelandd4b068a2017-03-20 06:30:51 -07001916 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1917 bool reserved = method->isHidlReserved();
1918
1919 if (!reserved) {
1920 out << "// no default implementation for: ";
1921 }
1922 method->generateCppSignature(out, iface->localName());
1923 if (reserved) {
1924 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001925 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001926 }).endl();
1927 }
1928
1929 out << "\n";
1930
1931 return OK;
1932 });
1933 if (err != OK) {
1934 return err;
1935 }
1936
Yifan Hong3d746092016-12-07 14:26:33 -08001937 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -07001938 out << "// static \n::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -08001939 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -07001940 << "> "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001941 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001942 << "::castFrom("
1943 << superType->getCppArgumentType()
Yifan Hong200209c2017-03-29 03:39:09 -07001944 << " parent, bool "
1945 << (iface == superType ? "/* emitError */" : "emitError")
1946 << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001947 out.indent();
1948 if (iface == superType) {
1949 out << "return parent;\n";
1950 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001951 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001952 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001953 << superType->fqName().cppName() << ", "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001954 << iface->getProxyName() << ", "
Yifan Hong51a65092017-01-04 15:41:44 -08001955 << superType->getProxyFqName().cppName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001956 << ">(\n";
1957 out.indent();
1958 out.indent();
1959 out << "parent, \""
1960 << iface->fqName().string()
Yifan Hong200209c2017-03-29 03:39:09 -07001961 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001962 out.unindent();
1963 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001964 }
Yifan Hong3d746092016-12-07 14:26:33 -08001965 out.unindent();
1966 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001967 }
1968
1969 return OK;
1970}
1971
Steven Moreland69e7c702016-09-09 11:16:32 -07001972status_t AST::generatePassthroughSource(Formatter &out) const {
1973 const Interface *iface = mRootScope->getInterface();
1974
Yifan Hongeefe4f22017-01-04 15:32:42 -08001975 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001976
1977 out << klassName
1978 << "::"
1979 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001980 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001981 << iface->fullName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001982 << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001983 << mPackage.string()
1984 << "\", \""
1985 << iface->localName()
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001986 << "\"), mImpl(impl) {";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001987 if (iface->hasOnewayMethods()) {
1988 out << "\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001989 out.indent([&] {
Yifan Hongf01dad42017-03-20 19:03:11 -07001990 out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001991 });
1992 }
1993 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001994
1995 if (iface->hasOnewayMethods()) {
1996 out << "::android::hardware::Return<void> "
1997 << klassName
1998 << "::addOnewayTask(std::function<void(void)> fun) {\n";
1999 out.indent();
Yifan Hong2cbc1472016-10-25 19:02:40 -07002000 out << "if (!mOnewayQueue.push(fun)) {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002001 out.indent();
Steven Moreland67f67b42016-09-29 08:59:02 -07002002 out << "return ::android::hardware::Status::fromExceptionCode(\n";
2003 out.indent();
2004 out.indent();
Steven Moreland610002f2017-06-16 13:02:49 -07002005 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
2006 << "\"Passthrough oneway function queue exceeds maximum size.\");\n";
Steven Moreland67f67b42016-09-29 08:59:02 -07002007 out.unindent();
2008 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002009 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002010 out << "}\n";
2011
Steven Morelandd366c262016-10-11 15:29:10 -07002012 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002013
2014 out.unindent();
2015 out << "}\n\n";
2016
2017
2018 }
2019
2020 return OK;
2021}
2022
Martijn Coenen7b295242016-11-04 16:52:56 +01002023status_t AST::generateCppAtraceCall(Formatter &out,
2024 InstrumentationEvent event,
2025 const Method *method) const {
2026 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08002027 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01002028 switch (event) {
2029 case SERVER_API_ENTRY:
2030 {
2031 out << "atrace_begin(ATRACE_TAG_HAL, \""
2032 << baseString + "::server\");\n";
2033 break;
2034 }
2035 case CLIENT_API_ENTRY:
2036 {
2037 out << "atrace_begin(ATRACE_TAG_HAL, \""
2038 << baseString + "::client\");\n";
2039 break;
2040 }
2041 case PASSTHROUGH_ENTRY:
2042 {
2043 out << "atrace_begin(ATRACE_TAG_HAL, \""
2044 << baseString + "::passthrough\");\n";
2045 break;
2046 }
2047 case SERVER_API_EXIT:
2048 case CLIENT_API_EXIT:
2049 case PASSTHROUGH_EXIT:
2050 {
2051 out << "atrace_end(ATRACE_TAG_HAL);\n";
2052 break;
2053 }
2054 default:
2055 {
2056 LOG(ERROR) << "Unsupported instrumentation event: " << event;
2057 return UNKNOWN_ERROR;
2058 }
2059 }
2060
2061 return OK;
2062}
2063
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002064status_t AST::generateCppInstrumentationCall(
2065 Formatter &out,
2066 InstrumentationEvent event,
Steven Moreland031ccf12016-10-31 15:54:38 -07002067 const Method *method) const {
Martijn Coenen7b295242016-11-04 16:52:56 +01002068 status_t err = generateCppAtraceCall(out, event, method);
2069 if (err != OK) {
2070 return err;
2071 }
2072
Steven Moreland30b76e92017-06-02 18:52:24 -07002073 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002074 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
2075 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002076 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002077 std::string event_str = "";
2078 switch (event) {
2079 case SERVER_API_ENTRY:
2080 {
2081 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
2082 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002083 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002084 << (arg->type().resultNeedsDeref() ? "" : "&")
2085 << arg->name()
2086 << ");\n";
2087 }
2088 break;
2089 }
2090 case SERVER_API_EXIT:
2091 {
2092 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07002093 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002094 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07002095 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002096 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002097 }
2098 break;
2099 }
2100 case CLIENT_API_ENTRY:
2101 {
2102 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
2103 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002104 out << "_hidl_args.push_back((void *)&"
2105 << arg->name()
2106 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002107 }
2108 break;
2109 }
2110 case CLIENT_API_EXIT:
2111 {
2112 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
2113 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002114 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002115 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002116 << "_hidl_out_"
2117 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002118 << ");\n";
2119 }
2120 break;
2121 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002122 case PASSTHROUGH_ENTRY:
2123 {
2124 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
2125 for (const auto &arg : method->args()) {
2126 out << "_hidl_args.push_back((void *)&"
2127 << arg->name()
2128 << ");\n";
2129 }
2130 break;
2131 }
2132 case PASSTHROUGH_EXIT:
2133 {
2134 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002135 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002136 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002137 << arg->name()
2138 << ");\n";
2139 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002140 break;
2141 }
Steven Moreland031ccf12016-10-31 15:54:38 -07002142 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002143 {
Steven Moreland031ccf12016-10-31 15:54:38 -07002144 LOG(ERROR) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002145 return UNKNOWN_ERROR;
2146 }
2147 }
2148
Steven Moreland031ccf12016-10-31 15:54:38 -07002149 const Interface *iface = mRootScope->getInterface();
2150
Steven Moreland1ab31442016-11-03 18:37:51 -07002151 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002152 out.indent();
2153 out << "callback("
2154 << event_str
2155 << ", \""
2156 << mPackage.package()
2157 << "\", \""
Yifan Hong90ea87f2016-11-01 14:25:47 -07002158 << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002159 << "\", \""
2160 << iface->localName()
2161 << "\", \""
2162 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002163 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002164 out.unindent();
2165 out << "}\n";
2166 out.unindent();
Steven Moreland30b76e92017-06-02 18:52:24 -07002167 out << "}\n";
2168 out << "#endif // __ANDROID_DEBUGGABLE__\n\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002169
2170 return OK;
2171}
2172
Andreas Huber881227d2016-08-02 14:20:21 -07002173} // namespace android