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 #ifndef SCAN_MANAGER_CLIENT_H
17 #define SCAN_MANAGER_CLIENT_H
18 #define TDD_ENABLE 1
19 
20 #include <condition_variable>
21 #include <map>
22 #include <mutex>
23 
24 #include "iscan_callback.h"
25 #include "iscan_service.h"
26 #include "iremote_object.h"
27 
28 #include "scan_sync_load_callback.h"
29 #include "scan_sa_death_recipient.h"
30 #include "scanner_info.h"
31 #include "refbase.h"
32 
33 namespace OHOS::Scan {
34 class ScanManagerClient : public RefBase {
35 public:
36     ScanManagerClient();
37     ~ScanManagerClient();
38     static sptr<ScanManagerClient> GetInstance();
39 
40     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
41 
42     int32_t InitScan(int32_t &scanVersion);
43     int32_t ExitScan();
44     int32_t GetScannerList();
45     int32_t StopDiscover();
46     int32_t OpenScanner(const std::string scannerId);
47     int32_t CloseScanner(const std::string scannerId);
48     int32_t GetScanOptionDesc(const std::string scannerId, const int32_t optionIndex, ScanOptionDescriptor &desc);
49     int32_t OpScanOptionValue(const std::string scannerId, const int32_t optionIndex,
50     const ScanOptionOpType op, ScanOptionValue &value, int32_t &info);
51     int32_t GetScanParameters(const std::string scannerId, ScanParameters &para);
52     int32_t StartScan(const std::string scannerId, const bool &batchMode);
53     int32_t GetSingleFrameFD(const std::string scannerId, uint32_t &size, uint32_t fd);
54     int32_t CancelScan(const std::string scannerId);
55     int32_t SetScanIOMode(const std::string scannerId, const bool isNonBlocking);
56     int32_t GetScanSelectFd(const std::string scannerId, int32_t &fd);
57     int32_t GetScannerState(int32_t &scannerState);
58     int32_t GetScanProgress(const std::string scannerId, ScanProgress &prog);
59     int32_t AddScanner(const std::string& serialNumber, const std::string& discoverMode);
60     int32_t DeleteScanner(const std::string& serialNumber, const std::string& discoverMode);
61     int32_t GetAddedScanner(std::vector<ScanDeviceInfo>& allAddedScanner);
62     int32_t UpdateScannerName(const std::string& serialNumber,
63         const std::string& discoverMode, const std::string& deviceName);
64 
65     int32_t On(const std::string &taskId, const std::string &type, const sptr<IScanCallback> &listener);
66     int32_t Off(const std::string &taskId, const std::string &type);
67 
68     void LoadServerSuccess();
69     void LoadServerFail();
70 
71 #ifndef TDD_ENABLE
72 private:
73 #endif
74     bool LoadServer();
75     sptr<IScanService> GetScanServiceProxy();
76 
77 #ifndef TDD_ENABLE
78 private:
79 #endif
80     static std::mutex instanceLock_;
81     static sptr<ScanManagerClient> instance_;
82     sptr<IScanService> scanServiceProxy_;
83     sptr<ScanSaDeathRecipient> deathRecipient_;
84 
85     std::mutex loadMutex_;
86     std::mutex conditionMutex_;
87     std::mutex proxyLock_;
88     std::condition_variable syncCon_;
89     bool ready_ = false;
90     static constexpr int LOAD_SA_TIMEOUT_MS = 15000;
91 };
92 } // namespace OHOS::Scan
93 #endif // SCAN_MANAGER_CLIENT_H
94