blob: 35cbce3358976e4bbfccd76e63ff430c996a29d1 [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,
48 LAST_HIDL_TRANSACTION = 0x00ffffff,
49};
50
Yifan Honga4b53d02016-10-31 17:29:10 -070051Interface::Interface(const char *localName, const Location &location, Interface *super)
52 : Scope(localName, location),
Andreas Huber9ed827c2016-08-22 12:31:13 -070053 mSuperType(super),
Andreas Huberea081b32016-08-17 15:57:47 -070054 mIsJavaCompatibleInProgress(false) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070055 mReservedMethods.push_back(createDescriptorChainMethod());
Andreas Huberc9410c72016-07-28 12:18:40 -070056}
57
Yifan Hong10fe0b52016-10-19 14:20:17 -070058Method *Interface::createDescriptorChainMethod() const {
59 VectorType *vecType = new VectorType();
60 vecType->setElementType(new StringType());
61 std::vector<TypedVar *> *results = new std::vector<TypedVar *>();
62 results->push_back(new TypedVar("indicator", vecType));
63
64 return new Method("interfaceChain",
65 new std::vector<TypedVar *>() /* args */,
66 results,
67 false /* oneway */,
68 new std::vector<Annotation *>(),
69 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
70 [this](auto &out) { /* cppImpl */
71 std::vector<const Interface *> chain = typeChain();
72 out << "::android::hardware::hidl_vec<::android::hardware::hidl_string> _hidl_return;\n";
73 out << "_hidl_return.resize(" << chain.size() << ");\n";
74 for (size_t i = 0; i < chain.size(); ++i) {
Steven Morelandd39133b2016-11-11 12:30:08 -080075 out << "_hidl_return[" << i << "] = "
76 << chain[i]->fullName()
77 << "::descriptor;\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -070078 }
79 out << "_hidl_cb(_hidl_return);\n";
80 out << "return ::android::hardware::Void();";
81 },
82 [this](auto &out) { /* javaImpl */
83 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -080084 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -070085 out.indent(); out.indent();
86 for (size_t i = 0; i < chain.size(); ++i) {
87 if (i != 0)
88 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -080089 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -070090 }
91 out << "));";
92 out.unindent(); out.unindent();
93 });
94}
95
96
Steven Moreland14ee6742016-10-18 12:58:28 -070097bool Interface::addMethod(Method *method) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070098 CHECK(!method->isHidlReserved());
Steven Moreland14ee6742016-10-18 12:58:28 -070099 if (lookupMethod(method->name()) != nullptr) {
100 LOG(ERROR) << "Redefinition of method " << method->name();
101 return false;
102 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700103 size_t serial = FIRST_CALL_TRANSACTION;
Steven Moreland14ee6742016-10-18 12:58:28 -0700104
Yifan Hong10fe0b52016-10-19 14:20:17 -0700105 serial += userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700106
Yifan Hong10fe0b52016-10-19 14:20:17 -0700107 const Interface *ancestor = mSuperType;
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700108 while (ancestor != nullptr) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700109 serial += ancestor->userDefinedMethods().size();
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700110 ancestor = ancestor->superType();
111 }
112
Yifan Hong10fe0b52016-10-19 14:20:17 -0700113 CHECK(serial <= LAST_CALL_TRANSACTION) << "More than "
114 << LAST_CALL_TRANSACTION << " methods are not allowed.";
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700115 method->setSerialId(serial);
Yifan Hong10fe0b52016-10-19 14:20:17 -0700116 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700117
118 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700119}
120
Yifan Hong10fe0b52016-10-19 14:20:17 -0700121
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700122const Interface *Interface::superType() const {
Andreas Huberc9410c72016-07-28 12:18:40 -0700123 return mSuperType;
124}
125
Yifan Hong10fe0b52016-10-19 14:20:17 -0700126std::vector<const Interface *> Interface::typeChain() const {
127 std::vector<const Interface *> v;
128 const Interface *iface = this;
129 while (iface != nullptr) {
130 v.push_back(iface);
131 iface = iface->mSuperType;
132 }
133 return v;
134}
135
Yifan Hongfe95aa22016-10-19 17:26:45 -0700136std::vector<const Interface *> Interface::superTypeChain() const {
137 return superType()->typeChain(); // should work even if superType is nullptr
138}
139
Andreas Hubera2723d22016-07-29 15:36:07 -0700140bool Interface::isInterface() const {
141 return true;
142}
143
Andreas Huber295ad302016-08-16 11:35:00 -0700144bool Interface::isBinder() const {
145 return true;
146}
147
Yifan Hong10fe0b52016-10-19 14:20:17 -0700148const std::vector<Method *> &Interface::userDefinedMethods() const {
149 return mUserMethods;
150}
151
152const std::vector<Method *> &Interface::hidlReservedMethods() const {
153 return mReservedMethods;
154}
155
156std::vector<Method *> Interface::methods() const {
157 std::vector<Method *> v(mUserMethods);
158 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
159 return v;
160}
161
162std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
163 std::vector<InterfaceAndMethod> v;
164 std::vector<const Interface *> chain = typeChain();
165 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
166 const Interface *iface = *it;
167 for (Method *userMethod : iface->userDefinedMethods()) {
168 v.push_back(InterfaceAndMethod(iface, userMethod));
169 }
170 }
171 for (Method *reservedMethod : hidlReservedMethods()) {
172 v.push_back(InterfaceAndMethod(this, reservedMethod));
173 }
174 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700175}
176
Steven Moreland14ee6742016-10-18 12:58:28 -0700177Method *Interface::lookupMethod(std::string name) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700178 for (const auto &tuple : allMethodsFromRoot()) {
179 Method *method = tuple.method();
180 if (method->name() == name) {
181 return method;
Steven Moreland14ee6742016-10-18 12:58:28 -0700182 }
Steven Moreland14ee6742016-10-18 12:58:28 -0700183 }
184
185 return nullptr;
186}
187
Steven Moreland40786312016-08-16 10:29:40 -0700188std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700189 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700190}
191
Yifan Hong158655a2016-11-08 12:34:07 -0800192FQName Interface::getHwName() const {
193 return FQName(fqName().package(), fqName().version(), "IHw" + getBaseName());
194}
195
Yifan Hong60e52bd2016-11-09 14:47:45 -0800196FQName Interface::getProxyName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800197 return FQName(fqName().package(), fqName().version(), "Bp" + getBaseName());
198}
199
Yifan Hong60e52bd2016-11-09 14:47:45 -0800200FQName Interface::getStubName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800201 return FQName(fqName().package(), fqName().version(), "Bn" + getBaseName());
202}
203
Yifan Hong60e52bd2016-11-09 14:47:45 -0800204FQName Interface::getPassthroughName() const {
Yifan Hong158655a2016-11-08 12:34:07 -0800205 return FQName(fqName().package(), fqName().version(), "Bs" + getBaseName());
206}
207
208
Steven Moreland979e0992016-09-07 09:18:08 -0700209std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700210 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700211 const std::string base =
212 std::string(specifyNamespaces ? "::android::" : "")
213 + "sp<"
214 + (specifyNamespaces ? fullName() : partialCppName())
215 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700216
217 switch (mode) {
218 case StorageMode_Stack:
219 case StorageMode_Result:
Martijn Coenenac587892016-11-17 15:14:19 +0100220 case StorageMode_Compound:
Andreas Huber881227d2016-08-02 14:20:21 -0700221 return base;
222
223 case StorageMode_Argument:
224 return "const " + base + "&";
225 }
226}
227
Yifan Hong4ed13472016-11-02 10:44:11 -0700228std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700229 return fullJavaName();
230}
231
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800232std::string Interface::getVtsType() const {
233 if (StringHelper::EndsWith(localName(), "Callback")) {
234 return "TYPE_HIDL_CALLBACK";
235 } else {
236 return "TYPE_HIDL_INTERFACE";
237 }
238}
239
Andreas Huber881227d2016-08-02 14:20:21 -0700240void Interface::emitReaderWriter(
241 Formatter &out,
242 const std::string &name,
243 const std::string &parcelObj,
244 bool parcelObjIsPointer,
245 bool isReader,
246 ErrorMode mode) const {
247 const std::string parcelObjDeref =
248 parcelObj + (parcelObjIsPointer ? "->" : ".");
249
250 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700251 out << "{\n";
252 out.indent();
253
Iliyan Malchev549e2592016-08-10 08:59:12 -0700254 const std::string binderName = "_hidl_" + name + "_binder";
Andreas Huber881227d2016-08-02 14:20:21 -0700255
Andreas Huber8a82ff72016-08-04 10:29:39 -0700256 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700257 << binderName << ";\n";
258
Iliyan Malchev549e2592016-08-10 08:59:12 -0700259 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700260 out << parcelObjDeref
261 << "readNullableStrongBinder(&"
262 << binderName
263 << ");\n";
264
265 handleError(out, mode);
266
267 out << name
268 << " = "
Steven Moreland40786312016-08-16 10:29:40 -0700269 << fqName().cppNamespace()
270 << "::IHw"
271 << getBaseName()
Andreas Huber881227d2016-08-02 14:20:21 -0700272 << "::asInterface("
273 << binderName
274 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700275
276 out.unindent();
277 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700278 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200279 out << "if (" << name << " == nullptr) {\n";
280 out.indent();
281 out << "_hidl_err = ";
282 out << parcelObjDeref
283 << "writeStrongBinder(nullptr);\n";
284 out.unindent();
285 out << "} else {\n";
286 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800287 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
288 << "::android::hardware::toBinder<\n";
289 out.indentBlock(2, [&] {
290 out << fqName().cppNamespace()
291 << "::I"
292 << getBaseName()
293 << ", "
294 << fqName().cppNamespace()
295 << "::IHw"
296 << getBaseName()
297 << ">("
298 << name
299 << ");\n";
300 });
301 out << "if (_hidl_binder.get() != nullptr) {\n";
302 out.indentBlock([&] {
303 out << "_hidl_err = "
304 << parcelObjDeref
305 << "writeStrongBinder(_hidl_binder);\n";
306 });
307 out << "} else {\n";
308 out.indentBlock([&] {
309 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
310 });
311 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200312 out.unindent();
313 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700314
Andreas Huber881227d2016-08-02 14:20:21 -0700315 handleError(out, mode);
316 }
317}
318
Andreas Huber2831d512016-08-15 09:33:47 -0700319void Interface::emitJavaReaderWriter(
320 Formatter &out,
321 const std::string &parcelObj,
322 const std::string &argName,
323 bool isReader) const {
324 if (isReader) {
325 out << fullJavaName()
326 << ".asInterface("
327 << parcelObj
328 << ".readStrongBinder());\n";
329 } else {
330 out << parcelObj
331 << ".writeStrongBinder("
332 << argName
333 << " == null ? null : "
334 << argName
335 << ".asBinder());\n";
336 }
337}
338
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700339status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const {
340 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700341 // Skip for TypeDef as it is just an alias of a defined type.
342 if (type->isTypeDef()) {
343 continue;
344 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700345 out << "attribute: {\n";
346 out.indent();
347 status_t status = type->emitVtsTypeDeclarations(out);
348 if (status != OK) {
349 return status;
350 }
351 out.unindent();
352 out << "}\n\n";
353 }
354 return OK;
355}
356
357status_t Interface::emitVtsMethodDeclaration(Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700358 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800359 if (method->isHidlReserved()) {
360 continue;
361 }
362
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700363 out << "api: {\n";
364 out.indent();
365 out << "name: \"" << method->name() << "\"\n";
366 // Generate declaration for each return value.
367 for (const auto &result : method->results()) {
368 out << "return_type_hidl: {\n";
369 out.indent();
370 status_t status = result->type().emitVtsAttributeType(out);
371 if (status != OK) {
372 return status;
373 }
374 out.unindent();
375 out << "}\n";
376 }
377 // Generate declaration for each input argument
378 for (const auto &arg : method->args()) {
379 out << "arg: {\n";
380 out.indent();
381 status_t status = arg->type().emitVtsAttributeType(out);
382 if (status != OK) {
383 return status;
384 }
385 out.unindent();
386 out << "}\n";
387 }
388 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700389 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700390 out << "callflow: {\n";
391 out.indent();
Steven Morelandd537ab02016-09-12 10:32:01 -0700392 std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700393 if (name == "entry") {
394 out << "entry: true\n";
395 } else if (name == "exit") {
396 out << "exit: true\n";
397 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700398 const AnnotationParam *param =
399 annotation->getParam("next");
400 if (param != nullptr) {
401 for (auto value : *param->getValues()) {
402 out << "next: " << value << "\n";
403 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700404 }
405 } else {
406 std::cerr << "Invalid annotation '"
407 << name << "' for method: " << method->name()
408 << ". Should be one of: entry, exit, callflow. \n";
409 return UNKNOWN_ERROR;
410 }
411 out.unindent();
412 out << "}\n";
413 }
414 out.unindent();
415 out << "}\n\n";
416 }
417 return OK;
418}
419
420status_t Interface::emitVtsAttributeType(Formatter &out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800421 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700422 << "predefined_type: \""
423 << localName()
Zhuoyao Zhang19933522016-08-29 15:06:38 -0700424 << "\"\n"
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800425 << "is_callback: "
426 << (StringHelper::EndsWith(localName(), "Callback") ? "true" : "false")
427 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700428 return OK;
429}
430
Steven Moreland69e7c702016-09-09 11:16:32 -0700431bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700432 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700433 if (method->isOneway()) {
434 return true;
435 }
436 }
437
438 const Interface* superClass = superType();
439
440 if (superClass != nullptr) {
441 return superClass->hasOnewayMethods();
442 }
443
444 return false;
445}
446
Andreas Huber70a59e12016-08-16 12:57:01 -0700447bool Interface::isJavaCompatible() const {
Andreas Huberea081b32016-08-17 15:57:47 -0700448 if (mIsJavaCompatibleInProgress) {
449 // We're currently trying to determine if this Interface is
450 // java-compatible and something is referencing this interface through
451 // one of its methods. Assume we'll ultimately succeed, if we were wrong
452 // the original invocation of Interface::isJavaCompatible() will then
453 // return the correct "false" result.
454 return true;
455 }
456
Andreas Huber0fa9e392016-08-31 09:05:44 -0700457 if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) {
458 mIsJavaCompatibleInProgress = false;
459 return false;
460 }
461
Andreas Huberea081b32016-08-17 15:57:47 -0700462 mIsJavaCompatibleInProgress = true;
463
Andreas Huber70a59e12016-08-16 12:57:01 -0700464 if (!Scope::isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700465 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700466 return false;
467 }
468
Yifan Hong10fe0b52016-10-19 14:20:17 -0700469 for (const auto &method : methods()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700470 if (!method->isJavaCompatible()) {
Andreas Huberea081b32016-08-17 15:57:47 -0700471 mIsJavaCompatibleInProgress = false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700472 return false;
473 }
474 }
475
Andreas Huberea081b32016-08-17 15:57:47 -0700476 mIsJavaCompatibleInProgress = false;
477
Andreas Huber70a59e12016-08-16 12:57:01 -0700478 return true;
479}
480
Andreas Huberc9410c72016-07-28 12:18:40 -0700481} // namespace android
482