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