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