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