blob: 36179e7d2dbbb4c6bd8b8bdb188b61dd21aeb6a7 [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();
95 out << "::android::hardware::hidl_vec<::android::hardware::hidl_string> _hidl_return;\n";
96 out << "_hidl_return.resize(" << chain.size() << ");\n";
97 for (size_t i = 0; i < chain.size(); ++i) {
Steven Morelandd39133b2016-11-11 12:30:08 -080098 out << "_hidl_return[" << i << "] = "
99 << chain[i]->fullName()
100 << "::descriptor;\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700101 }
102 out << "_hidl_cb(_hidl_return);\n";
103 out << "return ::android::hardware::Void();";
104 },
105 [this](auto &out) { /* javaImpl */
106 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800107 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700108 out.indent(); out.indent();
109 for (size_t i = 0; i < chain.size(); ++i) {
110 if (i != 0)
111 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800112 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700113 }
114 out << "));";
115 out.unindent(); out.unindent();
116 });
117}
118
119
Steven Moreland14ee6742016-10-18 12:58:28 -0700120bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800121 if (isIBase()) {
122 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
123 return true;
124 }
125
Yifan Hong10fe0b52016-10-19 14:20:17 -0700126 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700127 if (lookupMethod(method->name()) != nullptr) {
128 LOG(ERROR) << "Redefinition of method " << method->name();
129 return false;
130 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700131 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700132
Yifan Hong10fe0b52016-10-19 14:20:17 -0700133 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700134
Yifan Hong10fe0b52016-10-19 14:20:17 -0700135 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700136 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700137 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700138 ancestor = ancestor->superType();
139 }
140
Yifan Hong10fe0b52016-10-19 14:20:17 -0700141 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
142 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700143 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700144 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700145
146 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700147}
148
Yifan Hong10fe0b52016-10-19 14:20:17 -0700149
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700150const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700151 return mSuperType;
152}
153
Yifan Hong10fe0b52016-10-19 14:20:17 -0700154std::vector<const Interface *> Interface::typeChain() const {
155 std::vector<const Interface *> v;
156 const Interface *iface = this;
157 while (iface != nullptr) {
158 v.push_back(iface);
159 iface = iface->mSuperType;
160 }
161 return v;
162}
163
Yifan Hongfe95aa22016-10-19 17:26:45 -0700164std::vector<const Interface *> Interface::superTypeChain() const {
165 return superType()->typeChain(); // should work even if superType is nullptr
166}
167
Andreas Hubera2723d22016-07-29 15:36:07 -0700168bool Interface::isInterface() const {
169 return true;
170}
171
Andreas Huber295ad302016-08-16 11:35:00 -0700172bool Interface::isBinder() const {
173 return true;
174}
175
Yifan Hong10fe0b52016-10-19 14:20:17 -0700176const std::vector<Method *> &Interface::userDefinedMethods() const {
177 return mUserMethods;
178}
179
180const std::vector<Method *> &Interface::hidlReservedMethods() const {
181 return mReservedMethods;
182}
183
184std::vector<Method *> Interface::methods() const {
185 std::vector<Method *> v(mUserMethods);
186 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
187 return v;
188}
189
190std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
191 std::vector<InterfaceAndMethod> v;
192 std::vector<const Interface *> chain = typeChain();
193 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
194 const Interface *iface = *it;
195 for (Method *userMethod : iface->userDefinedMethods()) {
196 v.push_back(InterfaceAndMethod(iface, userMethod));
197 }
198 }
199 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800200 v.push_back(InterfaceAndMethod(
201 *chain.rbegin(), // IBase
202 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700203 }
204 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700205}
206
Steven Moreland14ee6742016-10-18 12:58:28 -0700207Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700208 for (const auto &tuple : allMethodsFromRoot()) {
209 Method *method = tuple.method();
210 if (method->name() == name) {
211 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700212 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700213 }
214
215 return nullptr;
216}
217
Steven Moreland40786312016-08-16 10:29:40 -0700218std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700219 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700220}
221
Yifan Hong158655a2016-11-08 12:34:07 -0800222FQName Interface::getHwName() const {
223 return FQName(fqName().package(), fqName().version(), "IHw" + getBaseName());
224}
225
Yifan Hong60e52bd2016-11-09 14:47:45 -0800226FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800227 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
228}
229
Yifan Hong60e52bd2016-11-09 14:47:45 -0800230FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800231 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
232}
233
Yifan Hong60e52bd2016-11-09 14:47:45 -0800234FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800235 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
236}
237
238
Steven Moreland979e0992016-09-07 09:18:08 -0700239std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700240 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700241 const std::string base =
242 std::string(specifyNamespaces ? "::android::" : "")
243 + "sp<"
244 + (specifyNamespaces ? fullName() : partialCppName())
245 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700246
247 switch (mode) {
248 case StorageMode_Stack:
249 case StorageMode_Result:
250 return base;
251
252 case StorageMode_Argument:
253 return "const " + base + "&";
254 }
255}
256
Yifan Hong4ed13472016-11-02 10:44:11 -0700257std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700258 return fullJavaName();
259}
260
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800261std::string Interface::getVtsType() const {
262 if (StringHelper::EndsWith(localName(), "Callback")) {
263 return "TYPE_HIDL_CALLBACK";
264 } else {
265 return "TYPE_HIDL_INTERFACE";
266 }
267}
268
Andreas Huber881227d2016-08-02 14:20:21 -0700269void Interface::emitReaderWriter(
270 Formatter &out,
271 const std::string &name,
272 const std::string &parcelObj,
273 bool parcelObjIsPointer,
274 bool isReader,
275 ErrorMode mode) const {
276 const std::string parcelObjDeref =
277 parcelObj + (parcelObjIsPointer ? "->" : ".");
278
279 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700280 out << "{\n";
281 out.indent();
282
Iliyan Malchev549e2592016-08-10 08:59:12 -0700283 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700284
Andreas Huber8a82ff72016-08-04 10:29:39 -0700285 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700286 << binderName << ";\n";
287
Iliyan Malchev549e2592016-08-10 08:59:12 -0700288 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700289 out << parcelObjDeref
290 << "readNullableStrongBinder(&"
291 << binderName
292 << ");\n";
293
294 handleError(out, mode);
295
296 out << name
297 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700298 << fqName().cppNamespace()
299 << "::IHw"
300 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700301 << "::asInterface("
302 << binderName
303 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700304
305 out.unindent();
306 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700307 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200308 out << "if (" << name << " == nullptr) {\n";
309 out.indent();
310 out << "_hidl_err = ";
311 out << parcelObjDeref
312 << "writeStrongBinder(nullptr);\n";
313 out.unindent();
314 out << "} else {\n";
315 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800316 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
317 << "::android::hardware::toBinder<\n";
318 out.indentBlock(2, [&] {
319 out << fqName().cppNamespace()
320 << "::I"
321 << getBaseName()
322 << ", "
323 << fqName().cppNamespace()
324 << "::IHw"
325 << getBaseName()
326 << ">("
327 << name
328 << ");\n";
329 });
330 out << "if (_hidl_binder.get() != nullptr) {\n";
331 out.indentBlock([&] {
332 out << "_hidl_err = "
333 << parcelObjDeref
334 << "writeStrongBinder(_hidl_binder);\n";
335 });
336 out << "} else {\n";
337 out.indentBlock([&] {
338 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
339 });
340 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200341 out.unindent();
342 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700343
Andreas Huber881227d2016-08-02 14:20:21 -0700344 handleError(out, mode);
345 }
346}
347
Andreas Huber2831d512016-08-15 09:33:47 -0700348void Interface::emitJavaReaderWriter(
349 Formatter &out,
350 const std::string &parcelObj,
351 const std::string &argName,
352 bool isReader) const {
353 if (isReader) {
354 out << fullJavaName()
355 << ".asInterface("
356 << parcelObj
357 << ".readStrongBinder());\n";
358 } else {
359 out << parcelObj
360 << ".writeStrongBinder("
361 << argName
362 << " == null ? null : "
363 << argName
364 << ".asBinder());\n";
365 }
366}
367
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700368status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
369 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700370 // Skip for TypeDef as it is just an alias of a defined type.
371 if (type->isTypeDef()) {
372 continue;
373 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700374 out << "attribute: {\n";
375 out.indent();
376 status_t status = type->emitVtsTypeDeclarations(out);
377 if (status != OK) {
378 return status;
379 }
380 out.unindent();
381 out << "}\n\n";
382 }
383 return OK;
384}
385
386status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700387 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800388 if (method->isHidlReserved()) {
389 continue;
390 }
391
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700392 out << "api: {\n";
393 out.indent();
394 out << "name: \"" << method->name() << "\"\n";
395 // Generate declaration for each return value.
396 for (const auto &result : method->results()) {
397 out << "return_type_hidl: {\n";
398 out.indent();
399 status_t status = result->type().emitVtsAttributeType(out);
400 if (status != OK) {
401 return status;
402 }
403 out.unindent();
404 out << "}\n";
405 }
406 // Generate declaration for each input argument
407 for (const auto &arg : method->args()) {
408 out << "arg: {\n";
409 out.indent();
410 status_t status = arg->type().emitVtsAttributeType(out);
411 if (status != OK) {
412 return status;
413 }
414 out.unindent();
415 out << "}\n";
416 }
417 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700418 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700419 out << "callflow: {\n";
420 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700421 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700422 if (name == "entry") {
423 out << "entry: true\n";
424 } else if (name == "exit") {
425 out << "exit: true\n";
426 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700427 const AnnotationParam *param =
428 annotation->getParam("next");
429 if (param != nullptr) {
430 for (auto value : *param->getValues()) {
431 out << "next: " << value << "\n";
432 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700433 }
434 } else {
435 std::cerr << "Invalid annotation '"
436 << name << "' for method: " << method->name()
437 << ". Should be one of: entry, exit, callflow. \n";
438 return UNKNOWN_ERROR;
439 }
440 out.unindent();
441 out << "}\n";
442 }
443 out.unindent();
444 out << "}\n\n";
445 }
446 return OK;
447}
448
449status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800450 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700451 << "predefined_type: \""
452 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700453 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800454 << "is_callback: "
455 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
456 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700457 return OK;
458}
459
Steven Moreland69e7c702016-09-09 11:16:32 -0700460bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700461 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700462 if (method->isOneway()) {
463 return true;
464 }
465 }
466
467 const Interface* superClass = superType();
468
469 if (superClass != nullptr) {
470 return superClass->hasOnewayMethods();
471 }
472
473 return false;
474}
475
Andreas Huber70a59e12016-08-16 12:57:01 -0700476bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700477 if (mIsJavaCompatibleInProgress) {
478 // We're currently trying to determine if this Interface is
479 // java-compatible and something is referencing this interface through
480 // one of its methods. Assume we'll ultimately succeed, if we were wrong
481 // the original invocation of Interface::isJavaCompatible() will then
482 // return the correct "false" result.
483 return true;
484 }
485
Andreas Huber0fa9e392016-08-31 09:05:44 -0700486 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
487 mIsJavaCompatibleInProgress = false;
488 return false;
489 }
490
Andreas Huberea081b32016-08-17 15:57:47 -0700491 mIsJavaCompatibleInProgress = true;
492
Andreas Huber70a59e12016-08-16 12:57:01 -0700493 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700494 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700495 return false;
496 }
497
Yifan Hong10fe0b52016-10-19 14:20:17 -0700498 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700499 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700500 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700501 return false;
502 }
503 }
504
Andreas Huberea081b32016-08-17 15:57:47 -0700505 mIsJavaCompatibleInProgress = false;
506
Andreas Huber70a59e12016-08-16 12:57:01 -0700507 return true;
508}
509
Andreas Huberc9410c72016-07-28 12:18:40 -0700510} // namespace android
511