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