• 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 #include "securec.h"
16 #include "iremote_broker.h"
17 #include "napi_scan_utils.h"
18 #include "scan_constant.h"
19 #include "scan_log.h"
20 #include "scanner_info.h"
21 #include "scan_service_proxy.h"
22 
23 namespace OHOS::Scan {
24 using namespace OHOS::HiviewDFX;
25 
ScanServiceProxy(const sptr<IRemoteObject> & object)26 ScanServiceProxy::ScanServiceProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IScanService>(object) {}
27 
GetResult(int32_t retCode,MessageParcel & reply)28 int32_t ScanServiceProxy::GetResult(int32_t retCode, MessageParcel &reply)
29 {
30     if (retCode != ERR_NONE) {
31         SCAN_HILOGE("rpc error code = %{public}d", retCode);
32         return E_SCAN_RPC_FAILURE;
33     }
34 
35     retCode = reply.ReadInt32();
36     SCAN_HILOGD("ScanServiceProxy end. ret = [%{public}d]", retCode);
37     return retCode;
38 }
39 
InitScan()40 int32_t ScanServiceProxy::InitScan()
41 {
42     SCAN_HILOGD("ScanServiceProxy InitScan start");
43     CREATE_PRC_MESSAGE;
44     auto remote = Remote();
45     if (remote == nullptr) {
46         SCAN_HILOGE("ScanServiceProxy::InitScan remote is null");
47         return E_SCAN_RPC_FAILURE;
48     }
49     int32_t ret = remote->SendRequest(CMD_INIT_SCAN, data, reply, option);
50     ret = GetResult(ret, reply);
51     if (ret != E_SCAN_NONE) {
52         SCAN_HILOGE("ScanServiceProxy InitScan failed");
53         return ret;
54     }
55     SCAN_HILOGD("ScanServiceProxy InitScan end.");
56     return ret;
57 }
58 
ExitScan()59 int32_t ScanServiceProxy::ExitScan()
60 {
61     SCAN_HILOGD("ScanServiceProxy ExitScan start");
62     CREATE_PRC_MESSAGE;
63     auto remote = Remote();
64     if (remote == nullptr) {
65         SCAN_HILOGE("ScanServiceProxy::ExitScan remote is null");
66         return E_SCAN_RPC_FAILURE;
67     }
68     int32_t ret = remote->SendRequest(CMD_EXIT_SCAN, data, reply, option);
69     ret = GetResult(ret, reply);
70     if (ret != E_SCAN_NONE) {
71         SCAN_HILOGE("ScanServiceProxy ExitScan failed");
72         return ret;
73     }
74     SCAN_HILOGD("ScanServiceProxy ExitScan end");
75     return ret;
76 }
77 
GetScannerList()78 int32_t ScanServiceProxy::GetScannerList()
79 {
80     SCAN_HILOGD("ScanServiceProxy GetScannerList start");
81     CREATE_PRC_MESSAGE;
82     auto remote = Remote();
83     if (remote == nullptr) {
84         SCAN_HILOGE("ScanServiceProxy::GetScannerList remote is null");
85         return E_SCAN_RPC_FAILURE;
86     }
87     int32_t ret = remote->SendRequest(CMD_GET_SCANNER_LIST, data, reply, option);
88     ret = GetResult(ret, reply);
89     if (ret != E_SCAN_NONE) {
90         SCAN_HILOGE("ScanServiceProxy GetScannerList failed");
91         return ret;
92     }
93     SCAN_HILOGD("ScanServiceProxy GetScannerList end");
94     return ret;
95 }
96 
OpenScanner(const std::string scannerId)97 int32_t ScanServiceProxy::OpenScanner(const std::string scannerId)
98 {
99     SCAN_HILOGD("ScanServiceProxy OpenScanner start");
100     CREATE_PRC_MESSAGE;
101     auto remote = Remote();
102     if (remote == nullptr) {
103         SCAN_HILOGE("ScanServiceProxy::OpenScanner remote is null");
104         return E_SCAN_RPC_FAILURE;
105     }
106     data.WriteString(scannerId);
107     int32_t ret = remote->SendRequest(CMD_OPEN_SCANNER, data, reply, option);
108     ret = GetResult(ret, reply);
109     if (ret != E_SCAN_NONE) {
110         SCAN_HILOGE("ScanServiceProxy OpenScanner failed");
111         return ret;
112     }
113     SCAN_HILOGD("ScanServiceProxy OpenScanner end");
114     return ret;
115 }
116 
CloseScanner(const std::string scannerId)117 int32_t ScanServiceProxy::CloseScanner(const std::string scannerId)
118 {
119     SCAN_HILOGD("ScanServiceProxy CloseScanner start");
120     CREATE_PRC_MESSAGE;
121     auto remote = Remote();
122     if (remote == nullptr) {
123         SCAN_HILOGE("ScanServiceProxy::CloseScanner remote is null");
124         return E_SCAN_RPC_FAILURE;
125     }
126     data.WriteString(scannerId);
127     int32_t ret = remote->SendRequest(CMD_CLOSE_SCANNER, data, reply, option);
128     ret = GetResult(ret, reply);
129     if (ret != E_SCAN_NONE) {
130         SCAN_HILOGE("ScanServiceProxy CloseScanner failed");
131         return ret;
132     }
133     SCAN_HILOGD("ScanServiceProxy CloseScanner end");
134     return ret;
135 }
136 
GetScanOptionDesc(const std::string scannerId,const int32_t optionIndex,ScanOptionDescriptor & desc)137 int32_t ScanServiceProxy::GetScanOptionDesc(const std::string scannerId, const int32_t optionIndex,
138     ScanOptionDescriptor &desc)
139 {
140     SCAN_HILOGD("ScanServiceProxy GetScanOptionDesc start");
141     CREATE_PRC_MESSAGE;
142     auto remote = Remote();
143     if (remote == nullptr) {
144         SCAN_HILOGE("ScanServiceProxy::GetScanOptionDesc remote is null");
145         return E_SCAN_RPC_FAILURE;
146     }
147     data.WriteString(scannerId);
148     data.WriteInt32(optionIndex);
149     int32_t ret = remote->SendRequest(CMD_GET_SCAN_OPTION_DESC, data, reply, option);
150     ret = GetResult(ret, reply);
151     if (ret != E_SCAN_NONE) {
152         SCAN_HILOGE("ScanServiceProxy GetScanOptionDesc failed");
153         return ret;
154     }
155     auto scanOptionDescriptor = ScanOptionDescriptor::Unmarshalling(reply);
156     desc = *scanOptionDescriptor;
157     SCAN_HILOGD("ScanServiceProxy GetScanOptionDesc end");
158     return ret;
159 }
160 
OpScanOptionValue(const std::string scannerId,const int32_t optionIndex,const ScanOptionOpType op,ScanOptionValue & value)161 int32_t ScanServiceProxy::OpScanOptionValue(const std::string scannerId,
162     const int32_t optionIndex, const ScanOptionOpType op, ScanOptionValue &value)
163 {
164     SCAN_HILOGD("ScanServiceProxy OpScanOptionValue start");
165     CREATE_PRC_MESSAGE;
166     auto remote = Remote();
167     if (remote == nullptr) {
168         SCAN_HILOGE("ScanServiceProxy::OpScanOptionValue remote is null");
169         return E_SCAN_RPC_FAILURE;
170     }
171     data.WriteString(scannerId);
172     data.WriteInt32(optionIndex);
173     data.WriteUint32(op);
174     value.Marshalling(data);
175     int32_t ret = remote->SendRequest(CMD_OP_SCAN_OPTION_VALUE, data, reply, option);
176     ret = GetResult(ret, reply);
177     if (ret != E_SCAN_NONE) {
178         SCAN_HILOGE("ScanServiceProxy OpScanOptionValue failed");
179         return ret;
180     }
181     auto scanOptionValue = ScanOptionValue::Unmarshalling(reply);
182     if (scanOptionValue == nullptr) {
183         SCAN_HILOGE("ScanServiceProxy scanOptionValue is a nullptr");
184         return E_SCAN_GENERIC_FAILURE;
185     }
186     value = *scanOptionValue;
187     SCAN_HILOGD("ScanServiceProxy OpScanOptionValue end");
188     return ret;
189 }
190 
GetScanParameters(const std::string scannerId,ScanParameters & para)191 int32_t ScanServiceProxy::GetScanParameters(const std::string scannerId, ScanParameters &para)
192 {
193     SCAN_HILOGD("ScanServiceProxy GetScanParameters start");
194     CREATE_PRC_MESSAGE;
195     auto remote = Remote();
196     if (remote == nullptr) {
197         SCAN_HILOGE("ScanServiceProxy::GetScanParameters remote is null");
198         return E_SCAN_RPC_FAILURE;
199     }
200     data.WriteString(scannerId);
201     int32_t ret = remote->SendRequest(CMD_GET_SCAN_PARAMETERS, data, reply, option);
202     ret = GetResult(ret, reply);
203     if (ret != E_SCAN_NONE) {
204         SCAN_HILOGE("ScanServiceProxy GetScanParameters failed");
205         return ret;
206     }
207     auto scanParameters = ScanParameters::Unmarshalling(reply);
208     para = *scanParameters;
209     SCAN_HILOGD("ScanServiceProxy GetScanParameters end");
210     return ret;
211 }
212 
StartScan(const std::string scannerId,const bool & batchMode)213 int32_t ScanServiceProxy::StartScan(const std::string scannerId, const bool &batchMode)
214 {
215     SCAN_HILOGD("ScanServiceProxy StartScan start");
216     CREATE_PRC_MESSAGE;
217     auto remote = Remote();
218     if (remote == nullptr) {
219         SCAN_HILOGE("ScanServiceProxy::StartScan remote is null");
220         return E_SCAN_RPC_FAILURE;
221     }
222     data.WriteString(scannerId);
223     data.WriteBool(batchMode);
224     int32_t ret = remote->SendRequest(CMD_START_SCAN, data, reply, option);
225     ret = GetResult(ret, reply);
226     if (ret != E_SCAN_NONE) {
227         SCAN_HILOGE("ScanServiceProxy StartScan failed");
228         return ret;
229     }
230     SCAN_HILOGD("ScanServiceProxy StartScan end");
231     return ret;
232 }
233 
CancelScan(const std::string scannerId)234 int32_t ScanServiceProxy::CancelScan(const std::string scannerId)
235 {
236     SCAN_HILOGD("ScanServiceProxy CancelScan start");
237     CREATE_PRC_MESSAGE;
238     auto remote = Remote();
239     if (remote == nullptr) {
240         SCAN_HILOGE("ScanServiceProxy::CancelScan remote is null");
241         return E_SCAN_RPC_FAILURE;
242     }
243     data.WriteString(scannerId);
244     int32_t ret = remote->SendRequest(CMD_CANCEL_SCAN, data, reply, option);
245     ret = GetResult(ret, reply);
246     if (ret != E_SCAN_NONE) {
247         SCAN_HILOGE("ScanServiceProxy CancelScan failed");
248         return ret;
249     }
250     SCAN_HILOGD("ScanServiceProxy CancelScan end");
251     return ret;
252 }
253 
On(const std::string taskId,const std::string & type,const sptr<IScanCallback> & listener)254 int32_t ScanServiceProxy::On(const std::string taskId, const std::string &type, const sptr<IScanCallback> &listener)
255 {
256     SCAN_HILOGD("ScanServiceProxy On start");
257     if (listener == nullptr) {
258         SCAN_HILOGE("listener is nullptr");
259         return E_SCAN_INVALID_PARAMETER;
260     }
261 
262     if (type.empty()) {
263         SCAN_HILOGE("ScanServiceProxy::On type is null.");
264         return E_SCAN_INVALID_PARAMETER;
265     }
266 
267     CREATE_PRC_MESSAGE;
268     data.WriteString(taskId);
269     data.WriteString(type);
270     data.WriteRemoteObject(listener->AsObject().GetRefPtr());
271 
272     auto remote = Remote();
273     if (remote == nullptr) {
274         SCAN_HILOGE("ScanServiceProxy::On remote is null");
275         return E_SCAN_RPC_FAILURE;
276     }
277 
278     int32_t ret = remote->SendRequest(CMD_ON, data, reply, option);
279     ret = GetResult(ret, reply);
280     if (ret != E_SCAN_NONE) {
281         SCAN_HILOGE("ScanServiceProxy On failed");
282         return ret;
283     }
284 
285     SCAN_HILOGD("ScanServiceProxy On end");
286     return ret;
287 }
288 
Off(const std::string taskId,const std::string & type)289 int32_t ScanServiceProxy::Off(const std::string taskId, const std::string &type)
290 {
291     SCAN_HILOGD("ScanServiceProxy::Off start");
292 
293     if (type.empty()) {
294         SCAN_HILOGE("ScanServiceProxy::Off type is null.");
295         return E_SCAN_INVALID_PARAMETER;
296     }
297 
298     CREATE_PRC_MESSAGE;
299     data.WriteString(taskId);
300     data.WriteString(type);
301 
302     auto remote = Remote();
303     if (remote == nullptr) {
304         SCAN_HILOGE("ScanServiceProxy::SetScanIOMode remote is null");
305         return E_SCAN_RPC_FAILURE;
306     }
307     int32_t ret = remote->SendRequest(CMD_OFF, data, reply, option);
308     ret = GetResult(ret, reply);
309     if (ret != E_SCAN_NONE) {
310         SCAN_HILOGE("ScanServiceProxy Off failed");
311         return ret;
312     }
313 
314     SCAN_HILOGD("ScanServiceProxy Off out");
315     return ret;
316 }
317 
GetScanProgress(const std::string scannerId,ScanProgress & prog)318 int32_t ScanServiceProxy::GetScanProgress(const std::string scannerId, ScanProgress &prog)
319 {
320     SCAN_HILOGI("ScanServiceProxy GetScanProgress start");
321     CREATE_PRC_MESSAGE;
322     auto remote = Remote();
323     if (remote == nullptr) {
324         SCAN_HILOGE("ScanServiceProxy::GetScanProgress remote is null");
325         return E_SCAN_RPC_FAILURE;
326     }
327     data.WriteString(scannerId);
328     int32_t ret = remote->SendRequest(CMD_GET_SCAN_PROGRESS, data, reply, option);
329     ret = GetResult(ret, reply);
330     if (ret != E_SCAN_NONE) {
331         SCAN_HILOGE("ScanServiceProxy GetScanProgress failed");
332         return ret;
333     }
334     auto scanProgress = ScanProgress::Unmarshalling(reply);
335     if (scanProgress != nullptr) {
336         prog = *scanProgress;
337     } else {
338         SCAN_HILOGE("get scanProgress is a nullptr ptr.");
339         return E_SCAN_GENERIC_FAILURE;
340     }
341     SCAN_HILOGI("ScanServiceProxy GetScanProgress end");
342     return ret;
343 }
344 
AddScanner(const std::string & serialNumber,const std::string & discoverMode)345 int32_t ScanServiceProxy::AddScanner(const std::string& serialNumber, const std::string& discoverMode)
346 {
347     SCAN_HILOGD("ScanServiceProxy AddScanner start");
348     CREATE_PRC_MESSAGE;
349     auto remote = Remote();
350     if (remote == nullptr) {
351         SCAN_HILOGE("ScanServiceProxy::AddScanner remote is null");
352         return E_SCAN_RPC_FAILURE;
353     }
354     data.WriteString(serialNumber);
355     data.WriteString(discoverMode);
356     int32_t ret = remote->SendRequest(CMD_CONNECT_SCANNER, data, reply, option);
357     ret = GetResult(ret, reply);
358     if (ret != E_SCAN_NONE) {
359         SCAN_HILOGE("ScanServiceProxy AddScanner failed");
360         return ret;
361     }
362     SCAN_HILOGD("ScanServiceProxy AddScanner end");
363     return ret;
364 }
365 
DeleteScanner(const std::string & serialNumber,const std::string & discoverMode)366 int32_t ScanServiceProxy::DeleteScanner(const std::string& serialNumber, const std::string& discoverMode)
367 {
368     SCAN_HILOGD("ScanServiceProxy DeleteScanner start");
369     CREATE_PRC_MESSAGE;
370     auto remote = Remote();
371     if (remote == nullptr) {
372         SCAN_HILOGE("ScanServiceProxy::DeleteScanner remote is null");
373         return E_SCAN_RPC_FAILURE;
374     }
375     data.WriteString(serialNumber);
376     data.WriteString(discoverMode);
377     int32_t ret = remote->SendRequest(CMD_DISCONNECT_SCANNER, data, reply, option);
378     ret = GetResult(ret, reply);
379     if (ret != E_SCAN_NONE) {
380         SCAN_HILOGE("ScanServiceProxy DeleteScanner failed");
381         return ret;
382     }
383     SCAN_HILOGD("ScanServiceProxy DeleteScanner end");
384     return ret;
385 }
386 
GetAddedScanner(std::vector<ScanDeviceInfo> & allAddedScanner)387 int32_t ScanServiceProxy::GetAddedScanner(std::vector<ScanDeviceInfo>& allAddedScanner)
388 {
389     SCAN_HILOGD("ScanServiceProxy GetAddedScanner start");
390     CREATE_PRC_MESSAGE;
391     auto remote = Remote();
392     if (remote == nullptr) {
393         SCAN_HILOGE("ScanServiceProxy::GetAddedScanner remote is null");
394         return E_SCAN_RPC_FAILURE;
395     }
396     int32_t ret = remote->SendRequest(CMD_GET_CONNECTED_SCANNER, data, reply, option);
397     ret = GetResult(ret, reply);
398     if (ret != E_SCAN_NONE) {
399         SCAN_HILOGE("ScanServiceProxy GetAddedScanner failed");
400         return ret;
401     }
402     uint32_t len = reply.ReadUint32();
403     if (len > SCAN_MAX_COUNT) {
404         SCAN_HILOGE("len is out of range.");
405         return E_SCAN_INVALID_PARAMETER;
406     }
407     for (uint32_t i = 0; i < len; i++) {
408         auto infoPtr = ScanDeviceInfo::Unmarshalling(reply);
409         if (infoPtr == nullptr) {
410             SCAN_HILOGE("wrong scanDeviceInfo from data");
411             return E_SCAN_GENERIC_FAILURE;
412         }
413         allAddedScanner.emplace_back(*infoPtr);
414     }
415     SCAN_HILOGD("ScanServiceProxy GetAddedScanner end");
416     return ret;
417 }
418 
419 } // namespace OHOS::Scan
420