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/StringHelper.h> |
| 28 | #include <hidl-util/Formatter.h> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 30 | #include <string> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | namespace android { |
| 34 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 35 | status_t AST::generateCpp(const std::string &outputPath) const { |
| 36 | status_t err = generateInterfaceHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 37 | |
| 38 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 39 | err = generateStubHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | if (err == OK) { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 43 | err = generateHwBinderHeader(outputPath); |
| 44 | } |
| 45 | |
| 46 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 47 | err = generateProxyHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 51 | err = generateAllSource(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | return err; |
| 55 | } |
| 56 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 57 | void AST::getPackageComponents( |
| 58 | std::vector<std::string> *components) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 59 | mPackage.getPackageComponents(components); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void AST::getPackageAndVersionComponents( |
| 63 | std::vector<std::string> *components, bool cpp_compatible) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 64 | mPackage.getPackageAndVersionComponents(components, cpp_compatible); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 67 | std::string AST::makeHeaderGuard(const std::string &baseName) const { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 68 | std::string guard = "HIDL_GENERATED_"; |
| 69 | guard += mPackage.tokenName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 70 | |
| 71 | guard += "_"; |
| 72 | guard += baseName; |
| 73 | guard += "_H_"; |
| 74 | |
| 75 | return guard; |
| 76 | } |
| 77 | |
| 78 | void AST::enterLeaveNamespace(Formatter &out, bool enter) const { |
| 79 | std::vector<std::string> packageComponents; |
| 80 | getPackageAndVersionComponents( |
| 81 | &packageComponents, true /* cpp_compatible */); |
| 82 | |
| 83 | if (enter) { |
| 84 | for (const auto &component : packageComponents) { |
| 85 | out << "namespace " << component << " {\n"; |
| 86 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 87 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 88 | out.setNamespace(mPackage.cppNamespace() + "::"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 89 | } else { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 90 | out.setNamespace(std::string()); |
| 91 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 92 | for (auto it = packageComponents.rbegin(); |
| 93 | it != packageComponents.rend(); |
| 94 | ++it) { |
| 95 | out << "} // namespace " << *it << "\n"; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 100 | status_t AST::generateInterfaceHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 101 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 102 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 103 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 104 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 105 | |
| 106 | std::string ifaceName; |
| 107 | bool isInterface = true; |
| 108 | if (!AST::isInterface(&ifaceName)) { |
| 109 | ifaceName = "types"; |
| 110 | isInterface = false; |
| 111 | } |
| 112 | path.append(ifaceName); |
| 113 | path.append(".h"); |
| 114 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 115 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 116 | FILE *file = fopen(path.c_str(), "w"); |
| 117 | |
| 118 | if (file == NULL) { |
| 119 | return -errno; |
| 120 | } |
| 121 | |
| 122 | Formatter out(file); |
| 123 | |
| 124 | const std::string guard = makeHeaderGuard(ifaceName); |
| 125 | |
| 126 | out << "#ifndef " << guard << "\n"; |
| 127 | out << "#define " << guard << "\n\n"; |
| 128 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 129 | for (const auto &item : mImportedNames) { |
| 130 | out << "#include <"; |
| 131 | |
| 132 | std::vector<std::string> components; |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 133 | item.getPackageAndVersionComponents( |
| 134 | &components, false /* cpp_compatible */); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 135 | |
| 136 | for (const auto &component : components) { |
| 137 | out << component << "/"; |
| 138 | } |
| 139 | |
| 140 | out << item.name() |
| 141 | << ".h>\n"; |
| 142 | } |
| 143 | |
| 144 | if (!mImportedNames.empty()) { |
| 145 | out << "\n"; |
| 146 | } |
| 147 | |
Martijn Coenen | 7473fab | 2016-08-19 14:05:40 +0200 | [diff] [blame] | 148 | out << "#include <hidl/HidlSupport.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 149 | out << "#include <hidl/IServiceManager.h>\n"; |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 150 | out << "#include <hidl/MQDescriptor.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 151 | |
| 152 | if (isInterface) { |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 153 | out << "#include <hidl/Status.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | out << "#include <utils/NativeHandle.h>\n\n"; |
| 157 | |
| 158 | enterLeaveNamespace(out, true /* enter */); |
| 159 | out << "\n"; |
| 160 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 161 | // cut off the leading 'I'. |
| 162 | const std::string baseName = ifaceName.substr(1); |
| 163 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 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(); |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 193 | out << "constexpr static hidl_version version = {" |
| 194 | << mPackage.getPackageMajorVersion() << "," |
| 195 | << mPackage.getPackageMinorVersion() << "};\n"; |
| 196 | out << "virtual const hidl_version& getInterfaceVersion() const {\n"; |
| 197 | out.indent(); |
| 198 | out << "return version;\n"; |
| 199 | out.unindent(); |
| 200 | out << "}\n\n"; |
Iliyan Malchev | 795dd43 | 2016-09-02 11:48:26 -0700 | [diff] [blame] | 201 | out << "virtual bool isRemote() const { return false; }\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 202 | bool haveCallbacks = false; |
| 203 | for (const auto &method : iface->methods()) { |
| 204 | const bool returnsValue = !method->results().empty(); |
| 205 | |
| 206 | if (!returnsValue) { |
| 207 | continue; |
| 208 | } |
| 209 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 210 | if (method->canElideCallback() != nullptr) { |
| 211 | continue; |
| 212 | } |
| 213 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 214 | haveCallbacks = true; |
| 215 | |
| 216 | out << "using " |
| 217 | << method->name() |
| 218 | << "_cb = std::function<void(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 219 | << Method::GetArgSignature(method->results(), |
| 220 | true /* specify namespaces */) |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 221 | << ")>;\n"; |
| 222 | } |
| 223 | |
| 224 | if (haveCallbacks) { |
| 225 | out << "\n"; |
| 226 | } |
| 227 | |
| 228 | for (const auto &method : iface->methods()) { |
| 229 | const bool returnsValue = !method->results().empty(); |
| 230 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 231 | method->dumpAnnotations(out); |
| 232 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 233 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 234 | if (elidedReturn) { |
| 235 | std::string extra; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 236 | out << "virtual ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 237 | out << elidedReturn->type().getCppResultType(&extra) << "> "; |
| 238 | } else { |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 239 | out << "virtual ::android::hardware::Return<void> "; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | out << method->name() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 243 | << "(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 244 | << Method::GetArgSignature(method->args(), |
| 245 | true /* specify namespaces */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 246 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 247 | if (returnsValue && elidedReturn == nullptr) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 248 | if (!method->args().empty()) { |
| 249 | out << ", "; |
| 250 | } |
| 251 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 252 | out << method->name() << "_cb _hidl_cb = nullptr"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | out << ") = 0;\n"; |
| 256 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 257 | |
| 258 | out << "DECLARE_REGISTER_AND_GET_SERVICE(" << baseName << ")\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (isInterface) { |
| 262 | out.unindent(); |
| 263 | |
| 264 | out << "};\n"; |
| 265 | } |
| 266 | |
| 267 | out << "\n"; |
| 268 | enterLeaveNamespace(out, false /* enter */); |
| 269 | |
| 270 | out << "\n#endif // " << guard << "\n"; |
| 271 | |
| 272 | return OK; |
| 273 | } |
| 274 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 275 | status_t AST::generateHwBinderHeader(const std::string &outputPath) const { |
| 276 | std::string ifaceName; |
| 277 | if(!AST::isInterface(&ifaceName)) { |
| 278 | // types.hal does not get an HwBinder header. |
| 279 | return OK; |
| 280 | } |
| 281 | |
| 282 | // cut off the leading 'I'. |
| 283 | const std::string baseName = ifaceName.substr(1); |
| 284 | |
| 285 | const std::string klassName = "IHw" + baseName; |
| 286 | |
| 287 | std::string path = outputPath; |
| 288 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 289 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 290 | path.append(klassName + ".h"); |
| 291 | |
| 292 | FILE* file = fopen(path.c_str(), "w"); |
| 293 | |
| 294 | if (file == NULL) { |
| 295 | return -errno; |
| 296 | } |
| 297 | |
| 298 | Formatter out(file); |
| 299 | |
| 300 | const std::string guard = makeHeaderGuard(klassName); |
| 301 | |
| 302 | out << "#ifndef " << guard << "\n"; |
| 303 | out << "#define " << guard << "\n\n"; |
| 304 | |
| 305 | std::vector<std::string> packageComponents; |
| 306 | getPackageAndVersionComponents( |
| 307 | &packageComponents, false /* cpp_compatible */); |
| 308 | |
| 309 | out << "#include <"; |
| 310 | for (const auto &component : packageComponents) { |
| 311 | out << component << "/"; |
| 312 | } |
| 313 | out << ifaceName << ".h>\n\n"; |
| 314 | |
| 315 | for (const auto &item : mImportedNames) { |
| 316 | if (item.name() == "types") { |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | out << "#include <"; |
| 321 | |
| 322 | std::vector<std::string> components; |
| 323 | item.getPackageAndVersionComponents( |
| 324 | &components, false /* cpp_compatible */); |
| 325 | |
| 326 | for (const auto &component : components) { |
| 327 | out << component << "/"; |
| 328 | } |
| 329 | |
| 330 | // cut off the leading I |
| 331 | const std::string itemBaseName = item.name().substr(1); |
| 332 | |
| 333 | out << "Bn" |
| 334 | << itemBaseName |
| 335 | << ".h>\n"; |
| 336 | } |
| 337 | |
| 338 | out << "\n"; |
| 339 | |
| 340 | out << "#include <hidl/HidlSupport.h>\n"; |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 341 | out << "#include <hidl/Status.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 342 | out << "#include <hwbinder/IBinder.h>\n"; |
| 343 | out << "#include <hwbinder/IInterface.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 344 | |
| 345 | out << "\n"; |
| 346 | |
| 347 | enterLeaveNamespace(out, true /* enter */); |
| 348 | out << "\n"; |
| 349 | |
| 350 | out << "struct " |
| 351 | << klassName |
| 352 | << " : public " |
| 353 | << ifaceName; |
| 354 | |
| 355 | const Interface *iface = mRootScope->getInterface(); |
| 356 | const Interface *superType = iface->superType(); |
| 357 | |
| 358 | out << ", public ::android::hardware::IInterface"; |
| 359 | |
| 360 | out << " {\n"; |
| 361 | |
| 362 | out.indent(); |
| 363 | |
| 364 | out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n"; |
| 365 | |
| 366 | out << "enum Call {\n"; |
| 367 | out.indent(); |
| 368 | |
| 369 | bool first = true; |
| 370 | for (const auto &method : iface->methods()) { |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 371 | out << StringHelper::Upcase(method->name()); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 372 | |
| 373 | if (first) { |
| 374 | out << " = "; |
| 375 | if (superType != NULL) { |
| 376 | out << superType->fqName().cppNamespace() |
| 377 | << "::IHw" |
| 378 | << superType->getBaseName() |
| 379 | << "::Call::CallCount"; |
| 380 | } else { |
| 381 | out << "::android::hardware::IBinder::FIRST_CALL_TRANSACTION"; |
| 382 | } |
| 383 | |
| 384 | first = false; |
| 385 | } |
| 386 | |
| 387 | out << ",\n"; |
| 388 | } |
| 389 | |
| 390 | out << "CallCount\n"; |
| 391 | |
| 392 | out.unindent(); |
| 393 | out << "};\n\n"; |
| 394 | |
| 395 | out.unindent(); |
| 396 | |
| 397 | out << "};\n\n"; |
| 398 | |
| 399 | enterLeaveNamespace(out, false /* enter */); |
| 400 | |
| 401 | out << "\n#endif // " << guard << "\n"; |
| 402 | |
| 403 | return OK; |
| 404 | } |
| 405 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 406 | status_t AST::emitTypeDeclarations(Formatter &out) const { |
| 407 | return mRootScope->emitTypeDeclarations(out); |
| 408 | } |
| 409 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 410 | status_t AST::generateStubMethod(Formatter &out, |
| 411 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 412 | const Method *method, |
| 413 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 414 | out << "inline "; |
| 415 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 416 | method->generateCppSignature(out, |
| 417 | className, |
| 418 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 419 | |
| 420 | const bool returnsValue = !method->results().empty(); |
| 421 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 422 | out << " {\n"; |
| 423 | out.indent(); |
| 424 | out << "return mImpl->" |
| 425 | << method->name() |
| 426 | << "("; |
| 427 | bool first = true; |
| 428 | for (const auto &arg : method->args()) { |
| 429 | if (!first) { |
| 430 | out << ", "; |
| 431 | } |
| 432 | first = false; |
| 433 | out << arg->name(); |
| 434 | } |
| 435 | if (returnsValue && elidedReturn == nullptr) { |
| 436 | if (!method->args().empty()) { |
| 437 | out << ", "; |
| 438 | } |
| 439 | |
| 440 | out << "_hidl_cb"; |
| 441 | } |
| 442 | out << ");\n"; |
| 443 | out.unindent(); |
| 444 | out << "}"; |
| 445 | |
| 446 | out << ";\n"; |
| 447 | |
| 448 | return OK; |
| 449 | } |
| 450 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 451 | status_t AST::generateProxyDeclaration(Formatter &out, |
| 452 | const std::string &className, |
| 453 | const Method *method, |
| 454 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 455 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 456 | method->generateCppSignature(out, |
| 457 | className, |
| 458 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 459 | out << " override;\n"; |
| 460 | |
| 461 | return OK; |
| 462 | } |
| 463 | |
| 464 | status_t AST::generateMethods( |
| 465 | Formatter &out, |
| 466 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 467 | MethodLocation type, |
| 468 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 469 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 470 | const Interface *iface = mRootScope->getInterface(); |
| 471 | |
| 472 | std::vector<const Interface *> chain; |
| 473 | while (iface != NULL) { |
| 474 | chain.push_back(iface); |
| 475 | iface = iface->superType(); |
| 476 | } |
| 477 | |
| 478 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 479 | const Interface *superInterface = *it; |
| 480 | |
| 481 | out << "// Methods from " |
| 482 | << superInterface->fullName() |
| 483 | << " follow.\n"; |
| 484 | |
| 485 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 486 | status_t err; |
| 487 | switch(type) { |
| 488 | case STUB_HEADER: |
| 489 | err = generateStubMethod(out, |
| 490 | className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 491 | method, |
| 492 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 493 | break; |
| 494 | case PROXY_HEADER: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 495 | err = generateProxyDeclaration(out, |
| 496 | className, |
| 497 | method, |
| 498 | specifyNamespaces); |
| 499 | break; |
| 500 | case IMPL_HEADER: |
| 501 | err = generateStubImplDeclaration(out, |
| 502 | className, |
| 503 | method, |
| 504 | specifyNamespaces); |
| 505 | break; |
| 506 | case IMPL_SOURCE: |
| 507 | err = generateStubImplMethod(out, |
| 508 | className, |
| 509 | method, |
| 510 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 511 | break; |
| 512 | default: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 513 | LOG(ERROR) << "Unkown method type: " << type; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 514 | err = UNKNOWN_ERROR; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 517 | if (err != OK) { |
| 518 | return err; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 519 | } |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | out << "\n"; |
| 523 | } |
| 524 | |
| 525 | return OK; |
| 526 | } |
| 527 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 528 | status_t AST::generateStubHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 529 | std::string ifaceName; |
| 530 | if (!AST::isInterface(&ifaceName)) { |
| 531 | // types.hal does not get a stub header. |
| 532 | return OK; |
| 533 | } |
| 534 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 535 | // cut off the leading 'I'. |
| 536 | const std::string baseName = ifaceName.substr(1); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 537 | const std::string klassName = "Bn" + baseName; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 538 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 539 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 540 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 541 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 542 | path.append(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 543 | path.append(".h"); |
| 544 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 545 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 546 | FILE *file = fopen(path.c_str(), "w"); |
| 547 | |
| 548 | if (file == NULL) { |
| 549 | return -errno; |
| 550 | } |
| 551 | |
| 552 | Formatter out(file); |
| 553 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 554 | const std::string guard = makeHeaderGuard(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 555 | |
| 556 | out << "#ifndef " << guard << "\n"; |
| 557 | out << "#define " << guard << "\n\n"; |
| 558 | |
| 559 | std::vector<std::string> packageComponents; |
| 560 | getPackageAndVersionComponents( |
| 561 | &packageComponents, false /* cpp_compatible */); |
| 562 | |
| 563 | out << "#include <"; |
| 564 | for (const auto &component : packageComponents) { |
| 565 | out << component << "/"; |
| 566 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 567 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 568 | |
| 569 | enterLeaveNamespace(out, true /* enter */); |
| 570 | out << "\n"; |
| 571 | |
| 572 | out << "struct " |
| 573 | << "Bn" |
| 574 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 575 | << " : public ::android::hardware::BnInterface<I" |
| 576 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 577 | << "> {\n"; |
| 578 | |
| 579 | out.indent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 580 | out << "explicit Bn" |
| 581 | << baseName |
| 582 | << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);" |
| 583 | << "\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 584 | out << "::android::status_t onTransact(\n"; |
| 585 | out.indent(); |
| 586 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 587 | out << "uint32_t _hidl_code,\n"; |
| 588 | out << "const ::android::hardware::Parcel &_hidl_data,\n"; |
| 589 | out << "::android::hardware::Parcel *_hidl_reply,\n"; |
| 590 | out << "uint32_t _hidl_flags = 0,\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 591 | out << "TransactCallback _hidl_cb = nullptr) override;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 592 | out.unindent(); |
| 593 | out.unindent(); |
| 594 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 595 | status_t err = generateMethods(out, |
| 596 | "" /* class name */, |
| 597 | MethodLocation::STUB_HEADER, |
| 598 | true /* specify namespaces */); |
| 599 | |
| 600 | if (err != OK) { |
| 601 | return err; |
| 602 | } |
| 603 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 604 | out.unindent(); |
| 605 | |
| 606 | out << "};\n\n"; |
| 607 | |
| 608 | enterLeaveNamespace(out, false /* enter */); |
| 609 | |
| 610 | out << "\n#endif // " << guard << "\n"; |
| 611 | |
| 612 | return OK; |
| 613 | } |
| 614 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 615 | status_t AST::generateProxyHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 616 | std::string ifaceName; |
| 617 | if (!AST::isInterface(&ifaceName)) { |
| 618 | // types.hal does not get a proxy header. |
| 619 | return OK; |
| 620 | } |
| 621 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 622 | // cut off the leading 'I'. |
| 623 | const std::string baseName = ifaceName.substr(1); |
| 624 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 625 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 626 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 627 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 628 | path.append("Bp"); |
| 629 | path.append(baseName); |
| 630 | path.append(".h"); |
| 631 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 632 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 633 | FILE *file = fopen(path.c_str(), "w"); |
| 634 | |
| 635 | if (file == NULL) { |
| 636 | return -errno; |
| 637 | } |
| 638 | |
| 639 | Formatter out(file); |
| 640 | |
| 641 | const std::string guard = makeHeaderGuard("Bp" + baseName); |
| 642 | |
| 643 | out << "#ifndef " << guard << "\n"; |
| 644 | out << "#define " << guard << "\n\n"; |
| 645 | |
| 646 | std::vector<std::string> packageComponents; |
| 647 | getPackageAndVersionComponents( |
| 648 | &packageComponents, false /* cpp_compatible */); |
| 649 | |
| 650 | out << "#include <"; |
| 651 | for (const auto &component : packageComponents) { |
| 652 | out << component << "/"; |
| 653 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 654 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 655 | |
| 656 | enterLeaveNamespace(out, true /* enter */); |
| 657 | out << "\n"; |
| 658 | |
| 659 | out << "struct " |
| 660 | << "Bp" |
| 661 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 662 | << " : public ::android::hardware::BpInterface<IHw" |
| 663 | << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 664 | << "> {\n"; |
| 665 | |
| 666 | out.indent(); |
| 667 | |
| 668 | out << "explicit Bp" |
| 669 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 670 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 671 | << "\n\n"; |
| 672 | |
Iliyan Malchev | 795dd43 | 2016-09-02 11:48:26 -0700 | [diff] [blame] | 673 | out << "virtual bool isRemote() const { return true; }\n\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 674 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 675 | status_t err = generateMethods(out, |
| 676 | "" /* class name */, |
| 677 | MethodLocation::PROXY_HEADER, |
| 678 | true /* generate specify namespaces */); |
| 679 | |
| 680 | if (err != OK) { |
| 681 | return err; |
| 682 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 683 | |
| 684 | out.unindent(); |
| 685 | |
| 686 | out << "};\n\n"; |
| 687 | |
| 688 | enterLeaveNamespace(out, false /* enter */); |
| 689 | |
| 690 | out << "\n#endif // " << guard << "\n"; |
| 691 | |
| 692 | return OK; |
| 693 | } |
| 694 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 695 | status_t AST::generateAllSource(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 696 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 697 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 698 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 699 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 700 | |
| 701 | std::string ifaceName; |
| 702 | std::string baseName; |
| 703 | |
| 704 | bool isInterface = true; |
| 705 | if (!AST::isInterface(&ifaceName)) { |
| 706 | baseName = "types"; |
| 707 | isInterface = false; |
| 708 | } else { |
| 709 | baseName = ifaceName.substr(1); // cut off the leading 'I'. |
| 710 | } |
| 711 | |
| 712 | path.append(baseName); |
| 713 | |
| 714 | if (baseName != "types") { |
| 715 | path.append("All"); |
| 716 | } |
| 717 | |
| 718 | path.append(".cpp"); |
| 719 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 720 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 721 | FILE *file = fopen(path.c_str(), "w"); |
| 722 | |
| 723 | if (file == NULL) { |
| 724 | return -errno; |
| 725 | } |
| 726 | |
| 727 | Formatter out(file); |
| 728 | |
| 729 | std::vector<std::string> packageComponents; |
| 730 | getPackageAndVersionComponents( |
| 731 | &packageComponents, false /* cpp_compatible */); |
| 732 | |
| 733 | std::string prefix; |
| 734 | for (const auto &component : packageComponents) { |
| 735 | prefix += component; |
| 736 | prefix += "/"; |
| 737 | } |
| 738 | |
| 739 | if (isInterface) { |
| 740 | out << "#include <" << prefix << "/Bp" << baseName << ".h>\n"; |
| 741 | out << "#include <" << prefix << "/Bn" << baseName << ".h>\n"; |
| 742 | } else { |
| 743 | out << "#include <" << prefix << "types.h>\n"; |
| 744 | } |
| 745 | |
| 746 | out << "\n"; |
| 747 | |
| 748 | enterLeaveNamespace(out, true /* enter */); |
| 749 | out << "\n"; |
| 750 | |
| 751 | status_t err = generateTypeSource(out, ifaceName); |
| 752 | |
| 753 | if (err == OK && isInterface) { |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 754 | out << "constexpr hidl_version " << ifaceName << "::version;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 755 | err = generateProxySource(out, baseName); |
| 756 | } |
| 757 | |
| 758 | if (err == OK && isInterface) { |
| 759 | err = generateStubSource(out, baseName); |
| 760 | } |
| 761 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 762 | if (err == OK && isInterface) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 763 | const Interface *iface = mRootScope->getInterface(); |
| 764 | |
| 765 | out << "IMPLEMENT_REGISTER_AND_GET_SERVICE(" |
| 766 | << baseName << ", " |
| 767 | << "\"" << iface->fqName().package() |
Iliyan Malchev | 4923f93 | 2016-09-09 13:04:59 -0700 | [diff] [blame] | 768 | << iface->fqName().version() << "-impl.so\"" |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 769 | << ")\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 772 | enterLeaveNamespace(out, false /* enter */); |
| 773 | |
| 774 | return err; |
| 775 | } |
| 776 | |
| 777 | status_t AST::generateTypeSource( |
| 778 | Formatter &out, const std::string &ifaceName) const { |
| 779 | return mRootScope->emitTypeDefinitions(out, ifaceName); |
| 780 | } |
| 781 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 782 | void AST::declareCppReaderLocals( |
| 783 | Formatter &out, const std::vector<TypedVar *> &args) const { |
| 784 | if (args.empty()) { |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | for (const auto &arg : args) { |
| 789 | const Type &type = arg->type(); |
| 790 | |
| 791 | std::string extra; |
| 792 | out << type.getCppResultType(&extra) |
| 793 | << " " |
| 794 | << arg->name() |
| 795 | << extra |
| 796 | << ";\n"; |
| 797 | } |
| 798 | |
| 799 | out << "\n"; |
| 800 | } |
| 801 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 802 | void AST::emitCppReaderWriter( |
| 803 | Formatter &out, |
| 804 | const std::string &parcelObj, |
| 805 | bool parcelObjIsPointer, |
| 806 | const TypedVar *arg, |
| 807 | bool isReader, |
| 808 | Type::ErrorMode mode) const { |
| 809 | const Type &type = arg->type(); |
| 810 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 811 | type.emitReaderWriter( |
| 812 | out, |
| 813 | arg->name(), |
| 814 | parcelObj, |
| 815 | parcelObjIsPointer, |
| 816 | isReader, |
| 817 | mode); |
| 818 | } |
| 819 | |
| 820 | status_t AST::generateProxySource( |
| 821 | Formatter &out, const std::string &baseName) const { |
| 822 | const std::string klassName = "Bp" + baseName; |
| 823 | |
| 824 | out << klassName |
| 825 | << "::" |
| 826 | << klassName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 827 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 828 | |
| 829 | out.indent(); |
| 830 | out.indent(); |
| 831 | |
| 832 | out << ": BpInterface" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 833 | << "<IHw" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 834 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 835 | << ">(_hidl_impl) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 836 | |
| 837 | out.unindent(); |
| 838 | out.unindent(); |
| 839 | out << "}\n\n"; |
| 840 | |
| 841 | const Interface *iface = mRootScope->getInterface(); |
| 842 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 843 | std::vector<const Interface *> chain; |
| 844 | while (iface != NULL) { |
| 845 | chain.push_back(iface); |
| 846 | iface = iface->superType(); |
| 847 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 848 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 849 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 850 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 851 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 852 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 853 | method->generateCppSignature(out, |
| 854 | klassName, |
| 855 | true /* specify namespaces */); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 856 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 857 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 858 | const TypedVar *elidedReturn = method->canElideCallback(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 859 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 860 | out << "{\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 861 | |
| 862 | out.indent(); |
| 863 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 864 | out << "::android::hardware::Parcel _hidl_data;\n"; |
| 865 | out << "::android::hardware::Parcel _hidl_reply;\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 866 | out << "::android::status_t _hidl_err;\n"; |
| 867 | out << "::android::hardware::Status _hidl_status;\n\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 868 | declareCppReaderLocals(out, method->results()); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 869 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 870 | out << "_hidl_err = _hidl_data.writeInterfaceToken(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 871 | << superInterface->fqName().cppNamespace() |
| 872 | << "::IHw" |
| 873 | << superInterface->getBaseName() |
| 874 | << "::descriptor);\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 875 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 876 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 877 | |
| 878 | for (const auto &arg : method->args()) { |
| 879 | emitCppReaderWriter( |
| 880 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 881 | "_hidl_data", |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 882 | false /* parcelObjIsPointer */, |
| 883 | arg, |
| 884 | false /* reader */, |
| 885 | Type::ErrorMode_Goto); |
| 886 | } |
| 887 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 888 | out << "_hidl_err = remote()->transact(" |
| 889 | << superInterface->fqName().cppNamespace() |
| 890 | << "::IHw" |
| 891 | << superInterface->getBaseName() |
| 892 | << "::Call::" |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 893 | << StringHelper::Upcase(method->name()) |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 894 | << ", _hidl_data, &_hidl_reply"; |
| 895 | if (method->isOneway()) { |
| 896 | out << ", ::android::hardware::IBinder::FLAG_ONEWAY"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 897 | } |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 898 | out << ");\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 899 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 900 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 901 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 902 | if (!method->isOneway()) { |
| 903 | out << "_hidl_err = _hidl_status.readFromParcel(_hidl_reply);\n"; |
| 904 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 905 | out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n"; |
| 906 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 907 | for (const auto &arg : method->results()) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 908 | emitCppReaderWriter( |
| 909 | out, |
| 910 | "_hidl_reply", |
| 911 | false /* parcelObjIsPointer */, |
| 912 | arg, |
| 913 | true /* reader */, |
| 914 | Type::ErrorMode_Goto); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 917 | if (returnsValue && elidedReturn == nullptr) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 918 | out << "if (_hidl_cb != nullptr) {\n"; |
| 919 | out.indent(); |
| 920 | out << "_hidl_cb("; |
| 921 | |
| 922 | bool first = true; |
| 923 | for (const auto &arg : method->results()) { |
| 924 | if (!first) { |
| 925 | out << ", "; |
| 926 | } |
| 927 | |
| 928 | if (arg->type().resultNeedsDeref()) { |
| 929 | out << "*"; |
| 930 | } |
| 931 | out << arg->name(); |
| 932 | |
| 933 | first = false; |
| 934 | } |
| 935 | |
| 936 | out << ");\n"; |
| 937 | out.unindent(); |
| 938 | out << "}\n\n"; |
| 939 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 942 | if (elidedReturn != nullptr) { |
| 943 | std::string extra; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 944 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 945 | out << "return ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 946 | out << elidedReturn->type().getCppResultType(&extra) |
| 947 | << ">(" << elidedReturn->name() << ");\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 948 | } else { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 949 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 950 | out << "return ::android::hardware::Return<void>();\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 951 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 952 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 953 | out.unindent(); |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 954 | out << "_hidl_error:\n"; |
| 955 | out.indent(); |
| 956 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 957 | out << "return ::android::hardware::Return<"; |
| 958 | if (elidedReturn != nullptr) { |
| 959 | std::string extra; |
| 960 | out << method->results().at(0)->type().getCppResultType(&extra); |
| 961 | } else { |
| 962 | out << "void"; |
| 963 | } |
| 964 | out << ">(_hidl_status);\n"; |
| 965 | |
| 966 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 967 | out << "}\n\n"; |
| 968 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | return OK; |
| 972 | } |
| 973 | |
| 974 | status_t AST::generateStubSource( |
| 975 | Formatter &out, const std::string &baseName) const { |
| 976 | out << "IMPLEMENT_HWBINDER_META_INTERFACE(" |
| 977 | << baseName |
| 978 | << ", \"" |
| 979 | << mPackage.string() |
| 980 | << "::I" |
| 981 | << baseName |
| 982 | << "\");\n\n"; |
| 983 | |
| 984 | const std::string klassName = "Bn" + baseName; |
| 985 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 986 | out << klassName |
| 987 | << "::" |
| 988 | << klassName |
| 989 | << "(const ::android::sp<I" << baseName <<"> &_hidl_impl)\n"; |
| 990 | |
| 991 | out.indent(); |
| 992 | out.indent(); |
| 993 | |
| 994 | out << ": BnInterface" |
| 995 | << "<I" |
| 996 | << baseName |
| 997 | << ", IHw" |
| 998 | << baseName |
| 999 | << ">(_hidl_impl) {\n"; |
| 1000 | |
| 1001 | out.unindent(); |
| 1002 | out.unindent(); |
| 1003 | out << "}\n\n"; |
| 1004 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1005 | out << "::android::status_t " << klassName << "::onTransact(\n"; |
| 1006 | |
| 1007 | out.indent(); |
| 1008 | out.indent(); |
| 1009 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1010 | out << "uint32_t _hidl_code,\n" |
| 1011 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 1012 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 1013 | << "uint32_t _hidl_flags,\n" |
| 1014 | << "TransactCallback _hidl_cb) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1015 | |
| 1016 | out.unindent(); |
| 1017 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1018 | out << "::android::status_t _hidl_err = ::android::OK;\n\n"; |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1019 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1020 | out.indent(); |
| 1021 | |
| 1022 | const Interface *iface = mRootScope->getInterface(); |
| 1023 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1024 | std::vector<const Interface *> chain; |
| 1025 | while (iface != NULL) { |
| 1026 | chain.push_back(iface); |
| 1027 | iface = iface->superType(); |
| 1028 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1029 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1030 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 1031 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1032 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1033 | for (const auto &method : superInterface->methods()) { |
| 1034 | out << "case " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1035 | << superInterface->fqName().cppNamespace() |
| 1036 | << "::IHw" |
| 1037 | << superInterface->getBaseName() |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1038 | << "::Call::" |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 1039 | << StringHelper::Upcase(method->name()) |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1040 | << ":\n{\n"; |
| 1041 | |
| 1042 | out.indent(); |
| 1043 | |
| 1044 | status_t err = |
| 1045 | generateStubSourceForMethod(out, superInterface, method); |
| 1046 | |
| 1047 | if (err != OK) { |
| 1048 | return err; |
| 1049 | } |
| 1050 | |
| 1051 | out.unindent(); |
| 1052 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1053 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | out << "default:\n{\n"; |
| 1057 | out.indent(); |
| 1058 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1059 | out << "return ::android::hardware::BnInterface<I" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1060 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1061 | << ">::onTransact(\n"; |
| 1062 | |
| 1063 | out.indent(); |
| 1064 | out.indent(); |
| 1065 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1066 | out << "_hidl_code, _hidl_data, _hidl_reply, " |
| 1067 | << "_hidl_flags, _hidl_cb);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1068 | |
| 1069 | out.unindent(); |
| 1070 | out.unindent(); |
| 1071 | |
| 1072 | out.unindent(); |
| 1073 | out << "}\n"; |
| 1074 | |
| 1075 | out.unindent(); |
| 1076 | out << "}\n\n"; |
| 1077 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1078 | out << "if (_hidl_err == ::android::UNEXPECTED_NULL) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1079 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1080 | out << "_hidl_err = ::android::hardware::Status::fromExceptionCode(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1081 | out.indent(); |
| 1082 | out.indent(); |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1083 | out << "::android::hardware::Status::EX_NULL_POINTER)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1084 | out.indent(); |
| 1085 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1086 | out << ".writeToParcel(_hidl_reply);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1087 | out.unindent(); |
| 1088 | out.unindent(); |
| 1089 | out.unindent(); |
| 1090 | out.unindent(); |
| 1091 | |
| 1092 | out.unindent(); |
| 1093 | out << "}\n\n"; |
| 1094 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1095 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1096 | |
| 1097 | out.unindent(); |
| 1098 | out << "}\n\n"; |
| 1099 | |
| 1100 | return OK; |
| 1101 | } |
| 1102 | |
| 1103 | status_t AST::generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1104 | Formatter &out, const Interface *iface, const Method *method) const { |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1105 | out << "if (!_hidl_data.enforceInterface(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1106 | << iface->fqName().cppNamespace() |
| 1107 | << "::IHw" |
| 1108 | << iface->getBaseName() |
| 1109 | << "::descriptor)) {\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1110 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1111 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1112 | out << "_hidl_err = ::android::BAD_TYPE;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1113 | out << "break;\n"; |
| 1114 | out.unindent(); |
| 1115 | out << "}\n\n"; |
| 1116 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1117 | declareCppReaderLocals(out, method->args()); |
| 1118 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1119 | for (const auto &arg : method->args()) { |
| 1120 | emitCppReaderWriter( |
| 1121 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1122 | "_hidl_data", |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1123 | false /* parcelObjIsPointer */, |
| 1124 | arg, |
| 1125 | true /* reader */, |
| 1126 | Type::ErrorMode_Break); |
| 1127 | } |
| 1128 | |
| 1129 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1130 | const TypedVar *elidedReturn = method->canElideCallback(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1131 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1132 | if (elidedReturn != nullptr) { |
| 1133 | std::string extra; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1134 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1135 | out << elidedReturn->type().getCppResultType(&extra) << " "; |
| 1136 | out << elidedReturn->name() << " = "; |
| 1137 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1138 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1139 | bool first = true; |
| 1140 | for (const auto &arg : method->args()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1141 | if (!first) { |
| 1142 | out << ", "; |
| 1143 | } |
| 1144 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1145 | if (arg->type().resultNeedsDeref()) { |
| 1146 | out << "*"; |
| 1147 | } |
| 1148 | |
| 1149 | out << arg->name(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1150 | |
| 1151 | first = false; |
| 1152 | } |
| 1153 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1154 | out << ");\n\n"; |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1155 | out << "::android::hardware::Status::ok()" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1156 | << ".writeToParcel(_hidl_reply);\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1157 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1158 | elidedReturn->type().emitReaderWriter( |
| 1159 | out, |
| 1160 | elidedReturn->name(), |
| 1161 | "_hidl_reply", |
| 1162 | true, /* parcelObjIsPointer */ |
| 1163 | false, /* isReader */ |
| 1164 | Type::ErrorMode_Ignore); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1165 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1166 | out << "_hidl_cb(*_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1167 | } else { |
| 1168 | if (returnsValue) { |
| 1169 | out << "bool _hidl_callbackCalled = false;\n\n"; |
| 1170 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1171 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1172 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1173 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1174 | bool first = true; |
| 1175 | for (const auto &arg : method->args()) { |
| 1176 | if (!first) { |
| 1177 | out << ", "; |
| 1178 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1179 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1180 | if (arg->type().resultNeedsDeref()) { |
| 1181 | out << "*"; |
| 1182 | } |
| 1183 | |
| 1184 | out << arg->name(); |
| 1185 | |
| 1186 | first = false; |
| 1187 | } |
| 1188 | |
| 1189 | if (returnsValue) { |
| 1190 | if (!first) { |
| 1191 | out << ", "; |
| 1192 | } |
| 1193 | |
| 1194 | out << "[&]("; |
| 1195 | |
| 1196 | first = true; |
| 1197 | for (const auto &arg : method->results()) { |
| 1198 | if (!first) { |
| 1199 | out << ", "; |
| 1200 | } |
| 1201 | |
| 1202 | out << "const auto &" << arg->name(); |
| 1203 | |
| 1204 | first = false; |
| 1205 | } |
| 1206 | |
| 1207 | out << ") {\n"; |
| 1208 | out.indent(); |
| 1209 | out << "_hidl_callbackCalled = true;\n\n"; |
| 1210 | |
| 1211 | out << "::android::hardware::Status::ok()" |
| 1212 | << ".writeToParcel(_hidl_reply);\n\n"; |
| 1213 | |
| 1214 | for (const auto &arg : method->results()) { |
| 1215 | emitCppReaderWriter( |
| 1216 | out, |
| 1217 | "_hidl_reply", |
| 1218 | true /* parcelObjIsPointer */, |
| 1219 | arg, |
| 1220 | false /* reader */, |
| 1221 | Type::ErrorMode_Ignore); |
| 1222 | } |
| 1223 | |
| 1224 | out << "_hidl_cb(*_hidl_reply);\n"; |
| 1225 | |
| 1226 | out.unindent(); |
| 1227 | out << "}\n"; |
| 1228 | } |
| 1229 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1230 | out << ");\n\n"; |
| 1231 | |
| 1232 | // What to do if the stub implementation has a synchronous callback |
| 1233 | // which does not get invoked? This is not a transport error but a |
| 1234 | // service error of sorts. For now, return OK to the caller, as this is |
| 1235 | // not a transport error. |
| 1236 | // |
| 1237 | // TODO(b/31365311) Figure out how to deal with this later. |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1238 | |
| 1239 | if (returnsValue) { |
| 1240 | out << "if (!_hidl_callbackCalled) {\n"; |
| 1241 | out.indent(); |
| 1242 | } |
| 1243 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1244 | out << "::android::hardware::Status::ok()" |
| 1245 | << ".writeToParcel(_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1246 | |
| 1247 | if (returnsValue) { |
| 1248 | out.unindent(); |
| 1249 | out << "}\n\n"; |
| 1250 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | out << "break;\n"; |
| 1254 | |
| 1255 | return OK; |
| 1256 | } |
| 1257 | |
| 1258 | } // namespace android |
| 1259 | |