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