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