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