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