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 |
Steven Moreland | 64af5e8 | 2017-01-04 10:58:55 -0800 | [diff] [blame] | 202 | << " getService(String serviceName) throws RemoteException {\n"; |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 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 | |
Chris Phoenix | db0d634 | 2017-01-11 16:10:00 -0800 | [diff] [blame] | 216 | out << "public static " |
| 217 | << ifaceName |
| 218 | << " getService() throws RemoteException {\n"; |
| 219 | |
| 220 | out.indent(); |
| 221 | |
| 222 | out << "return " |
| 223 | << ifaceName |
| 224 | << ".asInterface(android.os.HwBinder.getService(\"" |
| 225 | << iface->fqName().string() |
| 226 | << "\",\"default\"));\n"; |
| 227 | |
| 228 | out.unindent(); |
| 229 | |
| 230 | out << "}\n\n"; |
| 231 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 232 | status_t err = emitJavaTypeDeclarations(out); |
| 233 | |
| 234 | if (err != OK) { |
| 235 | return err; |
| 236 | } |
| 237 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 238 | for (const auto &method : iface->methods()) { |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 239 | if (method->isHiddenFromJava()) { |
| 240 | continue; |
| 241 | } |
| 242 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 243 | const bool returnsValue = !method->results().empty(); |
| 244 | const bool needsCallback = method->results().size() > 1; |
| 245 | |
| 246 | if (needsCallback) { |
Steven Moreland | bcd7903 | 2016-12-16 20:50:23 -0800 | [diff] [blame] | 247 | out << "\npublic interface " |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 248 | << method->name() |
| 249 | << "Callback {\n"; |
| 250 | |
| 251 | out.indent(); |
| 252 | |
Steven Moreland | bcd7903 | 2016-12-16 20:50:23 -0800 | [diff] [blame] | 253 | out << "public void onValues(" |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 254 | << Method::GetJavaArgSignature(method->results()) |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 255 | << ");\n"; |
| 256 | |
| 257 | out.unindent(); |
| 258 | out << "}\n\n"; |
| 259 | } |
| 260 | |
| 261 | if (returnsValue && !needsCallback) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 262 | out << method->results()[0]->type().getJavaType(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 263 | } else { |
| 264 | out << "void"; |
| 265 | } |
| 266 | |
| 267 | out << " " |
| 268 | << method->name() |
| 269 | << "(" |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 270 | << Method::GetJavaArgSignature(method->args()); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 271 | |
| 272 | if (needsCallback) { |
| 273 | if (!method->args().empty()) { |
| 274 | out << ", "; |
| 275 | } |
| 276 | |
| 277 | out << method->name() |
| 278 | << "Callback cb"; |
| 279 | } |
| 280 | |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 281 | out << ")\n"; |
| 282 | out.indent(); |
| 283 | out << "throws RemoteException;\n"; |
| 284 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | out << "\npublic static final class Proxy implements " |
| 288 | << ifaceName |
| 289 | << " {\n"; |
| 290 | |
| 291 | out.indent(); |
| 292 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 293 | out << "private android.os.IHwBinder mRemote;\n\n"; |
| 294 | out << "public Proxy(android.os.IHwBinder remote) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 295 | out.indent(); |
Yifan Hong | 729e796 | 2016-12-21 16:04:27 -0800 | [diff] [blame] | 296 | out << "mRemote = java.util.Objects.requireNonNull(remote);\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 297 | out.unindent(); |
| 298 | out << "}\n\n"; |
| 299 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 300 | out << "@Override\npublic android.os.IHwBinder asBinder() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 301 | out.indent(); |
| 302 | out << "return mRemote;\n"; |
| 303 | out.unindent(); |
| 304 | out << "}\n\n"; |
| 305 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 306 | const Interface *prevInterface = nullptr; |
| 307 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 308 | const Method *method = tuple.method(); |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 309 | |
| 310 | if (method->isHiddenFromJava()) { |
| 311 | continue; |
| 312 | } |
| 313 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 314 | const Interface *superInterface = tuple.interface(); |
| 315 | if (prevInterface != superInterface) { |
| 316 | out << "// Methods from " |
| 317 | << superInterface->fullName() |
| 318 | << " follow.\n"; |
| 319 | prevInterface = superInterface; |
| 320 | } |
| 321 | const bool returnsValue = !method->results().empty(); |
| 322 | const bool needsCallback = method->results().size() > 1; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 323 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 324 | out << "@Override\npublic "; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 325 | if (returnsValue && !needsCallback) { |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 326 | out << method->results()[0]->type().getJavaType(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 327 | } else { |
| 328 | out << "void"; |
| 329 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 330 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 331 | out << " " |
| 332 | << method->name() |
| 333 | << "(" |
| 334 | << Method::GetJavaArgSignature(method->args()); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 335 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 336 | if (needsCallback) { |
| 337 | if (!method->args().empty()) { |
| 338 | out << ", "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 341 | out << method->name() |
| 342 | << "Callback cb"; |
| 343 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 344 | |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 345 | out << ")\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 346 | out.indent(); |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 347 | out.indent(); |
| 348 | out << "throws RemoteException {\n"; |
| 349 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 350 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 351 | if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_PROXY)) { |
| 352 | method->javaImpl(IMPL_PROXY, out); |
| 353 | out.unindent(); |
| 354 | out << "}\n"; |
| 355 | continue; |
| 356 | } |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 357 | out << "android.os.HwParcel _hidl_request = new android.os.HwParcel();\n"; |
| 358 | out << "_hidl_request.writeInterfaceToken(" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 359 | << superInterface->fullJavaName() |
| 360 | << ".kInterfaceName);\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 361 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 362 | for (const auto &arg : method->args()) { |
| 363 | emitJavaReaderWriter( |
| 364 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 365 | "_hidl_request", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 366 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 367 | false /* isReader */, |
| 368 | false /* addPrefixToName */); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 369 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 370 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 371 | out << "\nandroid.os.HwParcel _hidl_reply = new android.os.HwParcel();\n" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 372 | << "mRemote.transact(" |
| 373 | << method->getSerialId() |
| 374 | << " /* " |
| 375 | << method->name() |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 376 | << " */, _hidl_request, _hidl_reply, "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 377 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 378 | if (method->isOneway()) { |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 379 | out << "android.os.IHwBinder.FLAG_ONEWAY"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 380 | } else { |
| 381 | out << "0 /* flags */"; |
| 382 | } |
| 383 | |
| 384 | out << ");\n"; |
| 385 | |
| 386 | if (!method->isOneway()) { |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 387 | out << "_hidl_reply.verifySuccess();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 388 | } else { |
| 389 | CHECK(!returnsValue); |
| 390 | } |
| 391 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 392 | out << "_hidl_request.releaseTemporaryStorage();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 393 | |
| 394 | if (returnsValue) { |
| 395 | out << "\n"; |
| 396 | |
| 397 | for (const auto &arg : method->results()) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 398 | emitJavaReaderWriter( |
| 399 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 400 | "_hidl_reply", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 401 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 402 | true /* isReader */, |
| 403 | true /* addPrefixToName */); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 406 | if (needsCallback) { |
| 407 | out << "cb.onValues("; |
Andreas Huber | 8b5da22 | 2016-08-18 14:28:18 -0700 | [diff] [blame] | 408 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 409 | bool firstField = true; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 410 | for (const auto &arg : method->results()) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 411 | if (!firstField) { |
| 412 | out << ", "; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 415 | out << "_hidl_out_" << arg->name(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 416 | firstField = false; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 417 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 418 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 419 | out << ");\n"; |
| 420 | } else { |
| 421 | const std::string returnName = method->results()[0]->name(); |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 422 | out << "return _hidl_out_" << returnName << ";\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 423 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 424 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 425 | |
| 426 | out.unindent(); |
| 427 | out << "}\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | out.unindent(); |
| 431 | out << "}\n"; |
| 432 | |
| 433 | //////////////////////////////////////////////////////////////////////////// |
| 434 | |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 435 | out << "\npublic static abstract class Stub extends android.os.HwBinder " |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 436 | << "implements " |
| 437 | << ifaceName << " {\n"; |
| 438 | |
| 439 | out.indent(); |
| 440 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 441 | out << "@Override\npublic android.os.IHwBinder asBinder() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 442 | out.indent(); |
| 443 | out << "return this;\n"; |
| 444 | out.unindent(); |
| 445 | out << "}\n\n"; |
| 446 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 447 | for (Method *method : iface->hidlReservedMethods()) { |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 448 | if (method->isHiddenFromJava()) { |
| 449 | continue; |
| 450 | } |
| 451 | |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 452 | // b/32383557 this is a hack. We need to change this if we have more reserved methods. |
| 453 | CHECK_LE(method->results().size(), 1u); |
| 454 | std::string resultType = method->results().size() == 0 ? "void" : |
| 455 | method->results()[0]->type().getJavaType(); |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 456 | out << "@Override\npublic final " |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 457 | << resultType |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 458 | << " " |
| 459 | << method->name() |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 460 | << "(" |
| 461 | << Method::GetJavaArgSignature(method->args()) |
| 462 | << ") {\n"; |
| 463 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 464 | out.indent(); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 465 | method->javaImpl(IMPL_HEADER, out); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 466 | out.unindent(); |
| 467 | out << "\n}\n\n"; |
| 468 | } |
| 469 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 470 | out << "@Override\n" |
| 471 | << "public android.os.IHwInterface queryLocalInterface(String descriptor) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 472 | out.indent(); |
| 473 | // XXX what about potential superClasses? |
| 474 | out << "if (kInterfaceName.equals(descriptor)) {\n"; |
| 475 | out.indent(); |
| 476 | out << "return this;\n"; |
| 477 | out.unindent(); |
| 478 | out << "}\n"; |
| 479 | out << "return null;\n"; |
| 480 | out.unindent(); |
| 481 | out << "}\n\n"; |
| 482 | |
Steven Moreland | 64af5e8 | 2017-01-04 10:58:55 -0800 | [diff] [blame] | 483 | out << "public void registerAsService(String serviceName) throws RemoteException {\n"; |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 484 | out.indent(); |
| 485 | |
Steven Moreland | 7e367bf | 2016-11-04 11:31:41 -0700 | [diff] [blame] | 486 | out << "registerService(interfaceChain(), serviceName);\n"; |
Andreas Huber | 2bb6e1e | 2016-10-25 13:26:43 -0700 | [diff] [blame] | 487 | |
| 488 | out.unindent(); |
| 489 | out << "}\n\n"; |
| 490 | |
Yifan Hong | 084d11f | 2016-12-21 15:33:43 -0800 | [diff] [blame] | 491 | out << "@Override\n" |
| 492 | << "public void onTransact(" |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 493 | << "int _hidl_code, " |
| 494 | << "android.os.HwParcel _hidl_request, " |
| 495 | << "final android.os.HwParcel _hidl_reply, " |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 496 | << "int _hidl_flags)\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 497 | out.indent(); |
Steven Moreland | dad1b30 | 2016-12-20 15:56:00 -0800 | [diff] [blame] | 498 | out.indent(); |
| 499 | out << "throws RemoteException {\n"; |
| 500 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 501 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 502 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 503 | |
| 504 | out.indent(); |
| 505 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 506 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 507 | const Method *method = tuple.method(); |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 508 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 509 | const Interface *superInterface = tuple.interface(); |
| 510 | const bool returnsValue = !method->results().empty(); |
| 511 | const bool needsCallback = method->results().size() > 1; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 512 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 513 | out << "case " |
| 514 | << method->getSerialId() |
| 515 | << " /* " |
| 516 | << method->name() |
| 517 | << " */:\n{\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 518 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 519 | out.indent(); |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 520 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 521 | if (method->isHidlReserved() && method->overridesJavaImpl(IMPL_STUB)) { |
| 522 | method->javaImpl(IMPL_STUB, out); |
| 523 | out.unindent(); |
| 524 | out << "break;\n"; |
| 525 | out << "}\n\n"; |
| 526 | continue; |
| 527 | } |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 528 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 529 | out << "_hidl_request.enforceInterface(" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 530 | << superInterface->fullJavaName() |
| 531 | << ".kInterfaceName);\n\n"; |
| 532 | |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 533 | if (method->isHiddenFromJava()) { |
| 534 | // This is a method hidden from the Java side of things, it must not |
| 535 | // return any value and will simply signal success. |
| 536 | CHECK(!returnsValue); |
| 537 | |
| 538 | out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n"; |
| 539 | out << "_hidl_reply.send();\n"; |
| 540 | out << "break;\n"; |
| 541 | out.unindent(); |
| 542 | out << "}\n\n"; |
| 543 | continue; |
| 544 | } |
| 545 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 546 | for (const auto &arg : method->args()) { |
| 547 | emitJavaReaderWriter( |
| 548 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 549 | "_hidl_request", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 550 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 551 | true /* isReader */, |
| 552 | false /* addPrefixToName */); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | if (!needsCallback && returnsValue) { |
| 556 | const TypedVar *returnArg = method->results()[0]; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 557 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 558 | out << returnArg->type().getJavaType() |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 559 | << " _hidl_out_" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 560 | << returnArg->name() |
| 561 | << " = "; |
| 562 | } |
| 563 | |
| 564 | out << method->name() |
| 565 | << "("; |
| 566 | |
| 567 | bool firstField = true; |
| 568 | for (const auto &arg : method->args()) { |
| 569 | if (!firstField) { |
| 570 | out << ", "; |
| 571 | } |
| 572 | |
| 573 | out << arg->name(); |
| 574 | |
| 575 | firstField = false; |
| 576 | } |
| 577 | |
| 578 | if (needsCallback) { |
| 579 | if (!firstField) { |
| 580 | out << ", "; |
| 581 | } |
| 582 | |
| 583 | out << "new " << method->name() << "Callback() {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 584 | out.indent(); |
| 585 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 586 | out << "@Override\n" |
| 587 | << "public void onValues(" |
| 588 | << Method::GetJavaArgSignature(method->results()) |
| 589 | << ") {\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 590 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 591 | out.indent(); |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 592 | out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 593 | |
| 594 | for (const auto &arg : method->results()) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 595 | emitJavaReaderWriter( |
| 596 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 597 | "_hidl_reply", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 598 | arg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 599 | false /* isReader */, |
| 600 | false /* addPrefixToName */); |
| 601 | // no need to add _hidl_out because out vars are are scoped |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 604 | out << "_hidl_reply.send();\n" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 605 | << "}}"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 606 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 607 | out.unindent(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 608 | out.unindent(); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 609 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 610 | |
| 611 | out << ");\n"; |
| 612 | |
Martijn Coenen | 0bb0d41 | 2017-02-08 10:21:30 +0100 | [diff] [blame] | 613 | if (!needsCallback && !method->isOneway()) { |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 614 | out << "_hidl_reply.writeStatus(android.os.HwParcel.STATUS_SUCCESS);\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 615 | |
| 616 | if (returnsValue) { |
| 617 | const TypedVar *returnArg = method->results()[0]; |
| 618 | |
| 619 | emitJavaReaderWriter( |
| 620 | out, |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 621 | "_hidl_reply", |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 622 | returnArg, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 623 | false /* isReader */, |
| 624 | true /* addPrefixToName */); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Steven Moreland | 4b39bc1 | 2016-11-16 10:42:24 -0800 | [diff] [blame] | 627 | out << "_hidl_reply.send();\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | out << "break;\n"; |
| 631 | out.unindent(); |
| 632 | out << "}\n\n"; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | out.unindent(); |
| 636 | out << "}\n"; |
| 637 | |
| 638 | out.unindent(); |
| 639 | out << "}\n"; |
| 640 | |
| 641 | out.unindent(); |
| 642 | out << "}\n"; |
| 643 | |
| 644 | out.unindent(); |
| 645 | out << "}\n"; |
| 646 | |
| 647 | return OK; |
| 648 | } |
| 649 | |
| 650 | status_t AST::emitJavaTypeDeclarations(Formatter &out) const { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 651 | return mRootScope->emitJavaTypeDeclarations(out, false /* atTopLevel */); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | } // namespace android |