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