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