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