blob: 52e9a98a2c0f7361f08b72a85aa6d65844f939fe [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 Hongc8934042016-11-17 17:10:52 -0800117 if (isIBase()) {
118 // ignore addMethod requests for IBase; they are all HIDL reserved methods.
119 return true;
120 }
121
Yifan Hong10fe0b52016-10-19 14:20:17 -0700122 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -0700123 if (lookupMethod(method->name()) != nullptr) {
124 LOG(ERROR) << "Redefinition of method " << method->name();
125 return false;
126 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700127 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700128
Yifan Hong10fe0b52016-10-19 14:20:17 -0700129 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700130
Yifan Hong10fe0b52016-10-19 14:20:17 -0700131 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700132 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700133 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700134 ancestor = ancestor->superType();
135 }
136
Yifan Hong10fe0b52016-10-19 14:20:17 -0700137 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
138 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700139 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700140 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700141
142 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700143}
144
Yifan Hong10fe0b52016-10-19 14:20:17 -0700145
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700146const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700147 return mSuperType;
148}
149
Yifan Hong10fe0b52016-10-19 14:20:17 -0700150std::vector<const Interface *> Interface::typeChain() const {
151 std::vector<const Interface *> v;
152 const Interface *iface = this;
153 while (iface != nullptr) {
154 v.push_back(iface);
155 iface = iface->mSuperType;
156 }
157 return v;
158}
159
Yifan Hongfe95aa22016-10-19 17:26:45 -0700160std::vector<const Interface *> Interface::superTypeChain() const {
161 return superType()->typeChain(); // should work even if superType is nullptr
162}
163
Andreas Hubera2723d22016-07-29 15:36:07 -0700164bool Interface::isInterface() const {
165 return true;
166}
167
Andreas Huber295ad302016-08-16 11:35:00 -0700168bool Interface::isBinder() const {
169 return true;
170}
171
Yifan Hong10fe0b52016-10-19 14:20:17 -0700172const std::vector<Method *> &Interface::userDefinedMethods() const {
173 return mUserMethods;
174}
175
176const std::vector<Method *> &Interface::hidlReservedMethods() const {
177 return mReservedMethods;
178}
179
180std::vector<Method *> Interface::methods() const {
181 std::vector<Method *> v(mUserMethods);
182 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
183 return v;
184}
185
186std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
187 std::vector<InterfaceAndMethod> v;
188 std::vector<const Interface *> chain = typeChain();
189 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
190 const Interface *iface = *it;
191 for (Method *userMethod : iface->userDefinedMethods()) {
192 v.push_back(InterfaceAndMethod(iface, userMethod));
193 }
194 }
195 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800196 v.push_back(InterfaceAndMethod(
197 *chain.rbegin(), // IBase
198 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700199 }
200 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700201}
202
Steven Moreland14ee6742016-10-18 12:58:28 -0700203Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700204 for (const auto &tuple : allMethodsFromRoot()) {
205 Method *method = tuple.method();
206 if (method->name() == name) {
207 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700208 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700209 }
210
211 return nullptr;
212}
213
Steven Moreland40786312016-08-16 10:29:40 -0700214std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700215 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700216}
217
Yifan Hong158655a2016-11-08 12:34:07 -0800218FQName Interface::getHwName() const {
219 return FQName(fqName().package(), fqName().version(), "IHw" + getBaseName());
220}
221
Yifan Hong60e52bd2016-11-09 14:47:45 -0800222FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800223 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
224}
225
Yifan Hong60e52bd2016-11-09 14:47:45 -0800226FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800227 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
228}
229
Yifan Hong60e52bd2016-11-09 14:47:45 -0800230FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800231 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
232}
233
234
Steven Moreland979e0992016-09-07 09:18:08 -0700235std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700236 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700237 const std::string base =
238 std::string(specifyNamespaces ? "::android::" : "")
239 + "sp<"
240 + (specifyNamespaces ? fullName() : partialCppName())
241 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700242
243 switch (mode) {
244 case StorageMode_Stack:
245 case StorageMode_Result:
246 return base;
247
248 case StorageMode_Argument:
249 return "const " + base + "&";
250 }
251}
252
Yifan Hong4ed13472016-11-02 10:44:11 -0700253std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700254 return fullJavaName();
255}
256
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800257std::string Interface::getVtsType() const {
258 if (StringHelper::EndsWith(localName(), "Callback")) {
259 return "TYPE_HIDL_CALLBACK";
260 } else {
261 return "TYPE_HIDL_INTERFACE";
262 }
263}
264
Andreas Huber881227d2016-08-02 14:20:21 -0700265void Interface::emitReaderWriter(
266 Formatter &out,
267 const std::string &name,
268 const std::string &parcelObj,
269 bool parcelObjIsPointer,
270 bool isReader,
271 ErrorMode mode) const {
272 const std::string parcelObjDeref =
273 parcelObj + (parcelObjIsPointer ? "->" : ".");
274
275 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700276 out << "{\n";
277 out.indent();
278
Iliyan Malchev549e2592016-08-10 08:59:12 -0700279 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700280
Andreas Huber8a82ff72016-08-04 10:29:39 -0700281 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700282 << binderName << ";\n";
283
Iliyan Malchev549e2592016-08-10 08:59:12 -0700284 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700285 out << parcelObjDeref
286 << "readNullableStrongBinder(&"
287 << binderName
288 << ");\n";
289
290 handleError(out, mode);
291
292 out << name
293 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700294 << fqName().cppNamespace()
295 << "::IHw"
296 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700297 << "::asInterface("
298 << binderName
299 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700300
301 out.unindent();
302 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700303 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200304 out << "if (" << name << " == nullptr) {\n";
305 out.indent();
306 out << "_hidl_err = ";
307 out << parcelObjDeref
308 << "writeStrongBinder(nullptr);\n";
309 out.unindent();
310 out << "} else {\n";
311 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800312 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
313 << "::android::hardware::toBinder<\n";
314 out.indentBlock(2, [&] {
315 out << fqName().cppNamespace()
316 << "::I"
317 << getBaseName()
318 << ", "
319 << fqName().cppNamespace()
320 << "::IHw"
321 << getBaseName()
322 << ">("
323 << name
324 << ");\n";
325 });
326 out << "if (_hidl_binder.get() != nullptr) {\n";
327 out.indentBlock([&] {
328 out << "_hidl_err = "
329 << parcelObjDeref
330 << "writeStrongBinder(_hidl_binder);\n";
331 });
332 out << "} else {\n";
333 out.indentBlock([&] {
334 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
335 });
336 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200337 out.unindent();
338 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700339
Andreas Huber881227d2016-08-02 14:20:21 -0700340 handleError(out, mode);
341 }
342}
343
Andreas Huber2831d512016-08-15 09:33:47 -0700344void Interface::emitJavaReaderWriter(
345 Formatter &out,
346 const std::string &parcelObj,
347 const std::string &argName,
348 bool isReader) const {
349 if (isReader) {
350 out << fullJavaName()
351 << ".asInterface("
352 << parcelObj
353 << ".readStrongBinder());\n";
354 } else {
355 out << parcelObj
356 << ".writeStrongBinder("
357 << argName
358 << " == null ? null : "
359 << argName
360 << ".asBinder());\n";
361 }
362}
363
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700364status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
365 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700366 // Skip for TypeDef as it is just an alias of a defined type.
367 if (type->isTypeDef()) {
368 continue;
369 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700370 out << "attribute: {\n";
371 out.indent();
372 status_t status = type->emitVtsTypeDeclarations(out);
373 if (status != OK) {
374 return status;
375 }
376 out.unindent();
377 out << "}\n\n";
378 }
379 return OK;
380}
381
382status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700383 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800384 if (method->isHidlReserved()) {
385 continue;
386 }
387
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700388 out << "api: {\n";
389 out.indent();
390 out << "name: \"" << method->name() << "\"\n";
391 // Generate declaration for each return value.
392 for (const auto &result : method->results()) {
393 out << "return_type_hidl: {\n";
394 out.indent();
395 status_t status = result->type().emitVtsAttributeType(out);
396 if (status != OK) {
397 return status;
398 }
399 out.unindent();
400 out << "}\n";
401 }
402 // Generate declaration for each input argument
403 for (const auto &arg : method->args()) {
404 out << "arg: {\n";
405 out.indent();
406 status_t status = arg->type().emitVtsAttributeType(out);
407 if (status != OK) {
408 return status;
409 }
410 out.unindent();
411 out << "}\n";
412 }
413 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700414 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700415 out << "callflow: {\n";
416 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700417 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700418 if (name == "entry") {
419 out << "entry: true\n";
420 } else if (name == "exit") {
421 out << "exit: true\n";
422 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700423 const AnnotationParam *param =
424 annotation->getParam("next");
425 if (param != nullptr) {
426 for (auto value : *param->getValues()) {
427 out << "next: " << value << "\n";
428 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700429 }
430 } else {
431 std::cerr << "Invalid annotation '"
432 << name << "' for method: " << method->name()
433 << ". Should be one of: entry, exit, callflow. \n";
434 return UNKNOWN_ERROR;
435 }
436 out.unindent();
437 out << "}\n";
438 }
439 out.unindent();
440 out << "}\n\n";
441 }
442 return OK;
443}
444
445status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800446 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700447 << "predefined_type: \""
448 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700449 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800450 << "is_callback: "
451 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
452 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700453 return OK;
454}
455
Steven Moreland69e7c702016-09-09 11:16:32 -0700456bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700457 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700458 if (method->isOneway()) {
459 return true;
460 }
461 }
462
463 const Interface* superClass = superType();
464
465 if (superClass != nullptr) {
466 return superClass->hasOnewayMethods();
467 }
468
469 return false;
470}
471
Andreas Huber70a59e12016-08-16 12:57:01 -0700472bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700473 if (mIsJavaCompatibleInProgress) {
474 // We're currently trying to determine if this Interface is
475 // java-compatible and something is referencing this interface through
476 // one of its methods. Assume we'll ultimately succeed, if we were wrong
477 // the original invocation of Interface::isJavaCompatible() will then
478 // return the correct "false" result.
479 return true;
480 }
481
Andreas Huber0fa9e392016-08-31 09:05:44 -0700482 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
483 mIsJavaCompatibleInProgress = false;
484 return false;
485 }
486
Andreas Huberea081b32016-08-17 15:57:47 -0700487 mIsJavaCompatibleInProgress = true;
488
Andreas Huber70a59e12016-08-16 12:57:01 -0700489 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700490 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700491 return false;
492 }
493
Yifan Hong10fe0b52016-10-19 14:20:17 -0700494 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700495 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700496 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700497 return false;
498 }
499 }
500
Andreas Huberea081b32016-08-17 15:57:47 -0700501 mIsJavaCompatibleInProgress = false;
502
Andreas Huber70a59e12016-08-16 12:57:01 -0700503 return true;
504}
505
Andreas Huberc9410c72016-07-28 12:18:40 -0700506} // namespace android
507