blob: 42186aff1874a900c3a5d2b4c51486dcbcfa6919 [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) {
Iliyan Malchev549e2592016-08-10 08:59:12 -070065 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -070066
Andreas Huber8a82ff72016-08-04 10:29:39 -070067 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -070068 << binderName << ";\n";
69
Iliyan Malchev549e2592016-08-10 08:59:12 -070070 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070071 out << parcelObjDeref
72 << "readNullableStrongBinder(&"
73 << binderName
74 << ");\n";
75
76 handleError(out, mode);
77
78 out << name
79 << " = "
Andreas Huber0e00de42016-08-03 09:56:02 -070080 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -070081 << "::asInterface("
82 << binderName
83 << ");\n";
84 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070085 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070086 out << parcelObjDeref
87 << "writeStrongBinder("
Andreas Huber0e00de42016-08-03 09:56:02 -070088 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -070089 << "::asBinder("
90 << name
91 << "));\n";
92
93 handleError(out, mode);
94 }
95}
96
Andreas Huber2831d512016-08-15 09:33:47 -070097void Interface::emitJavaReaderWriter(
98 Formatter &out,
99 const std::string &parcelObj,
100 const std::string &argName,
101 bool isReader) const {
102 if (isReader) {
103 out << fullJavaName()
104 << ".asInterface("
105 << parcelObj
106 << ".readStrongBinder());\n";
107 } else {
108 out << parcelObj
109 << ".writeStrongBinder("
110 << argName
111 << " == null ? null : "
112 << argName
113 << ".asBinder());\n";
114 }
115}
116
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700117status_t Interface::emitVtsArgumentType(Formatter &out) const {
118 out << "type: TYPE_HIDL_CALLBACK\n"
119 << "predefined_type: \""
120 << localName()
121 << "\"\n";
122 return OK;
123}
124
Andreas Huberc9410c72016-07-28 12:18:40 -0700125} // namespace android
126