blob: cafd794d2008831b42428041b1630240cebdbcf7 [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) {
Jiyong Park081b90a2019-04-03 20:05:01 +090041 const string i_header = options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW);
Steven Morelandb0057e72018-08-27 01:44:11 -070042 unique_ptr<CodeWriter> i_writer(io_delegate.GetCodeWriter(i_header));
43 GenerateInterfaceHeader(*i_writer, types, defined_type, options);
44 CHECK(i_writer->Close());
45
Steven Moreland7c933372018-10-11 15:20:04 -070046 const string bp_header =
47 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -070048 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
49 GenerateClientHeader(*bp_writer, types, defined_type, options);
50 CHECK(bp_writer->Close());
51
Steven Moreland7c933372018-10-11 15:20:04 -070052 const string bn_header =
53 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER);
Steven Morelandb0057e72018-08-27 01:44:11 -070054 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
55 GenerateServerHeader(*bn_writer, types, defined_type, options);
56 CHECK(bn_writer->Close());
57
58 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
59 GenerateSource(*source_writer, types, defined_type, options);
60 CHECK(source_writer->Close());
61}
62
63void GenerateNdkParcel(const string& output_file, const Options& options,
64 const AidlTypenames& types, const AidlStructuredParcelable& defined_type,
65 const IoDelegate& io_delegate) {
Steven Moreland7c933372018-10-11 15:20:04 -070066 const string header_path =
Jiyong Park081b90a2019-04-03 20:05:01 +090067 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW);
Steven Morelandb0057e72018-08-27 01:44:11 -070068 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
69 GenerateParcelHeader(*header_writer, types, defined_type, options);
70 CHECK(header_writer->Close());
71
Steven Moreland7c933372018-10-11 15:20:04 -070072 const string bp_header =
73 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -070074 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
75 *bp_writer << "#error TODO(b/111362593) defined_types do not have bp classes\n";
76 CHECK(bp_writer->Close());
77
Steven Moreland7c933372018-10-11 15:20:04 -070078 const string bn_header =
79 options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER);
Steven Morelandb0057e72018-08-27 01:44:11 -070080 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
81 *bn_writer << "#error TODO(b/111362593) defined_types do not have bn classes\n";
82 CHECK(bn_writer->Close());
83
84 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
85 GenerateParcelSource(*source_writer, types, defined_type, options);
86 CHECK(source_writer->Close());
87}
88
Steven Moreland2a9a7d62019-02-05 16:11:54 -080089void GenerateNdkParcelDeclaration(const std::string& filename, const IoDelegate& io_delegate) {
90 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
91 *code_writer
92 << "// This file is intentionally left blank as placeholder for parcel declaration.\n";
93 CHECK(code_writer->Close());
94}
95
Steven Morelandb0057e72018-08-27 01:44:11 -070096void GenerateNdk(const string& output_file, const Options& options, const AidlTypenames& types,
97 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
98 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
99 if (parcelable != nullptr) {
100 GenerateNdkParcel(output_file, options, types, *parcelable, io_delegate);
101 return;
102 }
103
Steven Moreland2a9a7d62019-02-05 16:11:54 -0800104 const AidlParcelable* parcelable_decl = defined_type.AsParcelable();
105 if (parcelable_decl != nullptr) {
106 GenerateNdkParcelDeclaration(output_file, io_delegate);
107 return;
108 }
109
Steven Morelandb0057e72018-08-27 01:44:11 -0700110 const AidlInterface* interface = defined_type.AsInterface();
111 if (interface != nullptr) {
112 GenerateNdkInterface(output_file, options, types, *interface, io_delegate);
113 return;
114 }
115
116 CHECK(false) << "Unrecognized type sent for cpp generation.";
117}
118namespace internals {
119
120void EnterNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
121 out << "namespace aidl {\n";
122 cpp::EnterNamespace(out, defined_type);
123}
124void LeaveNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
125 cpp::LeaveNamespace(out, defined_type);
126 out << "} // namespace aidl\n";
127}
128
Steven Morelandaada3422018-09-20 15:55:33 -0700129static void StatusCheckGoto(CodeWriter& out) {
130 out << "if (_aidl_ret_status != STATUS_OK) goto _aidl_error;\n\n";
131}
132static void StatusCheckBreak(CodeWriter& out) {
133 out << "if (_aidl_ret_status != STATUS_OK) break;\n\n";
134}
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700135static void StatusCheckReturn(CodeWriter& out) {
136 out << "if (_aidl_ret_status != STATUS_OK) return _aidl_ret_status;\n\n";
137}
Steven Morelandaada3422018-09-20 15:55:33 -0700138
Steven Moreland2bea13b2018-10-03 15:12:33 -0700139static void GenerateHeaderIncludes(CodeWriter& out, const AidlTypenames& types,
140 const AidlDefinedType& defined_type) {
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700141 out << "#include <android/binder_parcel_utils.h>\n";
142
Steven Moreland2bea13b2018-10-03 15:12:33 -0700143 types.IterateTypes([&](const AidlDefinedType& other_defined_type) {
144 if (&other_defined_type == &defined_type) return;
145
146 if (other_defined_type.AsInterface() != nullptr) {
147 out << "#include <"
Jiyong Park081b90a2019-04-03 20:05:01 +0900148 << NdkHeaderFile(other_defined_type, ClassNames::RAW, false /*use_os_sep*/) << ">\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700149 } else if (other_defined_type.AsStructuredParcelable() != nullptr) {
150 out << "#include <"
151 << NdkHeaderFile(other_defined_type, ClassNames::BASE, false /*use_os_sep*/) << ">\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700152 } else if (other_defined_type.AsParcelable() != nullptr) {
153 out << "#include \"" << other_defined_type.AsParcelable()->GetCppHeader() << "\"\n";
154 } else {
155 AIDL_FATAL(defined_type) << "Unrecognized type.";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700156 }
157 });
158}
159static void GenerateSourceIncludes(CodeWriter& out, const AidlTypenames& types,
160 const AidlDefinedType& /*defined_type*/) {
161 types.IterateTypes([&](const AidlDefinedType& a_defined_type) {
162 if (a_defined_type.AsInterface() != nullptr) {
Steven Moreland7c933372018-10-11 15:20:04 -0700163 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::CLIENT, false /*use_os_sep*/)
Steven Moreland2bea13b2018-10-03 15:12:33 -0700164 << ">\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700165 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::SERVER, false /*use_os_sep*/)
Steven Moreland2bea13b2018-10-03 15:12:33 -0700166 << ">\n";
Jiyong Park081b90a2019-04-03 20:05:01 +0900167 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::RAW, false /*use_os_sep*/)
168 << ">\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700169 }
170 });
171}
172
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700173static void GenerateConstantDeclarations(CodeWriter& out, const AidlInterface& interface) {
174 for (const auto& constant : interface.GetConstantDeclarations()) {
175 const AidlConstantValue& value = constant->GetValue();
176 if (value.GetType() == AidlConstantValue::Type::STRING) {
177 out << "static const char* " << constant->GetName() << ";\n";
178 }
179 }
180 out << "\n";
181
182 bool hasIntegralConstant = false;
183 for (const auto& constant : interface.GetConstantDeclarations()) {
184 const AidlConstantValue& value = constant->GetValue();
185 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
186 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
187 hasIntegralConstant = true;
188 break;
189 }
190 }
191
192 if (hasIntegralConstant) {
193 out << "enum : int32_t {\n";
194 out.Indent();
195 for (const auto& constant : interface.GetConstantDeclarations()) {
196 const AidlConstantValue& value = constant->GetValue();
197 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
198 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
199 out << constant->GetName() << " = " << constant->ValueString(AidlConstantValueDecorator)
200 << ",\n";
201 }
202 }
203 out.Dedent();
204 out << "};\n";
205 }
206}
207static void GenerateConstantDefinitions(CodeWriter& out, const AidlInterface& interface) {
208 const std::string clazz = ClassName(interface, ClassNames::INTERFACE);
209
210 for (const auto& constant : interface.GetConstantDeclarations()) {
211 const AidlConstantValue& value = constant->GetValue();
212 if (value.GetType() == AidlConstantValue::Type::STRING) {
213 out << "const char* " << clazz << "::" << constant->GetName() << " = "
214 << constant->ValueString(AidlConstantValueDecorator) << ";\n";
215 }
216 }
217}
218
Steven Morelandb0057e72018-08-27 01:44:11 -0700219void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
220 const Options& options) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700221 GenerateSourceIncludes(out, types, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700222 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700223
Steven Morelandb0057e72018-08-27 01:44:11 -0700224 EnterNdkNamespace(out, defined_type);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700225 GenerateClassSource(out, types, defined_type, options);
Steven Morelandb0057e72018-08-27 01:44:11 -0700226 GenerateClientSource(out, types, defined_type, options);
227 GenerateServerSource(out, types, defined_type, options);
228 GenerateInterfaceSource(out, types, defined_type, options);
229 LeaveNdkNamespace(out, defined_type);
230}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700231
Steven Morelandaada3422018-09-20 15:55:33 -0700232static std::string MethodId(const AidlMethod& m) {
233 return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
234}
235
Jeongik Cha8f553e72019-02-12 16:58:03 +0900236static void GenerateClientMethodDefinition(
237 CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
238 const AidlMethod& method, const std::optional<std::string> return_value_cached_to) {
Steven Morelandaada3422018-09-20 15:55:33 -0700239 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
240
Steven Moreland2bea13b2018-10-03 15:12:33 -0700241 out << NdkMethodDecl(types, method, clazz) << " {\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700242 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700243 out << "binder_status_t _aidl_ret_status = STATUS_OK;\n";
Steven Moreland63404532018-10-08 14:31:00 -0700244 out << "::ndk::ScopedAStatus _aidl_status;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900245
Jeongik Cha8f553e72019-02-12 16:58:03 +0900246 if (return_value_cached_to) {
247 out << "if (" << *return_value_cached_to << " != -1) {\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900248 out.Indent();
Jeongik Cha8f553e72019-02-12 16:58:03 +0900249 out << "*_aidl_return = " << *return_value_cached_to << ";\n"
Jeongik Chac9972922019-02-11 12:41:16 +0900250 << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"
251 << "return _aidl_status;\n";
252 out.Dedent();
253 out << "}\n";
254 }
255 out << "::ndk::ScopedAParcel _aidl_in;\n";
256 out << "::ndk::ScopedAParcel _aidl_out;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700257 out << "\n";
258
259 out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n";
260 StatusCheckGoto(out);
261
Steven Morelandeb38ee72018-10-15 14:20:04 -0700262 for (const auto& arg : method.GetArguments()) {
263 const std::string var_name = cpp::BuildVarName(*arg);
264
265 if (arg->IsIn()) {
266 out << "_aidl_ret_status = ";
267 const std::string prefix = (arg->IsOut() ? "*" : "");
268 WriteToParcelFor({out, types, arg->GetType(), "_aidl_in.get()", prefix + var_name});
269 out << ";\n";
270 StatusCheckGoto(out);
271 } else if (arg->IsOut() && arg->GetType().IsArray()) {
272 out << "_aidl_ret_status = ::ndk::AParcel_writeVectorSize(_aidl_in.get(), *" << var_name
273 << ");\n";
274 }
Steven Morelandaada3422018-09-20 15:55:33 -0700275 }
276 out << "_aidl_ret_status = AIBinder_transact(\n";
277 out.Indent();
278 out << "asBinder().get(),\n";
279 out << MethodId(method) << ",\n";
280 out << "_aidl_in.getR(),\n";
281 out << "_aidl_out.getR(),\n";
282 out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << ");\n";
283 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900284
285 // If the method is not implmented in the server side but the client has
286 // provided the default implementation, call it instead of failing hard.
287 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
288 out << "if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && ";
289 out << iface << "::getDefaultImpl()) {\n";
290 out.Indent();
291 out << "return " << iface << "::getDefaultImpl()->" << method.GetName() << "(";
292 out << NdkArgList(types, method, FormatArgNameOnly) << ");\n";
293 out.Dedent();
294 out << "}\n";
295
Steven Morelandaada3422018-09-20 15:55:33 -0700296 StatusCheckGoto(out);
297
298 if (!method.IsOneway()) {
299 out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n";
300 StatusCheckGoto(out);
301
302 out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n";
303 }
304
Steven Morelandaada3422018-09-20 15:55:33 -0700305 if (method.GetType().GetName() != "void") {
306 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700307 ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700308 out << ";\n";
309 StatusCheckGoto(out);
Jeongik Cha8f553e72019-02-12 16:58:03 +0900310 if (return_value_cached_to) {
311 out << *return_value_cached_to << " = *_aidl_return;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900312 }
Steven Morelandaada3422018-09-20 15:55:33 -0700313 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700314 for (const AidlArgument* arg : method.GetOutArguments()) {
315 out << "_aidl_ret_status = ";
316 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)});
317 out << ";\n";
318 StatusCheckGoto(out);
319 }
Steven Morelandaada3422018-09-20 15:55:33 -0700320
321 out << "_aidl_error:\n";
322 out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n";
323 out << "return _aidl_status;\n";
324 out.Dedent();
325 out << "}\n";
326}
327
Steven Moreland2bea13b2018-10-03 15:12:33 -0700328static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types,
329 const AidlInterface& /*defined_type*/,
Steven Morelandaada3422018-09-20 15:55:33 -0700330 const AidlMethod& method) {
331 out << "case " << MethodId(method) << ": {\n";
332 out.Indent();
333 for (const auto& arg : method.GetArguments()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700334 out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg)
335 << ";\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700336 }
337 if (method.GetType().GetName() != "void") {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700338 out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700339 }
340 out << "\n";
341
Steven Morelandeb38ee72018-10-15 14:20:04 -0700342 for (const auto& arg : method.GetArguments()) {
343 const std::string var_name = cpp::BuildVarName(*arg);
344
345 if (arg->IsIn()) {
346 out << "_aidl_ret_status = ";
347 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + var_name});
348 out << ";\n";
349 StatusCheckBreak(out);
350 } else if (arg->IsOut() && arg->GetType().IsArray()) {
351 out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n";
352 }
Steven Morelandaada3422018-09-20 15:55:33 -0700353 }
354
Steven Moreland63404532018-10-08 14:31:00 -0700355 out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "("
Jiyong Park965c5b92018-11-21 13:37:15 +0900356 << NdkArgList(types, method, FormatArgForCall) << ");\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700357
358 if (method.IsOneway()) {
359 // For a oneway transaction, the kernel will have already returned a result. This is for the
360 // in-process case when a oneway transaction is parceled/unparceled in the same process.
361 out << "_aidl_ret_status = STATUS_OK;\n";
362 } else {
363 out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n";
364 StatusCheckBreak(out);
365
366 out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n";
367
Steven Morelandaada3422018-09-20 15:55:33 -0700368 if (method.GetType().GetName() != "void") {
369 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700370 WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700371 out << ";\n";
372 StatusCheckBreak(out);
373 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700374 for (const AidlArgument* arg : method.GetOutArguments()) {
375 out << "_aidl_ret_status = ";
376 WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)});
377 out << ";\n";
378 StatusCheckBreak(out);
379 }
Steven Morelandaada3422018-09-20 15:55:33 -0700380 }
381
382 out << "break;\n";
383 out.Dedent();
384 out << "}\n";
385}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700386
Steven Moreland2bea13b2018-10-03 15:12:33 -0700387void GenerateClassSource(CodeWriter& out, const AidlTypenames& types,
Steven Moreland0cf3f082018-09-20 19:09:46 -0700388 const AidlInterface& defined_type, const Options& /*options*/) {
389 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
390 const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700391
Steven Moreland15b3ba72019-03-06 13:17:08 -0800392 out << "static binder_status_t "
393 << "_aidl_onTransact"
Steven Morelandaada3422018-09-20 15:55:33 -0700394 << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, "
395 "AParcel* _aidl_out) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700396 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700397 out << "(void)_aidl_in;\n";
398 out << "(void)_aidl_out;\n";
399 out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n";
400 if (!defined_type.GetMethods().empty()) {
Steven Moreland15b3ba72019-03-06 13:17:08 -0800401 // we know this cast is valid because this method is only called by the ICInterface
402 // AIBinder_Class object which is associated with this class.
403 out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz
404 << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700405 out << "switch (_aidl_code) {\n";
406 out.Indent();
407 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700408 GenerateServerCaseDefinition(out, types, defined_type, *method);
Steven Morelandaada3422018-09-20 15:55:33 -0700409 }
410 out.Dedent();
411 out << "}\n";
412 } else {
413 out << "(void)_aidl_binder;\n";
414 out << "(void)_aidl_code;\n";
415 }
416 out << "return _aidl_ret_status;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700417 out.Dedent();
418 out << "};\n\n";
419
Steven Moreland15b3ba72019-03-06 13:17:08 -0800420 out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz
421 << "::" << kDescriptor << ", _aidl_onTransact);\n\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700422}
Steven Morelandb0057e72018-08-27 01:44:11 -0700423void GenerateClientSource(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900424 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700425 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700426
Steven Moreland63404532018-10-08 14:31:00 -0700427 out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700428 out << clazz << "::~" << clazz << "() {}\n";
429 out << "\n";
430 for (const auto& method : defined_type.GetMethods()) {
Jeongik Chac9972922019-02-11 12:41:16 +0900431 // Only getInterfaceVersion can use cache.
432 const bool cacheable = !method->IsUserDefined() && method->GetName() == kGetInterfaceVersion &&
433 options.Version() > 0;
Jeongik Cha8f553e72019-02-12 16:58:03 +0900434 const auto return_value_cached_to =
435 cacheable ? std::make_optional<std::string>(kCacheVariable) : std::nullopt;
436 GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to);
Steven Morelandaada3422018-09-20 15:55:33 -0700437 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700438}
Jiyong Park965c5b92018-11-21 13:37:15 +0900439void GenerateServerSource(CodeWriter& out, const AidlTypenames& types,
440 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700441 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900442 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700443
444 out << "// Source for " << clazz << "\n";
445 out << clazz << "::" << clazz << "() {}\n";
446 out << clazz << "::~" << clazz << "() {}\n";
447
Steven Moreland63404532018-10-08 14:31:00 -0700448 out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700449 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800450 out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n";
Steven Moreland63404532018-10-08 14:31:00 -0700451 out << "return ::ndk::SpAIBinder(binder);\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700452 out.Dedent();
453 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900454
455 // Implement the meta methods
456 for (const auto& method : defined_type.GetMethods()) {
457 if (method->IsUserDefined()) {
458 continue;
459 }
460 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
461 out << NdkMethodDecl(types, *method, clazz) << " {\n";
462 out.Indent();
463 out << "*_aidl_return = " << iface << "::" << kVersion << ";\n";
464 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
465 out.Dedent();
466 out << "}\n";
467 }
468 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700469}
Jiyong Park965c5b92018-11-21 13:37:15 +0900470void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types,
471 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700472 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
Steven Moreland15b3ba72019-03-06 13:17:08 -0800473 const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -0700474
475 out << "// Source for " << clazz << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900476 out << "const char* " << clazz << "::" << kDescriptor << " = \""
477 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700478 out << clazz << "::" << clazz << "() {}\n";
479 out << clazz << "::~" << clazz << "() {}\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700480 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700481 GenerateConstantDefinitions(out, defined_type);
482 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700483
Steven Moreland3c316ed2019-01-17 09:39:37 -0800484 out << "std::shared_ptr<" << clazz << "> " << clazz
485 << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800486 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800487 out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n";
488 out << "std::shared_ptr<::ndk::ICInterface> interface = "
489 "::ndk::ICInterface::asInterface(binder.get());\n";
490 out << "if (interface) {\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800491 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800492 out << "return std::static_pointer_cast<" << clazz << ">(interface);\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800493 out.Dedent();
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800494 out << "}\n";
Steven Moreland15b3ba72019-03-06 13:17:08 -0800495 out << "return (new " << bp_clazz << "(binder))->ref<" << clazz << ">();\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800496 out.Dedent();
Steven Moreland3c316ed2019-01-17 09:39:37 -0800497 out << "}\n\n";
498
Steven Moreland2bea13b2018-10-03 15:12:33 -0700499 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<"
500 << clazz << ">& instance) {\n";
501 out.Indent();
502 out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : "
503 "nullptr);\n";
504 out.Dedent();
505 out << "}\n";
506
507 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<"
508 << clazz << ">* instance) {\n";
509 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700510 out << "::ndk::SpAIBinder binder;\n";
Steven Moreland055d8792018-11-14 12:48:42 -0800511 out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700512 out << "if (status != STATUS_OK) return status;\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800513 out << "*instance = " << clazz << "::fromBinder(binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700514 out << "return STATUS_OK;\n";
515 out.Dedent();
516 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900517
518 // defintion for static member setDefaultImpl
519 out << "bool " << clazz << "::setDefaultImpl(std::shared_ptr<" << clazz << "> impl) {\n";
520 out.Indent();
521 out << "if (!" << clazz << "::default_impl && impl) {\n";
522 out.Indent();
523 out << clazz << "::default_impl = impl;\n";
524 out << "return true;\n";
525 out.Dedent();
526 out << "}\n";
527 out << "return false;\n";
528 out.Dedent();
529 out << "}\n";
530
531 // definition for static member getDefaultImpl
532 out << "const std::shared_ptr<" << clazz << ">& " << clazz << "::getDefaultImpl() {\n";
533 out.Indent();
534 out << "return " << clazz << "::default_impl;\n";
535 out.Dedent();
536 out << "}\n";
537
538 // definition for the static field default_impl
539 out << "std::shared_ptr<" << clazz << "> " << clazz << "::default_impl = nullptr;\n";
540
541 // default implementation for the <Name>Default class members
542 const std::string defaultClazz = clazz + "Default";
543 for (const auto& method : defined_type.GetMethods()) {
544 if (method->IsUserDefined()) {
545 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
546 << NdkArgList(types, *method, FormatArgNameUnused) << ") {\n";
547 out.Indent();
548 out << "::ndk::ScopedAStatus _aidl_status;\n";
549 out << "_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));\n";
550 out << "return _aidl_status;\n";
551 out.Dedent();
552 out << "}\n";
553 } else {
554 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
555 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
556 << "int32_t* _aidl_return) {\n";
557 out.Indent();
558 out << "*_aidl_return = 0;\n";
559 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
560 out.Dedent();
561 out << "}\n";
562 }
563 }
564 }
565
566 out << "::ndk::SpAIBinder " << defaultClazz << "::asBinder() {\n";
567 out.Indent();
568 out << "return ::ndk::SpAIBinder();\n";
569 out.Dedent();
570 out << "}\n";
571
572 out << "bool " << defaultClazz << "::isRemote() {\n";
573 out.Indent();
574 out << "return false;\n";
575 out.Dedent();
576 out << "}\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700577}
Jiyong Park965c5b92018-11-21 13:37:15 +0900578
Steven Morelandb0057e72018-08-27 01:44:11 -0700579void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900580 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700581 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
582
583 out << "#pragma once\n\n";
Jiyong Park081b90a2019-04-03 20:05:01 +0900584 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700585 << "\"\n";
586 out << "\n";
587 out << "#include <android/binder_ibinder.h>\n";
588 out << "\n";
589 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700590 out << "class " << clazz << " : public ::ndk::BpCInterface<"
Steven Morelandb0057e72018-08-27 01:44:11 -0700591 << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
592 out << "public:\n";
593 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800594 out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700595 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700596 out << "\n";
597 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700598 out << NdkMethodDecl(types, *method) << " override;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700599 }
Jiyong Park965c5b92018-11-21 13:37:15 +0900600
601 if (options.Version() > 0) {
Jeongik Cha8f553e72019-02-12 16:58:03 +0900602 out << "int32_t " << kCacheVariable << " = -1;\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900603 }
604
Steven Morelandb0057e72018-08-27 01:44:11 -0700605 out.Dedent();
606 out << "};\n";
607 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700608}
Jiyong Park965c5b92018-11-21 13:37:15 +0900609void GenerateServerHeader(CodeWriter& out, const AidlTypenames& types,
610 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700611 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900612 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700613
614 out << "#pragma once\n\n";
Jiyong Park081b90a2019-04-03 20:05:01 +0900615 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700616 << "\"\n";
617 out << "\n";
618 out << "#include <android/binder_ibinder.h>\n";
619 out << "\n";
620 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900621 out << "class " << clazz << " : public ::ndk::BnCInterface<" << iface << "> {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700622 out << "public:\n";
623 out.Indent();
624 out << clazz << "();\n";
625 out << "virtual ~" << clazz << "();\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900626
627 // Declare the meta methods
628 for (const auto& method : defined_type.GetMethods()) {
629 if (method->IsUserDefined()) {
630 continue;
631 }
632 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
633 out << NdkMethodDecl(types, *method) << " final override;\n";
634 } else {
635 AIDL_FATAL(defined_type) << "Meta method '" << method->GetName() << "' is unimplemented.";
636 }
637 }
638
Steven Morelandb0057e72018-08-27 01:44:11 -0700639 out.Dedent();
Steven Moreland0cf3f082018-09-20 19:09:46 -0700640 out << "protected:\n";
641 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700642 out << "::ndk::SpAIBinder createBinder() override;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700643 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700644 out << "private:\n";
645 out.Indent();
646 out.Dedent();
647 out << "};\n";
648 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700649}
650void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900651 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700652 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
653
654 out << "#pragma once\n\n";
655 out << "#include <android/binder_interface_utils.h>\n";
656 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700657
658 GenerateHeaderIncludes(out, types, defined_type);
659 out << "\n";
660
Steven Morelandb0057e72018-08-27 01:44:11 -0700661 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700662 out << "class " << clazz << " : public ::ndk::ICInterface {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700663 out << "public:\n";
664 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900665 out << "static const char* " << kDescriptor << ";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700666 out << clazz << "();\n";
667 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700668 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700669 GenerateConstantDeclarations(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900670 if (options.Version() > 0) {
671 out << "static const int32_t " << kVersion << " = " << std::to_string(options.Version())
672 << ";\n";
673 }
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700674 out << "\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800675 out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700676 out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz
677 << ">& instance);";
Jiyong Park1b88cce2018-11-19 19:53:44 +0900678 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700679 out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz
680 << ">* instance);";
681 out << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900682 out << "static bool setDefaultImpl(std::shared_ptr<" << clazz << "> impl);";
683 out << "\n";
684 out << "static const std::shared_ptr<" << clazz << ">& getDefaultImpl();";
685 out << "\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700686 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700687 out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700688 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700689 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900690 out << "private:\n";
691 out.Indent();
692 out << "static std::shared_ptr<" << clazz << "> default_impl;\n";
693 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700694 out << "};\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900695
696 const std::string defaultClazz = clazz + "Default";
697
698 out << "class " << defaultClazz << " : public " << clazz << " {\n";
699 out << "public:\n";
700 out.Indent();
701 for (const auto& method : defined_type.GetMethods()) {
702 if (method->IsUserDefined()) {
703 out << NdkMethodDecl(types, *method) << " override;\n";
704 } else if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
705 out << NdkMethodDecl(types, *method) << " override;\n";
706 }
707 }
708 out << "::ndk::SpAIBinder asBinder() override;\n";
709 out << "bool isRemote() override;\n";
710 out.Dedent();
711 out << "};\n";
712
Steven Morelandb0057e72018-08-27 01:44:11 -0700713 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700714}
715void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700716 const AidlStructuredParcelable& defined_type,
717 const Options& /*options*/) {
718 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
719
Steven Morelandb0057e72018-08-27 01:44:11 -0700720 out << "#pragma once\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700721 out << "#include <android/binder_interface_utils.h>\n";
722 out << "\n";
723
724 GenerateHeaderIncludes(out, types, defined_type);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700725
Steven Morelandb0057e72018-08-27 01:44:11 -0700726 EnterNdkNamespace(out, defined_type);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700727 out << "class " << clazz << " {\n";
728 out << "public:\n";
729 out.Indent();
730 out << "static const char* descriptor;\n";
731 out << "\n";
732 for (const auto& variable : defined_type.GetFields()) {
733 out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName();
734 if (variable->GetDefaultValue()) {
735 out << " = " << variable->ValueString(AidlConstantValueDecorator);
736 }
737 out << ";\n";
738 }
739 out << "\n";
740 out << "binder_status_t readFromParcel(const AParcel* parcel);\n";
741 out << "binder_status_t writeToParcel(AParcel* parcel) const;\n";
742 out.Dedent();
743 out << "};\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700744 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700745}
746void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700747 const AidlStructuredParcelable& defined_type,
748 const Options& /*options*/) {
749 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
750
Jiyong Park081b90a2019-04-03 20:05:01 +0900751 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700752 << "\"\n";
753 out << "\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700754 GenerateSourceIncludes(out, types, defined_type);
755 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700756 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900757 out << "const char* " << clazz << "::" << kDescriptor << " = \""
758 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700759 out << "\n";
760
761 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n";
762 out.Indent();
763 out << "std::string _aidl_descriptor;\n";
764 out << "binder_status_t _aidl_ret_status;\n";
765
766 out << "int32_t _aidl_null;\n";
Jeongik Cha95eba572018-11-22 09:14:52 +0900767 out << "int32_t _aidl_parcelable_size;\n";
768 out << "int32_t _aidl_start_pos;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700769 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_null);\n";
770 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900771 out << "_aidl_start_pos = AParcel_getDataPosition(parcel);\n";
772 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_parcelable_size);\n";
773 out << "if (_aidl_parcelable_size < 0) return STATUS_BAD_VALUE;\n";
774 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700775
776 // TODO(b/117281836)
777 out << "if (_aidl_null == 0) return STATUS_UNEXPECTED_NULL;\n\n";
778
779 for (const auto& variable : defined_type.GetFields()) {
780 out << "_aidl_ret_status = ";
781 ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()});
782 out << ";\n";
783 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900784 out << "if (AParcel_getDataPosition(parcel) - _aidl_start_pos >= _aidl_parcelable_size) {\n"
785 << " AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
786 << " return _aidl_ret_status;\n"
787 << "}\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700788 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900789 out << "AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
790 << "return _aidl_ret_status;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700791 out.Dedent();
792 out << "}\n";
793
794 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n";
795 out.Indent();
796 out << "binder_status_t _aidl_ret_status;\n";
797
798 // non-null
799 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 1);\n";
800 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900801 out << "size_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n";
802 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 0);\n";
803 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700804
805 for (const auto& variable : defined_type.GetFields()) {
806 out << "_aidl_ret_status = ";
807 WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()});
808 out << ";\n";
809 StatusCheckReturn(out);
810 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900811 out << "size_t _aidl_end_pos = AParcel_getDataPosition(parcel);\n";
812 out << "AParcel_setDataPosition(parcel, _aidl_start_pos);\n";
813 out << "AParcel_writeInt32(parcel, _aidl_end_pos - _aidl_start_pos);\n";
814 out << "AParcel_setDataPosition(parcel, _aidl_end_pos);\n";
815
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700816 out << "return _aidl_ret_status;\n";
817 out.Dedent();
818 out << "}\n";
819 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700820 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700821}
822} // namespace internals
823} // namespace ndk
824} // namespace aidl
825} // namespace android