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_source_proxy.h"
17
18 #include "dscreen_ipc_interface_code.h"
19 #include "idscreen_source_callback.h"
20 #include "iremote_object.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23
24 #include "dscreen_constants.h"
25 #include "dscreen_errcode.h"
26 #include "dscreen_log.h"
27 #include "dscreen_util.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
InitSource(const std::string & params,const sptr<IDScreenSourceCallback> & callback)31 int32_t DScreenSourceProxy::InitSource(const std::string ¶ms, const sptr<IDScreenSourceCallback> &callback)
32 {
33 if (params.empty() || params.size() > PARAM_MAX_SIZE || callback == nullptr) {
34 DHLOGE("InitSource error: invalid parameter");
35 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
36 }
37 sptr<IRemoteObject> remote = Remote();
38 if (remote == nullptr) {
39 DHLOGE("DScreenSourceProxy remote service null.");
40 return DSCREEN_BAD_VALUE;
41 }
42
43 MessageParcel data;
44 MessageParcel reply;
45 MessageOption option;
46 if (!data.WriteInterfaceToken(GetDescriptor())) {
47 DHLOGE("WriteInterfaceToken Failed.");
48 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
49 }
50
51 if (!data.WriteString(params) || !data.WriteRemoteObject(callback->AsObject())) {
52 DHLOGE("Write param failed.");
53 return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
54 }
55
56 remote->SendRequest(static_cast<uint32_t>(IDScreenSourceInterfaceCode::INIT_SOURCE), data, reply, option);
57 int32_t ret = reply.ReadInt32();
58 return ret;
59 }
60
ReleaseSource()61 int32_t DScreenSourceProxy::ReleaseSource()
62 {
63 sptr<IRemoteObject> remote = Remote();
64 if (remote == nullptr) {
65 DHLOGE("DScreenSourceProxy remote service null");
66 return DSCREEN_BAD_VALUE;
67 }
68
69 MessageParcel data;
70 MessageParcel reply;
71 MessageOption option;
72 if (!data.WriteInterfaceToken(GetDescriptor())) {
73 DHLOGE("WriteInterfaceToken failed.");
74 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
75 }
76
77 remote->SendRequest(static_cast<uint32_t>(IDScreenSourceInterfaceCode::RELEASE_SOURCE), data, reply, option);
78 int32_t ret = reply.ReadInt32();
79 return ret;
80 }
81
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId)82 int32_t DScreenSourceProxy::RegisterDistributedHardware(const std::string &devId,
83 const std::string &dhId, const EnableParam ¶m, const std::string &reqId)
84 {
85 if (!CheckRegParams(devId, dhId, param, reqId)) {
86 DHLOGE("RegisterDistributedHardware error: invalid parameter");
87 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
88 }
89 sptr<IRemoteObject> remote = Remote();
90 if (remote == nullptr) {
91 DHLOGE("DScreenSourceProxy remote service null!");
92 return DSCREEN_BAD_VALUE;
93 }
94
95 MessageParcel data;
96 MessageParcel reply;
97 MessageOption option;
98 if (!data.WriteInterfaceToken(GetDescriptor())) {
99 DHLOGE("WriteInterfaceToken failed!");
100 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
101 }
102
103 if (!data.WriteString(devId) || !data.WriteString(dhId) ||
104 !data.WriteString(param.sinkVersion) || !data.WriteString(param.sinkAttrs) || !data.WriteString(reqId)) {
105 DHLOGE("Write param failed.");
106 return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
107 }
108 remote->SendRequest(static_cast<uint32_t>(IDScreenSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE),
109 data, reply, option);
110 int32_t ret = reply.ReadInt32();
111 return ret;
112 }
113
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & reqId)114 int32_t DScreenSourceProxy::UnregisterDistributedHardware(const std::string &devId,
115 const std::string &dhId, const std::string &reqId)
116 {
117 if (!CheckUnregParams(devId, dhId, reqId)) {
118 DHLOGE("UnregisterDistributedHardware error: invalid parameter");
119 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
120 }
121 sptr<IRemoteObject> remote = Remote();
122 if (remote == nullptr) {
123 DHLOGE("DScreenSourceProxy remote service is null");
124 return DSCREEN_BAD_VALUE;
125 }
126
127 MessageParcel data;
128 MessageParcel reply;
129 MessageOption option;
130 if (!data.WriteInterfaceToken(GetDescriptor())) {
131 DHLOGE("WriteInterfaceToken Failed");
132 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
133 }
134
135 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(reqId)) {
136 DHLOGE("Write param failed.");
137 return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
138 }
139 remote->SendRequest(static_cast<uint32_t>(IDScreenSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE),
140 data, reply, option);
141 int32_t ret = reply.ReadInt32();
142 return ret;
143 }
144
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)145 int32_t DScreenSourceProxy::ConfigDistributedHardware(const std::string &devId,
146 const std::string &dhId, const std::string &key, const std::string &value)
147 {
148 if (!CheckConfigParams(devId, dhId, key, value)) {
149 DHLOGE("ConfigDistributedHardware error: invalid parameter");
150 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
151 }
152 sptr<IRemoteObject> remote = Remote();
153 if (remote == nullptr) {
154 DHLOGE("DScreenSourceProxy remote service null");
155 return DSCREEN_BAD_VALUE;
156 }
157
158 MessageParcel data;
159 MessageParcel reply;
160 MessageOption option;
161 if (!data.WriteInterfaceToken(GetDescriptor())) {
162 DHLOGE("WriteInterfaceToken failed");
163 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
164 }
165
166 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(key) || !data.WriteString(value)) {
167 DHLOGE("Write param failed.");
168 return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
169 }
170 remote->SendRequest(static_cast<uint32_t>(IDScreenSourceInterfaceCode::CONFIG_DISTRIBUTED_HARDWARE),
171 data, reply, option);
172 int32_t ret = reply.ReadInt32();
173 return ret;
174 }
175
DScreenNotify(const std::string & devId,int32_t eventCode,const std::string & eventContent)176 void DScreenSourceProxy::DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent)
177 {
178 if (devId.empty() || devId.size() > DID_MAX_SIZE || eventContent.empty() ||
179 eventContent.size() > PARAM_MAX_SIZE) {
180 DHLOGE("DScreenNotify error: invalid parameter");
181 return;
182 }
183 sptr<IRemoteObject> remote = Remote();
184 if (remote == nullptr) {
185 DHLOGE("DScreenSourceProxy remote service null");
186 return;
187 }
188
189 MessageParcel data;
190 MessageParcel reply;
191 MessageOption option = { MessageOption::TF_ASYNC };
192 if (!data.WriteInterfaceToken(GetDescriptor())) {
193 DHLOGE("WriteInterfaceToken failed.");
194 return;
195 }
196
197 if (!data.WriteString(devId) || !data.WriteInt32(eventCode) || !data.WriteString(eventContent)) {
198 DHLOGE("Write param failed.");
199 return;
200 }
201
202 remote->SendRequest(static_cast<uint32_t>(IDScreenSourceInterfaceCode::DSCREEN_NOTIFY),
203 data, reply, option);
204 }
205
CheckRegParams(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId) const206 bool DScreenSourceProxy::CheckRegParams(const std::string &devId, const std::string &dhId,
207 const EnableParam ¶m, const std::string &reqId) const
208 {
209 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
210 DHLOGE("DScreenSourceProxy CheckRegParams devId or dhId is invalid.");
211 return false;
212 }
213 if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
214 DHLOGE("DScreenSourceProxy CheckRegParams reqId is invalid.");
215 return false;
216 }
217 if (param.sinkVersion.empty() || param.sinkVersion.size() > PARAM_MAX_SIZE || param.sinkAttrs.empty() ||
218 param.sinkAttrs.size() > PARAM_MAX_SIZE) {
219 DHLOGE("DScreenSourceProxy CheckRegParams param is invalid.");
220 return false;
221 }
222 return true;
223 }
224
CheckUnregParams(const std::string & devId,const std::string & dhId,const std::string & reqId) const225 bool DScreenSourceProxy::CheckUnregParams(const std::string &devId,
226 const std::string &dhId, const std::string &reqId) const
227 {
228 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
229 DHLOGE("DScreenSourceProxy CheckUnregParams devId or dhId is invalid.");
230 return false;
231 }
232 if (reqId.empty() || reqId.size() > DID_MAX_SIZE) {
233 DHLOGE("DScreenSourceProxy CheckUnregParams reqId is invalid.");
234 return false;
235 }
236 return true;
237 }
238
CheckConfigParams(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value) const239 bool DScreenSourceProxy::CheckConfigParams(const std::string &devId, const std::string &dhId,
240 const std::string &key, const std::string &value) const
241 {
242 if (devId.empty() || devId.size() > DID_MAX_SIZE || dhId.empty() || dhId.size() > DID_MAX_SIZE) {
243 DHLOGE("DScreenSourceProxy CheckConfigParams devId or dhId is invalid.");
244 return false;
245 }
246 if (key.empty() || key.size() > PARAM_MAX_SIZE || value.empty() || value.size() > PARAM_MAX_SIZE) {
247 DHLOGE("DScreenSourceProxy CheckConfigParams key or value is invalid.");
248 return false;
249 }
250 return true;
251 }
252 } // namespace DistributedHardware
253 } // namespace OHOS
254