• 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 #ifndef SCAN_SERVICE_ABILITY_H
17 #define SCAN_SERVICE_ABILITY_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 #include <functional>
23 #include <queue>
24 #include <chrono>
25 #include <atomic>
26 
27 #include "ability_manager_client.h"
28 #include "event_handler.h"
29 #include "extension_ability_info.h"
30 #include "iscan_callback.h"
31 #include "iremote_object.h"
32 #include "scan_constant.h"
33 #include "scan_service_stub.h"
34 #include "system_ability.h"
35 #include "scanner_info.h"
36 #include "scan_mdns_service.h"
37 #include "scan_option_descriptor.h"
38 #include "jpeglib.h"
39 namespace OHOS::Scan {
40 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
41 class ScanServiceAbility : public SystemAbility, public ScanServiceStub {
42     DECLARE_SYSTEM_ABILITY(ScanServiceAbility);
43 public:
44     DISALLOW_COPY_AND_MOVE(ScanServiceAbility);
45     ScanServiceAbility(int32_t systemAbilityId, bool runOnCreate);
46     ScanServiceAbility();
47     ~ScanServiceAbility();
48     static sptr<ScanServiceAbility> GetInstance();
49     int32_t InitScan() override;
50     int32_t ExitScan() override;
51     int32_t GetScannerList() override;
52     int32_t OpenScanner(const std::string scannerId) override;
53     int32_t CloseScanner(const std::string scannerId) override;
54     int32_t GetScanOptionDesc(const std::string scannerId, const int32_t optionIndex,
55         ScanOptionDescriptor &desc) override;
56     int32_t OpScanOptionValue(const std::string scannerId, const int32_t optionIndex,
57         const ScanOptionOpType op, ScanOptionValue &value) override;
58     int32_t GetScanParameters(const std::string scannerId, ScanParameters &para) override;
59     int32_t StartScan(const std::string scannerId, const bool &batchMode) override;
60     int32_t CancelScan(const std::string scannerId) override;
61     int32_t On(const std::string taskId, const std::string &type, const sptr<IScanCallback> &listener) override;
62     int32_t Off(const std::string taskId, const std::string &type) override;
63     int32_t GetScanProgress(const std::string scannerId, ScanProgress &prog) override;
64     int32_t AddScanner(const std::string& serialNumber, const std::string& discoverMode) override;
65     int32_t DeleteScanner(const std::string& serialNumber, const std::string& discoverMode) override;
66     int32_t GetAddedScanner(std::vector<ScanDeviceInfo>& allAddedScanner) override;
67     int32_t OnStartScan(const std::string scannerId, const bool &batchMode);
68     void DisConnectUsbScanner(std::string serialNumber, std::string newDeviceId); // public
69     void UpdateScannerId(const ScanDeviceInfoSync& usbSyncInfo); // public
70 
71 private:
72     int32_t ActionSetAuto(const std::string &scannerId, const int32_t &optionIndex);
73     int32_t ActionGetValue(const std::string &scannerId, ScanOptionValue &value, const int32_t &optionIndex);
74     int32_t ActionSetValue(const std::string &scannerId, ScanOptionValue &value,
75                            const int32_t &optionIndex);
76     int32_t DoScanTask(const std::string& scannerId);
77     void StartScanTask(const std::string scannerId);
78     void SendDeviceInfo(const ScanDeviceInfo &info, std::string event);
79     void SendDeviceInfoSync(const ScanDeviceInfoSync &info, std::string event);
80     void SetScannerSerialNumber(ScanDeviceInfo &info);
81     void SaneGetScanner();
82     void SetScannerSerialNumberByTCP(ScanDeviceInfo &info);
83     void SetScannerSerialNumberByUSB(ScanDeviceInfo &info);
84 public:
85     static std::map<std::string, ScanDeviceInfo> saneGetUsbDeviceInfoMap;
86     static std::map<std::string, ScanDeviceInfo> saneGetTcpDeviceInfoMap;
87     void UnloadSystemAbility();
88 protected:
89     void OnStart() override;
90     void OnStop() override;
91 
92 private:
93     int32_t ServiceInit();
94     void InitServiceHandler();
95     void ManualStart();
96     bool CheckPermission(const std::string &permissionName);
97     int32_t WriteJpegHeader(ScanParameters &parm, struct jpeg_error_mgr* jerr);
98     void GeneratePicture(const std::string &scannerId, std::string &fileName,
99         std::string &outputFile, int32_t &status, ScanProgress* &scanProPtr);
100     void GetPicFrame(const std::string scannerId,
101         int32_t &scanStatus, ScanParameters &parm);
102     bool WritePicData(int &jpegrow, int32_t curReadSize, ScanParameters &parm, int32_t &scanStatus);
103     void GeneratePictureBatch(const std::string &scannerId);
104     void GeneratePictureSingle(const std::string &scannerId);
105     bool GetUsbDevicePort(const std::string &deviceId, std::string &firstId, std::string &secondId);
106     bool GetTcpDeviceIp(const std::string &deviceId, std::string &ip);
107     void SendDeviceList(std::vector<ScanDeviceInfo> &info, std::string event);
108     int32_t GetCurrentUserId();
109     std::string ObtainUserCacheDirectory(const int32_t& userId);
110     void SetScanProgr(int64_t &totalBytes, const int64_t& hundredPercent,
111         const int32_t& curReadSize);
112     bool CreateAndOpenScanFile(std::string &outputFile);
113     void AddFoundScanner(ScanDeviceInfo& info);
114     void CleanUpAfterScan(int32_t scanStatus);
115     int32_t RestartScan(const std::string &scannerId);
116     void InitializeScanService();
117     void CleanupScanService();
118     std::set<std::string> openedScannerList_;
119     ServiceRunningState state_;
120     std::mutex lock_;
121     std::mutex clearMapLock_;
122     static std::mutex instanceLock_;
123     static sptr<ScanServiceAbility> instance_;
124     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
125     static std::map<std::string, sptr<IScanCallback>> registeredListeners_;
126     std::recursive_mutex apiMutex_;
127     std::recursive_mutex scanMutex_;
128     uint64_t currentJobId_;
129     int32_t currentUseScannerUserId_;
130     std::map<std::string, int32_t> imageFdMap_;
131     std::queue<int32_t> scanQueue;
132     std::map<int32_t, ScanProgress> scanTaskMap;
133     std::vector<ScanDeviceInfo> deviceInfos_;
134     int32_t nextPicId = 1;
135     int32_t buffer_size;
136     bool batchMode_ = false;
137     uint8_t *saneReadBuf;
138     struct jpeg_compress_struct *cinfoPtr;
139     FILE *ofp = nullptr;
140     int32_t dpi = 0;
141     JSAMPLE *jpegbuf = nullptr;
142     std::atomic<int32_t> appCount_{0};
143 };
144 } // namespace OHOS::Scan
145 #endif // SCAN_SYSTEM_ABILITY_H