blob: 302ce73649e02d8e560c84a26cee9e46a7630077 [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"
Yifan Hong10fe0b52016-10-19 14:20:17 -070021#include "StringType.h"
22#include "VectorType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023
Steven Moreland14ee6742016-10-18 12:58:28 -070024#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070025#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070026#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070027#include <iostream>
Yifan Hong10fe0b52016-10-19 14:20:17 -070028#include <sstream>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070029
Andreas Huberc9410c72016-07-28 12:18:40 -070030namespace android {
31
Yifan Hong10fe0b52016-10-19 14:20:17 -070032/* It is very important that these values NEVER change. These values
33 * must remain unchanged over the lifetime of android. This is
34 * because the framework on a device will be updated independently of
35 * the hals on a device. If the hals are compiled with one set of
36 * transaction values, and the framework with another, then the
37 * interface between them will be destroyed, and the device will not
38 * work.
39 */
40enum {
41 // These values are defined in hardware::IBinder.
42 /////////////////// User defined transactions
43 FIRST_CALL_TRANSACTION = 0x00000001,
44 LAST_CALL_TRANSACTION = 0x00efffff,
45 /////////////////// HIDL reserved
46 FIRST_HIDL_TRANSACTION = 0x00f00000,
47 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = FIRST_HIDL_TRANSACTION,
48 LAST_HIDL_TRANSACTION = 0x00ffffff,
49};
50
Yifan Honga4b53d02016-10-31 17:29:10 -070051Interface::Interface(const char *localName, const Location &location, Interface *super)
52 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070053 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070054 mIsJavaCompatibleInProgress(false) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070055 mReservedMethods.push_back(createDescriptorChainMethod());
Andreas Huberc9410c72016-07-28 12:18:40 -070056}
57
Yifan Hong10fe0b52016-10-19 14:20:17 -070058Method *Interface::createDescriptorChainMethod() const {
59 VectorType *vecType = new VectorType();
60 vecType->setElementType(new StringType());
61 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
62 results->push_back(new TypedVar("indicator", vecType));
63
64 return new Method("interfaceChain",
65 new std::vector<TypedVar *>() /* args */,
66 results,
67 false /* oneway */,
68 new std::vector<Annotation *>(),
69 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
70 [this](auto &out) { /* cppImpl */
71 std::vector<const Interface *> chain = typeChain();
72 out << "::android::hardware::hidl_vec<::android::hardware::hidl_string> _hidl_return;\n";
73 out << "_hidl_return.resize(" << chain.size() << ");\n";
74 for (size_t i = 0; i < chain.size(); ++i) {
75 out << "_hidl_return[" << i << "] = \"" << chain[i]->fqName().string() << "\";\n";
76 }
77 out << "_hidl_cb(_hidl_return);\n";
78 out << "return ::android::hardware::Void();";
79 },
80 [this](auto &out) { /* javaImpl */
81 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -080082 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -070083 out.indent(); out.indent();
84 for (size_t i = 0; i < chain.size(); ++i) {
85 if (i != 0)
86 out << ",\n";
87 out << "\"" << chain[i]->fqName().string() << "\"";
88 }
89 out << "));";
90 out.unindent(); out.unindent();
91 });
92}
93
94
Steven Moreland14ee6742016-10-18 12:58:28 -070095bool Interface::addMethod(Method *method) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070096 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -070097 if (lookupMethod(method->name()) != nullptr) {
98 LOG(ERROR) << "Redefinition of method " << method->name();
99 return false;
100 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700101 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700102
Yifan Hong10fe0b52016-10-19 14:20:17 -0700103 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700104
Yifan Hong10fe0b52016-10-19 14:20:17 -0700105 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700106 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700107 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700108 ancestor = ancestor->superType();
109 }
110
Yifan Hong10fe0b52016-10-19 14:20:17 -0700111 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
112 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700113 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700114 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700115
116 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700117}
118
Yifan Hong10fe0b52016-10-19 14:20:17 -0700119
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700120const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700121 return mSuperType;
122}
123
Yifan Hong10fe0b52016-10-19 14:20:17 -0700124std::vector<const Interface *> Interface::typeChain() const {
125 std::vector<const Interface *> v;
126 const Interface *iface = this;
127 while (iface != nullptr) {
128 v.push_back(iface);
129 iface = iface->mSuperType;
130 }
131 return v;
132}
133
Yifan Hongfe95aa22016-10-19 17:26:45 -0700134std::vector<const Interface *> Interface::superTypeChain() const {
135 return superType()->typeChain(); // should work even if superType is nullptr
136}
137
Andreas Hubera2723d22016-07-29 15:36:07 -0700138bool Interface::isInterface() const {
139 return true;
140}
141
Andreas Huber295ad302016-08-16 11:35:00 -0700142bool Interface::isBinder() const {
143 return true;
144}
145
Yifan Hong10fe0b52016-10-19 14:20:17 -0700146const std::vector<Method *> &Interface::userDefinedMethods() const {
147 return mUserMethods;
148}
149
150const std::vector<Method *> &Interface::hidlReservedMethods() const {
151 return mReservedMethods;
152}
153
154std::vector<Method *> Interface::methods() const {
155 std::vector<Method *> v(mUserMethods);
156 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
157 return v;
158}
159
160std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
161 std::vector<InterfaceAndMethod> v;
162 std::vector<const Interface *> chain = typeChain();
163 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
164 const Interface *iface = *it;
165 for (Method *userMethod : iface->userDefinedMethods()) {
166 v.push_back(InterfaceAndMethod(iface, userMethod));
167 }
168 }
169 for (Method *reservedMethod : hidlReservedMethods()) {
170 v.push_back(InterfaceAndMethod(this, reservedMethod));
171 }
172 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700173}
174
Steven Moreland14ee6742016-10-18 12:58:28 -0700175Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700176 for (const auto &tuple : allMethodsFromRoot()) {
177 Method *method = tuple.method();
178 if (method->name() == name) {
179 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700180 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700181 }
182
183 return nullptr;
184}
185
Steven Moreland40786312016-08-16 10:29:40 -0700186std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700187 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700188}
189
Yifan Hong158655a2016-11-08 12:34:07 -0800190FQName Interface::getHwName() const {
191 return FQName(fqName().package(), fqName().version(), "IHw" + getBaseName());
192}
193
Yifan Hong60e52bd2016-11-09 14:47:45 -0800194FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800195 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
196}
197
Yifan Hong60e52bd2016-11-09 14:47:45 -0800198FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800199 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
200}
201
Yifan Hong60e52bd2016-11-09 14:47:45 -0800202FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800203 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
204}
205
206
Steven Moreland979e0992016-09-07 09:18:08 -0700207std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700208 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700209 const std::string base =
210 std::string(specifyNamespaces ? "::android::" : "")
211 + "sp<"
212 + (specifyNamespaces ? fullName() : partialCppName())
213 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700214
215 switch (mode) {
216 case StorageMode_Stack:
217 case StorageMode_Result:
218 return base;
219
220 case StorageMode_Argument:
221 return "const " + base + "&";
222 }
223}
224
Yifan Hong4ed13472016-11-02 10:44:11 -0700225std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700226 return fullJavaName();
227}
228
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800229std::string Interface::getVtsType() const {
230 if (StringHelper::EndsWith(localName(), "Callback")) {
231 return "TYPE_HIDL_CALLBACK";
232 } else {
233 return "TYPE_HIDL_INTERFACE";
234 }
235}
236
Andreas Huber881227d2016-08-02 14:20:21 -0700237void Interface::emitReaderWriter(
238 Formatter &out,
239 const std::string &name,
240 const std::string &parcelObj,
241 bool parcelObjIsPointer,
242 bool isReader,
243 ErrorMode mode) const {
244 const std::string parcelObjDeref =
245 parcelObj + (parcelObjIsPointer ? "->" : ".");
246
247 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700248 out << "{\n";
249 out.indent();
250
Iliyan Malchev549e2592016-08-10 08:59:12 -0700251 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700252
Andreas Huber8a82ff72016-08-04 10:29:39 -0700253 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700254 << binderName << ";\n";
255
Iliyan Malchev549e2592016-08-10 08:59:12 -0700256 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700257 out << parcelObjDeref
258 << "readNullableStrongBinder(&"
259 << binderName
260 << ");\n";
261
262 handleError(out, mode);
263
264 out << name
265 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700266 << fqName().cppNamespace()
267 << "::IHw"
268 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700269 << "::asInterface("
270 << binderName
271 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700272
273 out.unindent();
274 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700275 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200276 out << "if (" << name << " == nullptr) {\n";
277 out.indent();
278 out << "_hidl_err = ";
279 out << parcelObjDeref
280 << "writeStrongBinder(nullptr);\n";
281 out.unindent();
282 out << "} else {\n";
283 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800284 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
285 << "::android::hardware::toBinder<\n";
286 out.indentBlock(2, [&] {
287 out << fqName().cppNamespace()
288 << "::I"
289 << getBaseName()
290 << ", "
291 << fqName().cppNamespace()
292 << "::IHw"
293 << getBaseName()
294 << ">("
295 << name
296 << ");\n";
297 });
298 out << "if (_hidl_binder.get() != nullptr) {\n";
299 out.indentBlock([&] {
300 out << "_hidl_err = "
301 << parcelObjDeref
302 << "writeStrongBinder(_hidl_binder);\n";
303 });
304 out << "} else {\n";
305 out.indentBlock([&] {
306 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
307 });
308 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200309 out.unindent();
310 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700311
Andreas Huber881227d2016-08-02 14:20:21 -0700312 handleError(out, mode);
313 }
314}
315
Andreas Huber2831d512016-08-15 09:33:47 -0700316void Interface::emitJavaReaderWriter(
317 Formatter &out,
318 const std::string &parcelObj,
319 const std::string &argName,
320 bool isReader) const {
321 if (isReader) {
322 out << fullJavaName()
323 << ".asInterface("
324 << parcelObj
325 << ".readStrongBinder());\n";
326 } else {
327 out << parcelObj
328 << ".writeStrongBinder("
329 << argName
330 << " == null ? null : "
331 << argName
332 << ".asBinder());\n";
333 }
334}
335
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700336status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
337 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700338 // Skip for TypeDef as it is just an alias of a defined type.
339 if (type->isTypeDef()) {
340 continue;
341 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700342 out << "attribute: {\n";
343 out.indent();
344 status_t status = type->emitVtsTypeDeclarations(out);
345 if (status != OK) {
346 return status;
347 }
348 out.unindent();
349 out << "}\n\n";
350 }
351 return OK;
352}
353
354status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700355 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800356 if (method->isHidlReserved()) {
357 continue;
358 }
359
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700360 out << "api: {\n";
361 out.indent();
362 out << "name: \"" << method->name() << "\"\n";
363 // Generate declaration for each return value.
364 for (const auto &result : method->results()) {
365 out << "return_type_hidl: {\n";
366 out.indent();
367 status_t status = result->type().emitVtsAttributeType(out);
368 if (status != OK) {
369 return status;
370 }
371 out.unindent();
372 out << "}\n";
373 }
374 // Generate declaration for each input argument
375 for (const auto &arg : method->args()) {
376 out << "arg: {\n";
377 out.indent();
378 status_t status = arg->type().emitVtsAttributeType(out);
379 if (status != OK) {
380 return status;
381 }
382 out.unindent();
383 out << "}\n";
384 }
385 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700386 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700387 out << "callflow: {\n";
388 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700389 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700390 if (name == "entry") {
391 out << "entry: true\n";
392 } else if (name == "exit") {
393 out << "exit: true\n";
394 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700395 const AnnotationParam *param =
396 annotation->getParam("next");
397 if (param != nullptr) {
398 for (auto value : *param->getValues()) {
399 out << "next: " << value << "\n";
400 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700401 }
402 } else {
403 std::cerr << "Invalid annotation '"
404 << name << "' for method: " << method->name()
405 << ". Should be one of: entry, exit, callflow. \n";
406 return UNKNOWN_ERROR;
407 }
408 out.unindent();
409 out << "}\n";
410 }
411 out.unindent();
412 out << "}\n\n";
413 }
414 return OK;
415}
416
417status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800418 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700419 << "predefined_type: \""
420 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700421 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800422 << "is_callback: "
423 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
424 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700425 return OK;
426}
427
Steven Moreland69e7c702016-09-09 11:16:32 -0700428bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700429 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700430 if (method->isOneway()) {
431 return true;
432 }
433 }
434
435 const Interface* superClass = superType();
436
437 if (superClass != nullptr) {
438 return superClass->hasOnewayMethods();
439 }
440
441 return false;
442}
443
Andreas Huber70a59e12016-08-16 12:57:01 -0700444bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700445 if (mIsJavaCompatibleInProgress) {
446 // We're currently trying to determine if this Interface is
447 // java-compatible and something is referencing this interface through
448 // one of its methods. Assume we'll ultimately succeed, if we were wrong
449 // the original invocation of Interface::isJavaCompatible() will then
450 // return the correct "false" result.
451 return true;
452 }
453
Andreas Huber0fa9e392016-08-31 09:05:44 -0700454 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
455 mIsJavaCompatibleInProgress = false;
456 return false;
457 }
458
Andreas Huberea081b32016-08-17 15:57:47 -0700459 mIsJavaCompatibleInProgress = true;
460
Andreas Huber70a59e12016-08-16 12:57:01 -0700461 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700462 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700463 return false;
464 }
465
Yifan Hong10fe0b52016-10-19 14:20:17 -0700466 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700467 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700468 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700469 return false;
470 }
471 }
472
Andreas Huberea081b32016-08-17 15:57:47 -0700473 mIsJavaCompatibleInProgress = false;
474
Andreas Huber70a59e12016-08-16 12:57:01 -0700475 return true;
476}
477
Andreas Huberc9410c72016-07-28 12:18:40 -0700478} // namespace android
479