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