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