blob: 2c6586545ed3ec4dd2408a5752d411f941cfe851 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "Interface.h"
2
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -07003#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07004#include "Formatter.h"
5#include "Method.h"
6
7namespace android {
8
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -07009Interface::Interface(
10 Interface *super,
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070011 AnnotationVector *annotations)
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070012 : mSuperType(super),
13 mAnnotationsByName(annotations) {
Andreas Huberc9410c72016-07-28 12:18:40 -070014}
15
16void Interface::addMethod(Method *method) {
17 mMethods.push_back(method);
18}
19
Andreas Huber6cb08cf2016-08-03 15:44:51 -070020const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -070021 return mSuperType;
22}
23
Andreas Hubera2723d22016-07-29 15:36:07 -070024bool Interface::isInterface() const {
25 return true;
26}
27
Andreas Huber881227d2016-08-02 14:20:21 -070028const std::vector<Method *> &Interface::methods() const {
29 return mMethods;
30}
31
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070032const AnnotationVector &Interface::annotations() const {
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070033 return *mAnnotationsByName;
34}
35
Andreas Huber881227d2016-08-02 14:20:21 -070036std::string Interface::getCppType(StorageMode mode, std::string *extra) const {
37 extra->clear();
Andreas Huber31629bc2016-08-03 09:06:40 -070038 const std::string base = "::android::sp<" + fullName() + ">";
Andreas Huber881227d2016-08-02 14:20:21 -070039
40 switch (mode) {
41 case StorageMode_Stack:
42 case StorageMode_Result:
43 return base;
44
45 case StorageMode_Argument:
46 return "const " + base + "&";
47 }
48}
49
Andreas Huber2831d512016-08-15 09:33:47 -070050std::string Interface::getJavaType() const {
51 return fullJavaName();
52}
53
Andreas Huber881227d2016-08-02 14:20:21 -070054void Interface::emitReaderWriter(
55 Formatter &out,
56 const std::string &name,
57 const std::string &parcelObj,
58 bool parcelObjIsPointer,
59 bool isReader,
60 ErrorMode mode) const {
61 const std::string parcelObjDeref =
62 parcelObj + (parcelObjIsPointer ? "->" : ".");
63
64 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -070065 out << "{\n";
66 out.indent();
67
Iliyan Malchev549e2592016-08-10 08:59:12 -070068 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -070069
Andreas Huber8a82ff72016-08-04 10:29:39 -070070 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -070071 << binderName << ";\n";
72
Iliyan Malchev549e2592016-08-10 08:59:12 -070073 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070074 out << parcelObjDeref
75 << "readNullableStrongBinder(&"
76 << binderName
77 << ");\n";
78
79 handleError(out, mode);
80
81 out << name
82 << " = "
Andreas Huber0e00de42016-08-03 09:56:02 -070083 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -070084 << "::asInterface("
85 << binderName
86 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -070087
88 out.unindent();
89 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -070090 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070091 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070092 out << parcelObjDeref
93 << "writeStrongBinder("
Andreas Huber0e00de42016-08-03 09:56:02 -070094 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -070095 << "::asBinder("
96 << name
97 << "));\n";
98
99 handleError(out, mode);
100 }
101}
102
Andreas Huber2831d512016-08-15 09:33:47 -0700103void Interface::emitJavaReaderWriter(
104 Formatter &out,
105 const std::string &parcelObj,
106 const std::string &argName,
107 bool isReader) const {
108 if (isReader) {
109 out << fullJavaName()
110 << ".asInterface("
111 << parcelObj
112 << ".readStrongBinder());\n";
113 } else {
114 out << parcelObj
115 << ".writeStrongBinder("
116 << argName
117 << " == null ? null : "
118 << argName
119 << ".asBinder());\n";
120 }
121}
122
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700123status_t Interface::emitVtsArgumentType(Formatter &out) const {
124 out << "type: TYPE_HIDL_CALLBACK\n"
125 << "predefined_type: \""
126 << localName()
127 << "\"\n";
128 return OK;
129}
130
Andreas Huberc9410c72016-07-28 12:18:40 -0700131} // namespace android
132