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