blob: fe2ad539f1593fda2a9027072267dd3d44b57ad4 [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
19#include "aidl_language.h"
20#include "aidl_to_cpp_common.h"
Steven Morelandaada3422018-09-20 15:55:33 -070021#include "aidl_to_ndk.h"
Steven Morelandb0057e72018-08-27 01:44:11 -070022
23#include <android-base/logging.h>
24
25namespace android {
26namespace aidl {
27namespace ndk {
28
29using namespace internals;
30using cpp::ClassNames;
31
32void GenerateNdkInterface(const string& output_file, const Options& options,
33 const AidlTypenames& types, const AidlInterface& defined_type,
34 const IoDelegate& io_delegate) {
Steven Morelandb0057e72018-08-27 01:44:11 -070035 const string i_header =
36 options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::INTERFACE);
37 unique_ptr<CodeWriter> i_writer(io_delegate.GetCodeWriter(i_header));
38 GenerateInterfaceHeader(*i_writer, types, defined_type, options);
39 CHECK(i_writer->Close());
40
41 const string bp_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::CLIENT);
42 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
43 GenerateClientHeader(*bp_writer, types, defined_type, options);
44 CHECK(bp_writer->Close());
45
46 const string bn_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::SERVER);
47 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
48 GenerateServerHeader(*bn_writer, types, defined_type, options);
49 CHECK(bn_writer->Close());
50
51 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
52 GenerateSource(*source_writer, types, defined_type, options);
53 CHECK(source_writer->Close());
54}
55
56void GenerateNdkParcel(const string& output_file, const Options& options,
57 const AidlTypenames& types, const AidlStructuredParcelable& defined_type,
58 const IoDelegate& io_delegate) {
59 const string header_path = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::BASE);
60 unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path));
61 GenerateParcelHeader(*header_writer, types, defined_type, options);
62 CHECK(header_writer->Close());
63
64 const string bp_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::CLIENT);
65 unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header));
66 *bp_writer << "#error TODO(b/111362593) defined_types do not have bp classes\n";
67 CHECK(bp_writer->Close());
68
69 const string bn_header = options.OutputHeaderDir() + HeaderFile(defined_type, ClassNames::SERVER);
70 unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header));
71 *bn_writer << "#error TODO(b/111362593) defined_types do not have bn classes\n";
72 CHECK(bn_writer->Close());
73
74 unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file);
75 GenerateParcelSource(*source_writer, types, defined_type, options);
76 CHECK(source_writer->Close());
77}
78
79void GenerateNdk(const string& output_file, const Options& options, const AidlTypenames& types,
80 const AidlDefinedType& defined_type, const IoDelegate& io_delegate) {
81 const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable();
82 if (parcelable != nullptr) {
83 GenerateNdkParcel(output_file, options, types, *parcelable, io_delegate);
84 return;
85 }
86
87 const AidlInterface* interface = defined_type.AsInterface();
88 if (interface != nullptr) {
89 GenerateNdkInterface(output_file, options, types, *interface, io_delegate);
90 return;
91 }
92
93 CHECK(false) << "Unrecognized type sent for cpp generation.";
94}
95namespace internals {
96
97void EnterNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
98 out << "namespace aidl {\n";
99 cpp::EnterNamespace(out, defined_type);
100}
101void LeaveNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
102 cpp::LeaveNamespace(out, defined_type);
103 out << "} // namespace aidl\n";
104}
105
Steven Morelandaada3422018-09-20 15:55:33 -0700106static void StatusCheckGoto(CodeWriter& out) {
107 out << "if (_aidl_ret_status != STATUS_OK) goto _aidl_error;\n\n";
108}
109static void StatusCheckBreak(CodeWriter& out) {
110 out << "if (_aidl_ret_status != STATUS_OK) break;\n\n";
111}
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700112static void StatusCheckReturn(CodeWriter& out) {
113 out << "if (_aidl_ret_status != STATUS_OK) return _aidl_ret_status;\n\n";
114}
Steven Morelandaada3422018-09-20 15:55:33 -0700115
Steven Moreland2bea13b2018-10-03 15:12:33 -0700116static void GenerateHeaderIncludes(CodeWriter& out, const AidlTypenames& types,
117 const AidlDefinedType& defined_type) {
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700118 out << "#include <android/binder_parcel_utils.h>\n";
119
Steven Moreland2bea13b2018-10-03 15:12:33 -0700120 types.IterateTypes([&](const AidlDefinedType& other_defined_type) {
121 if (&other_defined_type == &defined_type) return;
122
123 if (other_defined_type.AsInterface() != nullptr) {
124 out << "#include <"
125 << HeaderFile(other_defined_type, ClassNames::INTERFACE, false /*use_os_sep*/) << ">\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700126 } else if (other_defined_type.AsStructuredParcelable() != nullptr) {
127 out << "#include <" << HeaderFile(other_defined_type, ClassNames::BASE, false /*use_os_sep*/)
128 << ">\n";
129 } else if (other_defined_type.AsParcelable() != nullptr) {
130 out << "#include \"" << other_defined_type.AsParcelable()->GetCppHeader() << "\"\n";
131 } else {
132 AIDL_FATAL(defined_type) << "Unrecognized type.";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700133 }
134 });
135}
136static void GenerateSourceIncludes(CodeWriter& out, const AidlTypenames& types,
137 const AidlDefinedType& /*defined_type*/) {
138 types.IterateTypes([&](const AidlDefinedType& a_defined_type) {
139 if (a_defined_type.AsInterface() != nullptr) {
140 out << "#include <" << HeaderFile(a_defined_type, ClassNames::CLIENT, false /*use_os_sep*/)
141 << ">\n";
142 out << "#include <" << HeaderFile(a_defined_type, ClassNames::SERVER, false /*use_os_sep*/)
143 << ">\n";
144 out << "#include <" << HeaderFile(a_defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
145 << ">\n";
146 }
147 });
148}
149
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700150static void GenerateConstantDeclarations(CodeWriter& out, const AidlInterface& interface) {
151 for (const auto& constant : interface.GetConstantDeclarations()) {
152 const AidlConstantValue& value = constant->GetValue();
153 if (value.GetType() == AidlConstantValue::Type::STRING) {
154 out << "static const char* " << constant->GetName() << ";\n";
155 }
156 }
157 out << "\n";
158
159 bool hasIntegralConstant = false;
160 for (const auto& constant : interface.GetConstantDeclarations()) {
161 const AidlConstantValue& value = constant->GetValue();
162 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
163 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
164 hasIntegralConstant = true;
165 break;
166 }
167 }
168
169 if (hasIntegralConstant) {
170 out << "enum : int32_t {\n";
171 out.Indent();
172 for (const auto& constant : interface.GetConstantDeclarations()) {
173 const AidlConstantValue& value = constant->GetValue();
174 if (value.GetType() == AidlConstantValue::Type::HEXIDECIMAL ||
175 value.GetType() == AidlConstantValue::Type::INTEGRAL) {
176 out << constant->GetName() << " = " << constant->ValueString(AidlConstantValueDecorator)
177 << ",\n";
178 }
179 }
180 out.Dedent();
181 out << "};\n";
182 }
183}
184static void GenerateConstantDefinitions(CodeWriter& out, const AidlInterface& interface) {
185 const std::string clazz = ClassName(interface, ClassNames::INTERFACE);
186
187 for (const auto& constant : interface.GetConstantDeclarations()) {
188 const AidlConstantValue& value = constant->GetValue();
189 if (value.GetType() == AidlConstantValue::Type::STRING) {
190 out << "const char* " << clazz << "::" << constant->GetName() << " = "
191 << constant->ValueString(AidlConstantValueDecorator) << ";\n";
192 }
193 }
194}
195
Steven Morelandb0057e72018-08-27 01:44:11 -0700196void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type,
197 const Options& options) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700198 GenerateSourceIncludes(out, types, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700199 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700200
Steven Morelandb0057e72018-08-27 01:44:11 -0700201 EnterNdkNamespace(out, defined_type);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700202 GenerateClassSource(out, types, defined_type, options);
Steven Morelandb0057e72018-08-27 01:44:11 -0700203 GenerateClientSource(out, types, defined_type, options);
204 GenerateServerSource(out, types, defined_type, options);
205 GenerateInterfaceSource(out, types, defined_type, options);
206 LeaveNdkNamespace(out, defined_type);
207}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700208
209static std::string DataClassFor(const AidlInterface& defined_type) {
210 return "AidlClassData_" + ClassName(defined_type, ClassNames::INTERFACE);
211}
Steven Morelandaada3422018-09-20 15:55:33 -0700212static std::string MethodId(const AidlMethod& m) {
213 return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
214}
215
Steven Moreland2bea13b2018-10-03 15:12:33 -0700216static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types,
217 const AidlInterface& defined_type,
Steven Morelandaada3422018-09-20 15:55:33 -0700218 const AidlMethod& method) {
219 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
220
Steven Moreland2bea13b2018-10-03 15:12:33 -0700221 out << NdkMethodDecl(types, method, clazz) << " {\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700222 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700223 out << "::ndk::ScopedAParcel _aidl_in;\n";
224 out << "::ndk::ScopedAParcel _aidl_out;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700225 out << "binder_status_t _aidl_ret_status = STATUS_OK;\n";
Steven Moreland63404532018-10-08 14:31:00 -0700226 out << "::ndk::ScopedAStatus _aidl_status;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700227 out << "\n";
228
229 out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n";
230 StatusCheckGoto(out);
231
232 for (const AidlArgument* arg : method.GetInArguments()) {
233 out << "_aidl_ret_status = ";
234 std::string prefix = arg->IsOut() ? "*" : "";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700235 WriteToParcelFor(
236 {out, types, arg->GetType(), "_aidl_in.get()", prefix + cpp::BuildVarName(*arg)});
Steven Morelandaada3422018-09-20 15:55:33 -0700237 out << ";\n";
238 StatusCheckGoto(out);
239 }
240 out << "_aidl_ret_status = AIBinder_transact(\n";
241 out.Indent();
242 out << "asBinder().get(),\n";
243 out << MethodId(method) << ",\n";
244 out << "_aidl_in.getR(),\n";
245 out << "_aidl_out.getR(),\n";
246 out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << ");\n";
247 out.Dedent();
248 StatusCheckGoto(out);
249
250 if (!method.IsOneway()) {
251 out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n";
252 StatusCheckGoto(out);
253
254 out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n";
255 }
256
257 for (const AidlArgument* arg : method.GetOutArguments()) {
258 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700259 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)});
Steven Morelandaada3422018-09-20 15:55:33 -0700260 out << ";\n";
261 StatusCheckGoto(out);
262 }
263
264 if (method.GetType().GetName() != "void") {
265 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700266 ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700267 out << ";\n";
268 StatusCheckGoto(out);
269 }
270
271 out << "_aidl_error:\n";
272 out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n";
273 out << "return _aidl_status;\n";
274 out.Dedent();
275 out << "}\n";
276}
277
Steven Moreland2bea13b2018-10-03 15:12:33 -0700278static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types,
279 const AidlInterface& /*defined_type*/,
Steven Morelandaada3422018-09-20 15:55:33 -0700280 const AidlMethod& method) {
281 out << "case " << MethodId(method) << ": {\n";
282 out.Indent();
283 for (const auto& arg : method.GetArguments()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700284 out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg)
285 << ";\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700286 }
287 if (method.GetType().GetName() != "void") {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700288 out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700289 }
290 out << "\n";
291
292 for (const AidlArgument* arg : method.GetInArguments()) {
293 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700294 ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + cpp::BuildVarName(*arg)});
Steven Morelandaada3422018-09-20 15:55:33 -0700295 out << ";\n";
296 StatusCheckBreak(out);
297 }
298
Steven Moreland63404532018-10-08 14:31:00 -0700299 out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "("
Steven Morelandaada3422018-09-20 15:55:33 -0700300 << NdkCallListFor(method) << ");\n";
301
302 if (method.IsOneway()) {
303 // For a oneway transaction, the kernel will have already returned a result. This is for the
304 // in-process case when a oneway transaction is parceled/unparceled in the same process.
305 out << "_aidl_ret_status = STATUS_OK;\n";
306 } else {
307 out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n";
308 StatusCheckBreak(out);
309
310 out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n";
311
312 for (const AidlArgument* arg : method.GetOutArguments()) {
313 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700314 WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)});
Steven Morelandaada3422018-09-20 15:55:33 -0700315 out << ";\n";
316 StatusCheckBreak(out);
317 }
318 if (method.GetType().GetName() != "void") {
319 out << "_aidl_ret_status = ";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700320 WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"});
Steven Morelandaada3422018-09-20 15:55:33 -0700321 out << ";\n";
322 StatusCheckBreak(out);
323 }
324 }
325
326 out << "break;\n";
327 out.Dedent();
328 out << "}\n";
329}
Steven Moreland0cf3f082018-09-20 19:09:46 -0700330
Steven Moreland2bea13b2018-10-03 15:12:33 -0700331void GenerateClassSource(CodeWriter& out, const AidlTypenames& types,
Steven Moreland0cf3f082018-09-20 19:09:46 -0700332 const AidlInterface& defined_type, const Options& /*options*/) {
333 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
334 const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
335 const std::string data_clazz = DataClassFor(defined_type);
336 const std::string on_create = data_clazz + "_onCreate";
337 const std::string on_destroy = data_clazz + "_onDestory";
338 const std::string on_transact = data_clazz + "_onTransact";
339
340 out << "struct " << data_clazz << " {\n";
341 out.Indent();
342 out << "static AIBinder_Class* clazz;\n";
343 out << "std::shared_ptr<" << bn_clazz << "> instance;\n";
344 out.Dedent();
345 out << "};\n\n";
346
347 out << "static void* " << on_create << "(void* args) {\n";
348 out.Indent();
349 out << data_clazz << "* data = new " << data_clazz << "{static_cast<" << bn_clazz
350 << "*>(args)->ref<" << bn_clazz << ">()};\n";
351 out << "return static_cast<void*>(data);\n";
352 out.Dedent();
353 out << "};\n\n";
354
355 out << "static void " << on_destroy << "(void* userData) {\n";
356 out.Indent();
357 out << "delete static_cast<" << data_clazz << "*>(userData);\n";
358 out.Dedent();
359 out << "};\n\n";
360
361 out << "static binder_status_t " << on_transact
Steven Morelandaada3422018-09-20 15:55:33 -0700362 << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, "
363 "AParcel* _aidl_out) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700364 out.Indent();
Steven Morelandaada3422018-09-20 15:55:33 -0700365 out << "(void)_aidl_in;\n";
366 out << "(void)_aidl_out;\n";
367 out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n";
368 if (!defined_type.GetMethods().empty()) {
369 out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = static_cast<" << data_clazz
370 << "*>(AIBinder_getUserData(_aidl_binder))->instance;\n";
371 out << "switch (_aidl_code) {\n";
372 out.Indent();
373 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700374 GenerateServerCaseDefinition(out, types, defined_type, *method);
Steven Morelandaada3422018-09-20 15:55:33 -0700375 }
376 out.Dedent();
377 out << "}\n";
378 } else {
379 out << "(void)_aidl_binder;\n";
380 out << "(void)_aidl_code;\n";
381 }
382 out << "return _aidl_ret_status;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700383 out.Dedent();
384 out << "};\n\n";
385
386 out << "AIBinder_Class* " << data_clazz << "::clazz = AIBinder_Class_define(" << clazz
387 << "::descriptor, " << on_create << ", " << on_destroy << ", " << on_transact << ");\n\n";
388}
Steven Morelandb0057e72018-08-27 01:44:11 -0700389void GenerateClientSource(CodeWriter& out, const AidlTypenames& types,
Steven Moreland2bea13b2018-10-03 15:12:33 -0700390 const AidlInterface& defined_type, const Options& /*options*/) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700391 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700392 const std::string data_clazz = DataClassFor(defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700393
394 out << "// Source for " << clazz << "\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700395 out << "std::shared_ptr<" << clazz << "> " << clazz
Steven Moreland63404532018-10-08 14:31:00 -0700396 << "::associate(const ::ndk::SpAIBinder& binder) {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700397 out.Indent();
398 out << "if (!AIBinder_associateClass(binder.get(), " << data_clazz
399 << "::clazz)) { return nullptr; }\n";
Steven Moreland1c4b4052018-10-10 12:24:22 -0700400 out << "return (new " << clazz << "(binder))->ref<" << clazz << ">();\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700401 out.Dedent();
402 out << "}\n\n";
403
Steven Moreland63404532018-10-08 14:31:00 -0700404 out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700405 out << clazz << "::~" << clazz << "() {}\n";
406 out << "\n";
407 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700408 GenerateClientMethodDefinition(out, types, defined_type, *method);
Steven Morelandaada3422018-09-20 15:55:33 -0700409 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700410}
Steven Moreland2bea13b2018-10-03 15:12:33 -0700411void GenerateServerSource(CodeWriter& out, const AidlTypenames& /*types*/,
412 const AidlInterface& defined_type, const Options& /*options*/) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700413 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
Steven Moreland0cf3f082018-09-20 19:09:46 -0700414 const std::string data_clazz = DataClassFor(defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700415
416 out << "// Source for " << clazz << "\n";
417 out << clazz << "::" << clazz << "() {}\n";
418 out << clazz << "::~" << clazz << "() {}\n";
419
Steven Moreland63404532018-10-08 14:31:00 -0700420 out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700421 out.Indent();
422 out << "AIBinder* binder = AIBinder_new(" << data_clazz
423 << "::clazz, static_cast<void*>(this));\n";
Steven Moreland63404532018-10-08 14:31:00 -0700424 out << "return ::ndk::SpAIBinder(binder);\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700425 out.Dedent();
426 out << "}\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700427}
Steven Moreland2bea13b2018-10-03 15:12:33 -0700428void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& /*types*/,
429 const AidlInterface& defined_type, const Options& /*options*/) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700430 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700431 const std::string data_clazz = DataClassFor(defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700432
433 out << "// Source for " << clazz << "\n";
434 out << "const char* " << clazz << "::descriptor = \"" << defined_type.GetCanonicalName()
435 << "\";\n";
436 out << clazz << "::" << clazz << "() {}\n";
437 out << clazz << "::~" << clazz << "() {}\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700438 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700439 GenerateConstantDefinitions(out, defined_type);
440 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700441
Steven Moreland2bea13b2018-10-03 15:12:33 -0700442 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<"
443 << clazz << ">& instance) {\n";
444 out.Indent();
445 out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : "
446 "nullptr);\n";
447 out.Dedent();
448 out << "}\n";
449
450 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<"
451 << clazz << ">* instance) {\n";
452 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700453 out << "::ndk::SpAIBinder binder;\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700454 out << "binder_status_t status = AParcel_readNullableStrongBinder(parcel, binder.getR());\n";
455 out << "if (status != STATUS_OK) return status;\n";
456 out << data_clazz << "* data = static_cast<" << data_clazz
457 << "*>(AIBinder_getUserData(binder.get()));\n";
458 out << "if (data) {\n";
459 out.Indent();
460 out << "*instance = data->instance;\n";
461 out.Dedent();
462 out << "} else {\n";
463 out.Indent();
464 out << "*instance = " << NdkFullClassName(defined_type, ClassNames::CLIENT)
465 << "::associate(binder);\n";
466 out.Dedent();
467 out << "}\n";
468 out << "return STATUS_OK;\n";
469 out.Dedent();
470 out << "}\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700471}
472void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types,
Steven Moreland2bea13b2018-10-03 15:12:33 -0700473 const AidlInterface& defined_type, const Options& /*options*/) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700474 const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
475
476 out << "#pragma once\n\n";
477 out << "#include \"" << HeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
478 << "\"\n";
479 out << "\n";
480 out << "#include <android/binder_ibinder.h>\n";
481 out << "\n";
482 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700483 out << "class " << clazz << " : public ::ndk::BpCInterface<"
Steven Morelandb0057e72018-08-27 01:44:11 -0700484 << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
485 out << "public:\n";
486 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700487 out << "static std::shared_ptr<" << clazz << "> associate(const ::ndk::SpAIBinder& binder);\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700488 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700489 out << "\n";
490 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700491 out << NdkMethodDecl(types, *method) << " override;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700492 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700493 out.Dedent();
494 out << "private:\n";
495 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700496 out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700497 out.Dedent();
498 out << "};\n";
499 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700500}
Steven Moreland2bea13b2018-10-03 15:12:33 -0700501void GenerateServerHeader(CodeWriter& out, const AidlTypenames& /*types*/,
502 const AidlInterface& defined_type, const Options& /*options*/) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700503 const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
504
505 out << "#pragma once\n\n";
506 out << "#include \"" << HeaderFile(defined_type, ClassNames::INTERFACE, false /*use_os_sep*/)
507 << "\"\n";
508 out << "\n";
509 out << "#include <android/binder_ibinder.h>\n";
510 out << "\n";
511 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700512 out << "class " << clazz << " : public ::ndk::BnCInterface<"
Steven Morelandb0057e72018-08-27 01:44:11 -0700513 << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
514 out << "public:\n";
515 out.Indent();
516 out << clazz << "();\n";
517 out << "virtual ~" << clazz << "();\n";
518 out.Dedent();
Steven Moreland0cf3f082018-09-20 19:09:46 -0700519 out << "protected:\n";
520 out.Indent();
Steven Moreland63404532018-10-08 14:31:00 -0700521 out << "::ndk::SpAIBinder createBinder() override;\n";
Steven Moreland0cf3f082018-09-20 19:09:46 -0700522 out.Dedent();
Steven Morelandb0057e72018-08-27 01:44:11 -0700523 out << "private:\n";
524 out.Indent();
525 out.Dedent();
526 out << "};\n";
527 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700528}
529void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types,
Steven Moreland2bea13b2018-10-03 15:12:33 -0700530 const AidlInterface& defined_type, const Options& /*options*/) {
Steven Morelandb0057e72018-08-27 01:44:11 -0700531 const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
532
533 out << "#pragma once\n\n";
534 out << "#include <android/binder_interface_utils.h>\n";
535 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700536
537 GenerateHeaderIncludes(out, types, defined_type);
538 out << "\n";
539
Steven Morelandb0057e72018-08-27 01:44:11 -0700540 EnterNdkNamespace(out, defined_type);
Steven Moreland63404532018-10-08 14:31:00 -0700541 out << "class " << clazz << " : public ::ndk::ICInterface {\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700542 out << "public:\n";
543 out.Indent();
Steven Moreland0cf3f082018-09-20 19:09:46 -0700544 out << "static AIBinder_Class* clazz;\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700545 out << "static const char* descriptor;\n";
546 out << clazz << "();\n";
547 out << "virtual ~" << clazz << "();\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700548 out << "\n";
Steven Morelandac8fe7f2018-10-04 09:58:31 -0700549 GenerateConstantDeclarations(out, defined_type);
550 out << "\n";
Steven Moreland2bea13b2018-10-03 15:12:33 -0700551 out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz
552 << ">& instance);";
553 out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz
554 << ">* instance);";
555 out << "\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700556 for (const auto& method : defined_type.GetMethods()) {
Steven Moreland2bea13b2018-10-03 15:12:33 -0700557 out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n";
Steven Morelandaada3422018-09-20 15:55:33 -0700558 }
Steven Morelandb0057e72018-08-27 01:44:11 -0700559 out.Dedent();
560 out << "};\n";
561 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700562}
563void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700564 const AidlStructuredParcelable& defined_type,
565 const Options& /*options*/) {
566 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
567
Steven Morelandb0057e72018-08-27 01:44:11 -0700568 out << "#pragma once\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700569 out << "#include <android/binder_interface_utils.h>\n";
570 out << "\n";
571
572 GenerateHeaderIncludes(out, types, defined_type);
Steven Moreland2bea13b2018-10-03 15:12:33 -0700573
Steven Morelandb0057e72018-08-27 01:44:11 -0700574 EnterNdkNamespace(out, defined_type);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700575 out << "class " << clazz << " {\n";
576 out << "public:\n";
577 out.Indent();
578 out << "static const char* descriptor;\n";
579 out << "\n";
580 for (const auto& variable : defined_type.GetFields()) {
581 out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName();
582 if (variable->GetDefaultValue()) {
583 out << " = " << variable->ValueString(AidlConstantValueDecorator);
584 }
585 out << ";\n";
586 }
587 out << "\n";
588 out << "binder_status_t readFromParcel(const AParcel* parcel);\n";
589 out << "binder_status_t writeToParcel(AParcel* parcel) const;\n";
590 out.Dedent();
591 out << "};\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700592 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700593}
594void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types,
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700595 const AidlStructuredParcelable& defined_type,
596 const Options& /*options*/) {
597 const std::string clazz = ClassName(defined_type, ClassNames::BASE);
598
Steven Morelandb0057e72018-08-27 01:44:11 -0700599 out << "#include \"" << HeaderFile(defined_type, ClassNames::BASE, false /*use_os_sep*/)
600 << "\"\n";
601 out << "\n";
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700602 GenerateSourceIncludes(out, types, defined_type);
603 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700604 EnterNdkNamespace(out, defined_type);
Steven Morelandbb8f46c2018-10-03 17:38:00 -0700605 out << "const char* " << clazz << "::descriptor = \"" << defined_type.GetCanonicalName()
606 << "\";\n";
607 out << "\n";
608
609 out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n";
610 out.Indent();
611 out << "std::string _aidl_descriptor;\n";
612 out << "binder_status_t _aidl_ret_status;\n";
613
614 out << "int32_t _aidl_null;\n";
615 out << "_aidl_ret_status = AParcel_readInt32(parcel, &_aidl_null);\n";
616 StatusCheckReturn(out);
617
618 // TODO(b/117281836)
619 out << "if (_aidl_null == 0) return STATUS_UNEXPECTED_NULL;\n\n";
620
621 for (const auto& variable : defined_type.GetFields()) {
622 out << "_aidl_ret_status = ";
623 ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()});
624 out << ";\n";
625 StatusCheckReturn(out);
626 }
627 out << "return _aidl_ret_status;\n";
628 out.Dedent();
629 out << "}\n";
630
631 out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n";
632 out.Indent();
633 out << "binder_status_t _aidl_ret_status;\n";
634
635 // non-null
636 out << "_aidl_ret_status = AParcel_writeInt32(parcel, 1);\n";
637 StatusCheckReturn(out);
638
639 for (const auto& variable : defined_type.GetFields()) {
640 out << "_aidl_ret_status = ";
641 WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()});
642 out << ";\n";
643 StatusCheckReturn(out);
644 }
645 out << "return _aidl_ret_status;\n";
646 out.Dedent();
647 out << "}\n";
648 out << "\n";
Steven Morelandb0057e72018-08-27 01:44:11 -0700649 LeaveNdkNamespace(out, defined_type);
Steven Morelandb0057e72018-08-27 01:44:11 -0700650}
651} // namespace internals
652} // namespace ndk
653} // namespace aidl
654} // namespace android