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