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