1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include <android-base/logging.h>
18*288bf522SAndroid Build Coastguard Worker #include <android-base/properties.h>
19*288bf522SAndroid Build Coastguard Worker #include <android/lpdump/ILpdump.h>
20*288bf522SAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
21*288bf522SAndroid Build Coastguard Worker #include <iostream>
22*288bf522SAndroid Build Coastguard Worker #include <string>
23*288bf522SAndroid Build Coastguard Worker #include <vector>
24*288bf522SAndroid Build Coastguard Worker
25*288bf522SAndroid Build Coastguard Worker using namespace android;
26*288bf522SAndroid Build Coastguard Worker using namespace std::chrono_literals;
27*288bf522SAndroid Build Coastguard Worker using ::android::lpdump::ILpdump;
28*288bf522SAndroid Build Coastguard Worker
Run(ILpdump * service,const std::vector<std::string> & args)29*288bf522SAndroid Build Coastguard Worker int Run(ILpdump* service, const std::vector<std::string>& args) {
30*288bf522SAndroid Build Coastguard Worker std::string output;
31*288bf522SAndroid Build Coastguard Worker binder::Status status = service->run(args, &output);
32*288bf522SAndroid Build Coastguard Worker if (status.isOk()) {
33*288bf522SAndroid Build Coastguard Worker std::cout << output;
34*288bf522SAndroid Build Coastguard Worker return 0;
35*288bf522SAndroid Build Coastguard Worker }
36*288bf522SAndroid Build Coastguard Worker std::cerr << status.exceptionMessage();
37*288bf522SAndroid Build Coastguard Worker if (status.serviceSpecificErrorCode() != 0) {
38*288bf522SAndroid Build Coastguard Worker return status.serviceSpecificErrorCode();
39*288bf522SAndroid Build Coastguard Worker }
40*288bf522SAndroid Build Coastguard Worker return -status.exceptionCode();
41*288bf522SAndroid Build Coastguard Worker }
42*288bf522SAndroid Build Coastguard Worker
GetArgVector(int argc,char * argv[])43*288bf522SAndroid Build Coastguard Worker std::vector<std::string> GetArgVector(int argc, char* argv[]) {
44*288bf522SAndroid Build Coastguard Worker std::vector<std::string> args;
45*288bf522SAndroid Build Coastguard Worker args.reserve(argc);
46*288bf522SAndroid Build Coastguard Worker for (int i = 0; i < argc; ++i) {
47*288bf522SAndroid Build Coastguard Worker args.push_back(argv[i]);
48*288bf522SAndroid Build Coastguard Worker }
49*288bf522SAndroid Build Coastguard Worker return args;
50*288bf522SAndroid Build Coastguard Worker }
51*288bf522SAndroid Build Coastguard Worker
52*288bf522SAndroid Build Coastguard Worker class LpdumpService {
53*288bf522SAndroid Build Coastguard Worker public:
LpdumpService()54*288bf522SAndroid Build Coastguard Worker LpdumpService() {
55*288bf522SAndroid Build Coastguard Worker base::SetProperty("sys.lpdumpd", "start");
56*288bf522SAndroid Build Coastguard Worker status_t status = getService(String16("lpdump_service"), &service_);
57*288bf522SAndroid Build Coastguard Worker int wait_counter = 0;
58*288bf522SAndroid Build Coastguard Worker while (status != OK && wait_counter++ < 3) {
59*288bf522SAndroid Build Coastguard Worker sleep(1);
60*288bf522SAndroid Build Coastguard Worker status = getService(String16("lpdump"), &service_);
61*288bf522SAndroid Build Coastguard Worker }
62*288bf522SAndroid Build Coastguard Worker if (status != OK || service_ == nullptr) {
63*288bf522SAndroid Build Coastguard Worker std::cerr << "Cannot get lpdump service: " << strerror(-status) << std::endl;
64*288bf522SAndroid Build Coastguard Worker LOG(ERROR) << "Cannot get lpdump service: " << strerror(-status);
65*288bf522SAndroid Build Coastguard Worker }
66*288bf522SAndroid Build Coastguard Worker }
~LpdumpService()67*288bf522SAndroid Build Coastguard Worker ~LpdumpService() {
68*288bf522SAndroid Build Coastguard Worker base::SetProperty("sys.lpdumpd", "stop");
69*288bf522SAndroid Build Coastguard Worker }
get()70*288bf522SAndroid Build Coastguard Worker sp<ILpdump> get() { return service_; }
71*288bf522SAndroid Build Coastguard Worker private:
72*288bf522SAndroid Build Coastguard Worker sp<ILpdump> service_;
73*288bf522SAndroid Build Coastguard Worker };
74*288bf522SAndroid Build Coastguard Worker
main(int argc,char * argv[])75*288bf522SAndroid Build Coastguard Worker int main(int argc, char* argv[]) {
76*288bf522SAndroid Build Coastguard Worker LpdumpService wrapper;
77*288bf522SAndroid Build Coastguard Worker sp<ILpdump> service = wrapper.get();
78*288bf522SAndroid Build Coastguard Worker if (service == nullptr) {
79*288bf522SAndroid Build Coastguard Worker return 1;
80*288bf522SAndroid Build Coastguard Worker }
81*288bf522SAndroid Build Coastguard Worker int ret = Run(service.get(), GetArgVector(argc, argv));
82*288bf522SAndroid Build Coastguard Worker base::SetProperty("sys.lpdumpd", "stop");
83*288bf522SAndroid Build Coastguard Worker return ret;
84*288bf522SAndroid Build Coastguard Worker }
85