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