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) { |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 55 | err = generatePassthroughHeader(outputPath); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 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 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 128 | static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) { |
| 129 | out << "static ::android::sp<" << interfaceName << "> getService(" |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 130 | << "const std::string &serviceName, bool getStub=false);\n"; |
| 131 | out << "::android::status_t registerAsService(const std::string &serviceName);\n"; |
| 132 | out << "static bool registerForNotifications(\n"; |
| 133 | out.indent(2, [&] { |
| 134 | out << "const std::string &serviceName,\n" |
| 135 | << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> " |
| 136 | << "¬ification);\n"; |
| 137 | }); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | static void implementServiceManagerInteractions(Formatter &out, |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 142 | const FQName &fqName, const std::string &package) { |
| 143 | |
| 144 | const std::string interfaceName = fqName.getInterfaceName(); |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 145 | |
| 146 | out << "// static\n" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 147 | << "::android::sp<" << interfaceName << "> " << interfaceName << "::getService(" |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 148 | << "const std::string &serviceName, bool getStub) "; |
| 149 | out.block([&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 150 | out << "::android::sp<" << interfaceName << "> iface;\n" |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 151 | << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n"; |
| 152 | out.indent(2, [&] { |
| 153 | out << "= ::android::hardware::defaultServiceManager();\n"; |
| 154 | }); |
| 155 | out.sIf("sm != nullptr && !getStub", [&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 156 | out << "::android::hardware::Return<::android::sp<" << gIBaseFqName.cppName() << ">> ret = \n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 157 | out.indent(2, [&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 158 | out << "sm->get(\"" << package << "::" << interfaceName << "\", serviceName.c_str());\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 159 | }); |
Steven Moreland | cd00b9b | 2016-12-29 10:34:03 -0800 | [diff] [blame] | 160 | out.sIf("ret.isOk()", [&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 161 | out << "iface = " << interfaceName << "::castFrom(ret);\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 162 | out.sIf("iface != nullptr", [&] { |
| 163 | out << "return iface;\n"; |
| 164 | }).endl(); |
| 165 | }).endl(); |
| 166 | }).endl(); |
| 167 | out << "const int dlMode = RTLD_LAZY;\n"; |
| 168 | out << "void *handle = nullptr;\n"; |
| 169 | for (const auto &path : std::vector<std::string>({ |
| 170 | "HAL_LIBRARY_PATH_ODM", "HAL_LIBRARY_PATH_VENDOR", "HAL_LIBRARY_PATH_SYSTEM" |
| 171 | })) { |
| 172 | out.sIf("handle == nullptr", [&] { |
| 173 | out << "handle = dlopen(" |
| 174 | << path << " \"" << package << "-impl.so\", dlMode);\n"; |
| 175 | }).endl(); |
| 176 | } |
| 177 | out.sIf("handle == nullptr", [&] { |
| 178 | out << "return iface;\n"; |
| 179 | }).endl(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 180 | out << "" << interfaceName << "* (*generator)(const char* name);\n" |
| 181 | << "*(void **)(&generator) = dlsym(handle, \"HIDL_FETCH_" << interfaceName << "\");\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 182 | out.sIf("generator", [&] { |
| 183 | out << "iface = (*generator)(serviceName.c_str());\n"; |
| 184 | out.sIf("iface != nullptr", [&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 185 | out << "iface = new " << fqName.getInterfacePassthroughName() << "(iface);\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 186 | }).endl(); |
| 187 | }).endl(); |
| 188 | out << "return iface;\n"; |
| 189 | }).endl().endl(); |
| 190 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 191 | out << "::android::status_t " << interfaceName << "::registerAsService(" |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 192 | << "const std::string &serviceName) "; |
| 193 | out.block([&] { |
| 194 | out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n"; |
| 195 | out.indent(2, [&] { |
| 196 | out << "= ::android::hardware::defaultServiceManager();\n"; |
| 197 | }); |
| 198 | out.sIf("sm == nullptr", [&] { |
| 199 | out << "return ::android::INVALID_OPERATION;\n"; |
| 200 | }).endl(); |
| 201 | out << "bool success = false;\n" |
| 202 | << "::android::hardware::Return<void> ret =\n"; |
| 203 | out.indent(2, [&] { |
| 204 | out << "this->interfaceChain(" |
| 205 | << "[&success, &sm, &serviceName, this](const auto &chain) "; |
| 206 | out.block([&] { |
| 207 | out << "::android::hardware::Return<bool> addRet = " |
| 208 | << "sm->add(chain, serviceName.c_str(), this);\n"; |
| 209 | out << "success = addRet.isOk() && addRet;\n"; |
| 210 | }); |
| 211 | out << ");\n"; |
Steven Moreland | cd00b9b | 2016-12-29 10:34:03 -0800 | [diff] [blame] | 212 | out << "success = success && ret.isOk();\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 213 | }); |
| 214 | out << "return success ? ::android::OK : ::android::UNKNOWN_ERROR;\n"; |
| 215 | }).endl().endl(); |
| 216 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 217 | out << "bool " << interfaceName << "::registerForNotifications(\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 218 | out.indent(2, [&] { |
| 219 | out << "const std::string &serviceName,\n" |
| 220 | << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> " |
| 221 | << "¬ification) "; |
| 222 | }); |
| 223 | out.block([&] { |
| 224 | out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n"; |
| 225 | out.indent(2, [&] { |
| 226 | out << "= ::android::hardware::defaultServiceManager();\n"; |
| 227 | }); |
| 228 | out.sIf("sm == nullptr", [&] { |
| 229 | out << "return false;\n"; |
| 230 | }).endl(); |
| 231 | out << "::android::hardware::Return<bool> success =\n"; |
| 232 | out.indent(2, [&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 233 | out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 234 | out.indent(2, [&] { |
| 235 | out << "serviceName, notification);\n"; |
| 236 | }); |
| 237 | }); |
| 238 | out << "return success.isOk() && success;\n"; |
| 239 | }).endl().endl(); |
| 240 | } |
| 241 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 242 | status_t AST::generateInterfaceHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 243 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 244 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 245 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 246 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 247 | |
| 248 | std::string ifaceName; |
| 249 | bool isInterface = true; |
| 250 | if (!AST::isInterface(&ifaceName)) { |
| 251 | ifaceName = "types"; |
| 252 | isInterface = false; |
| 253 | } |
| 254 | path.append(ifaceName); |
| 255 | path.append(".h"); |
| 256 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 257 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 258 | FILE *file = fopen(path.c_str(), "w"); |
| 259 | |
| 260 | if (file == NULL) { |
| 261 | return -errno; |
| 262 | } |
| 263 | |
| 264 | Formatter out(file); |
| 265 | |
| 266 | const std::string guard = makeHeaderGuard(ifaceName); |
| 267 | |
| 268 | out << "#ifndef " << guard << "\n"; |
| 269 | out << "#define " << guard << "\n\n"; |
| 270 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 271 | for (const auto &item : mImportedNames) { |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 272 | generateCppPackageInclude(out, item, item.name()); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | if (!mImportedNames.empty()) { |
| 276 | out << "\n"; |
| 277 | } |
| 278 | |
Steven Moreland | 0693f31 | 2016-11-09 15:06:14 -0800 | [diff] [blame] | 279 | if (isInterface) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 280 | if (isIBase()) { |
| 281 | out << "// skipped #include IServiceNotification.h\n\n"; |
| 282 | } else { |
| 283 | out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n"; |
| 284 | } |
Steven Moreland | 0693f31 | 2016-11-09 15:06:14 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 287 | out << "#include <hidl/HidlSupport.h>\n"; |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 288 | out << "#include <hidl/MQDescriptor.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 289 | |
| 290 | if (isInterface) { |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 291 | out << "#include <hidl/Status.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 294 | out << "#include <utils/NativeHandle.h>\n"; |
| 295 | out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */ |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 296 | |
| 297 | enterLeaveNamespace(out, true /* enter */); |
| 298 | out << "\n"; |
| 299 | |
| 300 | if (isInterface) { |
| 301 | out << "struct " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 302 | << ifaceName; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 303 | |
| 304 | const Interface *iface = mRootScope->getInterface(); |
| 305 | const Interface *superType = iface->superType(); |
| 306 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 307 | if (superType == NULL) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 308 | out << " : virtual public ::android::RefBase"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 309 | } else { |
Steven Moreland | d916a70 | 2016-10-26 22:23:09 +0000 | [diff] [blame] | 310 | out << " : public " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 311 | << superType->fullName(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | out << " {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 315 | |
| 316 | out.indent(); |
| 317 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | status_t err = emitTypeDeclarations(out); |
| 321 | |
| 322 | if (err != OK) { |
| 323 | return err; |
| 324 | } |
| 325 | |
| 326 | if (isInterface) { |
| 327 | const Interface *iface = mRootScope->getInterface(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 328 | const Interface *superType = iface->superType(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 329 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 330 | out << "virtual bool isRemote() const "; |
| 331 | if (!isIBase()) { |
| 332 | out << "override "; |
| 333 | } |
| 334 | out << "{ return false; }\n\n"; |
Steven Moreland | d732ea1 | 2016-11-08 17:12:06 -0800 | [diff] [blame] | 335 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 336 | for (const auto &method : iface->methods()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 337 | out << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 338 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 339 | const bool returnsValue = !method->results().empty(); |
Steven Moreland | d732ea1 | 2016-11-08 17:12:06 -0800 | [diff] [blame] | 340 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 341 | |
| 342 | if (elidedReturn == nullptr && returnsValue) { |
| 343 | out << "using " |
| 344 | << method->name() |
| 345 | << "_cb = std::function<void(" |
| 346 | << Method::GetArgSignature(method->results(), |
| 347 | true /* specify namespaces */) |
| 348 | << ")>;\n"; |
| 349 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 350 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 351 | method->dumpAnnotations(out); |
| 352 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 353 | if (elidedReturn) { |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 354 | out << "virtual ::android::hardware::Return<"; |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 355 | out << elidedReturn->type().getCppResultType() << "> "; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 356 | } else { |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 357 | out << "virtual ::android::hardware::Return<void> "; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | out << method->name() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 361 | << "(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 362 | << Method::GetArgSignature(method->args(), |
| 363 | true /* specify namespaces */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 364 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 365 | if (returnsValue && elidedReturn == nullptr) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 366 | if (!method->args().empty()) { |
| 367 | out << ", "; |
| 368 | } |
| 369 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 370 | out << method->name() << "_cb _hidl_cb"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 373 | out << ")"; |
| 374 | if (method->isHidlReserved()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 375 | if (!isIBase()) { |
| 376 | out << " override"; |
| 377 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 378 | out << " {\n"; |
| 379 | out.indent(); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 380 | method->cppImpl(IMPL_HEADER, out); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 381 | out.unindent(); |
| 382 | out << "\n}\n"; |
| 383 | } else { |
| 384 | out << " = 0;\n"; |
| 385 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 386 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 387 | |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 388 | out << "// cast static functions\n"; |
| 389 | std::string childTypeResult = iface->getCppResultType(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 390 | |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 391 | for (const Interface *superType : iface->typeChain()) { |
| 392 | out << "static " |
| 393 | << childTypeResult |
| 394 | << " castFrom(" |
| 395 | << superType->getCppArgumentType() |
| 396 | << " parent" |
| 397 | << ");\n"; |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 400 | out << "\nstatic const char* descriptor;\n\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 401 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 402 | if (isIBase()) { |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 403 | out << "// skipped getService, registerAsService, registerForNotifications\n\n"; |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 404 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 405 | declareServiceManagerInteractions(out, iface->localName()); |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 406 | } |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 407 | |
| 408 | out << "private: static int hidlStaticBlock;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | if (isInterface) { |
| 412 | out.unindent(); |
| 413 | |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 414 | out << "};\n\n"; |
| 415 | } |
| 416 | |
| 417 | err = mRootScope->emitGlobalTypeDeclarations(out); |
| 418 | |
| 419 | if (err != OK) { |
| 420 | return err; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | out << "\n"; |
| 424 | enterLeaveNamespace(out, false /* enter */); |
| 425 | |
| 426 | out << "\n#endif // " << guard << "\n"; |
| 427 | |
| 428 | return OK; |
| 429 | } |
| 430 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 431 | status_t AST::generateHwBinderHeader(const std::string &outputPath) const { |
| 432 | std::string ifaceName; |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 433 | bool isInterface = AST::isInterface(&ifaceName); |
| 434 | const Interface *iface = nullptr; |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 435 | std::string klassName{}; |
| 436 | |
| 437 | if(isInterface) { |
| 438 | iface = mRootScope->getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 439 | klassName = iface->getHwName(); |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 440 | } else { |
| 441 | klassName = "hwtypes"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 444 | std::string path = outputPath; |
| 445 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 446 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 447 | path.append(klassName + ".h"); |
| 448 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 449 | FILE *file = fopen(path.c_str(), "w"); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 450 | |
| 451 | if (file == NULL) { |
| 452 | return -errno; |
| 453 | } |
| 454 | |
| 455 | Formatter out(file); |
| 456 | |
| 457 | const std::string guard = makeHeaderGuard(klassName); |
| 458 | |
| 459 | out << "#ifndef " << guard << "\n"; |
| 460 | out << "#define " << guard << "\n\n"; |
| 461 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 462 | if (isInterface) { |
| 463 | generateCppPackageInclude(out, mPackage, ifaceName); |
| 464 | } else { |
| 465 | generateCppPackageInclude(out, mPackage, "types"); |
| 466 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 467 | |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 468 | out << "\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 469 | |
| 470 | for (const auto &item : mImportedNames) { |
| 471 | if (item.name() == "types") { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 472 | generateCppPackageInclude(out, item, "hwtypes"); |
| 473 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 474 | generateCppPackageInclude(out, item, item.getInterfaceStubName()); |
| 475 | generateCppPackageInclude(out, item, item.getInterfaceProxyName()); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 476 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | out << "\n"; |
| 480 | |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 481 | out << "#include <hidl/Status.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 482 | out << "#include <hwbinder/IBinder.h>\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 483 | out << "#include <hwbinder/Parcel.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 484 | |
| 485 | out << "\n"; |
| 486 | |
| 487 | enterLeaveNamespace(out, true /* enter */); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 488 | |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 489 | status_t err = mRootScope->emitGlobalHwDeclarations(out); |
| 490 | if (err != OK) { |
| 491 | return err; |
| 492 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 493 | |
| 494 | enterLeaveNamespace(out, false /* enter */); |
| 495 | |
| 496 | out << "\n#endif // " << guard << "\n"; |
| 497 | |
| 498 | return OK; |
| 499 | } |
| 500 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 501 | status_t AST::emitTypeDeclarations(Formatter &out) const { |
| 502 | return mRootScope->emitTypeDeclarations(out); |
| 503 | } |
| 504 | |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 505 | static void wrapPassthroughArg(Formatter &out, |
| 506 | const TypedVar *arg, bool addPrefixToName, |
| 507 | std::function<void(void)> handleError) { |
| 508 | if (!arg->type().isInterface()) { |
| 509 | return; |
| 510 | } |
| 511 | std::string name = (addPrefixToName ? "_hidl_out_" : "") + arg->name(); |
| 512 | std::string wrappedName = (addPrefixToName ? "_hidl_out_wrapped_" : "_hidl_wrapped_") |
| 513 | + arg->name(); |
| 514 | const Interface &iface = static_cast<const Interface &>(arg->type()); |
| 515 | out << iface.getCppStackType() << " " << wrappedName << ";\n"; |
| 516 | // TODO(elsk): b/33754152 Should not wrap this if object is Bs* |
| 517 | out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] { |
| 518 | out << wrappedName |
| 519 | << " = " |
| 520 | << iface.fqName().cppName() |
| 521 | << "::castFrom(::android::hardware::wrapPassthrough(" |
| 522 | << name << "));\n"; |
| 523 | out.sIf(wrappedName + " == nullptr", [&] { |
| 524 | // Fatal error. Happens when the BsFoo class is not found in the binary |
| 525 | // or any dynamic libraries. |
| 526 | handleError(); |
| 527 | }).endl(); |
| 528 | }).sElse([&] { |
| 529 | out << wrappedName << " = " << name << ";\n"; |
| 530 | }).endl().endl(); |
| 531 | } |
| 532 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 533 | status_t AST::generatePassthroughMethod(Formatter &out, |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 534 | const Method *method) const { |
| 535 | method->generateCppSignature(out); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 536 | |
| 537 | out << " {\n"; |
| 538 | out.indent(); |
| 539 | |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 540 | if (method->isHidlReserved() |
| 541 | && method->overridesCppImpl(IMPL_PASSTHROUGH)) { |
| 542 | method->cppImpl(IMPL_PASSTHROUGH, out); |
| 543 | out.unindent(); |
| 544 | out << "}\n\n"; |
| 545 | return OK; |
| 546 | } |
| 547 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 548 | const bool returnsValue = !method->results().empty(); |
| 549 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 550 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 551 | if (returnsValue && elidedReturn == nullptr) { |
| 552 | generateCheckNonNull(out, "_hidl_cb"); |
| 553 | } |
| 554 | |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 555 | generateCppInstrumentationCall( |
| 556 | out, |
| 557 | InstrumentationEvent::PASSTHROUGH_ENTRY, |
| 558 | method); |
| 559 | |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 560 | |
| 561 | for (const auto &arg : method->args()) { |
| 562 | wrapPassthroughArg(out, arg, false /* addPrefixToName */, [&] { |
| 563 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 564 | out.indent(2, [&] { |
| 565 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n" |
Yifan Hong | 0abd739 | 2016-12-20 16:43:26 -0800 | [diff] [blame] | 566 | << "\"Cannot wrap passthrough interface.\");\n"; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 567 | }); |
| 568 | }); |
| 569 | } |
| 570 | |
| 571 | out << "auto _hidl_error = ::android::hardware::Void();\n"; |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 572 | out << "auto _hidl_return = "; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 573 | |
| 574 | if (method->isOneway()) { |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 575 | out << "addOnewayTask([this, &_hidl_error"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 576 | for (const auto &arg : method->args()) { |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 577 | out << ", " |
| 578 | << (arg->type().isInterface() ? "_hidl_wrapped_" : "") |
| 579 | << arg->name(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 580 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 581 | out << "] {\n"; |
| 582 | out.indent(); |
| 583 | out << "this->"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | out << "mImpl->" |
| 587 | << method->name() |
| 588 | << "("; |
| 589 | |
| 590 | bool first = true; |
| 591 | for (const auto &arg : method->args()) { |
| 592 | if (!first) { |
| 593 | out << ", "; |
| 594 | } |
| 595 | first = false; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 596 | out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 597 | } |
| 598 | if (returnsValue && elidedReturn == nullptr) { |
| 599 | if (!method->args().empty()) { |
| 600 | out << ", "; |
| 601 | } |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 602 | out << "[&]("; |
| 603 | first = true; |
| 604 | for (const auto &arg : method->results()) { |
| 605 | if (!first) { |
| 606 | out << ", "; |
| 607 | } |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 608 | |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 609 | out << "const auto &_hidl_out_" |
| 610 | << arg->name(); |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 611 | |
| 612 | first = false; |
| 613 | } |
| 614 | |
| 615 | out << ") {\n"; |
| 616 | out.indent(); |
| 617 | status_t status = generateCppInstrumentationCall( |
| 618 | out, |
| 619 | InstrumentationEvent::PASSTHROUGH_EXIT, |
| 620 | method); |
| 621 | if (status != OK) { |
| 622 | return status; |
| 623 | } |
| 624 | |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 625 | for (const auto &arg : method->results()) { |
| 626 | wrapPassthroughArg(out, arg, true /* addPrefixToName */, [&] { |
| 627 | out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n"; |
| 628 | out.indent(2, [&] { |
| 629 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n" |
Yifan Hong | 0abd739 | 2016-12-20 16:43:26 -0800 | [diff] [blame] | 630 | << "\"Cannot wrap passthrough interface.\");\n"; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 631 | }); |
| 632 | out << "return;\n"; |
| 633 | }); |
| 634 | } |
| 635 | |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 636 | out << "_hidl_cb("; |
| 637 | first = true; |
| 638 | for (const auto &arg : method->results()) { |
| 639 | if (!first) { |
| 640 | out << ", "; |
| 641 | } |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 642 | first = false; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 643 | out << (arg->type().isInterface() ? "_hidl_out_wrapped_" : "_hidl_out_") |
| 644 | << arg->name(); |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 645 | } |
| 646 | out << ");\n"; |
| 647 | out.unindent(); |
| 648 | out << "});\n\n"; |
| 649 | } else { |
| 650 | out << ");\n\n"; |
| 651 | if (elidedReturn != nullptr) { |
| 652 | out << elidedReturn->type().getCppResultType() |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 653 | << " _hidl_out_" |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 654 | << elidedReturn->name() |
Steven Moreland | 2ae5bca | 2016-12-01 05:56:49 +0000 | [diff] [blame] | 655 | << " = _hidl_return;\n"; |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 656 | } |
| 657 | status_t status = generateCppInstrumentationCall( |
| 658 | out, |
| 659 | InstrumentationEvent::PASSTHROUGH_EXIT, |
| 660 | method); |
| 661 | if (status != OK) { |
| 662 | return status; |
| 663 | } |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 664 | } |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 665 | |
| 666 | if (method->isOneway()) { |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 667 | out.unindent(); |
| 668 | out << "});\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 669 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 670 | |
| 671 | out << "return _hidl_return;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 672 | |
| 673 | out.unindent(); |
| 674 | out << "}\n"; |
| 675 | |
| 676 | return OK; |
| 677 | } |
| 678 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 679 | status_t AST::generateMethods(Formatter &out, MethodGenerator gen) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 680 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 681 | const Interface *iface = mRootScope->getInterface(); |
| 682 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 683 | const Interface *prevIterface = nullptr; |
| 684 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 685 | const Method *method = tuple.method(); |
| 686 | const Interface *superInterface = tuple.interface(); |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 687 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 688 | if(prevIterface != superInterface) { |
| 689 | if (prevIterface != nullptr) { |
| 690 | out << "\n"; |
| 691 | } |
| 692 | out << "// Methods from " |
| 693 | << superInterface->fullName() |
| 694 | << " follow.\n"; |
| 695 | prevIterface = superInterface; |
| 696 | } |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 697 | status_t err = gen(method, superInterface); |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 698 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 699 | if (err != OK) { |
| 700 | return err; |
| 701 | } |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 704 | out << "\n"; |
| 705 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 706 | return OK; |
| 707 | } |
| 708 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 709 | status_t AST::generateStubHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 710 | std::string ifaceName; |
| 711 | if (!AST::isInterface(&ifaceName)) { |
| 712 | // types.hal does not get a stub header. |
| 713 | return OK; |
| 714 | } |
| 715 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 716 | const Interface *iface = mRootScope->getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 717 | const std::string klassName = iface->getStubName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 718 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 719 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 720 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 721 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 722 | path.append(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 723 | path.append(".h"); |
| 724 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 725 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 726 | FILE *file = fopen(path.c_str(), "w"); |
| 727 | |
| 728 | if (file == NULL) { |
| 729 | return -errno; |
| 730 | } |
| 731 | |
| 732 | Formatter out(file); |
| 733 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 734 | const std::string guard = makeHeaderGuard(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 735 | |
| 736 | out << "#ifndef " << guard << "\n"; |
| 737 | out << "#define " << guard << "\n\n"; |
| 738 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 739 | generateCppPackageInclude(out, mPackage, iface->getHwName()); |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 740 | out << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 741 | |
| 742 | enterLeaveNamespace(out, true /* enter */); |
| 743 | out << "\n"; |
| 744 | |
| 745 | out << "struct " |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 746 | << klassName; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 747 | if (iface->isIBase()) { |
| 748 | out << " : public ::android::hardware::BBinder"; |
| 749 | out << ", public ::android::hardware::HidlInstrumentor {\n"; |
| 750 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 751 | out << " : public " |
| 752 | << gIBaseFqName.getInterfaceStubFqName().cppName() |
| 753 | << " {\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 754 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 755 | |
| 756 | out.indent(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 757 | out << "explicit " |
| 758 | << klassName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 759 | << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);" |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 760 | << "\n"; |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 761 | out << "explicit " |
| 762 | << klassName |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 763 | << "(const ::android::sp<" << ifaceName << "> &_hidl_impl," |
| 764 | << " const std::string& prefix);" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 765 | << "\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 766 | out << "::android::status_t onTransact(\n"; |
| 767 | out.indent(); |
| 768 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 769 | out << "uint32_t _hidl_code,\n"; |
| 770 | out << "const ::android::hardware::Parcel &_hidl_data,\n"; |
| 771 | out << "::android::hardware::Parcel *_hidl_reply,\n"; |
| 772 | out << "uint32_t _hidl_flags = 0,\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 773 | out << "TransactCallback _hidl_cb = nullptr) override;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 774 | out.unindent(); |
| 775 | out.unindent(); |
| 776 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 777 | out << "::android::sp<" << ifaceName << "> getImpl() { return _hidl_mImpl; };\n"; |
| 778 | out.unindent(); |
| 779 | out << "private:\n"; |
| 780 | out.indent(); |
| 781 | out << "::android::sp<" << ifaceName << "> _hidl_mImpl;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 782 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 783 | out << "};\n\n"; |
| 784 | |
| 785 | enterLeaveNamespace(out, false /* enter */); |
| 786 | |
| 787 | out << "\n#endif // " << guard << "\n"; |
| 788 | |
| 789 | return OK; |
| 790 | } |
| 791 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 792 | status_t AST::generateProxyHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 793 | std::string ifaceName; |
| 794 | if (!AST::isInterface(&ifaceName)) { |
| 795 | // types.hal does not get a proxy header. |
| 796 | return OK; |
| 797 | } |
| 798 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 799 | const Interface *iface = mRootScope->getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 800 | const std::string proxyName = iface->getProxyName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 801 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 802 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 803 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 804 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 805 | path.append(proxyName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 806 | path.append(".h"); |
| 807 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 808 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 809 | FILE *file = fopen(path.c_str(), "w"); |
| 810 | |
| 811 | if (file == NULL) { |
| 812 | return -errno; |
| 813 | } |
| 814 | |
| 815 | Formatter out(file); |
| 816 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 817 | const std::string guard = makeHeaderGuard(proxyName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 818 | |
| 819 | out << "#ifndef " << guard << "\n"; |
| 820 | out << "#define " << guard << "\n\n"; |
| 821 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 822 | out << "#include <hidl/HidlTransportSupport.h>\n\n"; |
| 823 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 824 | std::vector<std::string> packageComponents; |
| 825 | getPackageAndVersionComponents( |
| 826 | &packageComponents, false /* cpp_compatible */); |
| 827 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 828 | generateCppPackageInclude(out, mPackage, iface->getHwName()); |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 829 | out << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 830 | |
| 831 | enterLeaveNamespace(out, true /* enter */); |
| 832 | out << "\n"; |
| 833 | |
| 834 | out << "struct " |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 835 | << proxyName |
| 836 | << " : public ::android::hardware::BpInterface<" |
| 837 | << iface->localName() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 838 | << ">, public ::android::hardware::HidlInstrumentor {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 839 | |
| 840 | out.indent(); |
| 841 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 842 | out << "explicit " |
| 843 | << proxyName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 844 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 845 | << "\n\n"; |
| 846 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 847 | out << "virtual bool isRemote() const override { return true; }\n\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 848 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 849 | status_t err = generateMethods(out, [&](const Method *method, const Interface *) { |
| 850 | method->generateCppSignature(out); |
| 851 | out << " override;\n"; |
| 852 | return OK; |
| 853 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 854 | |
| 855 | if (err != OK) { |
| 856 | return err; |
| 857 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 858 | |
| 859 | out.unindent(); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 860 | out << "private:\n"; |
| 861 | out.indent(); |
| 862 | out << "std::mutex _hidl_mMutex;\n" |
| 863 | << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>" |
| 864 | << " _hidl_mDeathRecipients;\n"; |
| 865 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 866 | out << "};\n\n"; |
| 867 | |
| 868 | enterLeaveNamespace(out, false /* enter */); |
| 869 | |
| 870 | out << "\n#endif // " << guard << "\n"; |
| 871 | |
| 872 | return OK; |
| 873 | } |
| 874 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 875 | status_t AST::generateAllSource(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 876 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 877 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 878 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 879 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 880 | |
| 881 | std::string ifaceName; |
| 882 | std::string baseName; |
| 883 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 884 | const Interface *iface = nullptr; |
| 885 | bool isInterface; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 886 | if (!AST::isInterface(&ifaceName)) { |
| 887 | baseName = "types"; |
| 888 | isInterface = false; |
| 889 | } else { |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 890 | iface = mRootScope->getInterface(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 891 | baseName = iface->getBaseName(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 892 | isInterface = true; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | path.append(baseName); |
| 896 | |
| 897 | if (baseName != "types") { |
| 898 | path.append("All"); |
| 899 | } |
| 900 | |
| 901 | path.append(".cpp"); |
| 902 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 903 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 904 | FILE *file = fopen(path.c_str(), "w"); |
| 905 | |
| 906 | if (file == NULL) { |
| 907 | return -errno; |
| 908 | } |
| 909 | |
| 910 | Formatter out(file); |
| 911 | |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 912 | out << "#include <android/log.h>\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 913 | out << "#include <cutils/trace.h>\n"; |
| 914 | out << "#include <hidl/HidlTransportSupport.h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 915 | if (isInterface) { |
Steven Moreland | 19d5c17 | 2016-10-20 19:20:25 -0700 | [diff] [blame] | 916 | // This is a no-op for IServiceManager itself. |
| 917 | out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n"; |
| 918 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 919 | generateCppPackageInclude(out, mPackage, iface->getProxyName()); |
| 920 | generateCppPackageInclude(out, mPackage, iface->getStubName()); |
| 921 | generateCppPackageInclude(out, mPackage, iface->getPassthroughName()); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 922 | |
| 923 | for (const Interface *superType : iface->superTypeChain()) { |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 924 | generateCppPackageInclude(out, |
| 925 | superType->fqName(), |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 926 | superType->fqName().getInterfaceProxyName()); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 927 | } |
Yifan Hong | 2cbbdf9 | 2016-12-05 15:20:50 -0800 | [diff] [blame] | 928 | |
| 929 | out << "#include <hidl/ServiceManagement.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 930 | } else { |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 931 | generateCppPackageInclude(out, mPackage, "types"); |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 932 | generateCppPackageInclude(out, mPackage, "hwtypes"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | out << "\n"; |
| 936 | |
| 937 | enterLeaveNamespace(out, true /* enter */); |
| 938 | out << "\n"; |
| 939 | |
| 940 | status_t err = generateTypeSource(out, ifaceName); |
| 941 | |
| 942 | if (err == OK && isInterface) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 943 | const Interface *iface = mRootScope->getInterface(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 944 | |
| 945 | // need to be put here, generateStubSource is using this. |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 946 | out << "const char* " |
| 947 | << iface->localName() |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 948 | << "::descriptor(\"" |
| 949 | << iface->fqName().string() |
| 950 | << "\");\n\n"; |
| 951 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 952 | out << "int " |
| 953 | << iface->localName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 954 | << "::hidlStaticBlock = []() -> int {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 955 | out.indent([&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 956 | out << "::android::hardware::gBnConstructorMap[" |
| 957 | << iface->localName() |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 958 | << "::descriptor]\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 959 | out.indent(2, [&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 960 | out << "= [](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 961 | out.indent([&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 962 | out << "return new " |
| 963 | << iface->getStubName() |
| 964 | << "(reinterpret_cast<" |
| 965 | << iface->localName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 966 | << " *>(iIntf));\n"; |
| 967 | }); |
| 968 | out << "};\n"; |
| 969 | }); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 970 | out << "::android::hardware::gBsConstructorMap[" |
| 971 | << iface->localName() |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 972 | << "::descriptor]\n"; |
| 973 | out.indent(2, [&] { |
| 974 | out << "= [](void *iIntf) -> ::android::sp<" |
| 975 | << gIBaseFqName.cppName() |
| 976 | << "> {\n"; |
| 977 | out.indent([&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 978 | out << "return new " |
| 979 | << iface->getPassthroughName() |
| 980 | << "(reinterpret_cast<" |
| 981 | << iface->localName() |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 982 | << " *>(iIntf));\n"; |
| 983 | }); |
| 984 | out << "};\n"; |
| 985 | }); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 986 | out << "return 1;\n"; |
| 987 | }); |
| 988 | out << "}();\n\n"; |
| 989 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 990 | err = generateInterfaceSource(out); |
| 991 | } |
| 992 | |
| 993 | if (err == OK && isInterface) { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 994 | err = generateProxySource(out, iface->fqName()); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | if (err == OK && isInterface) { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 998 | err = generateStubSource(out, iface); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1001 | if (err == OK && isInterface) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1002 | err = generatePassthroughSource(out); |
| 1003 | } |
| 1004 | |
| 1005 | if (err == OK && isInterface) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 1006 | const Interface *iface = mRootScope->getInterface(); |
| 1007 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 1008 | if (isIBase()) { |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 1009 | out << "// skipped getService, registerAsService, registerForNotifications\n"; |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 1010 | } else { |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 1011 | std::string package = iface->fqName().package() |
| 1012 | + iface->fqName().atVersion(); |
| 1013 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1014 | implementServiceManagerInteractions(out, iface->fqName(), package); |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 1015 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1018 | enterLeaveNamespace(out, false /* enter */); |
| 1019 | |
| 1020 | return err; |
| 1021 | } |
| 1022 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 1023 | // static |
| 1024 | void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) { |
Yifan Hong | a018ed5 | 2016-12-13 16:35:08 -0800 | [diff] [blame] | 1025 | out.sIf(nonNull + " == nullptr", [&] { |
| 1026 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 1027 | out.indent(2, [&] { |
| 1028 | out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT);\n"; |
| 1029 | }); |
| 1030 | }).endl().endl(); |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1033 | status_t AST::generateTypeSource( |
| 1034 | Formatter &out, const std::string &ifaceName) const { |
| 1035 | return mRootScope->emitTypeDefinitions(out, ifaceName); |
| 1036 | } |
| 1037 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1038 | void AST::declareCppReaderLocals( |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1039 | Formatter &out, |
| 1040 | const std::vector<TypedVar *> &args, |
| 1041 | bool forResults) const { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1042 | if (args.empty()) { |
| 1043 | return; |
| 1044 | } |
| 1045 | |
| 1046 | for (const auto &arg : args) { |
| 1047 | const Type &type = arg->type(); |
| 1048 | |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1049 | out << type.getCppResultType() |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1050 | << " " |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1051 | << (forResults ? "_hidl_out_" : "") + arg->name() |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1052 | << ";\n"; |
| 1053 | } |
| 1054 | |
| 1055 | out << "\n"; |
| 1056 | } |
| 1057 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1058 | void AST::emitCppReaderWriter( |
| 1059 | Formatter &out, |
| 1060 | const std::string &parcelObj, |
| 1061 | bool parcelObjIsPointer, |
| 1062 | const TypedVar *arg, |
| 1063 | bool isReader, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1064 | Type::ErrorMode mode, |
| 1065 | bool addPrefixToName) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1066 | const Type &type = arg->type(); |
| 1067 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1068 | type.emitReaderWriter( |
| 1069 | out, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1070 | addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(), |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1071 | parcelObj, |
| 1072 | parcelObjIsPointer, |
| 1073 | isReader, |
| 1074 | mode); |
| 1075 | } |
| 1076 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1077 | void AST::emitCppResolveReferences( |
| 1078 | Formatter &out, |
| 1079 | const std::string &parcelObj, |
| 1080 | bool parcelObjIsPointer, |
| 1081 | const TypedVar *arg, |
| 1082 | bool isReader, |
| 1083 | Type::ErrorMode mode, |
| 1084 | bool addPrefixToName) const { |
| 1085 | const Type &type = arg->type(); |
| 1086 | if(type.needsResolveReferences()) { |
| 1087 | type.emitResolveReferences( |
| 1088 | out, |
| 1089 | addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(), |
| 1090 | isReader, // nameIsPointer |
| 1091 | parcelObj, |
| 1092 | parcelObjIsPointer, |
| 1093 | isReader, |
| 1094 | mode); |
| 1095 | } |
| 1096 | } |
| 1097 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1098 | status_t AST::generateProxyMethodSource(Formatter &out, |
| 1099 | const std::string &klassName, |
| 1100 | const Method *method, |
| 1101 | const Interface *superInterface) const { |
| 1102 | |
| 1103 | method->generateCppSignature(out, |
| 1104 | klassName, |
| 1105 | true /* specify namespaces */); |
| 1106 | |
| 1107 | const bool returnsValue = !method->results().empty(); |
| 1108 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 1109 | |
Steven Moreland | 41c6d2e | 2016-11-07 12:26:54 -0800 | [diff] [blame] | 1110 | out << " {\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1111 | |
| 1112 | out.indent(); |
| 1113 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 1114 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) { |
| 1115 | method->cppImpl(IMPL_PROXY, out); |
| 1116 | out.unindent(); |
| 1117 | out << "}\n\n"; |
| 1118 | return OK; |
| 1119 | } |
| 1120 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1121 | if (returnsValue && elidedReturn == nullptr) { |
| 1122 | generateCheckNonNull(out, "_hidl_cb"); |
| 1123 | } |
| 1124 | |
| 1125 | status_t status = generateCppInstrumentationCall( |
| 1126 | out, |
| 1127 | InstrumentationEvent::CLIENT_API_ENTRY, |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1128 | method); |
| 1129 | if (status != OK) { |
| 1130 | return status; |
| 1131 | } |
| 1132 | |
| 1133 | out << "::android::hardware::Parcel _hidl_data;\n"; |
| 1134 | out << "::android::hardware::Parcel _hidl_reply;\n"; |
| 1135 | out << "::android::status_t _hidl_err;\n"; |
| 1136 | out << "::android::hardware::Status _hidl_status;\n\n"; |
| 1137 | |
| 1138 | declareCppReaderLocals( |
| 1139 | out, method->results(), true /* forResults */); |
| 1140 | |
| 1141 | out << "_hidl_err = _hidl_data.writeInterfaceToken("; |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1142 | out << superInterface->fqName().cppName(); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1143 | out << "::descriptor);\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1144 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
| 1145 | |
Martijn Coenen | fff7335 | 2017-01-04 16:36:31 +0100 | [diff] [blame] | 1146 | bool hasInterfaceArgument = false; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1147 | // First DFS: write all buffers and resolve pointers for parent |
| 1148 | for (const auto &arg : method->args()) { |
Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 1149 | if (arg->type().isInterface()) { |
| 1150 | hasInterfaceArgument = true; |
| 1151 | } |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1152 | emitCppReaderWriter( |
| 1153 | out, |
| 1154 | "_hidl_data", |
| 1155 | false /* parcelObjIsPointer */, |
| 1156 | arg, |
| 1157 | false /* reader */, |
| 1158 | Type::ErrorMode_Goto, |
| 1159 | false /* addPrefixToName */); |
| 1160 | } |
| 1161 | |
| 1162 | // Second DFS: resolve references. |
| 1163 | for (const auto &arg : method->args()) { |
| 1164 | emitCppResolveReferences( |
| 1165 | out, |
| 1166 | "_hidl_data", |
| 1167 | false /* parcelObjIsPointer */, |
| 1168 | arg, |
| 1169 | false /* reader */, |
| 1170 | Type::ErrorMode_Goto, |
| 1171 | false /* addPrefixToName */); |
| 1172 | } |
| 1173 | |
Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 1174 | if (hasInterfaceArgument) { |
| 1175 | // Start binder threadpool to handle incoming transactions |
| 1176 | out << "::android::hardware::ProcessState::self()->startThreadPool();\n"; |
| 1177 | } |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1178 | out << "_hidl_err = remote()->transact(" |
| 1179 | << method->getSerialId() |
| 1180 | << " /* " |
| 1181 | << method->name() |
| 1182 | << " */, _hidl_data, &_hidl_reply"; |
| 1183 | |
| 1184 | if (method->isOneway()) { |
| 1185 | out << ", ::android::hardware::IBinder::FLAG_ONEWAY"; |
| 1186 | } |
| 1187 | out << ");\n"; |
| 1188 | |
| 1189 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
| 1190 | |
| 1191 | if (!method->isOneway()) { |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1192 | out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1193 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
| 1194 | out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n"; |
| 1195 | |
| 1196 | |
| 1197 | // First DFS: write all buffers and resolve pointers for parent |
| 1198 | for (const auto &arg : method->results()) { |
| 1199 | emitCppReaderWriter( |
| 1200 | out, |
| 1201 | "_hidl_reply", |
| 1202 | false /* parcelObjIsPointer */, |
| 1203 | arg, |
| 1204 | true /* reader */, |
| 1205 | Type::ErrorMode_Goto, |
| 1206 | true /* addPrefixToName */); |
| 1207 | } |
| 1208 | |
| 1209 | // Second DFS: resolve references. |
| 1210 | for (const auto &arg : method->results()) { |
| 1211 | emitCppResolveReferences( |
| 1212 | out, |
| 1213 | "_hidl_reply", |
| 1214 | false /* parcelObjIsPointer */, |
| 1215 | arg, |
| 1216 | true /* reader */, |
| 1217 | Type::ErrorMode_Goto, |
| 1218 | true /* addPrefixToName */); |
| 1219 | } |
| 1220 | |
| 1221 | if (returnsValue && elidedReturn == nullptr) { |
| 1222 | out << "_hidl_cb("; |
| 1223 | |
| 1224 | bool first = true; |
| 1225 | for (const auto &arg : method->results()) { |
| 1226 | if (!first) { |
| 1227 | out << ", "; |
| 1228 | } |
| 1229 | |
| 1230 | if (arg->type().resultNeedsDeref()) { |
| 1231 | out << "*"; |
| 1232 | } |
| 1233 | out << "_hidl_out_" << arg->name(); |
| 1234 | |
| 1235 | first = false; |
| 1236 | } |
| 1237 | |
| 1238 | out << ");\n\n"; |
| 1239 | } |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1240 | } |
| 1241 | status = generateCppInstrumentationCall( |
| 1242 | out, |
| 1243 | InstrumentationEvent::CLIENT_API_EXIT, |
| 1244 | method); |
| 1245 | if (status != OK) { |
| 1246 | return status; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | if (elidedReturn != nullptr) { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1250 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1251 | out << "return ::android::hardware::Return<"; |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1252 | out << elidedReturn->type().getCppResultType() |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1253 | << ">(_hidl_out_" << elidedReturn->name() << ");\n\n"; |
| 1254 | } else { |
| 1255 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1256 | out << "return ::android::hardware::Return<void>();\n\n"; |
| 1257 | } |
| 1258 | |
| 1259 | out.unindent(); |
| 1260 | out << "_hidl_error:\n"; |
| 1261 | out.indent(); |
| 1262 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1263 | out << "return ::android::hardware::Return<"; |
| 1264 | if (elidedReturn != nullptr) { |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1265 | out << method->results().at(0)->type().getCppResultType(); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1266 | } else { |
| 1267 | out << "void"; |
| 1268 | } |
| 1269 | out << ">(_hidl_status);\n"; |
| 1270 | |
| 1271 | out.unindent(); |
| 1272 | out << "}\n\n"; |
| 1273 | return OK; |
| 1274 | } |
| 1275 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1276 | status_t AST::generateProxySource( |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1277 | Formatter &out, const FQName &fqName) const { |
| 1278 | const std::string klassName = fqName.getInterfaceProxyName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1279 | |
| 1280 | out << klassName |
| 1281 | << "::" |
| 1282 | << klassName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1283 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1284 | |
| 1285 | out.indent(); |
| 1286 | out.indent(); |
| 1287 | |
| 1288 | out << ": BpInterface" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1289 | << "<" |
| 1290 | << fqName.getInterfaceName() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1291 | << ">(_hidl_impl),\n" |
Steven Moreland | 19d5c17 | 2016-10-20 19:20:25 -0700 | [diff] [blame] | 1292 | << " ::android::hardware::HidlInstrumentor(\"" |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1293 | << mPackage.string() |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1294 | << "::" |
| 1295 | << fqName.getInterfaceName() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1296 | << "\") {\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1297 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1298 | out.unindent(); |
| 1299 | out.unindent(); |
| 1300 | out << "}\n\n"; |
| 1301 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1302 | status_t err = generateMethods(out, [&](const Method *method, const Interface *superInterface) { |
| 1303 | return generateProxyMethodSource(out, klassName, method, superInterface); |
| 1304 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1305 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1306 | return err; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | status_t AST::generateStubSource( |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1310 | Formatter &out, |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1311 | const Interface *iface) const { |
| 1312 | const std::string interfaceName = iface->localName(); |
| 1313 | const std::string klassName = iface->getStubName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1314 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1315 | out << klassName |
| 1316 | << "::" |
| 1317 | << klassName |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1318 | << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1319 | |
| 1320 | out.indent(); |
| 1321 | out.indent(); |
| 1322 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1323 | if (iface->isIBase()) { |
| 1324 | out << ": ::android::hardware::HidlInstrumentor(\""; |
| 1325 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1326 | out << ": " |
| 1327 | << gIBaseFqName.getInterfaceStubFqName().cppName() |
| 1328 | << "(_hidl_impl, \""; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | out << mPackage.string() |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1332 | << "::" |
| 1333 | << interfaceName |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1334 | << "\") { \n"; |
| 1335 | out.indent(); |
| 1336 | out << "_hidl_mImpl = _hidl_impl;\n"; |
| 1337 | out.unindent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1338 | |
| 1339 | out.unindent(); |
| 1340 | out.unindent(); |
| 1341 | out << "}\n\n"; |
| 1342 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1343 | if (iface->isIBase()) { |
Yifan Hong | 01e7cde | 2017-01-09 17:45:45 -0800 | [diff] [blame^] | 1344 | // BnHwBase has a constructor to initialize the HidlInstrumentor |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1345 | // class properly. |
| 1346 | out << klassName |
| 1347 | << "::" |
| 1348 | << klassName |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1349 | << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl," |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1350 | << " const std::string &prefix)\n"; |
| 1351 | |
| 1352 | out.indent(); |
| 1353 | out.indent(); |
| 1354 | |
| 1355 | out << ": ::android::hardware::HidlInstrumentor(prefix) { \n"; |
| 1356 | out.indent(); |
| 1357 | out << "_hidl_mImpl = _hidl_impl;\n"; |
| 1358 | out.unindent(); |
| 1359 | |
| 1360 | out.unindent(); |
| 1361 | out.unindent(); |
| 1362 | out << "}\n\n"; |
| 1363 | } |
| 1364 | |
| 1365 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1366 | out << "::android::status_t " << klassName << "::onTransact(\n"; |
| 1367 | |
| 1368 | out.indent(); |
| 1369 | out.indent(); |
| 1370 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1371 | out << "uint32_t _hidl_code,\n" |
| 1372 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 1373 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 1374 | << "uint32_t _hidl_flags,\n" |
| 1375 | << "TransactCallback _hidl_cb) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1376 | |
| 1377 | out.unindent(); |
| 1378 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1379 | out << "::android::status_t _hidl_err = ::android::OK;\n\n"; |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1380 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1381 | out.indent(); |
| 1382 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1383 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 1384 | const Method *method = tuple.method(); |
| 1385 | const Interface *superInterface = tuple.interface(); |
| 1386 | out << "case " |
| 1387 | << method->getSerialId() |
| 1388 | << " /* " |
| 1389 | << method->name() |
| 1390 | << " */:\n{\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1391 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1392 | out.indent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1393 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1394 | status_t err = |
| 1395 | generateStubSourceForMethod(out, superInterface, method); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1396 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1397 | if (err != OK) { |
| 1398 | return err; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1399 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1400 | |
| 1401 | out.unindent(); |
| 1402 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | out << "default:\n{\n"; |
| 1406 | out.indent(); |
| 1407 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1408 | out << "return onTransact(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1409 | |
| 1410 | out.indent(); |
| 1411 | out.indent(); |
| 1412 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1413 | out << "_hidl_code, _hidl_data, _hidl_reply, " |
| 1414 | << "_hidl_flags, _hidl_cb);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1415 | |
| 1416 | out.unindent(); |
| 1417 | out.unindent(); |
| 1418 | |
| 1419 | out.unindent(); |
| 1420 | out << "}\n"; |
| 1421 | |
| 1422 | out.unindent(); |
| 1423 | out << "}\n\n"; |
| 1424 | |
Yifan Hong | a018ed5 | 2016-12-13 16:35:08 -0800 | [diff] [blame] | 1425 | out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] { |
| 1426 | out << "_hidl_err = ::android::hardware::writeToParcel(\n"; |
| 1427 | out.indent(2, [&] { |
| 1428 | out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n"; |
| 1429 | out << "_hidl_reply);\n"; |
| 1430 | }); |
| 1431 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1432 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1433 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1434 | |
| 1435 | out.unindent(); |
| 1436 | out << "}\n\n"; |
| 1437 | |
| 1438 | return OK; |
| 1439 | } |
| 1440 | |
| 1441 | status_t AST::generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1442 | Formatter &out, const Interface *iface, const Method *method) const { |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 1443 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) { |
| 1444 | method->cppImpl(IMPL_STUB, out); |
| 1445 | out << "break;\n"; |
| 1446 | return OK; |
| 1447 | } |
| 1448 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1449 | out << "if (!_hidl_data.enforceInterface(" |
| 1450 | << iface->fullName() |
| 1451 | << "::descriptor)) {\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1452 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1453 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1454 | out << "_hidl_err = ::android::BAD_TYPE;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1455 | out << "break;\n"; |
| 1456 | out.unindent(); |
| 1457 | out << "}\n\n"; |
| 1458 | |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1459 | declareCppReaderLocals(out, method->args(), false /* forResults */); |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1460 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1461 | // First DFS: write buffers |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1462 | for (const auto &arg : method->args()) { |
| 1463 | emitCppReaderWriter( |
| 1464 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1465 | "_hidl_data", |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1466 | false /* parcelObjIsPointer */, |
| 1467 | arg, |
| 1468 | true /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1469 | Type::ErrorMode_Break, |
| 1470 | false /* addPrefixToName */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1473 | // Second DFS: resolve references |
| 1474 | for (const auto &arg : method->args()) { |
| 1475 | emitCppResolveReferences( |
| 1476 | out, |
| 1477 | "_hidl_data", |
| 1478 | false /* parcelObjIsPointer */, |
| 1479 | arg, |
| 1480 | true /* reader */, |
| 1481 | Type::ErrorMode_Break, |
| 1482 | false /* addPrefixToName */); |
| 1483 | } |
| 1484 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1485 | status_t status = generateCppInstrumentationCall( |
| 1486 | out, |
| 1487 | InstrumentationEvent::SERVER_API_ENTRY, |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1488 | method); |
| 1489 | if (status != OK) { |
| 1490 | return status; |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1493 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1494 | const TypedVar *elidedReturn = method->canElideCallback(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1495 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1496 | if (elidedReturn != nullptr) { |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1497 | out << elidedReturn->type().getCppResultType() |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1498 | << " _hidl_out_" |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1499 | << elidedReturn->name() |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1500 | << " = " |
| 1501 | << "_hidl_mImpl->" << method->name() |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1502 | << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1503 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1504 | bool first = true; |
| 1505 | for (const auto &arg : method->args()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1506 | if (!first) { |
| 1507 | out << ", "; |
| 1508 | } |
| 1509 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1510 | if (arg->type().resultNeedsDeref()) { |
| 1511 | out << "*"; |
| 1512 | } |
| 1513 | |
| 1514 | out << arg->name(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1515 | |
| 1516 | first = false; |
| 1517 | } |
| 1518 | |
Steven Moreland | 2ae5bca | 2016-12-01 05:56:49 +0000 | [diff] [blame] | 1519 | out << ");\n\n"; |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1520 | out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), " |
| 1521 | << "_hidl_reply);\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1522 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1523 | elidedReturn->type().emitReaderWriter( |
| 1524 | out, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1525 | "_hidl_out_" + elidedReturn->name(), |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1526 | "_hidl_reply", |
| 1527 | true, /* parcelObjIsPointer */ |
| 1528 | false, /* isReader */ |
| 1529 | Type::ErrorMode_Ignore); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1530 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1531 | emitCppResolveReferences( |
| 1532 | out, |
| 1533 | "_hidl_reply", |
| 1534 | true /* parcelObjIsPointer */, |
| 1535 | elidedReturn, |
| 1536 | false /* reader */, |
| 1537 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1538 | true /* addPrefixToName */); |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1539 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1540 | status_t status = generateCppInstrumentationCall( |
| 1541 | out, |
| 1542 | InstrumentationEvent::SERVER_API_EXIT, |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1543 | method); |
| 1544 | if (status != OK) { |
| 1545 | return status; |
| 1546 | } |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1547 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1548 | out << "_hidl_cb(*_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1549 | } else { |
| 1550 | if (returnsValue) { |
| 1551 | out << "bool _hidl_callbackCalled = false;\n\n"; |
| 1552 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1553 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1554 | out << "_hidl_mImpl->" << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1555 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1556 | bool first = true; |
| 1557 | for (const auto &arg : method->args()) { |
| 1558 | if (!first) { |
| 1559 | out << ", "; |
| 1560 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1561 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1562 | if (arg->type().resultNeedsDeref()) { |
| 1563 | out << "*"; |
| 1564 | } |
| 1565 | |
| 1566 | out << arg->name(); |
| 1567 | |
| 1568 | first = false; |
| 1569 | } |
| 1570 | |
| 1571 | if (returnsValue) { |
| 1572 | if (!first) { |
| 1573 | out << ", "; |
| 1574 | } |
| 1575 | |
| 1576 | out << "[&]("; |
| 1577 | |
| 1578 | first = true; |
| 1579 | for (const auto &arg : method->results()) { |
| 1580 | if (!first) { |
| 1581 | out << ", "; |
| 1582 | } |
| 1583 | |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1584 | out << "const auto &_hidl_out_" << arg->name(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1585 | |
| 1586 | first = false; |
| 1587 | } |
| 1588 | |
| 1589 | out << ") {\n"; |
| 1590 | out.indent(); |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1591 | out << "if (_hidl_callbackCalled) {\n"; |
| 1592 | out.indent(); |
| 1593 | out << "LOG_ALWAYS_FATAL(\"" |
| 1594 | << method->name() |
| 1595 | << ": _hidl_cb called a second time, but must be called once.\");\n"; |
| 1596 | out.unindent(); |
| 1597 | out << "}\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1598 | out << "_hidl_callbackCalled = true;\n\n"; |
| 1599 | |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1600 | out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), " |
| 1601 | << "_hidl_reply);\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1602 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1603 | // First DFS: buffers |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1604 | for (const auto &arg : method->results()) { |
| 1605 | emitCppReaderWriter( |
| 1606 | out, |
| 1607 | "_hidl_reply", |
| 1608 | true /* parcelObjIsPointer */, |
| 1609 | arg, |
| 1610 | false /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1611 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1612 | true /* addPrefixToName */); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1615 | // Second DFS: resolve references |
| 1616 | for (const auto &arg : method->results()) { |
| 1617 | emitCppResolveReferences( |
| 1618 | out, |
| 1619 | "_hidl_reply", |
| 1620 | true /* parcelObjIsPointer */, |
| 1621 | arg, |
| 1622 | false /* reader */, |
| 1623 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1624 | true /* addPrefixToName */); |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1625 | } |
| 1626 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1627 | status_t status = generateCppInstrumentationCall( |
| 1628 | out, |
| 1629 | InstrumentationEvent::SERVER_API_EXIT, |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1630 | method); |
| 1631 | if (status != OK) { |
| 1632 | return status; |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1635 | out << "_hidl_cb(*_hidl_reply);\n"; |
| 1636 | |
| 1637 | out.unindent(); |
Martijn Coenen | 8e4fc84 | 2017-01-09 16:28:59 +0100 | [diff] [blame] | 1638 | out << "});\n\n"; |
| 1639 | } else { |
| 1640 | out << ");\n\n"; |
| 1641 | status_t status = generateCppInstrumentationCall( |
| 1642 | out, |
| 1643 | InstrumentationEvent::SERVER_API_EXIT, |
| 1644 | method); |
| 1645 | if (status != OK) { |
| 1646 | return status; |
| 1647 | } |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1648 | } |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1649 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1650 | if (returnsValue) { |
| 1651 | out << "if (!_hidl_callbackCalled) {\n"; |
| 1652 | out.indent(); |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1653 | out << "LOG_ALWAYS_FATAL(\"" |
| 1654 | << method->name() |
| 1655 | << ": _hidl_cb not called, but must be called once.\");\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1656 | out.unindent(); |
| 1657 | out << "}\n\n"; |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1658 | } else { |
| 1659 | out << "::android::hardware::writeToParcel(" |
| 1660 | << "::android::hardware::Status::ok(), " |
| 1661 | << "_hidl_reply);\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1662 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | out << "break;\n"; |
| 1666 | |
| 1667 | return OK; |
| 1668 | } |
| 1669 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1670 | status_t AST::generatePassthroughHeader(const std::string &outputPath) const { |
| 1671 | std::string ifaceName; |
| 1672 | if (!AST::isInterface(&ifaceName)) { |
| 1673 | // types.hal does not get a stub header. |
| 1674 | return OK; |
| 1675 | } |
| 1676 | |
| 1677 | const Interface *iface = mRootScope->getInterface(); |
| 1678 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1679 | const std::string klassName = iface->getPassthroughName(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1680 | |
| 1681 | bool supportOneway = iface->hasOnewayMethods(); |
| 1682 | |
| 1683 | std::string path = outputPath; |
| 1684 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 1685 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 1686 | path.append(klassName); |
| 1687 | path.append(".h"); |
| 1688 | |
| 1689 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 1690 | FILE *file = fopen(path.c_str(), "w"); |
| 1691 | |
| 1692 | if (file == NULL) { |
| 1693 | return -errno; |
| 1694 | } |
| 1695 | |
| 1696 | Formatter out(file); |
| 1697 | |
| 1698 | const std::string guard = makeHeaderGuard(klassName); |
| 1699 | |
| 1700 | out << "#ifndef " << guard << "\n"; |
| 1701 | out << "#define " << guard << "\n\n"; |
| 1702 | |
| 1703 | std::vector<std::string> packageComponents; |
| 1704 | getPackageAndVersionComponents( |
| 1705 | &packageComponents, false /* cpp_compatible */); |
| 1706 | |
Yifan Hong | b094943 | 2016-12-15 15:32:24 -0800 | [diff] [blame] | 1707 | out << "#include <cutils/trace.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1708 | out << "#include <future>\n"; |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 1709 | |
| 1710 | generateCppPackageInclude(out, mPackage, ifaceName); |
| 1711 | out << "\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1712 | |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 1713 | out << "#include <hidl/HidlPassthroughSupport.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1714 | if (supportOneway) { |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1715 | out << "#include <hidl/TaskRunner.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | enterLeaveNamespace(out, true /* enter */); |
| 1719 | out << "\n"; |
| 1720 | |
| 1721 | out << "struct " |
| 1722 | << klassName |
| 1723 | << " : " << ifaceName |
Steven Moreland | 19d5c17 | 2016-10-20 19:20:25 -0700 | [diff] [blame] | 1724 | << ", ::android::hardware::HidlInstrumentor {\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1725 | |
| 1726 | out.indent(); |
| 1727 | out << "explicit " |
| 1728 | << klassName |
Steven Moreland | c46e984 | 2016-11-02 13:21:26 -0700 | [diff] [blame] | 1729 | << "(const ::android::sp<" |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1730 | << ifaceName |
| 1731 | << "> impl);\n"; |
| 1732 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1733 | status_t err = generateMethods(out, [&](const Method *method, const Interface *) { |
| 1734 | return generatePassthroughMethod(out, method); |
| 1735 | }); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1736 | |
| 1737 | if (err != OK) { |
| 1738 | return err; |
| 1739 | } |
| 1740 | |
| 1741 | out.unindent(); |
| 1742 | out << "private:\n"; |
| 1743 | out.indent(); |
Steven Moreland | c46e984 | 2016-11-02 13:21:26 -0700 | [diff] [blame] | 1744 | out << "const ::android::sp<" << ifaceName << "> mImpl;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1745 | |
| 1746 | if (supportOneway) { |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1747 | out << "::android::hardware::TaskRunner mOnewayQueue;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1748 | |
| 1749 | out << "\n"; |
| 1750 | |
| 1751 | out << "::android::hardware::Return<void> addOnewayTask(" |
| 1752 | "std::function<void(void)>);\n\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | out.unindent(); |
| 1756 | |
| 1757 | out << "};\n\n"; |
| 1758 | |
| 1759 | enterLeaveNamespace(out, false /* enter */); |
| 1760 | |
| 1761 | out << "\n#endif // " << guard << "\n"; |
| 1762 | |
| 1763 | return OK; |
| 1764 | } |
| 1765 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1766 | status_t AST::generateInterfaceSource(Formatter &out) const { |
| 1767 | const Interface *iface = mRootScope->getInterface(); |
| 1768 | |
Yifan Hong | 2d7126b | 2016-10-20 15:12:57 -0700 | [diff] [blame] | 1769 | // generate castFrom functions |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1770 | std::string childTypeResult = iface->getCppResultType(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1771 | |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1772 | for (const Interface *superType : iface->typeChain()) { |
| 1773 | out << "// static \n" |
| 1774 | << childTypeResult |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1775 | << " " |
| 1776 | << iface->localName() |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1777 | << "::castFrom(" |
| 1778 | << superType->getCppArgumentType() |
| 1779 | << " parent) {\n"; |
| 1780 | out.indent(); |
| 1781 | if (iface == superType) { |
| 1782 | out << "return parent;\n"; |
| 1783 | } else { |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1784 | out << "return ::android::hardware::castInterface<"; |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1785 | out << iface->localName() << ", " |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1786 | << superType->fqName().cppName() << ", " |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1787 | << iface->getProxyName() << ", " |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 1788 | << superType->getProxyFqName().cppName() |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1789 | << ">(\n"; |
| 1790 | out.indent(); |
| 1791 | out.indent(); |
| 1792 | out << "parent, \"" |
| 1793 | << iface->fqName().string() |
| 1794 | << "\");\n"; |
| 1795 | out.unindent(); |
| 1796 | out.unindent(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1797 | } |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1798 | out.unindent(); |
| 1799 | out << "}\n\n"; |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | return OK; |
| 1803 | } |
| 1804 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1805 | status_t AST::generatePassthroughSource(Formatter &out) const { |
| 1806 | const Interface *iface = mRootScope->getInterface(); |
| 1807 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1808 | const std::string klassName = iface->getPassthroughName(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1809 | |
| 1810 | out << klassName |
| 1811 | << "::" |
| 1812 | << klassName |
Steven Moreland | c46e984 | 2016-11-02 13:21:26 -0700 | [diff] [blame] | 1813 | << "(const ::android::sp<" |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1814 | << iface->fullName() |
Steven Moreland | 19d5c17 | 2016-10-20 19:20:25 -0700 | [diff] [blame] | 1815 | << "> impl) : ::android::hardware::HidlInstrumentor(\"" |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 1816 | << iface->fqName().string() |
| 1817 | << "\"), mImpl(impl) {"; |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1818 | if (iface->hasOnewayMethods()) { |
| 1819 | out << "\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 1820 | out.indent([&] { |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1821 | out << "mOnewayQueue.setLimit(3000 /* similar limit to binderized */);\n"; |
| 1822 | }); |
| 1823 | } |
| 1824 | out << "}\n\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1825 | |
| 1826 | if (iface->hasOnewayMethods()) { |
| 1827 | out << "::android::hardware::Return<void> " |
| 1828 | << klassName |
| 1829 | << "::addOnewayTask(std::function<void(void)> fun) {\n"; |
| 1830 | out.indent(); |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1831 | out << "if (!mOnewayQueue.push(fun)) {\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1832 | out.indent(); |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 1833 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 1834 | out.indent(); |
| 1835 | out.indent(); |
| 1836 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED);\n"; |
| 1837 | out.unindent(); |
| 1838 | out.unindent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1839 | out.unindent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1840 | out << "}\n"; |
| 1841 | |
Steven Moreland | d366c26 | 2016-10-11 15:29:10 -0700 | [diff] [blame] | 1842 | out << "return ::android::hardware::Status();\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1843 | |
| 1844 | out.unindent(); |
| 1845 | out << "}\n\n"; |
| 1846 | |
| 1847 | |
| 1848 | } |
| 1849 | |
| 1850 | return OK; |
| 1851 | } |
| 1852 | |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1853 | status_t AST::generateCppAtraceCall(Formatter &out, |
| 1854 | InstrumentationEvent event, |
| 1855 | const Method *method) const { |
| 1856 | const Interface *iface = mRootScope->getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1857 | std::string baseString = "HIDL::" + iface->localName() + "::" + method->name(); |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1858 | switch (event) { |
| 1859 | case SERVER_API_ENTRY: |
| 1860 | { |
| 1861 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 1862 | << baseString + "::server\");\n"; |
| 1863 | break; |
| 1864 | } |
| 1865 | case CLIENT_API_ENTRY: |
| 1866 | { |
| 1867 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 1868 | << baseString + "::client\");\n"; |
| 1869 | break; |
| 1870 | } |
| 1871 | case PASSTHROUGH_ENTRY: |
| 1872 | { |
| 1873 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 1874 | << baseString + "::passthrough\");\n"; |
| 1875 | break; |
| 1876 | } |
| 1877 | case SERVER_API_EXIT: |
| 1878 | case CLIENT_API_EXIT: |
| 1879 | case PASSTHROUGH_EXIT: |
| 1880 | { |
| 1881 | out << "atrace_end(ATRACE_TAG_HAL);\n"; |
| 1882 | break; |
| 1883 | } |
| 1884 | default: |
| 1885 | { |
| 1886 | LOG(ERROR) << "Unsupported instrumentation event: " << event; |
| 1887 | return UNKNOWN_ERROR; |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | return OK; |
| 1892 | } |
| 1893 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1894 | status_t AST::generateCppInstrumentationCall( |
| 1895 | Formatter &out, |
| 1896 | InstrumentationEvent event, |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1897 | const Method *method) const { |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1898 | status_t err = generateCppAtraceCall(out, event, method); |
| 1899 | if (err != OK) { |
| 1900 | return err; |
| 1901 | } |
| 1902 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1903 | out << "if (UNLIKELY(mEnableInstrumentation)) {\n"; |
| 1904 | out.indent(); |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1905 | out << "std::vector<void *> _hidl_args;\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1906 | std::string event_str = ""; |
| 1907 | switch (event) { |
| 1908 | case SERVER_API_ENTRY: |
| 1909 | { |
| 1910 | event_str = "InstrumentationEvent::SERVER_API_ENTRY"; |
| 1911 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1912 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1913 | << (arg->type().resultNeedsDeref() ? "" : "&") |
| 1914 | << arg->name() |
| 1915 | << ");\n"; |
| 1916 | } |
| 1917 | break; |
| 1918 | } |
| 1919 | case SERVER_API_EXIT: |
| 1920 | { |
| 1921 | event_str = "InstrumentationEvent::SERVER_API_EXIT"; |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1922 | for (const auto &arg : method->results()) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1923 | out << "_hidl_args.push_back((void *)&_hidl_out_" |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1924 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1925 | << ");\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1926 | } |
| 1927 | break; |
| 1928 | } |
| 1929 | case CLIENT_API_ENTRY: |
| 1930 | { |
| 1931 | event_str = "InstrumentationEvent::CLIENT_API_ENTRY"; |
| 1932 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1933 | out << "_hidl_args.push_back((void *)&" |
| 1934 | << arg->name() |
| 1935 | << ");\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1936 | } |
| 1937 | break; |
| 1938 | } |
| 1939 | case CLIENT_API_EXIT: |
| 1940 | { |
| 1941 | event_str = "InstrumentationEvent::CLIENT_API_EXIT"; |
| 1942 | for (const auto &arg : method->results()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1943 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1944 | << (arg->type().resultNeedsDeref() ? "" : "&") |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1945 | << "_hidl_out_" |
| 1946 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1947 | << ");\n"; |
| 1948 | } |
| 1949 | break; |
| 1950 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 1951 | case PASSTHROUGH_ENTRY: |
| 1952 | { |
| 1953 | event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY"; |
| 1954 | for (const auto &arg : method->args()) { |
| 1955 | out << "_hidl_args.push_back((void *)&" |
| 1956 | << arg->name() |
| 1957 | << ");\n"; |
| 1958 | } |
| 1959 | break; |
| 1960 | } |
| 1961 | case PASSTHROUGH_EXIT: |
| 1962 | { |
| 1963 | event_str = "InstrumentationEvent::PASSTHROUGH_EXIT"; |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 1964 | for (const auto &arg : method->results()) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1965 | out << "_hidl_args.push_back((void *)&_hidl_out_" |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 1966 | << arg->name() |
| 1967 | << ");\n"; |
| 1968 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 1969 | break; |
| 1970 | } |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1971 | default: |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1972 | { |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1973 | LOG(ERROR) << "Unsupported instrumentation event: " << event; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1974 | return UNKNOWN_ERROR; |
| 1975 | } |
| 1976 | } |
| 1977 | |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1978 | const Interface *iface = mRootScope->getInterface(); |
| 1979 | |
Steven Moreland | 1ab3144 | 2016-11-03 18:37:51 -0700 | [diff] [blame] | 1980 | out << "for (const auto &callback: mInstrumentationCallbacks) {\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1981 | out.indent(); |
| 1982 | out << "callback(" |
| 1983 | << event_str |
| 1984 | << ", \"" |
| 1985 | << mPackage.package() |
| 1986 | << "\", \"" |
Yifan Hong | 90ea87f | 2016-11-01 14:25:47 -0700 | [diff] [blame] | 1987 | << mPackage.version() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1988 | << "\", \"" |
| 1989 | << iface->localName() |
| 1990 | << "\", \"" |
| 1991 | << method->name() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1992 | << "\", &_hidl_args);\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1993 | out.unindent(); |
| 1994 | out << "}\n"; |
| 1995 | out.unindent(); |
| 1996 | out << "}\n\n"; |
| 1997 | |
| 1998 | return OK; |
| 1999 | } |
| 2000 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 2001 | } // namespace android |
| 2002 | |