blob: 52d960e33822640f2068a22c6abc905cc1c176f2 [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
Steven Moreland40786312016-08-16 10:29:40 -070059std::string Interface::getBaseName() const {
60 // cut off the leading 'I'.
61 return localName().substr(1);
62}
63
Andreas Huber881227d2016-08-02 14:20:21 -070064std::string Interface::getCppType(StorageMode mode, std::string *extra) const {
65 extra->clear();
Andreas Huber31629bc2016-08-03 09:06:40 -070066 const std::string base = "::android::sp<" + fullName() + ">";
Andreas Huber881227d2016-08-02 14:20:21 -070067
68 switch (mode) {
69 case StorageMode_Stack:
70 case StorageMode_Result:
71 return base;
72
73 case StorageMode_Argument:
74 return "const " + base + "&";
75 }
76}
77
Andreas Huber2831d512016-08-15 09:33:47 -070078std::string Interface::getJavaType() const {
79 return fullJavaName();
80}
81
Andreas Huber881227d2016-08-02 14:20:21 -070082void Interface::emitReaderWriter(
83 Formatter &out,
84 const std::string &name,
85 const std::string &parcelObj,
86 bool parcelObjIsPointer,
87 bool isReader,
88 ErrorMode mode) const {
89 const std::string parcelObjDeref =
90 parcelObj + (parcelObjIsPointer ? "->" : ".");
91
92 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -070093 out << "{\n";
94 out.indent();
95
Iliyan Malchev549e2592016-08-10 08:59:12 -070096 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -070097
Andreas Huber8a82ff72016-08-04 10:29:39 -070098 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -070099 << binderName << ";\n";
100
Iliyan Malchev549e2592016-08-10 08:59:12 -0700101 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700102 out << parcelObjDeref
103 << "readNullableStrongBinder(&"
104 << binderName
105 << ");\n";
106
107 handleError(out, mode);
108
109 out << name
110 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700111 << fqName().cppNamespace()
112 << "::IHw"
113 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700114 << "::asInterface("
115 << binderName
116 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700117
118 out.unindent();
119 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700120 } else {
Steven Moreland40786312016-08-16 10:29:40 -0700121
122 out << "if (" << name << "->isRemote()) {\n";
123 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700124 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700125 out << parcelObjDeref
126 << "writeStrongBinder("
Steven Moreland40786312016-08-16 10:29:40 -0700127 << fqName().cppNamespace()
128 << "::IHw"
129 << getBaseName()
130 << "::asBinder(static_cast<"
131 << fqName().cppNamespace()
132 << "::IHw"
133 << getBaseName()
134 << "*>("
135 << name << ".get()"
136 << ")));\n";
137 out.unindent();
138 out << "} else {\n";
139 out.indent();
140 out << "_hidl_err = ";
141 out << parcelObjDeref
142 << "writeStrongBinder("
143 << "new " << fqName().cppNamespace()
144 << "::Bn" << getBaseName() << " "
145 << "(" << name <<"));\n";
146 out.unindent();
147 out << "}\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700148 handleError(out, mode);
149 }
150}
151
Andreas Huber2831d512016-08-15 09:33:47 -0700152void Interface::emitJavaReaderWriter(
153 Formatter &out,
154 const std::string &parcelObj,
155 const std::string &argName,
156 bool isReader) const {
157 if (isReader) {
158 out << fullJavaName()
159 << ".asInterface("
160 << parcelObj
161 << ".readStrongBinder());\n";
162 } else {
163 out << parcelObj
164 << ".writeStrongBinder("
165 << argName
166 << " == null ? null : "
167 << argName
168 << ".asBinder());\n";
169 }
170}
171
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700172status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
173 for (const auto &type : getSubTypes()) {
174 out << "attribute: {\n";
175 out.indent();
176 status_t status = type->emitVtsTypeDeclarations(out);
177 if (status != OK) {
178 return status;
179 }
180 out.unindent();
181 out << "}\n\n";
182 }
183 return OK;
184}
185
186status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
187 for (const auto &method : mMethods) {
188 out << "api: {\n";
189 out.indent();
190 out << "name: \"" << method->name() << "\"\n";
191 // Generate declaration for each return value.
192 for (const auto &result : method->results()) {
193 out << "return_type_hidl: {\n";
194 out.indent();
195 status_t status = result->type().emitVtsAttributeType(out);
196 if (status != OK) {
197 return status;
198 }
199 out.unindent();
200 out << "}\n";
201 }
202 // Generate declaration for each input argument
203 for (const auto &arg : method->args()) {
204 out << "arg: {\n";
205 out.indent();
206 status_t status = arg->type().emitVtsAttributeType(out);
207 if (status != OK) {
208 return status;
209 }
210 out.unindent();
211 out << "}\n";
212 }
213 // Generate declaration for each annotation.
214 const AnnotationVector & annotations = method->annotations();
215 for (size_t i = 0; i < annotations.size(); i++) {
216 out << "callflow: {\n";
217 out.indent();
218 std::string name = annotations.keyAt(i);
219 if (name == "entry") {
220 out << "entry: true\n";
221 } else if (name == "exit") {
222 out << "exit: true\n";
223 } else if (name == "callflow") {
224 Annotation* annotation = annotations.valueAt(i);
225 std::vector<std::string> * values = annotation->params()
226 .valueFor("next");
227 for (auto value : *values) {
228 out << "next: " << value << "\n";
229 }
230 } else {
231 std::cerr << "Invalid annotation '"
232 << name << "' for method: " << method->name()
233 << ". Should be one of: entry, exit, callflow. \n";
234 return UNKNOWN_ERROR;
235 }
236 out.unindent();
237 out << "}\n";
238 }
239 out.unindent();
240 out << "}\n\n";
241 }
242 return OK;
243}
244
245status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700246 out << "type: TYPE_HIDL_CALLBACK\n"
247 << "predefined_type: \""
248 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700249 << "\"\n"
250 << "is_callback: true\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700251 return OK;
252}
253
Andreas Huber70a59e12016-08-16 12:57:01 -0700254bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700255 if (mIsJavaCompatibleInProgress) {
256 // We're currently trying to determine if this Interface is
257 // java-compatible and something is referencing this interface through
258 // one of its methods. Assume we'll ultimately succeed, if we were wrong
259 // the original invocation of Interface::isJavaCompatible() will then
260 // return the correct "false" result.
261 return true;
262 }
263
Andreas Huber0fa9e392016-08-31 09:05:44 -0700264 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
265 mIsJavaCompatibleInProgress = false;
266 return false;
267 }
268
Andreas Huberea081b32016-08-17 15:57:47 -0700269 mIsJavaCompatibleInProgress = true;
270
Andreas Huber70a59e12016-08-16 12:57:01 -0700271 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700272 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700273 return false;
274 }
275
276 for (const auto &method : mMethods) {
277 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700278 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700279 return false;
280 }
281 }
282
Andreas Huberea081b32016-08-17 15:57:47 -0700283 mIsJavaCompatibleInProgress = false;
284
Andreas Huber70a59e12016-08-16 12:57:01 -0700285 return true;
286}
287
Andreas Huberc9410c72016-07-28 12:18:40 -0700288} // namespace android
289