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