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