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