1 /*
2  * Copyright (C) 2020 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 <android-base/properties.h>
18 #include <android/hardware/dumpstate/1.1/IDumpstateDevice.h>
19 #include <android/hardware/dumpstate/1.1/types.h>
20 #include <hidl/HidlLazyUtils.h>
21 #include <hidl/HidlSupport.h>
22 #include <hidl/HidlTransportSupport.h>
23 #include <log/log.h>
24 
25 #include "DumpstateUtil.h"
26 
27 namespace {
28 using ::android::hardware::hidl_handle;
29 using ::android::hardware::Return;
30 using ::android::hardware::Void;
31 
32 using ::android::hardware::dumpstate::V1_1::DumpstateMode;
33 using ::android::hardware::dumpstate::V1_1::DumpstateStatus;
34 using ::android::hardware::dumpstate::V1_1::IDumpstateDevice;
35 
36 using ::android::os::dumpstate::DumpFileToFd;
37 
38 const char kVerboseLoggingProperty[] = "persist.dumpstate.verbose_logging.enabled";
39 
40 struct DumpstateDevice : public IDumpstateDevice {
41     // 1.1
dumpstateBoard_1_1__anon55fbfdbe0111::DumpstateDevice42     Return<DumpstateStatus> dumpstateBoard_1_1(const hidl_handle& handle, const DumpstateMode mode,
43                                                uint64_t /*timeoutMillis*/) override {
44         if (handle == nullptr || handle->numFds < 1) {
45             ALOGE("no FDs\n");
46             return DumpstateStatus::ILLEGAL_ARGUMENT;
47         }
48 
49         int fd = handle->data[0];
50         if (fd < 0) {
51             ALOGE("invalid FD: %d\n", fd);
52             return DumpstateStatus::ILLEGAL_ARGUMENT;
53         }
54 
55         switch (mode) {
56             case DumpstateMode::FULL:
57                 return dumpstateBoardImpl(fd, true);
58 
59             case DumpstateMode::DEFAULT:
60                 return dumpstateBoardImpl(fd, false);
61 
62             case DumpstateMode::INTERACTIVE:
63             case DumpstateMode::REMOTE:
64             case DumpstateMode::WEAR:
65             case DumpstateMode::CONNECTIVITY:
66             case DumpstateMode::WIFI:
67             case DumpstateMode::PROTO:
68                 ALOGE("The requested mode is not supported: %s\n", toString(mode).c_str());
69                 return DumpstateStatus::UNSUPPORTED_MODE;
70 
71             default:
72                 ALOGE("The requested mode is invalid: %s\n", toString(mode).c_str());
73                 return DumpstateStatus::ILLEGAL_ARGUMENT;
74         }
75     }
76 
setVerboseLoggingEnabled__anon55fbfdbe0111::DumpstateDevice77     Return<void> setVerboseLoggingEnabled(bool enable) override {
78         ::android::base::SetProperty(kVerboseLoggingProperty, enable ? "true" : "false");
79         return Void();
80     }
81 
getVerboseLoggingEnabled__anon55fbfdbe0111::DumpstateDevice82     Return<bool> getVerboseLoggingEnabled() override { return getVerboseLoggingEnabledImpl(); }
83 
84     // 1.0
dumpstateBoard__anon55fbfdbe0111::DumpstateDevice85     Return<void> dumpstateBoard(const hidl_handle& h) override {
86         dumpstateBoard_1_1(h, DumpstateMode::DEFAULT, 0);
87         return Void();
88     }
89 
dumpstateBoardImpl__anon55fbfdbe0111::DumpstateDevice90     DumpstateStatus dumpstateBoardImpl(const int fd, const bool full) {
91         ALOGD("DumpstateDevice::dumpstateBoard() FD: %d\n", fd);
92         ALOGI("Dumpstate HIDL not provided by device\n");
93 
94         dprintf(fd, "verbose logging: %s\n",
95                 getVerboseLoggingEnabledImpl() ? "enabled" : "disabled");
96 
97         dprintf(fd, "[%s] %s\n", (full ? "full" : "default"), "Hello, world!");
98 
99         // Shows an example on how to use the libdumpstateutil API.
100         DumpFileToFd(fd, "cmdline", "/proc/self/cmdline");
101 
102         return DumpstateStatus::OK;
103     }
104 
getVerboseLoggingEnabledImpl__anon55fbfdbe0111::DumpstateDevice105     static bool getVerboseLoggingEnabledImpl() {
106         return ::android::base::GetBoolProperty(kVerboseLoggingProperty, false);
107     }
108 };
109 }  // namespace
110 
main(int,char **)111 int main(int, char**) {
112     using ::android::sp;
113     using ::android::hardware::configureRpcThreadpool;
114     using ::android::hardware::joinRpcThreadpool;
115     using ::android::hardware::LazyServiceRegistrar;
116 
117     configureRpcThreadpool(1, true);
118 
119     sp<DumpstateDevice> dumpstate(new DumpstateDevice);
120     auto serviceRegistrar = LazyServiceRegistrar::getInstance();
121 
122     if (serviceRegistrar.registerService(dumpstate) != ::android::OK) {
123         ALOGE("Could not register service.");
124         return 1;
125     }
126 
127     joinRpcThreadpool();
128     return 0;
129 }
130