blob: 2ca255d9abe98a342ac48d00c3a41b1e3da73ba8 [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
Jiyong Park965c5b92018-11-21 13:37:15 +090030static constexpr const char* kClazz = "clazz";
31static 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) {
Steven Morelandb0057e72018-08-27 01:44:11 -070041 const string i_header =
Steven Moreland7c933372018-10-11 15:20:04 -070042 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 =
68 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 <"
Steven Moreland7c933372018-10-11 15:20:04 -0700149 << NdkHeaderFile(other_defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700150 << ">\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";
Steven Moreland7c933372018-10-11 15:20:04 -0700169 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
234static std::string DataClassFor(const AidlInterface& defined_type) {
235 return "AidlClassData_" + ClassName(defined_type, ClassNames::INTERFACE);
236}
Steven Morelandaada3422018-09-20 15:55:33 -0700237static std::string MethodId(const AidlMethod& m) {
238 return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
239}
240
Jeongik Cha8f553e72019-02-12 16:58:03 +0900241static void GenerateClientMethodDefinition(
242 CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
243 const AidlMethod& method, const std::optional<std::string> return_value_cached_to) {
Steven Morelandaada3422018-09-20 15:55:33 -0700244 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
245
Steven Moreland2bea13b2018-10-03 15:12:33 -0700246 out << NdkMethodDecl(types, method, clazz) << " {\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700247 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700248 out << "binder_status_t _aidl_ret_status = STATUS_OK;\n";
Steven Moreland63404532018-10-08 14:31:00 -0700249 out << "::ndk::ScopedAStatus _aidl_status;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900250
Jeongik Cha8f553e72019-02-12 16:58:03 +0900251 if (return_value_cached_to) {
252 out << "if (" << *return_value_cached_to << " != -1) {\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900253 out.Indent();
Jeongik Cha8f553e72019-02-12 16:58:03 +0900254 out << "*_aidl_return = " << *return_value_cached_to << ";\n"
Jeongik Chac9972922019-02-11 12:41:16 +0900255 << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"
256 << "return _aidl_status;\n";
257 out.Dedent();
258 out << "}\n";
259 }
260 out << "::ndk::ScopedAParcel _aidl_in;\n";
261 out << "::ndk::ScopedAParcel _aidl_out;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700262 out << "\n";
263
264 out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n";
265 StatusCheckGoto(out);
266
Steven Morelandeb38ee72018-10-15 14:20:04 -0700267 for (const auto& arg : method.GetArguments()) {
268 const std::string var_name = cpp::BuildVarName(*arg);
269
270 if (arg->IsIn()) {
271 out << "_aidl_ret_status = ";
272 const std::string prefix = (arg->IsOut() ? "*" : "");
273 WriteToParcelFor({out, types, arg->GetType(), "_aidl_in.get()", prefix + var_name});
274 out << ";\n";
275 StatusCheckGoto(out);
276 } else if (arg->IsOut() && arg->GetType().IsArray()) {
277 out << "_aidl_ret_status = ::ndk::AParcel_writeVectorSize(_aidl_in.get(), *" << var_name
278 << ");\n";
279 }
Steven Morelandaada3422018-09-20 15:55:33 -0700280 }
281 out << "_aidl_ret_status = AIBinder_transact(\n";
282 out.Indent();
283 out << "asBinder().get(),\n";
284 out << MethodId(method) << ",\n";
285 out << "_aidl_in.getR(),\n";
286 out << "_aidl_out.getR(),\n";
287 out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << ");\n";
288 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900289
290 // If the method is not implmented in the server side but the client has
291 // provided the default implementation, call it instead of failing hard.
292 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
293 out << "if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && ";
294 out << iface << "::getDefaultImpl()) {\n";
295 out.Indent();
296 out << "return " << iface << "::getDefaultImpl()->" << method.GetName() << "(";
297 out << NdkArgList(types, method, FormatArgNameOnly) << ");\n";
298 out.Dedent();
299 out << "}\n";
300
Steven Morelandaada3422018-09-20 15:55:33 -0700301 StatusCheckGoto(out);
302
303 if (!method.IsOneway()) {
304 out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n";
305 StatusCheckGoto(out);
306
307 out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n";
308 }
309
Steven Morelandaada3422018-09-20 15:55:33 -0700310 if (method.GetType().GetName() != "void") {
311 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700312 ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700313 out << ";\n";
314 StatusCheckGoto(out);
Jeongik Cha8f553e72019-02-12 16:58:03 +0900315 if (return_value_cached_to) {
316 out << *return_value_cached_to << " = *_aidl_return;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900317 }
Steven Morelandaada3422018-09-20 15:55:33 -0700318 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700319 for (const AidlArgument* arg : method.GetOutArguments()) {
320 out << "_aidl_ret_status = ";
321 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)});
322 out << ";\n";
323 StatusCheckGoto(out);
324 }
Steven Morelandaada3422018-09-20 15:55:33 -0700325
326 out << "_aidl_error:\n";
327 out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n";
328 out << "return _aidl_status;\n";
329 out.Dedent();
330 out << "}\n";
331}
332
Steven Moreland2bea13b2018-10-03 15:12:33 -0700333static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types,
334 const AidlInterface& /*defined_type*/,
Steven Morelandaada3422018-09-20 15:55:33 -0700335 const AidlMethod& method) {
336 out << "case " << MethodId(method) << ": {\n";
337 out.Indent();
338 for (const auto& arg : method.GetArguments()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700339 out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg)
340 << ";\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700341 }
342 if (method.GetType().GetName() != "void") {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700343 out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700344 }
345 out << "\n";
346
Steven Morelandeb38ee72018-10-15 14:20:04 -0700347 for (const auto& arg : method.GetArguments()) {
348 const std::string var_name = cpp::BuildVarName(*arg);
349
350 if (arg->IsIn()) {
351 out << "_aidl_ret_status = ";
352 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + var_name});
353 out << ";\n";
354 StatusCheckBreak(out);
355 } else if (arg->IsOut() && arg->GetType().IsArray()) {
356 out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n";
357 }
Steven Morelandaada3422018-09-20 15:55:33 -0700358 }
359
Steven Moreland63404532018-10-08 14:31:00 -0700360 out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "("
Jiyong Park965c5b92018-11-21 13:37:15 +0900361 << NdkArgList(types, method, FormatArgForCall) << ");\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700362
363 if (method.IsOneway()) {
364 // For a oneway transaction, the kernel will have already returned a result. This is for the
365 // in-process case when a oneway transaction is parceled/unparceled in the same process.
366 out << "_aidl_ret_status = STATUS_OK;\n";
367 } else {
368 out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n";
369 StatusCheckBreak(out);
370
371 out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n";
372
Steven Morelandaada3422018-09-20 15:55:33 -0700373 if (method.GetType().GetName() != "void") {
374 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700375 WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700376 out << ";\n";
377 StatusCheckBreak(out);
378 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700379 for (const AidlArgument* arg : method.GetOutArguments()) {
380 out << "_aidl_ret_status = ";
381 WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)});
382 out << ";\n";
383 StatusCheckBreak(out);
384 }
Steven Morelandaada3422018-09-20 15:55:33 -0700385 }
386
387 out << "break;\n";
388 out.Dedent();
389 out << "}\n";
390}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700391
Steven Moreland2bea13b2018-10-03 15:12:33 -0700392void GenerateClassSource(CodeWriter& out, const AidlTypenames& types,
Steven Moreland0cf3f082018-09-20 19:09:46 -0700393 const AidlInterface& defined_type, const Options& /*options*/) {
394 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
395 const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
396 const std::string data_clazz = DataClassFor(defined_type);
397 const std::string on_create = data_clazz + "_onCreate";
398 const std::string on_destroy = data_clazz + "_onDestory";
399 const std::string on_transact = data_clazz + "_onTransact";
400
401 out << "struct " << data_clazz << " {\n";
402 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900403 out << "static AIBinder_Class* " << kClazz << ";\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700404 out << "std::shared_ptr<" << bn_clazz << "> instance;\n";
405 out.Dedent();
406 out << "};\n\n";
407
408 out << "static void* " << on_create << "(void* args) {\n";
409 out.Indent();
410 out << data_clazz << "* data = new " << data_clazz << "{static_cast<" << bn_clazz
411 << "*>(args)->ref<" << bn_clazz << ">()};\n";
412 out << "return static_cast<void*>(data);\n";
413 out.Dedent();
414 out << "};\n\n";
415
416 out << "static void " << on_destroy << "(void* userData) {\n";
417 out.Indent();
418 out << "delete static_cast<" << data_clazz << "*>(userData);\n";
419 out.Dedent();
420 out << "};\n\n";
421
422 out << "static binder_status_t " << on_transact
Steven Morelandaada3422018-09-20 15:55:33 -0700423 << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, "
424 "AParcel* _aidl_out) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700425 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700426 out << "(void)_aidl_in;\n";
427 out << "(void)_aidl_out;\n";
428 out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n";
429 if (!defined_type.GetMethods().empty()) {
430 out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = static_cast<" << data_clazz
431 << "*>(AIBinder_getUserData(_aidl_binder))->instance;\n";
432 out << "switch (_aidl_code) {\n";
433 out.Indent();
434 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700435 GenerateServerCaseDefinition(out, types, defined_type, *method);
Steven Morelandaada3422018-09-20 15:55:33 -0700436 }
437 out.Dedent();
438 out << "}\n";
439 } else {
440 out << "(void)_aidl_binder;\n";
441 out << "(void)_aidl_code;\n";
442 }
443 out << "return _aidl_ret_status;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700444 out.Dedent();
445 out << "};\n\n";
446
Jiyong Park965c5b92018-11-21 13:37:15 +0900447 out << "AIBinder_Class* " << data_clazz << ":: " << kClazz << " = AIBinder_Class_define(" << clazz
448 << "::" << kDescriptor << ", " << on_create << ", " << on_destroy << ", " << on_transact
449 << ");\n\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700450}
Steven Morelandb0057e72018-08-27 01:44:11 -0700451void GenerateClientSource(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900452 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700453 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700454 const std::string data_clazz = DataClassFor(defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700455
456 out << "// Source for " << clazz << "\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700457 out << "std::shared_ptr<" << clazz << "> " << clazz
Steven Moreland63404532018-10-08 14:31:00 -0700458 << "::associate(const ::ndk::SpAIBinder& binder) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700459 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900460 out << "if (!AIBinder_associateClass(binder.get(), " << data_clazz << "::" << kClazz
461 << ")) { return nullptr; }\n";
Steven Moreland1c4b4052018-10-10 12:24:22 -0700462 out << "return (new " << clazz << "(binder))->ref<" << clazz << ">();\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700463 out.Dedent();
464 out << "}\n\n";
465
Steven Moreland63404532018-10-08 14:31:00 -0700466 out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700467 out << clazz << "::~" << clazz << "() {}\n";
468 out << "\n";
469 for (const auto& method : defined_type.GetMethods()) {
Jeongik Chac9972922019-02-11 12:41:16 +0900470 // Only getInterfaceVersion can use cache.
471 const bool cacheable = !method->IsUserDefined() && method->GetName() == kGetInterfaceVersion &&
472 options.Version() > 0;
Jeongik Cha8f553e72019-02-12 16:58:03 +0900473 const auto return_value_cached_to =
474 cacheable ? std::make_optional<std::string>(kCacheVariable) : std::nullopt;
475 GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to);
Steven Morelandaada3422018-09-20 15:55:33 -0700476 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700477}
Jiyong Park965c5b92018-11-21 13:37:15 +0900478void GenerateServerSource(CodeWriter& out, const AidlTypenames& types,
479 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700480 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900481 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700482 const std::string data_clazz = DataClassFor(defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700483
484 out << "// Source for " << clazz << "\n";
485 out << clazz << "::" << clazz << "() {}\n";
486 out << clazz << "::~" << clazz << "() {}\n";
487
Steven Moreland63404532018-10-08 14:31:00 -0700488 out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700489 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900490 out << "AIBinder* binder = AIBinder_new(" << data_clazz << "::" << kClazz
491 << ", static_cast<void*>(this));\n";
Steven Moreland63404532018-10-08 14:31:00 -0700492 out << "return ::ndk::SpAIBinder(binder);\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700493 out.Dedent();
494 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900495
496 // Implement the meta methods
497 for (const auto& method : defined_type.GetMethods()) {
498 if (method->IsUserDefined()) {
499 continue;
500 }
501 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
502 out << NdkMethodDecl(types, *method, clazz) << " {\n";
503 out.Indent();
504 out << "*_aidl_return = " << iface << "::" << kVersion << ";\n";
505 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
506 out.Dedent();
507 out << "}\n";
508 }
509 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700510}
Jiyong Park965c5b92018-11-21 13:37:15 +0900511void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types,
512 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700513 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700514 const std::string data_clazz = DataClassFor(defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700515
516 out << "// Source for " << clazz << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900517 out << "const char* " << clazz << "::" << kDescriptor << " = \""
518 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700519 out << clazz << "::" << clazz << "() {}\n";
520 out << clazz << "::~" << clazz << "() {}\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700521 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700522 GenerateConstantDefinitions(out, defined_type);
523 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700524
Steven Moreland3c316ed2019-01-17 09:39:37 -0800525 out << "std::shared_ptr<" << clazz << "> " << clazz
526 << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800527 out.Indent();
Steven Moreland3c316ed2019-01-17 09:39:37 -0800528 out << data_clazz << "* data = static_cast<" << data_clazz
529 << "*>(AIBinder_getUserData(binder.get()));\n";
530 out << "if (data) {\n";
531 out.Indent();
532 out << "return data->instance;\n";
533 out.Dedent();
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800534 out << "}\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800535 // If it is local, it is not an 'ndk' instance, and parceling will happen locally.
536 out << "return " << NdkFullClassName(defined_type, ClassNames::CLIENT)
537 << "::associate(binder);\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800538 out.Dedent();
Steven Moreland3c316ed2019-01-17 09:39:37 -0800539 out << "}\n\n";
540
Steven Moreland2bea13b2018-10-03 15:12:33 -0700541 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<"
542 << clazz << ">& instance) {\n";
543 out.Indent();
544 out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : "
545 "nullptr);\n";
546 out.Dedent();
547 out << "}\n";
548
549 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<"
550 << clazz << ">* instance) {\n";
551 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700552 out << "::ndk::SpAIBinder binder;\n";
Steven Moreland055d8792018-11-14 12:48:42 -0800553 out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700554 out << "if (status != STATUS_OK) return status;\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800555 out << "*instance = " << clazz << "::fromBinder(binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700556 out << "return STATUS_OK;\n";
557 out.Dedent();
558 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900559
560 // defintion for static member setDefaultImpl
561 out << "bool " << clazz << "::setDefaultImpl(std::shared_ptr<" << clazz << "> impl) {\n";
562 out.Indent();
563 out << "if (!" << clazz << "::default_impl && impl) {\n";
564 out.Indent();
565 out << clazz << "::default_impl = impl;\n";
566 out << "return true;\n";
567 out.Dedent();
568 out << "}\n";
569 out << "return false;\n";
570 out.Dedent();
571 out << "}\n";
572
573 // definition for static member getDefaultImpl
574 out << "const std::shared_ptr<" << clazz << ">& " << clazz << "::getDefaultImpl() {\n";
575 out.Indent();
576 out << "return " << clazz << "::default_impl;\n";
577 out.Dedent();
578 out << "}\n";
579
580 // definition for the static field default_impl
581 out << "std::shared_ptr<" << clazz << "> " << clazz << "::default_impl = nullptr;\n";
582
583 // default implementation for the <Name>Default class members
584 const std::string defaultClazz = clazz + "Default";
585 for (const auto& method : defined_type.GetMethods()) {
586 if (method->IsUserDefined()) {
587 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
588 << NdkArgList(types, *method, FormatArgNameUnused) << ") {\n";
589 out.Indent();
590 out << "::ndk::ScopedAStatus _aidl_status;\n";
591 out << "_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));\n";
592 out << "return _aidl_status;\n";
593 out.Dedent();
594 out << "}\n";
595 } else {
596 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
597 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
598 << "int32_t* _aidl_return) {\n";
599 out.Indent();
600 out << "*_aidl_return = 0;\n";
601 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
602 out.Dedent();
603 out << "}\n";
604 }
605 }
606 }
607
608 out << "::ndk::SpAIBinder " << defaultClazz << "::asBinder() {\n";
609 out.Indent();
610 out << "return ::ndk::SpAIBinder();\n";
611 out.Dedent();
612 out << "}\n";
613
614 out << "bool " << defaultClazz << "::isRemote() {\n";
615 out.Indent();
616 out << "return false;\n";
617 out.Dedent();
618 out << "}\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700619}
Jiyong Park965c5b92018-11-21 13:37:15 +0900620
Steven Morelandb0057e72018-08-27 01:44:11 -0700621void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900622 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700623 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
624
625 out << "#pragma once\n\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700626 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700627 << "\"\n";
628 out << "\n";
629 out << "#include <android/binder_ibinder.h>\n";
630 out << "\n";
631 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700632 out << "class " << clazz << " : public ::ndk::BpCInterface<"
Steven Morelandb0057e72018-08-27 01:44:11 -0700633 << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
634 out << "public:\n";
635 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700636 out << "static std::shared_ptr<" << clazz << "> associate(const ::ndk::SpAIBinder& binder);\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700637 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700638 out << "\n";
639 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700640 out << NdkMethodDecl(types, *method) << " override;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700641 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700642 out.Dedent();
643 out << "private:\n";
644 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700645 out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900646
647 if (options.Version() > 0) {
Jeongik Cha8f553e72019-02-12 16:58:03 +0900648 out << "int32_t " << kCacheVariable << " = -1;\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900649 }
650
Steven Morelandb0057e72018-08-27 01:44:11 -0700651 out.Dedent();
652 out << "};\n";
653 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700654}
Jiyong Park965c5b92018-11-21 13:37:15 +0900655void GenerateServerHeader(CodeWriter& out, const AidlTypenames& types,
656 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700657 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900658 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700659
660 out << "#pragma once\n\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700661 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700662 << "\"\n";
663 out << "\n";
664 out << "#include <android/binder_ibinder.h>\n";
665 out << "\n";
666 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900667 out << "class " << clazz << " : public ::ndk::BnCInterface<" << iface << "> {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700668 out << "public:\n";
669 out.Indent();
670 out << clazz << "();\n";
671 out << "virtual ~" << clazz << "();\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900672
673 // Declare the meta methods
674 for (const auto& method : defined_type.GetMethods()) {
675 if (method->IsUserDefined()) {
676 continue;
677 }
678 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
679 out << NdkMethodDecl(types, *method) << " final override;\n";
680 } else {
681 AIDL_FATAL(defined_type) << "Meta method '" << method->GetName() << "' is unimplemented.";
682 }
683 }
684
Steven Morelandb0057e72018-08-27 01:44:11 -0700685 out.Dedent();
Steven Moreland0cf3f082018-09-20 19:09:46 -0700686 out << "protected:\n";
687 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700688 out << "::ndk::SpAIBinder createBinder() override;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700689 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700690 out << "private:\n";
691 out.Indent();
692 out.Dedent();
693 out << "};\n";
694 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700695}
696void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900697 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700698 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
699
700 out << "#pragma once\n\n";
701 out << "#include <android/binder_interface_utils.h>\n";
702 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700703
704 GenerateHeaderIncludes(out, types, defined_type);
705 out << "\n";
706
Steven Morelandb0057e72018-08-27 01:44:11 -0700707 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700708 out << "class " << clazz << " : public ::ndk::ICInterface {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700709 out << "public:\n";
710 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900711 out << "static AIBinder_Class* " << kClazz << ";\n";
712 out << "static const char* " << kDescriptor << ";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700713 out << clazz << "();\n";
714 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700715 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700716 GenerateConstantDeclarations(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900717 if (options.Version() > 0) {
718 out << "static const int32_t " << kVersion << " = " << std::to_string(options.Version())
719 << ";\n";
720 }
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700721 out << "\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800722 out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700723 out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz
724 << ">& instance);";
Jiyong Park1b88cce2018-11-19 19:53:44 +0900725 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700726 out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz
727 << ">* instance);";
728 out << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900729 out << "static bool setDefaultImpl(std::shared_ptr<" << clazz << "> impl);";
730 out << "\n";
731 out << "static const std::shared_ptr<" << clazz << ">& getDefaultImpl();";
732 out << "\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700733 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700734 out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700735 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700736 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900737 out << "private:\n";
738 out.Indent();
739 out << "static std::shared_ptr<" << clazz << "> default_impl;\n";
740 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700741 out << "};\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900742
743 const std::string defaultClazz = clazz + "Default";
744
745 out << "class " << defaultClazz << " : public " << clazz << " {\n";
746 out << "public:\n";
747 out.Indent();
748 for (const auto& method : defined_type.GetMethods()) {
749 if (method->IsUserDefined()) {
750 out << NdkMethodDecl(types, *method) << " override;\n";
751 } else if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
752 out << NdkMethodDecl(types, *method) << " override;\n";
753 }
754 }
755 out << "::ndk::SpAIBinder asBinder() override;\n";
756 out << "bool isRemote() override;\n";
757 out.Dedent();
758 out << "};\n";
759
Steven Morelandb0057e72018-08-27 01:44:11 -0700760 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700761}
762void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700763 const AidlStructuredParcelable& defined_type,
764 const Options& /*options*/) {
765 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
766
Steven Morelandb0057e72018-08-27 01:44:11 -0700767 out << "#pragma once\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700768 out << "#include <android/binder_interface_utils.h>\n";
769 out << "\n";
770
771 GenerateHeaderIncludes(out, types, defined_type);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700772
Steven Morelandb0057e72018-08-27 01:44:11 -0700773 EnterNdkNamespace(out, defined_type);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700774 out << "class " << clazz << " {\n";
775 out << "public:\n";
776 out.Indent();
777 out << "static const char* descriptor;\n";
778 out << "\n";
779 for (const auto& variable : defined_type.GetFields()) {
780 out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName();
781 if (variable->GetDefaultValue()) {
782 out << " = " << variable->ValueString(AidlConstantValueDecorator);
783 }
784 out << ";\n";
785 }
786 out << "\n";
787 out << "binder_status_t readFromParcel(const AParcel* parcel);\n";
788 out << "binder_status_t writeToParcel(AParcel* parcel) const;\n";
789 out.Dedent();
790 out << "};\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700791 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700792}
793void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700794 const AidlStructuredParcelable& defined_type,
795 const Options& /*options*/) {
796 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
797
Steven Moreland7c933372018-10-11 15:20:04 -0700798 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::BASE, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700799 << "\"\n";
800 out << "\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700801 GenerateSourceIncludes(out, types, defined_type);
802 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700803 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900804 out << "const char* " << clazz << "::" << kDescriptor << " = \""
805 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700806 out << "\n";
807
808 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n";
809 out.Indent();
810 out << "std::string _aidl_descriptor;\n";
811 out << "binder_status_t _aidl_ret_status;\n";
812
813 out << "int32_t _aidl_null;\n";
Jeongik Cha95eba572018-11-22 09:14:52 +0900814 out << "int32_t _aidl_parcelable_size;\n";
815 out << "int32_t _aidl_start_pos;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700816 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_null);\n";
817 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900818 out << "_aidl_start_pos = AParcel_getDataPosition(parcel);\n";
819 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_parcelable_size);\n";
820 out << "if (_aidl_parcelable_size < 0) return STATUS_BAD_VALUE;\n";
821 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700822
823 // TODO(b/117281836)
824 out << "if (_aidl_null == 0) return STATUS_UNEXPECTED_NULL;\n\n";
825
826 for (const auto& variable : defined_type.GetFields()) {
827 out << "_aidl_ret_status = ";
828 ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()});
829 out << ";\n";
830 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900831 out << "if (AParcel_getDataPosition(parcel) - _aidl_start_pos >= _aidl_parcelable_size) {\n"
832 << " AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
833 << " return _aidl_ret_status;\n"
834 << "}\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700835 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900836 out << "AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
837 << "return _aidl_ret_status;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700838 out.Dedent();
839 out << "}\n";
840
841 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n";
842 out.Indent();
843 out << "binder_status_t _aidl_ret_status;\n";
844
845 // non-null
846 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 1);\n";
847 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900848 out << "size_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n";
849 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 0);\n";
850 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700851
852 for (const auto& variable : defined_type.GetFields()) {
853 out << "_aidl_ret_status = ";
854 WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()});
855 out << ";\n";
856 StatusCheckReturn(out);
857 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900858 out << "size_t _aidl_end_pos = AParcel_getDataPosition(parcel);\n";
859 out << "AParcel_setDataPosition(parcel, _aidl_start_pos);\n";
860 out << "AParcel_writeInt32(parcel, _aidl_end_pos - _aidl_start_pos);\n";
861 out << "AParcel_setDataPosition(parcel, _aidl_end_pos);\n";
862
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700863 out << "return _aidl_ret_status;\n";
864 out.Dedent();
865 out << "}\n";
866 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700867 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700868}
869} // namespace internals
870} // namespace ndk
871} // namespace aidl
872} // namespace android