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