• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_CONTEXT_H
17 #define SCAN_CONTEXT_H
18 
19 #include <map>
20 #include <vector>
21 #include <string>
22 #include <mutex>
23 #include "ohscan.h"
24 #include "scanner_info.h"
25 #include "scan_option_descriptor.h"
26 #include "scan_option_value.h"
27 
28 namespace OHOS::Scan {
29 struct ScanParaTable {
30     std::vector<std::string> titBuff;
31     std::vector<std::string> desBuff;
32     std::vector<std::string> rangesBuff;
33     std::vector<uint32_t> optionTypeBuff;
34     std::map<int32_t, int32_t> quantMap; // key is index, value is quant
35     std::map<int32_t, int32_t> indexMap; // key is outerIndex, value is index in sane-backends
36     int32_t buffLength = 0;
37 };
38 
39 class ScanContext {
40 public:
41     static ScanContext& GetInstance();
42     ScanContext(const ScanContext&) = delete;
43     ScanContext& operator=(const ScanContext&) = delete;
44     ScanContext(ScanContext&&) = delete;
45     ScanContext& operator=(ScanContext&&) = delete;
46 
47     void SetDiscoverCallback(Scan_ScannerDiscoveryCallback callback);
48     void ExecuteCallback(const std::vector<ScanDeviceInfo>& infos);
49     Scan_ScannerOptions* ConvertToScannerOptions(ScanParaTable& paraTable);
50     int32_t GetScannerParaCount(const std::string& deviceId, int32_t& scannerParaCount);
51     int32_t GetScannerParameter(const std::string& deviceId, int32_t scannerParaCount, ScanParaTable& paraTable);
52     void SetScannerOptions(const std::string& scannerId, Scan_ScannerOptions* options);
53     Scan_ScannerOptions* GetScannerOptions(const std::string& scannerId);
54     void SetScanParaTable(const std::string& scannerId, std::unique_ptr<ScanParaTable> table);
55     const std::string& GetRegisterType();
56     void Clear();
57     bool ParaIndexConvert(const int32_t option, int32_t& innerOption, const std::string& deviceId);
58     int32_t GetOptionValueFromTable(const std::string& deviceId, int32_t option,
59         const char* value, ScanOptionValue& optionValue);
60     static int32_t StatusConvert(int32_t status);
61 
62 private:
63     ScanContext();
64     ~ScanContext();
65 
66     bool SetParaTable(ScanOptionDescriptor& desc, ScanParaTable& paraTable, int32_t& buffLength);
67     Scan_ScannerOptions* CreateScannerOptions(int32_t& optionCount);
68     bool CopySingleBuf(char* destBuf, const char* srcBuf, size_t bufferSize);
69     bool MemSetScannerOptions(Scan_ScannerOptions* scannerOptions, int32_t& optionCount, ScanParaTable& paraTable);
70     void FreeScannerOptionsMemory(Scan_ScannerOptions* scannerOptions);
71     ScanParaTable* GetScanParaTable(const std::string& scannerId);
72     std::string GetPhysicalUnitDesc(uint32_t physicalUnit);
73     std::string SetRangeStrInParaTable(const ScanOptionDescriptor& desc,
74         ScanParaTable& paraTable, const int32_t& buffLength);
75 
76     mutable std::mutex mutex_;
77     std::map<std::string, Scan_ScannerOptions*> exScanParaTables_;
78     std::map<std::string, std::unique_ptr<ScanParaTable>> innerScanParaTables_;
79     Scan_ScannerDiscoveryCallback discoverCallback_ = nullptr;
80 };
81 } // namespace OHOS::Scan
82 #endif // SCAN_CONTEXT_H
83