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