blob: fa2bf253288f4bc3f024cad7795ef79f8c0f541e [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Huber881227d2016-08-02 14:20:21 -070017#include "AST.h"
18
19#include "Coordinator.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070020#include "EnumType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070021#include "Interface.h"
Andreas Huber6755e9d2017-04-06 11:09:07 -070022#include "HidlTypeAssertion.h"
Andreas Huber881227d2016-08-02 14:20:21 -070023#include "Method.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070024#include "ScalarType.h"
Andreas Huber881227d2016-08-02 14:20:21 -070025#include "Scope.h"
26
Andreas Huberdca261f2016-08-04 13:47:51 -070027#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland5708edf2016-11-04 15:33:31 +000029#include <hidl-util/StringHelper.h>
Andreas Huber881227d2016-08-02 14:20:21 -070030#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070031#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070032#include <vector>
33
34namespace android {
35
Andreas Huberb82318c2016-08-02 14:45:54 -070036status_t AST::generateCpp(const std::string &outputPath) const {
Steven Moreland1cbf0362017-05-09 14:32:53 -070037 status_t err = generateCppHeaders(outputPath);
38
39 if (err == OK) {
40 err = generateCppSources(outputPath);
41 }
42
43 return err;
44}
45
46status_t AST::generateCppHeaders(const std::string &outputPath) const {
Andreas Huberb82318c2016-08-02 14:45:54 -070047 status_t err = generateInterfaceHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070048
49 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070050 err = generateStubHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070051 }
52
53 if (err == OK) {
Steven Moreland40786312016-08-16 10:29:40 -070054 err = generateHwBinderHeader(outputPath);
55 }
56
57 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070058 err = generateProxyHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070059 }
60
61 if (err == OK) {
Yifan Hong7a118f52016-12-07 11:21:15 -080062 err = generatePassthroughHeader(outputPath);
Steven Moreland69e7c702016-09-09 11:16:32 -070063 }
64
Andreas Huber881227d2016-08-02 14:20:21 -070065 return err;
66}
67
Andreas Huber737080b2016-08-02 15:38:04 -070068void AST::getPackageComponents(
69 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070070 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070071}
72
73void AST::getPackageAndVersionComponents(
74 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070075 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070076}
77
Steven Moreland5708edf2016-11-04 15:33:31 +000078std::string AST::makeHeaderGuard(const std::string &baseName,
79 bool indicateGenerated) const {
80 std::string guard;
Andreas Huber881227d2016-08-02 14:20:21 -070081
Steven Moreland5708edf2016-11-04 15:33:31 +000082 if (indicateGenerated) {
83 guard += "HIDL_GENERATED_";
84 }
85
86 guard += StringHelper::Uppercase(mPackage.tokenName());
Andreas Huber881227d2016-08-02 14:20:21 -070087 guard += "_";
Steven Moreland5708edf2016-11-04 15:33:31 +000088 guard += StringHelper::Uppercase(baseName);
89 guard += "_H";
Andreas Huber881227d2016-08-02 14:20:21 -070090
91 return guard;
92}
93
Steven Morelandee88eed2016-10-31 17:49:00 -070094void AST::generateCppPackageInclude(
95 Formatter &out,
96 const FQName &package,
97 const std::string &klass) {
98
99 out << "#include <";
100
101 std::vector<std::string> components;
102 package.getPackageAndVersionComponents(&components, false /* cpp_compatible */);
103
104 for (const auto &component : components) {
105 out << component << "/";
106 }
107
108 out << klass
109 << ".h>\n";
110}
111
Andreas Huber881227d2016-08-02 14:20:21 -0700112void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
113 std::vector<std::string> packageComponents;
114 getPackageAndVersionComponents(
115 &packageComponents, true /* cpp_compatible */);
116
117 if (enter) {
118 for (const auto &component : packageComponents) {
119 out << "namespace " << component << " {\n";
120 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700121
Andreas Huber2831d512016-08-15 09:33:47 -0700122 out.setNamespace(mPackage.cppNamespace() + "::");
Andreas Huber881227d2016-08-02 14:20:21 -0700123 } else {
Andreas Huber0e00de42016-08-03 09:56:02 -0700124 out.setNamespace(std::string());
125
Andreas Huber881227d2016-08-02 14:20:21 -0700126 for (auto it = packageComponents.rbegin();
127 it != packageComponents.rend();
128 ++it) {
129 out << "} // namespace " << *it << "\n";
130 }
131 }
132}
133
Steven Moreland038903b2017-03-30 12:11:24 -0700134static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) {
135 const std::string functionName = isTry ? "tryGetService" : "getService";
136
137 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800138 << "const std::string &serviceName=\"default\", bool getStub=false);\n";
Steven Moreland038903b2017-03-30 12:11:24 -0700139 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800140 << "const char serviceName[], bool getStub=false)"
141 << " { std::string str(serviceName ? serviceName : \"\");"
Steven Moreland038903b2017-03-30 12:11:24 -0700142 << " return " << functionName << "(str, getStub); }\n";
143 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
Chris Phoenixdb0d6342017-01-11 16:10:00 -0800144 << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)"
145 // without c_str the std::string constructor is ambiguous
146 << " { std::string str(serviceName.c_str());"
Steven Moreland038903b2017-03-30 12:11:24 -0700147 << " return " << functionName << "(str, getStub); }\n";
148 out << "static ::android::sp<" << interfaceName << "> " << functionName << "("
149 << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n";
150}
151
152static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) {
153 declareGetService(out, interfaceName, true /* isTry */);
154 declareGetService(out, interfaceName, false /* isTry */);
155
Steven Moreland90831502017-03-27 12:08:40 -0700156 out << "__attribute__ ((warn_unused_result))"
157 << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800158 out << "static bool registerForNotifications(\n";
159 out.indent(2, [&] {
160 out << "const std::string &serviceName,\n"
161 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
162 << "&notification);\n";
163 });
164
165}
166
Steven Moreland038903b2017-03-30 12:11:24 -0700167static void implementGetService(Formatter &out,
168 const FQName &fqName,
169 bool isTry) {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800170
171 const std::string interfaceName = fqName.getInterfaceName();
Steven Moreland038903b2017-03-30 12:11:24 -0700172 const std::string functionName = isTry ? "tryGetService" : "getService";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800173
174 out << "// static\n"
Steven Moreland038903b2017-03-30 12:11:24 -0700175 << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "("
Yifan Hong31f07ff2017-03-21 18:56:35 +0000176 << "const std::string &serviceName, const bool getStub) ";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800177 out.block([&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700178 out << "using ::android::hardware::defaultServiceManager;\n";
179 out << "using ::android::hardware::details::waitForHwService;\n";
180 out << "using ::android::hardware::getPassthroughServiceManager;\n";
181 out << "using ::android::hardware::Return;\n";
182 out << "using ::android::sp;\n";
183 out << "using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;\n\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000184
Steven Morelandbcf51802017-04-06 09:17:44 -0700185 out << "sp<" << interfaceName << "> iface = nullptr;\n";
186
187 out.endl();
188
189 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> sm"
190 << " = defaultServiceManager();\n";
191
192 out.sIf("sm == nullptr", [&] {
193 // hwbinder is not available on this device, so future tries
194 // would also be null. I can only return nullptr.
195 out << "ALOGE(\"getService: defaultServiceManager() is null\");\n"
196 << "return nullptr;\n";
197 }).endl().endl();
198
199 out << "Return<Transport> transportRet = sm->getTransport("
200 << interfaceName << "::descriptor, serviceName);\n\n";
201
202 out.sIf("!transportRet.isOk()", [&] {
203 out << "ALOGE(\"getService: defaultServiceManager()->getTransport returns %s\", "
204 << "transportRet.description().c_str());\n";
205 out << "return nullptr;\n";
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700206 }).endl();
Steven Morelandbcf51802017-04-06 09:17:44 -0700207
208 out << "Transport transport = transportRet;\n";
209 out << "const bool vintfHwbinder = (transport == Transport::HWBINDER);\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700210 << "const bool vintfPassthru = (transport == Transport::PASSTHROUGH);\n\n";
211
212 // This means that you must set TREBLE_TESTING_OVERRIDE when running a test such
213 // as hidl_test. Ideally these binaries set this value themselves. This allows
214 // test modules to dynamically add and unset services even though they are not
215 // declared in the device manifest. This prevents a problem where framework
216 // changes are accidentally made in a way that is not backwards compatible. For
217 // instance, consider the following situation for two devices developed in the
218 // same tree:
219 // A: serves @1.1::IFoo, declares @1.0::IFoo (incorrect)
220 // B: serves @1.0::IFoo, declares @1.0::IFoo (correct configuration)
221 // If development is done on device A, then framework code like: "V1_1::IFoo::
222 // getService()->doV1_0Api()" will work. However, this will unintentionally break
223 // the feature for devices like device B for which "V1_1::IFoo::getService()
224 // will return nullptr. In order to prevent problems like this, we only allow
225 // fetching an interface if it is declared in a VINTF manifest.
226 out << "#ifdef __ANDROID_TREBLE__\n\n"
227 << "#ifdef __ANDROID_DEBUGGABLE__\n"
228 << "const char* env = std::getenv(\"TREBLE_TESTING_OVERRIDE\");\n"
Steven Morelandd281ebc2017-06-20 16:27:35 -0700229 << "const bool trebleTestingOverride = env && !strcmp(env, \"true\");\n"
230 << "const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700231 << "#else // __ANDROID_TREBLE__ but not __ANDROID_DEBUGGABLE__\n"
Steven Morelandd281ebc2017-06-20 16:27:35 -0700232 << "const bool trebleTestingOverride = false;\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700233 << "const bool vintfLegacy = false;\n"
234 << "#endif // __ANDROID_DEBUGGABLE__\n\n"
235 << "#else // not __ANDROID_TREBLE__\n"
Steven Moreland82472102017-06-26 09:40:37 -0700236 << "const char* env = std::getenv(\"TREBLE_TESTING_OVERRIDE\");\n"
237 << "const bool trebleTestingOverride = env && !strcmp(env, \"true\");\n"
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700238 << "const bool vintfLegacy = (transport == Transport::EMPTY);\n\n"
239 << "#endif // __ANDROID_TREBLE__\n\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000240
241 // if (getStub) {
242 // getPassthroughServiceManager()->get only once.
243 // } else {
244 // if (vintfHwbinder) {
245 // while (no alive service) {
Steven Moreland369d0532017-06-09 12:42:10 -0700246 // if (have already tried to get service)
247 // waitForHwService
Yifan Hong31f07ff2017-03-21 18:56:35 +0000248 // defaultServiceManager()->get
249 // }
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700250 // } else if (vintfLegacy) {
Yifan Hong31f07ff2017-03-21 18:56:35 +0000251 // defaultServiceManager()->get only once.
252 // getPassthroughServiceManager()->get only once.
253 // } else if (vintfPassthru) {
254 // getPassthroughServiceManager()->get only once.
255 // }
256 // }
257
Steven Moreland369d0532017-06-09 12:42:10 -0700258 out.sFor("int tries = 0; !getStub && (vintfHwbinder || (vintfLegacy && tries == 0)); tries++", [&] {
Steven Moreland038903b2017-03-30 12:11:24 -0700259 if (!isTry) {
Steven Moreland369d0532017-06-09 12:42:10 -0700260 out.sIf("tries > 1", [&] {
261 // sleep only after the first time we've called waitForHwService.
262 out << "ALOGI(\"" << functionName << ": Will do try %d for %s/%s in 1s...\", tries, "
263 << interfaceName << "::descriptor, serviceName.c_str());\n"
264 << "sleep(1);\n";
265 }).endl();
266
267 out.sIf("vintfHwbinder && tries > 0", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700268 out << "waitForHwService("
269 << interfaceName << "::descriptor, serviceName);\n";
Steven Moreland038903b2017-03-30 12:11:24 -0700270 }).endl();
271 }
Yifan Hong31f07ff2017-03-21 18:56:35 +0000272
Steven Morelandbcf51802017-04-06 09:17:44 -0700273 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000274 out.indent(2, [&] {
275 out << "sm->get(" << interfaceName << "::descriptor, serviceName);\n";
276 });
277
278 out.sIf("!ret.isOk()", [&] {
Steven Moreland42394ce2017-03-27 17:03:04 -0700279 // hwservicemanager fails, may be security issue
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700280 out << "ALOGE(\"" << interfaceName << ": defaultServiceManager()->get returns %s\", "
Yifan Hong31f07ff2017-03-21 18:56:35 +0000281 << "ret.description().c_str());\n"
Steven Moreland42394ce2017-03-27 17:03:04 -0700282 << "break;\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000283 }).endl();
284
Steven Morelandbcf51802017-04-06 09:17:44 -0700285 out << "sp<" << gIBaseFqName.cppName() << "> base = ret;\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700286 out.sIf("base == nullptr", [&] {
Steven Moreland369d0532017-06-09 12:42:10 -0700287 // if tries > 0: race condition. hwservicemanager drops the service
Yifan Hong200209c2017-03-29 03:39:09 -0700288 // from waitForHwService to here
Steven Moreland369d0532017-06-09 12:42:10 -0700289 out.sIf("tries > 0", [&] {
290 out << "ALOGW(\"" << interfaceName << ": found null hwbinder interface\");\n";
291 });
292 out << (isTry ? "break" : "continue")
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700293 << ";\n";
Yifan Hong31f07ff2017-03-21 18:56:35 +0000294 }).endl();
Steven Morelandbcf51802017-04-06 09:17:44 -0700295 out << "Return<sp<" << interfaceName
Yifan Hong200209c2017-03-29 03:39:09 -0700296 << ">> castRet = " << interfaceName << "::castFrom(base, true /* emitError */);\n";
297 out.sIf("!castRet.isOk()", [&] {
298 out.sIf("castRet.isDeadObject()", [&] {
299 // service is dead (castFrom cannot call interfaceChain)
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700300 out << "ALOGW(\"" << interfaceName << ": found dead hwbinder service\");\n"
Yifan Hong9c74a5b2017-04-04 13:27:25 -0700301 << (isTry ? "break" : "continue")
302 << ";\n";
Yifan Hong200209c2017-03-29 03:39:09 -0700303 }).sElse([&] {
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700304 out << "ALOGW(\"" << interfaceName << ": cannot call into hwbinder service: %s"
Yifan Hong200209c2017-03-29 03:39:09 -0700305 << "; No permission? Check for selinux denials.\", "
306 << "castRet.description().c_str());\n"
307 << "break;\n";
308 }).endl();
309 }).endl();
310 out << "iface = castRet;\n";
311 out.sIf("iface == nullptr", [&] {
312 // returned service isn't of correct type; this is a bug
313 // to hwservicemanager or to the service itself (interfaceChain
314 // is not consistent).
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700315 out << "ALOGW(\"" << interfaceName << ": received incompatible service; bug in hwservicemanager?\");\n"
Yifan Hong200209c2017-03-29 03:39:09 -0700316 << "break;\n";
317 }).endl();
Yifan Hong31f07ff2017-03-21 18:56:35 +0000318
319 out << "return iface;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800320 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800321
Steven Morelandf9cf33b2017-05-18 13:58:54 -0700322 out.sIf("getStub || vintfPassthru || vintfLegacy", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700323 out << "const sp<::android::hidl::manager::V1_0::IServiceManager> pm"
324 << " = getPassthroughServiceManager();\n";
Steven Morelandf10af872017-01-25 16:01:56 +0000325
326 out.sIf("pm != nullptr", [&] () {
Steven Morelandbcf51802017-04-06 09:17:44 -0700327 out << "Return<sp<" << gIBaseFqName.cppName() << ">> ret = \n";
Steven Morelandf10af872017-01-25 16:01:56 +0000328 out.indent(2, [&] {
329 out << "pm->get(" << interfaceName << "::descriptor" << ", serviceName);\n";
Steven Moreland2c2dea82017-01-18 17:24:17 -0800330 });
Steven Morelandf10af872017-01-25 16:01:56 +0000331 out.sIf("ret.isOk()", [&] {
Steven Morelandbcf51802017-04-06 09:17:44 -0700332 out << "sp<" << gIBaseFqName.cppName()
Steven Morelandf10af872017-01-25 16:01:56 +0000333 << "> baseInterface = ret;\n";
334 out.sIf("baseInterface != nullptr", [&]() {
Steven Morelandd281ebc2017-06-20 16:27:35 -0700335 out << "iface = " << interfaceName << "::castFrom(baseInterface);\n";
336 out.sIf("!getStub || trebleTestingOverride", [&] () {
337 out << "iface = new " << fqName.getInterfacePassthroughName() << "(iface);\n";
338 }).endl();
Yifan Hong31f07ff2017-03-21 18:56:35 +0000339 }).endl();
Steven Morelandf10af872017-01-25 16:01:56 +0000340 }).endl();
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800341 }).endl();
342 }).endl();
Steven Moreland2c2dea82017-01-18 17:24:17 -0800343
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800344 out << "return iface;\n";
345 }).endl().endl();
Steven Moreland038903b2017-03-30 12:11:24 -0700346}
347
348static void implementServiceManagerInteractions(Formatter &out,
349 const FQName &fqName, const std::string &package) {
350
351 const std::string interfaceName = fqName.getInterfaceName();
352
353 implementGetService(out, fqName, true /* isTry */);
354 implementGetService(out, fqName, false /* isTry */);
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800355
Yifan Hongeefe4f22017-01-04 15:32:42 -0800356 out << "::android::status_t " << interfaceName << "::registerAsService("
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800357 << "const std::string &serviceName) ";
358 out.block([&] {
Steven Moreland58b478b2017-04-09 10:54:50 -0700359 out << "::android::hardware::details::onRegistration(\""
360 << fqName.getPackageAndVersion().string() << "\", \""
361 << interfaceName
362 << "\", serviceName);\n\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800363 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
364 out.indent(2, [&] {
365 out << "= ::android::hardware::defaultServiceManager();\n";
366 });
367 out.sIf("sm == nullptr", [&] {
368 out << "return ::android::INVALID_OPERATION;\n";
369 }).endl();
Martijn Coenenbc9f5c92017-03-06 13:04:05 +0100370 out << "::android::hardware::Return<bool> ret = "
371 << "sm->add(serviceName.c_str(), this);\n"
372 << "return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800373 }).endl().endl();
374
Yifan Hongeefe4f22017-01-04 15:32:42 -0800375 out << "bool " << interfaceName << "::registerForNotifications(\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800376 out.indent(2, [&] {
377 out << "const std::string &serviceName,\n"
378 << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> "
379 << "&notification) ";
380 });
381 out.block([&] {
382 out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n";
383 out.indent(2, [&] {
384 out << "= ::android::hardware::defaultServiceManager();\n";
385 });
386 out.sIf("sm == nullptr", [&] {
387 out << "return false;\n";
388 }).endl();
389 out << "::android::hardware::Return<bool> success =\n";
390 out.indent(2, [&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800391 out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n";
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800392 out.indent(2, [&] {
393 out << "serviceName, notification);\n";
394 });
395 });
396 out << "return success.isOk() && success;\n";
397 }).endl().endl();
398}
399
Andreas Huberb82318c2016-08-02 14:45:54 -0700400status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700401 const Interface *iface = getInterface();
402 std::string ifaceName = iface ? iface->localName() : "types";
Andreas Huber881227d2016-08-02 14:20:21 -0700403
Andreas Huberb82318c2016-08-02 14:45:54 -0700404 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700405 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700406 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700407 path.append(ifaceName);
408 path.append(".h");
409
Andreas Huberd2943e12016-08-05 11:59:31 -0700410 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700411 FILE *file = fopen(path.c_str(), "w");
412
413 if (file == NULL) {
414 return -errno;
415 }
416
417 Formatter out(file);
418
419 const std::string guard = makeHeaderGuard(ifaceName);
420
421 out << "#ifndef " << guard << "\n";
422 out << "#define " << guard << "\n\n";
423
Andreas Huber737080b2016-08-02 15:38:04 -0700424 for (const auto &item : mImportedNames) {
Steven Morelandee88eed2016-10-31 17:49:00 -0700425 generateCppPackageInclude(out, item, item.name());
Andreas Huber737080b2016-08-02 15:38:04 -0700426 }
427
428 if (!mImportedNames.empty()) {
429 out << "\n";
430 }
431
Steven Moreland19f11b52017-05-12 18:22:21 -0700432 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800433 if (isIBase()) {
434 out << "// skipped #include IServiceNotification.h\n\n";
435 } else {
436 out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n";
437 }
Steven Moreland0693f312016-11-09 15:06:14 -0800438 }
439
Yifan Hongc8934042016-11-17 17:10:52 -0800440 out << "#include <hidl/HidlSupport.h>\n";
Andreas Huber4bcf97d2016-08-30 11:27:49 -0700441 out << "#include <hidl/MQDescriptor.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700442
Steven Moreland19f11b52017-05-12 18:22:21 -0700443 if (iface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200444 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700445 }
446
Martijn Coenenaf712c02016-11-16 15:26:27 +0100447 out << "#include <utils/NativeHandle.h>\n";
448 out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */
Andreas Huber881227d2016-08-02 14:20:21 -0700449
450 enterLeaveNamespace(out, true /* enter */);
451 out << "\n";
452
Steven Moreland19f11b52017-05-12 18:22:21 -0700453 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700454 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700455 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700456
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700457 const Interface *superType = iface->superType();
458
Steven Moreland40786312016-08-16 10:29:40 -0700459 if (superType == NULL) {
Yifan Hongc8934042016-11-17 17:10:52 -0800460 out << " : virtual public ::android::RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700461 } else {
Steven Morelandd916a702016-10-26 22:23:09 +0000462 out << " : public "
Steven Moreland40786312016-08-16 10:29:40 -0700463 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700464 }
465
466 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700467
468 out.indent();
469
Andreas Huber881227d2016-08-02 14:20:21 -0700470 }
471
472 status_t err = emitTypeDeclarations(out);
473
474 if (err != OK) {
475 return err;
476 }
477
Steven Moreland19f11b52017-05-12 18:22:21 -0700478 if (iface) {
Yifan Hongc8934042016-11-17 17:10:52 -0800479 out << "virtual bool isRemote() const ";
480 if (!isIBase()) {
481 out << "override ";
482 }
483 out << "{ return false; }\n\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800484
Andreas Huber881227d2016-08-02 14:20:21 -0700485 for (const auto &method : iface->methods()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700486 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700487
Andreas Huber881227d2016-08-02 14:20:21 -0700488 const bool returnsValue = !method->results().empty();
Steven Morelandd732ea12016-11-08 17:12:06 -0800489 const TypedVar *elidedReturn = method->canElideCallback();
490
491 if (elidedReturn == nullptr && returnsValue) {
492 out << "using "
493 << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700494 << "_cb = std::function<void(";
495 method->emitCppResultSignature(out, true /* specify namespaces */);
496 out << ")>;\n";
Steven Morelandd732ea12016-11-08 17:12:06 -0800497 }
Andreas Huber881227d2016-08-02 14:20:21 -0700498
Andreas Huber3599d922016-08-09 10:42:57 -0700499 method->dumpAnnotations(out);
500
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700501 if (elidedReturn) {
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700502 out << "virtual ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -0700503 out << elidedReturn->type().getCppResultType() << "> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700504 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700505 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700506 }
507
508 out << method->name()
Yifan Hong932464e2017-03-30 15:40:22 -0700509 << "(";
510 method->emitCppArgSignature(out, true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700511
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700512 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700513 if (!method->args().empty()) {
514 out << ", ";
515 }
516
Steven Moreland67f67b42016-09-29 08:59:02 -0700517 out << method->name() << "_cb _hidl_cb";
Andreas Huber881227d2016-08-02 14:20:21 -0700518 }
519
Yifan Hong10fe0b52016-10-19 14:20:17 -0700520 out << ")";
521 if (method->isHidlReserved()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800522 if (!isIBase()) {
523 out << " override";
524 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700525 } else {
Steven Morelandd4b068a2017-03-20 06:30:51 -0700526 out << " = 0";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700527 }
Steven Morelandd4b068a2017-03-20 06:30:51 -0700528 out << ";\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700529 }
Steven Moreland40786312016-08-16 10:29:40 -0700530
Yifan Hong3d746092016-12-07 14:26:33 -0800531 out << "// cast static functions\n";
532 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700533
Yifan Hong3d746092016-12-07 14:26:33 -0800534 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -0700535 out << "static ::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -0800536 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -0700537 << "> castFrom("
Yifan Hong3d746092016-12-07 14:26:33 -0800538 << superType->getCppArgumentType()
539 << " parent"
Yifan Hong200209c2017-03-29 03:39:09 -0700540 << ", bool emitError = false);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -0700541 }
542
Steven Morelandd39133b2016-11-11 12:30:08 -0800543 out << "\nstatic const char* descriptor;\n\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700544
Yifan Hongc8934042016-11-17 17:10:52 -0800545 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -0800546 out << "// skipped getService, registerAsService, registerForNotifications\n\n";
Yifan Hongc8934042016-11-17 17:10:52 -0800547 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800548 declareServiceManagerInteractions(out, iface->localName());
Yifan Hongc8934042016-11-17 17:10:52 -0800549 }
Andreas Huber881227d2016-08-02 14:20:21 -0700550 }
551
Steven Moreland19f11b52017-05-12 18:22:21 -0700552 if (iface) {
Andreas Huber881227d2016-08-02 14:20:21 -0700553 out.unindent();
554
Andreas Hubere3f769a2016-10-10 10:54:44 -0700555 out << "};\n\n";
556 }
557
558 err = mRootScope->emitGlobalTypeDeclarations(out);
559
560 if (err != OK) {
561 return err;
Andreas Huber881227d2016-08-02 14:20:21 -0700562 }
563
564 out << "\n";
565 enterLeaveNamespace(out, false /* enter */);
566
567 out << "\n#endif // " << guard << "\n";
568
569 return OK;
570}
571
Steven Moreland40786312016-08-16 10:29:40 -0700572status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700573 const Interface *iface = getInterface();
574 std::string klassName = iface ? iface->getHwName() : "hwtypes";
Steven Moreland40786312016-08-16 10:29:40 -0700575
Steven Moreland40786312016-08-16 10:29:40 -0700576 std::string path = outputPath;
577 path.append(mCoordinator->convertPackageRootToPath(mPackage));
578 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
579 path.append(klassName + ".h");
580
Yifan Hong244e82d2016-11-11 11:13:57 -0800581 FILE *file = fopen(path.c_str(), "w");
Steven Moreland40786312016-08-16 10:29:40 -0700582
583 if (file == NULL) {
584 return -errno;
585 }
586
587 Formatter out(file);
588
589 const std::string guard = makeHeaderGuard(klassName);
590
591 out << "#ifndef " << guard << "\n";
592 out << "#define " << guard << "\n\n";
593
Steven Moreland19f11b52017-05-12 18:22:21 -0700594 generateCppPackageInclude(out, mPackage, iface ? iface->localName() : "types");
Steven Moreland40786312016-08-16 10:29:40 -0700595
Steven Morelandee88eed2016-10-31 17:49:00 -0700596 out << "\n";
Steven Moreland40786312016-08-16 10:29:40 -0700597
598 for (const auto &item : mImportedNames) {
599 if (item.name() == "types") {
Yifan Hong244e82d2016-11-11 11:13:57 -0800600 generateCppPackageInclude(out, item, "hwtypes");
601 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800602 generateCppPackageInclude(out, item, item.getInterfaceStubName());
603 generateCppPackageInclude(out, item, item.getInterfaceProxyName());
Steven Moreland40786312016-08-16 10:29:40 -0700604 }
Steven Moreland40786312016-08-16 10:29:40 -0700605 }
606
607 out << "\n";
608
Martijn Coenen93915102016-09-01 01:35:52 +0200609 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700610 out << "#include <hwbinder/IBinder.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100611 out << "#include <hwbinder/Parcel.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700612
613 out << "\n";
614
615 enterLeaveNamespace(out, true /* enter */);
Steven Moreland40786312016-08-16 10:29:40 -0700616
Yifan Hong244e82d2016-11-11 11:13:57 -0800617 status_t err = mRootScope->emitGlobalHwDeclarations(out);
618 if (err != OK) {
619 return err;
620 }
Steven Moreland40786312016-08-16 10:29:40 -0700621
622 enterLeaveNamespace(out, false /* enter */);
623
624 out << "\n#endif // " << guard << "\n";
625
626 return OK;
627}
628
Andreas Huber881227d2016-08-02 14:20:21 -0700629status_t AST::emitTypeDeclarations(Formatter &out) const {
630 return mRootScope->emitTypeDeclarations(out);
631}
632
Yifan Hong7a118f52016-12-07 11:21:15 -0800633static void wrapPassthroughArg(Formatter &out,
634 const TypedVar *arg, bool addPrefixToName,
635 std::function<void(void)> handleError) {
636 if (!arg->type().isInterface()) {
637 return;
638 }
639 std::string name = (addPrefixToName ? "_hidl_out_" : "") + arg->name();
640 std::string wrappedName = (addPrefixToName ? "_hidl_out_wrapped_" : "_hidl_wrapped_")
641 + arg->name();
642 const Interface &iface = static_cast<const Interface &>(arg->type());
643 out << iface.getCppStackType() << " " << wrappedName << ";\n";
644 // TODO(elsk): b/33754152 Should not wrap this if object is Bs*
645 out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] {
646 out << wrappedName
647 << " = "
648 << iface.fqName().cppName()
Yifan Hong341112d2017-04-20 18:12:05 -0700649 << "::castFrom(::android::hardware::details::wrapPassthrough<"
650 << iface.fqName().cppName()
651 << ">("
Yifan Hong7a118f52016-12-07 11:21:15 -0800652 << name << "));\n";
653 out.sIf(wrappedName + " == nullptr", [&] {
654 // Fatal error. Happens when the BsFoo class is not found in the binary
655 // or any dynamic libraries.
656 handleError();
657 }).endl();
658 }).sElse([&] {
659 out << wrappedName << " = " << name << ";\n";
660 }).endl().endl();
661}
662
Steven Moreland69e7c702016-09-09 11:16:32 -0700663status_t AST::generatePassthroughMethod(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -0700664 const Method *method) const {
665 method->generateCppSignature(out);
Steven Moreland69e7c702016-09-09 11:16:32 -0700666
667 out << " {\n";
668 out.indent();
669
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800670 if (method->isHidlReserved()
671 && method->overridesCppImpl(IMPL_PASSTHROUGH)) {
672 method->cppImpl(IMPL_PASSTHROUGH, out);
673 out.unindent();
674 out << "}\n\n";
675 return OK;
676 }
677
Steven Moreland69e7c702016-09-09 11:16:32 -0700678 const bool returnsValue = !method->results().empty();
679 const TypedVar *elidedReturn = method->canElideCallback();
680
Steven Moreland67f67b42016-09-29 08:59:02 -0700681 if (returnsValue && elidedReturn == nullptr) {
682 generateCheckNonNull(out, "_hidl_cb");
683 }
684
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700685 generateCppInstrumentationCall(
686 out,
687 InstrumentationEvent::PASSTHROUGH_ENTRY,
688 method);
689
Yifan Hong7a118f52016-12-07 11:21:15 -0800690
691 for (const auto &arg : method->args()) {
692 wrapPassthroughArg(out, arg, false /* addPrefixToName */, [&] {
693 out << "return ::android::hardware::Status::fromExceptionCode(\n";
694 out.indent(2, [&] {
695 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800696 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800697 });
698 });
699 }
700
701 out << "auto _hidl_error = ::android::hardware::Void();\n";
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700702 out << "auto _hidl_return = ";
Steven Moreland69e7c702016-09-09 11:16:32 -0700703
704 if (method->isOneway()) {
Steven Moreland836cb312017-06-05 17:25:55 -0700705 out << "addOnewayTask([mImpl = this->mImpl\n"
706 << "#ifdef __ANDROID_DEBUGGABLE__\n"
707 ", mEnableInstrumentation = this->mEnableInstrumentation, "
708 "mInstrumentationCallbacks = this->mInstrumentationCallbacks\n"
709 << "#endif // __ANDROID_DEBUGGABLE__\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700710 for (const auto &arg : method->args()) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800711 out << ", "
712 << (arg->type().isInterface() ? "_hidl_wrapped_" : "")
713 << arg->name();
Steven Moreland69e7c702016-09-09 11:16:32 -0700714 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700715 out << "] {\n";
716 out.indent();
Steven Moreland69e7c702016-09-09 11:16:32 -0700717 }
718
719 out << "mImpl->"
720 << method->name()
721 << "(";
722
Yifan Hong932464e2017-03-30 15:40:22 -0700723 out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800724 out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700725 });
Steven Moreland69e7c702016-09-09 11:16:32 -0700726 if (returnsValue && elidedReturn == nullptr) {
Steven Moreland340c8822017-05-02 14:41:49 -0700727 // never true if oneway since oneway methods don't return values
728
Steven Moreland69e7c702016-09-09 11:16:32 -0700729 if (!method->args().empty()) {
730 out << ", ";
731 }
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800732 out << "[&](";
Yifan Hong932464e2017-03-30 15:40:22 -0700733 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800734 out << "const auto &_hidl_out_"
735 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700736 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800737
738 out << ") {\n";
739 out.indent();
740 status_t status = generateCppInstrumentationCall(
741 out,
742 InstrumentationEvent::PASSTHROUGH_EXIT,
743 method);
744 if (status != OK) {
745 return status;
746 }
747
Yifan Hong7a118f52016-12-07 11:21:15 -0800748 for (const auto &arg : method->results()) {
749 wrapPassthroughArg(out, arg, true /* addPrefixToName */, [&] {
750 out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n";
751 out.indent(2, [&] {
752 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
Yifan Hong0abd7392016-12-20 16:43:26 -0800753 << "\"Cannot wrap passthrough interface.\");\n";
Yifan Hong7a118f52016-12-07 11:21:15 -0800754 });
755 out << "return;\n";
756 });
757 }
758
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800759 out << "_hidl_cb(";
Yifan Hong932464e2017-03-30 15:40:22 -0700760 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Hong7a118f52016-12-07 11:21:15 -0800761 out << (arg->type().isInterface() ? "_hidl_out_wrapped_" : "_hidl_out_")
762 << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -0700763 });
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800764 out << ");\n";
765 out.unindent();
766 out << "});\n\n";
767 } else {
768 out << ");\n\n";
Steven Moreland30b76e92017-06-02 18:52:24 -0700769
770 // used by generateCppInstrumentationCall
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800771 if (elidedReturn != nullptr) {
Steven Moreland30b76e92017-06-02 18:52:24 -0700772 out << "#ifdef __ANDROID_DEBUGGABLE__\n"
773 << elidedReturn->type().getCppResultType() << " _hidl_out_" << elidedReturn->name()
774 << " = _hidl_return;\n"
775 << "#endif // __ANDROID_DEBUGGABLE__\n";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -0800776 }
777 status_t status = generateCppInstrumentationCall(
778 out,
779 InstrumentationEvent::PASSTHROUGH_EXIT,
780 method);
781 if (status != OK) {
782 return status;
783 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700784 }
Steven Moreland69e7c702016-09-09 11:16:32 -0700785
786 if (method->isOneway()) {
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700787 out.unindent();
788 out << "});\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700789 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -0700790
791 out << "return _hidl_return;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -0700792
793 out.unindent();
794 out << "}\n";
795
796 return OK;
797}
798
Yifan Hong068c5522016-10-31 14:07:25 -0700799status_t AST::generateMethods(Formatter &out, MethodGenerator gen) const {
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700800 const Interface *iface = mRootScope->getInterface();
801
Yifan Hong10fe0b52016-10-19 14:20:17 -0700802 const Interface *prevIterface = nullptr;
803 for (const auto &tuple : iface->allMethodsFromRoot()) {
804 const Method *method = tuple.method();
805 const Interface *superInterface = tuple.interface();
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700806
Yifan Hong10fe0b52016-10-19 14:20:17 -0700807 if(prevIterface != superInterface) {
808 if (prevIterface != nullptr) {
809 out << "\n";
810 }
811 out << "// Methods from "
812 << superInterface->fullName()
813 << " follow.\n";
814 prevIterface = superInterface;
815 }
Yifan Hong068c5522016-10-31 14:07:25 -0700816 status_t err = gen(method, superInterface);
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700817
Yifan Hong10fe0b52016-10-19 14:20:17 -0700818 if (err != OK) {
819 return err;
820 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700821 }
822
Yifan Hong10fe0b52016-10-19 14:20:17 -0700823 out << "\n";
824
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700825 return OK;
826}
827
Steven Moreland0b843772017-06-23 16:33:38 -0700828void AST::generateTemplatizationLink(Formatter& out) const {
829 out << "typedef " << mRootScope->getInterface()->localName() << " Pure;\n\n";
830}
831
Andreas Huberb82318c2016-08-02 14:45:54 -0700832status_t AST::generateStubHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700833 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700834 // types.hal does not get a stub header.
835 return OK;
836 }
837
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700838 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800839 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -0700840
Andreas Huberb82318c2016-08-02 14:45:54 -0700841 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700842 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700843 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700844 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700845 path.append(".h");
846
Andreas Huberd2943e12016-08-05 11:59:31 -0700847 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700848 FILE *file = fopen(path.c_str(), "w");
849
850 if (file == NULL) {
851 return -errno;
852 }
853
854 Formatter out(file);
855
Steven Moreland40786312016-08-16 10:29:40 -0700856 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700857
858 out << "#ifndef " << guard << "\n";
859 out << "#define " << guard << "\n\n";
860
Yifan Hongeefe4f22017-01-04 15:32:42 -0800861 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700862 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700863
864 enterLeaveNamespace(out, true /* enter */);
865 out << "\n";
866
867 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800868 << klassName;
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100869 if (iface->isIBase()) {
Yifan Hong96a79e22017-01-12 14:22:05 -0800870 out << " : public ::android::hardware::BHwBinder";
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000871 out << ", public ::android::hardware::details::HidlInstrumentor {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100872 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800873 out << " : public "
874 << gIBaseFqName.getInterfaceStubFqName().cppName()
875 << " {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100876 }
Andreas Huber881227d2016-08-02 14:20:21 -0700877
878 out.indent();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800879 out << "explicit "
880 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700881 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl);"
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100882 << "\n";
Yifan Hongeefe4f22017-01-04 15:32:42 -0800883 out << "explicit "
884 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -0700885 << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl,"
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -0800886 << " const std::string& HidlInstrumentor_package,"
887 << " const std::string& HidlInstrumentor_interface);"
Steven Moreland40786312016-08-16 10:29:40 -0700888 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700889 out << "::android::status_t onTransact(\n";
890 out.indent();
891 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700892 out << "uint32_t _hidl_code,\n";
893 out << "const ::android::hardware::Parcel &_hidl_data,\n";
894 out << "::android::hardware::Parcel *_hidl_reply,\n";
895 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700896 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700897 out.unindent();
898 out.unindent();
899
Steven Moreland0b843772017-06-23 16:33:38 -0700900 out.endl();
901 generateTemplatizationLink(out);
902
Steven Moreland19f11b52017-05-12 18:22:21 -0700903 out << "::android::sp<" << iface->localName() << "> getImpl() { return _hidl_mImpl; };\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +0100904 out.unindent();
905 out << "private:\n";
906 out.indent();
Yifan Hongcd2ae452017-01-31 14:33:40 -0800907
908 status_t err = generateMethods(out, [&](const Method *method, const Interface *iface) {
909 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
910 return OK;
911 }
912 const bool returnsValue = !method->results().empty();
913 const TypedVar *elidedReturn = method->canElideCallback();
914
915 if (elidedReturn == nullptr && returnsValue) {
916 out << "using " << method->name() << "_cb = "
917 << iface->fqName().cppName()
918 << "::" << method->name() << "_cb;\n";
919 }
920 method->generateCppSignature(out);
Yifan Hongbcffce22017-02-01 15:52:06 -0800921 out << ";\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800922 return OK;
923 });
924 if (err != OK) {
925 return err;
926 }
927
Steven Moreland19f11b52017-05-12 18:22:21 -0700928 out << "::android::sp<" << iface->localName() << "> _hidl_mImpl;\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700929 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700930 out << "};\n\n";
931
932 enterLeaveNamespace(out, false /* enter */);
933
934 out << "\n#endif // " << guard << "\n";
935
936 return OK;
937}
938
Andreas Huberb82318c2016-08-02 14:45:54 -0700939status_t AST::generateProxyHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700940 if (!AST::isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -0700941 // types.hal does not get a proxy header.
942 return OK;
943 }
944
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700945 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -0800946 const std::string proxyName = iface->getProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -0700947
Andreas Huberb82318c2016-08-02 14:45:54 -0700948 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700949 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700950 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Yifan Hongeefe4f22017-01-04 15:32:42 -0800951 path.append(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700952 path.append(".h");
953
Andreas Huberd2943e12016-08-05 11:59:31 -0700954 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700955 FILE *file = fopen(path.c_str(), "w");
956
957 if (file == NULL) {
958 return -errno;
959 }
960
961 Formatter out(file);
962
Yifan Hongeefe4f22017-01-04 15:32:42 -0800963 const std::string guard = makeHeaderGuard(proxyName);
Andreas Huber881227d2016-08-02 14:20:21 -0700964
965 out << "#ifndef " << guard << "\n";
966 out << "#define " << guard << "\n\n";
967
Martijn Coenen115d4282016-12-19 05:14:04 +0100968 out << "#include <hidl/HidlTransportSupport.h>\n\n";
969
Andreas Huber881227d2016-08-02 14:20:21 -0700970 std::vector<std::string> packageComponents;
971 getPackageAndVersionComponents(
972 &packageComponents, false /* cpp_compatible */);
973
Yifan Hongeefe4f22017-01-04 15:32:42 -0800974 generateCppPackageInclude(out, mPackage, iface->getHwName());
Steven Morelandee88eed2016-10-31 17:49:00 -0700975 out << "\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700976
977 enterLeaveNamespace(out, true /* enter */);
978 out << "\n";
979
980 out << "struct "
Yifan Hongeefe4f22017-01-04 15:32:42 -0800981 << proxyName
982 << " : public ::android::hardware::BpInterface<"
983 << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +0000984 << ">, public ::android::hardware::details::HidlInstrumentor {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700985
986 out.indent();
987
Yifan Hongeefe4f22017-01-04 15:32:42 -0800988 out << "explicit "
989 << proxyName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700990 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700991 << "\n\n";
992
Steven Moreland0b843772017-06-23 16:33:38 -0700993 generateTemplatizationLink(out);
994
Yifan Hong10fe0b52016-10-19 14:20:17 -0700995 out << "virtual bool isRemote() const override { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700996
Yifan Hong068c5522016-10-31 14:07:25 -0700997 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
998 method->generateCppSignature(out);
999 out << " override;\n";
1000 return OK;
1001 });
Steven Moreland9c387612016-09-07 09:54:26 -07001002
1003 if (err != OK) {
1004 return err;
1005 }
Andreas Huber881227d2016-08-02 14:20:21 -07001006
1007 out.unindent();
Martijn Coenen115d4282016-12-19 05:14:04 +01001008 out << "private:\n";
1009 out.indent();
1010 out << "std::mutex _hidl_mMutex;\n"
1011 << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>"
1012 << " _hidl_mDeathRecipients;\n";
1013 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -07001014 out << "};\n\n";
1015
1016 enterLeaveNamespace(out, false /* enter */);
1017
1018 out << "\n#endif // " << guard << "\n";
1019
1020 return OK;
1021}
1022
Steven Moreland1cbf0362017-05-09 14:32:53 -07001023status_t AST::generateCppSources(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001024 std::string baseName = getBaseName();
1025 const Interface *iface = getInterface();
Andreas Huber881227d2016-08-02 14:20:21 -07001026
Andreas Huberb82318c2016-08-02 14:45:54 -07001027 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -07001028 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -07001029 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -07001030 path.append(baseName);
1031
1032 if (baseName != "types") {
1033 path.append("All");
1034 }
1035
1036 path.append(".cpp");
1037
Andreas Huberd2943e12016-08-05 11:59:31 -07001038 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -07001039 FILE *file = fopen(path.c_str(), "w");
1040
1041 if (file == NULL) {
1042 return -errno;
1043 }
1044
1045 Formatter out(file);
1046
Steven Moreland623c0042017-01-13 14:42:29 -08001047 out << "#define LOG_TAG \""
1048 << mPackage.string() << "::" << baseName
1049 << "\"\n\n";
1050
Steven Moreland05cd4232016-11-21 16:01:12 -08001051 out << "#include <android/log.h>\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001052 out << "#include <cutils/trace.h>\n";
1053 out << "#include <hidl/HidlTransportSupport.h>\n\n";
Steven Moreland19f11b52017-05-12 18:22:21 -07001054 if (iface) {
Steven Moreland19d5c172016-10-20 19:20:25 -07001055 // This is a no-op for IServiceManager itself.
1056 out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n";
1057
Yifan Hongeefe4f22017-01-04 15:32:42 -08001058 generateCppPackageInclude(out, mPackage, iface->getProxyName());
1059 generateCppPackageInclude(out, mPackage, iface->getStubName());
1060 generateCppPackageInclude(out, mPackage, iface->getPassthroughName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001061
1062 for (const Interface *superType : iface->superTypeChain()) {
Steven Morelandee88eed2016-10-31 17:49:00 -07001063 generateCppPackageInclude(out,
1064 superType->fqName(),
Yifan Hongeefe4f22017-01-04 15:32:42 -08001065 superType->fqName().getInterfaceProxyName());
Yifan Hongfe95aa22016-10-19 17:26:45 -07001066 }
Yifan Hong2cbbdf92016-12-05 15:20:50 -08001067
1068 out << "#include <hidl/ServiceManagement.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001069 } else {
Steven Morelandee88eed2016-10-31 17:49:00 -07001070 generateCppPackageInclude(out, mPackage, "types");
Yifan Hong244e82d2016-11-11 11:13:57 -08001071 generateCppPackageInclude(out, mPackage, "hwtypes");
Andreas Huber881227d2016-08-02 14:20:21 -07001072 }
1073
1074 out << "\n";
1075
1076 enterLeaveNamespace(out, true /* enter */);
1077 out << "\n";
1078
Steven Moreland19f11b52017-05-12 18:22:21 -07001079 status_t err = generateTypeSource(out, iface ? iface->localName() : "");
Andreas Huber881227d2016-08-02 14:20:21 -07001080
Steven Moreland19f11b52017-05-12 18:22:21 -07001081 if (err == OK && iface) {
Yifan Hong10fe0b52016-10-19 14:20:17 -07001082 const Interface *iface = mRootScope->getInterface();
Yifan Hong10fe0b52016-10-19 14:20:17 -07001083
1084 // need to be put here, generateStubSource is using this.
Yifan Hongeefe4f22017-01-04 15:32:42 -08001085 out << "const char* "
1086 << iface->localName()
Yifan Hong10fe0b52016-10-19 14:20:17 -07001087 << "::descriptor(\""
1088 << iface->fqName().string()
1089 << "\");\n\n";
Martijn Coenen8adcb652017-02-03 17:37:36 +01001090 out << "__attribute__((constructor))";
1091 out << "static void static_constructor() {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001092 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001093 out << "::android::hardware::details::gBnConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001094 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001095 << "::descriptor,\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001096 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001097 out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08001098 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001099 out << "return new "
1100 << iface->getStubName()
Yifan Hong341112d2017-04-20 18:12:05 -07001101 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001102 << iface->localName()
Yifan Hong158655a2016-11-08 12:34:07 -08001103 << " *>(iIntf));\n";
1104 });
Yifan Hongb04de382017-02-06 15:31:52 -08001105 out << "});\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001106 });
Yifan Honga159f3b2017-03-16 14:53:51 -07001107 out << "::android::hardware::details::gBsConstructorMap.set("
Yifan Hongeefe4f22017-01-04 15:32:42 -08001108 << iface->localName()
Yifan Hongb04de382017-02-06 15:31:52 -08001109 << "::descriptor,\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001110 out.indent(2, [&] {
Yifan Hongb04de382017-02-06 15:31:52 -08001111 out << "[](void *iIntf) -> ::android::sp<"
Yifan Hong7a118f52016-12-07 11:21:15 -08001112 << gIBaseFqName.cppName()
1113 << "> {\n";
1114 out.indent([&] {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001115 out << "return new "
1116 << iface->getPassthroughName()
Yifan Hong341112d2017-04-20 18:12:05 -07001117 << "(static_cast<"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001118 << iface->localName()
Yifan Hong7a118f52016-12-07 11:21:15 -08001119 << " *>(iIntf));\n";
1120 });
Yifan Hongb04de382017-02-06 15:31:52 -08001121 out << "});\n";
Yifan Hong7a118f52016-12-07 11:21:15 -08001122 });
Yifan Hong158655a2016-11-08 12:34:07 -08001123 });
Martijn Coenen8adcb652017-02-03 17:37:36 +01001124 out << "};\n\n";
1125 out << "__attribute__((destructor))";
1126 out << "static void static_destructor() {\n";
1127 out.indent([&] {
Yifan Honga159f3b2017-03-16 14:53:51 -07001128 out << "::android::hardware::details::gBnConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001129 << iface->localName()
1130 << "::descriptor);\n";
Yifan Honga159f3b2017-03-16 14:53:51 -07001131 out << "::android::hardware::details::gBsConstructorMap.erase("
Martijn Coenen8adcb652017-02-03 17:37:36 +01001132 << iface->localName()
1133 << "::descriptor);\n";
1134 });
1135 out << "};\n\n";
Yifan Hong158655a2016-11-08 12:34:07 -08001136
Yifan Hongfe95aa22016-10-19 17:26:45 -07001137 err = generateInterfaceSource(out);
1138 }
1139
Steven Moreland19f11b52017-05-12 18:22:21 -07001140 if (err == OK && iface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001141 err = generateProxySource(out, iface->fqName());
Andreas Huber881227d2016-08-02 14:20:21 -07001142 }
1143
Steven Moreland19f11b52017-05-12 18:22:21 -07001144 if (err == OK && iface) {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001145 err = generateStubSource(out, iface);
Andreas Huber881227d2016-08-02 14:20:21 -07001146 }
1147
Steven Moreland19f11b52017-05-12 18:22:21 -07001148 if (err == OK && iface) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001149 err = generatePassthroughSource(out);
1150 }
1151
Steven Moreland19f11b52017-05-12 18:22:21 -07001152 if (err == OK && iface) {
Steven Moreland9c387612016-09-07 09:54:26 -07001153 const Interface *iface = mRootScope->getInterface();
1154
Yifan Hongc8934042016-11-17 17:10:52 -08001155 if (isIBase()) {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001156 out << "// skipped getService, registerAsService, registerForNotifications\n";
Yifan Hongc8934042016-11-17 17:10:52 -08001157 } else {
Yifan Hong83c8e5f2016-12-13 14:33:53 -08001158 std::string package = iface->fqName().package()
1159 + iface->fqName().atVersion();
1160
Yifan Hongeefe4f22017-01-04 15:32:42 -08001161 implementServiceManagerInteractions(out, iface->fqName(), package);
Yifan Hongc8934042016-11-17 17:10:52 -08001162 }
Steven Moreland40786312016-08-16 10:29:40 -07001163 }
1164
Andreas Huber6755e9d2017-04-06 11:09:07 -07001165 HidlTypeAssertion::EmitAll(out);
1166 out << "\n";
1167
Andreas Huber881227d2016-08-02 14:20:21 -07001168 enterLeaveNamespace(out, false /* enter */);
1169
1170 return err;
1171}
1172
Steven Moreland67f67b42016-09-29 08:59:02 -07001173void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) {
Yifan Honga018ed52016-12-13 16:35:08 -08001174 out.sIf(nonNull + " == nullptr", [&] {
1175 out << "return ::android::hardware::Status::fromExceptionCode(\n";
1176 out.indent(2, [&] {
Steven Moreland610002f2017-06-16 13:02:49 -07001177 out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT,\n"
1178 << "\"Null synchronous callback passed.\");\n";
Yifan Honga018ed52016-12-13 16:35:08 -08001179 });
1180 }).endl().endl();
Steven Moreland67f67b42016-09-29 08:59:02 -07001181}
1182
Andreas Huber881227d2016-08-02 14:20:21 -07001183status_t AST::generateTypeSource(
1184 Formatter &out, const std::string &ifaceName) const {
1185 return mRootScope->emitTypeDefinitions(out, ifaceName);
1186}
1187
Andreas Hubere7ff2282016-08-16 13:50:03 -07001188void AST::declareCppReaderLocals(
Andreas Huber5e44a292016-09-27 14:52:39 -07001189 Formatter &out,
1190 const std::vector<TypedVar *> &args,
1191 bool forResults) const {
Andreas Hubere7ff2282016-08-16 13:50:03 -07001192 if (args.empty()) {
1193 return;
1194 }
1195
1196 for (const auto &arg : args) {
1197 const Type &type = arg->type();
1198
Yifan Hong3b320f82016-11-01 15:15:54 -07001199 out << type.getCppResultType()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001200 << " "
Yifan Hong3b320f82016-11-01 15:15:54 -07001201 << (forResults ? "_hidl_out_" : "") + arg->name()
Andreas Hubere7ff2282016-08-16 13:50:03 -07001202 << ";\n";
1203 }
1204
1205 out << "\n";
1206}
1207
Andreas Huber881227d2016-08-02 14:20:21 -07001208void AST::emitCppReaderWriter(
1209 Formatter &out,
1210 const std::string &parcelObj,
1211 bool parcelObjIsPointer,
1212 const TypedVar *arg,
1213 bool isReader,
Andreas Huber5e44a292016-09-27 14:52:39 -07001214 Type::ErrorMode mode,
1215 bool addPrefixToName) const {
Andreas Huber881227d2016-08-02 14:20:21 -07001216 const Type &type = arg->type();
1217
Andreas Huber881227d2016-08-02 14:20:21 -07001218 type.emitReaderWriter(
1219 out,
Andreas Huber5e44a292016-09-27 14:52:39 -07001220 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
Andreas Huber881227d2016-08-02 14:20:21 -07001221 parcelObj,
1222 parcelObjIsPointer,
1223 isReader,
1224 mode);
1225}
1226
Yifan Hongbf459bc2016-08-23 16:50:37 -07001227void AST::emitCppResolveReferences(
1228 Formatter &out,
1229 const std::string &parcelObj,
1230 bool parcelObjIsPointer,
1231 const TypedVar *arg,
1232 bool isReader,
1233 Type::ErrorMode mode,
1234 bool addPrefixToName) const {
1235 const Type &type = arg->type();
1236 if(type.needsResolveReferences()) {
1237 type.emitResolveReferences(
1238 out,
1239 addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(),
1240 isReader, // nameIsPointer
1241 parcelObj,
1242 parcelObjIsPointer,
1243 isReader,
1244 mode);
1245 }
1246}
1247
Yifan Hong068c5522016-10-31 14:07:25 -07001248status_t AST::generateProxyMethodSource(Formatter &out,
1249 const std::string &klassName,
1250 const Method *method,
1251 const Interface *superInterface) const {
1252
1253 method->generateCppSignature(out,
1254 klassName,
1255 true /* specify namespaces */);
1256
1257 const bool returnsValue = !method->results().empty();
1258 const TypedVar *elidedReturn = method->canElideCallback();
1259
Steven Moreland41c6d2e2016-11-07 12:26:54 -08001260 out << " {\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001261
1262 out.indent();
1263
Martijn Coenen115d4282016-12-19 05:14:04 +01001264 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) {
1265 method->cppImpl(IMPL_PROXY, out);
1266 out.unindent();
1267 out << "}\n\n";
1268 return OK;
1269 }
1270
Yifan Hong068c5522016-10-31 14:07:25 -07001271 if (returnsValue && elidedReturn == nullptr) {
1272 generateCheckNonNull(out, "_hidl_cb");
1273 }
1274
1275 status_t status = generateCppInstrumentationCall(
1276 out,
1277 InstrumentationEvent::CLIENT_API_ENTRY,
Yifan Hong068c5522016-10-31 14:07:25 -07001278 method);
1279 if (status != OK) {
1280 return status;
1281 }
1282
1283 out << "::android::hardware::Parcel _hidl_data;\n";
1284 out << "::android::hardware::Parcel _hidl_reply;\n";
1285 out << "::android::status_t _hidl_err;\n";
1286 out << "::android::hardware::Status _hidl_status;\n\n";
1287
1288 declareCppReaderLocals(
1289 out, method->results(), true /* forResults */);
1290
1291 out << "_hidl_err = _hidl_data.writeInterfaceToken(";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001292 out << superInterface->fqName().cppName();
Yifan Hong068c5522016-10-31 14:07:25 -07001293 out << "::descriptor);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001294 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1295
Martijn Coenenfff73352017-01-04 16:36:31 +01001296 bool hasInterfaceArgument = false;
Yifan Hong068c5522016-10-31 14:07:25 -07001297 // First DFS: write all buffers and resolve pointers for parent
1298 for (const auto &arg : method->args()) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001299 if (arg->type().isInterface()) {
1300 hasInterfaceArgument = true;
1301 }
Yifan Hong068c5522016-10-31 14:07:25 -07001302 emitCppReaderWriter(
1303 out,
1304 "_hidl_data",
1305 false /* parcelObjIsPointer */,
1306 arg,
1307 false /* reader */,
1308 Type::ErrorMode_Goto,
1309 false /* addPrefixToName */);
1310 }
1311
1312 // Second DFS: resolve references.
1313 for (const auto &arg : method->args()) {
1314 emitCppResolveReferences(
1315 out,
1316 "_hidl_data",
1317 false /* parcelObjIsPointer */,
1318 arg,
1319 false /* reader */,
1320 Type::ErrorMode_Goto,
1321 false /* addPrefixToName */);
1322 }
1323
Martijn Coenenfa55d6e2016-12-20 06:08:31 +01001324 if (hasInterfaceArgument) {
1325 // Start binder threadpool to handle incoming transactions
1326 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
1327 }
Yifan Hong068c5522016-10-31 14:07:25 -07001328 out << "_hidl_err = remote()->transact("
1329 << method->getSerialId()
1330 << " /* "
1331 << method->name()
1332 << " */, _hidl_data, &_hidl_reply";
1333
1334 if (method->isOneway()) {
1335 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
1336 }
1337 out << ");\n";
1338
1339 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1340
1341 if (!method->isOneway()) {
Yifan Hong859e53f2016-11-14 19:08:24 -08001342 out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n";
Yifan Hong068c5522016-10-31 14:07:25 -07001343 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
1344 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
1345
1346
1347 // First DFS: write all buffers and resolve pointers for parent
1348 for (const auto &arg : method->results()) {
1349 emitCppReaderWriter(
1350 out,
1351 "_hidl_reply",
1352 false /* parcelObjIsPointer */,
1353 arg,
1354 true /* reader */,
1355 Type::ErrorMode_Goto,
1356 true /* addPrefixToName */);
1357 }
1358
1359 // Second DFS: resolve references.
1360 for (const auto &arg : method->results()) {
1361 emitCppResolveReferences(
1362 out,
1363 "_hidl_reply",
1364 false /* parcelObjIsPointer */,
1365 arg,
1366 true /* reader */,
1367 Type::ErrorMode_Goto,
1368 true /* addPrefixToName */);
1369 }
1370
1371 if (returnsValue && elidedReturn == nullptr) {
1372 out << "_hidl_cb(";
1373
Yifan Hong932464e2017-03-30 15:40:22 -07001374 out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) {
Yifan Hong068c5522016-10-31 14:07:25 -07001375 if (arg->type().resultNeedsDeref()) {
1376 out << "*";
1377 }
1378 out << "_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001379 });
Yifan Hong068c5522016-10-31 14:07:25 -07001380
1381 out << ");\n\n";
1382 }
Martijn Coenen7b295242016-11-04 16:52:56 +01001383 }
1384 status = generateCppInstrumentationCall(
1385 out,
1386 InstrumentationEvent::CLIENT_API_EXIT,
1387 method);
1388 if (status != OK) {
1389 return status;
Yifan Hong068c5522016-10-31 14:07:25 -07001390 }
1391
1392 if (elidedReturn != nullptr) {
Yifan Hong068c5522016-10-31 14:07:25 -07001393 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1394 out << "return ::android::hardware::Return<";
Yifan Hong3b320f82016-11-01 15:15:54 -07001395 out << elidedReturn->type().getCppResultType()
Yifan Hong068c5522016-10-31 14:07:25 -07001396 << ">(_hidl_out_" << elidedReturn->name() << ");\n\n";
1397 } else {
1398 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1399 out << "return ::android::hardware::Return<void>();\n\n";
1400 }
1401
1402 out.unindent();
1403 out << "_hidl_error:\n";
1404 out.indent();
1405 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
1406 out << "return ::android::hardware::Return<";
1407 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001408 out << method->results().at(0)->type().getCppResultType();
Yifan Hong068c5522016-10-31 14:07:25 -07001409 } else {
1410 out << "void";
1411 }
1412 out << ">(_hidl_status);\n";
1413
1414 out.unindent();
1415 out << "}\n\n";
1416 return OK;
1417}
1418
Andreas Huber881227d2016-08-02 14:20:21 -07001419status_t AST::generateProxySource(
Yifan Hongeefe4f22017-01-04 15:32:42 -08001420 Formatter &out, const FQName &fqName) const {
1421 const std::string klassName = fqName.getInterfaceProxyName();
Andreas Huber881227d2016-08-02 14:20:21 -07001422
1423 out << klassName
1424 << "::"
1425 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -07001426 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001427
1428 out.indent();
1429 out.indent();
1430
1431 out << ": BpInterface"
Yifan Hongeefe4f22017-01-04 15:32:42 -08001432 << "<"
1433 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001434 << ">(_hidl_impl),\n"
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001435 << " ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001436 << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001437 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001438 << fqName.getInterfaceName()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07001439 << "\") {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001440
Andreas Huber881227d2016-08-02 14:20:21 -07001441 out.unindent();
1442 out.unindent();
1443 out << "}\n\n";
1444
Yifan Hong068c5522016-10-31 14:07:25 -07001445 status_t err = generateMethods(out, [&](const Method *method, const Interface *superInterface) {
1446 return generateProxyMethodSource(out, klassName, method, superInterface);
1447 });
Andreas Huber881227d2016-08-02 14:20:21 -07001448
Yifan Hong068c5522016-10-31 14:07:25 -07001449 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001450}
1451
1452status_t AST::generateStubSource(
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001453 Formatter &out,
Yifan Hongeefe4f22017-01-04 15:32:42 -08001454 const Interface *iface) const {
1455 const std::string interfaceName = iface->localName();
1456 const std::string klassName = iface->getStubName();
Andreas Huber881227d2016-08-02 14:20:21 -07001457
Steven Moreland40786312016-08-16 10:29:40 -07001458 out << klassName
1459 << "::"
1460 << klassName
Yifan Hongeefe4f22017-01-04 15:32:42 -08001461 << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n";
Steven Moreland40786312016-08-16 10:29:40 -07001462
1463 out.indent();
1464 out.indent();
1465
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001466 if (iface->isIBase()) {
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001467 out << ": ::android::hardware::details::HidlInstrumentor(\"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001468 } else {
Yifan Hongeefe4f22017-01-04 15:32:42 -08001469 out << ": "
1470 << gIBaseFqName.getInterfaceStubFqName().cppName()
1471 << "(_hidl_impl, \"";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001472 }
1473
1474 out << mPackage.string()
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001475 << "\", \""
Yifan Hongeefe4f22017-01-04 15:32:42 -08001476 << interfaceName
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001477 << "\") { \n";
1478 out.indent();
1479 out << "_hidl_mImpl = _hidl_impl;\n";
Martijn Coenenb4d77952017-05-03 13:44:29 -07001480 out << "auto prio = ::android::hardware::details::gServicePrioMap.get("
1481 << "_hidl_impl, {SCHED_NORMAL, 0});\n";
1482 out << "mSchedPolicy = prio.sched_policy;\n";
1483 out << "mSchedPriority = prio.prio;\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001484 out.unindent();
Steven Moreland40786312016-08-16 10:29:40 -07001485
1486 out.unindent();
1487 out.unindent();
1488 out << "}\n\n";
1489
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001490 if (iface->isIBase()) {
Yifan Hong01e7cde2017-01-09 17:45:45 -08001491 // BnHwBase has a constructor to initialize the HidlInstrumentor
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001492 // class properly.
1493 out << klassName
1494 << "::"
1495 << klassName
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001496 << "(const ::android::sp<" << interfaceName << "> &_hidl_impl,"
1497 << " const std::string &HidlInstrumentor_package,"
1498 << " const std::string &HidlInstrumentor_interface)\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001499
1500 out.indent();
1501 out.indent();
1502
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001503 out << ": ::android::hardware::details::HidlInstrumentor("
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001504 << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n";
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001505 out.indent();
1506 out << "_hidl_mImpl = _hidl_impl;\n";
1507 out.unindent();
1508
1509 out.unindent();
1510 out.unindent();
1511 out << "}\n\n";
1512 }
1513
Yifan Hongbcffce22017-02-01 15:52:06 -08001514 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1515 if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) {
1516 return OK;
1517 }
1518 method->generateCppSignature(out, iface->getStubName());
1519 out << " ";
1520 out.block([&] {
1521 method->cppImpl(IMPL_STUB_IMPL, out);
1522 }).endl();
1523 return OK;
1524 });
Steven Moreland60818632017-02-04 00:33:42 -08001525 if (err != OK) {
1526 return err;
1527 }
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001528
Andreas Huber881227d2016-08-02 14:20:21 -07001529 out << "::android::status_t " << klassName << "::onTransact(\n";
1530
1531 out.indent();
1532 out.indent();
1533
Iliyan Malchev549e2592016-08-10 08:59:12 -07001534 out << "uint32_t _hidl_code,\n"
1535 << "const ::android::hardware::Parcel &_hidl_data,\n"
1536 << "::android::hardware::Parcel *_hidl_reply,\n"
1537 << "uint32_t _hidl_flags,\n"
1538 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001539
1540 out.unindent();
1541
Iliyan Malchev549e2592016-08-10 08:59:12 -07001542 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -07001543 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001544 out.indent();
1545
Yifan Hong10fe0b52016-10-19 14:20:17 -07001546 for (const auto &tuple : iface->allMethodsFromRoot()) {
1547 const Method *method = tuple.method();
1548 const Interface *superInterface = tuple.interface();
1549 out << "case "
1550 << method->getSerialId()
1551 << " /* "
1552 << method->name()
1553 << " */:\n{\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001554
Yifan Hong10fe0b52016-10-19 14:20:17 -07001555 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001556
Yifan Hong10fe0b52016-10-19 14:20:17 -07001557 status_t err =
1558 generateStubSourceForMethod(out, superInterface, method);
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001559
Yifan Hong10fe0b52016-10-19 14:20:17 -07001560 if (err != OK) {
1561 return err;
Andreas Huber881227d2016-08-02 14:20:21 -07001562 }
Yifan Hong10fe0b52016-10-19 14:20:17 -07001563
1564 out.unindent();
1565 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001566 }
1567
1568 out << "default:\n{\n";
1569 out.indent();
1570
Martijn Coenen225bc922017-06-27 14:39:46 -07001571 if (iface->isIBase()) {
1572 out << "(void)_hidl_flags;\n";
1573 out << "return ::android::UNKNOWN_TRANSACTION;\n";
1574 } else {
1575 out << "return ";
1576 out << gIBaseFqName.getInterfaceStubFqName().cppName();
1577 out << "::onTransact(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001578
Martijn Coenen225bc922017-06-27 14:39:46 -07001579 out.indent();
1580 out.indent();
Andreas Huber881227d2016-08-02 14:20:21 -07001581
Martijn Coenen225bc922017-06-27 14:39:46 -07001582 out << "_hidl_code, _hidl_data, _hidl_reply, "
1583 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001584
Martijn Coenen225bc922017-06-27 14:39:46 -07001585 out.unindent();
1586 out.unindent();
1587 }
Andreas Huber881227d2016-08-02 14:20:21 -07001588
1589 out.unindent();
1590 out << "}\n";
1591
1592 out.unindent();
1593 out << "}\n\n";
1594
Yifan Honga018ed52016-12-13 16:35:08 -08001595 out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] {
1596 out << "_hidl_err = ::android::hardware::writeToParcel(\n";
1597 out.indent(2, [&] {
1598 out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n";
1599 out << "_hidl_reply);\n";
1600 });
1601 });
Andreas Huber881227d2016-08-02 14:20:21 -07001602
Iliyan Malchev549e2592016-08-10 08:59:12 -07001603 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001604
1605 out.unindent();
1606 out << "}\n\n";
1607
1608 return OK;
1609}
1610
1611status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001612 Formatter &out, const Interface *iface, const Method *method) const {
Martijn Coenen115d4282016-12-19 05:14:04 +01001613 if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) {
1614 method->cppImpl(IMPL_STUB, out);
1615 out << "break;\n";
1616 return OK;
1617 }
1618
Yifan Hongeefe4f22017-01-04 15:32:42 -08001619 out << "if (!_hidl_data.enforceInterface("
1620 << iface->fullName()
1621 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001622
Andreas Huber881227d2016-08-02 14:20:21 -07001623 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001624 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001625 out << "break;\n";
1626 out.unindent();
1627 out << "}\n\n";
1628
Andreas Huber5e44a292016-09-27 14:52:39 -07001629 declareCppReaderLocals(out, method->args(), false /* forResults */);
Andreas Hubere7ff2282016-08-16 13:50:03 -07001630
Yifan Hongbf459bc2016-08-23 16:50:37 -07001631 // First DFS: write buffers
Andreas Huber881227d2016-08-02 14:20:21 -07001632 for (const auto &arg : method->args()) {
1633 emitCppReaderWriter(
1634 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001635 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001636 false /* parcelObjIsPointer */,
1637 arg,
1638 true /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001639 Type::ErrorMode_Break,
1640 false /* addPrefixToName */);
Andreas Huber881227d2016-08-02 14:20:21 -07001641 }
1642
Yifan Hongbf459bc2016-08-23 16:50:37 -07001643 // Second DFS: resolve references
1644 for (const auto &arg : method->args()) {
1645 emitCppResolveReferences(
1646 out,
1647 "_hidl_data",
1648 false /* parcelObjIsPointer */,
1649 arg,
1650 true /* reader */,
1651 Type::ErrorMode_Break,
1652 false /* addPrefixToName */);
1653 }
1654
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001655 status_t status = generateCppInstrumentationCall(
1656 out,
1657 InstrumentationEvent::SERVER_API_ENTRY,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001658 method);
1659 if (status != OK) {
1660 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001661 }
1662
Andreas Huber881227d2016-08-02 14:20:21 -07001663 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001664 const TypedVar *elidedReturn = method->canElideCallback();
Yifan Hongcd2ae452017-01-31 14:33:40 -08001665 const std::string callee =
1666 (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL))
1667 ? "this" : "_hidl_mImpl";
Andreas Huber881227d2016-08-02 14:20:21 -07001668
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001669 if (elidedReturn != nullptr) {
Yifan Hong3b320f82016-11-01 15:15:54 -07001670 out << elidedReturn->type().getCppResultType()
Yifan Honga47eef32016-12-12 10:38:54 -08001671 << " _hidl_out_"
Yifan Hong3b320f82016-11-01 15:15:54 -07001672 << elidedReturn->name()
Martijn Coenen6ec2f0b2016-12-11 01:04:55 +01001673 << " = "
Yifan Hongcd2ae452017-01-31 14:33:40 -08001674 << callee << "->" << method->name()
Yifan Hong3b320f82016-11-01 15:15:54 -07001675 << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001676
Yifan Hong932464e2017-03-30 15:40:22 -07001677 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001678 if (arg->type().resultNeedsDeref()) {
1679 out << "*";
1680 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001681 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001682 });
Andreas Huber881227d2016-08-02 14:20:21 -07001683
Steven Moreland2ae5bca2016-12-01 05:56:49 +00001684 out << ");\n\n";
Yifan Hong859e53f2016-11-14 19:08:24 -08001685 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1686 << "_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001687
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001688 elidedReturn->type().emitReaderWriter(
1689 out,
Yifan Honga47eef32016-12-12 10:38:54 -08001690 "_hidl_out_" + elidedReturn->name(),
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001691 "_hidl_reply",
1692 true, /* parcelObjIsPointer */
1693 false, /* isReader */
1694 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001695
Yifan Hongbf459bc2016-08-23 16:50:37 -07001696 emitCppResolveReferences(
1697 out,
1698 "_hidl_reply",
1699 true /* parcelObjIsPointer */,
1700 elidedReturn,
1701 false /* reader */,
1702 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001703 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001704
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001705 status_t status = generateCppInstrumentationCall(
1706 out,
1707 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001708 method);
1709 if (status != OK) {
1710 return status;
1711 }
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001712
Iliyan Malchev549e2592016-08-10 08:59:12 -07001713 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001714 } else {
1715 if (returnsValue) {
1716 out << "bool _hidl_callbackCalled = false;\n\n";
1717 }
Andreas Huber881227d2016-08-02 14:20:21 -07001718
Yifan Hongcd2ae452017-01-31 14:33:40 -08001719 out << callee << "->" << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001720
Yifan Hong932464e2017-03-30 15:40:22 -07001721 out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001722 if (arg->type().resultNeedsDeref()) {
1723 out << "*";
1724 }
1725
1726 out << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001727 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001728
1729 if (returnsValue) {
Yifan Hong932464e2017-03-30 15:40:22 -07001730 if (!method->args().empty()) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001731 out << ", ";
1732 }
1733
1734 out << "[&](";
1735
Yifan Hong932464e2017-03-30 15:40:22 -07001736 out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) {
Yifan Honga47eef32016-12-12 10:38:54 -08001737 out << "const auto &_hidl_out_" << arg->name();
Yifan Hong932464e2017-03-30 15:40:22 -07001738 });
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001739
1740 out << ") {\n";
1741 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001742 out << "if (_hidl_callbackCalled) {\n";
1743 out.indent();
1744 out << "LOG_ALWAYS_FATAL(\""
1745 << method->name()
1746 << ": _hidl_cb called a second time, but must be called once.\");\n";
1747 out.unindent();
1748 out << "}\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001749 out << "_hidl_callbackCalled = true;\n\n";
1750
Yifan Hong859e53f2016-11-14 19:08:24 -08001751 out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), "
1752 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001753
Yifan Hongbf459bc2016-08-23 16:50:37 -07001754 // First DFS: buffers
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001755 for (const auto &arg : method->results()) {
1756 emitCppReaderWriter(
1757 out,
1758 "_hidl_reply",
1759 true /* parcelObjIsPointer */,
1760 arg,
1761 false /* reader */,
Andreas Huber5e44a292016-09-27 14:52:39 -07001762 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001763 true /* addPrefixToName */);
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001764 }
1765
Yifan Hongbf459bc2016-08-23 16:50:37 -07001766 // Second DFS: resolve references
1767 for (const auto &arg : method->results()) {
1768 emitCppResolveReferences(
1769 out,
1770 "_hidl_reply",
1771 true /* parcelObjIsPointer */,
1772 arg,
1773 false /* reader */,
1774 Type::ErrorMode_Ignore,
Yifan Honga47eef32016-12-12 10:38:54 -08001775 true /* addPrefixToName */);
Yifan Hongbf459bc2016-08-23 16:50:37 -07001776 }
1777
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001778 status_t status = generateCppInstrumentationCall(
1779 out,
1780 InstrumentationEvent::SERVER_API_EXIT,
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07001781 method);
1782 if (status != OK) {
1783 return status;
Zhuoyao Zhangde578002016-09-07 18:24:17 -07001784 }
1785
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001786 out << "_hidl_cb(*_hidl_reply);\n";
1787
1788 out.unindent();
Martijn Coenen8e4fc842017-01-09 16:28:59 +01001789 out << "});\n\n";
1790 } else {
1791 out << ");\n\n";
1792 status_t status = generateCppInstrumentationCall(
1793 out,
1794 InstrumentationEvent::SERVER_API_EXIT,
1795 method);
1796 if (status != OK) {
1797 return status;
1798 }
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001799 }
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001800
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001801 if (returnsValue) {
1802 out << "if (!_hidl_callbackCalled) {\n";
1803 out.indent();
Steven Moreland05cd4232016-11-21 16:01:12 -08001804 out << "LOG_ALWAYS_FATAL(\""
1805 << method->name()
1806 << ": _hidl_cb not called, but must be called once.\");\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001807 out.unindent();
1808 out << "}\n\n";
Steven Moreland05cd4232016-11-21 16:01:12 -08001809 } else {
1810 out << "::android::hardware::writeToParcel("
1811 << "::android::hardware::Status::ok(), "
1812 << "_hidl_reply);\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001813 }
Andreas Huber881227d2016-08-02 14:20:21 -07001814 }
1815
1816 out << "break;\n";
1817
1818 return OK;
1819}
1820
Steven Moreland69e7c702016-09-09 11:16:32 -07001821status_t AST::generatePassthroughHeader(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -07001822 if (!AST::isInterface()) {
Steven Moreland69e7c702016-09-09 11:16:32 -07001823 // types.hal does not get a stub header.
1824 return OK;
1825 }
1826
1827 const Interface *iface = mRootScope->getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -07001828 CHECK(iface != nullptr);
Steven Moreland69e7c702016-09-09 11:16:32 -07001829
Yifan Hongeefe4f22017-01-04 15:32:42 -08001830 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001831
1832 bool supportOneway = iface->hasOnewayMethods();
1833
1834 std::string path = outputPath;
1835 path.append(mCoordinator->convertPackageRootToPath(mPackage));
1836 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
1837 path.append(klassName);
1838 path.append(".h");
1839
1840 CHECK(Coordinator::MakeParentHierarchy(path));
1841 FILE *file = fopen(path.c_str(), "w");
1842
1843 if (file == NULL) {
1844 return -errno;
1845 }
1846
1847 Formatter out(file);
1848
1849 const std::string guard = makeHeaderGuard(klassName);
1850
1851 out << "#ifndef " << guard << "\n";
1852 out << "#define " << guard << "\n\n";
1853
1854 std::vector<std::string> packageComponents;
1855 getPackageAndVersionComponents(
1856 &packageComponents, false /* cpp_compatible */);
1857
Steven Moreland61d3f4b2017-04-28 17:30:38 -07001858 out << "#include <android-base/macros.h>\n";
Yifan Hongb0949432016-12-15 15:32:24 -08001859 out << "#include <cutils/trace.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001860 out << "#include <future>\n";
Steven Morelandee88eed2016-10-31 17:49:00 -07001861
Steven Moreland19f11b52017-05-12 18:22:21 -07001862 generateCppPackageInclude(out, mPackage, iface->localName());
Steven Morelandee88eed2016-10-31 17:49:00 -07001863 out << "\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001864
Yifan Hong7a118f52016-12-07 11:21:15 -08001865 out << "#include <hidl/HidlPassthroughSupport.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001866 if (supportOneway) {
Yifan Hong2cbc1472016-10-25 19:02:40 -07001867 out << "#include <hidl/TaskRunner.h>\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001868 }
1869
1870 enterLeaveNamespace(out, true /* enter */);
1871 out << "\n";
1872
1873 out << "struct "
1874 << klassName
Steven Moreland19f11b52017-05-12 18:22:21 -07001875 << " : " << iface->localName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001876 << ", ::android::hardware::details::HidlInstrumentor {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001877
1878 out.indent();
1879 out << "explicit "
1880 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001881 << "(const ::android::sp<"
Steven Moreland19f11b52017-05-12 18:22:21 -07001882 << iface->localName()
Steven Moreland69e7c702016-09-09 11:16:32 -07001883 << "> impl);\n";
1884
Steven Moreland0b843772017-06-23 16:33:38 -07001885 out.endl();
1886 generateTemplatizationLink(out);
1887
Yifan Hong068c5522016-10-31 14:07:25 -07001888 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1889 return generatePassthroughMethod(out, method);
1890 });
Steven Moreland69e7c702016-09-09 11:16:32 -07001891
1892 if (err != OK) {
1893 return err;
1894 }
1895
1896 out.unindent();
1897 out << "private:\n";
1898 out.indent();
Steven Moreland19f11b52017-05-12 18:22:21 -07001899 out << "const ::android::sp<" << iface->localName() << "> mImpl;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001900
1901 if (supportOneway) {
Yifan Hongef91d362017-03-20 17:18:13 -07001902 out << "::android::hardware::details::TaskRunner mOnewayQueue;\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001903
1904 out << "\n";
1905
1906 out << "::android::hardware::Return<void> addOnewayTask("
1907 "std::function<void(void)>);\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07001908 }
1909
1910 out.unindent();
1911
1912 out << "};\n\n";
1913
1914 enterLeaveNamespace(out, false /* enter */);
1915
1916 out << "\n#endif // " << guard << "\n";
1917
1918 return OK;
1919}
1920
Yifan Hongfe95aa22016-10-19 17:26:45 -07001921status_t AST::generateInterfaceSource(Formatter &out) const {
1922 const Interface *iface = mRootScope->getInterface();
1923
Yifan Hong2d7126b2016-10-20 15:12:57 -07001924 // generate castFrom functions
Yifan Hong3d746092016-12-07 14:26:33 -08001925 std::string childTypeResult = iface->getCppResultType();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001926
Steven Morelandd4b068a2017-03-20 06:30:51 -07001927 status_t err = generateMethods(out, [&](const Method *method, const Interface *) {
1928 bool reserved = method->isHidlReserved();
1929
1930 if (!reserved) {
1931 out << "// no default implementation for: ";
1932 }
1933 method->generateCppSignature(out, iface->localName());
1934 if (reserved) {
1935 out.block([&]() {
Steven Moreland937408a2017-03-20 09:54:18 -07001936 method->cppImpl(IMPL_INTERFACE, out);
Steven Morelandd4b068a2017-03-20 06:30:51 -07001937 }).endl();
1938 }
1939
1940 out << "\n";
1941
1942 return OK;
1943 });
1944 if (err != OK) {
1945 return err;
1946 }
1947
Yifan Hong3d746092016-12-07 14:26:33 -08001948 for (const Interface *superType : iface->typeChain()) {
Yifan Hong200209c2017-03-29 03:39:09 -07001949 out << "// static \n::android::hardware::Return<"
Yifan Hong3d746092016-12-07 14:26:33 -08001950 << childTypeResult
Yifan Hong200209c2017-03-29 03:39:09 -07001951 << "> "
Yifan Hongeefe4f22017-01-04 15:32:42 -08001952 << iface->localName()
Yifan Hong3d746092016-12-07 14:26:33 -08001953 << "::castFrom("
1954 << superType->getCppArgumentType()
Yifan Hong200209c2017-03-29 03:39:09 -07001955 << " parent, bool "
1956 << (iface == superType ? "/* emitError */" : "emitError")
1957 << ") {\n";
Yifan Hong3d746092016-12-07 14:26:33 -08001958 out.indent();
1959 if (iface == superType) {
1960 out << "return parent;\n";
1961 } else {
Yifan Hong33e78012017-03-13 17:46:33 -07001962 out << "return ::android::hardware::details::castInterface<";
Yifan Hongeefe4f22017-01-04 15:32:42 -08001963 out << iface->localName() << ", "
Yifan Hongfe95aa22016-10-19 17:26:45 -07001964 << superType->fqName().cppName() << ", "
Steven Morelanda8559fc2017-07-21 19:13:06 +00001965 << iface->getProxyName() << ", "
1966 << superType->getProxyFqName().cppName()
Yifan Hongfe95aa22016-10-19 17:26:45 -07001967 << ">(\n";
1968 out.indent();
1969 out.indent();
1970 out << "parent, \""
1971 << iface->fqName().string()
Yifan Hong200209c2017-03-29 03:39:09 -07001972 << "\", emitError);\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001973 out.unindent();
1974 out.unindent();
Yifan Hongfe95aa22016-10-19 17:26:45 -07001975 }
Yifan Hong3d746092016-12-07 14:26:33 -08001976 out.unindent();
1977 out << "}\n\n";
Yifan Hongfe95aa22016-10-19 17:26:45 -07001978 }
1979
1980 return OK;
1981}
1982
Steven Moreland69e7c702016-09-09 11:16:32 -07001983status_t AST::generatePassthroughSource(Formatter &out) const {
1984 const Interface *iface = mRootScope->getInterface();
1985
Yifan Hongeefe4f22017-01-04 15:32:42 -08001986 const std::string klassName = iface->getPassthroughName();
Steven Moreland69e7c702016-09-09 11:16:32 -07001987
1988 out << klassName
1989 << "::"
1990 << klassName
Steven Morelandc46e9842016-11-02 13:21:26 -07001991 << "(const ::android::sp<"
Steven Moreland69e7c702016-09-09 11:16:32 -07001992 << iface->fullName()
Zhuoyao Zhang7d3ac802017-02-15 21:05:49 +00001993 << "> impl) : ::android::hardware::details::HidlInstrumentor(\""
Zhuoyao Zhangd10feea2017-01-23 17:29:58 -08001994 << mPackage.string()
1995 << "\", \""
1996 << iface->localName()
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07001997 << "\"), mImpl(impl) {";
Yifan Hong2cbc1472016-10-25 19:02:40 -07001998 if (iface->hasOnewayMethods()) {
1999 out << "\n";
Yifan Hong33223ca2016-12-13 15:07:35 -08002000 out.indent([&] {
Yifan Hongf01dad42017-03-20 19:03:11 -07002001 out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n";
Yifan Hong2cbc1472016-10-25 19:02:40 -07002002 });
2003 }
2004 out << "}\n\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002005
2006 if (iface->hasOnewayMethods()) {
2007 out << "::android::hardware::Return<void> "
2008 << klassName
2009 << "::addOnewayTask(std::function<void(void)> fun) {\n";
2010 out.indent();
Yifan Hong2cbc1472016-10-25 19:02:40 -07002011 out << "if (!mOnewayQueue.push(fun)) {\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002012 out.indent();
Steven Moreland67f67b42016-09-29 08:59:02 -07002013 out << "return ::android::hardware::Status::fromExceptionCode(\n";
2014 out.indent();
2015 out.indent();
Steven Moreland610002f2017-06-16 13:02:49 -07002016 out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n"
2017 << "\"Passthrough oneway function queue exceeds maximum size.\");\n";
Steven Moreland67f67b42016-09-29 08:59:02 -07002018 out.unindent();
2019 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002020 out.unindent();
Steven Moreland69e7c702016-09-09 11:16:32 -07002021 out << "}\n";
2022
Steven Morelandd366c262016-10-11 15:29:10 -07002023 out << "return ::android::hardware::Status();\n";
Steven Moreland69e7c702016-09-09 11:16:32 -07002024
2025 out.unindent();
2026 out << "}\n\n";
2027
2028
2029 }
2030
2031 return OK;
2032}
2033
Martijn Coenen7b295242016-11-04 16:52:56 +01002034status_t AST::generateCppAtraceCall(Formatter &out,
2035 InstrumentationEvent event,
2036 const Method *method) const {
2037 const Interface *iface = mRootScope->getInterface();
Yifan Hongeefe4f22017-01-04 15:32:42 -08002038 std::string baseString = "HIDL::" + iface->localName() + "::" + method->name();
Martijn Coenen7b295242016-11-04 16:52:56 +01002039 switch (event) {
2040 case SERVER_API_ENTRY:
2041 {
2042 out << "atrace_begin(ATRACE_TAG_HAL, \""
2043 << baseString + "::server\");\n";
2044 break;
2045 }
2046 case CLIENT_API_ENTRY:
2047 {
2048 out << "atrace_begin(ATRACE_TAG_HAL, \""
2049 << baseString + "::client\");\n";
2050 break;
2051 }
2052 case PASSTHROUGH_ENTRY:
2053 {
2054 out << "atrace_begin(ATRACE_TAG_HAL, \""
2055 << baseString + "::passthrough\");\n";
2056 break;
2057 }
2058 case SERVER_API_EXIT:
2059 case CLIENT_API_EXIT:
2060 case PASSTHROUGH_EXIT:
2061 {
2062 out << "atrace_end(ATRACE_TAG_HAL);\n";
2063 break;
2064 }
2065 default:
2066 {
2067 LOG(ERROR) << "Unsupported instrumentation event: " << event;
2068 return UNKNOWN_ERROR;
2069 }
2070 }
2071
2072 return OK;
2073}
2074
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002075status_t AST::generateCppInstrumentationCall(
2076 Formatter &out,
2077 InstrumentationEvent event,
Steven Moreland031ccf12016-10-31 15:54:38 -07002078 const Method *method) const {
Martijn Coenen7b295242016-11-04 16:52:56 +01002079 status_t err = generateCppAtraceCall(out, event, method);
2080 if (err != OK) {
2081 return err;
2082 }
2083
Steven Moreland30b76e92017-06-02 18:52:24 -07002084 out << "#ifdef __ANDROID_DEBUGGABLE__\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002085 out << "if (UNLIKELY(mEnableInstrumentation)) {\n";
2086 out.indent();
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002087 out << "std::vector<void *> _hidl_args;\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002088 std::string event_str = "";
2089 switch (event) {
2090 case SERVER_API_ENTRY:
2091 {
2092 event_str = "InstrumentationEvent::SERVER_API_ENTRY";
2093 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002094 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002095 << (arg->type().resultNeedsDeref() ? "" : "&")
2096 << arg->name()
2097 << ");\n";
2098 }
2099 break;
2100 }
2101 case SERVER_API_EXIT:
2102 {
2103 event_str = "InstrumentationEvent::SERVER_API_EXIT";
Steven Moreland031ccf12016-10-31 15:54:38 -07002104 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002105 out << "_hidl_args.push_back((void *)&_hidl_out_"
Steven Moreland031ccf12016-10-31 15:54:38 -07002106 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002107 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002108 }
2109 break;
2110 }
2111 case CLIENT_API_ENTRY:
2112 {
2113 event_str = "InstrumentationEvent::CLIENT_API_ENTRY";
2114 for (const auto &arg : method->args()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002115 out << "_hidl_args.push_back((void *)&"
2116 << arg->name()
2117 << ");\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002118 }
2119 break;
2120 }
2121 case CLIENT_API_EXIT:
2122 {
2123 event_str = "InstrumentationEvent::CLIENT_API_EXIT";
2124 for (const auto &arg : method->results()) {
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002125 out << "_hidl_args.push_back((void *)"
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002126 << (arg->type().resultNeedsDeref() ? "" : "&")
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002127 << "_hidl_out_"
2128 << arg->name()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002129 << ");\n";
2130 }
2131 break;
2132 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002133 case PASSTHROUGH_ENTRY:
2134 {
2135 event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY";
2136 for (const auto &arg : method->args()) {
2137 out << "_hidl_args.push_back((void *)&"
2138 << arg->name()
2139 << ");\n";
2140 }
2141 break;
2142 }
2143 case PASSTHROUGH_EXIT:
2144 {
2145 event_str = "InstrumentationEvent::PASSTHROUGH_EXIT";
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002146 for (const auto &arg : method->results()) {
Yifan Honga47eef32016-12-12 10:38:54 -08002147 out << "_hidl_args.push_back((void *)&_hidl_out_"
Zhuoyao Zhang085a8c32016-11-17 15:35:49 -08002148 << arg->name()
2149 << ");\n";
2150 }
Steven Moreland9b1cbdf2016-11-01 12:23:27 -07002151 break;
2152 }
Steven Moreland031ccf12016-10-31 15:54:38 -07002153 default:
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002154 {
Steven Moreland031ccf12016-10-31 15:54:38 -07002155 LOG(ERROR) << "Unsupported instrumentation event: " << event;
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002156 return UNKNOWN_ERROR;
2157 }
2158 }
2159
Steven Moreland031ccf12016-10-31 15:54:38 -07002160 const Interface *iface = mRootScope->getInterface();
2161
Steven Moreland1ab31442016-11-03 18:37:51 -07002162 out << "for (const auto &callback: mInstrumentationCallbacks) {\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002163 out.indent();
2164 out << "callback("
2165 << event_str
2166 << ", \""
2167 << mPackage.package()
2168 << "\", \""
Yifan Hong90ea87f2016-11-01 14:25:47 -07002169 << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002170 << "\", \""
2171 << iface->localName()
2172 << "\", \""
2173 << method->name()
Zhuoyao Zhang964f72f2016-10-21 11:12:03 -07002174 << "\", &_hidl_args);\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002175 out.unindent();
2176 out << "}\n";
2177 out.unindent();
Steven Moreland30b76e92017-06-02 18:52:24 -07002178 out << "}\n";
2179 out << "#endif // __ANDROID_DEBUGGABLE__\n\n";
Zhuoyao Zhang8f492942016-09-28 14:27:56 -07002180
2181 return OK;
2182}
2183
Andreas Huber881227d2016-08-02 14:20:21 -07002184} // namespace android