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 | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 17 | #include "AST.h" |
| 18 | |
| 19 | #include "Coordinator.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 20 | #include "EnumType.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 21 | #include "Interface.h" |
| 22 | #include "Method.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 23 | #include "ScalarType.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 24 | #include "Scope.h" |
| 25 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 26 | #include <algorithm> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 27 | #include <hidl-util/Formatter.h> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 28 | #include <android-base/logging.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 29 | #include <string> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
| 32 | namespace android { |
| 33 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 34 | status_t AST::generateCpp(const std::string &outputPath) const { |
| 35 | status_t err = generateInterfaceHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 36 | |
| 37 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 38 | err = generateStubHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | if (err == OK) { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 42 | err = generateHwBinderHeader(outputPath); |
| 43 | } |
| 44 | |
| 45 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 46 | err = generateProxyHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 50 | err = generateAllSource(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 53 | if (err == OK) { |
| 54 | generatePassthroughHeader(outputPath); |
| 55 | } |
| 56 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 57 | return err; |
| 58 | } |
| 59 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 60 | void AST::getPackageComponents( |
| 61 | std::vector<std::string> *components) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 62 | mPackage.getPackageComponents(components); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void AST::getPackageAndVersionComponents( |
| 66 | std::vector<std::string> *components, bool cpp_compatible) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 67 | mPackage.getPackageAndVersionComponents(components, cpp_compatible); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 70 | std::string AST::makeHeaderGuard(const std::string &baseName) const { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 71 | std::string guard = "HIDL_GENERATED_"; |
| 72 | guard += mPackage.tokenName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 73 | |
| 74 | guard += "_"; |
| 75 | guard += baseName; |
| 76 | guard += "_H_"; |
| 77 | |
| 78 | return guard; |
| 79 | } |
| 80 | |
| 81 | void AST::enterLeaveNamespace(Formatter &out, bool enter) const { |
| 82 | std::vector<std::string> packageComponents; |
| 83 | getPackageAndVersionComponents( |
| 84 | &packageComponents, true /* cpp_compatible */); |
| 85 | |
| 86 | if (enter) { |
| 87 | for (const auto &component : packageComponents) { |
| 88 | out << "namespace " << component << " {\n"; |
| 89 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 90 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 91 | out.setNamespace(mPackage.cppNamespace() + "::"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 92 | } else { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 93 | out.setNamespace(std::string()); |
| 94 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 95 | for (auto it = packageComponents.rbegin(); |
| 96 | it != packageComponents.rend(); |
| 97 | ++it) { |
| 98 | out << "} // namespace " << *it << "\n"; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 103 | status_t AST::generateInterfaceHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 104 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 105 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 106 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 107 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 108 | |
| 109 | std::string ifaceName; |
| 110 | bool isInterface = true; |
| 111 | if (!AST::isInterface(&ifaceName)) { |
| 112 | ifaceName = "types"; |
| 113 | isInterface = false; |
| 114 | } |
| 115 | path.append(ifaceName); |
| 116 | path.append(".h"); |
| 117 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 118 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 119 | FILE *file = fopen(path.c_str(), "w"); |
| 120 | |
| 121 | if (file == NULL) { |
| 122 | return -errno; |
| 123 | } |
| 124 | |
| 125 | Formatter out(file); |
| 126 | |
| 127 | const std::string guard = makeHeaderGuard(ifaceName); |
| 128 | |
| 129 | out << "#ifndef " << guard << "\n"; |
| 130 | out << "#define " << guard << "\n\n"; |
| 131 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 132 | for (const auto &item : mImportedNames) { |
| 133 | out << "#include <"; |
| 134 | |
| 135 | std::vector<std::string> components; |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 136 | item.getPackageAndVersionComponents( |
| 137 | &components, false /* cpp_compatible */); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 138 | |
| 139 | for (const auto &component : components) { |
| 140 | out << component << "/"; |
| 141 | } |
| 142 | |
| 143 | out << item.name() |
| 144 | << ".h>\n"; |
| 145 | } |
| 146 | |
| 147 | if (!mImportedNames.empty()) { |
| 148 | out << "\n"; |
| 149 | } |
| 150 | |
Martijn Coenen | 7473fab | 2016-08-19 14:05:40 +0200 | [diff] [blame] | 151 | out << "#include <hidl/HidlSupport.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 152 | out << "#include <hidl/IServiceManager.h>\n"; |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 153 | out << "#include <hidl/MQDescriptor.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 154 | |
| 155 | if (isInterface) { |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 156 | out << "#include <hidl/Status.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | out << "#include <utils/NativeHandle.h>\n\n"; |
| 160 | |
| 161 | enterLeaveNamespace(out, true /* enter */); |
| 162 | out << "\n"; |
| 163 | |
| 164 | if (isInterface) { |
| 165 | out << "struct " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 166 | << ifaceName; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 167 | |
| 168 | const Interface *iface = mRootScope->getInterface(); |
| 169 | const Interface *superType = iface->superType(); |
| 170 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 171 | if (superType == NULL) { |
| 172 | out << " : virtual public RefBase"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 173 | } else { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 174 | out << " : public " |
| 175 | << superType->fullName(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | out << " {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 179 | |
| 180 | out.indent(); |
| 181 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | status_t err = emitTypeDeclarations(out); |
| 185 | |
| 186 | if (err != OK) { |
| 187 | return err; |
| 188 | } |
| 189 | |
| 190 | if (isInterface) { |
| 191 | const Interface *iface = mRootScope->getInterface(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 192 | const Interface *superType = iface->superType(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 193 | const std::string baseName = iface->getBaseName(); |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 194 | out << "constexpr static hidl_version version = {" |
| 195 | << mPackage.getPackageMajorVersion() << "," |
| 196 | << mPackage.getPackageMinorVersion() << "};\n"; |
| 197 | out << "virtual const hidl_version& getInterfaceVersion() const {\n"; |
| 198 | out.indent(); |
| 199 | out << "return version;\n"; |
| 200 | out.unindent(); |
| 201 | out << "}\n\n"; |
Iliyan Malchev | 795dd43 | 2016-09-02 11:48:26 -0700 | [diff] [blame] | 202 | out << "virtual bool isRemote() const { return false; }\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 203 | bool haveCallbacks = false; |
| 204 | for (const auto &method : iface->methods()) { |
| 205 | const bool returnsValue = !method->results().empty(); |
| 206 | |
| 207 | if (!returnsValue) { |
| 208 | continue; |
| 209 | } |
| 210 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 211 | if (method->canElideCallback() != nullptr) { |
| 212 | continue; |
| 213 | } |
| 214 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 215 | haveCallbacks = true; |
| 216 | |
| 217 | out << "using " |
| 218 | << method->name() |
| 219 | << "_cb = std::function<void(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 220 | << Method::GetArgSignature(method->results(), |
| 221 | true /* specify namespaces */) |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 222 | << ")>;\n"; |
| 223 | } |
| 224 | |
| 225 | if (haveCallbacks) { |
| 226 | out << "\n"; |
| 227 | } |
| 228 | |
| 229 | for (const auto &method : iface->methods()) { |
| 230 | const bool returnsValue = !method->results().empty(); |
| 231 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 232 | method->dumpAnnotations(out); |
| 233 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 234 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 235 | if (elidedReturn) { |
| 236 | std::string extra; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 237 | out << "virtual ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 238 | out << elidedReturn->type().getCppResultType(&extra) << "> "; |
| 239 | } else { |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 240 | out << "virtual ::android::hardware::Return<void> "; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | out << method->name() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 244 | << "(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 245 | << Method::GetArgSignature(method->args(), |
| 246 | true /* specify namespaces */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 247 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 248 | if (returnsValue && elidedReturn == nullptr) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 249 | if (!method->args().empty()) { |
| 250 | out << ", "; |
| 251 | } |
| 252 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 253 | out << method->name() << "_cb _hidl_cb"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | out << ") = 0;\n"; |
| 257 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 258 | |
| 259 | out << "DECLARE_REGISTER_AND_GET_SERVICE(" << baseName << ")\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | if (isInterface) { |
| 263 | out.unindent(); |
| 264 | |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 265 | out << "};\n\n"; |
| 266 | } |
| 267 | |
| 268 | err = mRootScope->emitGlobalTypeDeclarations(out); |
| 269 | |
| 270 | if (err != OK) { |
| 271 | return err; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | out << "\n"; |
| 275 | enterLeaveNamespace(out, false /* enter */); |
| 276 | |
| 277 | out << "\n#endif // " << guard << "\n"; |
| 278 | |
| 279 | return OK; |
| 280 | } |
| 281 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 282 | status_t AST::generateHwBinderHeader(const std::string &outputPath) const { |
| 283 | std::string ifaceName; |
| 284 | if(!AST::isInterface(&ifaceName)) { |
| 285 | // types.hal does not get an HwBinder header. |
| 286 | return OK; |
| 287 | } |
| 288 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 289 | const Interface *iface = mRootScope->getInterface(); |
| 290 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 291 | |
| 292 | const std::string klassName = "IHw" + baseName; |
| 293 | |
| 294 | std::string path = outputPath; |
| 295 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 296 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 297 | path.append(klassName + ".h"); |
| 298 | |
| 299 | FILE* file = fopen(path.c_str(), "w"); |
| 300 | |
| 301 | if (file == NULL) { |
| 302 | return -errno; |
| 303 | } |
| 304 | |
| 305 | Formatter out(file); |
| 306 | |
| 307 | const std::string guard = makeHeaderGuard(klassName); |
| 308 | |
| 309 | out << "#ifndef " << guard << "\n"; |
| 310 | out << "#define " << guard << "\n\n"; |
| 311 | |
| 312 | std::vector<std::string> packageComponents; |
| 313 | getPackageAndVersionComponents( |
| 314 | &packageComponents, false /* cpp_compatible */); |
| 315 | |
| 316 | out << "#include <"; |
| 317 | for (const auto &component : packageComponents) { |
| 318 | out << component << "/"; |
| 319 | } |
| 320 | out << ifaceName << ".h>\n\n"; |
| 321 | |
| 322 | for (const auto &item : mImportedNames) { |
| 323 | if (item.name() == "types") { |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | out << "#include <"; |
| 328 | |
| 329 | std::vector<std::string> components; |
| 330 | item.getPackageAndVersionComponents( |
| 331 | &components, false /* cpp_compatible */); |
| 332 | |
| 333 | for (const auto &component : components) { |
| 334 | out << component << "/"; |
| 335 | } |
| 336 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 337 | const std::string itemBaseName = item.getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 338 | |
| 339 | out << "Bn" |
| 340 | << itemBaseName |
| 341 | << ".h>\n"; |
| 342 | } |
| 343 | |
| 344 | out << "\n"; |
| 345 | |
| 346 | out << "#include <hidl/HidlSupport.h>\n"; |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 347 | out << "#include <hidl/Status.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 348 | out << "#include <hwbinder/IBinder.h>\n"; |
| 349 | out << "#include <hwbinder/IInterface.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 350 | |
| 351 | out << "\n"; |
| 352 | |
| 353 | enterLeaveNamespace(out, true /* enter */); |
| 354 | out << "\n"; |
| 355 | |
| 356 | out << "struct " |
| 357 | << klassName |
| 358 | << " : public " |
| 359 | << ifaceName; |
| 360 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 361 | const Interface *superType = iface->superType(); |
| 362 | |
| 363 | out << ", public ::android::hardware::IInterface"; |
| 364 | |
| 365 | out << " {\n"; |
| 366 | |
| 367 | out.indent(); |
| 368 | |
| 369 | out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n"; |
| 370 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 371 | out.unindent(); |
| 372 | |
| 373 | out << "};\n\n"; |
| 374 | |
| 375 | enterLeaveNamespace(out, false /* enter */); |
| 376 | |
| 377 | out << "\n#endif // " << guard << "\n"; |
| 378 | |
| 379 | return OK; |
| 380 | } |
| 381 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 382 | status_t AST::emitTypeDeclarations(Formatter &out) const { |
| 383 | return mRootScope->emitTypeDeclarations(out); |
| 384 | } |
| 385 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 386 | status_t AST::generateStubMethod(Formatter &out, |
| 387 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 388 | const Method *method, |
| 389 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 390 | out << "inline "; |
| 391 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 392 | method->generateCppSignature(out, |
| 393 | className, |
| 394 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 395 | |
| 396 | const bool returnsValue = !method->results().empty(); |
| 397 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 398 | out << " {\n"; |
| 399 | out.indent(); |
| 400 | out << "return mImpl->" |
| 401 | << method->name() |
| 402 | << "("; |
| 403 | bool first = true; |
| 404 | for (const auto &arg : method->args()) { |
| 405 | if (!first) { |
| 406 | out << ", "; |
| 407 | } |
| 408 | first = false; |
| 409 | out << arg->name(); |
| 410 | } |
| 411 | if (returnsValue && elidedReturn == nullptr) { |
| 412 | if (!method->args().empty()) { |
| 413 | out << ", "; |
| 414 | } |
| 415 | |
| 416 | out << "_hidl_cb"; |
| 417 | } |
| 418 | out << ");\n"; |
| 419 | out.unindent(); |
| 420 | out << "}"; |
| 421 | |
| 422 | out << ";\n"; |
| 423 | |
| 424 | return OK; |
| 425 | } |
| 426 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 427 | status_t AST::generateProxyDeclaration(Formatter &out, |
| 428 | const std::string &className, |
| 429 | const Method *method, |
| 430 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 431 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 432 | method->generateCppSignature(out, |
| 433 | className, |
| 434 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 435 | out << " override;\n"; |
| 436 | |
| 437 | return OK; |
| 438 | } |
| 439 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 440 | |
| 441 | status_t AST::generatePassthroughMethod(Formatter &out, |
| 442 | const std::string &className, |
| 443 | const Method *method, |
| 444 | bool specifyNamespaces) const { |
| 445 | method->generateCppSignature(out, className, specifyNamespaces); |
| 446 | |
| 447 | out << " {\n"; |
| 448 | out.indent(); |
| 449 | |
| 450 | const bool returnsValue = !method->results().empty(); |
| 451 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 452 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 453 | if (returnsValue && elidedReturn == nullptr) { |
| 454 | generateCheckNonNull(out, "_hidl_cb"); |
| 455 | } |
| 456 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 457 | out << "return "; |
| 458 | |
| 459 | if (method->isOneway()) { |
| 460 | out << "addOnewayTask([this"; |
| 461 | for (const auto &arg : method->args()) { |
| 462 | out << ", " << arg->name(); |
| 463 | } |
| 464 | out << "] {this->"; |
| 465 | } |
| 466 | |
| 467 | out << "mImpl->" |
| 468 | << method->name() |
| 469 | << "("; |
| 470 | |
| 471 | bool first = true; |
| 472 | for (const auto &arg : method->args()) { |
| 473 | if (!first) { |
| 474 | out << ", "; |
| 475 | } |
| 476 | first = false; |
| 477 | out << arg->name(); |
| 478 | } |
| 479 | if (returnsValue && elidedReturn == nullptr) { |
| 480 | if (!method->args().empty()) { |
| 481 | out << ", "; |
| 482 | } |
| 483 | |
| 484 | out << "_hidl_cb"; |
| 485 | } |
| 486 | out << ")"; |
| 487 | |
| 488 | if (method->isOneway()) { |
| 489 | out << ";})"; |
| 490 | } |
| 491 | out << ";\n"; |
| 492 | |
| 493 | out.unindent(); |
| 494 | out << "}\n"; |
| 495 | |
| 496 | return OK; |
| 497 | } |
| 498 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 499 | status_t AST::generateMethods( |
| 500 | Formatter &out, |
| 501 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 502 | MethodLocation type, |
| 503 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 504 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 505 | const Interface *iface = mRootScope->getInterface(); |
| 506 | |
| 507 | std::vector<const Interface *> chain; |
| 508 | while (iface != NULL) { |
| 509 | chain.push_back(iface); |
| 510 | iface = iface->superType(); |
| 511 | } |
| 512 | |
| 513 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 514 | const Interface *superInterface = *it; |
| 515 | |
| 516 | out << "// Methods from " |
| 517 | << superInterface->fullName() |
| 518 | << " follow.\n"; |
| 519 | |
| 520 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 521 | status_t err; |
| 522 | switch(type) { |
| 523 | case STUB_HEADER: |
| 524 | err = generateStubMethod(out, |
| 525 | className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 526 | method, |
| 527 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 528 | break; |
| 529 | case PROXY_HEADER: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 530 | err = generateProxyDeclaration(out, |
| 531 | className, |
| 532 | method, |
| 533 | specifyNamespaces); |
| 534 | break; |
| 535 | case IMPL_HEADER: |
| 536 | err = generateStubImplDeclaration(out, |
| 537 | className, |
| 538 | method, |
| 539 | specifyNamespaces); |
| 540 | break; |
| 541 | case IMPL_SOURCE: |
| 542 | err = generateStubImplMethod(out, |
| 543 | className, |
| 544 | method, |
| 545 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 546 | break; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 547 | case PASSTHROUGH_HEADER: |
| 548 | err = generatePassthroughMethod(out, |
| 549 | className, |
| 550 | method, |
| 551 | specifyNamespaces); |
| 552 | break; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 553 | default: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 554 | LOG(ERROR) << "Unkown method type: " << type; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 555 | err = UNKNOWN_ERROR; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 558 | if (err != OK) { |
| 559 | return err; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 560 | } |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | out << "\n"; |
| 564 | } |
| 565 | |
| 566 | return OK; |
| 567 | } |
| 568 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 569 | status_t AST::generateStubHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 570 | std::string ifaceName; |
| 571 | if (!AST::isInterface(&ifaceName)) { |
| 572 | // types.hal does not get a stub header. |
| 573 | return OK; |
| 574 | } |
| 575 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 576 | const Interface *iface = mRootScope->getInterface(); |
| 577 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 578 | const std::string klassName = "Bn" + baseName; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 579 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 580 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 581 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 582 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 583 | path.append(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 584 | path.append(".h"); |
| 585 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 586 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 587 | FILE *file = fopen(path.c_str(), "w"); |
| 588 | |
| 589 | if (file == NULL) { |
| 590 | return -errno; |
| 591 | } |
| 592 | |
| 593 | Formatter out(file); |
| 594 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 595 | const std::string guard = makeHeaderGuard(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 596 | |
| 597 | out << "#ifndef " << guard << "\n"; |
| 598 | out << "#define " << guard << "\n\n"; |
| 599 | |
| 600 | std::vector<std::string> packageComponents; |
| 601 | getPackageAndVersionComponents( |
| 602 | &packageComponents, false /* cpp_compatible */); |
| 603 | |
| 604 | out << "#include <"; |
| 605 | for (const auto &component : packageComponents) { |
| 606 | out << component << "/"; |
| 607 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 608 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 609 | |
| 610 | enterLeaveNamespace(out, true /* enter */); |
| 611 | out << "\n"; |
| 612 | |
| 613 | out << "struct " |
| 614 | << "Bn" |
| 615 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 616 | << " : public ::android::hardware::BnInterface<I" |
| 617 | << baseName << ", IHw" << baseName |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 618 | << ">, public ::android::hardware::HidlInstrumentor {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 619 | |
| 620 | out.indent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 621 | out << "explicit Bn" |
| 622 | << baseName |
| 623 | << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);" |
| 624 | << "\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 625 | out << "::android::status_t onTransact(\n"; |
| 626 | out.indent(); |
| 627 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 628 | out << "uint32_t _hidl_code,\n"; |
| 629 | out << "const ::android::hardware::Parcel &_hidl_data,\n"; |
| 630 | out << "::android::hardware::Parcel *_hidl_reply,\n"; |
| 631 | out << "uint32_t _hidl_flags = 0,\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 632 | out << "TransactCallback _hidl_cb = nullptr) override;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 633 | out.unindent(); |
| 634 | out.unindent(); |
| 635 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 636 | status_t err = generateMethods(out, |
| 637 | "" /* class name */, |
| 638 | MethodLocation::STUB_HEADER, |
| 639 | true /* specify namespaces */); |
| 640 | |
| 641 | if (err != OK) { |
| 642 | return err; |
| 643 | } |
| 644 | |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 645 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 646 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 647 | out << "};\n\n"; |
| 648 | |
| 649 | enterLeaveNamespace(out, false /* enter */); |
| 650 | |
| 651 | out << "\n#endif // " << guard << "\n"; |
| 652 | |
| 653 | return OK; |
| 654 | } |
| 655 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 656 | status_t AST::generateProxyHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 657 | std::string ifaceName; |
| 658 | if (!AST::isInterface(&ifaceName)) { |
| 659 | // types.hal does not get a proxy header. |
| 660 | return OK; |
| 661 | } |
| 662 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 663 | const Interface *iface = mRootScope->getInterface(); |
| 664 | const std::string baseName = iface->getBaseName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 665 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 666 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 667 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 668 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 669 | path.append("Bp"); |
| 670 | path.append(baseName); |
| 671 | path.append(".h"); |
| 672 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 673 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 674 | FILE *file = fopen(path.c_str(), "w"); |
| 675 | |
| 676 | if (file == NULL) { |
| 677 | return -errno; |
| 678 | } |
| 679 | |
| 680 | Formatter out(file); |
| 681 | |
| 682 | const std::string guard = makeHeaderGuard("Bp" + baseName); |
| 683 | |
| 684 | out << "#ifndef " << guard << "\n"; |
| 685 | out << "#define " << guard << "\n\n"; |
| 686 | |
| 687 | std::vector<std::string> packageComponents; |
| 688 | getPackageAndVersionComponents( |
| 689 | &packageComponents, false /* cpp_compatible */); |
| 690 | |
| 691 | out << "#include <"; |
| 692 | for (const auto &component : packageComponents) { |
| 693 | out << component << "/"; |
| 694 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 695 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 696 | |
| 697 | enterLeaveNamespace(out, true /* enter */); |
| 698 | out << "\n"; |
| 699 | |
| 700 | out << "struct " |
| 701 | << "Bp" |
| 702 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 703 | << " : public ::android::hardware::BpInterface<IHw" |
| 704 | << baseName |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 705 | << ">, public ::android::hardware::HidlInstrumentor {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 706 | |
| 707 | out.indent(); |
| 708 | |
| 709 | out << "explicit Bp" |
| 710 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 711 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 712 | << "\n\n"; |
| 713 | |
Iliyan Malchev | 795dd43 | 2016-09-02 11:48:26 -0700 | [diff] [blame] | 714 | out << "virtual bool isRemote() const { return true; }\n\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 715 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 716 | status_t err = generateMethods(out, |
| 717 | "" /* class name */, |
| 718 | MethodLocation::PROXY_HEADER, |
| 719 | true /* generate specify namespaces */); |
| 720 | |
| 721 | if (err != OK) { |
| 722 | return err; |
| 723 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 724 | |
| 725 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 726 | out << "};\n\n"; |
| 727 | |
| 728 | enterLeaveNamespace(out, false /* enter */); |
| 729 | |
| 730 | out << "\n#endif // " << guard << "\n"; |
| 731 | |
| 732 | return OK; |
| 733 | } |
| 734 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 735 | status_t AST::generateAllSource(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 736 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 737 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 738 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 739 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 740 | |
| 741 | std::string ifaceName; |
| 742 | std::string baseName; |
| 743 | |
| 744 | bool isInterface = true; |
| 745 | if (!AST::isInterface(&ifaceName)) { |
| 746 | baseName = "types"; |
| 747 | isInterface = false; |
| 748 | } else { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 749 | const Interface *iface = mRootScope->getInterface(); |
| 750 | baseName = iface->getBaseName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | path.append(baseName); |
| 754 | |
| 755 | if (baseName != "types") { |
| 756 | path.append("All"); |
| 757 | } |
| 758 | |
| 759 | path.append(".cpp"); |
| 760 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 761 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 762 | FILE *file = fopen(path.c_str(), "w"); |
| 763 | |
| 764 | if (file == NULL) { |
| 765 | return -errno; |
| 766 | } |
| 767 | |
| 768 | Formatter out(file); |
| 769 | |
| 770 | std::vector<std::string> packageComponents; |
| 771 | getPackageAndVersionComponents( |
| 772 | &packageComponents, false /* cpp_compatible */); |
| 773 | |
| 774 | std::string prefix; |
| 775 | for (const auto &component : packageComponents) { |
| 776 | prefix += component; |
| 777 | prefix += "/"; |
| 778 | } |
| 779 | |
| 780 | if (isInterface) { |
| 781 | out << "#include <" << prefix << "/Bp" << baseName << ".h>\n"; |
| 782 | out << "#include <" << prefix << "/Bn" << baseName << ".h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 783 | out << "#include <" << prefix << "/Bs" << baseName << ".h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 784 | } else { |
| 785 | out << "#include <" << prefix << "types.h>\n"; |
| 786 | } |
| 787 | |
| 788 | out << "\n"; |
| 789 | |
| 790 | enterLeaveNamespace(out, true /* enter */); |
| 791 | out << "\n"; |
| 792 | |
| 793 | status_t err = generateTypeSource(out, ifaceName); |
| 794 | |
| 795 | if (err == OK && isInterface) { |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 796 | out << "constexpr hidl_version " << ifaceName << "::version;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 797 | err = generateProxySource(out, baseName); |
| 798 | } |
| 799 | |
| 800 | if (err == OK && isInterface) { |
| 801 | err = generateStubSource(out, baseName); |
| 802 | } |
| 803 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 804 | if (err == OK && isInterface) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 805 | err = generatePassthroughSource(out); |
| 806 | } |
| 807 | |
| 808 | if (err == OK && isInterface) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 809 | const Interface *iface = mRootScope->getInterface(); |
| 810 | |
| 811 | out << "IMPLEMENT_REGISTER_AND_GET_SERVICE(" |
| 812 | << baseName << ", " |
| 813 | << "\"" << iface->fqName().package() |
Iliyan Malchev | 4923f93 | 2016-09-09 13:04:59 -0700 | [diff] [blame] | 814 | << iface->fqName().version() << "-impl.so\"" |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 815 | << ")\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 818 | enterLeaveNamespace(out, false /* enter */); |
| 819 | |
| 820 | return err; |
| 821 | } |
| 822 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 823 | // static |
| 824 | void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) { |
| 825 | out << "if (" << nonNull << " == nullptr) {\n"; |
| 826 | out.indent(); |
| 827 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 828 | out.indent(); |
| 829 | out.indent(); |
| 830 | out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT);\n"; |
| 831 | out.unindent(); |
| 832 | out.unindent(); |
| 833 | out.unindent(); |
| 834 | out << "}\n\n"; |
| 835 | } |
| 836 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 837 | status_t AST::generateTypeSource( |
| 838 | Formatter &out, const std::string &ifaceName) const { |
| 839 | return mRootScope->emitTypeDefinitions(out, ifaceName); |
| 840 | } |
| 841 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 842 | void AST::declareCppReaderLocals( |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 843 | Formatter &out, |
| 844 | const std::vector<TypedVar *> &args, |
| 845 | bool forResults) const { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 846 | if (args.empty()) { |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | for (const auto &arg : args) { |
| 851 | const Type &type = arg->type(); |
| 852 | |
| 853 | std::string extra; |
| 854 | out << type.getCppResultType(&extra) |
| 855 | << " " |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 856 | << (forResults ? "_hidl_out_" : "") |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 857 | << arg->name() |
| 858 | << extra |
| 859 | << ";\n"; |
| 860 | } |
| 861 | |
| 862 | out << "\n"; |
| 863 | } |
| 864 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 865 | void AST::emitCppReaderWriter( |
| 866 | Formatter &out, |
| 867 | const std::string &parcelObj, |
| 868 | bool parcelObjIsPointer, |
| 869 | const TypedVar *arg, |
| 870 | bool isReader, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 871 | Type::ErrorMode mode, |
| 872 | bool addPrefixToName) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 873 | const Type &type = arg->type(); |
| 874 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 875 | type.emitReaderWriter( |
| 876 | out, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 877 | addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(), |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 878 | parcelObj, |
| 879 | parcelObjIsPointer, |
| 880 | isReader, |
| 881 | mode); |
| 882 | } |
| 883 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 884 | void AST::emitCppResolveReferences( |
| 885 | Formatter &out, |
| 886 | const std::string &parcelObj, |
| 887 | bool parcelObjIsPointer, |
| 888 | const TypedVar *arg, |
| 889 | bool isReader, |
| 890 | Type::ErrorMode mode, |
| 891 | bool addPrefixToName) const { |
| 892 | const Type &type = arg->type(); |
| 893 | if(type.needsResolveReferences()) { |
| 894 | type.emitResolveReferences( |
| 895 | out, |
| 896 | addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(), |
| 897 | isReader, // nameIsPointer |
| 898 | parcelObj, |
| 899 | parcelObjIsPointer, |
| 900 | isReader, |
| 901 | mode); |
| 902 | } |
| 903 | } |
| 904 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 905 | status_t AST::generateProxySource( |
| 906 | Formatter &out, const std::string &baseName) const { |
| 907 | const std::string klassName = "Bp" + baseName; |
| 908 | |
| 909 | out << klassName |
| 910 | << "::" |
| 911 | << klassName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 912 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 913 | |
| 914 | out.indent(); |
| 915 | out.indent(); |
| 916 | |
| 917 | out << ": BpInterface" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 918 | << "<IHw" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 919 | << baseName |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 920 | << ">(_hidl_impl),\n" |
| 921 | << " HidlInstrumentor(\"" |
| 922 | << mPackage.string() |
| 923 | << "::I" |
| 924 | << baseName |
| 925 | << "\") {\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 926 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 927 | out.unindent(); |
| 928 | out.unindent(); |
| 929 | out << "}\n\n"; |
| 930 | |
| 931 | const Interface *iface = mRootScope->getInterface(); |
| 932 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 933 | std::vector<const Interface *> chain; |
| 934 | while (iface != NULL) { |
| 935 | chain.push_back(iface); |
| 936 | iface = iface->superType(); |
| 937 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 938 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 939 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 940 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 941 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 942 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 943 | method->generateCppSignature(out, |
| 944 | klassName, |
| 945 | true /* specify namespaces */); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 946 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 947 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 948 | const TypedVar *elidedReturn = method->canElideCallback(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 949 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 950 | out << "{\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 951 | |
| 952 | out.indent(); |
| 953 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 954 | if (returnsValue && elidedReturn == nullptr) { |
| 955 | generateCheckNonNull(out, "_hidl_cb"); |
| 956 | } |
| 957 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 958 | status_t status = generateCppInstrumentationCall( |
| 959 | out, |
| 960 | InstrumentationEvent::CLIENT_API_ENTRY, |
| 961 | superInterface, |
| 962 | method); |
| 963 | if (status != OK) { |
| 964 | return status; |
| 965 | } |
| 966 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 967 | out << "::android::hardware::Parcel _hidl_data;\n"; |
| 968 | out << "::android::hardware::Parcel _hidl_reply;\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 969 | out << "::android::status_t _hidl_err;\n"; |
| 970 | out << "::android::hardware::Status _hidl_status;\n\n"; |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 971 | |
| 972 | declareCppReaderLocals( |
| 973 | out, method->results(), true /* forResults */); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 974 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 975 | out << "_hidl_err = _hidl_data.writeInterfaceToken(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 976 | << superInterface->fqName().cppNamespace() |
| 977 | << "::IHw" |
| 978 | << superInterface->getBaseName() |
| 979 | << "::descriptor);\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 980 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 981 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 982 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 983 | // First DFS: write all buffers and resolve pointers for parent |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 984 | for (const auto &arg : method->args()) { |
| 985 | emitCppReaderWriter( |
| 986 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 987 | "_hidl_data", |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 988 | false /* parcelObjIsPointer */, |
| 989 | arg, |
| 990 | false /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 991 | Type::ErrorMode_Goto, |
| 992 | false /* addPrefixToName */); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 995 | // Second DFS: resolve references. |
| 996 | for (const auto &arg : method->args()) { |
| 997 | emitCppResolveReferences( |
| 998 | out, |
| 999 | "_hidl_data", |
| 1000 | false /* parcelObjIsPointer */, |
| 1001 | arg, |
| 1002 | false /* reader */, |
| 1003 | Type::ErrorMode_Goto, |
| 1004 | false /* addPrefixToName */); |
| 1005 | } |
| 1006 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1007 | out << "_hidl_err = remote()->transact(" |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 1008 | << method->getSerialId() |
| 1009 | << " /* " |
| 1010 | << method->name() |
| 1011 | << " */, _hidl_data, &_hidl_reply"; |
| 1012 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1013 | if (method->isOneway()) { |
| 1014 | out << ", ::android::hardware::IBinder::FLAG_ONEWAY"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1015 | } |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1016 | out << ");\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1017 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1018 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1019 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1020 | if (!method->isOneway()) { |
| 1021 | out << "_hidl_err = _hidl_status.readFromParcel(_hidl_reply);\n"; |
| 1022 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1023 | out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n"; |
| 1024 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1025 | |
| 1026 | // First DFS: write all buffers and resolve pointers for parent |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1027 | for (const auto &arg : method->results()) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1028 | emitCppReaderWriter( |
| 1029 | out, |
| 1030 | "_hidl_reply", |
| 1031 | false /* parcelObjIsPointer */, |
| 1032 | arg, |
| 1033 | true /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1034 | Type::ErrorMode_Goto, |
| 1035 | true /* addPrefixToName */); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1038 | // Second DFS: resolve references. |
| 1039 | for (const auto &arg : method->results()) { |
| 1040 | emitCppResolveReferences( |
| 1041 | out, |
| 1042 | "_hidl_reply", |
| 1043 | false /* parcelObjIsPointer */, |
| 1044 | arg, |
| 1045 | true /* reader */, |
| 1046 | Type::ErrorMode_Goto, |
| 1047 | true /* addPrefixToName */); |
| 1048 | } |
| 1049 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1050 | if (returnsValue && elidedReturn == nullptr) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1051 | out << "_hidl_cb("; |
| 1052 | |
| 1053 | bool first = true; |
| 1054 | for (const auto &arg : method->results()) { |
| 1055 | if (!first) { |
| 1056 | out << ", "; |
| 1057 | } |
| 1058 | |
| 1059 | if (arg->type().resultNeedsDeref()) { |
| 1060 | out << "*"; |
| 1061 | } |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1062 | out << "_hidl_out_" << arg->name(); |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1063 | |
| 1064 | first = false; |
| 1065 | } |
| 1066 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 1067 | out << ");\n\n"; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 1068 | } |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1069 | status_t status = generateCppInstrumentationCall( |
| 1070 | out, |
| 1071 | InstrumentationEvent::CLIENT_API_EXIT, |
| 1072 | superInterface, |
| 1073 | method); |
| 1074 | if (status != OK) { |
| 1075 | return status; |
| 1076 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1079 | if (elidedReturn != nullptr) { |
| 1080 | std::string extra; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1081 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 1082 | out << "return ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1083 | out << elidedReturn->type().getCppResultType(&extra) |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1084 | << ">(_hidl_out_" << elidedReturn->name() << ");\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1085 | } else { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1086 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1087 | out << "return ::android::hardware::Return<void>();\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1088 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1089 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1090 | out.unindent(); |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1091 | out << "_hidl_error:\n"; |
| 1092 | out.indent(); |
| 1093 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1094 | out << "return ::android::hardware::Return<"; |
| 1095 | if (elidedReturn != nullptr) { |
| 1096 | std::string extra; |
| 1097 | out << method->results().at(0)->type().getCppResultType(&extra); |
| 1098 | } else { |
| 1099 | out << "void"; |
| 1100 | } |
| 1101 | out << ">(_hidl_status);\n"; |
| 1102 | |
| 1103 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1104 | out << "}\n\n"; |
| 1105 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | return OK; |
| 1109 | } |
| 1110 | |
| 1111 | status_t AST::generateStubSource( |
| 1112 | Formatter &out, const std::string &baseName) const { |
| 1113 | out << "IMPLEMENT_HWBINDER_META_INTERFACE(" |
| 1114 | << baseName |
| 1115 | << ", \"" |
| 1116 | << mPackage.string() |
| 1117 | << "::I" |
| 1118 | << baseName |
| 1119 | << "\");\n\n"; |
| 1120 | |
| 1121 | const std::string klassName = "Bn" + baseName; |
| 1122 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1123 | out << klassName |
| 1124 | << "::" |
| 1125 | << klassName |
| 1126 | << "(const ::android::sp<I" << baseName <<"> &_hidl_impl)\n"; |
| 1127 | |
| 1128 | out.indent(); |
| 1129 | out.indent(); |
| 1130 | |
| 1131 | out << ": BnInterface" |
| 1132 | << "<I" |
| 1133 | << baseName |
| 1134 | << ", IHw" |
| 1135 | << baseName |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1136 | << ">(_hidl_impl),\n" |
| 1137 | << " HidlInstrumentor(\"" |
| 1138 | << mPackage.string() |
| 1139 | << "::I" |
| 1140 | << baseName |
| 1141 | << "\") {\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1142 | |
| 1143 | out.unindent(); |
| 1144 | out.unindent(); |
| 1145 | out << "}\n\n"; |
| 1146 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1147 | out << "::android::status_t " << klassName << "::onTransact(\n"; |
| 1148 | |
| 1149 | out.indent(); |
| 1150 | out.indent(); |
| 1151 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1152 | out << "uint32_t _hidl_code,\n" |
| 1153 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 1154 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 1155 | << "uint32_t _hidl_flags,\n" |
| 1156 | << "TransactCallback _hidl_cb) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1157 | |
| 1158 | out.unindent(); |
| 1159 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1160 | out << "::android::status_t _hidl_err = ::android::OK;\n\n"; |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1161 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1162 | out.indent(); |
| 1163 | |
| 1164 | const Interface *iface = mRootScope->getInterface(); |
| 1165 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1166 | std::vector<const Interface *> chain; |
| 1167 | while (iface != NULL) { |
| 1168 | chain.push_back(iface); |
| 1169 | iface = iface->superType(); |
| 1170 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1171 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1172 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 1173 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1174 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1175 | for (const auto &method : superInterface->methods()) { |
| 1176 | out << "case " |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 1177 | << method->getSerialId() |
| 1178 | << " /* " |
| 1179 | << method->name() |
| 1180 | << " */:\n{\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1181 | |
| 1182 | out.indent(); |
| 1183 | |
| 1184 | status_t err = |
| 1185 | generateStubSourceForMethod(out, superInterface, method); |
| 1186 | |
| 1187 | if (err != OK) { |
| 1188 | return err; |
| 1189 | } |
| 1190 | |
| 1191 | out.unindent(); |
| 1192 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1193 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | out << "default:\n{\n"; |
| 1197 | out.indent(); |
| 1198 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1199 | out << "return ::android::hardware::BnInterface<I" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1200 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1201 | << ">::onTransact(\n"; |
| 1202 | |
| 1203 | out.indent(); |
| 1204 | out.indent(); |
| 1205 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1206 | out << "_hidl_code, _hidl_data, _hidl_reply, " |
| 1207 | << "_hidl_flags, _hidl_cb);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1208 | |
| 1209 | out.unindent(); |
| 1210 | out.unindent(); |
| 1211 | |
| 1212 | out.unindent(); |
| 1213 | out << "}\n"; |
| 1214 | |
| 1215 | out.unindent(); |
| 1216 | out << "}\n\n"; |
| 1217 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1218 | out << "if (_hidl_err == ::android::UNEXPECTED_NULL) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1219 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1220 | out << "_hidl_err = ::android::hardware::Status::fromExceptionCode(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1221 | out.indent(); |
| 1222 | out.indent(); |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1223 | out << "::android::hardware::Status::EX_NULL_POINTER)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1224 | out.indent(); |
| 1225 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1226 | out << ".writeToParcel(_hidl_reply);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1227 | out.unindent(); |
| 1228 | out.unindent(); |
| 1229 | out.unindent(); |
| 1230 | out.unindent(); |
| 1231 | |
| 1232 | out.unindent(); |
| 1233 | out << "}\n\n"; |
| 1234 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1235 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1236 | |
| 1237 | out.unindent(); |
| 1238 | out << "}\n\n"; |
| 1239 | |
| 1240 | return OK; |
| 1241 | } |
| 1242 | |
| 1243 | status_t AST::generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1244 | Formatter &out, const Interface *iface, const Method *method) const { |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1245 | out << "if (!_hidl_data.enforceInterface(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1246 | << iface->fqName().cppNamespace() |
| 1247 | << "::IHw" |
| 1248 | << iface->getBaseName() |
| 1249 | << "::descriptor)) {\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1250 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1251 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1252 | out << "_hidl_err = ::android::BAD_TYPE;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1253 | out << "break;\n"; |
| 1254 | out.unindent(); |
| 1255 | out << "}\n\n"; |
| 1256 | |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1257 | declareCppReaderLocals(out, method->args(), false /* forResults */); |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1258 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1259 | // First DFS: write buffers |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1260 | for (const auto &arg : method->args()) { |
| 1261 | emitCppReaderWriter( |
| 1262 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1263 | "_hidl_data", |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1264 | false /* parcelObjIsPointer */, |
| 1265 | arg, |
| 1266 | true /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1267 | Type::ErrorMode_Break, |
| 1268 | false /* addPrefixToName */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1271 | // Second DFS: resolve references |
| 1272 | for (const auto &arg : method->args()) { |
| 1273 | emitCppResolveReferences( |
| 1274 | out, |
| 1275 | "_hidl_data", |
| 1276 | false /* parcelObjIsPointer */, |
| 1277 | arg, |
| 1278 | true /* reader */, |
| 1279 | Type::ErrorMode_Break, |
| 1280 | false /* addPrefixToName */); |
| 1281 | } |
| 1282 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1283 | status_t status = generateCppInstrumentationCall( |
| 1284 | out, |
| 1285 | InstrumentationEvent::SERVER_API_ENTRY, |
| 1286 | iface, |
| 1287 | method); |
| 1288 | if (status != OK) { |
| 1289 | return status; |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1292 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1293 | const TypedVar *elidedReturn = method->canElideCallback(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1294 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1295 | if (elidedReturn != nullptr) { |
| 1296 | std::string extra; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1297 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1298 | out << elidedReturn->type().getCppResultType(&extra) << " "; |
| 1299 | out << elidedReturn->name() << " = "; |
| 1300 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1301 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1302 | bool first = true; |
| 1303 | for (const auto &arg : method->args()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1304 | if (!first) { |
| 1305 | out << ", "; |
| 1306 | } |
| 1307 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1308 | if (arg->type().resultNeedsDeref()) { |
| 1309 | out << "*"; |
| 1310 | } |
| 1311 | |
| 1312 | out << arg->name(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1313 | |
| 1314 | first = false; |
| 1315 | } |
| 1316 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1317 | out << ");\n\n"; |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1318 | out << "::android::hardware::Status::ok()" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1319 | << ".writeToParcel(_hidl_reply);\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1320 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1321 | elidedReturn->type().emitReaderWriter( |
| 1322 | out, |
| 1323 | elidedReturn->name(), |
| 1324 | "_hidl_reply", |
| 1325 | true, /* parcelObjIsPointer */ |
| 1326 | false, /* isReader */ |
| 1327 | Type::ErrorMode_Ignore); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1328 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1329 | emitCppResolveReferences( |
| 1330 | out, |
| 1331 | "_hidl_reply", |
| 1332 | true /* parcelObjIsPointer */, |
| 1333 | elidedReturn, |
| 1334 | false /* reader */, |
| 1335 | Type::ErrorMode_Ignore, |
| 1336 | false /* addPrefixToName */); |
| 1337 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1338 | status_t status = generateCppInstrumentationCall( |
| 1339 | out, |
| 1340 | InstrumentationEvent::SERVER_API_EXIT, |
| 1341 | iface, |
| 1342 | method); |
| 1343 | if (status != OK) { |
| 1344 | return status; |
| 1345 | } |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1346 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1347 | out << "_hidl_cb(*_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1348 | } else { |
| 1349 | if (returnsValue) { |
| 1350 | out << "bool _hidl_callbackCalled = false;\n\n"; |
| 1351 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1352 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1353 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1354 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1355 | bool first = true; |
| 1356 | for (const auto &arg : method->args()) { |
| 1357 | if (!first) { |
| 1358 | out << ", "; |
| 1359 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1360 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1361 | if (arg->type().resultNeedsDeref()) { |
| 1362 | out << "*"; |
| 1363 | } |
| 1364 | |
| 1365 | out << arg->name(); |
| 1366 | |
| 1367 | first = false; |
| 1368 | } |
| 1369 | |
| 1370 | if (returnsValue) { |
| 1371 | if (!first) { |
| 1372 | out << ", "; |
| 1373 | } |
| 1374 | |
| 1375 | out << "[&]("; |
| 1376 | |
| 1377 | first = true; |
| 1378 | for (const auto &arg : method->results()) { |
| 1379 | if (!first) { |
| 1380 | out << ", "; |
| 1381 | } |
| 1382 | |
| 1383 | out << "const auto &" << arg->name(); |
| 1384 | |
| 1385 | first = false; |
| 1386 | } |
| 1387 | |
| 1388 | out << ") {\n"; |
| 1389 | out.indent(); |
| 1390 | out << "_hidl_callbackCalled = true;\n\n"; |
| 1391 | |
| 1392 | out << "::android::hardware::Status::ok()" |
| 1393 | << ".writeToParcel(_hidl_reply);\n\n"; |
| 1394 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1395 | // First DFS: buffers |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1396 | for (const auto &arg : method->results()) { |
| 1397 | emitCppReaderWriter( |
| 1398 | out, |
| 1399 | "_hidl_reply", |
| 1400 | true /* parcelObjIsPointer */, |
| 1401 | arg, |
| 1402 | false /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1403 | Type::ErrorMode_Ignore, |
| 1404 | false /* addPrefixToName */); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1407 | // Second DFS: resolve references |
| 1408 | for (const auto &arg : method->results()) { |
| 1409 | emitCppResolveReferences( |
| 1410 | out, |
| 1411 | "_hidl_reply", |
| 1412 | true /* parcelObjIsPointer */, |
| 1413 | arg, |
| 1414 | false /* reader */, |
| 1415 | Type::ErrorMode_Ignore, |
| 1416 | false /* addPrefixToName */); |
| 1417 | } |
| 1418 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1419 | status_t status = generateCppInstrumentationCall( |
| 1420 | out, |
| 1421 | InstrumentationEvent::SERVER_API_EXIT, |
| 1422 | iface, |
| 1423 | method); |
| 1424 | if (status != OK) { |
| 1425 | return status; |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1428 | out << "_hidl_cb(*_hidl_reply);\n"; |
| 1429 | |
| 1430 | out.unindent(); |
| 1431 | out << "}\n"; |
| 1432 | } |
| 1433 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1434 | out << ");\n\n"; |
| 1435 | |
| 1436 | // What to do if the stub implementation has a synchronous callback |
| 1437 | // which does not get invoked? This is not a transport error but a |
| 1438 | // service error of sorts. For now, return OK to the caller, as this is |
| 1439 | // not a transport error. |
| 1440 | // |
| 1441 | // TODO(b/31365311) Figure out how to deal with this later. |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1442 | |
| 1443 | if (returnsValue) { |
| 1444 | out << "if (!_hidl_callbackCalled) {\n"; |
| 1445 | out.indent(); |
| 1446 | } |
| 1447 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1448 | out << "::android::hardware::Status::ok()" |
| 1449 | << ".writeToParcel(_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1450 | |
| 1451 | if (returnsValue) { |
| 1452 | out.unindent(); |
| 1453 | out << "}\n\n"; |
| 1454 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | out << "break;\n"; |
| 1458 | |
| 1459 | return OK; |
| 1460 | } |
| 1461 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1462 | status_t AST::generatePassthroughHeader(const std::string &outputPath) const { |
| 1463 | std::string ifaceName; |
| 1464 | if (!AST::isInterface(&ifaceName)) { |
| 1465 | // types.hal does not get a stub header. |
| 1466 | return OK; |
| 1467 | } |
| 1468 | |
| 1469 | const Interface *iface = mRootScope->getInterface(); |
| 1470 | |
| 1471 | const std::string baseName = iface->getBaseName(); |
| 1472 | const std::string klassName = "Bs" + baseName; |
| 1473 | |
| 1474 | bool supportOneway = iface->hasOnewayMethods(); |
| 1475 | |
| 1476 | std::string path = outputPath; |
| 1477 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 1478 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 1479 | path.append(klassName); |
| 1480 | path.append(".h"); |
| 1481 | |
| 1482 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 1483 | FILE *file = fopen(path.c_str(), "w"); |
| 1484 | |
| 1485 | if (file == NULL) { |
| 1486 | return -errno; |
| 1487 | } |
| 1488 | |
| 1489 | Formatter out(file); |
| 1490 | |
| 1491 | const std::string guard = makeHeaderGuard(klassName); |
| 1492 | |
| 1493 | out << "#ifndef " << guard << "\n"; |
| 1494 | out << "#define " << guard << "\n\n"; |
| 1495 | |
| 1496 | std::vector<std::string> packageComponents; |
| 1497 | getPackageAndVersionComponents( |
| 1498 | &packageComponents, false /* cpp_compatible */); |
| 1499 | |
| 1500 | out << "#include <future>\n"; |
| 1501 | out << "#include <"; |
| 1502 | for (const auto &component : packageComponents) { |
| 1503 | out << component << "/"; |
| 1504 | } |
| 1505 | out << ifaceName << ".h>\n\n"; |
| 1506 | |
| 1507 | if (supportOneway) { |
| 1508 | out << "#include <hidl/SynchronizedQueue.h>\n"; |
| 1509 | } |
| 1510 | |
| 1511 | enterLeaveNamespace(out, true /* enter */); |
| 1512 | out << "\n"; |
| 1513 | |
| 1514 | out << "struct " |
| 1515 | << klassName |
| 1516 | << " : " << ifaceName |
| 1517 | << " {\n"; |
| 1518 | |
| 1519 | out.indent(); |
| 1520 | out << "explicit " |
| 1521 | << klassName |
| 1522 | << "(const sp<" |
| 1523 | << ifaceName |
| 1524 | << "> impl);\n"; |
| 1525 | |
| 1526 | status_t err = generateMethods(out, |
| 1527 | "" /* class name */, |
| 1528 | MethodLocation::PASSTHROUGH_HEADER, |
| 1529 | true /* specify namespaces */); |
| 1530 | |
| 1531 | if (err != OK) { |
| 1532 | return err; |
| 1533 | } |
| 1534 | |
| 1535 | out.unindent(); |
| 1536 | out << "private:\n"; |
| 1537 | out.indent(); |
| 1538 | out << "const sp<" << ifaceName << "> mImpl;\n"; |
| 1539 | |
| 1540 | if (supportOneway) { |
| 1541 | out << "SynchronizedQueue<std::function<void(void)>> mOnewayQueue;\n"; |
| 1542 | out << "std::thread *mOnewayThread = nullptr;\n"; |
| 1543 | |
| 1544 | out << "\n"; |
| 1545 | |
| 1546 | out << "::android::hardware::Return<void> addOnewayTask(" |
| 1547 | "std::function<void(void)>);\n\n"; |
| 1548 | |
| 1549 | out << "static const int kOnewayQueueMaxSize = 3000;\n"; |
| 1550 | } |
| 1551 | |
| 1552 | out.unindent(); |
| 1553 | |
| 1554 | out << "};\n\n"; |
| 1555 | |
| 1556 | enterLeaveNamespace(out, false /* enter */); |
| 1557 | |
| 1558 | out << "\n#endif // " << guard << "\n"; |
| 1559 | |
| 1560 | return OK; |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | status_t AST::generatePassthroughSource(Formatter &out) const { |
| 1565 | const Interface *iface = mRootScope->getInterface(); |
| 1566 | |
| 1567 | const std::string baseName = iface->getBaseName(); |
| 1568 | const std::string klassName = "Bs" + baseName; |
| 1569 | |
| 1570 | out << klassName |
| 1571 | << "::" |
| 1572 | << klassName |
| 1573 | << "(const sp<" |
| 1574 | << iface->fullName() |
| 1575 | << "> impl) : mImpl(impl) {}\n\n"; |
| 1576 | |
| 1577 | if (iface->hasOnewayMethods()) { |
| 1578 | out << "::android::hardware::Return<void> " |
| 1579 | << klassName |
| 1580 | << "::addOnewayTask(std::function<void(void)> fun) {\n"; |
| 1581 | out.indent(); |
| 1582 | out << "if (mOnewayThread == nullptr) {\n"; |
| 1583 | out.indent(); |
| 1584 | out << "mOnewayThread = new std::thread([this]() {\n"; |
| 1585 | out.indent(); |
| 1586 | out << "while(true) { (this->mOnewayQueue.wait_pop())(); }"; |
| 1587 | out.unindent(); |
| 1588 | out << "});\n"; |
| 1589 | out.unindent(); |
| 1590 | out << "}\n\n"; |
| 1591 | |
| 1592 | out << "if (mOnewayQueue.size() > kOnewayQueueMaxSize) {\n"; |
| 1593 | out.indent(); |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 1594 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 1595 | out.indent(); |
| 1596 | out.indent(); |
| 1597 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED);\n"; |
| 1598 | out.unindent(); |
| 1599 | out.unindent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1600 | out.unindent(); |
| 1601 | out << "} else {\n"; |
| 1602 | out.indent(); |
| 1603 | out << "mOnewayQueue.push(fun);\n"; |
| 1604 | out.unindent(); |
| 1605 | out << "}\n"; |
| 1606 | |
Steven Moreland | d366c26 | 2016-10-11 15:29:10 -0700 | [diff] [blame] | 1607 | out << "return ::android::hardware::Status();\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1608 | |
| 1609 | out.unindent(); |
| 1610 | out << "}\n\n"; |
| 1611 | |
| 1612 | |
| 1613 | } |
| 1614 | |
| 1615 | return OK; |
| 1616 | } |
| 1617 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1618 | status_t AST::generateCppInstrumentationCall( |
| 1619 | Formatter &out, |
| 1620 | InstrumentationEvent event, |
| 1621 | const Interface *iface, const Method *method) const { |
| 1622 | out << "if (UNLIKELY(mEnableInstrumentation)) {\n"; |
| 1623 | out.indent(); |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1624 | out << "std::vector<void *> _hidl_args;\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1625 | std::string event_str = ""; |
| 1626 | switch (event) { |
| 1627 | case SERVER_API_ENTRY: |
| 1628 | { |
| 1629 | event_str = "InstrumentationEvent::SERVER_API_ENTRY"; |
| 1630 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1631 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1632 | << (arg->type().resultNeedsDeref() ? "" : "&") |
| 1633 | << arg->name() |
| 1634 | << ");\n"; |
| 1635 | } |
| 1636 | break; |
| 1637 | } |
| 1638 | case SERVER_API_EXIT: |
| 1639 | { |
| 1640 | event_str = "InstrumentationEvent::SERVER_API_EXIT"; |
| 1641 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 1642 | if (elidedReturn != nullptr) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1643 | out << "_hidl_args.push_back((void *)&" |
| 1644 | << elidedReturn->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1645 | << ");\n"; |
| 1646 | } else { |
| 1647 | for (const auto &arg : method->results()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1648 | out << "_hidl_args.push_back((void *)&" |
| 1649 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1650 | << ");\n"; |
| 1651 | } |
| 1652 | } |
| 1653 | break; |
| 1654 | } |
| 1655 | case CLIENT_API_ENTRY: |
| 1656 | { |
| 1657 | event_str = "InstrumentationEvent::CLIENT_API_ENTRY"; |
| 1658 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1659 | out << "_hidl_args.push_back((void *)&" |
| 1660 | << arg->name() |
| 1661 | << ");\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1662 | } |
| 1663 | break; |
| 1664 | } |
| 1665 | case CLIENT_API_EXIT: |
| 1666 | { |
| 1667 | event_str = "InstrumentationEvent::CLIENT_API_EXIT"; |
| 1668 | for (const auto &arg : method->results()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1669 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1670 | << (arg->type().resultNeedsDeref() ? "" : "&") |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1671 | << "_hidl_out_" |
| 1672 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1673 | << ");\n"; |
| 1674 | } |
| 1675 | break; |
| 1676 | } |
| 1677 | case SYNC_CALLBACK_ENTRY: |
| 1678 | case SYNC_CALLBACK_EXIT: |
| 1679 | case ASYNC_CALLBACK_ENTRY: |
| 1680 | case ASYNC_CALLBACK_EXIT: |
| 1681 | { |
| 1682 | LOG(ERROR) << "Not supported instrumentation event: " << event; |
| 1683 | return UNKNOWN_ERROR; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | out << "for (auto callback: mInstrumentationCallbacks) {\n"; |
| 1688 | out.indent(); |
| 1689 | out << "callback(" |
| 1690 | << event_str |
| 1691 | << ", \"" |
| 1692 | << mPackage.package() |
| 1693 | << "\", \"" |
| 1694 | << mPackage.getPackageFullVersion() |
| 1695 | << "\", \"" |
| 1696 | << iface->localName() |
| 1697 | << "\", \"" |
| 1698 | << method->name() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame^] | 1699 | << "\", &_hidl_args);\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1700 | out.unindent(); |
| 1701 | out << "}\n"; |
| 1702 | out.unindent(); |
| 1703 | out << "}\n\n"; |
| 1704 | |
| 1705 | return OK; |
| 1706 | } |
| 1707 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1708 | } // namespace android |
| 1709 | |