blob: 8df7b5472186f6768690c42fa68f69591321cf06 [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"
22#include "Method.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070023#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070024#include "Scope.h"
25
Andreas Huberdca261f2016-08-04 13:47:51 -070026#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027#include <hidl-util/Formatter.h>
Steven Moreland5708edf2016-11-04 15:33:31 +000028#include <hidl-util/StringHelper.h>
Andreas Huber881227d2016-08-02 14:20:21 -070029#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070030#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070031#include <vector>
32
33namespace android {
34
Andreas Huberb82318c2016-08-02 14:45:54 -070035status_t AST::generateCpp(const std::string &outputPath) const {
36 status_t err = generateInterfaceHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070037
38 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070039 err = generateStubHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070040 }
41
42 if (err == OK) {
Steven Moreland40786312016-08-16 10:29:40 -070043 err = generateHwBinderHeader(outputPath);
44 }
45
46 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070047 err = generateProxyHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070048 }
49
50 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070051 err = generateAllSource(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070052 }
53
Steven Moreland69e7c702016-09-09 11:16:32 -070054 if (err == OK) {
Yifan Hong7a118f52016-12-07 11:21:15 -080055 err = generatePassthroughHeader(outputPath);
Steven Moreland69e7c702016-09-09 11:16:32 -070056 }
57
Andreas Huber881227d2016-08-02 14:20:21 -070058 return err;
59}
60
Andreas Huber737080b2016-08-02 15:38:04 -070061void AST::getPackageComponents(
62 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070063 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070064}
65
66void AST::getPackageAndVersionComponents(
67 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070068 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070069}
70
Steven Moreland5708edf2016-11-04 15:33:31 +000071std::string AST::makeHeaderGuard(const std::string &baseName,
72 bool indicateGenerated) const {
73 std::string guard;
Andreas Huber881227d2016-08-02 14:20:21 -070074
Steven Moreland5708edf2016-11-04 15:33:31 +000075 if (indicateGenerated) {
76 guard += "HIDL_GENERATED_";
77 }
78
79 guard += StringHelper::Uppercase(mPackage.tokenName());
Andreas Huber881227d2016-08-02 14:20:21 -070080 guard += "_";
Steven Moreland5708edf2016-11-04 15:33:31 +000081 guard += StringHelper::Uppercase(baseName);
82 guard += "_H";
Andreas Huber881227d2016-08-02 14:20:21 -070083
84 return guard;
85}
86
Steven Morelandee88eed2016-10-31 17:49:00 -070087// static
88void AST::generateCppPackageInclude(
89 Formatter &out,
90 const FQName &package,
91 const std::string &klass) {
92
93 out << "#include <";
94
95 std::vector<std::string> components;
96 package.getPackageAndVersionComponents(&components, false /* cpp_compatible */);
97
98 for (const auto &component : components) {
99 out << component << "/";
100 }
101
102 out << klass
103 << ".h>\n";
104}
105
Andreas Huber881227d2016-08-02 14:20:21 -0700106void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
107 std::vector<std::string> packageComponents;
108 getPackageAndVersionComponents(
109 &packageComponents, true /* cpp_compatible */);
110
111 if (enter) {
112 for (const auto &component : packageComponents) {
113 out << "namespace " << component << " {\n";
114 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700115
Andreas Huber2831d512016-08-15 09:33:47 -0700116 out.setNamespace(mPackage.cppNamespace() + "::");
Andreas Huber881227d2016-08-02 14:20:21 -0700117 } else {
Andreas Huber0e00de42016-08-03 09:56:02 -0700118 out.setNamespace(std::string());
119
Andreas Huber881227d2016-08-02 14:20:21 -0700120 for (auto it = packageComponents.rbegin();
121 it != packageComponents.rend();
122 ++it) {
123 out << "} // namespace " << *it << "\n";
124 }
125 }
126}
127
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700128static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
129 const std::string functionName = isTry ? "tryGetService" : "getService";
130
131 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800132 << "const std::string &serviceName=\"default\", bool getStub=false);\n";
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700133 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800134 << "const char serviceName[], bool getStub=false)"
135 << " { std::string str(serviceName ? serviceName : \"\");"
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700136 << " return " << functionName << "(str, getStub); }\n";
137 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800138 << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
139 // without c_str the std::string constructor is ambiguous
140 << " { std::string str(serviceName.c_str());"
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700141 << " return " << functionName << "(str, getStub); }\n";
142 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
143 << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
144}
145
146static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
147 declareGetService(out, interfaceName, true /* isTry */);
148 declareGetService(out, interfaceName, false /* isTry */);
149
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800150 out << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800151 out << "static bool registerForNotifications(\n";
152 out.indent(2, [&] {
153 out << "const std::string &serviceName,\n"
154 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
155 << "&notification);\n";
156 });
157
158}
159
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700160static void implementGetService(Formatter &out,
161 const FQName &fqName,
162 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800163
164 const std::string interfaceName = fqName.getInterfaceName();
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700165 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800166
167 out << "// static\n"
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700168 << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000169 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800170 out.block([&] {
Steven Morelandf10af872017-01-25 16:01:56 +0000171 out << "::android::sp<" << interfaceName << "> iface = nullptr;\n";
Yifan Hongd3b58ed2017-01-30 14:13:10 -0800172 out << "::android::vintf::Transport transport = ::android::hardware::getTransport("
Yifan Hong152866b2017-02-28 15:34:27 -0800173 << interfaceName << "::descriptor, serviceName);\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000174
Yifan Hong31f07ff2017-03-21 18:56:35 +0000175 // TODO(b/34274385) remove sysprop check
176 out << "const bool vintfHwbinder = (transport == ::android::vintf::Transport::HWBINDER) ||\n"
177 << " (transport == ::android::vintf::Transport::TOGGLED &&\n"
178 << " ::android::hardware::details::blockingHalBinderizationEnabled());\n"
179 << "const bool vintfPassthru = (transport == ::android::vintf::Transport::PASSTHROUGH) ||\n"
180 << " (transport == ::android::vintf::Transport::TOGGLED &&\n"
181 << " !::android::hardware::details::blockingHalBinderizationEnabled());\n"
182 << "const bool vintfEmpty = (transport == ::android::vintf::Transport::EMPTY);\n\n";
183
184 // if (getStub) {
185 // getPassthroughServiceManager()->get only once.
186 // } else {
187 // if (vintfHwbinder) {
188 // while (no alive service) {
189 // waitForHwService
190 // defaultServiceManager()->get
191 // }
192 // } else if (vintfEmpty) {
193 // defaultServiceManager()->get only once.
194 // getPassthroughServiceManager()->get only once.
195 // } else if (vintfPassthru) {
196 // getPassthroughServiceManager()->get only once.
197 // }
198 // }
199
Yifan Hongf1ef44f2017-03-23 17:17:57 +0000200 out << "bool tried = false;\n";
201 out.sWhile("!getStub && (vintfHwbinder || (vintfEmpty && !tried))", [&] {
Yifan Hong31f07ff2017-03-21 18:56:35 +0000202
203 out.sIf("tried", [&] {
204 // sleep only after the first trial.
205 out << "ALOGI(\"getService: retrying in 1s...\");\n"
206 << "sleep(1);\n";
207 }).endl();
208
Yifan Hongf1ef44f2017-03-23 17:17:57 +0000209 out << "tried = true;\n";
210
Steven Morelandf10af872017-01-25 16:01:56 +0000211 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800212 out.indent(2, [&] {
Steven Morelandf10af872017-01-25 16:01:56 +0000213 out << "= ::android::hardware::defaultServiceManager();\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800214 });
Yifan Hong31f07ff2017-03-21 18:56:35 +0000215 out.sIf("sm == nullptr", [&] {
216 // hwbinder is not available on this device, so future tries
217 // would also be null. I can only "break" here and
218 // (vintfEmpty) try passthrough or (vintfHwbinder) return nullptr.
219 out << "ALOGE(\"getService: defaultServiceManager() is null\");\n"
220 << "break;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800221 }).endl();
Yifan Hong31f07ff2017-03-21 18:56:35 +0000222
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700223 if (!isTry) {
224 out.sIf("vintfHwbinder", [&] {
225 out << "::android::hardware::details::waitForHwService("
226 << interfaceName << "::descriptor" << ", serviceName);\n";
227 }).endl();
228 }
Yifan Hong31f07ff2017-03-21 18:56:35 +0000229
230 out << "::android::hardware::Return<::android::sp<"
231 << gIBaseFqName.cppName() << ">> ret = \n";
232 out.indent(2, [&] {
233 out << "sm->get(" << interfaceName << "::descriptor, serviceName);\n";
234 });
235
236 out.sIf("!ret.isOk()", [&] {
Steven Moreland42394ce2017-03-27 17:03:04 -0700237 // hwservicemanager fails, may be security issue
Yifan Hong31f07ff2017-03-21 18:56:35 +0000238 out << "ALOGE(\"getService: defaultServiceManager()->get returns %s\", "
239 << "ret.description().c_str());\n"
Steven Moreland42394ce2017-03-27 17:03:04 -0700240 << "break;\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000241 }).endl();
242
243 out << "iface = " << interfaceName << "::castFrom(ret);\n";
244 out.sIf("iface == nullptr", [&] {
245 // 1. race condition. hwservicemanager drops the service
246 // from waitForHwService to here
247 // 2. service is dead (castFrom cannot call interfaceChain)
248 // 3. returned service isn't of correct type; this is a bug
249 // to hwservicemanager or to the service itself (interfaceChain
250 // is not consistent)
251 // In all cases, try again.
Steven Morelanddff644c2017-03-24 10:59:01 -0700252 out << "ALOGW(\"getService: found null hwbinder interface\");\n"
Steven Moreland9cca95b2017-03-29 00:55:32 -0700253 << "break;\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000254 }).endl();
255
256 out << "return iface;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800257 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800258
Yifan Hong31f07ff2017-03-21 18:56:35 +0000259 out.sIf("getStub || vintfPassthru || vintfEmpty", [&] {
Steven Morelandf10af872017-01-25 16:01:56 +0000260 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> pm\n";
Steven Morelande3fa5da2017-01-25 07:07:53 +0000261 out.indent(2, [&] {
Steven Morelandf10af872017-01-25 16:01:56 +0000262 out << "= ::android::hardware::getPassthroughServiceManager();\n";
Steven Morelande3fa5da2017-01-25 07:07:53 +0000263 });
Steven Morelandf10af872017-01-25 16:01:56 +0000264
265 out.sIf("pm != nullptr", [&] () {
266 out << "::android::hardware::Return<::android::sp<" << gIBaseFqName.cppName() << ">> ret = \n";
267 out.indent(2, [&] {
268 out << "pm->get(" << interfaceName << "::descriptor" << ", serviceName);\n";
Steven Moreland2c2dea82017-01-18 17:24:17 -0800269 });
Steven Morelandf10af872017-01-25 16:01:56 +0000270 out.sIf("ret.isOk()", [&] {
271 out << "::android::sp<" << gIBaseFqName.cppName()
272 << "> baseInterface = ret;\n";
273 out.sIf("baseInterface != nullptr", [&]() {
274 out << "iface = new " << fqName.getInterfacePassthroughName()
275 << "(" << interfaceName << "::castFrom(baseInterface));\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000276 }).endl();
Steven Morelandf10af872017-01-25 16:01:56 +0000277 }).endl();
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800278 }).endl();
279 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800280
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800281 out << "return iface;\n";
282 }).endl().endl();
Steven Morelandeec5f7a2017-03-30 12:11:24 -0700283}
284
285static void implementServiceManagerInteractions(Formatter &out,
286 const FQName &fqName, const std::string &package) {
287
288 const std::string interfaceName = fqName.getInterfaceName();
289
290 implementGetService(out, fqName, true /* isTry */);
291 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800292
Yifan Hongeefe4f22017-01-04 15:32:42 -0800293 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800294 << "const std::string &serviceName) ";
295 out.block([&] {
296 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
297 out.indent(2, [&] {
298 out << "= ::android::hardware::defaultServiceManager();\n";
299 });
300 out.sIf("sm == nullptr", [&] {
301 out << "return ::android::INVALID_OPERATION;\n";
302 }).endl();
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100303 out << "::android::hardware::Return<bool> ret = "
304 << "sm->add(serviceName.c_str(), this);\n"
305 << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800306 }).endl().endl();
307
Yifan Hongeefe4f22017-01-04 15:32:42 -0800308 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800309 out.indent(2, [&] {
310 out << "const std::string &serviceName,\n"
311 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
312 << "&notification) ";
313 });
314 out.block([&] {
315 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
316 out.indent(2, [&] {
317 out << "= ::android::hardware::defaultServiceManager();\n";
318 });
319 out.sIf("sm == nullptr", [&] {
320 out << "return false;\n";
321 }).endl();
322 out << "::android::hardware::Return<bool> success =\n";
323 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800324 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800325 out.indent(2, [&] {
326 out << "serviceName, notification);\n";
327 });
328 });
329 out << "return success.isOk() && success;\n";
330 }).endl().endl();
331}
332
Andreas Huberb82318c2016-08-02 14:45:54 -0700333status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700334
Andreas Huberb82318c2016-08-02 14:45:54 -0700335 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700336 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700337 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700338
339 std::string ifaceName;
340 bool isInterface = true;
341 if (!AST::isInterface(&ifaceName)) {
342 ifaceName = "types";
343 isInterface = false;
344 }
345 path.append(ifaceName);
346 path.append(".h");
347
Andreas Huberd2943e12016-08-05 11:59:31 -0700348 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700349 FILE *file = fopen(path.c_str(), "w");
350
351 if (file == NULL) {
352 return -errno;
353 }
354
355 Formatter out(file);
356
357 const std::string guard = makeHeaderGuard(ifaceName);
358
359 out << "#ifndef " << guard << "\n";
360 out << "#define " << guard << "\n\n";
361
Andreas Huber737080b2016-08-02 15:38:04 -0700362 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700363 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700364 }
365
366 if (!mImportedNames.empty()) {
367 out << "\n";
368 }
369
Steven Moreland0693f312016-11-09 15:06:14 -0800370 if (isInterface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800371 if (isIBase()) {
372 out << "// skipped #include IServiceNotification.h\n\n";
373 } else {
374 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
375 }
Steven Moreland0693f312016-11-09 15:06:14 -0800376 }
377
Yifan Hongc8934042016-11-17 17:10:52 -0800378 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700379 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700380
381 if (isInterface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200382 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700383 }
384
Martijn Coenenaf712c02016-11-16 15:26:27 +0100385 out << "#include <utils/NativeHandle.h>\n";
386 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700387
388 enterLeaveNamespace(out, true /* enter */);
389 out << "\n";
390
391 if (isInterface) {
392 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700393 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700394
395 const Interface *iface = mRootScope->getInterface();
396 const Interface *superType = iface->superType();
397
Steven Moreland40786312016-08-16 10:29:40 -0700398 if (superType == NULL) {
Yifan Hongc8934042016-11-17 17:10:52 -0800399 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700400 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000401 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700402 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700403 }
404
405 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700406
407 out.indent();
408
Andreas Huber881227d2016-08-02 14:20:21 -0700409 }
410
411 status_t err = emitTypeDeclarations(out);
412
413 if (err != OK) {
414 return err;
415 }
416
417 if (isInterface) {
418 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800419
Yifan Hongc8934042016-11-17 17:10:52 -0800420 out << "virtual bool isRemote() const ";
421 if (!isIBase()) {
422 out << "override ";
423 }
424 out << "{ return false; }\n\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800425
Andreas Huber881227d2016-08-02 14:20:21 -0700426 for (const auto &method : iface->methods()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700427 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700428
Andreas Huber881227d2016-08-02 14:20:21 -0700429 const bool returnsValue = !method->results().empty();
Steven Morelandd732ea12016-11-08 17:12:06 -0800430 const TypedVar *elidedReturn = method->canElideCallback();
431
432 if (elidedReturn == nullptr && returnsValue) {
433 out << "using "
434 << method->name()
435 << "_cb = std::function<void("
436 << Method::GetArgSignature(method->results(),
437 true /* specify namespaces */)
438 << ")>;\n";
439 }
Andreas Huber881227d2016-08-02 14:20:21 -0700440
Andreas Huber3599d922016-08-09 10:42:57 -0700441 method->dumpAnnotations(out);
442
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700443 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700444 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700445 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700446 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700447 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700448 }
449
450 out << method->name()
Andreas Huber881227d2016-08-02 14:20:21 -0700451 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700452 << Method::GetArgSignature(method->args(),
453 true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700454
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700455 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700456 if (!method->args().empty()) {
457 out << ", ";
458 }
459
Steven Moreland67f67b42016-09-29 08:59:02 -0700460 out << method->name() << "_cb _hidl_cb";
Andreas Huber881227d2016-08-02 14:20:21 -0700461 }
462
Yifan Hong10fe0b52016-10-19 14:20:17 -0700463 out << ")";
464 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800465 if (!isIBase()) {
466 out << " override";
467 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700468 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700469 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700470 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700471 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700472 }
Steven Moreland40786312016-08-16 10:29:40 -0700473
Yifan Hong3d746092016-12-07 14:26:33 -0800474 out << "// cast static functions\n";
475 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700476
Yifan Hong3d746092016-12-07 14:26:33 -0800477 for (const Interface *superType : iface->typeChain()) {
478 out << "static "
479 << childTypeResult
480 << " castFrom("
481 << superType->getCppArgumentType()
482 << " parent"
483 << ");\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700484 }
485
Steven Morelandd39133b2016-11-11 12:30:08 -0800486 out << "\nstatic const char* descriptor;\n\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700487
Yifan Hongc8934042016-11-17 17:10:52 -0800488 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800489 out << "// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800490 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800491 declareServiceManagerInteractions(out, iface->localName());
Yifan Hongc8934042016-11-17 17:10:52 -0800492 }
Andreas Huber881227d2016-08-02 14:20:21 -0700493 }
494
495 if (isInterface) {
496 out.unindent();
497
Andreas Hubere3f769a2016-10-10 10:54:44 -0700498 out << "};\n\n";
499 }
500
501 err = mRootScope->emitGlobalTypeDeclarations(out);
502
503 if (err != OK) {
504 return err;
Andreas Huber881227d2016-08-02 14:20:21 -0700505 }
506
507 out << "\n";
508 enterLeaveNamespace(out, false /* enter */);
509
510 out << "\n#endif // " << guard << "\n";
511
512 return OK;
513}
514
Steven Moreland40786312016-08-16 10:29:40 -0700515status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
516 std::string ifaceName;
Yifan Hong244e82d2016-11-11 11:13:57 -0800517 bool isInterface = AST::isInterface(&ifaceName);
518 const Interface *iface = nullptr;
Yifan Hong244e82d2016-11-11 11:13:57 -0800519 std::string klassName{};
520
521 if(isInterface) {
522 iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800523 klassName = iface->getHwName();
Yifan Hong244e82d2016-11-11 11:13:57 -0800524 } else {
525 klassName = "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700526 }
527
Steven Moreland40786312016-08-16 10:29:40 -0700528 std::string path = outputPath;
529 path.append(mCoordinator->convertPackageRootToPath(mPackage));
530 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
531 path.append(klassName + ".h");
532
Yifan Hong244e82d2016-11-11 11:13:57 -0800533 FILE *file = fopen(path.c_str(), "w");
Steven Moreland40786312016-08-16 10:29:40 -0700534
535 if (file == NULL) {
536 return -errno;
537 }
538
539 Formatter out(file);
540
541 const std::string guard = makeHeaderGuard(klassName);
542
543 out << "#ifndef " << guard << "\n";
544 out << "#define " << guard << "\n\n";
545
Yifan Hong244e82d2016-11-11 11:13:57 -0800546 if (isInterface) {
547 generateCppPackageInclude(out, mPackage, ifaceName);
548 } else {
549 generateCppPackageInclude(out, mPackage, "types");
550 }
Steven Moreland40786312016-08-16 10:29:40 -0700551
Steven Morelandee88eed2016-10-31 17:49:00 -0700552 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700553
554 for (const auto &item : mImportedNames) {
555 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800556 generateCppPackageInclude(out, item, "hwtypes");
557 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800558 generateCppPackageInclude(out, item, item.getInterfaceStubName());
559 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700560 }
Steven Moreland40786312016-08-16 10:29:40 -0700561 }
562
563 out << "\n";
564
Martijn Coenen93915102016-09-01 01:35:52 +0200565 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700566 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100567 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700568
569 out << "\n";
570
571 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700572
Yifan Hong244e82d2016-11-11 11:13:57 -0800573 status_t err = mRootScope->emitGlobalHwDeclarations(out);
574 if (err != OK) {
575 return err;
576 }
Steven Moreland40786312016-08-16 10:29:40 -0700577
578 enterLeaveNamespace(out, false /* enter */);
579
580 out << "\n#endif // " << guard << "\n";
581
582 return OK;
583}
584
Andreas Huber881227d2016-08-02 14:20:21 -0700585status_t AST::emitTypeDeclarations(Formatter &out) const {
586 return mRootScope->emitTypeDeclarations(out);
587}
588
Yifan Hong7a118f52016-12-07 11:21:15 -0800589static void wrapPassthroughArg(Formatter &out,
590 const TypedVar *arg, bool addPrefixToName,
591 std::function<void(void)> handleError) {
592 if (!arg->type().isInterface()) {
593 return;
594 }
595 std::string name = (addPrefixToName ? "_hidl_out_" : "") + arg->name();
596 std::string wrappedName = (addPrefixToName ? "_hidl_out_wrapped_" : "_hidl_wrapped_")
597 + arg->name();
598 const Interface &iface = static_cast<const Interface &>(arg->type());
599 out << iface.getCppStackType() << " " << wrappedName << ";\n";
600 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
601 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
602 out << wrappedName
603 << " = "
604 << iface.fqName().cppName()
Yifan Hong052425a2017-03-13 17:06:13 -0700605 << "::castFrom(::android::hardware::details::wrapPassthrough("
Yifan Hong7a118f52016-12-07 11:21:15 -0800606 << name << "));\n";
607 out.sIf(wrappedName + " == nullptr", [&] {
608 // Fatal error. Happens when the BsFoo class is not found in the binary
609 // or any dynamic libraries.
610 handleError();
611 }).endl();
612 }).sElse([&] {
613 out << wrappedName << " = " << name << ";\n";
614 }).endl().endl();
615}
616
Steven Moreland69e7c702016-09-09 11:16:32 -0700617status_t AST::generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700618 const Method *method) const {
619 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700620
621 out << " {\n";
622 out.indent();
623
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800624 if (method->isHidlReserved()
625 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
626 method->cppImpl(IMPL_PASSTHROUGH, out);
627 out.unindent();
628 out << "}\n\n";
629 return OK;
630 }
631
Steven Moreland69e7c702016-09-09 11:16:32 -0700632 const bool returnsValue = !method->results().empty();
633 const TypedVar *elidedReturn = method->canElideCallback();
634
Steven Moreland67f67b42016-09-29 08:59:02 -0700635 if (returnsValue && elidedReturn == nullptr) {
636 generateCheckNonNull(out, "_hidl_cb");
637 }
638
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700639 generateCppInstrumentationCall(
640 out,
641 InstrumentationEvent::PASSTHROUGH_ENTRY,
642 method);
643
Yifan Hong7a118f52016-12-07 11:21:15 -0800644
645 for (const auto &arg : method->args()) {
646 wrapPassthroughArg(out, arg, false /* addPrefixToName */, [&] {
647 out << "return ::android::hardware::Status::fromExceptionCode(\n";
648 out.indent(2, [&] {
649 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800650 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800651 });
652 });
653 }
654
655 out << "auto _hidl_error = ::android::hardware::Void();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700656 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700657
658 if (method->isOneway()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800659 out << "addOnewayTask([this, &_hidl_error";
Steven Moreland69e7c702016-09-09 11:16:32 -0700660 for (const auto &arg : method->args()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800661 out << ", "
662 << (arg->type().isInterface() ? "_hidl_wrapped_" : "")
663 << arg->name();
Steven Moreland69e7c702016-09-09 11:16:32 -0700664 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700665 out << "] {\n";
666 out.indent();
667 out << "this->";
Steven Moreland69e7c702016-09-09 11:16:32 -0700668 }
669
670 out << "mImpl->"
671 << method->name()
672 << "(";
673
674 bool first = true;
675 for (const auto &arg : method->args()) {
676 if (!first) {
677 out << ", ";
678 }
679 first = false;
Yifan Hong7a118f52016-12-07 11:21:15 -0800680 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Steven Moreland69e7c702016-09-09 11:16:32 -0700681 }
682 if (returnsValue && elidedReturn == nullptr) {
683 if (!method->args().empty()) {
684 out << ", ";
685 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800686 out << "[&](";
687 first = true;
688 for (const auto &arg : method->results()) {
689 if (!first) {
690 out << ", ";
691 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700692
Yifan Hong7a118f52016-12-07 11:21:15 -0800693 out << "const auto &_hidl_out_"
694 << arg->name();
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800695
696 first = false;
697 }
698
699 out << ") {\n";
700 out.indent();
701 status_t status = generateCppInstrumentationCall(
702 out,
703 InstrumentationEvent::PASSTHROUGH_EXIT,
704 method);
705 if (status != OK) {
706 return status;
707 }
708
Yifan Hong7a118f52016-12-07 11:21:15 -0800709 for (const auto &arg : method->results()) {
710 wrapPassthroughArg(out, arg, true /* addPrefixToName */, [&] {
711 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
712 out.indent(2, [&] {
713 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800714 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800715 });
716 out << "return;\n";
717 });
718 }
719
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800720 out << "_hidl_cb(";
721 first = true;
722 for (const auto &arg : method->results()) {
723 if (!first) {
724 out << ", ";
725 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800726 first = false;
Yifan Hong7a118f52016-12-07 11:21:15 -0800727 out << (arg->type().isInterface() ? "_hidl_out_wrapped_" : "_hidl_out_")
728 << arg->name();
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800729 }
730 out << ");\n";
731 out.unindent();
732 out << "});\n\n";
733 } else {
734 out << ");\n\n";
735 if (elidedReturn != nullptr) {
736 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -0800737 << " _hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800738 << elidedReturn->name()
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000739 << " = _hidl_return;\n";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800740 }
741 status_t status = generateCppInstrumentationCall(
742 out,
743 InstrumentationEvent::PASSTHROUGH_EXIT,
744 method);
745 if (status != OK) {
746 return status;
747 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700748 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700749
750 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700751 out.unindent();
752 out << "});\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700753 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700754
755 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700756
757 out.unindent();
758 out << "}\n";
759
760 return OK;
761}
762
Yifan Hong068c5522016-10-31 14:07:25 -0700763status_t AST::generateMethods(Formatter &out, MethodGenerator gen) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700764
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700765 const Interface *iface = mRootScope->getInterface();
766
Yifan Hong10fe0b52016-10-19 14:20:17 -0700767 const Interface *prevIterface = nullptr;
768 for (const auto &tuple : iface->allMethodsFromRoot()) {
769 const Method *method = tuple.method();
770 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700771
Yifan Hong10fe0b52016-10-19 14:20:17 -0700772 if(prevIterface != superInterface) {
773 if (prevIterface != nullptr) {
774 out << "\n";
775 }
776 out << "// Methods from "
777 << superInterface->fullName()
778 << " follow.\n";
779 prevIterface = superInterface;
780 }
Yifan Hong068c5522016-10-31 14:07:25 -0700781 status_t err = gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700782
Yifan Hong10fe0b52016-10-19 14:20:17 -0700783 if (err != OK) {
784 return err;
785 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700786 }
787
Yifan Hong10fe0b52016-10-19 14:20:17 -0700788 out << "\n";
789
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700790 return OK;
791}
792
Andreas Huberb82318c2016-08-02 14:45:54 -0700793status_t AST::generateStubHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700794 std::string ifaceName;
795 if (!AST::isInterface(&ifaceName)) {
796 // types.hal does not get a stub header.
797 return OK;
798 }
799
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700800 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800801 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -0700802
Andreas Huberb82318c2016-08-02 14:45:54 -0700803 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700804 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700805 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700806 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700807 path.append(".h");
808
Andreas Huberd2943e12016-08-05 11:59:31 -0700809 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700810 FILE *file = fopen(path.c_str(), "w");
811
812 if (file == NULL) {
813 return -errno;
814 }
815
816 Formatter out(file);
817
Steven Moreland40786312016-08-16 10:29:40 -0700818 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700819
820 out << "#ifndef " << guard << "\n";
821 out << "#define " << guard << "\n\n";
822
Yifan Hongeefe4f22017-01-04 15:32:42 -0800823 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700824 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700825
826 enterLeaveNamespace(out, true /* enter */);
827 out << "\n";
828
829 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800830 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100831 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800832 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000833 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100834 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800835 out << " : public "
836 << gIBaseFqName.getInterfaceStubFqName().cppName()
837 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100838 }
Andreas Huber881227d2016-08-02 14:20:21 -0700839
840 out.indent();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800841 out << "explicit "
842 << klassName
Steven Moreland40786312016-08-16 10:29:40 -0700843 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100844 << "\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800845 out << "explicit "
846 << klassName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100847 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800848 << " const std::string& HidlInstrumentor_package,"
849 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700850 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700851 out << "::android::status_t onTransact(\n";
852 out.indent();
853 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700854 out << "uint32_t _hidl_code,\n";
855 out << "const ::android::hardware::Parcel &_hidl_data,\n";
856 out << "::android::hardware::Parcel *_hidl_reply,\n";
857 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700858 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700859 out.unindent();
860 out.unindent();
861
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100862 out << "::android::sp<" << ifaceName << "> getImpl() { return _hidl_mImpl; };\n";
863 out.unindent();
864 out << "private:\n";
865 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800866
867 status_t err = generateMethods(out, [&](const Method *method, const Interface *iface) {
868 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
869 return OK;
870 }
871 const bool returnsValue = !method->results().empty();
872 const TypedVar *elidedReturn = method->canElideCallback();
873
874 if (elidedReturn == nullptr && returnsValue) {
875 out << "using " << method->name() << "_cb = "
876 << iface->fqName().cppName()
877 << "::" << method->name() << "_cb;\n";
878 }
879 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800880 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800881 return OK;
882 });
883 if (err != OK) {
884 return err;
885 }
886
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100887 out << "::android::sp<" << ifaceName << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700888 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700889 out << "};\n\n";
890
891 enterLeaveNamespace(out, false /* enter */);
892
893 out << "\n#endif // " << guard << "\n";
894
895 return OK;
896}
897
Andreas Huberb82318c2016-08-02 14:45:54 -0700898status_t AST::generateProxyHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700899 std::string ifaceName;
900 if (!AST::isInterface(&ifaceName)) {
901 // types.hal does not get a proxy header.
902 return OK;
903 }
904
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700905 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800906 const std::string proxyName = iface->getProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -0700907
Andreas Huberb82318c2016-08-02 14:45:54 -0700908 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700909 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700910 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Yifan Hongeefe4f22017-01-04 15:32:42 -0800911 path.append(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700912 path.append(".h");
913
Andreas Huberd2943e12016-08-05 11:59:31 -0700914 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700915 FILE *file = fopen(path.c_str(), "w");
916
917 if (file == NULL) {
918 return -errno;
919 }
920
921 Formatter out(file);
922
Yifan Hongeefe4f22017-01-04 15:32:42 -0800923 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700924
925 out << "#ifndef " << guard << "\n";
926 out << "#define " << guard << "\n\n";
927
Martijn Coenen115d4282016-12-19 05:14:04 +0100928 out << "#include <hidl/HidlTransportSupport.h>\n\n";
929
Andreas Huber881227d2016-08-02 14:20:21 -0700930 std::vector<std::string> packageComponents;
931 getPackageAndVersionComponents(
932 &packageComponents, false /* cpp_compatible */);
933
Yifan Hongeefe4f22017-01-04 15:32:42 -0800934 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700935 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700936
937 enterLeaveNamespace(out, true /* enter */);
938 out << "\n";
939
940 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800941 << proxyName
942 << " : public ::android::hardware::BpInterface<"
943 << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000944 << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700945
946 out.indent();
947
Yifan Hongeefe4f22017-01-04 15:32:42 -0800948 out << "explicit "
949 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700950 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700951 << "\n\n";
952
Yifan Hong10fe0b52016-10-19 14:20:17 -0700953 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700954
Yifan Hong068c5522016-10-31 14:07:25 -0700955 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
956 method->generateCppSignature(out);
957 out << " override;\n";
958 return OK;
959 });
Steven Moreland9c387612016-09-07 09:54:26 -0700960
961 if (err != OK) {
962 return err;
963 }
Andreas Huber881227d2016-08-02 14:20:21 -0700964
965 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100966 out << "private:\n";
967 out.indent();
968 out << "std::mutex _hidl_mMutex;\n"
969 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
970 << " _hidl_mDeathRecipients;\n";
971 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700972 out << "};\n\n";
973
974 enterLeaveNamespace(out, false /* enter */);
975
976 out << "\n#endif // " << guard << "\n";
977
978 return OK;
979}
980
Andreas Huberb82318c2016-08-02 14:45:54 -0700981status_t AST::generateAllSource(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700982
Andreas Huberb82318c2016-08-02 14:45:54 -0700983 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700984 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700985 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700986
987 std::string ifaceName;
988 std::string baseName;
989
Yifan Hongfe95aa22016-10-19 17:26:45 -0700990 const Interface *iface = nullptr;
991 bool isInterface;
Andreas Huber881227d2016-08-02 14:20:21 -0700992 if (!AST::isInterface(&ifaceName)) {
993 baseName = "types";
994 isInterface = false;
995 } else {
Yifan Hongfe95aa22016-10-19 17:26:45 -0700996 iface = mRootScope->getInterface();
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700997 baseName = iface->getBaseName();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700998 isInterface = true;
Andreas Huber881227d2016-08-02 14:20:21 -0700999 }
1000
1001 path.append(baseName);
1002
1003 if (baseName != "types") {
1004 path.append("All");
1005 }
1006
1007 path.append(".cpp");
1008
Andreas Huberd2943e12016-08-05 11:59:31 -07001009 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -07001010 FILE *file = fopen(path.c_str(), "w");
1011
1012 if (file == NULL) {
1013 return -errno;
1014 }
1015
1016 Formatter out(file);
1017
Steven Moreland623c0042017-01-13 14:42:29 -08001018 out << "#define LOG_TAG \""
1019 << mPackage.string() << "::" << baseName
1020 << "\"\n\n";
1021
Steven Moreland05cd4232016-11-21 16:01:12 -08001022 out << "#include <android/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001023 out << "#include <cutils/trace.h>\n";
1024 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001025 if (isInterface) {
Steven Moreland19d5c172016-10-20 19:20:25 -07001026 // This is a no-op for IServiceManager itself.
1027 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
1028
Steven Morelandbec74ed2017-01-25 17:42:35 -08001029 // TODO(b/34274385) remove this
1030 out << "#include <hidl/LegacySupport.h>\n";
1031
Yifan Hongeefe4f22017-01-04 15:32:42 -08001032 generateCppPackageInclude(out, mPackage, iface->getProxyName());
1033 generateCppPackageInclude(out, mPackage, iface->getStubName());
1034 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001035
1036 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -07001037 generateCppPackageInclude(out,
1038 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -08001039 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001040 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -08001041
1042 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001043 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -07001044 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -08001045 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -07001046 }
1047
1048 out << "\n";
1049
1050 enterLeaveNamespace(out, true /* enter */);
1051 out << "\n";
1052
1053 status_t err = generateTypeSource(out, ifaceName);
1054
1055 if (err == OK && isInterface) {
Yifan Hong10fe0b52016-10-19 14:20:17 -07001056 const Interface *iface = mRootScope->getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -07001057
1058 // need to be put here, generateStubSource is using this.
Yifan Hongeefe4f22017-01-04 15:32:42 -08001059 out << "const char* "
1060 << iface->localName()
Yifan Hong10fe0b52016-10-19 14:20:17 -07001061 << "::descriptor(\""
1062 << iface->fqName().string()
1063 << "\");\n\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +01001064 out << "__attribute__((constructor))";
1065 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001066 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001067 out << "::android::hardware::details::gBnConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001068 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001069 << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001070 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001071 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001072 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001073 out << "return new "
1074 << iface->getStubName()
1075 << "(reinterpret_cast<"
1076 << iface->localName()
Yifan Hong158655a2016-11-08 12:34:07 -08001077 << " *>(iIntf));\n";
1078 });
Yifan Hongb04de382017-02-06 15:31:52 -08001079 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001080 });
Yifan Honga159f3b2017-03-16 14:53:51 -07001081 out << "::android::hardware::details::gBsConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001082 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001083 << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001084 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001085 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -08001086 << gIBaseFqName.cppName()
1087 << "> {\n";
1088 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001089 out << "return new "
1090 << iface->getPassthroughName()
1091 << "(reinterpret_cast<"
1092 << iface->localName()
Yifan Hong7a118f52016-12-07 11:21:15 -08001093 << " *>(iIntf));\n";
1094 });
Yifan Hongb04de382017-02-06 15:31:52 -08001095 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001096 });
Yifan Hong158655a2016-11-08 12:34:07 -08001097 });
Martijn Coenen8adcb652017-02-03 17:37:36 +01001098 out << "};\n\n";
1099 out << "__attribute__((destructor))";
1100 out << "static void static_destructor() {\n";
1101 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001102 out << "::android::hardware::details::gBnConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001103 << iface->localName()
1104 << "::descriptor);\n";
Yifan Honga159f3b2017-03-16 14:53:51 -07001105 out << "::android::hardware::details::gBsConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001106 << iface->localName()
1107 << "::descriptor);\n";
1108 });
1109 out << "};\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001110
Yifan Hongfe95aa22016-10-19 17:26:45 -07001111 err = generateInterfaceSource(out);
1112 }
1113
1114 if (err == OK && isInterface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001115 err = generateProxySource(out, iface->fqName());
Andreas Huber881227d2016-08-02 14:20:21 -07001116 }
1117
1118 if (err == OK && isInterface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001119 err = generateStubSource(out, iface);
Andreas Huber881227d2016-08-02 14:20:21 -07001120 }
1121
Steven Moreland40786312016-08-16 10:29:40 -07001122 if (err == OK && isInterface) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001123 err = generatePassthroughSource(out);
1124 }
1125
1126 if (err == OK && isInterface) {
Steven Moreland9c387612016-09-07 09:54:26 -07001127 const Interface *iface = mRootScope->getInterface();
1128
Yifan Hongc8934042016-11-17 17:10:52 -08001129 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001130 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -08001131 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001132 std::string package = iface->fqName().package()
1133 + iface->fqName().atVersion();
1134
Yifan Hongeefe4f22017-01-04 15:32:42 -08001135 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -08001136 }
Steven Moreland40786312016-08-16 10:29:40 -07001137 }
1138
Andreas Huber881227d2016-08-02 14:20:21 -07001139 enterLeaveNamespace(out, false /* enter */);
1140
1141 return err;
1142}
1143
Steven Moreland67f67b42016-09-29 08:59:02 -07001144// static
1145void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) {
Yifan Honga018ed52016-12-13 16:35:08 -08001146 out.sIf(nonNull + " == nullptr", [&] {
1147 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1148 out.indent(2, [&] {
1149 out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT);\n";
1150 });
1151 }).endl().endl();
Steven Moreland67f67b42016-09-29 08:59:02 -07001152}
1153
Andreas Huber881227d2016-08-02 14:20:21 -07001154status_t AST::generateTypeSource(
1155 Formatter &out, const std::string &ifaceName) const {
1156 return mRootScope->emitTypeDefinitions(out, ifaceName);
1157}
1158
Andreas Hubere7ff2282016-08-16 13:50:03 -07001159void AST::declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -07001160 Formatter &out,
1161 const std::vector<TypedVar *> &args,
1162 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -07001163 if (args.empty()) {
1164 return;
1165 }
1166
1167 for (const auto &arg : args) {
1168 const Type &type = arg->type();
1169
Yifan Hong3b320f82016-11-01 15:15:54 -07001170 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001171 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -07001172 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001173 << ";\n";
1174 }
1175
1176 out << "\n";
1177}
1178
Andreas Huber881227d2016-08-02 14:20:21 -07001179void AST::emitCppReaderWriter(
1180 Formatter &out,
1181 const std::string &parcelObj,
1182 bool parcelObjIsPointer,
1183 const TypedVar *arg,
1184 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -07001185 Type::ErrorMode mode,
1186 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -07001187 const Type &type = arg->type();
1188
Andreas Huber881227d2016-08-02 14:20:21 -07001189 type.emitReaderWriter(
1190 out,
Andreas Huber5e44a292016-09-27 14:52:39 -07001191 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -07001192 parcelObj,
1193 parcelObjIsPointer,
1194 isReader,
1195 mode);
1196}
1197
Yifan Hongbf459bc2016-08-23 16:50:37 -07001198void AST::emitCppResolveReferences(
1199 Formatter &out,
1200 const std::string &parcelObj,
1201 bool parcelObjIsPointer,
1202 const TypedVar *arg,
1203 bool isReader,
1204 Type::ErrorMode mode,
1205 bool addPrefixToName) const {
1206 const Type &type = arg->type();
1207 if(type.needsResolveReferences()) {
1208 type.emitResolveReferences(
1209 out,
1210 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
1211 isReader, // nameIsPointer
1212 parcelObj,
1213 parcelObjIsPointer,
1214 isReader,
1215 mode);
1216 }
1217}
1218
Yifan Hong068c5522016-10-31 14:07:25 -07001219status_t AST::generateProxyMethodSource(Formatter &out,
1220 const std::string &klassName,
1221 const Method *method,
1222 const Interface *superInterface) const {
1223
1224 method->generateCppSignature(out,
1225 klassName,
1226 true /* specify namespaces */);
1227
1228 const bool returnsValue = !method->results().empty();
1229 const TypedVar *elidedReturn = method->canElideCallback();
1230
Steven Moreland41c6d2e2016-11-07 12:26:54 -08001231 out << " {\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001232
1233 out.indent();
1234
Martijn Coenen115d4282016-12-19 05:14:04 +01001235 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
1236 method->cppImpl(IMPL_PROXY, out);
1237 out.unindent();
1238 out << "}\n\n";
1239 return OK;
1240 }
1241
Yifan Hong068c5522016-10-31 14:07:25 -07001242 if (returnsValue && elidedReturn == nullptr) {
1243 generateCheckNonNull(out, "_hidl_cb");
1244 }
1245
1246 status_t status = generateCppInstrumentationCall(
1247 out,
1248 InstrumentationEvent::CLIENT_API_ENTRY,
Yifan Hong068c5522016-10-31 14:07:25 -07001249 method);
1250 if (status != OK) {
1251 return status;
1252 }
1253
1254 out << "::android::hardware::Parcel _hidl_data;\n";
1255 out << "::android::hardware::Parcel _hidl_reply;\n";
1256 out << "::android::status_t _hidl_err;\n";
1257 out << "::android::hardware::Status _hidl_status;\n\n";
1258
1259 declareCppReaderLocals(
1260 out, method->results(), true /* forResults */);
1261
1262 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001263 out << superInterface->fqName().cppName();
Yifan Hong068c5522016-10-31 14:07:25 -07001264 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001265 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1266
Martijn Coenenfff73352017-01-04 16:36:31 +01001267 bool hasInterfaceArgument = false;
Yifan Hong068c5522016-10-31 14:07:25 -07001268 // First DFS: write all buffers and resolve pointers for parent
1269 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001270 if (arg->type().isInterface()) {
1271 hasInterfaceArgument = true;
1272 }
Yifan Hong068c5522016-10-31 14:07:25 -07001273 emitCppReaderWriter(
1274 out,
1275 "_hidl_data",
1276 false /* parcelObjIsPointer */,
1277 arg,
1278 false /* reader */,
1279 Type::ErrorMode_Goto,
1280 false /* addPrefixToName */);
1281 }
1282
1283 // Second DFS: resolve references.
1284 for (const auto &arg : method->args()) {
1285 emitCppResolveReferences(
1286 out,
1287 "_hidl_data",
1288 false /* parcelObjIsPointer */,
1289 arg,
1290 false /* reader */,
1291 Type::ErrorMode_Goto,
1292 false /* addPrefixToName */);
1293 }
1294
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001295 if (hasInterfaceArgument) {
1296 // Start binder threadpool to handle incoming transactions
1297 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1298 }
Yifan Hong068c5522016-10-31 14:07:25 -07001299 out << "_hidl_err = remote()->transact("
1300 << method->getSerialId()
1301 << " /* "
1302 << method->name()
1303 << " */, _hidl_data, &_hidl_reply";
1304
1305 if (method->isOneway()) {
1306 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
1307 }
1308 out << ");\n";
1309
1310 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1311
1312 if (!method->isOneway()) {
Yifan Hong859e53f2016-11-14 19:08:24 -08001313 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001314 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1315 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1316
1317
1318 // First DFS: write all buffers and resolve pointers for parent
1319 for (const auto &arg : method->results()) {
1320 emitCppReaderWriter(
1321 out,
1322 "_hidl_reply",
1323 false /* parcelObjIsPointer */,
1324 arg,
1325 true /* reader */,
1326 Type::ErrorMode_Goto,
1327 true /* addPrefixToName */);
1328 }
1329
1330 // Second DFS: resolve references.
1331 for (const auto &arg : method->results()) {
1332 emitCppResolveReferences(
1333 out,
1334 "_hidl_reply",
1335 false /* parcelObjIsPointer */,
1336 arg,
1337 true /* reader */,
1338 Type::ErrorMode_Goto,
1339 true /* addPrefixToName */);
1340 }
1341
1342 if (returnsValue && elidedReturn == nullptr) {
1343 out << "_hidl_cb(";
1344
1345 bool first = true;
1346 for (const auto &arg : method->results()) {
1347 if (!first) {
1348 out << ", ";
1349 }
1350
1351 if (arg->type().resultNeedsDeref()) {
1352 out << "*";
1353 }
1354 out << "_hidl_out_" << arg->name();
1355
1356 first = false;
1357 }
1358
1359 out << ");\n\n";
1360 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001361 }
1362 status = generateCppInstrumentationCall(
1363 out,
1364 InstrumentationEvent::CLIENT_API_EXIT,
1365 method);
1366 if (status != OK) {
1367 return status;
Yifan Hong068c5522016-10-31 14:07:25 -07001368 }
1369
1370 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001371 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1372 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001373 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001374 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1375 } else {
1376 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1377 out << "return ::android::hardware::Return<void>();\n\n";
1378 }
1379
1380 out.unindent();
1381 out << "_hidl_error:\n";
1382 out.indent();
1383 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1384 out << "return ::android::hardware::Return<";
1385 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001386 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001387 } else {
1388 out << "void";
1389 }
1390 out << ">(_hidl_status);\n";
1391
1392 out.unindent();
1393 out << "}\n\n";
1394 return OK;
1395}
1396
Andreas Huber881227d2016-08-02 14:20:21 -07001397status_t AST::generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -08001398 Formatter &out, const FQName &fqName) const {
1399 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001400
1401 out << klassName
1402 << "::"
1403 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001404 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001405
1406 out.indent();
1407 out.indent();
1408
1409 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001410 << "<"
1411 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001412 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001413 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001414 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001415 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001416 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001417 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001418
Andreas Huber881227d2016-08-02 14:20:21 -07001419 out.unindent();
1420 out.unindent();
1421 out << "}\n\n";
1422
Yifan Hong068c5522016-10-31 14:07:25 -07001423 status_t err = generateMethods(out, [&](const Method *method, const Interface *superInterface) {
1424 return generateProxyMethodSource(out, klassName, method, superInterface);
1425 });
Andreas Huber881227d2016-08-02 14:20:21 -07001426
Yifan Hong068c5522016-10-31 14:07:25 -07001427 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001428}
1429
1430status_t AST::generateStubSource(
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001431 Formatter &out,
Yifan Hongeefe4f22017-01-04 15:32:42 -08001432 const Interface *iface) const {
1433 const std::string interfaceName = iface->localName();
1434 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001435
Steven Moreland40786312016-08-16 10:29:40 -07001436 out << klassName
1437 << "::"
1438 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001439 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001440
1441 out.indent();
1442 out.indent();
1443
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001444 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001445 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001446 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001447 out << ": "
1448 << gIBaseFqName.getInterfaceStubFqName().cppName()
1449 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001450 }
1451
1452 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001453 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001454 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001455 << "\") { \n";
1456 out.indent();
1457 out << "_hidl_mImpl = _hidl_impl;\n";
1458 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001459
1460 out.unindent();
1461 out.unindent();
1462 out << "}\n\n";
1463
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001464 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001465 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001466 // class properly.
1467 out << klassName
1468 << "::"
1469 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001470 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1471 << " const std::string &HidlInstrumentor_package,"
1472 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001473
1474 out.indent();
1475 out.indent();
1476
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001477 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001478 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001479 out.indent();
1480 out << "_hidl_mImpl = _hidl_impl;\n";
1481 out.unindent();
1482
1483 out.unindent();
1484 out.unindent();
1485 out << "}\n\n";
1486 }
1487
Yifan Hongbcffce22017-02-01 15:52:06 -08001488 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1489 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
1490 return OK;
1491 }
1492 method->generateCppSignature(out, iface->getStubName());
1493 out << " ";
1494 out.block([&] {
1495 method->cppImpl(IMPL_STUB_IMPL, out);
1496 }).endl();
1497 return OK;
1498 });
Steven Moreland60818632017-02-04 00:33:42 -08001499 if (err != OK) {
1500 return err;
1501 }
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001502
Andreas Huber881227d2016-08-02 14:20:21 -07001503 out << "::android::status_t " << klassName << "::onTransact(\n";
1504
1505 out.indent();
1506 out.indent();
1507
Iliyan Malchev549e2592016-08-10 08:59:12 -07001508 out << "uint32_t _hidl_code,\n"
1509 << "const ::android::hardware::Parcel &_hidl_data,\n"
1510 << "::android::hardware::Parcel *_hidl_reply,\n"
1511 << "uint32_t _hidl_flags,\n"
1512 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001513
1514 out.unindent();
1515
Iliyan Malchev549e2592016-08-10 08:59:12 -07001516 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001517 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001518 out.indent();
1519
Yifan Hong10fe0b52016-10-19 14:20:17 -07001520 for (const auto &tuple : iface->allMethodsFromRoot()) {
1521 const Method *method = tuple.method();
1522 const Interface *superInterface = tuple.interface();
1523 out << "case "
1524 << method->getSerialId()
1525 << " /* "
1526 << method->name()
1527 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001528
Yifan Hong10fe0b52016-10-19 14:20:17 -07001529 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001530
Yifan Hong10fe0b52016-10-19 14:20:17 -07001531 status_t err =
1532 generateStubSourceForMethod(out, superInterface, method);
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001533
Yifan Hong10fe0b52016-10-19 14:20:17 -07001534 if (err != OK) {
1535 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001536 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001537
1538 out.unindent();
1539 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001540 }
1541
1542 out << "default:\n{\n";
1543 out.indent();
1544
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001545 out << "return onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001546
1547 out.indent();
1548 out.indent();
1549
Iliyan Malchev549e2592016-08-10 08:59:12 -07001550 out << "_hidl_code, _hidl_data, _hidl_reply, "
1551 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001552
1553 out.unindent();
1554 out.unindent();
1555
1556 out.unindent();
1557 out << "}\n";
1558
1559 out.unindent();
1560 out << "}\n\n";
1561
Yifan Honga018ed52016-12-13 16:35:08 -08001562 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1563 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1564 out.indent(2, [&] {
1565 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1566 out << "_hidl_reply);\n";
1567 });
1568 });
Andreas Huber881227d2016-08-02 14:20:21 -07001569
Iliyan Malchev549e2592016-08-10 08:59:12 -07001570 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001571
1572 out.unindent();
1573 out << "}\n\n";
1574
1575 return OK;
1576}
1577
1578status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001579 Formatter &out, const Interface *iface, const Method *method) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001580 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1581 method->cppImpl(IMPL_STUB, out);
1582 out << "break;\n";
1583 return OK;
1584 }
1585
Yifan Hongeefe4f22017-01-04 15:32:42 -08001586 out << "if (!_hidl_data.enforceInterface("
1587 << iface->fullName()
1588 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001589
Andreas Huber881227d2016-08-02 14:20:21 -07001590 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001591 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001592 out << "break;\n";
1593 out.unindent();
1594 out << "}\n\n";
1595
Andreas Huber5e44a292016-09-27 14:52:39 -07001596 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001597
Yifan Hongbf459bc2016-08-23 16:50:37 -07001598 // First DFS: write buffers
Andreas Huber881227d2016-08-02 14:20:21 -07001599 for (const auto &arg : method->args()) {
1600 emitCppReaderWriter(
1601 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001602 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001603 false /* parcelObjIsPointer */,
1604 arg,
1605 true /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001606 Type::ErrorMode_Break,
1607 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001608 }
1609
Yifan Hongbf459bc2016-08-23 16:50:37 -07001610 // Second DFS: resolve references
1611 for (const auto &arg : method->args()) {
1612 emitCppResolveReferences(
1613 out,
1614 "_hidl_data",
1615 false /* parcelObjIsPointer */,
1616 arg,
1617 true /* reader */,
1618 Type::ErrorMode_Break,
1619 false /* addPrefixToName */);
1620 }
1621
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001622 status_t status = generateCppInstrumentationCall(
1623 out,
1624 InstrumentationEvent::SERVER_API_ENTRY,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001625 method);
1626 if (status != OK) {
1627 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001628 }
1629
Andreas Huber881227d2016-08-02 14:20:21 -07001630 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001631 const TypedVar *elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -08001632 const std::string callee =
1633 (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL))
1634 ? "this" : "_hidl_mImpl";
Andreas Huber881227d2016-08-02 14:20:21 -07001635
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001636 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001637 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001638 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001639 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001640 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001641 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001642 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001643
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001644 bool first = true;
1645 for (const auto &arg : method->args()) {
Andreas Huber881227d2016-08-02 14:20:21 -07001646 if (!first) {
1647 out << ", ";
1648 }
1649
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001650 if (arg->type().resultNeedsDeref()) {
1651 out << "*";
1652 }
1653
1654 out << arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -07001655
1656 first = false;
1657 }
1658
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001659 out << ");\n\n";
Yifan Hong859e53f2016-11-14 19:08:24 -08001660 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1661 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001662
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001663 elidedReturn->type().emitReaderWriter(
1664 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001665 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001666 "_hidl_reply",
1667 true, /* parcelObjIsPointer */
1668 false, /* isReader */
1669 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001670
Yifan Hongbf459bc2016-08-23 16:50:37 -07001671 emitCppResolveReferences(
1672 out,
1673 "_hidl_reply",
1674 true /* parcelObjIsPointer */,
1675 elidedReturn,
1676 false /* reader */,
1677 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001678 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001679
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001680 status_t status = generateCppInstrumentationCall(
1681 out,
1682 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001683 method);
1684 if (status != OK) {
1685 return status;
1686 }
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001687
Iliyan Malchev549e2592016-08-10 08:59:12 -07001688 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001689 } else {
1690 if (returnsValue) {
1691 out << "bool _hidl_callbackCalled = false;\n\n";
1692 }
Andreas Huber881227d2016-08-02 14:20:21 -07001693
Yifan Hongcd2ae452017-01-31 14:33:40 -08001694 out << callee << "->" << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001695
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001696 bool first = true;
1697 for (const auto &arg : method->args()) {
1698 if (!first) {
1699 out << ", ";
1700 }
Andreas Huber881227d2016-08-02 14:20:21 -07001701
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001702 if (arg->type().resultNeedsDeref()) {
1703 out << "*";
1704 }
1705
1706 out << arg->name();
1707
1708 first = false;
1709 }
1710
1711 if (returnsValue) {
1712 if (!first) {
1713 out << ", ";
1714 }
1715
1716 out << "[&](";
1717
1718 first = true;
1719 for (const auto &arg : method->results()) {
1720 if (!first) {
1721 out << ", ";
1722 }
1723
Yifan Honga47eef32016-12-12 10:38:54 -08001724 out << "const auto &_hidl_out_" << arg->name();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001725
1726 first = false;
1727 }
1728
1729 out << ") {\n";
1730 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001731 out << "if (_hidl_callbackCalled) {\n";
1732 out.indent();
1733 out << "LOG_ALWAYS_FATAL(\""
1734 << method->name()
1735 << ": _hidl_cb called a second time, but must be called once.\");\n";
1736 out.unindent();
1737 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001738 out << "_hidl_callbackCalled = true;\n\n";
1739
Yifan Hong859e53f2016-11-14 19:08:24 -08001740 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1741 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001742
Yifan Hongbf459bc2016-08-23 16:50:37 -07001743 // First DFS: buffers
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001744 for (const auto &arg : method->results()) {
1745 emitCppReaderWriter(
1746 out,
1747 "_hidl_reply",
1748 true /* parcelObjIsPointer */,
1749 arg,
1750 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001751 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001752 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001753 }
1754
Yifan Hongbf459bc2016-08-23 16:50:37 -07001755 // Second DFS: resolve references
1756 for (const auto &arg : method->results()) {
1757 emitCppResolveReferences(
1758 out,
1759 "_hidl_reply",
1760 true /* parcelObjIsPointer */,
1761 arg,
1762 false /* reader */,
1763 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001764 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001765 }
1766
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001767 status_t status = generateCppInstrumentationCall(
1768 out,
1769 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001770 method);
1771 if (status != OK) {
1772 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001773 }
1774
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001775 out << "_hidl_cb(*_hidl_reply);\n";
1776
1777 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001778 out << "});\n\n";
1779 } else {
1780 out << ");\n\n";
1781 status_t status = generateCppInstrumentationCall(
1782 out,
1783 InstrumentationEvent::SERVER_API_EXIT,
1784 method);
1785 if (status != OK) {
1786 return status;
1787 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001788 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001789
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001790 if (returnsValue) {
1791 out << "if (!_hidl_callbackCalled) {\n";
1792 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001793 out << "LOG_ALWAYS_FATAL(\""
1794 << method->name()
1795 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001796 out.unindent();
1797 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001798 } else {
1799 out << "::android::hardware::writeToParcel("
1800 << "::android::hardware::Status::ok(), "
1801 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001802 }
Andreas Huber881227d2016-08-02 14:20:21 -07001803 }
1804
1805 out << "break;\n";
1806
1807 return OK;
1808}
1809
Steven Moreland69e7c702016-09-09 11:16:32 -07001810status_t AST::generatePassthroughHeader(const std::string &outputPath) const {
1811 std::string ifaceName;
1812 if (!AST::isInterface(&ifaceName)) {
1813 // types.hal does not get a stub header.
1814 return OK;
1815 }
1816
1817 const Interface *iface = mRootScope->getInterface();
1818
Yifan Hongeefe4f22017-01-04 15:32:42 -08001819 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001820
1821 bool supportOneway = iface->hasOnewayMethods();
1822
1823 std::string path = outputPath;
1824 path.append(mCoordinator->convertPackageRootToPath(mPackage));
1825 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
1826 path.append(klassName);
1827 path.append(".h");
1828
1829 CHECK(Coordinator::MakeParentHierarchy(path));
1830 FILE *file = fopen(path.c_str(), "w");
1831
1832 if (file == NULL) {
1833 return -errno;
1834 }
1835
1836 Formatter out(file);
1837
1838 const std::string guard = makeHeaderGuard(klassName);
1839
1840 out << "#ifndef " << guard << "\n";
1841 out << "#define " << guard << "\n\n";
1842
1843 std::vector<std::string> packageComponents;
1844 getPackageAndVersionComponents(
1845 &packageComponents, false /* cpp_compatible */);
1846
Yifan Hongb0949432016-12-15 15:32:24 -08001847 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001848 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001849
1850 generateCppPackageInclude(out, mPackage, ifaceName);
1851 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001852
Yifan Hong7a118f52016-12-07 11:21:15 -08001853 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001854 if (supportOneway) {
Yifan Hong2cbc1472016-10-25 19:02:40 -07001855 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001856 }
1857
1858 enterLeaveNamespace(out, true /* enter */);
1859 out << "\n";
1860
1861 out << "struct "
1862 << klassName
1863 << " : " << ifaceName
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001864 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001865
1866 out.indent();
1867 out << "explicit "
1868 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001869 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001870 << ifaceName
1871 << "> impl);\n";
1872
Yifan Hong068c5522016-10-31 14:07:25 -07001873 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1874 return generatePassthroughMethod(out, method);
1875 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001876
1877 if (err != OK) {
1878 return err;
1879 }
1880
1881 out.unindent();
1882 out << "private:\n";
1883 out.indent();
Steven Morelandc46e9842016-11-02 13:21:26 -07001884 out << "const ::android::sp<" << ifaceName << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001885
1886 if (supportOneway) {
Yifan Hongef91d362017-03-20 17:18:13 -07001887 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001888
1889 out << "\n";
1890
1891 out << "::android::hardware::Return<void> addOnewayTask("
1892 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001893 }
1894
1895 out.unindent();
1896
1897 out << "};\n\n";
1898
1899 enterLeaveNamespace(out, false /* enter */);
1900
1901 out << "\n#endif // " << guard << "\n";
1902
1903 return OK;
1904}
1905
Yifan Hongfe95aa22016-10-19 17:26:45 -07001906status_t AST::generateInterfaceSource(Formatter &out) const {
1907 const Interface *iface = mRootScope->getInterface();
1908
Yifan Hong2d7126b2016-10-20 15:12:57 -07001909 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001910 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001911
Steven Morelandd4b068a2017-03-20 06:30:51 -07001912 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1913 bool reserved = method->isHidlReserved();
1914
1915 if (!reserved) {
1916 out << "// no default implementation for: ";
1917 }
1918 method->generateCppSignature(out, iface->localName());
1919 if (reserved) {
1920 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001921 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001922 }).endl();
1923 }
1924
1925 out << "\n";
1926
1927 return OK;
1928 });
1929 if (err != OK) {
1930 return err;
1931 }
1932
Yifan Hong3d746092016-12-07 14:26:33 -08001933 for (const Interface *superType : iface->typeChain()) {
1934 out << "// static \n"
1935 << childTypeResult
Yifan Hongeefe4f22017-01-04 15:32:42 -08001936 << " "
1937 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001938 << "::castFrom("
1939 << superType->getCppArgumentType()
1940 << " parent) {\n";
1941 out.indent();
1942 if (iface == superType) {
1943 out << "return parent;\n";
1944 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001945 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001946 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001947 << superType->fqName().cppName() << ", "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001948 << iface->getProxyName() << ", "
Yifan Hong51a65092017-01-04 15:41:44 -08001949 << superType->getProxyFqName().cppName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001950 << ">(\n";
1951 out.indent();
1952 out.indent();
1953 out << "parent, \""
1954 << iface->fqName().string()
1955 << "\");\n";
1956 out.unindent();
1957 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001958 }
Yifan Hong3d746092016-12-07 14:26:33 -08001959 out.unindent();
1960 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001961 }
1962
1963 return OK;
1964}
1965
Steven Moreland69e7c702016-09-09 11:16:32 -07001966status_t AST::generatePassthroughSource(Formatter &out) const {
1967 const Interface *iface = mRootScope->getInterface();
1968
Yifan Hongeefe4f22017-01-04 15:32:42 -08001969 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001970
1971 out << klassName
1972 << "::"
1973 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001974 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001975 << iface->fullName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001976 << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001977 << mPackage.string()
1978 << "\", \""
1979 << iface->localName()
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001980 << "\"), mImpl(impl) {";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001981 if (iface->hasOnewayMethods()) {
1982 out << "\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001983 out.indent([&] {
Yifan Hongf01dad42017-03-20 19:03:11 -07001984 out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001985 });
1986 }
1987 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001988
1989 if (iface->hasOnewayMethods()) {
1990 out << "::android::hardware::Return<void> "
1991 << klassName
1992 << "::addOnewayTask(std::function<void(void)> fun) {\n";
1993 out.indent();
Yifan Hong2cbc1472016-10-25 19:02:40 -07001994 out << "if (!mOnewayQueue.push(fun)) {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001995 out.indent();
Steven Moreland67f67b42016-09-29 08:59:02 -07001996 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1997 out.indent();
1998 out.indent();
1999 out << "::android::hardware::Status::EX_TRANSACTION_FAILED);\n";
2000 out.unindent();
2001 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002002 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002003 out << "}\n";
2004
Steven Morelandd366c262016-10-11 15:29:10 -07002005 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002006
2007 out.unindent();
2008 out << "}\n\n";
2009
2010
2011 }
2012
2013 return OK;
2014}
2015
Martijn Coenen7b295242016-11-04 16:52:56 +01002016status_t AST::generateCppAtraceCall(Formatter &out,
2017 InstrumentationEvent event,
2018 const Method *method) const {
2019 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08002020 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01002021 switch (event) {
2022 case SERVER_API_ENTRY:
2023 {
2024 out << "atrace_begin(ATRACE_TAG_HAL, \""
2025 << baseString + "::server\");\n";
2026 break;
2027 }
2028 case CLIENT_API_ENTRY:
2029 {
2030 out << "atrace_begin(ATRACE_TAG_HAL, \""
2031 << baseString + "::client\");\n";
2032 break;
2033 }
2034 case PASSTHROUGH_ENTRY:
2035 {
2036 out << "atrace_begin(ATRACE_TAG_HAL, \""
2037 << baseString + "::passthrough\");\n";
2038 break;
2039 }
2040 case SERVER_API_EXIT:
2041 case CLIENT_API_EXIT:
2042 case PASSTHROUGH_EXIT:
2043 {
2044 out << "atrace_end(ATRACE_TAG_HAL);\n";
2045 break;
2046 }
2047 default:
2048 {
2049 LOG(ERROR) << "Unsupported instrumentation event: " << event;
2050 return UNKNOWN_ERROR;
2051 }
2052 }
2053
2054 return OK;
2055}
2056
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002057status_t AST::generateCppInstrumentationCall(
2058 Formatter &out,
2059 InstrumentationEvent event,
Steven Moreland031ccf12016-10-31 15:54:38 -07002060 const Method *method) const {
Martijn Coenen7b295242016-11-04 16:52:56 +01002061 status_t err = generateCppAtraceCall(out, event, method);
2062 if (err != OK) {
2063 return err;
2064 }
2065
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002066 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
2067 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002068 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002069 std::string event_str = "";
2070 switch (event) {
2071 case SERVER_API_ENTRY:
2072 {
2073 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
2074 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002075 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002076 << (arg->type().resultNeedsDeref() ? "" : "&")
2077 << arg->name()
2078 << ");\n";
2079 }
2080 break;
2081 }
2082 case SERVER_API_EXIT:
2083 {
2084 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07002085 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002086 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07002087 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002088 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002089 }
2090 break;
2091 }
2092 case CLIENT_API_ENTRY:
2093 {
2094 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
2095 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002096 out << "_hidl_args.push_back((void *)&"
2097 << arg->name()
2098 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002099 }
2100 break;
2101 }
2102 case CLIENT_API_EXIT:
2103 {
2104 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
2105 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002106 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002107 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002108 << "_hidl_out_"
2109 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002110 << ");\n";
2111 }
2112 break;
2113 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002114 case PASSTHROUGH_ENTRY:
2115 {
2116 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
2117 for (const auto &arg : method->args()) {
2118 out << "_hidl_args.push_back((void *)&"
2119 << arg->name()
2120 << ");\n";
2121 }
2122 break;
2123 }
2124 case PASSTHROUGH_EXIT:
2125 {
2126 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002127 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002128 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002129 << arg->name()
2130 << ");\n";
2131 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002132 break;
2133 }
Steven Moreland031ccf12016-10-31 15:54:38 -07002134 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002135 {
Steven Moreland031ccf12016-10-31 15:54:38 -07002136 LOG(ERROR) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002137 return UNKNOWN_ERROR;
2138 }
2139 }
2140
Steven Moreland031ccf12016-10-31 15:54:38 -07002141 const Interface *iface = mRootScope->getInterface();
2142
Steven Moreland1ab31442016-11-03 18:37:51 -07002143 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002144 out.indent();
2145 out << "callback("
2146 << event_str
2147 << ", \""
2148 << mPackage.package()
2149 << "\", \""
Yifan Hong90ea87f2016-11-01 14:25:47 -07002150 << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002151 << "\", \""
2152 << iface->localName()
2153 << "\", \""
2154 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002155 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002156 out.unindent();
2157 out << "}\n";
2158 out.unindent();
2159 out << "}\n\n";
2160
2161 return OK;
2162}
2163
Andreas Huber881227d2016-08-02 14:20:21 -07002164} // namespace android