blob: 602b221ee75e7feb25185ffc38b0bd3027c0dc4d [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,
Martijn Coenenaf712c02016-11-16 15:26:27 +010048 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yifan Hong10fe0b52016-10-19 14:20:17 -070049 LAST_HIDL_TRANSACTION = 0x00ffffff,
50};
51
Yifan Honga4b53d02016-10-31 17:29:10 -070052Interface::Interface(const char *localName, const Location &location, Interface *super)
53 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070054 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070055 mIsJavaCompatibleInProgress(false) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070056 mReservedMethods.push_back(createDescriptorChainMethod());
Martijn Coenenaf712c02016-11-16 15:26:27 +010057 mReservedMethods.push_back(createSyspropsChangedMethod());
58}
59
Steven Moreland30bb6a82016-11-30 09:18:34 -080060std::string Interface::typeName() const {
61 return "interface " + localName();
62}
63
Martijn Coenenaf712c02016-11-16 15:26:27 +010064Method *Interface::createSyspropsChangedMethod() const {
65 return new Method("notifySyspropsChanged",
66 new std::vector<TypedVar *>() /*args */,
67 new std::vector<TypedVar *>() /*results */,
68 true /*oneway */,
69 new std::vector<Annotation *>(),
70 HIDL_SYSPROPS_CHANGED_TRANSACTION,
71 [this](auto &out) { /*cppImpl */
72 out << "::android::report_sysprop_change();\n";
73 out << "return ::android::hardware::Void();";
74 },
75 [this](auto &out) { /* javaImpl */
76 out << "android.os.SystemProperties.reportSyspropChanged();";
77 }
78 );
Andreas Huberc9410c72016-07-28 12:18:40 -070079}
80
Yifan Hong10fe0b52016-10-19 14:20:17 -070081Method *Interface::createDescriptorChainMethod() const {
82 VectorType *vecType = new VectorType();
83 vecType->setElementType(new StringType());
84 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
85 results->push_back(new TypedVar("indicator", vecType));
86
87 return new Method("interfaceChain",
88 new std::vector<TypedVar *>() /* args */,
89 results,
90 false /* oneway */,
91 new std::vector<Annotation *>(),
92 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
93 [this](auto &out) { /* cppImpl */
94 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -080095 out << "_hidl_cb(";
96 out.block([&] {
97 for (const Interface *iface : chain) {
98 out << iface->fullName() << "::descriptor,\n";
99 }
100 });
101 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700102 out << "return ::android::hardware::Void();";
103 },
104 [this](auto &out) { /* javaImpl */
105 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800106 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700107 out.indent(); out.indent();
108 for (size_t i = 0; i < chain.size(); ++i) {
109 if (i != 0)
110 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800111 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700112 }
113 out << "));";
114 out.unindent(); out.unindent();
115 });
116}
117
118
Steven Moreland14ee6742016-10-18 12:58:28 -0700119bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800120 if (isIBase()) {
121 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
122 return true;
123 }
124
Yifan Hong10fe0b52016-10-19 14:20:17 -0700125 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700126 if (lookupMethod(method->name()) != nullptr) {
127 LOG(ERROR) << "Redefinition of method " << method->name();
128 return false;
129 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700130 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700131
Yifan Hong10fe0b52016-10-19 14:20:17 -0700132 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700133
Yifan Hong10fe0b52016-10-19 14:20:17 -0700134 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700135 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700136 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700137 ancestor = ancestor->superType();
138 }
139
Yifan Hong10fe0b52016-10-19 14:20:17 -0700140 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
141 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700142 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700143 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700144
145 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700146}
147
Yifan Hong10fe0b52016-10-19 14:20:17 -0700148
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700149const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700150 return mSuperType;
151}
152
Yifan Hong10fe0b52016-10-19 14:20:17 -0700153std::vector<const Interface *> Interface::typeChain() const {
154 std::vector<const Interface *> v;
155 const Interface *iface = this;
156 while (iface != nullptr) {
157 v.push_back(iface);
158 iface = iface->mSuperType;
159 }
160 return v;
161}
162
Yifan Hongfe95aa22016-10-19 17:26:45 -0700163std::vector<const Interface *> Interface::superTypeChain() const {
164 return superType()->typeChain(); // should work even if superType is nullptr
165}
166
Andreas Hubera2723d22016-07-29 15:36:07 -0700167bool Interface::isInterface() const {
168 return true;
169}
170
Andreas Huber295ad302016-08-16 11:35:00 -0700171bool Interface::isBinder() const {
172 return true;
173}
174
Yifan Hong10fe0b52016-10-19 14:20:17 -0700175const std::vector<Method *> &Interface::userDefinedMethods() const {
176 return mUserMethods;
177}
178
179const std::vector<Method *> &Interface::hidlReservedMethods() const {
180 return mReservedMethods;
181}
182
183std::vector<Method *> Interface::methods() const {
184 std::vector<Method *> v(mUserMethods);
185 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
186 return v;
187}
188
189std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
190 std::vector<InterfaceAndMethod> v;
191 std::vector<const Interface *> chain = typeChain();
192 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
193 const Interface *iface = *it;
194 for (Method *userMethod : iface->userDefinedMethods()) {
195 v.push_back(InterfaceAndMethod(iface, userMethod));
196 }
197 }
198 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800199 v.push_back(InterfaceAndMethod(
200 *chain.rbegin(), // IBase
201 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700202 }
203 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700204}
205
Steven Moreland14ee6742016-10-18 12:58:28 -0700206Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700207 for (const auto &tuple : allMethodsFromRoot()) {
208 Method *method = tuple.method();
209 if (method->name() == name) {
210 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700211 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700212 }
213
214 return nullptr;
215}
216
Steven Moreland40786312016-08-16 10:29:40 -0700217std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700218 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700219}
220
Yifan Hong60e52bd2016-11-09 14:47:45 -0800221FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800222 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
223}
224
Yifan Hong60e52bd2016-11-09 14:47:45 -0800225FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800226 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
227}
228
Yifan Hong60e52bd2016-11-09 14:47:45 -0800229FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800230 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
231}
232
233
Steven Moreland979e0992016-09-07 09:18:08 -0700234std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700235 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700236 const std::string base =
237 std::string(specifyNamespaces ? "::android::" : "")
238 + "sp<"
239 + (specifyNamespaces ? fullName() : partialCppName())
240 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700241
242 switch (mode) {
243 case StorageMode_Stack:
244 case StorageMode_Result:
245 return base;
246
247 case StorageMode_Argument:
248 return "const " + base + "&";
249 }
250}
251
Yifan Hong4ed13472016-11-02 10:44:11 -0700252std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700253 return fullJavaName();
254}
255
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800256std::string Interface::getVtsType() const {
257 if (StringHelper::EndsWith(localName(), "Callback")) {
258 return "TYPE_HIDL_CALLBACK";
259 } else {
260 return "TYPE_HIDL_INTERFACE";
261 }
262}
263
Andreas Huber881227d2016-08-02 14:20:21 -0700264void Interface::emitReaderWriter(
265 Formatter &out,
266 const std::string &name,
267 const std::string &parcelObj,
268 bool parcelObjIsPointer,
269 bool isReader,
270 ErrorMode mode) const {
271 const std::string parcelObjDeref =
272 parcelObj + (parcelObjIsPointer ? "->" : ".");
273
274 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700275 out << "{\n";
276 out.indent();
277
Iliyan Malchev549e2592016-08-10 08:59:12 -0700278 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700279
Andreas Huber8a82ff72016-08-04 10:29:39 -0700280 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700281 << binderName << ";\n";
282
Iliyan Malchev549e2592016-08-10 08:59:12 -0700283 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700284 out << parcelObjDeref
285 << "readNullableStrongBinder(&"
286 << binderName
287 << ");\n";
288
289 handleError(out, mode);
290
291 out << name
292 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100293 << "::android::hardware::fromBinder<"
294 << fqName().cppName()
295 << ","
296 << getProxyName().cppName()
297 << ","
298 << getStubName().cppName()
299 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700300 << binderName
301 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700302
303 out.unindent();
304 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700305 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200306 out << "if (" << name << " == nullptr) {\n";
307 out.indent();
308 out << "_hidl_err = ";
309 out << parcelObjDeref
310 << "writeStrongBinder(nullptr);\n";
311 out.unindent();
312 out << "} else {\n";
313 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800314 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
315 << "::android::hardware::toBinder<\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800316 out.indent(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100317 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800318 << ", "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100319 << getProxyName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800320 << ">("
321 << name
322 << ");\n";
323 });
324 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800325 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800326 out << "_hidl_err = "
327 << parcelObjDeref
328 << "writeStrongBinder(_hidl_binder);\n";
329 });
330 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800331 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800332 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
333 });
334 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200335 out.unindent();
336 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700337
Andreas Huber881227d2016-08-02 14:20:21 -0700338 handleError(out, mode);
339 }
340}
341
Andreas Huber2831d512016-08-15 09:33:47 -0700342void Interface::emitJavaReaderWriter(
343 Formatter &out,
344 const std::string &parcelObj,
345 const std::string &argName,
346 bool isReader) const {
347 if (isReader) {
348 out << fullJavaName()
349 << ".asInterface("
350 << parcelObj
351 << ".readStrongBinder());\n";
352 } else {
353 out << parcelObj
354 << ".writeStrongBinder("
355 << argName
356 << " == null ? null : "
357 << argName
358 << ".asBinder());\n";
359 }
360}
361
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700362status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
363 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700364 // Skip for TypeDef as it is just an alias of a defined type.
365 if (type->isTypeDef()) {
366 continue;
367 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700368 out << "attribute: {\n";
369 out.indent();
370 status_t status = type->emitVtsTypeDeclarations(out);
371 if (status != OK) {
372 return status;
373 }
374 out.unindent();
375 out << "}\n\n";
376 }
377 return OK;
378}
379
380status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700381 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800382 if (method->isHidlReserved()) {
383 continue;
384 }
385
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700386 out << "api: {\n";
387 out.indent();
388 out << "name: \"" << method->name() << "\"\n";
389 // Generate declaration for each return value.
390 for (const auto &result : method->results()) {
391 out << "return_type_hidl: {\n";
392 out.indent();
393 status_t status = result->type().emitVtsAttributeType(out);
394 if (status != OK) {
395 return status;
396 }
397 out.unindent();
398 out << "}\n";
399 }
400 // Generate declaration for each input argument
401 for (const auto &arg : method->args()) {
402 out << "arg: {\n";
403 out.indent();
404 status_t status = arg->type().emitVtsAttributeType(out);
405 if (status != OK) {
406 return status;
407 }
408 out.unindent();
409 out << "}\n";
410 }
411 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700412 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700413 out << "callflow: {\n";
414 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700415 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700416 if (name == "entry") {
417 out << "entry: true\n";
418 } else if (name == "exit") {
419 out << "exit: true\n";
420 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700421 const AnnotationParam *param =
422 annotation->getParam("next");
423 if (param != nullptr) {
424 for (auto value : *param->getValues()) {
425 out << "next: " << value << "\n";
426 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700427 }
428 } else {
429 std::cerr << "Invalid annotation '"
430 << name << "' for method: " << method->name()
431 << ". Should be one of: entry, exit, callflow. \n";
432 return UNKNOWN_ERROR;
433 }
434 out.unindent();
435 out << "}\n";
436 }
437 out.unindent();
438 out << "}\n\n";
439 }
440 return OK;
441}
442
443status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800444 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700445 << "predefined_type: \""
446 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700447 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800448 << "is_callback: "
449 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
450 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700451 return OK;
452}
453
Steven Moreland69e7c702016-09-09 11:16:32 -0700454bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700455 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700456 if (method->isOneway()) {
457 return true;
458 }
459 }
460
461 const Interface* superClass = superType();
462
463 if (superClass != nullptr) {
464 return superClass->hasOnewayMethods();
465 }
466
467 return false;
468}
469
Andreas Huber70a59e12016-08-16 12:57:01 -0700470bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700471 if (mIsJavaCompatibleInProgress) {
472 // We're currently trying to determine if this Interface is
473 // java-compatible and something is referencing this interface through
474 // one of its methods. Assume we'll ultimately succeed, if we were wrong
475 // the original invocation of Interface::isJavaCompatible() will then
476 // return the correct "false" result.
477 return true;
478 }
479
Andreas Huber0fa9e392016-08-31 09:05:44 -0700480 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
481 mIsJavaCompatibleInProgress = false;
482 return false;
483 }
484
Andreas Huberea081b32016-08-17 15:57:47 -0700485 mIsJavaCompatibleInProgress = true;
486
Andreas Huber70a59e12016-08-16 12:57:01 -0700487 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700488 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700489 return false;
490 }
491
Yifan Hong10fe0b52016-10-19 14:20:17 -0700492 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700493 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700494 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700495 return false;
496 }
497 }
498
Andreas Huberea081b32016-08-17 15:57:47 -0700499 mIsJavaCompatibleInProgress = false;
500
Andreas Huber70a59e12016-08-16 12:57:01 -0700501 return true;
502}
503
Andreas Huberc9410c72016-07-28 12:18:40 -0700504} // namespace android
505