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_stub.h"
17 
18 #include "global.h"
19 #include "ime_system_channel.h"
20 #include "ipc_object_stub.h"
21 #include "ipc_skeleton.h"
22 #include "ipc_types.h"
23 #include "itypes_util.h"
24 #include "message.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
SystemCmdChannelStub()28 SystemCmdChannelStub::SystemCmdChannelStub()
29 {
30 }
31 
~SystemCmdChannelStub()32 SystemCmdChannelStub::~SystemCmdChannelStub()
33 {
34 }
35 
SendPrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)36 int32_t SystemCmdChannelStub::SendPrivateCommand(
37     const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
38 {
39     return ImeSystemCmdChannel::GetInstance()->ReceivePrivateCommand(privateCommand);
40 }
41 
SendPrivateCommandOnRemote(MessageParcel & data,MessageParcel & reply)42 int32_t SystemCmdChannelStub::SendPrivateCommandOnRemote(MessageParcel &data, MessageParcel &reply)
43 {
44     std::unordered_map<std::string, PrivateDataValue> privateCommand;
45     if (!ITypesUtil::Unmarshal(data, privateCommand)) {
46         IMSA_HILOGE("failed to read message parcel!");
47         return ErrorCode::ERROR_EX_PARCELABLE;
48     }
49     return reply.WriteInt32(SendPrivateCommand(privateCommand)) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;
50 }
51 
NotifyPanelStatus(const SysPanelStatus & sysPanelStatus)52 int32_t SystemCmdChannelStub::NotifyPanelStatus(const SysPanelStatus &sysPanelStatus)
53 {
54     return ImeSystemCmdChannel::GetInstance()->NotifyPanelStatus(sysPanelStatus);
55 }
56 
NotifyPanelStatusOnRemote(MessageParcel & data,MessageParcel & reply)57 int32_t SystemCmdChannelStub::NotifyPanelStatusOnRemote(MessageParcel &data, MessageParcel &reply)
58 {
59     SysPanelStatus sysPanelStatus;
60     if (!ITypesUtil::Unmarshal(data, sysPanelStatus)) {
61         IMSA_HILOGE("failed to read message parcel!");
62         return ErrorCode::ERROR_EX_PARCELABLE;
63     }
64     return reply.WriteInt32(NotifyPanelStatus(sysPanelStatus)) ? ErrorCode::NO_ERROR
65                                                                   : ErrorCode::ERROR_EX_PARCELABLE;
66 }
67 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)68 int32_t SystemCmdChannelStub::OnRemoteRequest(
69     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
70 {
71     IMSA_HILOGI("SystemCmdChannelStub, code: %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
72         IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
73     auto descriptorToken = data.ReadInterfaceToken();
74     if (descriptorToken != ISystemCmdChannel::GetDescriptor()) {
75         IMSA_HILOGE("SystemCmdChannelStub descriptor error!");
76         return ErrorCode::ERROR_STATUS_UNKNOWN_TRANSACTION;
77     }
78     if (code < SYSTEM_CMD_BEGIN || code >= SYSTEM_CMD_END) {
79         IMSA_HILOGE("code error, code = %{public}u, callingPid: %{public}d, callingUid: %{public}d.", code,
80             IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid());
81         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
82     }
83     return (this->*HANDLERS[code])(data, reply);
84 }
85 } // namespace MiscServices
86 } // namespace OHOS