1 /*
2 * Copyright (C) 2021 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 #include <aidl/android/frameworks/stats/IStats.h>
17
18 #include <android/binder_manager.h>
19
20 #include <statslog.h>
21
22 #include <getopt.h>
23 #include <iostream>
24
25 using aidl::android::frameworks::stats::IStats;
26 using aidl::android::frameworks::stats::VendorAtom;
27 using aidl::android::frameworks::stats::VendorAtomValue;
28
expect_message(int32_t action)29 void expect_message(int32_t action) {
30 std::cout << "expect the following log in logcat:\n";
31 std::cout << "statsd.*(" << action << ")0x10000->\n";
32 }
33
show_help()34 void show_help() {
35 std::cout << "AIDL Stats HAL client\n";
36 std::cout << " arguments:\n";
37 std::cout << " -h or --help - shows help information\n";
38 std::cout << " -v or --VendorAtom - tests report reportVendorAtom API\n";
39 std::cout << "Please enable statsd logging using 'cmd stats print-logs'";
40 std::cout << "\n\n you can use multiple arguments to trigger multiple events.\n";
41 }
42
main(int argc,char * argv[])43 int main(int argc, char* argv[]) {
44 // get instance of the aidl version
45 const std::string instance = std::string() + IStats::descriptor + "/default";
46 std::shared_ptr<IStats> service =
47 IStats::fromBinder(ndk::SpAIBinder(AServiceManager_getService(instance.c_str())));
48 if (!service) {
49 std::cerr << "No Stats aidl HAL";
50 return 1;
51 }
52
53 std::cout << "Service instance obtained : " << instance << std::endl;
54
55 static struct option opts[] = {
56 {"VendorAtom", no_argument, 0, 'v'},
57 {"help", no_argument, 0, 'h'},
58 };
59
60 int c;
61 int hal_calls = 0;
62 int failed_calls = 0;
63 while ((c = getopt_long(argc, argv, "vh", opts, nullptr)) != -1) {
64 switch (c) {
65 case 'h': {
66 show_help();
67 break;
68 }
69 case 'v': {
70 std::vector<VendorAtomValue> values;
71 VendorAtomValue tmp;
72 tmp.set<VendorAtomValue::longValue>(70000);
73 values.push_back(tmp);
74 tmp.set<VendorAtomValue::intValue>(7);
75 values.push_back(tmp);
76 tmp.set<VendorAtomValue::floatValue>(8.5);
77 values.push_back(tmp);
78 tmp.set<VendorAtomValue::stringValue>("test");
79 values.push_back(tmp);
80 tmp.set<VendorAtomValue::intValue>(3);
81 values.push_back(tmp);
82 VendorAtom atom = {
83 .reverseDomainName = "com.google.pixel", .atomId = 100001, .values = values};
84 const ndk::ScopedAStatus ret = service->reportVendorAtom(atom);
85 if (!ret.isOk()) {
86 std::cout << "reportVendorAtom failed: " << ret.getServiceSpecificError()
87 << ". Message: " << ret.getMessage() << std::endl;
88 ++failed_calls;
89 }
90 ++hal_calls;
91 break;
92 }
93 default: {
94 show_help();
95 return 1;
96 }
97 }
98 }
99
100 if (hal_calls > 0) {
101 std::cout << hal_calls << " HAL methods called.\n";
102 std::cout << "try: logcat | grep \"statsd.*0x1000\"\n";
103 }
104
105 return failed_calls;
106 }
107