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