blob: debfefde6868f91575f085847bcb8db2ae99f499 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Huber881227d2016-08-02 14:20:21 -070017#include "AST.h"
18
19#include "Coordinator.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070020#include "EnumType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070021#include "Interface.h"
Andreas Huber6755e9d2017-04-06 11:09:07 -070022#include "HidlTypeAssertion.h"
Andreas Huber881227d2016-08-02 14:20:21 -070023#include "Method.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070024#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070025#include "Scope.h"
26
Andreas Huberdca261f2016-08-04 13:47:51 -070027#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland5708edf2016-11-04 15:33:31 +000029#include <hidl-util/StringHelper.h>
Andreas Huber881227d2016-08-02 14:20:21 -070030#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070031#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070032#include <vector>
33
34namespace android {
35
Andreas Huberb82318c2016-08-02 14:45:54 -070036status_t AST::generateCpp(const std::string &outputPath) const {
Steven Moreland1cbf0362017-05-09 14:32:53 -070037 status_t err = generateCppHeaders(outputPath);
38
39 if (err == OK) {
40 err = generateCppSources(outputPath);
41 }
42
43 return err;
44}
45
46status_t AST::generateCppHeaders(const std::string &outputPath) const {
Andreas Huberb82318c2016-08-02 14:45:54 -070047 status_t err = generateInterfaceHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070048
49 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070050 err = generateStubHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070051 }
52
53 if (err == OK) {
Steven Moreland40786312016-08-16 10:29:40 -070054 err = generateHwBinderHeader(outputPath);
55 }
56
57 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070058 err = generateProxyHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070059 }
60
61 if (err == OK) {
Yifan Hong7a118f52016-12-07 11:21:15 -080062 err = generatePassthroughHeader(outputPath);
Steven Moreland69e7c702016-09-09 11:16:32 -070063 }
64
Andreas Huber881227d2016-08-02 14:20:21 -070065 return err;
66}
67
Andreas Huber737080b2016-08-02 15:38:04 -070068void AST::getPackageComponents(
69 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070070 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070071}
72
73void AST::getPackageAndVersionComponents(
74 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070075 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070076}
77
Steven Moreland5708edf2016-11-04 15:33:31 +000078std::string AST::makeHeaderGuard(const std::string &baseName,
79 bool indicateGenerated) const {
80 std::string guard;
Andreas Huber881227d2016-08-02 14:20:21 -070081
Steven Moreland5708edf2016-11-04 15:33:31 +000082 if (indicateGenerated) {
83 guard += "HIDL_GENERATED_";
84 }
85
86 guard += StringHelper::Uppercase(mPackage.tokenName());
Andreas Huber881227d2016-08-02 14:20:21 -070087 guard += "_";
Steven Moreland5708edf2016-11-04 15:33:31 +000088 guard += StringHelper::Uppercase(baseName);
89 guard += "_H";
Andreas Huber881227d2016-08-02 14:20:21 -070090
91 return guard;
92}
93
Steven Morelandee88eed2016-10-31 17:49:00 -070094void AST::generateCppPackageInclude(
95 Formatter &out,
96 const FQName &package,
97 const std::string &klass) {
98
99 out << "#include <";
100
101 std::vector<std::string> components;
102 package.getPackageAndVersionComponents(&components, false /* cpp_compatible */);
103
104 for (const auto &component : components) {
105 out << component << "/";
106 }
107
108 out << klass
109 << ".h>\n";
110}
111
Andreas Huber881227d2016-08-02 14:20:21 -0700112void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
113 std::vector<std::string> packageComponents;
114 getPackageAndVersionComponents(
115 &packageComponents, true /* cpp_compatible */);
116
117 if (enter) {
118 for (const auto &component : packageComponents) {
119 out << "namespace " << component << " {\n";
120 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700121
Andreas Huber2831d512016-08-15 09:33:47 -0700122 out.setNamespace(mPackage.cppNamespace() + "::");
Andreas Huber881227d2016-08-02 14:20:21 -0700123 } else {
Andreas Huber0e00de42016-08-03 09:56:02 -0700124 out.setNamespace(std::string());
125
Andreas Huber881227d2016-08-02 14:20:21 -0700126 for (auto it = packageComponents.rbegin();
127 it != packageComponents.rend();
128 ++it) {
129 out << "} // namespace " << *it << "\n";
130 }
131 }
132}
133
Steven Moreland038903b2017-03-30 12:11:24 -0700134static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
135 const std::string functionName = isTry ? "tryGetService" : "getService";
136
137 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800138 << "const std::string &serviceName=\"default\", bool getStub=false);\n";
Steven Moreland038903b2017-03-30 12:11:24 -0700139 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800140 << "const char serviceName[], bool getStub=false)"
141 << " { std::string str(serviceName ? serviceName : \"\");"
Steven Moreland038903b2017-03-30 12:11:24 -0700142 << " return " << functionName << "(str, getStub); }\n";
143 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800144 << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
145 // without c_str the std::string constructor is ambiguous
146 << " { std::string str(serviceName.c_str());"
Steven Moreland038903b2017-03-30 12:11:24 -0700147 << " return " << functionName << "(str, getStub); }\n";
148 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
149 << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
150}
151
152static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
153 declareGetService(out, interfaceName, true /* isTry */);
154 declareGetService(out, interfaceName, false /* isTry */);
155
Steven Moreland90831502017-03-27 12:08:40 -0700156 out << "__attribute__ ((warn_unused_result))"
157 << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800158 out << "static bool registerForNotifications(\n";
159 out.indent(2, [&] {
160 out << "const std::string &serviceName,\n"
161 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
162 << "&notification);\n";
163 });
164
165}
166
Steven Moreland038903b2017-03-30 12:11:24 -0700167static void implementGetService(Formatter &out,
168 const FQName &fqName,
169 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800170
171 const std::string interfaceName = fqName.getInterfaceName();
Steven Moreland038903b2017-03-30 12:11:24 -0700172 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800173
174 out << "// static\n"
Steven Moreland038903b2017-03-30 12:11:24 -0700175 << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000176 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800177 out.block([&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700178 out << "using ::android::hardware::defaultServiceManager;\n";
179 out << "using ::android::hardware::details::waitForHwService;\n";
180 out << "using ::android::hardware::getPassthroughServiceManager;\n";
181 out << "using ::android::hardware::Return;\n";
182 out << "using ::android::sp;\n";
183 out << "using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;\n\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000184
Steven Morelandbcf51802017-04-06 09:17:44 -0700185 out << "sp<" << interfaceName << "> iface = nullptr;\n";
186
187 out.endl();
188
189 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> sm"
190 << " = defaultServiceManager();\n";
191
192 out.sIf("sm == nullptr", [&] {
193 // hwbinder is not available on this device, so future tries
194 // would also be null. I can only return nullptr.
195 out << "ALOGE(\"getService: defaultServiceManager() is null\");\n"
196 << "return nullptr;\n";
197 }).endl().endl();
198
199 out << "Return<Transport> transportRet = sm->getTransport("
200 << interfaceName << "::descriptor, serviceName);\n\n";
201
202 out.sIf("!transportRet.isOk()", [&] {
203 out << "ALOGE(\"getService: defaultServiceManager()->getTransport returns %s\", "
204 << "transportRet.description().c_str());\n";
205 out << "return nullptr;\n";
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700206 }).endl();
Steven Morelandbcf51802017-04-06 09:17:44 -0700207
208 out << "Transport transport = transportRet;\n";
209 out << "const bool vintfHwbinder = (transport == Transport::HWBINDER);\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700210 << "const bool vintfPassthru = (transport == Transport::PASSTHROUGH);\n\n";
211
212 // This means that you must set TREBLE_TESTING_OVERRIDE when running a test such
213 // as hidl_test. Ideally these binaries set this value themselves. This allows
214 // test modules to dynamically add and unset services even though they are not
215 // declared in the device manifest. This prevents a problem where framework
216 // changes are accidentally made in a way that is not backwards compatible. For
217 // instance, consider the following situation for two devices developed in the
218 // same tree:
219 // A: serves @1.1::IFoo, declares @1.0::IFoo (incorrect)
220 // B: serves @1.0::IFoo, declares @1.0::IFoo (correct configuration)
221 // If development is done on device A, then framework code like: "V1_1::IFoo::
222 // getService()->doV1_0Api()" will work. However, this will unintentionally break
223 // the feature for devices like device B for which "V1_1::IFoo::getService()
224 // will return nullptr. In order to prevent problems like this, we only allow
225 // fetching an interface if it is declared in a VINTF manifest.
226 out << "#ifdef __ANDROID_TREBLE__\n\n"
227 << "#ifdef __ANDROID_DEBUGGABLE__\n"
228 << "const char* env = std::getenv(\"TREBLE_TESTING_OVERRIDE\");\n"
229 << "const bool vintfLegacy = (transport == Transport::EMPTY) && env && !strcmp(env, \"true\");\n"
230 << "#else // __ANDROID_TREBLE__ but not __ANDROID_DEBUGGABLE__\n"
231 << "const bool vintfLegacy = false;\n"
232 << "#endif // __ANDROID_DEBUGGABLE__\n\n"
233 << "#else // not __ANDROID_TREBLE__\n"
234 << "const bool vintfLegacy = (transport == Transport::EMPTY);\n\n"
235 << "#endif // __ANDROID_TREBLE__\n\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000236
237 // if (getStub) {
238 // getPassthroughServiceManager()->get only once.
239 // } else {
240 // if (vintfHwbinder) {
241 // while (no alive service) {
242 // waitForHwService
243 // defaultServiceManager()->get
244 // }
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700245 // } else if (vintfLegacy) {
Yifan Hong31f07ff2017-03-21 18:56:35 +0000246 // defaultServiceManager()->get only once.
247 // getPassthroughServiceManager()->get only once.
248 // } else if (vintfPassthru) {
249 // getPassthroughServiceManager()->get only once.
250 // }
251 // }
252
Yifan Hong223fd472017-03-23 17:17:57 +0000253 out << "bool tried = false;\n";
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700254 out.sWhile("!getStub && (vintfHwbinder || (vintfLegacy && !tried))", [&] {
Yifan Hong31f07ff2017-03-21 18:56:35 +0000255
256 out.sIf("tried", [&] {
257 // sleep only after the first trial.
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700258 out << "ALOGI(\"" << functionName << ": retrying in 1s...\");\n"
Yifan Hong31f07ff2017-03-21 18:56:35 +0000259 << "sleep(1);\n";
260 }).endl();
261
Yifan Hong223fd472017-03-23 17:17:57 +0000262 out << "tried = true;\n";
263
Yifan Hong31f07ff2017-03-21 18:56:35 +0000264
Steven Moreland038903b2017-03-30 12:11:24 -0700265 if (!isTry) {
266 out.sIf("vintfHwbinder", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700267 out << "waitForHwService("
268 << interfaceName << "::descriptor, serviceName);\n";
Steven Moreland038903b2017-03-30 12:11:24 -0700269 }).endl();
270 }
Yifan Hong31f07ff2017-03-21 18:56:35 +0000271
Steven Morelandbcf51802017-04-06 09:17:44 -0700272 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000273 out.indent(2, [&] {
274 out << "sm->get(" << interfaceName << "::descriptor, serviceName);\n";
275 });
276
277 out.sIf("!ret.isOk()", [&] {
Steven Moreland42394ce2017-03-27 17:03:04 -0700278 // hwservicemanager fails, may be security issue
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700279 out << "ALOGE(\"" << interfaceName << ": defaultServiceManager()->get returns %s\", "
Yifan Hong31f07ff2017-03-21 18:56:35 +0000280 << "ret.description().c_str());\n"
Steven Moreland42394ce2017-03-27 17:03:04 -0700281 << "break;\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000282 }).endl();
283
Steven Morelandbcf51802017-04-06 09:17:44 -0700284 out << "sp<" << gIBaseFqName.cppName() << "> base = ret;\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700285 out.sIf("base == nullptr", [&] {
286 // race condition. hwservicemanager drops the service
287 // from waitForHwService to here
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700288 out << "ALOGW(\"" << interfaceName << ": found null hwbinder interface\");\n"
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700289 << (isTry ? "break" : "continue")
290 << ";\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000291 }).endl();
Steven Morelandbcf51802017-04-06 09:17:44 -0700292 out << "Return<sp<" << interfaceName
Yifan Hong200209c2017-03-29 03:39:09 -0700293 << ">> castRet = " << interfaceName << "::castFrom(base, true /* emitError */);\n";
294 out.sIf("!castRet.isOk()", [&] {
295 out.sIf("castRet.isDeadObject()", [&] {
296 // service is dead (castFrom cannot call interfaceChain)
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700297 out << "ALOGW(\"" << interfaceName << ": found dead hwbinder service\");\n"
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700298 << (isTry ? "break" : "continue")
299 << ";\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700300 }).sElse([&] {
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700301 out << "ALOGW(\"" << interfaceName << ": cannot call into hwbinder service: %s"
Yifan Hong200209c2017-03-29 03:39:09 -0700302 << "; No permission? Check for selinux denials.\", "
303 << "castRet.description().c_str());\n"
304 << "break;\n";
305 }).endl();
306 }).endl();
307 out << "iface = castRet;\n";
308 out.sIf("iface == nullptr", [&] {
309 // returned service isn't of correct type; this is a bug
310 // to hwservicemanager or to the service itself (interfaceChain
311 // is not consistent).
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700312 out << "ALOGW(\"" << interfaceName << ": received incompatible service; bug in hwservicemanager?\");\n"
Yifan Hong200209c2017-03-29 03:39:09 -0700313 << "break;\n";
314 }).endl();
Yifan Hong31f07ff2017-03-21 18:56:35 +0000315
316 out << "return iface;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800317 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800318
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700319 out.sIf("getStub || vintfPassthru || vintfLegacy", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700320 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> pm"
321 << " = getPassthroughServiceManager();\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000322
323 out.sIf("pm != nullptr", [&] () {
Steven Morelandbcf51802017-04-06 09:17:44 -0700324 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Steven Morelandf10af872017-01-25 16:01:56 +0000325 out.indent(2, [&] {
326 out << "pm->get(" << interfaceName << "::descriptor" << ", serviceName);\n";
Steven Moreland2c2dea82017-01-18 17:24:17 -0800327 });
Steven Morelandf10af872017-01-25 16:01:56 +0000328 out.sIf("ret.isOk()", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700329 out << "sp<" << gIBaseFqName.cppName()
Steven Morelandf10af872017-01-25 16:01:56 +0000330 << "> baseInterface = ret;\n";
331 out.sIf("baseInterface != nullptr", [&]() {
332 out << "iface = new " << fqName.getInterfacePassthroughName()
333 << "(" << interfaceName << "::castFrom(baseInterface));\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000334 }).endl();
Steven Morelandf10af872017-01-25 16:01:56 +0000335 }).endl();
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800336 }).endl();
337 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800338
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800339 out << "return iface;\n";
340 }).endl().endl();
Steven Moreland038903b2017-03-30 12:11:24 -0700341}
342
343static void implementServiceManagerInteractions(Formatter &out,
344 const FQName &fqName, const std::string &package) {
345
346 const std::string interfaceName = fqName.getInterfaceName();
347
348 implementGetService(out, fqName, true /* isTry */);
349 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800350
Yifan Hongeefe4f22017-01-04 15:32:42 -0800351 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800352 << "const std::string &serviceName) ";
353 out.block([&] {
Steven Moreland58b478b2017-04-09 10:54:50 -0700354 out << "::android::hardware::details::onRegistration(\""
355 << fqName.getPackageAndVersion().string() << "\", \""
356 << interfaceName
357 << "\", serviceName);\n\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800358 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
359 out.indent(2, [&] {
360 out << "= ::android::hardware::defaultServiceManager();\n";
361 });
362 out.sIf("sm == nullptr", [&] {
363 out << "return ::android::INVALID_OPERATION;\n";
364 }).endl();
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100365 out << "::android::hardware::Return<bool> ret = "
366 << "sm->add(serviceName.c_str(), this);\n"
367 << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800368 }).endl().endl();
369
Yifan Hongeefe4f22017-01-04 15:32:42 -0800370 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800371 out.indent(2, [&] {
372 out << "const std::string &serviceName,\n"
373 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
374 << "&notification) ";
375 });
376 out.block([&] {
377 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
378 out.indent(2, [&] {
379 out << "= ::android::hardware::defaultServiceManager();\n";
380 });
381 out.sIf("sm == nullptr", [&] {
382 out << "return false;\n";
383 }).endl();
384 out << "::android::hardware::Return<bool> success =\n";
385 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800386 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800387 out.indent(2, [&] {
388 out << "serviceName, notification);\n";
389 });
390 });
391 out << "return success.isOk() && success;\n";
392 }).endl().endl();
393}
394
Andreas Huberb82318c2016-08-02 14:45:54 -0700395status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700396 const Interface *iface = getInterface();
397 std::string ifaceName = iface ? iface->localName() : "types";
Andreas Huber881227d2016-08-02 14:20:21 -0700398
Andreas Huberb82318c2016-08-02 14:45:54 -0700399 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700400 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700401 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700402 path.append(ifaceName);
403 path.append(".h");
404
Andreas Huberd2943e12016-08-05 11:59:31 -0700405 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700406 FILE *file = fopen(path.c_str(), "w");
407
408 if (file == NULL) {
409 return -errno;
410 }
411
412 Formatter out(file);
413
414 const std::string guard = makeHeaderGuard(ifaceName);
415
416 out << "#ifndef " << guard << "\n";
417 out << "#define " << guard << "\n\n";
418
Andreas Huber737080b2016-08-02 15:38:04 -0700419 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700420 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700421 }
422
423 if (!mImportedNames.empty()) {
424 out << "\n";
425 }
426
Steven Moreland19f11b52017-05-12 18:22:21 -0700427 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800428 if (isIBase()) {
429 out << "// skipped #include IServiceNotification.h\n\n";
430 } else {
431 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
432 }
Steven Moreland0693f312016-11-09 15:06:14 -0800433 }
434
Yifan Hongc8934042016-11-17 17:10:52 -0800435 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700436 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700437
Steven Moreland19f11b52017-05-12 18:22:21 -0700438 if (iface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200439 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700440 }
441
Martijn Coenenaf712c02016-11-16 15:26:27 +0100442 out << "#include <utils/NativeHandle.h>\n";
443 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700444
445 enterLeaveNamespace(out, true /* enter */);
446 out << "\n";
447
Steven Moreland19f11b52017-05-12 18:22:21 -0700448 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700449 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700450 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700451
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700452 const Interface *superType = iface->superType();
453
Steven Moreland40786312016-08-16 10:29:40 -0700454 if (superType == NULL) {
Yifan Hongc8934042016-11-17 17:10:52 -0800455 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700456 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000457 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700458 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700459 }
460
461 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700462
463 out.indent();
464
Andreas Huber881227d2016-08-02 14:20:21 -0700465 }
466
467 status_t err = emitTypeDeclarations(out);
468
469 if (err != OK) {
470 return err;
471 }
472
Steven Moreland19f11b52017-05-12 18:22:21 -0700473 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800474 out << "virtual bool isRemote() const ";
475 if (!isIBase()) {
476 out << "override ";
477 }
478 out << "{ return false; }\n\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800479
Andreas Huber881227d2016-08-02 14:20:21 -0700480 for (const auto &method : iface->methods()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700481 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700482
Andreas Huber881227d2016-08-02 14:20:21 -0700483 const bool returnsValue = !method->results().empty();
Steven Morelandd732ea12016-11-08 17:12:06 -0800484 const TypedVar *elidedReturn = method->canElideCallback();
485
486 if (elidedReturn == nullptr && returnsValue) {
487 out << "using "
488 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700489 << "_cb = std::function<void(";
490 method->emitCppResultSignature(out, true /* specify namespaces */);
491 out << ")>;\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800492 }
Andreas Huber881227d2016-08-02 14:20:21 -0700493
Andreas Huber3599d922016-08-09 10:42:57 -0700494 method->dumpAnnotations(out);
495
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700496 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700497 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700498 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700499 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700500 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700501 }
502
503 out << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700504 << "(";
505 method->emitCppArgSignature(out, true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700506
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700507 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700508 if (!method->args().empty()) {
509 out << ", ";
510 }
511
Steven Moreland67f67b42016-09-29 08:59:02 -0700512 out << method->name() << "_cb _hidl_cb";
Andreas Huber881227d2016-08-02 14:20:21 -0700513 }
514
Yifan Hong10fe0b52016-10-19 14:20:17 -0700515 out << ")";
516 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800517 if (!isIBase()) {
518 out << " override";
519 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700520 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700521 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700522 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700523 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700524 }
Steven Moreland40786312016-08-16 10:29:40 -0700525
Yifan Hong3d746092016-12-07 14:26:33 -0800526 out << "// cast static functions\n";
527 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700528
Yifan Hong3d746092016-12-07 14:26:33 -0800529 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -0700530 out << "static ::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -0800531 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -0700532 << "> castFrom("
Yifan Hong3d746092016-12-07 14:26:33 -0800533 << superType->getCppArgumentType()
534 << " parent"
Yifan Hong200209c2017-03-29 03:39:09 -0700535 << ", bool emitError = false);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700536 }
537
Steven Morelandd39133b2016-11-11 12:30:08 -0800538 out << "\nstatic const char* descriptor;\n\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700539
Yifan Hongc8934042016-11-17 17:10:52 -0800540 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800541 out << "// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800542 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800543 declareServiceManagerInteractions(out, iface->localName());
Yifan Hongc8934042016-11-17 17:10:52 -0800544 }
Andreas Huber881227d2016-08-02 14:20:21 -0700545 }
546
Steven Moreland19f11b52017-05-12 18:22:21 -0700547 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700548 out.unindent();
549
Andreas Hubere3f769a2016-10-10 10:54:44 -0700550 out << "};\n\n";
551 }
552
553 err = mRootScope->emitGlobalTypeDeclarations(out);
554
555 if (err != OK) {
556 return err;
Andreas Huber881227d2016-08-02 14:20:21 -0700557 }
558
559 out << "\n";
560 enterLeaveNamespace(out, false /* enter */);
561
562 out << "\n#endif // " << guard << "\n";
563
564 return OK;
565}
566
Steven Moreland40786312016-08-16 10:29:40 -0700567status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700568 const Interface *iface = getInterface();
569 std::string klassName = iface ? iface->getHwName() : "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700570
Steven Moreland40786312016-08-16 10:29:40 -0700571 std::string path = outputPath;
572 path.append(mCoordinator->convertPackageRootToPath(mPackage));
573 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
574 path.append(klassName + ".h");
575
Yifan Hong244e82d2016-11-11 11:13:57 -0800576 FILE *file = fopen(path.c_str(), "w");
Steven Moreland40786312016-08-16 10:29:40 -0700577
578 if (file == NULL) {
579 return -errno;
580 }
581
582 Formatter out(file);
583
584 const std::string guard = makeHeaderGuard(klassName);
585
586 out << "#ifndef " << guard << "\n";
587 out << "#define " << guard << "\n\n";
588
Steven Moreland19f11b52017-05-12 18:22:21 -0700589 generateCppPackageInclude(out, mPackage, iface ? iface->localName() : "types");
Steven Moreland40786312016-08-16 10:29:40 -0700590
Steven Morelandee88eed2016-10-31 17:49:00 -0700591 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700592
593 for (const auto &item : mImportedNames) {
594 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800595 generateCppPackageInclude(out, item, "hwtypes");
596 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800597 generateCppPackageInclude(out, item, item.getInterfaceStubName());
598 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700599 }
Steven Moreland40786312016-08-16 10:29:40 -0700600 }
601
602 out << "\n";
603
Martijn Coenen93915102016-09-01 01:35:52 +0200604 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700605 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100606 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700607
608 out << "\n";
609
610 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700611
Yifan Hong244e82d2016-11-11 11:13:57 -0800612 status_t err = mRootScope->emitGlobalHwDeclarations(out);
613 if (err != OK) {
614 return err;
615 }
Steven Moreland40786312016-08-16 10:29:40 -0700616
617 enterLeaveNamespace(out, false /* enter */);
618
619 out << "\n#endif // " << guard << "\n";
620
621 return OK;
622}
623
Andreas Huber881227d2016-08-02 14:20:21 -0700624status_t AST::emitTypeDeclarations(Formatter &out) const {
625 return mRootScope->emitTypeDeclarations(out);
626}
627
Yifan Hong7a118f52016-12-07 11:21:15 -0800628static void wrapPassthroughArg(Formatter &out,
629 const TypedVar *arg, bool addPrefixToName,
630 std::function<void(void)> handleError) {
631 if (!arg->type().isInterface()) {
632 return;
633 }
634 std::string name = (addPrefixToName ? "_hidl_out_" : "") + arg->name();
635 std::string wrappedName = (addPrefixToName ? "_hidl_out_wrapped_" : "_hidl_wrapped_")
636 + arg->name();
637 const Interface &iface = static_cast<const Interface &>(arg->type());
638 out << iface.getCppStackType() << " " << wrappedName << ";\n";
639 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
640 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
641 out << wrappedName
642 << " = "
643 << iface.fqName().cppName()
Yifan Hong341112d2017-04-20 18:12:05 -0700644 << "::castFrom(::android::hardware::details::wrapPassthrough<"
645 << iface.fqName().cppName()
646 << ">("
Yifan Hong7a118f52016-12-07 11:21:15 -0800647 << name << "));\n";
648 out.sIf(wrappedName + " == nullptr", [&] {
649 // Fatal error. Happens when the BsFoo class is not found in the binary
650 // or any dynamic libraries.
651 handleError();
652 }).endl();
653 }).sElse([&] {
654 out << wrappedName << " = " << name << ";\n";
655 }).endl().endl();
656}
657
Steven Moreland69e7c702016-09-09 11:16:32 -0700658status_t AST::generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700659 const Method *method) const {
660 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700661
662 out << " {\n";
663 out.indent();
664
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800665 if (method->isHidlReserved()
666 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
667 method->cppImpl(IMPL_PASSTHROUGH, out);
668 out.unindent();
669 out << "}\n\n";
670 return OK;
671 }
672
Steven Moreland69e7c702016-09-09 11:16:32 -0700673 const bool returnsValue = !method->results().empty();
674 const TypedVar *elidedReturn = method->canElideCallback();
675
Steven Moreland67f67b42016-09-29 08:59:02 -0700676 if (returnsValue && elidedReturn == nullptr) {
677 generateCheckNonNull(out, "_hidl_cb");
678 }
679
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700680 generateCppInstrumentationCall(
681 out,
682 InstrumentationEvent::PASSTHROUGH_ENTRY,
683 method);
684
Yifan Hong7a118f52016-12-07 11:21:15 -0800685
686 for (const auto &arg : method->args()) {
687 wrapPassthroughArg(out, arg, false /* addPrefixToName */, [&] {
688 out << "return ::android::hardware::Status::fromExceptionCode(\n";
689 out.indent(2, [&] {
690 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800691 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800692 });
693 });
694 }
695
696 out << "auto _hidl_error = ::android::hardware::Void();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700697 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700698
699 if (method->isOneway()) {
Steven Morelanda4c565f2017-05-02 12:10:13 -0700700 out << "addOnewayTask([mImpl = this->mImpl, "
701 "mEnableInstrumentation = this->mEnableInstrumentation, "
Steven Moreland340c8822017-05-02 14:41:49 -0700702 "mInstrumentationCallbacks = this->mInstrumentationCallbacks";
Steven Moreland69e7c702016-09-09 11:16:32 -0700703 for (const auto &arg : method->args()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800704 out << ", "
705 << (arg->type().isInterface() ? "_hidl_wrapped_" : "")
706 << arg->name();
Steven Moreland69e7c702016-09-09 11:16:32 -0700707 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700708 out << "] {\n";
709 out.indent();
Steven Moreland69e7c702016-09-09 11:16:32 -0700710 }
711
712 out << "mImpl->"
713 << method->name()
714 << "(";
715
Yifan Hong932464e2017-03-30 15:40:22 -0700716 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800717 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700718 });
Steven Moreland69e7c702016-09-09 11:16:32 -0700719 if (returnsValue && elidedReturn == nullptr) {
Steven Moreland340c8822017-05-02 14:41:49 -0700720 // never true if oneway since oneway methods don't return values
721
Steven Moreland69e7c702016-09-09 11:16:32 -0700722 if (!method->args().empty()) {
723 out << ", ";
724 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800725 out << "[&](";
Yifan Hong932464e2017-03-30 15:40:22 -0700726 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800727 out << "const auto &_hidl_out_"
728 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700729 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800730
731 out << ") {\n";
732 out.indent();
733 status_t status = generateCppInstrumentationCall(
734 out,
735 InstrumentationEvent::PASSTHROUGH_EXIT,
736 method);
737 if (status != OK) {
738 return status;
739 }
740
Yifan Hong7a118f52016-12-07 11:21:15 -0800741 for (const auto &arg : method->results()) {
742 wrapPassthroughArg(out, arg, true /* addPrefixToName */, [&] {
743 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
744 out.indent(2, [&] {
745 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800746 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800747 });
748 out << "return;\n";
749 });
750 }
751
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800752 out << "_hidl_cb(";
Yifan Hong932464e2017-03-30 15:40:22 -0700753 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800754 out << (arg->type().isInterface() ? "_hidl_out_wrapped_" : "_hidl_out_")
755 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700756 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800757 out << ");\n";
758 out.unindent();
759 out << "});\n\n";
760 } else {
761 out << ");\n\n";
762 if (elidedReturn != nullptr) {
763 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -0800764 << " _hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800765 << elidedReturn->name()
Steven Moreland2ae5bca2016-12-01 05:56:49 +0000766 << " = _hidl_return;\n";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800767 }
768 status_t status = generateCppInstrumentationCall(
769 out,
770 InstrumentationEvent::PASSTHROUGH_EXIT,
771 method);
772 if (status != OK) {
773 return status;
774 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700775 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700776
777 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700778 out.unindent();
779 out << "});\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700780 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700781
782 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700783
784 out.unindent();
785 out << "}\n";
786
787 return OK;
788}
789
Yifan Hong068c5522016-10-31 14:07:25 -0700790status_t AST::generateMethods(Formatter &out, MethodGenerator gen) const {
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700791 const Interface *iface = mRootScope->getInterface();
792
Yifan Hong10fe0b52016-10-19 14:20:17 -0700793 const Interface *prevIterface = nullptr;
794 for (const auto &tuple : iface->allMethodsFromRoot()) {
795 const Method *method = tuple.method();
796 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700797
Yifan Hong10fe0b52016-10-19 14:20:17 -0700798 if(prevIterface != superInterface) {
799 if (prevIterface != nullptr) {
800 out << "\n";
801 }
802 out << "// Methods from "
803 << superInterface->fullName()
804 << " follow.\n";
805 prevIterface = superInterface;
806 }
Yifan Hong068c5522016-10-31 14:07:25 -0700807 status_t err = gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700808
Yifan Hong10fe0b52016-10-19 14:20:17 -0700809 if (err != OK) {
810 return err;
811 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700812 }
813
Yifan Hong10fe0b52016-10-19 14:20:17 -0700814 out << "\n";
815
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700816 return OK;
817}
818
Andreas Huberb82318c2016-08-02 14:45:54 -0700819status_t AST::generateStubHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700820 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700821 // types.hal does not get a stub header.
822 return OK;
823 }
824
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700825 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800826 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -0700827
Andreas Huberb82318c2016-08-02 14:45:54 -0700828 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700829 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700830 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700831 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700832 path.append(".h");
833
Andreas Huberd2943e12016-08-05 11:59:31 -0700834 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700835 FILE *file = fopen(path.c_str(), "w");
836
837 if (file == NULL) {
838 return -errno;
839 }
840
841 Formatter out(file);
842
Steven Moreland40786312016-08-16 10:29:40 -0700843 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700844
845 out << "#ifndef " << guard << "\n";
846 out << "#define " << guard << "\n\n";
847
Yifan Hongeefe4f22017-01-04 15:32:42 -0800848 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700849 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700850
851 enterLeaveNamespace(out, true /* enter */);
852 out << "\n";
853
854 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800855 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100856 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800857 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000858 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100859 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800860 out << " : public "
861 << gIBaseFqName.getInterfaceStubFqName().cppName()
862 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100863 }
Andreas Huber881227d2016-08-02 14:20:21 -0700864
865 out.indent();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800866 out << "explicit "
867 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700868 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100869 << "\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800870 out << "explicit "
871 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700872 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800873 << " const std::string& HidlInstrumentor_package,"
874 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700875 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700876 out << "::android::status_t onTransact(\n";
877 out.indent();
878 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700879 out << "uint32_t _hidl_code,\n";
880 out << "const ::android::hardware::Parcel &_hidl_data,\n";
881 out << "::android::hardware::Parcel *_hidl_reply,\n";
882 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700883 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700884 out.unindent();
885 out.unindent();
886
Steven Moreland19f11b52017-05-12 18:22:21 -0700887 out << "::android::sp<" << iface->localName() << "> getImpl() { return _hidl_mImpl; };\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100888 out.unindent();
889 out << "private:\n";
890 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800891
892 status_t err = generateMethods(out, [&](const Method *method, const Interface *iface) {
893 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
894 return OK;
895 }
896 const bool returnsValue = !method->results().empty();
897 const TypedVar *elidedReturn = method->canElideCallback();
898
899 if (elidedReturn == nullptr && returnsValue) {
900 out << "using " << method->name() << "_cb = "
901 << iface->fqName().cppName()
902 << "::" << method->name() << "_cb;\n";
903 }
904 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800905 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800906 return OK;
907 });
908 if (err != OK) {
909 return err;
910 }
911
Steven Moreland19f11b52017-05-12 18:22:21 -0700912 out << "::android::sp<" << iface->localName() << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700913 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700914 out << "};\n\n";
915
916 enterLeaveNamespace(out, false /* enter */);
917
918 out << "\n#endif // " << guard << "\n";
919
920 return OK;
921}
922
Andreas Huberb82318c2016-08-02 14:45:54 -0700923status_t AST::generateProxyHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700924 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700925 // types.hal does not get a proxy header.
926 return OK;
927 }
928
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700929 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800930 const std::string proxyName = iface->getProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -0700931
Andreas Huberb82318c2016-08-02 14:45:54 -0700932 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700933 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700934 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Yifan Hongeefe4f22017-01-04 15:32:42 -0800935 path.append(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700936 path.append(".h");
937
Andreas Huberd2943e12016-08-05 11:59:31 -0700938 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700939 FILE *file = fopen(path.c_str(), "w");
940
941 if (file == NULL) {
942 return -errno;
943 }
944
945 Formatter out(file);
946
Yifan Hongeefe4f22017-01-04 15:32:42 -0800947 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700948
949 out << "#ifndef " << guard << "\n";
950 out << "#define " << guard << "\n\n";
951
Martijn Coenen115d4282016-12-19 05:14:04 +0100952 out << "#include <hidl/HidlTransportSupport.h>\n\n";
953
Andreas Huber881227d2016-08-02 14:20:21 -0700954 std::vector<std::string> packageComponents;
955 getPackageAndVersionComponents(
956 &packageComponents, false /* cpp_compatible */);
957
Yifan Hongeefe4f22017-01-04 15:32:42 -0800958 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700959 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700960
961 enterLeaveNamespace(out, true /* enter */);
962 out << "\n";
963
964 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800965 << proxyName
966 << " : public ::android::hardware::BpInterface<"
967 << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000968 << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700969
970 out.indent();
971
Yifan Hongeefe4f22017-01-04 15:32:42 -0800972 out << "explicit "
973 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700974 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700975 << "\n\n";
976
Yifan Hong10fe0b52016-10-19 14:20:17 -0700977 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700978
Yifan Hong068c5522016-10-31 14:07:25 -0700979 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
980 method->generateCppSignature(out);
981 out << " override;\n";
982 return OK;
983 });
Steven Moreland9c387612016-09-07 09:54:26 -0700984
985 if (err != OK) {
986 return err;
987 }
Andreas Huber881227d2016-08-02 14:20:21 -0700988
989 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +0100990 out << "private:\n";
991 out.indent();
992 out << "std::mutex _hidl_mMutex;\n"
993 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
994 << " _hidl_mDeathRecipients;\n";
995 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700996 out << "};\n\n";
997
998 enterLeaveNamespace(out, false /* enter */);
999
1000 out << "\n#endif // " << guard << "\n";
1001
1002 return OK;
1003}
1004
Steven Moreland1cbf0362017-05-09 14:32:53 -07001005status_t AST::generateCppSources(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001006 std::string baseName = getBaseName();
1007 const Interface *iface = getInterface();
Andreas Huber881227d2016-08-02 14:20:21 -07001008
Andreas Huberb82318c2016-08-02 14:45:54 -07001009 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -07001010 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -07001011 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -07001012 path.append(baseName);
1013
1014 if (baseName != "types") {
1015 path.append("All");
1016 }
1017
1018 path.append(".cpp");
1019
Andreas Huberd2943e12016-08-05 11:59:31 -07001020 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -07001021 FILE *file = fopen(path.c_str(), "w");
1022
1023 if (file == NULL) {
1024 return -errno;
1025 }
1026
1027 Formatter out(file);
1028
Steven Moreland623c0042017-01-13 14:42:29 -08001029 out << "#define LOG_TAG \""
1030 << mPackage.string() << "::" << baseName
1031 << "\"\n\n";
1032
Steven Moreland05cd4232016-11-21 16:01:12 -08001033 out << "#include <android/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001034 out << "#include <cutils/trace.h>\n";
1035 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Steven Moreland19f11b52017-05-12 18:22:21 -07001036 if (iface) {
Steven Moreland19d5c172016-10-20 19:20:25 -07001037 // This is a no-op for IServiceManager itself.
1038 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
1039
Steven Morelandbec74ed2017-01-25 17:42:35 -08001040 // TODO(b/34274385) remove this
1041 out << "#include <hidl/LegacySupport.h>\n";
1042
Yifan Hongeefe4f22017-01-04 15:32:42 -08001043 generateCppPackageInclude(out, mPackage, iface->getProxyName());
1044 generateCppPackageInclude(out, mPackage, iface->getStubName());
1045 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001046
1047 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -07001048 generateCppPackageInclude(out,
1049 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -08001050 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001051 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -08001052
1053 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001054 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -07001055 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -08001056 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -07001057 }
1058
1059 out << "\n";
1060
1061 enterLeaveNamespace(out, true /* enter */);
1062 out << "\n";
1063
Steven Moreland19f11b52017-05-12 18:22:21 -07001064 status_t err = generateTypeSource(out, iface ? iface->localName() : "");
Andreas Huber881227d2016-08-02 14:20:21 -07001065
Steven Moreland19f11b52017-05-12 18:22:21 -07001066 if (err == OK && iface) {
Yifan Hong10fe0b52016-10-19 14:20:17 -07001067 const Interface *iface = mRootScope->getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -07001068
1069 // need to be put here, generateStubSource is using this.
Yifan Hongeefe4f22017-01-04 15:32:42 -08001070 out << "const char* "
1071 << iface->localName()
Yifan Hong10fe0b52016-10-19 14:20:17 -07001072 << "::descriptor(\""
1073 << iface->fqName().string()
1074 << "\");\n\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +01001075 out << "__attribute__((constructor))";
1076 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001077 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001078 out << "::android::hardware::details::gBnConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001079 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001080 << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001081 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001082 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001083 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001084 out << "return new "
1085 << iface->getStubName()
Yifan Hong341112d2017-04-20 18:12:05 -07001086 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001087 << iface->localName()
Yifan Hong158655a2016-11-08 12:34:07 -08001088 << " *>(iIntf));\n";
1089 });
Yifan Hongb04de382017-02-06 15:31:52 -08001090 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001091 });
Yifan Honga159f3b2017-03-16 14:53:51 -07001092 out << "::android::hardware::details::gBsConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001093 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001094 << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001095 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001096 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -08001097 << gIBaseFqName.cppName()
1098 << "> {\n";
1099 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001100 out << "return new "
1101 << iface->getPassthroughName()
Yifan Hong341112d2017-04-20 18:12:05 -07001102 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001103 << iface->localName()
Yifan Hong7a118f52016-12-07 11:21:15 -08001104 << " *>(iIntf));\n";
1105 });
Yifan Hongb04de382017-02-06 15:31:52 -08001106 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001107 });
Yifan Hong158655a2016-11-08 12:34:07 -08001108 });
Martijn Coenen8adcb652017-02-03 17:37:36 +01001109 out << "};\n\n";
1110 out << "__attribute__((destructor))";
1111 out << "static void static_destructor() {\n";
1112 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001113 out << "::android::hardware::details::gBnConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001114 << iface->localName()
1115 << "::descriptor);\n";
Yifan Honga159f3b2017-03-16 14:53:51 -07001116 out << "::android::hardware::details::gBsConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001117 << iface->localName()
1118 << "::descriptor);\n";
1119 });
1120 out << "};\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001121
Yifan Hongfe95aa22016-10-19 17:26:45 -07001122 err = generateInterfaceSource(out);
1123 }
1124
Steven Moreland19f11b52017-05-12 18:22:21 -07001125 if (err == OK && iface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001126 err = generateProxySource(out, iface->fqName());
Andreas Huber881227d2016-08-02 14:20:21 -07001127 }
1128
Steven Moreland19f11b52017-05-12 18:22:21 -07001129 if (err == OK && iface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001130 err = generateStubSource(out, iface);
Andreas Huber881227d2016-08-02 14:20:21 -07001131 }
1132
Steven Moreland19f11b52017-05-12 18:22:21 -07001133 if (err == OK && iface) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001134 err = generatePassthroughSource(out);
1135 }
1136
Steven Moreland19f11b52017-05-12 18:22:21 -07001137 if (err == OK && iface) {
Steven Moreland9c387612016-09-07 09:54:26 -07001138 const Interface *iface = mRootScope->getInterface();
1139
Yifan Hongc8934042016-11-17 17:10:52 -08001140 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001141 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -08001142 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001143 std::string package = iface->fqName().package()
1144 + iface->fqName().atVersion();
1145
Yifan Hongeefe4f22017-01-04 15:32:42 -08001146 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -08001147 }
Steven Moreland40786312016-08-16 10:29:40 -07001148 }
1149
Andreas Huber6755e9d2017-04-06 11:09:07 -07001150 HidlTypeAssertion::EmitAll(out);
1151 out << "\n";
1152
Andreas Huber881227d2016-08-02 14:20:21 -07001153 enterLeaveNamespace(out, false /* enter */);
1154
1155 return err;
1156}
1157
Steven Moreland67f67b42016-09-29 08:59:02 -07001158void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) {
Yifan Honga018ed52016-12-13 16:35:08 -08001159 out.sIf(nonNull + " == nullptr", [&] {
1160 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1161 out.indent(2, [&] {
1162 out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT);\n";
1163 });
1164 }).endl().endl();
Steven Moreland67f67b42016-09-29 08:59:02 -07001165}
1166
Andreas Huber881227d2016-08-02 14:20:21 -07001167status_t AST::generateTypeSource(
1168 Formatter &out, const std::string &ifaceName) const {
1169 return mRootScope->emitTypeDefinitions(out, ifaceName);
1170}
1171
Andreas Hubere7ff2282016-08-16 13:50:03 -07001172void AST::declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -07001173 Formatter &out,
1174 const std::vector<TypedVar *> &args,
1175 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -07001176 if (args.empty()) {
1177 return;
1178 }
1179
1180 for (const auto &arg : args) {
1181 const Type &type = arg->type();
1182
Yifan Hong3b320f82016-11-01 15:15:54 -07001183 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001184 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -07001185 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001186 << ";\n";
1187 }
1188
1189 out << "\n";
1190}
1191
Andreas Huber881227d2016-08-02 14:20:21 -07001192void AST::emitCppReaderWriter(
1193 Formatter &out,
1194 const std::string &parcelObj,
1195 bool parcelObjIsPointer,
1196 const TypedVar *arg,
1197 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -07001198 Type::ErrorMode mode,
1199 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -07001200 const Type &type = arg->type();
1201
Andreas Huber881227d2016-08-02 14:20:21 -07001202 type.emitReaderWriter(
1203 out,
Andreas Huber5e44a292016-09-27 14:52:39 -07001204 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -07001205 parcelObj,
1206 parcelObjIsPointer,
1207 isReader,
1208 mode);
1209}
1210
Yifan Hongbf459bc2016-08-23 16:50:37 -07001211void AST::emitCppResolveReferences(
1212 Formatter &out,
1213 const std::string &parcelObj,
1214 bool parcelObjIsPointer,
1215 const TypedVar *arg,
1216 bool isReader,
1217 Type::ErrorMode mode,
1218 bool addPrefixToName) const {
1219 const Type &type = arg->type();
1220 if(type.needsResolveReferences()) {
1221 type.emitResolveReferences(
1222 out,
1223 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
1224 isReader, // nameIsPointer
1225 parcelObj,
1226 parcelObjIsPointer,
1227 isReader,
1228 mode);
1229 }
1230}
1231
Yifan Hong068c5522016-10-31 14:07:25 -07001232status_t AST::generateProxyMethodSource(Formatter &out,
1233 const std::string &klassName,
1234 const Method *method,
1235 const Interface *superInterface) const {
1236
1237 method->generateCppSignature(out,
1238 klassName,
1239 true /* specify namespaces */);
1240
1241 const bool returnsValue = !method->results().empty();
1242 const TypedVar *elidedReturn = method->canElideCallback();
1243
Steven Moreland41c6d2e2016-11-07 12:26:54 -08001244 out << " {\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001245
1246 out.indent();
1247
Martijn Coenen115d4282016-12-19 05:14:04 +01001248 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
1249 method->cppImpl(IMPL_PROXY, out);
1250 out.unindent();
1251 out << "}\n\n";
1252 return OK;
1253 }
1254
Yifan Hong068c5522016-10-31 14:07:25 -07001255 if (returnsValue && elidedReturn == nullptr) {
1256 generateCheckNonNull(out, "_hidl_cb");
1257 }
1258
1259 status_t status = generateCppInstrumentationCall(
1260 out,
1261 InstrumentationEvent::CLIENT_API_ENTRY,
Yifan Hong068c5522016-10-31 14:07:25 -07001262 method);
1263 if (status != OK) {
1264 return status;
1265 }
1266
1267 out << "::android::hardware::Parcel _hidl_data;\n";
1268 out << "::android::hardware::Parcel _hidl_reply;\n";
1269 out << "::android::status_t _hidl_err;\n";
1270 out << "::android::hardware::Status _hidl_status;\n\n";
1271
1272 declareCppReaderLocals(
1273 out, method->results(), true /* forResults */);
1274
1275 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001276 out << superInterface->fqName().cppName();
Yifan Hong068c5522016-10-31 14:07:25 -07001277 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001278 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1279
Martijn Coenenfff73352017-01-04 16:36:31 +01001280 bool hasInterfaceArgument = false;
Yifan Hong068c5522016-10-31 14:07:25 -07001281 // First DFS: write all buffers and resolve pointers for parent
1282 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001283 if (arg->type().isInterface()) {
1284 hasInterfaceArgument = true;
1285 }
Yifan Hong068c5522016-10-31 14:07:25 -07001286 emitCppReaderWriter(
1287 out,
1288 "_hidl_data",
1289 false /* parcelObjIsPointer */,
1290 arg,
1291 false /* reader */,
1292 Type::ErrorMode_Goto,
1293 false /* addPrefixToName */);
1294 }
1295
1296 // Second DFS: resolve references.
1297 for (const auto &arg : method->args()) {
1298 emitCppResolveReferences(
1299 out,
1300 "_hidl_data",
1301 false /* parcelObjIsPointer */,
1302 arg,
1303 false /* reader */,
1304 Type::ErrorMode_Goto,
1305 false /* addPrefixToName */);
1306 }
1307
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001308 if (hasInterfaceArgument) {
1309 // Start binder threadpool to handle incoming transactions
1310 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1311 }
Yifan Hong068c5522016-10-31 14:07:25 -07001312 out << "_hidl_err = remote()->transact("
1313 << method->getSerialId()
1314 << " /* "
1315 << method->name()
1316 << " */, _hidl_data, &_hidl_reply";
1317
1318 if (method->isOneway()) {
1319 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
1320 }
1321 out << ");\n";
1322
1323 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1324
1325 if (!method->isOneway()) {
Yifan Hong859e53f2016-11-14 19:08:24 -08001326 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001327 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1328 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1329
1330
1331 // First DFS: write all buffers and resolve pointers for parent
1332 for (const auto &arg : method->results()) {
1333 emitCppReaderWriter(
1334 out,
1335 "_hidl_reply",
1336 false /* parcelObjIsPointer */,
1337 arg,
1338 true /* reader */,
1339 Type::ErrorMode_Goto,
1340 true /* addPrefixToName */);
1341 }
1342
1343 // Second DFS: resolve references.
1344 for (const auto &arg : method->results()) {
1345 emitCppResolveReferences(
1346 out,
1347 "_hidl_reply",
1348 false /* parcelObjIsPointer */,
1349 arg,
1350 true /* reader */,
1351 Type::ErrorMode_Goto,
1352 true /* addPrefixToName */);
1353 }
1354
1355 if (returnsValue && elidedReturn == nullptr) {
1356 out << "_hidl_cb(";
1357
Yifan Hong932464e2017-03-30 15:40:22 -07001358 out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) {
Yifan Hong068c5522016-10-31 14:07:25 -07001359 if (arg->type().resultNeedsDeref()) {
1360 out << "*";
1361 }
1362 out << "_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001363 });
Yifan Hong068c5522016-10-31 14:07:25 -07001364
1365 out << ");\n\n";
1366 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001367 }
1368 status = generateCppInstrumentationCall(
1369 out,
1370 InstrumentationEvent::CLIENT_API_EXIT,
1371 method);
1372 if (status != OK) {
1373 return status;
Yifan Hong068c5522016-10-31 14:07:25 -07001374 }
1375
1376 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001377 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1378 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001379 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001380 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1381 } else {
1382 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1383 out << "return ::android::hardware::Return<void>();\n\n";
1384 }
1385
1386 out.unindent();
1387 out << "_hidl_error:\n";
1388 out.indent();
1389 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1390 out << "return ::android::hardware::Return<";
1391 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001392 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001393 } else {
1394 out << "void";
1395 }
1396 out << ">(_hidl_status);\n";
1397
1398 out.unindent();
1399 out << "}\n\n";
1400 return OK;
1401}
1402
Andreas Huber881227d2016-08-02 14:20:21 -07001403status_t AST::generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -08001404 Formatter &out, const FQName &fqName) const {
1405 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001406
1407 out << klassName
1408 << "::"
1409 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001410 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001411
1412 out.indent();
1413 out.indent();
1414
1415 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001416 << "<"
1417 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001418 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001419 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001420 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001421 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001422 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001423 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001424
Andreas Huber881227d2016-08-02 14:20:21 -07001425 out.unindent();
1426 out.unindent();
1427 out << "}\n\n";
1428
Yifan Hong068c5522016-10-31 14:07:25 -07001429 status_t err = generateMethods(out, [&](const Method *method, const Interface *superInterface) {
1430 return generateProxyMethodSource(out, klassName, method, superInterface);
1431 });
Andreas Huber881227d2016-08-02 14:20:21 -07001432
Yifan Hong068c5522016-10-31 14:07:25 -07001433 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001434}
1435
1436status_t AST::generateStubSource(
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001437 Formatter &out,
Yifan Hongeefe4f22017-01-04 15:32:42 -08001438 const Interface *iface) const {
1439 const std::string interfaceName = iface->localName();
1440 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001441
Steven Moreland40786312016-08-16 10:29:40 -07001442 out << klassName
1443 << "::"
1444 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001445 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001446
1447 out.indent();
1448 out.indent();
1449
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001450 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001451 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001452 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001453 out << ": "
1454 << gIBaseFqName.getInterfaceStubFqName().cppName()
1455 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001456 }
1457
1458 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001459 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001460 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001461 << "\") { \n";
1462 out.indent();
1463 out << "_hidl_mImpl = _hidl_impl;\n";
1464 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001465
1466 out.unindent();
1467 out.unindent();
1468 out << "}\n\n";
1469
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001470 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001471 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001472 // class properly.
1473 out << klassName
1474 << "::"
1475 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001476 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1477 << " const std::string &HidlInstrumentor_package,"
1478 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001479
1480 out.indent();
1481 out.indent();
1482
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001483 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001484 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001485 out.indent();
1486 out << "_hidl_mImpl = _hidl_impl;\n";
1487 out.unindent();
1488
1489 out.unindent();
1490 out.unindent();
1491 out << "}\n\n";
1492 }
1493
Yifan Hongbcffce22017-02-01 15:52:06 -08001494 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1495 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
1496 return OK;
1497 }
1498 method->generateCppSignature(out, iface->getStubName());
1499 out << " ";
1500 out.block([&] {
1501 method->cppImpl(IMPL_STUB_IMPL, out);
1502 }).endl();
1503 return OK;
1504 });
Steven Moreland60818632017-02-04 00:33:42 -08001505 if (err != OK) {
1506 return err;
1507 }
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001508
Andreas Huber881227d2016-08-02 14:20:21 -07001509 out << "::android::status_t " << klassName << "::onTransact(\n";
1510
1511 out.indent();
1512 out.indent();
1513
Iliyan Malchev549e2592016-08-10 08:59:12 -07001514 out << "uint32_t _hidl_code,\n"
1515 << "const ::android::hardware::Parcel &_hidl_data,\n"
1516 << "::android::hardware::Parcel *_hidl_reply,\n"
1517 << "uint32_t _hidl_flags,\n"
1518 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001519
1520 out.unindent();
1521
Iliyan Malchev549e2592016-08-10 08:59:12 -07001522 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001523 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001524 out.indent();
1525
Yifan Hong10fe0b52016-10-19 14:20:17 -07001526 for (const auto &tuple : iface->allMethodsFromRoot()) {
1527 const Method *method = tuple.method();
1528 const Interface *superInterface = tuple.interface();
1529 out << "case "
1530 << method->getSerialId()
1531 << " /* "
1532 << method->name()
1533 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001534
Yifan Hong10fe0b52016-10-19 14:20:17 -07001535 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001536
Yifan Hong10fe0b52016-10-19 14:20:17 -07001537 status_t err =
1538 generateStubSourceForMethod(out, superInterface, method);
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001539
Yifan Hong10fe0b52016-10-19 14:20:17 -07001540 if (err != OK) {
1541 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001542 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001543
1544 out.unindent();
1545 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001546 }
1547
1548 out << "default:\n{\n";
1549 out.indent();
1550
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001551 out << "return onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001552
1553 out.indent();
1554 out.indent();
1555
Iliyan Malchev549e2592016-08-10 08:59:12 -07001556 out << "_hidl_code, _hidl_data, _hidl_reply, "
1557 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001558
1559 out.unindent();
1560 out.unindent();
1561
1562 out.unindent();
1563 out << "}\n";
1564
1565 out.unindent();
1566 out << "}\n\n";
1567
Yifan Honga018ed52016-12-13 16:35:08 -08001568 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1569 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1570 out.indent(2, [&] {
1571 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1572 out << "_hidl_reply);\n";
1573 });
1574 });
Andreas Huber881227d2016-08-02 14:20:21 -07001575
Iliyan Malchev549e2592016-08-10 08:59:12 -07001576 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001577
1578 out.unindent();
1579 out << "}\n\n";
1580
1581 return OK;
1582}
1583
1584status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001585 Formatter &out, const Interface *iface, const Method *method) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001586 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1587 method->cppImpl(IMPL_STUB, out);
1588 out << "break;\n";
1589 return OK;
1590 }
1591
Yifan Hongeefe4f22017-01-04 15:32:42 -08001592 out << "if (!_hidl_data.enforceInterface("
1593 << iface->fullName()
1594 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001595
Andreas Huber881227d2016-08-02 14:20:21 -07001596 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001597 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001598 out << "break;\n";
1599 out.unindent();
1600 out << "}\n\n";
1601
Andreas Huber5e44a292016-09-27 14:52:39 -07001602 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001603
Yifan Hongbf459bc2016-08-23 16:50:37 -07001604 // First DFS: write buffers
Andreas Huber881227d2016-08-02 14:20:21 -07001605 for (const auto &arg : method->args()) {
1606 emitCppReaderWriter(
1607 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001608 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001609 false /* parcelObjIsPointer */,
1610 arg,
1611 true /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001612 Type::ErrorMode_Break,
1613 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001614 }
1615
Yifan Hongbf459bc2016-08-23 16:50:37 -07001616 // Second DFS: resolve references
1617 for (const auto &arg : method->args()) {
1618 emitCppResolveReferences(
1619 out,
1620 "_hidl_data",
1621 false /* parcelObjIsPointer */,
1622 arg,
1623 true /* reader */,
1624 Type::ErrorMode_Break,
1625 false /* addPrefixToName */);
1626 }
1627
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001628 status_t status = generateCppInstrumentationCall(
1629 out,
1630 InstrumentationEvent::SERVER_API_ENTRY,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001631 method);
1632 if (status != OK) {
1633 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001634 }
1635
Andreas Huber881227d2016-08-02 14:20:21 -07001636 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001637 const TypedVar *elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -08001638 const std::string callee =
1639 (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL))
1640 ? "this" : "_hidl_mImpl";
Andreas Huber881227d2016-08-02 14:20:21 -07001641
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001642 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001643 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001644 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001645 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001646 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001647 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001648 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001649
Yifan Hong932464e2017-03-30 15:40:22 -07001650 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001651 if (arg->type().resultNeedsDeref()) {
1652 out << "*";
1653 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001654 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001655 });
Andreas Huber881227d2016-08-02 14:20:21 -07001656
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001657 out << ");\n\n";
Yifan Hong859e53f2016-11-14 19:08:24 -08001658 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1659 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001660
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001661 elidedReturn->type().emitReaderWriter(
1662 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001663 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001664 "_hidl_reply",
1665 true, /* parcelObjIsPointer */
1666 false, /* isReader */
1667 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001668
Yifan Hongbf459bc2016-08-23 16:50:37 -07001669 emitCppResolveReferences(
1670 out,
1671 "_hidl_reply",
1672 true /* parcelObjIsPointer */,
1673 elidedReturn,
1674 false /* reader */,
1675 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001676 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001677
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001678 status_t status = generateCppInstrumentationCall(
1679 out,
1680 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001681 method);
1682 if (status != OK) {
1683 return status;
1684 }
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001685
Iliyan Malchev549e2592016-08-10 08:59:12 -07001686 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001687 } else {
1688 if (returnsValue) {
1689 out << "bool _hidl_callbackCalled = false;\n\n";
1690 }
Andreas Huber881227d2016-08-02 14:20:21 -07001691
Yifan Hongcd2ae452017-01-31 14:33:40 -08001692 out << callee << "->" << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001693
Yifan Hong932464e2017-03-30 15:40:22 -07001694 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001695 if (arg->type().resultNeedsDeref()) {
1696 out << "*";
1697 }
1698
1699 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001700 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001701
1702 if (returnsValue) {
Yifan Hong932464e2017-03-30 15:40:22 -07001703 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001704 out << ", ";
1705 }
1706
1707 out << "[&](";
1708
Yifan Hong932464e2017-03-30 15:40:22 -07001709 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001710 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001711 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001712
1713 out << ") {\n";
1714 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001715 out << "if (_hidl_callbackCalled) {\n";
1716 out.indent();
1717 out << "LOG_ALWAYS_FATAL(\""
1718 << method->name()
1719 << ": _hidl_cb called a second time, but must be called once.\");\n";
1720 out.unindent();
1721 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001722 out << "_hidl_callbackCalled = true;\n\n";
1723
Yifan Hong859e53f2016-11-14 19:08:24 -08001724 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1725 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001726
Yifan Hongbf459bc2016-08-23 16:50:37 -07001727 // First DFS: buffers
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001728 for (const auto &arg : method->results()) {
1729 emitCppReaderWriter(
1730 out,
1731 "_hidl_reply",
1732 true /* parcelObjIsPointer */,
1733 arg,
1734 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001735 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001736 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001737 }
1738
Yifan Hongbf459bc2016-08-23 16:50:37 -07001739 // Second DFS: resolve references
1740 for (const auto &arg : method->results()) {
1741 emitCppResolveReferences(
1742 out,
1743 "_hidl_reply",
1744 true /* parcelObjIsPointer */,
1745 arg,
1746 false /* reader */,
1747 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001748 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001749 }
1750
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001751 status_t status = generateCppInstrumentationCall(
1752 out,
1753 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001754 method);
1755 if (status != OK) {
1756 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001757 }
1758
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001759 out << "_hidl_cb(*_hidl_reply);\n";
1760
1761 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001762 out << "});\n\n";
1763 } else {
1764 out << ");\n\n";
1765 status_t status = generateCppInstrumentationCall(
1766 out,
1767 InstrumentationEvent::SERVER_API_EXIT,
1768 method);
1769 if (status != OK) {
1770 return status;
1771 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001772 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001773
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001774 if (returnsValue) {
1775 out << "if (!_hidl_callbackCalled) {\n";
1776 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001777 out << "LOG_ALWAYS_FATAL(\""
1778 << method->name()
1779 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001780 out.unindent();
1781 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001782 } else {
1783 out << "::android::hardware::writeToParcel("
1784 << "::android::hardware::Status::ok(), "
1785 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001786 }
Andreas Huber881227d2016-08-02 14:20:21 -07001787 }
1788
1789 out << "break;\n";
1790
1791 return OK;
1792}
1793
Steven Moreland69e7c702016-09-09 11:16:32 -07001794status_t AST::generatePassthroughHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001795 if (!AST::isInterface()) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001796 // types.hal does not get a stub header.
1797 return OK;
1798 }
1799
1800 const Interface *iface = mRootScope->getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -07001801 CHECK(iface != nullptr);
Steven Moreland69e7c702016-09-09 11:16:32 -07001802
Yifan Hongeefe4f22017-01-04 15:32:42 -08001803 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001804
1805 bool supportOneway = iface->hasOnewayMethods();
1806
1807 std::string path = outputPath;
1808 path.append(mCoordinator->convertPackageRootToPath(mPackage));
1809 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
1810 path.append(klassName);
1811 path.append(".h");
1812
1813 CHECK(Coordinator::MakeParentHierarchy(path));
1814 FILE *file = fopen(path.c_str(), "w");
1815
1816 if (file == NULL) {
1817 return -errno;
1818 }
1819
1820 Formatter out(file);
1821
1822 const std::string guard = makeHeaderGuard(klassName);
1823
1824 out << "#ifndef " << guard << "\n";
1825 out << "#define " << guard << "\n\n";
1826
1827 std::vector<std::string> packageComponents;
1828 getPackageAndVersionComponents(
1829 &packageComponents, false /* cpp_compatible */);
1830
Steven Moreland61d3f4b2017-04-28 17:30:38 -07001831 out << "#include <android-base/macros.h>\n";
Yifan Hongb0949432016-12-15 15:32:24 -08001832 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001833 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001834
Steven Moreland19f11b52017-05-12 18:22:21 -07001835 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Morelandee88eed2016-10-31 17:49:00 -07001836 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001837
Yifan Hong7a118f52016-12-07 11:21:15 -08001838 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001839 if (supportOneway) {
Yifan Hong2cbc1472016-10-25 19:02:40 -07001840 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001841 }
1842
1843 enterLeaveNamespace(out, true /* enter */);
1844 out << "\n";
1845
1846 out << "struct "
1847 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -07001848 << " : " << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001849 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001850
1851 out.indent();
1852 out << "explicit "
1853 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001854 << "(const ::android::sp<"
Steven Moreland19f11b52017-05-12 18:22:21 -07001855 << iface->localName()
Steven Moreland69e7c702016-09-09 11:16:32 -07001856 << "> impl);\n";
1857
Yifan Hong068c5522016-10-31 14:07:25 -07001858 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1859 return generatePassthroughMethod(out, method);
1860 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001861
1862 if (err != OK) {
1863 return err;
1864 }
1865
1866 out.unindent();
1867 out << "private:\n";
1868 out.indent();
Steven Moreland19f11b52017-05-12 18:22:21 -07001869 out << "const ::android::sp<" << iface->localName() << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001870
1871 if (supportOneway) {
Yifan Hongef91d362017-03-20 17:18:13 -07001872 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001873
1874 out << "\n";
1875
1876 out << "::android::hardware::Return<void> addOnewayTask("
1877 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001878 }
1879
1880 out.unindent();
1881
1882 out << "};\n\n";
1883
1884 enterLeaveNamespace(out, false /* enter */);
1885
1886 out << "\n#endif // " << guard << "\n";
1887
1888 return OK;
1889}
1890
Yifan Hongfe95aa22016-10-19 17:26:45 -07001891status_t AST::generateInterfaceSource(Formatter &out) const {
1892 const Interface *iface = mRootScope->getInterface();
1893
Yifan Hong2d7126b2016-10-20 15:12:57 -07001894 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001895 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001896
Steven Morelandd4b068a2017-03-20 06:30:51 -07001897 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1898 bool reserved = method->isHidlReserved();
1899
1900 if (!reserved) {
1901 out << "// no default implementation for: ";
1902 }
1903 method->generateCppSignature(out, iface->localName());
1904 if (reserved) {
1905 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001906 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001907 }).endl();
1908 }
1909
1910 out << "\n";
1911
1912 return OK;
1913 });
1914 if (err != OK) {
1915 return err;
1916 }
1917
Yifan Hong3d746092016-12-07 14:26:33 -08001918 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -07001919 out << "// static \n::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -08001920 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -07001921 << "> "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001922 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001923 << "::castFrom("
1924 << superType->getCppArgumentType()
Yifan Hong200209c2017-03-29 03:39:09 -07001925 << " parent, bool "
1926 << (iface == superType ? "/* emitError */" : "emitError")
1927 << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001928 out.indent();
1929 if (iface == superType) {
1930 out << "return parent;\n";
1931 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001932 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001933 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001934 << superType->fqName().cppName() << ", "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001935 << iface->getProxyName() << ", "
Yifan Hong51a65092017-01-04 15:41:44 -08001936 << superType->getProxyFqName().cppName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001937 << ">(\n";
1938 out.indent();
1939 out.indent();
1940 out << "parent, \""
1941 << iface->fqName().string()
Yifan Hong200209c2017-03-29 03:39:09 -07001942 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001943 out.unindent();
1944 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001945 }
Yifan Hong3d746092016-12-07 14:26:33 -08001946 out.unindent();
1947 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001948 }
1949
1950 return OK;
1951}
1952
Steven Moreland69e7c702016-09-09 11:16:32 -07001953status_t AST::generatePassthroughSource(Formatter &out) const {
1954 const Interface *iface = mRootScope->getInterface();
1955
Yifan Hongeefe4f22017-01-04 15:32:42 -08001956 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001957
1958 out << klassName
1959 << "::"
1960 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001961 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001962 << iface->fullName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001963 << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001964 << mPackage.string()
1965 << "\", \""
1966 << iface->localName()
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001967 << "\"), mImpl(impl) {";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001968 if (iface->hasOnewayMethods()) {
1969 out << "\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001970 out.indent([&] {
Yifan Hongf01dad42017-03-20 19:03:11 -07001971 out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001972 });
1973 }
1974 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001975
1976 if (iface->hasOnewayMethods()) {
1977 out << "::android::hardware::Return<void> "
1978 << klassName
1979 << "::addOnewayTask(std::function<void(void)> fun) {\n";
1980 out.indent();
Yifan Hong2cbc1472016-10-25 19:02:40 -07001981 out << "if (!mOnewayQueue.push(fun)) {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001982 out.indent();
Steven Moreland67f67b42016-09-29 08:59:02 -07001983 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1984 out.indent();
1985 out.indent();
1986 out << "::android::hardware::Status::EX_TRANSACTION_FAILED);\n";
1987 out.unindent();
1988 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07001989 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07001990 out << "}\n";
1991
Steven Morelandd366c262016-10-11 15:29:10 -07001992 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001993
1994 out.unindent();
1995 out << "}\n\n";
1996
1997
1998 }
1999
2000 return OK;
2001}
2002
Martijn Coenen7b295242016-11-04 16:52:56 +01002003status_t AST::generateCppAtraceCall(Formatter &out,
2004 InstrumentationEvent event,
2005 const Method *method) const {
2006 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08002007 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01002008 switch (event) {
2009 case SERVER_API_ENTRY:
2010 {
2011 out << "atrace_begin(ATRACE_TAG_HAL, \""
2012 << baseString + "::server\");\n";
2013 break;
2014 }
2015 case CLIENT_API_ENTRY:
2016 {
2017 out << "atrace_begin(ATRACE_TAG_HAL, \""
2018 << baseString + "::client\");\n";
2019 break;
2020 }
2021 case PASSTHROUGH_ENTRY:
2022 {
2023 out << "atrace_begin(ATRACE_TAG_HAL, \""
2024 << baseString + "::passthrough\");\n";
2025 break;
2026 }
2027 case SERVER_API_EXIT:
2028 case CLIENT_API_EXIT:
2029 case PASSTHROUGH_EXIT:
2030 {
2031 out << "atrace_end(ATRACE_TAG_HAL);\n";
2032 break;
2033 }
2034 default:
2035 {
2036 LOG(ERROR) << "Unsupported instrumentation event: " << event;
2037 return UNKNOWN_ERROR;
2038 }
2039 }
2040
2041 return OK;
2042}
2043
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002044status_t AST::generateCppInstrumentationCall(
2045 Formatter &out,
2046 InstrumentationEvent event,
Steven Moreland031ccf12016-10-31 15:54:38 -07002047 const Method *method) const {
Martijn Coenen7b295242016-11-04 16:52:56 +01002048 status_t err = generateCppAtraceCall(out, event, method);
2049 if (err != OK) {
2050 return err;
2051 }
2052
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002053 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
2054 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002055 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002056 std::string event_str = "";
2057 switch (event) {
2058 case SERVER_API_ENTRY:
2059 {
2060 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
2061 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002062 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002063 << (arg->type().resultNeedsDeref() ? "" : "&")
2064 << arg->name()
2065 << ");\n";
2066 }
2067 break;
2068 }
2069 case SERVER_API_EXIT:
2070 {
2071 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07002072 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002073 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07002074 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002075 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002076 }
2077 break;
2078 }
2079 case CLIENT_API_ENTRY:
2080 {
2081 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
2082 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002083 out << "_hidl_args.push_back((void *)&"
2084 << arg->name()
2085 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002086 }
2087 break;
2088 }
2089 case CLIENT_API_EXIT:
2090 {
2091 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
2092 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002093 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002094 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002095 << "_hidl_out_"
2096 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002097 << ");\n";
2098 }
2099 break;
2100 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002101 case PASSTHROUGH_ENTRY:
2102 {
2103 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
2104 for (const auto &arg : method->args()) {
2105 out << "_hidl_args.push_back((void *)&"
2106 << arg->name()
2107 << ");\n";
2108 }
2109 break;
2110 }
2111 case PASSTHROUGH_EXIT:
2112 {
2113 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002114 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002115 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002116 << arg->name()
2117 << ");\n";
2118 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002119 break;
2120 }
Steven Moreland031ccf12016-10-31 15:54:38 -07002121 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002122 {
Steven Moreland031ccf12016-10-31 15:54:38 -07002123 LOG(ERROR) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002124 return UNKNOWN_ERROR;
2125 }
2126 }
2127
Steven Moreland031ccf12016-10-31 15:54:38 -07002128 const Interface *iface = mRootScope->getInterface();
2129
Steven Moreland1ab31442016-11-03 18:37:51 -07002130 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002131 out.indent();
2132 out << "callback("
2133 << event_str
2134 << ", \""
2135 << mPackage.package()
2136 << "\", \""
Yifan Hong90ea87f2016-11-01 14:25:47 -07002137 << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002138 << "\", \""
2139 << iface->localName()
2140 << "\", \""
2141 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002142 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002143 out.unindent();
2144 out << "}\n";
2145 out.unindent();
2146 out << "}\n\n";
2147
2148 return OK;
2149}
2150
Andreas Huber881227d2016-08-02 14:20:21 -07002151} // namespace android