blob: a5d3c9be089daea6ee274a1353184c304b31b225 [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 {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -070061 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -070062}
63
Steven Moreland979e0992016-09-07 09:18:08 -070064std::string Interface::getCppType(StorageMode mode,
65 std::string *extra,
66 bool specifyNamespaces) const {
Andreas Huber881227d2016-08-02 14:20:21 -070067 extra->clear();
Steven Moreland979e0992016-09-07 09:18:08 -070068 const std::string base =
69 std::string(specifyNamespaces ? "::android::" : "")
70 + "sp<"
71 + (specifyNamespaces ? fullName() : partialCppName())
72 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -070073
74 switch (mode) {
75 case StorageMode_Stack:
76 case StorageMode_Result:
77 return base;
78
79 case StorageMode_Argument:
80 return "const " + base + "&";
81 }
82}
83
Andreas Huber4c865b72016-09-14 15:26:27 -070084std::string Interface::getJavaType(
85 std::string *extra, bool /* forInitializer */) const {
86 extra->clear();
Andreas Huber2831d512016-08-15 09:33:47 -070087 return fullJavaName();
88}
89
Andreas Huber881227d2016-08-02 14:20:21 -070090void Interface::emitReaderWriter(
91 Formatter &out,
92 const std::string &name,
93 const std::string &parcelObj,
94 bool parcelObjIsPointer,
95 bool isReader,
96 ErrorMode mode) const {
97 const std::string parcelObjDeref =
98 parcelObj + (parcelObjIsPointer ? "->" : ".");
99
100 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700101 out << "{\n";
102 out.indent();
103
Iliyan Malchev549e2592016-08-10 08:59:12 -0700104 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700105
Andreas Huber8a82ff72016-08-04 10:29:39 -0700106 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700107 << binderName << ";\n";
108
Iliyan Malchev549e2592016-08-10 08:59:12 -0700109 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700110 out << parcelObjDeref
111 << "readNullableStrongBinder(&"
112 << binderName
113 << ");\n";
114
115 handleError(out, mode);
116
117 out << name
118 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700119 << fqName().cppNamespace()
120 << "::IHw"
121 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700122 << "::asInterface("
123 << binderName
124 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700125
126 out.unindent();
127 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700128 } else {
Steven Moreland40786312016-08-16 10:29:40 -0700129
130 out << "if (" << name << "->isRemote()) {\n";
131 out.indent();
Iliyan Malchev549e2592016-08-10 08:59:12 -0700132 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700133 out << parcelObjDeref
134 << "writeStrongBinder("
Steven Moreland40786312016-08-16 10:29:40 -0700135 << fqName().cppNamespace()
136 << "::IHw"
137 << getBaseName()
138 << "::asBinder(static_cast<"
139 << fqName().cppNamespace()
140 << "::IHw"
141 << getBaseName()
142 << "*>("
143 << name << ".get()"
144 << ")));\n";
145 out.unindent();
146 out << "} else {\n";
147 out.indent();
148 out << "_hidl_err = ";
149 out << parcelObjDeref
150 << "writeStrongBinder("
151 << "new " << fqName().cppNamespace()
152 << "::Bn" << getBaseName() << " "
153 << "(" << name <<"));\n";
154 out.unindent();
155 out << "}\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700156 handleError(out, mode);
157 }
158}
159
Andreas Huber2831d512016-08-15 09:33:47 -0700160void Interface::emitJavaReaderWriter(
161 Formatter &out,
162 const std::string &parcelObj,
163 const std::string &argName,
164 bool isReader) const {
165 if (isReader) {
166 out << fullJavaName()
167 << ".asInterface("
168 << parcelObj
169 << ".readStrongBinder());\n";
170 } else {
171 out << parcelObj
172 << ".writeStrongBinder("
173 << argName
174 << " == null ? null : "
175 << argName
176 << ".asBinder());\n";
177 }
178}
179
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700180status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
181 for (const auto &type : getSubTypes()) {
182 out << "attribute: {\n";
183 out.indent();
184 status_t status = type->emitVtsTypeDeclarations(out);
185 if (status != OK) {
186 return status;
187 }
188 out.unindent();
189 out << "}\n\n";
190 }
191 return OK;
192}
193
194status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
195 for (const auto &method : mMethods) {
196 out << "api: {\n";
197 out.indent();
198 out << "name: \"" << method->name() << "\"\n";
199 // Generate declaration for each return value.
200 for (const auto &result : method->results()) {
201 out << "return_type_hidl: {\n";
202 out.indent();
203 status_t status = result->type().emitVtsAttributeType(out);
204 if (status != OK) {
205 return status;
206 }
207 out.unindent();
208 out << "}\n";
209 }
210 // Generate declaration for each input argument
211 for (const auto &arg : method->args()) {
212 out << "arg: {\n";
213 out.indent();
214 status_t status = arg->type().emitVtsAttributeType(out);
215 if (status != OK) {
216 return status;
217 }
218 out.unindent();
219 out << "}\n";
220 }
221 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700222 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700223 out << "callflow: {\n";
224 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700225 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700226 if (name == "entry") {
227 out << "entry: true\n";
228 } else if (name == "exit") {
229 out << "exit: true\n";
230 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700231 const AnnotationParam *param =
232 annotation->getParam("next");
233 if (param != nullptr) {
234 for (auto value : *param->getValues()) {
235 out << "next: " << value << "\n";
236 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700237 }
238 } else {
239 std::cerr << "Invalid annotation '"
240 << name << "' for method: " << method->name()
241 << ". Should be one of: entry, exit, callflow. \n";
242 return UNKNOWN_ERROR;
243 }
244 out.unindent();
245 out << "}\n";
246 }
247 out.unindent();
248 out << "}\n\n";
249 }
250 return OK;
251}
252
253status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700254 out << "type: TYPE_HIDL_CALLBACK\n"
255 << "predefined_type: \""
256 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700257 << "\"\n"
258 << "is_callback: true\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700259 return OK;
260}
261
Steven Moreland69e7c702016-09-09 11:16:32 -0700262
263bool Interface::hasOnewayMethods() const {
264 for (auto const &method : mMethods) {
265 if (method->isOneway()) {
266 return true;
267 }
268 }
269
270 const Interface* superClass = superType();
271
272 if (superClass != nullptr) {
273 return superClass->hasOnewayMethods();
274 }
275
276 return false;
277}
278
Andreas Huber70a59e12016-08-16 12:57:01 -0700279bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700280 if (mIsJavaCompatibleInProgress) {
281 // We're currently trying to determine if this Interface is
282 // java-compatible and something is referencing this interface through
283 // one of its methods. Assume we'll ultimately succeed, if we were wrong
284 // the original invocation of Interface::isJavaCompatible() will then
285 // return the correct "false" result.
286 return true;
287 }
288
Andreas Huber0fa9e392016-08-31 09:05:44 -0700289 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
290 mIsJavaCompatibleInProgress = false;
291 return false;
292 }
293
Andreas Huberea081b32016-08-17 15:57:47 -0700294 mIsJavaCompatibleInProgress = true;
295
Andreas Huber70a59e12016-08-16 12:57:01 -0700296 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700297 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700298 return false;
299 }
300
301 for (const auto &method : mMethods) {
302 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700303 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700304 return false;
305 }
306 }
307
Andreas Huberea081b32016-08-17 15:57:47 -0700308 mIsJavaCompatibleInProgress = false;
309
Andreas Huber70a59e12016-08-16 12:57:01 -0700310 return true;
311}
312
Andreas Huberc9410c72016-07-28 12:18:40 -0700313} // namespace android
314