1 /*
2 * Copyright (c) 2022-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 "dscreen_sink_stub.h"
17
18 #include "accesstoken_kit.h"
19 #include "dscreen_constants.h"
20 #include "dscreen_errcode.h"
21 #include "dscreen_ipc_interface_code.h"
22 #include "dscreen_log.h"
23 #include "ipc_skeleton.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
DScreenSinkStub()27 DScreenSinkStub::DScreenSinkStub()
28 {
29 memberFuncMap_[static_cast<uint32_t>(IDScreenSinkInterfaceCode::INIT_SINK)] =
30 &DScreenSinkStub::InitSinkInner;
31 memberFuncMap_[static_cast<uint32_t>(IDScreenSinkInterfaceCode::RELEASE_SINK)] =
32 &DScreenSinkStub::ReleaseSinkInner;
33 memberFuncMap_[static_cast<uint32_t>(IDScreenSinkInterfaceCode::SUBSCRIBE_DISTRIBUTED_HARDWARE)] =
34 &DScreenSinkStub::SubscribeDistributedHardwareInner;
35 memberFuncMap_[static_cast<uint32_t>(IDScreenSinkInterfaceCode::UNSUBSCRIBE_DISTRIBUTED_HARDWARE)] =
36 &DScreenSinkStub::UnsubscribeDistributedHardwareInner;
37 memberFuncMap_[static_cast<uint32_t>(IDScreenSinkInterfaceCode::DSCREEN_NOTIFY)] =
38 &DScreenSinkStub::DScreenNotifyInner;
39 }
40
HasEnableDHPermission()41 bool DScreenSinkStub::HasEnableDHPermission()
42 {
43 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
44 const std::string permissionName = "ohos.permission.ENABLE_DISTRIBUTED_HARDWARE";
45 int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
46 permissionName);
47 return (result == Security::AccessToken::PERMISSION_GRANTED);
48 }
49
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)50 int32_t DScreenSinkStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
51 MessageOption &option)
52 {
53 std::u16string desc = DScreenSinkStub::GetDescriptor();
54 std::u16string remoteDesc = data.ReadInterfaceToken();
55 if (desc != remoteDesc) {
56 DHLOGE("DScreenSinkStub::OnRemoteRequest remoteDesc is invalid!");
57 return ERR_INVALID_DATA;
58 }
59
60 const auto &iter = memberFuncMap_.find(code);
61 if (iter == memberFuncMap_.end()) {
62 DHLOGE("invalid request code.");
63 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
64 }
65 DScreenSinkFunc &func = iter->second;
66 return (this->*func)(data, reply, option);
67 }
68
InitSinkInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)69 int32_t DScreenSinkStub::InitSinkInner(MessageParcel &data, MessageParcel &reply,
70 MessageOption &option)
71 {
72 (void)option;
73 if (!HasEnableDHPermission()) {
74 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
75 return ERR_DH_SCREEN_SA_CHECK_ENABLE_PERMISSION_FAIL;
76 }
77 std::string param = data.ReadString();
78 if (param.empty() || param.size() > PARAM_MAX_SIZE) {
79 DHLOGE("InitSinkInner error: invalid parameter.");
80 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
81 }
82 int32_t ret = InitSink(param);
83 reply.WriteInt32(ret);
84 return DH_SUCCESS;
85 }
86
ReleaseSinkInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)87 int32_t DScreenSinkStub::ReleaseSinkInner(MessageParcel &data, MessageParcel &reply,
88 MessageOption &option)
89 {
90 (void)data;
91 (void)option;
92 if (!HasEnableDHPermission()) {
93 DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
94 return DSCREEN_INIT_ERR;
95 }
96 int32_t ret = ReleaseSink();
97 reply.WriteInt32(ret);
98 return DH_SUCCESS;
99 }
100
SubscribeDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)101 int32_t DScreenSinkStub::SubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
102 MessageOption &option)
103 {
104 (void)option;
105 std::string dhId = data.ReadString();
106 std::string param = data.ReadString();
107 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || param.empty() || param.size() > PARAM_MAX_SIZE) {
108 DHLOGE("SubscribeDistributedHardwareInner error: invalid parameter.");
109 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
110 }
111 int32_t ret = SubscribeLocalHardware(dhId, param);
112 reply.WriteInt32(ret);
113 return DH_SUCCESS;
114 }
115
UnsubscribeDistributedHardwareInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)116 int32_t DScreenSinkStub::UnsubscribeDistributedHardwareInner(MessageParcel &data, MessageParcel &reply,
117 MessageOption &option)
118 {
119 (void)option;
120 std::string dhId = data.ReadString();
121 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
122 DHLOGE("UnsubscribeDistributedHardwareInner error: invalid parameter.");
123 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
124 }
125 int32_t ret = UnsubscribeLocalHardware(dhId);
126 reply.WriteInt32(ret);
127 return DH_SUCCESS;
128 }
129
DScreenNotifyInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)130 int32_t DScreenSinkStub::DScreenNotifyInner(MessageParcel &data, MessageParcel &reply,
131 MessageOption &option)
132 {
133 (void)reply;
134 (void)option;
135 std::string devId = data.ReadString();
136 int32_t eventCode = data.ReadInt32();
137 std::string eventContent = data.ReadString();
138 if (devId.empty() || devId.size() > DID_MAX_SIZE || eventContent.empty() ||
139 eventContent.size() > PARAM_MAX_SIZE) {
140 DHLOGE("DScreenNotifyInner error: invalid parameter.");
141 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
142 }
143 DScreenNotify(devId, eventCode, eventContent);
144 return DH_SUCCESS;
145 }
146 } // namespace DistributedHardware
147 } // namespace OHOS