blob: 68af9748d03ce30566b9429323a9e9d3e4d121a6 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Huber881227d2016-08-02 14:20:21 -070017#include "AST.h"
18
19#include "Coordinator.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070020#include "EnumType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070021#include "Interface.h"
Andreas Huber6755e9d2017-04-06 11:09:07 -070022#include "HidlTypeAssertion.h"
Andreas Huber881227d2016-08-02 14:20:21 -070023#include "Method.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070024#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070025#include "Scope.h"
26
Andreas Huberdca261f2016-08-04 13:47:51 -070027#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland5708edf2016-11-04 15:33:31 +000029#include <hidl-util/StringHelper.h>
Andreas Huber881227d2016-08-02 14:20:21 -070030#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070031#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070032#include <vector>
33
34namespace android {
35
Andreas Huberb82318c2016-08-02 14:45:54 -070036status_t AST::generateCpp(const std::string &outputPath) const {
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 Moreland038903b2017-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 Moreland038903b2017-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 Moreland038903b2017-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 Moreland038903b2017-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
Steven Moreland90831502017-03-27 12:08:40 -0700151 out << "__attribute__ ((warn_unused_result))"
152 << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800153 out << "static bool registerForNotifications(\n";
154 out.indent(2, [&] {
155 out << "const std::string &serviceName,\n"
156 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
157 << "&notification);\n";
158 });
159
160}
161
Steven Moreland038903b2017-03-30 12:11:24 -0700162static void implementGetService(Formatter &out,
163 const FQName &fqName,
164 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800165
166 const std::string interfaceName = fqName.getInterfaceName();
Steven Moreland038903b2017-03-30 12:11:24 -0700167 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800168
169 out << "// static\n"
Steven Moreland038903b2017-03-30 12:11:24 -0700170 << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000171 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800172 out.block([&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700173 out << "using ::android::hardware::defaultServiceManager;\n";
174 out << "using ::android::hardware::details::waitForHwService;\n";
175 out << "using ::android::hardware::getPassthroughServiceManager;\n";
176 out << "using ::android::hardware::Return;\n";
177 out << "using ::android::sp;\n";
178 out << "using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;\n\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000179
Steven Morelandbcf51802017-04-06 09:17:44 -0700180 out << "sp<" << interfaceName << "> iface = nullptr;\n";
181
182 out.endl();
183
184 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> sm"
185 << " = defaultServiceManager();\n";
186
187 out.sIf("sm == nullptr", [&] {
188 // hwbinder is not available on this device, so future tries
189 // would also be null. I can only return nullptr.
190 out << "ALOGE(\"getService: defaultServiceManager() is null\");\n"
191 << "return nullptr;\n";
192 }).endl().endl();
193
194 out << "Return<Transport> transportRet = sm->getTransport("
195 << interfaceName << "::descriptor, serviceName);\n\n";
196
197 out.sIf("!transportRet.isOk()", [&] {
198 out << "ALOGE(\"getService: defaultServiceManager()->getTransport returns %s\", "
199 << "transportRet.description().c_str());\n";
200 out << "return nullptr;\n";
201 });
202
203 out.endl();
204
205 out << "Transport transport = transportRet;\n";
206 out << "const bool vintfHwbinder = (transport == Transport::HWBINDER);\n"
207 << "const bool vintfPassthru = (transport == Transport::PASSTHROUGH);\n"
208 << "const bool vintfEmpty = (transport == Transport::EMPTY);\n\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000209
210 // if (getStub) {
211 // getPassthroughServiceManager()->get only once.
212 // } else {
213 // if (vintfHwbinder) {
214 // while (no alive service) {
215 // waitForHwService
216 // defaultServiceManager()->get
217 // }
218 // } else if (vintfEmpty) {
219 // defaultServiceManager()->get only once.
220 // getPassthroughServiceManager()->get only once.
221 // } else if (vintfPassthru) {
222 // getPassthroughServiceManager()->get only once.
223 // }
224 // }
225
Yifan Hong223fd472017-03-23 17:17:57 +0000226 out << "bool tried = false;\n";
227 out.sWhile("!getStub && (vintfHwbinder || (vintfEmpty && !tried))", [&] {
Yifan Hong31f07ff2017-03-21 18:56:35 +0000228
229 out.sIf("tried", [&] {
230 // sleep only after the first trial.
231 out << "ALOGI(\"getService: retrying in 1s...\");\n"
232 << "sleep(1);\n";
233 }).endl();
234
Yifan Hong223fd472017-03-23 17:17:57 +0000235 out << "tried = true;\n";
236
Yifan Hong31f07ff2017-03-21 18:56:35 +0000237
Steven Moreland038903b2017-03-30 12:11:24 -0700238 if (!isTry) {
239 out.sIf("vintfHwbinder", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700240 out << "waitForHwService("
241 << interfaceName << "::descriptor, serviceName);\n";
Steven Moreland038903b2017-03-30 12:11:24 -0700242 }).endl();
243 }
Yifan Hong31f07ff2017-03-21 18:56:35 +0000244
Steven Morelandbcf51802017-04-06 09:17:44 -0700245 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000246 out.indent(2, [&] {
247 out << "sm->get(" << interfaceName << "::descriptor, serviceName);\n";
248 });
249
250 out.sIf("!ret.isOk()", [&] {
Steven Moreland42394ce2017-03-27 17:03:04 -0700251 // hwservicemanager fails, may be security issue
Yifan Hong31f07ff2017-03-21 18:56:35 +0000252 out << "ALOGE(\"getService: defaultServiceManager()->get returns %s\", "
253 << "ret.description().c_str());\n"
Steven Moreland42394ce2017-03-27 17:03:04 -0700254 << "break;\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000255 }).endl();
256
Steven Morelandbcf51802017-04-06 09:17:44 -0700257 out << "sp<" << gIBaseFqName.cppName() << "> base = ret;\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700258 out.sIf("base == nullptr", [&] {
259 // race condition. hwservicemanager drops the service
260 // from waitForHwService to here
Steven Morelanddff644c2017-03-24 10:59:01 -0700261 out << "ALOGW(\"getService: found null hwbinder interface\");\n"
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700262 << (isTry ? "break" : "continue")
263 << ";\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000264 }).endl();
Steven Morelandbcf51802017-04-06 09:17:44 -0700265 out << "Return<sp<" << interfaceName
Yifan Hong200209c2017-03-29 03:39:09 -0700266 << ">> castRet = " << interfaceName << "::castFrom(base, true /* emitError */);\n";
267 out.sIf("!castRet.isOk()", [&] {
268 out.sIf("castRet.isDeadObject()", [&] {
269 // service is dead (castFrom cannot call interfaceChain)
270 out << "ALOGW(\"getService: found dead hwbinder service\");\n"
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700271 << (isTry ? "break" : "continue")
272 << ";\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700273 }).sElse([&] {
274 out << "ALOGW(\"getService: cannot call into hwbinder service: %s"
275 << "; No permission? Check for selinux denials.\", "
276 << "castRet.description().c_str());\n"
277 << "break;\n";
278 }).endl();
279 }).endl();
280 out << "iface = castRet;\n";
281 out.sIf("iface == nullptr", [&] {
282 // returned service isn't of correct type; this is a bug
283 // to hwservicemanager or to the service itself (interfaceChain
284 // is not consistent).
285 out << "ALOGW(\"getService: received incompatible service; bug in hwservicemanager?\");\n"
286 << "break;\n";
287 }).endl();
Yifan Hong31f07ff2017-03-21 18:56:35 +0000288
289 out << "return iface;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800290 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800291
Yifan Hong31f07ff2017-03-21 18:56:35 +0000292 out.sIf("getStub || vintfPassthru || vintfEmpty", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700293 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> pm"
294 << " = getPassthroughServiceManager();\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000295
296 out.sIf("pm != nullptr", [&] () {
Steven Morelandbcf51802017-04-06 09:17:44 -0700297 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Steven Morelandf10af872017-01-25 16:01:56 +0000298 out.indent(2, [&] {
299 out << "pm->get(" << interfaceName << "::descriptor" << ", serviceName);\n";
Steven Moreland2c2dea82017-01-18 17:24:17 -0800300 });
Steven Morelandf10af872017-01-25 16:01:56 +0000301 out.sIf("ret.isOk()", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700302 out << "sp<" << gIBaseFqName.cppName()
Steven Morelandf10af872017-01-25 16:01:56 +0000303 << "> baseInterface = ret;\n";
304 out.sIf("baseInterface != nullptr", [&]() {
305 out << "iface = new " << fqName.getInterfacePassthroughName()
306 << "(" << interfaceName << "::castFrom(baseInterface));\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000307 }).endl();
Steven Morelandf10af872017-01-25 16:01:56 +0000308 }).endl();
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800309 }).endl();
310 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800311
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800312 out << "return iface;\n";
313 }).endl().endl();
Steven Moreland038903b2017-03-30 12:11:24 -0700314}
315
316static void implementServiceManagerInteractions(Formatter &out,
317 const FQName &fqName, const std::string &package) {
318
319 const std::string interfaceName = fqName.getInterfaceName();
320
321 implementGetService(out, fqName, true /* isTry */);
322 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800323
Yifan Hongeefe4f22017-01-04 15:32:42 -0800324 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800325 << "const std::string &serviceName) ";
326 out.block([&] {
Steven Moreland58b478b2017-04-09 10:54:50 -0700327 out << "::android::hardware::details::onRegistration(\""
328 << fqName.getPackageAndVersion().string() << "\", \""
329 << interfaceName
330 << "\", serviceName);\n\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800331 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
332 out.indent(2, [&] {
333 out << "= ::android::hardware::defaultServiceManager();\n";
334 });
335 out.sIf("sm == nullptr", [&] {
336 out << "return ::android::INVALID_OPERATION;\n";
337 }).endl();
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100338 out << "::android::hardware::Return<bool> ret = "
339 << "sm->add(serviceName.c_str(), this);\n"
340 << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800341 }).endl().endl();
342
Yifan Hongeefe4f22017-01-04 15:32:42 -0800343 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800344 out.indent(2, [&] {
345 out << "const std::string &serviceName,\n"
346 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
347 << "&notification) ";
348 });
349 out.block([&] {
350 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
351 out.indent(2, [&] {
352 out << "= ::android::hardware::defaultServiceManager();\n";
353 });
354 out.sIf("sm == nullptr", [&] {
355 out << "return false;\n";
356 }).endl();
357 out << "::android::hardware::Return<bool> success =\n";
358 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800359 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800360 out.indent(2, [&] {
361 out << "serviceName, notification);\n";
362 });
363 });
364 out << "return success.isOk() && success;\n";
365 }).endl().endl();
366}
367
Andreas Huberb82318c2016-08-02 14:45:54 -0700368status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700369
Andreas Huberb82318c2016-08-02 14:45:54 -0700370 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700371 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700372 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700373
374 std::string ifaceName;
375 bool isInterface = true;
376 if (!AST::isInterface(&ifaceName)) {
377 ifaceName = "types";
378 isInterface = false;
379 }
380 path.append(ifaceName);
381 path.append(".h");
382
Andreas Huberd2943e12016-08-05 11:59:31 -0700383 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700384 FILE *file = fopen(path.c_str(), "w");
385
386 if (file == NULL) {
387 return -errno;
388 }
389
390 Formatter out(file);
391
392 const std::string guard = makeHeaderGuard(ifaceName);
393
394 out << "#ifndef " << guard << "\n";
395 out << "#define " << guard << "\n\n";
396
Andreas Huber737080b2016-08-02 15:38:04 -0700397 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700398 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700399 }
400
401 if (!mImportedNames.empty()) {
402 out << "\n";
403 }
404
Steven Moreland0693f312016-11-09 15:06:14 -0800405 if (isInterface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800406 if (isIBase()) {
407 out << "// skipped #include IServiceNotification.h\n\n";
408 } else {
409 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
410 }
Steven Moreland0693f312016-11-09 15:06:14 -0800411 }
412
Yifan Hongc8934042016-11-17 17:10:52 -0800413 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700414 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700415
416 if (isInterface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200417 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700418 }
419
Martijn Coenenaf712c02016-11-16 15:26:27 +0100420 out << "#include <utils/NativeHandle.h>\n";
421 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700422
423 enterLeaveNamespace(out, true /* enter */);
424 out << "\n";
425
426 if (isInterface) {
427 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700428 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700429
430 const Interface *iface = mRootScope->getInterface();
431 const Interface *superType = iface->superType();
432
Steven Moreland40786312016-08-16 10:29:40 -0700433 if (superType == NULL) {
Yifan Hongc8934042016-11-17 17:10:52 -0800434 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700435 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000436 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700437 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700438 }
439
440 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700441
442 out.indent();
443
Andreas Huber881227d2016-08-02 14:20:21 -0700444 }
445
446 status_t err = emitTypeDeclarations(out);
447
448 if (err != OK) {
449 return err;
450 }
451
452 if (isInterface) {
453 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800454
Yifan Hongc8934042016-11-17 17:10:52 -0800455 out << "virtual bool isRemote() const ";
456 if (!isIBase()) {
457 out << "override ";
458 }
459 out << "{ return false; }\n\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800460
Andreas Huber881227d2016-08-02 14:20:21 -0700461 for (const auto &method : iface->methods()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700462 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700463
Andreas Huber881227d2016-08-02 14:20:21 -0700464 const bool returnsValue = !method->results().empty();
Steven Morelandd732ea12016-11-08 17:12:06 -0800465 const TypedVar *elidedReturn = method->canElideCallback();
466
467 if (elidedReturn == nullptr && returnsValue) {
468 out << "using "
469 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700470 << "_cb = std::function<void(";
471 method->emitCppResultSignature(out, true /* specify namespaces */);
472 out << ")>;\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800473 }
Andreas Huber881227d2016-08-02 14:20:21 -0700474
Andreas Huber3599d922016-08-09 10:42:57 -0700475 method->dumpAnnotations(out);
476
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700477 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700478 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700479 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700480 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700481 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700482 }
483
484 out << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700485 << "(";
486 method->emitCppArgSignature(out, true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700487
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700488 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700489 if (!method->args().empty()) {
490 out << ", ";
491 }
492
Steven Moreland67f67b42016-09-29 08:59:02 -0700493 out << method->name() << "_cb _hidl_cb";
Andreas Huber881227d2016-08-02 14:20:21 -0700494 }
495
Yifan Hong10fe0b52016-10-19 14:20:17 -0700496 out << ")";
497 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800498 if (!isIBase()) {
499 out << " override";
500 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700501 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700502 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700503 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700504 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700505 }
Steven Moreland40786312016-08-16 10:29:40 -0700506
Yifan Hong3d746092016-12-07 14:26:33 -0800507 out << "// cast static functions\n";
508 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700509
Yifan Hong3d746092016-12-07 14:26:33 -0800510 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -0700511 out << "static ::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -0800512 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -0700513 << "> castFrom("
Yifan Hong3d746092016-12-07 14:26:33 -0800514 << superType->getCppArgumentType()
515 << " parent"
Yifan Hong200209c2017-03-29 03:39:09 -0700516 << ", bool emitError = false);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700517 }
518
Steven Morelandd39133b2016-11-11 12:30:08 -0800519 out << "\nstatic const char* descriptor;\n\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700520
Yifan Hongc8934042016-11-17 17:10:52 -0800521 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800522 out << "// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800523 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800524 declareServiceManagerInteractions(out, iface->localName());
Yifan Hongc8934042016-11-17 17:10:52 -0800525 }
Andreas Huber881227d2016-08-02 14:20:21 -0700526 }
527
528 if (isInterface) {
529 out.unindent();
530
Andreas Hubere3f769a2016-10-10 10:54:44 -0700531 out << "};\n\n";
532 }
533
534 err = mRootScope->emitGlobalTypeDeclarations(out);
535
536 if (err != OK) {
537 return err;
Andreas Huber881227d2016-08-02 14:20:21 -0700538 }
539
540 out << "\n";
541 enterLeaveNamespace(out, false /* enter */);
542
543 out << "\n#endif // " << guard << "\n";
544
545 return OK;
546}
547
Steven Moreland40786312016-08-16 10:29:40 -0700548status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
549 std::string ifaceName;
Yifan Hong244e82d2016-11-11 11:13:57 -0800550 bool isInterface = AST::isInterface(&ifaceName);
551 const Interface *iface = nullptr;
Yifan Hong244e82d2016-11-11 11:13:57 -0800552 std::string klassName{};
553
554 if(isInterface) {
555 iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800556 klassName = iface->getHwName();
Yifan Hong244e82d2016-11-11 11:13:57 -0800557 } else {
558 klassName = "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700559 }
560
Steven Moreland40786312016-08-16 10:29:40 -0700561 std::string path = outputPath;
562 path.append(mCoordinator->convertPackageRootToPath(mPackage));
563 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
564 path.append(klassName + ".h");
565
Yifan Hong244e82d2016-11-11 11:13:57 -0800566 FILE *file = fopen(path.c_str(), "w");
Steven Moreland40786312016-08-16 10:29:40 -0700567
568 if (file == NULL) {
569 return -errno;
570 }
571
572 Formatter out(file);
573
574 const std::string guard = makeHeaderGuard(klassName);
575
576 out << "#ifndef " << guard << "\n";
577 out << "#define " << guard << "\n\n";
578
Yifan Hong244e82d2016-11-11 11:13:57 -0800579 if (isInterface) {
580 generateCppPackageInclude(out, mPackage, ifaceName);
581 } else {
582 generateCppPackageInclude(out, mPackage, "types");
583 }
Steven Moreland40786312016-08-16 10:29:40 -0700584
Steven Morelandee88eed2016-10-31 17:49:00 -0700585 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700586
587 for (const auto &item : mImportedNames) {
588 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800589 generateCppPackageInclude(out, item, "hwtypes");
590 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800591 generateCppPackageInclude(out, item, item.getInterfaceStubName());
592 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700593 }
Steven Moreland40786312016-08-16 10:29:40 -0700594 }
595
596 out << "\n";
597
Martijn Coenen93915102016-09-01 01:35:52 +0200598 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700599 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100600 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700601
602 out << "\n";
603
604 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700605
Yifan Hong244e82d2016-11-11 11:13:57 -0800606 status_t err = mRootScope->emitGlobalHwDeclarations(out);
607 if (err != OK) {
608 return err;
609 }
Steven Moreland40786312016-08-16 10:29:40 -0700610
611 enterLeaveNamespace(out, false /* enter */);
612
613 out << "\n#endif // " << guard << "\n";
614
615 return OK;
616}
617
Andreas Huber881227d2016-08-02 14:20:21 -0700618status_t AST::emitTypeDeclarations(Formatter &out) const {
619 return mRootScope->emitTypeDeclarations(out);
620}
621
Yifan Hong7a118f52016-12-07 11:21:15 -0800622static void wrapPassthroughArg(Formatter &out,
623 const TypedVar *arg, bool addPrefixToName,
624 std::function<void(void)> handleError) {
625 if (!arg->type().isInterface()) {
626 return;
627 }
628 std::string name = (addPrefixToName ? "_hidl_out_" : "") + arg->name();
629 std::string wrappedName = (addPrefixToName ? "_hidl_out_wrapped_" : "_hidl_wrapped_")
630 + arg->name();
631 const Interface &iface = static_cast<const Interface &>(arg->type());
632 out << iface.getCppStackType() << " " << wrappedName << ";\n";
633 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
634 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
635 out << wrappedName
636 << " = "
637 << iface.fqName().cppName()
Yifan Hong341112d2017-04-20 18:12:05 -0700638 << "::castFrom(::android::hardware::details::wrapPassthrough<"
639 << iface.fqName().cppName()
640 << ">("
Yifan Hong7a118f52016-12-07 11:21:15 -0800641 << name << "));\n";
642 out.sIf(wrappedName + " == nullptr", [&] {
643 // Fatal error. Happens when the BsFoo class is not found in the binary
644 // or any dynamic libraries.
645 handleError();
646 }).endl();
647 }).sElse([&] {
648 out << wrappedName << " = " << name << ";\n";
649 }).endl().endl();
650}
651
Steven Moreland69e7c702016-09-09 11:16:32 -0700652status_t AST::generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700653 const Method *method) const {
654 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700655
656 out << " {\n";
657 out.indent();
658
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800659 if (method->isHidlReserved()
660 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
661 method->cppImpl(IMPL_PASSTHROUGH, out);
662 out.unindent();
663 out << "}\n\n";
664 return OK;
665 }
666
Steven Moreland69e7c702016-09-09 11:16:32 -0700667 const bool returnsValue = !method->results().empty();
668 const TypedVar *elidedReturn = method->canElideCallback();
669
Steven Moreland67f67b42016-09-29 08:59:02 -0700670 if (returnsValue && elidedReturn == nullptr) {
671 generateCheckNonNull(out, "_hidl_cb");
672 }
673
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700674 generateCppInstrumentationCall(
675 out,
676 InstrumentationEvent::PASSTHROUGH_ENTRY,
677 method);
678
Yifan Hong7a118f52016-12-07 11:21:15 -0800679
680 for (const auto &arg : method->args()) {
681 wrapPassthroughArg(out, arg, false /* addPrefixToName */, [&] {
682 out << "return ::android::hardware::Status::fromExceptionCode(\n";
683 out.indent(2, [&] {
684 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800685 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800686 });
687 });
688 }
689
690 out << "auto _hidl_error = ::android::hardware::Void();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700691 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700692
693 if (method->isOneway()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800694 out << "addOnewayTask([this, &_hidl_error";
Steven Moreland69e7c702016-09-09 11:16:32 -0700695 for (const auto &arg : method->args()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800696 out << ", "
697 << (arg->type().isInterface() ? "_hidl_wrapped_" : "")
698 << arg->name();
Steven Moreland69e7c702016-09-09 11:16:32 -0700699 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700700 out << "] {\n";
701 out.indent();
702 out << "this->";
Steven Moreland69e7c702016-09-09 11:16:32 -0700703 }
704
705 out << "mImpl->"
706 << method->name()
707 << "(";
708
Yifan Hong932464e2017-03-30 15:40:22 -0700709 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800710 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700711 });
Steven Moreland69e7c702016-09-09 11:16:32 -0700712 if (returnsValue && elidedReturn == nullptr) {
713 if (!method->args().empty()) {
714 out << ", ";
715 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800716 out << "[&](";
Yifan Hong932464e2017-03-30 15:40:22 -0700717 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800718 out << "const auto &_hidl_out_"
719 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700720 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800721
722 out << ") {\n";
723 out.indent();
724 status_t status = generateCppInstrumentationCall(
725 out,
726 InstrumentationEvent::PASSTHROUGH_EXIT,
727 method);
728 if (status != OK) {
729 return status;
730 }
731
Yifan Hong7a118f52016-12-07 11:21:15 -0800732 for (const auto &arg : method->results()) {
733 wrapPassthroughArg(out, arg, true /* addPrefixToName */, [&] {
734 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
735 out.indent(2, [&] {
736 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800737 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800738 });
739 out << "return;\n";
740 });
741 }
742
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800743 out << "_hidl_cb(";
Yifan Hong932464e2017-03-30 15:40:22 -0700744 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800745 out << (arg->type().isInterface() ? "_hidl_out_wrapped_" : "_hidl_out_")
746 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700747 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800748 out << ");\n";
749 out.unindent();
750 out << "});\n\n";
751 } else {
752 out << ");\n\n";
753 if (elidedReturn != nullptr) {
754 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -0800755 << " _hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800756 << elidedReturn->name()
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000757 << " = _hidl_return;\n";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800758 }
759 status_t status = generateCppInstrumentationCall(
760 out,
761 InstrumentationEvent::PASSTHROUGH_EXIT,
762 method);
763 if (status != OK) {
764 return status;
765 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700766 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700767
768 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700769 out.unindent();
770 out << "});\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700771 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700772
773 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700774
775 out.unindent();
776 out << "}\n";
777
778 return OK;
779}
780
Yifan Hong068c5522016-10-31 14:07:25 -0700781status_t AST::generateMethods(Formatter &out, MethodGenerator gen) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700782
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700783 const Interface *iface = mRootScope->getInterface();
784
Yifan Hong10fe0b52016-10-19 14:20:17 -0700785 const Interface *prevIterface = nullptr;
786 for (const auto &tuple : iface->allMethodsFromRoot()) {
787 const Method *method = tuple.method();
788 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700789
Yifan Hong10fe0b52016-10-19 14:20:17 -0700790 if(prevIterface != superInterface) {
791 if (prevIterface != nullptr) {
792 out << "\n";
793 }
794 out << "// Methods from "
795 << superInterface->fullName()
796 << " follow.\n";
797 prevIterface = superInterface;
798 }
Yifan Hong068c5522016-10-31 14:07:25 -0700799 status_t err = gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700800
Yifan Hong10fe0b52016-10-19 14:20:17 -0700801 if (err != OK) {
802 return err;
803 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700804 }
805
Yifan Hong10fe0b52016-10-19 14:20:17 -0700806 out << "\n";
807
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700808 return OK;
809}
810
Andreas Huberb82318c2016-08-02 14:45:54 -0700811status_t AST::generateStubHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700812 std::string ifaceName;
813 if (!AST::isInterface(&ifaceName)) {
814 // types.hal does not get a stub header.
815 return OK;
816 }
817
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700818 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800819 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -0700820
Andreas Huberb82318c2016-08-02 14:45:54 -0700821 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700822 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700823 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700824 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700825 path.append(".h");
826
Andreas Huberd2943e12016-08-05 11:59:31 -0700827 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700828 FILE *file = fopen(path.c_str(), "w");
829
830 if (file == NULL) {
831 return -errno;
832 }
833
834 Formatter out(file);
835
Steven Moreland40786312016-08-16 10:29:40 -0700836 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700837
838 out << "#ifndef " << guard << "\n";
839 out << "#define " << guard << "\n\n";
840
Yifan Hongeefe4f22017-01-04 15:32:42 -0800841 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700842 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700843
844 enterLeaveNamespace(out, true /* enter */);
845 out << "\n";
846
847 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800848 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100849 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800850 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000851 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100852 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800853 out << " : public "
854 << gIBaseFqName.getInterfaceStubFqName().cppName()
855 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100856 }
Andreas Huber881227d2016-08-02 14:20:21 -0700857
858 out.indent();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800859 out << "explicit "
860 << klassName
Steven Moreland40786312016-08-16 10:29:40 -0700861 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100862 << "\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800863 out << "explicit "
864 << klassName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100865 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800866 << " const std::string& HidlInstrumentor_package,"
867 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700868 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700869 out << "::android::status_t onTransact(\n";
870 out.indent();
871 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700872 out << "uint32_t _hidl_code,\n";
873 out << "const ::android::hardware::Parcel &_hidl_data,\n";
874 out << "::android::hardware::Parcel *_hidl_reply,\n";
875 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700876 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700877 out.unindent();
878 out.unindent();
879
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100880 out << "::android::sp<" << ifaceName << "> getImpl() { return _hidl_mImpl; };\n";
881 out.unindent();
882 out << "private:\n";
883 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800884
885 status_t err = generateMethods(out, [&](const Method *method, const Interface *iface) {
886 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
887 return OK;
888 }
889 const bool returnsValue = !method->results().empty();
890 const TypedVar *elidedReturn = method->canElideCallback();
891
892 if (elidedReturn == nullptr && returnsValue) {
893 out << "using " << method->name() << "_cb = "
894 << iface->fqName().cppName()
895 << "::" << method->name() << "_cb;\n";
896 }
897 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800898 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800899 return OK;
900 });
901 if (err != OK) {
902 return err;
903 }
904
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100905 out << "::android::sp<" << ifaceName << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700906 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700907 out << "};\n\n";
908
909 enterLeaveNamespace(out, false /* enter */);
910
911 out << "\n#endif // " << guard << "\n";
912
913 return OK;
914}
915
Andreas Huberb82318c2016-08-02 14:45:54 -0700916status_t AST::generateProxyHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700917 std::string ifaceName;
918 if (!AST::isInterface(&ifaceName)) {
919 // types.hal does not get a proxy header.
920 return OK;
921 }
922
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700923 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800924 const std::string proxyName = iface->getProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -0700925
Andreas Huberb82318c2016-08-02 14:45:54 -0700926 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700927 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700928 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Yifan Hongeefe4f22017-01-04 15:32:42 -0800929 path.append(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700930 path.append(".h");
931
Andreas Huberd2943e12016-08-05 11:59:31 -0700932 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700933 FILE *file = fopen(path.c_str(), "w");
934
935 if (file == NULL) {
936 return -errno;
937 }
938
939 Formatter out(file);
940
Yifan Hongeefe4f22017-01-04 15:32:42 -0800941 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700942
943 out << "#ifndef " << guard << "\n";
944 out << "#define " << guard << "\n\n";
945
Martijn Coenen115d4282016-12-19 05:14:04 +0100946 out << "#include <hidl/HidlTransportSupport.h>\n\n";
947
Andreas Huber881227d2016-08-02 14:20:21 -0700948 std::vector<std::string> packageComponents;
949 getPackageAndVersionComponents(
950 &packageComponents, false /* cpp_compatible */);
951
Yifan Hongeefe4f22017-01-04 15:32:42 -0800952 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700953 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700954
955 enterLeaveNamespace(out, true /* enter */);
956 out << "\n";
957
958 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800959 << proxyName
960 << " : public ::android::hardware::BpInterface<"
961 << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000962 << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700963
964 out.indent();
965
Yifan Hongeefe4f22017-01-04 15:32:42 -0800966 out << "explicit "
967 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700968 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700969 << "\n\n";
970
Yifan Hong10fe0b52016-10-19 14:20:17 -0700971 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700972
Yifan Hong068c5522016-10-31 14:07:25 -0700973 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
974 method->generateCppSignature(out);
975 out << " override;\n";
976 return OK;
977 });
Steven Moreland9c387612016-09-07 09:54:26 -0700978
979 if (err != OK) {
980 return err;
981 }
Andreas Huber881227d2016-08-02 14:20:21 -0700982
983 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100984 out << "private:\n";
985 out.indent();
986 out << "std::mutex _hidl_mMutex;\n"
987 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
988 << " _hidl_mDeathRecipients;\n";
989 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700990 out << "};\n\n";
991
992 enterLeaveNamespace(out, false /* enter */);
993
994 out << "\n#endif // " << guard << "\n";
995
996 return OK;
997}
998
Andreas Huberb82318c2016-08-02 14:45:54 -0700999status_t AST::generateAllSource(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -07001000
Andreas Huberb82318c2016-08-02 14:45:54 -07001001 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -07001002 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -07001003 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -07001004
1005 std::string ifaceName;
1006 std::string baseName;
1007
Yifan Hongfe95aa22016-10-19 17:26:45 -07001008 const Interface *iface = nullptr;
1009 bool isInterface;
Andreas Huber881227d2016-08-02 14:20:21 -07001010 if (!AST::isInterface(&ifaceName)) {
1011 baseName = "types";
1012 isInterface = false;
1013 } else {
Yifan Hongfe95aa22016-10-19 17:26:45 -07001014 iface = mRootScope->getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -07001015 baseName = iface->getBaseName();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001016 isInterface = true;
Andreas Huber881227d2016-08-02 14:20:21 -07001017 }
1018
1019 path.append(baseName);
1020
1021 if (baseName != "types") {
1022 path.append("All");
1023 }
1024
1025 path.append(".cpp");
1026
Andreas Huberd2943e12016-08-05 11:59:31 -07001027 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -07001028 FILE *file = fopen(path.c_str(), "w");
1029
1030 if (file == NULL) {
1031 return -errno;
1032 }
1033
1034 Formatter out(file);
1035
Steven Moreland623c0042017-01-13 14:42:29 -08001036 out << "#define LOG_TAG \""
1037 << mPackage.string() << "::" << baseName
1038 << "\"\n\n";
1039
Steven Moreland05cd4232016-11-21 16:01:12 -08001040 out << "#include <android/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001041 out << "#include <cutils/trace.h>\n";
1042 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001043 if (isInterface) {
Steven Moreland19d5c172016-10-20 19:20:25 -07001044 // This is a no-op for IServiceManager itself.
1045 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
1046
Steven Morelandbec74ed2017-01-25 17:42:35 -08001047 // TODO(b/34274385) remove this
1048 out << "#include <hidl/LegacySupport.h>\n";
1049
Yifan Hongeefe4f22017-01-04 15:32:42 -08001050 generateCppPackageInclude(out, mPackage, iface->getProxyName());
1051 generateCppPackageInclude(out, mPackage, iface->getStubName());
1052 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001053
1054 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -07001055 generateCppPackageInclude(out,
1056 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -08001057 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001058 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -08001059
1060 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001061 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -07001062 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -08001063 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -07001064 }
1065
1066 out << "\n";
1067
1068 enterLeaveNamespace(out, true /* enter */);
1069 out << "\n";
1070
1071 status_t err = generateTypeSource(out, ifaceName);
1072
1073 if (err == OK && isInterface) {
Yifan Hong10fe0b52016-10-19 14:20:17 -07001074 const Interface *iface = mRootScope->getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -07001075
1076 // need to be put here, generateStubSource is using this.
Yifan Hongeefe4f22017-01-04 15:32:42 -08001077 out << "const char* "
1078 << iface->localName()
Yifan Hong10fe0b52016-10-19 14:20:17 -07001079 << "::descriptor(\""
1080 << iface->fqName().string()
1081 << "\");\n\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +01001082 out << "__attribute__((constructor))";
1083 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001084 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001085 out << "::android::hardware::details::gBnConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001086 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001087 << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001088 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001089 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001090 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001091 out << "return new "
1092 << iface->getStubName()
Yifan Hong341112d2017-04-20 18:12:05 -07001093 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001094 << iface->localName()
Yifan Hong158655a2016-11-08 12:34:07 -08001095 << " *>(iIntf));\n";
1096 });
Yifan Hongb04de382017-02-06 15:31:52 -08001097 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001098 });
Yifan Honga159f3b2017-03-16 14:53:51 -07001099 out << "::android::hardware::details::gBsConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001100 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001101 << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001102 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001103 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -08001104 << gIBaseFqName.cppName()
1105 << "> {\n";
1106 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001107 out << "return new "
1108 << iface->getPassthroughName()
Yifan Hong341112d2017-04-20 18:12:05 -07001109 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001110 << iface->localName()
Yifan Hong7a118f52016-12-07 11:21:15 -08001111 << " *>(iIntf));\n";
1112 });
Yifan Hongb04de382017-02-06 15:31:52 -08001113 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001114 });
Yifan Hong158655a2016-11-08 12:34:07 -08001115 });
Martijn Coenen8adcb652017-02-03 17:37:36 +01001116 out << "};\n\n";
1117 out << "__attribute__((destructor))";
1118 out << "static void static_destructor() {\n";
1119 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001120 out << "::android::hardware::details::gBnConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001121 << iface->localName()
1122 << "::descriptor);\n";
Yifan Honga159f3b2017-03-16 14:53:51 -07001123 out << "::android::hardware::details::gBsConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001124 << iface->localName()
1125 << "::descriptor);\n";
1126 });
1127 out << "};\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001128
Yifan Hongfe95aa22016-10-19 17:26:45 -07001129 err = generateInterfaceSource(out);
1130 }
1131
1132 if (err == OK && isInterface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001133 err = generateProxySource(out, iface->fqName());
Andreas Huber881227d2016-08-02 14:20:21 -07001134 }
1135
1136 if (err == OK && isInterface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001137 err = generateStubSource(out, iface);
Andreas Huber881227d2016-08-02 14:20:21 -07001138 }
1139
Steven Moreland40786312016-08-16 10:29:40 -07001140 if (err == OK && isInterface) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001141 err = generatePassthroughSource(out);
1142 }
1143
1144 if (err == OK && isInterface) {
Steven Moreland9c387612016-09-07 09:54:26 -07001145 const Interface *iface = mRootScope->getInterface();
1146
Yifan Hongc8934042016-11-17 17:10:52 -08001147 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001148 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -08001149 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001150 std::string package = iface->fqName().package()
1151 + iface->fqName().atVersion();
1152
Yifan Hongeefe4f22017-01-04 15:32:42 -08001153 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -08001154 }
Steven Moreland40786312016-08-16 10:29:40 -07001155 }
1156
Andreas Huber6755e9d2017-04-06 11:09:07 -07001157 HidlTypeAssertion::EmitAll(out);
1158 out << "\n";
1159
Andreas Huber881227d2016-08-02 14:20:21 -07001160 enterLeaveNamespace(out, false /* enter */);
1161
1162 return err;
1163}
1164
Steven Moreland67f67b42016-09-29 08:59:02 -07001165// static
1166void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) {
Yifan Honga018ed52016-12-13 16:35:08 -08001167 out.sIf(nonNull + " == nullptr", [&] {
1168 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1169 out.indent(2, [&] {
1170 out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT);\n";
1171 });
1172 }).endl().endl();
Steven Moreland67f67b42016-09-29 08:59:02 -07001173}
1174
Andreas Huber881227d2016-08-02 14:20:21 -07001175status_t AST::generateTypeSource(
1176 Formatter &out, const std::string &ifaceName) const {
1177 return mRootScope->emitTypeDefinitions(out, ifaceName);
1178}
1179
Andreas Hubere7ff2282016-08-16 13:50:03 -07001180void AST::declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -07001181 Formatter &out,
1182 const std::vector<TypedVar *> &args,
1183 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -07001184 if (args.empty()) {
1185 return;
1186 }
1187
1188 for (const auto &arg : args) {
1189 const Type &type = arg->type();
1190
Yifan Hong3b320f82016-11-01 15:15:54 -07001191 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001192 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -07001193 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001194 << ";\n";
1195 }
1196
1197 out << "\n";
1198}
1199
Andreas Huber881227d2016-08-02 14:20:21 -07001200void AST::emitCppReaderWriter(
1201 Formatter &out,
1202 const std::string &parcelObj,
1203 bool parcelObjIsPointer,
1204 const TypedVar *arg,
1205 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -07001206 Type::ErrorMode mode,
1207 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -07001208 const Type &type = arg->type();
1209
Andreas Huber881227d2016-08-02 14:20:21 -07001210 type.emitReaderWriter(
1211 out,
Andreas Huber5e44a292016-09-27 14:52:39 -07001212 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -07001213 parcelObj,
1214 parcelObjIsPointer,
1215 isReader,
1216 mode);
1217}
1218
Yifan Hongbf459bc2016-08-23 16:50:37 -07001219void AST::emitCppResolveReferences(
1220 Formatter &out,
1221 const std::string &parcelObj,
1222 bool parcelObjIsPointer,
1223 const TypedVar *arg,
1224 bool isReader,
1225 Type::ErrorMode mode,
1226 bool addPrefixToName) const {
1227 const Type &type = arg->type();
1228 if(type.needsResolveReferences()) {
1229 type.emitResolveReferences(
1230 out,
1231 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
1232 isReader, // nameIsPointer
1233 parcelObj,
1234 parcelObjIsPointer,
1235 isReader,
1236 mode);
1237 }
1238}
1239
Yifan Hong068c5522016-10-31 14:07:25 -07001240status_t AST::generateProxyMethodSource(Formatter &out,
1241 const std::string &klassName,
1242 const Method *method,
1243 const Interface *superInterface) const {
1244
1245 method->generateCppSignature(out,
1246 klassName,
1247 true /* specify namespaces */);
1248
1249 const bool returnsValue = !method->results().empty();
1250 const TypedVar *elidedReturn = method->canElideCallback();
1251
Steven Moreland41c6d2e2016-11-07 12:26:54 -08001252 out << " {\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001253
1254 out.indent();
1255
Martijn Coenen115d4282016-12-19 05:14:04 +01001256 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
1257 method->cppImpl(IMPL_PROXY, out);
1258 out.unindent();
1259 out << "}\n\n";
1260 return OK;
1261 }
1262
Yifan Hong068c5522016-10-31 14:07:25 -07001263 if (returnsValue && elidedReturn == nullptr) {
1264 generateCheckNonNull(out, "_hidl_cb");
1265 }
1266
1267 status_t status = generateCppInstrumentationCall(
1268 out,
1269 InstrumentationEvent::CLIENT_API_ENTRY,
Yifan Hong068c5522016-10-31 14:07:25 -07001270 method);
1271 if (status != OK) {
1272 return status;
1273 }
1274
1275 out << "::android::hardware::Parcel _hidl_data;\n";
1276 out << "::android::hardware::Parcel _hidl_reply;\n";
1277 out << "::android::status_t _hidl_err;\n";
1278 out << "::android::hardware::Status _hidl_status;\n\n";
1279
1280 declareCppReaderLocals(
1281 out, method->results(), true /* forResults */);
1282
1283 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001284 out << superInterface->fqName().cppName();
Yifan Hong068c5522016-10-31 14:07:25 -07001285 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001286 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1287
Martijn Coenenfff73352017-01-04 16:36:31 +01001288 bool hasInterfaceArgument = false;
Yifan Hong068c5522016-10-31 14:07:25 -07001289 // First DFS: write all buffers and resolve pointers for parent
1290 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001291 if (arg->type().isInterface()) {
1292 hasInterfaceArgument = true;
1293 }
Yifan Hong068c5522016-10-31 14:07:25 -07001294 emitCppReaderWriter(
1295 out,
1296 "_hidl_data",
1297 false /* parcelObjIsPointer */,
1298 arg,
1299 false /* reader */,
1300 Type::ErrorMode_Goto,
1301 false /* addPrefixToName */);
1302 }
1303
1304 // Second DFS: resolve references.
1305 for (const auto &arg : method->args()) {
1306 emitCppResolveReferences(
1307 out,
1308 "_hidl_data",
1309 false /* parcelObjIsPointer */,
1310 arg,
1311 false /* reader */,
1312 Type::ErrorMode_Goto,
1313 false /* addPrefixToName */);
1314 }
1315
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001316 if (hasInterfaceArgument) {
1317 // Start binder threadpool to handle incoming transactions
1318 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1319 }
Yifan Hong068c5522016-10-31 14:07:25 -07001320 out << "_hidl_err = remote()->transact("
1321 << method->getSerialId()
1322 << " /* "
1323 << method->name()
1324 << " */, _hidl_data, &_hidl_reply";
1325
1326 if (method->isOneway()) {
1327 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
1328 }
1329 out << ");\n";
1330
1331 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1332
1333 if (!method->isOneway()) {
Yifan Hong859e53f2016-11-14 19:08:24 -08001334 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001335 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1336 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1337
1338
1339 // First DFS: write all buffers and resolve pointers for parent
1340 for (const auto &arg : method->results()) {
1341 emitCppReaderWriter(
1342 out,
1343 "_hidl_reply",
1344 false /* parcelObjIsPointer */,
1345 arg,
1346 true /* reader */,
1347 Type::ErrorMode_Goto,
1348 true /* addPrefixToName */);
1349 }
1350
1351 // Second DFS: resolve references.
1352 for (const auto &arg : method->results()) {
1353 emitCppResolveReferences(
1354 out,
1355 "_hidl_reply",
1356 false /* parcelObjIsPointer */,
1357 arg,
1358 true /* reader */,
1359 Type::ErrorMode_Goto,
1360 true /* addPrefixToName */);
1361 }
1362
1363 if (returnsValue && elidedReturn == nullptr) {
1364 out << "_hidl_cb(";
1365
Yifan Hong932464e2017-03-30 15:40:22 -07001366 out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) {
Yifan Hong068c5522016-10-31 14:07:25 -07001367 if (arg->type().resultNeedsDeref()) {
1368 out << "*";
1369 }
1370 out << "_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001371 });
Yifan Hong068c5522016-10-31 14:07:25 -07001372
1373 out << ");\n\n";
1374 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001375 }
1376 status = generateCppInstrumentationCall(
1377 out,
1378 InstrumentationEvent::CLIENT_API_EXIT,
1379 method);
1380 if (status != OK) {
1381 return status;
Yifan Hong068c5522016-10-31 14:07:25 -07001382 }
1383
1384 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001385 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1386 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001387 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001388 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1389 } else {
1390 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1391 out << "return ::android::hardware::Return<void>();\n\n";
1392 }
1393
1394 out.unindent();
1395 out << "_hidl_error:\n";
1396 out.indent();
1397 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1398 out << "return ::android::hardware::Return<";
1399 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001400 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001401 } else {
1402 out << "void";
1403 }
1404 out << ">(_hidl_status);\n";
1405
1406 out.unindent();
1407 out << "}\n\n";
1408 return OK;
1409}
1410
Andreas Huber881227d2016-08-02 14:20:21 -07001411status_t AST::generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -08001412 Formatter &out, const FQName &fqName) const {
1413 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001414
1415 out << klassName
1416 << "::"
1417 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001418 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001419
1420 out.indent();
1421 out.indent();
1422
1423 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001424 << "<"
1425 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001426 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001427 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001428 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001429 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001430 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001431 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001432
Andreas Huber881227d2016-08-02 14:20:21 -07001433 out.unindent();
1434 out.unindent();
1435 out << "}\n\n";
1436
Yifan Hong068c5522016-10-31 14:07:25 -07001437 status_t err = generateMethods(out, [&](const Method *method, const Interface *superInterface) {
1438 return generateProxyMethodSource(out, klassName, method, superInterface);
1439 });
Andreas Huber881227d2016-08-02 14:20:21 -07001440
Yifan Hong068c5522016-10-31 14:07:25 -07001441 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001442}
1443
1444status_t AST::generateStubSource(
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001445 Formatter &out,
Yifan Hongeefe4f22017-01-04 15:32:42 -08001446 const Interface *iface) const {
1447 const std::string interfaceName = iface->localName();
1448 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001449
Steven Moreland40786312016-08-16 10:29:40 -07001450 out << klassName
1451 << "::"
1452 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001453 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001454
1455 out.indent();
1456 out.indent();
1457
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001458 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001459 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001460 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001461 out << ": "
1462 << gIBaseFqName.getInterfaceStubFqName().cppName()
1463 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001464 }
1465
1466 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001467 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001468 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001469 << "\") { \n";
1470 out.indent();
1471 out << "_hidl_mImpl = _hidl_impl;\n";
1472 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001473
1474 out.unindent();
1475 out.unindent();
1476 out << "}\n\n";
1477
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001478 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001479 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001480 // class properly.
1481 out << klassName
1482 << "::"
1483 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001484 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1485 << " const std::string &HidlInstrumentor_package,"
1486 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001487
1488 out.indent();
1489 out.indent();
1490
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001491 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001492 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001493 out.indent();
1494 out << "_hidl_mImpl = _hidl_impl;\n";
1495 out.unindent();
1496
1497 out.unindent();
1498 out.unindent();
1499 out << "}\n\n";
1500 }
1501
Yifan Hongbcffce22017-02-01 15:52:06 -08001502 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1503 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
1504 return OK;
1505 }
1506 method->generateCppSignature(out, iface->getStubName());
1507 out << " ";
1508 out.block([&] {
1509 method->cppImpl(IMPL_STUB_IMPL, out);
1510 }).endl();
1511 return OK;
1512 });
Steven Moreland60818632017-02-04 00:33:42 -08001513 if (err != OK) {
1514 return err;
1515 }
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001516
Andreas Huber881227d2016-08-02 14:20:21 -07001517 out << "::android::status_t " << klassName << "::onTransact(\n";
1518
1519 out.indent();
1520 out.indent();
1521
Iliyan Malchev549e2592016-08-10 08:59:12 -07001522 out << "uint32_t _hidl_code,\n"
1523 << "const ::android::hardware::Parcel &_hidl_data,\n"
1524 << "::android::hardware::Parcel *_hidl_reply,\n"
1525 << "uint32_t _hidl_flags,\n"
1526 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001527
1528 out.unindent();
1529
Iliyan Malchev549e2592016-08-10 08:59:12 -07001530 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001531 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001532 out.indent();
1533
Yifan Hong10fe0b52016-10-19 14:20:17 -07001534 for (const auto &tuple : iface->allMethodsFromRoot()) {
1535 const Method *method = tuple.method();
1536 const Interface *superInterface = tuple.interface();
1537 out << "case "
1538 << method->getSerialId()
1539 << " /* "
1540 << method->name()
1541 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001542
Yifan Hong10fe0b52016-10-19 14:20:17 -07001543 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001544
Yifan Hong10fe0b52016-10-19 14:20:17 -07001545 status_t err =
1546 generateStubSourceForMethod(out, superInterface, method);
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001547
Yifan Hong10fe0b52016-10-19 14:20:17 -07001548 if (err != OK) {
1549 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001550 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001551
1552 out.unindent();
1553 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001554 }
1555
1556 out << "default:\n{\n";
1557 out.indent();
1558
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001559 out << "return onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001560
1561 out.indent();
1562 out.indent();
1563
Iliyan Malchev549e2592016-08-10 08:59:12 -07001564 out << "_hidl_code, _hidl_data, _hidl_reply, "
1565 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001566
1567 out.unindent();
1568 out.unindent();
1569
1570 out.unindent();
1571 out << "}\n";
1572
1573 out.unindent();
1574 out << "}\n\n";
1575
Yifan Honga018ed52016-12-13 16:35:08 -08001576 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1577 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1578 out.indent(2, [&] {
1579 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1580 out << "_hidl_reply);\n";
1581 });
1582 });
Andreas Huber881227d2016-08-02 14:20:21 -07001583
Iliyan Malchev549e2592016-08-10 08:59:12 -07001584 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001585
1586 out.unindent();
1587 out << "}\n\n";
1588
1589 return OK;
1590}
1591
1592status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001593 Formatter &out, const Interface *iface, const Method *method) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001594 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1595 method->cppImpl(IMPL_STUB, out);
1596 out << "break;\n";
1597 return OK;
1598 }
1599
Yifan Hongeefe4f22017-01-04 15:32:42 -08001600 out << "if (!_hidl_data.enforceInterface("
1601 << iface->fullName()
1602 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001603
Andreas Huber881227d2016-08-02 14:20:21 -07001604 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001605 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001606 out << "break;\n";
1607 out.unindent();
1608 out << "}\n\n";
1609
Andreas Huber5e44a292016-09-27 14:52:39 -07001610 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001611
Yifan Hongbf459bc2016-08-23 16:50:37 -07001612 // First DFS: write buffers
Andreas Huber881227d2016-08-02 14:20:21 -07001613 for (const auto &arg : method->args()) {
1614 emitCppReaderWriter(
1615 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001616 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001617 false /* parcelObjIsPointer */,
1618 arg,
1619 true /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001620 Type::ErrorMode_Break,
1621 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001622 }
1623
Yifan Hongbf459bc2016-08-23 16:50:37 -07001624 // Second DFS: resolve references
1625 for (const auto &arg : method->args()) {
1626 emitCppResolveReferences(
1627 out,
1628 "_hidl_data",
1629 false /* parcelObjIsPointer */,
1630 arg,
1631 true /* reader */,
1632 Type::ErrorMode_Break,
1633 false /* addPrefixToName */);
1634 }
1635
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001636 status_t status = generateCppInstrumentationCall(
1637 out,
1638 InstrumentationEvent::SERVER_API_ENTRY,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001639 method);
1640 if (status != OK) {
1641 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001642 }
1643
Andreas Huber881227d2016-08-02 14:20:21 -07001644 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001645 const TypedVar *elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -08001646 const std::string callee =
1647 (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL))
1648 ? "this" : "_hidl_mImpl";
Andreas Huber881227d2016-08-02 14:20:21 -07001649
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001650 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001651 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001652 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001653 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001654 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001655 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001656 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001657
Yifan Hong932464e2017-03-30 15:40:22 -07001658 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001659 if (arg->type().resultNeedsDeref()) {
1660 out << "*";
1661 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001662 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001663 });
Andreas Huber881227d2016-08-02 14:20:21 -07001664
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001665 out << ");\n\n";
Yifan Hong859e53f2016-11-14 19:08:24 -08001666 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1667 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001668
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001669 elidedReturn->type().emitReaderWriter(
1670 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001671 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001672 "_hidl_reply",
1673 true, /* parcelObjIsPointer */
1674 false, /* isReader */
1675 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001676
Yifan Hongbf459bc2016-08-23 16:50:37 -07001677 emitCppResolveReferences(
1678 out,
1679 "_hidl_reply",
1680 true /* parcelObjIsPointer */,
1681 elidedReturn,
1682 false /* reader */,
1683 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001684 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001685
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001686 status_t status = generateCppInstrumentationCall(
1687 out,
1688 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001689 method);
1690 if (status != OK) {
1691 return status;
1692 }
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001693
Iliyan Malchev549e2592016-08-10 08:59:12 -07001694 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001695 } else {
1696 if (returnsValue) {
1697 out << "bool _hidl_callbackCalled = false;\n\n";
1698 }
Andreas Huber881227d2016-08-02 14:20:21 -07001699
Yifan Hongcd2ae452017-01-31 14:33:40 -08001700 out << callee << "->" << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001701
Yifan Hong932464e2017-03-30 15:40:22 -07001702 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001703 if (arg->type().resultNeedsDeref()) {
1704 out << "*";
1705 }
1706
1707 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001708 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001709
1710 if (returnsValue) {
Yifan Hong932464e2017-03-30 15:40:22 -07001711 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001712 out << ", ";
1713 }
1714
1715 out << "[&](";
1716
Yifan Hong932464e2017-03-30 15:40:22 -07001717 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001718 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001719 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001720
1721 out << ") {\n";
1722 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001723 out << "if (_hidl_callbackCalled) {\n";
1724 out.indent();
1725 out << "LOG_ALWAYS_FATAL(\""
1726 << method->name()
1727 << ": _hidl_cb called a second time, but must be called once.\");\n";
1728 out.unindent();
1729 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001730 out << "_hidl_callbackCalled = true;\n\n";
1731
Yifan Hong859e53f2016-11-14 19:08:24 -08001732 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1733 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001734
Yifan Hongbf459bc2016-08-23 16:50:37 -07001735 // First DFS: buffers
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001736 for (const auto &arg : method->results()) {
1737 emitCppReaderWriter(
1738 out,
1739 "_hidl_reply",
1740 true /* parcelObjIsPointer */,
1741 arg,
1742 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001743 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001744 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001745 }
1746
Yifan Hongbf459bc2016-08-23 16:50:37 -07001747 // Second DFS: resolve references
1748 for (const auto &arg : method->results()) {
1749 emitCppResolveReferences(
1750 out,
1751 "_hidl_reply",
1752 true /* parcelObjIsPointer */,
1753 arg,
1754 false /* reader */,
1755 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001756 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001757 }
1758
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001759 status_t status = generateCppInstrumentationCall(
1760 out,
1761 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001762 method);
1763 if (status != OK) {
1764 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001765 }
1766
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001767 out << "_hidl_cb(*_hidl_reply);\n";
1768
1769 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001770 out << "});\n\n";
1771 } else {
1772 out << ");\n\n";
1773 status_t status = generateCppInstrumentationCall(
1774 out,
1775 InstrumentationEvent::SERVER_API_EXIT,
1776 method);
1777 if (status != OK) {
1778 return status;
1779 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001780 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001781
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001782 if (returnsValue) {
1783 out << "if (!_hidl_callbackCalled) {\n";
1784 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001785 out << "LOG_ALWAYS_FATAL(\""
1786 << method->name()
1787 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001788 out.unindent();
1789 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001790 } else {
1791 out << "::android::hardware::writeToParcel("
1792 << "::android::hardware::Status::ok(), "
1793 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001794 }
Andreas Huber881227d2016-08-02 14:20:21 -07001795 }
1796
1797 out << "break;\n";
1798
1799 return OK;
1800}
1801
Steven Moreland69e7c702016-09-09 11:16:32 -07001802status_t AST::generatePassthroughHeader(const std::string &outputPath) const {
1803 std::string ifaceName;
1804 if (!AST::isInterface(&ifaceName)) {
1805 // types.hal does not get a stub header.
1806 return OK;
1807 }
1808
1809 const Interface *iface = mRootScope->getInterface();
1810
Yifan Hongeefe4f22017-01-04 15:32:42 -08001811 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001812
1813 bool supportOneway = iface->hasOnewayMethods();
1814
1815 std::string path = outputPath;
1816 path.append(mCoordinator->convertPackageRootToPath(mPackage));
1817 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
1818 path.append(klassName);
1819 path.append(".h");
1820
1821 CHECK(Coordinator::MakeParentHierarchy(path));
1822 FILE *file = fopen(path.c_str(), "w");
1823
1824 if (file == NULL) {
1825 return -errno;
1826 }
1827
1828 Formatter out(file);
1829
1830 const std::string guard = makeHeaderGuard(klassName);
1831
1832 out << "#ifndef " << guard << "\n";
1833 out << "#define " << guard << "\n\n";
1834
1835 std::vector<std::string> packageComponents;
1836 getPackageAndVersionComponents(
1837 &packageComponents, false /* cpp_compatible */);
1838
Yifan Hongb0949432016-12-15 15:32:24 -08001839 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001840 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001841
1842 generateCppPackageInclude(out, mPackage, ifaceName);
1843 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001844
Yifan Hong7a118f52016-12-07 11:21:15 -08001845 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001846 if (supportOneway) {
Yifan Hong2cbc1472016-10-25 19:02:40 -07001847 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001848 }
1849
1850 enterLeaveNamespace(out, true /* enter */);
1851 out << "\n";
1852
1853 out << "struct "
1854 << klassName
1855 << " : " << ifaceName
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001856 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001857
1858 out.indent();
1859 out << "explicit "
1860 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001861 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001862 << ifaceName
1863 << "> impl);\n";
1864
Yifan Hong068c5522016-10-31 14:07:25 -07001865 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1866 return generatePassthroughMethod(out, method);
1867 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001868
1869 if (err != OK) {
1870 return err;
1871 }
1872
1873 out.unindent();
1874 out << "private:\n";
1875 out.indent();
Steven Morelandc46e9842016-11-02 13:21:26 -07001876 out << "const ::android::sp<" << ifaceName << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001877
1878 if (supportOneway) {
Yifan Hongef91d362017-03-20 17:18:13 -07001879 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001880
1881 out << "\n";
1882
1883 out << "::android::hardware::Return<void> addOnewayTask("
1884 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001885 }
1886
1887 out.unindent();
1888
1889 out << "};\n\n";
1890
1891 enterLeaveNamespace(out, false /* enter */);
1892
1893 out << "\n#endif // " << guard << "\n";
1894
1895 return OK;
1896}
1897
Yifan Hongfe95aa22016-10-19 17:26:45 -07001898status_t AST::generateInterfaceSource(Formatter &out) const {
1899 const Interface *iface = mRootScope->getInterface();
1900
Yifan Hong2d7126b2016-10-20 15:12:57 -07001901 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001902 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001903
Steven Morelandd4b068a2017-03-20 06:30:51 -07001904 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1905 bool reserved = method->isHidlReserved();
1906
1907 if (!reserved) {
1908 out << "// no default implementation for: ";
1909 }
1910 method->generateCppSignature(out, iface->localName());
1911 if (reserved) {
1912 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001913 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001914 }).endl();
1915 }
1916
1917 out << "\n";
1918
1919 return OK;
1920 });
1921 if (err != OK) {
1922 return err;
1923 }
1924
Yifan Hong3d746092016-12-07 14:26:33 -08001925 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -07001926 out << "// static \n::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -08001927 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -07001928 << "> "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001929 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001930 << "::castFrom("
1931 << superType->getCppArgumentType()
Yifan Hong200209c2017-03-29 03:39:09 -07001932 << " parent, bool "
1933 << (iface == superType ? "/* emitError */" : "emitError")
1934 << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001935 out.indent();
1936 if (iface == superType) {
1937 out << "return parent;\n";
1938 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001939 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001940 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001941 << superType->fqName().cppName() << ", "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001942 << iface->getProxyName() << ", "
Yifan Hong51a65092017-01-04 15:41:44 -08001943 << superType->getProxyFqName().cppName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001944 << ">(\n";
1945 out.indent();
1946 out.indent();
1947 out << "parent, \""
1948 << iface->fqName().string()
Yifan Hong200209c2017-03-29 03:39:09 -07001949 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001950 out.unindent();
1951 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001952 }
Yifan Hong3d746092016-12-07 14:26:33 -08001953 out.unindent();
1954 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001955 }
1956
1957 return OK;
1958}
1959
Steven Moreland69e7c702016-09-09 11:16:32 -07001960status_t AST::generatePassthroughSource(Formatter &out) const {
1961 const Interface *iface = mRootScope->getInterface();
1962
Yifan Hongeefe4f22017-01-04 15:32:42 -08001963 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001964
1965 out << klassName
1966 << "::"
1967 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001968 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001969 << iface->fullName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001970 << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001971 << mPackage.string()
1972 << "\", \""
1973 << iface->localName()
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001974 << "\"), mImpl(impl) {";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001975 if (iface->hasOnewayMethods()) {
1976 out << "\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001977 out.indent([&] {
Yifan Hongf01dad42017-03-20 19:03:11 -07001978 out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001979 });
1980 }
1981 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001982
1983 if (iface->hasOnewayMethods()) {
1984 out << "::android::hardware::Return<void> "
1985 << klassName
1986 << "::addOnewayTask(std::function<void(void)> fun) {\n";
1987 out.indent();
Yifan Hong2cbc1472016-10-25 19:02:40 -07001988 out << "if (!mOnewayQueue.push(fun)) {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001989 out.indent();
Steven Moreland67f67b42016-09-29 08:59:02 -07001990 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1991 out.indent();
1992 out.indent();
1993 out << "::android::hardware::Status::EX_TRANSACTION_FAILED);\n";
1994 out.unindent();
1995 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07001996 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07001997 out << "}\n";
1998
Steven Morelandd366c262016-10-11 15:29:10 -07001999 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002000
2001 out.unindent();
2002 out << "}\n\n";
2003
2004
2005 }
2006
2007 return OK;
2008}
2009
Martijn Coenen7b295242016-11-04 16:52:56 +01002010status_t AST::generateCppAtraceCall(Formatter &out,
2011 InstrumentationEvent event,
2012 const Method *method) const {
2013 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08002014 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01002015 switch (event) {
2016 case SERVER_API_ENTRY:
2017 {
2018 out << "atrace_begin(ATRACE_TAG_HAL, \""
2019 << baseString + "::server\");\n";
2020 break;
2021 }
2022 case CLIENT_API_ENTRY:
2023 {
2024 out << "atrace_begin(ATRACE_TAG_HAL, \""
2025 << baseString + "::client\");\n";
2026 break;
2027 }
2028 case PASSTHROUGH_ENTRY:
2029 {
2030 out << "atrace_begin(ATRACE_TAG_HAL, \""
2031 << baseString + "::passthrough\");\n";
2032 break;
2033 }
2034 case SERVER_API_EXIT:
2035 case CLIENT_API_EXIT:
2036 case PASSTHROUGH_EXIT:
2037 {
2038 out << "atrace_end(ATRACE_TAG_HAL);\n";
2039 break;
2040 }
2041 default:
2042 {
2043 LOG(ERROR) << "Unsupported instrumentation event: " << event;
2044 return UNKNOWN_ERROR;
2045 }
2046 }
2047
2048 return OK;
2049}
2050
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002051status_t AST::generateCppInstrumentationCall(
2052 Formatter &out,
2053 InstrumentationEvent event,
Steven Moreland031ccf12016-10-31 15:54:38 -07002054 const Method *method) const {
Martijn Coenen7b295242016-11-04 16:52:56 +01002055 status_t err = generateCppAtraceCall(out, event, method);
2056 if (err != OK) {
2057 return err;
2058 }
2059
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002060 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
2061 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002062 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002063 std::string event_str = "";
2064 switch (event) {
2065 case SERVER_API_ENTRY:
2066 {
2067 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
2068 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002069 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002070 << (arg->type().resultNeedsDeref() ? "" : "&")
2071 << arg->name()
2072 << ");\n";
2073 }
2074 break;
2075 }
2076 case SERVER_API_EXIT:
2077 {
2078 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07002079 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002080 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07002081 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002082 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002083 }
2084 break;
2085 }
2086 case CLIENT_API_ENTRY:
2087 {
2088 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
2089 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002090 out << "_hidl_args.push_back((void *)&"
2091 << arg->name()
2092 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002093 }
2094 break;
2095 }
2096 case CLIENT_API_EXIT:
2097 {
2098 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
2099 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002100 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002101 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002102 << "_hidl_out_"
2103 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002104 << ");\n";
2105 }
2106 break;
2107 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002108 case PASSTHROUGH_ENTRY:
2109 {
2110 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
2111 for (const auto &arg : method->args()) {
2112 out << "_hidl_args.push_back((void *)&"
2113 << arg->name()
2114 << ");\n";
2115 }
2116 break;
2117 }
2118 case PASSTHROUGH_EXIT:
2119 {
2120 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002121 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002122 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002123 << arg->name()
2124 << ");\n";
2125 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002126 break;
2127 }
Steven Moreland031ccf12016-10-31 15:54:38 -07002128 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002129 {
Steven Moreland031ccf12016-10-31 15:54:38 -07002130 LOG(ERROR) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002131 return UNKNOWN_ERROR;
2132 }
2133 }
2134
Steven Moreland031ccf12016-10-31 15:54:38 -07002135 const Interface *iface = mRootScope->getInterface();
2136
Steven Moreland1ab31442016-11-03 18:37:51 -07002137 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002138 out.indent();
2139 out << "callback("
2140 << event_str
2141 << ", \""
2142 << mPackage.package()
2143 << "\", \""
Yifan Hong90ea87f2016-11-01 14:25:47 -07002144 << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002145 << "\", \""
2146 << iface->localName()
2147 << "\", \""
2148 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002149 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002150 out.unindent();
2151 out << "}\n";
2152 out.unindent();
2153 out << "}\n\n";
2154
2155 return OK;
2156}
2157
Andreas Huber881227d2016-08-02 14:20:21 -07002158} // namespace android