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