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