blob: f38286136818308f5db6ec4074bd3911078bd9d8 [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 Cha9d730bb2019-04-18 13:44:26 +0900236static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types,
237 const AidlInterface& defined_type,
238 const AidlMethod& method,
239 const std::optional<std::string> return_value_cached_to,
240 const Options& options) {
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
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900261 if (options.GenLog()) {
262 out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::CLIENT), method,
263 false /* isServer */, true /* isNdk */);
264 }
265
Steven Morelandaada3422018-09-20 15:55:33 -0700266 out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n";
267 StatusCheckGoto(out);
268
Steven Morelandeb38ee72018-10-15 14:20:04 -0700269 for (const auto& arg : method.GetArguments()) {
270 const std::string var_name = cpp::BuildVarName(*arg);
271
272 if (arg->IsIn()) {
273 out << "_aidl_ret_status = ";
274 const std::string prefix = (arg->IsOut() ? "*" : "");
275 WriteToParcelFor({out, types, arg->GetType(), "_aidl_in.get()", prefix + var_name});
276 out << ";\n";
277 StatusCheckGoto(out);
278 } else if (arg->IsOut() && arg->GetType().IsArray()) {
279 out << "_aidl_ret_status = ::ndk::AParcel_writeVectorSize(_aidl_in.get(), *" << var_name
280 << ");\n";
281 }
Steven Morelandaada3422018-09-20 15:55:33 -0700282 }
283 out << "_aidl_ret_status = AIBinder_transact(\n";
284 out.Indent();
285 out << "asBinder().get(),\n";
286 out << MethodId(method) << ",\n";
287 out << "_aidl_in.getR(),\n";
288 out << "_aidl_out.getR(),\n";
289 out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << ");\n";
290 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900291
292 // If the method is not implmented in the server side but the client has
293 // provided the default implementation, call it instead of failing hard.
294 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
295 out << "if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && ";
296 out << iface << "::getDefaultImpl()) {\n";
297 out.Indent();
298 out << "return " << iface << "::getDefaultImpl()->" << method.GetName() << "(";
299 out << NdkArgList(types, method, FormatArgNameOnly) << ");\n";
300 out.Dedent();
301 out << "}\n";
302
Steven Morelandaada3422018-09-20 15:55:33 -0700303 StatusCheckGoto(out);
304
305 if (!method.IsOneway()) {
306 out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n";
307 StatusCheckGoto(out);
308
309 out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n";
310 }
311
Steven Morelandaada3422018-09-20 15:55:33 -0700312 if (method.GetType().GetName() != "void") {
313 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700314 ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700315 out << ";\n";
316 StatusCheckGoto(out);
Jeongik Cha8f553e72019-02-12 16:58:03 +0900317 if (return_value_cached_to) {
318 out << *return_value_cached_to << " = *_aidl_return;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900319 }
Steven Morelandaada3422018-09-20 15:55:33 -0700320 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700321 for (const AidlArgument* arg : method.GetOutArguments()) {
322 out << "_aidl_ret_status = ";
323 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)});
324 out << ";\n";
325 StatusCheckGoto(out);
326 }
Steven Morelandaada3422018-09-20 15:55:33 -0700327
328 out << "_aidl_error:\n";
329 out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n";
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900330 if (options.GenLog()) {
331 out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::CLIENT), defined_type,
332 method, "_aidl_status", "_aidl_return", false /* isServer */,
333 true /* isNdk */);
334 }
Steven Morelandaada3422018-09-20 15:55:33 -0700335 out << "return _aidl_status;\n";
336 out.Dedent();
337 out << "}\n";
338}
339
Steven Moreland2bea13b2018-10-03 15:12:33 -0700340static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types,
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900341 const AidlInterface& defined_type,
342 const AidlMethod& method, const Options& options) {
Steven Morelandaada3422018-09-20 15:55:33 -0700343 out << "case " << MethodId(method) << ": {\n";
344 out.Indent();
345 for (const auto& arg : method.GetArguments()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700346 out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg)
347 << ";\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700348 }
349 if (method.GetType().GetName() != "void") {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700350 out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700351 }
352 out << "\n";
353
Steven Morelandeb38ee72018-10-15 14:20:04 -0700354 for (const auto& arg : method.GetArguments()) {
355 const std::string var_name = cpp::BuildVarName(*arg);
356
357 if (arg->IsIn()) {
358 out << "_aidl_ret_status = ";
359 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + var_name});
360 out << ";\n";
361 StatusCheckBreak(out);
362 } else if (arg->IsOut() && arg->GetType().IsArray()) {
363 out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n";
364 }
Steven Morelandaada3422018-09-20 15:55:33 -0700365 }
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900366 if (options.GenLog()) {
367 out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::SERVER), method,
368 true /* isServer */, true /* isNdk */);
369 }
Steven Moreland63404532018-10-08 14:31:00 -0700370 out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "("
Jiyong Park965c5b92018-11-21 13:37:15 +0900371 << NdkArgList(types, method, FormatArgForCall) << ");\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700372
Jeongik Chaba724992019-05-04 00:32:35 +0900373 if (options.GenLog()) {
374 out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::SERVER), defined_type,
375 method, "_aidl_status", "_aidl_return", true /* isServer */,
376 true /* isNdk */);
377 }
Steven Morelandaada3422018-09-20 15:55:33 -0700378 if (method.IsOneway()) {
379 // For a oneway transaction, the kernel will have already returned a result. This is for the
380 // in-process case when a oneway transaction is parceled/unparceled in the same process.
381 out << "_aidl_ret_status = STATUS_OK;\n";
382 } else {
383 out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n";
384 StatusCheckBreak(out);
385
386 out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n";
387
Steven Morelandaada3422018-09-20 15:55:33 -0700388 if (method.GetType().GetName() != "void") {
389 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700390 WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700391 out << ";\n";
392 StatusCheckBreak(out);
393 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700394 for (const AidlArgument* arg : method.GetOutArguments()) {
395 out << "_aidl_ret_status = ";
396 WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)});
397 out << ";\n";
398 StatusCheckBreak(out);
399 }
Steven Morelandaada3422018-09-20 15:55:33 -0700400 }
Steven Morelandaada3422018-09-20 15:55:33 -0700401 out << "break;\n";
402 out.Dedent();
403 out << "}\n";
404}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700405
Steven Moreland2bea13b2018-10-03 15:12:33 -0700406void GenerateClassSource(CodeWriter& out, const AidlTypenames& types,
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900407 const AidlInterface& defined_type, const Options& options) {
Steven Moreland0cf3f082018-09-20 19:09:46 -0700408 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
409 const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700410
Steven Moreland15b3ba72019-03-06 13:17:08 -0800411 out << "static binder_status_t "
412 << "_aidl_onTransact"
Steven Morelandaada3422018-09-20 15:55:33 -0700413 << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, "
414 "AParcel* _aidl_out) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700415 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700416 out << "(void)_aidl_in;\n";
417 out << "(void)_aidl_out;\n";
418 out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n";
419 if (!defined_type.GetMethods().empty()) {
Steven Moreland15b3ba72019-03-06 13:17:08 -0800420 // we know this cast is valid because this method is only called by the ICInterface
421 // AIBinder_Class object which is associated with this class.
422 out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz
423 << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700424 out << "switch (_aidl_code) {\n";
425 out.Indent();
426 for (const auto& method : defined_type.GetMethods()) {
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900427 GenerateServerCaseDefinition(out, types, defined_type, *method, options);
Steven Morelandaada3422018-09-20 15:55:33 -0700428 }
429 out.Dedent();
430 out << "}\n";
431 } else {
432 out << "(void)_aidl_binder;\n";
433 out << "(void)_aidl_code;\n";
434 }
435 out << "return _aidl_ret_status;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700436 out.Dedent();
437 out << "};\n\n";
438
Steven Moreland15b3ba72019-03-06 13:17:08 -0800439 out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz
440 << "::" << kDescriptor << ", _aidl_onTransact);\n\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700441}
Steven Morelandb0057e72018-08-27 01:44:11 -0700442void GenerateClientSource(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900443 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700444 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700445
Steven Moreland63404532018-10-08 14:31:00 -0700446 out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700447 out << clazz << "::~" << clazz << "() {}\n";
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900448 if (options.GenLog()) {
449 out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n";
450 }
Steven Morelandaada3422018-09-20 15:55:33 -0700451 out << "\n";
452 for (const auto& method : defined_type.GetMethods()) {
Jeongik Chac9972922019-02-11 12:41:16 +0900453 // Only getInterfaceVersion can use cache.
454 const bool cacheable = !method->IsUserDefined() && method->GetName() == kGetInterfaceVersion &&
455 options.Version() > 0;
Jeongik Cha8f553e72019-02-12 16:58:03 +0900456 const auto return_value_cached_to =
457 cacheable ? std::make_optional<std::string>(kCacheVariable) : std::nullopt;
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900458 GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to,
459 options);
Steven Morelandaada3422018-09-20 15:55:33 -0700460 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700461}
Jiyong Park965c5b92018-11-21 13:37:15 +0900462void GenerateServerSource(CodeWriter& out, const AidlTypenames& types,
463 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700464 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900465 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700466
467 out << "// Source for " << clazz << "\n";
468 out << clazz << "::" << clazz << "() {}\n";
469 out << clazz << "::~" << clazz << "() {}\n";
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900470 if (options.GenLog()) {
471 out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n";
472 }
Steven Moreland63404532018-10-08 14:31:00 -0700473 out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700474 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800475 out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n";
Steven Moreland63404532018-10-08 14:31:00 -0700476 out << "return ::ndk::SpAIBinder(binder);\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700477 out.Dedent();
478 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900479
480 // Implement the meta methods
481 for (const auto& method : defined_type.GetMethods()) {
482 if (method->IsUserDefined()) {
483 continue;
484 }
485 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
486 out << NdkMethodDecl(types, *method, clazz) << " {\n";
487 out.Indent();
488 out << "*_aidl_return = " << iface << "::" << kVersion << ";\n";
489 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
490 out.Dedent();
491 out << "}\n";
492 }
493 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700494}
Jiyong Park965c5b92018-11-21 13:37:15 +0900495void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types,
496 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700497 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
Steven Moreland15b3ba72019-03-06 13:17:08 -0800498 const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -0700499
500 out << "// Source for " << clazz << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900501 out << "const char* " << clazz << "::" << kDescriptor << " = \""
502 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700503 out << clazz << "::" << clazz << "() {}\n";
504 out << clazz << "::~" << clazz << "() {}\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700505 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700506 GenerateConstantDefinitions(out, defined_type);
507 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700508
Steven Moreland3c316ed2019-01-17 09:39:37 -0800509 out << "std::shared_ptr<" << clazz << "> " << clazz
510 << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800511 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800512 out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n";
513 out << "std::shared_ptr<::ndk::ICInterface> interface = "
514 "::ndk::ICInterface::asInterface(binder.get());\n";
515 out << "if (interface) {\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800516 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800517 out << "return std::static_pointer_cast<" << clazz << ">(interface);\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800518 out.Dedent();
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800519 out << "}\n";
Steven Moreland15b3ba72019-03-06 13:17:08 -0800520 out << "return (new " << bp_clazz << "(binder))->ref<" << clazz << ">();\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800521 out.Dedent();
Steven Moreland3c316ed2019-01-17 09:39:37 -0800522 out << "}\n\n";
523
Steven Moreland2bea13b2018-10-03 15:12:33 -0700524 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<"
525 << clazz << ">& instance) {\n";
526 out.Indent();
527 out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : "
528 "nullptr);\n";
529 out.Dedent();
530 out << "}\n";
531
532 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<"
533 << clazz << ">* instance) {\n";
534 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700535 out << "::ndk::SpAIBinder binder;\n";
Steven Moreland055d8792018-11-14 12:48:42 -0800536 out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700537 out << "if (status != STATUS_OK) return status;\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800538 out << "*instance = " << clazz << "::fromBinder(binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700539 out << "return STATUS_OK;\n";
540 out.Dedent();
541 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900542
543 // defintion for static member setDefaultImpl
544 out << "bool " << clazz << "::setDefaultImpl(std::shared_ptr<" << clazz << "> impl) {\n";
545 out.Indent();
546 out << "if (!" << clazz << "::default_impl && impl) {\n";
547 out.Indent();
548 out << clazz << "::default_impl = impl;\n";
549 out << "return true;\n";
550 out.Dedent();
551 out << "}\n";
552 out << "return false;\n";
553 out.Dedent();
554 out << "}\n";
555
556 // definition for static member getDefaultImpl
557 out << "const std::shared_ptr<" << clazz << ">& " << clazz << "::getDefaultImpl() {\n";
558 out.Indent();
559 out << "return " << clazz << "::default_impl;\n";
560 out.Dedent();
561 out << "}\n";
562
563 // definition for the static field default_impl
564 out << "std::shared_ptr<" << clazz << "> " << clazz << "::default_impl = nullptr;\n";
565
566 // default implementation for the <Name>Default class members
567 const std::string defaultClazz = clazz + "Default";
568 for (const auto& method : defined_type.GetMethods()) {
569 if (method->IsUserDefined()) {
570 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
571 << NdkArgList(types, *method, FormatArgNameUnused) << ") {\n";
572 out.Indent();
573 out << "::ndk::ScopedAStatus _aidl_status;\n";
574 out << "_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));\n";
575 out << "return _aidl_status;\n";
576 out.Dedent();
577 out << "}\n";
578 } else {
579 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
580 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
581 << "int32_t* _aidl_return) {\n";
582 out.Indent();
583 out << "*_aidl_return = 0;\n";
584 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
585 out.Dedent();
586 out << "}\n";
587 }
588 }
589 }
590
591 out << "::ndk::SpAIBinder " << defaultClazz << "::asBinder() {\n";
592 out.Indent();
593 out << "return ::ndk::SpAIBinder();\n";
594 out.Dedent();
595 out << "}\n";
596
597 out << "bool " << defaultClazz << "::isRemote() {\n";
598 out.Indent();
599 out << "return false;\n";
600 out.Dedent();
601 out << "}\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700602}
Jiyong Park965c5b92018-11-21 13:37:15 +0900603
Steven Morelandb0057e72018-08-27 01:44:11 -0700604void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900605 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700606 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
607
608 out << "#pragma once\n\n";
Jiyong Park081b90a2019-04-03 20:05:01 +0900609 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700610 << "\"\n";
611 out << "\n";
612 out << "#include <android/binder_ibinder.h>\n";
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900613 if (options.GenLog()) {
614 out << "#include <json/value.h>\n";
615 out << "#include <functional>\n";
616 out << "#include <chrono>\n";
617 out << "#include <sstream>\n";
618 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700619 out << "\n";
620 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700621 out << "class " << clazz << " : public ::ndk::BpCInterface<"
Steven Morelandb0057e72018-08-27 01:44:11 -0700622 << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
623 out << "public:\n";
624 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800625 out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700626 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700627 out << "\n";
628 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700629 out << NdkMethodDecl(types, *method) << " override;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700630 }
Jiyong Park965c5b92018-11-21 13:37:15 +0900631
632 if (options.Version() > 0) {
Jeongik Cha8f553e72019-02-12 16:58:03 +0900633 out << "int32_t " << kCacheVariable << " = -1;\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900634 }
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900635 if (options.GenLog()) {
636 out << "static std::function<void(const Json::Value&)> logFunc;\n";
637 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700638 out.Dedent();
639 out << "};\n";
640 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700641}
Jiyong Park965c5b92018-11-21 13:37:15 +0900642void GenerateServerHeader(CodeWriter& out, const AidlTypenames& types,
643 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700644 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900645 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700646
647 out << "#pragma once\n\n";
Jiyong Park081b90a2019-04-03 20:05:01 +0900648 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700649 << "\"\n";
650 out << "\n";
651 out << "#include <android/binder_ibinder.h>\n";
652 out << "\n";
653 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900654 out << "class " << clazz << " : public ::ndk::BnCInterface<" << iface << "> {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700655 out << "public:\n";
656 out.Indent();
657 out << clazz << "();\n";
658 out << "virtual ~" << clazz << "();\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900659
660 // Declare the meta methods
661 for (const auto& method : defined_type.GetMethods()) {
662 if (method->IsUserDefined()) {
663 continue;
664 }
665 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
666 out << NdkMethodDecl(types, *method) << " final override;\n";
667 } else {
668 AIDL_FATAL(defined_type) << "Meta method '" << method->GetName() << "' is unimplemented.";
669 }
670 }
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900671 if (options.GenLog()) {
672 out << "static std::function<void(const Json::Value&)> logFunc;\n";
673 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700674 out.Dedent();
Steven Moreland0cf3f082018-09-20 19:09:46 -0700675 out << "protected:\n";
676 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700677 out << "::ndk::SpAIBinder createBinder() override;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700678 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700679 out << "private:\n";
680 out.Indent();
681 out.Dedent();
682 out << "};\n";
683 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700684}
685void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900686 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700687 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
688
689 out << "#pragma once\n\n";
690 out << "#include <android/binder_interface_utils.h>\n";
Jeongik Cha9d730bb2019-04-18 13:44:26 +0900691 if (options.GenLog()) {
692 out << "#include <json/value.h>\n";
693 out << "#include <functional>\n";
694 out << "#include <chrono>\n";
695 out << "#include <sstream>\n";
696 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700697 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700698
699 GenerateHeaderIncludes(out, types, defined_type);
700 out << "\n";
701
Steven Morelandb0057e72018-08-27 01:44:11 -0700702 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700703 out << "class " << clazz << " : public ::ndk::ICInterface {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700704 out << "public:\n";
705 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900706 out << "static const char* " << kDescriptor << ";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700707 out << clazz << "();\n";
708 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700709 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700710 GenerateConstantDeclarations(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900711 if (options.Version() > 0) {
712 out << "static const int32_t " << kVersion << " = " << std::to_string(options.Version())
713 << ";\n";
714 }
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700715 out << "\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800716 out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700717 out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz
718 << ">& instance);";
Jiyong Park1b88cce2018-11-19 19:53:44 +0900719 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700720 out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz
721 << ">* instance);";
722 out << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900723 out << "static bool setDefaultImpl(std::shared_ptr<" << clazz << "> impl);";
724 out << "\n";
725 out << "static const std::shared_ptr<" << clazz << ">& getDefaultImpl();";
726 out << "\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700727 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700728 out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700729 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700730 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900731 out << "private:\n";
732 out.Indent();
733 out << "static std::shared_ptr<" << clazz << "> default_impl;\n";
734 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700735 out << "};\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900736
737 const std::string defaultClazz = clazz + "Default";
738
739 out << "class " << defaultClazz << " : public " << clazz << " {\n";
740 out << "public:\n";
741 out.Indent();
742 for (const auto& method : defined_type.GetMethods()) {
743 if (method->IsUserDefined()) {
744 out << NdkMethodDecl(types, *method) << " override;\n";
745 } else if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
746 out << NdkMethodDecl(types, *method) << " override;\n";
747 }
748 }
749 out << "::ndk::SpAIBinder asBinder() override;\n";
750 out << "bool isRemote() override;\n";
751 out.Dedent();
752 out << "};\n";
753
Steven Morelandb0057e72018-08-27 01:44:11 -0700754 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700755}
756void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700757 const AidlStructuredParcelable& defined_type,
758 const Options& /*options*/) {
759 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
760
Steven Morelandb0057e72018-08-27 01:44:11 -0700761 out << "#pragma once\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700762 out << "#include <android/binder_interface_utils.h>\n";
763 out << "\n";
764
765 GenerateHeaderIncludes(out, types, defined_type);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700766
Steven Morelandb0057e72018-08-27 01:44:11 -0700767 EnterNdkNamespace(out, defined_type);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700768 out << "class " << clazz << " {\n";
769 out << "public:\n";
770 out.Indent();
771 out << "static const char* descriptor;\n";
772 out << "\n";
773 for (const auto& variable : defined_type.GetFields()) {
774 out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName();
775 if (variable->GetDefaultValue()) {
776 out << " = " << variable->ValueString(AidlConstantValueDecorator);
Jooyung Han1859a002022-03-25 22:03:56 +0900777 } else if (AidlTypenames::IsPrimitiveTypename(variable->GetType().GetName()) &&
778 !variable->GetType().IsArray()) {
779 out << " = {}";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700780 }
781 out << ";\n";
782 }
783 out << "\n";
784 out << "binder_status_t readFromParcel(const AParcel* parcel);\n";
785 out << "binder_status_t writeToParcel(AParcel* parcel) const;\n";
786 out.Dedent();
787 out << "};\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700788 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700789}
790void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700791 const AidlStructuredParcelable& defined_type,
792 const Options& /*options*/) {
793 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
794
Jiyong Park081b90a2019-04-03 20:05:01 +0900795 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700796 << "\"\n";
797 out << "\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700798 GenerateSourceIncludes(out, types, defined_type);
799 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700800 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900801 out << "const char* " << clazz << "::" << kDescriptor << " = \""
802 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700803 out << "\n";
804
805 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n";
806 out.Indent();
807 out << "std::string _aidl_descriptor;\n";
808 out << "binder_status_t _aidl_ret_status;\n";
809
810 out << "int32_t _aidl_null;\n";
Jeongik Cha95eba572018-11-22 09:14:52 +0900811 out << "int32_t _aidl_parcelable_size;\n";
812 out << "int32_t _aidl_start_pos;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700813 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_null);\n";
814 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900815 out << "_aidl_start_pos = AParcel_getDataPosition(parcel);\n";
816 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_parcelable_size);\n";
817 out << "if (_aidl_parcelable_size < 0) return STATUS_BAD_VALUE;\n";
818 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700819
820 // TODO(b/117281836)
821 out << "if (_aidl_null == 0) return STATUS_UNEXPECTED_NULL;\n\n";
822
823 for (const auto& variable : defined_type.GetFields()) {
824 out << "_aidl_ret_status = ";
825 ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()});
826 out << ";\n";
827 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900828 out << "if (AParcel_getDataPosition(parcel) - _aidl_start_pos >= _aidl_parcelable_size) {\n"
829 << " AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
830 << " return _aidl_ret_status;\n"
831 << "}\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700832 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900833 out << "AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
834 << "return _aidl_ret_status;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700835 out.Dedent();
836 out << "}\n";
837
838 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n";
839 out.Indent();
840 out << "binder_status_t _aidl_ret_status;\n";
841
842 // non-null
843 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 1);\n";
844 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900845 out << "size_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n";
846 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 0);\n";
847 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700848
849 for (const auto& variable : defined_type.GetFields()) {
850 out << "_aidl_ret_status = ";
851 WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()});
852 out << ";\n";
853 StatusCheckReturn(out);
854 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900855 out << "size_t _aidl_end_pos = AParcel_getDataPosition(parcel);\n";
856 out << "AParcel_setDataPosition(parcel, _aidl_start_pos);\n";
857 out << "AParcel_writeInt32(parcel, _aidl_end_pos - _aidl_start_pos);\n";
858 out << "AParcel_setDataPosition(parcel, _aidl_end_pos);\n";
859
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700860 out << "return _aidl_ret_status;\n";
861 out.Dedent();
862 out << "}\n";
863 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700864 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700865}
866} // namespace internals
867} // namespace ndk
868} // namespace aidl
869} // namespace android