• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <android-base/properties.h>
18 #include <log/log.h>
19 #include "DumpstateUtil.h"
20 
21 #include "Dumpstate.h"
22 
23 using android::os::dumpstate::DumpFileToFd;
24 
25 namespace aidl {
26 namespace android {
27 namespace hardware {
28 namespace dumpstate {
29 
30 const char kVerboseLoggingProperty[] = "persist.dumpstate.verbose_logging.enabled";
31 
dumpstateBoard(const std::vector<::ndk::ScopedFileDescriptor> & in_fds,IDumpstateDevice::DumpstateMode in_mode,int64_t in_timeoutMillis)32 ndk::ScopedAStatus Dumpstate::dumpstateBoard(const std::vector<::ndk::ScopedFileDescriptor>& in_fds,
33                                              IDumpstateDevice::DumpstateMode in_mode,
34                                              int64_t in_timeoutMillis) {
35     (void)in_timeoutMillis;
36 
37     if (in_fds.size() < 1) {
38         return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
39                                                                 "No file descriptor");
40     }
41 
42     int fd = in_fds[0].get();
43     if (fd < 0) {
44         return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
45                                                                 "Invalid file descriptor");
46     }
47 
48     switch (in_mode) {
49         case IDumpstateDevice::DumpstateMode::FULL:
50             return dumpstateBoardImpl(fd, true);
51 
52         case IDumpstateDevice::DumpstateMode::DEFAULT:
53             return dumpstateBoardImpl(fd, false);
54 
55         case IDumpstateDevice::DumpstateMode::INTERACTIVE:
56         case IDumpstateDevice::DumpstateMode::REMOTE:
57         case IDumpstateDevice::DumpstateMode::WEAR:
58         case IDumpstateDevice::DumpstateMode::CONNECTIVITY:
59         case IDumpstateDevice::DumpstateMode::WIFI:
60         case IDumpstateDevice::DumpstateMode::PROTO:
61             return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(ERROR_UNSUPPORTED_MODE,
62                                                                            "Unsupported mode");
63 
64         default:
65             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
66                                                                     "Invalid mode");
67     }
68 
69     return ndk::ScopedAStatus::ok();
70 }
71 
getVerboseLoggingEnabled(bool * _aidl_return)72 ndk::ScopedAStatus Dumpstate::getVerboseLoggingEnabled(bool* _aidl_return) {
73     *_aidl_return = getVerboseLoggingEnabledImpl();
74     return ndk::ScopedAStatus::ok();
75 }
76 
setVerboseLoggingEnabled(bool in_enable)77 ndk::ScopedAStatus Dumpstate::setVerboseLoggingEnabled(bool in_enable) {
78     ::android::base::SetProperty(kVerboseLoggingProperty, in_enable ? "true" : "false");
79     return ndk::ScopedAStatus::ok();
80 }
81 
getVerboseLoggingEnabledImpl()82 bool Dumpstate::getVerboseLoggingEnabledImpl() {
83     return ::android::base::GetBoolProperty(kVerboseLoggingProperty, false);
84 }
85 
dumpstateBoardImpl(const int fd,const bool full)86 ndk::ScopedAStatus Dumpstate::dumpstateBoardImpl(const int fd, const bool full) {
87     ALOGD("DumpstateDevice::dumpstateBoard() FD: %d\n", fd);
88 
89     dprintf(fd, "verbose logging: %s\n", getVerboseLoggingEnabledImpl() ? "enabled" : "disabled");
90     dprintf(fd, "[%s] %s\n", (full ? "full" : "default"), "Hello, world!");
91 
92     // Shows an example on how to use the libdumpstateutil API.
93     DumpFileToFd(fd, "cmdline", "/proc/self/cmdline");
94 
95     return ndk::ScopedAStatus::ok();
96 }
97 
98 }  // namespace dumpstate
99 }  // namespace hardware
100 }  // namespace android
101 }  // namespace aidl
102