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