1 /*
2  * Copyright (c) 2021-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 <unistd.h>
17 
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "distributed_notification_service_ipc_interface_code.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "parcel.h"
25 #include "ans_manager_proxy.h"
26 
27 namespace OHOS {
28 namespace Notification {
InnerTransact(NotificationInterfaceCode code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)29 ErrCode AnsManagerProxy::InnerTransact(NotificationInterfaceCode code,
30     MessageOption &flags, MessageParcel &data, MessageParcel &reply)
31 {
32     auto remote = Remote();
33     if (remote == nullptr) {
34         ANS_LOGE("[InnerTransact] defeat: get Remote defeat code %{public}u", code);
35         return ERR_DEAD_OBJECT;
36     }
37     int32_t err = remote->SendRequest(static_cast<uint32_t>(code), data, reply, flags);
38     switch (err) {
39         case NO_ERROR: {
40             return ERR_OK;
41         }
42         case DEAD_OBJECT: {
43             ANS_LOGE("[InnerTransact] defeat: ipcErr=%{public}d code %{public}d", err, code);
44             return ERR_DEAD_OBJECT;
45         }
46         default: {
47             ANS_LOGE("[InnerTransact] defeat: ipcErr=%{public}d code %{public}d", err, code);
48             return ERR_ANS_TRANSACT_FAILED;
49         }
50     }
51 }
52 
ShellDump(const std::string & cmd,const std::string & bundle,int32_t userId,int32_t recvUserId,std::vector<std::string> & dumpInfo)53 ErrCode AnsManagerProxy::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId,
54     int32_t recvUserId, std::vector<std::string> &dumpInfo)
55 {
56     MessageParcel data;
57     if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) {
58         ANS_LOGE("[ShellDump] fail: write interface token failed.");
59         return ERR_ANS_PARCELABLE_FAILED;
60     }
61     if (!data.WriteString(cmd)) {
62         ANS_LOGE("[ShellDump] fail: write dump cmd failed.");
63         return ERR_ANS_PARCELABLE_FAILED;
64     }
65     if (!data.WriteString(bundle)) {
66         ANS_LOGE("[ShellDump] fail: write dump bundle failed.");
67         return ERR_ANS_PARCELABLE_FAILED;
68     }
69     if (!data.WriteInt32(userId)) {
70         ANS_LOGE("[ShellDump] fail: write dump userId failed.");
71         return ERR_ANS_PARCELABLE_FAILED;
72     }
73     if (!data.WriteInt32(recvUserId)) {
74         ANS_LOGE("[ShellDump] fail: write dump recvUserId failed.");
75         return ERR_ANS_PARCELABLE_FAILED;
76     }
77     MessageParcel reply;
78     MessageOption option = {MessageOption::TF_SYNC};
79     ErrCode result = InnerTransact(NotificationInterfaceCode::SHELL_DUMP, option, data, reply);
80     if (result != ERR_OK) {
81         ANS_LOGE("[ShellDump] fail: transact ErrCode=%{public}d", result);
82         return ERR_ANS_TRANSACT_FAILED;
83     }
84     if (!reply.ReadInt32(result)) {
85         ANS_LOGE("[ShellDump] fail: read result failed.");
86         return ERR_ANS_PARCELABLE_FAILED;
87     }
88     if (!reply.ReadStringVector(&dumpInfo)) {
89         ANS_LOGE("[ShellDump] fail: read dumpInfo failed.");
90         return ERR_ANS_PARCELABLE_FAILED;
91     }
92     return result;
93 }
94 }  // namespace Notification
95 }  // namespace OHOS
96