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 "Formatter.h" |
| 22 | #include "Interface.h" |
| 23 | #include "Method.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 24 | #include "ScalarType.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 25 | #include "Scope.h" |
| 26 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 27 | #include <algorithm> |
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 | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 34 | static std::string upcase(const std::string in) { |
| 35 | std::string out{in}; |
| 36 | |
| 37 | for (auto &ch : out) { |
| 38 | ch = toupper(ch); |
| 39 | } |
| 40 | |
| 41 | return out; |
| 42 | } |
| 43 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 44 | status_t AST::generateCpp(const std::string &outputPath) const { |
| 45 | status_t err = generateInterfaceHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 46 | |
| 47 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 48 | err = generateStubHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (err == OK) { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 52 | err = generateHwBinderHeader(outputPath); |
| 53 | } |
| 54 | |
| 55 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 56 | err = generateProxyHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 60 | err = generateAllSource(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return err; |
| 64 | } |
| 65 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 66 | void AST::getPackageComponents( |
| 67 | std::vector<std::string> *components) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 68 | mPackage.getPackageComponents(components); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void AST::getPackageAndVersionComponents( |
| 72 | std::vector<std::string> *components, bool cpp_compatible) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 73 | mPackage.getPackageAndVersionComponents(components, cpp_compatible); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 76 | std::string AST::makeHeaderGuard(const std::string &baseName) const { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame^] | 77 | std::string guard = "HIDL_GENERATED_"; |
| 78 | guard += mPackage.tokenName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 79 | |
| 80 | guard += "_"; |
| 81 | guard += baseName; |
| 82 | guard += "_H_"; |
| 83 | |
| 84 | return guard; |
| 85 | } |
| 86 | |
| 87 | void AST::enterLeaveNamespace(Formatter &out, bool enter) const { |
| 88 | std::vector<std::string> packageComponents; |
| 89 | getPackageAndVersionComponents( |
| 90 | &packageComponents, true /* cpp_compatible */); |
| 91 | |
| 92 | if (enter) { |
| 93 | for (const auto &component : packageComponents) { |
| 94 | out << "namespace " << component << " {\n"; |
| 95 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 96 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 97 | out.setNamespace(mPackage.cppNamespace() + "::"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 98 | } else { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 99 | out.setNamespace(std::string()); |
| 100 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 101 | for (auto it = packageComponents.rbegin(); |
| 102 | it != packageComponents.rend(); |
| 103 | ++it) { |
| 104 | out << "} // namespace " << *it << "\n"; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 109 | status_t AST::generateInterfaceHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 110 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 111 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 112 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 113 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 114 | |
| 115 | std::string ifaceName; |
| 116 | bool isInterface = true; |
| 117 | if (!AST::isInterface(&ifaceName)) { |
| 118 | ifaceName = "types"; |
| 119 | isInterface = false; |
| 120 | } |
| 121 | path.append(ifaceName); |
| 122 | path.append(".h"); |
| 123 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 124 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 125 | FILE *file = fopen(path.c_str(), "w"); |
| 126 | |
| 127 | if (file == NULL) { |
| 128 | return -errno; |
| 129 | } |
| 130 | |
| 131 | Formatter out(file); |
| 132 | |
| 133 | const std::string guard = makeHeaderGuard(ifaceName); |
| 134 | |
| 135 | out << "#ifndef " << guard << "\n"; |
| 136 | out << "#define " << guard << "\n\n"; |
| 137 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 138 | for (const auto &item : mImportedNames) { |
| 139 | out << "#include <"; |
| 140 | |
| 141 | std::vector<std::string> components; |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 142 | item.getPackageAndVersionComponents( |
| 143 | &components, false /* cpp_compatible */); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 144 | |
| 145 | for (const auto &component : components) { |
| 146 | out << component << "/"; |
| 147 | } |
| 148 | |
| 149 | out << item.name() |
| 150 | << ".h>\n"; |
| 151 | } |
| 152 | |
| 153 | if (!mImportedNames.empty()) { |
| 154 | out << "\n"; |
| 155 | } |
| 156 | |
Martijn Coenen | 7473fab | 2016-08-19 14:05:40 +0200 | [diff] [blame] | 157 | out << "#include <hidl/HidlSupport.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 158 | out << "#include <hidl/IServiceManager.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 159 | |
| 160 | if (isInterface) { |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 161 | out << "#include <hidl/Status.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | out << "#include <utils/NativeHandle.h>\n\n"; |
| 165 | |
| 166 | enterLeaveNamespace(out, true /* enter */); |
| 167 | out << "\n"; |
| 168 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 169 | // cut off the leading 'I'. |
| 170 | const std::string baseName = ifaceName.substr(1); |
| 171 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 172 | if (isInterface) { |
| 173 | out << "struct " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 174 | << ifaceName; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 175 | |
| 176 | const Interface *iface = mRootScope->getInterface(); |
| 177 | const Interface *superType = iface->superType(); |
| 178 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 179 | if (superType == NULL) { |
| 180 | out << " : virtual public RefBase"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 181 | } else { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 182 | out << " : public " |
| 183 | << superType->fullName(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | out << " {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 187 | |
| 188 | out.indent(); |
| 189 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | status_t err = emitTypeDeclarations(out); |
| 193 | |
| 194 | if (err != OK) { |
| 195 | return err; |
| 196 | } |
| 197 | |
| 198 | if (isInterface) { |
| 199 | const Interface *iface = mRootScope->getInterface(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 200 | const Interface *superType = iface->superType(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 201 | |
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 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 253 | out << method->name() << "_cb _hidl_cb = nullptr"; |
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 | |
| 265 | out << "};\n"; |
| 266 | } |
| 267 | |
| 268 | out << "\n"; |
| 269 | enterLeaveNamespace(out, false /* enter */); |
| 270 | |
| 271 | out << "\n#endif // " << guard << "\n"; |
| 272 | |
| 273 | return OK; |
| 274 | } |
| 275 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 276 | status_t AST::generateHwBinderHeader(const std::string &outputPath) const { |
| 277 | std::string ifaceName; |
| 278 | if(!AST::isInterface(&ifaceName)) { |
| 279 | // types.hal does not get an HwBinder header. |
| 280 | return OK; |
| 281 | } |
| 282 | |
| 283 | // cut off the leading 'I'. |
| 284 | const std::string baseName = ifaceName.substr(1); |
| 285 | |
| 286 | const std::string klassName = "IHw" + baseName; |
| 287 | |
| 288 | std::string path = outputPath; |
| 289 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 290 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 291 | path.append(klassName + ".h"); |
| 292 | |
| 293 | FILE* file = fopen(path.c_str(), "w"); |
| 294 | |
| 295 | if (file == NULL) { |
| 296 | return -errno; |
| 297 | } |
| 298 | |
| 299 | Formatter out(file); |
| 300 | |
| 301 | const std::string guard = makeHeaderGuard(klassName); |
| 302 | |
| 303 | out << "#ifndef " << guard << "\n"; |
| 304 | out << "#define " << guard << "\n\n"; |
| 305 | |
| 306 | std::vector<std::string> packageComponents; |
| 307 | getPackageAndVersionComponents( |
| 308 | &packageComponents, false /* cpp_compatible */); |
| 309 | |
| 310 | out << "#include <"; |
| 311 | for (const auto &component : packageComponents) { |
| 312 | out << component << "/"; |
| 313 | } |
| 314 | out << ifaceName << ".h>\n\n"; |
| 315 | |
| 316 | for (const auto &item : mImportedNames) { |
| 317 | if (item.name() == "types") { |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | out << "#include <"; |
| 322 | |
| 323 | std::vector<std::string> components; |
| 324 | item.getPackageAndVersionComponents( |
| 325 | &components, false /* cpp_compatible */); |
| 326 | |
| 327 | for (const auto &component : components) { |
| 328 | out << component << "/"; |
| 329 | } |
| 330 | |
| 331 | // cut off the leading I |
| 332 | const std::string itemBaseName = item.name().substr(1); |
| 333 | |
| 334 | out << "Bn" |
| 335 | << itemBaseName |
| 336 | << ".h>\n"; |
| 337 | } |
| 338 | |
| 339 | out << "\n"; |
| 340 | |
| 341 | out << "#include <hidl/HidlSupport.h>\n"; |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 342 | out << "#include <hidl/Status.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 343 | out << "#include <hwbinder/IBinder.h>\n"; |
| 344 | out << "#include <hwbinder/IInterface.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 345 | |
| 346 | out << "\n"; |
| 347 | |
| 348 | enterLeaveNamespace(out, true /* enter */); |
| 349 | out << "\n"; |
| 350 | |
| 351 | out << "struct " |
| 352 | << klassName |
| 353 | << " : public " |
| 354 | << ifaceName; |
| 355 | |
| 356 | const Interface *iface = mRootScope->getInterface(); |
| 357 | const Interface *superType = iface->superType(); |
| 358 | |
| 359 | out << ", public ::android::hardware::IInterface"; |
| 360 | |
| 361 | out << " {\n"; |
| 362 | |
| 363 | out.indent(); |
| 364 | |
| 365 | out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n"; |
| 366 | |
| 367 | out << "enum Call {\n"; |
| 368 | out.indent(); |
| 369 | |
| 370 | bool first = true; |
| 371 | for (const auto &method : iface->methods()) { |
| 372 | out << upcase(method->name()); |
| 373 | |
| 374 | if (first) { |
| 375 | out << " = "; |
| 376 | if (superType != NULL) { |
| 377 | out << superType->fqName().cppNamespace() |
| 378 | << "::IHw" |
| 379 | << superType->getBaseName() |
| 380 | << "::Call::CallCount"; |
| 381 | } else { |
| 382 | out << "::android::hardware::IBinder::FIRST_CALL_TRANSACTION"; |
| 383 | } |
| 384 | |
| 385 | first = false; |
| 386 | } |
| 387 | |
| 388 | out << ",\n"; |
| 389 | } |
| 390 | |
| 391 | out << "CallCount\n"; |
| 392 | |
| 393 | out.unindent(); |
| 394 | out << "};\n\n"; |
| 395 | |
| 396 | out.unindent(); |
| 397 | |
| 398 | out << "};\n\n"; |
| 399 | |
| 400 | enterLeaveNamespace(out, false /* enter */); |
| 401 | |
| 402 | out << "\n#endif // " << guard << "\n"; |
| 403 | |
| 404 | return OK; |
| 405 | } |
| 406 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 407 | status_t AST::emitTypeDeclarations(Formatter &out) const { |
| 408 | return mRootScope->emitTypeDeclarations(out); |
| 409 | } |
| 410 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 411 | status_t AST::generateStubMethod(Formatter &out, |
| 412 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 413 | const Method *method, |
| 414 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 415 | out << "inline "; |
| 416 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 417 | method->generateCppSignature(out, |
| 418 | className, |
| 419 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 420 | |
| 421 | const bool returnsValue = !method->results().empty(); |
| 422 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 423 | out << " {\n"; |
| 424 | out.indent(); |
| 425 | out << "return mImpl->" |
| 426 | << method->name() |
| 427 | << "("; |
| 428 | bool first = true; |
| 429 | for (const auto &arg : method->args()) { |
| 430 | if (!first) { |
| 431 | out << ", "; |
| 432 | } |
| 433 | first = false; |
| 434 | out << arg->name(); |
| 435 | } |
| 436 | if (returnsValue && elidedReturn == nullptr) { |
| 437 | if (!method->args().empty()) { |
| 438 | out << ", "; |
| 439 | } |
| 440 | |
| 441 | out << "_hidl_cb"; |
| 442 | } |
| 443 | out << ");\n"; |
| 444 | out.unindent(); |
| 445 | out << "}"; |
| 446 | |
| 447 | out << ";\n"; |
| 448 | |
| 449 | return OK; |
| 450 | } |
| 451 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame^] | 452 | status_t AST::generateProxyDeclaration(Formatter &out, |
| 453 | const std::string &className, |
| 454 | const Method *method, |
| 455 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 456 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 457 | method->generateCppSignature(out, |
| 458 | className, |
| 459 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 460 | out << " override;\n"; |
| 461 | |
| 462 | return OK; |
| 463 | } |
| 464 | |
| 465 | status_t AST::generateMethods( |
| 466 | Formatter &out, |
| 467 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 468 | MethodLocation type, |
| 469 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 470 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 471 | const Interface *iface = mRootScope->getInterface(); |
| 472 | |
| 473 | std::vector<const Interface *> chain; |
| 474 | while (iface != NULL) { |
| 475 | chain.push_back(iface); |
| 476 | iface = iface->superType(); |
| 477 | } |
| 478 | |
| 479 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 480 | const Interface *superInterface = *it; |
| 481 | |
| 482 | out << "// Methods from " |
| 483 | << superInterface->fullName() |
| 484 | << " follow.\n"; |
| 485 | |
| 486 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 487 | status_t err; |
| 488 | switch(type) { |
| 489 | case STUB_HEADER: |
| 490 | err = generateStubMethod(out, |
| 491 | className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 492 | method, |
| 493 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 494 | break; |
| 495 | case PROXY_HEADER: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame^] | 496 | err = generateProxyDeclaration(out, |
| 497 | className, |
| 498 | method, |
| 499 | specifyNamespaces); |
| 500 | break; |
| 501 | case IMPL_HEADER: |
| 502 | err = generateStubImplDeclaration(out, |
| 503 | className, |
| 504 | method, |
| 505 | specifyNamespaces); |
| 506 | break; |
| 507 | case IMPL_SOURCE: |
| 508 | err = generateStubImplMethod(out, |
| 509 | className, |
| 510 | method, |
| 511 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 512 | break; |
| 513 | default: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame^] | 514 | LOG(ERROR) << "Unkown method type: " << type; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 515 | err = UNKNOWN_ERROR; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 518 | if (err != OK) { |
| 519 | return err; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 520 | } |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | out << "\n"; |
| 524 | } |
| 525 | |
| 526 | return OK; |
| 527 | } |
| 528 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 529 | status_t AST::generateStubHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 530 | std::string ifaceName; |
| 531 | if (!AST::isInterface(&ifaceName)) { |
| 532 | // types.hal does not get a stub header. |
| 533 | return OK; |
| 534 | } |
| 535 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 536 | // cut off the leading 'I'. |
| 537 | const std::string baseName = ifaceName.substr(1); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 538 | const std::string klassName = "Bn" + baseName; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 539 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 540 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 541 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 542 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 543 | path.append(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 544 | path.append(".h"); |
| 545 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 546 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 547 | FILE *file = fopen(path.c_str(), "w"); |
| 548 | |
| 549 | if (file == NULL) { |
| 550 | return -errno; |
| 551 | } |
| 552 | |
| 553 | Formatter out(file); |
| 554 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 555 | const std::string guard = makeHeaderGuard(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 556 | |
| 557 | out << "#ifndef " << guard << "\n"; |
| 558 | out << "#define " << guard << "\n\n"; |
| 559 | |
| 560 | std::vector<std::string> packageComponents; |
| 561 | getPackageAndVersionComponents( |
| 562 | &packageComponents, false /* cpp_compatible */); |
| 563 | |
| 564 | out << "#include <"; |
| 565 | for (const auto &component : packageComponents) { |
| 566 | out << component << "/"; |
| 567 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 568 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 569 | |
| 570 | enterLeaveNamespace(out, true /* enter */); |
| 571 | out << "\n"; |
| 572 | |
| 573 | out << "struct " |
| 574 | << "Bn" |
| 575 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 576 | << " : public ::android::hardware::BnInterface<I" |
| 577 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 578 | << "> {\n"; |
| 579 | |
| 580 | out.indent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 581 | out << "explicit Bn" |
| 582 | << baseName |
| 583 | << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);" |
| 584 | << "\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 585 | out << "::android::status_t onTransact(\n"; |
| 586 | out.indent(); |
| 587 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 588 | out << "uint32_t _hidl_code,\n"; |
| 589 | out << "const ::android::hardware::Parcel &_hidl_data,\n"; |
| 590 | out << "::android::hardware::Parcel *_hidl_reply,\n"; |
| 591 | out << "uint32_t _hidl_flags = 0,\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 592 | out << "TransactCallback _hidl_cb = nullptr) override;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 593 | out.unindent(); |
| 594 | out.unindent(); |
| 595 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame^] | 596 | status_t err = generateMethods(out, |
| 597 | "" /* class name */, |
| 598 | MethodLocation::STUB_HEADER, |
| 599 | true /* specify namespaces */); |
| 600 | |
| 601 | if (err != OK) { |
| 602 | return err; |
| 603 | } |
| 604 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 605 | out.unindent(); |
| 606 | |
| 607 | out << "};\n\n"; |
| 608 | |
| 609 | enterLeaveNamespace(out, false /* enter */); |
| 610 | |
| 611 | out << "\n#endif // " << guard << "\n"; |
| 612 | |
| 613 | return OK; |
| 614 | } |
| 615 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 616 | status_t AST::generateProxyHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 617 | std::string ifaceName; |
| 618 | if (!AST::isInterface(&ifaceName)) { |
| 619 | // types.hal does not get a proxy header. |
| 620 | return OK; |
| 621 | } |
| 622 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 623 | // cut off the leading 'I'. |
| 624 | const std::string baseName = ifaceName.substr(1); |
| 625 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 626 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 627 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 628 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 629 | path.append("Bp"); |
| 630 | path.append(baseName); |
| 631 | path.append(".h"); |
| 632 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 633 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 634 | FILE *file = fopen(path.c_str(), "w"); |
| 635 | |
| 636 | if (file == NULL) { |
| 637 | return -errno; |
| 638 | } |
| 639 | |
| 640 | Formatter out(file); |
| 641 | |
| 642 | const std::string guard = makeHeaderGuard("Bp" + baseName); |
| 643 | |
| 644 | out << "#ifndef " << guard << "\n"; |
| 645 | out << "#define " << guard << "\n\n"; |
| 646 | |
| 647 | std::vector<std::string> packageComponents; |
| 648 | getPackageAndVersionComponents( |
| 649 | &packageComponents, false /* cpp_compatible */); |
| 650 | |
| 651 | out << "#include <"; |
| 652 | for (const auto &component : packageComponents) { |
| 653 | out << component << "/"; |
| 654 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 655 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 656 | |
| 657 | enterLeaveNamespace(out, true /* enter */); |
| 658 | out << "\n"; |
| 659 | |
| 660 | out << "struct " |
| 661 | << "Bp" |
| 662 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 663 | << " : public ::android::hardware::BpInterface<IHw" |
| 664 | << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 665 | << "> {\n"; |
| 666 | |
| 667 | out.indent(); |
| 668 | |
| 669 | out << "explicit Bp" |
| 670 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 671 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 672 | << "\n\n"; |
| 673 | |
Iliyan Malchev | 795dd43 | 2016-09-02 11:48:26 -0700 | [diff] [blame] | 674 | out << "virtual bool isRemote() const { return true; }\n\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 675 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame^] | 676 | status_t err = generateMethods(out, |
| 677 | "" /* class name */, |
| 678 | MethodLocation::PROXY_HEADER, |
| 679 | true /* generate specify namespaces */); |
| 680 | |
| 681 | if (err != OK) { |
| 682 | return err; |
| 683 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 684 | |
| 685 | out.unindent(); |
| 686 | |
| 687 | out << "};\n\n"; |
| 688 | |
| 689 | enterLeaveNamespace(out, false /* enter */); |
| 690 | |
| 691 | out << "\n#endif // " << guard << "\n"; |
| 692 | |
| 693 | return OK; |
| 694 | } |
| 695 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 696 | status_t AST::generateAllSource(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 697 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 698 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 699 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 700 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 701 | |
| 702 | std::string ifaceName; |
| 703 | std::string baseName; |
| 704 | |
| 705 | bool isInterface = true; |
| 706 | if (!AST::isInterface(&ifaceName)) { |
| 707 | baseName = "types"; |
| 708 | isInterface = false; |
| 709 | } else { |
| 710 | baseName = ifaceName.substr(1); // cut off the leading 'I'. |
| 711 | } |
| 712 | |
| 713 | path.append(baseName); |
| 714 | |
| 715 | if (baseName != "types") { |
| 716 | path.append("All"); |
| 717 | } |
| 718 | |
| 719 | path.append(".cpp"); |
| 720 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 721 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 722 | FILE *file = fopen(path.c_str(), "w"); |
| 723 | |
| 724 | if (file == NULL) { |
| 725 | return -errno; |
| 726 | } |
| 727 | |
| 728 | Formatter out(file); |
| 729 | |
| 730 | std::vector<std::string> packageComponents; |
| 731 | getPackageAndVersionComponents( |
| 732 | &packageComponents, false /* cpp_compatible */); |
| 733 | |
| 734 | std::string prefix; |
| 735 | for (const auto &component : packageComponents) { |
| 736 | prefix += component; |
| 737 | prefix += "/"; |
| 738 | } |
| 739 | |
| 740 | if (isInterface) { |
| 741 | out << "#include <" << prefix << "/Bp" << baseName << ".h>\n"; |
| 742 | out << "#include <" << prefix << "/Bn" << baseName << ".h>\n"; |
| 743 | } else { |
| 744 | out << "#include <" << prefix << "types.h>\n"; |
| 745 | } |
| 746 | |
| 747 | out << "\n"; |
| 748 | |
| 749 | enterLeaveNamespace(out, true /* enter */); |
| 750 | out << "\n"; |
| 751 | |
| 752 | status_t err = generateTypeSource(out, ifaceName); |
| 753 | |
| 754 | if (err == OK && isInterface) { |
| 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() |
| 768 | << iface->fqName().version() << ".impl.so\"" |
| 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::" |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 893 | << 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::" |
| 1039 | << upcase(method->name()) |
| 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 | |