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 | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 17 | #include "AST.h" |
| 18 | |
| 19 | #include "Coordinator.h" |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 20 | #include "Interface.h" |
| 21 | #include "Method.h" |
| 22 | #include "Scope.h" |
| 23 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 24 | #include <hidl-util/Formatter.h> |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 29 | void AST::emitJavaReaderWriter( |
| 30 | Formatter &out, |
| 31 | const std::string &parcelObj, |
| 32 | const TypedVar *arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 33 | bool isReader, |
| 34 | bool addPrefixToName) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 35 | if (isReader) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 36 | out << arg->type().getJavaType() |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 37 | << " " |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 38 | << (addPrefixToName ? "_hidl_out_" : "") |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 39 | << arg->name() |
| 40 | << " = "; |
| 41 | } |
| 42 | |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 43 | arg->type().emitJavaReaderWriter(out, parcelObj, |
| 44 | (addPrefixToName ? "_hidl_out_" : "") + arg->name(), |
| 45 | isReader); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 48 | status_t AST::generateJavaTypes( |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 49 | const std::string &outputPath, const std::string &limitToType) const { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 50 | // Splits types.hal up into one java file per declared type. |
| 51 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 52 | for (const auto &type : mRootScope->getSubTypes()) { |
| 53 | std::string typeName = type->localName(); |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 54 | |
| 55 | if (type->isTypeDef()) { |
| 56 | continue; |
| 57 | } |
| 58 | |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 59 | if (!limitToType.empty() && typeName != limitToType) { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 60 | continue; |
| 61 | } |
| 62 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 63 | std::string path = outputPath; |
| 64 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 65 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */, |
| 66 | true /* sanitized */)); |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 67 | path.append(typeName); |
| 68 | path.append(".java"); |
| 69 | |
| 70 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 71 | FILE *file = fopen(path.c_str(), "w"); |
| 72 | |
| 73 | if (file == NULL) { |
| 74 | return -errno; |
| 75 | } |
| 76 | |
| 77 | Formatter out(file); |
| 78 | |
| 79 | std::vector<std::string> packageComponents; |
| 80 | getPackageAndVersionComponents( |
| 81 | &packageComponents, true /* cpp_compatible */); |
| 82 | |
| 83 | out << "package " << mPackage.javaPackage() << ";\n\n"; |
| 84 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 85 | out << "\n"; |
| 86 | |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 87 | status_t err = |
| 88 | type->emitJavaTypeDeclarations(out, true /* atTopLevel */); |
| 89 | |
| 90 | if (err != OK) { |
| 91 | return err; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return OK; |
| 96 | } |
| 97 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 98 | status_t AST::generateJava( |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 99 | const std::string &outputPath, const std::string &limitToType) const { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 100 | if (!isJavaCompatible()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 101 | fprintf(stderr, |
| 102 | "ERROR: This interface is not Java compatible. The Java backend" |
Andreas Huber | f03332a | 2016-09-22 15:35:43 -0700 | [diff] [blame] | 103 | " does NOT support union types nor native handles. " |
| 104 | "In addition, vectors of arrays are limited to at most " |
Andreas Huber | 86a112b | 2016-10-19 14:25:16 -0700 | [diff] [blame] | 105 | "one-dimensional arrays and vectors of {vectors,interfaces} are" |
| 106 | " not supported.\n"); |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 107 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 108 | return UNKNOWN_ERROR; |
| 109 | } |
| 110 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 111 | std::string ifaceName; |
| 112 | if (!AST::isInterface(&ifaceName)) { |
| 113 | return generateJavaTypes(outputPath, limitToType); |
| 114 | } |
| 115 | |
| 116 | const Interface *iface = mRootScope->getInterface(); |
| 117 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 118 | const std::string baseName = iface->getBaseName(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 119 | |
| 120 | std::string path = outputPath; |
| 121 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 122 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */, |
| 123 | true /* sanitized */)); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 124 | path.append(ifaceName); |
| 125 | path.append(".java"); |
| 126 | |
| 127 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 128 | FILE *file = fopen(path.c_str(), "w"); |
| 129 | |
| 130 | if (file == NULL) { |
| 131 | return -errno; |
| 132 | } |
| 133 | |
| 134 | Formatter out(file); |
| 135 | |
| 136 | std::vector<std::string> packageComponents; |
| 137 | getPackageAndVersionComponents( |
| 138 | &packageComponents, true /* cpp_compatible */); |
| 139 | |
| 140 | out << "package " << mPackage.javaPackage() << ";\n\n"; |
| 141 | |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 142 | out << "import android.os.RemoteException;\n\n"; |
| 143 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 144 | out.setNamespace(mPackage.javaPackage() + "."); |
| 145 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 146 | const Interface *superType = iface->superType(); |
| 147 | |
| 148 | out << "public interface " << ifaceName << " extends "; |
| 149 | |
| 150 | if (superType != NULL) { |
| 151 | out << superType->fullJavaName(); |
| 152 | } else { |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 153 | out << "android.os.IHwInterface"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | out << " {\n"; |
| 157 | out.indent(); |
| 158 | |
| 159 | out << "public static final String kInterfaceName = \"" |
| 160 | << mPackage.string() |
| 161 | << "::" |
| 162 | << ifaceName |
| 163 | << "\";\n\n"; |
| 164 | |
| 165 | out << "public static " |
| 166 | << ifaceName |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 167 | << " asInterface(android.os.IHwBinder binder) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 168 | |
| 169 | out.indent(); |
| 170 | |
| 171 | out << "if (binder == null) {\n"; |
| 172 | out.indent(); |
| 173 | out << "return null;\n"; |
| 174 | out.unindent(); |
| 175 | out << "}\n\n"; |
| 176 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 177 | out << "android.os.IHwInterface iface =\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 178 | out.indent(); |
| 179 | out.indent(); |
| 180 | out << "binder.queryLocalInterface(kInterfaceName);\n\n"; |
| 181 | out.unindent(); |
| 182 | out.unindent(); |
| 183 | |
| 184 | out << "if ((iface != null) && (iface instanceof " |
| 185 | << ifaceName |
| 186 | << ")) {\n"; |
| 187 | |
| 188 | out.indent(); |
| 189 | out << "return (" << ifaceName << ")iface;\n"; |
| 190 | out.unindent(); |
| 191 | out << "}\n\n"; |
| 192 | |
| 193 | out << "return new " << ifaceName << ".Proxy(binder);\n"; |
| 194 | |
| 195 | out.unindent(); |
| 196 | out << "}\n\n"; |
| 197 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 198 | out << "@Override\npublic android.os.IHwBinder asBinder();\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 199 | |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 200 | out << "public static " |
| 201 | << ifaceName |
| 202 | << " getService(String serviceName) {\n"; |
| 203 | |
| 204 | out.indent(); |
| 205 | |
| 206 | out << "return " |
| 207 | << ifaceName |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 208 | << ".asInterface(android.os.HwBinder.getService(\"" |
Steven Moreland | 7e367bf | 2016-11-04 11:31:41 -0700 | [diff] [blame] | 209 | << iface->fqName().string() |
| 210 | << "\",serviceName));\n"; |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 211 | |
| 212 | out.unindent(); |
| 213 | |
| 214 | out << "}\n\n"; |
| 215 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 216 | status_t err = emitJavaTypeDeclarations(out); |
| 217 | |
| 218 | if (err != OK) { |
| 219 | return err; |
| 220 | } |
| 221 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 222 | for (const auto &method : iface->methods()) { |
| 223 | const bool returnsValue = !method->results().empty(); |
| 224 | const bool needsCallback = method->results().size() > 1; |
| 225 | |
| 226 | if (needsCallback) { |
Steven Moreland | bcd7903 | 2016-12-16 20:50:23 -0800 | [diff] [blame] | 227 | out << "\npublic interface " |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 228 | << method->name() |
| 229 | << "Callback {\n"; |
| 230 | |
| 231 | out.indent(); |
| 232 | |
Steven Moreland | bcd7903 | 2016-12-16 20:50:23 -0800 | [diff] [blame] | 233 | out << "public void onValues(" |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 234 | << Method::GetJavaArgSignature(method->results()) |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 235 | << ");\n"; |
| 236 | |
| 237 | out.unindent(); |
| 238 | out << "}\n\n"; |
| 239 | } |
| 240 | |
| 241 | if (returnsValue && !needsCallback) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 242 | out << method->results()[0]->type().getJavaType(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 243 | } else { |
| 244 | out << "void"; |
| 245 | } |
| 246 | |
| 247 | out << " " |
| 248 | << method->name() |
| 249 | << "(" |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 250 | << Method::GetJavaArgSignature(method->args()); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 251 | |
| 252 | if (needsCallback) { |
| 253 | if (!method->args().empty()) { |
| 254 | out << ", "; |
| 255 | } |
| 256 | |
| 257 | out << method->name() |
| 258 | << "Callback cb"; |
| 259 | } |
| 260 | |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 261 | out << ")\n"; |
| 262 | out.indent(); |
| 263 | out << "throws RemoteException;\n"; |
| 264 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | out << "\npublic static final class Proxy implements " |
| 268 | << ifaceName |
| 269 | << " {\n"; |
| 270 | |
| 271 | out.indent(); |
| 272 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 273 | out << "private android.os.IHwBinder mRemote;\n\n"; |
| 274 | out << "public Proxy(android.os.IHwBinder remote) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 275 | out.indent(); |
Yifan Hong | 729e796 | 2016-12-21 16:04:27 -0800 | [diff] [blame] | 276 | out << "mRemote = java.util.Objects.requireNonNull(remote);\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 277 | out.unindent(); |
| 278 | out << "}\n\n"; |
| 279 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 280 | out << "@Override\npublic android.os.IHwBinder asBinder() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 281 | out.indent(); |
| 282 | out << "return mRemote;\n"; |
| 283 | out.unindent(); |
| 284 | out << "}\n\n"; |
| 285 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 286 | const Interface *prevInterface = nullptr; |
| 287 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 288 | const Method *method = tuple.method(); |
| 289 | const Interface *superInterface = tuple.interface(); |
| 290 | if (prevInterface != superInterface) { |
| 291 | out << "// Methods from " |
| 292 | << superInterface->fullName() |
| 293 | << " follow.\n"; |
| 294 | prevInterface = superInterface; |
| 295 | } |
| 296 | const bool returnsValue = !method->results().empty(); |
| 297 | const bool needsCallback = method->results().size() > 1; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 298 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 299 | out << "@Override\npublic "; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 300 | if (returnsValue && !needsCallback) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 301 | out << method->results()[0]->type().getJavaType(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 302 | } else { |
| 303 | out << "void"; |
| 304 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 305 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 306 | out << " " |
| 307 | << method->name() |
| 308 | << "(" |
| 309 | << Method::GetJavaArgSignature(method->args()); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 310 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 311 | if (needsCallback) { |
| 312 | if (!method->args().empty()) { |
| 313 | out << ", "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 316 | out << method->name() |
| 317 | << "Callback cb"; |
| 318 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 319 | |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 320 | out << ")\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 321 | out.indent(); |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 322 | out.indent(); |
| 323 | out << "throws RemoteException {\n"; |
| 324 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 325 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 326 | if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) { |
| 327 | method->javaImpl(IMPL_PROXY, out); |
| 328 | out.unindent(); |
| 329 | out << "}\n"; |
| 330 | continue; |
| 331 | } |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 332 | out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n"; |
| 333 | out << "_hidl_request.writeInterfaceToken(" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 334 | << superInterface->fullJavaName() |
| 335 | << ".kInterfaceName);\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 336 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 337 | for (const auto &arg : method->args()) { |
| 338 | emitJavaReaderWriter( |
| 339 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 340 | "_hidl_request", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 341 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 342 | false /* isReader */, |
| 343 | false /* addPrefixToName */); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 344 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 345 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 346 | out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 347 | << "mRemote.transact(" |
| 348 | << method->getSerialId() |
| 349 | << " /* " |
| 350 | << method->name() |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 351 | << " */, _hidl_request, _hidl_reply, "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 352 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 353 | if (method->isOneway()) { |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 354 | out << "android.os.IHwBinder.FLAG_ONEWAY"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 355 | } else { |
| 356 | out << "0 /* flags */"; |
| 357 | } |
| 358 | |
| 359 | out << ");\n"; |
| 360 | |
| 361 | if (!method->isOneway()) { |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 362 | out << "_hidl_reply.verifySuccess();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 363 | } else { |
| 364 | CHECK(!returnsValue); |
| 365 | } |
| 366 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 367 | out << "_hidl_request.releaseTemporaryStorage();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 368 | |
| 369 | if (returnsValue) { |
| 370 | out << "\n"; |
| 371 | |
| 372 | for (const auto &arg : method->results()) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 373 | emitJavaReaderWriter( |
| 374 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 375 | "_hidl_reply", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 376 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 377 | true /* isReader */, |
| 378 | true /* addPrefixToName */); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 381 | if (needsCallback) { |
| 382 | out << "cb.onValues("; |
Andreas Huber | 8b5da22 | 2016-08-18 14:28:18 -0700 | [diff] [blame] | 383 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 384 | bool firstField = true; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 385 | for (const auto &arg : method->results()) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 386 | if (!firstField) { |
| 387 | out << ", "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 390 | out << "_hidl_out_" << arg->name(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 391 | firstField = false; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 392 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 393 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 394 | out << ");\n"; |
| 395 | } else { |
| 396 | const std::string returnName = method->results()[0]->name(); |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 397 | out << "return _hidl_out_" << returnName << ";\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 398 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 399 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 400 | |
| 401 | out.unindent(); |
| 402 | out << "}\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | out.unindent(); |
| 406 | out << "}\n"; |
| 407 | |
| 408 | //////////////////////////////////////////////////////////////////////////// |
| 409 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 410 | out << "\npublic static abstract class Stub extends android.os.HwBinder " |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 411 | << "implements " |
| 412 | << ifaceName << " {\n"; |
| 413 | |
| 414 | out.indent(); |
| 415 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 416 | out << "@Override\npublic android.os.IHwBinder asBinder() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 417 | out.indent(); |
| 418 | out << "return this;\n"; |
| 419 | out.unindent(); |
| 420 | out << "}\n\n"; |
| 421 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 422 | for (Method *method : iface->hidlReservedMethods()) { |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 423 | // b/32383557 this is a hack. We need to change this if we have more reserved methods. |
| 424 | CHECK_LE(method->results().size(), 1u); |
| 425 | std::string resultType = method->results().size() == 0 ? "void" : |
| 426 | method->results()[0]->type().getJavaType(); |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 427 | out << "@Override\npublic final " |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 428 | << resultType |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 429 | << " " |
| 430 | << method->name() |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 431 | << "(" |
| 432 | << Method::GetJavaArgSignature(method->args()) |
| 433 | << ") {\n"; |
| 434 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 435 | out.indent(); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 436 | method->javaImpl(IMPL_HEADER, out); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 437 | out.unindent(); |
| 438 | out << "\n}\n\n"; |
| 439 | } |
| 440 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 441 | out << "@Override\n" |
| 442 | << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 443 | out.indent(); |
| 444 | // XXX what about potential superClasses? |
| 445 | out << "if (kInterfaceName.equals(descriptor)) {\n"; |
| 446 | out.indent(); |
| 447 | out << "return this;\n"; |
| 448 | out.unindent(); |
| 449 | out << "}\n"; |
| 450 | out << "return null;\n"; |
| 451 | out.unindent(); |
| 452 | out << "}\n\n"; |
| 453 | |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 454 | out << "public void registerAsService(String serviceName) {\n"; |
| 455 | out.indent(); |
| 456 | |
Steven Moreland | 7e367bf | 2016-11-04 11:31:41 -0700 | [diff] [blame] | 457 | out << "registerService(interfaceChain(), serviceName);\n"; |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 458 | |
| 459 | out.unindent(); |
| 460 | out << "}\n\n"; |
| 461 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 462 | out << "@Override\n" |
| 463 | << "public void onTransact(" |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 464 | << "int _hidl_code, " |
| 465 | << "android.os.HwParcel _hidl_request, " |
| 466 | << "final android.os.HwParcel _hidl_reply, " |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 467 | << "int _hidl_flags)\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 468 | out.indent(); |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 469 | out.indent(); |
| 470 | out << "throws RemoteException {\n"; |
| 471 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 472 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 473 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 474 | |
| 475 | out.indent(); |
| 476 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 477 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 478 | const Method *method = tuple.method(); |
| 479 | const Interface *superInterface = tuple.interface(); |
| 480 | const bool returnsValue = !method->results().empty(); |
| 481 | const bool needsCallback = method->results().size() > 1; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 482 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 483 | out << "case " |
| 484 | << method->getSerialId() |
| 485 | << " /* " |
| 486 | << method->name() |
| 487 | << " */:\n{\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 488 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 489 | out.indent(); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 490 | if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) { |
| 491 | method->javaImpl(IMPL_STUB, out); |
| 492 | out.unindent(); |
| 493 | out << "break;\n"; |
| 494 | out << "}\n\n"; |
| 495 | continue; |
| 496 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 497 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 498 | out << "_hidl_request.enforceInterface(" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 499 | << superInterface->fullJavaName() |
| 500 | << ".kInterfaceName);\n\n"; |
| 501 | |
| 502 | for (const auto &arg : method->args()) { |
| 503 | emitJavaReaderWriter( |
| 504 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 505 | "_hidl_request", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 506 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 507 | true /* isReader */, |
| 508 | false /* addPrefixToName */); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | if (!needsCallback && returnsValue) { |
| 512 | const TypedVar *returnArg = method->results()[0]; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 513 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 514 | out << returnArg->type().getJavaType() |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 515 | << " _hidl_out_" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 516 | << returnArg->name() |
| 517 | << " = "; |
| 518 | } |
| 519 | |
| 520 | out << method->name() |
| 521 | << "("; |
| 522 | |
| 523 | bool firstField = true; |
| 524 | for (const auto &arg : method->args()) { |
| 525 | if (!firstField) { |
| 526 | out << ", "; |
| 527 | } |
| 528 | |
| 529 | out << arg->name(); |
| 530 | |
| 531 | firstField = false; |
| 532 | } |
| 533 | |
| 534 | if (needsCallback) { |
| 535 | if (!firstField) { |
| 536 | out << ", "; |
| 537 | } |
| 538 | |
| 539 | out << "new " << method->name() << "Callback() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 540 | out.indent(); |
| 541 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 542 | out << "@Override\n" |
| 543 | << "public void onValues(" |
| 544 | << Method::GetJavaArgSignature(method->results()) |
| 545 | << ") {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 546 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 547 | out.indent(); |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 548 | out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 549 | |
| 550 | for (const auto &arg : method->results()) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 551 | emitJavaReaderWriter( |
| 552 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 553 | "_hidl_reply", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 554 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 555 | false /* isReader */, |
| 556 | false /* addPrefixToName */); |
| 557 | // no need to add _hidl_out because out vars are are scoped |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 560 | out << "_hidl_reply.send();\n" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 561 | << "}}"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 562 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 563 | out.unindent(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 564 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 565 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 566 | |
| 567 | out << ");\n"; |
| 568 | |
| 569 | if (!needsCallback) { |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 570 | out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 571 | |
| 572 | if (returnsValue) { |
| 573 | const TypedVar *returnArg = method->results()[0]; |
| 574 | |
| 575 | emitJavaReaderWriter( |
| 576 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 577 | "_hidl_reply", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 578 | returnArg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 579 | false /* isReader */, |
| 580 | true /* addPrefixToName */); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 583 | out << "_hidl_reply.send();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | out << "break;\n"; |
| 587 | out.unindent(); |
| 588 | out << "}\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | out.unindent(); |
| 592 | out << "}\n"; |
| 593 | |
| 594 | out.unindent(); |
| 595 | out << "}\n"; |
| 596 | |
| 597 | out.unindent(); |
| 598 | out << "}\n"; |
| 599 | |
| 600 | out.unindent(); |
| 601 | out << "}\n"; |
| 602 | |
| 603 | return OK; |
| 604 | } |
| 605 | |
| 606 | status_t AST::emitJavaTypeDeclarations(Formatter &out) const { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 607 | return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | } // namespace android |
| 611 | |