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