1/*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "hello_dump.h"
17#include <securec.h>
18#include <stdio.h>
19#include "devhost_dump_reg.h"
20#include "hdf_base.h"
21#include "hello_log.h"
22
23#define HDF_LOG_TAG    uhdf_hello_service
24
25// -c dump the helloworld info
26static const char *g_dumpHelp =
27    " usage:\n"
28    " -h, --help: dump help\n"
29    " -c, --channel: dump the hello channel info\n";
30
31static uint32_t ShowHelloworldInfo(struct HdfSBuf *reply)
32{
33    int32_t ret;
34    const char *helloWorldMessage = "Hello, World!";
35
36    ret = HdfSbufWriteString(reply, helloWorldMessage);
37    if (ret != HDF_SUCCESS) {
38        HDF_LOGE("%{public}s: write hello world info failed", __func__);
39        return HDF_FAILURE;
40    }
41    
42    HDF_LOGI("%{public}s: hellodump: print hello world !", __func__);
43
44    return HDF_SUCCESS;
45
46}
47
48static int32_t DumpHelloChannel(struct HdfSBuf *reply)
49{
50    int32_t ret;
51    HDF_LOGI("%{public}s: get hello dump channel begin", __func__);
52    ret = ShowHelloworldInfo(reply);
53    if (ret != HDF_SUCCESS) {
54        HDF_LOGE("%{public}s: show hello world info failed", __func__);
55        return HDF_FAILURE;
56    }
57
58    return HDF_SUCCESS;
59}
60
61static int32_t HelloDriverDump(struct HdfSBuf *data, struct HdfSBuf *reply)
62{
63    uint32_t i;
64    uint32_t argv = 0;
65    HDF_LOGI("%{public}s: get hello dump begin xx", __func__);
66    if (data == NULL || reply == NULL) {
67        return HDF_FAILURE;
68    }
69
70    if (!HdfSbufReadUint32(data, &argv)) {
71        HDF_LOGE("%{public}s: read argv failed", __func__);
72        return HDF_FAILURE;
73    }
74
75    if (argv == 0) {
76        if (!HdfSbufWriteString(reply, g_dumpHelp)) {
77            HDF_LOGE("%{public}s: write -h failed", __func__);
78            return HDF_FAILURE;
79        }
80    }
81
82    for (i = 0; i < argv; i++) {
83        const char *value = HdfSbufReadString(data);
84        if (value == NULL) {
85            HDF_LOGE("%{public}s value is invalid", __func__);
86            return HDF_FAILURE;
87        }
88
89        if (strcmp(value, "-h") == HDF_SUCCESS) {
90            if (!HdfSbufWriteString(reply, g_dumpHelp)) {
91                HDF_LOGE("%{public}s: write -h failed", __func__);
92                return HDF_FAILURE;
93            }
94            continue;
95        } else if (strcmp(value, "-c") == HDF_SUCCESS) {
96            DumpHelloChannel(reply);
97            continue;
98        }
99    }
100
101    return HDF_SUCCESS;
102}
103
104int32_t GetHelloDump(struct HdfSBuf *data, struct HdfSBuf *reply)
105{
106    HDF_LOGI("%{public}s: get hello dump begin", __func__);
107    int32_t ret = HelloDriverDump(data, reply);
108    if (ret != HDF_SUCCESS) {
109        HDF_LOGE("%{public}s: get hello dump failed", __func__);
110        return HDF_FAILURE;
111    }
112
113    return HDF_SUCCESS;
114}
115