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 "distributed_input_sink_proxy.h"
17
18 #include "dinput_errcode.h"
19 #include "dinput_ipc_interface_code.h"
20 #include "dinput_log.h"
21 #include "dinput_utils_tool.h"
22 #include "i_get_sink_screen_infos_call_back.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace DistributedInput {
DistributedInputSinkProxy(const sptr<IRemoteObject> & object)27 DistributedInputSinkProxy::DistributedInputSinkProxy(const sptr<IRemoteObject> &object)
28 : IRemoteProxy<IDistributedSinkInput>(object)
29 {}
30
~DistributedInputSinkProxy()31 DistributedInputSinkProxy::~DistributedInputSinkProxy()
32 {}
33
Init()34 int32_t DistributedInputSinkProxy::Init()
35 {
36 MessageParcel data;
37 MessageParcel reply;
38 if (!data.WriteInterfaceToken(GetDescriptor())) {
39 DHLOGE("DistributedInputSinkProxy write token valid failed");
40 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
41 }
42 int32_t result = ERR_DH_INPUT_SINK_PROXY_INIT_FAIL;
43 bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::INIT), data, reply);
44 if (!ret) {
45 DHLOGE("SendRequest fail!");
46 return ERR_DH_INPUT_SINK_PROXY_INIT_FAIL;
47 }
48 result = reply.ReadInt32();
49 return result;
50 }
51
Release()52 int32_t DistributedInputSinkProxy::Release()
53 {
54 MessageParcel data;
55 MessageParcel reply;
56 if (!data.WriteInterfaceToken(GetDescriptor())) {
57 DHLOGE("DistributedInputSinkProxy write token valid failed");
58 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
59 }
60 int32_t result = ERR_DH_INPUT_SINK_PROXY_RELEASE_FAIL;
61 bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::RELEASE), data, reply);
62 if (!ret) {
63 DHLOGE("SendRequest fail!");
64 return ERR_DH_INPUT_SINK_PROXY_RELEASE_FAIL;
65 }
66 result = reply.ReadInt32();
67 return result;
68 }
69
RegisterGetSinkScreenInfosCallback(sptr<IGetSinkScreenInfosCallback> callback)70 int32_t DistributedInputSinkProxy::RegisterGetSinkScreenInfosCallback(sptr<IGetSinkScreenInfosCallback> callback)
71 {
72 if (callback == nullptr) {
73 DHLOGE("getSinkScreenInfosCallback is null.");
74 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
75 }
76 MessageParcel data;
77 if (!data.WriteInterfaceToken(GetDescriptor())) {
78 DHLOGE("write token valid failed");
79 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
80 }
81 if (!data.WriteRemoteObject(callback->AsObject())) {
82 DHLOGE("write callback failed");
83 return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
84 }
85 MessageParcel reply;
86 int32_t result = ERR_DH_INPUT_SINK_PROXY_REGISTER_GETSINKSCREENINFOS_FAIL;
87 bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::GET_SINK_SCREEN_INFOS), data, reply);
88 if (ret) {
89 result = reply.ReadInt32();
90 }
91 return result;
92 }
93
NotifyStartDScreen(const SrcScreenInfo & remoteCtrlInfo)94 int32_t DistributedInputSinkProxy::NotifyStartDScreen(const SrcScreenInfo &remoteCtrlInfo)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98 if (!data.WriteInterfaceToken(GetDescriptor())) {
99 DHLOGE("WriteInterfaceToken fail!");
100 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
101 }
102 DHLOGI("DistributedInputSinkProxy the data: devId: %s, sourceWinId: %d, sourceWinWidth: %d, sourceWinHeight: %d, "
103 "sourcePhyId: %s, sourcePhyFd: %d, sourcePhyWidth: %d, sourcePhyHeight: %d",
104 GetAnonyString(remoteCtrlInfo.devId).c_str(), remoteCtrlInfo.sourceWinId, remoteCtrlInfo.sourceWinWidth,
105 remoteCtrlInfo.sourceWinHeight, GetAnonyString(remoteCtrlInfo.sourcePhyId).c_str(),
106 remoteCtrlInfo.sourcePhyFd, remoteCtrlInfo.sourcePhyWidth, remoteCtrlInfo.sourcePhyHeight);
107 if (!data.WriteString(remoteCtrlInfo.devId) || !data.WriteInt32(remoteCtrlInfo.sessionId) ||
108 !data.WriteString(remoteCtrlInfo.uuid) || !data.WriteUint64(remoteCtrlInfo.sourceWinId) ||
109 !data.WriteUint32(remoteCtrlInfo.sourceWinWidth) || !data.WriteUint32(remoteCtrlInfo.sourceWinHeight) ||
110 !data.WriteString(remoteCtrlInfo.sourcePhyId) || !data.WriteUint32(remoteCtrlInfo.sourcePhyFd) ||
111 !data.WriteUint32(remoteCtrlInfo.sourcePhyWidth) || !data.WriteUint32(remoteCtrlInfo.sourcePhyHeight)) {
112 DHLOGE("DistributedInputSinkProxy write params failed");
113 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
114 }
115 int32_t result = ERR_DH_INPUT_NOTIFY_START_DSCREEN_FAIL;
116 bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::NOTIFY_START_DSCREEN), data, reply);
117 if (!ret) {
118 DHLOGE("SendRequest fail!");
119 return ERR_DH_INPUT_NOTIFY_START_DSCREEN_FAIL;
120 }
121 result = reply.ReadInt32();
122 return result;
123 }
124
NotifyStopDScreen(const std::string & srcScreenInfoKey)125 int32_t DistributedInputSinkProxy::NotifyStopDScreen(const std::string &srcScreenInfoKey)
126 {
127 MessageParcel data;
128 MessageParcel reply;
129 if (!data.WriteInterfaceToken(GetDescriptor())) {
130 DHLOGE("WriteInterfaceToken fail!");
131 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
132 }
133 if (!data.WriteString(srcScreenInfoKey)) {
134 DHLOGE("DistributedInputSinkProxy write params failed");
135 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
136 }
137 int32_t result = ERR_DH_INPUT_NOTIFY_STOP_DSCREEN_FAIL;
138 bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::NOTIFY_STOP_DSCREEN), data, reply);
139 if (!ret) {
140 DHLOGE("SendRequest fail!");
141 return ERR_DH_INPUT_NOTIFY_STOP_DSCREEN_FAIL;
142 }
143 result = reply.ReadInt32();
144 return result;
145 }
146
RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener)147 int32_t DistributedInputSinkProxy::RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener)
148 {
149 MessageParcel data;
150 if (!data.WriteInterfaceToken(GetDescriptor())) {
151 DHLOGE("RegisterSharingDhIdListener write token valid failed");
152 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
153 }
154 if (!data.WriteRemoteObject(sharingDhIdListener->AsObject())) {
155 DHLOGE("RegisterSharingDhIdListener write callback failed");
156 return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
157 }
158
159 MessageParcel reply;
160 int32_t result = ERR_DH_INPUT_SINK_PROXY_REGISTER_SHARING_DHID_LISTENER_FAIL;
161 bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::REGISTER_SHARING_DHID_LISTENER),
162 data, reply);
163 if (ret) {
164 result = reply.ReadInt32();
165 }
166 return result;
167 }
168
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)169 bool DistributedInputSinkProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
170 {
171 sptr<IRemoteObject> remote = Remote();
172 if (remote == nullptr) {
173 return false;
174 }
175 MessageOption option(MessageOption::TF_SYNC);
176 int32_t result = remote->SendRequest(code, data, reply, option);
177 if (result != DH_SUCCESS) {
178 return false;
179 }
180 return true;
181 }
182 } // namespace DistributedInput
183 } // namespace DistributedHardware
184 } // namespace OHOS
185