1 /*
2 * Copyright (c) 2021-2025 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_hardware_stub.h"
17
18 #include <cinttypes>
19
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "tokenid_kit.h"
23
24 #include "device_manager.h"
25
26 #include "anonymous_string.h"
27 #include "constants.h"
28 #include "dhardware_ipc_interface_code.h"
29 #include "dh_context.h"
30 #include "dh_utils_tool.h"
31 #include "distributed_hardware_errno.h"
32 #include "distributed_hardware_log.h"
33 #include "hdf_operate.h"
34 #include "publisher_listener_proxy.h"
35 #include "av_trans_errno.h"
36
37 namespace OHOS {
38 namespace DistributedHardware {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t DistributedHardwareStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
40 MessageOption &option)
41 {
42 if (data.ReadInterfaceToken() != GetDescriptor()) {
43 DHLOGE("IPC Token valid fail!");
44 return ERR_INVALID_DATA;
45 }
46 if (code != static_cast<uint32_t>(DHMsgInterfaceCode::NOTIFY_SOURCE_DEVICE_REMOTE_DMSDP_STARTED)) {
47 if (!IPCSkeleton::IsLocalCalling()) {
48 DHLOGE("Invalid request, only support local, code = %{public}u.", code);
49 return ERR_DH_FWK_IS_LOCAL_PROCESS_FAIL;
50 }
51 }
52
53 switch (code) {
54 case static_cast<uint32_t>(DHMsgInterfaceCode::REG_PUBLISHER_LISTNER): {
55 return RegisterPublisherListenerInner(data, reply);
56 }
57 case static_cast<uint32_t>(DHMsgInterfaceCode::UNREG_PUBLISHER_LISTENER): {
58 return UnregisterPublisherListenerInner(data, reply);
59 }
60 case static_cast<uint32_t>(DHMsgInterfaceCode::PUBLISH_MESSAGE): {
61 return PublishMessageInner(data, reply);
62 }
63 case static_cast<uint32_t>(DHMsgInterfaceCode::INIT_CTL_CEN): {
64 return InitializeAVCenterInner(data, reply);
65 }
66 case static_cast<uint32_t>(DHMsgInterfaceCode::RELEASE_CTL_CEN): {
67 return ReleaseAVCenterInner(data, reply);
68 }
69 case static_cast<uint32_t>(DHMsgInterfaceCode::CREATE_CTL_CEN_CHANNEL): {
70 return CreateControlChannelInner(data, reply);
71 }
72 case static_cast<uint32_t>(DHMsgInterfaceCode::NOTIFY_AV_EVENT): {
73 return NotifyAVCenterInner(data, reply);
74 }
75 case static_cast<uint32_t>(DHMsgInterfaceCode::REGISTER_CTL_CEN_CALLBACK): {
76 return RegisterControlCenterCallbackInner(data, reply);
77 }
78 case static_cast<uint32_t>(DHMsgInterfaceCode::QUERY_LOCAL_SYS_SPEC): {
79 return QueryLocalSysSpecInner(data, reply);
80 }
81 case static_cast<uint32_t>(DHMsgInterfaceCode::NOTIFY_SOURCE_DEVICE_REMOTE_DMSDP_STARTED): {
82 return HandleNotifySourceRemoteSinkStarted(data, reply);
83 }
84 default:
85 return OnRemoteRequestEx(code, data, reply, option);
86 }
87 return DH_FWK_SUCCESS;
88 }
89
RegisterPublisherListenerInner(MessageParcel & data,MessageParcel & reply)90 int32_t DistributedHardwareStub::RegisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply)
91 {
92 if (!HasAccessDHPermission()) {
93 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
94 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
95 }
96
97 uint32_t topicInt = data.ReadUint32();
98 if (!ValidTopic(topicInt)) {
99 DHLOGE("Topic invalid: %{public}" PRIu32, topicInt);
100 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
101 return ERR_DH_FWK_PARA_INVALID;
102 }
103
104 DHTopic topic = (DHTopic)topicInt;
105 sptr<IPublisherListener> listener = iface_cast<IPublisherListener>(data.ReadRemoteObject());
106 if (listener == nullptr) {
107 DHLOGE("Register publisher listener is null");
108 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
109 return ERR_DH_FWK_PARA_INVALID;
110 }
111 DHLOGI("Register listener, topic: %{public}" PRIu32, (uint32_t)topic);
112 RegisterPublisherListener(topic, listener);
113 reply.WriteInt32(DH_FWK_SUCCESS);
114 return DH_FWK_SUCCESS;
115 }
116
UnregisterPublisherListenerInner(MessageParcel & data,MessageParcel & reply)117 int32_t DistributedHardwareStub::UnregisterPublisherListenerInner(MessageParcel &data, MessageParcel &reply)
118 {
119 if (!HasAccessDHPermission()) {
120 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
121 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
122 }
123
124 uint32_t topicInt = data.ReadUint32();
125 if (!ValidTopic(topicInt)) {
126 DHLOGE("Topic invalid: %{public}" PRIu32, topicInt);
127 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
128 return ERR_DH_FWK_PARA_INVALID;
129 }
130
131 DHTopic topic = (DHTopic)topicInt;
132 sptr<IPublisherListener> listener = iface_cast<IPublisherListener>(data.ReadRemoteObject());
133 if (listener == nullptr) {
134 DHLOGE("Unregister publisher listener is null");
135 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
136 return ERR_DH_FWK_PARA_INVALID;
137 }
138 DHLOGI("Unregister listener, topic: %{public}" PRIu32, (uint32_t)topic);
139 UnregisterPublisherListener(topic, listener);
140 reply.WriteInt32(DH_FWK_SUCCESS);
141 return DH_FWK_SUCCESS;
142 }
143
PublishMessageInner(MessageParcel & data,MessageParcel & reply)144 int32_t DistributedHardwareStub::PublishMessageInner(MessageParcel &data, MessageParcel &reply)
145 {
146 if (!HasAccessDHPermission()) {
147 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
148 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
149 }
150
151 uint32_t topicInt = data.ReadUint32();
152 if (!ValidTopic(topicInt)) {
153 DHLOGE("Topic invalid: %{public}" PRIu32, topicInt);
154 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
155 return ERR_DH_FWK_PARA_INVALID;
156 }
157
158 DHTopic topic = (DHTopic)topicInt;
159 std::string message = data.ReadString();
160 DHLOGI("Publish message, topic: %{public}" PRIu32, (uint32_t)topic);
161 PublishMessage(topic, message);
162 reply.WriteInt32(DH_FWK_SUCCESS);
163 return DH_FWK_SUCCESS;
164 }
165
QueryLocalSysSpecInner(MessageParcel & data,MessageParcel & reply)166 int32_t DistributedHardwareStub::QueryLocalSysSpecInner(MessageParcel &data, MessageParcel &reply)
167 {
168 if (!HasAccessDHPermission()) {
169 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
170 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
171 }
172
173 uint32_t specInt = data.ReadUint32();
174 if (!ValidQueryLocalSpec(specInt)) {
175 DHLOGE("Spec invalid: %{public}" PRIu32, specInt);
176 reply.WriteInt32(ERR_DH_FWK_PARA_INVALID);
177 return ERR_DH_FWK_PARA_INVALID;
178 }
179
180 QueryLocalSysSpecType spec = (QueryLocalSysSpecType)specInt;
181 DHLOGI("Query Local Sys Spec: %{public}" PRIu32, (uint32_t)spec);
182 std::string res = QueryLocalSysSpec(spec);
183 DHLOGI("Get Local spec: %{public}s", res.c_str());
184 reply.WriteString(res);
185 return DH_FWK_SUCCESS;
186 }
187
InitializeAVCenterInner(MessageParcel & data,MessageParcel & reply)188 int32_t DistributedHardwareStub::InitializeAVCenterInner(MessageParcel &data, MessageParcel &reply)
189 {
190 if (!HasAccessDHPermission()) {
191 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
192 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
193 }
194
195 TransRole transRole = (TransRole)(data.ReadUint32());
196 int32_t engineId = 0;
197 int32_t ret = InitializeAVCenter(transRole, engineId);
198 if (!reply.WriteInt32(engineId)) {
199 DHLOGE("Write engine id failed");
200 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
201 }
202 if (!reply.WriteInt32(ret)) {
203 DHLOGE("Write ret code failed");
204 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
205 }
206 return DH_FWK_SUCCESS;
207 }
208
ReleaseAVCenterInner(MessageParcel & data,MessageParcel & reply)209 int32_t DistributedHardwareStub::ReleaseAVCenterInner(MessageParcel &data, MessageParcel &reply)
210 {
211 if (!HasAccessDHPermission()) {
212 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
213 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
214 }
215
216 int32_t engineId = data.ReadInt32();
217 int32_t ret = ReleaseAVCenter(engineId);
218 if (!reply.WriteInt32(ret)) {
219 DHLOGE("Write ret code failed");
220 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
221 }
222 return DH_FWK_SUCCESS;
223 }
224
CreateControlChannelInner(MessageParcel & data,MessageParcel & reply)225 int32_t DistributedHardwareStub::CreateControlChannelInner(MessageParcel &data, MessageParcel &reply)
226 {
227 if (!HasAccessDHPermission()) {
228 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
229 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
230 }
231
232 int32_t engineId = data.ReadInt32();
233 std::string peerDevId = data.ReadString();
234 int32_t ret = CreateControlChannel(engineId, peerDevId);
235 if (!reply.WriteInt32(ret)) {
236 DHLOGE("Write ret code failed");
237 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
238 }
239 return DH_FWK_SUCCESS;
240 }
241
NotifyAVCenterInner(MessageParcel & data,MessageParcel & reply)242 int32_t DistributedHardwareStub::NotifyAVCenterInner(MessageParcel &data, MessageParcel &reply)
243 {
244 if (!HasAccessDHPermission()) {
245 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
246 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
247 }
248
249 int32_t engineId = data.ReadInt32();
250 uint32_t type = data.ReadUint32();
251 std::string content = data.ReadString();
252 std::string peerDevId = data.ReadString();
253 int32_t ret = NotifyAVCenter(engineId, AVTransEvent{ (EventType)type, content, peerDevId });
254 if (!reply.WriteInt32(ret)) {
255 DHLOGE("Write ret code failed");
256 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
257 }
258 return DH_FWK_SUCCESS;
259 }
260
RegisterControlCenterCallbackInner(MessageParcel & data,MessageParcel & reply)261 int32_t DistributedHardwareStub::RegisterControlCenterCallbackInner(MessageParcel &data, MessageParcel &reply)
262 {
263 if (!HasAccessDHPermission()) {
264 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
265 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
266 }
267
268 int32_t engineId = data.ReadInt32();
269 sptr<IAvTransControlCenterCallback> callback = iface_cast<IAvTransControlCenterCallback>(data.ReadRemoteObject());
270 if (callback == nullptr) {
271 DHLOGE("Input av control center callback is null");
272 return ERR_DH_FWK_PARA_INVALID;
273 }
274
275 int32_t ret = RegisterCtlCenterCallback(engineId, callback);
276 if (!reply.WriteInt32(ret)) {
277 DHLOGE("Write ret code failed");
278 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
279 }
280 return DH_FWK_SUCCESS;
281 }
282
HandleNotifySourceRemoteSinkStarted(MessageParcel & data,MessageParcel & reply)283 int32_t OHOS::DistributedHardware::DistributedHardwareStub::HandleNotifySourceRemoteSinkStarted(MessageParcel &data,
284 MessageParcel &reply)
285 {
286 DHLOGI("HandleNotifySourceRemoteSinkStarted Start.");
287 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetDCallingTokenID();
288 std::string udid = data.ReadString();
289 if (!IsIdLengthValid(udid)) {
290 DHLOGE("the udid: %{public}s is invalid.", GetAnonyString(udid).c_str());
291 return ERR_DH_FWK_PARA_INVALID;
292 }
293 std::string networkId = "";
294 DeviceManager::GetInstance().GetNetworkIdByUdid(DH_FWK_PKG_NAME, udid, networkId);
295 if (!IsIdLengthValid(networkId)) {
296 DHLOGE("the networkId: %{public}s is invalid, not a trusted device.", GetAnonyString(networkId).c_str());
297 return ERR_DH_FWK_PARA_INVALID;
298 }
299 uint32_t dAccessToken = Security::AccessToken::AccessTokenKit::AllocLocalTokenID(networkId, callerToken);
300 const std::string permissionName = "ohos.permission.ACCESS_DISTRIBUTED_HARDWARE";
301 int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(dAccessToken, permissionName);
302 if (result != Security::AccessToken::PERMISSION_GRANTED) {
303 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
304 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
305 }
306
307 int32_t ret = NotifySourceRemoteSinkStarted(udid);
308 if (!reply.WriteInt32(ret)) {
309 DHLOGE("write ret failed.");
310 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
311 }
312 DHLOGI("HandleNotifySourceRemoteSinkStarted End.");
313 return DH_FWK_SUCCESS;
314 }
315
ValidTopic(uint32_t topic)316 bool DistributedHardwareStub::ValidTopic(uint32_t topic)
317 {
318 if (topic <= (uint32_t)DHTopic::TOPIC_MIN || topic >= (uint32_t)DHTopic::TOPIC_MAX) {
319 return false;
320 }
321 return true;
322 }
323
ValidQueryLocalSpec(uint32_t spec)324 bool DistributedHardwareStub::ValidQueryLocalSpec(uint32_t spec)
325 {
326 if (spec <= (uint32_t)QueryLocalSysSpecType::MIN || spec >= (uint32_t)QueryLocalSysSpecType::MAX) {
327 return false;
328 }
329 return true;
330 }
331
PauseDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)332 int32_t DistributedHardwareStub::PauseDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
333 {
334 if (!IsSystemHap()) {
335 DHLOGE("GetCallerProcessName not system hap.");
336 return ERR_DH_FWK_IS_SYSTEM_HAP_CHECK_FAIL;
337 }
338 if (!HasAccessDHPermission()) {
339 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
340 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
341 }
342 DHType dhType = static_cast<DHType>(data.ReadInt32());
343 std::string networkId = data.ReadString();
344 int32_t ret = PauseDistributedHardware(dhType, networkId);
345 if (!reply.WriteInt32(ret)) {
346 DHLOGE("Write ret code failed");
347 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
348 }
349 return DH_FWK_SUCCESS;
350 }
351
ResumeDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)352 int32_t DistributedHardwareStub::ResumeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
353 {
354 if (!IsSystemHap()) {
355 DHLOGE("GetCallerProcessName not system hap.");
356 return ERR_DH_FWK_IS_SYSTEM_HAP_CHECK_FAIL;
357 }
358 if (!HasAccessDHPermission()) {
359 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
360 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
361 }
362 DHType dhType = static_cast<DHType>(data.ReadInt32());
363 std::string networkId = data.ReadString();
364 int32_t ret = ResumeDistributedHardware(dhType, networkId);
365 if (!reply.WriteInt32(ret)) {
366 DHLOGE("Write ret code failed");
367 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
368 }
369 return DH_FWK_SUCCESS;
370 }
371
StopDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)372 int32_t DistributedHardwareStub::StopDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
373 {
374 if (!IsSystemHap()) {
375 DHLOGE("GetCallerProcessName not system hap.");
376 return ERR_DH_FWK_IS_SYSTEM_HAP_CHECK_FAIL;
377 }
378 if (!HasAccessDHPermission()) {
379 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
380 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
381 }
382 DHType dhType = static_cast<DHType>(data.ReadInt32());
383 std::string networkId = data.ReadString();
384 int32_t ret = StopDistributedHardware(dhType, networkId);
385 if (!reply.WriteInt32(ret)) {
386 DHLOGE("Write ret code failed");
387 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
388 }
389 return DH_FWK_SUCCESS;
390 }
391
GetDistributedHardwareInner(MessageParcel & data,MessageParcel & reply)392 int32_t DistributedHardwareStub::GetDistributedHardwareInner(MessageParcel &data, MessageParcel &reply)
393 {
394 if (!HasAccessDHPermission()) {
395 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
396 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
397 }
398 std::string networkId = data.ReadString();
399 EnableStep enableStep = static_cast<EnableStep>(data.ReadUint32());
400 sptr<IGetDhDescriptorsCallback> callback =
401 iface_cast<IGetDhDescriptorsCallback>(data.ReadRemoteObject());
402 if (callback == nullptr) {
403 DHLOGE("Input get distributed hardware callback is null!");
404 return ERR_DH_FWK_PARA_INVALID;
405 }
406 int32_t ret = GetDistributedHardware(networkId, enableStep, callback);
407 if (!reply.WriteInt32(ret)) {
408 DHLOGE("Write ret code failed!");
409 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
410 }
411 return DH_FWK_SUCCESS;
412 }
413
RegisterDHStatusSinkListenerInner(MessageParcel & data,MessageParcel & reply)414 int32_t DistributedHardwareStub::RegisterDHStatusSinkListenerInner(MessageParcel &data, MessageParcel &reply)
415 {
416 if (!HasAccessDHPermission()) {
417 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
418 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
419 }
420 sptr<IHDSinkStatusListener> listener = iface_cast<IHDSinkStatusListener>(data.ReadRemoteObject());
421 if (listener == nullptr) {
422 DHLOGE("Input distributed hardware status sink listener is null");
423 return ERR_DH_FWK_PARA_INVALID;
424 }
425 int32_t ret = RegisterDHStatusListener(listener);
426 if (!reply.WriteInt32(ret)) {
427 DHLOGE("Write ret code failed!");
428 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
429 }
430 return DH_FWK_SUCCESS;
431 }
432
UnregisterDHStatusSinkListenerInner(MessageParcel & data,MessageParcel & reply)433 int32_t DistributedHardwareStub::UnregisterDHStatusSinkListenerInner(MessageParcel &data, MessageParcel &reply)
434 {
435 if (!HasAccessDHPermission()) {
436 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
437 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
438 }
439 sptr<IHDSinkStatusListener> listener = iface_cast<IHDSinkStatusListener>(data.ReadRemoteObject());
440 if (listener == nullptr) {
441 DHLOGE("Input distributed hardware status sink listener is null");
442 return ERR_DH_FWK_PARA_INVALID;
443 }
444 int32_t ret = UnregisterDHStatusListener(listener);
445 if (!reply.WriteInt32(ret)) {
446 DHLOGE("Write ret code failed!");
447 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
448 }
449 return DH_FWK_SUCCESS;
450 }
451
RegisterDHStatusSourceListenerInner(MessageParcel & data,MessageParcel & reply)452 int32_t DistributedHardwareStub::RegisterDHStatusSourceListenerInner(MessageParcel &data, MessageParcel &reply)
453 {
454 if (!HasAccessDHPermission()) {
455 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
456 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
457 }
458 std::string networkId = data.ReadString();
459 sptr<IHDSourceStatusListener> listener = iface_cast<IHDSourceStatusListener>(data.ReadRemoteObject());
460 if (listener == nullptr) {
461 DHLOGE("Input distributed hardware status source listener is null");
462 return ERR_DH_FWK_PARA_INVALID;
463 }
464 int32_t ret = RegisterDHStatusListener(networkId, listener);
465 if (!reply.WriteInt32(ret)) {
466 DHLOGE("Write ret code failed!");
467 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
468 }
469 return DH_FWK_SUCCESS;
470 }
471
UnregisterDHStatusSourceListenerInner(MessageParcel & data,MessageParcel & reply)472 int32_t DistributedHardwareStub::UnregisterDHStatusSourceListenerInner(MessageParcel &data, MessageParcel &reply)
473 {
474 if (!HasAccessDHPermission()) {
475 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
476 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
477 }
478 std::string networkId = data.ReadString();
479 sptr<IHDSourceStatusListener> listener = iface_cast<IHDSourceStatusListener>(data.ReadRemoteObject());
480 if (listener == nullptr) {
481 DHLOGE("Input distributed hardware status source listener is null");
482 return ERR_DH_FWK_PARA_INVALID;
483 }
484 int32_t ret = UnregisterDHStatusListener(networkId, listener);
485 if (!reply.WriteInt32(ret)) {
486 DHLOGE("Write ret code failed!");
487 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
488 }
489 return DH_FWK_SUCCESS;
490 }
491
EnableSinkInner(MessageParcel & data,MessageParcel & reply)492 int32_t DistributedHardwareStub::EnableSinkInner(MessageParcel &data, MessageParcel &reply)
493 {
494 if (!HasAccessDHPermission()) {
495 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
496 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
497 }
498 std::vector<DHDescriptor> descriptors;
499 ReadDescriptors(data, descriptors);
500 int32_t ret = EnableSink(descriptors);
501 if (!reply.WriteInt32(ret)) {
502 DHLOGE("Write ret code failed!");
503 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
504 }
505 return DH_FWK_SUCCESS;
506 }
507
DisableSinkInner(MessageParcel & data,MessageParcel & reply)508 int32_t DistributedHardwareStub::DisableSinkInner(MessageParcel &data, MessageParcel &reply)
509 {
510 if (!HasAccessDHPermission()) {
511 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
512 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
513 }
514 std::vector<DHDescriptor> descriptors;
515 ReadDescriptors(data, descriptors);
516 int32_t ret = DisableSink(descriptors);
517 if (!reply.WriteInt32(ret)) {
518 DHLOGE("Write ret code failed!");
519 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
520 }
521 return DH_FWK_SUCCESS;
522 }
523
EnableSourceInner(MessageParcel & data,MessageParcel & reply)524 int32_t DistributedHardwareStub::EnableSourceInner(MessageParcel &data, MessageParcel &reply)
525 {
526 if (!HasAccessDHPermission()) {
527 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
528 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
529 }
530 std::string networkId = data.ReadString();
531 std::vector<DHDescriptor> descriptors;
532 ReadDescriptors(data, descriptors);
533 int32_t ret = EnableSource(networkId, descriptors);
534 if (!reply.WriteInt32(ret)) {
535 DHLOGE("Write ret code failed!");
536 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
537 }
538 return DH_FWK_SUCCESS;
539 }
540
DisableSourceInner(MessageParcel & data,MessageParcel & reply)541 int32_t DistributedHardwareStub::DisableSourceInner(MessageParcel &data, MessageParcel &reply)
542 {
543 if (!HasAccessDHPermission()) {
544 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
545 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
546 }
547 std::string networkId = data.ReadString();
548 std::vector<DHDescriptor> descriptors;
549 ReadDescriptors(data, descriptors);
550 int32_t ret = DisableSource(networkId, descriptors);
551 if (!reply.WriteInt32(ret)) {
552 DHLOGE("Write ret code failed!");
553 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
554 }
555 return DH_FWK_SUCCESS;
556 }
557
LoadDistributedHDFInner(MessageParcel & data,MessageParcel & reply)558 int32_t DistributedHardwareStub::LoadDistributedHDFInner(MessageParcel &data, MessageParcel &reply)
559 {
560 if (!HasAccessDHPermission()) {
561 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
562 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
563 }
564 DHType dhType = static_cast<DHType>(data.ReadUint32());
565 int32_t ret = LoadDistributedHDF(dhType);
566 if (ret == DH_FWK_SUCCESS) {
567 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
568 if (remoteObj != nullptr) {
569 ret = HdfOperateManager::GetInstance().AddDeathRecipient(dhType, remoteObj);
570 if (ret != DH_FWK_SUCCESS) {
571 DHLOGE("AddDeathRecipient failed!");
572 UnLoadDistributedHDF(dhType);
573 }
574 }
575 }
576 if (!reply.WriteInt32(ret)) {
577 DHLOGE("Write ret code failed!");
578 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
579 }
580 return DH_FWK_SUCCESS;
581 }
582
UnLoadDistributedHDFInner(MessageParcel & data,MessageParcel & reply)583 int32_t DistributedHardwareStub::UnLoadDistributedHDFInner(MessageParcel &data, MessageParcel &reply)
584 {
585 if (!HasAccessDHPermission()) {
586 DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
587 return ERR_DH_FWK_ACCESS_PERMISSION_CHECK_FAIL;
588 }
589 DHType dhType = static_cast<DHType>(data.ReadUint32());
590 int32_t ret = UnLoadDistributedHDF(dhType);
591 if (ret == DH_FWK_SUCCESS) {
592 sptr<IRemoteObject> remoteObj = data.ReadRemoteObject();
593 if (remoteObj != nullptr) {
594 ret = HdfOperateManager::GetInstance().RemoveDeathRecipient(dhType, remoteObj);
595 if (ret != DH_FWK_SUCCESS) {
596 DHLOGE("RemoveDeathRecipient failed!");
597 }
598 }
599 }
600 if (!reply.WriteInt32(ret)) {
601 DHLOGE("Write ret code failed!");
602 return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL;
603 }
604 return DH_FWK_SUCCESS;
605 }
606
ReadDescriptors(MessageParcel & data,std::vector<DHDescriptor> & descriptors)607 int32_t DistributedHardwareStub::ReadDescriptors(MessageParcel &data, std::vector<DHDescriptor> &descriptors)
608 {
609 int32_t size = data.ReadInt32();
610 if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) {
611 DHLOGE("The array descriptors are too large, size: %{public}d!", size);
612 return ERR_DH_FWK_PARA_INVALID;
613 }
614 for (int32_t i = 0; i < size; ++i) {
615 DHDescriptor descriptor;
616 descriptor.dhType = static_cast<DHType>(data.ReadInt32());
617 descriptor.id = data.ReadString();
618 descriptors.push_back(descriptor);
619 }
620 return NO_ERROR;
621 }
622
WriteDescriptors(MessageParcel & data,const std::vector<DHDescriptor> & descriptors)623 int32_t DistributedHardwareStub::WriteDescriptors(MessageParcel &data, const std::vector<DHDescriptor> &descriptors)
624 {
625 int32_t size = (int32_t)descriptors.size();
626 if (size > int32_t(MAX_DH_DESCRIPTOR_ARRAY_SIZE)) {
627 DHLOGE("The array descriptors are too large, size: %{public}d!", size);
628 return ERR_DH_FWK_PARA_INVALID;
629 }
630 if (!data.WriteInt32(size)) {
631 DHLOGE("Write descriptors size failed!");
632 return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
633 }
634 for (int32_t i = 0; i < size; ++i) {
635 const DHDescriptor &descriptor = descriptors.at(i);
636 int32_t type = static_cast<int32_t>(descriptor.dhType);
637 if (!data.WriteInt32(type)) {
638 DHLOGE("Write descriptor.dhType failed!");
639 return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
640 }
641 if (!data.WriteString(descriptor.id)) {
642 DHLOGE("Write descriptor.id failed!");
643 return ERR_DH_AVT_SERVICE_WRITE_INFO_FAIL;
644 }
645 }
646 return NO_ERROR;
647 }
648
OnRemoteRequestEx(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)649 int32_t DistributedHardwareStub::OnRemoteRequestEx(uint32_t code, MessageParcel &data, MessageParcel &reply,
650 MessageOption &option)
651 {
652 switch (code) {
653 case static_cast<uint32_t>(DHMsgInterfaceCode::PAUSE_DISTRIBUTED_HARDWARE): {
654 return PauseDistributedHardwareInner(data, reply);
655 }
656 case static_cast<uint32_t>(DHMsgInterfaceCode::RESUME_DISTRIBUTED_HARDWARE): {
657 return ResumeDistributedHardwareInner(data, reply);
658 }
659 case static_cast<uint32_t>(DHMsgInterfaceCode::STOP_DISTRIBUTED_HARDWARE): {
660 return StopDistributedHardwareInner(data, reply);
661 }
662 case static_cast<uint32_t>(DHMsgInterfaceCode::GET_DISTRIBUTED_HARDWARE): {
663 return GetDistributedHardwareInner(data, reply);
664 }
665 case static_cast<uint32_t>(DHMsgInterfaceCode::REG_DH_SINK_STATUS_LISTNER): {
666 return RegisterDHStatusSinkListenerInner(data, reply);
667 }
668 case static_cast<uint32_t>(DHMsgInterfaceCode::UNREG_DH_SINK_STATUS_LISTNER): {
669 return UnregisterDHStatusSinkListenerInner(data, reply);
670 }
671 case static_cast<uint32_t>(DHMsgInterfaceCode::REG_DH_SOURCE_STATUS_LISTNER): {
672 return RegisterDHStatusSourceListenerInner(data, reply);
673 }
674 case static_cast<uint32_t>(DHMsgInterfaceCode::UNREG_DH_SOURCE_STATUS_LISTNER): {
675 return UnregisterDHStatusSourceListenerInner(data, reply);
676 }
677 case static_cast<uint32_t>(DHMsgInterfaceCode::ENABLE_SINK): {
678 return EnableSinkInner(data, reply);
679 }
680 case static_cast<uint32_t>(DHMsgInterfaceCode::DISABLE_SINK): {
681 return DisableSinkInner(data, reply);
682 }
683 case static_cast<uint32_t>(DHMsgInterfaceCode::ENABLE_SOURCE): {
684 return EnableSourceInner(data, reply);
685 }
686 case static_cast<uint32_t>(DHMsgInterfaceCode::DISABLE_SOURCE): {
687 return DisableSourceInner(data, reply);
688 }
689 case static_cast<uint32_t>(DHMsgInterfaceCode::LOAD_HDF): {
690 return LoadDistributedHDFInner(data, reply);
691 }
692 case static_cast<uint32_t>(DHMsgInterfaceCode::UNLOAD_HDF): {
693 return UnLoadDistributedHDFInner(data, reply);
694 }
695 default:
696 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
697 }
698 return DH_FWK_SUCCESS;
699 }
700
HasAccessDHPermission()701 bool DistributedHardwareStub::HasAccessDHPermission()
702 {
703 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
704 const std::string permissionName = "ohos.permission.ACCESS_DISTRIBUTED_HARDWARE";
705 int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
706 permissionName);
707 return (result == Security::AccessToken::PERMISSION_GRANTED);
708 }
709
IsSystemHap()710 bool DistributedHardwareStub::IsSystemHap()
711 {
712 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
713 if (!OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) {
714 return false;
715 }
716 return true;
717 }
718 } // namespace DistributedHardware
719 } // namespace OHOS
720