blob: 41171779dbe6c192ef2f0d41aa1a003c4d7e0cf0 [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 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 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100294 << "::android::hardware::fromBinder<"
295 << fqName().cppName()
296 << ","
297 << getProxyName().cppName()
298 << ","
299 << getStubName().cppName()
300 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700301 << binderName
302 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700303
304 out.unindent();
305 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700306 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200307 out << "if (" << name << " == nullptr) {\n";
308 out.indent();
309 out << "_hidl_err = ";
310 out << parcelObjDeref
311 << "writeStrongBinder(nullptr);\n";
312 out.unindent();
313 out << "} else {\n";
314 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800315 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
316 << "::android::hardware::toBinder<\n";
317 out.indentBlock(2, [&] {
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100318 out << fqName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800319 << ", "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100320 << getProxyName().cppName()
Yifan Hong158655a2016-11-08 12:34:07 -0800321 << ">("
322 << name
323 << ");\n";
324 });
325 out << "if (_hidl_binder.get() != nullptr) {\n";
326 out.indentBlock([&] {
327 out << "_hidl_err = "
328 << parcelObjDeref
329 << "writeStrongBinder(_hidl_binder);\n";
330 });
331 out << "} else {\n";
332 out.indentBlock([&] {
333 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
334 });
335 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200336 out.unindent();
337 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700338
Andreas Huber881227d2016-08-02 14:20:21 -0700339 handleError(out, mode);
340 }
341}
342
Andreas Huber2831d512016-08-15 09:33:47 -0700343void Interface::emitJavaReaderWriter(
344 Formatter &out,
345 const std::string &parcelObj,
346 const std::string &argName,
347 bool isReader) const {
348 if (isReader) {
349 out << fullJavaName()
350 << ".asInterface("
351 << parcelObj
352 << ".readStrongBinder());\n";
353 } else {
354 out << parcelObj
355 << ".writeStrongBinder("
356 << argName
357 << " == null ? null : "
358 << argName
359 << ".asBinder());\n";
360 }
361}
362
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700363status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
364 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700365 // Skip for TypeDef as it is just an alias of a defined type.
366 if (type->isTypeDef()) {
367 continue;
368 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700369 out << "attribute: {\n";
370 out.indent();
371 status_t status = type->emitVtsTypeDeclarations(out);
372 if (status != OK) {
373 return status;
374 }
375 out.unindent();
376 out << "}\n\n";
377 }
378 return OK;
379}
380
381status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700382 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800383 if (method->isHidlReserved()) {
384 continue;
385 }
386
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700387 out << "api: {\n";
388 out.indent();
389 out << "name: \"" << method->name() << "\"\n";
390 // Generate declaration for each return value.
391 for (const auto &result : method->results()) {
392 out << "return_type_hidl: {\n";
393 out.indent();
394 status_t status = result->type().emitVtsAttributeType(out);
395 if (status != OK) {
396 return status;
397 }
398 out.unindent();
399 out << "}\n";
400 }
401 // Generate declaration for each input argument
402 for (const auto &arg : method->args()) {
403 out << "arg: {\n";
404 out.indent();
405 status_t status = arg->type().emitVtsAttributeType(out);
406 if (status != OK) {
407 return status;
408 }
409 out.unindent();
410 out << "}\n";
411 }
412 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700413 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700414 out << "callflow: {\n";
415 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700416 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700417 if (name == "entry") {
418 out << "entry: true\n";
419 } else if (name == "exit") {
420 out << "exit: true\n";
421 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700422 const AnnotationParam *param =
423 annotation->getParam("next");
424 if (param != nullptr) {
425 for (auto value : *param->getValues()) {
426 out << "next: " << value << "\n";
427 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700428 }
429 } else {
430 std::cerr << "Invalid annotation '"
431 << name << "' for method: " << method->name()
432 << ". Should be one of: entry, exit, callflow. \n";
433 return UNKNOWN_ERROR;
434 }
435 out.unindent();
436 out << "}\n";
437 }
438 out.unindent();
439 out << "}\n\n";
440 }
441 return OK;
442}
443
444status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800445 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700446 << "predefined_type: \""
447 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700448 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800449 << "is_callback: "
450 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
451 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700452 return OK;
453}
454
Steven Moreland69e7c702016-09-09 11:16:32 -0700455bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700456 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700457 if (method->isOneway()) {
458 return true;
459 }
460 }
461
462 const Interface* superClass = superType();
463
464 if (superClass != nullptr) {
465 return superClass->hasOnewayMethods();
466 }
467
468 return false;
469}
470
Andreas Huber70a59e12016-08-16 12:57:01 -0700471bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700472 if (mIsJavaCompatibleInProgress) {
473 // We're currently trying to determine if this Interface is
474 // java-compatible and something is referencing this interface through
475 // one of its methods. Assume we'll ultimately succeed, if we were wrong
476 // the original invocation of Interface::isJavaCompatible() will then
477 // return the correct "false" result.
478 return true;
479 }
480
Andreas Huber0fa9e392016-08-31 09:05:44 -0700481 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
482 mIsJavaCompatibleInProgress = false;
483 return false;
484 }
485
Andreas Huberea081b32016-08-17 15:57:47 -0700486 mIsJavaCompatibleInProgress = true;
487
Andreas Huber70a59e12016-08-16 12:57:01 -0700488 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700489 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700490 return false;
491 }
492
Yifan Hong10fe0b52016-10-19 14:20:17 -0700493 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700494 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700495 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700496 return false;
497 }
498 }
499
Andreas Huberea081b32016-08-17 15:57:47 -0700500 mIsJavaCompatibleInProgress = false;
501
Andreas Huber70a59e12016-08-16 12:57:01 -0700502 return true;
503}
504
Andreas Huberc9410c72016-07-28 12:18:40 -0700505} // namespace android
506