Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1 | /* |
| 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 Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 19 | #include "aidl.h" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 20 | #include "aidl_language.h" |
| 21 | #include "aidl_to_cpp_common.h" |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 22 | #include "aidl_to_ndk.h" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace aidl { |
| 28 | namespace ndk { |
| 29 | |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 30 | static constexpr const char* kClazz = "_g_aidl_clazz"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 31 | static constexpr const char* kDescriptor = "descriptor"; |
| 32 | static constexpr const char* kVersion = "version"; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 33 | static constexpr const char* kHash = "hash"; |
| 34 | static constexpr const char* kCachedVersion = "_aidl_cached_version"; |
| 35 | static constexpr const char* kCachedHash = "_aidl_cached_hash"; |
| 36 | static constexpr const char* kCachedHashMutex = "_aidl_cached_hash_mutex"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 37 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 38 | using namespace internals; |
| 39 | using cpp::ClassNames; |
| 40 | |
| 41 | void GenerateNdkInterface(const string& output_file, const Options& options, |
| 42 | const AidlTypenames& types, const AidlInterface& defined_type, |
| 43 | const IoDelegate& io_delegate) { |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 44 | const string i_header = options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 45 | unique_ptr<CodeWriter> i_writer(io_delegate.GetCodeWriter(i_header)); |
| 46 | GenerateInterfaceHeader(*i_writer, types, defined_type, options); |
| 47 | CHECK(i_writer->Close()); |
| 48 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 49 | const string bp_header = |
| 50 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 51 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 52 | GenerateClientHeader(*bp_writer, types, defined_type, options); |
| 53 | CHECK(bp_writer->Close()); |
| 54 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 55 | const string bn_header = |
| 56 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 57 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 58 | GenerateServerHeader(*bn_writer, types, defined_type, options); |
| 59 | CHECK(bn_writer->Close()); |
| 60 | |
| 61 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
| 62 | GenerateSource(*source_writer, types, defined_type, options); |
| 63 | CHECK(source_writer->Close()); |
| 64 | } |
| 65 | |
| 66 | void GenerateNdkParcel(const string& output_file, const Options& options, |
| 67 | const AidlTypenames& types, const AidlStructuredParcelable& defined_type, |
| 68 | const IoDelegate& io_delegate) { |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 69 | const string header_path = |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 70 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 71 | unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path)); |
| 72 | GenerateParcelHeader(*header_writer, types, defined_type, options); |
| 73 | CHECK(header_writer->Close()); |
| 74 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 75 | const string bp_header = |
| 76 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 77 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 78 | *bp_writer << "#error TODO(b/111362593) defined_types do not have bp classes\n"; |
| 79 | CHECK(bp_writer->Close()); |
| 80 | |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 81 | const string bn_header = |
| 82 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 83 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 84 | *bn_writer << "#error TODO(b/111362593) defined_types do not have bn classes\n"; |
| 85 | CHECK(bn_writer->Close()); |
| 86 | |
| 87 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
| 88 | GenerateParcelSource(*source_writer, types, defined_type, options); |
| 89 | CHECK(source_writer->Close()); |
| 90 | } |
| 91 | |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 92 | void GenerateNdkParcelDeclaration(const std::string& filename, const IoDelegate& io_delegate) { |
| 93 | CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename); |
| 94 | *code_writer |
| 95 | << "// This file is intentionally left blank as placeholder for parcel declaration.\n"; |
| 96 | CHECK(code_writer->Close()); |
| 97 | } |
| 98 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 99 | void GenerateNdkEnumDeclaration(const string& output_file, const Options& options, |
| 100 | const AidlTypenames& types, const AidlEnumDeclaration& defined_type, |
| 101 | const IoDelegate& io_delegate) { |
| 102 | const string header_path = |
| 103 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::RAW); |
| 104 | unique_ptr<CodeWriter> header_writer(io_delegate.GetCodeWriter(header_path)); |
| 105 | GenerateEnumHeader(*header_writer, types, defined_type, options); |
| 106 | CHECK(header_writer->Close()); |
| 107 | |
| 108 | const string bp_header = |
| 109 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::CLIENT); |
| 110 | unique_ptr<CodeWriter> bp_writer(io_delegate.GetCodeWriter(bp_header)); |
| 111 | *bp_writer << "#error TODO(b/111362593) enums do not have bp classes\n"; |
| 112 | CHECK(bp_writer->Close()); |
| 113 | |
| 114 | const string bn_header = |
| 115 | options.OutputHeaderDir() + NdkHeaderFile(defined_type, ClassNames::SERVER); |
| 116 | unique_ptr<CodeWriter> bn_writer(io_delegate.GetCodeWriter(bn_header)); |
| 117 | *bn_writer << "#error TODO(b/111362593) enums do not have bn classes\n"; |
| 118 | CHECK(bn_writer->Close()); |
| 119 | |
| 120 | unique_ptr<CodeWriter> source_writer = io_delegate.GetCodeWriter(output_file); |
| 121 | *source_writer |
| 122 | << "// This file is intentionally left blank as placeholder for enum declaration.\n"; |
| 123 | CHECK(source_writer->Close()); |
| 124 | } |
| 125 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 126 | void GenerateNdk(const string& output_file, const Options& options, const AidlTypenames& types, |
| 127 | const AidlDefinedType& defined_type, const IoDelegate& io_delegate) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 128 | if (const AidlStructuredParcelable* parcelable = defined_type.AsStructuredParcelable(); |
| 129 | parcelable != nullptr) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 130 | GenerateNdkParcel(output_file, options, types, *parcelable, io_delegate); |
| 131 | return; |
| 132 | } |
| 133 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 134 | if (const AidlParcelable* parcelable_decl = defined_type.AsParcelable(); |
| 135 | parcelable_decl != nullptr) { |
Steven Moreland | 2a9a7d6 | 2019-02-05 16:11:54 -0800 | [diff] [blame] | 136 | GenerateNdkParcelDeclaration(output_file, io_delegate); |
| 137 | return; |
| 138 | } |
| 139 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 140 | if (const AidlEnumDeclaration* enum_decl = defined_type.AsEnumDeclaration(); |
| 141 | enum_decl != nullptr) { |
| 142 | GenerateNdkEnumDeclaration(output_file, options, types, *enum_decl, io_delegate); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | if (const AidlInterface* interface = defined_type.AsInterface(); interface != nullptr) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 147 | GenerateNdkInterface(output_file, options, types, *interface, io_delegate); |
| 148 | return; |
| 149 | } |
| 150 | |
Daniel Norman | 85aed54 | 2019-08-21 12:01:14 -0700 | [diff] [blame] | 151 | CHECK(false) << "Unrecognized type sent for NDK cpp generation."; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 152 | } |
| 153 | namespace internals { |
| 154 | |
| 155 | void EnterNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) { |
| 156 | out << "namespace aidl {\n"; |
| 157 | cpp::EnterNamespace(out, defined_type); |
| 158 | } |
| 159 | void LeaveNdkNamespace(CodeWriter& out, const AidlDefinedType& defined_type) { |
| 160 | cpp::LeaveNamespace(out, defined_type); |
| 161 | out << "} // namespace aidl\n"; |
| 162 | } |
| 163 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 164 | static void StatusCheckGoto(CodeWriter& out) { |
| 165 | out << "if (_aidl_ret_status != STATUS_OK) goto _aidl_error;\n\n"; |
| 166 | } |
| 167 | static void StatusCheckBreak(CodeWriter& out) { |
| 168 | out << "if (_aidl_ret_status != STATUS_OK) break;\n\n"; |
| 169 | } |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 170 | static void StatusCheckReturn(CodeWriter& out) { |
| 171 | out << "if (_aidl_ret_status != STATUS_OK) return _aidl_ret_status;\n\n"; |
| 172 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 173 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 174 | static void GenerateHeaderIncludes(CodeWriter& out, const AidlTypenames& types, |
| 175 | const AidlDefinedType& defined_type) { |
Steven Moreland | 8769f93 | 2019-10-30 15:38:10 -0700 | [diff] [blame] | 176 | out << "#include <cstdint>\n"; |
| 177 | out << "#include <memory>\n"; |
| 178 | out << "#include <optional>\n"; |
| 179 | out << "#include <string>\n"; |
| 180 | out << "#include <vector>\n"; |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 181 | out << "#ifdef BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 182 | out << "#include <android/binder_stability.h>\n"; |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 183 | out << "#endif // BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 184 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 185 | types.IterateTypes([&](const AidlDefinedType& other_defined_type) { |
| 186 | if (&other_defined_type == &defined_type) return; |
| 187 | |
| 188 | if (other_defined_type.AsInterface() != nullptr) { |
| 189 | out << "#include <" |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 190 | << NdkHeaderFile(other_defined_type, ClassNames::RAW, false /*use_os_sep*/) << ">\n"; |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 191 | } else if (other_defined_type.AsStructuredParcelable() != nullptr) { |
| 192 | out << "#include <" |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 193 | << NdkHeaderFile(other_defined_type, ClassNames::RAW, false /*use_os_sep*/) << ">\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 194 | } else if (other_defined_type.AsParcelable() != nullptr) { |
| 195 | out << "#include \"" << other_defined_type.AsParcelable()->GetCppHeader() << "\"\n"; |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 196 | } else if (other_defined_type.AsEnumDeclaration() != nullptr) { |
| 197 | out << "#include <" |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 198 | << NdkHeaderFile(other_defined_type, ClassNames::RAW, false /*use_os_sep*/) << ">\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 199 | } else { |
| 200 | AIDL_FATAL(defined_type) << "Unrecognized type."; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 201 | } |
| 202 | }); |
| 203 | } |
| 204 | static void GenerateSourceIncludes(CodeWriter& out, const AidlTypenames& types, |
| 205 | const AidlDefinedType& /*defined_type*/) { |
Steven Moreland | 8769f93 | 2019-10-30 15:38:10 -0700 | [diff] [blame] | 206 | out << "#include <android/binder_parcel_utils.h>\n"; |
| 207 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 208 | types.IterateTypes([&](const AidlDefinedType& a_defined_type) { |
| 209 | if (a_defined_type.AsInterface() != nullptr) { |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 210 | out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::CLIENT, false /*use_os_sep*/) |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 211 | << ">\n"; |
Steven Moreland | 7c93337 | 2018-10-11 15:20:04 -0700 | [diff] [blame] | 212 | out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::SERVER, false /*use_os_sep*/) |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 213 | << ">\n"; |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 214 | out << "#include <" << NdkHeaderFile(a_defined_type, ClassNames::RAW, false /*use_os_sep*/) |
| 215 | << ">\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 216 | } |
| 217 | }); |
| 218 | } |
| 219 | |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 220 | static void GenerateConstantDeclarations(CodeWriter& out, const AidlInterface& interface) { |
| 221 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 222 | const AidlConstantValue& value = constant->GetValue(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 223 | CHECK(value.GetType() != AidlConstantValue::Type::UNARY && |
| 224 | value.GetType() != AidlConstantValue::Type::BINARY); |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 225 | if (value.GetType() == AidlConstantValue::Type::STRING) { |
| 226 | out << "static const char* " << constant->GetName() << ";\n"; |
| 227 | } |
| 228 | } |
| 229 | out << "\n"; |
| 230 | |
| 231 | bool hasIntegralConstant = false; |
| 232 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 233 | const AidlConstantValue& value = constant->GetValue(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 234 | CHECK(value.GetType() != AidlConstantValue::Type::UNARY && |
| 235 | value.GetType() != AidlConstantValue::Type::BINARY); |
| 236 | if (value.GetType() == AidlConstantValue::Type::BOOLEAN || |
| 237 | value.GetType() == AidlConstantValue::Type::INT8 || |
| 238 | value.GetType() == AidlConstantValue::Type::INT32) { |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 239 | hasIntegralConstant = true; |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (hasIntegralConstant) { |
| 245 | out << "enum : int32_t {\n"; |
| 246 | out.Indent(); |
| 247 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 248 | const AidlConstantValue& value = constant->GetValue(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 249 | if (value.GetType() == AidlConstantValue::Type::BOOLEAN || |
| 250 | value.GetType() == AidlConstantValue::Type::INT8 || |
| 251 | value.GetType() == AidlConstantValue::Type::INT32) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 252 | out << constant->GetName() << " = " << constant->ValueString(ConstantValueDecorator) |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 253 | << ",\n"; |
| 254 | } |
| 255 | } |
| 256 | out.Dedent(); |
| 257 | out << "};\n"; |
| 258 | } |
| 259 | } |
| 260 | static void GenerateConstantDefinitions(CodeWriter& out, const AidlInterface& interface) { |
| 261 | const std::string clazz = ClassName(interface, ClassNames::INTERFACE); |
| 262 | |
| 263 | for (const auto& constant : interface.GetConstantDeclarations()) { |
| 264 | const AidlConstantValue& value = constant->GetValue(); |
Will McVicker | d7d18df | 2019-09-12 13:40:50 -0700 | [diff] [blame] | 265 | CHECK(value.GetType() != AidlConstantValue::Type::UNARY && |
| 266 | value.GetType() != AidlConstantValue::Type::BINARY); |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 267 | if (value.GetType() == AidlConstantValue::Type::STRING) { |
| 268 | out << "const char* " << clazz << "::" << constant->GetName() << " = " |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 269 | << constant->ValueString(ConstantValueDecorator) << ";\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 274 | void GenerateSource(CodeWriter& out, const AidlTypenames& types, const AidlInterface& defined_type, |
| 275 | const Options& options) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 276 | GenerateSourceIncludes(out, types, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 277 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 278 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 279 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 280 | GenerateClassSource(out, types, defined_type, options); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 281 | GenerateClientSource(out, types, defined_type, options); |
| 282 | GenerateServerSource(out, types, defined_type, options); |
| 283 | GenerateInterfaceSource(out, types, defined_type, options); |
| 284 | LeaveNdkNamespace(out, defined_type); |
| 285 | } |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 286 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 287 | static std::string MethodId(const AidlMethod& m) { |
| 288 | return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)"; |
| 289 | } |
| 290 | |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 291 | static void GenerateClientMethodDefinition(CodeWriter& out, const AidlTypenames& types, |
| 292 | const AidlInterface& defined_type, |
| 293 | const AidlMethod& method, |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 294 | const Options& options) { |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 295 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
| 296 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 297 | out << NdkMethodDecl(types, method, clazz) << " {\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 298 | out.Indent(); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 299 | out << "binder_status_t _aidl_ret_status = STATUS_OK;\n"; |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 300 | out << "::ndk::ScopedAStatus _aidl_status;\n"; |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 301 | |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 302 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 303 | out << "const std::lock_guard<std::mutex> lock(" << kCachedHashMutex << ");\n"; |
| 304 | out << "if (" << kCachedHash << " != \"-1\") {\n"; |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 305 | out.Indent(); |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 306 | out << "*_aidl_return = " << kCachedHash << ";\n" |
| 307 | << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n" |
| 308 | << "return _aidl_status;\n"; |
| 309 | out.Dedent(); |
| 310 | out << "}\n"; |
| 311 | } else if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 312 | out << "if (" << kCachedVersion << " != -1) {\n"; |
| 313 | out.Indent(); |
| 314 | out << "*_aidl_return = " << kCachedVersion << ";\n" |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 315 | << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n" |
| 316 | << "return _aidl_status;\n"; |
| 317 | out.Dedent(); |
| 318 | out << "}\n"; |
| 319 | } |
| 320 | out << "::ndk::ScopedAParcel _aidl_in;\n"; |
| 321 | out << "::ndk::ScopedAParcel _aidl_out;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 322 | out << "\n"; |
| 323 | |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 324 | if (options.GenLog()) { |
| 325 | out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::CLIENT), method, |
| 326 | false /* isServer */, true /* isNdk */); |
| 327 | } |
| 328 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 329 | out << "_aidl_ret_status = AIBinder_prepareTransaction(asBinder().get(), _aidl_in.getR());\n"; |
| 330 | StatusCheckGoto(out); |
| 331 | |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 332 | for (const auto& arg : method.GetArguments()) { |
| 333 | const std::string var_name = cpp::BuildVarName(*arg); |
| 334 | |
| 335 | if (arg->IsIn()) { |
| 336 | out << "_aidl_ret_status = "; |
| 337 | const std::string prefix = (arg->IsOut() ? "*" : ""); |
| 338 | WriteToParcelFor({out, types, arg->GetType(), "_aidl_in.get()", prefix + var_name}); |
| 339 | out << ";\n"; |
| 340 | StatusCheckGoto(out); |
| 341 | } else if (arg->IsOut() && arg->GetType().IsArray()) { |
| 342 | out << "_aidl_ret_status = ::ndk::AParcel_writeVectorSize(_aidl_in.get(), *" << var_name |
| 343 | << ");\n"; |
| 344 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 345 | } |
| 346 | out << "_aidl_ret_status = AIBinder_transact(\n"; |
| 347 | out.Indent(); |
| 348 | out << "asBinder().get(),\n"; |
| 349 | out << MethodId(method) << ",\n"; |
| 350 | out << "_aidl_in.getR(),\n"; |
| 351 | out << "_aidl_out.getR(),\n"; |
Steven Moreland | b72a1e7 | 2019-10-15 11:23:26 -0700 | [diff] [blame] | 352 | out << (method.IsOneway() ? "FLAG_ONEWAY" : "0") << "\n"; |
| 353 | out << "#ifdef BINDER_STABILITY_SUPPORT\n"; |
| 354 | out << "| FLAG_PRIVATE_LOCAL\n"; |
| 355 | out << "#endif // BINDER_STABILITY_SUPPORT\n"; |
| 356 | out << ");\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 357 | out.Dedent(); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 358 | |
| 359 | // If the method is not implmented in the server side but the client has |
| 360 | // provided the default implementation, call it instead of failing hard. |
| 361 | const std::string iface = ClassName(defined_type, ClassNames::INTERFACE); |
| 362 | out << "if (_aidl_ret_status == STATUS_UNKNOWN_TRANSACTION && "; |
| 363 | out << iface << "::getDefaultImpl()) {\n"; |
| 364 | out.Indent(); |
| 365 | out << "return " << iface << "::getDefaultImpl()->" << method.GetName() << "("; |
| 366 | out << NdkArgList(types, method, FormatArgNameOnly) << ");\n"; |
| 367 | out.Dedent(); |
| 368 | out << "}\n"; |
| 369 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 370 | StatusCheckGoto(out); |
| 371 | |
| 372 | if (!method.IsOneway()) { |
| 373 | out << "_aidl_ret_status = AParcel_readStatusHeader(_aidl_out.get(), _aidl_status.getR());\n"; |
| 374 | StatusCheckGoto(out); |
| 375 | |
| 376 | out << "if (!AStatus_isOk(_aidl_status.get())) return _aidl_status;\n\n"; |
| 377 | } |
| 378 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 379 | if (method.GetType().GetName() != "void") { |
| 380 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 381 | ReadFromParcelFor({out, types, method.GetType(), "_aidl_out.get()", "_aidl_return"}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 382 | out << ";\n"; |
| 383 | StatusCheckGoto(out); |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 384 | if (method.GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 385 | out << kCachedHash << " = *_aidl_return;\n"; |
| 386 | } else if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 387 | out << kCachedVersion << " = *_aidl_return;\n"; |
Jeongik Cha | c997292 | 2019-02-11 12:41:16 +0900 | [diff] [blame] | 388 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 389 | } |
Steven Moreland | 7c78d5d | 2018-10-15 14:21:11 -0700 | [diff] [blame] | 390 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 391 | out << "_aidl_ret_status = "; |
| 392 | ReadFromParcelFor({out, types, arg->GetType(), "_aidl_out.get()", cpp::BuildVarName(*arg)}); |
| 393 | out << ";\n"; |
| 394 | StatusCheckGoto(out); |
| 395 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 396 | |
| 397 | out << "_aidl_error:\n"; |
| 398 | out << "_aidl_status.set(AStatus_fromStatus(_aidl_ret_status));\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 399 | if (options.GenLog()) { |
| 400 | out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::CLIENT), defined_type, |
| 401 | method, "_aidl_status", "_aidl_return", false /* isServer */, |
| 402 | true /* isNdk */); |
| 403 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 404 | out << "return _aidl_status;\n"; |
| 405 | out.Dedent(); |
| 406 | out << "}\n"; |
| 407 | } |
| 408 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 409 | static void GenerateServerCaseDefinition(CodeWriter& out, const AidlTypenames& types, |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 410 | const AidlInterface& defined_type, |
| 411 | const AidlMethod& method, const Options& options) { |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 412 | out << "case " << MethodId(method) << ": {\n"; |
| 413 | out.Indent(); |
| 414 | for (const auto& arg : method.GetArguments()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 415 | out << NdkNameOf(types, arg->GetType(), StorageMode::STACK) << " " << cpp::BuildVarName(*arg) |
| 416 | << ";\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 417 | } |
| 418 | if (method.GetType().GetName() != "void") { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 419 | out << NdkNameOf(types, method.GetType(), StorageMode::STACK) << " _aidl_return;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 420 | } |
| 421 | out << "\n"; |
| 422 | |
Steven Moreland | eb38ee7 | 2018-10-15 14:20:04 -0700 | [diff] [blame] | 423 | for (const auto& arg : method.GetArguments()) { |
| 424 | const std::string var_name = cpp::BuildVarName(*arg); |
| 425 | |
| 426 | if (arg->IsIn()) { |
| 427 | out << "_aidl_ret_status = "; |
| 428 | ReadFromParcelFor({out, types, arg->GetType(), "_aidl_in", "&" + var_name}); |
| 429 | out << ";\n"; |
| 430 | StatusCheckBreak(out); |
| 431 | } else if (arg->IsOut() && arg->GetType().IsArray()) { |
| 432 | out << "_aidl_ret_status = ::ndk::AParcel_resizeVector(_aidl_in, &" << var_name << ");\n"; |
| 433 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 434 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 435 | if (options.GenLog()) { |
| 436 | out << cpp::GenLogBeforeExecute(ClassName(defined_type, ClassNames::SERVER), method, |
| 437 | true /* isServer */, true /* isNdk */); |
| 438 | } |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 439 | out << "::ndk::ScopedAStatus _aidl_status = _aidl_impl->" << method.GetName() << "(" |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 440 | << NdkArgList(types, method, FormatArgForCall) << ");\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 441 | |
Jeongik Cha | eaf978e | 2019-05-04 00:32:35 +0900 | [diff] [blame] | 442 | if (options.GenLog()) { |
| 443 | out << cpp::GenLogAfterExecute(ClassName(defined_type, ClassNames::SERVER), defined_type, |
| 444 | method, "_aidl_status", "_aidl_return", true /* isServer */, |
| 445 | true /* isNdk */); |
| 446 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 447 | if (method.IsOneway()) { |
| 448 | // For a oneway transaction, the kernel will have already returned a result. This is for the |
| 449 | // in-process case when a oneway transaction is parceled/unparceled in the same process. |
| 450 | out << "_aidl_ret_status = STATUS_OK;\n"; |
| 451 | } else { |
| 452 | out << "_aidl_ret_status = AParcel_writeStatusHeader(_aidl_out, _aidl_status.get());\n"; |
| 453 | StatusCheckBreak(out); |
| 454 | |
| 455 | out << "if (!AStatus_isOk(_aidl_status.get())) break;\n\n"; |
| 456 | |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 457 | if (method.GetType().GetName() != "void") { |
| 458 | out << "_aidl_ret_status = "; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 459 | WriteToParcelFor({out, types, method.GetType(), "_aidl_out", "_aidl_return"}); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 460 | out << ";\n"; |
| 461 | StatusCheckBreak(out); |
| 462 | } |
Steven Moreland | 7c78d5d | 2018-10-15 14:21:11 -0700 | [diff] [blame] | 463 | for (const AidlArgument* arg : method.GetOutArguments()) { |
| 464 | out << "_aidl_ret_status = "; |
| 465 | WriteToParcelFor({out, types, arg->GetType(), "_aidl_out", cpp::BuildVarName(*arg)}); |
| 466 | out << ";\n"; |
| 467 | StatusCheckBreak(out); |
| 468 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 469 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 470 | out << "break;\n"; |
| 471 | out.Dedent(); |
| 472 | out << "}\n"; |
| 473 | } |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 474 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 475 | void GenerateClassSource(CodeWriter& out, const AidlTypenames& types, |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 476 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 477 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
| 478 | const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 479 | |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 480 | out << "static binder_status_t " |
| 481 | << "_aidl_onTransact" |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 482 | << "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, " |
| 483 | "AParcel* _aidl_out) {\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 484 | out.Indent(); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 485 | out << "(void)_aidl_in;\n"; |
| 486 | out << "(void)_aidl_out;\n"; |
| 487 | out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n"; |
| 488 | if (!defined_type.GetMethods().empty()) { |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 489 | // we know this cast is valid because this method is only called by the ICInterface |
| 490 | // AIBinder_Class object which is associated with this class. |
| 491 | out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz |
| 492 | << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 493 | out << "switch (_aidl_code) {\n"; |
| 494 | out.Indent(); |
| 495 | for (const auto& method : defined_type.GetMethods()) { |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 496 | GenerateServerCaseDefinition(out, types, defined_type, *method, options); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 497 | } |
| 498 | out.Dedent(); |
| 499 | out << "}\n"; |
| 500 | } else { |
| 501 | out << "(void)_aidl_binder;\n"; |
| 502 | out << "(void)_aidl_code;\n"; |
| 503 | } |
| 504 | out << "return _aidl_ret_status;\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 505 | out.Dedent(); |
Steven Moreland | 73d6624 | 2019-09-26 16:03:54 -0700 | [diff] [blame] | 506 | out << "}\n\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 507 | |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 508 | out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz |
| 509 | << "::" << kDescriptor << ", _aidl_onTransact);\n\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 510 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 511 | void GenerateClientSource(CodeWriter& out, const AidlTypenames& types, |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 512 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 513 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 514 | |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 515 | out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 516 | out << clazz << "::~" << clazz << "() {}\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 517 | if (options.GenLog()) { |
| 518 | out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n"; |
| 519 | } |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 520 | out << "\n"; |
| 521 | for (const auto& method : defined_type.GetMethods()) { |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 522 | GenerateClientMethodDefinition(out, types, defined_type, *method, options); |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 523 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 524 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 525 | void GenerateServerSource(CodeWriter& out, const AidlTypenames& types, |
| 526 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 527 | const std::string clazz = ClassName(defined_type, ClassNames::SERVER); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 528 | const std::string iface = ClassName(defined_type, ClassNames::INTERFACE); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 529 | |
| 530 | out << "// Source for " << clazz << "\n"; |
| 531 | out << clazz << "::" << clazz << "() {}\n"; |
| 532 | out << clazz << "::~" << clazz << "() {}\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 533 | if (options.GenLog()) { |
| 534 | out << "std::function<void(const Json::Value&)> " << clazz << "::logFunc;\n"; |
| 535 | } |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 536 | out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 537 | out.Indent(); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 538 | out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 539 | |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 540 | out << "#ifdef BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 541 | if (defined_type.IsVintfStability()) { |
| 542 | out << "AIBinder_markVintfStability(binder);\n"; |
| 543 | } else { |
| 544 | out << "AIBinder_markCompilationUnitStability(binder);\n"; |
| 545 | } |
Steven Moreland | 88378b1 | 2019-10-11 13:13:24 -0700 | [diff] [blame] | 546 | out << "#endif // BINDER_STABILITY_SUPPORT\n"; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 547 | |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 548 | out << "return ::ndk::SpAIBinder(binder);\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 549 | out.Dedent(); |
| 550 | out << "}\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 551 | |
| 552 | // Implement the meta methods |
| 553 | for (const auto& method : defined_type.GetMethods()) { |
| 554 | if (method->IsUserDefined()) { |
| 555 | continue; |
| 556 | } |
| 557 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 558 | out << NdkMethodDecl(types, *method, clazz) << " {\n"; |
| 559 | out.Indent(); |
| 560 | out << "*_aidl_return = " << iface << "::" << kVersion << ";\n"; |
| 561 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 562 | out.Dedent(); |
| 563 | out << "}\n"; |
| 564 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 565 | if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 566 | out << NdkMethodDecl(types, *method, clazz) << " {\n"; |
| 567 | out.Indent(); |
| 568 | out << "*_aidl_return = " << iface << "::" << kHash << ";\n"; |
| 569 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 570 | out.Dedent(); |
| 571 | out << "}\n"; |
| 572 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 573 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 574 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 575 | void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types, |
| 576 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 577 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 578 | const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 579 | |
| 580 | out << "// Source for " << clazz << "\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 581 | out << "const char* " << clazz << "::" << kDescriptor << " = \"" |
| 582 | << defined_type.GetCanonicalName() << "\";\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 583 | out << clazz << "::" << clazz << "() {}\n"; |
| 584 | out << clazz << "::~" << clazz << "() {}\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 585 | out << "\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 586 | GenerateConstantDefinitions(out, defined_type); |
| 587 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 588 | |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 589 | out << "std::shared_ptr<" << clazz << "> " << clazz |
| 590 | << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n"; |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 591 | out.Indent(); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 592 | out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n"; |
| 593 | out << "std::shared_ptr<::ndk::ICInterface> interface = " |
| 594 | "::ndk::ICInterface::asInterface(binder.get());\n"; |
| 595 | out << "if (interface) {\n"; |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 596 | out.Indent(); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 597 | out << "return std::static_pointer_cast<" << clazz << ">(interface);\n"; |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 598 | out.Dedent(); |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 599 | out << "}\n"; |
Steven Moreland | c09d34c | 2020-02-13 09:42:49 -0800 | [diff] [blame] | 600 | out << "return ::ndk::SharedRefBase::make<" << bp_clazz << ">(binder);\n"; |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 601 | out.Dedent(); |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 602 | out << "}\n\n"; |
| 603 | |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 604 | out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<" |
| 605 | << clazz << ">& instance) {\n"; |
| 606 | out.Indent(); |
| 607 | out << "return AParcel_writeStrongBinder(parcel, instance ? instance->asBinder().get() : " |
| 608 | "nullptr);\n"; |
| 609 | out.Dedent(); |
| 610 | out << "}\n"; |
| 611 | |
| 612 | out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel, std::shared_ptr<" |
| 613 | << clazz << ">* instance) {\n"; |
| 614 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 615 | out << "::ndk::SpAIBinder binder;\n"; |
Steven Moreland | 055d879 | 2018-11-14 12:48:42 -0800 | [diff] [blame] | 616 | out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 617 | out << "if (status != STATUS_OK) return status;\n"; |
Steven Moreland | 3c316ed | 2019-01-17 09:39:37 -0800 | [diff] [blame] | 618 | out << "*instance = " << clazz << "::fromBinder(binder);\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 619 | out << "return STATUS_OK;\n"; |
| 620 | out.Dedent(); |
| 621 | out << "}\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 622 | |
| 623 | // defintion for static member setDefaultImpl |
| 624 | out << "bool " << clazz << "::setDefaultImpl(std::shared_ptr<" << clazz << "> impl) {\n"; |
| 625 | out.Indent(); |
Jooyung Han | d4d6534 | 2020-05-13 17:17:07 +0900 | [diff] [blame] | 626 | out << "// Only one user of this interface can use this function\n"; |
| 627 | out << "// at a time. This is a heuristic to detect if two different\n"; |
| 628 | out << "// users in the same process use this function.\n"; |
| 629 | out << "assert(!" << clazz << "::default_impl);\n"; |
| 630 | out << "if (impl) {\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 631 | out.Indent(); |
| 632 | out << clazz << "::default_impl = impl;\n"; |
| 633 | out << "return true;\n"; |
| 634 | out.Dedent(); |
| 635 | out << "}\n"; |
| 636 | out << "return false;\n"; |
| 637 | out.Dedent(); |
| 638 | out << "}\n"; |
| 639 | |
| 640 | // definition for static member getDefaultImpl |
| 641 | out << "const std::shared_ptr<" << clazz << ">& " << clazz << "::getDefaultImpl() {\n"; |
| 642 | out.Indent(); |
| 643 | out << "return " << clazz << "::default_impl;\n"; |
| 644 | out.Dedent(); |
| 645 | out << "}\n"; |
| 646 | |
| 647 | // definition for the static field default_impl |
| 648 | out << "std::shared_ptr<" << clazz << "> " << clazz << "::default_impl = nullptr;\n"; |
| 649 | |
| 650 | // default implementation for the <Name>Default class members |
| 651 | const std::string defaultClazz = clazz + "Default"; |
| 652 | for (const auto& method : defined_type.GetMethods()) { |
| 653 | if (method->IsUserDefined()) { |
| 654 | out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "(" |
| 655 | << NdkArgList(types, *method, FormatArgNameUnused) << ") {\n"; |
| 656 | out.Indent(); |
| 657 | out << "::ndk::ScopedAStatus _aidl_status;\n"; |
| 658 | out << "_aidl_status.set(AStatus_fromStatus(STATUS_UNKNOWN_TRANSACTION));\n"; |
| 659 | out << "return _aidl_status;\n"; |
| 660 | out.Dedent(); |
| 661 | out << "}\n"; |
| 662 | } else { |
| 663 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 664 | out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "(" |
| 665 | << "int32_t* _aidl_return) {\n"; |
| 666 | out.Indent(); |
| 667 | out << "*_aidl_return = 0;\n"; |
| 668 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 669 | out.Dedent(); |
| 670 | out << "}\n"; |
| 671 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 672 | if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 673 | out << "::ndk::ScopedAStatus " << defaultClazz << "::" << method->GetName() << "(" |
| 674 | << "std::string* _aidl_return) {\n"; |
| 675 | out.Indent(); |
| 676 | out << "*_aidl_return = \"\";\n"; |
| 677 | out << "return ::ndk::ScopedAStatus(AStatus_newOk());\n"; |
| 678 | out.Dedent(); |
| 679 | out << "}\n"; |
| 680 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
| 684 | out << "::ndk::SpAIBinder " << defaultClazz << "::asBinder() {\n"; |
| 685 | out.Indent(); |
| 686 | out << "return ::ndk::SpAIBinder();\n"; |
| 687 | out.Dedent(); |
| 688 | out << "}\n"; |
| 689 | |
| 690 | out << "bool " << defaultClazz << "::isRemote() {\n"; |
| 691 | out.Indent(); |
| 692 | out << "return false;\n"; |
| 693 | out.Dedent(); |
| 694 | out << "}\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 695 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 696 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 697 | void GenerateClientHeader(CodeWriter& out, const AidlTypenames& types, |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 698 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 699 | const std::string clazz = ClassName(defined_type, ClassNames::CLIENT); |
| 700 | |
| 701 | out << "#pragma once\n\n"; |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 702 | out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/) |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 703 | << "\"\n"; |
| 704 | out << "\n"; |
| 705 | out << "#include <android/binder_ibinder.h>\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 706 | if (options.GenLog()) { |
| 707 | out << "#include <json/value.h>\n"; |
| 708 | out << "#include <functional>\n"; |
| 709 | out << "#include <chrono>\n"; |
| 710 | out << "#include <sstream>\n"; |
| 711 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 712 | out << "\n"; |
| 713 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 714 | out << "class " << clazz << " : public ::ndk::BpCInterface<" |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 715 | << ClassName(defined_type, ClassNames::INTERFACE) << "> {\n"; |
| 716 | out << "public:\n"; |
| 717 | out.Indent(); |
Steven Moreland | 15b3ba7 | 2019-03-06 13:17:08 -0800 | [diff] [blame] | 718 | out << clazz << "(const ::ndk::SpAIBinder& binder);\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 719 | out << "virtual ~" << clazz << "();\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 720 | out << "\n"; |
| 721 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 722 | out << NdkMethodDecl(types, *method) << " override;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 723 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 724 | |
| 725 | if (options.Version() > 0) { |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 726 | out << "int32_t " << kCachedVersion << " = -1;\n"; |
| 727 | } |
| 728 | |
| 729 | if (!options.Hash().empty()) { |
| 730 | out << "std::string " << kCachedHash << " = \"-1\";\n"; |
| 731 | out << "std::mutex " << kCachedHashMutex << ";\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 732 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 733 | if (options.GenLog()) { |
| 734 | out << "static std::function<void(const Json::Value&)> logFunc;\n"; |
| 735 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 736 | out.Dedent(); |
| 737 | out << "};\n"; |
| 738 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 739 | } |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 740 | void GenerateServerHeader(CodeWriter& out, const AidlTypenames& types, |
| 741 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 742 | const std::string clazz = ClassName(defined_type, ClassNames::SERVER); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 743 | const std::string iface = ClassName(defined_type, ClassNames::INTERFACE); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 744 | |
| 745 | out << "#pragma once\n\n"; |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 746 | out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/) |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 747 | << "\"\n"; |
| 748 | out << "\n"; |
| 749 | out << "#include <android/binder_ibinder.h>\n"; |
| 750 | out << "\n"; |
| 751 | EnterNdkNamespace(out, defined_type); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 752 | out << "class " << clazz << " : public ::ndk::BnCInterface<" << iface << "> {\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 753 | out << "public:\n"; |
| 754 | out.Indent(); |
| 755 | out << clazz << "();\n"; |
| 756 | out << "virtual ~" << clazz << "();\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 757 | |
| 758 | // Declare the meta methods |
| 759 | for (const auto& method : defined_type.GetMethods()) { |
| 760 | if (method->IsUserDefined()) { |
| 761 | continue; |
| 762 | } |
| 763 | if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 764 | out << NdkMethodDecl(types, *method) << " final override;\n"; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 765 | } else if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 766 | out << NdkMethodDecl(types, *method) << " final override;\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 767 | } else { |
| 768 | AIDL_FATAL(defined_type) << "Meta method '" << method->GetName() << "' is unimplemented."; |
| 769 | } |
| 770 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 771 | if (options.GenLog()) { |
| 772 | out << "static std::function<void(const Json::Value&)> logFunc;\n"; |
| 773 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 774 | out.Dedent(); |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 775 | out << "protected:\n"; |
| 776 | out.Indent(); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 777 | out << "::ndk::SpAIBinder createBinder() override;\n"; |
Steven Moreland | 0cf3f08 | 2018-09-20 19:09:46 -0700 | [diff] [blame] | 778 | out.Dedent(); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 779 | out << "private:\n"; |
| 780 | out.Indent(); |
| 781 | out.Dedent(); |
| 782 | out << "};\n"; |
| 783 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 784 | } |
| 785 | void GenerateInterfaceHeader(CodeWriter& out, const AidlTypenames& types, |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 786 | const AidlInterface& defined_type, const Options& options) { |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 787 | const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE); |
| 788 | |
| 789 | out << "#pragma once\n\n"; |
| 790 | out << "#include <android/binder_interface_utils.h>\n"; |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 791 | if (options.GenLog()) { |
| 792 | out << "#include <json/value.h>\n"; |
| 793 | out << "#include <functional>\n"; |
| 794 | out << "#include <chrono>\n"; |
| 795 | out << "#include <sstream>\n"; |
| 796 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 797 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 798 | |
| 799 | GenerateHeaderIncludes(out, types, defined_type); |
| 800 | out << "\n"; |
| 801 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 802 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | 6340453 | 2018-10-08 14:31:00 -0700 | [diff] [blame] | 803 | out << "class " << clazz << " : public ::ndk::ICInterface {\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 804 | out << "public:\n"; |
| 805 | out.Indent(); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 806 | out << "static const char* " << kDescriptor << ";\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 807 | out << clazz << "();\n"; |
| 808 | out << "virtual ~" << clazz << "();\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 809 | out << "\n"; |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 810 | GenerateConstantDeclarations(out, defined_type); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 811 | if (options.Version() > 0) { |
| 812 | out << "static const int32_t " << kVersion << " = " << std::to_string(options.Version()) |
| 813 | << ";\n"; |
| 814 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 815 | if (!options.Hash().empty()) { |
| 816 | out << "static inline const std::string " << kHash << " = \"" << options.Hash() << "\";\n"; |
| 817 | } |
Steven Moreland | ac8fe7f | 2018-10-04 09:58:31 -0700 | [diff] [blame] | 818 | out << "\n"; |
Steven Moreland | 4cb2e6d | 2019-01-18 14:03:43 -0800 | [diff] [blame] | 819 | out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 820 | out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz |
| 821 | << ">& instance);"; |
Jiyong Park | 1b88cce | 2018-11-19 19:53:44 +0900 | [diff] [blame] | 822 | out << "\n"; |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 823 | out << "static binder_status_t readFromParcel(const AParcel* parcel, std::shared_ptr<" << clazz |
| 824 | << ">* instance);"; |
| 825 | out << "\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 826 | out << "static bool setDefaultImpl(std::shared_ptr<" << clazz << "> impl);"; |
| 827 | out << "\n"; |
| 828 | out << "static const std::shared_ptr<" << clazz << ">& getDefaultImpl();"; |
| 829 | out << "\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 830 | for (const auto& method : defined_type.GetMethods()) { |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 831 | out << "virtual " << NdkMethodDecl(types, *method) << " = 0;\n"; |
Steven Moreland | aada342 | 2018-09-20 15:55:33 -0700 | [diff] [blame] | 832 | } |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 833 | out.Dedent(); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 834 | out << "private:\n"; |
| 835 | out.Indent(); |
| 836 | out << "static std::shared_ptr<" << clazz << "> default_impl;\n"; |
| 837 | out.Dedent(); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 838 | out << "};\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 839 | |
| 840 | const std::string defaultClazz = clazz + "Default"; |
| 841 | |
| 842 | out << "class " << defaultClazz << " : public " << clazz << " {\n"; |
| 843 | out << "public:\n"; |
| 844 | out.Indent(); |
| 845 | for (const auto& method : defined_type.GetMethods()) { |
| 846 | if (method->IsUserDefined()) { |
| 847 | out << NdkMethodDecl(types, *method) << " override;\n"; |
| 848 | } else if (method->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 849 | out << NdkMethodDecl(types, *method) << " override;\n"; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 850 | } else if (method->GetName() == kGetInterfaceHash && !options.Hash().empty()) { |
| 851 | out << NdkMethodDecl(types, *method) << " override;\n"; |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | out << "::ndk::SpAIBinder asBinder() override;\n"; |
| 855 | out << "bool isRemote() override;\n"; |
| 856 | out.Dedent(); |
| 857 | out << "};\n"; |
| 858 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 859 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 860 | } |
| 861 | void GenerateParcelHeader(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 862 | const AidlStructuredParcelable& defined_type, |
| 863 | const Options& /*options*/) { |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 864 | const std::string clazz = ClassName(defined_type, ClassNames::RAW); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 865 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 866 | out << "#pragma once\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 867 | out << "#include <android/binder_interface_utils.h>\n"; |
| 868 | out << "\n"; |
| 869 | |
| 870 | GenerateHeaderIncludes(out, types, defined_type); |
Steven Moreland | 2bea13b | 2018-10-03 15:12:33 -0700 | [diff] [blame] | 871 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 872 | EnterNdkNamespace(out, defined_type); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 873 | out << "class " << clazz << " {\n"; |
| 874 | out << "public:\n"; |
| 875 | out.Indent(); |
| 876 | out << "static const char* descriptor;\n"; |
| 877 | out << "\n"; |
| 878 | for (const auto& variable : defined_type.GetFields()) { |
| 879 | out << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << " " << variable->GetName(); |
| 880 | if (variable->GetDefaultValue()) { |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 881 | out << " = " << variable->ValueString(ConstantValueDecorator); |
Jeongik Cha | db561c9 | 2021-09-25 01:19:56 +0900 | [diff] [blame] | 882 | } else if (auto type = types.TryGetDefinedType(variable->GetType().GetName()); type) { |
| 883 | if (auto enum_type = type->AsEnumDeclaration(); enum_type) { |
| 884 | if (!variable->GetType().IsArray()) { |
| 885 | // if an enum doesn't have explicit default value, do zero-initialization |
| 886 | out << " = " << NdkNameOf(types, variable->GetType(), StorageMode::STACK) << "(0)"; |
| 887 | } |
| 888 | } |
Jooyung Han | a2dafff | 2022-03-25 22:03:56 +0900 | [diff] [blame] | 889 | } else if (AidlTypenames::IsPrimitiveTypename(variable->GetType().GetName()) && |
| 890 | !variable->GetType().IsArray()) { |
| 891 | out << " = {}"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 892 | } |
| 893 | out << ";\n"; |
| 894 | } |
| 895 | out << "\n"; |
| 896 | out << "binder_status_t readFromParcel(const AParcel* parcel);\n"; |
| 897 | out << "binder_status_t writeToParcel(AParcel* parcel) const;\n"; |
| 898 | out.Dedent(); |
| 899 | out << "};\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 900 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 901 | } |
| 902 | void GenerateParcelSource(CodeWriter& out, const AidlTypenames& types, |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 903 | const AidlStructuredParcelable& defined_type, |
| 904 | const Options& /*options*/) { |
Steven Moreland | b8df37d | 2019-11-21 12:33:24 -0800 | [diff] [blame] | 905 | const std::string clazz = ClassName(defined_type, ClassNames::RAW); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 906 | |
Jiyong Park | 5b7e532 | 2019-04-03 20:05:01 +0900 | [diff] [blame] | 907 | out << "#include \"" << NdkHeaderFile(defined_type, ClassNames::RAW, false /*use_os_sep*/) |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 908 | << "\"\n"; |
| 909 | out << "\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 910 | GenerateSourceIncludes(out, types, defined_type); |
| 911 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 912 | EnterNdkNamespace(out, defined_type); |
Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 913 | out << "const char* " << clazz << "::" << kDescriptor << " = \"" |
| 914 | << defined_type.GetCanonicalName() << "\";\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 915 | out << "\n"; |
| 916 | |
| 917 | out << "binder_status_t " << clazz << "::readFromParcel(const AParcel* parcel) {\n"; |
| 918 | out.Indent(); |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 919 | out << "int32_t _aidl_parcelable_size;\n"; |
Steven Moreland | 4348f9a | 2019-12-16 16:33:01 -0800 | [diff] [blame] | 920 | out << "int32_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n"; |
| 921 | out << "binder_status_t _aidl_ret_status = AParcel_readInt32(parcel, &_aidl_parcelable_size);\n"; |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 922 | out << "if (_aidl_parcelable_size < 0) return STATUS_BAD_VALUE;\n"; |
| 923 | StatusCheckReturn(out); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 924 | |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 925 | for (const auto& variable : defined_type.GetFields()) { |
| 926 | out << "_aidl_ret_status = "; |
| 927 | ReadFromParcelFor({out, types, variable->GetType(), "parcel", "&" + variable->GetName()}); |
| 928 | out << ";\n"; |
| 929 | StatusCheckReturn(out); |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 930 | out << "if (AParcel_getDataPosition(parcel) - _aidl_start_pos >= _aidl_parcelable_size) {\n" |
| 931 | << " AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n" |
| 932 | << " return _aidl_ret_status;\n" |
| 933 | << "}\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 934 | } |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 935 | out << "AParcel_setDataPosition(parcel, _aidl_start_pos + _aidl_parcelable_size);\n" |
| 936 | << "return _aidl_ret_status;\n"; |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 937 | out.Dedent(); |
| 938 | out << "}\n"; |
| 939 | |
| 940 | out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel) const {\n"; |
| 941 | out.Indent(); |
| 942 | out << "binder_status_t _aidl_ret_status;\n"; |
| 943 | |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 944 | out << "size_t _aidl_start_pos = AParcel_getDataPosition(parcel);\n"; |
| 945 | out << "_aidl_ret_status = AParcel_writeInt32(parcel, 0);\n"; |
| 946 | StatusCheckReturn(out); |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 947 | |
| 948 | for (const auto& variable : defined_type.GetFields()) { |
| 949 | out << "_aidl_ret_status = "; |
| 950 | WriteToParcelFor({out, types, variable->GetType(), "parcel", variable->GetName()}); |
| 951 | out << ";\n"; |
| 952 | StatusCheckReturn(out); |
| 953 | } |
Jeongik Cha | 95eba57 | 2018-11-22 09:14:52 +0900 | [diff] [blame] | 954 | out << "size_t _aidl_end_pos = AParcel_getDataPosition(parcel);\n"; |
| 955 | out << "AParcel_setDataPosition(parcel, _aidl_start_pos);\n"; |
| 956 | out << "AParcel_writeInt32(parcel, _aidl_end_pos - _aidl_start_pos);\n"; |
| 957 | out << "AParcel_setDataPosition(parcel, _aidl_end_pos);\n"; |
| 958 | |
Steven Moreland | bb8f46c | 2018-10-03 17:38:00 -0700 | [diff] [blame] | 959 | out << "return _aidl_ret_status;\n"; |
| 960 | out.Dedent(); |
| 961 | out << "}\n"; |
| 962 | out << "\n"; |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 963 | LeaveNdkNamespace(out, defined_type); |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 964 | } |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 965 | |
Daniel Norman | 455d95c | 2019-11-19 09:55:39 -0800 | [diff] [blame] | 966 | std::string GenerateEnumToString(const AidlTypenames& typenames, |
| 967 | const AidlEnumDeclaration& enum_decl) { |
| 968 | std::ostringstream code; |
| 969 | code << "static inline std::string toString(" << enum_decl.GetName() << " val) {\n"; |
| 970 | code << " switch(val) {\n"; |
| 971 | std::set<std::string> unique_cases; |
| 972 | for (const auto& enumerator : enum_decl.GetEnumerators()) { |
| 973 | std::string c = enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator); |
| 974 | // Only add a case if its value has not yet been used in the switch |
| 975 | // statement. C++ does not allow multiple cases with the same value, but |
| 976 | // enums does allow this. In this scenario, the first declared |
| 977 | // enumerator with the given value is printed. |
| 978 | if (unique_cases.count(c) == 0) { |
| 979 | unique_cases.insert(c); |
| 980 | code << " case " << enum_decl.GetName() << "::" << enumerator->GetName() << ":\n"; |
| 981 | code << " return \"" << enumerator->GetName() << "\";\n"; |
| 982 | } |
| 983 | } |
| 984 | code << " default:\n"; |
| 985 | code << " return std::to_string(static_cast<" |
| 986 | << NdkNameOf(typenames, enum_decl.GetBackingType(), StorageMode::STACK) << ">(val));\n"; |
| 987 | code << " }\n"; |
| 988 | code << "}\n"; |
| 989 | return code.str(); |
| 990 | } |
| 991 | |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 992 | void GenerateEnumHeader(CodeWriter& out, const AidlTypenames& types, |
| 993 | const AidlEnumDeclaration& enum_decl, const Options& /*options*/) { |
| 994 | out << "#pragma once\n"; |
| 995 | out << "\n"; |
| 996 | |
| 997 | GenerateHeaderIncludes(out, types, enum_decl); |
Jooyung Han | 7a9aceb | 2019-12-17 14:18:15 +0000 | [diff] [blame] | 998 | // enum specific headers |
| 999 | out << "#include <array>\n"; |
| 1000 | out << "#include <android/binder_enums.h>\n"; |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1001 | |
| 1002 | EnterNdkNamespace(out, enum_decl); |
| 1003 | out << "enum class " << enum_decl.GetName() << " : " |
| 1004 | << NdkNameOf(types, enum_decl.GetBackingType(), StorageMode::STACK) << " {\n"; |
| 1005 | out.Indent(); |
| 1006 | for (const auto& enumerator : enum_decl.GetEnumerators()) { |
| 1007 | out << enumerator->GetName() << " = " |
| 1008 | << enumerator->ValueString(enum_decl.GetBackingType(), ConstantValueDecorator) << ",\n"; |
| 1009 | } |
| 1010 | out.Dedent(); |
| 1011 | out << "};\n"; |
Daniel Norman | 455d95c | 2019-11-19 09:55:39 -0800 | [diff] [blame] | 1012 | out << "\n"; |
| 1013 | out << GenerateEnumToString(types, enum_decl); |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1014 | LeaveNdkNamespace(out, enum_decl); |
Jooyung Han | 7a9aceb | 2019-12-17 14:18:15 +0000 | [diff] [blame] | 1015 | |
| 1016 | out << "namespace ndk {\n"; |
| 1017 | out << "namespace internal {\n"; |
| 1018 | out << cpp::GenerateEnumValues(enum_decl, {"aidl"}); |
| 1019 | out << "} // namespace internal\n"; |
| 1020 | out << "} // namespace android\n"; |
Daniel Norman | 37d43dd | 2019-09-09 17:22:34 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Steven Moreland | b0057e7 | 2018-08-27 01:44:11 -0700 | [diff] [blame] | 1023 | } // namespace internals |
| 1024 | } // namespace ndk |
| 1025 | } // namespace aidl |
| 1026 | } // namespace android |