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_vdi.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_vdiComposerDumpHelp =
39    " Get Vdi Dump Info options:\n"
40    "     -cmd [name]: operate vdi dump.\n"
41    "        [name]\n"
42    "           buffer: dump buffer\n"
43    "           user: update user config\n";
44
45void VdiDumper::SetDumpInfoFunc(GetDumpInfoFunc DumpInfoFunc_)
46{
47    if (DumpInfoFunc_ == nullptr) {
48        HDF_LOGE("%{public}s: SetDumpInfoFunc failed, DumpInfoFunc_ null", __func__);
49    }
50
51    getDumpInfoFunc_ = DumpInfoFunc_;
52}
53
54void VdiDumper::SetConfigFunc(UpdateConfigFunc ConfigFunc_)
55{
56    if (ConfigFunc_ == nullptr) {
57        HDF_LOGE("%{public}s: SetConfigFunc failed, ConfigFunc_ null", __func__);
58    }
59
60    updateConfigFunc_ = ConfigFunc_;
61}
62
63int32_t VdiDumper::DumpBuffer(HdfSBuf *reply)
64{
65    string result;
66    if (getDumpInfoFunc_ != nullptr) {
67        getDumpInfoFunc_(result);
68    } else {
69        result += "vdi -cmd buffer not support.\n";
70    }
71
72    if (!HdfSbufWriteString(reply, result.c_str())) {
73        HDF_LOGI("%{public}s: dump buffer failed", __func__);
74        return HDF_FAILURE;
75    }
76
77    return HDF_SUCCESS;
78}
79
80int32_t VdiDumper::UpdateUserConfig(HdfSBuf *reply)
81{
82    string result;
83    if (updateConfigFunc_ != nullptr) {
84        updateConfigFunc_(result);
85    } else {
86        result += "vdi -cmd user not support.\n";
87    }
88
89    if (!HdfSbufWriteString(reply, result.c_str())) {
90        HDF_LOGI("%{public}s: udpate failed", __func__);
91        return HDF_FAILURE;
92    }
93
94    return HDF_SUCCESS;
95}
96
97int32_t VdiDumper::ShowDumpMenu(HdfSBuf *reply)
98{
99    if (reply != nullptr) {
100        (void)HdfSbufWriteString(reply, g_vdiComposerDumpHelp);
101        return HDF_SUCCESS;
102    } else {
103        return HDF_FAILURE;
104    }
105}
106
107enum {
108    DUMP_VDI_EVENT_NONE,
109    DUMP_VDI_EVENT_USER,
110    DUMP_VDI_EVENT_BUFFER,
111};
112
113int32_t GetDumpVdiEvent(struct HdfSBuf *data)
114{
115    const char *op1 = HdfSbufReadString(data);
116    if (op1 == nullptr || strcmp(op1, "-cmd") != 0) {
117        return DUMP_VDI_EVENT_NONE;
118    }
119    const char *op2 = HdfSbufReadString(data);
120    if (op2 == nullptr) {
121        return DUMP_VDI_EVENT_NONE;
122    }
123    if (strcmp(op2, "user") == 0) {
124        return DUMP_VDI_EVENT_USER;
125    }
126    if (strcmp(op2, "buffer") == 0) {
127        return DUMP_VDI_EVENT_BUFFER;
128    }
129    return DUMP_VDI_EVENT_NONE;
130}
131
132int32_t VdiDumper::ComposerHostDumpProcess(struct HdfSBuf *data, struct HdfSBuf *reply, uint32_t argsNum)
133{
134    if (reply == nullptr || data == nullptr) {
135        return HDF_FAILURE;
136    }
137    int32_t ret;
138    int32_t event = GetDumpVdiEvent(data);
139    switch (event) {
140        case DUMP_VDI_EVENT_USER:
141            ret = UpdateUserConfig(reply);
142            break;
143        case DUMP_VDI_EVENT_BUFFER:
144            ret = DumpBuffer(reply);
145            break;
146        default:
147            ret = ShowDumpMenu(reply);
148            break;
149    }
150
151    if (ret != HDF_SUCCESS) {
152        HDF_LOGE("%{public}s: get composer vdi dump failed", __func__);
153        return HDF_FAILURE;
154    }
155
156    return HDF_SUCCESS;
157}
158
159} //namespace V1_0
160} //namespace Composer
161} //namespace Display
162} //namespace HDI
163} //namespace OHOS