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