1 /*
2  * Copyright (c) 2021-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 "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("DinputSinkProxy the data: devId: %{public}s, sourceWinId: %{public}" PRIu64 ", sourceWinWidth: %{public}d, "
103         "sourceWinHeight: %{public}d, sourcePhyId: %{public}s, sourcePhyFd: %{public}d, sourcePhyWidth: %{public}d, "
104         "sourcePhyHeight: %{public}d", GetAnonyString(remoteCtrlInfo.devId).c_str(), remoteCtrlInfo.sourceWinId,
105         remoteCtrlInfo.sourceWinWidth, remoteCtrlInfo.sourceWinHeight,
106         GetAnonyString(remoteCtrlInfo.sourcePhyId).c_str(), remoteCtrlInfo.sourcePhyFd,
107         remoteCtrlInfo.sourcePhyWidth, remoteCtrlInfo.sourcePhyHeight);
108     if (!data.WriteString(remoteCtrlInfo.devId) || !data.WriteInt32(remoteCtrlInfo.sessionId) ||
109         !data.WriteString(remoteCtrlInfo.uuid) || !data.WriteUint64(remoteCtrlInfo.sourceWinId) ||
110         !data.WriteUint32(remoteCtrlInfo.sourceWinWidth) || !data.WriteUint32(remoteCtrlInfo.sourceWinHeight) ||
111         !data.WriteString(remoteCtrlInfo.sourcePhyId) || !data.WriteUint32(remoteCtrlInfo.sourcePhyFd) ||
112         !data.WriteUint32(remoteCtrlInfo.sourcePhyWidth) || !data.WriteUint32(remoteCtrlInfo.sourcePhyHeight)) {
113         DHLOGE("DistributedInputSinkProxy write params failed");
114         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
115     }
116     int32_t result = ERR_DH_INPUT_NOTIFY_START_DSCREEN_FAIL;
117     bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::NOTIFY_START_DSCREEN), data, reply);
118     if (!ret) {
119         DHLOGE("SendRequest fail!");
120         return ERR_DH_INPUT_NOTIFY_START_DSCREEN_FAIL;
121     }
122     result = reply.ReadInt32();
123     return result;
124 }
125 
NotifyStopDScreen(const std::string & srcScreenInfoKey)126 int32_t DistributedInputSinkProxy::NotifyStopDScreen(const std::string &srcScreenInfoKey)
127 {
128     MessageParcel data;
129     MessageParcel reply;
130     if (!data.WriteInterfaceToken(GetDescriptor())) {
131         DHLOGE("WriteInterfaceToken fail!");
132         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
133     }
134     if (!data.WriteString(srcScreenInfoKey)) {
135         DHLOGE("DistributedInputSinkProxy write params failed");
136         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
137     }
138     int32_t result = ERR_DH_INPUT_NOTIFY_STOP_DSCREEN_FAIL;
139     bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::NOTIFY_STOP_DSCREEN), data, reply);
140     if (!ret) {
141         DHLOGE("SendRequest fail!");
142         return ERR_DH_INPUT_NOTIFY_STOP_DSCREEN_FAIL;
143     }
144     result = reply.ReadInt32();
145     return result;
146 }
147 
RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener)148 int32_t DistributedInputSinkProxy::RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener)
149 {
150     if (sharingDhIdListener == nullptr) {
151         DHLOGE("sharingDhIdListener is nullptr");
152         return ERR_DH_INPUT_SINK_PROXY_DH_LISTENER_IS_NULL;
153     }
154     MessageParcel data;
155     if (!data.WriteInterfaceToken(GetDescriptor())) {
156         DHLOGE("RegisterSharingDhIdListener write token valid failed");
157         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
158     }
159     if (!data.WriteRemoteObject(sharingDhIdListener->AsObject())) {
160         DHLOGE("RegisterSharingDhIdListener write callback failed");
161         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
162     }
163 
164     MessageParcel reply;
165     int32_t result = ERR_DH_INPUT_SINK_PROXY_REGISTER_SHARING_DHID_LISTENER_FAIL;
166     bool ret = SendRequest(static_cast<uint32_t>(IDInputSinkInterfaceCode::REGISTER_SHARING_DHID_LISTENER),
167         data, reply);
168     if (ret) {
169         result = reply.ReadInt32();
170     }
171     return result;
172 }
173 
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)174 bool DistributedInputSinkProxy::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
175 {
176     sptr<IRemoteObject> remote = Remote();
177     if (remote == nullptr) {
178         return false;
179     }
180     MessageOption option(MessageOption::TF_SYNC);
181     int32_t result = remote->SendRequest(code, data, reply, option);
182     if (result != DH_SUCCESS) {
183         return false;
184     }
185     return true;
186 }
187 } // namespace DistributedInput
188 } // namespace DistributedHardware
189 } // namespace OHOS
190