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