aboutsummaryrefslogtreecommitdiff
path: root/aidl_to_cpp_common.cpp
blob: 897831105634b9397c08b4597f605bdf82aaa15a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * Copyright (C) 2018, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unordered_map>

#include "aidl_to_cpp_common.h"
#include "logging.h"
#include "os.h"

namespace android {
namespace aidl {
namespace cpp {

string ClassName(const AidlDefinedType& defined_type, ClassNames type) {
  string base_name = defined_type.GetName();
  if (base_name.length() >= 2 && base_name[0] == 'I' && isupper(base_name[1])) {
    base_name = base_name.substr(1);
  }

  switch (type) {
    case ClassNames::CLIENT:
      return "Bp" + base_name;
    case ClassNames::SERVER:
      return "Bn" + base_name;
    case ClassNames::INTERFACE:
      return "I" + base_name;
    case ClassNames::DEFAULT_IMPL:
      return "I" + base_name + "Default";
    case ClassNames::BASE:
      return base_name;
    case ClassNames::RAW:
      [[fallthrough]];
    default:
      return defined_type.GetName();
  }
}

std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
                       bool use_os_sep) {
  std::string file_path = defined_type.GetPackage();
  for (char& c : file_path) {
    if (c == '.') {
      c = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
    }
  }
  if (!file_path.empty()) {
    file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/';
  }
  file_path += ClassName(defined_type, class_type);
  file_path += ".h";

  return file_path;
}

void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
  const std::vector<std::string> packages = defined_type.GetSplitPackage();
  for (const std::string& package : packages) {
    out << "namespace " << package << " {\n";
  }
}
void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
  const std::vector<std::string> packages = defined_type.GetSplitPackage();
  for (auto it = packages.rbegin(); it != packages.rend(); ++it) {
    out << "}  // namespace " << *it << "\n";
  }
}

string BuildVarName(const AidlArgument& a) {
  string prefix = "out_";
  if (a.GetDirection() & AidlArgument::IN_DIR) {
    prefix = "in_";
  }
  return prefix + a.GetName();
}

struct TypeInfo {
  // name of the type in C++ output
  std::string cpp_name;

  // function that writes an expression to convert a variable to a Json::Value
  // object
  std::function<void(CodeWriter& w, const string& var_name, bool isNdk)> toJsonValueExpr;
};

const static std::unordered_map<std::string, TypeInfo> kTypeInfoMap = {
    {"void", {"void", nullptr}},
    {"boolean",
     {
         "bool",
         [](CodeWriter& c, const string& var_name, bool) {
           c << "Json::Value(" << var_name << "? \"true\" : \"false\")";
         },
     }},
    {"byte",
     {
         "int8_t",
         [](CodeWriter& c, const string& var_name, bool) {
           c << "Json::Value(" << var_name << ")";
         },
     }},
    {"char",
     {
         "char16_t",
         [](CodeWriter& c, const string& var_name, bool isNdk) {
           if (isNdk) {
             c << "Json::Value(" << var_name << ")";
           } else {
             c << "Json::Value(std::string(android::String8(&" << var_name << ", 1)))";
           }
         },
     }},
    {"int",
     {
         "int32_t",
         [](CodeWriter& c, const string& var_name, bool) {
           c << "Json::Value(" << var_name << ")";
         },
     }},
    {"long",
     {
         "int64_t",
         [](CodeWriter& c, const string& var_name, bool) {
           c << "Json::Value(static_cast<Json::Int64>(" << var_name << "))";
         },
     }},
    {"float",
     {
         "float",
         [](CodeWriter& c, const string& var_name, bool) {
           c << "Json::Value(" << var_name << ")";
         },
     }},
    {"double",
     {
         "double",
         [](CodeWriter& c, const string& var_name, bool) {
           c << "Json::Value(" << var_name << ")";
         },
     }},
    {"String",
     {
         "std::string",
         [](CodeWriter& c, const string& var_name, bool) {
           c << "Json::Value(" << var_name << ")";
         },
     }}
    // missing List, Map, ParcelFileDescriptor, IBinder
};

TypeInfo GetTypeInfo(const AidlTypeSpecifier& aidl) {
  CHECK(aidl.IsResolved()) << aidl.ToString();
  const string& aidl_name = aidl.GetName();

  TypeInfo info;
  if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
    auto it = kTypeInfoMap.find(aidl_name);
    if (it != kTypeInfoMap.end()) {
      info = it->second;
    }
  }
  // Missing interface and parcelable type
  return info;
}

inline bool CanWriteLog(const TypeInfo& t) {
  return t.cpp_name != "";
}

bool CanWriteLog(const AidlTypeSpecifier& aidl) {
  return CanWriteLog(GetTypeInfo(aidl));
}

void WriteLogFor(CodeWriter& writer, const AidlTypeSpecifier& type, const std::string& name,
                 bool isPointer, const std::string& log, bool isNdk) {
  const TypeInfo info = GetTypeInfo(type);
  if (!CanWriteLog(info)) {
    return;
  }

  const string var_object_expr = ((isPointer ? "*" : "")) + name;
  if (type.IsArray()) {
    writer << log << " = Json::Value(Json::arrayValue);\n";
    writer << "for (const auto& v: " << var_object_expr << ") " << log << ".append(";
    info.toJsonValueExpr(writer, "v", isNdk);
    writer << ");";
  } else {
    writer << log << " = ";
    info.toJsonValueExpr(writer, var_object_expr, isNdk);
    writer << ";";
  }
  writer << "\n";
}

void WriteLogForArguments(CodeWriterPtr& writer, const AidlArgument& a, bool isServer,
                          string logVarName, bool isNdk) {
  if (!CanWriteLog(a.GetType())) {
    return;
  }
  string logElementVarName = "_log_arg_element";
  (*writer) << "{\n";
  (*writer).Indent();
  (*writer) << "Json::Value " << logElementVarName << "(Json::objectValue);\n";
  string varName = isServer || isNdk ? BuildVarName(a) : a.GetName();
  (*writer) << logElementVarName << "[\"name\"] = \"" << varName << "\";\n";

  bool isPointer = a.IsOut() && !isServer;
  WriteLogFor(*(writer.get()), a.GetType(), varName, isPointer, logElementVarName + "[\"value\"]",
              isNdk);
  (*writer) << logVarName << ".append(" << logElementVarName << ");\n";
  (*writer) << "}\n";
  (*writer).Dedent();
}

const string GenLogBeforeExecute(const string className, const AidlMethod& method, bool isServer,
                                 bool isNdk) {
  string code;
  CodeWriterPtr writer = CodeWriter::ForString(&code);
  (*writer) << "Json::Value _log_input_args(Json::arrayValue);\n";

  (*writer) << "if (" << className << "::logFunc != nullptr) {\n";
  (*writer).Indent();

  for (const auto& a : method.GetArguments()) {
    if (a->IsIn()) {
      WriteLogForArguments(writer, *a, isServer, "_log_input_args", isNdk);
    }
  }

  (*writer).Dedent();
  (*writer) << "}\n";

  (*writer) << "auto _log_start = std::chrono::steady_clock::now();\n";
  writer->Close();
  return code;
}

const string GenLogAfterExecute(const string className, const AidlInterface& interface,
                                const AidlMethod& method, const string& statusVarName,
                                const string& returnVarName, bool isServer, bool isNdk) {
  string code;
  CodeWriterPtr writer = CodeWriter::ForString(&code);

  (*writer) << "if (" << className << "::logFunc != nullptr) {\n";
  (*writer).Indent();

  // Write the log as a Json object. For example,
  //
  // Json log object for following interface description
  //
  // package foo.bar;
  // interface IFoo {
  //   String TestMethod(int arg1, inout String[] arg2, out double arg3);
  // }
  //
  // would be:
  //
  // {
  //   duration_ms: 100.42,
  //   interface_name: "foo.bar.IFoo",
  //   method_name: "TestMethod",
  //   (proxy|stub)_address: "0x12345678",
  //   input_args: [
  //     {name: "arg1", value: 30,},
  //     {name: "arg2", value: ["apple", "grape"],},
  //   ],
  //   output_args: [
  //     {name: "arg2", value: ["mango", "banana"],},
  //     {name: "arg3", value: "10.5",},
  //   ],
  //   _aidl_return: "ok",
  //   binder_status: {
  //     exception_code: -8,
  //     exception_message: "Something wrong",
  //     transaction_error: 0,
  //     service_specific_error_code: -42,
  //   },
  // }
  (*writer) << "auto _log_end = std::chrono::steady_clock::now();\n";
  (*writer) << "Json::Value _log_transaction(Json::objectValue);\n";
  (*writer) << "_log_transaction[\"duration_ms\"] = "
            << "std::chrono::duration<double, std::milli>(_log_end - "
               "_log_start).count();\n";
  (*writer) << "_log_transaction[\"interface_name\"] = "
            << "Json::Value(\"" << interface.GetCanonicalName() << "\");\n";
  (*writer) << "_log_transaction[\"method_name\"] = "
            << "Json::Value(\"" << method.GetName() << "\");\n";

  (*writer) << "_log_transaction[\"" << (isServer ? "stub_address" : "proxy_address") << "\"] = ";
  (*writer) << "Json::Value("
            << "(std::ostringstream() << "
            << (isNdk && isServer ? "_aidl_impl" : "static_cast<const void*>(this)") << ").str()"
            << ");\n";
  (*writer) << "_log_transaction[\"input_args\"] = _log_input_args;\n";
  (*writer) << "Json::Value _log_output_args(Json::arrayValue);\n";

  (*writer) << "Json::Value _log_status(Json::objectValue);\n";
  if (isNdk) {
    (*writer) << "_log_status[\"exception_code\"] = Json::Value(AStatus_getExceptionCode("
              << statusVarName << ".get()));\n";
    (*writer) << "_log_status[\"exception_message\"] = Json::Value(AStatus_getMessage("
              << statusVarName << ".get()));\n";
    (*writer) << "_log_status[\"transaction_error\"] = Json::Value(AStatus_getStatus("
              << statusVarName << ".get()));\n";
    (*writer) << "_log_status[\"service_specific_error_code\"] = "
                 "Json::Value(AStatus_getServiceSpecificError("
              << statusVarName << ".get()));\n";
  } else {
    (*writer) << "_log_status[\"exception_code\"] = Json::Value(" << statusVarName
              << ".exceptionCode());\n";
    (*writer) << "_log_status[\"exception_message\"] = Json::Value(" << statusVarName
              << ".exceptionMessage());\n";
    (*writer) << "_log_status[\"transaction_error\"] = Json::Value(" << statusVarName
              << ".transactionError());\n";
    (*writer) << "_log_status[\"service_specific_error_code\"] = Json::Value(" << statusVarName
              << ".serviceSpecificErrorCode());\n";
  }

  (*writer) << "_log_transaction[\"binder_status\"] = _log_status;\n";

  for (const auto& a : method.GetOutArguments()) {
    WriteLogForArguments(writer, *a, isServer, "_log_output_args", isNdk);
  }

  (*writer) << "_log_transaction[\"output_args\"] = _log_output_args;\n";

  if (method.GetType().GetName() != "void") {
    WriteLogFor(*(writer.get()), method.GetType(), returnVarName, !isServer,
                "_log_transaction[\"" + returnVarName + "\"]", isNdk);
  }

  // call the user-provided function with the Json object for the entire
  // transaction
  (*writer) << className << "::logFunc(_log_transaction);\n";

  (*writer).Dedent();
  (*writer) << "}\n";

  writer->Close();
  return code;
}

}  // namespace cpp
}  // namespace aidl
}  // namespace android