1 /*
2  * Copyright (C) 2024 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 "system_cmd_channel_proxy.h"
17 
18 #include "global.h"
19 #include "ipc_types.h"
20 #include "itypes_util.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 
24 namespace OHOS {
25 namespace MiscServices {
SystemCmdChannelProxy(const sptr<IRemoteObject> & object)26 SystemCmdChannelProxy::SystemCmdChannelProxy(const sptr<IRemoteObject> &object)
27     : IRemoteProxy<ISystemCmdChannel>(object)
28 {
29 }
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)30 int32_t SystemCmdChannelProxy::SendPrivateCommand(
31     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
32 {
33     return SendRequest(SEND_PRIVATE_COMMAND,
34         [&privateCommand](MessageParcel &parcel) { return ITypesUtil::Marshal(parcel, privateCommand); });
35 }
36 
NotifyPanelStatus(const SysPanelStatus & sysPanelStatus)37 int32_t SystemCmdChannelProxy::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus)
38 {
39     return SendRequest(
40         SHOULD_SYSTEM_PANEL_SHOW, [sysPanelStatus](MessageParcel &parcel) {
41             return ITypesUtil::Marshal(parcel, sysPanelStatus);
42         });
43 }
44 
SendRequest(int code,ParcelHandler input,ParcelHandler output)45 int32_t SystemCmdChannelProxy::SendRequest(int code, ParcelHandler input, ParcelHandler output)
46 {
47     IMSA_HILOGD("SystemCmdChannelProxy run in, code = %{public}d.", code);
48     MessageParcel data;
49     MessageParcel reply;
50     MessageOption option{ MessageOption::TF_ASYNC };
51     if (!data.WriteInterfaceToken(GetDescriptor())) {
52         IMSA_HILOGE("write interface token failed!");
53         return ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT;
54     }
55     if (input != nullptr && (!input(data))) {
56         IMSA_HILOGE("write data failed!");
57         return ErrorCode::ERROR_EX_PARCELABLE;
58     }
59     auto remote = Remote();
60     if (remote == nullptr) {
61         IMSA_HILOGE("remote is nullptr!");
62         return ErrorCode::ERROR_EX_NULL_POINTER;
63     }
64     auto ret = remote->SendRequest(code, data, reply, option);
65     if (ret != ErrorCode::NO_ERROR) {
66         IMSA_HILOGE("send request failed, code: %{public}d, ret: %{public}d!", code, ret);
67         return ret;
68     }
69     if (output != nullptr && (!output(reply))) {
70         IMSA_HILOGE("reply parcel error!");
71         return ErrorCode::ERROR_EX_PARCELABLE;
72     }
73     return ret;
74 }
75 } // namespace MiscServices
76 } // namespace OHOS