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