blob: 04296bdd9aca0369a0a54e340e3b33b0b5c28b3d [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 Park5b7e5322019-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 Park5b7e5322019-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
Daniel Norman85aed542019-08-21 12:01:14 -0700116 // TODO(b/123321528): Generate enums.
117 CHECK(false) << "Unrecognized type sent for NDK cpp generation.";
Steven Morelandb0057e72018-08-27 01:44:11 -0700118}
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";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700143 out << "#ifndef __ANDROID_NDK__\n";
144 out << "#include <android/binder_stability.h>\n";
145 out << "#endif // __ANDROID_NDK__\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700146
Steven Moreland2bea13b2018-10-03 15:12:33 -0700147 types.IterateTypes([&](const AidlDefinedType& other_defined_type) {
148 if (&other_defined_type == &defined_type) return;
149
150 if (other_defined_type.AsInterface() != nullptr) {
151 out << "#include <"
Jiyong Park5b7e5322019-04-03 20:05:01 +0900152 << NdkHeaderFile(other_defined_type, ClassNames::RAW, false /*use_os_sep*/) << ">\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700153 } else if (other_defined_type.AsStructuredParcelable() != nullptr) {
154 out << "#include <"
155 << NdkHeaderFile(other_defined_type, ClassNames::BASE, false /*use_os_sep*/) << ">\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700156 } else if (other_defined_type.AsParcelable() != nullptr) {
157 out << "#include \"" << other_defined_type.AsParcelable()->GetCppHeader() << "\"\n";
158 } else {
159 AIDL_FATAL(defined_type) << "Unrecognized type.";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700160 }
161 });
162}
163static void GenerateSourceIncludes(CodeWriter& out, const AidlTypenames& types,
164 const AidlDefinedType& /*defined_type*/) {
165 types.IterateTypes([&](const AidlDefinedType& a_defined_type) {
166 if (a_defined_type.AsInterface() != nullptr) {
Steven Moreland7c933372018-10-11 15:20:04 -0700167 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::CLIENT, false /*use_os_sep*/)
Steven Moreland2bea13b2018-10-03 15:12:33 -0700168 << ">\n";
Steven Moreland7c933372018-10-11 15:20:04 -0700169 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::SERVER, false /*use_os_sep*/)
Steven Moreland2bea13b2018-10-03 15:12:33 -0700170 << ">\n";
Jiyong Park5b7e5322019-04-03 20:05:01 +0900171 out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::RAW, false /*use_os_sep*/)
172 << ">\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700173 }
174 });
175}
176
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700177static void GenerateConstantDeclarations(CodeWriter& out, const AidlInterface& interface) {
178 for (const auto& constant : interface.GetConstantDeclarations()) {
179 const AidlConstantValue& value = constant->GetValue();
180 if (value.GetType() == AidlConstantValue::Type::STRING) {
181 out << "static const char* " << constant->GetName() << ";\n";
182 }
183 }
184 out << "\n";
185
186 bool hasIntegralConstant = false;
187 for (const auto& constant : interface.GetConstantDeclarations()) {
188 const AidlConstantValue& value = constant->GetValue();
189 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
190 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
191 hasIntegralConstant = true;
192 break;
193 }
194 }
195
196 if (hasIntegralConstant) {
197 out << "enum : int32_t {\n";
198 out.Indent();
199 for (const auto& constant : interface.GetConstantDeclarations()) {
200 const AidlConstantValue& value = constant->GetValue();
201 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
202 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
203 out << constant->GetName() << " = " << constant->ValueString(AidlConstantValueDecorator)
204 << ",\n";
205 }
206 }
207 out.Dedent();
208 out << "};\n";
209 }
210}
211static void GenerateConstantDefinitions(CodeWriter& out, const AidlInterface& interface) {
212 const std::string clazz = ClassName(interface, ClassNames::INTERFACE);
213
214 for (const auto& constant : interface.GetConstantDeclarations()) {
215 const AidlConstantValue& value = constant->GetValue();
216 if (value.GetType() == AidlConstantValue::Type::STRING) {
217 out << "const char* " << clazz << "::" << constant->GetName() << " = "
218 << constant->ValueString(AidlConstantValueDecorator) << ";\n";
219 }
220 }
221}
222
Steven Morelandb0057e72018-08-27 01:44:11 -0700223void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
224 const Options& options) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700225 GenerateSourceIncludes(out, types, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700226 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700227
Steven Morelandb0057e72018-08-27 01:44:11 -0700228 EnterNdkNamespace(out, defined_type);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700229 GenerateClassSource(out, types, defined_type, options);
Steven Morelandb0057e72018-08-27 01:44:11 -0700230 GenerateClientSource(out, types, defined_type, options);
231 GenerateServerSource(out, types, defined_type, options);
232 GenerateInterfaceSource(out, types, defined_type, options);
233 LeaveNdkNamespace(out, defined_type);
234}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700235
Steven Morelandaada3422018-09-20 15:55:33 -0700236static std::string MethodId(const AidlMethod& m) {
237 return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
238}
239
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900240static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types,
241 const AidlInterface& defined_type,
242 const AidlMethod& method,
243 const std::optional<std::string> return_value_cached_to,
244 const Options& options) {
Steven Morelandaada3422018-09-20 15:55:33 -0700245 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
246
Steven Moreland2bea13b2018-10-03 15:12:33 -0700247 out << NdkMethodDecl(types, method, clazz) << " {\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700248 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700249 out << "binder_status_t _aidl_ret_status = STATUS_OK;\n";
Steven Moreland63404532018-10-08 14:31:00 -0700250 out << "::ndk::ScopedAStatus _aidl_status;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900251
Jeongik Cha8f553e72019-02-12 16:58:03 +0900252 if (return_value_cached_to) {
253 out << "if (" << *return_value_cached_to << " != -1) {\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900254 out.Indent();
Jeongik Cha8f553e72019-02-12 16:58:03 +0900255 out << "*_aidl_return = " << *return_value_cached_to << ";\n"
Jeongik Chac9972922019-02-11 12:41:16 +0900256 << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"
257 << "return _aidl_status;\n";
258 out.Dedent();
259 out << "}\n";
260 }
261 out << "::ndk::ScopedAParcel _aidl_in;\n";
262 out << "::ndk::ScopedAParcel _aidl_out;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700263 out << "\n";
264
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900265 if (options.GenLog()) {
266 out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::CLIENT), method,
267 false /* isServer */, true /* isNdk */);
268 }
269
Steven Morelandaada3422018-09-20 15:55:33 -0700270 out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n";
271 StatusCheckGoto(out);
272
Steven Morelandeb38ee72018-10-15 14:20:04 -0700273 for (const auto& arg : method.GetArguments()) {
274 const std::string var_name = cpp::BuildVarName(*arg);
275
276 if (arg->IsIn()) {
277 out << "_aidl_ret_status = ";
278 const std::string prefix = (arg->IsOut() ? "*" : "");
279 WriteToParcelFor({out, types, arg->GetType(), "_aidl_in.get()", prefix + var_name});
280 out << ";\n";
281 StatusCheckGoto(out);
282 } else if (arg->IsOut() && arg->GetType().IsArray()) {
283 out << "_aidl_ret_status = ::ndk::AParcel_writeVectorSize(_aidl_in.get(), *" << var_name
284 << ");\n";
285 }
Steven Morelandaada3422018-09-20 15:55:33 -0700286 }
287 out << "_aidl_ret_status = AIBinder_transact(\n";
288 out.Indent();
289 out << "asBinder().get(),\n";
290 out << MethodId(method) << ",\n";
291 out << "_aidl_in.getR(),\n";
292 out << "_aidl_out.getR(),\n";
293 out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << ");\n";
294 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900295
296 // If the method is not implmented in the server side but the client has
297 // provided the default implementation, call it instead of failing hard.
298 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
299 out << "if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && ";
300 out << iface << "::getDefaultImpl()) {\n";
301 out.Indent();
302 out << "return " << iface << "::getDefaultImpl()->" << method.GetName() << "(";
303 out << NdkArgList(types, method, FormatArgNameOnly) << ");\n";
304 out.Dedent();
305 out << "}\n";
306
Steven Morelandaada3422018-09-20 15:55:33 -0700307 StatusCheckGoto(out);
308
309 if (!method.IsOneway()) {
310 out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n";
311 StatusCheckGoto(out);
312
313 out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n";
314 }
315
Steven Morelandaada3422018-09-20 15:55:33 -0700316 if (method.GetType().GetName() != "void") {
317 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700318 ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700319 out << ";\n";
320 StatusCheckGoto(out);
Jeongik Cha8f553e72019-02-12 16:58:03 +0900321 if (return_value_cached_to) {
322 out << *return_value_cached_to << " = *_aidl_return;\n";
Jeongik Chac9972922019-02-11 12:41:16 +0900323 }
Steven Morelandaada3422018-09-20 15:55:33 -0700324 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700325 for (const AidlArgument* arg : method.GetOutArguments()) {
326 out << "_aidl_ret_status = ";
327 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)});
328 out << ";\n";
329 StatusCheckGoto(out);
330 }
Steven Morelandaada3422018-09-20 15:55:33 -0700331
332 out << "_aidl_error:\n";
333 out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n";
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900334 if (options.GenLog()) {
335 out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::CLIENT), defined_type,
336 method, "_aidl_status", "_aidl_return", false /* isServer */,
337 true /* isNdk */);
338 }
Steven Morelandaada3422018-09-20 15:55:33 -0700339 out << "return _aidl_status;\n";
340 out.Dedent();
341 out << "}\n";
342}
343
Steven Moreland2bea13b2018-10-03 15:12:33 -0700344static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types,
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900345 const AidlInterface& defined_type,
346 const AidlMethod& method, const Options& options) {
Steven Morelandaada3422018-09-20 15:55:33 -0700347 out << "case " << MethodId(method) << ": {\n";
348 out.Indent();
349 for (const auto& arg : method.GetArguments()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700350 out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg)
351 << ";\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700352 }
353 if (method.GetType().GetName() != "void") {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700354 out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700355 }
356 out << "\n";
357
Steven Morelandeb38ee72018-10-15 14:20:04 -0700358 for (const auto& arg : method.GetArguments()) {
359 const std::string var_name = cpp::BuildVarName(*arg);
360
361 if (arg->IsIn()) {
362 out << "_aidl_ret_status = ";
363 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + var_name});
364 out << ";\n";
365 StatusCheckBreak(out);
366 } else if (arg->IsOut() && arg->GetType().IsArray()) {
367 out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n";
368 }
Steven Morelandaada3422018-09-20 15:55:33 -0700369 }
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900370 if (options.GenLog()) {
371 out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::SERVER), method,
372 true /* isServer */, true /* isNdk */);
373 }
Steven Moreland63404532018-10-08 14:31:00 -0700374 out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "("
Jiyong Park965c5b92018-11-21 13:37:15 +0900375 << NdkArgList(types, method, FormatArgForCall) << ");\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700376
Jeongik Chaeaf978e2019-05-04 00:32:35 +0900377 if (options.GenLog()) {
378 out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::SERVER), defined_type,
379 method, "_aidl_status", "_aidl_return", true /* isServer */,
380 true /* isNdk */);
381 }
Steven Morelandaada3422018-09-20 15:55:33 -0700382 if (method.IsOneway()) {
383 // For a oneway transaction, the kernel will have already returned a result. This is for the
384 // in-process case when a oneway transaction is parceled/unparceled in the same process.
385 out << "_aidl_ret_status = STATUS_OK;\n";
386 } else {
387 out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n";
388 StatusCheckBreak(out);
389
390 out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n";
391
Steven Morelandaada3422018-09-20 15:55:33 -0700392 if (method.GetType().GetName() != "void") {
393 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700394 WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700395 out << ";\n";
396 StatusCheckBreak(out);
397 }
Steven Moreland7c78d5d2018-10-15 14:21:11 -0700398 for (const AidlArgument* arg : method.GetOutArguments()) {
399 out << "_aidl_ret_status = ";
400 WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)});
401 out << ";\n";
402 StatusCheckBreak(out);
403 }
Steven Morelandaada3422018-09-20 15:55:33 -0700404 }
Steven Morelandaada3422018-09-20 15:55:33 -0700405 out << "break;\n";
406 out.Dedent();
407 out << "}\n";
408}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700409
Steven Moreland2bea13b2018-10-03 15:12:33 -0700410void GenerateClassSource(CodeWriter& out, const AidlTypenames& types,
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900411 const AidlInterface& defined_type, const Options& options) {
Steven Moreland0cf3f082018-09-20 19:09:46 -0700412 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
413 const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700414
Steven Moreland15b3ba72019-03-06 13:17:08 -0800415 out << "static binder_status_t "
416 << "_aidl_onTransact"
Steven Morelandaada3422018-09-20 15:55:33 -0700417 << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, "
418 "AParcel* _aidl_out) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700419 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700420 out << "(void)_aidl_in;\n";
421 out << "(void)_aidl_out;\n";
422 out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n";
423 if (!defined_type.GetMethods().empty()) {
Steven Moreland15b3ba72019-03-06 13:17:08 -0800424 // we know this cast is valid because this method is only called by the ICInterface
425 // AIBinder_Class object which is associated with this class.
426 out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz
427 << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700428 out << "switch (_aidl_code) {\n";
429 out.Indent();
430 for (const auto& method : defined_type.GetMethods()) {
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900431 GenerateServerCaseDefinition(out, types, defined_type, *method, options);
Steven Morelandaada3422018-09-20 15:55:33 -0700432 }
433 out.Dedent();
434 out << "}\n";
435 } else {
436 out << "(void)_aidl_binder;\n";
437 out << "(void)_aidl_code;\n";
438 }
439 out << "return _aidl_ret_status;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700440 out.Dedent();
441 out << "};\n\n";
442
Steven Moreland15b3ba72019-03-06 13:17:08 -0800443 out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz
444 << "::" << kDescriptor << ", _aidl_onTransact);\n\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700445}
Steven Morelandb0057e72018-08-27 01:44:11 -0700446void GenerateClientSource(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900447 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700448 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700449
Steven Moreland63404532018-10-08 14:31:00 -0700450 out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700451 out << clazz << "::~" << clazz << "() {}\n";
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900452 if (options.GenLog()) {
453 out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n";
454 }
Steven Morelandaada3422018-09-20 15:55:33 -0700455 out << "\n";
456 for (const auto& method : defined_type.GetMethods()) {
Jeongik Chac9972922019-02-11 12:41:16 +0900457 // Only getInterfaceVersion can use cache.
458 const bool cacheable = !method->IsUserDefined() && method->GetName() == kGetInterfaceVersion &&
459 options.Version() > 0;
Jeongik Cha8f553e72019-02-12 16:58:03 +0900460 const auto return_value_cached_to =
461 cacheable ? std::make_optional<std::string>(kCacheVariable) : std::nullopt;
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900462 GenerateClientMethodDefinition(out, types, defined_type, *method, return_value_cached_to,
463 options);
Steven Morelandaada3422018-09-20 15:55:33 -0700464 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700465}
Jiyong Park965c5b92018-11-21 13:37:15 +0900466void GenerateServerSource(CodeWriter& out, const AidlTypenames& types,
467 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700468 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Jiyong Park965c5b92018-11-21 13:37:15 +0900469 const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
Steven Morelandb0057e72018-08-27 01:44:11 -0700470
471 out << "// Source for " << clazz << "\n";
472 out << clazz << "::" << clazz << "() {}\n";
473 out << clazz << "::~" << clazz << "() {}\n";
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900474 if (options.GenLog()) {
475 out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n";
476 }
Steven Moreland63404532018-10-08 14:31:00 -0700477 out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700478 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800479 out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n";
Steven Morelanda57d0a62019-07-30 09:41:14 -0700480
481 out << "#ifndef __ANDROID_NDK__\n";
482 if (defined_type.IsVintfStability()) {
483 out << "AIBinder_markVintfStability(binder);\n";
484 } else {
485 out << "AIBinder_markCompilationUnitStability(binder);\n";
486 }
487 out << "#endif // __ANDROID_NDK__\n";
488
Steven Moreland63404532018-10-08 14:31:00 -0700489 out << "return ::ndk::SpAIBinder(binder);\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700490 out.Dedent();
491 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900492
493 // Implement the meta methods
494 for (const auto& method : defined_type.GetMethods()) {
495 if (method->IsUserDefined()) {
496 continue;
497 }
498 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
499 out << NdkMethodDecl(types, *method, clazz) << " {\n";
500 out.Indent();
501 out << "*_aidl_return = " << iface << "::" << kVersion << ";\n";
502 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
503 out.Dedent();
504 out << "}\n";
505 }
506 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700507}
Jiyong Park965c5b92018-11-21 13:37:15 +0900508void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types,
509 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700510 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
Steven Moreland15b3ba72019-03-06 13:17:08 -0800511 const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Morelandb0057e72018-08-27 01:44:11 -0700512
513 out << "// Source for " << clazz << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900514 out << "const char* " << clazz << "::" << kDescriptor << " = \""
515 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700516 out << clazz << "::" << clazz << "() {}\n";
517 out << clazz << "::~" << clazz << "() {}\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700518 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700519 GenerateConstantDefinitions(out, defined_type);
520 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700521
Steven Moreland3c316ed2019-01-17 09:39:37 -0800522 out << "std::shared_ptr<" << clazz << "> " << clazz
523 << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800524 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800525 out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n";
526 out << "std::shared_ptr<::ndk::ICInterface> interface = "
527 "::ndk::ICInterface::asInterface(binder.get());\n";
528 out << "if (interface) {\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800529 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800530 out << "return std::static_pointer_cast<" << clazz << ">(interface);\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800531 out.Dedent();
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800532 out << "}\n";
Steven Moreland15b3ba72019-03-06 13:17:08 -0800533 out << "return (new " << bp_clazz << "(binder))->ref<" << clazz << ">();\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800534 out.Dedent();
Steven Moreland3c316ed2019-01-17 09:39:37 -0800535 out << "}\n\n";
536
Steven Moreland2bea13b2018-10-03 15:12:33 -0700537 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<"
538 << clazz << ">& instance) {\n";
539 out.Indent();
540 out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : "
541 "nullptr);\n";
542 out.Dedent();
543 out << "}\n";
544
545 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<"
546 << clazz << ">* instance) {\n";
547 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700548 out << "::ndk::SpAIBinder binder;\n";
Steven Moreland055d8792018-11-14 12:48:42 -0800549 out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700550 out << "if (status != STATUS_OK) return status;\n";
Steven Moreland3c316ed2019-01-17 09:39:37 -0800551 out << "*instance = " << clazz << "::fromBinder(binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700552 out << "return STATUS_OK;\n";
553 out.Dedent();
554 out << "}\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900555
556 // defintion for static member setDefaultImpl
557 out << "bool " << clazz << "::setDefaultImpl(std::shared_ptr<" << clazz << "> impl) {\n";
558 out.Indent();
559 out << "if (!" << clazz << "::default_impl && impl) {\n";
560 out.Indent();
561 out << clazz << "::default_impl = impl;\n";
562 out << "return true;\n";
563 out.Dedent();
564 out << "}\n";
565 out << "return false;\n";
566 out.Dedent();
567 out << "}\n";
568
569 // definition for static member getDefaultImpl
570 out << "const std::shared_ptr<" << clazz << ">& " << clazz << "::getDefaultImpl() {\n";
571 out.Indent();
572 out << "return " << clazz << "::default_impl;\n";
573 out.Dedent();
574 out << "}\n";
575
576 // definition for the static field default_impl
577 out << "std::shared_ptr<" << clazz << "> " << clazz << "::default_impl = nullptr;\n";
578
579 // default implementation for the <Name>Default class members
580 const std::string defaultClazz = clazz + "Default";
581 for (const auto& method : defined_type.GetMethods()) {
582 if (method->IsUserDefined()) {
583 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
584 << NdkArgList(types, *method, FormatArgNameUnused) << ") {\n";
585 out.Indent();
586 out << "::ndk::ScopedAStatus _aidl_status;\n";
587 out << "_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));\n";
588 out << "return _aidl_status;\n";
589 out.Dedent();
590 out << "}\n";
591 } else {
592 if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
593 out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "("
594 << "int32_t* _aidl_return) {\n";
595 out.Indent();
596 out << "*_aidl_return = 0;\n";
597 out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n";
598 out.Dedent();
599 out << "}\n";
600 }
601 }
602 }
603
604 out << "::ndk::SpAIBinder " << defaultClazz << "::asBinder() {\n";
605 out.Indent();
606 out << "return ::ndk::SpAIBinder();\n";
607 out.Dedent();
608 out << "}\n";
609
610 out << "bool " << defaultClazz << "::isRemote() {\n";
611 out.Indent();
612 out << "return false;\n";
613 out.Dedent();
614 out << "}\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700615}
Jiyong Park965c5b92018-11-21 13:37:15 +0900616
Steven Morelandb0057e72018-08-27 01:44:11 -0700617void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900618 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700619 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
620
621 out << "#pragma once\n\n";
Jiyong Park5b7e5322019-04-03 20:05:01 +0900622 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700623 << "\"\n";
624 out << "\n";
625 out << "#include <android/binder_ibinder.h>\n";
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900626 if (options.GenLog()) {
627 out << "#include <json/value.h>\n";
628 out << "#include <functional>\n";
629 out << "#include <chrono>\n";
630 out << "#include <sstream>\n";
631 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700632 out << "\n";
633 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700634 out << "class " << clazz << " : public ::ndk::BpCInterface<"
Steven Morelandb0057e72018-08-27 01:44:11 -0700635 << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
636 out << "public:\n";
637 out.Indent();
Steven Moreland15b3ba72019-03-06 13:17:08 -0800638 out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700639 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700640 out << "\n";
641 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700642 out << NdkMethodDecl(types, *method) << " override;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700643 }
Jiyong Park965c5b92018-11-21 13:37:15 +0900644
645 if (options.Version() > 0) {
Jeongik Cha8f553e72019-02-12 16:58:03 +0900646 out << "int32_t " << kCacheVariable << " = -1;\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900647 }
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900648 if (options.GenLog()) {
649 out << "static std::function<void(const Json::Value&)> logFunc;\n";
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";
Jiyong Park5b7e5322019-04-03 20:05:01 +0900661 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, 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 }
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900684 if (options.GenLog()) {
685 out << "static std::function<void(const Json::Value&)> logFunc;\n";
686 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700687 out.Dedent();
Steven Moreland0cf3f082018-09-20 19:09:46 -0700688 out << "protected:\n";
689 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700690 out << "::ndk::SpAIBinder createBinder() override;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700691 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700692 out << "private:\n";
693 out.Indent();
694 out.Dedent();
695 out << "};\n";
696 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700697}
698void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types,
Jiyong Park965c5b92018-11-21 13:37:15 +0900699 const AidlInterface& defined_type, const Options& options) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700700 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
701
702 out << "#pragma once\n\n";
703 out << "#include <android/binder_interface_utils.h>\n";
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900704 if (options.GenLog()) {
705 out << "#include <json/value.h>\n";
706 out << "#include <functional>\n";
707 out << "#include <chrono>\n";
708 out << "#include <sstream>\n";
709 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700710 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700711
712 GenerateHeaderIncludes(out, types, defined_type);
713 out << "\n";
714
Steven Morelandb0057e72018-08-27 01:44:11 -0700715 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700716 out << "class " << clazz << " : public ::ndk::ICInterface {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700717 out << "public:\n";
718 out.Indent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900719 out << "static const char* " << kDescriptor << ";\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700720 out << clazz << "();\n";
721 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700722 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700723 GenerateConstantDeclarations(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900724 if (options.Version() > 0) {
725 out << "static const int32_t " << kVersion << " = " << std::to_string(options.Version())
726 << ";\n";
727 }
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700728 out << "\n";
Steven Moreland4cb2e6d2019-01-18 14:03:43 -0800729 out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700730 out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz
731 << ">& instance);";
Jiyong Park1b88cce2018-11-19 19:53:44 +0900732 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700733 out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz
734 << ">* instance);";
735 out << "\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900736 out << "static bool setDefaultImpl(std::shared_ptr<" << clazz << "> impl);";
737 out << "\n";
738 out << "static const std::shared_ptr<" << clazz << ">& getDefaultImpl();";
739 out << "\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700740 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700741 out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700742 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700743 out.Dedent();
Jiyong Park965c5b92018-11-21 13:37:15 +0900744 out << "private:\n";
745 out.Indent();
746 out << "static std::shared_ptr<" << clazz << "> default_impl;\n";
747 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700748 out << "};\n";
Jiyong Park965c5b92018-11-21 13:37:15 +0900749
750 const std::string defaultClazz = clazz + "Default";
751
752 out << "class " << defaultClazz << " : public " << clazz << " {\n";
753 out << "public:\n";
754 out.Indent();
755 for (const auto& method : defined_type.GetMethods()) {
756 if (method->IsUserDefined()) {
757 out << NdkMethodDecl(types, *method) << " override;\n";
758 } else if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) {
759 out << NdkMethodDecl(types, *method) << " override;\n";
760 }
761 }
762 out << "::ndk::SpAIBinder asBinder() override;\n";
763 out << "bool isRemote() override;\n";
764 out.Dedent();
765 out << "};\n";
766
Steven Morelandb0057e72018-08-27 01:44:11 -0700767 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700768}
769void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700770 const AidlStructuredParcelable& defined_type,
771 const Options& /*options*/) {
772 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
773
Steven Morelandb0057e72018-08-27 01:44:11 -0700774 out << "#pragma once\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700775 out << "#include <android/binder_interface_utils.h>\n";
776 out << "\n";
777
778 GenerateHeaderIncludes(out, types, defined_type);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700779
Steven Morelandb0057e72018-08-27 01:44:11 -0700780 EnterNdkNamespace(out, defined_type);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700781 out << "class " << clazz << " {\n";
782 out << "public:\n";
783 out.Indent();
784 out << "static const char* descriptor;\n";
785 out << "\n";
786 for (const auto& variable : defined_type.GetFields()) {
787 out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName();
788 if (variable->GetDefaultValue()) {
789 out << " = " << variable->ValueString(AidlConstantValueDecorator);
790 }
791 out << ";\n";
792 }
793 out << "\n";
794 out << "binder_status_t readFromParcel(const AParcel* parcel);\n";
795 out << "binder_status_t writeToParcel(AParcel* parcel) const;\n";
796 out.Dedent();
797 out << "};\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700798 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700799}
800void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700801 const AidlStructuredParcelable& defined_type,
802 const Options& /*options*/) {
803 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
804
Jiyong Park5b7e5322019-04-03 20:05:01 +0900805 out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/)
Steven Morelandb0057e72018-08-27 01:44:11 -0700806 << "\"\n";
807 out << "\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700808 GenerateSourceIncludes(out, types, defined_type);
809 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700810 EnterNdkNamespace(out, defined_type);
Jiyong Park965c5b92018-11-21 13:37:15 +0900811 out << "const char* " << clazz << "::" << kDescriptor << " = \""
812 << defined_type.GetCanonicalName() << "\";\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700813 out << "\n";
814
815 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n";
816 out.Indent();
817 out << "std::string _aidl_descriptor;\n";
818 out << "binder_status_t _aidl_ret_status;\n";
819
820 out << "int32_t _aidl_null;\n";
Jeongik Cha95eba572018-11-22 09:14:52 +0900821 out << "int32_t _aidl_parcelable_size;\n";
822 out << "int32_t _aidl_start_pos;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700823 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_null);\n";
824 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900825 out << "_aidl_start_pos = AParcel_getDataPosition(parcel);\n";
826 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_parcelable_size);\n";
827 out << "if (_aidl_parcelable_size < 0) return STATUS_BAD_VALUE;\n";
828 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700829
830 // TODO(b/117281836)
831 out << "if (_aidl_null == 0) return STATUS_UNEXPECTED_NULL;\n\n";
832
833 for (const auto& variable : defined_type.GetFields()) {
834 out << "_aidl_ret_status = ";
835 ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()});
836 out << ";\n";
837 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900838 out << "if (AParcel_getDataPosition(parcel) - _aidl_start_pos >= _aidl_parcelable_size) {\n"
839 << " AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
840 << " return _aidl_ret_status;\n"
841 << "}\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700842 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900843 out << "AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n"
844 << "return _aidl_ret_status;\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700845 out.Dedent();
846 out << "}\n";
847
848 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n";
849 out.Indent();
850 out << "binder_status_t _aidl_ret_status;\n";
851
852 // non-null
853 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 1);\n";
854 StatusCheckReturn(out);
Jeongik Cha95eba572018-11-22 09:14:52 +0900855 out << "size_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n";
856 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 0);\n";
857 StatusCheckReturn(out);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700858
859 for (const auto& variable : defined_type.GetFields()) {
860 out << "_aidl_ret_status = ";
861 WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()});
862 out << ";\n";
863 StatusCheckReturn(out);
864 }
Jeongik Cha95eba572018-11-22 09:14:52 +0900865 out << "size_t _aidl_end_pos = AParcel_getDataPosition(parcel);\n";
866 out << "AParcel_setDataPosition(parcel, _aidl_start_pos);\n";
867 out << "AParcel_writeInt32(parcel, _aidl_end_pos - _aidl_start_pos);\n";
868 out << "AParcel_setDataPosition(parcel, _aidl_end_pos);\n";
869
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700870 out << "return _aidl_ret_status;\n";
871 out.Dedent();
872 out << "}\n";
873 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700874 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700875}
876} // namespace internals
877} // namespace ndk
878} // namespace aidl
879} // namespace android