blob: cd9dcd3c604ac1633a41ca08ad5b26f16d56e70c [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 Huber7c5ddfb2016-09-29 13:45:22 -070027Interface::Interface(const char *localName, Interface *super)
Andreas Huber9ed827c2016-08-22 12:31:13 -070028 : Scope(localName),
29 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070030 mIsJavaCompatibleInProgress(false) {
Andreas Huberc9410c72016-07-28 12:18:40 -070031}
32
33void Interface::addMethod(Method *method) {
34 mMethods.push_back(method);
35}
36
Andreas Huber6cb08cf2016-08-03 15:44:51 -070037const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -070038 return mSuperType;
39}
40
Andreas Hubera2723d22016-07-29 15:36:07 -070041bool Interface::isInterface() const {
42 return true;
43}
44
Andreas Huber295ad302016-08-16 11:35:00 -070045bool Interface::isBinder() const {
46 return true;
47}
48
Andreas Huber881227d2016-08-02 14:20:21 -070049const std::vector<Method *> &Interface::methods() const {
50 return mMethods;
51}
52
Steven Moreland40786312016-08-16 10:29:40 -070053std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -070054 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -070055}
56
Steven Moreland979e0992016-09-07 09:18:08 -070057std::string Interface::getCppType(StorageMode mode,
58 std::string *extra,
59 bool specifyNamespaces) const {
Andreas Huber881227d2016-08-02 14:20:21 -070060 extra->clear();
Steven Moreland979e0992016-09-07 09:18:08 -070061 const std::string base =
62 std::string(specifyNamespaces ? "::android::" : "")
63 + "sp<"
64 + (specifyNamespaces ? fullName() : partialCppName())
65 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -070066
67 switch (mode) {
68 case StorageMode_Stack:
69 case StorageMode_Result:
70 return base;
71
72 case StorageMode_Argument:
73 return "const " + base + "&";
74 }
75}
76
Andreas Huber4c865b72016-09-14 15:26:27 -070077std::string Interface::getJavaType(
78 std::string *extra, bool /* forInitializer */) const {
79 extra->clear();
Andreas Huber2831d512016-08-15 09:33:47 -070080 return fullJavaName();
81}
82
Andreas Huber881227d2016-08-02 14:20:21 -070083void Interface::emitReaderWriter(
84 Formatter &out,
85 const std::string &name,
86 const std::string &parcelObj,
87 bool parcelObjIsPointer,
88 bool isReader,
89 ErrorMode mode) const {
90 const std::string parcelObjDeref =
91 parcelObj + (parcelObjIsPointer ? "->" : ".");
92
93 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -070094 out << "{\n";
95 out.indent();
96
Iliyan Malchev549e2592016-08-10 08:59:12 -070097 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -070098
Andreas Huber8a82ff72016-08-04 10:29:39 -070099 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700100 << binderName << ";\n";
101
Iliyan Malchev549e2592016-08-10 08:59:12 -0700102 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700103 out << parcelObjDeref
104 << "readNullableStrongBinder(&"
105 << binderName
106 << ");\n";
107
108 handleError(out, mode);
109
110 out << name
111 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700112 << fqName().cppNamespace()
113 << "::IHw"
114 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700115 << "::asInterface("
116 << binderName
117 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700118
119 out.unindent();
120 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700121 } else {
Steven Moreland40786312016-08-16 10:29:40 -0700122
123 out << "if (" << name << "->isRemote()) {\n";
124 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700125 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700126 out << parcelObjDeref
127 << "writeStrongBinder("
Steven Moreland40786312016-08-16 10:29:40 -0700128 << fqName().cppNamespace()
129 << "::IHw"
130 << getBaseName()
131 << "::asBinder(static_cast<"
132 << fqName().cppNamespace()
133 << "::IHw"
134 << getBaseName()
135 << "*>("
136 << name << ".get()"
137 << ")));\n";
138 out.unindent();
139 out << "} else {\n";
140 out.indent();
141 out << "_hidl_err = ";
142 out << parcelObjDeref
143 << "writeStrongBinder("
144 << "new " << fqName().cppNamespace()
145 << "::Bn" << getBaseName() << " "
146 << "(" << name <<"));\n";
147 out.unindent();
148 out << "}\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700149 handleError(out, mode);
150 }
151}
152
Andreas Huber2831d512016-08-15 09:33:47 -0700153void Interface::emitJavaReaderWriter(
154 Formatter &out,
155 const std::string &parcelObj,
156 const std::string &argName,
157 bool isReader) const {
158 if (isReader) {
159 out << fullJavaName()
160 << ".asInterface("
161 << parcelObj
162 << ".readStrongBinder());\n";
163 } else {
164 out << parcelObj
165 << ".writeStrongBinder("
166 << argName
167 << " == null ? null : "
168 << argName
169 << ".asBinder());\n";
170 }
171}
172
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700173status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
174 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700175 // Skip for TypeDef as it is just an alias of a defined type.
176 if (type->isTypeDef()) {
177 continue;
178 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700179 out << "attribute: {\n";
180 out.indent();
181 status_t status = type->emitVtsTypeDeclarations(out);
182 if (status != OK) {
183 return status;
184 }
185 out.unindent();
186 out << "}\n\n";
187 }
188 return OK;
189}
190
191status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
192 for (const auto &method : mMethods) {
193 out << "api: {\n";
194 out.indent();
195 out << "name: \"" << method->name() << "\"\n";
196 // Generate declaration for each return value.
197 for (const auto &result : method->results()) {
198 out << "return_type_hidl: {\n";
199 out.indent();
200 status_t status = result->type().emitVtsAttributeType(out);
201 if (status != OK) {
202 return status;
203 }
204 out.unindent();
205 out << "}\n";
206 }
207 // Generate declaration for each input argument
208 for (const auto &arg : method->args()) {
209 out << "arg: {\n";
210 out.indent();
211 status_t status = arg->type().emitVtsAttributeType(out);
212 if (status != OK) {
213 return status;
214 }
215 out.unindent();
216 out << "}\n";
217 }
218 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700219 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700220 out << "callflow: {\n";
221 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700222 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700223 if (name == "entry") {
224 out << "entry: true\n";
225 } else if (name == "exit") {
226 out << "exit: true\n";
227 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700228 const AnnotationParam *param =
229 annotation->getParam("next");
230 if (param != nullptr) {
231 for (auto value : *param->getValues()) {
232 out << "next: " << value << "\n";
233 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700234 }
235 } else {
236 std::cerr << "Invalid annotation '"
237 << name << "' for method: " << method->name()
238 << ". Should be one of: entry, exit, callflow. \n";
239 return UNKNOWN_ERROR;
240 }
241 out.unindent();
242 out << "}\n";
243 }
244 out.unindent();
245 out << "}\n\n";
246 }
247 return OK;
248}
249
250status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700251 out << "type: TYPE_HIDL_CALLBACK\n"
252 << "predefined_type: \""
253 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700254 << "\"\n"
255 << "is_callback: true\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700256 return OK;
257}
258
Steven Moreland69e7c702016-09-09 11:16:32 -0700259
260bool Interface::hasOnewayMethods() const {
261 for (auto const &method : mMethods) {
262 if (method->isOneway()) {
263 return true;
264 }
265 }
266
267 const Interface* superClass = superType();
268
269 if (superClass != nullptr) {
270 return superClass->hasOnewayMethods();
271 }
272
273 return false;
274}
275
Andreas Huber70a59e12016-08-16 12:57:01 -0700276bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700277 if (mIsJavaCompatibleInProgress) {
278 // We're currently trying to determine if this Interface is
279 // java-compatible and something is referencing this interface through
280 // one of its methods. Assume we'll ultimately succeed, if we were wrong
281 // the original invocation of Interface::isJavaCompatible() will then
282 // return the correct "false" result.
283 return true;
284 }
285
Andreas Huber0fa9e392016-08-31 09:05:44 -0700286 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
287 mIsJavaCompatibleInProgress = false;
288 return false;
289 }
290
Andreas Huberea081b32016-08-17 15:57:47 -0700291 mIsJavaCompatibleInProgress = true;
292
Andreas Huber70a59e12016-08-16 12:57:01 -0700293 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700294 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700295 return false;
296 }
297
298 for (const auto &method : mMethods) {
299 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700300 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700301 return false;
302 }
303 }
304
Andreas Huberea081b32016-08-17 15:57:47 -0700305 mIsJavaCompatibleInProgress = false;
306
Andreas Huber70a59e12016-08-16 12:57:01 -0700307 return true;
308}
309
Andreas Huberc9410c72016-07-28 12:18:40 -0700310} // namespace android
311