summaryrefslogtreecommitdiff
path: root/dumpstate/1.1/default/main.cpp
blob: 4510f63647897e85f9d8d8b38a1395e52e7e4854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/properties.h>
#include <android/hardware/dumpstate/1.1/IDumpstateDevice.h>
#include <android/hardware/dumpstate/1.1/types.h>
#include <hidl/HidlLazyUtils.h>
#include <hidl/HidlSupport.h>
#include <hidl/HidlTransportSupport.h>
#include <log/log.h>

#include "DumpstateUtil.h"

namespace {
using ::android::hardware::hidl_handle;
using ::android::hardware::Return;
using ::android::hardware::Void;

using ::android::hardware::dumpstate::V1_1::DumpstateMode;
using ::android::hardware::dumpstate::V1_1::DumpstateStatus;
using ::android::hardware::dumpstate::V1_1::IDumpstateDevice;

using ::android::os::dumpstate::DumpFileToFd;

const char kVerboseLoggingProperty[] = "persist.dumpstate.verbose_logging.enabled";

struct DumpstateDevice : public IDumpstateDevice {
    // 1.1
    Return<DumpstateStatus> dumpstateBoard_1_1(const hidl_handle& handle, const DumpstateMode mode,
                                               uint64_t /*timeoutMillis*/) override {
        if (handle == nullptr || handle->numFds < 1) {
            ALOGE("no FDs\n");
            return DumpstateStatus::ILLEGAL_ARGUMENT;
        }

        int fd = handle->data[0];
        if (fd < 0) {
            ALOGE("invalid FD: %d\n", fd);
            return DumpstateStatus::ILLEGAL_ARGUMENT;
        }

        switch (mode) {
            case DumpstateMode::FULL:
                return dumpstateBoardImpl(fd, true);

            case DumpstateMode::DEFAULT:
                return dumpstateBoardImpl(fd, false);

            case DumpstateMode::INTERACTIVE:
            case DumpstateMode::REMOTE:
            case DumpstateMode::WEAR:
            case DumpstateMode::CONNECTIVITY:
            case DumpstateMode::WIFI:
            case DumpstateMode::PROTO:
                ALOGE("The requested mode is not supported: %s\n", toString(mode).c_str());
                return DumpstateStatus::UNSUPPORTED_MODE;

            default:
                ALOGE("The requested mode is invalid: %s\n", toString(mode).c_str());
                return DumpstateStatus::ILLEGAL_ARGUMENT;
        }
    }

    Return<void> setVerboseLoggingEnabled(bool enable) override {
        ::android::base::SetProperty(kVerboseLoggingProperty, enable ? "true" : "false");
        return Void();
    }

    Return<bool> getVerboseLoggingEnabled() override { return getVerboseLoggingEnabledImpl(); }

    // 1.0
    Return<void> dumpstateBoard(const hidl_handle& h) override {
        dumpstateBoard_1_1(h, DumpstateMode::DEFAULT, 0);
        return Void();
    }

    DumpstateStatus dumpstateBoardImpl(const int fd, const bool full) {
        ALOGD("DumpstateDevice::dumpstateBoard() FD: %d\n", fd);
        ALOGI("Dumpstate HIDL not provided by device\n");

        dprintf(fd, "verbose logging: %s\n",
                getVerboseLoggingEnabledImpl() ? "enabled" : "disabled");

        dprintf(fd, "[%s] %s\n", (full ? "full" : "default"), "Hello, world!");

        // Shows an example on how to use the libdumpstateutil API.
        DumpFileToFd(fd, "cmdline", "/proc/self/cmdline");

        return DumpstateStatus::OK;
    }

    static bool getVerboseLoggingEnabledImpl() {
        return ::android::base::GetBoolProperty(kVerboseLoggingProperty, false);
    }
};
}  // namespace

int main(int, char**) {
    using ::android::sp;
    using ::android::hardware::configureRpcThreadpool;
    using ::android::hardware::joinRpcThreadpool;
    using ::android::hardware::LazyServiceRegistrar;

    configureRpcThreadpool(1, true);

    auto dumpstate = sp<DumpstateDevice>::make();
    auto serviceRegistrar = LazyServiceRegistrar::getInstance();

    if (serviceRegistrar.registerService(dumpstate) != ::android::OK) {
        ALOGE("Could not register service.");
        return 1;
    }

    joinRpcThreadpool();
    return 0;
}