• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hdf_dump.h"
17 
18 #include "file_ex.h"
19 #include "string_ex.h"
20 
21 #include "hdf_base.h"
22 #include "hdf_dump_reg.h"
23 #include "hdf_log.h"
24 #include "hdf_sbuf.h"
25 
26 #define HDF_LOG_TAG hdf_dump
27 
28 static DevHostDumpFunc g_dump = nullptr;
29 
30 // The maximum parameter is the parameter sent to the host, including public(num=2) and private(mux_num=20) parameters
31 static constexpr int32_t MAX_PARA_NUM = 22;
32 
HdfRegisterDumpFunc(DevHostDumpFunc dump)33 void HdfRegisterDumpFunc(DevHostDumpFunc dump)
34 {
35     g_dump = dump;
36 }
37 
HdfDump(int32_t fd,const std::vector<std::u16string> & args)38 int32_t HdfDump(int32_t fd, const std::vector<std::u16string> &args)
39 {
40     if (g_dump == nullptr) {
41         HDF_LOGE("%{public}s g_dump is null",  __func__);
42         return HDF_FAILURE;
43     }
44 
45     uint32_t argv = args.size();
46     HDF_LOGI("%{public}s argv:%{public}u", __func__, argv);
47 
48     if (argv > MAX_PARA_NUM) {
49         HDF_LOGE("%{public}s argv %{public}u is invalid",  __func__, argv);
50         return HDF_FAILURE;
51     }
52 
53     struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
54     if (data == nullptr) {
55         return HDF_FAILURE;
56     }
57 
58     int32_t ret = HDF_SUCCESS;
59     std::string result;
60     const char *value = nullptr;
61 
62     struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC);
63     if (reply == nullptr) {
64         goto FINISHED;
65     }
66 
67     if (!HdfSbufWriteUint32(data, argv)) {
68         goto FINISHED;
69     }
70 
71     // Convert vector to sbuf structure
72     for (uint32_t i = 0; i < argv; i++) {
73         HDF_LOGI("%{public}s para:%{public}s", __func__, OHOS::Str16ToStr8(args.at(i)).data());
74         if (!HdfSbufWriteString(data, OHOS::Str16ToStr8(args.at(i)).data())) {
75             goto FINISHED;
76         }
77     }
78 
79     (void)g_dump(data, reply);
80 
81     value = HdfSbufReadString(reply);
82     while (value != nullptr) {
83         HDF_LOGI("%{public}s reply:%{public}s", __func__, value);
84         result.append(value);
85         value = HdfSbufReadString(reply);
86     }
87 
88     if (!OHOS::SaveStringToFd(fd, result)) {
89         ret = HDF_FAILURE;
90     }
91 
92 FINISHED:
93     HdfSbufRecycle(data);
94     HdfSbufRecycle(reply);
95     return ret;
96 }