Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #include "Interface.h" |
| 18 | |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 19 | #include "Annotation.h" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 20 | #include "DeathRecipientType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 21 | #include "Method.h" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 22 | #include "ScalarType.h" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 23 | #include "StringType.h" |
| 24 | #include "VectorType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 25 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 27 | #include <hidl-util/Formatter.h> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 28 | #include <hidl-util/StringHelper.h> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 29 | #include <iostream> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 30 | #include <sstream> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 31 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 34 | /* It is very important that these values NEVER change. These values |
| 35 | * must remain unchanged over the lifetime of android. This is |
| 36 | * because the framework on a device will be updated independently of |
| 37 | * the hals on a device. If the hals are compiled with one set of |
| 38 | * transaction values, and the framework with another, then the |
| 39 | * interface between them will be destroyed, and the device will not |
| 40 | * work. |
| 41 | */ |
| 42 | enum { |
| 43 | // These values are defined in hardware::IBinder. |
| 44 | /////////////////// User defined transactions |
| 45 | FIRST_CALL_TRANSACTION = 0x00000001, |
| 46 | LAST_CALL_TRANSACTION = 0x00efffff, |
| 47 | /////////////////// HIDL reserved |
| 48 | FIRST_HIDL_TRANSACTION = 0x00f00000, |
| 49 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION = FIRST_HIDL_TRANSACTION, |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 50 | HIDL_GET_DESCRIPTOR_TRANSACTION, |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 51 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 52 | HIDL_LINK_TO_DEATH_TRANSACTION, |
| 53 | HIDL_UNLINK_TO_DEATH_TRANSACTION, |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 54 | HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION, |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 55 | HIDL_GET_REF_INFO_TRANSACTION, |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 56 | LAST_HIDL_TRANSACTION = 0x00ffffff, |
| 57 | }; |
| 58 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 59 | Interface::Interface(const char *localName, const Location &location, Interface *super) |
| 60 | : Scope(localName, location), |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 61 | mSuperType(super), |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 62 | mIsJavaCompatibleInProgress(false) { |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 65 | std::string Interface::typeName() const { |
| 66 | return "interface " + localName(); |
| 67 | } |
| 68 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 69 | bool Interface::fillLinkToDeathMethod(Method *method) const { |
| 70 | if (method->name() != "linkToDeath") { |
| 71 | return false; |
| 72 | } |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 73 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 74 | method->fillImplementation( |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 75 | HIDL_LINK_TO_DEATH_TRANSACTION, |
| 76 | { |
| 77 | {IMPL_HEADER, |
| 78 | [](auto &out) { |
| 79 | out << "(void)cookie;\n" |
| 80 | << "return (recipient != nullptr);\n"; |
| 81 | } |
| 82 | }, |
| 83 | {IMPL_PROXY, |
| 84 | [](auto &out) { |
Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 85 | out << "::android::hardware::ProcessState::self()->startThreadPool();\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 86 | out << "::android::hardware::hidl_binder_death_recipient *binder_recipient" |
| 87 | << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n" |
| 88 | << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| 89 | << "_hidl_mDeathRecipients.push_back(binder_recipient);\n" |
| 90 | << "return (remote()->linkToDeath(binder_recipient)" |
| 91 | << " == ::android::OK);\n"; |
| 92 | } |
| 93 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 94 | {IMPL_STUB, nullptr} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 95 | }, /*cppImpl*/ |
| 96 | { |
| 97 | {IMPL_HEADER, |
| 98 | [this](auto &out) { |
| 99 | out << "return true;"; |
| 100 | } |
| 101 | }, |
| 102 | {IMPL_PROXY, |
| 103 | [this](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 104 | out << "return mRemote.linkToDeath(recipient, cookie);\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 105 | } |
| 106 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 107 | {IMPL_STUB, nullptr} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 108 | } /*javaImpl*/ |
| 109 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 110 | return true; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 113 | bool Interface::fillUnlinkToDeathMethod(Method *method) const { |
| 114 | if (method->name() != "unlinkToDeath") { |
| 115 | return false; |
| 116 | } |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 117 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 118 | method->fillImplementation( |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 119 | HIDL_UNLINK_TO_DEATH_TRANSACTION, |
| 120 | { |
| 121 | {IMPL_HEADER, |
| 122 | [](auto &out) { |
| 123 | out << "return (recipient != nullptr);\n"; |
| 124 | } |
| 125 | }, |
| 126 | {IMPL_PROXY, |
| 127 | [](auto &out) { |
| 128 | out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| 129 | << "for (auto it = _hidl_mDeathRecipients.begin();" |
| 130 | << "it != _hidl_mDeathRecipients.end();" |
| 131 | << "++it) {\n"; |
| 132 | out.indent([&] { |
| 133 | out.sIf("(*it)->getRecipient() == recipient", [&] { |
| 134 | out << "::android::status_t status = remote()->unlinkToDeath(*it);\n" |
| 135 | << "_hidl_mDeathRecipients.erase(it);\n" |
| 136 | << "return status == ::android::OK;\n"; |
| 137 | }); |
| 138 | }); |
| 139 | out << "}\n"; |
| 140 | out << "return false;\n"; |
| 141 | } |
| 142 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 143 | {IMPL_STUB, nullptr /* don't generate code */} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 144 | }, /*cppImpl*/ |
| 145 | { |
| 146 | {IMPL_HEADER, |
| 147 | [this](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 148 | out << "return true;\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 149 | } |
| 150 | }, |
| 151 | {IMPL_PROXY, |
| 152 | [this](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 153 | out << "return mRemote.unlinkToDeath(recipient);\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 154 | } |
| 155 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 156 | {IMPL_STUB, nullptr /* don't generate code */} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 157 | } /*javaImpl*/ |
| 158 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 159 | return true; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 160 | } |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 161 | bool Interface::fillSyspropsChangedMethod(Method *method) const { |
| 162 | if (method->name() != "notifySyspropsChanged") { |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | method->fillImplementation( |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 167 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 168 | { { IMPL_HEADER, [this](auto &out) { |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 169 | out << "::android::report_sysprop_change();\n"; |
| 170 | out << "return ::android::hardware::Void();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 171 | } } }, /*cppImpl */ |
| 172 | { { IMPL_HEADER, [](auto &out) { /* javaImpl */ |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 173 | out << "android.os.SystemProperties.reportSyspropChanged();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 174 | } } } /*javaImpl */ |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 175 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 176 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 179 | bool Interface::fillSetHALInstrumentationMethod(Method *method) const { |
| 180 | if (method->name() != "setHALInstrumentation") { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | method->fillImplementation( |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 185 | HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION, |
| 186 | { |
| 187 | {IMPL_HEADER, |
| 188 | [this](auto &out) { |
| 189 | // do nothing for base class. |
| 190 | out << "return ::android::hardware::Void();\n"; |
| 191 | } |
| 192 | }, |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 193 | {IMPL_STUB, |
| 194 | [](auto &out) { |
| 195 | out << "configureInstrumentation();\n"; |
| 196 | } |
| 197 | }, |
| 198 | {IMPL_PASSTHROUGH, |
| 199 | [](auto &out) { |
| 200 | out << "configureInstrumentation();\n"; |
| 201 | out << "return ::android::hardware::Void();\n"; |
| 202 | } |
| 203 | }, |
| 204 | }, /*cppImpl */ |
| 205 | { { IMPL_HEADER, [](auto & /*out*/) { /* javaImpl */ |
| 206 | // Not support for Java Impl for now. |
| 207 | } } } /*javaImpl */ |
| 208 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 209 | return true; |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 212 | bool Interface::fillDescriptorChainMethod(Method *method) const { |
| 213 | if (method->name() != "interfaceChain") { |
| 214 | return false; |
| 215 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 216 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 217 | method->fillImplementation( |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 218 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 219 | { { IMPL_HEADER, [this](auto &out) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 220 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 0393512 | 2016-12-19 11:10:40 -0800 | [diff] [blame] | 221 | out << "_hidl_cb("; |
| 222 | out.block([&] { |
| 223 | for (const Interface *iface : chain) { |
| 224 | out << iface->fullName() << "::descriptor,\n"; |
| 225 | } |
| 226 | }); |
| 227 | out << ");\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 228 | out << "return ::android::hardware::Void();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 229 | } } }, /* cppImpl */ |
| 230 | { { IMPL_HEADER, [this](auto &out) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 231 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 232 | out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 233 | out.indent(); out.indent(); |
| 234 | for (size_t i = 0; i < chain.size(); ++i) { |
| 235 | if (i != 0) |
| 236 | out << ",\n"; |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 237 | out << chain[i]->fullJavaName() << ".kInterfaceName"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 238 | } |
| 239 | out << "));"; |
| 240 | out.unindent(); out.unindent(); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 241 | } } } /* javaImpl */ |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 242 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 243 | return true; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 246 | bool Interface::fillGetDescriptorMethod(Method *method) const { |
| 247 | if (method->name() != "interfaceDescriptor") { |
| 248 | return false; |
| 249 | } |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 250 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 251 | method->fillImplementation( |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 252 | HIDL_GET_DESCRIPTOR_TRANSACTION, |
| 253 | { { IMPL_HEADER, [this](auto &out) { |
| 254 | out << "_hidl_cb(" |
| 255 | << fullName() |
| 256 | << "::descriptor);\n" |
| 257 | << "return ::android::hardware::Void();"; |
| 258 | } } }, /* cppImpl */ |
| 259 | { { IMPL_HEADER, [this](auto &out) { |
| 260 | out << "return " |
| 261 | << fullJavaName() |
| 262 | << ".kInterfaceName;\n"; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 263 | } } } /* javaImpl */ |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 264 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 265 | return true; |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 266 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 267 | |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 268 | bool Interface::fillGetDebugInfoMethod(Method *method) const { |
| 269 | if (method->name() != "getDebugInfo") { |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | |
| 273 | method->fillImplementation( |
| 274 | HIDL_GET_REF_INFO_TRANSACTION, |
| 275 | { |
| 276 | {IMPL_HEADER, |
| 277 | [this](auto &out) { |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 278 | // getDebugInfo returns N/A for local objects. |
| 279 | out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */ });\n" |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 280 | << "return ::android::hardware::Void();"; |
| 281 | } |
| 282 | }, |
| 283 | {IMPL_STUB_IMPL, |
| 284 | [this](auto &out) { |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 285 | out << "_hidl_cb({ getpid(), reinterpret_cast<uint64_t>(this) });\n" |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 286 | << "return ::android::hardware::Void();"; |
| 287 | } |
| 288 | } |
| 289 | }, /* cppImpl */ |
| 290 | { { IMPL_HEADER, [this, method](auto &out) { |
| 291 | const Type &refInfo = method->results().front()->type(); |
| 292 | out << refInfo.getJavaType(false /* forInitializer */) << " info = new " |
| 293 | << refInfo.getJavaType(true /* forInitializer */) << "();\n" |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 294 | // TODO(b/34777099): PID for java. |
| 295 | << "info.pid = -1;\n" |
| 296 | << "info.ptr = 0;\n" |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 297 | << "return info;"; |
| 298 | } } } /* javaImpl */ |
| 299 | ); |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 304 | static std::map<std::string, Method *> gAllReservedMethods; |
| 305 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 306 | bool Interface::addMethod(Method *method) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 307 | if (isIBase()) { |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 308 | if (!gAllReservedMethods.emplace(method->name(), method).second) { |
| 309 | LOG(ERROR) << "ERROR: hidl-gen encountered duplicated reserved method " |
| 310 | << method->name(); |
| 311 | return false; |
| 312 | } |
| 313 | // will add it in addAllReservedMethods |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 317 | CHECK(!method->isHidlReserved()); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 318 | if (lookupMethod(method->name()) != nullptr) { |
| 319 | LOG(ERROR) << "Redefinition of method " << method->name(); |
| 320 | return false; |
| 321 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 322 | size_t serial = FIRST_CALL_TRANSACTION; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 323 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 324 | serial += userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 325 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 326 | const Interface *ancestor = mSuperType; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 327 | while (ancestor != nullptr) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 328 | serial += ancestor->userDefinedMethods().size(); |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 329 | ancestor = ancestor->superType(); |
| 330 | } |
| 331 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 332 | CHECK(serial <= LAST_CALL_TRANSACTION) << "More than " |
| 333 | << LAST_CALL_TRANSACTION << " methods are not allowed."; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 334 | method->setSerialId(serial); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 335 | mUserMethods.push_back(method); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 336 | |
| 337 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 340 | bool Interface::addAllReservedMethods() { |
| 341 | // use a sorted map to insert them in serial ID order. |
| 342 | std::map<int32_t, Method *> reservedMethodsById; |
| 343 | for (const auto &pair : gAllReservedMethods) { |
| 344 | Method *method = pair.second->copySignature(); |
| 345 | bool fillSuccess = fillDescriptorChainMethod(method) |
| 346 | || fillGetDescriptorMethod(method) |
| 347 | || fillSyspropsChangedMethod(method) |
| 348 | || fillLinkToDeathMethod(method) |
| 349 | || fillUnlinkToDeathMethod(method) |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 350 | || fillSetHALInstrumentationMethod(method) |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 351 | || fillGetDebugInfoMethod(method); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 352 | if (!fillSuccess) { |
| 353 | LOG(ERROR) << "ERROR: hidl-gen does not recognize a reserved method " |
| 354 | << method->name(); |
| 355 | return false; |
| 356 | } |
| 357 | if (!reservedMethodsById.emplace(method->getSerialId(), method).second) { |
| 358 | LOG(ERROR) << "ERROR: hidl-gen uses duplicated serial id for " |
| 359 | << method->name() << " and " |
| 360 | << reservedMethodsById[method->getSerialId()]->name() |
| 361 | << ", serialId = " << method->getSerialId(); |
| 362 | return false; |
| 363 | } |
| 364 | } |
| 365 | for (const auto &pair : reservedMethodsById) { |
| 366 | this->mReservedMethods.push_back(pair.second); |
| 367 | } |
| 368 | return true; |
| 369 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 370 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 371 | const Interface *Interface::superType() const { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 372 | return mSuperType; |
| 373 | } |
| 374 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 375 | std::vector<const Interface *> Interface::typeChain() const { |
| 376 | std::vector<const Interface *> v; |
| 377 | const Interface *iface = this; |
| 378 | while (iface != nullptr) { |
| 379 | v.push_back(iface); |
| 380 | iface = iface->mSuperType; |
| 381 | } |
| 382 | return v; |
| 383 | } |
| 384 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 385 | std::vector<const Interface *> Interface::superTypeChain() const { |
| 386 | return superType()->typeChain(); // should work even if superType is nullptr |
| 387 | } |
| 388 | |
Martijn Coenen | b40ef02 | 2017-01-02 15:21:46 +0100 | [diff] [blame] | 389 | bool Interface::isElidableType() const { |
| 390 | return true; |
| 391 | } |
| 392 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 393 | bool Interface::isInterface() const { |
| 394 | return true; |
| 395 | } |
| 396 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 397 | bool Interface::isBinder() const { |
| 398 | return true; |
| 399 | } |
| 400 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 401 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 402 | return mUserMethods; |
| 403 | } |
| 404 | |
| 405 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 406 | return mReservedMethods; |
| 407 | } |
| 408 | |
| 409 | std::vector<Method *> Interface::methods() const { |
| 410 | std::vector<Method *> v(mUserMethods); |
| 411 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 412 | return v; |
| 413 | } |
| 414 | |
| 415 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 416 | std::vector<InterfaceAndMethod> v; |
| 417 | std::vector<const Interface *> chain = typeChain(); |
| 418 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 419 | const Interface *iface = *it; |
| 420 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 421 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 422 | } |
| 423 | } |
| 424 | for (Method *reservedMethod : hidlReservedMethods()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 425 | v.push_back(InterfaceAndMethod( |
| 426 | *chain.rbegin(), // IBase |
| 427 | reservedMethod)); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 428 | } |
| 429 | return v; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 432 | Method *Interface::lookupMethod(std::string name) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 433 | for (const auto &tuple : allMethodsFromRoot()) { |
| 434 | Method *method = tuple.method(); |
| 435 | if (method->name() == name) { |
| 436 | return method; |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 437 | } |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | return nullptr; |
| 441 | } |
| 442 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 443 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 444 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 447 | std::string Interface::getProxyName() const { |
| 448 | return fqName().getInterfaceProxyName(); |
| 449 | } |
| 450 | |
| 451 | std::string Interface::getStubName() const { |
| 452 | return fqName().getInterfaceStubName(); |
| 453 | } |
| 454 | |
| 455 | std::string Interface::getHwName() const { |
| 456 | return fqName().getInterfaceHwName(); |
| 457 | } |
| 458 | |
| 459 | std::string Interface::getPassthroughName() const { |
| 460 | return fqName().getInterfacePassthroughName(); |
| 461 | } |
| 462 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 463 | FQName Interface::getProxyFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 464 | return fqName().getInterfaceProxyFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 467 | FQName Interface::getStubFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 468 | return fqName().getInterfaceStubFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 471 | FQName Interface::getPassthroughFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 472 | return fqName().getInterfacePassthroughFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 475 | std::string Interface::getCppType(StorageMode mode, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 476 | bool specifyNamespaces) const { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 477 | const std::string base = |
| 478 | std::string(specifyNamespaces ? "::android::" : "") |
| 479 | + "sp<" |
| 480 | + (specifyNamespaces ? fullName() : partialCppName()) |
| 481 | + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 482 | |
| 483 | switch (mode) { |
| 484 | case StorageMode_Stack: |
| 485 | case StorageMode_Result: |
| 486 | return base; |
| 487 | |
| 488 | case StorageMode_Argument: |
| 489 | return "const " + base + "&"; |
| 490 | } |
| 491 | } |
| 492 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 493 | std::string Interface::getJavaType(bool /* forInitializer */) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 494 | return fullJavaName(); |
| 495 | } |
| 496 | |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 497 | std::string Interface::getVtsType() const { |
| 498 | if (StringHelper::EndsWith(localName(), "Callback")) { |
| 499 | return "TYPE_HIDL_CALLBACK"; |
| 500 | } else { |
| 501 | return "TYPE_HIDL_INTERFACE"; |
| 502 | } |
| 503 | } |
| 504 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 505 | void Interface::emitReaderWriter( |
| 506 | Formatter &out, |
| 507 | const std::string &name, |
| 508 | const std::string &parcelObj, |
| 509 | bool parcelObjIsPointer, |
| 510 | bool isReader, |
| 511 | ErrorMode mode) const { |
| 512 | const std::string parcelObjDeref = |
| 513 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 514 | |
| 515 | if (isReader) { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 516 | out << "{\n"; |
| 517 | out.indent(); |
| 518 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 519 | const std::string binderName = "_hidl_" + name + "_binder"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 520 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 521 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 522 | << binderName << ";\n"; |
| 523 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 524 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 525 | out << parcelObjDeref |
| 526 | << "readNullableStrongBinder(&" |
| 527 | << binderName |
| 528 | << ");\n"; |
| 529 | |
| 530 | handleError(out, mode); |
| 531 | |
| 532 | out << name |
| 533 | << " = " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 534 | << "::android::hardware::fromBinder<" |
| 535 | << fqName().cppName() |
| 536 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 537 | << getProxyFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 538 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 539 | << getStubFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 540 | << ">(" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 541 | << binderName |
| 542 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 543 | |
| 544 | out.unindent(); |
| 545 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 546 | } else { |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 547 | out << "if (" << name << " == nullptr) {\n"; |
| 548 | out.indent(); |
| 549 | out << "_hidl_err = "; |
| 550 | out << parcelObjDeref |
| 551 | << "writeStrongBinder(nullptr);\n"; |
| 552 | out.unindent(); |
| 553 | out << "} else {\n"; |
| 554 | out.indent(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 555 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
| 556 | << "::android::hardware::toBinder<\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 557 | out.indent(2, [&] { |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 558 | out << fqName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 559 | << ", " |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 560 | << getProxyFqName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 561 | << ">(" |
| 562 | << name |
| 563 | << ");\n"; |
| 564 | }); |
| 565 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 566 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 567 | out << "_hidl_err = " |
| 568 | << parcelObjDeref |
| 569 | << "writeStrongBinder(_hidl_binder);\n"; |
| 570 | }); |
| 571 | out << "} else {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 572 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 573 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 574 | }); |
| 575 | out << "}\n"; |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 576 | out.unindent(); |
| 577 | out << "}\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 578 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 579 | handleError(out, mode); |
| 580 | } |
| 581 | } |
| 582 | |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 583 | status_t Interface::emitGlobalTypeDeclarations(Formatter &out) const { |
| 584 | status_t status = Scope::emitGlobalTypeDeclarations(out); |
| 585 | if (status != OK) { |
| 586 | return status; |
| 587 | } |
| 588 | out << "std::string toString(" |
| 589 | << getCppArgumentType() |
| 590 | << ");\n"; |
| 591 | return OK; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | status_t Interface::emitTypeDefinitions( |
| 596 | Formatter &out, const std::string prefix) const { |
| 597 | std::string space = prefix.empty() ? "" : (prefix + "::"); |
| 598 | status_t err = Scope::emitTypeDefinitions(out, space + localName()); |
| 599 | if (err != OK) { |
| 600 | return err; |
| 601 | } |
| 602 | |
| 603 | out << "std::string toString(" |
| 604 | << getCppArgumentType() |
| 605 | << " o) "; |
| 606 | |
| 607 | out.block([&] { |
| 608 | out << "std::string os;\nbool ok = false;\n"; |
| 609 | // TODO b/34136228 use interfaceDescriptor instead |
| 610 | out << "auto ret = o->interfaceChain([&os, &ok] (const auto &chain) "; |
| 611 | out.block([&] { |
| 612 | out.sIf("chain.size() >= 1", [&] { |
| 613 | out << "os += chain[0].c_str();\n" |
| 614 | << "ok = true;\n"; |
| 615 | }).endl(); |
| 616 | }); |
| 617 | out << ");\n"; |
| 618 | out.sIf("!ret.isOk() || !ok", [&] { |
| 619 | out << "os += \"[class or subclass of \";\n" |
| 620 | << "os += " << fullName() << "::descriptor;\n" |
| 621 | << "os += \"]\";\n"; |
| 622 | }).endl(); |
| 623 | out << "os += o->isRemote() ? \"@remote\" : \"@local\";\n" |
| 624 | << "return os;\n"; |
| 625 | }).endl().endl(); |
| 626 | |
| 627 | return OK; |
| 628 | } |
| 629 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 630 | void Interface::emitJavaReaderWriter( |
| 631 | Formatter &out, |
| 632 | const std::string &parcelObj, |
| 633 | const std::string &argName, |
| 634 | bool isReader) const { |
| 635 | if (isReader) { |
| 636 | out << fullJavaName() |
| 637 | << ".asInterface(" |
| 638 | << parcelObj |
| 639 | << ".readStrongBinder());\n"; |
| 640 | } else { |
| 641 | out << parcelObj |
| 642 | << ".writeStrongBinder(" |
| 643 | << argName |
| 644 | << " == null ? null : " |
| 645 | << argName |
| 646 | << ".asBinder());\n"; |
| 647 | } |
| 648 | } |
| 649 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 650 | status_t Interface::emitVtsAttributeDeclaration(Formatter &out) const { |
| 651 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 652 | // Skip for TypeDef as it is just an alias of a defined type. |
| 653 | if (type->isTypeDef()) { |
| 654 | continue; |
| 655 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 656 | out << "attribute: {\n"; |
| 657 | out.indent(); |
| 658 | status_t status = type->emitVtsTypeDeclarations(out); |
| 659 | if (status != OK) { |
| 660 | return status; |
| 661 | } |
| 662 | out.unindent(); |
| 663 | out << "}\n\n"; |
| 664 | } |
| 665 | return OK; |
| 666 | } |
| 667 | |
| 668 | status_t Interface::emitVtsMethodDeclaration(Formatter &out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 669 | for (const auto &method : methods()) { |
Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 670 | if (method->isHidlReserved()) { |
| 671 | continue; |
| 672 | } |
| 673 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 674 | out << "api: {\n"; |
| 675 | out.indent(); |
| 676 | out << "name: \"" << method->name() << "\"\n"; |
| 677 | // Generate declaration for each return value. |
| 678 | for (const auto &result : method->results()) { |
| 679 | out << "return_type_hidl: {\n"; |
| 680 | out.indent(); |
| 681 | status_t status = result->type().emitVtsAttributeType(out); |
| 682 | if (status != OK) { |
| 683 | return status; |
| 684 | } |
| 685 | out.unindent(); |
| 686 | out << "}\n"; |
| 687 | } |
| 688 | // Generate declaration for each input argument |
| 689 | for (const auto &arg : method->args()) { |
| 690 | out << "arg: {\n"; |
| 691 | out.indent(); |
| 692 | status_t status = arg->type().emitVtsAttributeType(out); |
| 693 | if (status != OK) { |
| 694 | return status; |
| 695 | } |
| 696 | out.unindent(); |
| 697 | out << "}\n"; |
| 698 | } |
| 699 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 700 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 701 | out << "callflow: {\n"; |
| 702 | out.indent(); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 703 | std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 704 | if (name == "entry") { |
| 705 | out << "entry: true\n"; |
| 706 | } else if (name == "exit") { |
| 707 | out << "exit: true\n"; |
| 708 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 709 | const AnnotationParam *param = |
| 710 | annotation->getParam("next"); |
| 711 | if (param != nullptr) { |
| 712 | for (auto value : *param->getValues()) { |
| 713 | out << "next: " << value << "\n"; |
| 714 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 715 | } |
| 716 | } else { |
| 717 | std::cerr << "Invalid annotation '" |
| 718 | << name << "' for method: " << method->name() |
| 719 | << ". Should be one of: entry, exit, callflow. \n"; |
| 720 | return UNKNOWN_ERROR; |
| 721 | } |
| 722 | out.unindent(); |
| 723 | out << "}\n"; |
| 724 | } |
| 725 | out.unindent(); |
| 726 | out << "}\n\n"; |
| 727 | } |
| 728 | return OK; |
| 729 | } |
| 730 | |
| 731 | status_t Interface::emitVtsAttributeType(Formatter &out) const { |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 732 | out << "type: " << getVtsType() << "\n" |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 733 | << "predefined_type: \"" |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 734 | << fullName() |
| 735 | << "\"\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 736 | return OK; |
| 737 | } |
| 738 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 739 | bool Interface::hasOnewayMethods() const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 740 | for (auto const &method : methods()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 741 | if (method->isOneway()) { |
| 742 | return true; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | const Interface* superClass = superType(); |
| 747 | |
| 748 | if (superClass != nullptr) { |
| 749 | return superClass->hasOnewayMethods(); |
| 750 | } |
| 751 | |
| 752 | return false; |
| 753 | } |
| 754 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 755 | bool Interface::isJavaCompatible() const { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 756 | if (mIsJavaCompatibleInProgress) { |
| 757 | // We're currently trying to determine if this Interface is |
| 758 | // java-compatible and something is referencing this interface through |
| 759 | // one of its methods. Assume we'll ultimately succeed, if we were wrong |
| 760 | // the original invocation of Interface::isJavaCompatible() will then |
| 761 | // return the correct "false" result. |
| 762 | return true; |
| 763 | } |
| 764 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 765 | if (mSuperType != nullptr && !mSuperType->isJavaCompatible()) { |
| 766 | mIsJavaCompatibleInProgress = false; |
| 767 | return false; |
| 768 | } |
| 769 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 770 | mIsJavaCompatibleInProgress = true; |
| 771 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 772 | if (!Scope::isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 773 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 774 | return false; |
| 775 | } |
| 776 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 777 | for (const auto &method : methods()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 778 | if (!method->isJavaCompatible()) { |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 779 | mIsJavaCompatibleInProgress = false; |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 780 | return false; |
| 781 | } |
| 782 | } |
| 783 | |
Andreas Huber | ea081b3 | 2016-08-17 15:57:47 -0700 | [diff] [blame] | 784 | mIsJavaCompatibleInProgress = false; |
| 785 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 786 | return true; |
| 787 | } |
| 788 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 789 | } // namespace android |
| 790 | |