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