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"; |
Martijn Coenen | b4d7795 | 2017-05-03 13:44:29 -0700 | [diff] [blame] | 1480 | out << "auto prio = ::android::hardware::details::gServicePrioMap.get(" |
| 1481 | << "_hidl_impl, {SCHED_NORMAL, 0});\n"; |
| 1482 | out << "mSchedPolicy = prio.sched_policy;\n"; |
| 1483 | out << "mSchedPriority = prio.prio;\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1484 | out.unindent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1485 | |
| 1486 | out.unindent(); |
| 1487 | out.unindent(); |
| 1488 | out << "}\n\n"; |
| 1489 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1490 | if (iface->isIBase()) { |
Yifan Hong | 01e7cde | 2017-01-09 17:45:45 -0800 | [diff] [blame] | 1491 | // BnHwBase has a constructor to initialize the HidlInstrumentor |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1492 | // class properly. |
| 1493 | out << klassName |
| 1494 | << "::" |
| 1495 | << klassName |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1496 | << "(const ::android::sp<" << interfaceName << "> &_hidl_impl," |
| 1497 | << " const std::string &HidlInstrumentor_package," |
| 1498 | << " const std::string &HidlInstrumentor_interface)\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1499 | |
| 1500 | out.indent(); |
| 1501 | out.indent(); |
| 1502 | |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1503 | out << ": ::android::hardware::details::HidlInstrumentor(" |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1504 | << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1505 | out.indent(); |
| 1506 | out << "_hidl_mImpl = _hidl_impl;\n"; |
| 1507 | out.unindent(); |
| 1508 | |
| 1509 | out.unindent(); |
| 1510 | out.unindent(); |
| 1511 | out << "}\n\n"; |
| 1512 | } |
| 1513 | |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 1514 | status_t err = generateMethods(out, [&](const Method *method, const Interface *) { |
| 1515 | if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) { |
| 1516 | return OK; |
| 1517 | } |
| 1518 | method->generateCppSignature(out, iface->getStubName()); |
| 1519 | out << " "; |
| 1520 | out.block([&] { |
| 1521 | method->cppImpl(IMPL_STUB_IMPL, out); |
| 1522 | }).endl(); |
| 1523 | return OK; |
| 1524 | }); |
Steven Moreland | 6081863 | 2017-02-04 00:33:42 -0800 | [diff] [blame] | 1525 | if (err != OK) { |
| 1526 | return err; |
| 1527 | } |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1528 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1529 | out << "::android::status_t " << klassName << "::onTransact(\n"; |
| 1530 | |
| 1531 | out.indent(); |
| 1532 | out.indent(); |
| 1533 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1534 | out << "uint32_t _hidl_code,\n" |
| 1535 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 1536 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 1537 | << "uint32_t _hidl_flags,\n" |
| 1538 | << "TransactCallback _hidl_cb) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1539 | |
| 1540 | out.unindent(); |
| 1541 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1542 | out << "::android::status_t _hidl_err = ::android::OK;\n\n"; |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1543 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1544 | out.indent(); |
| 1545 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1546 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 1547 | const Method *method = tuple.method(); |
| 1548 | const Interface *superInterface = tuple.interface(); |
| 1549 | out << "case " |
| 1550 | << method->getSerialId() |
| 1551 | << " /* " |
| 1552 | << method->name() |
| 1553 | << " */:\n{\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1554 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1555 | out.indent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1556 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1557 | status_t err = |
| 1558 | generateStubSourceForMethod(out, superInterface, method); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1559 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1560 | if (err != OK) { |
| 1561 | return err; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1562 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1563 | |
| 1564 | out.unindent(); |
| 1565 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | out << "default:\n{\n"; |
| 1569 | out.indent(); |
| 1570 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1571 | if (iface->isIBase()) { |
| 1572 | out << "(void)_hidl_flags;\n"; |
| 1573 | out << "return ::android::UNKNOWN_TRANSACTION;\n"; |
| 1574 | } else { |
| 1575 | out << "return "; |
| 1576 | out << gIBaseFqName.getInterfaceStubFqName().cppName(); |
| 1577 | out << "::onTransact(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1578 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1579 | out.indent(); |
| 1580 | out.indent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1581 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1582 | out << "_hidl_code, _hidl_data, _hidl_reply, " |
| 1583 | << "_hidl_flags, _hidl_cb);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1584 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1585 | out.unindent(); |
| 1586 | out.unindent(); |
| 1587 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1588 | |
| 1589 | out.unindent(); |
| 1590 | out << "}\n"; |
| 1591 | |
| 1592 | out.unindent(); |
| 1593 | out << "}\n\n"; |
| 1594 | |
Yifan Hong | a018ed5 | 2016-12-13 16:35:08 -0800 | [diff] [blame] | 1595 | out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] { |
| 1596 | out << "_hidl_err = ::android::hardware::writeToParcel(\n"; |
| 1597 | out.indent(2, [&] { |
| 1598 | out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n"; |
| 1599 | out << "_hidl_reply);\n"; |
| 1600 | }); |
| 1601 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1602 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1603 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1604 | |
| 1605 | out.unindent(); |
| 1606 | out << "}\n\n"; |
| 1607 | |
| 1608 | return OK; |
| 1609 | } |
| 1610 | |
| 1611 | status_t AST::generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1612 | Formatter &out, const Interface *iface, const Method *method) const { |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 1613 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) { |
| 1614 | method->cppImpl(IMPL_STUB, out); |
| 1615 | out << "break;\n"; |
| 1616 | return OK; |
| 1617 | } |
| 1618 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1619 | out << "if (!_hidl_data.enforceInterface(" |
| 1620 | << iface->fullName() |
| 1621 | << "::descriptor)) {\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1622 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1623 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1624 | out << "_hidl_err = ::android::BAD_TYPE;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1625 | out << "break;\n"; |
| 1626 | out.unindent(); |
| 1627 | out << "}\n\n"; |
| 1628 | |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1629 | declareCppReaderLocals(out, method->args(), false /* forResults */); |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1630 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1631 | // First DFS: write buffers |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1632 | for (const auto &arg : method->args()) { |
| 1633 | emitCppReaderWriter( |
| 1634 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1635 | "_hidl_data", |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1636 | false /* parcelObjIsPointer */, |
| 1637 | arg, |
| 1638 | true /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1639 | Type::ErrorMode_Break, |
| 1640 | false /* addPrefixToName */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1643 | // Second DFS: resolve references |
| 1644 | for (const auto &arg : method->args()) { |
| 1645 | emitCppResolveReferences( |
| 1646 | out, |
| 1647 | "_hidl_data", |
| 1648 | false /* parcelObjIsPointer */, |
| 1649 | arg, |
| 1650 | true /* reader */, |
| 1651 | Type::ErrorMode_Break, |
| 1652 | false /* addPrefixToName */); |
| 1653 | } |
| 1654 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1655 | status_t status = generateCppInstrumentationCall( |
| 1656 | out, |
| 1657 | InstrumentationEvent::SERVER_API_ENTRY, |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1658 | method); |
| 1659 | if (status != OK) { |
| 1660 | return status; |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1663 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1664 | const TypedVar *elidedReturn = method->canElideCallback(); |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 1665 | const std::string callee = |
| 1666 | (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL)) |
| 1667 | ? "this" : "_hidl_mImpl"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1668 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1669 | if (elidedReturn != nullptr) { |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1670 | out << elidedReturn->type().getCppResultType() |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1671 | << " _hidl_out_" |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1672 | << elidedReturn->name() |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1673 | << " = " |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 1674 | << callee << "->" << method->name() |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1675 | << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1676 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1677 | out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1678 | if (arg->type().resultNeedsDeref()) { |
| 1679 | out << "*"; |
| 1680 | } |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1681 | out << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1682 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1683 | |
Steven Moreland | 2ae5bca | 2016-12-01 05:56:49 +0000 | [diff] [blame] | 1684 | out << ");\n\n"; |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1685 | out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), " |
| 1686 | << "_hidl_reply);\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1687 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1688 | elidedReturn->type().emitReaderWriter( |
| 1689 | out, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1690 | "_hidl_out_" + elidedReturn->name(), |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1691 | "_hidl_reply", |
| 1692 | true, /* parcelObjIsPointer */ |
| 1693 | false, /* isReader */ |
| 1694 | Type::ErrorMode_Ignore); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1695 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1696 | emitCppResolveReferences( |
| 1697 | out, |
| 1698 | "_hidl_reply", |
| 1699 | true /* parcelObjIsPointer */, |
| 1700 | elidedReturn, |
| 1701 | false /* reader */, |
| 1702 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1703 | true /* addPrefixToName */); |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1704 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1705 | status_t status = generateCppInstrumentationCall( |
| 1706 | out, |
| 1707 | InstrumentationEvent::SERVER_API_EXIT, |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1708 | method); |
| 1709 | if (status != OK) { |
| 1710 | return status; |
| 1711 | } |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1712 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1713 | out << "_hidl_cb(*_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1714 | } else { |
| 1715 | if (returnsValue) { |
| 1716 | out << "bool _hidl_callbackCalled = false;\n\n"; |
| 1717 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1718 | |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 1719 | out << callee << "->" << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1720 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1721 | out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1722 | if (arg->type().resultNeedsDeref()) { |
| 1723 | out << "*"; |
| 1724 | } |
| 1725 | |
| 1726 | out << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1727 | }); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1728 | |
| 1729 | if (returnsValue) { |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1730 | if (!method->args().empty()) { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1731 | out << ", "; |
| 1732 | } |
| 1733 | |
| 1734 | out << "[&]("; |
| 1735 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1736 | out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1737 | out << "const auto &_hidl_out_" << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1738 | }); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1739 | |
| 1740 | out << ") {\n"; |
| 1741 | out.indent(); |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1742 | out << "if (_hidl_callbackCalled) {\n"; |
| 1743 | out.indent(); |
| 1744 | out << "LOG_ALWAYS_FATAL(\"" |
| 1745 | << method->name() |
| 1746 | << ": _hidl_cb called a second time, but must be called once.\");\n"; |
| 1747 | out.unindent(); |
| 1748 | out << "}\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1749 | out << "_hidl_callbackCalled = true;\n\n"; |
| 1750 | |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1751 | out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), " |
| 1752 | << "_hidl_reply);\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1753 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1754 | // First DFS: buffers |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1755 | for (const auto &arg : method->results()) { |
| 1756 | emitCppReaderWriter( |
| 1757 | out, |
| 1758 | "_hidl_reply", |
| 1759 | true /* parcelObjIsPointer */, |
| 1760 | arg, |
| 1761 | false /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1762 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1763 | true /* addPrefixToName */); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1766 | // Second DFS: resolve references |
| 1767 | for (const auto &arg : method->results()) { |
| 1768 | emitCppResolveReferences( |
| 1769 | out, |
| 1770 | "_hidl_reply", |
| 1771 | true /* parcelObjIsPointer */, |
| 1772 | arg, |
| 1773 | false /* reader */, |
| 1774 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1775 | true /* addPrefixToName */); |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1778 | status_t status = generateCppInstrumentationCall( |
| 1779 | out, |
| 1780 | InstrumentationEvent::SERVER_API_EXIT, |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1781 | method); |
| 1782 | if (status != OK) { |
| 1783 | return status; |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1786 | out << "_hidl_cb(*_hidl_reply);\n"; |
| 1787 | |
| 1788 | out.unindent(); |
Martijn Coenen | 8e4fc84 | 2017-01-09 16:28:59 +0100 | [diff] [blame] | 1789 | out << "});\n\n"; |
| 1790 | } else { |
| 1791 | out << ");\n\n"; |
| 1792 | status_t status = generateCppInstrumentationCall( |
| 1793 | out, |
| 1794 | InstrumentationEvent::SERVER_API_EXIT, |
| 1795 | method); |
| 1796 | if (status != OK) { |
| 1797 | return status; |
| 1798 | } |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1799 | } |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1800 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1801 | if (returnsValue) { |
| 1802 | out << "if (!_hidl_callbackCalled) {\n"; |
| 1803 | out.indent(); |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1804 | out << "LOG_ALWAYS_FATAL(\"" |
| 1805 | << method->name() |
| 1806 | << ": _hidl_cb not called, but must be called once.\");\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1807 | out.unindent(); |
| 1808 | out << "}\n\n"; |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1809 | } else { |
| 1810 | out << "::android::hardware::writeToParcel(" |
| 1811 | << "::android::hardware::Status::ok(), " |
| 1812 | << "_hidl_reply);\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1813 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | out << "break;\n"; |
| 1817 | |
| 1818 | return OK; |
| 1819 | } |
| 1820 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1821 | status_t AST::generatePassthroughHeader(const std::string &outputPath) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1822 | if (!AST::isInterface()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1823 | // types.hal does not get a stub header. |
| 1824 | return OK; |
| 1825 | } |
| 1826 | |
| 1827 | const Interface *iface = mRootScope->getInterface(); |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1828 | CHECK(iface != nullptr); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1829 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1830 | const std::string klassName = iface->getPassthroughName(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1831 | |
| 1832 | bool supportOneway = iface->hasOnewayMethods(); |
| 1833 | |
| 1834 | std::string path = outputPath; |
| 1835 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 1836 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 1837 | path.append(klassName); |
| 1838 | path.append(".h"); |
| 1839 | |
| 1840 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 1841 | FILE *file = fopen(path.c_str(), "w"); |
| 1842 | |
| 1843 | if (file == NULL) { |
| 1844 | return -errno; |
| 1845 | } |
| 1846 | |
| 1847 | Formatter out(file); |
| 1848 | |
| 1849 | const std::string guard = makeHeaderGuard(klassName); |
| 1850 | |
| 1851 | out << "#ifndef " << guard << "\n"; |
| 1852 | out << "#define " << guard << "\n\n"; |
| 1853 | |
| 1854 | std::vector<std::string> packageComponents; |
| 1855 | getPackageAndVersionComponents( |
| 1856 | &packageComponents, false /* cpp_compatible */); |
| 1857 | |
Steven Moreland | 61d3f4b | 2017-04-28 17:30:38 -0700 | [diff] [blame] | 1858 | out << "#include <android-base/macros.h>\n"; |
Yifan Hong | b094943 | 2016-12-15 15:32:24 -0800 | [diff] [blame] | 1859 | out << "#include <cutils/trace.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1860 | out << "#include <future>\n"; |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 1861 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1862 | generateCppPackageInclude(out, mPackage, iface->localName()); |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 1863 | out << "\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1864 | |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 1865 | out << "#include <hidl/HidlPassthroughSupport.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1866 | if (supportOneway) { |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1867 | out << "#include <hidl/TaskRunner.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | enterLeaveNamespace(out, true /* enter */); |
| 1871 | out << "\n"; |
| 1872 | |
| 1873 | out << "struct " |
| 1874 | << klassName |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1875 | << " : " << iface->localName() |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1876 | << ", ::android::hardware::details::HidlInstrumentor {\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1877 | |
| 1878 | out.indent(); |
| 1879 | out << "explicit " |
| 1880 | << klassName |
Steven Moreland | c46e984 | 2016-11-02 13:21:26 -0700 | [diff] [blame] | 1881 | << "(const ::android::sp<" |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1882 | << iface->localName() |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1883 | << "> impl);\n"; |
| 1884 | |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 1885 | out.endl(); |
| 1886 | generateTemplatizationLink(out); |
| 1887 | |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1888 | status_t err = generateMethods(out, [&](const Method *method, const Interface *) { |
| 1889 | return generatePassthroughMethod(out, method); |
| 1890 | }); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1891 | |
| 1892 | if (err != OK) { |
| 1893 | return err; |
| 1894 | } |
| 1895 | |
| 1896 | out.unindent(); |
| 1897 | out << "private:\n"; |
| 1898 | out.indent(); |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1899 | out << "const ::android::sp<" << iface->localName() << "> mImpl;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1900 | |
| 1901 | if (supportOneway) { |
Yifan Hong | ef91d36 | 2017-03-20 17:18:13 -0700 | [diff] [blame] | 1902 | out << "::android::hardware::details::TaskRunner mOnewayQueue;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1903 | |
| 1904 | out << "\n"; |
| 1905 | |
| 1906 | out << "::android::hardware::Return<void> addOnewayTask(" |
| 1907 | "std::function<void(void)>);\n\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | out.unindent(); |
| 1911 | |
| 1912 | out << "};\n\n"; |
| 1913 | |
| 1914 | enterLeaveNamespace(out, false /* enter */); |
| 1915 | |
| 1916 | out << "\n#endif // " << guard << "\n"; |
| 1917 | |
| 1918 | return OK; |
| 1919 | } |
| 1920 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1921 | status_t AST::generateInterfaceSource(Formatter &out) const { |
| 1922 | const Interface *iface = mRootScope->getInterface(); |
| 1923 | |
Yifan Hong | 2d7126b | 2016-10-20 15:12:57 -0700 | [diff] [blame] | 1924 | // generate castFrom functions |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1925 | std::string childTypeResult = iface->getCppResultType(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1926 | |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 1927 | status_t err = generateMethods(out, [&](const Method *method, const Interface *) { |
| 1928 | bool reserved = method->isHidlReserved(); |
| 1929 | |
| 1930 | if (!reserved) { |
| 1931 | out << "// no default implementation for: "; |
| 1932 | } |
| 1933 | method->generateCppSignature(out, iface->localName()); |
| 1934 | if (reserved) { |
| 1935 | out.block([&]() { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 1936 | method->cppImpl(IMPL_INTERFACE, out); |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 1937 | }).endl(); |
| 1938 | } |
| 1939 | |
| 1940 | out << "\n"; |
| 1941 | |
| 1942 | return OK; |
| 1943 | }); |
| 1944 | if (err != OK) { |
| 1945 | return err; |
| 1946 | } |
| 1947 | |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1948 | for (const Interface *superType : iface->typeChain()) { |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 1949 | out << "// static \n::android::hardware::Return<" |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1950 | << childTypeResult |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 1951 | << "> " |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1952 | << iface->localName() |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1953 | << "::castFrom(" |
| 1954 | << superType->getCppArgumentType() |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 1955 | << " parent, bool " |
| 1956 | << (iface == superType ? "/* emitError */" : "emitError") |
| 1957 | << ") {\n"; |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1958 | out.indent(); |
| 1959 | if (iface == superType) { |
| 1960 | out << "return parent;\n"; |
| 1961 | } else { |
Yifan Hong | 33e7801 | 2017-03-13 17:46:33 -0700 | [diff] [blame] | 1962 | out << "return ::android::hardware::details::castInterface<"; |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1963 | out << iface->localName() << ", " |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1964 | << superType->fqName().cppName() << ", " |
Steven Moreland | a8559fc | 2017-07-21 19:13:06 +0000 | [diff] [blame] | 1965 | << iface->getProxyName() << ", " |
| 1966 | << superType->getProxyFqName().cppName() |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1967 | << ">(\n"; |
| 1968 | out.indent(); |
| 1969 | out.indent(); |
| 1970 | out << "parent, \"" |
| 1971 | << iface->fqName().string() |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 1972 | << "\", emitError);\n"; |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1973 | out.unindent(); |
| 1974 | out.unindent(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1975 | } |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1976 | out.unindent(); |
| 1977 | out << "}\n\n"; |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | return OK; |
| 1981 | } |
| 1982 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1983 | status_t AST::generatePassthroughSource(Formatter &out) const { |
| 1984 | const Interface *iface = mRootScope->getInterface(); |
| 1985 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1986 | const std::string klassName = iface->getPassthroughName(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1987 | |
| 1988 | out << klassName |
| 1989 | << "::" |
| 1990 | << klassName |
Steven Moreland | c46e984 | 2016-11-02 13:21:26 -0700 | [diff] [blame] | 1991 | << "(const ::android::sp<" |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1992 | << iface->fullName() |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1993 | << "> impl) : ::android::hardware::details::HidlInstrumentor(\"" |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1994 | << mPackage.string() |
| 1995 | << "\", \"" |
| 1996 | << iface->localName() |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 1997 | << "\"), mImpl(impl) {"; |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1998 | if (iface->hasOnewayMethods()) { |
| 1999 | out << "\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 2000 | out.indent([&] { |
Yifan Hong | f01dad4 | 2017-03-20 19:03:11 -0700 | [diff] [blame] | 2001 | out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n"; |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 2002 | }); |
| 2003 | } |
| 2004 | out << "}\n\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 2005 | |
| 2006 | if (iface->hasOnewayMethods()) { |
| 2007 | out << "::android::hardware::Return<void> " |
| 2008 | << klassName |
| 2009 | << "::addOnewayTask(std::function<void(void)> fun) {\n"; |
| 2010 | out.indent(); |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 2011 | out << "if (!mOnewayQueue.push(fun)) {\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 2012 | out.indent(); |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 2013 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 2014 | out.indent(); |
| 2015 | out.indent(); |
Steven Moreland | 610002f | 2017-06-16 13:02:49 -0700 | [diff] [blame] | 2016 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n" |
| 2017 | << "\"Passthrough oneway function queue exceeds maximum size.\");\n"; |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 2018 | out.unindent(); |
| 2019 | out.unindent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 2020 | out.unindent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 2021 | out << "}\n"; |
| 2022 | |
Steven Moreland | d366c26 | 2016-10-11 15:29:10 -0700 | [diff] [blame] | 2023 | out << "return ::android::hardware::Status();\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 2024 | |
| 2025 | out.unindent(); |
| 2026 | out << "}\n\n"; |
| 2027 | |
| 2028 | |
| 2029 | } |
| 2030 | |
| 2031 | return OK; |
| 2032 | } |
| 2033 | |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 2034 | status_t AST::generateCppAtraceCall(Formatter &out, |
| 2035 | InstrumentationEvent event, |
| 2036 | const Method *method) const { |
| 2037 | const Interface *iface = mRootScope->getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 2038 | std::string baseString = "HIDL::" + iface->localName() + "::" + method->name(); |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 2039 | switch (event) { |
| 2040 | case SERVER_API_ENTRY: |
| 2041 | { |
| 2042 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 2043 | << baseString + "::server\");\n"; |
| 2044 | break; |
| 2045 | } |
| 2046 | case CLIENT_API_ENTRY: |
| 2047 | { |
| 2048 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 2049 | << baseString + "::client\");\n"; |
| 2050 | break; |
| 2051 | } |
| 2052 | case PASSTHROUGH_ENTRY: |
| 2053 | { |
| 2054 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 2055 | << baseString + "::passthrough\");\n"; |
| 2056 | break; |
| 2057 | } |
| 2058 | case SERVER_API_EXIT: |
| 2059 | case CLIENT_API_EXIT: |
| 2060 | case PASSTHROUGH_EXIT: |
| 2061 | { |
| 2062 | out << "atrace_end(ATRACE_TAG_HAL);\n"; |
| 2063 | break; |
| 2064 | } |
| 2065 | default: |
| 2066 | { |
| 2067 | LOG(ERROR) << "Unsupported instrumentation event: " << event; |
| 2068 | return UNKNOWN_ERROR; |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | return OK; |
| 2073 | } |
| 2074 | |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2075 | status_t AST::generateCppInstrumentationCall( |
| 2076 | Formatter &out, |
| 2077 | InstrumentationEvent event, |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 2078 | const Method *method) const { |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 2079 | status_t err = generateCppAtraceCall(out, event, method); |
| 2080 | if (err != OK) { |
| 2081 | return err; |
| 2082 | } |
| 2083 | |
Steven Moreland | 30b76e9 | 2017-06-02 18:52:24 -0700 | [diff] [blame] | 2084 | out << "#ifdef __ANDROID_DEBUGGABLE__\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2085 | out << "if (UNLIKELY(mEnableInstrumentation)) {\n"; |
| 2086 | out.indent(); |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 2087 | out << "std::vector<void *> _hidl_args;\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2088 | std::string event_str = ""; |
| 2089 | switch (event) { |
| 2090 | case SERVER_API_ENTRY: |
| 2091 | { |
| 2092 | event_str = "InstrumentationEvent::SERVER_API_ENTRY"; |
| 2093 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 2094 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2095 | << (arg->type().resultNeedsDeref() ? "" : "&") |
| 2096 | << arg->name() |
| 2097 | << ");\n"; |
| 2098 | } |
| 2099 | break; |
| 2100 | } |
| 2101 | case SERVER_API_EXIT: |
| 2102 | { |
| 2103 | event_str = "InstrumentationEvent::SERVER_API_EXIT"; |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 2104 | for (const auto &arg : method->results()) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 2105 | out << "_hidl_args.push_back((void *)&_hidl_out_" |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 2106 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2107 | << ");\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2108 | } |
| 2109 | break; |
| 2110 | } |
| 2111 | case CLIENT_API_ENTRY: |
| 2112 | { |
| 2113 | event_str = "InstrumentationEvent::CLIENT_API_ENTRY"; |
| 2114 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 2115 | out << "_hidl_args.push_back((void *)&" |
| 2116 | << arg->name() |
| 2117 | << ");\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2118 | } |
| 2119 | break; |
| 2120 | } |
| 2121 | case CLIENT_API_EXIT: |
| 2122 | { |
| 2123 | event_str = "InstrumentationEvent::CLIENT_API_EXIT"; |
| 2124 | for (const auto &arg : method->results()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 2125 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2126 | << (arg->type().resultNeedsDeref() ? "" : "&") |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 2127 | << "_hidl_out_" |
| 2128 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2129 | << ");\n"; |
| 2130 | } |
| 2131 | break; |
| 2132 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 2133 | case PASSTHROUGH_ENTRY: |
| 2134 | { |
| 2135 | event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY"; |
| 2136 | for (const auto &arg : method->args()) { |
| 2137 | out << "_hidl_args.push_back((void *)&" |
| 2138 | << arg->name() |
| 2139 | << ");\n"; |
| 2140 | } |
| 2141 | break; |
| 2142 | } |
| 2143 | case PASSTHROUGH_EXIT: |
| 2144 | { |
| 2145 | event_str = "InstrumentationEvent::PASSTHROUGH_EXIT"; |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 2146 | for (const auto &arg : method->results()) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 2147 | out << "_hidl_args.push_back((void *)&_hidl_out_" |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 2148 | << arg->name() |
| 2149 | << ");\n"; |
| 2150 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 2151 | break; |
| 2152 | } |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 2153 | default: |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2154 | { |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 2155 | LOG(ERROR) << "Unsupported instrumentation event: " << event; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2156 | return UNKNOWN_ERROR; |
| 2157 | } |
| 2158 | } |
| 2159 | |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 2160 | const Interface *iface = mRootScope->getInterface(); |
| 2161 | |
Steven Moreland | 1ab3144 | 2016-11-03 18:37:51 -0700 | [diff] [blame] | 2162 | out << "for (const auto &callback: mInstrumentationCallbacks) {\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2163 | out.indent(); |
| 2164 | out << "callback(" |
| 2165 | << event_str |
| 2166 | << ", \"" |
| 2167 | << mPackage.package() |
| 2168 | << "\", \"" |
Yifan Hong | 90ea87f | 2016-11-01 14:25:47 -0700 | [diff] [blame] | 2169 | << mPackage.version() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2170 | << "\", \"" |
| 2171 | << iface->localName() |
| 2172 | << "\", \"" |
| 2173 | << method->name() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 2174 | << "\", &_hidl_args);\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2175 | out.unindent(); |
| 2176 | out << "}\n"; |
| 2177 | out.unindent(); |
Steven Moreland | 30b76e9 | 2017-06-02 18:52:24 -0700 | [diff] [blame] | 2178 | out << "}\n"; |
| 2179 | out << "#endif // __ANDROID_DEBUGGABLE__\n\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 2180 | |
| 2181 | return OK; |
| 2182 | } |
| 2183 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 2184 | } // namespace android |