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_stub.h"
17
18 #include "accesstoken_kit.h"
19 #include "constants_dinput.h"
20 #include "dinput_errcode.h"
21 #include "dinput_ipc_interface_code.h"
22 #include "dinput_log.h"
23 #include "dinput_utils_tool.h"
24 #include "i_sharing_dhid_listener.h"
25 #include "ipc_skeleton.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace DistributedInput {
DistributedInputSinkStub()30 DistributedInputSinkStub::DistributedInputSinkStub()
31 {
32 DHLOGI("DistributedInputSinkStub ctor!");
33 }
34
~DistributedInputSinkStub()35 DistributedInputSinkStub::~DistributedInputSinkStub()
36 {
37 DHLOGI("DistributedInputSinkStub dtor!");
38 }
39
HasEnableDHPermission()40 bool DistributedInputSinkStub::HasEnableDHPermission()
41 {
42 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
43 const std::string permissionName = "ohos.permission.ENABLE_DISTRIBUTED_HARDWARE";
44 int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
45 permissionName);
46 return (result == Security::AccessToken::PERMISSION_GRANTED);
47 }
48
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 int32_t DistributedInputSinkStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
50 MessageOption &option)
51 {
52 if (data.ReadInterfaceToken() != GetDescriptor()) {
53 DHLOGE("DistributedInputSinkStub read token valid failed");
54 return ERR_DH_INPUT_IPC_READ_TOKEN_VALID_FAIL;
55 }
56 switch (code) {
57 case static_cast<uint32_t>(IDInputSinkInterfaceCode::INIT):
58 return InitInner(data, reply, option);
59 case static_cast<uint32_t>(IDInputSinkInterfaceCode::RELEASE):
60 return ReleaseInner(data, reply, option);
61 case static_cast<uint32_t>(IDInputSinkInterfaceCode::NOTIFY_START_DSCREEN):
62 return NotifyStartDScreenInner(data, reply, option);
63 case static_cast<uint32_t>(IDInputSinkInterfaceCode::NOTIFY_STOP_DSCREEN):
64 return NotifyStopDScreenInner(data, reply, option);
65 case static_cast<uint32_t>(IDInputSinkInterfaceCode::REGISTER_SHARING_DHID_LISTENER):
66 return RegisterSharingDhIdListenerInner(data, reply, option);
67 case static_cast<uint32_t>(IDInputSinkInterfaceCode::GET_SINK_SCREEN_INFOS):
68 return RegisterGetSinkScreenInfosInner(data, reply, option);
69 default:
70 DHLOGE("invalid request code is %{public}u.", code);
71 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 }
73 }
74
InitInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)75 int32_t DistributedInputSinkStub::InitInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
76 {
77 if (!HasEnableDHPermission()) {
78 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
79 return ERR_DH_INPUT_SINK_ENABLE_PERMISSION_CHECK_FAIL;
80 }
81 DHLOGI("DistributedInputSinkStub InitInner start");
82 int32_t ret = Init();
83 if (!reply.WriteInt32(ret)) {
84 DHLOGE("DistributedInputSinkStub write ret failed, ret = %{public}d", ret);
85 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
86 }
87 return ret;
88 }
89
ReleaseInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)90 int32_t DistributedInputSinkStub::ReleaseInner(MessageParcel &data, MessageParcel &reply, MessageOption &option)
91 {
92 if (!HasEnableDHPermission()) {
93 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
94 return ERR_DH_INPUT_SINK_ENABLE_PERMISSION_CHECK_FAIL;
95 }
96 int32_t ret = Release();
97 if (!reply.WriteInt32(ret)) {
98 DHLOGE("DistributedInputSinkStub write ret failed, ret = %{public}d", ret);
99 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
100 }
101 return ret;
102 }
103
NotifyStartDScreenInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)104 int32_t DistributedInputSinkStub::NotifyStartDScreenInner(MessageParcel &data, MessageParcel &reply,
105 MessageOption &option)
106 {
107 std::string devId = data.ReadString();
108 int32_t sessionId = data.ReadInt32();
109 std::string uuid = data.ReadString();
110 uint64_t sourceWinId = data.ReadUint64();
111 uint32_t sourceWinWidth = data.ReadUint32();
112 uint32_t sourceWinHeight = data.ReadUint32();
113 std::string sourcePhyId = data.ReadString();
114 uint32_t sourcePhyFd = data.ReadUint32();
115 uint32_t sourcePhyWidth = data.ReadUint32();
116 uint32_t sourcePhyHeight = data.ReadUint32();
117 DHLOGI("OnRemoteRequest the data: devId: %{public}s, sourceWinId: %{public}" PRIu64 ", sourceWinWidth: %{public}d, "
118 "sourceWinHeight: %{public}d, sourcePhyId: %{public}s, sourcePhyFd: %{public}d, sourcePhyWidth: %{public}d, "
119 "sourcePhyHeight: %{public}d", GetAnonyString(devId).c_str(), sourceWinId, sourceWinWidth, sourceWinHeight,
120 GetAnonyString(sourcePhyId).c_str(), sourcePhyFd, sourcePhyWidth, sourcePhyHeight);
121 SrcScreenInfo srcScreenInfo = {
122 .devId = devId,
123 .uuid = uuid,
124 .sessionId = sessionId,
125 .sourceWinId = sourceWinId,
126 .sourceWinWidth = sourceWinWidth,
127 .sourceWinHeight = sourceWinHeight,
128 .sourcePhyId = sourcePhyId,
129 .sourcePhyFd = sourcePhyFd,
130 .sourcePhyWidth = sourcePhyWidth,
131 .sourcePhyHeight = sourcePhyHeight,
132 };
133 int32_t ret = NotifyStartDScreen(srcScreenInfo);
134 if (!reply.WriteInt32(ret)) {
135 DHLOGE("write reply failed ret = %{public}d", ret);
136 return ERR_DH_INPUT_RPC_REPLY_FAIL;
137 }
138 return ret;
139 }
140
NotifyStopDScreenInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)141 int32_t DistributedInputSinkStub::NotifyStopDScreenInner(MessageParcel &data, MessageParcel &reply,
142 MessageOption &option)
143 {
144 std::string srcScreenInfoKey = data.ReadString();
145 DHLOGI("OnRemoteRequest srcScreenInfoKey: %{public}s", GetAnonyString(srcScreenInfoKey).c_str());
146 int ret = NotifyStopDScreen(srcScreenInfoKey);
147 if (!reply.WriteInt32(ret)) {
148 DHLOGE("write version failed, ret = %{public}d", ret);
149 return ERR_DH_INPUT_RPC_REPLY_FAIL;
150 }
151 return ret;
152 }
153
RegisterSharingDhIdListenerInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)154 int32_t DistributedInputSinkStub::RegisterSharingDhIdListenerInner(MessageParcel &data, MessageParcel &reply,
155 MessageOption &option)
156 {
157 sptr<ISharingDhIdListener> listener = iface_cast<ISharingDhIdListener>(data.ReadRemoteObject());
158 if (listener == nullptr) {
159 DHLOGE("RegisterSharingDhIdListenerInner failed, listener is nullptr.");
160 return ERR_DH_INPUT_POINTER_NULL;
161 }
162 int32_t ret = RegisterSharingDhIdListener(listener);
163 if (!reply.WriteInt32(ret)) {
164 DHLOGE("RegisterSharingDhIdListenerInner write ret failed, ret = %{public}d", ret);
165 return ERR_DH_INPUT_SINK_STUB_REGISTER_SHARING_DHID_LISTENER_FAIL;
166 }
167
168 return DH_SUCCESS;
169 }
170
RegisterGetSinkScreenInfosInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)171 int32_t DistributedInputSinkStub::RegisterGetSinkScreenInfosInner(MessageParcel &data, MessageParcel &reply,
172 MessageOption &option)
173 {
174 sptr<IGetSinkScreenInfosCallback> callback =
175 iface_cast<IGetSinkScreenInfosCallback>(data.ReadRemoteObject());
176 if (callback == nullptr) {
177 DHLOGE("RegisterGetSinkScreenInfosInner failed, callback is nullptr.");
178 return ERR_DH_INPUT_POINTER_NULL;
179 }
180 int32_t ret = RegisterGetSinkScreenInfosCallback(callback);
181 if (!reply.WriteInt32(ret)) {
182 DHLOGE("write ret failed, ret = %{public}d", ret);
183 return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
184 }
185 return ret;
186 }
187 } // namespace DistributedInput
188 } // namespace DistributedHardware
189 } // namespace OHOS
190