blob: b72018df993d8c8011ae630699613c08a6e64a0e [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
60Method *Interface::createSyspropsChangedMethod() const {
61 return new Method("notifySyspropsChanged",
62 new std::vector<TypedVar *>() /*args */,
63 new std::vector<TypedVar *>() /*results */,
64 true /*oneway */,
65 new std::vector<Annotation *>(),
66 HIDL_SYSPROPS_CHANGED_TRANSACTION,
67 [this](auto &out) { /*cppImpl */
68 out << "::android::report_sysprop_change();\n";
69 out << "return ::android::hardware::Void();";
70 },
71 [this](auto &out) { /* javaImpl */
72 out << "android.os.SystemProperties.reportSyspropChanged();";
73 }
74 );
Andreas Huberc9410c72016-07-28 12:18:40 -070075}
76
Yifan Hong10fe0b52016-10-19 14:20:17 -070077Method *Interface::createDescriptorChainMethod() const {
78 VectorType *vecType = new VectorType();
79 vecType->setElementType(new StringType());
80 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
81 results->push_back(new TypedVar("indicator", vecType));
82
83 return new Method("interfaceChain",
84 new std::vector<TypedVar *>() /* args */,
85 results,
86 false /* oneway */,
87 new std::vector<Annotation *>(),
88 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
89 [this](auto &out) { /* cppImpl */
90 std::vector<const Interface *> chain = typeChain();
91 out << "::android::hardware::hidl_vec<::android::hardware::hidl_string> _hidl_return;\n";
92 out << "_hidl_return.resize(" << chain.size() << ");\n";
93 for (size_t i = 0; i < chain.size(); ++i) {
Steven Morelandd39133b2016-11-11 12:30:08 -080094 out << "_hidl_return[" << i << "] = "
95 << chain[i]->fullName()
96 << "::descriptor;\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -070097 }
98 out << "_hidl_cb(_hidl_return);\n";
99 out << "return ::android::hardware::Void();";
100 },
101 [this](auto &out) { /* javaImpl */
102 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800103 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700104 out.indent(); out.indent();
105 for (size_t i = 0; i < chain.size(); ++i) {
106 if (i != 0)
107 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800108 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700109 }
110 out << "));";
111 out.unindent(); out.unindent();
112 });
113}
114
115
Steven Moreland14ee6742016-10-18 12:58:28 -0700116bool Interface::addMethod(Method *method) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700117 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700118 if (lookupMethod(method->name()) != nullptr) {
119 LOG(ERROR) << "Redefinition of method " << method->name();
120 return false;
121 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700122 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700123
Yifan Hong10fe0b52016-10-19 14:20:17 -0700124 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700125
Yifan Hong10fe0b52016-10-19 14:20:17 -0700126 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700127 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700128 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700129 ancestor = ancestor->superType();
130 }
131
Yifan Hong10fe0b52016-10-19 14:20:17 -0700132 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
133 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700134 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700135 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700136
137 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700138}
139
Yifan Hong10fe0b52016-10-19 14:20:17 -0700140
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700141const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700142 return mSuperType;
143}
144
Yifan Hong10fe0b52016-10-19 14:20:17 -0700145std::vector<const Interface *> Interface::typeChain() const {
146 std::vector<const Interface *> v;
147 const Interface *iface = this;
148 while (iface != nullptr) {
149 v.push_back(iface);
150 iface = iface->mSuperType;
151 }
152 return v;
153}
154
Yifan Hongfe95aa22016-10-19 17:26:45 -0700155std::vector<const Interface *> Interface::superTypeChain() const {
156 return superType()->typeChain(); // should work even if superType is nullptr
157}
158
Andreas Hubera2723d22016-07-29 15:36:07 -0700159bool Interface::isInterface() const {
160 return true;
161}
162
Andreas Huber295ad302016-08-16 11:35:00 -0700163bool Interface::isBinder() const {
164 return true;
165}
166
Yifan Hong10fe0b52016-10-19 14:20:17 -0700167const std::vector<Method *> &Interface::userDefinedMethods() const {
168 return mUserMethods;
169}
170
171const std::vector<Method *> &Interface::hidlReservedMethods() const {
172 return mReservedMethods;
173}
174
175std::vector<Method *> Interface::methods() const {
176 std::vector<Method *> v(mUserMethods);
177 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
178 return v;
179}
180
181std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
182 std::vector<InterfaceAndMethod> v;
183 std::vector<const Interface *> chain = typeChain();
184 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
185 const Interface *iface = *it;
186 for (Method *userMethod : iface->userDefinedMethods()) {
187 v.push_back(InterfaceAndMethod(iface, userMethod));
188 }
189 }
190 for (Method *reservedMethod : hidlReservedMethods()) {
191 v.push_back(InterfaceAndMethod(this, reservedMethod));
192 }
193 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700194}
195
Steven Moreland14ee6742016-10-18 12:58:28 -0700196Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700197 for (const auto &tuple : allMethodsFromRoot()) {
198 Method *method = tuple.method();
199 if (method->name() == name) {
200 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700201 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700202 }
203
204 return nullptr;
205}
206
Steven Moreland40786312016-08-16 10:29:40 -0700207std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700208 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700209}
210
Yifan Hong158655a2016-11-08 12:34:07 -0800211FQName Interface::getHwName() const {
212 return FQName(fqName().package(), fqName().version(), "IHw" + getBaseName());
213}
214
Yifan Hong60e52bd2016-11-09 14:47:45 -0800215FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800216 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
217}
218
Yifan Hong60e52bd2016-11-09 14:47:45 -0800219FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800220 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
221}
222
Yifan Hong60e52bd2016-11-09 14:47:45 -0800223FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800224 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
225}
226
227
Steven Moreland979e0992016-09-07 09:18:08 -0700228std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700229 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700230 const std::string base =
231 std::string(specifyNamespaces ? "::android::" : "")
232 + "sp<"
233 + (specifyNamespaces ? fullName() : partialCppName())
234 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700235
236 switch (mode) {
237 case StorageMode_Stack:
238 case StorageMode_Result:
Martijn Coenenac587892016-11-17 15:14:19 +0100239 case StorageMode_Compound:
Andreas Huber881227d2016-08-02 14:20:21 -0700240 return base;
241
242 case StorageMode_Argument:
243 return "const " + base + "&";
244 }
245}
246
Yifan Hong4ed13472016-11-02 10:44:11 -0700247std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700248 return fullJavaName();
249}
250
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800251std::string Interface::getVtsType() const {
252 if (StringHelper::EndsWith(localName(), "Callback")) {
253 return "TYPE_HIDL_CALLBACK";
254 } else {
255 return "TYPE_HIDL_INTERFACE";
256 }
257}
258
Andreas Huber881227d2016-08-02 14:20:21 -0700259void Interface::emitReaderWriter(
260 Formatter &out,
261 const std::string &name,
262 const std::string &parcelObj,
263 bool parcelObjIsPointer,
264 bool isReader,
265 ErrorMode mode) const {
266 const std::string parcelObjDeref =
267 parcelObj + (parcelObjIsPointer ? "->" : ".");
268
269 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700270 out << "{\n";
271 out.indent();
272
Iliyan Malchev549e2592016-08-10 08:59:12 -0700273 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700274
Andreas Huber8a82ff72016-08-04 10:29:39 -0700275 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700276 << binderName << ";\n";
277
Iliyan Malchev549e2592016-08-10 08:59:12 -0700278 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700279 out << parcelObjDeref
280 << "readNullableStrongBinder(&"
281 << binderName
282 << ");\n";
283
284 handleError(out, mode);
285
286 out << name
287 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700288 << fqName().cppNamespace()
289 << "::IHw"
290 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700291 << "::asInterface("
292 << binderName
293 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700294
295 out.unindent();
296 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700297 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200298 out << "if (" << name << " == nullptr) {\n";
299 out.indent();
300 out << "_hidl_err = ";
301 out << parcelObjDeref
302 << "writeStrongBinder(nullptr);\n";
303 out.unindent();
304 out << "} else {\n";
305 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800306 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
307 << "::android::hardware::toBinder<\n";
308 out.indentBlock(2, [&] {
309 out << fqName().cppNamespace()
310 << "::I"
311 << getBaseName()
312 << ", "
313 << fqName().cppNamespace()
314 << "::IHw"
315 << getBaseName()
316 << ">("
317 << name
318 << ");\n";
319 });
320 out << "if (_hidl_binder.get() != nullptr) {\n";
321 out.indentBlock([&] {
322 out << "_hidl_err = "
323 << parcelObjDeref
324 << "writeStrongBinder(_hidl_binder);\n";
325 });
326 out << "} else {\n";
327 out.indentBlock([&] {
328 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
329 });
330 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200331 out.unindent();
332 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700333
Andreas Huber881227d2016-08-02 14:20:21 -0700334 handleError(out, mode);
335 }
336}
337
Andreas Huber2831d512016-08-15 09:33:47 -0700338void Interface::emitJavaReaderWriter(
339 Formatter &out,
340 const std::string &parcelObj,
341 const std::string &argName,
342 bool isReader) const {
343 if (isReader) {
344 out << fullJavaName()
345 << ".asInterface("
346 << parcelObj
347 << ".readStrongBinder());\n";
348 } else {
349 out << parcelObj
350 << ".writeStrongBinder("
351 << argName
352 << " == null ? null : "
353 << argName
354 << ".asBinder());\n";
355 }
356}
357
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700358status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
359 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700360 // Skip for TypeDef as it is just an alias of a defined type.
361 if (type->isTypeDef()) {
362 continue;
363 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700364 out << "attribute: {\n";
365 out.indent();
366 status_t status = type->emitVtsTypeDeclarations(out);
367 if (status != OK) {
368 return status;
369 }
370 out.unindent();
371 out << "}\n\n";
372 }
373 return OK;
374}
375
376status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700377 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800378 if (method->isHidlReserved()) {
379 continue;
380 }
381
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700382 out << "api: {\n";
383 out.indent();
384 out << "name: \"" << method->name() << "\"\n";
385 // Generate declaration for each return value.
386 for (const auto &result : method->results()) {
387 out << "return_type_hidl: {\n";
388 out.indent();
389 status_t status = result->type().emitVtsAttributeType(out);
390 if (status != OK) {
391 return status;
392 }
393 out.unindent();
394 out << "}\n";
395 }
396 // Generate declaration for each input argument
397 for (const auto &arg : method->args()) {
398 out << "arg: {\n";
399 out.indent();
400 status_t status = arg->type().emitVtsAttributeType(out);
401 if (status != OK) {
402 return status;
403 }
404 out.unindent();
405 out << "}\n";
406 }
407 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700408 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700409 out << "callflow: {\n";
410 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700411 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700412 if (name == "entry") {
413 out << "entry: true\n";
414 } else if (name == "exit") {
415 out << "exit: true\n";
416 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700417 const AnnotationParam *param =
418 annotation->getParam("next");
419 if (param != nullptr) {
420 for (auto value : *param->getValues()) {
421 out << "next: " << value << "\n";
422 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700423 }
424 } else {
425 std::cerr << "Invalid annotation '"
426 << name << "' for method: " << method->name()
427 << ". Should be one of: entry, exit, callflow. \n";
428 return UNKNOWN_ERROR;
429 }
430 out.unindent();
431 out << "}\n";
432 }
433 out.unindent();
434 out << "}\n\n";
435 }
436 return OK;
437}
438
439status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800440 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700441 << "predefined_type: \""
442 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700443 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800444 << "is_callback: "
445 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
446 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700447 return OK;
448}
449
Steven Moreland69e7c702016-09-09 11:16:32 -0700450bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700451 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700452 if (method->isOneway()) {
453 return true;
454 }
455 }
456
457 const Interface* superClass = superType();
458
459 if (superClass != nullptr) {
460 return superClass->hasOnewayMethods();
461 }
462
463 return false;
464}
465
Andreas Huber70a59e12016-08-16 12:57:01 -0700466bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700467 if (mIsJavaCompatibleInProgress) {
468 // We're currently trying to determine if this Interface is
469 // java-compatible and something is referencing this interface through
470 // one of its methods. Assume we'll ultimately succeed, if we were wrong
471 // the original invocation of Interface::isJavaCompatible() will then
472 // return the correct "false" result.
473 return true;
474 }
475
Andreas Huber0fa9e392016-08-31 09:05:44 -0700476 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
477 mIsJavaCompatibleInProgress = false;
478 return false;
479 }
480
Andreas Huberea081b32016-08-17 15:57:47 -0700481 mIsJavaCompatibleInProgress = true;
482
Andreas Huber70a59e12016-08-16 12:57:01 -0700483 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700484 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700485 return false;
486 }
487
Yifan Hong10fe0b52016-10-19 14:20:17 -0700488 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700489 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700490 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700491 return false;
492 }
493 }
494
Andreas Huberea081b32016-08-17 15:57:47 -0700495 mIsJavaCompatibleInProgress = false;
496
Andreas Huber70a59e12016-08-16 12:57:01 -0700497 return true;
498}
499
Andreas Huberc9410c72016-07-28 12:18:40 -0700500} // namespace android
501