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