blob: fa90a581057d452b9f7917043a3834c65d9f98bc [file] [log] [blame]
Steven Morelandb0057e72018-08-27 01:44:11 -07001/*
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
Jiyong Park965c5b92018-11-21 13:37:15 +090019#include "aidl.h"
Steven Morelandb0057e72018-08-27 01:44:11 -070020#include "aidl_language.h"
21#include "aidl_to_cpp_common.h"
Steven Morelandaada3422018-09-20 15:55:33 -070022#include "aidl_to_ndk.h"
Steven Morelandb0057e72018-08-27 01:44:11 -070023
24#include <android-base/logging.h>
25
26namespace android {
27namespace aidl {
28namespace ndk {
29
Steven Moreland15b3ba72019-03-06 13:17:08 -080030static constexpr const char* kClazz = "_g_aidl_clazz";
Jiyong Park965c5b92018-11-21 13:37:15 +090031static constexpr const char* kDescriptor = "descriptor";
32static constexpr const char* kVersion = "version";
Jeongik Cha8f553e72019-02-12 16:58:03 +090033static constexpr const char* kCacheVariable = "_aidl_cached_value";
Jiyong Park965c5b92018-11-21 13:37:15 +090034
Steven Morelandb0057e72018-08-27 01:44:11 -070035using namespace internals;
36using cpp::ClassNames;
37
38void GenerateNdkInterface(const string& output_file, const Options& options,
39 const AidlTypenames& types, const AidlInterface& defined_type,
40 const IoDelegate& io_delegate) {
Neil Fullercc14a582019-04-03 10:25:25 +000041 const string i_header =
42 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -070043 unique_ptr<CodeWriter> i_writer(io_delegate.GetCodeWriter(i_header));
44 GenerateInterfaceHeader(*i_writer, types, defined_type, options);
45 CHECK(i_writer->Close());
46
Steven Moreland7c933372018-10-11 15:20:04 -070047 const string bp_header =
48 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -070049 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
50 GenerateClientHeader(*bp_writer, types, defined_type, options);
51 CHECK(bp_writer->Close());
52
Steven Moreland7c933372018-10-11 15:20:04 -070053 const string bn_header =
54 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER);
Steven Morelandb0057e72018-08-27 01:44:11 -070055 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
56 GenerateServerHeader(*bn_writer, types, defined_type, options);
57 CHECK(bn_writer->Close());
58
59 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
60 GenerateSource(*source_writer, types, defined_type, options);
61 CHECK(source_writer->Close());
62}
63
64void GenerateNdkParcel(const string& output_file, const Options& options,
65 const AidlTypenames& types, const AidlStructuredParcelable& defined_type,
66 const IoDelegate& io_delegate) {
Steven Moreland7c933372018-10-11 15:20:04 -070067 const string header_path =
Neil Fullercc14a582019-04-03 10:25:25 +000068 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::BASE);
Steven Morelandb0057e72018-08-27 01:44:11 -070069 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
70 GenerateParcelHeader(*header_writer, types, defined_type, options);
71 CHECK(header_writer->Close());
72
Steven Moreland7c933372018-10-11 15:20:04 -070073 const string bp_header =
74 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -070075 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
76 *bp_writer << "#error TODO(b/111362593) defined_types do not have bp classes\n";
77 CHECK(bp_writer->Close());
78
Steven Moreland7c933372018-10-11 15:20:04 -070079 const string bn_header =
80 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER);
Steven Morelandb0057e72018-08-27 01:44:11 -070081 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
82 *bn_writer << "#error TODO(b/111362593) defined_types do not have bn classes\n";
83 CHECK(bn_writer->Close());
84
85 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
86 GenerateParcelSource(*source_writer, types, defined_type, options);
87 CHECK(source_writer->Close());
88}
89
Steven Moreland2a9a7d62019-02-05 16:11:54 -080090void GenerateNdkParcelDeclaration(const std::string& filename, const IoDelegate& io_delegate) {
91 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
92 *code_writer
93 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
94 CHECK(code_writer->Close());
95}
96
Steven Morelandb0057e72018-08-27 01:44:11 -070097void GenerateNdk(const string& output_file, const Options& options, const AidlTypenames& types,
98 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
99 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
100 if (parcelable != nullptr) {
101 GenerateNdkParcel(output_file, options, types, *parcelable, io_delegate);
102 return;
103 }
104
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800105 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
106 if (parcelable_decl != nullptr) {
107 GenerateNdkParcelDeclaration(output_file, io_delegate);
108 return;
109 }
110
Steven Morelandb0057e72018-08-27 01:44:11 -0700111 const AidlInterface* interface = defined_type.AsInterface();
112 if (interface != nullptr) {
113 GenerateNdkInterface(output_file, options, types, *interface, io_delegate);
114 return;
115 }
116
117 CHECK(false) << "Unrecognized type sent for cpp generation.";
118}
119namespace internals {
120
121void EnterNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
122 out << "namespace aidl {\n";
123 cpp::EnterNamespace(out, defined_type);
124}
125void LeaveNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
126 cpp::LeaveNamespace(out, defined_type);
127 out << "} // namespace aidl\n";
128}
129
Steven Morelandaada3422018-09-20 15:55:33 -0700130static void StatusCheckGoto(CodeWriter& out) {
131 out << "if (_aidl_ret_status != STATUS_OK) goto _aidl_error;\n\n";
132}
133static void StatusCheckBreak(CodeWriter& out) {
134 out << "if (_aidl_ret_status != STATUS_OK) break;\n\n";
135}
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700136static void StatusCheckReturn(CodeWriter& out) {
137 out << "if (_aidl_ret_status != STATUS_OK) return _aidl_ret_status;\n\n";
138}
Steven Morelandaada3422018-09-20 15:55:33 -0700139
Steven Moreland2bea13b2018-10-03 15:12:33 -0700140static void GenerateHeaderIncludes(CodeWriter& out, const AidlTypenames& types,
141 const AidlDefinedType& defined_type) {
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700142 out << "#include <android/binder_parcel_utils.h>\n";
143
Steven Moreland2bea13b2018-10-03 15:12:33 -0700144 types.IterateTypes([&](const AidlDefinedType& other_defined_type) {
145 if (&other_defined_type == &defined_type) return;
146
147 if (other_defined_type.AsInterface() != nullptr) {
148 out << "#include <"
Neil Fullercc14a582019-04-03 10:25:25 +0000149 << NdkHeaderFile(other_defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
150 << ">\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700151 } else if (other_defined_type.AsStructuredParcelable() != nullptr) {
152 out << "#include <"
153 << NdkHeaderFile(other_defined_type, ClassNames::BASE, false /*use_os_sep*/) << ">\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700154 } else if (other_defined_type.AsParcelable() != nullptr) {
155 out << "#include \"" << other_defined_type.AsParcelable()->GetCppHeader() << "\"\n";
156 } else {
157 AIDL_FATAL(defined_type) << "Unrecognized type.";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700158 }
159 });
160}
161static void GenerateSourceIncludes(CodeWriter& out, const AidlTypenames& types,
162 const AidlDefinedType& /*defined_type*/) {
163 types.IterateTypes([&](const AidlDefinedType& a_defined_type) {
164 if (a_defined_type.AsInterface() != nullptr) {
Steven Moreland7c933372018-10-11 15:20:04 -0700165 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::CLIENT, false /*use_os_sep*/)
Steven Moreland2bea13b2018-10-03 15:12:33 -0700166 << ">\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700167 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::SERVER, false /*use_os_sep*/)
Steven Moreland2bea13b2018-10-03 15:12:33 -0700168 << ">\n";
Neil Fullercc14a582019-04-03 10:25:25 +0000169 out << "#include <"
170 << NdkHeaderFile(a_defined_type, ClassNames::INTERFACE, false /*use_os_sep*/) << ">\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700171 }
172 });
173}
174
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700175static void GenerateConstantDeclarations(CodeWriter& out, const AidlInterface& interface) {
176 for (const auto& constant : interface.GetConstantDeclarations()) {
177 const AidlConstantValue& value = constant->GetValue();
178 if (value.GetType() == AidlConstantValue::Type::STRING) {
179 out << "static const char* " << constant->GetName() << ";\n";
180 }
181 }
182 out << "\n";
183
184 bool hasIntegralConstant = false;
185 for (const auto& constant : interface.GetConstantDeclarations()) {
186 const AidlConstantValue& value = constant->GetValue();
187 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
188 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
189 hasIntegralConstant = true;
190 break;
191 }
192 }
193
194 if (hasIntegralConstant) {
195 out << "enum : int32_t {\n";
196 out.Indent();
197 for (const auto& constant : interface.GetConstantDeclarations()) {
198 const AidlConstantValue& value = constant->GetValue();
199 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
200 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
201 out << constant->GetName() << " = " << constant->ValueString(AidlConstantValueDecorator)
202 << ",\n";
203 }
204 }
205 out.Dedent();
206 out << "};\n";
207 }
208}
209static void GenerateConstantDefinitions(CodeWriter& out, const AidlInterface& interface) {
210 const std::string clazz = ClassName(interface, ClassNames::INTERFACE);
211
212 for (const auto& constant : interface.GetConstantDeclarations()) {
213 const AidlConstantValue& value = constant->GetValue();
214 if (value.GetType() == AidlConstantValue::Type::STRING) {
215 out << "const char* " << clazz << "::" << constant->GetName() << " = "
216 << constant->ValueString(AidlConstantValueDecorator) << ";\n";
217 }
218 }
219}
220
Steven Morelandb0057e72018-08-27 01:44:11 -0700221void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
222 const Options& options) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700223 GenerateSourceIncludes(out, types, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700224 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700225
Steven Morelandb0057e72018-08-27 01:44:11 -0700226 EnterNdkNamespace(out, defined_type);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700227 GenerateClassSource(out, types, defined_type, options);
Steven Morelandb0057e72018-08-27 01:44:11 -0700228 GenerateClientSource(out, types, defined_type, options);
229 GenerateServerSource(out, types, defined_type, options);
230 GenerateInterfaceSource(out, types, defined_type, options);
231 LeaveNdkNamespace(out, defined_type);
232}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700233
Steven Morelandaada3422018-09-20 15:55:33 -0700234static std::string MethodId(const AidlMethod& m) {
235 return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
236}
237
Jeongik Cha8f553e72019-02-12 16:58:03 +0900238static void GenerateClientMethodDefinition(
239 CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
240 const AidlMethod& method, const std::optional<std::string> return_value_cached_to) {
Steven Morelandaada3422018-09-20 15:55:33 -0700241 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
242
Steven Moreland2bea13b2018-10-03 15:12:33 -0700243 out << NdkMethodDecl(types, method, clazz) << " {\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700244 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700245 out << "binder_status_t _aidl_ret_status = STATUS_OK;\n";
Steven Moreland63404532018-10-08 14:31:00 -0700246 out << "::ndk::ScopedAStatus _aidl_status;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900247
Jeongik Cha8f553e72019-02-12 16:58:03 +0900248 if (return_value_cached_to) {
249 out << "if (" << *return_value_cached_to << " != -1) {\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900250 out.Indent();
Jeongik Cha8f553e72019-02-12 16:58:03 +0900251 out << "*_aidl_return = " << *return_value_cached_to << ";\n"
Jeongik Chac9972922019-02-11 12:41:16 +0900252 << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"
253 << "return _aidl_status;\n";
254 out.Dedent();
255 out << "}\n";
256 }
257 out << "::ndk::ScopedAParcel _aidl_in;\n";
258 out << "::ndk::ScopedAParcel _aidl_out;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700259 out << "\n";
260
261 out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n";
262 StatusCheckGoto(out);
263
Steven Morelandeb38ee72018-10-15 14:20:04 -0700264 for (const auto& arg : method.GetArguments()) {
265 const std::string var_name = cpp::BuildVarName(*arg);
266
267 if (arg->IsIn()) {
268 out << "_aidl_ret_status = ";
269 const std::string prefix = (arg->IsOut() ? "*" : "");
270 WriteToParcelFor({out, types, arg->GetType(), "_aidl_in.get()", prefix + var_name});
271 out << ";\n";
272 StatusCheckGoto(out);
273 } else if (arg->IsOut() && arg->GetType().IsArray()) {
274 out << "_aidl_ret_status = ::ndk::AParcel_writeVectorSize(_aidl_in.get(), *" << var_name
275 << ");\n";
276 }
Steven Morelandaada3422018-09-20 15:55:33 -0700277 }
278 out << "_aidl_ret_status = AIBinder_transact(\n";
279 out.Indent();
280 out << "asBinder().get(),\n";
281 out << MethodId(method) << ",\n";
282 out << "_aidl_in.getR(),\n";
283 out << "_aidl_out.getR(),\n";
284 out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << ");\n";
285 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900286
287 // If the method is not implmented in the server side but the client has
288 // provided the default implementation, call it instead of failing hard.
289 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
290 out << "if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && ";
291 out << iface << "::getDefaultImpl()) {\n";
292 out.Indent();
293 out << "return " << iface << "::getDefaultImpl()->" << method.GetName() << "(";
294 out << NdkArgList(types, method, FormatArgNameOnly) << ");\n";
295 out.Dedent();
296 out << "}\n";
297
Steven Morelandaada3422018-09-20 15:55:33 -0700298 StatusCheckGoto(out);
299
300 if (!method.IsOneway()) {
301 out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n";
302 StatusCheckGoto(out);
303
304 out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n";
305 }
306
Steven Morelandaada3422018-09-20 15:55:33 -0700307 if (method.GetType().GetName() != "void") {
308 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700309 ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700310 out << ";\n";
311 StatusCheckGoto(out);
Jeongik Cha8f553e72019-02-12 16:58:03 +0900312 if (return_value_cached_to) {
313 out << *return_value_cached_to << " = *_aidl_return;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900314 }
Steven Morelandaada3422018-09-20 15:55:33 -0700315 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700316 for (const AidlArgument* arg : method.GetOutArguments()) {
317 out << "_aidl_ret_status = ";
318 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)});
319 out << ";\n";
320 StatusCheckGoto(out);
321 }
Steven Morelandaada3422018-09-20 15:55:33 -0700322
323 out << "_aidl_error:\n";
324 out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n";
325 out << "return _aidl_status;\n";
326 out.Dedent();
327 out << "}\n";
328}
329
Steven Moreland2bea13b2018-10-03 15:12:33 -0700330static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types,
331 const AidlInterface& /*defined_type*/,
Steven Morelandaada3422018-09-20 15:55:33 -0700332 const AidlMethod& method) {
333 out << "case " << MethodId(method) << ": {\n";
334 out.Indent();
335 for (const auto& arg : method.GetArguments()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700336 out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg)
337 << ";\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700338 }
339 if (method.GetType().GetName() != "void") {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700340 out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700341 }
342 out << "\n";
343
Steven Morelandeb38ee72018-10-15 14:20:04 -0700344 for (const auto& arg : method.GetArguments()) {
345 const std::string var_name = cpp::BuildVarName(*arg);
346
347 if (arg->IsIn()) {
348 out << "_aidl_ret_status = ";
349 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + var_name});
350 out << ";\n";
351 StatusCheckBreak(out);
352 } else if (arg->IsOut() && arg->GetType().IsArray()) {
353 out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n";
354 }
Steven Morelandaada3422018-09-20 15:55:33 -0700355 }
356
Steven Moreland63404532018-10-08 14:31:00 -0700357 out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "("
Jiyong Park965c5b92018-11-21 13:37:15 +0900358 << NdkArgList(types, method, FormatArgForCall) << ");\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700359
360 if (method.IsOneway()) {
361 // For a oneway transaction, the kernel will have already returned a result. This is for the
362 // in-process case when a oneway transaction is parceled/unparceled in the same process.
363 out << "_aidl_ret_status = STATUS_OK;\n";
364 } else {
365 out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n";
366 StatusCheckBreak(out);
367
368 out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n";
369
Steven Morelandaada3422018-09-20 15:55:33 -0700370 if (method.GetType().GetName() != "void") {
371 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700372 WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700373 out << ";\n";
374 StatusCheckBreak(out);
375 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700376 for (const AidlArgument* arg : method.GetOutArguments()) {
377 out << "_aidl_ret_status = ";
378 WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)});
379 out << ";\n";
380 StatusCheckBreak(out);
381 }
Steven Morelandaada3422018-09-20 15:55:33 -0700382 }
383
384 out << "break;\n";
385 out.Dedent();
386 out << "}\n";
387}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700388
Steven Moreland2bea13b2018-10-03 15:12:33 -0700389void GenerateClassSource(CodeWriter& out, const AidlTypenames& types,
Steven Moreland0cf3f082018-09-20 19:09:46 -0700390 const AidlInterface& defined_type, const Options& /*options*/) {
391 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
392 const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700393
Steven Moreland15b3ba72019-03-06 13:17:08 -0800394 out << "static binder_status_t "
395 << "_aidl_onTransact"
Steven Morelandaada3422018-09-20 15:55:33 -0700396 << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, "
397 "AParcel* _aidl_out) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700398 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700399 out << "(void)_aidl_in;\n";
400 out << "(void)_aidl_out;\n";
401 out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n";
402 if (!defined_type.GetMethods().empty()) {
Steven Moreland15b3ba72019-03-06 13:17:08 -0800403 // we know this cast is valid because this method is only called by the ICInterface
404 // AIBinder_Class object which is associated with this class.
405 out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz
406 << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700407 out << "switch (_aidl_code) {\n";
408 out.Indent();
409 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700410 GenerateServerCaseDefinition(out, types, defined_type, *method);
Steven Morelandaada3422018-09-20 15:55:33 -0700411 }
412 out.Dedent();
413 out << "}\n";
414 } else {
415 out << "(void)_aidl_binder;\n";
416 out << "(void)_aidl_code;\n";
417 }
418 out << "return _aidl_ret_status;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700419 out.Dedent();
420 out << "};\n\n";
421
Steven Moreland15b3ba72019-03-06 13:17:08 -0800422 out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz
423 << "::" << kDescriptor << ", _aidl_onTransact);\n\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700424}
Steven Morelandb0057e72018-08-27 01:44:11 -0700425void GenerateClientSource(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900426 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700427 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700428
Steven Moreland63404532018-10-08 14:31:00 -0700429 out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700430 out << clazz << "::~" << clazz << "() {}\n";
431 out << "\n";
432 for (const auto& method : defined_type.GetMethods()) {
Jeongik Chac9972922019-02-11 12:41:16 +0900433 // Only getInterfaceVersion can use cache.
434 const bool cacheable = !method->IsUserDefined() && method->GetName() == kGetInterfaceVersion &&
435 options.Version() > 0;
Jeongik Cha8f553e72019-02-12 16:58:03 +0900436 const auto return_value_cached_to =
437 cacheable ? std::make_optional<std::string>(kCacheVariable) : std::nullopt;
438 GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to);
Steven Morelandaada3422018-09-20 15:55:33 -0700439 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700440}
Jiyong Park965c5b92018-11-21 13:37:15 +0900441void GenerateServerSource(CodeWriter& out, const AidlTypenames& types,
442 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700443 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900444 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700445
446 out << "// Source for " << clazz << "\n";
447 out << clazz << "::" << clazz << "() {}\n";
448 out << clazz << "::~" << clazz << "() {}\n";
449
Steven Moreland63404532018-10-08 14:31:00 -0700450 out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700451 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800452 out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n";
Steven Moreland63404532018-10-08 14:31:00 -0700453 out << "return ::ndk::SpAIBinder(binder);\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700454 out.Dedent();
455 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900456
457 // Implement the meta methods
458 for (const auto& method : defined_type.GetMethods()) {
459 if (method->IsUserDefined()) {
460 continue;
461 }
462 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
463 out << NdkMethodDecl(types, *method, clazz) << " {\n";
464 out.Indent();
465 out << "*_aidl_return = " << iface << "::" << kVersion << ";\n";
466 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
467 out.Dedent();
468 out << "}\n";
469 }
470 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700471}
Jiyong Park965c5b92018-11-21 13:37:15 +0900472void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types,
473 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700474 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
Steven Moreland15b3ba72019-03-06 13:17:08 -0800475 const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -0700476
477 out << "// Source for " << clazz << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900478 out << "const char* " << clazz << "::" << kDescriptor << " = \""
479 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700480 out << clazz << "::" << clazz << "() {}\n";
481 out << clazz << "::~" << clazz << "() {}\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700482 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700483 GenerateConstantDefinitions(out, defined_type);
484 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700485
Steven Moreland3c316ed2019-01-17 09:39:37 -0800486 out << "std::shared_ptr<" << clazz << "> " << clazz
487 << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800488 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800489 out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n";
490 out << "std::shared_ptr<::ndk::ICInterface> interface = "
491 "::ndk::ICInterface::asInterface(binder.get());\n";
492 out << "if (interface) {\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800493 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800494 out << "return std::static_pointer_cast<" << clazz << ">(interface);\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800495 out.Dedent();
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800496 out << "}\n";
Steven Moreland15b3ba72019-03-06 13:17:08 -0800497 out << "return (new " << bp_clazz << "(binder))->ref<" << clazz << ">();\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800498 out.Dedent();
Steven Moreland3c316ed2019-01-17 09:39:37 -0800499 out << "}\n\n";
500
Steven Moreland2bea13b2018-10-03 15:12:33 -0700501 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<"
502 << clazz << ">& instance) {\n";
503 out.Indent();
504 out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : "
505 "nullptr);\n";
506 out.Dedent();
507 out << "}\n";
508
509 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<"
510 << clazz << ">* instance) {\n";
511 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700512 out << "::ndk::SpAIBinder binder;\n";
Steven Moreland055d8792018-11-14 12:48:42 -0800513 out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700514 out << "if (status != STATUS_OK) return status;\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800515 out << "*instance = " << clazz << "::fromBinder(binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700516 out << "return STATUS_OK;\n";
517 out.Dedent();
518 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900519
520 // defintion for static member setDefaultImpl
521 out << "bool " << clazz << "::setDefaultImpl(std::shared_ptr<" << clazz << "> impl) {\n";
522 out.Indent();
523 out << "if (!" << clazz << "::default_impl && impl) {\n";
524 out.Indent();
525 out << clazz << "::default_impl = impl;\n";
526 out << "return true;\n";
527 out.Dedent();
528 out << "}\n";
529 out << "return false;\n";
530 out.Dedent();
531 out << "}\n";
532
533 // definition for static member getDefaultImpl
534 out << "const std::shared_ptr<" << clazz << ">& " << clazz << "::getDefaultImpl() {\n";
535 out.Indent();
536 out << "return " << clazz << "::default_impl;\n";
537 out.Dedent();
538 out << "}\n";
539
540 // definition for the static field default_impl
541 out << "std::shared_ptr<" << clazz << "> " << clazz << "::default_impl = nullptr;\n";
542
543 // default implementation for the <Name>Default class members
544 const std::string defaultClazz = clazz + "Default";
545 for (const auto& method : defined_type.GetMethods()) {
546 if (method->IsUserDefined()) {
547 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
548 << NdkArgList(types, *method, FormatArgNameUnused) << ") {\n";
549 out.Indent();
550 out << "::ndk::ScopedAStatus _aidl_status;\n";
551 out << "_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));\n";
552 out << "return _aidl_status;\n";
553 out.Dedent();
554 out << "}\n";
555 } else {
556 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
557 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
558 << "int32_t* _aidl_return) {\n";
559 out.Indent();
560 out << "*_aidl_return = 0;\n";
561 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
562 out.Dedent();
563 out << "}\n";
564 }
565 }
566 }
567
568 out << "::ndk::SpAIBinder " << defaultClazz << "::asBinder() {\n";
569 out.Indent();
570 out << "return ::ndk::SpAIBinder();\n";
571 out.Dedent();
572 out << "}\n";
573
574 out << "bool " << defaultClazz << "::isRemote() {\n";
575 out.Indent();
576 out << "return false;\n";
577 out.Dedent();
578 out << "}\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700579}
Jiyong Park965c5b92018-11-21 13:37:15 +0900580
Steven Morelandb0057e72018-08-27 01:44:11 -0700581void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900582 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700583 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
584
585 out << "#pragma once\n\n";
Neil Fullercc14a582019-04-03 10:25:25 +0000586 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700587 << "\"\n";
588 out << "\n";
589 out << "#include <android/binder_ibinder.h>\n";
590 out << "\n";
591 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700592 out << "class " << clazz << " : public ::ndk::BpCInterface<"
Steven Morelandb0057e72018-08-27 01:44:11 -0700593 << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
594 out << "public:\n";
595 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800596 out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700597 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700598 out << "\n";
599 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700600 out << NdkMethodDecl(types, *method) << " override;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700601 }
Jiyong Park965c5b92018-11-21 13:37:15 +0900602
603 if (options.Version() > 0) {
Jeongik Cha8f553e72019-02-12 16:58:03 +0900604 out << "int32_t " << kCacheVariable << " = -1;\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900605 }
606
Steven Morelandb0057e72018-08-27 01:44:11 -0700607 out.Dedent();
608 out << "};\n";
609 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700610}
Jiyong Park965c5b92018-11-21 13:37:15 +0900611void GenerateServerHeader(CodeWriter& out, const AidlTypenames& types,
612 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700613 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900614 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700615
616 out << "#pragma once\n\n";
Neil Fullercc14a582019-04-03 10:25:25 +0000617 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700618 << "\"\n";
619 out << "\n";
620 out << "#include <android/binder_ibinder.h>\n";
621 out << "\n";
622 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900623 out << "class " << clazz << " : public ::ndk::BnCInterface<" << iface << "> {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700624 out << "public:\n";
625 out.Indent();
626 out << clazz << "();\n";
627 out << "virtual ~" << clazz << "();\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900628
629 // Declare the meta methods
630 for (const auto& method : defined_type.GetMethods()) {
631 if (method->IsUserDefined()) {
632 continue;
633 }
634 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
635 out << NdkMethodDecl(types, *method) << " final override;\n";
636 } else {
637 AIDL_FATAL(defined_type) << "Meta method '" << method->GetName() << "' is unimplemented.";
638 }
639 }
640
Steven Morelandb0057e72018-08-27 01:44:11 -0700641 out.Dedent();
Steven Moreland0cf3f082018-09-20 19:09:46 -0700642 out << "protected:\n";
643 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700644 out << "::ndk::SpAIBinder createBinder() override;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700645 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700646 out << "private:\n";
647 out.Indent();
648 out.Dedent();
649 out << "};\n";
650 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700651}
652void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900653 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700654 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
655
656 out << "#pragma once\n\n";
657 out << "#include <android/binder_interface_utils.h>\n";
658 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700659
660 GenerateHeaderIncludes(out, types, defined_type);
661 out << "\n";
662
Steven Morelandb0057e72018-08-27 01:44:11 -0700663 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700664 out << "class " << clazz << " : public ::ndk::ICInterface {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700665 out << "public:\n";
666 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900667 out << "static const char* " << kDescriptor << ";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700668 out << clazz << "();\n";
669 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700670 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700671 GenerateConstantDeclarations(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900672 if (options.Version() > 0) {
673 out << "static const int32_t " << kVersion << " = " << std::to_string(options.Version())
674 << ";\n";
675 }
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700676 out << "\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800677 out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700678 out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz
679 << ">& instance);";
Jiyong Park1b88cce2018-11-19 19:53:44 +0900680 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700681 out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz
682 << ">* instance);";
683 out << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900684 out << "static bool setDefaultImpl(std::shared_ptr<" << clazz << "> impl);";
685 out << "\n";
686 out << "static const std::shared_ptr<" << clazz << ">& getDefaultImpl();";
687 out << "\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700688 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700689 out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700690 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700691 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900692 out << "private:\n";
693 out.Indent();
694 out << "static std::shared_ptr<" << clazz << "> default_impl;\n";
695 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700696 out << "};\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900697
698 const std::string defaultClazz = clazz + "Default";
699
700 out << "class " << defaultClazz << " : public " << clazz << " {\n";
701 out << "public:\n";
702 out.Indent();
703 for (const auto& method : defined_type.GetMethods()) {
704 if (method->IsUserDefined()) {
705 out << NdkMethodDecl(types, *method) << " override;\n";
706 } else if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
707 out << NdkMethodDecl(types, *method) << " override;\n";
708 }
709 }
710 out << "::ndk::SpAIBinder asBinder() override;\n";
711 out << "bool isRemote() override;\n";
712 out.Dedent();
713 out << "};\n";
714
Steven Morelandb0057e72018-08-27 01:44:11 -0700715 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700716}
717void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700718 const AidlStructuredParcelable& defined_type,
719 const Options& /*options*/) {
720 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
721
Steven Morelandb0057e72018-08-27 01:44:11 -0700722 out << "#pragma once\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700723 out << "#include <android/binder_interface_utils.h>\n";
724 out << "\n";
725
726 GenerateHeaderIncludes(out, types, defined_type);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700727
Steven Morelandb0057e72018-08-27 01:44:11 -0700728 EnterNdkNamespace(out, defined_type);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700729 out << "class " << clazz << " {\n";
730 out << "public:\n";
731 out.Indent();
732 out << "static const char* descriptor;\n";
733 out << "\n";
734 for (const auto& variable : defined_type.GetFields()) {
735 out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName();
736 if (variable->GetDefaultValue()) {
737 out << " = " << variable->ValueString(AidlConstantValueDecorator);
738 }
739 out << ";\n";
740 }
741 out << "\n";
742 out << "binder_status_t readFromParcel(const AParcel* parcel);\n";
743 out << "binder_status_t writeToParcel(AParcel* parcel) const;\n";
744 out.Dedent();
745 out << "};\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700746 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700747}
748void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700749 const AidlStructuredParcelable& defined_type,
750 const Options& /*options*/) {
751 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
752
Neil Fullercc14a582019-04-03 10:25:25 +0000753 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::BASE, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700754 << "\"\n";
755 out << "\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700756 GenerateSourceIncludes(out, types, defined_type);
757 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700758 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900759 out << "const char* " << clazz << "::" << kDescriptor << " = \""
760 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700761 out << "\n";
762
763 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n";
764 out.Indent();
765 out << "std::string _aidl_descriptor;\n";
766 out << "binder_status_t _aidl_ret_status;\n";
767
768 out << "int32_t _aidl_null;\n";
Jeongik Cha95eba572018-11-22 09:14:52 +0900769 out << "int32_t _aidl_parcelable_size;\n";
770 out << "int32_t _aidl_start_pos;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700771 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_null);\n";
772 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900773 out << "_aidl_start_pos = AParcel_getDataPosition(parcel);\n";
774 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_parcelable_size);\n";
775 out << "if (_aidl_parcelable_size < 0) return STATUS_BAD_VALUE;\n";
776 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700777
778 // TODO(b/117281836)
779 out << "if (_aidl_null == 0) return STATUS_UNEXPECTED_NULL;\n\n";
780
781 for (const auto& variable : defined_type.GetFields()) {
782 out << "_aidl_ret_status = ";
783 ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()});
784 out << ";\n";
785 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900786 out << "if (AParcel_getDataPosition(parcel) - _aidl_start_pos >= _aidl_parcelable_size) {\n"
787 << " AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
788 << " return _aidl_ret_status;\n"
789 << "}\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700790 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900791 out << "AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
792 << "return _aidl_ret_status;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700793 out.Dedent();
794 out << "}\n";
795
796 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n";
797 out.Indent();
798 out << "binder_status_t _aidl_ret_status;\n";
799
800 // non-null
801 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 1);\n";
802 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900803 out << "size_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n";
804 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 0);\n";
805 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700806
807 for (const auto& variable : defined_type.GetFields()) {
808 out << "_aidl_ret_status = ";
809 WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()});
810 out << ";\n";
811 StatusCheckReturn(out);
812 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900813 out << "size_t _aidl_end_pos = AParcel_getDataPosition(parcel);\n";
814 out << "AParcel_setDataPosition(parcel, _aidl_start_pos);\n";
815 out << "AParcel_writeInt32(parcel, _aidl_end_pos - _aidl_start_pos);\n";
816 out << "AParcel_setDataPosition(parcel, _aidl_end_pos);\n";
817
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700818 out << "return _aidl_ret_status;\n";
819 out.Dedent();
820 out << "}\n";
821 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700822 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700823}
824} // namespace internals
825} // namespace ndk
826} // namespace aidl
827} // namespace android