Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018, 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 | |
| 17 | #include "generate_ndk.h" |
| 18 | |
| 19 | #include "aidl_language.h" |
| 20 | #include "aidl_to_cpp_common.h" |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 21 | #include "aidl_to_ndk.h" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace aidl { |
| 27 | namespace ndk { |
| 28 | |
| 29 | using namespace internals; |
| 30 | using cpp::ClassNames; |
| 31 | |
| 32 | void GenerateNdkInterface(const string& output_file, const Options& options, |
| 33 | const AidlTypenames& types, const AidlInterface& defined_type, |
| 34 | const IoDelegate& io_delegate) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 35 | const string i_header = |
| 36 | options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::INTERFACE); |
| 37 | unique_ptr<CodeWriter> i_writer(io_delegate.GetCodeWriter(i_header)); |
| 38 | GenerateInterfaceHeader(*i_writer, types, defined_type, options); |
| 39 | CHECK(i_writer->Close()); |
| 40 | |
| 41 | const string bp_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::CLIENT); |
| 42 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 43 | GenerateClientHeader(*bp_writer, types, defined_type, options); |
| 44 | CHECK(bp_writer->Close()); |
| 45 | |
| 46 | const string bn_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::SERVER); |
| 47 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 48 | GenerateServerHeader(*bn_writer, types, defined_type, options); |
| 49 | CHECK(bn_writer->Close()); |
| 50 | |
| 51 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
| 52 | GenerateSource(*source_writer, types, defined_type, options); |
| 53 | CHECK(source_writer->Close()); |
| 54 | } |
| 55 | |
| 56 | void GenerateNdkParcel(const string& output_file, const Options& options, |
| 57 | const AidlTypenames& types, const AidlStructuredParcelable& defined_type, |
| 58 | const IoDelegate& io_delegate) { |
| 59 | const string header_path = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::BASE); |
| 60 | unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path)); |
| 61 | GenerateParcelHeader(*header_writer, types, defined_type, options); |
| 62 | CHECK(header_writer->Close()); |
| 63 | |
| 64 | const string bp_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::CLIENT); |
| 65 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 66 | *bp_writer << "#error TODO(b/111362593) defined_types do not have bp classes\n"; |
| 67 | CHECK(bp_writer->Close()); |
| 68 | |
| 69 | const string bn_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::SERVER); |
| 70 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 71 | *bn_writer << "#error TODO(b/111362593) defined_types do not have bn classes\n"; |
| 72 | CHECK(bn_writer->Close()); |
| 73 | |
| 74 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
| 75 | GenerateParcelSource(*source_writer, types, defined_type, options); |
| 76 | CHECK(source_writer->Close()); |
| 77 | } |
| 78 | |
| 79 | void GenerateNdk(const string& output_file, const Options& options, const AidlTypenames& types, |
| 80 | const AidlDefinedType& defined_type, const IoDelegate& io_delegate) { |
| 81 | const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable(); |
| 82 | if (parcelable != nullptr) { |
| 83 | GenerateNdkParcel(output_file, options, types, *parcelable, io_delegate); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | const AidlInterface* interface = defined_type.AsInterface(); |
| 88 | if (interface != nullptr) { |
| 89 | GenerateNdkInterface(output_file, options, types, *interface, io_delegate); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | CHECK(false) << "Unrecognized type sent for cpp generation."; |
| 94 | } |
| 95 | namespace internals { |
| 96 | |
| 97 | void EnterNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) { |
| 98 | out << "namespace aidl {\n"; |
| 99 | cpp::EnterNamespace(out, defined_type); |
| 100 | } |
| 101 | void LeaveNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) { |
| 102 | cpp::LeaveNamespace(out, defined_type); |
| 103 | out << "} // namespace aidl\n"; |
| 104 | } |
| 105 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 106 | static void StatusCheckGoto(CodeWriter& out) { |
| 107 | out << "if (_aidl_ret_status != STATUS_OK) goto _aidl_error;\n\n"; |
| 108 | } |
| 109 | static void StatusCheckBreak(CodeWriter& out) { |
| 110 | out << "if (_aidl_ret_status != STATUS_OK) break;\n\n"; |
| 111 | } |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 112 | static void StatusCheckReturn(CodeWriter& out) { |
| 113 | out << "if (_aidl_ret_status != STATUS_OK) return _aidl_ret_status;\n\n"; |
| 114 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 115 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 116 | static void GenerateHeaderIncludes(CodeWriter& out, const AidlTypenames& types, |
| 117 | const AidlDefinedType& defined_type) { |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 118 | out << "#include <android/binder_parcel_utils.h>\n"; |
| 119 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 120 | types.IterateTypes([&](const AidlDefinedType& other_defined_type) { |
| 121 | if (&other_defined_type == &defined_type) return; |
| 122 | |
| 123 | if (other_defined_type.AsInterface() != nullptr) { |
| 124 | out << "#include <" |
| 125 | << HeaderFile(other_defined_type, ClassNames::INTERFACE, false /*use_os_sep*/) << ">\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 126 | } else if (other_defined_type.AsStructuredParcelable() != nullptr) { |
| 127 | out << "#include <" << HeaderFile(other_defined_type, ClassNames::BASE, false /*use_os_sep*/) |
| 128 | << ">\n"; |
| 129 | } else if (other_defined_type.AsParcelable() != nullptr) { |
| 130 | out << "#include \"" << other_defined_type.AsParcelable()->GetCppHeader() << "\"\n"; |
| 131 | } else { |
| 132 | AIDL_FATAL(defined_type) << "Unrecognized type."; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 133 | } |
| 134 | }); |
| 135 | } |
| 136 | static void GenerateSourceIncludes(CodeWriter& out, const AidlTypenames& types, |
| 137 | const AidlDefinedType& /*defined_type*/) { |
| 138 | types.IterateTypes([&](const AidlDefinedType& a_defined_type) { |
| 139 | if (a_defined_type.AsInterface() != nullptr) { |
| 140 | out << "#include <" << HeaderFile(a_defined_type, ClassNames::CLIENT, false /*use_os_sep*/) |
| 141 | << ">\n"; |
| 142 | out << "#include <" << HeaderFile(a_defined_type, ClassNames::SERVER, false /*use_os_sep*/) |
| 143 | << ">\n"; |
| 144 | out << "#include <" << HeaderFile(a_defined_type, ClassNames::INTERFACE, false /*use_os_sep*/) |
| 145 | << ">\n"; |
| 146 | } |
| 147 | }); |
| 148 | } |
| 149 | |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 150 | static void GenerateConstantDeclarations(CodeWriter& out, const AidlInterface& interface) { |
| 151 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 152 | const AidlConstantValue& value = constant->GetValue(); |
| 153 | if (value.GetType() == AidlConstantValue::Type::STRING) { |
| 154 | out << "static const char* " << constant->GetName() << ";\n"; |
| 155 | } |
| 156 | } |
| 157 | out << "\n"; |
| 158 | |
| 159 | bool hasIntegralConstant = false; |
| 160 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 161 | const AidlConstantValue& value = constant->GetValue(); |
| 162 | if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL || |
| 163 | value.GetType() == AidlConstantValue::Type::INTEGRAL) { |
| 164 | hasIntegralConstant = true; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (hasIntegralConstant) { |
| 170 | out << "enum : int32_t {\n"; |
| 171 | out.Indent(); |
| 172 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 173 | const AidlConstantValue& value = constant->GetValue(); |
| 174 | if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL || |
| 175 | value.GetType() == AidlConstantValue::Type::INTEGRAL) { |
| 176 | out << constant->GetName() << " = " << constant->ValueString(AidlConstantValueDecorator) |
| 177 | << ",\n"; |
| 178 | } |
| 179 | } |
| 180 | out.Dedent(); |
| 181 | out << "};\n"; |
| 182 | } |
| 183 | } |
| 184 | static void GenerateConstantDefinitions(CodeWriter& out, const AidlInterface& interface) { |
| 185 | const std::string clazz = ClassName(interface, ClassNames::INTERFACE); |
| 186 | |
| 187 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 188 | const AidlConstantValue& value = constant->GetValue(); |
| 189 | if (value.GetType() == AidlConstantValue::Type::STRING) { |
| 190 | out << "const char* " << clazz << "::" << constant->GetName() << " = " |
| 191 | << constant->ValueString(AidlConstantValueDecorator) << ";\n"; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 196 | void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type, |
| 197 | const Options& options) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 198 | GenerateSourceIncludes(out, types, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 199 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 200 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 201 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 202 | GenerateClassSource(out, types, defined_type, options); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 203 | GenerateClientSource(out, types, defined_type, options); |
| 204 | GenerateServerSource(out, types, defined_type, options); |
| 205 | GenerateInterfaceSource(out, types, defined_type, options); |
| 206 | LeaveNdkNamespace(out, defined_type); |
| 207 | } |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 208 | |
| 209 | static std::string DataClassFor(const AidlInterface& defined_type) { |
| 210 | return "AidlClassData_" + ClassName(defined_type, ClassNames::INTERFACE); |
| 211 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 212 | static std::string MethodId(const AidlMethod& m) { |
| 213 | return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)"; |
| 214 | } |
| 215 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 216 | static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types, |
| 217 | const AidlInterface& defined_type, |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 218 | const AidlMethod& method) { |
| 219 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
| 220 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 221 | out << NdkMethodDecl(types, method, clazz) << " {\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 222 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 223 | out << "::ndk::ScopedAParcel _aidl_in;\n"; |
| 224 | out << "::ndk::ScopedAParcel _aidl_out;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 225 | out << "binder_status_t _aidl_ret_status = STATUS_OK;\n"; |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 226 | out << "::ndk::ScopedAStatus _aidl_status;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 227 | out << "\n"; |
| 228 | |
| 229 | out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n"; |
| 230 | StatusCheckGoto(out); |
| 231 | |
| 232 | for (const AidlArgument* arg : method.GetInArguments()) { |
| 233 | out << "_aidl_ret_status = "; |
| 234 | std::string prefix = arg->IsOut() ? "*" : ""; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 235 | WriteToParcelFor( |
| 236 | {out, types, arg->GetType(), "_aidl_in.get()", prefix + cpp::BuildVarName(*arg)}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 237 | out << ";\n"; |
| 238 | StatusCheckGoto(out); |
| 239 | } |
| 240 | out << "_aidl_ret_status = AIBinder_transact(\n"; |
| 241 | out.Indent(); |
| 242 | out << "asBinder().get(),\n"; |
| 243 | out << MethodId(method) << ",\n"; |
| 244 | out << "_aidl_in.getR(),\n"; |
| 245 | out << "_aidl_out.getR(),\n"; |
| 246 | out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << ");\n"; |
| 247 | out.Dedent(); |
| 248 | StatusCheckGoto(out); |
| 249 | |
| 250 | if (!method.IsOneway()) { |
| 251 | out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n"; |
| 252 | StatusCheckGoto(out); |
| 253 | |
| 254 | out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n"; |
| 255 | } |
| 256 | |
| 257 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 258 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 259 | ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 260 | out << ";\n"; |
| 261 | StatusCheckGoto(out); |
| 262 | } |
| 263 | |
| 264 | if (method.GetType().GetName() != "void") { |
| 265 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 266 | ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 267 | out << ";\n"; |
| 268 | StatusCheckGoto(out); |
| 269 | } |
| 270 | |
| 271 | out << "_aidl_error:\n"; |
| 272 | out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"; |
| 273 | out << "return _aidl_status;\n"; |
| 274 | out.Dedent(); |
| 275 | out << "}\n"; |
| 276 | } |
| 277 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 278 | static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types, |
| 279 | const AidlInterface& /*defined_type*/, |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 280 | const AidlMethod& method) { |
| 281 | out << "case " << MethodId(method) << ": {\n"; |
| 282 | out.Indent(); |
| 283 | for (const auto& arg : method.GetArguments()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 284 | out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg) |
| 285 | << ";\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 286 | } |
| 287 | if (method.GetType().GetName() != "void") { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 288 | out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 289 | } |
| 290 | out << "\n"; |
| 291 | |
| 292 | for (const AidlArgument* arg : method.GetInArguments()) { |
| 293 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 294 | ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + cpp::BuildVarName(*arg)}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 295 | out << ";\n"; |
| 296 | StatusCheckBreak(out); |
| 297 | } |
| 298 | |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 299 | out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "(" |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 300 | << NdkCallListFor(method) << ");\n"; |
| 301 | |
| 302 | if (method.IsOneway()) { |
| 303 | // For a oneway transaction, the kernel will have already returned a result. This is for the |
| 304 | // in-process case when a oneway transaction is parceled/unparceled in the same process. |
| 305 | out << "_aidl_ret_status = STATUS_OK;\n"; |
| 306 | } else { |
| 307 | out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n"; |
| 308 | StatusCheckBreak(out); |
| 309 | |
| 310 | out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n"; |
| 311 | |
| 312 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 313 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 314 | WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 315 | out << ";\n"; |
| 316 | StatusCheckBreak(out); |
| 317 | } |
| 318 | if (method.GetType().GetName() != "void") { |
| 319 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 320 | WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 321 | out << ";\n"; |
| 322 | StatusCheckBreak(out); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | out << "break;\n"; |
| 327 | out.Dedent(); |
| 328 | out << "}\n"; |
| 329 | } |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 330 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 331 | void GenerateClassSource(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 332 | const AidlInterface& defined_type, const Options& /*options*/) { |
| 333 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
| 334 | const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER); |
| 335 | const std::string data_clazz = DataClassFor(defined_type); |
| 336 | const std::string on_create = data_clazz + "_onCreate"; |
| 337 | const std::string on_destroy = data_clazz + "_onDestory"; |
| 338 | const std::string on_transact = data_clazz + "_onTransact"; |
| 339 | |
| 340 | out << "struct " << data_clazz << " {\n"; |
| 341 | out.Indent(); |
| 342 | out << "static AIBinder_Class* clazz;\n"; |
| 343 | out << "std::shared_ptr<" << bn_clazz << "> instance;\n"; |
| 344 | out.Dedent(); |
| 345 | out << "};\n\n"; |
| 346 | |
| 347 | out << "static void* " << on_create << "(void* args) {\n"; |
| 348 | out.Indent(); |
| 349 | out << data_clazz << "* data = new " << data_clazz << "{static_cast<" << bn_clazz |
| 350 | << "*>(args)->ref<" << bn_clazz << ">()};\n"; |
| 351 | out << "return static_cast<void*>(data);\n"; |
| 352 | out.Dedent(); |
| 353 | out << "};\n\n"; |
| 354 | |
| 355 | out << "static void " << on_destroy << "(void* userData) {\n"; |
| 356 | out.Indent(); |
| 357 | out << "delete static_cast<" << data_clazz << "*>(userData);\n"; |
| 358 | out.Dedent(); |
| 359 | out << "};\n\n"; |
| 360 | |
| 361 | out << "static binder_status_t " << on_transact |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 362 | << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, " |
| 363 | "AParcel* _aidl_out) {\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 364 | out.Indent(); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 365 | out << "(void)_aidl_in;\n"; |
| 366 | out << "(void)_aidl_out;\n"; |
| 367 | out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n"; |
| 368 | if (!defined_type.GetMethods().empty()) { |
| 369 | out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = static_cast<" << data_clazz |
| 370 | << "*>(AIBinder_getUserData(_aidl_binder))->instance;\n"; |
| 371 | out << "switch (_aidl_code) {\n"; |
| 372 | out.Indent(); |
| 373 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 374 | GenerateServerCaseDefinition(out, types, defined_type, *method); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 375 | } |
| 376 | out.Dedent(); |
| 377 | out << "}\n"; |
| 378 | } else { |
| 379 | out << "(void)_aidl_binder;\n"; |
| 380 | out << "(void)_aidl_code;\n"; |
| 381 | } |
| 382 | out << "return _aidl_ret_status;\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 383 | out.Dedent(); |
| 384 | out << "};\n\n"; |
| 385 | |
| 386 | out << "AIBinder_Class* " << data_clazz << "::clazz = AIBinder_Class_define(" << clazz |
| 387 | << "::descriptor, " << on_create << ", " << on_destroy << ", " << on_transact << ");\n\n"; |
| 388 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 389 | void GenerateClientSource(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 390 | const AidlInterface& defined_type, const Options& /*options*/) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 391 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 392 | const std::string data_clazz = DataClassFor(defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 393 | |
| 394 | out << "// Source for " << clazz << "\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 395 | out << "std::shared_ptr<" << clazz << "> " << clazz |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 396 | << "::associate(const ::ndk::SpAIBinder& binder) {\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 397 | out.Indent(); |
| 398 | out << "if (!AIBinder_associateClass(binder.get(), " << data_clazz |
| 399 | << "::clazz)) { return nullptr; }\n"; |
Steven Moreland | 1c4b405 | 2018-10-10 12:24:22 -0700 | [diff] [blame^] | 400 | out << "return (new " << clazz << "(binder))->ref<" << clazz << ">();\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 401 | out.Dedent(); |
| 402 | out << "}\n\n"; |
| 403 | |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 404 | out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 405 | out << clazz << "::~" << clazz << "() {}\n"; |
| 406 | out << "\n"; |
| 407 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 408 | GenerateClientMethodDefinition(out, types, defined_type, *method); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 409 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 410 | } |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 411 | void GenerateServerSource(CodeWriter& out, const AidlTypenames& /*types*/, |
| 412 | const AidlInterface& defined_type, const Options& /*options*/) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 413 | const std::string clazz = ClassName(defined_type, ClassNames::SERVER); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 414 | const std::string data_clazz = DataClassFor(defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 415 | |
| 416 | out << "// Source for " << clazz << "\n"; |
| 417 | out << clazz << "::" << clazz << "() {}\n"; |
| 418 | out << clazz << "::~" << clazz << "() {}\n"; |
| 419 | |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 420 | out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 421 | out.Indent(); |
| 422 | out << "AIBinder* binder = AIBinder_new(" << data_clazz |
| 423 | << "::clazz, static_cast<void*>(this));\n"; |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 424 | out << "return ::ndk::SpAIBinder(binder);\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 425 | out.Dedent(); |
| 426 | out << "}\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 427 | } |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 428 | void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& /*types*/, |
| 429 | const AidlInterface& defined_type, const Options& /*options*/) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 430 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 431 | const std::string data_clazz = DataClassFor(defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 432 | |
| 433 | out << "// Source for " << clazz << "\n"; |
| 434 | out << "const char* " << clazz << "::descriptor = \"" << defined_type.GetCanonicalName() |
| 435 | << "\";\n"; |
| 436 | out << clazz << "::" << clazz << "() {}\n"; |
| 437 | out << clazz << "::~" << clazz << "() {}\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 438 | out << "\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 439 | GenerateConstantDefinitions(out, defined_type); |
| 440 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 441 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 442 | out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<" |
| 443 | << clazz << ">& instance) {\n"; |
| 444 | out.Indent(); |
| 445 | out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : " |
| 446 | "nullptr);\n"; |
| 447 | out.Dedent(); |
| 448 | out << "}\n"; |
| 449 | |
| 450 | out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<" |
| 451 | << clazz << ">* instance) {\n"; |
| 452 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 453 | out << "::ndk::SpAIBinder binder;\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 454 | out << "binder_status_t status = AParcel_readNullableStrongBinder(parcel, binder.getR());\n"; |
| 455 | out << "if (status != STATUS_OK) return status;\n"; |
| 456 | out << data_clazz << "* data = static_cast<" << data_clazz |
| 457 | << "*>(AIBinder_getUserData(binder.get()));\n"; |
| 458 | out << "if (data) {\n"; |
| 459 | out.Indent(); |
| 460 | out << "*instance = data->instance;\n"; |
| 461 | out.Dedent(); |
| 462 | out << "} else {\n"; |
| 463 | out.Indent(); |
| 464 | out << "*instance = " << NdkFullClassName(defined_type, ClassNames::CLIENT) |
| 465 | << "::associate(binder);\n"; |
| 466 | out.Dedent(); |
| 467 | out << "}\n"; |
| 468 | out << "return STATUS_OK;\n"; |
| 469 | out.Dedent(); |
| 470 | out << "}\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 471 | } |
| 472 | void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 473 | const AidlInterface& defined_type, const Options& /*options*/) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 474 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
| 475 | |
| 476 | out << "#pragma once\n\n"; |
| 477 | out << "#include \"" << HeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/) |
| 478 | << "\"\n"; |
| 479 | out << "\n"; |
| 480 | out << "#include <android/binder_ibinder.h>\n"; |
| 481 | out << "\n"; |
| 482 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 483 | out << "class " << clazz << " : public ::ndk::BpCInterface<" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 484 | << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n"; |
| 485 | out << "public:\n"; |
| 486 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 487 | out << "static std::shared_ptr<" << clazz << "> associate(const ::ndk::SpAIBinder& binder);\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 488 | out << "virtual ~" << clazz << "();\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 489 | out << "\n"; |
| 490 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 491 | out << NdkMethodDecl(types, *method) << " override;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 492 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 493 | out.Dedent(); |
| 494 | out << "private:\n"; |
| 495 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 496 | out << clazz << "(const ::ndk::SpAIBinder& binder);\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 497 | out.Dedent(); |
| 498 | out << "};\n"; |
| 499 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 500 | } |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 501 | void GenerateServerHeader(CodeWriter& out, const AidlTypenames& /*types*/, |
| 502 | const AidlInterface& defined_type, const Options& /*options*/) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 503 | const std::string clazz = ClassName(defined_type, ClassNames::SERVER); |
| 504 | |
| 505 | out << "#pragma once\n\n"; |
| 506 | out << "#include \"" << HeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/) |
| 507 | << "\"\n"; |
| 508 | out << "\n"; |
| 509 | out << "#include <android/binder_ibinder.h>\n"; |
| 510 | out << "\n"; |
| 511 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 512 | out << "class " << clazz << " : public ::ndk::BnCInterface<" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 513 | << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n"; |
| 514 | out << "public:\n"; |
| 515 | out.Indent(); |
| 516 | out << clazz << "();\n"; |
| 517 | out << "virtual ~" << clazz << "();\n"; |
| 518 | out.Dedent(); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 519 | out << "protected:\n"; |
| 520 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 521 | out << "::ndk::SpAIBinder createBinder() override;\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 522 | out.Dedent(); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 523 | out << "private:\n"; |
| 524 | out.Indent(); |
| 525 | out.Dedent(); |
| 526 | out << "};\n"; |
| 527 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 528 | } |
| 529 | void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 530 | const AidlInterface& defined_type, const Options& /*options*/) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 531 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
| 532 | |
| 533 | out << "#pragma once\n\n"; |
| 534 | out << "#include <android/binder_interface_utils.h>\n"; |
| 535 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 536 | |
| 537 | GenerateHeaderIncludes(out, types, defined_type); |
| 538 | out << "\n"; |
| 539 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 540 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 541 | out << "class " << clazz << " : public ::ndk::ICInterface {\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 542 | out << "public:\n"; |
| 543 | out.Indent(); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 544 | out << "static AIBinder_Class* clazz;\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 545 | out << "static const char* descriptor;\n"; |
| 546 | out << clazz << "();\n"; |
| 547 | out << "virtual ~" << clazz << "();\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 548 | out << "\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 549 | GenerateConstantDeclarations(out, defined_type); |
| 550 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 551 | out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz |
| 552 | << ">& instance);"; |
| 553 | out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz |
| 554 | << ">* instance);"; |
| 555 | out << "\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 556 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 557 | out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 558 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 559 | out.Dedent(); |
| 560 | out << "};\n"; |
| 561 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 562 | } |
| 563 | void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 564 | const AidlStructuredParcelable& defined_type, |
| 565 | const Options& /*options*/) { |
| 566 | const std::string clazz = ClassName(defined_type, ClassNames::BASE); |
| 567 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 568 | out << "#pragma once\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 569 | out << "#include <android/binder_interface_utils.h>\n"; |
| 570 | out << "\n"; |
| 571 | |
| 572 | GenerateHeaderIncludes(out, types, defined_type); |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 573 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 574 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 575 | out << "class " << clazz << " {\n"; |
| 576 | out << "public:\n"; |
| 577 | out.Indent(); |
| 578 | out << "static const char* descriptor;\n"; |
| 579 | out << "\n"; |
| 580 | for (const auto& variable : defined_type.GetFields()) { |
| 581 | out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName(); |
| 582 | if (variable->GetDefaultValue()) { |
| 583 | out << " = " << variable->ValueString(AidlConstantValueDecorator); |
| 584 | } |
| 585 | out << ";\n"; |
| 586 | } |
| 587 | out << "\n"; |
| 588 | out << "binder_status_t readFromParcel(const AParcel* parcel);\n"; |
| 589 | out << "binder_status_t writeToParcel(AParcel* parcel) const;\n"; |
| 590 | out.Dedent(); |
| 591 | out << "};\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 592 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 593 | } |
| 594 | void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 595 | const AidlStructuredParcelable& defined_type, |
| 596 | const Options& /*options*/) { |
| 597 | const std::string clazz = ClassName(defined_type, ClassNames::BASE); |
| 598 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 599 | out << "#include \"" << HeaderFile(defined_type, ClassNames::BASE, false /*use_os_sep*/) |
| 600 | << "\"\n"; |
| 601 | out << "\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 602 | GenerateSourceIncludes(out, types, defined_type); |
| 603 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 604 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 605 | out << "const char* " << clazz << "::descriptor = \"" << defined_type.GetCanonicalName() |
| 606 | << "\";\n"; |
| 607 | out << "\n"; |
| 608 | |
| 609 | out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n"; |
| 610 | out.Indent(); |
| 611 | out << "std::string _aidl_descriptor;\n"; |
| 612 | out << "binder_status_t _aidl_ret_status;\n"; |
| 613 | |
| 614 | out << "int32_t _aidl_null;\n"; |
| 615 | out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_null);\n"; |
| 616 | StatusCheckReturn(out); |
| 617 | |
| 618 | // TODO(b/117281836) |
| 619 | out << "if (_aidl_null == 0) return STATUS_UNEXPECTED_NULL;\n\n"; |
| 620 | |
| 621 | for (const auto& variable : defined_type.GetFields()) { |
| 622 | out << "_aidl_ret_status = "; |
| 623 | ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()}); |
| 624 | out << ";\n"; |
| 625 | StatusCheckReturn(out); |
| 626 | } |
| 627 | out << "return _aidl_ret_status;\n"; |
| 628 | out.Dedent(); |
| 629 | out << "}\n"; |
| 630 | |
| 631 | out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n"; |
| 632 | out.Indent(); |
| 633 | out << "binder_status_t _aidl_ret_status;\n"; |
| 634 | |
| 635 | // non-null |
| 636 | out << "_aidl_ret_status = AParcel_writeInt32(parcel, 1);\n"; |
| 637 | StatusCheckReturn(out); |
| 638 | |
| 639 | for (const auto& variable : defined_type.GetFields()) { |
| 640 | out << "_aidl_ret_status = "; |
| 641 | WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()}); |
| 642 | out << ";\n"; |
| 643 | StatusCheckReturn(out); |
| 644 | } |
| 645 | out << "return _aidl_ret_status;\n"; |
| 646 | out.Dedent(); |
| 647 | out << "}\n"; |
| 648 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 649 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 650 | } |
| 651 | } // namespace internals |
| 652 | } // namespace ndk |
| 653 | } // namespace aidl |
| 654 | } // namespace android |