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