blob: 33e8c88d0ec1d0ef036fe98e2d93a0721e308bfa [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 Huberc9410c72016-07-28 12:18:40 -070017#include "Interface.h"
18
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070019#include "Annotation.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070020#include "Formatter.h"
21#include "Method.h"
22
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070023#include <iostream>
24
Andreas Huberc9410c72016-07-28 12:18:40 -070025namespace android {
26
Andreas Huber9ed827c2016-08-22 12:31:13 -070027Interface::Interface(
28 const char *localName, Interface *super, AnnotationVector *annotations)
29 : Scope(localName),
30 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070031 mAnnotationsByName(annotations),
32 mIsJavaCompatibleInProgress(false) {
Andreas Huberc9410c72016-07-28 12:18:40 -070033}
34
35void Interface::addMethod(Method *method) {
36 mMethods.push_back(method);
37}
38
Andreas Huber6cb08cf2016-08-03 15:44:51 -070039const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -070040 return mSuperType;
41}
42
Andreas Hubera2723d22016-07-29 15:36:07 -070043bool Interface::isInterface() const {
44 return true;
45}
46
Andreas Huber295ad302016-08-16 11:35:00 -070047bool Interface::isBinder() const {
48 return true;
49}
50
Andreas Huber881227d2016-08-02 14:20:21 -070051const std::vector<Method *> &Interface::methods() const {
52 return mMethods;
53}
54
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070055const AnnotationVector &Interface::annotations() const {
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070056 return *mAnnotationsByName;
57}
58
Andreas Huber881227d2016-08-02 14:20:21 -070059std::string Interface::getCppType(StorageMode mode, std::string *extra) const {
60 extra->clear();
Andreas Huber31629bc2016-08-03 09:06:40 -070061 const std::string base = "::android::sp<" + fullName() + ">";
Andreas Huber881227d2016-08-02 14:20:21 -070062
63 switch (mode) {
64 case StorageMode_Stack:
65 case StorageMode_Result:
66 return base;
67
68 case StorageMode_Argument:
69 return "const " + base + "&";
70 }
71}
72
Andreas Huber2831d512016-08-15 09:33:47 -070073std::string Interface::getJavaType() const {
74 return fullJavaName();
75}
76
Andreas Huber881227d2016-08-02 14:20:21 -070077void Interface::emitReaderWriter(
78 Formatter &out,
79 const std::string &name,
80 const std::string &parcelObj,
81 bool parcelObjIsPointer,
82 bool isReader,
83 ErrorMode mode) const {
84 const std::string parcelObjDeref =
85 parcelObj + (parcelObjIsPointer ? "->" : ".");
86
87 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -070088 out << "{\n";
89 out.indent();
90
Iliyan Malchev549e2592016-08-10 08:59:12 -070091 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -070092
Andreas Huber8a82ff72016-08-04 10:29:39 -070093 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -070094 << binderName << ";\n";
95
Iliyan Malchev549e2592016-08-10 08:59:12 -070096 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -070097 out << parcelObjDeref
98 << "readNullableStrongBinder(&"
99 << binderName
100 << ");\n";
101
102 handleError(out, mode);
103
104 out << name
105 << " = "
Andreas Huber0e00de42016-08-03 09:56:02 -0700106 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -0700107 << "::asInterface("
108 << binderName
109 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700110
111 out.unindent();
112 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700113 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -0700114 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700115 out << parcelObjDeref
116 << "writeStrongBinder("
Andreas Huber0e00de42016-08-03 09:56:02 -0700117 << fullName()
Andreas Huber881227d2016-08-02 14:20:21 -0700118 << "::asBinder("
119 << name
120 << "));\n";
121
122 handleError(out, mode);
123 }
124}
125
Andreas Huber2831d512016-08-15 09:33:47 -0700126void Interface::emitJavaReaderWriter(
127 Formatter &out,
128 const std::string &parcelObj,
129 const std::string &argName,
130 bool isReader) const {
131 if (isReader) {
132 out << fullJavaName()
133 << ".asInterface("
134 << parcelObj
135 << ".readStrongBinder());\n";
136 } else {
137 out << parcelObj
138 << ".writeStrongBinder("
139 << argName
140 << " == null ? null : "
141 << argName
142 << ".asBinder());\n";
143 }
144}
145
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700146status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
147 for (const auto &type : getSubTypes()) {
148 out << "attribute: {\n";
149 out.indent();
150 status_t status = type->emitVtsTypeDeclarations(out);
151 if (status != OK) {
152 return status;
153 }
154 out.unindent();
155 out << "}\n\n";
156 }
157 return OK;
158}
159
160status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
161 for (const auto &method : mMethods) {
162 out << "api: {\n";
163 out.indent();
164 out << "name: \"" << method->name() << "\"\n";
165 // Generate declaration for each return value.
166 for (const auto &result : method->results()) {
167 out << "return_type_hidl: {\n";
168 out.indent();
169 status_t status = result->type().emitVtsAttributeType(out);
170 if (status != OK) {
171 return status;
172 }
173 out.unindent();
174 out << "}\n";
175 }
176 // Generate declaration for each input argument
177 for (const auto &arg : method->args()) {
178 out << "arg: {\n";
179 out.indent();
180 status_t status = arg->type().emitVtsAttributeType(out);
181 if (status != OK) {
182 return status;
183 }
184 out.unindent();
185 out << "}\n";
186 }
187 // Generate declaration for each annotation.
188 const AnnotationVector & annotations = method->annotations();
189 for (size_t i = 0; i < annotations.size(); i++) {
190 out << "callflow: {\n";
191 out.indent();
192 std::string name = annotations.keyAt(i);
193 if (name == "entry") {
194 out << "entry: true\n";
195 } else if (name == "exit") {
196 out << "exit: true\n";
197 } else if (name == "callflow") {
198 Annotation* annotation = annotations.valueAt(i);
199 std::vector<std::string> * values = annotation->params()
200 .valueFor("next");
201 for (auto value : *values) {
202 out << "next: " << value << "\n";
203 }
204 } else {
205 std::cerr << "Invalid annotation '"
206 << name << "' for method: " << method->name()
207 << ". Should be one of: entry, exit, callflow. \n";
208 return UNKNOWN_ERROR;
209 }
210 out.unindent();
211 out << "}\n";
212 }
213 out.unindent();
214 out << "}\n\n";
215 }
216 return OK;
217}
218
219status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700220 out << "type: TYPE_HIDL_CALLBACK\n"
221 << "predefined_type: \""
222 << localName()
223 << "\"\n";
224 return OK;
225}
226
Andreas Huber70a59e12016-08-16 12:57:01 -0700227bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700228 if (mIsJavaCompatibleInProgress) {
229 // We're currently trying to determine if this Interface is
230 // java-compatible and something is referencing this interface through
231 // one of its methods. Assume we'll ultimately succeed, if we were wrong
232 // the original invocation of Interface::isJavaCompatible() will then
233 // return the correct "false" result.
234 return true;
235 }
236
237 mIsJavaCompatibleInProgress = true;
238
Andreas Huber70a59e12016-08-16 12:57:01 -0700239 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700240 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700241 return false;
242 }
243
244 for (const auto &method : mMethods) {
245 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700246 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700247 return false;
248 }
249 }
250
Andreas Huberea081b32016-08-17 15:57:47 -0700251 mIsJavaCompatibleInProgress = false;
252
Andreas Huber70a59e12016-08-16 12:57:01 -0700253 return true;
254}
255
Andreas Huberc9410c72016-07-28 12:18:40 -0700256} // namespace android
257