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