1 /*
2 * Copyright (C) 2019 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 "src/android_internal/atrace_hal.h"
18
19 #include <android/hardware/atrace/1.0/IAtraceDevice.h>
20 #include <iostream>
21
22 namespace perfetto {
23 namespace android_internal {
24
25 using android::hardware::atrace::V1_0::IAtraceDevice;
26 using android::hardware::atrace::V1_0::TracingCategory;
27 using android::hardware::hidl_vec;
28 using android::hardware::Return;
29
30 namespace {
31
32 android::sp<IAtraceDevice> g_atraceHal;
33
GetService()34 bool GetService() {
35 if (!g_atraceHal)
36 g_atraceHal = IAtraceDevice::getService();
37
38 return g_atraceHal != nullptr;
39 }
40
41 } // namespace
42
GetCategories(TracingVendorCategory * categories,size_t * size_of_arr)43 bool GetCategories(TracingVendorCategory* categories, size_t* size_of_arr) {
44 const size_t in_array_size = *size_of_arr;
45 *size_of_arr = 0;
46 if (!GetService())
47 return false;
48
49 auto category_cb = [categories, size_of_arr,
50 &in_array_size](hidl_vec<TracingCategory> r) {
51 *size_of_arr = std::min(in_array_size, r.size());
52 for (int i = 0; i < *size_of_arr; ++i) {
53 const TracingCategory& cat = r[i];
54 TracingVendorCategory& result = categories[i];
55 strncpy(result.name, cat.name.c_str(), sizeof(result.name));
56 strncpy(result.description, cat.description.c_str(),
57 sizeof(result.description));
58 result.name[sizeof(result.name) - 1] = '\0';
59 result.description[sizeof(result.description) - 1] = '\0';
60 }
61 };
62
63 g_atraceHal->listCategories(category_cb);
64 return true;
65 }
66
67 } // namespace android_internal
68 } // namespace perfetto
69