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