blob: 9cfb938541f9b1b27ba97dac6d99cf2ec80ddea5 [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 Morelanda7a421a2016-09-07 08:35:18 -0700227 << Method::GetArgSignature(method->results())
Andreas Huber881227d2016-08-02 14:20:21 -0700228 << ")>;\n";
229 }
230
231 if (haveCallbacks) {
232 out << "\n";
233 }
234
235 for (const auto &method : iface->methods()) {
236 const bool returnsValue = !method->results().empty();
237
Andreas Huber3599d922016-08-09 10:42:57 -0700238 method->dumpAnnotations(out);
239
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700240 const TypedVar *elidedReturn = method->canElideCallback();
241 if (elidedReturn) {
242 std::string extra;
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700243 out << "virtual ::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700244 out << elidedReturn->type().getCppResultType(&extra) << "> ";
245 } else {
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700246 out << "virtual ::android::hardware::Return<void> ";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700247 }
248
249 out << method->name()
Andreas Huber881227d2016-08-02 14:20:21 -0700250 << "("
Steven Morelanda7a421a2016-09-07 08:35:18 -0700251 << Method::GetArgSignature(method->args());
Andreas Huber881227d2016-08-02 14:20:21 -0700252
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700253 if (returnsValue && elidedReturn == nullptr) {
Andreas Huber881227d2016-08-02 14:20:21 -0700254 if (!method->args().empty()) {
255 out << ", ";
256 }
257
Iliyan Malchev549e2592016-08-10 08:59:12 -0700258 out << method->name() << "_cb _hidl_cb = nullptr";
Andreas Huber881227d2016-08-02 14:20:21 -0700259 }
260
261 out << ") = 0;\n";
262 }
Steven Moreland40786312016-08-16 10:29:40 -0700263
264 out << "DECLARE_REGISTER_AND_GET_SERVICE(" << baseName << ")\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700265 }
266
267 if (isInterface) {
268 out.unindent();
269
270 out << "};\n";
271 }
272
273 out << "\n";
274 enterLeaveNamespace(out, false /* enter */);
275
276 out << "\n#endif // " << guard << "\n";
277
278 return OK;
279}
280
Steven Moreland40786312016-08-16 10:29:40 -0700281status_t AST::generateHwBinderHeader(const std::string &outputPath) const {
282 std::string ifaceName;
283 if(!AST::isInterface(&ifaceName)) {
284 // types.hal does not get an HwBinder header.
285 return OK;
286 }
287
288 // cut off the leading 'I'.
289 const std::string baseName = ifaceName.substr(1);
290
291 const std::string klassName = "IHw" + baseName;
292
293 std::string path = outputPath;
294 path.append(mCoordinator->convertPackageRootToPath(mPackage));
295 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
296 path.append(klassName + ".h");
297
298 FILE* file = fopen(path.c_str(), "w");
299
300 if (file == NULL) {
301 return -errno;
302 }
303
304 Formatter out(file);
305
306 const std::string guard = makeHeaderGuard(klassName);
307
308 out << "#ifndef " << guard << "\n";
309 out << "#define " << guard << "\n\n";
310
311 std::vector<std::string> packageComponents;
312 getPackageAndVersionComponents(
313 &packageComponents, false /* cpp_compatible */);
314
315 out << "#include <";
316 for (const auto &component : packageComponents) {
317 out << component << "/";
318 }
319 out << ifaceName << ".h>\n\n";
320
321 for (const auto &item : mImportedNames) {
322 if (item.name() == "types") {
323 continue;
324 }
325
326 out << "#include <";
327
328 std::vector<std::string> components;
329 item.getPackageAndVersionComponents(
330 &components, false /* cpp_compatible */);
331
332 for (const auto &component : components) {
333 out << component << "/";
334 }
335
336 // cut off the leading I
337 const std::string itemBaseName = item.name().substr(1);
338
339 out << "Bn"
340 << itemBaseName
341 << ".h>\n";
342 }
343
344 out << "\n";
345
346 out << "#include <hidl/HidlSupport.h>\n";
Martijn Coenen93915102016-09-01 01:35:52 +0200347 out << "#include <hidl/Status.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700348 out << "#include <hwbinder/IBinder.h>\n";
349 out << "#include <hwbinder/IInterface.h>\n";
Steven Moreland40786312016-08-16 10:29:40 -0700350
351 out << "\n";
352
353 enterLeaveNamespace(out, true /* enter */);
354 out << "\n";
355
356 out << "struct "
357 << klassName
358 << " : public "
359 << ifaceName;
360
361 const Interface *iface = mRootScope->getInterface();
362 const Interface *superType = iface->superType();
363
364 out << ", public ::android::hardware::IInterface";
365
366 out << " {\n";
367
368 out.indent();
369
370 out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n";
371
372 out << "enum Call {\n";
373 out.indent();
374
375 bool first = true;
376 for (const auto &method : iface->methods()) {
377 out << upcase(method->name());
378
379 if (first) {
380 out << " = ";
381 if (superType != NULL) {
382 out << superType->fqName().cppNamespace()
383 << "::IHw"
384 << superType->getBaseName()
385 << "::Call::CallCount";
386 } else {
387 out << "::android::hardware::IBinder::FIRST_CALL_TRANSACTION";
388 }
389
390 first = false;
391 }
392
393 out << ",\n";
394 }
395
396 out << "CallCount\n";
397
398 out.unindent();
399 out << "};\n\n";
400
401 out.unindent();
402
403 out << "};\n\n";
404
405 enterLeaveNamespace(out, false /* enter */);
406
407 out << "\n#endif // " << guard << "\n";
408
409 return OK;
410}
411
Andreas Huber881227d2016-08-02 14:20:21 -0700412status_t AST::emitTypeDeclarations(Formatter &out) const {
413 return mRootScope->emitTypeDeclarations(out);
414}
415
Steven Morelanda7a421a2016-09-07 08:35:18 -0700416status_t AST::generateStubMethod(Formatter &out,
417 const std::string &className,
418 const Method *method) const {
419 out << "inline ";
420
421 method->generateCppSignature(out, className);
422
423 const bool returnsValue = !method->results().empty();
424 const TypedVar *elidedReturn = method->canElideCallback();
425 out << " {\n";
426 out.indent();
427 out << "return mImpl->"
428 << method->name()
429 << "(";
430 bool first = true;
431 for (const auto &arg : method->args()) {
432 if (!first) {
433 out << ", ";
434 }
435 first = false;
436 out << arg->name();
437 }
438 if (returnsValue && elidedReturn == nullptr) {
439 if (!method->args().empty()) {
440 out << ", ";
441 }
442
443 out << "_hidl_cb";
444 }
445 out << ");\n";
446 out.unindent();
447 out << "}";
448
449 out << ";\n";
450
451 return OK;
452}
453
454status_t AST::generateProxyMethod(Formatter &out,
455 const std::string &className,
456 const Method *method) const {
457
458 method->generateCppSignature(out, className);
459 out << " override;\n";
460
461 return OK;
462}
463
464status_t AST::generateMethods(
465 Formatter &out,
466 const std::string &className,
467 MethodLocation type) const {
468
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700469 const Interface *iface = mRootScope->getInterface();
470
471 std::vector<const Interface *> chain;
472 while (iface != NULL) {
473 chain.push_back(iface);
474 iface = iface->superType();
475 }
476
477 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
478 const Interface *superInterface = *it;
479
480 out << "// Methods from "
481 << superInterface->fullName()
482 << " follow.\n";
483
484 for (const auto &method : superInterface->methods()) {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700485 status_t err;
486 switch(type) {
487 case STUB_HEADER:
488 err = generateStubMethod(out,
489 className,
490 method);
491 break;
492 case PROXY_HEADER:
493 err = generateProxyMethod(out,
494 className,
495 method);
496 break;
497 default:
498 err = UNKNOWN_ERROR;
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700499 }
500
Steven Morelanda7a421a2016-09-07 08:35:18 -0700501 if (err != OK) {
502 return err;
Steven Moreland40786312016-08-16 10:29:40 -0700503 }
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700504 }
505
506 out << "\n";
507 }
508
509 return OK;
510}
511
Andreas Huberb82318c2016-08-02 14:45:54 -0700512status_t AST::generateStubHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700513 std::string ifaceName;
514 if (!AST::isInterface(&ifaceName)) {
515 // types.hal does not get a stub header.
516 return OK;
517 }
518
Andreas Huber881227d2016-08-02 14:20:21 -0700519 // cut off the leading 'I'.
520 const std::string baseName = ifaceName.substr(1);
Steven Moreland40786312016-08-16 10:29:40 -0700521 const std::string klassName = "Bn" + baseName;
Andreas Huber881227d2016-08-02 14:20:21 -0700522
Andreas Huberb82318c2016-08-02 14:45:54 -0700523 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700524 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700525 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Steven Moreland40786312016-08-16 10:29:40 -0700526 path.append(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700527 path.append(".h");
528
Andreas Huberd2943e12016-08-05 11:59:31 -0700529 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700530 FILE *file = fopen(path.c_str(), "w");
531
532 if (file == NULL) {
533 return -errno;
534 }
535
536 Formatter out(file);
537
Steven Moreland40786312016-08-16 10:29:40 -0700538 const std::string guard = makeHeaderGuard(klassName);
Andreas Huber881227d2016-08-02 14:20:21 -0700539
540 out << "#ifndef " << guard << "\n";
541 out << "#define " << guard << "\n\n";
542
543 std::vector<std::string> packageComponents;
544 getPackageAndVersionComponents(
545 &packageComponents, false /* cpp_compatible */);
546
547 out << "#include <";
548 for (const auto &component : packageComponents) {
549 out << component << "/";
550 }
Steven Moreland40786312016-08-16 10:29:40 -0700551 out << "IHw" << baseName << ".h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700552
553 enterLeaveNamespace(out, true /* enter */);
554 out << "\n";
555
556 out << "struct "
557 << "Bn"
558 << baseName
Steven Moreland40786312016-08-16 10:29:40 -0700559 << " : public ::android::hardware::BnInterface<I"
560 << baseName << ", IHw" << baseName
Andreas Huber881227d2016-08-02 14:20:21 -0700561 << "> {\n";
562
563 out.indent();
Steven Moreland40786312016-08-16 10:29:40 -0700564 out << "explicit Bn"
565 << baseName
566 << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);"
567 << "\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700568 out << "::android::status_t onTransact(\n";
569 out.indent();
570 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700571 out << "uint32_t _hidl_code,\n";
572 out << "const ::android::hardware::Parcel &_hidl_data,\n";
573 out << "::android::hardware::Parcel *_hidl_reply,\n";
574 out << "uint32_t _hidl_flags = 0,\n";
Iliyan Malchev62c3d182016-08-16 20:33:39 -0700575 out << "TransactCallback _hidl_cb = nullptr) override;\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700576 out.unindent();
577 out.unindent();
578
Steven Morelanda7a421a2016-09-07 08:35:18 -0700579 generateMethods(out,
580 "" /* class name */,
581 MethodLocation::STUB_HEADER);
Andreas Huber881227d2016-08-02 14:20:21 -0700582 out.unindent();
583
584 out << "};\n\n";
585
586 enterLeaveNamespace(out, false /* enter */);
587
588 out << "\n#endif // " << guard << "\n";
589
590 return OK;
591}
592
Andreas Huberb82318c2016-08-02 14:45:54 -0700593status_t AST::generateProxyHeader(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700594 std::string ifaceName;
595 if (!AST::isInterface(&ifaceName)) {
596 // types.hal does not get a proxy header.
597 return OK;
598 }
599
Andreas Huber881227d2016-08-02 14:20:21 -0700600 // cut off the leading 'I'.
601 const std::string baseName = ifaceName.substr(1);
602
Andreas Huberb82318c2016-08-02 14:45:54 -0700603 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700604 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700605 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700606 path.append("Bp");
607 path.append(baseName);
608 path.append(".h");
609
Andreas Huberd2943e12016-08-05 11:59:31 -0700610 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700611 FILE *file = fopen(path.c_str(), "w");
612
613 if (file == NULL) {
614 return -errno;
615 }
616
617 Formatter out(file);
618
619 const std::string guard = makeHeaderGuard("Bp" + baseName);
620
621 out << "#ifndef " << guard << "\n";
622 out << "#define " << guard << "\n\n";
623
624 std::vector<std::string> packageComponents;
625 getPackageAndVersionComponents(
626 &packageComponents, false /* cpp_compatible */);
627
628 out << "#include <";
629 for (const auto &component : packageComponents) {
630 out << component << "/";
631 }
Steven Moreland40786312016-08-16 10:29:40 -0700632 out << "IHw" << baseName << ".h>\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700633
634 enterLeaveNamespace(out, true /* enter */);
635 out << "\n";
636
637 out << "struct "
638 << "Bp"
639 << baseName
Steven Moreland40786312016-08-16 10:29:40 -0700640 << " : public ::android::hardware::BpInterface<IHw"
641 << baseName
Andreas Huber881227d2016-08-02 14:20:21 -0700642 << "> {\n";
643
644 out.indent();
645
646 out << "explicit Bp"
647 << baseName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700648 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);"
Andreas Huber881227d2016-08-02 14:20:21 -0700649 << "\n\n";
650
Iliyan Malchev795dd432016-09-02 11:48:26 -0700651 out << "virtual bool isRemote() const { return true; }\n\n";
Steven Moreland40786312016-08-16 10:29:40 -0700652
Steven Morelanda7a421a2016-09-07 08:35:18 -0700653 generateMethods(out,
654 "" /* class name */,
655 MethodLocation::PROXY_HEADER);
Andreas Huber881227d2016-08-02 14:20:21 -0700656
657 out.unindent();
658
659 out << "};\n\n";
660
661 enterLeaveNamespace(out, false /* enter */);
662
663 out << "\n#endif // " << guard << "\n";
664
665 return OK;
666}
667
Andreas Huberb82318c2016-08-02 14:45:54 -0700668status_t AST::generateAllSource(const std::string &outputPath) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700669
Andreas Huberb82318c2016-08-02 14:45:54 -0700670 std::string path = outputPath;
Andreas Huberd2943e12016-08-05 11:59:31 -0700671 path.append(mCoordinator->convertPackageRootToPath(mPackage));
Andreas Huberdca261f2016-08-04 13:47:51 -0700672 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Andreas Huber881227d2016-08-02 14:20:21 -0700673
674 std::string ifaceName;
675 std::string baseName;
676
677 bool isInterface = true;
678 if (!AST::isInterface(&ifaceName)) {
679 baseName = "types";
680 isInterface = false;
681 } else {
682 baseName = ifaceName.substr(1); // cut off the leading 'I'.
683 }
684
685 path.append(baseName);
686
687 if (baseName != "types") {
688 path.append("All");
689 }
690
691 path.append(".cpp");
692
Andreas Huberd2943e12016-08-05 11:59:31 -0700693 CHECK(Coordinator::MakeParentHierarchy(path));
Andreas Huber881227d2016-08-02 14:20:21 -0700694 FILE *file = fopen(path.c_str(), "w");
695
696 if (file == NULL) {
697 return -errno;
698 }
699
700 Formatter out(file);
701
702 std::vector<std::string> packageComponents;
703 getPackageAndVersionComponents(
704 &packageComponents, false /* cpp_compatible */);
705
706 std::string prefix;
707 for (const auto &component : packageComponents) {
708 prefix += component;
709 prefix += "/";
710 }
711
712 if (isInterface) {
713 out << "#include <" << prefix << "/Bp" << baseName << ".h>\n";
714 out << "#include <" << prefix << "/Bn" << baseName << ".h>\n";
715 } else {
716 out << "#include <" << prefix << "types.h>\n";
717 }
718
719 out << "\n";
720
721 enterLeaveNamespace(out, true /* enter */);
722 out << "\n";
723
724 status_t err = generateTypeSource(out, ifaceName);
725
726 if (err == OK && isInterface) {
727 err = generateProxySource(out, baseName);
728 }
729
730 if (err == OK && isInterface) {
731 err = generateStubSource(out, baseName);
732 }
733
Steven Moreland40786312016-08-16 10:29:40 -0700734 if (err == OK && isInterface) {
735 out << "IMPLEMENT_REGISTER_AND_GET_SERVICE(" << baseName << ")\n";
736 }
737
Andreas Huber881227d2016-08-02 14:20:21 -0700738 enterLeaveNamespace(out, false /* enter */);
739
740 return err;
741}
742
743status_t AST::generateTypeSource(
744 Formatter &out, const std::string &ifaceName) const {
745 return mRootScope->emitTypeDefinitions(out, ifaceName);
746}
747
Andreas Hubere7ff2282016-08-16 13:50:03 -0700748void AST::declareCppReaderLocals(
749 Formatter &out, const std::vector<TypedVar *> &args) const {
750 if (args.empty()) {
751 return;
752 }
753
754 for (const auto &arg : args) {
755 const Type &type = arg->type();
756
757 std::string extra;
758 out << type.getCppResultType(&extra)
759 << " "
760 << arg->name()
761 << extra
762 << ";\n";
763 }
764
765 out << "\n";
766}
767
Andreas Huber881227d2016-08-02 14:20:21 -0700768void AST::emitCppReaderWriter(
769 Formatter &out,
770 const std::string &parcelObj,
771 bool parcelObjIsPointer,
772 const TypedVar *arg,
773 bool isReader,
774 Type::ErrorMode mode) const {
775 const Type &type = arg->type();
776
Andreas Huber881227d2016-08-02 14:20:21 -0700777 type.emitReaderWriter(
778 out,
779 arg->name(),
780 parcelObj,
781 parcelObjIsPointer,
782 isReader,
783 mode);
784}
785
786status_t AST::generateProxySource(
787 Formatter &out, const std::string &baseName) const {
788 const std::string klassName = "Bp" + baseName;
789
790 out << klassName
791 << "::"
792 << klassName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700793 << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700794
795 out.indent();
796 out.indent();
797
798 out << ": BpInterface"
Steven Moreland40786312016-08-16 10:29:40 -0700799 << "<IHw"
Andreas Huber881227d2016-08-02 14:20:21 -0700800 << baseName
Iliyan Malchev549e2592016-08-10 08:59:12 -0700801 << ">(_hidl_impl) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700802
803 out.unindent();
804 out.unindent();
805 out << "}\n\n";
806
807 const Interface *iface = mRootScope->getInterface();
808
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700809 std::vector<const Interface *> chain;
810 while (iface != NULL) {
811 chain.push_back(iface);
812 iface = iface->superType();
813 }
Andreas Huber881227d2016-08-02 14:20:21 -0700814
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700815 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
816 const Interface *superInterface = *it;
Andreas Huber881227d2016-08-02 14:20:21 -0700817
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700818 for (const auto &method : superInterface->methods()) {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700819 method->generateCppSignature(out, klassName);
820
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700821 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700822 const TypedVar *elidedReturn = method->canElideCallback();
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700823
Steven Morelanda7a421a2016-09-07 08:35:18 -0700824 out << "{\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700825
826 out.indent();
827
Iliyan Malchev549e2592016-08-10 08:59:12 -0700828 out << "::android::hardware::Parcel _hidl_data;\n";
829 out << "::android::hardware::Parcel _hidl_reply;\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700830 out << "::android::status_t _hidl_err;\n";
831 out << "::android::hardware::Status _hidl_status;\n\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700832 declareCppReaderLocals(out, method->results());
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700833
Iliyan Malchev549e2592016-08-10 08:59:12 -0700834 out << "_hidl_err = _hidl_data.writeInterfaceToken("
Steven Moreland40786312016-08-16 10:29:40 -0700835 << superInterface->fqName().cppNamespace()
836 << "::IHw"
837 << superInterface->getBaseName()
838 << "::descriptor);\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700839
Iliyan Malchev549e2592016-08-10 08:59:12 -0700840 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700841
842 for (const auto &arg : method->args()) {
843 emitCppReaderWriter(
844 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -0700845 "_hidl_data",
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700846 false /* parcelObjIsPointer */,
847 arg,
848 false /* reader */,
849 Type::ErrorMode_Goto);
850 }
851
Steven Moreland40786312016-08-16 10:29:40 -0700852 out << "_hidl_err = remote()->transact("
853 << superInterface->fqName().cppNamespace()
854 << "::IHw"
855 << superInterface->getBaseName()
856 << "::Call::"
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700857 << upcase(method->name())
Iliyan Malchev639bff82016-08-13 14:24:11 -0700858 << ", _hidl_data, &_hidl_reply";
859 if (method->isOneway()) {
860 out << ", ::android::hardware::IBinder::FLAG_ONEWAY";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700861 }
Iliyan Malchev639bff82016-08-13 14:24:11 -0700862 out << ");\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700863
Iliyan Malchev639bff82016-08-13 14:24:11 -0700864 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700865
Iliyan Malchev639bff82016-08-13 14:24:11 -0700866 if (!method->isOneway()) {
867 out << "_hidl_err = _hidl_status.readFromParcel(_hidl_reply);\n";
868 out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n";
Iliyan Malchev639bff82016-08-13 14:24:11 -0700869 out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n";
870
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700871 for (const auto &arg : method->results()) {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700872 emitCppReaderWriter(
873 out,
874 "_hidl_reply",
875 false /* parcelObjIsPointer */,
876 arg,
877 true /* reader */,
878 Type::ErrorMode_Goto);
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700879 }
880
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700881 if (returnsValue && elidedReturn == nullptr) {
Iliyan Malchev639bff82016-08-13 14:24:11 -0700882 out << "if (_hidl_cb != nullptr) {\n";
883 out.indent();
884 out << "_hidl_cb(";
885
886 bool first = true;
887 for (const auto &arg : method->results()) {
888 if (!first) {
889 out << ", ";
890 }
891
892 if (arg->type().resultNeedsDeref()) {
893 out << "*";
894 }
895 out << arg->name();
896
897 first = false;
898 }
899
900 out << ");\n";
901 out.unindent();
902 out << "}\n\n";
903 }
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700904 }
905
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700906 if (elidedReturn != nullptr) {
907 std::string extra;
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700908 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
Iliyan Malchev2b6591b2016-08-18 19:15:19 -0700909 out << "return ::android::hardware::Return<";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700910 out << elidedReturn->type().getCppResultType(&extra)
911 << ">(" << elidedReturn->name() << ");\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700912 } else {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700913 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700914 out << "return ::android::hardware::Return<void>();\n\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700915 }
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700916
Andreas Huber881227d2016-08-02 14:20:21 -0700917 out.unindent();
Iliyan Malchevd57066f2016-09-08 13:59:38 -0700918 out << "_hidl_error:\n";
919 out.indent();
920 out << "_hidl_status.setFromStatusT(_hidl_err);\n";
921 out << "return ::android::hardware::Return<";
922 if (elidedReturn != nullptr) {
923 std::string extra;
924 out << method->results().at(0)->type().getCppResultType(&extra);
925 } else {
926 out << "void";
927 }
928 out << ">(_hidl_status);\n";
929
930 out.unindent();
Andreas Huber881227d2016-08-02 14:20:21 -0700931 out << "}\n\n";
932 }
Andreas Huber881227d2016-08-02 14:20:21 -0700933 }
934
935 return OK;
936}
937
938status_t AST::generateStubSource(
939 Formatter &out, const std::string &baseName) const {
940 out << "IMPLEMENT_HWBINDER_META_INTERFACE("
941 << baseName
942 << ", \""
943 << mPackage.string()
944 << "::I"
945 << baseName
946 << "\");\n\n";
947
948 const std::string klassName = "Bn" + baseName;
949
Steven Moreland40786312016-08-16 10:29:40 -0700950 out << klassName
951 << "::"
952 << klassName
953 << "(const ::android::sp<I" << baseName <<"> &_hidl_impl)\n";
954
955 out.indent();
956 out.indent();
957
958 out << ": BnInterface"
959 << "<I"
960 << baseName
961 << ", IHw"
962 << baseName
963 << ">(_hidl_impl) {\n";
964
965 out.unindent();
966 out.unindent();
967 out << "}\n\n";
968
Andreas Huber881227d2016-08-02 14:20:21 -0700969 out << "::android::status_t " << klassName << "::onTransact(\n";
970
971 out.indent();
972 out.indent();
973
Iliyan Malchev549e2592016-08-10 08:59:12 -0700974 out << "uint32_t _hidl_code,\n"
975 << "const ::android::hardware::Parcel &_hidl_data,\n"
976 << "::android::hardware::Parcel *_hidl_reply,\n"
977 << "uint32_t _hidl_flags,\n"
978 << "TransactCallback _hidl_cb) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700979
980 out.unindent();
981
Iliyan Malchev549e2592016-08-10 08:59:12 -0700982 out << "::android::status_t _hidl_err = ::android::OK;\n\n";
Iliyan Malchev549e2592016-08-10 08:59:12 -0700983 out << "switch (_hidl_code) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700984 out.indent();
985
986 const Interface *iface = mRootScope->getInterface();
987
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700988 std::vector<const Interface *> chain;
989 while (iface != NULL) {
990 chain.push_back(iface);
991 iface = iface->superType();
992 }
Andreas Huber881227d2016-08-02 14:20:21 -0700993
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700994 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
995 const Interface *superInterface = *it;
Andreas Huber881227d2016-08-02 14:20:21 -0700996
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700997 for (const auto &method : superInterface->methods()) {
998 out << "case "
Steven Moreland40786312016-08-16 10:29:40 -0700999 << superInterface->fqName().cppNamespace()
1000 << "::IHw"
1001 << superInterface->getBaseName()
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001002 << "::Call::"
1003 << upcase(method->name())
1004 << ":\n{\n";
1005
1006 out.indent();
1007
1008 status_t err =
1009 generateStubSourceForMethod(out, superInterface, method);
1010
1011 if (err != OK) {
1012 return err;
1013 }
1014
1015 out.unindent();
1016 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001017 }
Andreas Huber881227d2016-08-02 14:20:21 -07001018 }
1019
1020 out << "default:\n{\n";
1021 out.indent();
1022
Andreas Huber8a82ff72016-08-04 10:29:39 -07001023 out << "return ::android::hardware::BnInterface<I"
Steven Moreland40786312016-08-16 10:29:40 -07001024 << baseName << ", IHw" << baseName
Andreas Huber881227d2016-08-02 14:20:21 -07001025 << ">::onTransact(\n";
1026
1027 out.indent();
1028 out.indent();
1029
Iliyan Malchev549e2592016-08-10 08:59:12 -07001030 out << "_hidl_code, _hidl_data, _hidl_reply, "
1031 << "_hidl_flags, _hidl_cb);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001032
1033 out.unindent();
1034 out.unindent();
1035
1036 out.unindent();
1037 out << "}\n";
1038
1039 out.unindent();
1040 out << "}\n\n";
1041
Iliyan Malchev549e2592016-08-10 08:59:12 -07001042 out << "if (_hidl_err == ::android::UNEXPECTED_NULL) {\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001043 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001044 out << "_hidl_err = ::android::hardware::Status::fromExceptionCode(\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001045 out.indent();
1046 out.indent();
Andreas Huber8a82ff72016-08-04 10:29:39 -07001047 out << "::android::hardware::Status::EX_NULL_POINTER)\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001048 out.indent();
1049 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001050 out << ".writeToParcel(_hidl_reply);\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001051 out.unindent();
1052 out.unindent();
1053 out.unindent();
1054 out.unindent();
1055
1056 out.unindent();
1057 out << "}\n\n";
1058
Iliyan Malchev549e2592016-08-10 08:59:12 -07001059 out << "return _hidl_err;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001060
1061 out.unindent();
1062 out << "}\n\n";
1063
1064 return OK;
1065}
1066
1067status_t AST::generateStubSourceForMethod(
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001068 Formatter &out, const Interface *iface, const Method *method) const {
Iliyan Malchev549e2592016-08-10 08:59:12 -07001069 out << "if (!_hidl_data.enforceInterface("
Steven Moreland40786312016-08-16 10:29:40 -07001070 << iface->fqName().cppNamespace()
1071 << "::IHw"
1072 << iface->getBaseName()
1073 << "::descriptor)) {\n";
Andreas Huber6cb08cf2016-08-03 15:44:51 -07001074
Andreas Huber881227d2016-08-02 14:20:21 -07001075 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -07001076 out << "_hidl_err = ::android::BAD_TYPE;\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001077 out << "break;\n";
1078 out.unindent();
1079 out << "}\n\n";
1080
Andreas Hubere7ff2282016-08-16 13:50:03 -07001081 declareCppReaderLocals(out, method->args());
1082
Andreas Huber881227d2016-08-02 14:20:21 -07001083 for (const auto &arg : method->args()) {
1084 emitCppReaderWriter(
1085 out,
Iliyan Malchev549e2592016-08-10 08:59:12 -07001086 "_hidl_data",
Andreas Huber881227d2016-08-02 14:20:21 -07001087 false /* parcelObjIsPointer */,
1088 arg,
1089 true /* reader */,
1090 Type::ErrorMode_Break);
1091 }
1092
1093 const bool returnsValue = !method->results().empty();
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001094 const TypedVar *elidedReturn = method->canElideCallback();
Andreas Huber881227d2016-08-02 14:20:21 -07001095
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001096 if (elidedReturn != nullptr) {
1097 std::string extra;
Andreas Huber881227d2016-08-02 14:20:21 -07001098
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001099 out << elidedReturn->type().getCppResultType(&extra) << " ";
1100 out << elidedReturn->name() << " = ";
1101 out << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001102
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001103 bool first = true;
1104 for (const auto &arg : method->args()) {
Andreas Huber881227d2016-08-02 14:20:21 -07001105 if (!first) {
1106 out << ", ";
1107 }
1108
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001109 if (arg->type().resultNeedsDeref()) {
1110 out << "*";
1111 }
1112
1113 out << arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -07001114
1115 first = false;
1116 }
1117
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001118 out << ");\n\n";
Andreas Huber8a82ff72016-08-04 10:29:39 -07001119 out << "::android::hardware::Status::ok()"
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001120 << ".writeToParcel(_hidl_reply);\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -07001121
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001122 elidedReturn->type().emitReaderWriter(
1123 out,
1124 elidedReturn->name(),
1125 "_hidl_reply",
1126 true, /* parcelObjIsPointer */
1127 false, /* isReader */
1128 Type::ErrorMode_Ignore);
Andreas Huber881227d2016-08-02 14:20:21 -07001129
Iliyan Malchev549e2592016-08-10 08:59:12 -07001130 out << "_hidl_cb(*_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001131 } else {
1132 if (returnsValue) {
1133 out << "bool _hidl_callbackCalled = false;\n\n";
1134 }
Andreas Huber881227d2016-08-02 14:20:21 -07001135
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001136 out << method->name() << "(";
Andreas Huber881227d2016-08-02 14:20:21 -07001137
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001138 bool first = true;
1139 for (const auto &arg : method->args()) {
1140 if (!first) {
1141 out << ", ";
1142 }
Andreas Huber881227d2016-08-02 14:20:21 -07001143
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001144 if (arg->type().resultNeedsDeref()) {
1145 out << "*";
1146 }
1147
1148 out << arg->name();
1149
1150 first = false;
1151 }
1152
1153 if (returnsValue) {
1154 if (!first) {
1155 out << ", ";
1156 }
1157
1158 out << "[&](";
1159
1160 first = true;
1161 for (const auto &arg : method->results()) {
1162 if (!first) {
1163 out << ", ";
1164 }
1165
1166 out << "const auto &" << arg->name();
1167
1168 first = false;
1169 }
1170
1171 out << ") {\n";
1172 out.indent();
1173 out << "_hidl_callbackCalled = true;\n\n";
1174
1175 out << "::android::hardware::Status::ok()"
1176 << ".writeToParcel(_hidl_reply);\n\n";
1177
1178 for (const auto &arg : method->results()) {
1179 emitCppReaderWriter(
1180 out,
1181 "_hidl_reply",
1182 true /* parcelObjIsPointer */,
1183 arg,
1184 false /* reader */,
1185 Type::ErrorMode_Ignore);
1186 }
1187
1188 out << "_hidl_cb(*_hidl_reply);\n";
1189
1190 out.unindent();
1191 out << "}\n";
1192 }
1193
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001194 out << ");\n\n";
1195
1196 // What to do if the stub implementation has a synchronous callback
1197 // which does not get invoked? This is not a transport error but a
1198 // service error of sorts. For now, return OK to the caller, as this is
1199 // not a transport error.
1200 //
1201 // TODO(b/31365311) Figure out how to deal with this later.
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001202
1203 if (returnsValue) {
1204 out << "if (!_hidl_callbackCalled) {\n";
1205 out.indent();
1206 }
1207
Iliyan Malchevd57066f2016-09-08 13:59:38 -07001208 out << "::android::hardware::Status::ok()"
1209 << ".writeToParcel(_hidl_reply);\n";
Iliyan Malchev40d474a2016-08-16 06:20:17 -07001210
1211 if (returnsValue) {
1212 out.unindent();
1213 out << "}\n\n";
1214 }
Andreas Huber881227d2016-08-02 14:20:21 -07001215 }
1216
1217 out << "break;\n";
1218
1219 return OK;
1220}
1221
1222} // namespace android
1223