blob: e0c44612191870e5b1519af2b0bac34f60132ea3 [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:
239 return base;
240
241 case StorageMode_Argument:
242 return "const " + base + "&";
243 }
244}
245
Yifan Hong4ed13472016-11-02 10:44:11 -0700246std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700247 return fullJavaName();
248}
249
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800250std::string Interface::getVtsType() const {
251 if (StringHelper::EndsWith(localName(), "Callback")) {
252 return "TYPE_HIDL_CALLBACK";
253 } else {
254 return "TYPE_HIDL_INTERFACE";
255 }
256}
257
Andreas Huber881227d2016-08-02 14:20:21 -0700258void Interface::emitReaderWriter(
259 Formatter &out,
260 const std::string &name,
261 const std::string &parcelObj,
262 bool parcelObjIsPointer,
263 bool isReader,
264 ErrorMode mode) const {
265 const std::string parcelObjDeref =
266 parcelObj + (parcelObjIsPointer ? "->" : ".");
267
268 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700269 out << "{\n";
270 out.indent();
271
Iliyan Malchev549e2592016-08-10 08:59:12 -0700272 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700273
Andreas Huber8a82ff72016-08-04 10:29:39 -0700274 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700275 << binderName << ";\n";
276
Iliyan Malchev549e2592016-08-10 08:59:12 -0700277 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700278 out << parcelObjDeref
279 << "readNullableStrongBinder(&"
280 << binderName
281 << ");\n";
282
283 handleError(out, mode);
284
285 out << name
286 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700287 << fqName().cppNamespace()
288 << "::IHw"
289 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700290 << "::asInterface("
291 << binderName
292 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700293
294 out.unindent();
295 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700296 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200297 out << "if (" << name << " == nullptr) {\n";
298 out.indent();
299 out << "_hidl_err = ";
300 out << parcelObjDeref
301 << "writeStrongBinder(nullptr);\n";
302 out.unindent();
303 out << "} else {\n";
304 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800305 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
306 << "::android::hardware::toBinder<\n";
307 out.indentBlock(2, [&] {
308 out << fqName().cppNamespace()
309 << "::I"
310 << getBaseName()
311 << ", "
312 << fqName().cppNamespace()
313 << "::IHw"
314 << getBaseName()
315 << ">("
316 << name
317 << ");\n";
318 });
319 out << "if (_hidl_binder.get() != nullptr) {\n";
320 out.indentBlock([&] {
321 out << "_hidl_err = "
322 << parcelObjDeref
323 << "writeStrongBinder(_hidl_binder);\n";
324 });
325 out << "} else {\n";
326 out.indentBlock([&] {
327 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
328 });
329 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200330 out.unindent();
331 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700332
Andreas Huber881227d2016-08-02 14:20:21 -0700333 handleError(out, mode);
334 }
335}
336
Andreas Huber2831d512016-08-15 09:33:47 -0700337void Interface::emitJavaReaderWriter(
338 Formatter &out,
339 const std::string &parcelObj,
340 const std::string &argName,
341 bool isReader) const {
342 if (isReader) {
343 out << fullJavaName()
344 << ".asInterface("
345 << parcelObj
346 << ".readStrongBinder());\n";
347 } else {
348 out << parcelObj
349 << ".writeStrongBinder("
350 << argName
351 << " == null ? null : "
352 << argName
353 << ".asBinder());\n";
354 }
355}
356
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700357status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
358 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700359 // Skip for TypeDef as it is just an alias of a defined type.
360 if (type->isTypeDef()) {
361 continue;
362 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700363 out << "attribute: {\n";
364 out.indent();
365 status_t status = type->emitVtsTypeDeclarations(out);
366 if (status != OK) {
367 return status;
368 }
369 out.unindent();
370 out << "}\n\n";
371 }
372 return OK;
373}
374
375status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700376 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800377 if (method->isHidlReserved()) {
378 continue;
379 }
380
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700381 out << "api: {\n";
382 out.indent();
383 out << "name: \"" << method->name() << "\"\n";
384 // Generate declaration for each return value.
385 for (const auto &result : method->results()) {
386 out << "return_type_hidl: {\n";
387 out.indent();
388 status_t status = result->type().emitVtsAttributeType(out);
389 if (status != OK) {
390 return status;
391 }
392 out.unindent();
393 out << "}\n";
394 }
395 // Generate declaration for each input argument
396 for (const auto &arg : method->args()) {
397 out << "arg: {\n";
398 out.indent();
399 status_t status = arg->type().emitVtsAttributeType(out);
400 if (status != OK) {
401 return status;
402 }
403 out.unindent();
404 out << "}\n";
405 }
406 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700407 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700408 out << "callflow: {\n";
409 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700410 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700411 if (name == "entry") {
412 out << "entry: true\n";
413 } else if (name == "exit") {
414 out << "exit: true\n";
415 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700416 const AnnotationParam *param =
417 annotation->getParam("next");
418 if (param != nullptr) {
419 for (auto value : *param->getValues()) {
420 out << "next: " << value << "\n";
421 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700422 }
423 } else {
424 std::cerr << "Invalid annotation '"
425 << name << "' for method: " << method->name()
426 << ". Should be one of: entry, exit, callflow. \n";
427 return UNKNOWN_ERROR;
428 }
429 out.unindent();
430 out << "}\n";
431 }
432 out.unindent();
433 out << "}\n\n";
434 }
435 return OK;
436}
437
438status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800439 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700440 << "predefined_type: \""
441 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700442 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800443 << "is_callback: "
444 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
445 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700446 return OK;
447}
448
Steven Moreland69e7c702016-09-09 11:16:32 -0700449bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700450 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700451 if (method->isOneway()) {
452 return true;
453 }
454 }
455
456 const Interface* superClass = superType();
457
458 if (superClass != nullptr) {
459 return superClass->hasOnewayMethods();
460 }
461
462 return false;
463}
464
Andreas Huber70a59e12016-08-16 12:57:01 -0700465bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700466 if (mIsJavaCompatibleInProgress) {
467 // We're currently trying to determine if this Interface is
468 // java-compatible and something is referencing this interface through
469 // one of its methods. Assume we'll ultimately succeed, if we were wrong
470 // the original invocation of Interface::isJavaCompatible() will then
471 // return the correct "false" result.
472 return true;
473 }
474
Andreas Huber0fa9e392016-08-31 09:05:44 -0700475 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
476 mIsJavaCompatibleInProgress = false;
477 return false;
478 }
479
Andreas Huberea081b32016-08-17 15:57:47 -0700480 mIsJavaCompatibleInProgress = true;
481
Andreas Huber70a59e12016-08-16 12:57:01 -0700482 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700483 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700484 return false;
485 }
486
Yifan Hong10fe0b52016-10-19 14:20:17 -0700487 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700488 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700489 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700490 return false;
491 }
492 }
493
Andreas Huberea081b32016-08-17 15:57:47 -0700494 mIsJavaCompatibleInProgress = false;
495
Andreas Huber70a59e12016-08-16 12:57:01 -0700496 return true;
497}
498
Andreas Huberc9410c72016-07-28 12:18:40 -0700499} // namespace android
500