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