blob: 0fe7e872cbc76a34ef07768a6ab03934f4d90f0b [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 "Method.h"
21
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070022#include <hidl-util/Formatter.h>
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(
Steven Morelandd537ab02016-09-12 10:32:01 -070028 const char *localName, Interface *super,
29 std::vector<Annotation *> *annotations)
Andreas Huber9ed827c2016-08-22 12:31:13 -070030 : Scope(localName),
31 mSuperType(super),
Steven Morelandd537ab02016-09-12 10:32:01 -070032 mAnnotations(annotations),
Andreas Huberea081b32016-08-17 15:57:47 -070033 mIsJavaCompatibleInProgress(false) {
Andreas Huberc9410c72016-07-28 12:18:40 -070034}
35
36void Interface::addMethod(Method *method) {
37 mMethods.push_back(method);
38}
39
Andreas Huber6cb08cf2016-08-03 15:44:51 -070040const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -070041 return mSuperType;
42}
43
Andreas Hubera2723d22016-07-29 15:36:07 -070044bool Interface::isInterface() const {
45 return true;
46}
47
Andreas Huber295ad302016-08-16 11:35:00 -070048bool Interface::isBinder() const {
49 return true;
50}
51
Andreas Huber881227d2016-08-02 14:20:21 -070052const std::vector<Method *> &Interface::methods() const {
53 return mMethods;
54}
55
Steven Morelandd537ab02016-09-12 10:32:01 -070056const std::vector<Annotation *> &Interface::annotations() const {
57 return *mAnnotations;
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070058}
59
Steven Moreland40786312016-08-16 10:29:40 -070060std::string Interface::getBaseName() const {
61 // cut off the leading 'I'.
62 return localName().substr(1);
63}
64
Steven Moreland979e0992016-09-07 09:18:08 -070065std::string Interface::getCppType(StorageMode mode,
66 std::string *extra,
67 bool specifyNamespaces) const {
Andreas Huber881227d2016-08-02 14:20:21 -070068 extra->clear();
Steven Moreland979e0992016-09-07 09:18:08 -070069 const std::string base =
70 std::string(specifyNamespaces ? "::android::" : "")
71 + "sp<"
72 + (specifyNamespaces ? fullName() : partialCppName())
73 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -070074
75 switch (mode) {
76 case StorageMode_Stack:
77 case StorageMode_Result:
78 return base;
79
80 case StorageMode_Argument:
81 return "const " + base + "&";
82 }
83}
84
Andreas Huber4c865b72016-09-14 15:26:27 -070085std::string Interface::getJavaType(
86 std::string *extra, bool /* forInitializer */) const {
87 extra->clear();
Andreas Huber2831d512016-08-15 09:33:47 -070088 return fullJavaName();
89}
90
Andreas Huber881227d2016-08-02 14:20:21 -070091void Interface::emitReaderWriter(
92 Formatter &out,
93 const std::string &name,
94 const std::string &parcelObj,
95 bool parcelObjIsPointer,
96 bool isReader,
97 ErrorMode mode) const {
98 const std::string parcelObjDeref =
99 parcelObj + (parcelObjIsPointer ? "->" : ".");
100
101 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700102 out << "{\n";
103 out.indent();
104
Iliyan Malchev549e2592016-08-10 08:59:12 -0700105 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700106
Andreas Huber8a82ff72016-08-04 10:29:39 -0700107 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700108 << binderName << ";\n";
109
Iliyan Malchev549e2592016-08-10 08:59:12 -0700110 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700111 out << parcelObjDeref
112 << "readNullableStrongBinder(&"
113 << binderName
114 << ");\n";
115
116 handleError(out, mode);
117
118 out << name
119 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700120 << fqName().cppNamespace()
121 << "::IHw"
122 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700123 << "::asInterface("
124 << binderName
125 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700126
127 out.unindent();
128 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700129 } else {
Steven Moreland40786312016-08-16 10:29:40 -0700130
131 out << "if (" << name << "->isRemote()) {\n";
132 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700133 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700134 out << parcelObjDeref
135 << "writeStrongBinder("
Steven Moreland40786312016-08-16 10:29:40 -0700136 << fqName().cppNamespace()
137 << "::IHw"
138 << getBaseName()
139 << "::asBinder(static_cast<"
140 << fqName().cppNamespace()
141 << "::IHw"
142 << getBaseName()
143 << "*>("
144 << name << ".get()"
145 << ")));\n";
146 out.unindent();
147 out << "} else {\n";
148 out.indent();
149 out << "_hidl_err = ";
150 out << parcelObjDeref
151 << "writeStrongBinder("
152 << "new " << fqName().cppNamespace()
153 << "::Bn" << getBaseName() << " "
154 << "(" << name <<"));\n";
155 out.unindent();
156 out << "}\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700157 handleError(out, mode);
158 }
159}
160
Andreas Huber2831d512016-08-15 09:33:47 -0700161void Interface::emitJavaReaderWriter(
162 Formatter &out,
163 const std::string &parcelObj,
164 const std::string &argName,
165 bool isReader) const {
166 if (isReader) {
167 out << fullJavaName()
168 << ".asInterface("
169 << parcelObj
170 << ".readStrongBinder());\n";
171 } else {
172 out << parcelObj
173 << ".writeStrongBinder("
174 << argName
175 << " == null ? null : "
176 << argName
177 << ".asBinder());\n";
178 }
179}
180
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700181status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
182 for (const auto &type : getSubTypes()) {
183 out << "attribute: {\n";
184 out.indent();
185 status_t status = type->emitVtsTypeDeclarations(out);
186 if (status != OK) {
187 return status;
188 }
189 out.unindent();
190 out << "}\n\n";
191 }
192 return OK;
193}
194
195status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
196 for (const auto &method : mMethods) {
197 out << "api: {\n";
198 out.indent();
199 out << "name: \"" << method->name() << "\"\n";
200 // Generate declaration for each return value.
201 for (const auto &result : method->results()) {
202 out << "return_type_hidl: {\n";
203 out.indent();
204 status_t status = result->type().emitVtsAttributeType(out);
205 if (status != OK) {
206 return status;
207 }
208 out.unindent();
209 out << "}\n";
210 }
211 // Generate declaration for each input argument
212 for (const auto &arg : method->args()) {
213 out << "arg: {\n";
214 out.indent();
215 status_t status = arg->type().emitVtsAttributeType(out);
216 if (status != OK) {
217 return status;
218 }
219 out.unindent();
220 out << "}\n";
221 }
222 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700223 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700224 out << "callflow: {\n";
225 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700226 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700227 if (name == "entry") {
228 out << "entry: true\n";
229 } else if (name == "exit") {
230 out << "exit: true\n";
231 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700232 const AnnotationParam *param =
233 annotation->getParam("next");
234 if (param != nullptr) {
235 for (auto value : *param->getValues()) {
236 out << "next: " << value << "\n";
237 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700238 }
239 } else {
240 std::cerr << "Invalid annotation '"
241 << name << "' for method: " << method->name()
242 << ". Should be one of: entry, exit, callflow. \n";
243 return UNKNOWN_ERROR;
244 }
245 out.unindent();
246 out << "}\n";
247 }
248 out.unindent();
249 out << "}\n\n";
250 }
251 return OK;
252}
253
254status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700255 out << "type: TYPE_HIDL_CALLBACK\n"
256 << "predefined_type: \""
257 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700258 << "\"\n"
259 << "is_callback: true\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700260 return OK;
261}
262
Steven Moreland69e7c702016-09-09 11:16:32 -0700263
264bool Interface::hasOnewayMethods() const {
265 for (auto const &method : mMethods) {
266 if (method->isOneway()) {
267 return true;
268 }
269 }
270
271 const Interface* superClass = superType();
272
273 if (superClass != nullptr) {
274 return superClass->hasOnewayMethods();
275 }
276
277 return false;
278}
279
Andreas Huber70a59e12016-08-16 12:57:01 -0700280bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700281 if (mIsJavaCompatibleInProgress) {
282 // We're currently trying to determine if this Interface is
283 // java-compatible and something is referencing this interface through
284 // one of its methods. Assume we'll ultimately succeed, if we were wrong
285 // the original invocation of Interface::isJavaCompatible() will then
286 // return the correct "false" result.
287 return true;
288 }
289
Andreas Huber0fa9e392016-08-31 09:05:44 -0700290 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
291 mIsJavaCompatibleInProgress = false;
292 return false;
293 }
294
Andreas Huberea081b32016-08-17 15:57:47 -0700295 mIsJavaCompatibleInProgress = true;
296
Andreas Huber70a59e12016-08-16 12:57:01 -0700297 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700298 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700299 return false;
300 }
301
302 for (const auto &method : mMethods) {
303 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700304 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700305 return false;
306 }
307 }
308
Andreas Huberea081b32016-08-17 15:57:47 -0700309 mIsJavaCompatibleInProgress = false;
310
Andreas Huber70a59e12016-08-16 12:57:01 -0700311 return true;
312}
313
Andreas Huberc9410c72016-07-28 12:18:40 -0700314} // namespace android
315