1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "display_dump_service.h"
17
18#include <string>
19#include <securec.h>
20#include <cstdio>
21#include <dlfcn.h>
22
23#include "devhost_dump_reg.h"
24#include "hdf_base.h"
25#include "hdf_log.h"
26
27#define HDF_LOG_TAG uhdf_composer_host
28
29namespace OHOS {
30namespace HDI {
31namespace Display {
32namespace Composer {
33namespace V1_0 {
34
35using namespace std;
36using namespace OHOS::HDI::Display::Composer::V1_0;
37
38const char *g_composerDumpHelp =
39    " Composer Host dump options:\n"
40    "     -module [name]: get dump info.\n"
41    "          [name]\n"
42    "             vdi: get vdi dump info\n"
43    "             display: get display dump info";
44
45static int32_t ShowDumpMenu(struct HdfSBuf *reply)
46{
47    (void)HdfSbufWriteString(reply, g_composerDumpHelp);
48    return HDF_SUCCESS;
49}
50
51enum {
52    DUMP_EVENT_NONE,
53    DUMP_EVENT_VDI,
54    DUMP_EVENT_DISPLAY,
55};
56
57static int32_t GetDumpEvent(struct HdfSBuf *data, uint32_t *argsNum)
58{
59    if (!HdfSbufReadUint32(data, argsNum)) {
60        HDF_LOGE("%{public}s: read argsNum failed!", __func__);
61        return DUMP_EVENT_NONE;
62    }
63    const char *op1 = HdfSbufReadString(data);
64    if (op1 == nullptr || strcmp(op1, "-module") != 0) {
65        return DUMP_EVENT_NONE;
66    }
67    const char *op2 = HdfSbufReadString(data);
68    if (op2 == nullptr) {
69        return DUMP_EVENT_NONE;
70    }
71    if (strcmp(op2, "vdi") == 0) {
72        return DUMP_EVENT_VDI;
73    }
74    if (strcmp(op2, "display") == 0) {
75        return DUMP_EVENT_DISPLAY;
76    }
77    return DUMP_EVENT_NONE;
78}
79
80int32_t ComposerDumpEvent(struct HdfSBuf *data, struct HdfSBuf *reply)
81{
82    if (data == nullptr || reply == nullptr) {
83        HDF_LOGE("%{public}s: %{public}s is nullptr", __func__, (data == nullptr) ? "data" : "reply");
84        return HDF_FAILURE;
85    }
86
87    int32_t ret;
88    uint32_t argsNum = 0;
89    int32_t event = GetDumpEvent(data, &argsNum);
90    VdiDumper &vdiDumper = VdiDumper::GetInstance();
91    DisplayDumper &dispDumper = DisplayDumper::GetInstance();
92    switch (event) {
93        case DUMP_EVENT_VDI:
94            ret = vdiDumper.ComposerHostDumpProcess(data, reply, argsNum);
95            break;
96        case DUMP_EVENT_DISPLAY:
97            ret = dispDumper.ComposerHostDumpProcess(data, reply, argsNum);
98            break;
99        default:
100            ret = ShowDumpMenu(reply);
101            break;
102    }
103
104    if (ret != HDF_SUCCESS) {
105        HDF_LOGE("%{public}s: get composer dump failed, ret=%{public}d", __func__, ret);
106        return HDF_FAILURE;
107    }
108
109    return HDF_SUCCESS;
110}
111
112} //namespace V1_0
113} //namespace Composer
114} //namespace Display
115} //namespace HDI
116} //namespace OHOS