blob: 3668fd6fff77212a35a8588d9ef4904b635306ca [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 "Formatter.h"
22#include "Interface.h"
23#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>
Andreas Huber881227d2016-08-02 14:20:21 -070028#include <android-base/logging.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070029#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070030#include <vector>
31
32namespace android {
33
Andreas Huber881227d2016-08-02 14:20:21 -070034static std::string upcase(const std::string in) {
35 std::string out{in};
36
37 for (auto &ch : out) {
38 ch = toupper(ch);
39 }
40
41 return out;
42}
43
Andreas Huberb82318c2016-08-02 14:45:54 -070044status_t AST::generateCpp(const std::string &outputPath) const {
45 status_t err = generateInterfaceHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070046
47 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070048 err = generateStubHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070049 }
50
51 if (err == OK) {
Steven Moreland40786312016-08-16 10:29:40 -070052 err = generateHwBinderHeader(outputPath);
53 }
54
55 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070056 err = generateProxyHeader(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070057 }
58
59 if (err == OK) {
Andreas Huberb82318c2016-08-02 14:45:54 -070060 err = generateAllSource(outputPath);
Andreas Huber881227d2016-08-02 14:20:21 -070061 }
62
63 return err;
64}
65
Andreas Huber737080b2016-08-02 15:38:04 -070066void AST::getPackageComponents(
67 std::vector<std::string> *components) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070068 mPackage.getPackageComponents(components);
Andreas Huber737080b2016-08-02 15:38:04 -070069}
70
71void AST::getPackageAndVersionComponents(
72 std::vector<std::string> *components, bool cpp_compatible) const {
Andreas Huber0e00de42016-08-03 09:56:02 -070073 mPackage.getPackageAndVersionComponents(components, cpp_compatible);
Andreas Huber737080b2016-08-02 15:38:04 -070074}
75
Andreas Huber881227d2016-08-02 14:20:21 -070076std::string AST::makeHeaderGuard(const std::string &baseName) const {
77 std::vector<std::string> packageComponents;
78 getPackageAndVersionComponents(
79 &packageComponents, true /* cpp_compatible */);
80
81 std::string guard = "HIDL_GENERATED";
82 for (const auto &component : packageComponents) {
83 guard += "_";
84 guard += component;
85 }
86
87 guard += "_";
88 guard += baseName;
89 guard += "_H_";
90
91 return guard;
92}
93
94void AST::enterLeaveNamespace(Formatter &out, bool enter) const {
95 std::vector<std::string> packageComponents;
96 getPackageAndVersionComponents(
97 &packageComponents, true /* cpp_compatible */);
98
99 if (enter) {
100 for (const auto &component : packageComponents) {
101 out << "namespace " << component << " {\n";
102 }
Andreas Huber0e00de42016-08-03 09:56:02 -0700103
Andreas Huber2831d512016-08-15 09:33:47 -0700104 out.setNamespace(mPackage.cppNamespace() + "::");
Andreas Huber881227d2016-08-02 14:20:21 -0700105 } else {
Andreas Huber0e00de42016-08-03 09:56:02 -0700106 out.setNamespace(std::string());
107
Andreas Huber881227d2016-08-02 14:20:21 -0700108 for (auto it = packageComponents.rbegin();
109 it != packageComponents.rend();
110 ++it) {
111 out << "} // namespace " << *it << "\n";
112 }
113 }
114}
115
Andreas Huberb82318c2016-08-02 14:45:54 -0700116status_t AST::generateInterfaceHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700117
Andreas Huberb82318c2016-08-02 14:45:54 -0700118 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700119 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700120 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700121
122 std::string ifaceName;
123 bool isInterface = true;
124 if (!AST::isInterface(&ifaceName)) {
125 ifaceName = "types";
126 isInterface = false;
127 }
128 path.append(ifaceName);
129 path.append(".h");
130
Andreas Huberd2943e12016-08-05 11:59:31 -0700131 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700132 FILE *file = fopen(path.c_str(), "w");
133
134 if (file == NULL) {
135 return -errno;
136 }
137
138 Formatter out(file);
139
140 const std::string guard = makeHeaderGuard(ifaceName);
141
142 out << "#ifndef " << guard << "\n";
143 out << "#define " << guard << "\n\n";
144
Andreas Huber737080b2016-08-02 15:38:04 -0700145 for (const auto &item : mImportedNames) {
146 out << "#include <";
147
148 std::vector<std::string> components;
Andreas Huber0e00de42016-08-03 09:56:02 -0700149 item.getPackageAndVersionComponents(
150 &components, false /* cpp_compatible */);
Andreas Huber737080b2016-08-02 15:38:04 -0700151
152 for (const auto &component : components) {
153 out << component << "/";
154 }
155
156 out << item.name()
157 << ".h>\n";
158 }
159
160 if (!mImportedNames.empty()) {
161 out << "\n";
162 }
163
Martijn Coenen7473fab2016-08-19 14:05:40 +0200164 out << "#include <hidl/HidlSupport.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700165 out << "#include <hidl/IServiceManager.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700166
167 if (isInterface) {
Martijn Coenen93915102016-09-01 01:35:52 +0200168 out << "#include <hidl/Status.h>\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700169 }
170
171 out << "#include <utils/NativeHandle.h>\n\n";
172
173 enterLeaveNamespace(out, true /* enter */);
174 out << "\n";
175
Steven Moreland40786312016-08-16 10:29:40 -0700176 // cut off the leading 'I'.
177 const std::string baseName = ifaceName.substr(1);
178
Andreas Huber881227d2016-08-02 14:20:21 -0700179 if (isInterface) {
180 out << "struct "
Steven Moreland40786312016-08-16 10:29:40 -0700181 << ifaceName;
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700182
183 const Interface *iface = mRootScope->getInterface();
184 const Interface *superType = iface->superType();
185
Steven Moreland40786312016-08-16 10:29:40 -0700186 if (superType == NULL) {
187 out << " : virtual public RefBase";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700188 } else {
Steven Moreland40786312016-08-16 10:29:40 -0700189 out << " : public "
190 << superType->fullName();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700191 }
192
193 out << " {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700194
195 out.indent();
196
Andreas Huber881227d2016-08-02 14:20:21 -0700197 }
198
199 status_t err = emitTypeDeclarations(out);
200
201 if (err != OK) {
202 return err;
203 }
204
205 if (isInterface) {
206 const Interface *iface = mRootScope->getInterface();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700207 const Interface *superType = iface->superType();
Andreas Huber881227d2016-08-02 14:20:21 -0700208
Iliyan Malchev795dd432016-09-02 11:48:26 -0700209 out << "virtual bool isRemote() const { return false; }\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700210 bool haveCallbacks = false;
211 for (const auto &method : iface->methods()) {
212 const bool returnsValue = !method->results().empty();
213
214 if (!returnsValue) {
215 continue;
216 }
217
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700218 if (method->canElideCallback() != nullptr) {
219 continue;
220 }
221
Andreas Huber881227d2016-08-02 14:20:21 -0700222 haveCallbacks = true;
223
224 out << "using "
225 << method->name()
226 << "_cb = std::function<void("
Steven Moreland979e0992016-09-07 09:18:08 -0700227 << Method::GetArgSignature(method->results(),
228 true /* specify namespaces */)
Andreas Huber881227d2016-08-02 14:20:21 -0700229 << ")>;\n";
230 }
231
232 if (haveCallbacks) {
233 out << "\n";
234 }
235
236 for (const auto &method : iface->methods()) {
237 const bool returnsValue = !method->results().empty();
238
Andreas Huber3599d922016-08-09 10:42:57 -0700239 method->dumpAnnotations(out);
240
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700241 const TypedVar *elidedReturn = method->canElideCallback();
242 if (elidedReturn) {
243 std::string extra;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700244 out << "virtual ::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700245 out << elidedReturn->type().getCppResultType(&extra) << "> ";
246 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700247 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700248 }
249
250 out << method->name()
Andreas Huber881227d2016-08-02 14:20:21 -0700251 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700252 << Method::GetArgSignature(method->args(),
253 true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700254
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700255 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700256 if (!method->args().empty()) {
257 out << ", ";
258 }
259
Iliyan Malchev549e2592016-08-10 08:59:12 -0700260 out << method->name() << "_cb _hidl_cb = nullptr";
Andreas Huber881227d2016-08-02 14:20:21 -0700261 }
262
263 out << ") = 0;\n";
264 }
Steven Moreland40786312016-08-16 10:29:40 -0700265
266 out << "DECLARE_REGISTER_AND_GET_SERVICE(" << baseName << ")\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700267 }
268
269 if (isInterface) {
270 out.unindent();
271
272 out << "};\n";
273 }
274
275 out << "\n";
276 enterLeaveNamespace(out, false /* enter */);
277
278 out << "\n#endif // " << guard << "\n";
279
280 return OK;
281}
282
Steven Moreland40786312016-08-16 10:29:40 -0700283status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
284 std::string ifaceName;
285 if(!AST::isInterface(&ifaceName)) {
286 // types.hal does not get an HwBinder header.
287 return OK;
288 }
289
290 // cut off the leading 'I'.
291 const std::string baseName = ifaceName.substr(1);
292
293 const std::string klassName = "IHw" + baseName;
294
295 std::string path = outputPath;
296 path.append(mCoordinator->convertPackageRootToPath(mPackage));
297 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
298 path.append(klassName + ".h");
299
300 FILE* file = fopen(path.c_str(), "w");
301
302 if (file == NULL) {
303 return -errno;
304 }
305
306 Formatter out(file);
307
308 const std::string guard = makeHeaderGuard(klassName);
309
310 out << "#ifndef " << guard << "\n";
311 out << "#define " << guard << "\n\n";
312
313 std::vector<std::string> packageComponents;
314 getPackageAndVersionComponents(
315 &packageComponents, false /* cpp_compatible */);
316
317 out << "#include <";
318 for (const auto &component : packageComponents) {
319 out << component << "/";
320 }
321 out << ifaceName << ".h>\n\n";
322
323 for (const auto &item : mImportedNames) {
324 if (item.name() == "types") {
325 continue;
326 }
327
328 out << "#include <";
329
330 std::vector<std::string> components;
331 item.getPackageAndVersionComponents(
332 &components, false /* cpp_compatible */);
333
334 for (const auto &component : components) {
335 out << component << "/";
336 }
337
338 // cut off the leading I
339 const std::string itemBaseName = item.name().substr(1);
340
341 out << "Bn"
342 << itemBaseName
343 << ".h>\n";
344 }
345
346 out << "\n";
347
348 out << "#include <hidl/HidlSupport.h>\n";
Martijn Coenen93915102016-09-01 01:35:52 +0200349 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700350 out << "#include <hwbinder/IBinder.h>\n";
351 out << "#include <hwbinder/IInterface.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700352
353 out << "\n";
354
355 enterLeaveNamespace(out, true /* enter */);
356 out << "\n";
357
358 out << "struct "
359 << klassName
360 << " : public "
361 << ifaceName;
362
363 const Interface *iface = mRootScope->getInterface();
364 const Interface *superType = iface->superType();
365
366 out << ", public ::android::hardware::IInterface";
367
368 out << " {\n";
369
370 out.indent();
371
372 out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n";
373
374 out << "enum Call {\n";
375 out.indent();
376
377 bool first = true;
378 for (const auto &method : iface->methods()) {
379 out << upcase(method->name());
380
381 if (first) {
382 out << " = ";
383 if (superType != NULL) {
384 out << superType->fqName().cppNamespace()
385 << "::IHw"
386 << superType->getBaseName()
387 << "::Call::CallCount";
388 } else {
389 out << "::android::hardware::IBinder::FIRST_CALL_TRANSACTION";
390 }
391
392 first = false;
393 }
394
395 out << ",\n";
396 }
397
398 out << "CallCount\n";
399
400 out.unindent();
401 out << "};\n\n";
402
403 out.unindent();
404
405 out << "};\n\n";
406
407 enterLeaveNamespace(out, false /* enter */);
408
409 out << "\n#endif // " << guard << "\n";
410
411 return OK;
412}
413
Andreas Huber881227d2016-08-02 14:20:21 -0700414status_t AST::emitTypeDeclarations(Formatter &out) const {
415 return mRootScope->emitTypeDeclarations(out);
416}
417
Steven Morelanda7a421a2016-09-07 08:35:18 -0700418status_t AST::generateStubMethod(Formatter &out,
419 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700420 const Method *method,
421 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700422 out << "inline ";
423
Steven Moreland979e0992016-09-07 09:18:08 -0700424 method->generateCppSignature(out,
425 className,
426 specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700427
428 const bool returnsValue = !method->results().empty();
429 const TypedVar *elidedReturn = method->canElideCallback();
430 out << " {\n";
431 out.indent();
432 out << "return mImpl->"
433 << method->name()
434 << "(";
435 bool first = true;
436 for (const auto &arg : method->args()) {
437 if (!first) {
438 out << ", ";
439 }
440 first = false;
441 out << arg->name();
442 }
443 if (returnsValue && elidedReturn == nullptr) {
444 if (!method->args().empty()) {
445 out << ", ";
446 }
447
448 out << "_hidl_cb";
449 }
450 out << ");\n";
451 out.unindent();
452 out << "}";
453
454 out << ";\n";
455
456 return OK;
457}
458
459status_t AST::generateProxyMethod(Formatter &out,
460 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700461 const Method *method,
462 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700463
Steven Moreland979e0992016-09-07 09:18:08 -0700464 method->generateCppSignature(out,
465 className,
466 specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700467 out << " override;\n";
468
469 return OK;
470}
471
472status_t AST::generateMethods(
473 Formatter &out,
474 const std::string &className,
Steven Moreland979e0992016-09-07 09:18:08 -0700475 MethodLocation type,
476 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700477
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700478 const Interface *iface = mRootScope->getInterface();
479
480 std::vector<const Interface *> chain;
481 while (iface != NULL) {
482 chain.push_back(iface);
483 iface = iface->superType();
484 }
485
486 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
487 const Interface *superInterface = *it;
488
489 out << "// Methods from "
490 << superInterface->fullName()
491 << " follow.\n";
492
493 for (const auto &method : superInterface->methods()) {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700494 status_t err;
495 switch(type) {
496 case STUB_HEADER:
497 err = generateStubMethod(out,
498 className,
Steven Moreland979e0992016-09-07 09:18:08 -0700499 method,
500 specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700501 break;
502 case PROXY_HEADER:
503 err = generateProxyMethod(out,
504 className,
Steven Moreland979e0992016-09-07 09:18:08 -0700505 method,
506 specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700507 break;
508 default:
509 err = UNKNOWN_ERROR;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700510 }
511
Steven Morelanda7a421a2016-09-07 08:35:18 -0700512 if (err != OK) {
513 return err;
Steven Moreland40786312016-08-16 10:29:40 -0700514 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700515 }
516
517 out << "\n";
518 }
519
520 return OK;
521}
522
Andreas Huberb82318c2016-08-02 14:45:54 -0700523status_t AST::generateStubHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700524 std::string ifaceName;
525 if (!AST::isInterface(&ifaceName)) {
526 // types.hal does not get a stub header.
527 return OK;
528 }
529
Andreas Huber881227d2016-08-02 14:20:21 -0700530 // cut off the leading 'I'.
531 const std::string baseName = ifaceName.substr(1);
Steven Moreland40786312016-08-16 10:29:40 -0700532 const std::string klassName = "Bn" + baseName;
Andreas Huber881227d2016-08-02 14:20:21 -0700533
Andreas Huberb82318c2016-08-02 14:45:54 -0700534 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700535 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700536 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700537 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700538 path.append(".h");
539
Andreas Huberd2943e12016-08-05 11:59:31 -0700540 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700541 FILE *file = fopen(path.c_str(), "w");
542
543 if (file == NULL) {
544 return -errno;
545 }
546
547 Formatter out(file);
548
Steven Moreland40786312016-08-16 10:29:40 -0700549 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700550
551 out << "#ifndef " << guard << "\n";
552 out << "#define " << guard << "\n\n";
553
554 std::vector<std::string> packageComponents;
555 getPackageAndVersionComponents(
556 &packageComponents, false /* cpp_compatible */);
557
558 out << "#include <";
559 for (const auto &component : packageComponents) {
560 out << component << "/";
561 }
Steven Moreland40786312016-08-16 10:29:40 -0700562 out << "IHw" << baseName << ".h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700563
564 enterLeaveNamespace(out, true /* enter */);
565 out << "\n";
566
567 out << "struct "
568 << "Bn"
569 << baseName
Steven Moreland40786312016-08-16 10:29:40 -0700570 << " : public ::android::hardware::BnInterface<I"
571 << baseName << ", IHw" << baseName
Andreas Huber881227d2016-08-02 14:20:21 -0700572 << "> {\n";
573
574 out.indent();
Steven Moreland40786312016-08-16 10:29:40 -0700575 out << "explicit Bn"
576 << baseName
577 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);"
578 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700579 out << "::android::status_t onTransact(\n";
580 out.indent();
581 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700582 out << "uint32_t _hidl_code,\n";
583 out << "const ::android::hardware::Parcel &_hidl_data,\n";
584 out << "::android::hardware::Parcel *_hidl_reply,\n";
585 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700586 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700587 out.unindent();
588 out.unindent();
589
Steven Morelanda7a421a2016-09-07 08:35:18 -0700590 generateMethods(out,
591 "" /* class name */,
Steven Moreland979e0992016-09-07 09:18:08 -0700592 MethodLocation::STUB_HEADER,
593 true /* specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700594 out.unindent();
595
596 out << "};\n\n";
597
598 enterLeaveNamespace(out, false /* enter */);
599
600 out << "\n#endif // " << guard << "\n";
601
602 return OK;
603}
604
Andreas Huberb82318c2016-08-02 14:45:54 -0700605status_t AST::generateProxyHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700606 std::string ifaceName;
607 if (!AST::isInterface(&ifaceName)) {
608 // types.hal does not get a proxy header.
609 return OK;
610 }
611
Andreas Huber881227d2016-08-02 14:20:21 -0700612 // cut off the leading 'I'.
613 const std::string baseName = ifaceName.substr(1);
614
Andreas Huberb82318c2016-08-02 14:45:54 -0700615 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700616 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700617 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700618 path.append("Bp");
619 path.append(baseName);
620 path.append(".h");
621
Andreas Huberd2943e12016-08-05 11:59:31 -0700622 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700623 FILE *file = fopen(path.c_str(), "w");
624
625 if (file == NULL) {
626 return -errno;
627 }
628
629 Formatter out(file);
630
631 const std::string guard = makeHeaderGuard("Bp" + baseName);
632
633 out << "#ifndef " << guard << "\n";
634 out << "#define " << guard << "\n\n";
635
636 std::vector<std::string> packageComponents;
637 getPackageAndVersionComponents(
638 &packageComponents, false /* cpp_compatible */);
639
640 out << "#include <";
641 for (const auto &component : packageComponents) {
642 out << component << "/";
643 }
Steven Moreland40786312016-08-16 10:29:40 -0700644 out << "IHw" << baseName << ".h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700645
646 enterLeaveNamespace(out, true /* enter */);
647 out << "\n";
648
649 out << "struct "
650 << "Bp"
651 << baseName
Steven Moreland40786312016-08-16 10:29:40 -0700652 << " : public ::android::hardware::BpInterface<IHw"
653 << baseName
Andreas Huber881227d2016-08-02 14:20:21 -0700654 << "> {\n";
655
656 out.indent();
657
658 out << "explicit Bp"
659 << baseName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700660 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700661 << "\n\n";
662
Iliyan Malchev795dd432016-09-02 11:48:26 -0700663 out << "virtual bool isRemote() const { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700664
Steven Morelanda7a421a2016-09-07 08:35:18 -0700665 generateMethods(out,
666 "" /* class name */,
Steven Moreland979e0992016-09-07 09:18:08 -0700667 MethodLocation::PROXY_HEADER,
668 true /* generate specify namespaces */);
Andreas Huber881227d2016-08-02 14:20:21 -0700669
670 out.unindent();
671
672 out << "};\n\n";
673
674 enterLeaveNamespace(out, false /* enter */);
675
676 out << "\n#endif // " << guard << "\n";
677
678 return OK;
679}
680
Andreas Huberb82318c2016-08-02 14:45:54 -0700681status_t AST::generateAllSource(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700682
Andreas Huberb82318c2016-08-02 14:45:54 -0700683 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700684 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700685 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700686
687 std::string ifaceName;
688 std::string baseName;
689
690 bool isInterface = true;
691 if (!AST::isInterface(&ifaceName)) {
692 baseName = "types";
693 isInterface = false;
694 } else {
695 baseName = ifaceName.substr(1); // cut off the leading 'I'.
696 }
697
698 path.append(baseName);
699
700 if (baseName != "types") {
701 path.append("All");
702 }
703
704 path.append(".cpp");
705
Andreas Huberd2943e12016-08-05 11:59:31 -0700706 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700707 FILE *file = fopen(path.c_str(), "w");
708
709 if (file == NULL) {
710 return -errno;
711 }
712
713 Formatter out(file);
714
715 std::vector<std::string> packageComponents;
716 getPackageAndVersionComponents(
717 &packageComponents, false /* cpp_compatible */);
718
719 std::string prefix;
720 for (const auto &component : packageComponents) {
721 prefix += component;
722 prefix += "/";
723 }
724
725 if (isInterface) {
726 out << "#include <" << prefix << "/Bp" << baseName << ".h>\n";
727 out << "#include <" << prefix << "/Bn" << baseName << ".h>\n";
728 } else {
729 out << "#include <" << prefix << "types.h>\n";
730 }
731
732 out << "\n";
733
734 enterLeaveNamespace(out, true /* enter */);
735 out << "\n";
736
737 status_t err = generateTypeSource(out, ifaceName);
738
739 if (err == OK && isInterface) {
740 err = generateProxySource(out, baseName);
741 }
742
743 if (err == OK && isInterface) {
744 err = generateStubSource(out, baseName);
745 }
746
Steven Moreland40786312016-08-16 10:29:40 -0700747 if (err == OK && isInterface) {
748 out << "IMPLEMENT_REGISTER_AND_GET_SERVICE(" << baseName << ")\n";
749 }
750
Andreas Huber881227d2016-08-02 14:20:21 -0700751 enterLeaveNamespace(out, false /* enter */);
752
753 return err;
754}
755
756status_t AST::generateTypeSource(
757 Formatter &out, const std::string &ifaceName) const {
758 return mRootScope->emitTypeDefinitions(out, ifaceName);
759}
760
Andreas Hubere7ff2282016-08-16 13:50:03 -0700761void AST::declareCppReaderLocals(
762 Formatter &out, const std::vector<TypedVar *> &args) const {
763 if (args.empty()) {
764 return;
765 }
766
767 for (const auto &arg : args) {
768 const Type &type = arg->type();
769
770 std::string extra;
771 out << type.getCppResultType(&extra)
772 << " "
773 << arg->name()
774 << extra
775 << ";\n";
776 }
777
778 out << "\n";
779}
780
Andreas Huber881227d2016-08-02 14:20:21 -0700781void AST::emitCppReaderWriter(
782 Formatter &out,
783 const std::string &parcelObj,
784 bool parcelObjIsPointer,
785 const TypedVar *arg,
786 bool isReader,
787 Type::ErrorMode mode) const {
788 const Type &type = arg->type();
789
Andreas Huber881227d2016-08-02 14:20:21 -0700790 type.emitReaderWriter(
791 out,
792 arg->name(),
793 parcelObj,
794 parcelObjIsPointer,
795 isReader,
796 mode);
797}
798
799status_t AST::generateProxySource(
800 Formatter &out, const std::string &baseName) const {
801 const std::string klassName = "Bp" + baseName;
802
803 out << klassName
804 << "::"
805 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700806 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700807
808 out.indent();
809 out.indent();
810
811 out << ": BpInterface"
Steven Moreland40786312016-08-16 10:29:40 -0700812 << "<IHw"
Andreas Huber881227d2016-08-02 14:20:21 -0700813 << baseName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700814 << ">(_hidl_impl) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700815
816 out.unindent();
817 out.unindent();
818 out << "}\n\n";
819
820 const Interface *iface = mRootScope->getInterface();
821
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700822 std::vector<const Interface *> chain;
823 while (iface != NULL) {
824 chain.push_back(iface);
825 iface = iface->superType();
826 }
Andreas Huber881227d2016-08-02 14:20:21 -0700827
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700828 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
829 const Interface *superInterface = *it;
Andreas Huber881227d2016-08-02 14:20:21 -0700830
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700831 for (const auto &method : superInterface->methods()) {
Steven Moreland979e0992016-09-07 09:18:08 -0700832 method->generateCppSignature(out,
833 klassName,
834 true /* specify namespaces */);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700835
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700836 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700837 const TypedVar *elidedReturn = method->canElideCallback();
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700838
Steven Morelanda7a421a2016-09-07 08:35:18 -0700839 out << "{\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700840
841 out.indent();
842
Iliyan Malchev549e2592016-08-10 08:59:12 -0700843 out << "::android::hardware::Parcel _hidl_data;\n";
844 out << "::android::hardware::Parcel _hidl_reply;\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700845 out << "::android::status_t _hidl_err;\n";
846 out << "::android::hardware::Status _hidl_status;\n\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700847 declareCppReaderLocals(out, method->results());
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700848
Iliyan Malchev549e2592016-08-10 08:59:12 -0700849 out << "_hidl_err = _hidl_data.writeInterfaceToken("
Steven Moreland40786312016-08-16 10:29:40 -0700850 << superInterface->fqName().cppNamespace()
851 << "::IHw"
852 << superInterface->getBaseName()
853 << "::descriptor);\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700854
Iliyan Malchev549e2592016-08-10 08:59:12 -0700855 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700856
857 for (const auto &arg : method->args()) {
858 emitCppReaderWriter(
859 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -0700860 "_hidl_data",
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700861 false /* parcelObjIsPointer */,
862 arg,
863 false /* reader */,
864 Type::ErrorMode_Goto);
865 }
866
Steven Moreland40786312016-08-16 10:29:40 -0700867 out << "_hidl_err = remote()->transact("
868 << superInterface->fqName().cppNamespace()
869 << "::IHw"
870 << superInterface->getBaseName()
871 << "::Call::"
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700872 << upcase(method->name())
Iliyan Malchev639bff82016-08-13 14:24:11 -0700873 << ", _hidl_data, &_hidl_reply";
874 if (method->isOneway()) {
875 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700876 }
Iliyan Malchev639bff82016-08-13 14:24:11 -0700877 out << ");\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700878
Iliyan Malchev639bff82016-08-13 14:24:11 -0700879 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700880
Iliyan Malchev639bff82016-08-13 14:24:11 -0700881 if (!method->isOneway()) {
882 out << "_hidl_err = _hidl_status.readFromParcel(_hidl_reply);\n";
883 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Iliyan Malchev639bff82016-08-13 14:24:11 -0700884 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
885
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700886 for (const auto &arg : method->results()) {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700887 emitCppReaderWriter(
888 out,
889 "_hidl_reply",
890 false /* parcelObjIsPointer */,
891 arg,
892 true /* reader */,
893 Type::ErrorMode_Goto);
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700894 }
895
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700896 if (returnsValue && elidedReturn == nullptr) {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700897 out << "if (_hidl_cb != nullptr) {\n";
898 out.indent();
899 out << "_hidl_cb(";
900
901 bool first = true;
902 for (const auto &arg : method->results()) {
903 if (!first) {
904 out << ", ";
905 }
906
907 if (arg->type().resultNeedsDeref()) {
908 out << "*";
909 }
910 out << arg->name();
911
912 first = false;
913 }
914
915 out << ");\n";
916 out.unindent();
917 out << "}\n\n";
918 }
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700919 }
920
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700921 if (elidedReturn != nullptr) {
922 std::string extra;
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700923 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700924 out << "return ::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700925 out << elidedReturn->type().getCppResultType(&extra)
926 << ">(" << elidedReturn->name() << ");\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700927 } else {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700928 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700929 out << "return ::android::hardware::Return<void>();\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700930 }
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700931
Andreas Huber881227d2016-08-02 14:20:21 -0700932 out.unindent();
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700933 out << "_hidl_error:\n";
934 out.indent();
935 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
936 out << "return ::android::hardware::Return<";
937 if (elidedReturn != nullptr) {
938 std::string extra;
939 out << method->results().at(0)->type().getCppResultType(&extra);
940 } else {
941 out << "void";
942 }
943 out << ">(_hidl_status);\n";
944
945 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700946 out << "}\n\n";
947 }
Andreas Huber881227d2016-08-02 14:20:21 -0700948 }
949
950 return OK;
951}
952
953status_t AST::generateStubSource(
954 Formatter &out, const std::string &baseName) const {
955 out << "IMPLEMENT_HWBINDER_META_INTERFACE("
956 << baseName
957 << ", \""
958 << mPackage.string()
959 << "::I"
960 << baseName
961 << "\");\n\n";
962
963 const std::string klassName = "Bn" + baseName;
964
Steven Moreland40786312016-08-16 10:29:40 -0700965 out << klassName
966 << "::"
967 << klassName
968 << "(const ::android::sp<I" << baseName <<"> &_hidl_impl)\n";
969
970 out.indent();
971 out.indent();
972
973 out << ": BnInterface"
974 << "<I"
975 << baseName
976 << ", IHw"
977 << baseName
978 << ">(_hidl_impl) {\n";
979
980 out.unindent();
981 out.unindent();
982 out << "}\n\n";
983
Andreas Huber881227d2016-08-02 14:20:21 -0700984 out << "::android::status_t " << klassName << "::onTransact(\n";
985
986 out.indent();
987 out.indent();
988
Iliyan Malchev549e2592016-08-10 08:59:12 -0700989 out << "uint32_t _hidl_code,\n"
990 << "const ::android::hardware::Parcel &_hidl_data,\n"
991 << "::android::hardware::Parcel *_hidl_reply,\n"
992 << "uint32_t _hidl_flags,\n"
993 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700994
995 out.unindent();
996
Iliyan Malchev549e2592016-08-10 08:59:12 -0700997 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -0700998 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700999 out.indent();
1000
1001 const Interface *iface = mRootScope->getInterface();
1002
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001003 std::vector<const Interface *> chain;
1004 while (iface != NULL) {
1005 chain.push_back(iface);
1006 iface = iface->superType();
1007 }
Andreas Huber881227d2016-08-02 14:20:21 -07001008
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001009 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
1010 const Interface *superInterface = *it;
Andreas Huber881227d2016-08-02 14:20:21 -07001011
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001012 for (const auto &method : superInterface->methods()) {
1013 out << "case "
Steven Moreland40786312016-08-16 10:29:40 -07001014 << superInterface->fqName().cppNamespace()
1015 << "::IHw"
1016 << superInterface->getBaseName()
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001017 << "::Call::"
1018 << upcase(method->name())
1019 << ":\n{\n";
1020
1021 out.indent();
1022
1023 status_t err =
1024 generateStubSourceForMethod(out, superInterface, method);
1025
1026 if (err != OK) {
1027 return err;
1028 }
1029
1030 out.unindent();
1031 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001032 }
Andreas Huber881227d2016-08-02 14:20:21 -07001033 }
1034
1035 out << "default:\n{\n";
1036 out.indent();
1037
Andreas Huber8a82ff72016-08-04 10:29:39 -07001038 out << "return ::android::hardware::BnInterface<I"
Steven Moreland40786312016-08-16 10:29:40 -07001039 << baseName << ", IHw" << baseName
Andreas Huber881227d2016-08-02 14:20:21 -07001040 << ">::onTransact(\n";
1041
1042 out.indent();
1043 out.indent();
1044
Iliyan Malchev549e2592016-08-10 08:59:12 -07001045 out << "_hidl_code, _hidl_data, _hidl_reply, "
1046 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001047
1048 out.unindent();
1049 out.unindent();
1050
1051 out.unindent();
1052 out << "}\n";
1053
1054 out.unindent();
1055 out << "}\n\n";
1056
Iliyan Malchev549e2592016-08-10 08:59:12 -07001057 out << "if (_hidl_err == ::android::UNEXPECTED_NULL) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001058 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001059 out << "_hidl_err = ::android::hardware::Status::fromExceptionCode(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001060 out.indent();
1061 out.indent();
Andreas Huber8a82ff72016-08-04 10:29:39 -07001062 out << "::android::hardware::Status::EX_NULL_POINTER)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001063 out.indent();
1064 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001065 out << ".writeToParcel(_hidl_reply);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001066 out.unindent();
1067 out.unindent();
1068 out.unindent();
1069 out.unindent();
1070
1071 out.unindent();
1072 out << "}\n\n";
1073
Iliyan Malchev549e2592016-08-10 08:59:12 -07001074 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001075
1076 out.unindent();
1077 out << "}\n\n";
1078
1079 return OK;
1080}
1081
1082status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001083 Formatter &out, const Interface *iface, const Method *method) const {
Iliyan Malchev549e2592016-08-10 08:59:12 -07001084 out << "if (!_hidl_data.enforceInterface("
Steven Moreland40786312016-08-16 10:29:40 -07001085 << iface->fqName().cppNamespace()
1086 << "::IHw"
1087 << iface->getBaseName()
1088 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001089
Andreas Huber881227d2016-08-02 14:20:21 -07001090 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001091 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001092 out << "break;\n";
1093 out.unindent();
1094 out << "}\n\n";
1095
Andreas Hubere7ff2282016-08-16 13:50:03 -07001096 declareCppReaderLocals(out, method->args());
1097
Andreas Huber881227d2016-08-02 14:20:21 -07001098 for (const auto &arg : method->args()) {
1099 emitCppReaderWriter(
1100 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001101 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001102 false /* parcelObjIsPointer */,
1103 arg,
1104 true /* reader */,
1105 Type::ErrorMode_Break);
1106 }
1107
1108 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001109 const TypedVar *elidedReturn = method->canElideCallback();
Andreas Huber881227d2016-08-02 14:20:21 -07001110
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001111 if (elidedReturn != nullptr) {
1112 std::string extra;
Andreas Huber881227d2016-08-02 14:20:21 -07001113
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001114 out << elidedReturn->type().getCppResultType(&extra) << " ";
1115 out << elidedReturn->name() << " = ";
1116 out << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001117
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001118 bool first = true;
1119 for (const auto &arg : method->args()) {
Andreas Huber881227d2016-08-02 14:20:21 -07001120 if (!first) {
1121 out << ", ";
1122 }
1123
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001124 if (arg->type().resultNeedsDeref()) {
1125 out << "*";
1126 }
1127
1128 out << arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -07001129
1130 first = false;
1131 }
1132
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001133 out << ");\n\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -07001134 out << "::android::hardware::Status::ok()"
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001135 << ".writeToParcel(_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001136
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001137 elidedReturn->type().emitReaderWriter(
1138 out,
1139 elidedReturn->name(),
1140 "_hidl_reply",
1141 true, /* parcelObjIsPointer */
1142 false, /* isReader */
1143 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001144
Iliyan Malchev549e2592016-08-10 08:59:12 -07001145 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001146 } else {
1147 if (returnsValue) {
1148 out << "bool _hidl_callbackCalled = false;\n\n";
1149 }
Andreas Huber881227d2016-08-02 14:20:21 -07001150
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001151 out << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001152
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001153 bool first = true;
1154 for (const auto &arg : method->args()) {
1155 if (!first) {
1156 out << ", ";
1157 }
Andreas Huber881227d2016-08-02 14:20:21 -07001158
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001159 if (arg->type().resultNeedsDeref()) {
1160 out << "*";
1161 }
1162
1163 out << arg->name();
1164
1165 first = false;
1166 }
1167
1168 if (returnsValue) {
1169 if (!first) {
1170 out << ", ";
1171 }
1172
1173 out << "[&](";
1174
1175 first = true;
1176 for (const auto &arg : method->results()) {
1177 if (!first) {
1178 out << ", ";
1179 }
1180
1181 out << "const auto &" << arg->name();
1182
1183 first = false;
1184 }
1185
1186 out << ") {\n";
1187 out.indent();
1188 out << "_hidl_callbackCalled = true;\n\n";
1189
1190 out << "::android::hardware::Status::ok()"
1191 << ".writeToParcel(_hidl_reply);\n\n";
1192
1193 for (const auto &arg : method->results()) {
1194 emitCppReaderWriter(
1195 out,
1196 "_hidl_reply",
1197 true /* parcelObjIsPointer */,
1198 arg,
1199 false /* reader */,
1200 Type::ErrorMode_Ignore);
1201 }
1202
1203 out << "_hidl_cb(*_hidl_reply);\n";
1204
1205 out.unindent();
1206 out << "}\n";
1207 }
1208
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001209 out << ");\n\n";
1210
1211 // What to do if the stub implementation has a synchronous callback
1212 // which does not get invoked? This is not a transport error but a
1213 // service error of sorts. For now, return OK to the caller, as this is
1214 // not a transport error.
1215 //
1216 // TODO(b/31365311) Figure out how to deal with this later.
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001217
1218 if (returnsValue) {
1219 out << "if (!_hidl_callbackCalled) {\n";
1220 out.indent();
1221 }
1222
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001223 out << "::android::hardware::Status::ok()"
1224 << ".writeToParcel(_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001225
1226 if (returnsValue) {
1227 out.unindent();
1228 out << "}\n\n";
1229 }
Andreas Huber881227d2016-08-02 14:20:21 -07001230 }
1231
1232 out << "break;\n";
1233
1234 return OK;
1235}
1236
1237} // namespace android
1238