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