blob: 44c785b2fedacad0ef98a89c83a4020296b3249e [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 Huber02ef12f2017-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 {
37 status_t err = generateInterfaceHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070038
39 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070040 err = generateStubHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070041 }
42
43 if (err == OK) {
Steven Moreland40786312016-08-16 10:29:40 -070044 err = generateHwBinderHeader(outputPath);
45 }
46
47 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070048 err = generateProxyHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070049 }
50
51 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070052 err = generateAllSource(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070053 }
54
Steven Moreland69e7c702016-09-09 11:16:32 -070055 if (err == OK) {
Yifan Hong7a118f52016-12-07 11:21:15 -080056 err = generatePassthroughHeader(outputPath);
Steven Moreland69e7c702016-09-09 11:16:32 -070057 }
58
Andreas Huber881227d2016-08-02 14:20:21 -070059 return err;
60}
61
Andreas Huber737080b2016-08-02 15:38:04 -070062void AST::getPackageComponents(
63 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070064 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070065}
66
67void AST::getPackageAndVersionComponents(
68 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070069 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070070}
71
Steven Moreland5708edf2016-11-04 15:33:31 +000072std::string AST::makeHeaderGuard(const std::string &baseName,
73 bool indicateGenerated) const {
74 std::string guard;
Andreas Huber881227d2016-08-02 14:20:21 -070075
Steven Moreland5708edf2016-11-04 15:33:31 +000076 if (indicateGenerated) {
77 guard += "HIDL_GENERATED_";
78 }
79
80 guard += StringHelper::Uppercase(mPackage.tokenName());
Andreas Huber881227d2016-08-02 14:20:21 -070081 guard += "_";
Steven Moreland5708edf2016-11-04 15:33:31 +000082 guard += StringHelper::Uppercase(baseName);
83 guard += "_H";
Andreas Huber881227d2016-08-02 14:20:21 -070084
85 return guard;
86}
87
Steven Morelandee88eed2016-10-31 17:49:00 -070088// static
89void AST::generateCppPackageInclude(
90 Formatter &out,
91 const FQName &package,
92 const std::string &klass) {
93
94 out << "#include <";
95
96 std::vector<std::string> components;
97 package.getPackageAndVersionComponents(&components, false /* cpp_compatible */);
98
99 for (const auto &component : components) {
100 out << component << "/";
101 }
102
103 out << klass
104 << ".h>\n";
105}
106
Andreas Huber881227d2016-08-02 14:20:21 -0700107void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
108 std::vector<std::string> packageComponents;
109 getPackageAndVersionComponents(
110 &packageComponents, true /* cpp_compatible */);
111
112 if (enter) {
113 for (const auto &component : packageComponents) {
114 out << "namespace " << component << " {\n";
115 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700116
Andreas Huber2831d512016-08-15 09:33:47 -0700117 out.setNamespace(mPackage.cppNamespace() + "::");
Andreas Huber881227d2016-08-02 14:20:21 -0700118 } else {
Andreas Huber0e00de42016-08-03 09:56:02 -0700119 out.setNamespace(std::string());
120
Andreas Huber881227d2016-08-02 14:20:21 -0700121 for (auto it = packageComponents.rbegin();
122 it != packageComponents.rend();
123 ++it) {
124 out << "} // namespace " << *it << "\n";
125 }
126 }
127}
128
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700129static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
130 const std::string functionName = isTry ? "tryGetService" : "getService";
131
132 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800133 << "const std::string &serviceName=\"default\", bool getStub=false);\n";
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700134 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800135 << "const char serviceName[], bool getStub=false)"
136 << " { std::string str(serviceName ? serviceName : \"\");"
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700137 << " return " << functionName << "(str, getStub); }\n";
138 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800139 << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
140 // without c_str the std::string constructor is ambiguous
141 << " { std::string str(serviceName.c_str());"
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700142 << " return " << functionName << "(str, getStub); }\n";
143 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
144 << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
145}
146
147static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
148 declareGetService(out, interfaceName, true /* isTry */);
149 declareGetService(out, interfaceName, false /* isTry */);
150
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800151 out << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800152 out << "static bool registerForNotifications(\n";
153 out.indent(2, [&] {
154 out << "const std::string &serviceName,\n"
155 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
156 << "&notification);\n";
157 });
158
159}
160
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700161static void implementGetService(Formatter &out,
162 const FQName &fqName,
163 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800164
165 const std::string interfaceName = fqName.getInterfaceName();
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700166 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800167
168 out << "// static\n"
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700169 << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000170 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800171 out.block([&] {
Steven Moreland8146c322017-04-06 09:17:44 -0700172 out << "using ::android::hardware::defaultServiceManager;\n";
173 out << "using ::android::hardware::details::waitForHwService;\n";
174 out << "using ::android::hardware::getPassthroughServiceManager;\n";
175 out << "using ::android::hardware::Return;\n";
176 out << "using ::android::sp;\n";
177 out << "using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;\n\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000178
Steven Moreland8146c322017-04-06 09:17:44 -0700179 out << "sp<" << interfaceName << "> iface = nullptr;\n";
180
181 out.endl();
182
183 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> sm"
184 << " = defaultServiceManager();\n";
185
186 out.sIf("sm == nullptr", [&] {
187 // hwbinder is not available on this device, so future tries
188 // would also be null. I can only return nullptr.
189 out << "ALOGE(\"getService: defaultServiceManager() is null\");\n"
190 << "return nullptr;\n";
191 }).endl().endl();
192
193 out << "Return<Transport> transportRet = sm->getTransport("
194 << interfaceName << "::descriptor, serviceName);\n\n";
195
196 out.sIf("!transportRet.isOk()", [&] {
197 out << "ALOGE(\"getService: defaultServiceManager()->getTransport returns %s\", "
198 << "transportRet.description().c_str());\n";
199 out << "return nullptr;\n";
200 });
201
202 out.endl();
203
204 out << "Transport transport = transportRet;\n";
205 out << "const bool vintfHwbinder = (transport == Transport::HWBINDER);\n"
206 << "const bool vintfPassthru = (transport == Transport::PASSTHROUGH);\n"
207 << "const bool vintfEmpty = (transport == Transport::EMPTY);\n\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000208
209 // if (getStub) {
210 // getPassthroughServiceManager()->get only once.
211 // } else {
212 // if (vintfHwbinder) {
213 // while (no alive service) {
214 // waitForHwService
215 // defaultServiceManager()->get
216 // }
217 // } else if (vintfEmpty) {
218 // defaultServiceManager()->get only once.
219 // getPassthroughServiceManager()->get only once.
220 // } else if (vintfPassthru) {
221 // getPassthroughServiceManager()->get only once.
222 // }
223 // }
224
Yifan Hongf1ef44f2017-03-23 17:17:57 +0000225 out << "bool tried = false;\n";
226 out.sWhile("!getStub && (vintfHwbinder || (vintfEmpty && !tried))", [&] {
Yifan Hong31f07ff2017-03-21 18:56:35 +0000227
228 out.sIf("tried", [&] {
229 // sleep only after the first trial.
230 out << "ALOGI(\"getService: retrying in 1s...\");\n"
231 << "sleep(1);\n";
232 }).endl();
233
Yifan Hongf1ef44f2017-03-23 17:17:57 +0000234 out << "tried = true;\n";
235
Yifan Hong31f07ff2017-03-21 18:56:35 +0000236
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700237 if (!isTry) {
238 out.sIf("vintfHwbinder", [&] {
Steven Moreland8146c322017-04-06 09:17:44 -0700239 out << "waitForHwService("
240 << interfaceName << "::descriptor, serviceName);\n";
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700241 }).endl();
242 }
Yifan Hong31f07ff2017-03-21 18:56:35 +0000243
Steven Moreland8146c322017-04-06 09:17:44 -0700244 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000245 out.indent(2, [&] {
246 out << "sm->get(" << interfaceName << "::descriptor, serviceName);\n";
247 });
248
249 out.sIf("!ret.isOk()", [&] {
Steven Moreland42394ce2017-03-27 17:03:04 -0700250 // hwservicemanager fails, may be security issue
Yifan Hong31f07ff2017-03-21 18:56:35 +0000251 out << "ALOGE(\"getService: defaultServiceManager()->get returns %s\", "
252 << "ret.description().c_str());\n"
Steven Moreland42394ce2017-03-27 17:03:04 -0700253 << "break;\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000254 }).endl();
255
Steven Moreland8146c322017-04-06 09:17:44 -0700256 out << "sp<" << gIBaseFqName.cppName() << "> base = ret;\n";
Yifan Hong7783bb02017-03-29 03:39:09 -0700257 out.sIf("base == nullptr", [&] {
258 // race condition. hwservicemanager drops the service
259 // from waitForHwService to here
Steven Morelanddff644c2017-03-24 10:59:01 -0700260 out << "ALOGW(\"getService: found null hwbinder interface\");\n"
Yifan Hongdd0b55d2017-04-04 13:27:25 -0700261 << (isTry ? "break" : "continue")
262 << ";\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000263 }).endl();
Steven Moreland8146c322017-04-06 09:17:44 -0700264 out << "Return<sp<" << interfaceName
Yifan Hong7783bb02017-03-29 03:39:09 -0700265 << ">> castRet = " << interfaceName << "::castFrom(base, true /* emitError */);\n";
266 out.sIf("!castRet.isOk()", [&] {
267 out.sIf("castRet.isDeadObject()", [&] {
268 // service is dead (castFrom cannot call interfaceChain)
269 out << "ALOGW(\"getService: found dead hwbinder service\");\n"
Yifan Hongdd0b55d2017-04-04 13:27:25 -0700270 << (isTry ? "break" : "continue")
271 << ";\n";
Yifan Hong7783bb02017-03-29 03:39:09 -0700272 }).sElse([&] {
273 out << "ALOGW(\"getService: cannot call into hwbinder service: %s"
274 << "; No permission? Check for selinux denials.\", "
275 << "castRet.description().c_str());\n"
276 << "break;\n";
277 }).endl();
278 }).endl();
279 out << "iface = castRet;\n";
280 out.sIf("iface == nullptr", [&] {
281 // returned service isn't of correct type; this is a bug
282 // to hwservicemanager or to the service itself (interfaceChain
283 // is not consistent).
284 out << "ALOGW(\"getService: received incompatible service; bug in hwservicemanager?\");\n"
Yifan Hong31f07ff2017-03-21 18:56:35 +0000285 << "break;\n";
286 }).endl();
287
288 out << "return iface;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800289 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800290
Yifan Hong31f07ff2017-03-21 18:56:35 +0000291 out.sIf("getStub || vintfPassthru || vintfEmpty", [&] {
Steven Moreland8146c322017-04-06 09:17:44 -0700292 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> pm"
293 << " = getPassthroughServiceManager();\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000294
295 out.sIf("pm != nullptr", [&] () {
Steven Moreland8146c322017-04-06 09:17:44 -0700296 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Steven Morelandf10af872017-01-25 16:01:56 +0000297 out.indent(2, [&] {
298 out << "pm->get(" << interfaceName << "::descriptor" << ", serviceName);\n";
Steven Moreland2c2dea82017-01-18 17:24:17 -0800299 });
Steven Morelandf10af872017-01-25 16:01:56 +0000300 out.sIf("ret.isOk()", [&] {
Steven Moreland8146c322017-04-06 09:17:44 -0700301 out << "sp<" << gIBaseFqName.cppName()
Steven Morelandf10af872017-01-25 16:01:56 +0000302 << "> baseInterface = ret;\n";
303 out.sIf("baseInterface != nullptr", [&]() {
304 out << "iface = new " << fqName.getInterfacePassthroughName()
305 << "(" << interfaceName << "::castFrom(baseInterface));\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000306 }).endl();
Steven Morelandf10af872017-01-25 16:01:56 +0000307 }).endl();
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800308 }).endl();
309 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800310
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800311 out << "return iface;\n";
312 }).endl().endl();
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700313}
314
315static void implementServiceManagerInteractions(Formatter &out,
316 const FQName &fqName, const std::string &package) {
317
318 const std::string interfaceName = fqName.getInterfaceName();
319
320 implementGetService(out, fqName, true /* isTry */);
321 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800322
Yifan Hongeefe4f22017-01-04 15:32:42 -0800323 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800324 << "const std::string &serviceName) ";
325 out.block([&] {
Steven Morelanda9582e62017-04-09 10:54:50 -0700326 out << "::android::hardware::details::onRegistration(\""
327 << fqName.getPackageAndVersion().string() << "\", \""
328 << interfaceName
329 << "\", serviceName);\n\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800330 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
331 out.indent(2, [&] {
332 out << "= ::android::hardware::defaultServiceManager();\n";
333 });
334 out.sIf("sm == nullptr", [&] {
335 out << "return ::android::INVALID_OPERATION;\n";
336 }).endl();
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100337 out << "::android::hardware::Return<bool> ret = "
338 << "sm->add(serviceName.c_str(), this);\n"
339 << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800340 }).endl().endl();
341
Yifan Hongeefe4f22017-01-04 15:32:42 -0800342 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800343 out.indent(2, [&] {
344 out << "const std::string &serviceName,\n"
345 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
346 << "&notification) ";
347 });
348 out.block([&] {
349 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
350 out.indent(2, [&] {
351 out << "= ::android::hardware::defaultServiceManager();\n";
352 });
353 out.sIf("sm == nullptr", [&] {
354 out << "return false;\n";
355 }).endl();
356 out << "::android::hardware::Return<bool> success =\n";
357 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800358 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800359 out.indent(2, [&] {
360 out << "serviceName, notification);\n";
361 });
362 });
363 out << "return success.isOk() && success;\n";
364 }).endl().endl();
365}
366
Andreas Huberb82318c2016-08-02 14:45:54 -0700367status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700368
Andreas Huberb82318c2016-08-02 14:45:54 -0700369 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700370 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700371 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700372
373 std::string ifaceName;
374 bool isInterface = true;
375 if (!AST::isInterface(&ifaceName)) {
376 ifaceName = "types";
377 isInterface = false;
378 }
379 path.append(ifaceName);
380 path.append(".h");
381
Andreas Huberd2943e12016-08-05 11:59:31 -0700382 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700383 FILE *file = fopen(path.c_str(), "w");
384
385 if (file == NULL) {
386 return -errno;
387 }
388
389 Formatter out(file);
390
391 const std::string guard = makeHeaderGuard(ifaceName);
392
393 out << "#ifndef " << guard << "\n";
394 out << "#define " << guard << "\n\n";
395
Andreas Huber737080b2016-08-02 15:38:04 -0700396 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700397 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700398 }
399
400 if (!mImportedNames.empty()) {
401 out << "\n";
402 }
403
Steven Moreland0693f312016-11-09 15:06:14 -0800404 if (isInterface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800405 if (isIBase()) {
406 out << "// skipped #include IServiceNotification.h\n\n";
407 } else {
408 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
409 }
Steven Moreland0693f312016-11-09 15:06:14 -0800410 }
411
Yifan Hongc8934042016-11-17 17:10:52 -0800412 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700413 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700414
415 if (isInterface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200416 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700417 }
418
Martijn Coenenaf712c02016-11-16 15:26:27 +0100419 out << "#include <utils/NativeHandle.h>\n";
420 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700421
422 enterLeaveNamespace(out, true /* enter */);
423 out << "\n";
424
425 if (isInterface) {
426 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700427 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700428
429 const Interface *iface = mRootScope->getInterface();
430 const Interface *superType = iface->superType();
431
Steven Moreland40786312016-08-16 10:29:40 -0700432 if (superType == NULL) {
Yifan Hongc8934042016-11-17 17:10:52 -0800433 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700434 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000435 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700436 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700437 }
438
439 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700440
441 out.indent();
442
Andreas Huber881227d2016-08-02 14:20:21 -0700443 }
444
445 status_t err = emitTypeDeclarations(out);
446
447 if (err != OK) {
448 return err;
449 }
450
451 if (isInterface) {
452 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800453
Yifan Hongc8934042016-11-17 17:10:52 -0800454 out << "virtual bool isRemote() const ";
455 if (!isIBase()) {
456 out << "override ";
457 }
458 out << "{ return false; }\n\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800459
Andreas Huber881227d2016-08-02 14:20:21 -0700460 for (const auto &method : iface->methods()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700461 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700462
Andreas Huber881227d2016-08-02 14:20:21 -0700463 const bool returnsValue = !method->results().empty();
Steven Morelandd732ea12016-11-08 17:12:06 -0800464 const TypedVar *elidedReturn = method->canElideCallback();
465
466 if (elidedReturn == nullptr && returnsValue) {
467 out << "using "
468 << method->name()
Yifan Hong7d234ea2017-03-30 15:40:22 -0700469 << "_cb = std::function<void(";
470 method->emitCppResultSignature(out, true /* specify namespaces */);
471 out << ")>;\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800472 }
Andreas Huber881227d2016-08-02 14:20:21 -0700473
Andreas Huber3599d922016-08-09 10:42:57 -0700474 method->dumpAnnotations(out);
475
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700476 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700477 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700478 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700479 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700480 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700481 }
482
483 out << method->name()
Yifan Hong7d234ea2017-03-30 15:40:22 -0700484 << "(";
485 method->emitCppArgSignature(out, true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700486
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700487 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700488 if (!method->args().empty()) {
489 out << ", ";
490 }
491
Steven Moreland67f67b42016-09-29 08:59:02 -0700492 out << method->name() << "_cb _hidl_cb";
Andreas Huber881227d2016-08-02 14:20:21 -0700493 }
494
Yifan Hong10fe0b52016-10-19 14:20:17 -0700495 out << ")";
496 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800497 if (!isIBase()) {
498 out << " override";
499 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700500 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700501 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700502 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700503 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700504 }
Steven Moreland40786312016-08-16 10:29:40 -0700505
Yifan Hong3d746092016-12-07 14:26:33 -0800506 out << "// cast static functions\n";
507 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700508
Yifan Hong3d746092016-12-07 14:26:33 -0800509 for (const Interface *superType : iface->typeChain()) {
Yifan Hong7783bb02017-03-29 03:39:09 -0700510 out << "static ::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -0800511 << childTypeResult
Yifan Hong7783bb02017-03-29 03:39:09 -0700512 << "> castFrom("
Yifan Hong3d746092016-12-07 14:26:33 -0800513 << superType->getCppArgumentType()
514 << " parent"
Yifan Hong7783bb02017-03-29 03:39:09 -0700515 << ", bool emitError = false);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700516 }
517
Steven Morelandd39133b2016-11-11 12:30:08 -0800518 out << "\nstatic const char* descriptor;\n\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700519
Yifan Hongc8934042016-11-17 17:10:52 -0800520 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800521 out << "// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800522 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800523 declareServiceManagerInteractions(out, iface->localName());
Yifan Hongc8934042016-11-17 17:10:52 -0800524 }
Andreas Huber881227d2016-08-02 14:20:21 -0700525 }
526
527 if (isInterface) {
528 out.unindent();
529
Andreas Hubere3f769a2016-10-10 10:54:44 -0700530 out << "};\n\n";
531 }
532
533 err = mRootScope->emitGlobalTypeDeclarations(out);
534
535 if (err != OK) {
536 return err;
Andreas Huber881227d2016-08-02 14:20:21 -0700537 }
538
539 out << "\n";
540 enterLeaveNamespace(out, false /* enter */);
541
542 out << "\n#endif // " << guard << "\n";
543
544 return OK;
545}
546
Steven Moreland40786312016-08-16 10:29:40 -0700547status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
548 std::string ifaceName;
Yifan Hong244e82d2016-11-11 11:13:57 -0800549 bool isInterface = AST::isInterface(&ifaceName);
550 const Interface *iface = nullptr;
Yifan Hong244e82d2016-11-11 11:13:57 -0800551 std::string klassName{};
552
553 if(isInterface) {
554 iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800555 klassName = iface->getHwName();
Yifan Hong244e82d2016-11-11 11:13:57 -0800556 } else {
557 klassName = "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700558 }
559
Steven Moreland40786312016-08-16 10:29:40 -0700560 std::string path = outputPath;
561 path.append(mCoordinator->convertPackageRootToPath(mPackage));
562 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
563 path.append(klassName + ".h");
564
Yifan Hong244e82d2016-11-11 11:13:57 -0800565 FILE *file = fopen(path.c_str(), "w");
Steven Moreland40786312016-08-16 10:29:40 -0700566
567 if (file == NULL) {
568 return -errno;
569 }
570
571 Formatter out(file);
572
573 const std::string guard = makeHeaderGuard(klassName);
574
575 out << "#ifndef " << guard << "\n";
576 out << "#define " << guard << "\n\n";
577
Yifan Hong244e82d2016-11-11 11:13:57 -0800578 if (isInterface) {
579 generateCppPackageInclude(out, mPackage, ifaceName);
580 } else {
581 generateCppPackageInclude(out, mPackage, "types");
582 }
Steven Moreland40786312016-08-16 10:29:40 -0700583
Steven Morelandee88eed2016-10-31 17:49:00 -0700584 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700585
586 for (const auto &item : mImportedNames) {
587 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800588 generateCppPackageInclude(out, item, "hwtypes");
589 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800590 generateCppPackageInclude(out, item, item.getInterfaceStubName());
591 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700592 }
Steven Moreland40786312016-08-16 10:29:40 -0700593 }
594
595 out << "\n";
596
Martijn Coenen93915102016-09-01 01:35:52 +0200597 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700598 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100599 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700600
601 out << "\n";
602
603 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700604
Yifan Hong244e82d2016-11-11 11:13:57 -0800605 status_t err = mRootScope->emitGlobalHwDeclarations(out);
606 if (err != OK) {
607 return err;
608 }
Steven Moreland40786312016-08-16 10:29:40 -0700609
610 enterLeaveNamespace(out, false /* enter */);
611
612 out << "\n#endif // " << guard << "\n";
613
614 return OK;
615}
616
Andreas Huber881227d2016-08-02 14:20:21 -0700617status_t AST::emitTypeDeclarations(Formatter &out) const {
618 return mRootScope->emitTypeDeclarations(out);
619}
620
Yifan Hong7a118f52016-12-07 11:21:15 -0800621static void wrapPassthroughArg(Formatter &out,
622 const TypedVar *arg, bool addPrefixToName,
623 std::function<void(void)> handleError) {
624 if (!arg->type().isInterface()) {
625 return;
626 }
627 std::string name = (addPrefixToName ? "_hidl_out_" : "") + arg->name();
628 std::string wrappedName = (addPrefixToName ? "_hidl_out_wrapped_" : "_hidl_wrapped_")
629 + arg->name();
630 const Interface &iface = static_cast<const Interface &>(arg->type());
631 out << iface.getCppStackType() << " " << wrappedName << ";\n";
632 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
633 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
634 out << wrappedName
635 << " = "
636 << iface.fqName().cppName()
Yifan Hongeb4fe782017-04-20 18:12:05 -0700637 << "::castFrom(::android::hardware::details::wrapPassthrough<"
638 << iface.fqName().cppName()
639 << ">("
Yifan Hong7a118f52016-12-07 11:21:15 -0800640 << name << "));\n";
641 out.sIf(wrappedName + " == nullptr", [&] {
642 // Fatal error. Happens when the BsFoo class is not found in the binary
643 // or any dynamic libraries.
644 handleError();
645 }).endl();
646 }).sElse([&] {
647 out << wrappedName << " = " << name << ";\n";
648 }).endl().endl();
649}
650
Steven Moreland69e7c702016-09-09 11:16:32 -0700651status_t AST::generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700652 const Method *method) const {
653 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700654
655 out << " {\n";
656 out.indent();
657
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800658 if (method->isHidlReserved()
659 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
660 method->cppImpl(IMPL_PASSTHROUGH, out);
661 out.unindent();
662 out << "}\n\n";
663 return OK;
664 }
665
Steven Moreland69e7c702016-09-09 11:16:32 -0700666 const bool returnsValue = !method->results().empty();
667 const TypedVar *elidedReturn = method->canElideCallback();
668
Steven Moreland67f67b42016-09-29 08:59:02 -0700669 if (returnsValue && elidedReturn == nullptr) {
670 generateCheckNonNull(out, "_hidl_cb");
671 }
672
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700673 generateCppInstrumentationCall(
674 out,
675 InstrumentationEvent::PASSTHROUGH_ENTRY,
676 method);
677
Yifan Hong7a118f52016-12-07 11:21:15 -0800678
679 for (const auto &arg : method->args()) {
680 wrapPassthroughArg(out, arg, false /* addPrefixToName */, [&] {
681 out << "return ::android::hardware::Status::fromExceptionCode(\n";
682 out.indent(2, [&] {
683 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800684 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800685 });
686 });
687 }
688
689 out << "auto _hidl_error = ::android::hardware::Void();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700690 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700691
692 if (method->isOneway()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800693 out << "addOnewayTask([this, &_hidl_error";
Steven Moreland69e7c702016-09-09 11:16:32 -0700694 for (const auto &arg : method->args()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800695 out << ", "
696 << (arg->type().isInterface() ? "_hidl_wrapped_" : "")
697 << arg->name();
Steven Moreland69e7c702016-09-09 11:16:32 -0700698 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700699 out << "] {\n";
700 out.indent();
701 out << "this->";
Steven Moreland69e7c702016-09-09 11:16:32 -0700702 }
703
704 out << "mImpl->"
705 << method->name()
706 << "(";
707
Yifan Hong7d234ea2017-03-30 15:40:22 -0700708 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800709 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Yifan Hong7d234ea2017-03-30 15:40:22 -0700710 });
Steven Moreland69e7c702016-09-09 11:16:32 -0700711 if (returnsValue && elidedReturn == nullptr) {
712 if (!method->args().empty()) {
713 out << ", ";
714 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800715 out << "[&](";
Yifan Hong7d234ea2017-03-30 15:40:22 -0700716 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800717 out << "const auto &_hidl_out_"
718 << arg->name();
Yifan Hong7d234ea2017-03-30 15:40:22 -0700719 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800720
721 out << ") {\n";
722 out.indent();
723 status_t status = generateCppInstrumentationCall(
724 out,
725 InstrumentationEvent::PASSTHROUGH_EXIT,
726 method);
727 if (status != OK) {
728 return status;
729 }
730
Yifan Hong7a118f52016-12-07 11:21:15 -0800731 for (const auto &arg : method->results()) {
732 wrapPassthroughArg(out, arg, true /* addPrefixToName */, [&] {
733 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
734 out.indent(2, [&] {
735 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800736 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800737 });
738 out << "return;\n";
739 });
740 }
741
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800742 out << "_hidl_cb(";
Yifan Hong7d234ea2017-03-30 15:40:22 -0700743 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800744 out << (arg->type().isInterface() ? "_hidl_out_wrapped_" : "_hidl_out_")
745 << arg->name();
Yifan Hong7d234ea2017-03-30 15:40:22 -0700746 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800747 out << ");\n";
748 out.unindent();
749 out << "});\n\n";
750 } else {
751 out << ");\n\n";
752 if (elidedReturn != nullptr) {
753 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -0800754 << " _hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800755 << elidedReturn->name()
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000756 << " = _hidl_return;\n";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800757 }
758 status_t status = generateCppInstrumentationCall(
759 out,
760 InstrumentationEvent::PASSTHROUGH_EXIT,
761 method);
762 if (status != OK) {
763 return status;
764 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700765 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700766
767 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700768 out.unindent();
769 out << "});\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700770 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700771
772 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700773
774 out.unindent();
775 out << "}\n";
776
777 return OK;
778}
779
Yifan Hong068c5522016-10-31 14:07:25 -0700780status_t AST::generateMethods(Formatter &out, MethodGenerator gen) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700781
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700782 const Interface *iface = mRootScope->getInterface();
783
Yifan Hong10fe0b52016-10-19 14:20:17 -0700784 const Interface *prevIterface = nullptr;
785 for (const auto &tuple : iface->allMethodsFromRoot()) {
786 const Method *method = tuple.method();
787 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700788
Yifan Hong10fe0b52016-10-19 14:20:17 -0700789 if(prevIterface != superInterface) {
790 if (prevIterface != nullptr) {
791 out << "\n";
792 }
793 out << "// Methods from "
794 << superInterface->fullName()
795 << " follow.\n";
796 prevIterface = superInterface;
797 }
Yifan Hong068c5522016-10-31 14:07:25 -0700798 status_t err = gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700799
Yifan Hong10fe0b52016-10-19 14:20:17 -0700800 if (err != OK) {
801 return err;
802 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700803 }
804
Yifan Hong10fe0b52016-10-19 14:20:17 -0700805 out << "\n";
806
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700807 return OK;
808}
809
Andreas Huberb82318c2016-08-02 14:45:54 -0700810status_t AST::generateStubHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700811 std::string ifaceName;
812 if (!AST::isInterface(&ifaceName)) {
813 // types.hal does not get a stub header.
814 return OK;
815 }
816
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700817 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800818 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -0700819
Andreas Huberb82318c2016-08-02 14:45:54 -0700820 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700821 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700822 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700823 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700824 path.append(".h");
825
Andreas Huberd2943e12016-08-05 11:59:31 -0700826 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700827 FILE *file = fopen(path.c_str(), "w");
828
829 if (file == NULL) {
830 return -errno;
831 }
832
833 Formatter out(file);
834
Steven Moreland40786312016-08-16 10:29:40 -0700835 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700836
837 out << "#ifndef " << guard << "\n";
838 out << "#define " << guard << "\n\n";
839
Yifan Hongeefe4f22017-01-04 15:32:42 -0800840 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700841 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700842
843 enterLeaveNamespace(out, true /* enter */);
844 out << "\n";
845
846 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800847 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100848 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800849 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000850 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100851 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800852 out << " : public "
853 << gIBaseFqName.getInterfaceStubFqName().cppName()
854 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100855 }
Andreas Huber881227d2016-08-02 14:20:21 -0700856
857 out.indent();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800858 out << "explicit "
859 << klassName
Steven Moreland40786312016-08-16 10:29:40 -0700860 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100861 << "\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800862 out << "explicit "
863 << klassName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100864 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800865 << " const std::string& HidlInstrumentor_package,"
866 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700867 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700868 out << "::android::status_t onTransact(\n";
869 out.indent();
870 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700871 out << "uint32_t _hidl_code,\n";
872 out << "const ::android::hardware::Parcel &_hidl_data,\n";
873 out << "::android::hardware::Parcel *_hidl_reply,\n";
874 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700875 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700876 out.unindent();
877 out.unindent();
878
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100879 out << "::android::sp<" << ifaceName << "> getImpl() { return _hidl_mImpl; };\n";
880 out.unindent();
881 out << "private:\n";
882 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800883
884 status_t err = generateMethods(out, [&](const Method *method, const Interface *iface) {
885 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
886 return OK;
887 }
888 const bool returnsValue = !method->results().empty();
889 const TypedVar *elidedReturn = method->canElideCallback();
890
891 if (elidedReturn == nullptr && returnsValue) {
892 out << "using " << method->name() << "_cb = "
893 << iface->fqName().cppName()
894 << "::" << method->name() << "_cb;\n";
895 }
896 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800897 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800898 return OK;
899 });
900 if (err != OK) {
901 return err;
902 }
903
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100904 out << "::android::sp<" << ifaceName << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700905 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700906 out << "};\n\n";
907
908 enterLeaveNamespace(out, false /* enter */);
909
910 out << "\n#endif // " << guard << "\n";
911
912 return OK;
913}
914
Andreas Huberb82318c2016-08-02 14:45:54 -0700915status_t AST::generateProxyHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700916 std::string ifaceName;
917 if (!AST::isInterface(&ifaceName)) {
918 // types.hal does not get a proxy header.
919 return OK;
920 }
921
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700922 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800923 const std::string proxyName = iface->getProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -0700924
Andreas Huberb82318c2016-08-02 14:45:54 -0700925 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700926 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700927 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Yifan Hongeefe4f22017-01-04 15:32:42 -0800928 path.append(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700929 path.append(".h");
930
Andreas Huberd2943e12016-08-05 11:59:31 -0700931 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700932 FILE *file = fopen(path.c_str(), "w");
933
934 if (file == NULL) {
935 return -errno;
936 }
937
938 Formatter out(file);
939
Yifan Hongeefe4f22017-01-04 15:32:42 -0800940 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700941
942 out << "#ifndef " << guard << "\n";
943 out << "#define " << guard << "\n\n";
944
Martijn Coenen115d4282016-12-19 05:14:04 +0100945 out << "#include <hidl/HidlTransportSupport.h>\n\n";
946
Andreas Huber881227d2016-08-02 14:20:21 -0700947 std::vector<std::string> packageComponents;
948 getPackageAndVersionComponents(
949 &packageComponents, false /* cpp_compatible */);
950
Yifan Hongeefe4f22017-01-04 15:32:42 -0800951 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700952 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700953
954 enterLeaveNamespace(out, true /* enter */);
955 out << "\n";
956
957 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800958 << proxyName
959 << " : public ::android::hardware::BpInterface<"
960 << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000961 << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700962
963 out.indent();
964
Yifan Hongeefe4f22017-01-04 15:32:42 -0800965 out << "explicit "
966 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700967 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700968 << "\n\n";
969
Yifan Hong10fe0b52016-10-19 14:20:17 -0700970 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700971
Yifan Hong068c5522016-10-31 14:07:25 -0700972 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
973 method->generateCppSignature(out);
974 out << " override;\n";
975 return OK;
976 });
Steven Moreland9c387612016-09-07 09:54:26 -0700977
978 if (err != OK) {
979 return err;
980 }
Andreas Huber881227d2016-08-02 14:20:21 -0700981
982 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100983 out << "private:\n";
984 out.indent();
985 out << "std::mutex _hidl_mMutex;\n"
986 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
987 << " _hidl_mDeathRecipients;\n";
988 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700989 out << "};\n\n";
990
991 enterLeaveNamespace(out, false /* enter */);
992
993 out << "\n#endif // " << guard << "\n";
994
995 return OK;
996}
997
Andreas Huberb82318c2016-08-02 14:45:54 -0700998status_t AST::generateAllSource(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700999
Andreas Huberb82318c2016-08-02 14:45:54 -07001000 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -07001001 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -07001002 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -07001003
1004 std::string ifaceName;
1005 std::string baseName;
1006
Yifan Hongfe95aa22016-10-19 17:26:45 -07001007 const Interface *iface = nullptr;
1008 bool isInterface;
Andreas Huber881227d2016-08-02 14:20:21 -07001009 if (!AST::isInterface(&ifaceName)) {
1010 baseName = "types";
1011 isInterface = false;
1012 } else {
Yifan Hongfe95aa22016-10-19 17:26:45 -07001013 iface = mRootScope->getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -07001014 baseName = iface->getBaseName();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001015 isInterface = true;
Andreas Huber881227d2016-08-02 14:20:21 -07001016 }
1017
1018 path.append(baseName);
1019
1020 if (baseName != "types") {
1021 path.append("All");
1022 }
1023
1024 path.append(".cpp");
1025
Andreas Huberd2943e12016-08-05 11:59:31 -07001026 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -07001027 FILE *file = fopen(path.c_str(), "w");
1028
1029 if (file == NULL) {
1030 return -errno;
1031 }
1032
1033 Formatter out(file);
1034
Steven Moreland623c0042017-01-13 14:42:29 -08001035 out << "#define LOG_TAG \""
1036 << mPackage.string() << "::" << baseName
1037 << "\"\n\n";
1038
Steven Moreland05cd4232016-11-21 16:01:12 -08001039 out << "#include <android/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001040 out << "#include <cutils/trace.h>\n";
1041 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001042 if (isInterface) {
Steven Moreland19d5c172016-10-20 19:20:25 -07001043 // This is a no-op for IServiceManager itself.
1044 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
1045
Steven Morelandbec74ed2017-01-25 17:42:35 -08001046 // TODO(b/34274385) remove this
1047 out << "#include <hidl/LegacySupport.h>\n";
1048
Yifan Hongeefe4f22017-01-04 15:32:42 -08001049 generateCppPackageInclude(out, mPackage, iface->getProxyName());
1050 generateCppPackageInclude(out, mPackage, iface->getStubName());
1051 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001052
1053 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -07001054 generateCppPackageInclude(out,
1055 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -08001056 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001057 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -08001058
1059 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001060 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -07001061 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -08001062 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -07001063 }
1064
1065 out << "\n";
1066
1067 enterLeaveNamespace(out, true /* enter */);
1068 out << "\n";
1069
1070 status_t err = generateTypeSource(out, ifaceName);
1071
1072 if (err == OK && isInterface) {
Yifan Hong10fe0b52016-10-19 14:20:17 -07001073 const Interface *iface = mRootScope->getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -07001074
1075 // need to be put here, generateStubSource is using this.
Yifan Hongeefe4f22017-01-04 15:32:42 -08001076 out << "const char* "
1077 << iface->localName()
Yifan Hong10fe0b52016-10-19 14:20:17 -07001078 << "::descriptor(\""
1079 << iface->fqName().string()
1080 << "\");\n\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +01001081 out << "__attribute__((constructor))";
1082 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001083 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001084 out << "::android::hardware::details::gBnConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001085 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001086 << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001087 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001088 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001089 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001090 out << "return new "
1091 << iface->getStubName()
Yifan Hongeb4fe782017-04-20 18:12:05 -07001092 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001093 << iface->localName()
Yifan Hong158655a2016-11-08 12:34:07 -08001094 << " *>(iIntf));\n";
1095 });
Yifan Hongb04de382017-02-06 15:31:52 -08001096 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001097 });
Yifan Honga159f3b2017-03-16 14:53:51 -07001098 out << "::android::hardware::details::gBsConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001099 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001100 << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001101 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001102 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -08001103 << gIBaseFqName.cppName()
1104 << "> {\n";
1105 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001106 out << "return new "
1107 << iface->getPassthroughName()
Yifan Hongeb4fe782017-04-20 18:12:05 -07001108 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001109 << iface->localName()
Yifan Hong7a118f52016-12-07 11:21:15 -08001110 << " *>(iIntf));\n";
1111 });
Yifan Hongb04de382017-02-06 15:31:52 -08001112 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001113 });
Yifan Hong158655a2016-11-08 12:34:07 -08001114 });
Martijn Coenen8adcb652017-02-03 17:37:36 +01001115 out << "};\n\n";
1116 out << "__attribute__((destructor))";
1117 out << "static void static_destructor() {\n";
1118 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001119 out << "::android::hardware::details::gBnConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001120 << iface->localName()
1121 << "::descriptor);\n";
Yifan Honga159f3b2017-03-16 14:53:51 -07001122 out << "::android::hardware::details::gBsConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001123 << iface->localName()
1124 << "::descriptor);\n";
1125 });
1126 out << "};\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001127
Yifan Hongfe95aa22016-10-19 17:26:45 -07001128 err = generateInterfaceSource(out);
1129 }
1130
1131 if (err == OK && isInterface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001132 err = generateProxySource(out, iface->fqName());
Andreas Huber881227d2016-08-02 14:20:21 -07001133 }
1134
1135 if (err == OK && isInterface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001136 err = generateStubSource(out, iface);
Andreas Huber881227d2016-08-02 14:20:21 -07001137 }
1138
Steven Moreland40786312016-08-16 10:29:40 -07001139 if (err == OK && isInterface) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001140 err = generatePassthroughSource(out);
1141 }
1142
1143 if (err == OK && isInterface) {
Steven Moreland9c387612016-09-07 09:54:26 -07001144 const Interface *iface = mRootScope->getInterface();
1145
Yifan Hongc8934042016-11-17 17:10:52 -08001146 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001147 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -08001148 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001149 std::string package = iface->fqName().package()
1150 + iface->fqName().atVersion();
1151
Yifan Hongeefe4f22017-01-04 15:32:42 -08001152 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -08001153 }
Steven Moreland40786312016-08-16 10:29:40 -07001154 }
1155
Andreas Huber02ef12f2017-04-06 11:09:07 -07001156 HidlTypeAssertion::EmitAll(out);
1157 out << "\n";
1158
Andreas Huber881227d2016-08-02 14:20:21 -07001159 enterLeaveNamespace(out, false /* enter */);
1160
1161 return err;
1162}
1163
Steven Moreland67f67b42016-09-29 08:59:02 -07001164// static
1165void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) {
Yifan Honga018ed52016-12-13 16:35:08 -08001166 out.sIf(nonNull + " == nullptr", [&] {
1167 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1168 out.indent(2, [&] {
1169 out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT);\n";
1170 });
1171 }).endl().endl();
Steven Moreland67f67b42016-09-29 08:59:02 -07001172}
1173
Andreas Huber881227d2016-08-02 14:20:21 -07001174status_t AST::generateTypeSource(
1175 Formatter &out, const std::string &ifaceName) const {
1176 return mRootScope->emitTypeDefinitions(out, ifaceName);
1177}
1178
Andreas Hubere7ff2282016-08-16 13:50:03 -07001179void AST::declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -07001180 Formatter &out,
1181 const std::vector<TypedVar *> &args,
1182 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -07001183 if (args.empty()) {
1184 return;
1185 }
1186
1187 for (const auto &arg : args) {
1188 const Type &type = arg->type();
1189
Yifan Hong3b320f82016-11-01 15:15:54 -07001190 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001191 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -07001192 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001193 << ";\n";
1194 }
1195
1196 out << "\n";
1197}
1198
Andreas Huber881227d2016-08-02 14:20:21 -07001199void AST::emitCppReaderWriter(
1200 Formatter &out,
1201 const std::string &parcelObj,
1202 bool parcelObjIsPointer,
1203 const TypedVar *arg,
1204 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -07001205 Type::ErrorMode mode,
1206 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -07001207 const Type &type = arg->type();
1208
Andreas Huber881227d2016-08-02 14:20:21 -07001209 type.emitReaderWriter(
1210 out,
Andreas Huber5e44a292016-09-27 14:52:39 -07001211 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -07001212 parcelObj,
1213 parcelObjIsPointer,
1214 isReader,
1215 mode);
1216}
1217
Yifan Hongbf459bc2016-08-23 16:50:37 -07001218void AST::emitCppResolveReferences(
1219 Formatter &out,
1220 const std::string &parcelObj,
1221 bool parcelObjIsPointer,
1222 const TypedVar *arg,
1223 bool isReader,
1224 Type::ErrorMode mode,
1225 bool addPrefixToName) const {
1226 const Type &type = arg->type();
1227 if(type.needsResolveReferences()) {
1228 type.emitResolveReferences(
1229 out,
1230 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
1231 isReader, // nameIsPointer
1232 parcelObj,
1233 parcelObjIsPointer,
1234 isReader,
1235 mode);
1236 }
1237}
1238
Yifan Hong068c5522016-10-31 14:07:25 -07001239status_t AST::generateProxyMethodSource(Formatter &out,
1240 const std::string &klassName,
1241 const Method *method,
1242 const Interface *superInterface) const {
1243
1244 method->generateCppSignature(out,
1245 klassName,
1246 true /* specify namespaces */);
1247
1248 const bool returnsValue = !method->results().empty();
1249 const TypedVar *elidedReturn = method->canElideCallback();
1250
Steven Moreland41c6d2e2016-11-07 12:26:54 -08001251 out << " {\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001252
1253 out.indent();
1254
Martijn Coenen115d4282016-12-19 05:14:04 +01001255 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
1256 method->cppImpl(IMPL_PROXY, out);
1257 out.unindent();
1258 out << "}\n\n";
1259 return OK;
1260 }
1261
Yifan Hong068c5522016-10-31 14:07:25 -07001262 if (returnsValue && elidedReturn == nullptr) {
1263 generateCheckNonNull(out, "_hidl_cb");
1264 }
1265
1266 status_t status = generateCppInstrumentationCall(
1267 out,
1268 InstrumentationEvent::CLIENT_API_ENTRY,
Yifan Hong068c5522016-10-31 14:07:25 -07001269 method);
1270 if (status != OK) {
1271 return status;
1272 }
1273
1274 out << "::android::hardware::Parcel _hidl_data;\n";
1275 out << "::android::hardware::Parcel _hidl_reply;\n";
1276 out << "::android::status_t _hidl_err;\n";
1277 out << "::android::hardware::Status _hidl_status;\n\n";
1278
1279 declareCppReaderLocals(
1280 out, method->results(), true /* forResults */);
1281
1282 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001283 out << superInterface->fqName().cppName();
Yifan Hong068c5522016-10-31 14:07:25 -07001284 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001285 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1286
Martijn Coenenfff73352017-01-04 16:36:31 +01001287 bool hasInterfaceArgument = false;
Yifan Hong068c5522016-10-31 14:07:25 -07001288 // First DFS: write all buffers and resolve pointers for parent
1289 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001290 if (arg->type().isInterface()) {
1291 hasInterfaceArgument = true;
1292 }
Yifan Hong068c5522016-10-31 14:07:25 -07001293 emitCppReaderWriter(
1294 out,
1295 "_hidl_data",
1296 false /* parcelObjIsPointer */,
1297 arg,
1298 false /* reader */,
1299 Type::ErrorMode_Goto,
1300 false /* addPrefixToName */);
1301 }
1302
1303 // Second DFS: resolve references.
1304 for (const auto &arg : method->args()) {
1305 emitCppResolveReferences(
1306 out,
1307 "_hidl_data",
1308 false /* parcelObjIsPointer */,
1309 arg,
1310 false /* reader */,
1311 Type::ErrorMode_Goto,
1312 false /* addPrefixToName */);
1313 }
1314
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001315 if (hasInterfaceArgument) {
1316 // Start binder threadpool to handle incoming transactions
1317 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1318 }
Yifan Hong068c5522016-10-31 14:07:25 -07001319 out << "_hidl_err = remote()->transact("
1320 << method->getSerialId()
1321 << " /* "
1322 << method->name()
1323 << " */, _hidl_data, &_hidl_reply";
1324
1325 if (method->isOneway()) {
1326 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
1327 }
1328 out << ");\n";
1329
1330 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1331
1332 if (!method->isOneway()) {
Yifan Hong859e53f2016-11-14 19:08:24 -08001333 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001334 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1335 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1336
1337
1338 // First DFS: write all buffers and resolve pointers for parent
1339 for (const auto &arg : method->results()) {
1340 emitCppReaderWriter(
1341 out,
1342 "_hidl_reply",
1343 false /* parcelObjIsPointer */,
1344 arg,
1345 true /* reader */,
1346 Type::ErrorMode_Goto,
1347 true /* addPrefixToName */);
1348 }
1349
1350 // Second DFS: resolve references.
1351 for (const auto &arg : method->results()) {
1352 emitCppResolveReferences(
1353 out,
1354 "_hidl_reply",
1355 false /* parcelObjIsPointer */,
1356 arg,
1357 true /* reader */,
1358 Type::ErrorMode_Goto,
1359 true /* addPrefixToName */);
1360 }
1361
1362 if (returnsValue && elidedReturn == nullptr) {
1363 out << "_hidl_cb(";
1364
Yifan Hong7d234ea2017-03-30 15:40:22 -07001365 out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) {
Yifan Hong068c5522016-10-31 14:07:25 -07001366 if (arg->type().resultNeedsDeref()) {
1367 out << "*";
1368 }
1369 out << "_hidl_out_" << arg->name();
Yifan Hong7d234ea2017-03-30 15:40:22 -07001370 });
Yifan Hong068c5522016-10-31 14:07:25 -07001371
1372 out << ");\n\n";
1373 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001374 }
1375 status = generateCppInstrumentationCall(
1376 out,
1377 InstrumentationEvent::CLIENT_API_EXIT,
1378 method);
1379 if (status != OK) {
1380 return status;
Yifan Hong068c5522016-10-31 14:07:25 -07001381 }
1382
1383 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001384 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1385 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001386 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001387 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1388 } else {
1389 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1390 out << "return ::android::hardware::Return<void>();\n\n";
1391 }
1392
1393 out.unindent();
1394 out << "_hidl_error:\n";
1395 out.indent();
1396 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1397 out << "return ::android::hardware::Return<";
1398 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001399 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001400 } else {
1401 out << "void";
1402 }
1403 out << ">(_hidl_status);\n";
1404
1405 out.unindent();
1406 out << "}\n\n";
1407 return OK;
1408}
1409
Andreas Huber881227d2016-08-02 14:20:21 -07001410status_t AST::generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -08001411 Formatter &out, const FQName &fqName) const {
1412 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001413
1414 out << klassName
1415 << "::"
1416 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001417 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001418
1419 out.indent();
1420 out.indent();
1421
1422 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001423 << "<"
1424 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001425 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001426 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001427 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001428 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001429 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001430 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001431
Andreas Huber881227d2016-08-02 14:20:21 -07001432 out.unindent();
1433 out.unindent();
1434 out << "}\n\n";
1435
Yifan Hong068c5522016-10-31 14:07:25 -07001436 status_t err = generateMethods(out, [&](const Method *method, const Interface *superInterface) {
1437 return generateProxyMethodSource(out, klassName, method, superInterface);
1438 });
Andreas Huber881227d2016-08-02 14:20:21 -07001439
Yifan Hong068c5522016-10-31 14:07:25 -07001440 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001441}
1442
1443status_t AST::generateStubSource(
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001444 Formatter &out,
Yifan Hongeefe4f22017-01-04 15:32:42 -08001445 const Interface *iface) const {
1446 const std::string interfaceName = iface->localName();
1447 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001448
Steven Moreland40786312016-08-16 10:29:40 -07001449 out << klassName
1450 << "::"
1451 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001452 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001453
1454 out.indent();
1455 out.indent();
1456
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001457 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001458 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001459 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001460 out << ": "
1461 << gIBaseFqName.getInterfaceStubFqName().cppName()
1462 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001463 }
1464
1465 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001466 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001467 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001468 << "\") { \n";
1469 out.indent();
1470 out << "_hidl_mImpl = _hidl_impl;\n";
1471 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001472
1473 out.unindent();
1474 out.unindent();
1475 out << "}\n\n";
1476
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001477 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001478 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001479 // class properly.
1480 out << klassName
1481 << "::"
1482 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001483 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1484 << " const std::string &HidlInstrumentor_package,"
1485 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001486
1487 out.indent();
1488 out.indent();
1489
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001490 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001491 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001492 out.indent();
1493 out << "_hidl_mImpl = _hidl_impl;\n";
1494 out.unindent();
1495
1496 out.unindent();
1497 out.unindent();
1498 out << "}\n\n";
1499 }
1500
Yifan Hongbcffce22017-02-01 15:52:06 -08001501 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1502 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
1503 return OK;
1504 }
1505 method->generateCppSignature(out, iface->getStubName());
1506 out << " ";
1507 out.block([&] {
1508 method->cppImpl(IMPL_STUB_IMPL, out);
1509 }).endl();
1510 return OK;
1511 });
Steven Moreland60818632017-02-04 00:33:42 -08001512 if (err != OK) {
1513 return err;
1514 }
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001515
Andreas Huber881227d2016-08-02 14:20:21 -07001516 out << "::android::status_t " << klassName << "::onTransact(\n";
1517
1518 out.indent();
1519 out.indent();
1520
Iliyan Malchev549e2592016-08-10 08:59:12 -07001521 out << "uint32_t _hidl_code,\n"
1522 << "const ::android::hardware::Parcel &_hidl_data,\n"
1523 << "::android::hardware::Parcel *_hidl_reply,\n"
1524 << "uint32_t _hidl_flags,\n"
1525 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001526
1527 out.unindent();
1528
Iliyan Malchev549e2592016-08-10 08:59:12 -07001529 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001530 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001531 out.indent();
1532
Yifan Hong10fe0b52016-10-19 14:20:17 -07001533 for (const auto &tuple : iface->allMethodsFromRoot()) {
1534 const Method *method = tuple.method();
1535 const Interface *superInterface = tuple.interface();
1536 out << "case "
1537 << method->getSerialId()
1538 << " /* "
1539 << method->name()
1540 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001541
Yifan Hong10fe0b52016-10-19 14:20:17 -07001542 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001543
Yifan Hong10fe0b52016-10-19 14:20:17 -07001544 status_t err =
1545 generateStubSourceForMethod(out, superInterface, method);
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001546
Yifan Hong10fe0b52016-10-19 14:20:17 -07001547 if (err != OK) {
1548 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001549 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001550
1551 out.unindent();
1552 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001553 }
1554
1555 out << "default:\n{\n";
1556 out.indent();
1557
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001558 out << "return onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001559
1560 out.indent();
1561 out.indent();
1562
Iliyan Malchev549e2592016-08-10 08:59:12 -07001563 out << "_hidl_code, _hidl_data, _hidl_reply, "
1564 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001565
1566 out.unindent();
1567 out.unindent();
1568
1569 out.unindent();
1570 out << "}\n";
1571
1572 out.unindent();
1573 out << "}\n\n";
1574
Yifan Honga018ed52016-12-13 16:35:08 -08001575 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1576 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1577 out.indent(2, [&] {
1578 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1579 out << "_hidl_reply);\n";
1580 });
1581 });
Andreas Huber881227d2016-08-02 14:20:21 -07001582
Iliyan Malchev549e2592016-08-10 08:59:12 -07001583 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001584
1585 out.unindent();
1586 out << "}\n\n";
1587
1588 return OK;
1589}
1590
1591status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001592 Formatter &out, const Interface *iface, const Method *method) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001593 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1594 method->cppImpl(IMPL_STUB, out);
1595 out << "break;\n";
1596 return OK;
1597 }
1598
Yifan Hongeefe4f22017-01-04 15:32:42 -08001599 out << "if (!_hidl_data.enforceInterface("
1600 << iface->fullName()
1601 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001602
Andreas Huber881227d2016-08-02 14:20:21 -07001603 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001604 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001605 out << "break;\n";
1606 out.unindent();
1607 out << "}\n\n";
1608
Andreas Huber5e44a292016-09-27 14:52:39 -07001609 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001610
Yifan Hongbf459bc2016-08-23 16:50:37 -07001611 // First DFS: write buffers
Andreas Huber881227d2016-08-02 14:20:21 -07001612 for (const auto &arg : method->args()) {
1613 emitCppReaderWriter(
1614 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001615 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001616 false /* parcelObjIsPointer */,
1617 arg,
1618 true /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001619 Type::ErrorMode_Break,
1620 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001621 }
1622
Yifan Hongbf459bc2016-08-23 16:50:37 -07001623 // Second DFS: resolve references
1624 for (const auto &arg : method->args()) {
1625 emitCppResolveReferences(
1626 out,
1627 "_hidl_data",
1628 false /* parcelObjIsPointer */,
1629 arg,
1630 true /* reader */,
1631 Type::ErrorMode_Break,
1632 false /* addPrefixToName */);
1633 }
1634
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001635 status_t status = generateCppInstrumentationCall(
1636 out,
1637 InstrumentationEvent::SERVER_API_ENTRY,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001638 method);
1639 if (status != OK) {
1640 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001641 }
1642
Andreas Huber881227d2016-08-02 14:20:21 -07001643 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001644 const TypedVar *elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -08001645 const std::string callee =
1646 (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL))
1647 ? "this" : "_hidl_mImpl";
Andreas Huber881227d2016-08-02 14:20:21 -07001648
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001649 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001650 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001651 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001652 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001653 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001654 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001655 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001656
Yifan Hong7d234ea2017-03-30 15:40:22 -07001657 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001658 if (arg->type().resultNeedsDeref()) {
1659 out << "*";
1660 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001661 out << arg->name();
Yifan Hong7d234ea2017-03-30 15:40:22 -07001662 });
Andreas Huber881227d2016-08-02 14:20:21 -07001663
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001664 out << ");\n\n";
Yifan Hong859e53f2016-11-14 19:08:24 -08001665 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1666 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001667
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001668 elidedReturn->type().emitReaderWriter(
1669 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001670 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001671 "_hidl_reply",
1672 true, /* parcelObjIsPointer */
1673 false, /* isReader */
1674 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001675
Yifan Hongbf459bc2016-08-23 16:50:37 -07001676 emitCppResolveReferences(
1677 out,
1678 "_hidl_reply",
1679 true /* parcelObjIsPointer */,
1680 elidedReturn,
1681 false /* reader */,
1682 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001683 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001684
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001685 status_t status = generateCppInstrumentationCall(
1686 out,
1687 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001688 method);
1689 if (status != OK) {
1690 return status;
1691 }
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001692
Iliyan Malchev549e2592016-08-10 08:59:12 -07001693 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001694 } else {
1695 if (returnsValue) {
1696 out << "bool _hidl_callbackCalled = false;\n\n";
1697 }
Andreas Huber881227d2016-08-02 14:20:21 -07001698
Yifan Hongcd2ae452017-01-31 14:33:40 -08001699 out << callee << "->" << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001700
Yifan Hong7d234ea2017-03-30 15:40:22 -07001701 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001702 if (arg->type().resultNeedsDeref()) {
1703 out << "*";
1704 }
1705
1706 out << arg->name();
Yifan Hong7d234ea2017-03-30 15:40:22 -07001707 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001708
1709 if (returnsValue) {
Yifan Hong7d234ea2017-03-30 15:40:22 -07001710 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001711 out << ", ";
1712 }
1713
1714 out << "[&](";
1715
Yifan Hong7d234ea2017-03-30 15:40:22 -07001716 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001717 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong7d234ea2017-03-30 15:40:22 -07001718 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001719
1720 out << ") {\n";
1721 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001722 out << "if (_hidl_callbackCalled) {\n";
1723 out.indent();
1724 out << "LOG_ALWAYS_FATAL(\""
1725 << method->name()
1726 << ": _hidl_cb called a second time, but must be called once.\");\n";
1727 out.unindent();
1728 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001729 out << "_hidl_callbackCalled = true;\n\n";
1730
Yifan Hong859e53f2016-11-14 19:08:24 -08001731 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1732 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001733
Yifan Hongbf459bc2016-08-23 16:50:37 -07001734 // First DFS: buffers
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001735 for (const auto &arg : method->results()) {
1736 emitCppReaderWriter(
1737 out,
1738 "_hidl_reply",
1739 true /* parcelObjIsPointer */,
1740 arg,
1741 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001742 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001743 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001744 }
1745
Yifan Hongbf459bc2016-08-23 16:50:37 -07001746 // Second DFS: resolve references
1747 for (const auto &arg : method->results()) {
1748 emitCppResolveReferences(
1749 out,
1750 "_hidl_reply",
1751 true /* parcelObjIsPointer */,
1752 arg,
1753 false /* reader */,
1754 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001755 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001756 }
1757
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001758 status_t status = generateCppInstrumentationCall(
1759 out,
1760 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001761 method);
1762 if (status != OK) {
1763 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001764 }
1765
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001766 out << "_hidl_cb(*_hidl_reply);\n";
1767
1768 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001769 out << "});\n\n";
1770 } else {
1771 out << ");\n\n";
1772 status_t status = generateCppInstrumentationCall(
1773 out,
1774 InstrumentationEvent::SERVER_API_EXIT,
1775 method);
1776 if (status != OK) {
1777 return status;
1778 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001779 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001780
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001781 if (returnsValue) {
1782 out << "if (!_hidl_callbackCalled) {\n";
1783 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001784 out << "LOG_ALWAYS_FATAL(\""
1785 << method->name()
1786 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001787 out.unindent();
1788 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001789 } else {
1790 out << "::android::hardware::writeToParcel("
1791 << "::android::hardware::Status::ok(), "
1792 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001793 }
Andreas Huber881227d2016-08-02 14:20:21 -07001794 }
1795
1796 out << "break;\n";
1797
1798 return OK;
1799}
1800
Steven Moreland69e7c702016-09-09 11:16:32 -07001801status_t AST::generatePassthroughHeader(const std::string &outputPath) const {
1802 std::string ifaceName;
1803 if (!AST::isInterface(&ifaceName)) {
1804 // types.hal does not get a stub header.
1805 return OK;
1806 }
1807
1808 const Interface *iface = mRootScope->getInterface();
1809
Yifan Hongeefe4f22017-01-04 15:32:42 -08001810 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001811
1812 bool supportOneway = iface->hasOnewayMethods();
1813
1814 std::string path = outputPath;
1815 path.append(mCoordinator->convertPackageRootToPath(mPackage));
1816 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
1817 path.append(klassName);
1818 path.append(".h");
1819
1820 CHECK(Coordinator::MakeParentHierarchy(path));
1821 FILE *file = fopen(path.c_str(), "w");
1822
1823 if (file == NULL) {
1824 return -errno;
1825 }
1826
1827 Formatter out(file);
1828
1829 const std::string guard = makeHeaderGuard(klassName);
1830
1831 out << "#ifndef " << guard << "\n";
1832 out << "#define " << guard << "\n\n";
1833
1834 std::vector<std::string> packageComponents;
1835 getPackageAndVersionComponents(
1836 &packageComponents, false /* cpp_compatible */);
1837
Yifan Hongb0949432016-12-15 15:32:24 -08001838 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001839 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001840
1841 generateCppPackageInclude(out, mPackage, ifaceName);
1842 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001843
Yifan Hong7a118f52016-12-07 11:21:15 -08001844 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001845 if (supportOneway) {
Yifan Hong2cbc1472016-10-25 19:02:40 -07001846 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001847 }
1848
1849 enterLeaveNamespace(out, true /* enter */);
1850 out << "\n";
1851
1852 out << "struct "
1853 << klassName
1854 << " : " << ifaceName
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001855 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001856
1857 out.indent();
1858 out << "explicit "
1859 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001860 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001861 << ifaceName
1862 << "> impl);\n";
1863
Yifan Hong068c5522016-10-31 14:07:25 -07001864 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1865 return generatePassthroughMethod(out, method);
1866 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001867
1868 if (err != OK) {
1869 return err;
1870 }
1871
1872 out.unindent();
1873 out << "private:\n";
1874 out.indent();
Steven Morelandc46e9842016-11-02 13:21:26 -07001875 out << "const ::android::sp<" << ifaceName << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001876
1877 if (supportOneway) {
Yifan Hongef91d362017-03-20 17:18:13 -07001878 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001879
1880 out << "\n";
1881
1882 out << "::android::hardware::Return<void> addOnewayTask("
1883 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001884 }
1885
1886 out.unindent();
1887
1888 out << "};\n\n";
1889
1890 enterLeaveNamespace(out, false /* enter */);
1891
1892 out << "\n#endif // " << guard << "\n";
1893
1894 return OK;
1895}
1896
Yifan Hongfe95aa22016-10-19 17:26:45 -07001897status_t AST::generateInterfaceSource(Formatter &out) const {
1898 const Interface *iface = mRootScope->getInterface();
1899
Yifan Hong2d7126b2016-10-20 15:12:57 -07001900 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001901 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001902
Steven Morelandd4b068a2017-03-20 06:30:51 -07001903 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1904 bool reserved = method->isHidlReserved();
1905
1906 if (!reserved) {
1907 out << "// no default implementation for: ";
1908 }
1909 method->generateCppSignature(out, iface->localName());
1910 if (reserved) {
1911 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001912 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001913 }).endl();
1914 }
1915
1916 out << "\n";
1917
1918 return OK;
1919 });
1920 if (err != OK) {
1921 return err;
1922 }
1923
Yifan Hong3d746092016-12-07 14:26:33 -08001924 for (const Interface *superType : iface->typeChain()) {
Yifan Hong7783bb02017-03-29 03:39:09 -07001925 out << "// static \n::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -08001926 << childTypeResult
Yifan Hong7783bb02017-03-29 03:39:09 -07001927 << "> "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001928 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001929 << "::castFrom("
1930 << superType->getCppArgumentType()
Yifan Hong7783bb02017-03-29 03:39:09 -07001931 << " parent, bool "
1932 << (iface == superType ? "/* emitError */" : "emitError")
1933 << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001934 out.indent();
1935 if (iface == superType) {
1936 out << "return parent;\n";
1937 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001938 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001939 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001940 << superType->fqName().cppName() << ", "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001941 << iface->getProxyName() << ", "
Yifan Hong51a65092017-01-04 15:41:44 -08001942 << superType->getProxyFqName().cppName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001943 << ">(\n";
1944 out.indent();
1945 out.indent();
1946 out << "parent, \""
1947 << iface->fqName().string()
Yifan Hong7783bb02017-03-29 03:39:09 -07001948 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001949 out.unindent();
1950 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001951 }
Yifan Hong3d746092016-12-07 14:26:33 -08001952 out.unindent();
1953 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001954 }
1955
1956 return OK;
1957}
1958
Steven Moreland69e7c702016-09-09 11:16:32 -07001959status_t AST::generatePassthroughSource(Formatter &out) const {
1960 const Interface *iface = mRootScope->getInterface();
1961
Yifan Hongeefe4f22017-01-04 15:32:42 -08001962 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001963
1964 out << klassName
1965 << "::"
1966 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001967 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001968 << iface->fullName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001969 << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001970 << mPackage.string()
1971 << "\", \""
1972 << iface->localName()
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001973 << "\"), mImpl(impl) {";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001974 if (iface->hasOnewayMethods()) {
1975 out << "\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001976 out.indent([&] {
Yifan Hongf01dad42017-03-20 19:03:11 -07001977 out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001978 });
1979 }
1980 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001981
1982 if (iface->hasOnewayMethods()) {
1983 out << "::android::hardware::Return<void> "
1984 << klassName
1985 << "::addOnewayTask(std::function<void(void)> fun) {\n";
1986 out.indent();
Yifan Hong2cbc1472016-10-25 19:02:40 -07001987 out << "if (!mOnewayQueue.push(fun)) {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001988 out.indent();
Steven Moreland67f67b42016-09-29 08:59:02 -07001989 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1990 out.indent();
1991 out.indent();
1992 out << "::android::hardware::Status::EX_TRANSACTION_FAILED);\n";
1993 out.unindent();
1994 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07001995 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07001996 out << "}\n";
1997
Steven Morelandd366c262016-10-11 15:29:10 -07001998 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001999
2000 out.unindent();
2001 out << "}\n\n";
2002
2003
2004 }
2005
2006 return OK;
2007}
2008
Martijn Coenen7b295242016-11-04 16:52:56 +01002009status_t AST::generateCppAtraceCall(Formatter &out,
2010 InstrumentationEvent event,
2011 const Method *method) const {
2012 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08002013 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01002014 switch (event) {
2015 case SERVER_API_ENTRY:
2016 {
2017 out << "atrace_begin(ATRACE_TAG_HAL, \""
2018 << baseString + "::server\");\n";
2019 break;
2020 }
2021 case CLIENT_API_ENTRY:
2022 {
2023 out << "atrace_begin(ATRACE_TAG_HAL, \""
2024 << baseString + "::client\");\n";
2025 break;
2026 }
2027 case PASSTHROUGH_ENTRY:
2028 {
2029 out << "atrace_begin(ATRACE_TAG_HAL, \""
2030 << baseString + "::passthrough\");\n";
2031 break;
2032 }
2033 case SERVER_API_EXIT:
2034 case CLIENT_API_EXIT:
2035 case PASSTHROUGH_EXIT:
2036 {
2037 out << "atrace_end(ATRACE_TAG_HAL);\n";
2038 break;
2039 }
2040 default:
2041 {
2042 LOG(ERROR) << "Unsupported instrumentation event: " << event;
2043 return UNKNOWN_ERROR;
2044 }
2045 }
2046
2047 return OK;
2048}
2049
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002050status_t AST::generateCppInstrumentationCall(
2051 Formatter &out,
2052 InstrumentationEvent event,
Steven Moreland031ccf12016-10-31 15:54:38 -07002053 const Method *method) const {
Martijn Coenen7b295242016-11-04 16:52:56 +01002054 status_t err = generateCppAtraceCall(out, event, method);
2055 if (err != OK) {
2056 return err;
2057 }
2058
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002059 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
2060 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002061 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002062 std::string event_str = "";
2063 switch (event) {
2064 case SERVER_API_ENTRY:
2065 {
2066 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
2067 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002068 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002069 << (arg->type().resultNeedsDeref() ? "" : "&")
2070 << arg->name()
2071 << ");\n";
2072 }
2073 break;
2074 }
2075 case SERVER_API_EXIT:
2076 {
2077 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07002078 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002079 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07002080 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002081 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002082 }
2083 break;
2084 }
2085 case CLIENT_API_ENTRY:
2086 {
2087 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
2088 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002089 out << "_hidl_args.push_back((void *)&"
2090 << arg->name()
2091 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002092 }
2093 break;
2094 }
2095 case CLIENT_API_EXIT:
2096 {
2097 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
2098 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002099 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002100 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002101 << "_hidl_out_"
2102 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002103 << ");\n";
2104 }
2105 break;
2106 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002107 case PASSTHROUGH_ENTRY:
2108 {
2109 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
2110 for (const auto &arg : method->args()) {
2111 out << "_hidl_args.push_back((void *)&"
2112 << arg->name()
2113 << ");\n";
2114 }
2115 break;
2116 }
2117 case PASSTHROUGH_EXIT:
2118 {
2119 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002120 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002121 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002122 << arg->name()
2123 << ");\n";
2124 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002125 break;
2126 }
Steven Moreland031ccf12016-10-31 15:54:38 -07002127 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002128 {
Steven Moreland031ccf12016-10-31 15:54:38 -07002129 LOG(ERROR) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002130 return UNKNOWN_ERROR;
2131 }
2132 }
2133
Steven Moreland031ccf12016-10-31 15:54:38 -07002134 const Interface *iface = mRootScope->getInterface();
2135
Steven Moreland1ab31442016-11-03 18:37:51 -07002136 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002137 out.indent();
2138 out << "callback("
2139 << event_str
2140 << ", \""
2141 << mPackage.package()
2142 << "\", \""
Yifan Hong90ea87f2016-11-01 14:25:47 -07002143 << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002144 << "\", \""
2145 << iface->localName()
2146 << "\", \""
2147 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002148 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002149 out.unindent();
2150 out << "}\n";
2151 out.unindent();
2152 out << "}\n\n";
2153
2154 return OK;
2155}
2156
Andreas Huber881227d2016-08-02 14:20:21 -07002157} // namespace android