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